1*9880d681SAndroid Build Coastguard Worker //===-- Module.cpp - Implement the Module class ---------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the Module class for the IR library.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
15*9880d681SAndroid Build Coastguard Worker #include "SymbolTableListTraitsImpl.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallString.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringExtras.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DebugInfoMetadata.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GVMaterializer.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstrTypes.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/TypeFinder.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Dwarf.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Path.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/RandomNumberGenerator.h"
30*9880d681SAndroid Build Coastguard Worker #include <algorithm>
31*9880d681SAndroid Build Coastguard Worker #include <cstdarg>
32*9880d681SAndroid Build Coastguard Worker #include <cstdlib>
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker using namespace llvm;
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
37*9880d681SAndroid Build Coastguard Worker // Methods to implement the globals and functions lists.
38*9880d681SAndroid Build Coastguard Worker //
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker // Explicit instantiations of SymbolTableListTraits since some of the methods
41*9880d681SAndroid Build Coastguard Worker // are not in the public header file.
42*9880d681SAndroid Build Coastguard Worker template class llvm::SymbolTableListTraits<Function>;
43*9880d681SAndroid Build Coastguard Worker template class llvm::SymbolTableListTraits<GlobalVariable>;
44*9880d681SAndroid Build Coastguard Worker template class llvm::SymbolTableListTraits<GlobalAlias>;
45*9880d681SAndroid Build Coastguard Worker template class llvm::SymbolTableListTraits<GlobalIFunc>;
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
48*9880d681SAndroid Build Coastguard Worker // Primitive Module methods.
49*9880d681SAndroid Build Coastguard Worker //
50*9880d681SAndroid Build Coastguard Worker
Module(StringRef MID,LLVMContext & C)51*9880d681SAndroid Build Coastguard Worker Module::Module(StringRef MID, LLVMContext &C)
52*9880d681SAndroid Build Coastguard Worker : Context(C), Materializer(), ModuleID(MID), SourceFileName(MID), DL("") {
53*9880d681SAndroid Build Coastguard Worker ValSymTab = new ValueSymbolTable();
54*9880d681SAndroid Build Coastguard Worker NamedMDSymTab = new StringMap<NamedMDNode *>();
55*9880d681SAndroid Build Coastguard Worker Context.addModule(this);
56*9880d681SAndroid Build Coastguard Worker }
57*9880d681SAndroid Build Coastguard Worker
~Module()58*9880d681SAndroid Build Coastguard Worker Module::~Module() {
59*9880d681SAndroid Build Coastguard Worker Context.removeModule(this);
60*9880d681SAndroid Build Coastguard Worker dropAllReferences();
61*9880d681SAndroid Build Coastguard Worker GlobalList.clear();
62*9880d681SAndroid Build Coastguard Worker FunctionList.clear();
63*9880d681SAndroid Build Coastguard Worker AliasList.clear();
64*9880d681SAndroid Build Coastguard Worker IFuncList.clear();
65*9880d681SAndroid Build Coastguard Worker NamedMDList.clear();
66*9880d681SAndroid Build Coastguard Worker delete ValSymTab;
67*9880d681SAndroid Build Coastguard Worker delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
68*9880d681SAndroid Build Coastguard Worker }
69*9880d681SAndroid Build Coastguard Worker
createRNG(const Pass * P) const70*9880d681SAndroid Build Coastguard Worker RandomNumberGenerator *Module::createRNG(const Pass* P) const {
71*9880d681SAndroid Build Coastguard Worker SmallString<32> Salt(P->getPassName());
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker // This RNG is guaranteed to produce the same random stream only
74*9880d681SAndroid Build Coastguard Worker // when the Module ID and thus the input filename is the same. This
75*9880d681SAndroid Build Coastguard Worker // might be problematic if the input filename extension changes
76*9880d681SAndroid Build Coastguard Worker // (e.g. from .c to .bc or .ll).
77*9880d681SAndroid Build Coastguard Worker //
78*9880d681SAndroid Build Coastguard Worker // We could store this salt in NamedMetadata, but this would make
79*9880d681SAndroid Build Coastguard Worker // the parameter non-const. This would unfortunately make this
80*9880d681SAndroid Build Coastguard Worker // interface unusable by any Machine passes, since they only have a
81*9880d681SAndroid Build Coastguard Worker // const reference to their IR Module. Alternatively we can always
82*9880d681SAndroid Build Coastguard Worker // store salt metadata from the Module constructor.
83*9880d681SAndroid Build Coastguard Worker Salt += sys::path::filename(getModuleIdentifier());
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker return new RandomNumberGenerator(Salt);
86*9880d681SAndroid Build Coastguard Worker }
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker /// getNamedValue - Return the first global value in the module with
89*9880d681SAndroid Build Coastguard Worker /// the specified name, of arbitrary type. This method returns null
90*9880d681SAndroid Build Coastguard Worker /// if a global with the specified name is not found.
getNamedValue(StringRef Name) const91*9880d681SAndroid Build Coastguard Worker GlobalValue *Module::getNamedValue(StringRef Name) const {
92*9880d681SAndroid Build Coastguard Worker return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
93*9880d681SAndroid Build Coastguard Worker }
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
96*9880d681SAndroid Build Coastguard Worker /// This ID is uniqued across modules in the current LLVMContext.
getMDKindID(StringRef Name) const97*9880d681SAndroid Build Coastguard Worker unsigned Module::getMDKindID(StringRef Name) const {
98*9880d681SAndroid Build Coastguard Worker return Context.getMDKindID(Name);
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker /// getMDKindNames - Populate client supplied SmallVector with the name for
102*9880d681SAndroid Build Coastguard Worker /// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
103*9880d681SAndroid Build Coastguard Worker /// so it is filled in as an empty string.
getMDKindNames(SmallVectorImpl<StringRef> & Result) const104*9880d681SAndroid Build Coastguard Worker void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
105*9880d681SAndroid Build Coastguard Worker return Context.getMDKindNames(Result);
106*9880d681SAndroid Build Coastguard Worker }
107*9880d681SAndroid Build Coastguard Worker
getOperandBundleTags(SmallVectorImpl<StringRef> & Result) const108*9880d681SAndroid Build Coastguard Worker void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const {
109*9880d681SAndroid Build Coastguard Worker return Context.getOperandBundleTags(Result);
110*9880d681SAndroid Build Coastguard Worker }
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
113*9880d681SAndroid Build Coastguard Worker // Methods for easy access to the functions in the module.
114*9880d681SAndroid Build Coastguard Worker //
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker // getOrInsertFunction - Look up the specified function in the module symbol
117*9880d681SAndroid Build Coastguard Worker // table. If it does not exist, add a prototype for the function and return
118*9880d681SAndroid Build Coastguard Worker // it. This is nice because it allows most passes to get away with not handling
119*9880d681SAndroid Build Coastguard Worker // the symbol table directly for this common task.
120*9880d681SAndroid Build Coastguard Worker //
getOrInsertFunction(StringRef Name,FunctionType * Ty,AttributeSet AttributeList)121*9880d681SAndroid Build Coastguard Worker Constant *Module::getOrInsertFunction(StringRef Name,
122*9880d681SAndroid Build Coastguard Worker FunctionType *Ty,
123*9880d681SAndroid Build Coastguard Worker AttributeSet AttributeList) {
124*9880d681SAndroid Build Coastguard Worker // See if we have a definition for the specified function already.
125*9880d681SAndroid Build Coastguard Worker GlobalValue *F = getNamedValue(Name);
126*9880d681SAndroid Build Coastguard Worker if (!F) {
127*9880d681SAndroid Build Coastguard Worker // Nope, add it
128*9880d681SAndroid Build Coastguard Worker Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
129*9880d681SAndroid Build Coastguard Worker if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
130*9880d681SAndroid Build Coastguard Worker New->setAttributes(AttributeList);
131*9880d681SAndroid Build Coastguard Worker FunctionList.push_back(New);
132*9880d681SAndroid Build Coastguard Worker return New; // Return the new prototype.
133*9880d681SAndroid Build Coastguard Worker }
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker // If the function exists but has the wrong type, return a bitcast to the
136*9880d681SAndroid Build Coastguard Worker // right type.
137*9880d681SAndroid Build Coastguard Worker if (F->getType() != PointerType::getUnqual(Ty))
138*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker // Otherwise, we just found the existing function or a prototype.
141*9880d681SAndroid Build Coastguard Worker return F;
142*9880d681SAndroid Build Coastguard Worker }
143*9880d681SAndroid Build Coastguard Worker
getOrInsertFunction(StringRef Name,FunctionType * Ty)144*9880d681SAndroid Build Coastguard Worker Constant *Module::getOrInsertFunction(StringRef Name,
145*9880d681SAndroid Build Coastguard Worker FunctionType *Ty) {
146*9880d681SAndroid Build Coastguard Worker return getOrInsertFunction(Name, Ty, AttributeSet());
147*9880d681SAndroid Build Coastguard Worker }
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker // getOrInsertFunction - Look up the specified function in the module symbol
150*9880d681SAndroid Build Coastguard Worker // table. If it does not exist, add a prototype for the function and return it.
151*9880d681SAndroid Build Coastguard Worker // This version of the method takes a null terminated list of function
152*9880d681SAndroid Build Coastguard Worker // arguments, which makes it easier for clients to use.
153*9880d681SAndroid Build Coastguard Worker //
getOrInsertFunction(StringRef Name,AttributeSet AttributeList,Type * RetTy,...)154*9880d681SAndroid Build Coastguard Worker Constant *Module::getOrInsertFunction(StringRef Name,
155*9880d681SAndroid Build Coastguard Worker AttributeSet AttributeList,
156*9880d681SAndroid Build Coastguard Worker Type *RetTy, ...) {
157*9880d681SAndroid Build Coastguard Worker va_list Args;
158*9880d681SAndroid Build Coastguard Worker va_start(Args, RetTy);
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker // Build the list of argument types...
161*9880d681SAndroid Build Coastguard Worker std::vector<Type*> ArgTys;
162*9880d681SAndroid Build Coastguard Worker while (Type *ArgTy = va_arg(Args, Type*))
163*9880d681SAndroid Build Coastguard Worker ArgTys.push_back(ArgTy);
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker va_end(Args);
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Worker // Build the function type and chain to the other getOrInsertFunction...
168*9880d681SAndroid Build Coastguard Worker return getOrInsertFunction(Name,
169*9880d681SAndroid Build Coastguard Worker FunctionType::get(RetTy, ArgTys, false),
170*9880d681SAndroid Build Coastguard Worker AttributeList);
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker
getOrInsertFunction(StringRef Name,Type * RetTy,...)173*9880d681SAndroid Build Coastguard Worker Constant *Module::getOrInsertFunction(StringRef Name,
174*9880d681SAndroid Build Coastguard Worker Type *RetTy, ...) {
175*9880d681SAndroid Build Coastguard Worker va_list Args;
176*9880d681SAndroid Build Coastguard Worker va_start(Args, RetTy);
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker // Build the list of argument types...
179*9880d681SAndroid Build Coastguard Worker std::vector<Type*> ArgTys;
180*9880d681SAndroid Build Coastguard Worker while (Type *ArgTy = va_arg(Args, Type*))
181*9880d681SAndroid Build Coastguard Worker ArgTys.push_back(ArgTy);
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker va_end(Args);
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker // Build the function type and chain to the other getOrInsertFunction...
186*9880d681SAndroid Build Coastguard Worker return getOrInsertFunction(Name,
187*9880d681SAndroid Build Coastguard Worker FunctionType::get(RetTy, ArgTys, false),
188*9880d681SAndroid Build Coastguard Worker AttributeSet());
189*9880d681SAndroid Build Coastguard Worker }
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Worker // getFunction - Look up the specified function in the module symbol table.
192*9880d681SAndroid Build Coastguard Worker // If it does not exist, return null.
193*9880d681SAndroid Build Coastguard Worker //
getFunction(StringRef Name) const194*9880d681SAndroid Build Coastguard Worker Function *Module::getFunction(StringRef Name) const {
195*9880d681SAndroid Build Coastguard Worker return dyn_cast_or_null<Function>(getNamedValue(Name));
196*9880d681SAndroid Build Coastguard Worker }
197*9880d681SAndroid Build Coastguard Worker
198*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
199*9880d681SAndroid Build Coastguard Worker // Methods for easy access to the global variables in the module.
200*9880d681SAndroid Build Coastguard Worker //
201*9880d681SAndroid Build Coastguard Worker
202*9880d681SAndroid Build Coastguard Worker /// getGlobalVariable - Look up the specified global variable in the module
203*9880d681SAndroid Build Coastguard Worker /// symbol table. If it does not exist, return null. The type argument
204*9880d681SAndroid Build Coastguard Worker /// should be the underlying type of the global, i.e., it should not have
205*9880d681SAndroid Build Coastguard Worker /// the top-level PointerType, which represents the address of the global.
206*9880d681SAndroid Build Coastguard Worker /// If AllowLocal is set to true, this function will return types that
207*9880d681SAndroid Build Coastguard Worker /// have an local. By default, these types are not returned.
208*9880d681SAndroid Build Coastguard Worker ///
getGlobalVariable(StringRef Name,bool AllowLocal)209*9880d681SAndroid Build Coastguard Worker GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
210*9880d681SAndroid Build Coastguard Worker if (GlobalVariable *Result =
211*9880d681SAndroid Build Coastguard Worker dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
212*9880d681SAndroid Build Coastguard Worker if (AllowLocal || !Result->hasLocalLinkage())
213*9880d681SAndroid Build Coastguard Worker return Result;
214*9880d681SAndroid Build Coastguard Worker return nullptr;
215*9880d681SAndroid Build Coastguard Worker }
216*9880d681SAndroid Build Coastguard Worker
217*9880d681SAndroid Build Coastguard Worker /// getOrInsertGlobal - Look up the specified global in the module symbol table.
218*9880d681SAndroid Build Coastguard Worker /// 1. If it does not exist, add a declaration of the global and return it.
219*9880d681SAndroid Build Coastguard Worker /// 2. Else, the global exists but has the wrong type: return the function
220*9880d681SAndroid Build Coastguard Worker /// with a constantexpr cast to the right type.
221*9880d681SAndroid Build Coastguard Worker /// 3. Finally, if the existing global is the correct declaration, return the
222*9880d681SAndroid Build Coastguard Worker /// existing global.
getOrInsertGlobal(StringRef Name,Type * Ty)223*9880d681SAndroid Build Coastguard Worker Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
224*9880d681SAndroid Build Coastguard Worker // See if we have a definition for the specified global already.
225*9880d681SAndroid Build Coastguard Worker GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
226*9880d681SAndroid Build Coastguard Worker if (!GV) {
227*9880d681SAndroid Build Coastguard Worker // Nope, add it
228*9880d681SAndroid Build Coastguard Worker GlobalVariable *New =
229*9880d681SAndroid Build Coastguard Worker new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
230*9880d681SAndroid Build Coastguard Worker nullptr, Name);
231*9880d681SAndroid Build Coastguard Worker return New; // Return the new declaration.
232*9880d681SAndroid Build Coastguard Worker }
233*9880d681SAndroid Build Coastguard Worker
234*9880d681SAndroid Build Coastguard Worker // If the variable exists but has the wrong type, return a bitcast to the
235*9880d681SAndroid Build Coastguard Worker // right type.
236*9880d681SAndroid Build Coastguard Worker Type *GVTy = GV->getType();
237*9880d681SAndroid Build Coastguard Worker PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
238*9880d681SAndroid Build Coastguard Worker if (GVTy != PTy)
239*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(GV, PTy);
240*9880d681SAndroid Build Coastguard Worker
241*9880d681SAndroid Build Coastguard Worker // Otherwise, we just found the existing function or a prototype.
242*9880d681SAndroid Build Coastguard Worker return GV;
243*9880d681SAndroid Build Coastguard Worker }
244*9880d681SAndroid Build Coastguard Worker
245*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
246*9880d681SAndroid Build Coastguard Worker // Methods for easy access to the global variables in the module.
247*9880d681SAndroid Build Coastguard Worker //
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard Worker // getNamedAlias - Look up the specified global in the module symbol table.
250*9880d681SAndroid Build Coastguard Worker // If it does not exist, return null.
251*9880d681SAndroid Build Coastguard Worker //
getNamedAlias(StringRef Name) const252*9880d681SAndroid Build Coastguard Worker GlobalAlias *Module::getNamedAlias(StringRef Name) const {
253*9880d681SAndroid Build Coastguard Worker return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
254*9880d681SAndroid Build Coastguard Worker }
255*9880d681SAndroid Build Coastguard Worker
getNamedIFunc(StringRef Name) const256*9880d681SAndroid Build Coastguard Worker GlobalIFunc *Module::getNamedIFunc(StringRef Name) const {
257*9880d681SAndroid Build Coastguard Worker return dyn_cast_or_null<GlobalIFunc>(getNamedValue(Name));
258*9880d681SAndroid Build Coastguard Worker }
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker /// getNamedMetadata - Return the first NamedMDNode in the module with the
261*9880d681SAndroid Build Coastguard Worker /// specified name. This method returns null if a NamedMDNode with the
262*9880d681SAndroid Build Coastguard Worker /// specified name is not found.
getNamedMetadata(const Twine & Name) const263*9880d681SAndroid Build Coastguard Worker NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
264*9880d681SAndroid Build Coastguard Worker SmallString<256> NameData;
265*9880d681SAndroid Build Coastguard Worker StringRef NameRef = Name.toStringRef(NameData);
266*9880d681SAndroid Build Coastguard Worker return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
267*9880d681SAndroid Build Coastguard Worker }
268*9880d681SAndroid Build Coastguard Worker
269*9880d681SAndroid Build Coastguard Worker /// getOrInsertNamedMetadata - Return the first named MDNode in the module
270*9880d681SAndroid Build Coastguard Worker /// with the specified name. This method returns a new NamedMDNode if a
271*9880d681SAndroid Build Coastguard Worker /// NamedMDNode with the specified name is not found.
getOrInsertNamedMetadata(StringRef Name)272*9880d681SAndroid Build Coastguard Worker NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
273*9880d681SAndroid Build Coastguard Worker NamedMDNode *&NMD =
274*9880d681SAndroid Build Coastguard Worker (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
275*9880d681SAndroid Build Coastguard Worker if (!NMD) {
276*9880d681SAndroid Build Coastguard Worker NMD = new NamedMDNode(Name);
277*9880d681SAndroid Build Coastguard Worker NMD->setParent(this);
278*9880d681SAndroid Build Coastguard Worker NamedMDList.push_back(NMD);
279*9880d681SAndroid Build Coastguard Worker }
280*9880d681SAndroid Build Coastguard Worker return NMD;
281*9880d681SAndroid Build Coastguard Worker }
282*9880d681SAndroid Build Coastguard Worker
283*9880d681SAndroid Build Coastguard Worker /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
284*9880d681SAndroid Build Coastguard Worker /// delete it.
eraseNamedMetadata(NamedMDNode * NMD)285*9880d681SAndroid Build Coastguard Worker void Module::eraseNamedMetadata(NamedMDNode *NMD) {
286*9880d681SAndroid Build Coastguard Worker static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
287*9880d681SAndroid Build Coastguard Worker NamedMDList.erase(NMD->getIterator());
288*9880d681SAndroid Build Coastguard Worker }
289*9880d681SAndroid Build Coastguard Worker
isValidModFlagBehavior(Metadata * MD,ModFlagBehavior & MFB)290*9880d681SAndroid Build Coastguard Worker bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
291*9880d681SAndroid Build Coastguard Worker if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
292*9880d681SAndroid Build Coastguard Worker uint64_t Val = Behavior->getLimitedValue();
293*9880d681SAndroid Build Coastguard Worker if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
294*9880d681SAndroid Build Coastguard Worker MFB = static_cast<ModFlagBehavior>(Val);
295*9880d681SAndroid Build Coastguard Worker return true;
296*9880d681SAndroid Build Coastguard Worker }
297*9880d681SAndroid Build Coastguard Worker }
298*9880d681SAndroid Build Coastguard Worker return false;
299*9880d681SAndroid Build Coastguard Worker }
300*9880d681SAndroid Build Coastguard Worker
301*9880d681SAndroid Build Coastguard Worker /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
302*9880d681SAndroid Build Coastguard Worker void Module::
getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> & Flags) const303*9880d681SAndroid Build Coastguard Worker getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
304*9880d681SAndroid Build Coastguard Worker const NamedMDNode *ModFlags = getModuleFlagsMetadata();
305*9880d681SAndroid Build Coastguard Worker if (!ModFlags) return;
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker for (const MDNode *Flag : ModFlags->operands()) {
308*9880d681SAndroid Build Coastguard Worker ModFlagBehavior MFB;
309*9880d681SAndroid Build Coastguard Worker if (Flag->getNumOperands() >= 3 &&
310*9880d681SAndroid Build Coastguard Worker isValidModFlagBehavior(Flag->getOperand(0), MFB) &&
311*9880d681SAndroid Build Coastguard Worker dyn_cast_or_null<MDString>(Flag->getOperand(1))) {
312*9880d681SAndroid Build Coastguard Worker // Check the operands of the MDNode before accessing the operands.
313*9880d681SAndroid Build Coastguard Worker // The verifier will actually catch these failures.
314*9880d681SAndroid Build Coastguard Worker MDString *Key = cast<MDString>(Flag->getOperand(1));
315*9880d681SAndroid Build Coastguard Worker Metadata *Val = Flag->getOperand(2);
316*9880d681SAndroid Build Coastguard Worker Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
317*9880d681SAndroid Build Coastguard Worker }
318*9880d681SAndroid Build Coastguard Worker }
319*9880d681SAndroid Build Coastguard Worker }
320*9880d681SAndroid Build Coastguard Worker
321*9880d681SAndroid Build Coastguard Worker /// Return the corresponding value if Key appears in module flags, otherwise
322*9880d681SAndroid Build Coastguard Worker /// return null.
getModuleFlag(StringRef Key) const323*9880d681SAndroid Build Coastguard Worker Metadata *Module::getModuleFlag(StringRef Key) const {
324*9880d681SAndroid Build Coastguard Worker SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
325*9880d681SAndroid Build Coastguard Worker getModuleFlagsMetadata(ModuleFlags);
326*9880d681SAndroid Build Coastguard Worker for (const ModuleFlagEntry &MFE : ModuleFlags) {
327*9880d681SAndroid Build Coastguard Worker if (Key == MFE.Key->getString())
328*9880d681SAndroid Build Coastguard Worker return MFE.Val;
329*9880d681SAndroid Build Coastguard Worker }
330*9880d681SAndroid Build Coastguard Worker return nullptr;
331*9880d681SAndroid Build Coastguard Worker }
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard Worker /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
334*9880d681SAndroid Build Coastguard Worker /// represents module-level flags. This method returns null if there are no
335*9880d681SAndroid Build Coastguard Worker /// module-level flags.
getModuleFlagsMetadata() const336*9880d681SAndroid Build Coastguard Worker NamedMDNode *Module::getModuleFlagsMetadata() const {
337*9880d681SAndroid Build Coastguard Worker return getNamedMetadata("llvm.module.flags");
338*9880d681SAndroid Build Coastguard Worker }
339*9880d681SAndroid Build Coastguard Worker
340*9880d681SAndroid Build Coastguard Worker /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
341*9880d681SAndroid Build Coastguard Worker /// represents module-level flags. If module-level flags aren't found, it
342*9880d681SAndroid Build Coastguard Worker /// creates the named metadata that contains them.
getOrInsertModuleFlagsMetadata()343*9880d681SAndroid Build Coastguard Worker NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
344*9880d681SAndroid Build Coastguard Worker return getOrInsertNamedMetadata("llvm.module.flags");
345*9880d681SAndroid Build Coastguard Worker }
346*9880d681SAndroid Build Coastguard Worker
347*9880d681SAndroid Build Coastguard Worker /// addModuleFlag - Add a module-level flag to the module-level flags
348*9880d681SAndroid Build Coastguard Worker /// metadata. It will create the module-level flags named metadata if it doesn't
349*9880d681SAndroid Build Coastguard Worker /// already exist.
addModuleFlag(ModFlagBehavior Behavior,StringRef Key,Metadata * Val)350*9880d681SAndroid Build Coastguard Worker void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
351*9880d681SAndroid Build Coastguard Worker Metadata *Val) {
352*9880d681SAndroid Build Coastguard Worker Type *Int32Ty = Type::getInt32Ty(Context);
353*9880d681SAndroid Build Coastguard Worker Metadata *Ops[3] = {
354*9880d681SAndroid Build Coastguard Worker ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
355*9880d681SAndroid Build Coastguard Worker MDString::get(Context, Key), Val};
356*9880d681SAndroid Build Coastguard Worker getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
357*9880d681SAndroid Build Coastguard Worker }
addModuleFlag(ModFlagBehavior Behavior,StringRef Key,Constant * Val)358*9880d681SAndroid Build Coastguard Worker void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
359*9880d681SAndroid Build Coastguard Worker Constant *Val) {
360*9880d681SAndroid Build Coastguard Worker addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
361*9880d681SAndroid Build Coastguard Worker }
addModuleFlag(ModFlagBehavior Behavior,StringRef Key,uint32_t Val)362*9880d681SAndroid Build Coastguard Worker void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
363*9880d681SAndroid Build Coastguard Worker uint32_t Val) {
364*9880d681SAndroid Build Coastguard Worker Type *Int32Ty = Type::getInt32Ty(Context);
365*9880d681SAndroid Build Coastguard Worker addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
366*9880d681SAndroid Build Coastguard Worker }
addModuleFlag(MDNode * Node)367*9880d681SAndroid Build Coastguard Worker void Module::addModuleFlag(MDNode *Node) {
368*9880d681SAndroid Build Coastguard Worker assert(Node->getNumOperands() == 3 &&
369*9880d681SAndroid Build Coastguard Worker "Invalid number of operands for module flag!");
370*9880d681SAndroid Build Coastguard Worker assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
371*9880d681SAndroid Build Coastguard Worker isa<MDString>(Node->getOperand(1)) &&
372*9880d681SAndroid Build Coastguard Worker "Invalid operand types for module flag!");
373*9880d681SAndroid Build Coastguard Worker getOrInsertModuleFlagsMetadata()->addOperand(Node);
374*9880d681SAndroid Build Coastguard Worker }
375*9880d681SAndroid Build Coastguard Worker
setDataLayout(StringRef Desc)376*9880d681SAndroid Build Coastguard Worker void Module::setDataLayout(StringRef Desc) {
377*9880d681SAndroid Build Coastguard Worker DL.reset(Desc);
378*9880d681SAndroid Build Coastguard Worker }
379*9880d681SAndroid Build Coastguard Worker
setDataLayout(const DataLayout & Other)380*9880d681SAndroid Build Coastguard Worker void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
381*9880d681SAndroid Build Coastguard Worker
getDataLayout() const382*9880d681SAndroid Build Coastguard Worker const DataLayout &Module::getDataLayout() const { return DL; }
383*9880d681SAndroid Build Coastguard Worker
operator *() const384*9880d681SAndroid Build Coastguard Worker DICompileUnit *Module::debug_compile_units_iterator::operator*() const {
385*9880d681SAndroid Build Coastguard Worker return cast<DICompileUnit>(CUs->getOperand(Idx));
386*9880d681SAndroid Build Coastguard Worker }
operator ->() const387*9880d681SAndroid Build Coastguard Worker DICompileUnit *Module::debug_compile_units_iterator::operator->() const {
388*9880d681SAndroid Build Coastguard Worker return cast<DICompileUnit>(CUs->getOperand(Idx));
389*9880d681SAndroid Build Coastguard Worker }
390*9880d681SAndroid Build Coastguard Worker
SkipNoDebugCUs()391*9880d681SAndroid Build Coastguard Worker void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
392*9880d681SAndroid Build Coastguard Worker while (CUs && (Idx < CUs->getNumOperands()) &&
393*9880d681SAndroid Build Coastguard Worker ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
394*9880d681SAndroid Build Coastguard Worker ++Idx;
395*9880d681SAndroid Build Coastguard Worker }
396*9880d681SAndroid Build Coastguard Worker
397*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
398*9880d681SAndroid Build Coastguard Worker // Methods to control the materialization of GlobalValues in the Module.
399*9880d681SAndroid Build Coastguard Worker //
setMaterializer(GVMaterializer * GVM)400*9880d681SAndroid Build Coastguard Worker void Module::setMaterializer(GVMaterializer *GVM) {
401*9880d681SAndroid Build Coastguard Worker assert(!Materializer &&
402*9880d681SAndroid Build Coastguard Worker "Module already has a GVMaterializer. Call materializeAll"
403*9880d681SAndroid Build Coastguard Worker " to clear it out before setting another one.");
404*9880d681SAndroid Build Coastguard Worker Materializer.reset(GVM);
405*9880d681SAndroid Build Coastguard Worker }
406*9880d681SAndroid Build Coastguard Worker
materialize(GlobalValue * GV)407*9880d681SAndroid Build Coastguard Worker std::error_code Module::materialize(GlobalValue *GV) {
408*9880d681SAndroid Build Coastguard Worker if (!Materializer)
409*9880d681SAndroid Build Coastguard Worker return std::error_code();
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker return Materializer->materialize(GV);
412*9880d681SAndroid Build Coastguard Worker }
413*9880d681SAndroid Build Coastguard Worker
materializeAll()414*9880d681SAndroid Build Coastguard Worker std::error_code Module::materializeAll() {
415*9880d681SAndroid Build Coastguard Worker if (!Materializer)
416*9880d681SAndroid Build Coastguard Worker return std::error_code();
417*9880d681SAndroid Build Coastguard Worker std::unique_ptr<GVMaterializer> M = std::move(Materializer);
418*9880d681SAndroid Build Coastguard Worker return M->materializeModule();
419*9880d681SAndroid Build Coastguard Worker }
420*9880d681SAndroid Build Coastguard Worker
materializeMetadata()421*9880d681SAndroid Build Coastguard Worker std::error_code Module::materializeMetadata() {
422*9880d681SAndroid Build Coastguard Worker if (!Materializer)
423*9880d681SAndroid Build Coastguard Worker return std::error_code();
424*9880d681SAndroid Build Coastguard Worker return Materializer->materializeMetadata();
425*9880d681SAndroid Build Coastguard Worker }
426*9880d681SAndroid Build Coastguard Worker
427*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
428*9880d681SAndroid Build Coastguard Worker // Other module related stuff.
429*9880d681SAndroid Build Coastguard Worker //
430*9880d681SAndroid Build Coastguard Worker
getIdentifiedStructTypes() const431*9880d681SAndroid Build Coastguard Worker std::vector<StructType *> Module::getIdentifiedStructTypes() const {
432*9880d681SAndroid Build Coastguard Worker // If we have a materializer, it is possible that some unread function
433*9880d681SAndroid Build Coastguard Worker // uses a type that is currently not visible to a TypeFinder, so ask
434*9880d681SAndroid Build Coastguard Worker // the materializer which types it created.
435*9880d681SAndroid Build Coastguard Worker if (Materializer)
436*9880d681SAndroid Build Coastguard Worker return Materializer->getIdentifiedStructTypes();
437*9880d681SAndroid Build Coastguard Worker
438*9880d681SAndroid Build Coastguard Worker std::vector<StructType *> Ret;
439*9880d681SAndroid Build Coastguard Worker TypeFinder SrcStructTypes;
440*9880d681SAndroid Build Coastguard Worker SrcStructTypes.run(*this, true);
441*9880d681SAndroid Build Coastguard Worker Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
442*9880d681SAndroid Build Coastguard Worker return Ret;
443*9880d681SAndroid Build Coastguard Worker }
444*9880d681SAndroid Build Coastguard Worker
445*9880d681SAndroid Build Coastguard Worker // dropAllReferences() - This function causes all the subelements to "let go"
446*9880d681SAndroid Build Coastguard Worker // of all references that they are maintaining. This allows one to 'delete' a
447*9880d681SAndroid Build Coastguard Worker // whole module at a time, even though there may be circular references... first
448*9880d681SAndroid Build Coastguard Worker // all references are dropped, and all use counts go to zero. Then everything
449*9880d681SAndroid Build Coastguard Worker // is deleted for real. Note that no operations are valid on an object that
450*9880d681SAndroid Build Coastguard Worker // has "dropped all references", except operator delete.
451*9880d681SAndroid Build Coastguard Worker //
dropAllReferences()452*9880d681SAndroid Build Coastguard Worker void Module::dropAllReferences() {
453*9880d681SAndroid Build Coastguard Worker for (Function &F : *this)
454*9880d681SAndroid Build Coastguard Worker F.dropAllReferences();
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard Worker for (GlobalVariable &GV : globals())
457*9880d681SAndroid Build Coastguard Worker GV.dropAllReferences();
458*9880d681SAndroid Build Coastguard Worker
459*9880d681SAndroid Build Coastguard Worker for (GlobalAlias &GA : aliases())
460*9880d681SAndroid Build Coastguard Worker GA.dropAllReferences();
461*9880d681SAndroid Build Coastguard Worker
462*9880d681SAndroid Build Coastguard Worker for (GlobalIFunc &GIF : ifuncs())
463*9880d681SAndroid Build Coastguard Worker GIF.dropAllReferences();
464*9880d681SAndroid Build Coastguard Worker }
465*9880d681SAndroid Build Coastguard Worker
getDwarfVersion() const466*9880d681SAndroid Build Coastguard Worker unsigned Module::getDwarfVersion() const {
467*9880d681SAndroid Build Coastguard Worker auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
468*9880d681SAndroid Build Coastguard Worker if (!Val)
469*9880d681SAndroid Build Coastguard Worker return 0;
470*9880d681SAndroid Build Coastguard Worker return cast<ConstantInt>(Val->getValue())->getZExtValue();
471*9880d681SAndroid Build Coastguard Worker }
472*9880d681SAndroid Build Coastguard Worker
getCodeViewFlag() const473*9880d681SAndroid Build Coastguard Worker unsigned Module::getCodeViewFlag() const {
474*9880d681SAndroid Build Coastguard Worker auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
475*9880d681SAndroid Build Coastguard Worker if (!Val)
476*9880d681SAndroid Build Coastguard Worker return 0;
477*9880d681SAndroid Build Coastguard Worker return cast<ConstantInt>(Val->getValue())->getZExtValue();
478*9880d681SAndroid Build Coastguard Worker }
479*9880d681SAndroid Build Coastguard Worker
getOrInsertComdat(StringRef Name)480*9880d681SAndroid Build Coastguard Worker Comdat *Module::getOrInsertComdat(StringRef Name) {
481*9880d681SAndroid Build Coastguard Worker auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
482*9880d681SAndroid Build Coastguard Worker Entry.second.Name = &Entry;
483*9880d681SAndroid Build Coastguard Worker return &Entry.second;
484*9880d681SAndroid Build Coastguard Worker }
485*9880d681SAndroid Build Coastguard Worker
getPICLevel() const486*9880d681SAndroid Build Coastguard Worker PICLevel::Level Module::getPICLevel() const {
487*9880d681SAndroid Build Coastguard Worker auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
488*9880d681SAndroid Build Coastguard Worker
489*9880d681SAndroid Build Coastguard Worker if (!Val)
490*9880d681SAndroid Build Coastguard Worker return PICLevel::NotPIC;
491*9880d681SAndroid Build Coastguard Worker
492*9880d681SAndroid Build Coastguard Worker return static_cast<PICLevel::Level>(
493*9880d681SAndroid Build Coastguard Worker cast<ConstantInt>(Val->getValue())->getZExtValue());
494*9880d681SAndroid Build Coastguard Worker }
495*9880d681SAndroid Build Coastguard Worker
setPICLevel(PICLevel::Level PL)496*9880d681SAndroid Build Coastguard Worker void Module::setPICLevel(PICLevel::Level PL) {
497*9880d681SAndroid Build Coastguard Worker addModuleFlag(ModFlagBehavior::Error, "PIC Level", PL);
498*9880d681SAndroid Build Coastguard Worker }
499*9880d681SAndroid Build Coastguard Worker
getPIELevel() const500*9880d681SAndroid Build Coastguard Worker PIELevel::Level Module::getPIELevel() const {
501*9880d681SAndroid Build Coastguard Worker auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIE Level"));
502*9880d681SAndroid Build Coastguard Worker
503*9880d681SAndroid Build Coastguard Worker if (!Val)
504*9880d681SAndroid Build Coastguard Worker return PIELevel::Default;
505*9880d681SAndroid Build Coastguard Worker
506*9880d681SAndroid Build Coastguard Worker return static_cast<PIELevel::Level>(
507*9880d681SAndroid Build Coastguard Worker cast<ConstantInt>(Val->getValue())->getZExtValue());
508*9880d681SAndroid Build Coastguard Worker }
509*9880d681SAndroid Build Coastguard Worker
setPIELevel(PIELevel::Level PL)510*9880d681SAndroid Build Coastguard Worker void Module::setPIELevel(PIELevel::Level PL) {
511*9880d681SAndroid Build Coastguard Worker addModuleFlag(ModFlagBehavior::Error, "PIE Level", PL);
512*9880d681SAndroid Build Coastguard Worker }
513*9880d681SAndroid Build Coastguard Worker
setProfileSummary(Metadata * M)514*9880d681SAndroid Build Coastguard Worker void Module::setProfileSummary(Metadata *M) {
515*9880d681SAndroid Build Coastguard Worker addModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M);
516*9880d681SAndroid Build Coastguard Worker }
517*9880d681SAndroid Build Coastguard Worker
getProfileSummary()518*9880d681SAndroid Build Coastguard Worker Metadata *Module::getProfileSummary() {
519*9880d681SAndroid Build Coastguard Worker return getModuleFlag("ProfileSummary");
520*9880d681SAndroid Build Coastguard Worker }
521*9880d681SAndroid Build Coastguard Worker
collectUsedGlobalVariables(const Module & M,SmallPtrSetImpl<GlobalValue * > & Set,bool CompilerUsed)522*9880d681SAndroid Build Coastguard Worker GlobalVariable *llvm::collectUsedGlobalVariables(
523*9880d681SAndroid Build Coastguard Worker const Module &M, SmallPtrSetImpl<GlobalValue *> &Set, bool CompilerUsed) {
524*9880d681SAndroid Build Coastguard Worker const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
525*9880d681SAndroid Build Coastguard Worker GlobalVariable *GV = M.getGlobalVariable(Name);
526*9880d681SAndroid Build Coastguard Worker if (!GV || !GV->hasInitializer())
527*9880d681SAndroid Build Coastguard Worker return GV;
528*9880d681SAndroid Build Coastguard Worker
529*9880d681SAndroid Build Coastguard Worker const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
530*9880d681SAndroid Build Coastguard Worker for (Value *Op : Init->operands()) {
531*9880d681SAndroid Build Coastguard Worker GlobalValue *G = cast<GlobalValue>(Op->stripPointerCastsNoFollowAliases());
532*9880d681SAndroid Build Coastguard Worker Set.insert(G);
533*9880d681SAndroid Build Coastguard Worker }
534*9880d681SAndroid Build Coastguard Worker return GV;
535*9880d681SAndroid Build Coastguard Worker }
536