This is a patch for RFC: Change TargetTransformInfo::getGEPCost to take GetElementPtrInst as a parameter (http://lists.llvm.org/pipermail/llvm-dev/2017-March/111066.html)
The current signature of TargetTransformInfo::getGEPCost 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(), …):
lib/Analysis/InlineCost.cpp
TTI.getGEPCost(GEP.getSourceElementType(), GEP.getPointerOperand(),
lib/Transforms/Scalar/NaryReassociate.cpp
return TTI->getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(),
lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
return TTI->getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(),
Rationale:
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.
Useless assertion/