From 6e4134459bdb1e537731a16123f75c8d2ec66c5a Mon Sep 17 00:00:00 2001
From: Eugene Leviant <eleviant@accesssoftek.com>
Date: Sun, 1 Jul 2018 11:02:07 +0000
Subject: [PATCH] [Evaluator] Improve evaluation of call instruction

Recommit of r335324 after buildbot failure fix

llvm-svn: 336059
---
 .../include/llvm/Transforms/Utils/Evaluator.h | 13 +++
 llvm/lib/Transforms/Utils/Evaluator.cpp       | 69 +++++++++++--
 .../GlobalOpt/evaluate-call-errors.ll         | 99 +++++++++++++++++++
 .../Transforms/GlobalOpt/evaluate-call.ll     | 85 ++++++++++++++++
 .../GlobalOpt/evaluate-constfold-call.ll      | 55 +++++++++++
 5 files changed, 314 insertions(+), 7 deletions(-)
 create mode 100644 llvm/test/Transforms/GlobalOpt/evaluate-call-errors.ll
 create mode 100644 llvm/test/Transforms/GlobalOpt/evaluate-call.ll
 create mode 100644 llvm/test/Transforms/GlobalOpt/evaluate-constfold-call.ll

diff --git a/llvm/include/llvm/Transforms/Utils/Evaluator.h b/llvm/include/llvm/Transforms/Utils/Evaluator.h
index 0e987b93177a..9908ae6fd393 100644
--- a/llvm/include/llvm/Transforms/Utils/Evaluator.h
+++ b/llvm/include/llvm/Transforms/Utils/Evaluator.h
@@ -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;
   }
diff --git a/llvm/lib/Transforms/Utils/Evaluator.cpp b/llvm/lib/Transforms/Utils/Evaluator.cpp
index 9440ae3ef2af..264bc21d604a 100644
--- a/llvm/lib/Transforms/Utils/Evaluator.cpp
+++ b/llvm/lib/Transforms/Utils/Evaluator.cpp
@@ -217,6 +217,60 @@ Constant *Evaluator::ComputeLoadResult(Constant *P) {
   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::iterator CurInst,
       }
 
       // 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::iterator CurInst,
           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: "
diff --git a/llvm/test/Transforms/GlobalOpt/evaluate-call-errors.ll b/llvm/test/Transforms/GlobalOpt/evaluate-call-errors.ll
new file mode 100644
index 000000000000..88f7cbd8df1b
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/evaluate-call-errors.ll
@@ -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 }
+
+@_s = global %struct.S zeroinitializer, align 4
+@_q = global %struct.Q zeroinitializer, align 4
+@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
+}
diff --git a/llvm/test/Transforms/GlobalOpt/evaluate-call.ll b/llvm/test/Transforms/GlobalOpt/evaluate-call.ll
new file mode 100644
index 000000000000..27211007c6e0
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/evaluate-call.ll
@@ -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 }
+
+@gBar = global %struct.Bar zeroinitializer, align 4
+@_s = global %struct.S zeroinitializer, align 4
+@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
+}
diff --git a/llvm/test/Transforms/GlobalOpt/evaluate-constfold-call.ll b/llvm/test/Transforms/GlobalOpt/evaluate-constfold-call.ll
new file mode 100644
index 000000000000..aeaa5e800c70
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/evaluate-constfold-call.ll
@@ -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
+
+@_q = dso_local global %struct.Q zeroinitializer, align 4
+@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
+}