LLVM: lib/Target/WebAssembly/WebAssemblyAddMissingPrototypes.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
29using namespace llvm;
30
31#define DEBUG_TYPE "wasm-add-missing-prototypes"
32
33namespace {
34class WebAssemblyAddMissingPrototypes final : public ModulePass {
35 StringRef getPassName() const override {
36 return "Add prototypes to prototypes-less functions";
37 }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
42 }
43
44 bool runOnModule(Module &M) override;
45
46public:
47 static char ID;
48 WebAssemblyAddMissingPrototypes() : ModulePass(ID) {}
49};
50}
51
52char WebAssemblyAddMissingPrototypes::ID = 0;
54 "Add prototypes to prototypes-less functions", false, false)
55
57 return new WebAssemblyAddMissingPrototypes();
58}
59
60bool WebAssemblyAddMissingPrototypes::runOnModule(Module &M) {
61 LLVM_DEBUG(dbgs() << "********** Add Missing Prototypes **********\n");
62
63 std::vector<std::pair<Function *, Function *>> Replacements;
64
65
66 for (Function &F : M) {
67 if (.isDeclaration() ||
.hasFnAttribute("no-prototype"))
68 continue;
69
70 LLVM_DEBUG(dbgs() << "Found no-prototype function: " << F.getName()
71 << "\n");
72
73
74
75
76 if (.isVarArg())
78 "Functions with 'no-prototype' attribute must take varargs: " +
79 F.getName());
80 unsigned NumParams = F.getFunctionType()->getNumParams();
81 if (NumParams != 0) {
82 if (!(NumParams == 1 && F.arg_begin()->hasStructRetAttr()))
84 "not have params: " +
85 F.getName());
86 }
87
88
92 while (!Worklist.empty()) {
94 for (User *U : V->users()) {
98 if (CB->getCalledOperand() == V)
100 }
101 }
102
103
104 FunctionType *NewType = nullptr;
105 for (CallBase *CB : Calls) {
106 LLVM_DEBUG(dbgs() << "prototype-less call of " << F.getName() << ":\n");
108 FunctionType *DestType = CB->getFunctionType();
109 if (!NewType) {
110
111 NewType = DestType;
112 LLVM_DEBUG(dbgs() << "found function type: " << *NewType << "\n");
113 } else if (NewType != DestType) {
114 errs() << "warning: prototype-less function used with "
115 "conflicting signatures: "
116 << F.getName() << "\n";
119 }
120 }
121
122 if (!NewType) {
124 dbgs() << "could not derive a function prototype from usage: " +
125 F.getName() + "\n");
126
127
128
129
130
131 NewType = FunctionType::get(F.getFunctionType()->getReturnType(), false);
132 }
133
135 Function::Create(NewType, F.getLinkage(), F.getName() + ".fixed_sig");
138 Replacements.emplace_back(&F, NewF);
139 }
140
141 for (auto &Pair : Replacements) {
143 Function *NewF = Pair.second;
144 std::string Name = std::string(OldF->getName());
145 M.getFunctionList().push_back(NewF);
150 }
151
152 return !Replacements.empty();
153}
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)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
static LLVM_ABI Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
void removeFnAttr(Attribute::AttrKind Kind)
Remove function attributes from this function.
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
PointerType * getType() const
Global values are always pointers.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
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...
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
ModulePass * createWebAssemblyAddMissingPrototypes()
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.