| OLD | NEW |
| 1 //===- ExpandVarArgs.cpp - Expand out variable argument function calls-----===// | 1 //===- ExpandVarArgs.cpp - Expand out variable argument function calls-----===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This pass expands out all use of variable argument functions. | 10 // This pass expands out all use of variable argument functions. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 } | 39 } |
| 40 virtual bool runOnModule(Module &M); | 40 virtual bool runOnModule(Module &M); |
| 41 }; | 41 }; |
| 42 } | 42 } |
| 43 | 43 |
| 44 char ExpandVarArgs::ID = 0; | 44 char ExpandVarArgs::ID = 0; |
| 45 INITIALIZE_PASS(ExpandVarArgs, "expand-varargs", | 45 INITIALIZE_PASS(ExpandVarArgs, "expand-varargs", |
| 46 "Expand out variable argument function definitions and calls", | 46 "Expand out variable argument function definitions and calls", |
| 47 false, false) | 47 false, false) |
| 48 | 48 |
| 49 static bool isEmscriptenJSArgsFunc(Module *M, StringRef Name) { | |
| 50 // TODO(jfb) Make these intrinsics in clang and remove the assert: these | |
| 51 // intrinsics should only exist for Emscripten. | |
| 52 bool isEmscriptenSpecial = Name.equals("emscripten_asm_const_int") || | |
| 53 Name.equals("emscripten_asm_const_double") || | |
| 54 Name.equals("emscripten_landingpad") || | |
| 55 Name.equals("emscripten_resume"); | |
| 56 assert(isEmscriptenSpecial ? Triple(M->getTargetTriple()).isOSEmscripten() | |
| 57 : true); | |
| 58 return isEmscriptenSpecial; | |
| 59 } | |
| 60 | |
| 61 static bool ExpandVarArgFunc(Module *M, Function *Func) { | 49 static bool ExpandVarArgFunc(Module *M, Function *Func) { |
| 62 if (isEmscriptenJSArgsFunc(M, Func->getName())) | |
| 63 return false; | |
| 64 | |
| 65 Type *PtrType = Type::getInt8PtrTy(Func->getContext()); | 50 Type *PtrType = Type::getInt8PtrTy(Func->getContext()); |
| 66 | 51 |
| 67 FunctionType *FTy = Func->getFunctionType(); | 52 FunctionType *FTy = Func->getFunctionType(); |
| 68 SmallVector<Type *, 8> Params(FTy->param_begin(), FTy->param_end()); | 53 SmallVector<Type *, 8> Params(FTy->param_begin(), FTy->param_end()); |
| 69 Params.push_back(PtrType); | 54 Params.push_back(PtrType); |
| 70 FunctionType *NFTy = | 55 FunctionType *NFTy = |
| 71 FunctionType::get(FTy->getReturnType(), Params, /*isVarArg=*/false); | 56 FunctionType::get(FTy->getReturnType(), Params, /*isVarArg=*/false); |
| 72 Function *NewFunc = RecreateFunction(Func, NFTy); | 57 Function *NewFunc = RecreateFunction(Func, NFTy); |
| 73 | 58 |
| 74 // Declare the new argument as "noalias". | 59 // Declare the new argument as "noalias". |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 } | 147 } |
| 163 | 148 |
| 164 // ExpandVarArgCall() converts a CallInst or InvokeInst to expand out | 149 // ExpandVarArgCall() converts a CallInst or InvokeInst to expand out |
| 165 // of varargs. It returns whether the module was modified. | 150 // of varargs. It returns whether the module was modified. |
| 166 template <class InstType> | 151 template <class InstType> |
| 167 static bool ExpandVarArgCall(Module *M, InstType *Call, DataLayout *DL) { | 152 static bool ExpandVarArgCall(Module *M, InstType *Call, DataLayout *DL) { |
| 168 FunctionType *FuncType = cast<FunctionType>( | 153 FunctionType *FuncType = cast<FunctionType>( |
| 169 Call->getCalledValue()->getType()->getPointerElementType()); | 154 Call->getCalledValue()->getType()->getPointerElementType()); |
| 170 if (!FuncType->isFunctionVarArg()) | 155 if (!FuncType->isFunctionVarArg()) |
| 171 return false; | 156 return false; |
| 172 if (auto *F = dyn_cast<Function>(Call->getCalledValue())) | |
| 173 if (isEmscriptenJSArgsFunc(M, F->getName())) | |
| 174 return false; | |
| 175 | 157 |
| 176 Function *F = Call->getParent()->getParent(); | 158 Function *F = Call->getParent()->getParent(); |
| 177 LLVMContext &Ctx = M->getContext(); | 159 LLVMContext &Ctx = M->getContext(); |
| 178 | 160 |
| 179 SmallVector<AttributeSet, 8> Attrs; | 161 SmallVector<AttributeSet, 8> Attrs; |
| 180 Attrs.push_back(Call->getAttributes().getFnAttributes()); | 162 Attrs.push_back(Call->getAttributes().getFnAttributes()); |
| 181 Attrs.push_back(Call->getAttributes().getRetAttributes()); | 163 Attrs.push_back(Call->getAttributes().getRetAttributes()); |
| 182 | 164 |
| 183 // Split argument list into fixed and variable arguments. | 165 // Split argument list into fixed and variable arguments. |
| 184 SmallVector<Value *, 8> FixedArgs; | 166 SmallVector<Value *, 8> FixedArgs; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 } | 294 } |
| 313 | 295 |
| 314 if (F->isVarArg()) | 296 if (F->isVarArg()) |
| 315 Changed |= ExpandVarArgFunc(&M, F); | 297 Changed |= ExpandVarArgFunc(&M, F); |
| 316 } | 298 } |
| 317 | 299 |
| 318 return Changed; | 300 return Changed; |
| 319 } | 301 } |
| 320 | 302 |
| 321 ModulePass *llvm::createExpandVarArgsPass() { return new ExpandVarArgs(); } | 303 ModulePass *llvm::createExpandVarArgsPass() { return new ExpandVarArgs(); } |
| OLD | NEW |