From 973dc3b1d8f81389ed87664772595afd75567dd3 Mon Sep 17 00:00:00 2001 From: Bill Wendling Date: Thu, 15 Jul 2010 23:32:40 +0000 Subject: 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 . llvm-svn: 108473 --- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp') diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 458e865a6b3..1fa887c69e0 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -796,6 +796,7 @@ void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) { #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 @@ void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) { 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(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(); -- cgit v1.2.3