annotate llvm/lib/IR/Module.cpp @ 239:173fe712db74

merge LLVM16
author kono
date Wed, 09 Nov 2022 18:03:41 +0900
parents c4bab56944e8
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- Module.cpp - Implement the Module class ----------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file implements the Module class for the IR library.
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include "llvm/IR/Module.h"
anatofuz
parents:
diff changeset
14 #include "SymbolTableListTraitsImpl.h"
anatofuz
parents:
diff changeset
15 #include "llvm/ADT/Optional.h"
anatofuz
parents:
diff changeset
16 #include "llvm/ADT/SmallString.h"
anatofuz
parents:
diff changeset
17 #include "llvm/ADT/SmallVector.h"
anatofuz
parents:
diff changeset
18 #include "llvm/ADT/StringMap.h"
anatofuz
parents:
diff changeset
19 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
20 #include "llvm/ADT/Twine.h"
anatofuz
parents:
diff changeset
21 #include "llvm/IR/Attributes.h"
anatofuz
parents:
diff changeset
22 #include "llvm/IR/Comdat.h"
anatofuz
parents:
diff changeset
23 #include "llvm/IR/Constants.h"
anatofuz
parents:
diff changeset
24 #include "llvm/IR/DataLayout.h"
anatofuz
parents:
diff changeset
25 #include "llvm/IR/DebugInfoMetadata.h"
anatofuz
parents:
diff changeset
26 #include "llvm/IR/DerivedTypes.h"
anatofuz
parents:
diff changeset
27 #include "llvm/IR/Function.h"
anatofuz
parents:
diff changeset
28 #include "llvm/IR/GVMaterializer.h"
anatofuz
parents:
diff changeset
29 #include "llvm/IR/GlobalAlias.h"
anatofuz
parents:
diff changeset
30 #include "llvm/IR/GlobalIFunc.h"
anatofuz
parents:
diff changeset
31 #include "llvm/IR/GlobalValue.h"
anatofuz
parents:
diff changeset
32 #include "llvm/IR/GlobalVariable.h"
anatofuz
parents:
diff changeset
33 #include "llvm/IR/LLVMContext.h"
anatofuz
parents:
diff changeset
34 #include "llvm/IR/Metadata.h"
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
35 #include "llvm/IR/ModuleSummaryIndex.h"
150
anatofuz
parents:
diff changeset
36 #include "llvm/IR/SymbolTableListTraits.h"
anatofuz
parents:
diff changeset
37 #include "llvm/IR/Type.h"
anatofuz
parents:
diff changeset
38 #include "llvm/IR/TypeFinder.h"
anatofuz
parents:
diff changeset
39 #include "llvm/IR/Value.h"
anatofuz
parents:
diff changeset
40 #include "llvm/IR/ValueSymbolTable.h"
anatofuz
parents:
diff changeset
41 #include "llvm/Support/Casting.h"
anatofuz
parents:
diff changeset
42 #include "llvm/Support/CodeGen.h"
anatofuz
parents:
diff changeset
43 #include "llvm/Support/Error.h"
anatofuz
parents:
diff changeset
44 #include "llvm/Support/MemoryBuffer.h"
anatofuz
parents:
diff changeset
45 #include "llvm/Support/Path.h"
anatofuz
parents:
diff changeset
46 #include "llvm/Support/RandomNumberGenerator.h"
anatofuz
parents:
diff changeset
47 #include "llvm/Support/VersionTuple.h"
anatofuz
parents:
diff changeset
48 #include <algorithm>
anatofuz
parents:
diff changeset
49 #include <cassert>
anatofuz
parents:
diff changeset
50 #include <cstdint>
anatofuz
parents:
diff changeset
51 #include <memory>
anatofuz
parents:
diff changeset
52 #include <utility>
anatofuz
parents:
diff changeset
53 #include <vector>
anatofuz
parents:
diff changeset
54
anatofuz
parents:
diff changeset
55 using namespace llvm;
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
58 // Methods to implement the globals and functions lists.
anatofuz
parents:
diff changeset
59 //
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 // Explicit instantiations of SymbolTableListTraits since some of the methods
anatofuz
parents:
diff changeset
62 // are not in the public header file.
anatofuz
parents:
diff changeset
63 template class llvm::SymbolTableListTraits<Function>;
anatofuz
parents:
diff changeset
64 template class llvm::SymbolTableListTraits<GlobalVariable>;
anatofuz
parents:
diff changeset
65 template class llvm::SymbolTableListTraits<GlobalAlias>;
anatofuz
parents:
diff changeset
66 template class llvm::SymbolTableListTraits<GlobalIFunc>;
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
69 // Primitive Module methods.
anatofuz
parents:
diff changeset
70 //
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 Module::Module(StringRef MID, LLVMContext &C)
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
73 : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>(-1)),
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
74 ModuleID(std::string(MID)), SourceFileName(std::string(MID)), DL("") {
150
anatofuz
parents:
diff changeset
75 Context.addModule(this);
anatofuz
parents:
diff changeset
76 }
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 Module::~Module() {
anatofuz
parents:
diff changeset
79 Context.removeModule(this);
anatofuz
parents:
diff changeset
80 dropAllReferences();
anatofuz
parents:
diff changeset
81 GlobalList.clear();
anatofuz
parents:
diff changeset
82 FunctionList.clear();
anatofuz
parents:
diff changeset
83 AliasList.clear();
anatofuz
parents:
diff changeset
84 IFuncList.clear();
anatofuz
parents:
diff changeset
85 }
anatofuz
parents:
diff changeset
86
anatofuz
parents:
diff changeset
87 std::unique_ptr<RandomNumberGenerator>
anatofuz
parents:
diff changeset
88 Module::createRNG(const StringRef Name) const {
anatofuz
parents:
diff changeset
89 SmallString<32> Salt(Name);
anatofuz
parents:
diff changeset
90
anatofuz
parents:
diff changeset
91 // This RNG is guaranteed to produce the same random stream only
anatofuz
parents:
diff changeset
92 // when the Module ID and thus the input filename is the same. This
anatofuz
parents:
diff changeset
93 // might be problematic if the input filename extension changes
anatofuz
parents:
diff changeset
94 // (e.g. from .c to .bc or .ll).
anatofuz
parents:
diff changeset
95 //
anatofuz
parents:
diff changeset
96 // We could store this salt in NamedMetadata, but this would make
anatofuz
parents:
diff changeset
97 // the parameter non-const. This would unfortunately make this
anatofuz
parents:
diff changeset
98 // interface unusable by any Machine passes, since they only have a
anatofuz
parents:
diff changeset
99 // const reference to their IR Module. Alternatively we can always
anatofuz
parents:
diff changeset
100 // store salt metadata from the Module constructor.
anatofuz
parents:
diff changeset
101 Salt += sys::path::filename(getModuleIdentifier());
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 return std::unique_ptr<RandomNumberGenerator>(
anatofuz
parents:
diff changeset
104 new RandomNumberGenerator(Salt));
anatofuz
parents:
diff changeset
105 }
anatofuz
parents:
diff changeset
106
anatofuz
parents:
diff changeset
107 /// getNamedValue - Return the first global value in the module with
anatofuz
parents:
diff changeset
108 /// the specified name, of arbitrary type. This method returns null
anatofuz
parents:
diff changeset
109 /// if a global with the specified name is not found.
anatofuz
parents:
diff changeset
110 GlobalValue *Module::getNamedValue(StringRef Name) const {
anatofuz
parents:
diff changeset
111 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
anatofuz
parents:
diff changeset
112 }
anatofuz
parents:
diff changeset
113
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
114 unsigned Module::getNumNamedValues() const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
115 return getValueSymbolTable().size();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
116 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
117
150
anatofuz
parents:
diff changeset
118 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
anatofuz
parents:
diff changeset
119 /// This ID is uniqued across modules in the current LLVMContext.
anatofuz
parents:
diff changeset
120 unsigned Module::getMDKindID(StringRef Name) const {
anatofuz
parents:
diff changeset
121 return Context.getMDKindID(Name);
anatofuz
parents:
diff changeset
122 }
anatofuz
parents:
diff changeset
123
anatofuz
parents:
diff changeset
124 /// getMDKindNames - Populate client supplied SmallVector with the name for
anatofuz
parents:
diff changeset
125 /// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
anatofuz
parents:
diff changeset
126 /// so it is filled in as an empty string.
anatofuz
parents:
diff changeset
127 void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
anatofuz
parents:
diff changeset
128 return Context.getMDKindNames(Result);
anatofuz
parents:
diff changeset
129 }
anatofuz
parents:
diff changeset
130
anatofuz
parents:
diff changeset
131 void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const {
anatofuz
parents:
diff changeset
132 return Context.getOperandBundleTags(Result);
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
136 // Methods for easy access to the functions in the module.
anatofuz
parents:
diff changeset
137 //
anatofuz
parents:
diff changeset
138
anatofuz
parents:
diff changeset
139 // getOrInsertFunction - Look up the specified function in the module symbol
anatofuz
parents:
diff changeset
140 // table. If it does not exist, add a prototype for the function and return
anatofuz
parents:
diff changeset
141 // it. This is nice because it allows most passes to get away with not handling
anatofuz
parents:
diff changeset
142 // the symbol table directly for this common task.
anatofuz
parents:
diff changeset
143 //
anatofuz
parents:
diff changeset
144 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
anatofuz
parents:
diff changeset
145 AttributeList AttributeList) {
anatofuz
parents:
diff changeset
146 // See if we have a definition for the specified function already.
anatofuz
parents:
diff changeset
147 GlobalValue *F = getNamedValue(Name);
anatofuz
parents:
diff changeset
148 if (!F) {
anatofuz
parents:
diff changeset
149 // Nope, add it
anatofuz
parents:
diff changeset
150 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage,
anatofuz
parents:
diff changeset
151 DL.getProgramAddressSpace(), Name);
anatofuz
parents:
diff changeset
152 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
anatofuz
parents:
diff changeset
153 New->setAttributes(AttributeList);
anatofuz
parents:
diff changeset
154 FunctionList.push_back(New);
anatofuz
parents:
diff changeset
155 return {Ty, New}; // Return the new prototype.
anatofuz
parents:
diff changeset
156 }
anatofuz
parents:
diff changeset
157
anatofuz
parents:
diff changeset
158 // If the function exists but has the wrong type, return a bitcast to the
anatofuz
parents:
diff changeset
159 // right type.
anatofuz
parents:
diff changeset
160 auto *PTy = PointerType::get(Ty, F->getAddressSpace());
anatofuz
parents:
diff changeset
161 if (F->getType() != PTy)
anatofuz
parents:
diff changeset
162 return {Ty, ConstantExpr::getBitCast(F, PTy)};
anatofuz
parents:
diff changeset
163
anatofuz
parents:
diff changeset
164 // Otherwise, we just found the existing function or a prototype.
anatofuz
parents:
diff changeset
165 return {Ty, F};
anatofuz
parents:
diff changeset
166 }
anatofuz
parents:
diff changeset
167
anatofuz
parents:
diff changeset
168 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) {
anatofuz
parents:
diff changeset
169 return getOrInsertFunction(Name, Ty, AttributeList());
anatofuz
parents:
diff changeset
170 }
anatofuz
parents:
diff changeset
171
anatofuz
parents:
diff changeset
172 // getFunction - Look up the specified function in the module symbol table.
anatofuz
parents:
diff changeset
173 // If it does not exist, return null.
anatofuz
parents:
diff changeset
174 //
anatofuz
parents:
diff changeset
175 Function *Module::getFunction(StringRef Name) const {
anatofuz
parents:
diff changeset
176 return dyn_cast_or_null<Function>(getNamedValue(Name));
anatofuz
parents:
diff changeset
177 }
anatofuz
parents:
diff changeset
178
anatofuz
parents:
diff changeset
179 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
180 // Methods for easy access to the global variables in the module.
anatofuz
parents:
diff changeset
181 //
anatofuz
parents:
diff changeset
182
anatofuz
parents:
diff changeset
183 /// getGlobalVariable - Look up the specified global variable in the module
anatofuz
parents:
diff changeset
184 /// symbol table. If it does not exist, return null. The type argument
anatofuz
parents:
diff changeset
185 /// should be the underlying type of the global, i.e., it should not have
anatofuz
parents:
diff changeset
186 /// the top-level PointerType, which represents the address of the global.
anatofuz
parents:
diff changeset
187 /// If AllowLocal is set to true, this function will return types that
anatofuz
parents:
diff changeset
188 /// have an local. By default, these types are not returned.
anatofuz
parents:
diff changeset
189 ///
anatofuz
parents:
diff changeset
190 GlobalVariable *Module::getGlobalVariable(StringRef Name,
anatofuz
parents:
diff changeset
191 bool AllowLocal) const {
anatofuz
parents:
diff changeset
192 if (GlobalVariable *Result =
anatofuz
parents:
diff changeset
193 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
anatofuz
parents:
diff changeset
194 if (AllowLocal || !Result->hasLocalLinkage())
anatofuz
parents:
diff changeset
195 return Result;
anatofuz
parents:
diff changeset
196 return nullptr;
anatofuz
parents:
diff changeset
197 }
anatofuz
parents:
diff changeset
198
anatofuz
parents:
diff changeset
199 /// getOrInsertGlobal - Look up the specified global in the module symbol table.
anatofuz
parents:
diff changeset
200 /// 1. If it does not exist, add a declaration of the global and return it.
anatofuz
parents:
diff changeset
201 /// 2. Else, the global exists but has the wrong type: return the function
anatofuz
parents:
diff changeset
202 /// with a constantexpr cast to the right type.
anatofuz
parents:
diff changeset
203 /// 3. Finally, if the existing global is the correct declaration, return the
anatofuz
parents:
diff changeset
204 /// existing global.
anatofuz
parents:
diff changeset
205 Constant *Module::getOrInsertGlobal(
anatofuz
parents:
diff changeset
206 StringRef Name, Type *Ty,
anatofuz
parents:
diff changeset
207 function_ref<GlobalVariable *()> CreateGlobalCallback) {
anatofuz
parents:
diff changeset
208 // See if we have a definition for the specified global already.
anatofuz
parents:
diff changeset
209 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
anatofuz
parents:
diff changeset
210 if (!GV)
anatofuz
parents:
diff changeset
211 GV = CreateGlobalCallback();
anatofuz
parents:
diff changeset
212 assert(GV && "The CreateGlobalCallback is expected to create a global");
anatofuz
parents:
diff changeset
213
anatofuz
parents:
diff changeset
214 // If the variable exists but has the wrong type, return a bitcast to the
anatofuz
parents:
diff changeset
215 // right type.
anatofuz
parents:
diff changeset
216 Type *GVTy = GV->getType();
anatofuz
parents:
diff changeset
217 PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
anatofuz
parents:
diff changeset
218 if (GVTy != PTy)
anatofuz
parents:
diff changeset
219 return ConstantExpr::getBitCast(GV, PTy);
anatofuz
parents:
diff changeset
220
anatofuz
parents:
diff changeset
221 // Otherwise, we just found the existing function or a prototype.
anatofuz
parents:
diff changeset
222 return GV;
anatofuz
parents:
diff changeset
223 }
anatofuz
parents:
diff changeset
224
anatofuz
parents:
diff changeset
225 // Overload to construct a global variable using its constructor's defaults.
anatofuz
parents:
diff changeset
226 Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
anatofuz
parents:
diff changeset
227 return getOrInsertGlobal(Name, Ty, [&] {
anatofuz
parents:
diff changeset
228 return new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
anatofuz
parents:
diff changeset
229 nullptr, Name);
anatofuz
parents:
diff changeset
230 });
anatofuz
parents:
diff changeset
231 }
anatofuz
parents:
diff changeset
232
anatofuz
parents:
diff changeset
233 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
234 // Methods for easy access to the global variables in the module.
anatofuz
parents:
diff changeset
235 //
anatofuz
parents:
diff changeset
236
anatofuz
parents:
diff changeset
237 // getNamedAlias - Look up the specified global in the module symbol table.
anatofuz
parents:
diff changeset
238 // If it does not exist, return null.
anatofuz
parents:
diff changeset
239 //
anatofuz
parents:
diff changeset
240 GlobalAlias *Module::getNamedAlias(StringRef Name) const {
anatofuz
parents:
diff changeset
241 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
anatofuz
parents:
diff changeset
242 }
anatofuz
parents:
diff changeset
243
anatofuz
parents:
diff changeset
244 GlobalIFunc *Module::getNamedIFunc(StringRef Name) const {
anatofuz
parents:
diff changeset
245 return dyn_cast_or_null<GlobalIFunc>(getNamedValue(Name));
anatofuz
parents:
diff changeset
246 }
anatofuz
parents:
diff changeset
247
anatofuz
parents:
diff changeset
248 /// getNamedMetadata - Return the first NamedMDNode in the module with the
anatofuz
parents:
diff changeset
249 /// specified name. This method returns null if a NamedMDNode with the
anatofuz
parents:
diff changeset
250 /// specified name is not found.
anatofuz
parents:
diff changeset
251 NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
anatofuz
parents:
diff changeset
252 SmallString<256> NameData;
anatofuz
parents:
diff changeset
253 StringRef NameRef = Name.toStringRef(NameData);
anatofuz
parents:
diff changeset
254 return NamedMDSymTab.lookup(NameRef);
anatofuz
parents:
diff changeset
255 }
anatofuz
parents:
diff changeset
256
anatofuz
parents:
diff changeset
257 /// getOrInsertNamedMetadata - Return the first named MDNode in the module
anatofuz
parents:
diff changeset
258 /// with the specified name. This method returns a new NamedMDNode if a
anatofuz
parents:
diff changeset
259 /// NamedMDNode with the specified name is not found.
anatofuz
parents:
diff changeset
260 NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
anatofuz
parents:
diff changeset
261 NamedMDNode *&NMD = NamedMDSymTab[Name];
anatofuz
parents:
diff changeset
262 if (!NMD) {
anatofuz
parents:
diff changeset
263 NMD = new NamedMDNode(Name);
anatofuz
parents:
diff changeset
264 NMD->setParent(this);
anatofuz
parents:
diff changeset
265 NamedMDList.push_back(NMD);
anatofuz
parents:
diff changeset
266 }
anatofuz
parents:
diff changeset
267 return NMD;
anatofuz
parents:
diff changeset
268 }
anatofuz
parents:
diff changeset
269
anatofuz
parents:
diff changeset
270 /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
anatofuz
parents:
diff changeset
271 /// delete it.
anatofuz
parents:
diff changeset
272 void Module::eraseNamedMetadata(NamedMDNode *NMD) {
anatofuz
parents:
diff changeset
273 NamedMDSymTab.erase(NMD->getName());
anatofuz
parents:
diff changeset
274 NamedMDList.erase(NMD->getIterator());
anatofuz
parents:
diff changeset
275 }
anatofuz
parents:
diff changeset
276
anatofuz
parents:
diff changeset
277 bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
anatofuz
parents:
diff changeset
278 if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
anatofuz
parents:
diff changeset
279 uint64_t Val = Behavior->getLimitedValue();
anatofuz
parents:
diff changeset
280 if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
anatofuz
parents:
diff changeset
281 MFB = static_cast<ModFlagBehavior>(Val);
anatofuz
parents:
diff changeset
282 return true;
anatofuz
parents:
diff changeset
283 }
anatofuz
parents:
diff changeset
284 }
anatofuz
parents:
diff changeset
285 return false;
anatofuz
parents:
diff changeset
286 }
anatofuz
parents:
diff changeset
287
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
288 bool Module::isValidModuleFlag(const MDNode &ModFlag, ModFlagBehavior &MFB,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
289 MDString *&Key, Metadata *&Val) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
290 if (ModFlag.getNumOperands() < 3)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
291 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
292 if (!isValidModFlagBehavior(ModFlag.getOperand(0), MFB))
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
293 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
294 MDString *K = dyn_cast_or_null<MDString>(ModFlag.getOperand(1));
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
295 if (!K)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
296 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
297 Key = K;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
298 Val = ModFlag.getOperand(2);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
299 return true;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
300 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
301
150
anatofuz
parents:
diff changeset
302 /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
anatofuz
parents:
diff changeset
303 void Module::
anatofuz
parents:
diff changeset
304 getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
anatofuz
parents:
diff changeset
305 const NamedMDNode *ModFlags = getModuleFlagsMetadata();
anatofuz
parents:
diff changeset
306 if (!ModFlags) return;
anatofuz
parents:
diff changeset
307
anatofuz
parents:
diff changeset
308 for (const MDNode *Flag : ModFlags->operands()) {
anatofuz
parents:
diff changeset
309 ModFlagBehavior MFB;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
310 MDString *Key = nullptr;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
311 Metadata *Val = nullptr;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
312 if (isValidModuleFlag(*Flag, MFB, Key, Val)) {
150
anatofuz
parents:
diff changeset
313 // Check the operands of the MDNode before accessing the operands.
anatofuz
parents:
diff changeset
314 // The verifier will actually catch these failures.
anatofuz
parents:
diff changeset
315 Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
anatofuz
parents:
diff changeset
316 }
anatofuz
parents:
diff changeset
317 }
anatofuz
parents:
diff changeset
318 }
anatofuz
parents:
diff changeset
319
anatofuz
parents:
diff changeset
320 /// Return the corresponding value if Key appears in module flags, otherwise
anatofuz
parents:
diff changeset
321 /// return null.
anatofuz
parents:
diff changeset
322 Metadata *Module::getModuleFlag(StringRef Key) const {
anatofuz
parents:
diff changeset
323 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
anatofuz
parents:
diff changeset
324 getModuleFlagsMetadata(ModuleFlags);
anatofuz
parents:
diff changeset
325 for (const ModuleFlagEntry &MFE : ModuleFlags) {
anatofuz
parents:
diff changeset
326 if (Key == MFE.Key->getString())
anatofuz
parents:
diff changeset
327 return MFE.Val;
anatofuz
parents:
diff changeset
328 }
anatofuz
parents:
diff changeset
329 return nullptr;
anatofuz
parents:
diff changeset
330 }
anatofuz
parents:
diff changeset
331
anatofuz
parents:
diff changeset
332 /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
anatofuz
parents:
diff changeset
333 /// represents module-level flags. This method returns null if there are no
anatofuz
parents:
diff changeset
334 /// module-level flags.
anatofuz
parents:
diff changeset
335 NamedMDNode *Module::getModuleFlagsMetadata() const {
anatofuz
parents:
diff changeset
336 return getNamedMetadata("llvm.module.flags");
anatofuz
parents:
diff changeset
337 }
anatofuz
parents:
diff changeset
338
anatofuz
parents:
diff changeset
339 /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
anatofuz
parents:
diff changeset
340 /// represents module-level flags. If module-level flags aren't found, it
anatofuz
parents:
diff changeset
341 /// creates the named metadata that contains them.
anatofuz
parents:
diff changeset
342 NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
anatofuz
parents:
diff changeset
343 return getOrInsertNamedMetadata("llvm.module.flags");
anatofuz
parents:
diff changeset
344 }
anatofuz
parents:
diff changeset
345
anatofuz
parents:
diff changeset
346 /// addModuleFlag - Add a module-level flag to the module-level flags
anatofuz
parents:
diff changeset
347 /// metadata. It will create the module-level flags named metadata if it doesn't
anatofuz
parents:
diff changeset
348 /// already exist.
anatofuz
parents:
diff changeset
349 void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
anatofuz
parents:
diff changeset
350 Metadata *Val) {
anatofuz
parents:
diff changeset
351 Type *Int32Ty = Type::getInt32Ty(Context);
anatofuz
parents:
diff changeset
352 Metadata *Ops[3] = {
anatofuz
parents:
diff changeset
353 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
anatofuz
parents:
diff changeset
354 MDString::get(Context, Key), Val};
anatofuz
parents:
diff changeset
355 getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
anatofuz
parents:
diff changeset
356 }
anatofuz
parents:
diff changeset
357 void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
anatofuz
parents:
diff changeset
358 Constant *Val) {
anatofuz
parents:
diff changeset
359 addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
anatofuz
parents:
diff changeset
360 }
anatofuz
parents:
diff changeset
361 void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
anatofuz
parents:
diff changeset
362 uint32_t Val) {
anatofuz
parents:
diff changeset
363 Type *Int32Ty = Type::getInt32Ty(Context);
anatofuz
parents:
diff changeset
364 addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
anatofuz
parents:
diff changeset
365 }
anatofuz
parents:
diff changeset
366 void Module::addModuleFlag(MDNode *Node) {
anatofuz
parents:
diff changeset
367 assert(Node->getNumOperands() == 3 &&
anatofuz
parents:
diff changeset
368 "Invalid number of operands for module flag!");
anatofuz
parents:
diff changeset
369 assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
anatofuz
parents:
diff changeset
370 isa<MDString>(Node->getOperand(1)) &&
anatofuz
parents:
diff changeset
371 "Invalid operand types for module flag!");
anatofuz
parents:
diff changeset
372 getOrInsertModuleFlagsMetadata()->addOperand(Node);
anatofuz
parents:
diff changeset
373 }
anatofuz
parents:
diff changeset
374
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
375 void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
376 Metadata *Val) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
377 NamedMDNode *ModFlags = getOrInsertModuleFlagsMetadata();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
378 // Replace the flag if it already exists.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
379 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
380 MDNode *Flag = ModFlags->getOperand(I);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
381 ModFlagBehavior MFB;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
382 MDString *K = nullptr;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
383 Metadata *V = nullptr;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
384 if (isValidModuleFlag(*Flag, MFB, K, V) && K->getString() == Key) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
385 Flag->replaceOperandWith(2, Val);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
386 return;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
387 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
388 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
389 addModuleFlag(Behavior, Key, Val);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
390 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
391
150
anatofuz
parents:
diff changeset
392 void Module::setDataLayout(StringRef Desc) {
anatofuz
parents:
diff changeset
393 DL.reset(Desc);
anatofuz
parents:
diff changeset
394 }
anatofuz
parents:
diff changeset
395
anatofuz
parents:
diff changeset
396 void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
anatofuz
parents:
diff changeset
397
anatofuz
parents:
diff changeset
398 const DataLayout &Module::getDataLayout() const { return DL; }
anatofuz
parents:
diff changeset
399
anatofuz
parents:
diff changeset
400 DICompileUnit *Module::debug_compile_units_iterator::operator*() const {
anatofuz
parents:
diff changeset
401 return cast<DICompileUnit>(CUs->getOperand(Idx));
anatofuz
parents:
diff changeset
402 }
anatofuz
parents:
diff changeset
403 DICompileUnit *Module::debug_compile_units_iterator::operator->() const {
anatofuz
parents:
diff changeset
404 return cast<DICompileUnit>(CUs->getOperand(Idx));
anatofuz
parents:
diff changeset
405 }
anatofuz
parents:
diff changeset
406
anatofuz
parents:
diff changeset
407 void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
anatofuz
parents:
diff changeset
408 while (CUs && (Idx < CUs->getNumOperands()) &&
anatofuz
parents:
diff changeset
409 ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
anatofuz
parents:
diff changeset
410 ++Idx;
anatofuz
parents:
diff changeset
411 }
anatofuz
parents:
diff changeset
412
anatofuz
parents:
diff changeset
413 iterator_range<Module::global_object_iterator> Module::global_objects() {
anatofuz
parents:
diff changeset
414 return concat<GlobalObject>(functions(), globals());
anatofuz
parents:
diff changeset
415 }
anatofuz
parents:
diff changeset
416 iterator_range<Module::const_global_object_iterator>
anatofuz
parents:
diff changeset
417 Module::global_objects() const {
anatofuz
parents:
diff changeset
418 return concat<const GlobalObject>(functions(), globals());
anatofuz
parents:
diff changeset
419 }
anatofuz
parents:
diff changeset
420
anatofuz
parents:
diff changeset
421 iterator_range<Module::global_value_iterator> Module::global_values() {
anatofuz
parents:
diff changeset
422 return concat<GlobalValue>(functions(), globals(), aliases(), ifuncs());
anatofuz
parents:
diff changeset
423 }
anatofuz
parents:
diff changeset
424 iterator_range<Module::const_global_value_iterator>
anatofuz
parents:
diff changeset
425 Module::global_values() const {
anatofuz
parents:
diff changeset
426 return concat<const GlobalValue>(functions(), globals(), aliases(), ifuncs());
anatofuz
parents:
diff changeset
427 }
anatofuz
parents:
diff changeset
428
anatofuz
parents:
diff changeset
429 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
430 // Methods to control the materialization of GlobalValues in the Module.
anatofuz
parents:
diff changeset
431 //
anatofuz
parents:
diff changeset
432 void Module::setMaterializer(GVMaterializer *GVM) {
anatofuz
parents:
diff changeset
433 assert(!Materializer &&
anatofuz
parents:
diff changeset
434 "Module already has a GVMaterializer. Call materializeAll"
anatofuz
parents:
diff changeset
435 " to clear it out before setting another one.");
anatofuz
parents:
diff changeset
436 Materializer.reset(GVM);
anatofuz
parents:
diff changeset
437 }
anatofuz
parents:
diff changeset
438
anatofuz
parents:
diff changeset
439 Error Module::materialize(GlobalValue *GV) {
anatofuz
parents:
diff changeset
440 if (!Materializer)
anatofuz
parents:
diff changeset
441 return Error::success();
anatofuz
parents:
diff changeset
442
anatofuz
parents:
diff changeset
443 return Materializer->materialize(GV);
anatofuz
parents:
diff changeset
444 }
anatofuz
parents:
diff changeset
445
anatofuz
parents:
diff changeset
446 Error Module::materializeAll() {
anatofuz
parents:
diff changeset
447 if (!Materializer)
anatofuz
parents:
diff changeset
448 return Error::success();
anatofuz
parents:
diff changeset
449 std::unique_ptr<GVMaterializer> M = std::move(Materializer);
anatofuz
parents:
diff changeset
450 return M->materializeModule();
anatofuz
parents:
diff changeset
451 }
anatofuz
parents:
diff changeset
452
anatofuz
parents:
diff changeset
453 Error Module::materializeMetadata() {
anatofuz
parents:
diff changeset
454 if (!Materializer)
anatofuz
parents:
diff changeset
455 return Error::success();
anatofuz
parents:
diff changeset
456 return Materializer->materializeMetadata();
anatofuz
parents:
diff changeset
457 }
anatofuz
parents:
diff changeset
458
anatofuz
parents:
diff changeset
459 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
460 // Other module related stuff.
anatofuz
parents:
diff changeset
461 //
anatofuz
parents:
diff changeset
462
anatofuz
parents:
diff changeset
463 std::vector<StructType *> Module::getIdentifiedStructTypes() const {
anatofuz
parents:
diff changeset
464 // If we have a materializer, it is possible that some unread function
anatofuz
parents:
diff changeset
465 // uses a type that is currently not visible to a TypeFinder, so ask
anatofuz
parents:
diff changeset
466 // the materializer which types it created.
anatofuz
parents:
diff changeset
467 if (Materializer)
anatofuz
parents:
diff changeset
468 return Materializer->getIdentifiedStructTypes();
anatofuz
parents:
diff changeset
469
anatofuz
parents:
diff changeset
470 std::vector<StructType *> Ret;
anatofuz
parents:
diff changeset
471 TypeFinder SrcStructTypes;
anatofuz
parents:
diff changeset
472 SrcStructTypes.run(*this, true);
anatofuz
parents:
diff changeset
473 Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
anatofuz
parents:
diff changeset
474 return Ret;
anatofuz
parents:
diff changeset
475 }
anatofuz
parents:
diff changeset
476
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
477 std::string Module::getUniqueIntrinsicName(StringRef BaseName, Intrinsic::ID Id,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
478 const FunctionType *Proto) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
479 auto Encode = [&BaseName](unsigned Suffix) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
480 return (Twine(BaseName) + "." + Twine(Suffix)).str();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
481 };
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
482
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
483 {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
484 // fast path - the prototype is already known
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
485 auto UinItInserted = UniquedIntrinsicNames.insert({{Id, Proto}, 0});
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
486 if (!UinItInserted.second)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
487 return Encode(UinItInserted.first->second);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
488 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
489
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
490 // Not known yet. A new entry was created with index 0. Check if there already
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
491 // exists a matching declaration, or select a new entry.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
492
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
493 // Start looking for names with the current known maximum count (or 0).
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
494 auto NiidItInserted = CurrentIntrinsicIds.insert({BaseName, 0});
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
495 unsigned Count = NiidItInserted.first->second;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
496
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
497 // This might be slow if a whole population of intrinsics already existed, but
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
498 // we cache the values for later usage.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
499 std::string NewName;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
500 while (true) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
501 NewName = Encode(Count);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
502 GlobalValue *F = getNamedValue(NewName);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
503 if (!F) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
504 // Reserve this entry for the new proto
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
505 UniquedIntrinsicNames[{Id, Proto}] = Count;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
506 break;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
507 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
508
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
509 // A declaration with this name already exists. Remember it.
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
510 FunctionType *FT = dyn_cast<FunctionType>(F->getValueType());
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
511 auto UinItInserted = UniquedIntrinsicNames.insert({{Id, FT}, Count});
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
512 if (FT == Proto) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
513 // It was a declaration for our prototype. This entry was allocated in the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
514 // beginning. Update the count to match the existing declaration.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
515 UinItInserted.first->second = Count;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
516 break;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
517 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
518
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
519 ++Count;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
520 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
521
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
522 NiidItInserted.first->second = Count + 1;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
523
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
524 return NewName;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
525 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
526
150
anatofuz
parents:
diff changeset
527 // dropAllReferences() - This function causes all the subelements to "let go"
anatofuz
parents:
diff changeset
528 // of all references that they are maintaining. This allows one to 'delete' a
anatofuz
parents:
diff changeset
529 // whole module at a time, even though there may be circular references... first
anatofuz
parents:
diff changeset
530 // all references are dropped, and all use counts go to zero. Then everything
anatofuz
parents:
diff changeset
531 // is deleted for real. Note that no operations are valid on an object that
anatofuz
parents:
diff changeset
532 // has "dropped all references", except operator delete.
anatofuz
parents:
diff changeset
533 //
anatofuz
parents:
diff changeset
534 void Module::dropAllReferences() {
anatofuz
parents:
diff changeset
535 for (Function &F : *this)
anatofuz
parents:
diff changeset
536 F.dropAllReferences();
anatofuz
parents:
diff changeset
537
anatofuz
parents:
diff changeset
538 for (GlobalVariable &GV : globals())
anatofuz
parents:
diff changeset
539 GV.dropAllReferences();
anatofuz
parents:
diff changeset
540
anatofuz
parents:
diff changeset
541 for (GlobalAlias &GA : aliases())
anatofuz
parents:
diff changeset
542 GA.dropAllReferences();
anatofuz
parents:
diff changeset
543
anatofuz
parents:
diff changeset
544 for (GlobalIFunc &GIF : ifuncs())
anatofuz
parents:
diff changeset
545 GIF.dropAllReferences();
anatofuz
parents:
diff changeset
546 }
anatofuz
parents:
diff changeset
547
anatofuz
parents:
diff changeset
548 unsigned Module::getNumberRegisterParameters() const {
anatofuz
parents:
diff changeset
549 auto *Val =
anatofuz
parents:
diff changeset
550 cast_or_null<ConstantAsMetadata>(getModuleFlag("NumRegisterParameters"));
anatofuz
parents:
diff changeset
551 if (!Val)
anatofuz
parents:
diff changeset
552 return 0;
anatofuz
parents:
diff changeset
553 return cast<ConstantInt>(Val->getValue())->getZExtValue();
anatofuz
parents:
diff changeset
554 }
anatofuz
parents:
diff changeset
555
anatofuz
parents:
diff changeset
556 unsigned Module::getDwarfVersion() const {
anatofuz
parents:
diff changeset
557 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
anatofuz
parents:
diff changeset
558 if (!Val)
anatofuz
parents:
diff changeset
559 return 0;
anatofuz
parents:
diff changeset
560 return cast<ConstantInt>(Val->getValue())->getZExtValue();
anatofuz
parents:
diff changeset
561 }
anatofuz
parents:
diff changeset
562
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
563 bool Module::isDwarf64() const {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
564 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("DWARF64"));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
565 return Val && cast<ConstantInt>(Val->getValue())->isOne();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
566 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
567
150
anatofuz
parents:
diff changeset
568 unsigned Module::getCodeViewFlag() const {
anatofuz
parents:
diff changeset
569 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
anatofuz
parents:
diff changeset
570 if (!Val)
anatofuz
parents:
diff changeset
571 return 0;
anatofuz
parents:
diff changeset
572 return cast<ConstantInt>(Val->getValue())->getZExtValue();
anatofuz
parents:
diff changeset
573 }
anatofuz
parents:
diff changeset
574
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
575 unsigned Module::getInstructionCount() const {
150
anatofuz
parents:
diff changeset
576 unsigned NumInstrs = 0;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
577 for (const Function &F : FunctionList)
150
anatofuz
parents:
diff changeset
578 NumInstrs += F.getInstructionCount();
anatofuz
parents:
diff changeset
579 return NumInstrs;
anatofuz
parents:
diff changeset
580 }
anatofuz
parents:
diff changeset
581
anatofuz
parents:
diff changeset
582 Comdat *Module::getOrInsertComdat(StringRef Name) {
anatofuz
parents:
diff changeset
583 auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
anatofuz
parents:
diff changeset
584 Entry.second.Name = &Entry;
anatofuz
parents:
diff changeset
585 return &Entry.second;
anatofuz
parents:
diff changeset
586 }
anatofuz
parents:
diff changeset
587
anatofuz
parents:
diff changeset
588 PICLevel::Level Module::getPICLevel() const {
anatofuz
parents:
diff changeset
589 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
anatofuz
parents:
diff changeset
590
anatofuz
parents:
diff changeset
591 if (!Val)
anatofuz
parents:
diff changeset
592 return PICLevel::NotPIC;
anatofuz
parents:
diff changeset
593
anatofuz
parents:
diff changeset
594 return static_cast<PICLevel::Level>(
anatofuz
parents:
diff changeset
595 cast<ConstantInt>(Val->getValue())->getZExtValue());
anatofuz
parents:
diff changeset
596 }
anatofuz
parents:
diff changeset
597
anatofuz
parents:
diff changeset
598 void Module::setPICLevel(PICLevel::Level PL) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
599 // The merge result of a non-PIC object and a PIC object can only be reliably
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
600 // used as a non-PIC object, so use the Min merge behavior.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
601 addModuleFlag(ModFlagBehavior::Min, "PIC Level", PL);
150
anatofuz
parents:
diff changeset
602 }
anatofuz
parents:
diff changeset
603
anatofuz
parents:
diff changeset
604 PIELevel::Level Module::getPIELevel() const {
anatofuz
parents:
diff changeset
605 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIE Level"));
anatofuz
parents:
diff changeset
606
anatofuz
parents:
diff changeset
607 if (!Val)
anatofuz
parents:
diff changeset
608 return PIELevel::Default;
anatofuz
parents:
diff changeset
609
anatofuz
parents:
diff changeset
610 return static_cast<PIELevel::Level>(
anatofuz
parents:
diff changeset
611 cast<ConstantInt>(Val->getValue())->getZExtValue());
anatofuz
parents:
diff changeset
612 }
anatofuz
parents:
diff changeset
613
anatofuz
parents:
diff changeset
614 void Module::setPIELevel(PIELevel::Level PL) {
anatofuz
parents:
diff changeset
615 addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL);
anatofuz
parents:
diff changeset
616 }
anatofuz
parents:
diff changeset
617
anatofuz
parents:
diff changeset
618 Optional<CodeModel::Model> Module::getCodeModel() const {
anatofuz
parents:
diff changeset
619 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Code Model"));
anatofuz
parents:
diff changeset
620
anatofuz
parents:
diff changeset
621 if (!Val)
anatofuz
parents:
diff changeset
622 return None;
anatofuz
parents:
diff changeset
623
anatofuz
parents:
diff changeset
624 return static_cast<CodeModel::Model>(
anatofuz
parents:
diff changeset
625 cast<ConstantInt>(Val->getValue())->getZExtValue());
anatofuz
parents:
diff changeset
626 }
anatofuz
parents:
diff changeset
627
anatofuz
parents:
diff changeset
628 void Module::setCodeModel(CodeModel::Model CL) {
anatofuz
parents:
diff changeset
629 // Linking object files with different code models is undefined behavior
anatofuz
parents:
diff changeset
630 // because the compiler would have to generate additional code (to span
anatofuz
parents:
diff changeset
631 // longer jumps) if a larger code model is used with a smaller one.
anatofuz
parents:
diff changeset
632 // Therefore we will treat attempts to mix code models as an error.
anatofuz
parents:
diff changeset
633 addModuleFlag(ModFlagBehavior::Error, "Code Model", CL);
anatofuz
parents:
diff changeset
634 }
anatofuz
parents:
diff changeset
635
anatofuz
parents:
diff changeset
636 void Module::setProfileSummary(Metadata *M, ProfileSummary::Kind Kind) {
anatofuz
parents:
diff changeset
637 if (Kind == ProfileSummary::PSK_CSInstr)
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
638 setModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M);
150
anatofuz
parents:
diff changeset
639 else
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
640 setModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M);
150
anatofuz
parents:
diff changeset
641 }
anatofuz
parents:
diff changeset
642
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
643 Metadata *Module::getProfileSummary(bool IsCS) const {
150
anatofuz
parents:
diff changeset
644 return (IsCS ? getModuleFlag("CSProfileSummary")
anatofuz
parents:
diff changeset
645 : getModuleFlag("ProfileSummary"));
anatofuz
parents:
diff changeset
646 }
anatofuz
parents:
diff changeset
647
anatofuz
parents:
diff changeset
648 bool Module::getSemanticInterposition() const {
anatofuz
parents:
diff changeset
649 Metadata *MF = getModuleFlag("SemanticInterposition");
anatofuz
parents:
diff changeset
650
anatofuz
parents:
diff changeset
651 auto *Val = cast_or_null<ConstantAsMetadata>(MF);
anatofuz
parents:
diff changeset
652 if (!Val)
anatofuz
parents:
diff changeset
653 return false;
anatofuz
parents:
diff changeset
654
anatofuz
parents:
diff changeset
655 return cast<ConstantInt>(Val->getValue())->getZExtValue();
anatofuz
parents:
diff changeset
656 }
anatofuz
parents:
diff changeset
657
anatofuz
parents:
diff changeset
658 void Module::setSemanticInterposition(bool SI) {
anatofuz
parents:
diff changeset
659 addModuleFlag(ModFlagBehavior::Error, "SemanticInterposition", SI);
anatofuz
parents:
diff changeset
660 }
anatofuz
parents:
diff changeset
661
anatofuz
parents:
diff changeset
662 void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) {
anatofuz
parents:
diff changeset
663 OwnedMemoryBuffer = std::move(MB);
anatofuz
parents:
diff changeset
664 }
anatofuz
parents:
diff changeset
665
anatofuz
parents:
diff changeset
666 bool Module::getRtLibUseGOT() const {
anatofuz
parents:
diff changeset
667 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("RtLibUseGOT"));
anatofuz
parents:
diff changeset
668 return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
anatofuz
parents:
diff changeset
669 }
anatofuz
parents:
diff changeset
670
anatofuz
parents:
diff changeset
671 void Module::setRtLibUseGOT() {
anatofuz
parents:
diff changeset
672 addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1);
anatofuz
parents:
diff changeset
673 }
anatofuz
parents:
diff changeset
674
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
675 UWTableKind Module::getUwtable() const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
676 if (auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("uwtable")))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
677 return UWTableKind(cast<ConstantInt>(Val->getValue())->getZExtValue());
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
678 return UWTableKind::None;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
679 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
680
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
681 void Module::setUwtable(UWTableKind Kind) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
682 addModuleFlag(ModFlagBehavior::Max, "uwtable", uint32_t(Kind));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
683 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
684
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
685 FramePointerKind Module::getFramePointer() const {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
686 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("frame-pointer"));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
687 return static_cast<FramePointerKind>(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
688 Val ? cast<ConstantInt>(Val->getValue())->getZExtValue() : 0);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
689 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
690
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
691 void Module::setFramePointer(FramePointerKind Kind) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
692 addModuleFlag(ModFlagBehavior::Max, "frame-pointer", static_cast<int>(Kind));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
693 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
694
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
695 StringRef Module::getStackProtectorGuard() const {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
696 Metadata *MD = getModuleFlag("stack-protector-guard");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
697 if (auto *MDS = dyn_cast_or_null<MDString>(MD))
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
698 return MDS->getString();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
699 return {};
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
700 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
701
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
702 void Module::setStackProtectorGuard(StringRef Kind) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
703 MDString *ID = MDString::get(getContext(), Kind);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
704 addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard", ID);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
705 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
706
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
707 StringRef Module::getStackProtectorGuardReg() const {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
708 Metadata *MD = getModuleFlag("stack-protector-guard-reg");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
709 if (auto *MDS = dyn_cast_or_null<MDString>(MD))
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
710 return MDS->getString();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
711 return {};
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
712 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
713
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
714 void Module::setStackProtectorGuardReg(StringRef Reg) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
715 MDString *ID = MDString::get(getContext(), Reg);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
716 addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-reg", ID);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
717 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
718
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
719 StringRef Module::getStackProtectorGuardSymbol() const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
720 Metadata *MD = getModuleFlag("stack-protector-guard-symbol");
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
721 if (auto *MDS = dyn_cast_or_null<MDString>(MD))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
722 return MDS->getString();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
723 return {};
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
724 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
725
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
726 void Module::setStackProtectorGuardSymbol(StringRef Symbol) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
727 MDString *ID = MDString::get(getContext(), Symbol);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
728 addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-symbol", ID);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
729 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
730
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
731 int Module::getStackProtectorGuardOffset() const {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
732 Metadata *MD = getModuleFlag("stack-protector-guard-offset");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
733 if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
734 return CI->getSExtValue();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
735 return INT_MAX;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
736 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
737
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
738 void Module::setStackProtectorGuardOffset(int Offset) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
739 addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-offset", Offset);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
740 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
741
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
742 unsigned Module::getOverrideStackAlignment() const {
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
743 Metadata *MD = getModuleFlag("override-stack-alignment");
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
744 if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
745 return CI->getZExtValue();
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
746 return 0;
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
747 }
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
748
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
749 void Module::setOverrideStackAlignment(unsigned Align) {
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
750 addModuleFlag(ModFlagBehavior::Error, "override-stack-alignment", Align);
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
751 }
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
752
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
753 static void addSDKVersionMD(const VersionTuple &V, Module &M, StringRef Name) {
150
anatofuz
parents:
diff changeset
754 SmallVector<unsigned, 3> Entries;
anatofuz
parents:
diff changeset
755 Entries.push_back(V.getMajor());
anatofuz
parents:
diff changeset
756 if (auto Minor = V.getMinor()) {
anatofuz
parents:
diff changeset
757 Entries.push_back(*Minor);
anatofuz
parents:
diff changeset
758 if (auto Subminor = V.getSubminor())
anatofuz
parents:
diff changeset
759 Entries.push_back(*Subminor);
anatofuz
parents:
diff changeset
760 // Ignore the 'build' component as it can't be represented in the object
anatofuz
parents:
diff changeset
761 // file.
anatofuz
parents:
diff changeset
762 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
763 M.addModuleFlag(Module::ModFlagBehavior::Warning, Name,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
764 ConstantDataArray::get(M.getContext(), Entries));
150
anatofuz
parents:
diff changeset
765 }
anatofuz
parents:
diff changeset
766
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
767 void Module::setSDKVersion(const VersionTuple &V) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
768 addSDKVersionMD(V, *this, "SDK Version");
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
769 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
770
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
771 static VersionTuple getSDKVersionMD(Metadata *MD) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
772 auto *CM = dyn_cast_or_null<ConstantAsMetadata>(MD);
150
anatofuz
parents:
diff changeset
773 if (!CM)
anatofuz
parents:
diff changeset
774 return {};
anatofuz
parents:
diff changeset
775 auto *Arr = dyn_cast_or_null<ConstantDataArray>(CM->getValue());
anatofuz
parents:
diff changeset
776 if (!Arr)
anatofuz
parents:
diff changeset
777 return {};
anatofuz
parents:
diff changeset
778 auto getVersionComponent = [&](unsigned Index) -> Optional<unsigned> {
anatofuz
parents:
diff changeset
779 if (Index >= Arr->getNumElements())
anatofuz
parents:
diff changeset
780 return None;
anatofuz
parents:
diff changeset
781 return (unsigned)Arr->getElementAsInteger(Index);
anatofuz
parents:
diff changeset
782 };
anatofuz
parents:
diff changeset
783 auto Major = getVersionComponent(0);
anatofuz
parents:
diff changeset
784 if (!Major)
anatofuz
parents:
diff changeset
785 return {};
anatofuz
parents:
diff changeset
786 VersionTuple Result = VersionTuple(*Major);
anatofuz
parents:
diff changeset
787 if (auto Minor = getVersionComponent(1)) {
anatofuz
parents:
diff changeset
788 Result = VersionTuple(*Major, *Minor);
anatofuz
parents:
diff changeset
789 if (auto Subminor = getVersionComponent(2)) {
anatofuz
parents:
diff changeset
790 Result = VersionTuple(*Major, *Minor, *Subminor);
anatofuz
parents:
diff changeset
791 }
anatofuz
parents:
diff changeset
792 }
anatofuz
parents:
diff changeset
793 return Result;
anatofuz
parents:
diff changeset
794 }
anatofuz
parents:
diff changeset
795
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
796 VersionTuple Module::getSDKVersion() const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
797 return getSDKVersionMD(getModuleFlag("SDK Version"));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
798 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
799
150
anatofuz
parents:
diff changeset
800 GlobalVariable *llvm::collectUsedGlobalVariables(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
801 const Module &M, SmallVectorImpl<GlobalValue *> &Vec, bool CompilerUsed) {
150
anatofuz
parents:
diff changeset
802 const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
anatofuz
parents:
diff changeset
803 GlobalVariable *GV = M.getGlobalVariable(Name);
anatofuz
parents:
diff changeset
804 if (!GV || !GV->hasInitializer())
anatofuz
parents:
diff changeset
805 return GV;
anatofuz
parents:
diff changeset
806
anatofuz
parents:
diff changeset
807 const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
anatofuz
parents:
diff changeset
808 for (Value *Op : Init->operands()) {
anatofuz
parents:
diff changeset
809 GlobalValue *G = cast<GlobalValue>(Op->stripPointerCasts());
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
810 Vec.push_back(G);
150
anatofuz
parents:
diff changeset
811 }
anatofuz
parents:
diff changeset
812 return GV;
anatofuz
parents:
diff changeset
813 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
814
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
815 void Module::setPartialSampleProfileRatio(const ModuleSummaryIndex &Index) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
816 if (auto *SummaryMD = getProfileSummary(/*IsCS*/ false)) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
817 std::unique_ptr<ProfileSummary> ProfileSummary(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
818 ProfileSummary::getFromMD(SummaryMD));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
819 if (ProfileSummary) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
820 if (ProfileSummary->getKind() != ProfileSummary::PSK_Sample ||
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
821 !ProfileSummary->isPartialProfile())
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
822 return;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
823 uint64_t BlockCount = Index.getBlockCount();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
824 uint32_t NumCounts = ProfileSummary->getNumCounts();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
825 if (!NumCounts)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
826 return;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
827 double Ratio = (double)BlockCount / NumCounts;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
828 ProfileSummary->setPartialProfileRatio(Ratio);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
829 setProfileSummary(ProfileSummary->getMD(getContext()),
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
830 ProfileSummary::PSK_Sample);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
831 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
832 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
833 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
834
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
835 StringRef Module::getDarwinTargetVariantTriple() const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
836 if (const auto *MD = getModuleFlag("darwin.target_variant.triple"))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
837 return cast<MDString>(MD)->getString();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
838 return "";
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
839 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
840
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
841 void Module::setDarwinTargetVariantTriple(StringRef T) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
842 addModuleFlag(ModFlagBehavior::Override, "darwin.target_variant.triple",
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
843 MDString::get(getContext(), T));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
844 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
845
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
846 VersionTuple Module::getDarwinTargetVariantSDKVersion() const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
847 return getSDKVersionMD(getModuleFlag("darwin.target_variant.SDK Version"));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
848 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
849
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
850 void Module::setDarwinTargetVariantSDKVersion(VersionTuple Version) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
851 addSDKVersionMD(Version, *this, "darwin.target_variant.SDK Version");
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
852 }