xref: /llvm-project-15.0.7/llvm/lib/IR/Module.cpp (revision 01909b4e)
1eba7e4ecSEugene Zelenko //===- Module.cpp - Implement the Module class ----------------------------===//
2ef860a24SChandler Carruth //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ef860a24SChandler Carruth //
7ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
8ef860a24SChandler Carruth //
9ef860a24SChandler Carruth // This file implements the Module class for the IR library.
10ef860a24SChandler Carruth //
11ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
12ef860a24SChandler Carruth 
136bda14b3SChandler Carruth #include "llvm/IR/Module.h"
14ef860a24SChandler Carruth #include "SymbolTableListTraitsImpl.h"
153dea3f9eSCaroline Tice #include "llvm/ADT/Optional.h"
16b35cc691STeresa Johnson #include "llvm/ADT/SmallPtrSet.h"
17ef860a24SChandler Carruth #include "llvm/ADT/SmallString.h"
18eba7e4ecSEugene Zelenko #include "llvm/ADT/SmallVector.h"
19eba7e4ecSEugene Zelenko #include "llvm/ADT/StringMap.h"
20eba7e4ecSEugene Zelenko #include "llvm/ADT/StringRef.h"
21eba7e4ecSEugene Zelenko #include "llvm/ADT/Twine.h"
22eba7e4ecSEugene Zelenko #include "llvm/IR/Attributes.h"
23eba7e4ecSEugene Zelenko #include "llvm/IR/Comdat.h"
249fb823bbSChandler Carruth #include "llvm/IR/Constants.h"
25eba7e4ecSEugene Zelenko #include "llvm/IR/DataLayout.h"
265992a72bSAdrian Prantl #include "llvm/IR/DebugInfoMetadata.h"
276bda14b3SChandler Carruth #include "llvm/IR/DerivedTypes.h"
28eba7e4ecSEugene Zelenko #include "llvm/IR/Function.h"
296bda14b3SChandler Carruth #include "llvm/IR/GVMaterializer.h"
30eba7e4ecSEugene Zelenko #include "llvm/IR/GlobalAlias.h"
31eba7e4ecSEugene Zelenko #include "llvm/IR/GlobalIFunc.h"
32eba7e4ecSEugene Zelenko #include "llvm/IR/GlobalValue.h"
33eba7e4ecSEugene Zelenko #include "llvm/IR/GlobalVariable.h"
349fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h"
35eba7e4ecSEugene Zelenko #include "llvm/IR/Metadata.h"
36eba7e4ecSEugene Zelenko #include "llvm/IR/SymbolTableListTraits.h"
37eba7e4ecSEugene Zelenko #include "llvm/IR/Type.h"
382fa1e43aSRafael Espindola #include "llvm/IR/TypeFinder.h"
39eba7e4ecSEugene Zelenko #include "llvm/IR/Value.h"
40eba7e4ecSEugene Zelenko #include "llvm/IR/ValueSymbolTable.h"
41eba7e4ecSEugene Zelenko #include "llvm/Pass.h"
42eba7e4ecSEugene Zelenko #include "llvm/Support/Casting.h"
43eba7e4ecSEugene Zelenko #include "llvm/Support/CodeGen.h"
447f00d0a1SPeter Collingbourne #include "llvm/Support/Error.h"
45e2dcf7c3SPeter Collingbourne #include "llvm/Support/MemoryBuffer.h"
46144829d3SJF Bastien #include "llvm/Support/Path.h"
47144829d3SJF Bastien #include "llvm/Support/RandomNumberGenerator.h"
48afa75d78SAlex Lorenz #include "llvm/Support/VersionTuple.h"
49ef860a24SChandler Carruth #include <algorithm>
50eba7e4ecSEugene Zelenko #include <cassert>
51eba7e4ecSEugene Zelenko #include <cstdint>
52eba7e4ecSEugene Zelenko #include <memory>
53eba7e4ecSEugene Zelenko #include <utility>
54eba7e4ecSEugene Zelenko #include <vector>
55083ca9bbSHans Wennborg 
56ef860a24SChandler Carruth using namespace llvm;
57ef860a24SChandler Carruth 
58ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
59ef860a24SChandler Carruth // Methods to implement the globals and functions lists.
60ef860a24SChandler Carruth //
61ef860a24SChandler Carruth 
62ef860a24SChandler Carruth // Explicit instantiations of SymbolTableListTraits since some of the methods
63ef860a24SChandler Carruth // are not in the public header file.
6437bf678aSDuncan P. N. Exon Smith template class llvm::SymbolTableListTraits<Function>;
6537bf678aSDuncan P. N. Exon Smith template class llvm::SymbolTableListTraits<GlobalVariable>;
6637bf678aSDuncan P. N. Exon Smith template class llvm::SymbolTableListTraits<GlobalAlias>;
67a1feff70SDmitry Polukhin template class llvm::SymbolTableListTraits<GlobalIFunc>;
68ef860a24SChandler Carruth 
69ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
70ef860a24SChandler Carruth // Primitive Module methods.
71ef860a24SChandler Carruth //
72ef860a24SChandler Carruth 
73ef860a24SChandler Carruth Module::Module(StringRef MID, LLVMContext &C)
7446ed9331SDavid Blaikie     : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>()),
75adcd0268SBenjamin Kramer       Materializer(), ModuleID(std::string(MID)),
76adcd0268SBenjamin Kramer       SourceFileName(std::string(MID)), DL("") {
77ef860a24SChandler Carruth   Context.addModule(this);
78ef860a24SChandler Carruth }
79ef860a24SChandler Carruth 
80ef860a24SChandler Carruth Module::~Module() {
81ef860a24SChandler Carruth   Context.removeModule(this);
82ef860a24SChandler Carruth   dropAllReferences();
83ef860a24SChandler Carruth   GlobalList.clear();
84ef860a24SChandler Carruth   FunctionList.clear();
85ef860a24SChandler Carruth   AliasList.clear();
86a1feff70SDmitry Polukhin   IFuncList.clear();
87ef860a24SChandler Carruth }
88ef860a24SChandler Carruth 
8973713f3eSDominic Chen std::unique_ptr<RandomNumberGenerator>
9073713f3eSDominic Chen Module::createRNG(const StringRef Name) const {
9173713f3eSDominic Chen   SmallString<32> Salt(Name);
92e6acbdc4SJF Bastien 
93e6acbdc4SJF Bastien   // This RNG is guaranteed to produce the same random stream only
94e6acbdc4SJF Bastien   // when the Module ID and thus the input filename is the same. This
95e6acbdc4SJF Bastien   // might be problematic if the input filename extension changes
96e6acbdc4SJF Bastien   // (e.g. from .c to .bc or .ll).
97e6acbdc4SJF Bastien   //
98e6acbdc4SJF Bastien   // We could store this salt in NamedMetadata, but this would make
99e6acbdc4SJF Bastien   // the parameter non-const. This would unfortunately make this
100e6acbdc4SJF Bastien   // interface unusable by any Machine passes, since they only have a
101e6acbdc4SJF Bastien   // const reference to their IR Module. Alternatively we can always
102e6acbdc4SJF Bastien   // store salt metadata from the Module constructor.
103e6acbdc4SJF Bastien   Salt += sys::path::filename(getModuleIdentifier());
104e6acbdc4SJF Bastien 
10573713f3eSDominic Chen   return std::unique_ptr<RandomNumberGenerator>(
10673713f3eSDominic Chen       new RandomNumberGenerator(Salt));
107e6acbdc4SJF Bastien }
108e6acbdc4SJF Bastien 
109ef860a24SChandler Carruth /// getNamedValue - Return the first global value in the module with
110ef860a24SChandler Carruth /// the specified name, of arbitrary type.  This method returns null
111ef860a24SChandler Carruth /// if a global with the specified name is not found.
112ef860a24SChandler Carruth GlobalValue *Module::getNamedValue(StringRef Name) const {
113ef860a24SChandler Carruth   return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
114ef860a24SChandler Carruth }
115ef860a24SChandler Carruth 
116ef860a24SChandler Carruth /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
117ef860a24SChandler Carruth /// This ID is uniqued across modules in the current LLVMContext.
118ef860a24SChandler Carruth unsigned Module::getMDKindID(StringRef Name) const {
119ef860a24SChandler Carruth   return Context.getMDKindID(Name);
120ef860a24SChandler Carruth }
121ef860a24SChandler Carruth 
122ef860a24SChandler Carruth /// getMDKindNames - Populate client supplied SmallVector with the name for
123ef860a24SChandler Carruth /// custom metadata IDs registered in this LLVMContext.   ID #0 is not used,
124ef860a24SChandler Carruth /// so it is filled in as an empty string.
125ef860a24SChandler Carruth void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
126ef860a24SChandler Carruth   return Context.getMDKindNames(Result);
127ef860a24SChandler Carruth }
128ef860a24SChandler Carruth 
1299303c246SSanjoy Das void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const {
1309303c246SSanjoy Das   return Context.getOperandBundleTags(Result);
1319303c246SSanjoy Das }
132ef860a24SChandler Carruth 
133ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
134ef860a24SChandler Carruth // Methods for easy access to the functions in the module.
135ef860a24SChandler Carruth //
136ef860a24SChandler Carruth 
137ef860a24SChandler Carruth // getOrInsertFunction - Look up the specified function in the module symbol
138ef860a24SChandler Carruth // table.  If it does not exist, add a prototype for the function and return
139ef860a24SChandler Carruth // it.  This is nice because it allows most passes to get away with not handling
140ef860a24SChandler Carruth // the symbol table directly for this common task.
141ef860a24SChandler Carruth //
14213680223SJames Y Knight FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
143b518054bSReid Kleckner                                            AttributeList AttributeList) {
144ef860a24SChandler Carruth   // See if we have a definition for the specified function already.
145ef860a24SChandler Carruth   GlobalValue *F = getNamedValue(Name);
146c620761cSCraig Topper   if (!F) {
147ef860a24SChandler Carruth     // Nope, add it
1486bcf2ba2SAlexander Richardson     Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage,
1496bcf2ba2SAlexander Richardson                                      DL.getProgramAddressSpace(), Name);
150ef860a24SChandler Carruth     if (!New->isIntrinsic())       // Intrinsics get attrs set on construction
151ef860a24SChandler Carruth       New->setAttributes(AttributeList);
152ef860a24SChandler Carruth     FunctionList.push_back(New);
15313680223SJames Y Knight     return {Ty, New}; // Return the new prototype.
154ef860a24SChandler Carruth   }
155ef860a24SChandler Carruth 
156ef860a24SChandler Carruth   // If the function exists but has the wrong type, return a bitcast to the
157ef860a24SChandler Carruth   // right type.
1586bcf2ba2SAlexander Richardson   auto *PTy = PointerType::get(Ty, F->getAddressSpace());
1596bcf2ba2SAlexander Richardson   if (F->getType() != PTy)
16013680223SJames Y Knight     return {Ty, ConstantExpr::getBitCast(F, PTy)};
161ef860a24SChandler Carruth 
162ef860a24SChandler Carruth   // Otherwise, we just found the existing function or a prototype.
16313680223SJames Y Knight   return {Ty, F};
164ef860a24SChandler Carruth }
165ef860a24SChandler Carruth 
16613680223SJames Y Knight FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) {
167b518054bSReid Kleckner   return getOrInsertFunction(Name, Ty, AttributeList());
168ef860a24SChandler Carruth }
169ef860a24SChandler Carruth 
170ef860a24SChandler Carruth // getFunction - Look up the specified function in the module symbol table.
171ef860a24SChandler Carruth // If it does not exist, return null.
172ef860a24SChandler Carruth //
173ef860a24SChandler Carruth Function *Module::getFunction(StringRef Name) const {
174ef860a24SChandler Carruth   return dyn_cast_or_null<Function>(getNamedValue(Name));
175ef860a24SChandler Carruth }
176ef860a24SChandler Carruth 
177ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
178ef860a24SChandler Carruth // Methods for easy access to the global variables in the module.
179ef860a24SChandler Carruth //
180ef860a24SChandler Carruth 
181ef860a24SChandler Carruth /// getGlobalVariable - Look up the specified global variable in the module
182ef860a24SChandler Carruth /// symbol table.  If it does not exist, return null.  The type argument
183ef860a24SChandler Carruth /// should be the underlying type of the global, i.e., it should not have
184ef860a24SChandler Carruth /// the top-level PointerType, which represents the address of the global.
185ef860a24SChandler Carruth /// If AllowLocal is set to true, this function will return types that
186ef860a24SChandler Carruth /// have an local. By default, these types are not returned.
187ef860a24SChandler Carruth ///
1881dd20e65SCraig Topper GlobalVariable *Module::getGlobalVariable(StringRef Name,
1891dd20e65SCraig Topper                                           bool AllowLocal) const {
190ef860a24SChandler Carruth   if (GlobalVariable *Result =
191ef860a24SChandler Carruth       dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
192ef860a24SChandler Carruth     if (AllowLocal || !Result->hasLocalLinkage())
193ef860a24SChandler Carruth       return Result;
194c620761cSCraig Topper   return nullptr;
195ef860a24SChandler Carruth }
196ef860a24SChandler Carruth 
197ef860a24SChandler Carruth /// getOrInsertGlobal - Look up the specified global in the module symbol table.
198ef860a24SChandler Carruth ///   1. If it does not exist, add a declaration of the global and return it.
199ef860a24SChandler Carruth ///   2. Else, the global exists but has the wrong type: return the function
200ef860a24SChandler Carruth ///      with a constantexpr cast to the right type.
2015200fdf0SMatt Arsenault ///   3. Finally, if the existing global is the correct declaration, return the
202ef860a24SChandler Carruth ///      existing global.
2036bc98ad7SPhilip Pfaffe Constant *Module::getOrInsertGlobal(
2046bc98ad7SPhilip Pfaffe     StringRef Name, Type *Ty,
2056bc98ad7SPhilip Pfaffe     function_ref<GlobalVariable *()> CreateGlobalCallback) {
206ef860a24SChandler Carruth   // See if we have a definition for the specified global already.
207ef860a24SChandler Carruth   GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
2086bc98ad7SPhilip Pfaffe   if (!GV)
2096bc98ad7SPhilip Pfaffe     GV = CreateGlobalCallback();
2106bc98ad7SPhilip Pfaffe   assert(GV && "The CreateGlobalCallback is expected to create a global");
211ef860a24SChandler Carruth 
212ef860a24SChandler Carruth   // If the variable exists but has the wrong type, return a bitcast to the
213ef860a24SChandler Carruth   // right type.
21427e783e9SMatt Arsenault   Type *GVTy = GV->getType();
21527e783e9SMatt Arsenault   PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
216a90a340fSMatt Arsenault   if (GVTy != PTy)
21727e783e9SMatt Arsenault     return ConstantExpr::getBitCast(GV, PTy);
218ef860a24SChandler Carruth 
219ef860a24SChandler Carruth   // Otherwise, we just found the existing function or a prototype.
220ef860a24SChandler Carruth   return GV;
221ef860a24SChandler Carruth }
222ef860a24SChandler Carruth 
2236bc98ad7SPhilip Pfaffe // Overload to construct a global variable using its constructor's defaults.
2246bc98ad7SPhilip Pfaffe Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
2256bc98ad7SPhilip Pfaffe   return getOrInsertGlobal(Name, Ty, [&] {
2266bc98ad7SPhilip Pfaffe     return new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
2276bc98ad7SPhilip Pfaffe                               nullptr, Name);
2286bc98ad7SPhilip Pfaffe   });
2296bc98ad7SPhilip Pfaffe }
2306bc98ad7SPhilip Pfaffe 
231ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
232ef860a24SChandler Carruth // Methods for easy access to the global variables in the module.
233ef860a24SChandler Carruth //
234ef860a24SChandler Carruth 
235ef860a24SChandler Carruth // getNamedAlias - Look up the specified global in the module symbol table.
236ef860a24SChandler Carruth // If it does not exist, return null.
237ef860a24SChandler Carruth //
238ef860a24SChandler Carruth GlobalAlias *Module::getNamedAlias(StringRef Name) const {
239ef860a24SChandler Carruth   return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
240ef860a24SChandler Carruth }
241ef860a24SChandler Carruth 
242a1feff70SDmitry Polukhin GlobalIFunc *Module::getNamedIFunc(StringRef Name) const {
243a1feff70SDmitry Polukhin   return dyn_cast_or_null<GlobalIFunc>(getNamedValue(Name));
244a1feff70SDmitry Polukhin }
245a1feff70SDmitry Polukhin 
246ef860a24SChandler Carruth /// getNamedMetadata - Return the first NamedMDNode in the module with the
247ef860a24SChandler Carruth /// specified name. This method returns null if a NamedMDNode with the
248ef860a24SChandler Carruth /// specified name is not found.
249ef860a24SChandler Carruth NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
250ef860a24SChandler Carruth   SmallString<256> NameData;
251ef860a24SChandler Carruth   StringRef NameRef = Name.toStringRef(NameData);
252daab9227SBrian Gesiak   return NamedMDSymTab.lookup(NameRef);
253ef860a24SChandler Carruth }
254ef860a24SChandler Carruth 
255ef860a24SChandler Carruth /// getOrInsertNamedMetadata - Return the first named MDNode in the module
256ef860a24SChandler Carruth /// with the specified name. This method returns a new NamedMDNode if a
257ef860a24SChandler Carruth /// NamedMDNode with the specified name is not found.
258ef860a24SChandler Carruth NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
259daab9227SBrian Gesiak   NamedMDNode *&NMD = NamedMDSymTab[Name];
260ef860a24SChandler Carruth   if (!NMD) {
261ef860a24SChandler Carruth     NMD = new NamedMDNode(Name);
262ef860a24SChandler Carruth     NMD->setParent(this);
263ef860a24SChandler Carruth     NamedMDList.push_back(NMD);
264ef860a24SChandler Carruth   }
265ef860a24SChandler Carruth   return NMD;
266ef860a24SChandler Carruth }
267ef860a24SChandler Carruth 
268ef860a24SChandler Carruth /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
269ef860a24SChandler Carruth /// delete it.
270ef860a24SChandler Carruth void Module::eraseNamedMetadata(NamedMDNode *NMD) {
271daab9227SBrian Gesiak   NamedMDSymTab.erase(NMD->getName());
27252888a67SDuncan P. N. Exon Smith   NamedMDList.erase(NMD->getIterator());
273ef860a24SChandler Carruth }
274ef860a24SChandler Carruth 
2755bf8fef5SDuncan P. N. Exon Smith bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
276d7677e7aSDavid Majnemer   if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
277af023adbSAlexey Samsonov     uint64_t Val = Behavior->getLimitedValue();
278af023adbSAlexey Samsonov     if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
279af023adbSAlexey Samsonov       MFB = static_cast<ModFlagBehavior>(Val);
280af023adbSAlexey Samsonov       return true;
281af023adbSAlexey Samsonov     }
282af023adbSAlexey Samsonov   }
283af023adbSAlexey Samsonov   return false;
284af023adbSAlexey Samsonov }
285af023adbSAlexey Samsonov 
286*01909b4eSHiroshi Yamauchi bool Module::isValidModuleFlag(const MDNode &ModFlag, ModFlagBehavior &MFB,
287*01909b4eSHiroshi Yamauchi                                MDString *&Key, Metadata *&Val) {
288*01909b4eSHiroshi Yamauchi   if (ModFlag.getNumOperands() < 3)
289*01909b4eSHiroshi Yamauchi     return false;
290*01909b4eSHiroshi Yamauchi   if (!isValidModFlagBehavior(ModFlag.getOperand(0), MFB))
291*01909b4eSHiroshi Yamauchi     return false;
292*01909b4eSHiroshi Yamauchi   MDString *K = dyn_cast_or_null<MDString>(ModFlag.getOperand(1));
293*01909b4eSHiroshi Yamauchi   if (!K)
294*01909b4eSHiroshi Yamauchi     return false;
295*01909b4eSHiroshi Yamauchi   Key = K;
296*01909b4eSHiroshi Yamauchi   Val = ModFlag.getOperand(2);
297*01909b4eSHiroshi Yamauchi   return true;
298*01909b4eSHiroshi Yamauchi }
299*01909b4eSHiroshi Yamauchi 
300ef860a24SChandler Carruth /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
301ef860a24SChandler Carruth void Module::
302ef860a24SChandler Carruth getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
303ef860a24SChandler Carruth   const NamedMDNode *ModFlags = getModuleFlagsMetadata();
304ef860a24SChandler Carruth   if (!ModFlags) return;
305ef860a24SChandler Carruth 
306de36e804SDuncan P. N. Exon Smith   for (const MDNode *Flag : ModFlags->operands()) {
307af023adbSAlexey Samsonov     ModFlagBehavior MFB;
308*01909b4eSHiroshi Yamauchi     MDString *Key = nullptr;
309*01909b4eSHiroshi Yamauchi     Metadata *Val = nullptr;
310*01909b4eSHiroshi Yamauchi     if (isValidModuleFlag(*Flag, MFB, Key, Val)) {
3118b4306ceSManman Ren       // Check the operands of the MDNode before accessing the operands.
3128b4306ceSManman Ren       // The verifier will actually catch these failures.
313af023adbSAlexey Samsonov       Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
314ef860a24SChandler Carruth     }
315ef860a24SChandler Carruth   }
3168b4306ceSManman Ren }
317ef860a24SChandler Carruth 
3188bfde891SManman Ren /// Return the corresponding value if Key appears in module flags, otherwise
3198bfde891SManman Ren /// return null.
3205bf8fef5SDuncan P. N. Exon Smith Metadata *Module::getModuleFlag(StringRef Key) const {
3218bfde891SManman Ren   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
3228bfde891SManman Ren   getModuleFlagsMetadata(ModuleFlags);
3233ad5c962SBenjamin Kramer   for (const ModuleFlagEntry &MFE : ModuleFlags) {
3248bfde891SManman Ren     if (Key == MFE.Key->getString())
3258bfde891SManman Ren       return MFE.Val;
3268bfde891SManman Ren   }
327c620761cSCraig Topper   return nullptr;
3288bfde891SManman Ren }
3298bfde891SManman Ren 
330ef860a24SChandler Carruth /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
331ef860a24SChandler Carruth /// represents module-level flags. This method returns null if there are no
332ef860a24SChandler Carruth /// module-level flags.
333ef860a24SChandler Carruth NamedMDNode *Module::getModuleFlagsMetadata() const {
334ef860a24SChandler Carruth   return getNamedMetadata("llvm.module.flags");
335ef860a24SChandler Carruth }
336ef860a24SChandler Carruth 
337ef860a24SChandler Carruth /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
338ef860a24SChandler Carruth /// represents module-level flags. If module-level flags aren't found, it
339ef860a24SChandler Carruth /// creates the named metadata that contains them.
340ef860a24SChandler Carruth NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
341ef860a24SChandler Carruth   return getOrInsertNamedMetadata("llvm.module.flags");
342ef860a24SChandler Carruth }
343ef860a24SChandler Carruth 
344ef860a24SChandler Carruth /// addModuleFlag - Add a module-level flag to the module-level flags
345ef860a24SChandler Carruth /// metadata. It will create the module-level flags named metadata if it doesn't
346ef860a24SChandler Carruth /// already exist.
347ef860a24SChandler Carruth void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
3485bf8fef5SDuncan P. N. Exon Smith                            Metadata *Val) {
349ef860a24SChandler Carruth   Type *Int32Ty = Type::getInt32Ty(Context);
3505bf8fef5SDuncan P. N. Exon Smith   Metadata *Ops[3] = {
3515bf8fef5SDuncan P. N. Exon Smith       ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
3525bf8fef5SDuncan P. N. Exon Smith       MDString::get(Context, Key), Val};
353ef860a24SChandler Carruth   getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
354ef860a24SChandler Carruth }
355ef860a24SChandler Carruth void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
3565bf8fef5SDuncan P. N. Exon Smith                            Constant *Val) {
3575bf8fef5SDuncan P. N. Exon Smith   addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
3585bf8fef5SDuncan P. N. Exon Smith }
3595bf8fef5SDuncan P. N. Exon Smith void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
360ef860a24SChandler Carruth                            uint32_t Val) {
361ef860a24SChandler Carruth   Type *Int32Ty = Type::getInt32Ty(Context);
362ef860a24SChandler Carruth   addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
363ef860a24SChandler Carruth }
364ef860a24SChandler Carruth void Module::addModuleFlag(MDNode *Node) {
365ef860a24SChandler Carruth   assert(Node->getNumOperands() == 3 &&
366ef860a24SChandler Carruth          "Invalid number of operands for module flag!");
3675bf8fef5SDuncan P. N. Exon Smith   assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
368ef860a24SChandler Carruth          isa<MDString>(Node->getOperand(1)) &&
369ef860a24SChandler Carruth          "Invalid operand types for module flag!");
370ef860a24SChandler Carruth   getOrInsertModuleFlagsMetadata()->addOperand(Node);
371ef860a24SChandler Carruth }
372ef860a24SChandler Carruth 
373*01909b4eSHiroshi Yamauchi void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
374*01909b4eSHiroshi Yamauchi                            Metadata *Val) {
375*01909b4eSHiroshi Yamauchi   NamedMDNode *ModFlags = getOrInsertModuleFlagsMetadata();
376*01909b4eSHiroshi Yamauchi   // Replace the flag if it already exists.
377*01909b4eSHiroshi Yamauchi   for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
378*01909b4eSHiroshi Yamauchi     MDNode *Flag = ModFlags->getOperand(I);
379*01909b4eSHiroshi Yamauchi     ModFlagBehavior MFB;
380*01909b4eSHiroshi Yamauchi     MDString *K = nullptr;
381*01909b4eSHiroshi Yamauchi     Metadata *V = nullptr;
382*01909b4eSHiroshi Yamauchi     if (isValidModuleFlag(*Flag, MFB, K, V) && K->getString() == Key) {
383*01909b4eSHiroshi Yamauchi       Flag->replaceOperandWith(2, Val);
384*01909b4eSHiroshi Yamauchi       return;
385*01909b4eSHiroshi Yamauchi     }
386*01909b4eSHiroshi Yamauchi   }
387*01909b4eSHiroshi Yamauchi   addModuleFlag(Behavior, Key, Val);
388*01909b4eSHiroshi Yamauchi }
389*01909b4eSHiroshi Yamauchi 
390f863ee29SRafael Espindola void Module::setDataLayout(StringRef Desc) {
391248ac139SRafael Espindola   DL.reset(Desc);
392f863ee29SRafael Espindola }
393f863ee29SRafael Espindola 
39446a43556SMehdi Amini void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
395f863ee29SRafael Espindola 
39646a43556SMehdi Amini const DataLayout &Module::getDataLayout() const { return DL; }
397f863ee29SRafael Espindola 
3985992a72bSAdrian Prantl DICompileUnit *Module::debug_compile_units_iterator::operator*() const {
3995992a72bSAdrian Prantl   return cast<DICompileUnit>(CUs->getOperand(Idx));
4005992a72bSAdrian Prantl }
4015992a72bSAdrian Prantl DICompileUnit *Module::debug_compile_units_iterator::operator->() const {
4025992a72bSAdrian Prantl   return cast<DICompileUnit>(CUs->getOperand(Idx));
4035992a72bSAdrian Prantl }
4045992a72bSAdrian Prantl 
4055992a72bSAdrian Prantl void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
4065992a72bSAdrian Prantl   while (CUs && (Idx < CUs->getNumOperands()) &&
4075992a72bSAdrian Prantl          ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
4085992a72bSAdrian Prantl     ++Idx;
4095992a72bSAdrian Prantl }
4105992a72bSAdrian Prantl 
411285cf9a8SReid Kleckner iterator_range<Module::global_object_iterator> Module::global_objects() {
412285cf9a8SReid Kleckner   return concat<GlobalObject>(functions(), globals());
413285cf9a8SReid Kleckner }
414285cf9a8SReid Kleckner iterator_range<Module::const_global_object_iterator>
415285cf9a8SReid Kleckner Module::global_objects() const {
416285cf9a8SReid Kleckner   return concat<const GlobalObject>(functions(), globals());
417285cf9a8SReid Kleckner }
418285cf9a8SReid Kleckner 
419285cf9a8SReid Kleckner iterator_range<Module::global_value_iterator> Module::global_values() {
420285cf9a8SReid Kleckner   return concat<GlobalValue>(functions(), globals(), aliases(), ifuncs());
421285cf9a8SReid Kleckner }
422285cf9a8SReid Kleckner iterator_range<Module::const_global_value_iterator>
423285cf9a8SReid Kleckner Module::global_values() const {
424285cf9a8SReid Kleckner   return concat<const GlobalValue>(functions(), globals(), aliases(), ifuncs());
425285cf9a8SReid Kleckner }
426285cf9a8SReid Kleckner 
427ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
428ef860a24SChandler Carruth // Methods to control the materialization of GlobalValues in the Module.
429ef860a24SChandler Carruth //
430ef860a24SChandler Carruth void Module::setMaterializer(GVMaterializer *GVM) {
431ef860a24SChandler Carruth   assert(!Materializer &&
432c4a03483SRafael Espindola          "Module already has a GVMaterializer.  Call materializeAll"
433ef860a24SChandler Carruth          " to clear it out before setting another one.");
434ef860a24SChandler Carruth   Materializer.reset(GVM);
435ef860a24SChandler Carruth }
436ef860a24SChandler Carruth 
4377f00d0a1SPeter Collingbourne Error Module::materialize(GlobalValue *GV) {
4382b11ad4fSRafael Espindola   if (!Materializer)
4397f00d0a1SPeter Collingbourne     return Error::success();
4402b11ad4fSRafael Espindola 
4415a52e6dcSRafael Espindola   return Materializer->materialize(GV);
442ef860a24SChandler Carruth }
443ef860a24SChandler Carruth 
4447f00d0a1SPeter Collingbourne Error Module::materializeAll() {
445ef860a24SChandler Carruth   if (!Materializer)
4467f00d0a1SPeter Collingbourne     return Error::success();
447257a3536SRafael Espindola   std::unique_ptr<GVMaterializer> M = std::move(Materializer);
448257a3536SRafael Espindola   return M->materializeModule();
449ef860a24SChandler Carruth }
450ef860a24SChandler Carruth 
4517f00d0a1SPeter Collingbourne Error Module::materializeMetadata() {
452cba833a0SRafael Espindola   if (!Materializer)
4537f00d0a1SPeter Collingbourne     return Error::success();
454cba833a0SRafael Espindola   return Materializer->materializeMetadata();
455cba833a0SRafael Espindola }
456cba833a0SRafael Espindola 
457ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
458ef860a24SChandler Carruth // Other module related stuff.
459ef860a24SChandler Carruth //
460ef860a24SChandler Carruth 
4612fa1e43aSRafael Espindola std::vector<StructType *> Module::getIdentifiedStructTypes() const {
4622fa1e43aSRafael Espindola   // If we have a materializer, it is possible that some unread function
4632fa1e43aSRafael Espindola   // uses a type that is currently not visible to a TypeFinder, so ask
4642fa1e43aSRafael Espindola   // the materializer which types it created.
4652fa1e43aSRafael Espindola   if (Materializer)
4662fa1e43aSRafael Espindola     return Materializer->getIdentifiedStructTypes();
4672fa1e43aSRafael Espindola 
4682fa1e43aSRafael Espindola   std::vector<StructType *> Ret;
4692fa1e43aSRafael Espindola   TypeFinder SrcStructTypes;
4702fa1e43aSRafael Espindola   SrcStructTypes.run(*this, true);
4712fa1e43aSRafael Espindola   Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
4722fa1e43aSRafael Espindola   return Ret;
4732fa1e43aSRafael Espindola }
474ef860a24SChandler Carruth 
475ef860a24SChandler Carruth // dropAllReferences() - This function causes all the subelements to "let go"
476ef860a24SChandler Carruth // of all references that they are maintaining.  This allows one to 'delete' a
477ef860a24SChandler Carruth // whole module at a time, even though there may be circular references... first
478ef860a24SChandler Carruth // all references are dropped, and all use counts go to zero.  Then everything
479ef860a24SChandler Carruth // is deleted for real.  Note that no operations are valid on an object that
480ef860a24SChandler Carruth // has "dropped all references", except operator delete.
481ef860a24SChandler Carruth //
482ef860a24SChandler Carruth void Module::dropAllReferences() {
4833374910fSDavid Majnemer   for (Function &F : *this)
4843374910fSDavid Majnemer     F.dropAllReferences();
485ef860a24SChandler Carruth 
4863374910fSDavid Majnemer   for (GlobalVariable &GV : globals())
4873374910fSDavid Majnemer     GV.dropAllReferences();
488ef860a24SChandler Carruth 
4893374910fSDavid Majnemer   for (GlobalAlias &GA : aliases())
4903374910fSDavid Majnemer     GA.dropAllReferences();
491a1feff70SDmitry Polukhin 
492a1feff70SDmitry Polukhin   for (GlobalIFunc &GIF : ifuncs())
493a1feff70SDmitry Polukhin     GIF.dropAllReferences();
494ef860a24SChandler Carruth }
4950915c047SDiego Novillo 
496ac6081cbSNirav Dave unsigned Module::getNumberRegisterParameters() const {
497ac6081cbSNirav Dave   auto *Val =
498ac6081cbSNirav Dave       cast_or_null<ConstantAsMetadata>(getModuleFlag("NumRegisterParameters"));
499ac6081cbSNirav Dave   if (!Val)
500ac6081cbSNirav Dave     return 0;
501ac6081cbSNirav Dave   return cast<ConstantInt>(Val->getValue())->getZExtValue();
502ac6081cbSNirav Dave }
503ac6081cbSNirav Dave 
5040915c047SDiego Novillo unsigned Module::getDwarfVersion() const {
5055bf8fef5SDuncan P. N. Exon Smith   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
5060915c047SDiego Novillo   if (!Val)
50712d2c120SReid Kleckner     return 0;
50812d2c120SReid Kleckner   return cast<ConstantInt>(Val->getValue())->getZExtValue();
50912d2c120SReid Kleckner }
51012d2c120SReid Kleckner 
51112d2c120SReid Kleckner unsigned Module::getCodeViewFlag() const {
51212d2c120SReid Kleckner   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
51312d2c120SReid Kleckner   if (!Val)
51412d2c120SReid Kleckner     return 0;
5155bf8fef5SDuncan P. N. Exon Smith   return cast<ConstantInt>(Val->getValue())->getZExtValue();
5160915c047SDiego Novillo }
517dad0a645SDavid Majnemer 
518e49374d0SJessica Paquette unsigned Module::getInstructionCount() {
519e49374d0SJessica Paquette   unsigned NumInstrs = 0;
520e49374d0SJessica Paquette   for (Function &F : FunctionList)
521e49374d0SJessica Paquette     NumInstrs += F.getInstructionCount();
522e49374d0SJessica Paquette   return NumInstrs;
523e49374d0SJessica Paquette }
524e49374d0SJessica Paquette 
525dad0a645SDavid Majnemer Comdat *Module::getOrInsertComdat(StringRef Name) {
5265106ce78SDavid Blaikie   auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
527dad0a645SDavid Majnemer   Entry.second.Name = &Entry;
528dad0a645SDavid Majnemer   return &Entry.second;
529dad0a645SDavid Majnemer }
530771c132eSJustin Hibbits 
531771c132eSJustin Hibbits PICLevel::Level Module::getPICLevel() const {
5325bf8fef5SDuncan P. N. Exon Smith   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
533771c132eSJustin Hibbits 
534083ca9bbSHans Wennborg   if (!Val)
5354cccc488SDavide Italiano     return PICLevel::NotPIC;
536771c132eSJustin Hibbits 
5375bf8fef5SDuncan P. N. Exon Smith   return static_cast<PICLevel::Level>(
5385bf8fef5SDuncan P. N. Exon Smith       cast<ConstantInt>(Val->getValue())->getZExtValue());
539771c132eSJustin Hibbits }
540771c132eSJustin Hibbits 
541771c132eSJustin Hibbits void Module::setPICLevel(PICLevel::Level PL) {
5422db1369cSTeresa Johnson   addModuleFlag(ModFlagBehavior::Max, "PIC Level", PL);
543771c132eSJustin Hibbits }
544ecb05e51SEaswaran Raman 
54546d47b8cSSriraman Tallam PIELevel::Level Module::getPIELevel() const {
54646d47b8cSSriraman Tallam   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIE Level"));
54746d47b8cSSriraman Tallam 
54846d47b8cSSriraman Tallam   if (!Val)
54946d47b8cSSriraman Tallam     return PIELevel::Default;
55046d47b8cSSriraman Tallam 
55146d47b8cSSriraman Tallam   return static_cast<PIELevel::Level>(
55246d47b8cSSriraman Tallam       cast<ConstantInt>(Val->getValue())->getZExtValue());
55346d47b8cSSriraman Tallam }
55446d47b8cSSriraman Tallam 
55546d47b8cSSriraman Tallam void Module::setPIELevel(PIELevel::Level PL) {
5562db1369cSTeresa Johnson   addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL);
55746d47b8cSSriraman Tallam }
55846d47b8cSSriraman Tallam 
5593dea3f9eSCaroline Tice Optional<CodeModel::Model> Module::getCodeModel() const {
5603dea3f9eSCaroline Tice   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Code Model"));
5613dea3f9eSCaroline Tice 
5623dea3f9eSCaroline Tice   if (!Val)
5633dea3f9eSCaroline Tice     return None;
5643dea3f9eSCaroline Tice 
5653dea3f9eSCaroline Tice   return static_cast<CodeModel::Model>(
5663dea3f9eSCaroline Tice       cast<ConstantInt>(Val->getValue())->getZExtValue());
5673dea3f9eSCaroline Tice }
5683dea3f9eSCaroline Tice 
5693dea3f9eSCaroline Tice void Module::setCodeModel(CodeModel::Model CL) {
5703dea3f9eSCaroline Tice   // Linking object files with different code models is undefined behavior
5713dea3f9eSCaroline Tice   // because the compiler would have to generate additional code (to span
5723dea3f9eSCaroline Tice   // longer jumps) if a larger code model is used with a smaller one.
5733dea3f9eSCaroline Tice   // Therefore we will treat attempts to mix code models as an error.
5743dea3f9eSCaroline Tice   addModuleFlag(ModFlagBehavior::Error, "Code Model", CL);
5753dea3f9eSCaroline Tice }
5763dea3f9eSCaroline Tice 
577a6ff69f6SRong Xu void Module::setProfileSummary(Metadata *M, ProfileSummary::Kind Kind) {
578a6ff69f6SRong Xu   if (Kind == ProfileSummary::PSK_CSInstr)
579*01909b4eSHiroshi Yamauchi     setModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M);
580a6ff69f6SRong Xu   else
581*01909b4eSHiroshi Yamauchi     setModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M);
58226628d30SEaswaran Raman }
58326628d30SEaswaran Raman 
584a6ff69f6SRong Xu Metadata *Module::getProfileSummary(bool IsCS) {
585a6ff69f6SRong Xu   return (IsCS ? getModuleFlag("CSProfileSummary")
586a6ff69f6SRong Xu                : getModuleFlag("ProfileSummary"));
58726628d30SEaswaran Raman }
588b35cc691STeresa Johnson 
589fd09f12fSserge-sans-paille bool Module::getSemanticInterposition() const {
590fd09f12fSserge-sans-paille   Metadata *MF = getModuleFlag("SemanticInterposition");
591fd09f12fSserge-sans-paille 
592fd09f12fSserge-sans-paille   auto *Val = cast_or_null<ConstantAsMetadata>(MF);
593fd09f12fSserge-sans-paille   if (!Val)
594fd09f12fSserge-sans-paille     return false;
595fd09f12fSserge-sans-paille 
596fd09f12fSserge-sans-paille   return cast<ConstantInt>(Val->getValue())->getZExtValue();
597fd09f12fSserge-sans-paille }
598fd09f12fSserge-sans-paille 
599fd09f12fSserge-sans-paille void Module::setSemanticInterposition(bool SI) {
600fd09f12fSserge-sans-paille   addModuleFlag(ModFlagBehavior::Error, "SemanticInterposition", SI);
601fd09f12fSserge-sans-paille }
602fd09f12fSserge-sans-paille 
603e2dcf7c3SPeter Collingbourne void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) {
604e2dcf7c3SPeter Collingbourne   OwnedMemoryBuffer = std::move(MB);
605e2dcf7c3SPeter Collingbourne }
606e2dcf7c3SPeter Collingbourne 
607609f8c01SSriraman Tallam bool Module::getRtLibUseGOT() const {
608609f8c01SSriraman Tallam   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("RtLibUseGOT"));
609609f8c01SSriraman Tallam   return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
610609f8c01SSriraman Tallam }
611609f8c01SSriraman Tallam 
612609f8c01SSriraman Tallam void Module::setRtLibUseGOT() {
613609f8c01SSriraman Tallam   addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1);
614609f8c01SSriraman Tallam }
615609f8c01SSriraman Tallam 
616afa75d78SAlex Lorenz void Module::setSDKVersion(const VersionTuple &V) {
617afa75d78SAlex Lorenz   SmallVector<unsigned, 3> Entries;
618afa75d78SAlex Lorenz   Entries.push_back(V.getMajor());
619afa75d78SAlex Lorenz   if (auto Minor = V.getMinor()) {
620afa75d78SAlex Lorenz     Entries.push_back(*Minor);
621afa75d78SAlex Lorenz     if (auto Subminor = V.getSubminor())
622afa75d78SAlex Lorenz       Entries.push_back(*Subminor);
623afa75d78SAlex Lorenz     // Ignore the 'build' component as it can't be represented in the object
624afa75d78SAlex Lorenz     // file.
625afa75d78SAlex Lorenz   }
626afa75d78SAlex Lorenz   addModuleFlag(ModFlagBehavior::Warning, "SDK Version",
627afa75d78SAlex Lorenz                 ConstantDataArray::get(Context, Entries));
628afa75d78SAlex Lorenz }
629afa75d78SAlex Lorenz 
630afa75d78SAlex Lorenz VersionTuple Module::getSDKVersion() const {
631afa75d78SAlex Lorenz   auto *CM = dyn_cast_or_null<ConstantAsMetadata>(getModuleFlag("SDK Version"));
632afa75d78SAlex Lorenz   if (!CM)
633afa75d78SAlex Lorenz     return {};
634afa75d78SAlex Lorenz   auto *Arr = dyn_cast_or_null<ConstantDataArray>(CM->getValue());
635afa75d78SAlex Lorenz   if (!Arr)
636afa75d78SAlex Lorenz     return {};
637afa75d78SAlex Lorenz   auto getVersionComponent = [&](unsigned Index) -> Optional<unsigned> {
638afa75d78SAlex Lorenz     if (Index >= Arr->getNumElements())
639afa75d78SAlex Lorenz       return None;
640afa75d78SAlex Lorenz     return (unsigned)Arr->getElementAsInteger(Index);
641afa75d78SAlex Lorenz   };
642afa75d78SAlex Lorenz   auto Major = getVersionComponent(0);
643afa75d78SAlex Lorenz   if (!Major)
644afa75d78SAlex Lorenz     return {};
645afa75d78SAlex Lorenz   VersionTuple Result = VersionTuple(*Major);
646afa75d78SAlex Lorenz   if (auto Minor = getVersionComponent(1)) {
647afa75d78SAlex Lorenz     Result = VersionTuple(*Major, *Minor);
648afa75d78SAlex Lorenz     if (auto Subminor = getVersionComponent(2)) {
649afa75d78SAlex Lorenz       Result = VersionTuple(*Major, *Minor, *Subminor);
650afa75d78SAlex Lorenz     }
651afa75d78SAlex Lorenz   }
652afa75d78SAlex Lorenz   return Result;
653afa75d78SAlex Lorenz }
654afa75d78SAlex Lorenz 
655b35cc691STeresa Johnson GlobalVariable *llvm::collectUsedGlobalVariables(
656b35cc691STeresa Johnson     const Module &M, SmallPtrSetImpl<GlobalValue *> &Set, bool CompilerUsed) {
657b35cc691STeresa Johnson   const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
658b35cc691STeresa Johnson   GlobalVariable *GV = M.getGlobalVariable(Name);
659b35cc691STeresa Johnson   if (!GV || !GV->hasInitializer())
660b35cc691STeresa Johnson     return GV;
661b35cc691STeresa Johnson 
662b35cc691STeresa Johnson   const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
663b35cc691STeresa Johnson   for (Value *Op : Init->operands()) {
6642452d703SPeter Collingbourne     GlobalValue *G = cast<GlobalValue>(Op->stripPointerCasts());
665b35cc691STeresa Johnson     Set.insert(G);
666b35cc691STeresa Johnson   }
667b35cc691STeresa Johnson   return GV;
668b35cc691STeresa Johnson }
669