35#define DEBUG_TYPE "wasm-fix-function-bitcasts"
38class FixFunctionBitcasts final :
public ModulePass {
40 return "WebAssembly Fix Function Bitcasts";
56char FixFunctionBitcasts::ID = 0;
58 "Fix mismatching bitcasts for WebAssembly",
false,
false)
61 return new FixFunctionBitcasts();
68 for (
User *U : V->users()) {
69 if (
auto *BC = dyn_cast<BitCastOperator>(U))
71 else if (
auto *
A = dyn_cast<GlobalAlias>(U))
73 else if (
auto *CB = dyn_cast<CallBase>(U)) {
74 Value *Callee = CB->getCalledOperand();
78 if (CB->getFunctionType() ==
F.getValueType())
81 Uses.push_back(std::make_pair(CB, &
F));
113 F->getName() +
"_bitcast", M);
114 Wrapper->setAttributes(
F->getAttributes());
124 bool TypeMismatch =
false;
125 bool WrapperNeeded =
false;
127 Type *ExpectedRtnType =
F->getFunctionType()->getReturnType();
128 Type *RtnType = Ty->getReturnType();
130 if ((
F->getFunctionType()->getNumParams() != Ty->getNumParams()) ||
131 (
F->getFunctionType()->isVarArg() != Ty->isVarArg()) ||
132 (ExpectedRtnType != RtnType))
133 WrapperNeeded =
true;
135 for (; AI != AE && PI != PE; ++AI, ++PI) {
137 Type *ParamType = *PI;
139 if (ArgType == ParamType) {
140 Args.push_back(&*AI);
146 Args.push_back(PtrCast);
148 LLVM_DEBUG(
dbgs() <<
"createWrapper: struct param type in bitcast: "
149 <<
F->getName() <<
"\n");
150 WrapperNeeded =
false;
153 <<
F->getName() <<
"\n");
155 << *ParamType <<
" Got: " << *ArgType <<
"\n");
162 if (WrapperNeeded && !TypeMismatch) {
163 for (; PI != PE; ++PI)
166 for (; AI != AE; ++AI)
167 Args.push_back(&*AI);
171 Type *ExpectedRtnType =
F->getFunctionType()->getReturnType();
172 Type *RtnType = Ty->getReturnType();
176 }
else if (ExpectedRtnType->
isVoidTy()) {
177 LLVM_DEBUG(
dbgs() <<
"Creating dummy return: " << *RtnType <<
"\n");
179 }
else if (RtnType == ExpectedRtnType) {
188 LLVM_DEBUG(
dbgs() <<
"createWrapper: struct return type in bitcast: "
189 <<
F->getName() <<
"\n");
190 WrapperNeeded =
false;
192 LLVM_DEBUG(
dbgs() <<
"createWrapper: return type mismatch calling: "
193 <<
F->getName() <<
"\n");
195 <<
" Got: " << *RtnType <<
"\n");
204 F->getName() +
"_bitcast_invalid", M);
205 Wrapper->setAttributes(
F->getAttributes());
208 Wrapper->setName(
F->getName() +
"_bitcast_invalid");
209 }
else if (!WrapperNeeded) {
210 LLVM_DEBUG(
dbgs() <<
"createWrapper: no wrapper needed: " <<
F->getName()
225 return FuncTy->getReturnType() == MainTy->getReturnType() &&
226 FuncTy->getNumParams() == 0 &&
230bool FixFunctionBitcasts::runOnModule(
Module &M) {
231 LLVM_DEBUG(
dbgs() <<
"********** Fix Function Bitcasts **********\n");
249 if (
F.getName() ==
"main") {
256 LLVM_DEBUG(
dbgs() <<
"Found `main` function with incorrect type: "
257 << *
F.getFunctionType() <<
"\n");
261 Uses.push_back(std::make_pair(CallMain, &
F));
268 for (
auto &UseFunc :
Uses) {
273 auto Pair = Wrappers.
insert(std::make_pair(std::make_pair(
F, Ty),
nullptr));
287 Main->
setName(
"__original_main");
298 MainWrapper->setName(
"main");
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Remove Loads Into Fake Uses
static void findUses(Value *V, Function &F, SmallVectorImpl< std::pair< CallBase *, Function * > > &Uses)
static bool shouldFixMainFunction(FunctionType *FuncTy, FunctionType *MainTy)
static Function * createWrapper(Function *F, FunctionType *Ty)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
This class represents an incoming formal argument to a Function.
LLVM Basic Block Representation.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Value * getCalledOperand() const
FunctionType * getFunctionType() const
void setCalledOperand(Value *V)
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static bool isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Check whether a bitcast, inttoptr, or ptrtoint cast between these types is valid and a no-op.
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
A parsed version of the target data layout string in and methods for querying it.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Type::subtype_iterator param_iterator
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
VisibilityTypes getVisibility() const
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LinkageTypes getLinkage() const
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
This is an important class for using LLVM in a threaded context.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isStructTy() const
True if this is an instance of StructType.
static IntegerType * getInt32Ty(LLVMContext &C)
bool isVoidTy() const
Return true if this is 'void'.
This function has undefined behavior.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
void setName(const Twine &Name)
Change the name of the value.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ Swift
Calling convention for Swift.
@ C
The default llvm calling convention, compatible with C.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
ModulePass * createWebAssemblyFixFunctionBitcasts()