Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 //===- RewritePNaClLibraryCalls.cpp - PNaCl library calls to intrinsics ---===// | |
| 2 // | |
| 3 // The LLVM Compiler Infrastructure | |
| 4 // | |
| 5 // This file is distributed under the University of Illinois Open Source | |
| 6 // License. See LICENSE.TXT for details. | |
| 7 // | |
| 8 //===----------------------------------------------------------------------===// | |
| 9 // | |
| 10 // This pass replaces calls to known library functions by calls to intrinsics | |
|
Mark Seaborn
2013/05/10 16:34:36
'by' -> 'with'
eliben
2013/05/10 17:00:51
Done.
| |
| 11 // that are part of the PNaCl stable bitcode ABI. | |
| 12 // | |
| 13 //===----------------------------------------------------------------------===// | |
| 14 | |
| 15 #include "llvm/ADT/Twine.h" | |
| 16 #include "llvm/IR/Instructions.h" | |
| 17 #include "llvm/IR/Intrinsics.h" | |
| 18 #include "llvm/IR/Module.h" | |
| 19 #include "llvm/Pass.h" | |
| 20 #include "llvm/Transforms/NaCl.h" | |
| 21 | |
| 22 using namespace llvm; | |
| 23 | |
| 24 namespace { | |
| 25 class RewritePNaClLibraryCalls : public ModulePass { | |
|
Mark Seaborn
2013/05/10 16:34:36
Maybe comment why this is a ModulePass? Does it n
eliben
2013/05/10 17:00:51
Done.
| |
| 26 public: | |
| 27 static char ID; | |
| 28 RewritePNaClLibraryCalls() : ModulePass(ID) { | |
| 29 initializeExpandTlsPass(*PassRegistry::getPassRegistry()); | |
| 30 } | |
| 31 | |
| 32 virtual bool runOnModule(Module &M); | |
| 33 private: | |
| 34 /// Rewrite the given \p Call to setjmp | |
| 35 void rewriteSetjmp(CallInst *Call); | |
| 36 /// Rewrite the given \p Call to longjmp | |
| 37 void rewriteLongjmp(CallInst *Call); | |
| 38 | |
| 39 Module *TheModule; | |
| 40 }; | |
| 41 } | |
| 42 | |
| 43 char RewritePNaClLibraryCalls::ID = 0; | |
| 44 INITIALIZE_PASS(RewritePNaClLibraryCalls, "rewrite-pnacl-library-calls", | |
| 45 "Rewrite PNaCl library calls to stable intrinsics", | |
| 46 false, false) | |
| 47 | |
| 48 bool RewritePNaClLibraryCalls::runOnModule(Module &M) { | |
| 49 TheModule = &M; | |
| 50 bool Changed = false; | |
| 51 | |
| 52 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) { | |
|
Mark Seaborn
2013/05/10 16:34:36
Iterating through all calls is inefficient. It wo
eliben
2013/05/10 17:00:51
Yeah, although given a larger amount of functions
eliben
2013/05/10 23:39:52
Done.
| |
| 53 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI) { | |
| 54 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) { | |
| 55 Instruction *Inst = I++; | |
| 56 | |
| 57 if (CallInst *Call = dyn_cast<CallInst>(Inst)) { | |
| 58 StringRef CalleeName = Call->getCalledFunction()->getName(); | |
|
Mark Seaborn
2013/05/10 16:34:36
I think you need to check the linkage type too. T
eliben
2013/05/10 23:39:52
Done.
| |
| 59 | |
| 60 if (CalleeName == "setjmp") { | |
| 61 rewriteSetjmp(Call); | |
| 62 Changed = true; | |
| 63 } else if (CalleeName == "longjmp") { | |
| 64 rewriteLongjmp(Call); | |
| 65 Changed = true; | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 return Changed; | |
| 73 } | |
| 74 | |
| 75 void RewritePNaClLibraryCalls::rewriteSetjmp(CallInst *Call) { | |
| 76 FunctionType *FTy = Call->getCalledFunction()->getFunctionType(); | |
| 77 | |
| 78 // Sanity check on the setjmp call. | |
| 79 if (FTy->getNumParams() != 1 || | |
| 80 !FTy->getReturnType()->isIntegerTy() || | |
| 81 !FTy->getParamType(0)->isPointerTy()) { | |
| 82 report_fatal_error(Twine("Wrong signature of function ") + | |
| 83 Call->getCalledFunction()->getName()); | |
| 84 } | |
| 85 const DebugLoc &DLoc = Call->getDebugLoc(); | |
| 86 | |
| 87 // Cast the jmp_buf argument to i8* since this is what the intrinsic expects. | |
| 88 Type *I8Ptr = Type::getInt8Ty(TheModule->getContext())->getPointerTo(); | |
|
Mark Seaborn
2013/05/10 16:34:36
You could use:
NaclSetjmpFunc->getFunctionType()->
| |
| 89 BitCastInst *JmpBufCast = new BitCastInst(Call->getArgOperand(0), I8Ptr, | |
| 90 "jmp_buf_i8", Call); | |
| 91 JmpBufCast->setDebugLoc(DLoc); | |
| 92 | |
| 93 // Find the intrinsic function and call it. | |
| 94 Function *NaclSetjmpFunc = Intrinsic::getDeclaration(TheModule, | |
|
Mark Seaborn
2013/05/10 16:34:36
Maybe "Nacl" -> "NaCl"?
eliben
2013/05/10 23:39:52
Done.
| |
| 95 Intrinsic::nacl_setjmp); | |
| 96 SmallVector<Value *, 2> Args; | |
| 97 Args.push_back(JmpBufCast); | |
| 98 CallInst *NaclSetjmpCall = CallInst::Create(NaclSetjmpFunc, Args, "", Call); | |
| 99 NaclSetjmpCall->setDebugLoc(DLoc); | |
|
Mark Seaborn
2013/05/10 16:34:36
Use takeName() too
eliben
2013/05/10 23:39:52
Done.
| |
| 100 | |
| 101 // Replace the original call. | |
| 102 Call->replaceAllUsesWith(NaclSetjmpCall); | |
| 103 Call->eraseFromParent(); | |
| 104 } | |
| 105 | |
| 106 void RewritePNaClLibraryCalls::rewriteLongjmp(CallInst *Call) { | |
| 107 FunctionType *FTy = Call->getCalledFunction()->getFunctionType(); | |
| 108 | |
| 109 // Sanity check on the longjmp call. | |
| 110 if (FTy->getNumParams() != 2 || | |
| 111 !FTy->getReturnType()->isVoidTy() || | |
| 112 !FTy->getParamType(0)->isPointerTy()) { | |
| 113 report_fatal_error(Twine("Wrong signature of function ") + | |
| 114 Call->getCalledFunction()->getName()); | |
| 115 } | |
| 116 const DebugLoc &DLoc = Call->getDebugLoc(); | |
| 117 | |
| 118 // Cast the jmp_buf argument to i8* since this is what the intrinsic expects. | |
| 119 Type *I8Ptr = Type::getInt8Ty(TheModule->getContext())->getPointerTo(); | |
| 120 BitCastInst *JmpBufCast = new BitCastInst(Call->getArgOperand(0), I8Ptr, | |
| 121 "jmp_buf_i8", Call); | |
| 122 JmpBufCast->setDebugLoc(DLoc); | |
| 123 | |
| 124 // Find the intrinsic function and call it. | |
| 125 Function *NaclLongjmpFunc = Intrinsic::getDeclaration(TheModule, | |
| 126 Intrinsic::nacl_longjmp) ; | |
|
Mark Seaborn
2013/05/10 16:34:36
Line >80 chars
eliben
2013/05/10 23:39:52
Done.
| |
| 127 SmallVector<Value *, 2> Args; | |
| 128 Args.push_back(JmpBufCast); | |
| 129 Args.push_back(Call->getArgOperand(1)); | |
| 130 CallInst *NaclLongjmpCall = CallInst::Create(NaclLongjmpFunc, Args, "", Call); | |
| 131 NaclLongjmpCall->setDebugLoc(DLoc); | |
| 132 | |
| 133 // Remove the original call. There's no need for RAUW because longjmp | |
| 134 // returns void. | |
| 135 Call->eraseFromParent(); | |
| 136 } | |
| 137 | |
| 138 ModulePass *llvm::createRewritePNaClLibraryCallsPass() { | |
| 139 return new RewritePNaClLibraryCalls(); | |
| 140 } | |
| OLD | NEW |