[PATCH] D49690: ConstantFolding: Avoid a crash.

Manoj Gupta via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 23 14:20:12 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL337742: ConstantFolding: Avoid a crash. (authored by manojgupta, committed by ).

Repository:
  rL LLVM

https://reviews.llvm.org/D49690

Files:
  llvm/trunk/lib/Analysis/ConstantFolding.cpp
  llvm/trunk/test/Transforms/Inline/inline_inv_group.ll


Index: llvm/trunk/test/Transforms/Inline/inline_inv_group.ll
===================================================================
--- llvm/trunk/test/Transforms/Inline/inline_inv_group.ll
+++ llvm/trunk/test/Transforms/Inline/inline_inv_group.ll
@@ -0,0 +1,19 @@
+; RUN: opt < %s -inline -S | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i8* @callee() alwaysinline {
+; CHECK-LABEL: define i8* @callee()
+    %1 = call i8* @llvm.strip.invariant.group.p0i8(i8* null)
+    ret i8* %1
+}
+
+define i8* @caller() {
+; CHECK-LABEL: define i8* @caller()
+; CHECK-NEXT: call i8* @llvm.strip.invariant.group.p0i8(i8* null)
+    %1 = call i8* @callee()
+    ret i8* %1
+}
+
+declare i8* @llvm.strip.invariant.group.p0i8(i8*)
Index: llvm/trunk/lib/Analysis/ConstantFolding.cpp
===================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp
@@ -1603,14 +1603,21 @@
         return Operands[0];
     }
 
-    if (isa<ConstantPointerNull>(Operands[0]) &&
-        !NullPointerIsDefined(
-            CS.getCaller(), Operands[0]->getType()->getPointerAddressSpace())) {
+    if (isa<ConstantPointerNull>(Operands[0])) {
       // launder(null) == null == strip(null) iff in addrspace 0
       if (IntrinsicID == Intrinsic::launder_invariant_group ||
-          IntrinsicID == Intrinsic::strip_invariant_group)
-        return Operands[0];
-      return nullptr;
+          IntrinsicID == Intrinsic::strip_invariant_group) {
+        // If instruction is not yet put in a basic block (e.g. when cloning
+        // a function during inlining), CS caller may not be available.
+        // So check CS's BB first before querying CS.getCaller.
+        const Function *Caller = CS.getParent() ? CS.getCaller() : nullptr;
+        if (Caller &&
+            !NullPointerIsDefined(
+                Caller, Operands[0]->getType()->getPointerAddressSpace())) {
+          return Operands[0];
+        }
+        return nullptr;
+      }
     }
 
     if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49690.156880.patch
Type: text/x-patch
Size: 2167 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180723/956dea67/attachment.bin>


More information about the llvm-commits mailing list