[llvm-commits] [llvm] r46387 - in /llvm/trunk: lib/Transforms/Scalar/SimplifyCFG.cpp test/CFrontend/2008-01-25-EmptyFunction.c

Bill Wendling isanbard at gmail.com
Fri Jan 25 17:43:44 PST 2008


Author: void
Date: Fri Jan 25 19:43:44 2008
New Revision: 46387

URL: http://llvm.org/viewvc/llvm-project?rev=46387&view=rev
Log:
If we have a function like this:

void bork() {
  int *address = 0;
  *address = 0;
}

It's compiled into LLVM code that looks like this:

define void @bork() noreturn nounwind  {
entry:
        unreachable
}

This is bad on some platforms (like PPC) because it will generate the label for
the function but no body. The label could end up being associated with some
non-code related stuff, like a section. This places a "trap" instruction if the
SimplifyCFG pass removed all code from the function leaving only one
"unreachable" instruction.

Added:
    llvm/trunk/test/CFrontend/2008-01-25-EmptyFunction.c
Modified:
    llvm/trunk/lib/Transforms/Scalar/SimplifyCFG.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SimplifyCFG.cpp?rev=46387&r1=46386&r2=46387&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SimplifyCFG.cpp Fri Jan 25 19:43:44 2008
@@ -26,6 +26,7 @@
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Constants.h"
 #include "llvm/Instructions.h"
+#include "llvm/Intrinsics.h"
 #include "llvm/Module.h"
 #include "llvm/ParameterAttributes.h"
 #include "llvm/Support/CFG.h"
@@ -154,8 +155,20 @@
   bool Changed = MarkAliveBlocks(F.begin(), Reachable);
   
   // If there are unreachable blocks in the CFG...
-  if (Reachable.size() == F.size())
+  if (Reachable.size() == F.size()) {
+    if (F.size() == 1) {
+      // If the function has only one block with an "unreachable" instruction,
+      // then we should create *some* code for it. Issue a "trap" instead.
+      BasicBlock &BB = F.front();
+
+      if (BB.size() == 1 && dyn_cast<UnreachableInst>(&BB.front()))
+        new CallInst(Intrinsic::getDeclaration(F.getParent(),
+                                               Intrinsic::trap),
+                     "", &BB.front());
+    }
+
     return Changed;
+  }
   
   assert(Reachable.size() < F.size());
   NumSimpl += F.size()-Reachable.size();

Added: llvm/trunk/test/CFrontend/2008-01-25-EmptyFunction.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CFrontend/2008-01-25-EmptyFunction.c?rev=46387&view=auto

==============================================================================
--- llvm/trunk/test/CFrontend/2008-01-25-EmptyFunction.c (added)
+++ llvm/trunk/test/CFrontend/2008-01-25-EmptyFunction.c Fri Jan 25 19:43:44 2008
@@ -0,0 +1,7 @@
+// RUN: %llvmgcc -O2 -S -o - -emit-llvm %s | grep llvm.trap
+// RUN: %llvmgcc -O2 -S -o - -emit-llvm %s | grep unreachable
+
+void bork() {
+  int *address = 0;
+  *address = 0;
+}





More information about the llvm-commits mailing list