[RFC] Change TargetTransformInfo::getGEPCost to have GetElementPtrInst as parameter


I’d like to discuss a possibility of a change of TargetTransformInfo::getGEPCost to have GetElementPtrInst as a parameter.

Its current signature is:

/// \brief Estimate the cost of a GEP operation when lowered.


/// The contract for this function is the same as \c getOperationCost except

/// that it supports an interface that provides extra information specific to

/// the GEP operation.

int getGEPCost(Type *PointeeType, const Value *Ptr,

ArrayRef<const Value *> Operands) const;

I’d like to change it to:

int getGEPCost(const GetElementPtrInst *GEP, ArrayRef<const Value *> Operands) const;

All uses of the current getGEPCost look like: TTI.getGEPCost(GEP.getSourceElementType(), GEP.getPointerOperand(), …):


TTI.getGEPCost(GEP.getSourceElementType(), GEP.getPointerOperand(),


return TTI->getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(),


return TTI->getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(),

If PointeeType and Ptr are always from GEP why do we need the possibility to specify them independently without their owner?


In the following IR produced from the code of a simple memcopy function GEPs are not free:

while.cond: ; preds = %while.body, %entry

%dest.addr.0 = phi i8* [ %dest, %entry ], [ %incdec.ptr1, %while.body ]

%src.addr.0 = phi i8* [ %src, %entry ], [ %incdec.ptr, %while.body ]

%tobool = icmp eq i32 %size.addr.0, 0

br i1 %tobool, label %while.end, label %while.body

while.body: ; preds = %while.cond

%dec = add nsw i32 %size.addr.0, -1

%incdec.ptr = getelementptr inbounds i8, i8* %src.addr.0, i32 1

%0 = load i8, i8* %src.addr.0, align 1, !tbaa !12

%incdec.ptr1 = getelementptr inbounds i8, i8* %dest.addr.0, i32 1

store i8 %0, i8* %dest.addr.0, align 1, !tbaa !12

br label %while.cond

while.end: ; preds = %while.cond

For x86 and ARM they are lowered into ADD instructions. So they are not free but the current getGEPCost returns they are free. E.g., this affects the cost of inlining. The calculated cost is lower than it should be and functions are inlined.

We can do the analysis before the call of getGEPCost but this will require to do it at all places where getGEPCost is called. So it’s better to do this in one place, in the getGEPCost function or its implementations for targets.

To detect this case and other Def-Use based cases GEPs need to be accessed in getGEPCost which is not possible with the current signature.

Any thoughts?


Evgeny Astigeevich

Senior Compiler Engineer

Compilation Tools