[PATCH] D19276: folding compares if pointers do not escape

Anna Thomas via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 20 11:18:39 PDT 2016


anna removed rL LLVM as the repository for this revision.
anna updated this revision to Diff 54394.
anna marked 3 inline comments as done.
anna added a comment.

revised diff with minor changes


http://reviews.llvm.org/D19276

Files:
  lib/Transforms/InstCombine/InstructionCombining.cpp
  test/Transforms/InstCombine/compared-unescaped.ll

Index: test/Transforms/InstCombine/compared-unescaped.ll
===================================================================
--- test/Transforms/InstCombine/compared-unescaped.ll
+++ test/Transforms/InstCombine/compared-unescaped.ll
@@ -0,0 +1,40 @@
+; RUN: opt -instcombine -S %s | FileCheck %s
+
+ at gp= global i32* null, align 8
+
+declare i8* @malloc(i64) #1
+
+define i1 @compare_global_trivialeq() {
+  %m = call i8* @malloc(i64 4)
+  %bc = bitcast i8* %m to i32*
+  %lgp = load i32*, i32** @gp, align 8
+  %cmp = icmp eq i32* %bc, %lgp
+  ret i1 %cmp
+; CHECK-LABEL: compare_global_trivialeq
+; CHECK: ret i1 false
+}
+
+define i1 @compare_global_trivialne(){
+  %m = call i8* @malloc(i64 4)
+  %bc = bitcast i8* %m to i32*
+  %lgp = load i32*, i32** @gp, align 8
+  %cmp = icmp ne i32* %bc, %lgp
+  ret i1 %cmp
+; CHECK-LABEL: compare_global_trivialne
+; CHECK: ret i1 true 
+}
+
+; Although the %m is marked nocapture in the deopt operand in call to function f,
+; we cannot remove the alloc site: call to malloc 
+declare void @f()
+define i32 @compare_and_call_with_deopt(){
+; CHECK-LABEL: compare_and_call_with_deopt
+  %m = call i8* @malloc(i64 24)
+  %bc = bitcast i8* %m to i32*
+  %lgp = load i32*, i32** @gp, align 8
+  %cmp = icmp eq i32* %bc, %lgp
+  %rt = zext i1 %cmp to i32
+  tail call void @f() [ "deopt"(i8* %m) ]
+  ret i32 %rt 
+; CHECK: ret i32 %rt
+}
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1862,6 +1862,18 @@
   return nullptr;
 }
 
+static bool isNeverEqualToUnescapedAlloc(Instruction *CmpOp) {
+  switch (CmpOp->getOpcode()) {
+  default:
+    return false;
+  case Instruction::Load: {
+    LoadInst *LI = cast<LoadInst>(CmpOp);
+    // Comparison to global pointer.
+    return isa<GlobalVariable>(LI->getPointerOperand());
+  }
+  }
+}
+
 static bool
 isAllocSiteRemovable(Instruction *AI, SmallVectorImpl<WeakVH> &Users,
                      const TargetLibraryInfo *TLI) {
@@ -1886,8 +1898,17 @@
       case Instruction::ICmp: {
         ICmpInst *ICI = cast<ICmpInst>(I);
         // We can fold eq/ne comparisons with null to false/true, respectively.
-        if (!ICI->isEquality() || !isa<ConstantPointerNull>(ICI->getOperand(1)))
+        if (!ICI->isEquality())
           return false;
+
+        if (!isa<ConstantPointerNull>(ICI->getOperand(1))) {
+          unsigned OtherIndex = (ICI->getOperand(0) == PI)? 1:0;
+          Instruction *OtherOp = dyn_cast<Instruction>(ICI->getOperand(OtherIndex));
+          // We fold comparisons in some conditions provided the alloc has not
+          // escaped.
+          if (!OtherOp || !isNeverEqualToUnescapedAlloc(OtherOp))
+            return false;
+        }
         Users.emplace_back(I);
         continue;
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19276.54394.patch
Type: text/x-patch
Size: 2919 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160420/8561a04f/attachment.bin>


More information about the llvm-commits mailing list