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