[llvm] r336059 - [Evaluator] Improve evaluation of call instruction
Eugene Leviant via llvm-commits
llvm-commits at lists.llvm.org
Sun Jul 1 04:02:07 PDT 2018
Author: evgeny777
Date: Sun Jul 1 04:02:07 2018
New Revision: 336059
URL: http://llvm.org/viewvc/llvm-project?rev=336059&view=rev
Log:
[Evaluator] Improve evaluation of call instruction
Recommit of r335324 after buildbot failure fix
Added:
llvm/trunk/test/Transforms/GlobalOpt/evaluate-call-errors.ll
llvm/trunk/test/Transforms/GlobalOpt/evaluate-call.ll
llvm/trunk/test/Transforms/GlobalOpt/evaluate-constfold-call.ll
Modified:
llvm/trunk/include/llvm/Transforms/Utils/Evaluator.h
llvm/trunk/lib/Transforms/Utils/Evaluator.cpp
Modified: llvm/trunk/include/llvm/Transforms/Utils/Evaluator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/Evaluator.h?rev=336059&r1=336058&r2=336059&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/Evaluator.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/Evaluator.h Sun Jul 1 04:02:07 2018
@@ -18,6 +18,7 @@
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/CallSite.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
@@ -73,6 +74,18 @@ public:
ValueStack.back()[V] = C;
}
+ /// Given call site return callee and list of its formal arguments
+ Function *getCalleeWithFormalArgs(CallSite &CS,
+ SmallVector<Constant *, 8> &Formals);
+
+ /// Given call site and callee returns list of callee formal argument
+ /// values converting them when necessary
+ bool getFormalParams(CallSite &CS, Function *F,
+ SmallVector<Constant *, 8> &Formals);
+
+ /// Casts call result to a type of bitcast call expression
+ Constant *castCallResultIfNeeded(Value *CallExpr, Constant *RV);
+
const DenseMap<Constant*, Constant*> &getMutatedMemory() const {
return MutatedMemory;
}
Modified: llvm/trunk/lib/Transforms/Utils/Evaluator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/Evaluator.cpp?rev=336059&r1=336058&r2=336059&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/Evaluator.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/Evaluator.cpp Sun Jul 1 04:02:07 2018
@@ -217,6 +217,60 @@ Constant *Evaluator::ComputeLoadResult(C
return nullptr; // don't know how to evaluate.
}
+Function *
+Evaluator::getCalleeWithFormalArgs(CallSite &CS,
+ SmallVector<Constant *, 8> &Formals) {
+ auto *V = CS.getCalledValue();
+ if (auto *Fn = dyn_cast<Function>(getVal(V)))
+ return getFormalParams(CS, Fn, Formals) ? Fn : nullptr;
+
+ auto *CE = dyn_cast<ConstantExpr>(V);
+ if (!CE || CE->getOpcode() != Instruction::BitCast ||
+ !getFormalParams(CS, cast<Function>(CE->getOperand(0)), Formals))
+ return nullptr;
+
+ return dyn_cast<Function>(
+ ConstantFoldLoadThroughBitcast(CE, CE->getOperand(0)->getType(), DL));
+}
+
+bool Evaluator::getFormalParams(CallSite &CS, Function *F,
+ SmallVector<Constant *, 8> &Formals) {
+ auto *FTy = F->getFunctionType();
+ if (FTy->getNumParams() > CS.getNumArgOperands()) {
+ LLVM_DEBUG(dbgs() << "Too few arguments for function.\n");
+ return false;
+ }
+
+ auto ArgI = CS.arg_begin();
+ for (auto ParI = FTy->param_begin(), ParE = FTy->param_end(); ParI != ParE;
+ ++ParI) {
+ auto *ArgC = ConstantFoldLoadThroughBitcast(getVal(*ArgI), *ParI, DL);
+ if (!ArgC) {
+ LLVM_DEBUG(dbgs() << "Can not convert function argument.\n");
+ return false;
+ }
+ Formals.push_back(ArgC);
+ ++ArgI;
+ }
+ return true;
+}
+
+/// If call expression contains bitcast then we may need to cast
+/// evaluated return value to a type of the call expression.
+Constant *Evaluator::castCallResultIfNeeded(Value *CallExpr, Constant *RV) {
+ ConstantExpr *CE = dyn_cast<ConstantExpr>(CallExpr);
+ if (!RV || !CE || CE->getOpcode() != Instruction::BitCast)
+ return RV;
+
+ if (auto *FT =
+ dyn_cast<FunctionType>(CE->getType()->getPointerElementType())) {
+ RV = ConstantFoldLoadThroughBitcast(RV, FT->getReturnType(), DL);
+ if (!RV)
+ LLVM_DEBUG(dbgs() << "Failed to fold bitcast call expr\n");
+ }
+ return RV;
+}
+
/// Evaluate all instructions in block BB, returning true if successful, false
/// if we can't evaluate it. NewBB returns the next BB that control flows into,
/// or null upon return.
@@ -465,20 +519,19 @@ bool Evaluator::EvaluateBlock(BasicBlock
}
// Resolve function pointers.
- Function *Callee = dyn_cast<Function>(getVal(CS.getCalledValue()));
+ SmallVector<Constant *, 8> Formals;
+ Function *Callee = getCalleeWithFormalArgs(CS, Formals);
if (!Callee || Callee->isInterposable()) {
LLVM_DEBUG(dbgs() << "Can not resolve function pointer.\n");
return false; // Cannot resolve.
}
- SmallVector<Constant*, 8> Formals;
- for (User::op_iterator i = CS.arg_begin(), e = CS.arg_end(); i != e; ++i)
- Formals.push_back(getVal(*i));
-
if (Callee->isDeclaration()) {
// If this is a function we can constant fold, do it.
if (Constant *C = ConstantFoldCall(CS, Callee, Formals, TLI)) {
- InstResult = C;
+ InstResult = castCallResultIfNeeded(CS.getCalledValue(), C);
+ if (!InstResult)
+ return false;
LLVM_DEBUG(dbgs() << "Constant folded function call. Result: "
<< *InstResult << "\n");
} else {
@@ -499,7 +552,9 @@ bool Evaluator::EvaluateBlock(BasicBlock
return false;
}
ValueStack.pop_back();
- InstResult = RetVal;
+ InstResult = castCallResultIfNeeded(CS.getCalledValue(), RetVal);
+ if (RetVal && !InstResult)
+ return false;
if (InstResult) {
LLVM_DEBUG(dbgs() << "Successfully evaluated function. Result: "
Added: llvm/trunk/test/Transforms/GlobalOpt/evaluate-call-errors.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/evaluate-call-errors.ll?rev=336059&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GlobalOpt/evaluate-call-errors.ll (added)
+++ llvm/trunk/test/Transforms/GlobalOpt/evaluate-call-errors.ll Sun Jul 1 04:02:07 2018
@@ -0,0 +1,99 @@
+; Checks for few bitcasted call evaluation errors
+
+; REQUIRES: asserts
+; RUN: opt -globalopt -instcombine -S -debug-only=evaluator %s -o %t 2>&1 | FileCheck %s
+
+; CHECK: Failed to fold bitcast call expr
+; CHECK: Can not convert function argument
+
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.12.0"
+
+%struct.S = type { i32 }
+%struct.Q = type { i32 }
+%struct.Foo = type { i32 }
+
+ at _s = global %struct.S zeroinitializer, align 4
+ at _q = global %struct.Q zeroinitializer, align 4
+ at llvm.global_ctors = appending global [2 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_main2.cpp, i8* null }, { i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_main3.cpp, i8* null }]
+
+define internal void @__cxx_global_var_init() section "__TEXT,__StaticInit,regular,pure_instructions" {
+ call void @_ZN1SC1Ev(%struct.S* @_s)
+ ret void
+}
+
+define linkonce_odr void @_ZN1SC1Ev(%struct.S*) unnamed_addr align 2 {
+ %2 = alloca %struct.S*, align 8
+ store %struct.S* %0, %struct.S** %2, align 8
+ %3 = load %struct.S*, %struct.S** %2, align 8
+ call void @_ZN1SC2Ev(%struct.S* %3)
+ ret void
+}
+
+define internal void @__cxx_global_var_init.1() #0 section "__TEXT,__StaticInit,regular,pure_instructions" {
+ call void @_ZN1QC1Ev(%struct.Q* @_q)
+ ret void
+}
+
+define linkonce_odr void @_ZN1QC1Ev(%struct.Q*) unnamed_addr align 2 {
+ %2 = alloca %struct.Q*, align 8
+ store %struct.Q* %0, %struct.Q** %2, align 8
+ %3 = load %struct.Q*, %struct.Q** %2, align 8
+ call void @_ZN1QC2Ev(%struct.Q* %3)
+ ret void
+}
+
+define i32 @main() {
+ %1 = alloca i32, align 4
+ store i32 0, i32* %1, align 4
+ ret i32 0
+}
+
+define linkonce_odr void @_ZN1SC2Ev(%struct.S*) unnamed_addr align 2 {
+ %2 = alloca %struct.S*, align 8
+ %3 = alloca %struct.Foo, align 4
+ store %struct.S* %0, %struct.S** %2, align 8
+ %4 = load %struct.S*, %struct.S** %2, align 8
+ %5 = getelementptr inbounds %struct.S, %struct.S* %4, i32 0, i32 0
+ %6 = call i32 bitcast (%struct.Foo* ()* @_ZL3foov to i32 ()*)()
+ %7 = getelementptr inbounds %struct.Foo, %struct.Foo* %3, i32 0, i32 0
+ store i32 %6, i32* %7, align 4
+ %8 = getelementptr inbounds %struct.Foo, %struct.Foo* %3, i32 0, i32 0
+ %9 = load i32, i32* %8, align 4
+ store i32 %9, i32* %5, align 4
+ ret void
+}
+
+define internal %struct.Foo* @_ZL3foov() {
+ ret %struct.Foo* null
+}
+
+define linkonce_odr void @_ZN1QC2Ev(%struct.Q*) unnamed_addr align 2 {
+ %2 = alloca %struct.Q*, align 8
+ store %struct.Q* %0, %struct.Q** %2, align 8
+ %3 = load %struct.Q*, %struct.Q** %2, align 8
+ %4 = getelementptr inbounds %struct.Q, %struct.Q* %3, i32 0, i32 0
+ %5 = call i32 bitcast (i32 (i32)* @_ZL3baz3Foo to i32 (%struct.Foo*)*)(%struct.Foo* null)
+ store i32 %5, i32* %4, align 4
+ ret void
+}
+
+define internal i32 @_ZL3baz3Foo(i32) {
+ %2 = alloca %struct.Foo, align 4
+ %3 = getelementptr inbounds %struct.Foo, %struct.Foo* %2, i32 0, i32 0
+ store i32 %0, i32* %3, align 4
+ %4 = getelementptr inbounds %struct.Foo, %struct.Foo* %2, i32 0, i32 0
+ %5 = load i32, i32* %4, align 4
+ ret i32 %5
+}
+
+; Function Attrs: noinline ssp uwtable
+define internal void @_GLOBAL__sub_I_main2.cpp() section "__TEXT,__StaticInit,regular,pure_instructions" {
+ call void @__cxx_global_var_init()
+ ret void
+}
+
+define internal void @_GLOBAL__sub_I_main3.cpp() section "__TEXT,__StaticInit,regular,pure_instructions" {
+ call void @__cxx_global_var_init.1()
+ ret void
+}
Added: llvm/trunk/test/Transforms/GlobalOpt/evaluate-call.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/evaluate-call.ll?rev=336059&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GlobalOpt/evaluate-call.ll (added)
+++ llvm/trunk/test/Transforms/GlobalOpt/evaluate-call.ll Sun Jul 1 04:02:07 2018
@@ -0,0 +1,85 @@
+; Checks if bitcasted call expression can be evaluated
+; Given call expresion:
+; %struct.Bar* bitcast (%struct.Foo* (%struct.Foo*)* @_ZL3fooP3Foo to %struct.Bar* (%struct.Bar*)*)(%struct.Bar* @gBar)
+; We evaluate call to function @_ZL3fooP3Foo casting both parameter and return value
+; Given call expression:
+; void bitcast (void (%struct.Foo*)* @_ZL3bazP3Foo to void (%struct.Bar*)*)(%struct.Bar* @gBar)
+; We evaluate call to function _ZL3bazP3Foo casting its parameter and check that evaluated value (nullptr)
+; is handled correctly
+
+; RUN: opt -globalopt -instcombine -S %s -o - | FileCheck %s
+
+; CHECK: @gBar = local_unnamed_addr global %struct.Bar { i32 2 }
+; CHECK-NEXT: @_s = local_unnamed_addr global %struct.S { i32 1 }, align 4
+; CHECK-NEXT: @llvm.global_ctors = appending global [0 x { i32, void ()*, i8* }] zeroinitializer
+
+; CHECK: define i32 @main()
+; CHECK-NEXT: ret i32 0
+
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.12.0"
+
+%struct.Bar = type { i32 }
+%struct.S = type { i32 }
+%struct.Foo = type { i32 }
+
+ at gBar = global %struct.Bar zeroinitializer, align 4
+ at _s = global %struct.S zeroinitializer, align 4
+ at llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_main.cpp, i8* null }]
+
+define internal void @__cxx_global_var_init() section "__TEXT,__StaticInit,regular,pure_instructions" {
+ call void @_ZN1SC1Ev(%struct.S* @_s)
+ ret void
+}
+
+define linkonce_odr void @_ZN1SC1Ev(%struct.S*) unnamed_addr align 2 {
+ %2 = alloca %struct.S*, align 8
+ store %struct.S* %0, %struct.S** %2, align 8
+ %3 = load %struct.S*, %struct.S** %2, align 8
+ call void @_ZN1SC2Ev(%struct.S* %3)
+ ret void
+}
+
+define i32 @main() {
+ %1 = alloca i32, align 4
+ store i32 0, i32* %1, align 4
+ ret i32 0
+}
+
+define linkonce_odr void @_ZN1SC2Ev(%struct.S*) unnamed_addr align 2 {
+ %2 = alloca %struct.S*, align 8
+ store %struct.S* %0, %struct.S** %2, align 8
+ %3 = load %struct.S*, %struct.S** %2, align 8
+ %4 = getelementptr inbounds %struct.S, %struct.S* %3, i32 0, i32 0
+ %5 = call %struct.Bar* bitcast (%struct.Foo* (%struct.Foo*)* @_ZL3fooP3Foo to %struct.Bar* (%struct.Bar*)*)(%struct.Bar* @gBar)
+ %6 = getelementptr inbounds %struct.Bar, %struct.Bar* %5, i32 0, i32 0
+ %7 = load i32, i32* %6, align 4
+ store i32 %7, i32* %4, align 4
+ call void bitcast (void (%struct.Foo*)* @_ZL3bazP3Foo to void (%struct.Bar*)*)(%struct.Bar* @gBar)
+ ret void
+}
+
+define internal %struct.Foo* @_ZL3fooP3Foo(%struct.Foo*) {
+ %2 = alloca %struct.Foo*, align 8
+ store %struct.Foo* %0, %struct.Foo** %2, align 8
+ %3 = load %struct.Foo*, %struct.Foo** %2, align 8
+ %4 = getelementptr inbounds %struct.Foo, %struct.Foo* %3, i32 0, i32 0
+ store i32 1, i32* %4, align 4
+ %5 = load %struct.Foo*, %struct.Foo** %2, align 8
+ ret %struct.Foo* %5
+}
+
+define internal void @_ZL3bazP3Foo(%struct.Foo*) {
+ %2 = alloca %struct.Foo*, align 8
+ store %struct.Foo* %0, %struct.Foo** %2, align 8
+ %3 = load %struct.Foo*, %struct.Foo** %2, align 8
+ %4 = getelementptr inbounds %struct.Foo, %struct.Foo* %3, i32 0, i32 0
+ store i32 2, i32* %4, align 4
+ ret void
+}
+
+; Function Attrs: noinline ssp uwtable
+define internal void @_GLOBAL__sub_I_main.cpp() section "__TEXT,__StaticInit,regular,pure_instructions" {
+ call void @__cxx_global_var_init()
+ ret void
+}
Added: llvm/trunk/test/Transforms/GlobalOpt/evaluate-constfold-call.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/evaluate-constfold-call.ll?rev=336059&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GlobalOpt/evaluate-constfold-call.ll (added)
+++ llvm/trunk/test/Transforms/GlobalOpt/evaluate-constfold-call.ll Sun Jul 1 04:02:07 2018
@@ -0,0 +1,55 @@
+; Check if we can evaluate a bitcasted call to a function which is constant folded.
+; Evaluator folds call to fmodf, replacing it with constant value in case both operands
+; are known at compile time.
+; RUN: opt -globalopt -instcombine %s -S -o - | FileCheck %s
+
+; CHECK: @_q = dso_local local_unnamed_addr global %struct.Q { i32 1066527622 }
+; CHECK: define dso_local i32 @main
+; CHECK-NEXT: %[[V:.+]] = load i32, i32* getelementptr inbounds (%struct.Q, %struct.Q* @_q, i64 0, i32 0)
+; CHECK-NEXT: ret i32 %[[V]]
+
+source_filename = "main.cpp"
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-none-linux-gnu"
+
+%struct.Q = type { i32 }
+
+$_ZN1QC2Ev = comdat any
+
+ at _q = dso_local global %struct.Q zeroinitializer, align 4
+ at llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_main.cpp, i8* null }]
+
+define internal void @__cxx_global_var_init() section ".text.startup" {
+ call void @_ZN1QC2Ev(%struct.Q* @_q)
+ ret void
+}
+
+define linkonce_odr dso_local void @_ZN1QC2Ev(%struct.Q*) unnamed_addr #1 comdat align 2 {
+ %2 = alloca %struct.Q*, align 8
+ store %struct.Q* %0, %struct.Q** %2, align 8
+ %3 = load %struct.Q*, %struct.Q** %2, align 8
+ %4 = getelementptr inbounds %struct.Q, %struct.Q* %3, i32 0, i32 0
+ %5 = call i32 bitcast (float (float, float)* @fmodf to i32 (float, float)*)(float 0x40091EB860000000, float 2.000000e+00)
+ store i32 %5, i32* %4, align 4
+ ret void
+}
+
+define dso_local i32 @main(i32, i8**) {
+ %3 = alloca i32, align 4
+ %4 = alloca i32, align 4
+ %5 = alloca i8**, align 8
+ store i32 0, i32* %3, align 4
+ store i32 %0, i32* %4, align 4
+ store i8** %1, i8*** %5, align 8
+ %6 = load i32, i32* getelementptr inbounds (%struct.Q, %struct.Q* @_q, i32 0, i32 0), align 4
+ ret i32 %6
+}
+
+; Function Attrs: nounwind
+declare dso_local float @fmodf(float, float)
+
+; Function Attrs: noinline uwtable
+define internal void @_GLOBAL__sub_I_main.cpp() section ".text.startup" {
+ call void @__cxx_global_var_init()
+ ret void
+}
More information about the llvm-commits
mailing list