11b637458SDan Gohman //===-- WebAssemblyFixFunctionBitcasts.cpp - Fix function bitcasts --------===// 21b637458SDan Gohman // 31b637458SDan Gohman // The LLVM Compiler Infrastructure 41b637458SDan Gohman // 51b637458SDan Gohman // This file is distributed under the University of Illinois Open Source 61b637458SDan Gohman // License. See LICENSE.TXT for details. 71b637458SDan Gohman // 81b637458SDan Gohman //===----------------------------------------------------------------------===// 91b637458SDan Gohman /// 101b637458SDan Gohman /// \file 111b637458SDan Gohman /// \brief Fix bitcasted functions. 121b637458SDan Gohman /// 131b637458SDan Gohman /// WebAssembly requires caller and callee signatures to match, however in LLVM, 141b637458SDan Gohman /// some amount of slop is vaguely permitted. Detect mismatch by looking for 151b637458SDan Gohman /// bitcasts of functions and rewrite them to use wrapper functions instead. 161b637458SDan Gohman /// 171b637458SDan Gohman /// This doesn't catch all cases, such as when a function's address is taken in 181b637458SDan Gohman /// one place and casted in another, but it works for many common cases. 191b637458SDan Gohman /// 201b637458SDan Gohman /// Note that LLVM already optimizes away function bitcasts in common cases by 211b637458SDan Gohman /// dropping arguments as needed, so this pass only ends up getting used in less 221b637458SDan Gohman /// common cases. 231b637458SDan Gohman /// 241b637458SDan Gohman //===----------------------------------------------------------------------===// 251b637458SDan Gohman 261b637458SDan Gohman #include "WebAssembly.h" 2737af00e7SJacob Gravelle #include "llvm/IR/CallSite.h" 281b637458SDan Gohman #include "llvm/IR/Constants.h" 291b637458SDan Gohman #include "llvm/IR/Instructions.h" 301b637458SDan Gohman #include "llvm/IR/Module.h" 311b637458SDan Gohman #include "llvm/IR/Operator.h" 321b637458SDan Gohman #include "llvm/Pass.h" 331b637458SDan Gohman #include "llvm/Support/Debug.h" 341b637458SDan Gohman #include "llvm/Support/raw_ostream.h" 351b637458SDan Gohman using namespace llvm; 361b637458SDan Gohman 371b637458SDan Gohman #define DEBUG_TYPE "wasm-fix-function-bitcasts" 381b637458SDan Gohman 391b637458SDan Gohman namespace { 401b637458SDan Gohman class FixFunctionBitcasts final : public ModulePass { 411b637458SDan Gohman StringRef getPassName() const override { 421b637458SDan Gohman return "WebAssembly Fix Function Bitcasts"; 431b637458SDan Gohman } 441b637458SDan Gohman 451b637458SDan Gohman void getAnalysisUsage(AnalysisUsage &AU) const override { 461b637458SDan Gohman AU.setPreservesCFG(); 471b637458SDan Gohman ModulePass::getAnalysisUsage(AU); 481b637458SDan Gohman } 491b637458SDan Gohman 501b637458SDan Gohman bool runOnModule(Module &M) override; 511b637458SDan Gohman 521b637458SDan Gohman public: 531b637458SDan Gohman static char ID; 541b637458SDan Gohman FixFunctionBitcasts() : ModulePass(ID) {} 551b637458SDan Gohman }; 561b637458SDan Gohman } // End anonymous namespace 571b637458SDan Gohman 581b637458SDan Gohman char FixFunctionBitcasts::ID = 0; 591b637458SDan Gohman ModulePass *llvm::createWebAssemblyFixFunctionBitcasts() { 601b637458SDan Gohman return new FixFunctionBitcasts(); 611b637458SDan Gohman } 621b637458SDan Gohman 631b637458SDan Gohman // Recursively descend the def-use lists from V to find non-bitcast users of 641b637458SDan Gohman // bitcasts of V. 651b637458SDan Gohman static void FindUses(Value *V, Function &F, 667acb42a4SDerek Schuff SmallVectorImpl<std::pair<Use *, Function *>> &Uses, 677acb42a4SDerek Schuff SmallPtrSetImpl<Constant *> &ConstantBCs) { 681b637458SDan Gohman for (Use &U : V->uses()) { 691b637458SDan Gohman if (BitCastOperator *BC = dyn_cast<BitCastOperator>(U.getUser())) 707acb42a4SDerek Schuff FindUses(BC, F, Uses, ConstantBCs); 717acb42a4SDerek Schuff else if (U.get()->getType() != F.getType()) { 7237af00e7SJacob Gravelle CallSite CS(U.getUser()); 7337af00e7SJacob Gravelle if (!CS) 7437af00e7SJacob Gravelle // Skip uses that aren't immediately called 7537af00e7SJacob Gravelle continue; 7637af00e7SJacob Gravelle Value *Callee = CS.getCalledValue(); 7737af00e7SJacob Gravelle if (Callee != V) 7837af00e7SJacob Gravelle // Skip calls where the function isn't the callee 7937af00e7SJacob Gravelle continue; 807acb42a4SDerek Schuff if (isa<Constant>(U.get())) { 817acb42a4SDerek Schuff // Only add constant bitcasts to the list once; they get RAUW'd 827acb42a4SDerek Schuff auto c = ConstantBCs.insert(cast<Constant>(U.get())); 8337af00e7SJacob Gravelle if (!c.second) 8437af00e7SJacob Gravelle continue; 857acb42a4SDerek Schuff } 861b637458SDan Gohman Uses.push_back(std::make_pair(&U, &F)); 871b637458SDan Gohman } 881b637458SDan Gohman } 897acb42a4SDerek Schuff } 901b637458SDan Gohman 911b637458SDan Gohman // Create a wrapper function with type Ty that calls F (which may have a 921b637458SDan Gohman // different type). Attempt to support common bitcasted function idioms: 931b637458SDan Gohman // - Call with more arguments than needed: arguments are dropped 941b637458SDan Gohman // - Call with fewer arguments than needed: arguments are filled in with undef 951b637458SDan Gohman // - Return value is not needed: drop it 961b637458SDan Gohman // - Return value needed but not present: supply an undef 970e2ceb81SDan Gohman // 980e2ceb81SDan Gohman // For now, return nullptr without creating a wrapper if the wrapper cannot 990e2ceb81SDan Gohman // be generated due to incompatible types. 1001b637458SDan Gohman static Function *CreateWrapper(Function *F, FunctionType *Ty) { 1011b637458SDan Gohman Module *M = F->getParent(); 1021b637458SDan Gohman 1031b637458SDan Gohman Function *Wrapper = 1041b637458SDan Gohman Function::Create(Ty, Function::PrivateLinkage, "bitcast", M); 1051b637458SDan Gohman BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper); 1061b637458SDan Gohman 1071b637458SDan Gohman // Determine what arguments to pass. 1081b637458SDan Gohman SmallVector<Value *, 4> Args; 1091b637458SDan Gohman Function::arg_iterator AI = Wrapper->arg_begin(); 110*2803bfafSDan Gohman Function::arg_iterator AE = Wrapper->arg_end(); 1111b637458SDan Gohman FunctionType::param_iterator PI = F->getFunctionType()->param_begin(); 1121b637458SDan Gohman FunctionType::param_iterator PE = F->getFunctionType()->param_end(); 113*2803bfafSDan Gohman for (; AI != AE && PI != PE; ++AI, ++PI) { 1140e2ceb81SDan Gohman if (AI->getType() != *PI) { 1150e2ceb81SDan Gohman Wrapper->eraseFromParent(); 1160e2ceb81SDan Gohman return nullptr; 1170e2ceb81SDan Gohman } 1181b637458SDan Gohman Args.push_back(&*AI); 1191b637458SDan Gohman } 1201b637458SDan Gohman for (; PI != PE; ++PI) 1211b637458SDan Gohman Args.push_back(UndefValue::get(*PI)); 122*2803bfafSDan Gohman if (F->isVarArg()) 123*2803bfafSDan Gohman for (; AI != AE; ++AI) 124*2803bfafSDan Gohman Args.push_back(&*AI); 1251b637458SDan Gohman 1261b637458SDan Gohman CallInst *Call = CallInst::Create(F, Args, "", BB); 1271b637458SDan Gohman 1281b637458SDan Gohman // Determine what value to return. 1291b637458SDan Gohman if (Ty->getReturnType()->isVoidTy()) 1301b637458SDan Gohman ReturnInst::Create(M->getContext(), BB); 1311b637458SDan Gohman else if (F->getFunctionType()->getReturnType()->isVoidTy()) 1321b637458SDan Gohman ReturnInst::Create(M->getContext(), UndefValue::get(Ty->getReturnType()), 1331b637458SDan Gohman BB); 1341b637458SDan Gohman else if (F->getFunctionType()->getReturnType() == Ty->getReturnType()) 1351b637458SDan Gohman ReturnInst::Create(M->getContext(), Call, BB); 1360e2ceb81SDan Gohman else { 1370e2ceb81SDan Gohman Wrapper->eraseFromParent(); 1380e2ceb81SDan Gohman return nullptr; 1390e2ceb81SDan Gohman } 1401b637458SDan Gohman 1411b637458SDan Gohman return Wrapper; 1421b637458SDan Gohman } 1431b637458SDan Gohman 1441b637458SDan Gohman bool FixFunctionBitcasts::runOnModule(Module &M) { 145d5eda355SDan Gohman SmallVector<std::pair<Use *, Function *>, 0> Uses; 1467acb42a4SDerek Schuff SmallPtrSet<Constant *, 2> ConstantBCs; 147d5eda355SDan Gohman 1481b637458SDan Gohman // Collect all the places that need wrappers. 1497acb42a4SDerek Schuff for (Function &F : M) FindUses(&F, F, Uses, ConstantBCs); 1501b637458SDan Gohman 1511b637458SDan Gohman DenseMap<std::pair<Function *, FunctionType *>, Function *> Wrappers; 1521b637458SDan Gohman 1531b637458SDan Gohman for (auto &UseFunc : Uses) { 1541b637458SDan Gohman Use *U = UseFunc.first; 1551b637458SDan Gohman Function *F = UseFunc.second; 1561b637458SDan Gohman PointerType *PTy = cast<PointerType>(U->get()->getType()); 1571b637458SDan Gohman FunctionType *Ty = dyn_cast<FunctionType>(PTy->getElementType()); 1581b637458SDan Gohman 1591b637458SDan Gohman // If the function is casted to something like i8* as a "generic pointer" 1601b637458SDan Gohman // to be later casted to something else, we can't generate a wrapper for it. 1611b637458SDan Gohman // Just ignore such casts for now. 1621b637458SDan Gohman if (!Ty) 1631b637458SDan Gohman continue; 1641b637458SDan Gohman 1651b637458SDan Gohman auto Pair = Wrappers.insert(std::make_pair(std::make_pair(F, Ty), nullptr)); 1661b637458SDan Gohman if (Pair.second) 1671b637458SDan Gohman Pair.first->second = CreateWrapper(F, Ty); 1681b637458SDan Gohman 1690e2ceb81SDan Gohman Function *Wrapper = Pair.first->second; 1700e2ceb81SDan Gohman if (!Wrapper) 1710e2ceb81SDan Gohman continue; 1720e2ceb81SDan Gohman 1731b637458SDan Gohman if (isa<Constant>(U->get())) 1740e2ceb81SDan Gohman U->get()->replaceAllUsesWith(Wrapper); 1751b637458SDan Gohman else 1760e2ceb81SDan Gohman U->set(Wrapper); 1771b637458SDan Gohman } 1781b637458SDan Gohman 1791b637458SDan Gohman return true; 1801b637458SDan Gohman } 181