[llvm] r267035 - Folding compares with unescaped allocations

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 21 12:26:46 PDT 2016


Author: sanjoy
Date: Thu Apr 21 14:26:45 2016
New Revision: 267035

URL: http://llvm.org/viewvc/llvm-project?rev=267035&view=rev
Log:
Folding compares with unescaped allocations

Summary:
If we know that the pointer allocated within a function does not escape,
we can fold away comparisons that are done with global pointers

Patch by Anna Thomas!

Reviewers: reames, majnemer, sanjoy

Subscribers: mgrang, mcrosier, majnemer, llvm-commits

Differential Revision: http://reviews.llvm.org/D19276

Added:
    llvm/trunk/test/Transforms/InstCombine/compare-unescaped.ll
Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=267035&r1=267034&r2=267035&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Thu Apr 21 14:26:45 2016
@@ -1863,6 +1863,14 @@ Instruction *InstCombiner::visitGetEleme
   return nullptr;
 }
 
+static bool isNeverEqualToUnescapedAlloc(Value *V) {
+  if (isa<ConstantPointerNull>(V))
+    return true;
+  if (auto *LI = dyn_cast<LoadInst>(V))
+    return isa<GlobalVariable>(LI->getPointerOperand());
+  return false;
+}
+
 static bool
 isAllocSiteRemovable(Instruction *AI, SmallVectorImpl<WeakVH> &Users,
                      const TargetLibraryInfo *TLI) {
@@ -1887,7 +1895,12 @@ isAllocSiteRemovable(Instruction *AI, Sm
       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)))
+        // We fold comparisons in some conditions provided the alloc has not
+        // escaped.
+        if (!ICI->isEquality())
+          return false;
+        unsigned OtherIndex = (ICI->getOperand(0) == PI) ? 1 : 0;
+        if (!isNeverEqualToUnescapedAlloc(ICI->getOperand(OtherIndex)))
           return false;
         Users.emplace_back(I);
         continue;

Added: llvm/trunk/test/Transforms/InstCombine/compare-unescaped.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/compare-unescaped.ll?rev=267035&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/compare-unescaped.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/compare-unescaped.ll Thu Apr 21 14:26:45 2016
@@ -0,0 +1,42 @@
+; 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
+; FIXME: The comparison should fold to false irrespective of whether the call to malloc can be elided or not
+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
+}




More information about the llvm-commits mailing list