diff --git a/llvm/lib/Transforms/Scalar/SimplifyCFG.cpp b/llvm/lib/Transforms/Scalar/SimplifyCFG.cpp index 3d1a5bea97ce..9d0757a7146e 100644 --- a/llvm/lib/Transforms/Scalar/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Scalar/SimplifyCFG.cpp @@ -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 @@ static bool RemoveUnreachableBlocks(Function &F) { 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(&BB.front())) + new CallInst(Intrinsic::getDeclaration(F.getParent(), + Intrinsic::trap), + "", &BB.front()); + } + return Changed; + } assert(Reachable.size() < F.size()); NumSimpl += F.size()-Reachable.size(); diff --git a/llvm/test/CFrontend/2008-01-25-EmptyFunction.c b/llvm/test/CFrontend/2008-01-25-EmptyFunction.c new file mode 100644 index 000000000000..896738ee0637 --- /dev/null +++ b/llvm/test/CFrontend/2008-01-25-EmptyFunction.c @@ -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; +}