Index: llvm/lib/Analysis/InlineCost.cpp =================================================================== --- llvm/lib/Analysis/InlineCost.cpp +++ llvm/lib/Analysis/InlineCost.cpp @@ -1003,6 +1003,15 @@ return true; }; + if (simplifyInstruction(I, [&](SmallVectorImpl &COps) { + SmallVector Indices; + for (unsigned int Index = 1 ; Index < COps.size() ; ++Index) + Indices.push_back(COps[Index]); + return ConstantExpr::getGetElementPtr(I.getSourceElementType(), COps[0], + Indices, I.isInBounds()); + })) + return true; + if ((I.isInBounds() && canFoldInboundsGEP(I)) || IsGEPOffsetConstant(I)) { if (SROAArg) SROAArgValues[&I] = SROAArg; Index: llvm/test/Transforms/Inline/gep_from_constant.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/Inline/gep_from_constant.ll @@ -0,0 +1,15 @@ +; RUN: opt < %s -passes="print" 2>&1 | FileCheck %s + +; CHECK-LABEL: @foo +; CHECK: cost before = {{.*}}, cost after = {{.*}}, threshold before = {{.*}}, threshold after = {{.*}}, cost delta = {{.*}}, simplified to i8 addrspace(1)** getelementptr (i8 addrspace(1)*, i8 addrspace(1)** inttoptr (i64 754974720 to i8 addrspace(1)**), i64 5) + +define i8 addrspace(1)** @foo(i64 %0) { + %2 = inttoptr i64 754974720 to i8 addrspace(1)** + %3 = getelementptr i8 addrspace(1)*, i8 addrspace(1)** %2, i64 %0 + ret i8 addrspace(1)** %3 +} + +define i8 addrspace(1)** @main() { + %1 = call i8 addrspace(1)** @foo(i64 5) + ret i8 addrspace(1)** %1 +}