[llvm-commits] [llvm] r108473 - in /llvm/trunk: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h test/CodeGen/PowerPC/2008-01-25-EmptyFunction.ll test/CodeGen/X86/2008-01-25-EmptyFunction.ll

Bill Wendling isanbard at gmail.com
Thu Jul 15 16:32:40 PDT 2010


Author: void
Date: Thu Jul 15 18:32:40 2010
New Revision: 108473

URL: http://llvm.org/viewvc/llvm-project?rev=108473&view=rev
Log:
Handle code gen for the unreachable instruction if it's the only instruction in
the function. We'll just turn it into a "trap" instruction instead.

The problem with not handling this is that it might generate a prologue without
the equivalent epilogue to go with it:

$ cat t.ll
define void @foo() {
entry:
  unreachable
}
$ llc -o - t.ll -relocation-model=pic -disable-fp-elim -unwind-tables
        .section        __TEXT,__text,regular,pure_instructions
        .globl  _foo
        .align  4, 0x90
_foo:                                   ## @foo
Leh_func_begin0:
## BB#0:                                ## %entry
        pushq   %rbp
Ltmp0:
        movq    %rsp, %rbp
Ltmp1:
Leh_func_end0:
...

The unwind tables then have bad data in them causing all sorts of problems.

Fixes <rdar://problem/8096481>.

Modified:
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
    llvm/trunk/test/CodeGen/PowerPC/2008-01-25-EmptyFunction.ll
    llvm/trunk/test/CodeGen/X86/2008-01-25-EmptyFunction.ll

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp?rev=108473&r1=108472&r2=108473&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp Thu Jul 15 18:32:40 2010
@@ -796,6 +796,7 @@
 #define HANDLE_INST(NUM, OPCODE, CLASS) \
     case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
 #include "llvm/Instruction.def"
+#undef HANDLE_INST
   }
 
   // Assign the ordering to the freshly created DAG nodes.
@@ -2194,6 +2195,19 @@
                           getValue(I.getAddress())));
 }
 
+void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
+  // If the function consists of a single "unreachable" instruction, emit a
+  // "trap". This prevents the back-ends from generating empty functions or
+  // functions which have a prologue, but no epilogue.
+  const BasicBlock *BB = I.getParent();
+  const Function *F = BB->getParent();
+
+  if (F->size() == 1 && BB->size() == 1 &&
+      isa<UnreachableInst>(BB->getTerminator()))
+      DAG.setRoot(DAG.getNode(ISD::TRAP, getCurDebugLoc(),
+                              MVT::Other, getRoot()));
+}
+
 void SelectionDAGBuilder::visitFSub(const User &I) {
   // -0.0 - X --> fneg
   const Type *Ty = I.getType();

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h?rev=108473&r1=108472&r2=108473&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h Thu Jul 15 18:32:40 2010
@@ -385,7 +385,7 @@
   void visitBr(const BranchInst &I);
   void visitSwitch(const SwitchInst &I);
   void visitIndirectBr(const IndirectBrInst &I);
-  void visitUnreachable(const UnreachableInst &I) { /* noop */ }
+  void visitUnreachable(const UnreachableInst &I);
 
   // Helpers for visitSwitch
   bool handleSmallSwitchRange(CaseRec& CR,

Modified: llvm/trunk/test/CodeGen/PowerPC/2008-01-25-EmptyFunction.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/PowerPC/2008-01-25-EmptyFunction.ll?rev=108473&r1=108472&r2=108473&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/PowerPC/2008-01-25-EmptyFunction.ll (original)
+++ llvm/trunk/test/CodeGen/PowerPC/2008-01-25-EmptyFunction.ll Thu Jul 15 18:32:40 2010
@@ -1,4 +1,4 @@
-; RUN: llc < %s -march=ppc32 | grep nop
+; RUN: llc < %s -march=ppc32 | grep trap
 target triple = "powerpc-apple-darwin8"
 
 

Modified: llvm/trunk/test/CodeGen/X86/2008-01-25-EmptyFunction.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/2008-01-25-EmptyFunction.ll?rev=108473&r1=108472&r2=108473&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/2008-01-25-EmptyFunction.ll (original)
+++ llvm/trunk/test/CodeGen/X86/2008-01-25-EmptyFunction.ll Thu Jul 15 18:32:40 2010
@@ -1,8 +1,17 @@
-; RUN: llc < %s -march=x86 | grep nop
+; RUN: llc < %s -march=x86 | FileCheck -check-prefix=NO-FP %s
+; RUN: llc < %s -march=x86 -disable-fp-elim | FileCheck -check-prefix=FP %s
 target triple = "i686-apple-darwin8"
 
+define void @func1() noreturn nounwind  {
+entry:
+; NO-FP: ud2
+        unreachable
+}
 
-define void @bork() noreturn nounwind  {
+define void @func2() noreturn nounwind  {
 entry:
+; FP: pushl %ebp
+; FP: movl %esp, %ebp
+; FP: ud2
         unreachable
 }





More information about the llvm-commits mailing list