LLVM API Documentation
00001 //===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This transformation is designed for use by code generators which do not yet 00011 // support stack unwinding. This pass converts 'invoke' instructions to 'call' 00012 // instructions, so that any exception-handling 'landingpad' blocks become dead 00013 // code (which can be removed by running the '-simplifycfg' pass afterwards). 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #include "llvm/Transforms/Scalar.h" 00018 #include "llvm/ADT/SmallVector.h" 00019 #include "llvm/ADT/Statistic.h" 00020 #include "llvm/IR/Instructions.h" 00021 #include "llvm/IR/LLVMContext.h" 00022 #include "llvm/IR/Module.h" 00023 #include "llvm/Pass.h" 00024 #include "llvm/Support/CommandLine.h" 00025 using namespace llvm; 00026 00027 #define DEBUG_TYPE "lowerinvoke" 00028 00029 STATISTIC(NumInvokes, "Number of invokes replaced"); 00030 00031 namespace { 00032 class LowerInvoke : public FunctionPass { 00033 public: 00034 static char ID; // Pass identification, replacement for typeid 00035 explicit LowerInvoke() : FunctionPass(ID) { 00036 initializeLowerInvokePass(*PassRegistry::getPassRegistry()); 00037 } 00038 bool runOnFunction(Function &F) override; 00039 }; 00040 } 00041 00042 char LowerInvoke::ID = 0; 00043 INITIALIZE_PASS(LowerInvoke, "lowerinvoke", 00044 "Lower invoke and unwind, for unwindless code generators", 00045 false, false) 00046 00047 char &llvm::LowerInvokePassID = LowerInvoke::ID; 00048 00049 // Public Interface To the LowerInvoke pass. 00050 FunctionPass *llvm::createLowerInvokePass() { 00051 return new LowerInvoke(); 00052 } 00053 00054 bool LowerInvoke::runOnFunction(Function &F) { 00055 bool Changed = false; 00056 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 00057 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { 00058 SmallVector<Value*,16> CallArgs(II->op_begin(), II->op_end() - 3); 00059 // Insert a normal call instruction... 00060 CallInst *NewCall = CallInst::Create(II->getCalledValue(), 00061 CallArgs, "", II); 00062 NewCall->takeName(II); 00063 NewCall->setCallingConv(II->getCallingConv()); 00064 NewCall->setAttributes(II->getAttributes()); 00065 NewCall->setDebugLoc(II->getDebugLoc()); 00066 II->replaceAllUsesWith(NewCall); 00067 00068 // Insert an unconditional branch to the normal destination. 00069 BranchInst::Create(II->getNormalDest(), II); 00070 00071 // Remove any PHI node entries from the exception destination. 00072 II->getUnwindDest()->removePredecessor(BB); 00073 00074 // Remove the invoke instruction now. 00075 BB->getInstList().erase(II); 00076 00077 ++NumInvokes; Changed = true; 00078 } 00079 return Changed; 00080 }