[llvm] r273776 - [SimplifyCFG] Replace calls to null/undef with unreachable

David Majnemer via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 25 00:37:27 PDT 2016


Author: majnemer
Date: Sat Jun 25 02:37:27 2016
New Revision: 273776

URL: http://llvm.org/viewvc/llvm-project?rev=273776&view=rev
Log:
[SimplifyCFG] Replace calls to null/undef with unreachable

Calling null is undefined behavior, a call to undef can be trivially
treated as a call to null.

Modified:
    llvm/trunk/lib/Transforms/Utils/Local.cpp
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/trunk/test/Transforms/SimplifyCFG/UnreachableEliminate.ll

Modified: llvm/trunk/lib/Transforms/Utils/Local.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/Local.cpp?rev=273776&r1=273775&r2=273776&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/Local.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/Local.cpp Sat Jun 25 02:37:27 2016
@@ -1414,6 +1414,12 @@ static bool markAliveBlocks(Function &F,
       }
 
       if (CallInst *CI = dyn_cast<CallInst>(BBI)) {
+        Value *Callee = CI->getCalledValue();
+        if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
+          changeToUnreachable(CI, /*UseLLVMTrap=*/false);
+          Changed = true;
+          break;
+        }
         if (CI->doesNotReturn()) {
           // If we found a call to a no-return function, insert an unreachable
           // instruction after it.  Make sure there isn't *already* one there

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=273776&r1=273775&r2=273776&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Sat Jun 25 02:37:27 2016
@@ -5388,7 +5388,7 @@ static bool passingValueIsAlwaysUndefine
   if (I->use_empty())
     return false;
 
-  if (C->isNullValue()) {
+  if (C->isNullValue() || isa<UndefValue>(C)) {
     // Only look at the first use, avoid hurting compile time with long uselists
     User *Use = *I->user_begin();
 
@@ -5417,6 +5417,10 @@ static bool passingValueIsAlwaysUndefine
       if (!SI->isVolatile())
         return SI->getPointerAddressSpace() == 0 &&
                SI->getPointerOperand() == I;
+
+    // A call to null is undefined.
+    if (auto CS = CallSite(Use))
+      return CS.getCalledValue() == I;
   }
   return false;
 }

Modified: llvm/trunk/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/UnreachableEliminate.ll?rev=273776&r1=273775&r2=273776&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/UnreachableEliminate.ll (original)
+++ llvm/trunk/test/Transforms/SimplifyCFG/UnreachableEliminate.ll Sat Jun 25 02:37:27 2016
@@ -96,3 +96,34 @@ bb2:
   store i8 2, i8* %ptr.2, align 8
   ret void
 }
+
+define i32 @test7(i1 %X) {
+entry:
+  br i1 %X, label %if, label %else
+
+if:
+  call void undef()
+  br label %else
+
+else:
+  %phi = phi i32 [ 0, %entry ], [ 1, %if ]
+  ret i32 %phi
+}
+; CHECK-LABEL: define i32 @test7(
+; CHECK-NOT: call
+; CHECK: ret i32 0
+
+define void @test8(i1 %X, void ()* %Y) {
+entry:
+  br i1 %X, label %if, label %else
+
+if:
+  br label %else
+
+else:
+  %phi = phi void ()* [ %Y, %entry ], [ null, %if ]
+  call void %phi()
+  ret void
+}
+; CHECK-LABEL: define void @test8(
+; CHECK: call void %Y(




More information about the llvm-commits mailing list