[PATCH] [SCCP] Calculate Null Pointer Comparisons

Nick White n.j.white at gmail.com
Wed May 13 14:31:14 PDT 2015


Change to SimplifyCFG, not SCCP


http://reviews.llvm.org/D9634

Files:
  lib/Transforms/Utils/SimplifyCFG.cpp
  test/Transforms/SimplifyCFG/load-means-not-null.ll
  test/Transforms/SimplifyCFG/phi-undef-loadstore.ll

Index: lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyCFG.cpp
+++ lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4610,13 +4610,23 @@
         IRBuilder<> Builder(T);
         if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
           BB->removePredecessor(PHI->getIncomingBlock(i));
-          // Turn uncoditional branches into unreachables and remove the dead
+          // Turn unconditional branches into unreachables and remove the dead
           // destination from conditional branches.
           if (BI->isUnconditional())
             Builder.CreateUnreachable();
-          else
-            Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1) :
-                                                         BI->getSuccessor(0));
+          else {
+            bool PickFirst = BI->getSuccessor(1) == BB;
+            Builder.CreateBr(PickFirst
+              ? BI->getSuccessor(0)
+              : BI->getSuccessor(1));
+            if (auto Cond = dyn_cast<Instruction>(BI->getCondition())) {
+              // Inline the result of the conditional check downstream as a
+              // constant, and then get rid of it if possible.
+              Value *Const = ConstantInt::get(Cond->getType(), PickFirst);
+              Cond->replaceAllUsesWith(Const);
+              RecursivelyDeleteTriviallyDeadInstructions(Cond);
+            }
+          }
           BI->eraseFromParent();
           return true;
         }
Index: test/Transforms/SimplifyCFG/load-means-not-null.ll
===================================================================
--- /dev/null
+++ test/Transforms/SimplifyCFG/load-means-not-null.ll
@@ -0,0 +1,23 @@
+; RUN: opt < %s -simplifycfg -S | FileCheck %s
+
+declare void @g(i8)
+
+; CHECK-LABEL: @test_postdominate
+; CHECK-NEXT: a:
+; CHECK-NEXT: call void @g(i8 0)
+; CHECK: ret i1 true
+define i1 @test_postdominate(i8* %mem) {
+  %test = icmp eq i8* %mem, null
+  br i1 %test, label %a, label %b
+a:
+  call void @g(i8 0)
+  br label %c
+b:
+  br label %c
+c:
+  %join = phi i8* [ %mem, %a ], [ null, %b ]
+  %gep = getelementptr i8, i8* %join, i64 4
+  %res = load i8, i8* %gep
+  call void @g(i8 %res)
+  ret i1 %test
+}
Index: test/Transforms/SimplifyCFG/phi-undef-loadstore.ll
===================================================================
--- test/Transforms/SimplifyCFG/phi-undef-loadstore.ll
+++ test/Transforms/SimplifyCFG/phi-undef-loadstore.ll
@@ -25,10 +25,9 @@
   ret i32 %tmp9
 
 ; CHECK-LABEL: @test1(
-; CHECK: if.else:
-; CHECK: br label %if.end7
-
-; CHECK: phi i32* [ %a, %if.then ], [ %c, %if.else ]
+; CHECK: tail call void @bar()
+; CHECK: %c.a = select i1 %tobool, i32* %c, i32* %a
+; CHECK: %tmp9 = load i32, i32* %c.a
 }
 
 define i32 @test2(i32* %a, i32 %b, i32* %c, i32 %d) nounwind {
@@ -53,9 +52,8 @@
   %tmp9 = load i32, i32* %x.0
   ret i32 %tmp9
 ; CHECK-LABEL: @test2(
-; CHECK: if.else:
-; CHECK: unreachable
-
+; CHECK: tail call void @bar()
+; CHECK: %tmp9 = load i32, i32* %a
 ; CHECK-NOT: phi
 }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9634.25734.patch
Type: text/x-patch
Size: 3077 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150513/99485739/attachment.bin>


More information about the llvm-commits mailing list