From 056922ee4181f19d441415d01ffd99604cd97da5 Mon Sep 17 00:00:00 2001 From: Kirill Naumov Date: Tue, 2 Jun 2020 19:22:41 +0000 Subject: [PATCH] [InlineCost] GetElementPtr with constant operands If the GEP instruction contanins only constants as its arguments, then it should be recognized as a constant. For now, there was also added a flag to turn off this simplification if it causes any regressions ("disable-gep-const-evaluation") which is off by default. Once I gather needed data of the effectiveness of this simplification, the flag will be deleted. Reviewers: apilipenko, davidxl, mtrofin Reviewed By: mtrofin Differential Revision: https://reviews.llvm.org/D81026 --- lib/Analysis/InlineCost.cpp | 14 ++++++++++++++ test/Transforms/Inline/gep_from_constant.ll | 15 +++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 test/Transforms/Inline/gep_from_constant.ll diff --git a/lib/Analysis/InlineCost.cpp b/lib/Analysis/InlineCost.cpp index c05d1ee7e9a..33d714406d7 100644 --- a/lib/Analysis/InlineCost.cpp +++ b/lib/Analysis/InlineCost.cpp @@ -110,6 +110,10 @@ static cl::opt InlineCallerSupersetNoBuiltin( cl::desc("Allow inlining when caller has a superset of callee's nobuiltin " "attributes.")); +static cl::opt DisableGEPConstOperand( + "disable-gep-const-evaluation", cl::Hidden, cl::init(false), + cl::desc("Disables evaluation of GetElementPtr with constant operands")); + namespace { class InlineCostCallAnalyzer; @@ -1019,6 +1023,16 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) { return true; }; + if (!DisableGEPConstOperand) + 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; diff --git a/test/Transforms/Inline/gep_from_constant.ll b/test/Transforms/Inline/gep_from_constant.ll new file mode 100644 index 00000000000..eafb4710690 --- /dev/null +++ b/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 +}