File: | src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp |
Warning: | line 509, column 36 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===// | ||||
2 | // | ||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | // | ||||
7 | //===----------------------------------------------------------------------===// | ||||
8 | // | ||||
9 | // This pass builds a ModuleSummaryIndex object for the module, to be written | ||||
10 | // to bitcode or LLVM assembly. | ||||
11 | // | ||||
12 | //===----------------------------------------------------------------------===// | ||||
13 | |||||
14 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" | ||||
15 | #include "llvm/ADT/ArrayRef.h" | ||||
16 | #include "llvm/ADT/DenseSet.h" | ||||
17 | #include "llvm/ADT/MapVector.h" | ||||
18 | #include "llvm/ADT/STLExtras.h" | ||||
19 | #include "llvm/ADT/SetVector.h" | ||||
20 | #include "llvm/ADT/SmallPtrSet.h" | ||||
21 | #include "llvm/ADT/SmallVector.h" | ||||
22 | #include "llvm/ADT/StringRef.h" | ||||
23 | #include "llvm/Analysis/BlockFrequencyInfo.h" | ||||
24 | #include "llvm/Analysis/BranchProbabilityInfo.h" | ||||
25 | #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" | ||||
26 | #include "llvm/Analysis/LoopInfo.h" | ||||
27 | #include "llvm/Analysis/ProfileSummaryInfo.h" | ||||
28 | #include "llvm/Analysis/StackSafetyAnalysis.h" | ||||
29 | #include "llvm/Analysis/TypeMetadataUtils.h" | ||||
30 | #include "llvm/IR/Attributes.h" | ||||
31 | #include "llvm/IR/BasicBlock.h" | ||||
32 | #include "llvm/IR/Constant.h" | ||||
33 | #include "llvm/IR/Constants.h" | ||||
34 | #include "llvm/IR/Dominators.h" | ||||
35 | #include "llvm/IR/Function.h" | ||||
36 | #include "llvm/IR/GlobalAlias.h" | ||||
37 | #include "llvm/IR/GlobalValue.h" | ||||
38 | #include "llvm/IR/GlobalVariable.h" | ||||
39 | #include "llvm/IR/Instructions.h" | ||||
40 | #include "llvm/IR/IntrinsicInst.h" | ||||
41 | #include "llvm/IR/Intrinsics.h" | ||||
42 | #include "llvm/IR/Metadata.h" | ||||
43 | #include "llvm/IR/Module.h" | ||||
44 | #include "llvm/IR/ModuleSummaryIndex.h" | ||||
45 | #include "llvm/IR/Use.h" | ||||
46 | #include "llvm/IR/User.h" | ||||
47 | #include "llvm/InitializePasses.h" | ||||
48 | #include "llvm/Object/ModuleSymbolTable.h" | ||||
49 | #include "llvm/Object/SymbolicFile.h" | ||||
50 | #include "llvm/Pass.h" | ||||
51 | #include "llvm/Support/Casting.h" | ||||
52 | #include "llvm/Support/CommandLine.h" | ||||
53 | #include "llvm/Support/FileSystem.h" | ||||
54 | #include <algorithm> | ||||
55 | #include <cassert> | ||||
56 | #include <cstdint> | ||||
57 | #include <vector> | ||||
58 | |||||
59 | using namespace llvm; | ||||
60 | |||||
61 | #define DEBUG_TYPE"module-summary-analysis" "module-summary-analysis" | ||||
62 | |||||
63 | // Option to force edges cold which will block importing when the | ||||
64 | // -import-cold-multiplier is set to 0. Useful for debugging. | ||||
65 | FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold = | ||||
66 | FunctionSummary::FSHT_None; | ||||
67 | cl::opt<FunctionSummary::ForceSummaryHotnessType, true> FSEC( | ||||
68 | "force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold), | ||||
69 | cl::desc("Force all edges in the function summary to cold"), | ||||
70 | cl::values(clEnumValN(FunctionSummary::FSHT_None, "none", "None.")llvm::cl::OptionEnumValue { "none", int(FunctionSummary::FSHT_None ), "None." }, | ||||
71 | clEnumValN(FunctionSummary::FSHT_AllNonCritical,llvm::cl::OptionEnumValue { "all-non-critical", int(FunctionSummary ::FSHT_AllNonCritical), "All non-critical edges." } | ||||
72 | "all-non-critical", "All non-critical edges.")llvm::cl::OptionEnumValue { "all-non-critical", int(FunctionSummary ::FSHT_AllNonCritical), "All non-critical edges." }, | ||||
73 | clEnumValN(FunctionSummary::FSHT_All, "all", "All edges.")llvm::cl::OptionEnumValue { "all", int(FunctionSummary::FSHT_All ), "All edges." })); | ||||
74 | |||||
75 | cl::opt<std::string> ModuleSummaryDotFile( | ||||
76 | "module-summary-dot-file", cl::init(""), cl::Hidden, | ||||
77 | cl::value_desc("filename"), | ||||
78 | cl::desc("File to emit dot graph of new summary into.")); | ||||
79 | |||||
80 | // Walk through the operands of a given User via worklist iteration and populate | ||||
81 | // the set of GlobalValue references encountered. Invoked either on an | ||||
82 | // Instruction or a GlobalVariable (which walks its initializer). | ||||
83 | // Return true if any of the operands contains blockaddress. This is important | ||||
84 | // to know when computing summary for global var, because if global variable | ||||
85 | // references basic block address we can't import it separately from function | ||||
86 | // containing that basic block. For simplicity we currently don't import such | ||||
87 | // global vars at all. When importing function we aren't interested if any | ||||
88 | // instruction in it takes an address of any basic block, because instruction | ||||
89 | // can only take an address of basic block located in the same function. | ||||
90 | static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser, | ||||
91 | SetVector<ValueInfo> &RefEdges, | ||||
92 | SmallPtrSet<const User *, 8> &Visited) { | ||||
93 | bool HasBlockAddress = false; | ||||
94 | SmallVector<const User *, 32> Worklist; | ||||
95 | if (Visited.insert(CurUser).second) | ||||
96 | Worklist.push_back(CurUser); | ||||
97 | |||||
98 | while (!Worklist.empty()) { | ||||
99 | const User *U = Worklist.pop_back_val(); | ||||
100 | const auto *CB = dyn_cast<CallBase>(U); | ||||
101 | |||||
102 | for (const auto &OI : U->operands()) { | ||||
103 | const User *Operand = dyn_cast<User>(OI); | ||||
104 | if (!Operand) | ||||
105 | continue; | ||||
106 | if (isa<BlockAddress>(Operand)) { | ||||
107 | HasBlockAddress = true; | ||||
108 | continue; | ||||
109 | } | ||||
110 | if (auto *GV = dyn_cast<GlobalValue>(Operand)) { | ||||
111 | // We have a reference to a global value. This should be added to | ||||
112 | // the reference set unless it is a callee. Callees are handled | ||||
113 | // specially by WriteFunction and are added to a separate list. | ||||
114 | if (!(CB && CB->isCallee(&OI))) | ||||
115 | RefEdges.insert(Index.getOrInsertValueInfo(GV)); | ||||
116 | continue; | ||||
117 | } | ||||
118 | if (Visited.insert(Operand).second) | ||||
119 | Worklist.push_back(Operand); | ||||
120 | } | ||||
121 | } | ||||
122 | return HasBlockAddress; | ||||
123 | } | ||||
124 | |||||
125 | static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount, | ||||
126 | ProfileSummaryInfo *PSI) { | ||||
127 | if (!PSI) | ||||
128 | return CalleeInfo::HotnessType::Unknown; | ||||
129 | if (PSI->isHotCount(ProfileCount)) | ||||
130 | return CalleeInfo::HotnessType::Hot; | ||||
131 | if (PSI->isColdCount(ProfileCount)) | ||||
132 | return CalleeInfo::HotnessType::Cold; | ||||
133 | return CalleeInfo::HotnessType::None; | ||||
134 | } | ||||
135 | |||||
136 | static bool isNonRenamableLocal(const GlobalValue &GV) { | ||||
137 | return GV.hasSection() && GV.hasLocalLinkage(); | ||||
138 | } | ||||
139 | |||||
140 | /// Determine whether this call has all constant integer arguments (excluding | ||||
141 | /// "this") and summarize it to VCalls or ConstVCalls as appropriate. | ||||
142 | static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid, | ||||
143 | SetVector<FunctionSummary::VFuncId> &VCalls, | ||||
144 | SetVector<FunctionSummary::ConstVCall> &ConstVCalls) { | ||||
145 | std::vector<uint64_t> Args; | ||||
146 | // Start from the second argument to skip the "this" pointer. | ||||
147 | for (auto &Arg : drop_begin(Call.CB.args())) { | ||||
148 | auto *CI = dyn_cast<ConstantInt>(Arg); | ||||
149 | if (!CI || CI->getBitWidth() > 64) { | ||||
150 | VCalls.insert({Guid, Call.Offset}); | ||||
151 | return; | ||||
152 | } | ||||
153 | Args.push_back(CI->getZExtValue()); | ||||
154 | } | ||||
155 | ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)}); | ||||
156 | } | ||||
157 | |||||
158 | /// If this intrinsic call requires that we add information to the function | ||||
159 | /// summary, do so via the non-constant reference arguments. | ||||
160 | static void addIntrinsicToSummary( | ||||
161 | const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests, | ||||
162 | SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls, | ||||
163 | SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls, | ||||
164 | SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls, | ||||
165 | SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls, | ||||
166 | DominatorTree &DT) { | ||||
167 | switch (CI->getCalledFunction()->getIntrinsicID()) { | ||||
168 | case Intrinsic::type_test: { | ||||
169 | auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1)); | ||||
170 | auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata()); | ||||
171 | if (!TypeId) | ||||
172 | break; | ||||
173 | GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString()); | ||||
174 | |||||
175 | // Produce a summary from type.test intrinsics. We only summarize type.test | ||||
176 | // intrinsics that are used other than by an llvm.assume intrinsic. | ||||
177 | // Intrinsics that are assumed are relevant only to the devirtualization | ||||
178 | // pass, not the type test lowering pass. | ||||
179 | bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) { | ||||
180 | return !isa<AssumeInst>(CIU.getUser()); | ||||
181 | }); | ||||
182 | if (HasNonAssumeUses) | ||||
183 | TypeTests.insert(Guid); | ||||
184 | |||||
185 | SmallVector<DevirtCallSite, 4> DevirtCalls; | ||||
186 | SmallVector<CallInst *, 4> Assumes; | ||||
187 | findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT); | ||||
188 | for (auto &Call : DevirtCalls) | ||||
189 | addVCallToSet(Call, Guid, TypeTestAssumeVCalls, | ||||
190 | TypeTestAssumeConstVCalls); | ||||
191 | |||||
192 | break; | ||||
193 | } | ||||
194 | |||||
195 | case Intrinsic::type_checked_load: { | ||||
196 | auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2)); | ||||
197 | auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata()); | ||||
198 | if (!TypeId) | ||||
199 | break; | ||||
200 | GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString()); | ||||
201 | |||||
202 | SmallVector<DevirtCallSite, 4> DevirtCalls; | ||||
203 | SmallVector<Instruction *, 4> LoadedPtrs; | ||||
204 | SmallVector<Instruction *, 4> Preds; | ||||
205 | bool HasNonCallUses = false; | ||||
206 | findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds, | ||||
207 | HasNonCallUses, CI, DT); | ||||
208 | // Any non-call uses of the result of llvm.type.checked.load will | ||||
209 | // prevent us from optimizing away the llvm.type.test. | ||||
210 | if (HasNonCallUses) | ||||
211 | TypeTests.insert(Guid); | ||||
212 | for (auto &Call : DevirtCalls) | ||||
213 | addVCallToSet(Call, Guid, TypeCheckedLoadVCalls, | ||||
214 | TypeCheckedLoadConstVCalls); | ||||
215 | |||||
216 | break; | ||||
217 | } | ||||
218 | default: | ||||
219 | break; | ||||
220 | } | ||||
221 | } | ||||
222 | |||||
223 | static bool isNonVolatileLoad(const Instruction *I) { | ||||
224 | if (const auto *LI = dyn_cast<LoadInst>(I)) | ||||
225 | return !LI->isVolatile(); | ||||
226 | |||||
227 | return false; | ||||
228 | } | ||||
229 | |||||
230 | static bool isNonVolatileStore(const Instruction *I) { | ||||
231 | if (const auto *SI = dyn_cast<StoreInst>(I)) | ||||
232 | return !SI->isVolatile(); | ||||
233 | |||||
234 | return false; | ||||
235 | } | ||||
236 | |||||
237 | static void computeFunctionSummary( | ||||
238 | ModuleSummaryIndex &Index, const Module &M, const Function &F, | ||||
239 | BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, DominatorTree &DT, | ||||
240 | bool HasLocalsInUsedOrAsm, DenseSet<GlobalValue::GUID> &CantBePromoted, | ||||
241 | bool IsThinLTO, | ||||
242 | std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) { | ||||
243 | // Summary not currently supported for anonymous functions, they should | ||||
244 | // have been named. | ||||
245 | assert(F.hasName())((void)0); | ||||
246 | |||||
247 | unsigned NumInsts = 0; | ||||
248 | // Map from callee ValueId to profile count. Used to accumulate profile | ||||
249 | // counts for all static calls to a given callee. | ||||
250 | MapVector<ValueInfo, CalleeInfo> CallGraphEdges; | ||||
251 | SetVector<ValueInfo> RefEdges, LoadRefEdges, StoreRefEdges; | ||||
252 | SetVector<GlobalValue::GUID> TypeTests; | ||||
253 | SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls, | ||||
254 | TypeCheckedLoadVCalls; | ||||
255 | SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls, | ||||
256 | TypeCheckedLoadConstVCalls; | ||||
257 | ICallPromotionAnalysis ICallAnalysis; | ||||
258 | SmallPtrSet<const User *, 8> Visited; | ||||
259 | |||||
260 | // Add personality function, prefix data and prologue data to function's ref | ||||
261 | // list. | ||||
262 | findRefEdges(Index, &F, RefEdges, Visited); | ||||
263 | std::vector<const Instruction *> NonVolatileLoads; | ||||
264 | std::vector<const Instruction *> NonVolatileStores; | ||||
265 | |||||
266 | bool HasInlineAsmMaybeReferencingInternal = false; | ||||
267 | for (const BasicBlock &BB : F) | ||||
268 | for (const Instruction &I : BB) { | ||||
269 | if (isa<DbgInfoIntrinsic>(I)) | ||||
270 | continue; | ||||
271 | ++NumInsts; | ||||
272 | // Regular LTO module doesn't participate in ThinLTO import, | ||||
273 | // so no reference from it can be read/writeonly, since this | ||||
274 | // would require importing variable as local copy | ||||
275 | if (IsThinLTO) { | ||||
276 | if (isNonVolatileLoad(&I)) { | ||||
277 | // Postpone processing of non-volatile load instructions | ||||
278 | // See comments below | ||||
279 | Visited.insert(&I); | ||||
280 | NonVolatileLoads.push_back(&I); | ||||
281 | continue; | ||||
282 | } else if (isNonVolatileStore(&I)) { | ||||
283 | Visited.insert(&I); | ||||
284 | NonVolatileStores.push_back(&I); | ||||
285 | // All references from second operand of store (destination address) | ||||
286 | // can be considered write-only if they're not referenced by any | ||||
287 | // non-store instruction. References from first operand of store | ||||
288 | // (stored value) can't be treated either as read- or as write-only | ||||
289 | // so we add them to RefEdges as we do with all other instructions | ||||
290 | // except non-volatile load. | ||||
291 | Value *Stored = I.getOperand(0); | ||||
292 | if (auto *GV = dyn_cast<GlobalValue>(Stored)) | ||||
293 | // findRefEdges will try to examine GV operands, so instead | ||||
294 | // of calling it we should add GV to RefEdges directly. | ||||
295 | RefEdges.insert(Index.getOrInsertValueInfo(GV)); | ||||
296 | else if (auto *U = dyn_cast<User>(Stored)) | ||||
297 | findRefEdges(Index, U, RefEdges, Visited); | ||||
298 | continue; | ||||
299 | } | ||||
300 | } | ||||
301 | findRefEdges(Index, &I, RefEdges, Visited); | ||||
302 | const auto *CB = dyn_cast<CallBase>(&I); | ||||
303 | if (!CB) | ||||
304 | continue; | ||||
305 | |||||
306 | const auto *CI = dyn_cast<CallInst>(&I); | ||||
307 | // Since we don't know exactly which local values are referenced in inline | ||||
308 | // assembly, conservatively mark the function as possibly referencing | ||||
309 | // a local value from inline assembly to ensure we don't export a | ||||
310 | // reference (which would require renaming and promotion of the | ||||
311 | // referenced value). | ||||
312 | if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm()) | ||||
313 | HasInlineAsmMaybeReferencingInternal = true; | ||||
314 | |||||
315 | auto *CalledValue = CB->getCalledOperand(); | ||||
316 | auto *CalledFunction = CB->getCalledFunction(); | ||||
317 | if (CalledValue && !CalledFunction) { | ||||
318 | CalledValue = CalledValue->stripPointerCasts(); | ||||
319 | // Stripping pointer casts can reveal a called function. | ||||
320 | CalledFunction = dyn_cast<Function>(CalledValue); | ||||
321 | } | ||||
322 | // Check if this is an alias to a function. If so, get the | ||||
323 | // called aliasee for the checks below. | ||||
324 | if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) { | ||||
325 | assert(!CalledFunction && "Expected null called function in callsite for alias")((void)0); | ||||
326 | CalledFunction = dyn_cast<Function>(GA->getBaseObject()); | ||||
327 | } | ||||
328 | // Check if this is a direct call to a known function or a known | ||||
329 | // intrinsic, or an indirect call with profile data. | ||||
330 | if (CalledFunction) { | ||||
331 | if (CI && CalledFunction->isIntrinsic()) { | ||||
332 | addIntrinsicToSummary( | ||||
333 | CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls, | ||||
334 | TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT); | ||||
335 | continue; | ||||
336 | } | ||||
337 | // We should have named any anonymous globals | ||||
338 | assert(CalledFunction->hasName())((void)0); | ||||
339 | auto ScaledCount = PSI->getProfileCount(*CB, BFI); | ||||
340 | auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI) | ||||
341 | : CalleeInfo::HotnessType::Unknown; | ||||
342 | if (ForceSummaryEdgesCold != FunctionSummary::FSHT_None) | ||||
343 | Hotness = CalleeInfo::HotnessType::Cold; | ||||
344 | |||||
345 | // Use the original CalledValue, in case it was an alias. We want | ||||
346 | // to record the call edge to the alias in that case. Eventually | ||||
347 | // an alias summary will be created to associate the alias and | ||||
348 | // aliasee. | ||||
349 | auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo( | ||||
350 | cast<GlobalValue>(CalledValue))]; | ||||
351 | ValueInfo.updateHotness(Hotness); | ||||
352 | // Add the relative block frequency to CalleeInfo if there is no profile | ||||
353 | // information. | ||||
354 | if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) { | ||||
355 | uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency(); | ||||
356 | uint64_t EntryFreq = BFI->getEntryFreq(); | ||||
357 | ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq); | ||||
358 | } | ||||
359 | } else { | ||||
360 | // Skip inline assembly calls. | ||||
361 | if (CI && CI->isInlineAsm()) | ||||
362 | continue; | ||||
363 | // Skip direct calls. | ||||
364 | if (!CalledValue || isa<Constant>(CalledValue)) | ||||
365 | continue; | ||||
366 | |||||
367 | // Check if the instruction has a callees metadata. If so, add callees | ||||
368 | // to CallGraphEdges to reflect the references from the metadata, and | ||||
369 | // to enable importing for subsequent indirect call promotion and | ||||
370 | // inlining. | ||||
371 | if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) { | ||||
372 | for (auto &Op : MD->operands()) { | ||||
373 | Function *Callee = mdconst::extract_or_null<Function>(Op); | ||||
374 | if (Callee) | ||||
375 | CallGraphEdges[Index.getOrInsertValueInfo(Callee)]; | ||||
376 | } | ||||
377 | } | ||||
378 | |||||
379 | uint32_t NumVals, NumCandidates; | ||||
380 | uint64_t TotalCount; | ||||
381 | auto CandidateProfileData = | ||||
382 | ICallAnalysis.getPromotionCandidatesForInstruction( | ||||
383 | &I, NumVals, TotalCount, NumCandidates); | ||||
384 | for (auto &Candidate : CandidateProfileData) | ||||
385 | CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)] | ||||
386 | .updateHotness(getHotness(Candidate.Count, PSI)); | ||||
387 | } | ||||
388 | } | ||||
389 | Index.addBlockCount(F.size()); | ||||
390 | |||||
391 | std::vector<ValueInfo> Refs; | ||||
392 | if (IsThinLTO) { | ||||
393 | auto AddRefEdges = [&](const std::vector<const Instruction *> &Instrs, | ||||
394 | SetVector<ValueInfo> &Edges, | ||||
395 | SmallPtrSet<const User *, 8> &Cache) { | ||||
396 | for (const auto *I : Instrs) { | ||||
397 | Cache.erase(I); | ||||
398 | findRefEdges(Index, I, Edges, Cache); | ||||
399 | } | ||||
400 | }; | ||||
401 | |||||
402 | // By now we processed all instructions in a function, except | ||||
403 | // non-volatile loads and non-volatile value stores. Let's find | ||||
404 | // ref edges for both of instruction sets | ||||
405 | AddRefEdges(NonVolatileLoads, LoadRefEdges, Visited); | ||||
406 | // We can add some values to the Visited set when processing load | ||||
407 | // instructions which are also used by stores in NonVolatileStores. | ||||
408 | // For example this can happen if we have following code: | ||||
409 | // | ||||
410 | // store %Derived* @foo, %Derived** bitcast (%Base** @bar to %Derived**) | ||||
411 | // %42 = load %Derived*, %Derived** bitcast (%Base** @bar to %Derived**) | ||||
412 | // | ||||
413 | // After processing loads we'll add bitcast to the Visited set, and if | ||||
414 | // we use the same set while processing stores, we'll never see store | ||||
415 | // to @bar and @bar will be mistakenly treated as readonly. | ||||
416 | SmallPtrSet<const llvm::User *, 8> StoreCache; | ||||
417 | AddRefEdges(NonVolatileStores, StoreRefEdges, StoreCache); | ||||
418 | |||||
419 | // If both load and store instruction reference the same variable | ||||
420 | // we won't be able to optimize it. Add all such reference edges | ||||
421 | // to RefEdges set. | ||||
422 | for (auto &VI : StoreRefEdges) | ||||
423 | if (LoadRefEdges.remove(VI)) | ||||
424 | RefEdges.insert(VI); | ||||
425 | |||||
426 | unsigned RefCnt = RefEdges.size(); | ||||
427 | // All new reference edges inserted in two loops below are either | ||||
428 | // read or write only. They will be grouped in the end of RefEdges | ||||
429 | // vector, so we can use a single integer value to identify them. | ||||
430 | for (auto &VI : LoadRefEdges) | ||||
431 | RefEdges.insert(VI); | ||||
432 | |||||
433 | unsigned FirstWORef = RefEdges.size(); | ||||
434 | for (auto &VI : StoreRefEdges) | ||||
435 | RefEdges.insert(VI); | ||||
436 | |||||
437 | Refs = RefEdges.takeVector(); | ||||
438 | for (; RefCnt < FirstWORef; ++RefCnt) | ||||
439 | Refs[RefCnt].setReadOnly(); | ||||
440 | |||||
441 | for (; RefCnt < Refs.size(); ++RefCnt) | ||||
442 | Refs[RefCnt].setWriteOnly(); | ||||
443 | } else { | ||||
444 | Refs = RefEdges.takeVector(); | ||||
445 | } | ||||
446 | // Explicit add hot edges to enforce importing for designated GUIDs for | ||||
447 | // sample PGO, to enable the same inlines as the profiled optimized binary. | ||||
448 | for (auto &I : F.getImportGUIDs()) | ||||
449 | CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness( | ||||
450 | ForceSummaryEdgesCold == FunctionSummary::FSHT_All | ||||
451 | ? CalleeInfo::HotnessType::Cold | ||||
452 | : CalleeInfo::HotnessType::Critical); | ||||
453 | |||||
454 | bool NonRenamableLocal = isNonRenamableLocal(F); | ||||
455 | bool NotEligibleForImport = | ||||
456 | NonRenamableLocal || HasInlineAsmMaybeReferencingInternal; | ||||
457 | GlobalValueSummary::GVFlags Flags( | ||||
458 | F.getLinkage(), F.getVisibility(), NotEligibleForImport, | ||||
459 | /* Live = */ false, F.isDSOLocal(), | ||||
460 | F.hasLinkOnceODRLinkage() && F.hasGlobalUnnamedAddr()); | ||||
461 | FunctionSummary::FFlags FunFlags{ | ||||
462 | F.hasFnAttribute(Attribute::ReadNone), | ||||
463 | F.hasFnAttribute(Attribute::ReadOnly), | ||||
464 | F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(), | ||||
465 | // FIXME: refactor this to use the same code that inliner is using. | ||||
466 | // Don't try to import functions with noinline attribute. | ||||
467 | F.getAttributes().hasFnAttribute(Attribute::NoInline), | ||||
468 | F.hasFnAttribute(Attribute::AlwaysInline)}; | ||||
469 | std::vector<FunctionSummary::ParamAccess> ParamAccesses; | ||||
470 | if (auto *SSI = GetSSICallback(F)) | ||||
471 | ParamAccesses = SSI->getParamAccesses(Index); | ||||
472 | auto FuncSummary = std::make_unique<FunctionSummary>( | ||||
473 | Flags, NumInsts, FunFlags, /*EntryCount=*/0, std::move(Refs), | ||||
474 | CallGraphEdges.takeVector(), TypeTests.takeVector(), | ||||
475 | TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(), | ||||
476 | TypeTestAssumeConstVCalls.takeVector(), | ||||
477 | TypeCheckedLoadConstVCalls.takeVector(), std::move(ParamAccesses)); | ||||
478 | if (NonRenamableLocal) | ||||
479 | CantBePromoted.insert(F.getGUID()); | ||||
480 | Index.addGlobalValueSummary(F, std::move(FuncSummary)); | ||||
481 | } | ||||
482 | |||||
483 | /// Find function pointers referenced within the given vtable initializer | ||||
484 | /// (or subset of an initializer) \p I. The starting offset of \p I within | ||||
485 | /// the vtable initializer is \p StartingOffset. Any discovered function | ||||
486 | /// pointers are added to \p VTableFuncs along with their cumulative offset | ||||
487 | /// within the initializer. | ||||
488 | static void findFuncPointers(const Constant *I, uint64_t StartingOffset, | ||||
489 | const Module &M, ModuleSummaryIndex &Index, | ||||
490 | VTableFuncList &VTableFuncs) { | ||||
491 | // First check if this is a function pointer. | ||||
492 | if (I->getType()->isPointerTy()) { | ||||
493 | auto Fn = dyn_cast<Function>(I->stripPointerCasts()); | ||||
494 | // We can disregard __cxa_pure_virtual as a possible call target, as | ||||
495 | // calls to pure virtuals are UB. | ||||
496 | if (Fn && Fn->getName() != "__cxa_pure_virtual") | ||||
497 | VTableFuncs.push_back({Index.getOrInsertValueInfo(Fn), StartingOffset}); | ||||
498 | return; | ||||
499 | } | ||||
500 | |||||
501 | // Walk through the elements in the constant struct or array and recursively | ||||
502 | // look for virtual function pointers. | ||||
503 | const DataLayout &DL = M.getDataLayout(); | ||||
504 | if (auto *C
| ||||
505 | StructType *STy = dyn_cast<StructType>(C->getType()); | ||||
506 | assert(STy)((void)0); | ||||
507 | const StructLayout *SL = DL.getStructLayout(C->getType()); | ||||
508 | |||||
509 | for (auto EI : llvm::enumerate(STy->elements())) { | ||||
| |||||
510 | auto Offset = SL->getElementOffset(EI.index()); | ||||
511 | unsigned Op = SL->getElementContainingOffset(Offset); | ||||
512 | findFuncPointers(cast<Constant>(I->getOperand(Op)), | ||||
513 | StartingOffset + Offset, M, Index, VTableFuncs); | ||||
514 | } | ||||
515 | } else if (auto *C = dyn_cast<ConstantArray>(I)) { | ||||
516 | ArrayType *ATy = C->getType(); | ||||
517 | Type *EltTy = ATy->getElementType(); | ||||
518 | uint64_t EltSize = DL.getTypeAllocSize(EltTy); | ||||
519 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { | ||||
520 | findFuncPointers(cast<Constant>(I->getOperand(i)), | ||||
521 | StartingOffset + i * EltSize, M, Index, VTableFuncs); | ||||
522 | } | ||||
523 | } | ||||
524 | } | ||||
525 | |||||
526 | // Identify the function pointers referenced by vtable definition \p V. | ||||
527 | static void computeVTableFuncs(ModuleSummaryIndex &Index, | ||||
528 | const GlobalVariable &V, const Module &M, | ||||
529 | VTableFuncList &VTableFuncs) { | ||||
530 | if (!V.isConstant()) | ||||
531 | return; | ||||
532 | |||||
533 | findFuncPointers(V.getInitializer(), /*StartingOffset=*/0, M, Index, | ||||
534 | VTableFuncs); | ||||
535 | |||||
536 | #ifndef NDEBUG1 | ||||
537 | // Validate that the VTableFuncs list is ordered by offset. | ||||
538 | uint64_t PrevOffset = 0; | ||||
539 | for (auto &P : VTableFuncs) { | ||||
540 | // The findVFuncPointers traversal should have encountered the | ||||
541 | // functions in offset order. We need to use ">=" since PrevOffset | ||||
542 | // starts at 0. | ||||
543 | assert(P.VTableOffset >= PrevOffset)((void)0); | ||||
544 | PrevOffset = P.VTableOffset; | ||||
545 | } | ||||
546 | #endif | ||||
547 | } | ||||
548 | |||||
549 | /// Record vtable definition \p V for each type metadata it references. | ||||
550 | static void | ||||
551 | recordTypeIdCompatibleVtableReferences(ModuleSummaryIndex &Index, | ||||
552 | const GlobalVariable &V, | ||||
553 | SmallVectorImpl<MDNode *> &Types) { | ||||
554 | for (MDNode *Type : Types) { | ||||
555 | auto TypeID = Type->getOperand(1).get(); | ||||
556 | |||||
557 | uint64_t Offset = | ||||
558 | cast<ConstantInt>( | ||||
559 | cast<ConstantAsMetadata>(Type->getOperand(0))->getValue()) | ||||
560 | ->getZExtValue(); | ||||
561 | |||||
562 | if (auto *TypeId = dyn_cast<MDString>(TypeID)) | ||||
563 | Index.getOrInsertTypeIdCompatibleVtableSummary(TypeId->getString()) | ||||
564 | .push_back({Offset, Index.getOrInsertValueInfo(&V)}); | ||||
565 | } | ||||
566 | } | ||||
567 | |||||
568 | static void computeVariableSummary(ModuleSummaryIndex &Index, | ||||
569 | const GlobalVariable &V, | ||||
570 | DenseSet<GlobalValue::GUID> &CantBePromoted, | ||||
571 | const Module &M, | ||||
572 | SmallVectorImpl<MDNode *> &Types) { | ||||
573 | SetVector<ValueInfo> RefEdges; | ||||
574 | SmallPtrSet<const User *, 8> Visited; | ||||
575 | bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited); | ||||
576 | bool NonRenamableLocal = isNonRenamableLocal(V); | ||||
577 | GlobalValueSummary::GVFlags Flags( | ||||
578 | V.getLinkage(), V.getVisibility(), NonRenamableLocal, | ||||
579 | /* Live = */ false, V.isDSOLocal(), | ||||
580 | V.hasLinkOnceODRLinkage() && V.hasGlobalUnnamedAddr()); | ||||
581 | |||||
582 | VTableFuncList VTableFuncs; | ||||
583 | // If splitting is not enabled, then we compute the summary information | ||||
584 | // necessary for index-based whole program devirtualization. | ||||
585 | if (!Index.enableSplitLTOUnit()) { | ||||
586 | Types.clear(); | ||||
587 | V.getMetadata(LLVMContext::MD_type, Types); | ||||
588 | if (!Types.empty()) { | ||||
589 | // Identify the function pointers referenced by this vtable definition. | ||||
590 | computeVTableFuncs(Index, V, M, VTableFuncs); | ||||
591 | |||||
592 | // Record this vtable definition for each type metadata it references. | ||||
593 | recordTypeIdCompatibleVtableReferences(Index, V, Types); | ||||
594 | } | ||||
595 | } | ||||
596 | |||||
597 | // Don't mark variables we won't be able to internalize as read/write-only. | ||||
598 | bool CanBeInternalized = | ||||
599 | !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() && | ||||
600 | !V.hasAvailableExternallyLinkage() && !V.hasDLLExportStorageClass(); | ||||
601 | bool Constant = V.isConstant(); | ||||
602 | GlobalVarSummary::GVarFlags VarFlags(CanBeInternalized, | ||||
603 | Constant ? false : CanBeInternalized, | ||||
604 | Constant, V.getVCallVisibility()); | ||||
605 | auto GVarSummary = std::make_unique<GlobalVarSummary>(Flags, VarFlags, | ||||
606 | RefEdges.takeVector()); | ||||
607 | if (NonRenamableLocal) | ||||
608 | CantBePromoted.insert(V.getGUID()); | ||||
609 | if (HasBlockAddress) | ||||
610 | GVarSummary->setNotEligibleToImport(); | ||||
611 | if (!VTableFuncs.empty()) | ||||
612 | GVarSummary->setVTableFuncs(VTableFuncs); | ||||
613 | Index.addGlobalValueSummary(V, std::move(GVarSummary)); | ||||
614 | } | ||||
615 | |||||
616 | static void | ||||
617 | computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A, | ||||
618 | DenseSet<GlobalValue::GUID> &CantBePromoted) { | ||||
619 | bool NonRenamableLocal = isNonRenamableLocal(A); | ||||
620 | GlobalValueSummary::GVFlags Flags( | ||||
621 | A.getLinkage(), A.getVisibility(), NonRenamableLocal, | ||||
622 | /* Live = */ false, A.isDSOLocal(), | ||||
623 | A.hasLinkOnceODRLinkage() && A.hasGlobalUnnamedAddr()); | ||||
624 | auto AS = std::make_unique<AliasSummary>(Flags); | ||||
625 | auto *Aliasee = A.getBaseObject(); | ||||
626 | auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID()); | ||||
627 | assert(AliaseeVI && "Alias expects aliasee summary to be available")((void)0); | ||||
628 | assert(AliaseeVI.getSummaryList().size() == 1 &&((void)0) | ||||
629 | "Expected a single entry per aliasee in per-module index")((void)0); | ||||
630 | AS->setAliasee(AliaseeVI, AliaseeVI.getSummaryList()[0].get()); | ||||
631 | if (NonRenamableLocal) | ||||
632 | CantBePromoted.insert(A.getGUID()); | ||||
633 | Index.addGlobalValueSummary(A, std::move(AS)); | ||||
634 | } | ||||
635 | |||||
636 | // Set LiveRoot flag on entries matching the given value name. | ||||
637 | static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) { | ||||
638 | if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name))) | ||||
639 | for (auto &Summary : VI.getSummaryList()) | ||||
640 | Summary->setLive(true); | ||||
641 | } | ||||
642 | |||||
643 | ModuleSummaryIndex llvm::buildModuleSummaryIndex( | ||||
644 | const Module &M, | ||||
645 | std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback, | ||||
646 | ProfileSummaryInfo *PSI, | ||||
647 | std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) { | ||||
648 | assert(PSI)((void)0); | ||||
649 | bool EnableSplitLTOUnit = false; | ||||
650 | if (auto *MD
| ||||
651 | M.getModuleFlag("EnableSplitLTOUnit"))) | ||||
652 | EnableSplitLTOUnit = MD->getZExtValue(); | ||||
653 | ModuleSummaryIndex Index(/*HaveGVs=*/true, EnableSplitLTOUnit); | ||||
654 | |||||
655 | // Identify the local values in the llvm.used and llvm.compiler.used sets, | ||||
656 | // which should not be exported as they would then require renaming and | ||||
657 | // promotion, but we may have opaque uses e.g. in inline asm. We collect them | ||||
658 | // here because we use this information to mark functions containing inline | ||||
659 | // assembly calls as not importable. | ||||
660 | SmallPtrSet<GlobalValue *, 4> LocalsUsed; | ||||
661 | SmallVector<GlobalValue *, 4> Used; | ||||
662 | // First collect those in the llvm.used set. | ||||
663 | collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/false); | ||||
664 | // Next collect those in the llvm.compiler.used set. | ||||
665 | collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/true); | ||||
666 | DenseSet<GlobalValue::GUID> CantBePromoted; | ||||
667 | for (auto *V : Used) { | ||||
668 | if (V->hasLocalLinkage()) { | ||||
669 | LocalsUsed.insert(V); | ||||
670 | CantBePromoted.insert(V->getGUID()); | ||||
671 | } | ||||
672 | } | ||||
673 | |||||
674 | bool HasLocalInlineAsmSymbol = false; | ||||
675 | if (!M.getModuleInlineAsm().empty()) { | ||||
676 | // Collect the local values defined by module level asm, and set up | ||||
677 | // summaries for these symbols so that they can be marked as NoRename, | ||||
678 | // to prevent export of any use of them in regular IR that would require | ||||
679 | // renaming within the module level asm. Note we don't need to create a | ||||
680 | // summary for weak or global defs, as they don't need to be flagged as | ||||
681 | // NoRename, and defs in module level asm can't be imported anyway. | ||||
682 | // Also, any values used but not defined within module level asm should | ||||
683 | // be listed on the llvm.used or llvm.compiler.used global and marked as | ||||
684 | // referenced from there. | ||||
685 | ModuleSymbolTable::CollectAsmSymbols( | ||||
686 | M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) { | ||||
687 | // Symbols not marked as Weak or Global are local definitions. | ||||
688 | if (Flags & (object::BasicSymbolRef::SF_Weak | | ||||
689 | object::BasicSymbolRef::SF_Global)) | ||||
690 | return; | ||||
691 | HasLocalInlineAsmSymbol = true; | ||||
692 | GlobalValue *GV = M.getNamedValue(Name); | ||||
693 | if (!GV) | ||||
694 | return; | ||||
695 | assert(GV->isDeclaration() && "Def in module asm already has definition")((void)0); | ||||
696 | GlobalValueSummary::GVFlags GVFlags( | ||||
697 | GlobalValue::InternalLinkage, GlobalValue::DefaultVisibility, | ||||
698 | /* NotEligibleToImport = */ true, | ||||
699 | /* Live = */ true, | ||||
700 | /* Local */ GV->isDSOLocal(), | ||||
701 | GV->hasLinkOnceODRLinkage() && GV->hasGlobalUnnamedAddr()); | ||||
702 | CantBePromoted.insert(GV->getGUID()); | ||||
703 | // Create the appropriate summary type. | ||||
704 | if (Function *F = dyn_cast<Function>(GV)) { | ||||
705 | std::unique_ptr<FunctionSummary> Summary = | ||||
706 | std::make_unique<FunctionSummary>( | ||||
707 | GVFlags, /*InstCount=*/0, | ||||
708 | FunctionSummary::FFlags{ | ||||
709 | F->hasFnAttribute(Attribute::ReadNone), | ||||
710 | F->hasFnAttribute(Attribute::ReadOnly), | ||||
711 | F->hasFnAttribute(Attribute::NoRecurse), | ||||
712 | F->returnDoesNotAlias(), | ||||
713 | /* NoInline = */ false, | ||||
714 | F->hasFnAttribute(Attribute::AlwaysInline)}, | ||||
715 | /*EntryCount=*/0, ArrayRef<ValueInfo>{}, | ||||
716 | ArrayRef<FunctionSummary::EdgeTy>{}, | ||||
717 | ArrayRef<GlobalValue::GUID>{}, | ||||
718 | ArrayRef<FunctionSummary::VFuncId>{}, | ||||
719 | ArrayRef<FunctionSummary::VFuncId>{}, | ||||
720 | ArrayRef<FunctionSummary::ConstVCall>{}, | ||||
721 | ArrayRef<FunctionSummary::ConstVCall>{}, | ||||
722 | ArrayRef<FunctionSummary::ParamAccess>{}); | ||||
723 | Index.addGlobalValueSummary(*GV, std::move(Summary)); | ||||
724 | } else { | ||||
725 | std::unique_ptr<GlobalVarSummary> Summary = | ||||
726 | std::make_unique<GlobalVarSummary>( | ||||
727 | GVFlags, | ||||
728 | GlobalVarSummary::GVarFlags( | ||||
729 | false, false, cast<GlobalVariable>(GV)->isConstant(), | ||||
730 | GlobalObject::VCallVisibilityPublic), | ||||
731 | ArrayRef<ValueInfo>{}); | ||||
732 | Index.addGlobalValueSummary(*GV, std::move(Summary)); | ||||
733 | } | ||||
734 | }); | ||||
735 | } | ||||
736 | |||||
737 | bool IsThinLTO = true; | ||||
738 | if (auto *MD
| ||||
739 | mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO"))) | ||||
740 | IsThinLTO = MD->getZExtValue(); | ||||
741 | |||||
742 | // Compute summaries for all functions defined in module, and save in the | ||||
743 | // index. | ||||
744 | for (auto &F : M) { | ||||
745 | if (F.isDeclaration()) | ||||
746 | continue; | ||||
747 | |||||
748 | DominatorTree DT(const_cast<Function &>(F)); | ||||
749 | BlockFrequencyInfo *BFI = nullptr; | ||||
750 | std::unique_ptr<BlockFrequencyInfo> BFIPtr; | ||||
751 | if (GetBFICallback) | ||||
752 | BFI = GetBFICallback(F); | ||||
753 | else if (F.hasProfileData()) { | ||||
754 | LoopInfo LI{DT}; | ||||
755 | BranchProbabilityInfo BPI{F, LI}; | ||||
756 | BFIPtr = std::make_unique<BlockFrequencyInfo>(F, BPI, LI); | ||||
757 | BFI = BFIPtr.get(); | ||||
758 | } | ||||
759 | |||||
760 | computeFunctionSummary(Index, M, F, BFI, PSI, DT, | ||||
761 | !LocalsUsed.empty() || HasLocalInlineAsmSymbol, | ||||
762 | CantBePromoted, IsThinLTO, GetSSICallback); | ||||
763 | } | ||||
764 | |||||
765 | // Compute summaries for all variables defined in module, and save in the | ||||
766 | // index. | ||||
767 | SmallVector<MDNode *, 2> Types; | ||||
768 | for (const GlobalVariable &G : M.globals()) { | ||||
769 | if (G.isDeclaration()) | ||||
770 | continue; | ||||
771 | computeVariableSummary(Index, G, CantBePromoted, M, Types); | ||||
772 | } | ||||
773 | |||||
774 | // Compute summaries for all aliases defined in module, and save in the | ||||
775 | // index. | ||||
776 | for (const GlobalAlias &A : M.aliases()) | ||||
777 | computeAliasSummary(Index, A, CantBePromoted); | ||||
778 | |||||
779 | for (auto *V : LocalsUsed) { | ||||
780 | auto *Summary = Index.getGlobalValueSummary(*V); | ||||
781 | assert(Summary && "Missing summary for global value")((void)0); | ||||
782 | Summary->setNotEligibleToImport(); | ||||
783 | } | ||||
784 | |||||
785 | // The linker doesn't know about these LLVM produced values, so we need | ||||
786 | // to flag them as live in the index to ensure index-based dead value | ||||
787 | // analysis treats them as live roots of the analysis. | ||||
788 | setLiveRoot(Index, "llvm.used"); | ||||
789 | setLiveRoot(Index, "llvm.compiler.used"); | ||||
790 | setLiveRoot(Index, "llvm.global_ctors"); | ||||
791 | setLiveRoot(Index, "llvm.global_dtors"); | ||||
792 | setLiveRoot(Index, "llvm.global.annotations"); | ||||
793 | |||||
794 | for (auto &GlobalList : Index) { | ||||
795 | // Ignore entries for references that are undefined in the current module. | ||||
796 | if (GlobalList.second.SummaryList.empty()) | ||||
797 | continue; | ||||
798 | |||||
799 | assert(GlobalList.second.SummaryList.size() == 1 &&((void)0) | ||||
800 | "Expected module's index to have one summary per GUID")((void)0); | ||||
801 | auto &Summary = GlobalList.second.SummaryList[0]; | ||||
802 | if (!IsThinLTO) { | ||||
803 | Summary->setNotEligibleToImport(); | ||||
804 | continue; | ||||
805 | } | ||||
806 | |||||
807 | bool AllRefsCanBeExternallyReferenced = | ||||
808 | llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) { | ||||
809 | return !CantBePromoted.count(VI.getGUID()); | ||||
810 | }); | ||||
811 | if (!AllRefsCanBeExternallyReferenced) { | ||||
812 | Summary->setNotEligibleToImport(); | ||||
813 | continue; | ||||
814 | } | ||||
815 | |||||
816 | if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) { | ||||
817 | bool AllCallsCanBeExternallyReferenced = llvm::all_of( | ||||
818 | FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) { | ||||
819 | return !CantBePromoted.count(Edge.first.getGUID()); | ||||
820 | }); | ||||
821 | if (!AllCallsCanBeExternallyReferenced) | ||||
822 | Summary->setNotEligibleToImport(); | ||||
823 | } | ||||
824 | } | ||||
825 | |||||
826 | if (!ModuleSummaryDotFile.empty()) { | ||||
827 | std::error_code EC; | ||||
828 | raw_fd_ostream OSDot(ModuleSummaryDotFile, EC, sys::fs::OpenFlags::OF_None); | ||||
829 | if (EC) | ||||
830 | report_fatal_error(Twine("Failed to open dot file ") + | ||||
831 | ModuleSummaryDotFile + ": " + EC.message() + "\n"); | ||||
832 | Index.exportToDot(OSDot, {}); | ||||
833 | } | ||||
834 | |||||
835 | return Index; | ||||
836 | } | ||||
837 | |||||
838 | AnalysisKey ModuleSummaryIndexAnalysis::Key; | ||||
839 | |||||
840 | ModuleSummaryIndex | ||||
841 | ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) { | ||||
842 | ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); | ||||
843 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); | ||||
844 | bool NeedSSI = needsParamAccessSummary(M); | ||||
845 | return buildModuleSummaryIndex( | ||||
846 | M, | ||||
847 | [&FAM](const Function &F) { | ||||
848 | return &FAM.getResult<BlockFrequencyAnalysis>( | ||||
849 | *const_cast<Function *>(&F)); | ||||
850 | }, | ||||
851 | &PSI, | ||||
852 | [&FAM, NeedSSI](const Function &F) -> const StackSafetyInfo * { | ||||
853 | return NeedSSI ? &FAM.getResult<StackSafetyAnalysis>( | ||||
854 | const_cast<Function &>(F)) | ||||
855 | : nullptr; | ||||
856 | }); | ||||
857 | } | ||||
858 | |||||
859 | char ModuleSummaryIndexWrapperPass::ID = 0; | ||||
860 | |||||
861 | INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",static void *initializeModuleSummaryIndexWrapperPassPassOnce( PassRegistry &Registry) { | ||||
862 | "Module Summary Analysis", false, true)static void *initializeModuleSummaryIndexWrapperPassPassOnce( PassRegistry &Registry) { | ||||
863 | INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)initializeBlockFrequencyInfoWrapperPassPass(Registry); | ||||
864 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)initializeProfileSummaryInfoWrapperPassPass(Registry); | ||||
865 | INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass)initializeStackSafetyInfoWrapperPassPass(Registry); | ||||
866 | INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",PassInfo *PI = new PassInfo( "Module Summary Analysis", "module-summary-analysis" , &ModuleSummaryIndexWrapperPass::ID, PassInfo::NormalCtor_t (callDefaultCtor<ModuleSummaryIndexWrapperPass>), false , true); Registry.registerPass(*PI, true); return PI; } static llvm::once_flag InitializeModuleSummaryIndexWrapperPassPassFlag ; void llvm::initializeModuleSummaryIndexWrapperPassPass(PassRegistry &Registry) { llvm::call_once(InitializeModuleSummaryIndexWrapperPassPassFlag , initializeModuleSummaryIndexWrapperPassPassOnce, std::ref(Registry )); } | ||||
867 | "Module Summary Analysis", false, true)PassInfo *PI = new PassInfo( "Module Summary Analysis", "module-summary-analysis" , &ModuleSummaryIndexWrapperPass::ID, PassInfo::NormalCtor_t (callDefaultCtor<ModuleSummaryIndexWrapperPass>), false , true); Registry.registerPass(*PI, true); return PI; } static llvm::once_flag InitializeModuleSummaryIndexWrapperPassPassFlag ; void llvm::initializeModuleSummaryIndexWrapperPassPass(PassRegistry &Registry) { llvm::call_once(InitializeModuleSummaryIndexWrapperPassPassFlag , initializeModuleSummaryIndexWrapperPassPassOnce, std::ref(Registry )); } | ||||
868 | |||||
869 | ModulePass *llvm::createModuleSummaryIndexWrapperPass() { | ||||
870 | return new ModuleSummaryIndexWrapperPass(); | ||||
871 | } | ||||
872 | |||||
873 | ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass() | ||||
874 | : ModulePass(ID) { | ||||
875 | initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry()); | ||||
876 | } | ||||
877 | |||||
878 | bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) { | ||||
879 | auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); | ||||
880 | bool NeedSSI = needsParamAccessSummary(M); | ||||
881 | Index.emplace(buildModuleSummaryIndex( | ||||
| |||||
882 | M, | ||||
883 | [this](const Function &F) { | ||||
884 | return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>( | ||||
885 | *const_cast<Function *>(&F)) | ||||
886 | .getBFI()); | ||||
887 | }, | ||||
888 | PSI, | ||||
889 | [&](const Function &F) -> const StackSafetyInfo * { | ||||
890 | return NeedSSI ? &getAnalysis<StackSafetyInfoWrapperPass>( | ||||
891 | const_cast<Function &>(F)) | ||||
892 | .getResult() | ||||
893 | : nullptr; | ||||
894 | })); | ||||
895 | return false; | ||||
896 | } | ||||
897 | |||||
898 | bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) { | ||||
899 | Index.reset(); | ||||
900 | return false; | ||||
901 | } | ||||
902 | |||||
903 | void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { | ||||
904 | AU.setPreservesAll(); | ||||
905 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); | ||||
906 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); | ||||
907 | AU.addRequired<StackSafetyInfoWrapperPass>(); | ||||
908 | } | ||||
909 | |||||
910 | char ImmutableModuleSummaryIndexWrapperPass::ID = 0; | ||||
911 | |||||
912 | ImmutableModuleSummaryIndexWrapperPass::ImmutableModuleSummaryIndexWrapperPass( | ||||
913 | const ModuleSummaryIndex *Index) | ||||
914 | : ImmutablePass(ID), Index(Index) { | ||||
915 | initializeImmutableModuleSummaryIndexWrapperPassPass( | ||||
916 | *PassRegistry::getPassRegistry()); | ||||
917 | } | ||||
918 | |||||
919 | void ImmutableModuleSummaryIndexWrapperPass::getAnalysisUsage( | ||||
920 | AnalysisUsage &AU) const { | ||||
921 | AU.setPreservesAll(); | ||||
922 | } | ||||
923 | |||||
924 | ImmutablePass *llvm::createImmutableModuleSummaryIndexWrapperPass( | ||||
925 | const ModuleSummaryIndex *Index) { | ||||
926 | return new ImmutableModuleSummaryIndexWrapperPass(Index); | ||||
927 | } | ||||
928 | |||||
929 | INITIALIZE_PASS(ImmutableModuleSummaryIndexWrapperPass, "module-summary-info",static void *initializeImmutableModuleSummaryIndexWrapperPassPassOnce (PassRegistry &Registry) { PassInfo *PI = new PassInfo( "Module summary info" , "module-summary-info", &ImmutableModuleSummaryIndexWrapperPass ::ID, PassInfo::NormalCtor_t(callDefaultCtor<ImmutableModuleSummaryIndexWrapperPass >), false, true); Registry.registerPass(*PI, true); return PI; } static llvm::once_flag InitializeImmutableModuleSummaryIndexWrapperPassPassFlag ; void llvm::initializeImmutableModuleSummaryIndexWrapperPassPass (PassRegistry &Registry) { llvm::call_once(InitializeImmutableModuleSummaryIndexWrapperPassPassFlag , initializeImmutableModuleSummaryIndexWrapperPassPassOnce, std ::ref(Registry)); } | ||||
930 | "Module summary info", false, true)static void *initializeImmutableModuleSummaryIndexWrapperPassPassOnce (PassRegistry &Registry) { PassInfo *PI = new PassInfo( "Module summary info" , "module-summary-info", &ImmutableModuleSummaryIndexWrapperPass ::ID, PassInfo::NormalCtor_t(callDefaultCtor<ImmutableModuleSummaryIndexWrapperPass >), false, true); Registry.registerPass(*PI, true); return PI; } static llvm::once_flag InitializeImmutableModuleSummaryIndexWrapperPassPassFlag ; void llvm::initializeImmutableModuleSummaryIndexWrapperPassPass (PassRegistry &Registry) { llvm::call_once(InitializeImmutableModuleSummaryIndexWrapperPassPassFlag , initializeImmutableModuleSummaryIndexWrapperPassPassOnce, std ::ref(Registry)); } |
1 | //===- llvm/Type.h - Classes for handling data types ------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file contains the declaration of the Type class. For more "Type" |
10 | // stuff, look in DerivedTypes.h. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_IR_TYPE_H |
15 | #define LLVM_IR_TYPE_H |
16 | |
17 | #include "llvm/ADT/APFloat.h" |
18 | #include "llvm/ADT/ArrayRef.h" |
19 | #include "llvm/ADT/SmallPtrSet.h" |
20 | #include "llvm/Support/CBindingWrapping.h" |
21 | #include "llvm/Support/Casting.h" |
22 | #include "llvm/Support/Compiler.h" |
23 | #include "llvm/Support/ErrorHandling.h" |
24 | #include "llvm/Support/TypeSize.h" |
25 | #include <cassert> |
26 | #include <cstdint> |
27 | #include <iterator> |
28 | |
29 | namespace llvm { |
30 | |
31 | class IntegerType; |
32 | class LLVMContext; |
33 | class PointerType; |
34 | class raw_ostream; |
35 | class StringRef; |
36 | |
37 | /// The instances of the Type class are immutable: once they are created, |
38 | /// they are never changed. Also note that only one instance of a particular |
39 | /// type is ever created. Thus seeing if two types are equal is a matter of |
40 | /// doing a trivial pointer comparison. To enforce that no two equal instances |
41 | /// are created, Type instances can only be created via static factory methods |
42 | /// in class Type and in derived classes. Once allocated, Types are never |
43 | /// free'd. |
44 | /// |
45 | class Type { |
46 | public: |
47 | //===--------------------------------------------------------------------===// |
48 | /// Definitions of all of the base types for the Type system. Based on this |
49 | /// value, you can cast to a class defined in DerivedTypes.h. |
50 | /// Note: If you add an element to this, you need to add an element to the |
51 | /// Type::getPrimitiveType function, or else things will break! |
52 | /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding. |
53 | /// |
54 | enum TypeID { |
55 | // PrimitiveTypes |
56 | HalfTyID = 0, ///< 16-bit floating point type |
57 | BFloatTyID, ///< 16-bit floating point type (7-bit significand) |
58 | FloatTyID, ///< 32-bit floating point type |
59 | DoubleTyID, ///< 64-bit floating point type |
60 | X86_FP80TyID, ///< 80-bit floating point type (X87) |
61 | FP128TyID, ///< 128-bit floating point type (112-bit significand) |
62 | PPC_FP128TyID, ///< 128-bit floating point type (two 64-bits, PowerPC) |
63 | VoidTyID, ///< type with no size |
64 | LabelTyID, ///< Labels |
65 | MetadataTyID, ///< Metadata |
66 | X86_MMXTyID, ///< MMX vectors (64 bits, X86 specific) |
67 | X86_AMXTyID, ///< AMX vectors (8192 bits, X86 specific) |
68 | TokenTyID, ///< Tokens |
69 | |
70 | // Derived types... see DerivedTypes.h file. |
71 | IntegerTyID, ///< Arbitrary bit width integers |
72 | FunctionTyID, ///< Functions |
73 | PointerTyID, ///< Pointers |
74 | StructTyID, ///< Structures |
75 | ArrayTyID, ///< Arrays |
76 | FixedVectorTyID, ///< Fixed width SIMD vector type |
77 | ScalableVectorTyID ///< Scalable SIMD vector type |
78 | }; |
79 | |
80 | private: |
81 | /// This refers to the LLVMContext in which this type was uniqued. |
82 | LLVMContext &Context; |
83 | |
84 | TypeID ID : 8; // The current base type of this type. |
85 | unsigned SubclassData : 24; // Space for subclasses to store data. |
86 | // Note that this should be synchronized with |
87 | // MAX_INT_BITS value in IntegerType class. |
88 | |
89 | protected: |
90 | friend class LLVMContextImpl; |
91 | |
92 | explicit Type(LLVMContext &C, TypeID tid) |
93 | : Context(C), ID(tid), SubclassData(0) {} |
94 | ~Type() = default; |
95 | |
96 | unsigned getSubclassData() const { return SubclassData; } |
97 | |
98 | void setSubclassData(unsigned val) { |
99 | SubclassData = val; |
100 | // Ensure we don't have any accidental truncation. |
101 | assert(getSubclassData() == val && "Subclass data too large for field")((void)0); |
102 | } |
103 | |
104 | /// Keeps track of how many Type*'s there are in the ContainedTys list. |
105 | unsigned NumContainedTys = 0; |
106 | |
107 | /// A pointer to the array of Types contained by this Type. For example, this |
108 | /// includes the arguments of a function type, the elements of a structure, |
109 | /// the pointee of a pointer, the element type of an array, etc. This pointer |
110 | /// may be 0 for types that don't contain other types (Integer, Double, |
111 | /// Float). |
112 | Type * const *ContainedTys = nullptr; |
113 | |
114 | public: |
115 | /// Print the current type. |
116 | /// Omit the type details if \p NoDetails == true. |
117 | /// E.g., let %st = type { i32, i16 } |
118 | /// When \p NoDetails is true, we only print %st. |
119 | /// Put differently, \p NoDetails prints the type as if |
120 | /// inlined with the operands when printing an instruction. |
121 | void print(raw_ostream &O, bool IsForDebug = false, |
122 | bool NoDetails = false) const; |
123 | |
124 | void dump() const; |
125 | |
126 | /// Return the LLVMContext in which this type was uniqued. |
127 | LLVMContext &getContext() const { return Context; } |
128 | |
129 | //===--------------------------------------------------------------------===// |
130 | // Accessors for working with types. |
131 | // |
132 | |
133 | /// Return the type id for the type. This will return one of the TypeID enum |
134 | /// elements defined above. |
135 | TypeID getTypeID() const { return ID; } |
136 | |
137 | /// Return true if this is 'void'. |
138 | bool isVoidTy() const { return getTypeID() == VoidTyID; } |
139 | |
140 | /// Return true if this is 'half', a 16-bit IEEE fp type. |
141 | bool isHalfTy() const { return getTypeID() == HalfTyID; } |
142 | |
143 | /// Return true if this is 'bfloat', a 16-bit bfloat type. |
144 | bool isBFloatTy() const { return getTypeID() == BFloatTyID; } |
145 | |
146 | /// Return true if this is 'float', a 32-bit IEEE fp type. |
147 | bool isFloatTy() const { return getTypeID() == FloatTyID; } |
148 | |
149 | /// Return true if this is 'double', a 64-bit IEEE fp type. |
150 | bool isDoubleTy() const { return getTypeID() == DoubleTyID; } |
151 | |
152 | /// Return true if this is x86 long double. |
153 | bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; } |
154 | |
155 | /// Return true if this is 'fp128'. |
156 | bool isFP128Ty() const { return getTypeID() == FP128TyID; } |
157 | |
158 | /// Return true if this is powerpc long double. |
159 | bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; } |
160 | |
161 | /// Return true if this is one of the six floating-point types |
162 | bool isFloatingPointTy() const { |
163 | return getTypeID() == HalfTyID || getTypeID() == BFloatTyID || |
164 | getTypeID() == FloatTyID || getTypeID() == DoubleTyID || |
165 | getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID || |
166 | getTypeID() == PPC_FP128TyID; |
167 | } |
168 | |
169 | const fltSemantics &getFltSemantics() const { |
170 | switch (getTypeID()) { |
171 | case HalfTyID: return APFloat::IEEEhalf(); |
172 | case BFloatTyID: return APFloat::BFloat(); |
173 | case FloatTyID: return APFloat::IEEEsingle(); |
174 | case DoubleTyID: return APFloat::IEEEdouble(); |
175 | case X86_FP80TyID: return APFloat::x87DoubleExtended(); |
176 | case FP128TyID: return APFloat::IEEEquad(); |
177 | case PPC_FP128TyID: return APFloat::PPCDoubleDouble(); |
178 | default: llvm_unreachable("Invalid floating type")__builtin_unreachable(); |
179 | } |
180 | } |
181 | |
182 | /// Return true if this is X86 MMX. |
183 | bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; } |
184 | |
185 | /// Return true if this is X86 AMX. |
186 | bool isX86_AMXTy() const { return getTypeID() == X86_AMXTyID; } |
187 | |
188 | /// Return true if this is a FP type or a vector of FP. |
189 | bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); } |
190 | |
191 | /// Return true if this is 'label'. |
192 | bool isLabelTy() const { return getTypeID() == LabelTyID; } |
193 | |
194 | /// Return true if this is 'metadata'. |
195 | bool isMetadataTy() const { return getTypeID() == MetadataTyID; } |
196 | |
197 | /// Return true if this is 'token'. |
198 | bool isTokenTy() const { return getTypeID() == TokenTyID; } |
199 | |
200 | /// True if this is an instance of IntegerType. |
201 | bool isIntegerTy() const { return getTypeID() == IntegerTyID; } |
202 | |
203 | /// Return true if this is an IntegerType of the given width. |
204 | bool isIntegerTy(unsigned Bitwidth) const; |
205 | |
206 | /// Return true if this is an integer type or a vector of integer types. |
207 | bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); } |
208 | |
209 | /// Return true if this is an integer type or a vector of integer types of |
210 | /// the given width. |
211 | bool isIntOrIntVectorTy(unsigned BitWidth) const { |
212 | return getScalarType()->isIntegerTy(BitWidth); |
213 | } |
214 | |
215 | /// Return true if this is an integer type or a pointer type. |
216 | bool isIntOrPtrTy() const { return isIntegerTy() || isPointerTy(); } |
217 | |
218 | /// True if this is an instance of FunctionType. |
219 | bool isFunctionTy() const { return getTypeID() == FunctionTyID; } |
220 | |
221 | /// True if this is an instance of StructType. |
222 | bool isStructTy() const { return getTypeID() == StructTyID; } |
223 | |
224 | /// True if this is an instance of ArrayType. |
225 | bool isArrayTy() const { return getTypeID() == ArrayTyID; } |
226 | |
227 | /// True if this is an instance of PointerType. |
228 | bool isPointerTy() const { return getTypeID() == PointerTyID; } |
229 | |
230 | /// True if this is an instance of an opaque PointerType. |
231 | bool isOpaquePointerTy() const; |
232 | |
233 | /// Return true if this is a pointer type or a vector of pointer types. |
234 | bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); } |
235 | |
236 | /// True if this is an instance of VectorType. |
237 | inline bool isVectorTy() const { |
238 | return getTypeID() == ScalableVectorTyID || getTypeID() == FixedVectorTyID; |
239 | } |
240 | |
241 | /// Return true if this type could be converted with a lossless BitCast to |
242 | /// type 'Ty'. For example, i8* to i32*. BitCasts are valid for types of the |
243 | /// same size only where no re-interpretation of the bits is done. |
244 | /// Determine if this type could be losslessly bitcast to Ty |
245 | bool canLosslesslyBitCastTo(Type *Ty) const; |
246 | |
247 | /// Return true if this type is empty, that is, it has no elements or all of |
248 | /// its elements are empty. |
249 | bool isEmptyTy() const; |
250 | |
251 | /// Return true if the type is "first class", meaning it is a valid type for a |
252 | /// Value. |
253 | bool isFirstClassType() const { |
254 | return getTypeID() != FunctionTyID && getTypeID() != VoidTyID; |
255 | } |
256 | |
257 | /// Return true if the type is a valid type for a register in codegen. This |
258 | /// includes all first-class types except struct and array types. |
259 | bool isSingleValueType() const { |
260 | return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() || |
261 | isPointerTy() || isVectorTy() || isX86_AMXTy(); |
262 | } |
263 | |
264 | /// Return true if the type is an aggregate type. This means it is valid as |
265 | /// the first operand of an insertvalue or extractvalue instruction. This |
266 | /// includes struct and array types, but does not include vector types. |
267 | bool isAggregateType() const { |
268 | return getTypeID() == StructTyID || getTypeID() == ArrayTyID; |
269 | } |
270 | |
271 | /// Return true if it makes sense to take the size of this type. To get the |
272 | /// actual size for a particular target, it is reasonable to use the |
273 | /// DataLayout subsystem to do this. |
274 | bool isSized(SmallPtrSetImpl<Type*> *Visited = nullptr) const { |
275 | // If it's a primitive, it is always sized. |
276 | if (getTypeID() == IntegerTyID || isFloatingPointTy() || |
277 | getTypeID() == PointerTyID || getTypeID() == X86_MMXTyID || |
278 | getTypeID() == X86_AMXTyID) |
279 | return true; |
280 | // If it is not something that can have a size (e.g. a function or label), |
281 | // it doesn't have a size. |
282 | if (getTypeID() != StructTyID && getTypeID() != ArrayTyID && !isVectorTy()) |
283 | return false; |
284 | // Otherwise we have to try harder to decide. |
285 | return isSizedDerivedType(Visited); |
286 | } |
287 | |
288 | /// Return the basic size of this type if it is a primitive type. These are |
289 | /// fixed by LLVM and are not target-dependent. |
290 | /// This will return zero if the type does not have a size or is not a |
291 | /// primitive type. |
292 | /// |
293 | /// If this is a scalable vector type, the scalable property will be set and |
294 | /// the runtime size will be a positive integer multiple of the base size. |
295 | /// |
296 | /// Note that this may not reflect the size of memory allocated for an |
297 | /// instance of the type or the number of bytes that are written when an |
298 | /// instance of the type is stored to memory. The DataLayout class provides |
299 | /// additional query functions to provide this information. |
300 | /// |
301 | TypeSize getPrimitiveSizeInBits() const LLVM_READONLY__attribute__((__pure__)); |
302 | |
303 | /// If this is a vector type, return the getPrimitiveSizeInBits value for the |
304 | /// element type. Otherwise return the getPrimitiveSizeInBits value for this |
305 | /// type. |
306 | unsigned getScalarSizeInBits() const LLVM_READONLY__attribute__((__pure__)); |
307 | |
308 | /// Return the width of the mantissa of this type. This is only valid on |
309 | /// floating-point types. If the FP type does not have a stable mantissa (e.g. |
310 | /// ppc long double), this method returns -1. |
311 | int getFPMantissaWidth() const; |
312 | |
313 | /// Return whether the type is IEEE compatible, as defined by the eponymous |
314 | /// method in APFloat. |
315 | bool isIEEE() const { return APFloat::getZero(getFltSemantics()).isIEEE(); } |
316 | |
317 | /// If this is a vector type, return the element type, otherwise return |
318 | /// 'this'. |
319 | inline Type *getScalarType() const { |
320 | if (isVectorTy()) |
321 | return getContainedType(0); |
322 | return const_cast<Type *>(this); |
323 | } |
324 | |
325 | //===--------------------------------------------------------------------===// |
326 | // Type Iteration support. |
327 | // |
328 | using subtype_iterator = Type * const *; |
329 | |
330 | subtype_iterator subtype_begin() const { return ContainedTys; } |
331 | subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];} |
332 | ArrayRef<Type*> subtypes() const { |
333 | return makeArrayRef(subtype_begin(), subtype_end()); |
334 | } |
335 | |
336 | using subtype_reverse_iterator = std::reverse_iterator<subtype_iterator>; |
337 | |
338 | subtype_reverse_iterator subtype_rbegin() const { |
339 | return subtype_reverse_iterator(subtype_end()); |
340 | } |
341 | subtype_reverse_iterator subtype_rend() const { |
342 | return subtype_reverse_iterator(subtype_begin()); |
343 | } |
344 | |
345 | /// This method is used to implement the type iterator (defined at the end of |
346 | /// the file). For derived types, this returns the types 'contained' in the |
347 | /// derived type. |
348 | Type *getContainedType(unsigned i) const { |
349 | assert(i < NumContainedTys && "Index out of range!")((void)0); |
350 | return ContainedTys[i]; |
351 | } |
352 | |
353 | /// Return the number of types in the derived type. |
354 | unsigned getNumContainedTypes() const { return NumContainedTys; } |
355 | |
356 | //===--------------------------------------------------------------------===// |
357 | // Helper methods corresponding to subclass methods. This forces a cast to |
358 | // the specified subclass and calls its accessor. "getArrayNumElements" (for |
359 | // example) is shorthand for cast<ArrayType>(Ty)->getNumElements(). This is |
360 | // only intended to cover the core methods that are frequently used, helper |
361 | // methods should not be added here. |
362 | |
363 | inline unsigned getIntegerBitWidth() const; |
364 | |
365 | inline Type *getFunctionParamType(unsigned i) const; |
366 | inline unsigned getFunctionNumParams() const; |
367 | inline bool isFunctionVarArg() const; |
368 | |
369 | inline StringRef getStructName() const; |
370 | inline unsigned getStructNumElements() const; |
371 | inline Type *getStructElementType(unsigned N) const; |
372 | |
373 | inline uint64_t getArrayNumElements() const; |
374 | |
375 | Type *getArrayElementType() const { |
376 | assert(getTypeID() == ArrayTyID)((void)0); |
377 | return ContainedTys[0]; |
378 | } |
379 | |
380 | Type *getPointerElementType() const { |
381 | assert(getTypeID() == PointerTyID)((void)0); |
382 | return ContainedTys[0]; |
383 | } |
384 | |
385 | /// Given vector type, change the element type, |
386 | /// whilst keeping the old number of elements. |
387 | /// For non-vectors simply returns \p EltTy. |
388 | inline Type *getWithNewType(Type *EltTy) const; |
389 | |
390 | /// Given an integer or vector type, change the lane bitwidth to NewBitwidth, |
391 | /// whilst keeping the old number of lanes. |
392 | inline Type *getWithNewBitWidth(unsigned NewBitWidth) const; |
393 | |
394 | /// Given scalar/vector integer type, returns a type with elements twice as |
395 | /// wide as in the original type. For vectors, preserves element count. |
396 | inline Type *getExtendedType() const; |
397 | |
398 | /// Get the address space of this pointer or pointer vector type. |
399 | inline unsigned getPointerAddressSpace() const; |
400 | |
401 | //===--------------------------------------------------------------------===// |
402 | // Static members exported by the Type class itself. Useful for getting |
403 | // instances of Type. |
404 | // |
405 | |
406 | /// Return a type based on an identifier. |
407 | static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber); |
408 | |
409 | //===--------------------------------------------------------------------===// |
410 | // These are the builtin types that are always available. |
411 | // |
412 | static Type *getVoidTy(LLVMContext &C); |
413 | static Type *getLabelTy(LLVMContext &C); |
414 | static Type *getHalfTy(LLVMContext &C); |
415 | static Type *getBFloatTy(LLVMContext &C); |
416 | static Type *getFloatTy(LLVMContext &C); |
417 | static Type *getDoubleTy(LLVMContext &C); |
418 | static Type *getMetadataTy(LLVMContext &C); |
419 | static Type *getX86_FP80Ty(LLVMContext &C); |
420 | static Type *getFP128Ty(LLVMContext &C); |
421 | static Type *getPPC_FP128Ty(LLVMContext &C); |
422 | static Type *getX86_MMXTy(LLVMContext &C); |
423 | static Type *getX86_AMXTy(LLVMContext &C); |
424 | static Type *getTokenTy(LLVMContext &C); |
425 | static IntegerType *getIntNTy(LLVMContext &C, unsigned N); |
426 | static IntegerType *getInt1Ty(LLVMContext &C); |
427 | static IntegerType *getInt8Ty(LLVMContext &C); |
428 | static IntegerType *getInt16Ty(LLVMContext &C); |
429 | static IntegerType *getInt32Ty(LLVMContext &C); |
430 | static IntegerType *getInt64Ty(LLVMContext &C); |
431 | static IntegerType *getInt128Ty(LLVMContext &C); |
432 | template <typename ScalarTy> static Type *getScalarTy(LLVMContext &C) { |
433 | int noOfBits = sizeof(ScalarTy) * CHAR_BIT8; |
434 | if (std::is_integral<ScalarTy>::value) { |
435 | return (Type*) Type::getIntNTy(C, noOfBits); |
436 | } else if (std::is_floating_point<ScalarTy>::value) { |
437 | switch (noOfBits) { |
438 | case 32: |
439 | return Type::getFloatTy(C); |
440 | case 64: |
441 | return Type::getDoubleTy(C); |
442 | } |
443 | } |
444 | llvm_unreachable("Unsupported type in Type::getScalarTy")__builtin_unreachable(); |
445 | } |
446 | static Type *getFloatingPointTy(LLVMContext &C, const fltSemantics &S) { |
447 | Type *Ty; |
448 | if (&S == &APFloat::IEEEhalf()) |
449 | Ty = Type::getHalfTy(C); |
450 | else if (&S == &APFloat::BFloat()) |
451 | Ty = Type::getBFloatTy(C); |
452 | else if (&S == &APFloat::IEEEsingle()) |
453 | Ty = Type::getFloatTy(C); |
454 | else if (&S == &APFloat::IEEEdouble()) |
455 | Ty = Type::getDoubleTy(C); |
456 | else if (&S == &APFloat::x87DoubleExtended()) |
457 | Ty = Type::getX86_FP80Ty(C); |
458 | else if (&S == &APFloat::IEEEquad()) |
459 | Ty = Type::getFP128Ty(C); |
460 | else { |
461 | assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format")((void)0); |
462 | Ty = Type::getPPC_FP128Ty(C); |
463 | } |
464 | return Ty; |
465 | } |
466 | |
467 | //===--------------------------------------------------------------------===// |
468 | // Convenience methods for getting pointer types with one of the above builtin |
469 | // types as pointee. |
470 | // |
471 | static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0); |
472 | static PointerType *getBFloatPtrTy(LLVMContext &C, unsigned AS = 0); |
473 | static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0); |
474 | static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0); |
475 | static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0); |
476 | static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0); |
477 | static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0); |
478 | static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0); |
479 | static PointerType *getX86_AMXPtrTy(LLVMContext &C, unsigned AS = 0); |
480 | static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0); |
481 | static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0); |
482 | static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0); |
483 | static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0); |
484 | static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0); |
485 | static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0); |
486 | |
487 | /// Return a pointer to the current type. This is equivalent to |
488 | /// PointerType::get(Foo, AddrSpace). |
489 | /// TODO: Remove this after opaque pointer transition is complete. |
490 | PointerType *getPointerTo(unsigned AddrSpace = 0) const; |
491 | |
492 | private: |
493 | /// Derived types like structures and arrays are sized iff all of the members |
494 | /// of the type are sized as well. Since asking for their size is relatively |
495 | /// uncommon, move this operation out-of-line. |
496 | bool isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited = nullptr) const; |
497 | }; |
498 | |
499 | // Printing of types. |
500 | inline raw_ostream &operator<<(raw_ostream &OS, const Type &T) { |
501 | T.print(OS); |
502 | return OS; |
503 | } |
504 | |
505 | // allow isa<PointerType>(x) to work without DerivedTypes.h included. |
506 | template <> struct isa_impl<PointerType, Type> { |
507 | static inline bool doit(const Type &Ty) { |
508 | return Ty.getTypeID() == Type::PointerTyID; |
509 | } |
510 | }; |
511 | |
512 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
513 | DEFINE_ISA_CONVERSION_FUNCTIONS(Type, LLVMTypeRef)inline Type *unwrap(LLVMTypeRef P) { return reinterpret_cast< Type*>(P); } inline LLVMTypeRef wrap(const Type *P) { return reinterpret_cast<LLVMTypeRef>(const_cast<Type*>( P)); } template<typename T> inline T *unwrap(LLVMTypeRef P) { return cast<T>(unwrap(P)); } |
514 | |
515 | /* Specialized opaque type conversions. |
516 | */ |
517 | inline Type **unwrap(LLVMTypeRef* Tys) { |
518 | return reinterpret_cast<Type**>(Tys); |
519 | } |
520 | |
521 | inline LLVMTypeRef *wrap(Type **Tys) { |
522 | return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys)); |
523 | } |
524 | |
525 | } // end namespace llvm |
526 | |
527 | #endif // LLVM_IR_TYPE_H |