WebAssemblyFixFunctionBitcasts.cpp revision 321369
1311818Sdim//===-- WebAssemblyFixFunctionBitcasts.cpp - Fix function bitcasts --------===// 2311818Sdim// 3311818Sdim// The LLVM Compiler Infrastructure 4311818Sdim// 5311818Sdim// This file is distributed under the University of Illinois Open Source 6311818Sdim// License. See LICENSE.TXT for details. 7311818Sdim// 8311818Sdim//===----------------------------------------------------------------------===// 9311818Sdim/// 10311818Sdim/// \file 11311818Sdim/// \brief Fix bitcasted functions. 12311818Sdim/// 13311818Sdim/// WebAssembly requires caller and callee signatures to match, however in LLVM, 14311818Sdim/// some amount of slop is vaguely permitted. Detect mismatch by looking for 15311818Sdim/// bitcasts of functions and rewrite them to use wrapper functions instead. 16311818Sdim/// 17311818Sdim/// This doesn't catch all cases, such as when a function's address is taken in 18311818Sdim/// one place and casted in another, but it works for many common cases. 19311818Sdim/// 20311818Sdim/// Note that LLVM already optimizes away function bitcasts in common cases by 21311818Sdim/// dropping arguments as needed, so this pass only ends up getting used in less 22311818Sdim/// common cases. 23311818Sdim/// 24311818Sdim//===----------------------------------------------------------------------===// 25311818Sdim 26311818Sdim#include "WebAssembly.h" 27311818Sdim#include "llvm/IR/Constants.h" 28311818Sdim#include "llvm/IR/Instructions.h" 29311818Sdim#include "llvm/IR/Module.h" 30311818Sdim#include "llvm/IR/Operator.h" 31311818Sdim#include "llvm/Pass.h" 32311818Sdim#include "llvm/Support/Debug.h" 33311818Sdim#include "llvm/Support/raw_ostream.h" 34311818Sdimusing namespace llvm; 35311818Sdim 36311818Sdim#define DEBUG_TYPE "wasm-fix-function-bitcasts" 37311818Sdim 38311818Sdimnamespace { 39311818Sdimclass FixFunctionBitcasts final : public ModulePass { 40311818Sdim StringRef getPassName() const override { 41311818Sdim return "WebAssembly Fix Function Bitcasts"; 42311818Sdim } 43311818Sdim 44311818Sdim void getAnalysisUsage(AnalysisUsage &AU) const override { 45311818Sdim AU.setPreservesCFG(); 46311818Sdim ModulePass::getAnalysisUsage(AU); 47311818Sdim } 48311818Sdim 49311818Sdim bool runOnModule(Module &M) override; 50311818Sdim 51311818Sdimpublic: 52311818Sdim static char ID; 53311818Sdim FixFunctionBitcasts() : ModulePass(ID) {} 54311818Sdim}; 55311818Sdim} // End anonymous namespace 56311818Sdim 57311818Sdimchar FixFunctionBitcasts::ID = 0; 58311818SdimModulePass *llvm::createWebAssemblyFixFunctionBitcasts() { 59311818Sdim return new FixFunctionBitcasts(); 60311818Sdim} 61311818Sdim 62311818Sdim// Recursively descend the def-use lists from V to find non-bitcast users of 63311818Sdim// bitcasts of V. 64311818Sdimstatic void FindUses(Value *V, Function &F, 65312197Sdim SmallVectorImpl<std::pair<Use *, Function *>> &Uses, 66312197Sdim SmallPtrSetImpl<Constant *> &ConstantBCs) { 67311818Sdim for (Use &U : V->uses()) { 68311818Sdim if (BitCastOperator *BC = dyn_cast<BitCastOperator>(U.getUser())) 69312197Sdim FindUses(BC, F, Uses, ConstantBCs); 70312197Sdim else if (U.get()->getType() != F.getType()) { 71312197Sdim if (isa<Constant>(U.get())) { 72312197Sdim // Only add constant bitcasts to the list once; they get RAUW'd 73312197Sdim auto c = ConstantBCs.insert(cast<Constant>(U.get())); 74312197Sdim if (!c.second) continue; 75312197Sdim } 76311818Sdim Uses.push_back(std::make_pair(&U, &F)); 77312197Sdim } 78311818Sdim } 79311818Sdim} 80311818Sdim 81311818Sdim// Create a wrapper function with type Ty that calls F (which may have a 82311818Sdim// different type). Attempt to support common bitcasted function idioms: 83311818Sdim// - Call with more arguments than needed: arguments are dropped 84311818Sdim// - Call with fewer arguments than needed: arguments are filled in with undef 85311818Sdim// - Return value is not needed: drop it 86311818Sdim// - Return value needed but not present: supply an undef 87321369Sdim// 88311818Sdim// For now, return nullptr without creating a wrapper if the wrapper cannot 89311818Sdim// be generated due to incompatible types. 90311818Sdimstatic Function *CreateWrapper(Function *F, FunctionType *Ty) { 91311818Sdim Module *M = F->getParent(); 92311818Sdim 93311818Sdim Function *Wrapper = 94311818Sdim Function::Create(Ty, Function::PrivateLinkage, "bitcast", M); 95311818Sdim BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper); 96311818Sdim 97311818Sdim // Determine what arguments to pass. 98311818Sdim SmallVector<Value *, 4> Args; 99311818Sdim Function::arg_iterator AI = Wrapper->arg_begin(); 100311818Sdim FunctionType::param_iterator PI = F->getFunctionType()->param_begin(); 101311818Sdim FunctionType::param_iterator PE = F->getFunctionType()->param_end(); 102311818Sdim for (; AI != Wrapper->arg_end() && PI != PE; ++AI, ++PI) { 103311818Sdim if (AI->getType() != *PI) { 104311818Sdim Wrapper->eraseFromParent(); 105311818Sdim return nullptr; 106311818Sdim } 107311818Sdim Args.push_back(&*AI); 108311818Sdim } 109311818Sdim for (; PI != PE; ++PI) 110311818Sdim Args.push_back(UndefValue::get(*PI)); 111311818Sdim 112311818Sdim CallInst *Call = CallInst::Create(F, Args, "", BB); 113311818Sdim 114311818Sdim // Determine what value to return. 115311818Sdim if (Ty->getReturnType()->isVoidTy()) 116311818Sdim ReturnInst::Create(M->getContext(), BB); 117311818Sdim else if (F->getFunctionType()->getReturnType()->isVoidTy()) 118311818Sdim ReturnInst::Create(M->getContext(), UndefValue::get(Ty->getReturnType()), 119311818Sdim BB); 120311818Sdim else if (F->getFunctionType()->getReturnType() == Ty->getReturnType()) 121311818Sdim ReturnInst::Create(M->getContext(), Call, BB); 122311818Sdim else { 123311818Sdim Wrapper->eraseFromParent(); 124311818Sdim return nullptr; 125311818Sdim } 126311818Sdim 127311818Sdim return Wrapper; 128311818Sdim} 129311818Sdim 130311818Sdimbool FixFunctionBitcasts::runOnModule(Module &M) { 131311818Sdim SmallVector<std::pair<Use *, Function *>, 0> Uses; 132312197Sdim SmallPtrSet<Constant *, 2> ConstantBCs; 133311818Sdim 134311818Sdim // Collect all the places that need wrappers. 135312197Sdim for (Function &F : M) FindUses(&F, F, Uses, ConstantBCs); 136311818Sdim 137311818Sdim DenseMap<std::pair<Function *, FunctionType *>, Function *> Wrappers; 138311818Sdim 139311818Sdim for (auto &UseFunc : Uses) { 140311818Sdim Use *U = UseFunc.first; 141311818Sdim Function *F = UseFunc.second; 142311818Sdim PointerType *PTy = cast<PointerType>(U->get()->getType()); 143311818Sdim FunctionType *Ty = dyn_cast<FunctionType>(PTy->getElementType()); 144311818Sdim 145311818Sdim // If the function is casted to something like i8* as a "generic pointer" 146311818Sdim // to be later casted to something else, we can't generate a wrapper for it. 147311818Sdim // Just ignore such casts for now. 148311818Sdim if (!Ty) 149311818Sdim continue; 150311818Sdim 151321369Sdim // Wasm varargs are not ABI-compatible with non-varargs. Just ignore 152321369Sdim // such casts for now. 153321369Sdim if (Ty->isVarArg() || F->isVarArg()) 154321369Sdim continue; 155321369Sdim 156311818Sdim auto Pair = Wrappers.insert(std::make_pair(std::make_pair(F, Ty), nullptr)); 157311818Sdim if (Pair.second) 158311818Sdim Pair.first->second = CreateWrapper(F, Ty); 159311818Sdim 160311818Sdim Function *Wrapper = Pair.first->second; 161311818Sdim if (!Wrapper) 162311818Sdim continue; 163311818Sdim 164311818Sdim if (isa<Constant>(U->get())) 165311818Sdim U->get()->replaceAllUsesWith(Wrapper); 166311818Sdim else 167311818Sdim U->set(Wrapper); 168311818Sdim } 169311818Sdim 170311818Sdim return true; 171311818Sdim} 172