Put all LLVM code into the llvm namespace, as per bug 109.
[oota-llvm.git] / lib / Transforms / Utils / LowerInvoke.cpp
1 //===- LowerInvoke.cpp - Eliminate Invoke & Unwind instructions -----------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This transformation is designed for use by code generators which do not yet
11 // support stack unwinding.  This pass gives them the ability to execute any
12 // program which does not throw an exception, by turning 'invoke' instructions
13 // into calls and by turning 'unwind' instructions into calls to abort().
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Pass.h"
19 #include "llvm/iTerminators.h"
20 #include "llvm/iOther.h"
21 #include "llvm/Module.h"
22 #include "llvm/Type.h"
23 #include "llvm/Constant.h"
24 #include "Support/Statistic.h"
25
26 namespace llvm {
27
28 namespace {
29   Statistic<> NumLowered("lowerinvoke", "Number of invoke & unwinds replaced");
30
31   class LowerInvoke : public FunctionPass {
32     Function *AbortFn;
33   public:
34     bool doInitialization(Module &M);
35     bool runOnFunction(Function &F);
36   };
37
38   RegisterOpt<LowerInvoke>
39   X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
40 }
41
42 // Public Interface To the LowerInvoke pass.
43 FunctionPass *createLowerInvokePass() { return new LowerInvoke(); }
44
45 // doInitialization - Make sure that there is a prototype for abort in the
46 // current module.
47 bool LowerInvoke::doInitialization(Module &M) {
48   AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, 0);
49   return true;
50 }
51
52 bool LowerInvoke::runOnFunction(Function &F) {
53   bool Changed = false;
54   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
55     if (InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) {
56       // Insert a normal call instruction...
57       std::string Name = II->getName(); II->setName("");
58       Value *NewCall = new CallInst(II->getCalledValue(),
59                                     std::vector<Value*>(II->op_begin()+3,
60                                                         II->op_end()), Name,II);
61       II->replaceAllUsesWith(NewCall);
62       
63       // Insert an unconditional branch to the normal destination
64       new BranchInst(II->getNormalDest(), II);
65
66       // Remove the invoke instruction now.
67       I->getInstList().erase(II);
68
69       ++NumLowered; Changed = true;
70     } else if (UnwindInst *UI = dyn_cast<UnwindInst>(I->getTerminator())) {
71       // Insert a call to abort()
72       new CallInst(AbortFn, std::vector<Value*>(), "", UI);
73
74       // Insert a return instruction.
75       new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 :
76                             Constant::getNullValue(F.getReturnType()), UI);
77
78       // Remove the unwind instruction now.
79       I->getInstList().erase(UI);
80
81       ++NumLowered; Changed = true;
82     }
83   return Changed;
84 }
85
86 } // End llvm namespace