/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/IR/GlobalValue.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- llvm/GlobalValue.h - Class to represent a global value --*- 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 is a common base class of all globally definable objects. As such, |
10 | | // it is subclassed by GlobalVariable, GlobalAlias and by Function. This is |
11 | | // used because you can do certain things with these global objects that you |
12 | | // can't do to anything else. For example, use the address of one as a |
13 | | // constant. |
14 | | // |
15 | | //===----------------------------------------------------------------------===// |
16 | | |
17 | | #ifndef LLVM_IR_GLOBALVALUE_H |
18 | | #define LLVM_IR_GLOBALVALUE_H |
19 | | |
20 | | #include "llvm/ADT/StringRef.h" |
21 | | #include "llvm/ADT/Twine.h" |
22 | | #include "llvm/IR/Constant.h" |
23 | | #include "llvm/IR/DerivedTypes.h" |
24 | | #include "llvm/IR/Value.h" |
25 | | #include "llvm/Support/Casting.h" |
26 | | #include "llvm/Support/ErrorHandling.h" |
27 | | #include "llvm/Support/MD5.h" |
28 | | #include <cassert> |
29 | | #include <cstdint> |
30 | | #include <string> |
31 | | |
32 | | namespace llvm { |
33 | | |
34 | | class Comdat; |
35 | | class ConstantRange; |
36 | | class Error; |
37 | | class GlobalObject; |
38 | | class Module; |
39 | | |
40 | | namespace Intrinsic { |
41 | | enum ID : unsigned; |
42 | | } // end namespace Intrinsic |
43 | | |
44 | | class GlobalValue : public Constant { |
45 | | public: |
46 | | /// An enumeration for the kinds of linkage for global values. |
47 | | enum LinkageTypes { |
48 | | ExternalLinkage = 0,///< Externally visible function |
49 | | AvailableExternallyLinkage, ///< Available for inspection, not emission. |
50 | | LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline) |
51 | | LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent. |
52 | | WeakAnyLinkage, ///< Keep one copy of named function when linking (weak) |
53 | | WeakODRLinkage, ///< Same, but only replaced by something equivalent. |
54 | | AppendingLinkage, ///< Special purpose, only applies to global arrays |
55 | | InternalLinkage, ///< Rename collisions when linking (static functions). |
56 | | PrivateLinkage, ///< Like Internal, but omit from symbol table. |
57 | | ExternalWeakLinkage,///< ExternalWeak linkage description. |
58 | | CommonLinkage ///< Tentative definitions. |
59 | | }; |
60 | | |
61 | | /// An enumeration for the kinds of visibility of global values. |
62 | | enum VisibilityTypes { |
63 | | DefaultVisibility = 0, ///< The GV is visible |
64 | | HiddenVisibility, ///< The GV is hidden |
65 | | ProtectedVisibility ///< The GV is protected |
66 | | }; |
67 | | |
68 | | /// Storage classes of global values for PE targets. |
69 | | enum DLLStorageClassTypes { |
70 | | DefaultStorageClass = 0, |
71 | | DLLImportStorageClass = 1, ///< Function to be imported from DLL |
72 | | DLLExportStorageClass = 2 ///< Function to be accessible from DLL. |
73 | | }; |
74 | | |
75 | | protected: |
76 | | GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, |
77 | | LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace) |
78 | | : Constant(PointerType::get(Ty, AddressSpace), VTy, Ops, NumOps), |
79 | | ValueType(Ty), Visibility(DefaultVisibility), |
80 | | UnnamedAddrVal(unsigned(UnnamedAddr::None)), |
81 | | DllStorageClass(DefaultStorageClass), ThreadLocal(NotThreadLocal), |
82 | | HasLLVMReservedName(false), IsDSOLocal(false), HasPartition(false), |
83 | 2.80M | IntID((Intrinsic::ID)0U), Parent(nullptr) { |
84 | 2.80M | setLinkage(Linkage); |
85 | 2.80M | setName(Name); |
86 | 2.80M | } |
87 | | |
88 | | Type *ValueType; |
89 | | |
90 | | static const unsigned GlobalValueSubClassDataBits = 16; |
91 | | |
92 | | // All bitfields use unsigned as the underlying type so that MSVC will pack |
93 | | // them. |
94 | | unsigned Linkage : 4; // The linkage of this global |
95 | | unsigned Visibility : 2; // The visibility style of this global |
96 | | unsigned UnnamedAddrVal : 2; // This value's address is not significant |
97 | | unsigned DllStorageClass : 2; // DLL storage class |
98 | | |
99 | | unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is |
100 | | // the desired model? |
101 | | |
102 | | /// True if the function's name starts with "llvm.". This corresponds to the |
103 | | /// value of Function::isIntrinsic(), which may be true even if |
104 | | /// Function::intrinsicID() returns Intrinsic::not_intrinsic. |
105 | | unsigned HasLLVMReservedName : 1; |
106 | | |
107 | | /// If true then there is a definition within the same linkage unit and that |
108 | | /// definition cannot be runtime preempted. |
109 | | unsigned IsDSOLocal : 1; |
110 | | |
111 | | /// True if this symbol has a partition name assigned (see |
112 | | /// https://lld.llvm.org/Partitions.html). |
113 | | unsigned HasPartition : 1; |
114 | | |
115 | | private: |
116 | | // Give subclasses access to what otherwise would be wasted padding. |
117 | | // (16 + 4 + 2 + 2 + 2 + 3 + 1 + 1 + 1) == 32. |
118 | | unsigned SubClassData : GlobalValueSubClassDataBits; |
119 | | |
120 | | friend class Constant; |
121 | | |
122 | | void destroyConstantImpl(); |
123 | | Value *handleOperandChangeImpl(Value *From, Value *To); |
124 | | |
125 | | /// Returns true if the definition of this global may be replaced by a |
126 | | /// differently optimized variant of the same source level function at link |
127 | | /// time. |
128 | 9.22M | bool mayBeDerefined() const { |
129 | 9.22M | switch (getLinkage()) { |
130 | 9.22M | case WeakODRLinkage: |
131 | 3.16M | case LinkOnceODRLinkage: |
132 | 3.16M | case AvailableExternallyLinkage: |
133 | 3.16M | return true; |
134 | 3.16M | |
135 | 6.06M | case WeakAnyLinkage: |
136 | 6.06M | case LinkOnceAnyLinkage: |
137 | 6.06M | case CommonLinkage: |
138 | 6.06M | case ExternalWeakLinkage: |
139 | 6.06M | case ExternalLinkage: |
140 | 6.06M | case AppendingLinkage: |
141 | 6.06M | case InternalLinkage: |
142 | 6.06M | case PrivateLinkage: |
143 | 6.06M | return isInterposable(); |
144 | 0 | } |
145 | 0 | |
146 | 0 | llvm_unreachable("Fully covered switch above!"); |
147 | 0 | } |
148 | | |
149 | 7.34M | void maybeSetDsoLocal() { |
150 | 7.34M | if (hasLocalLinkage() || |
151 | 7.34M | (6.35M !hasDefaultVisibility()6.35M && !hasExternalWeakLinkage()358k )) |
152 | 1.35M | setDSOLocal(true); |
153 | 7.34M | } |
154 | | |
155 | | protected: |
156 | | /// The intrinsic ID for this subclass (which must be a Function). |
157 | | /// |
158 | | /// This member is defined by this class, but not used for anything. |
159 | | /// Subclasses can use it to store their intrinsic ID, if they have one. |
160 | | /// |
161 | | /// This is stored here to save space in Function on 64-bit hosts. |
162 | | Intrinsic::ID IntID; |
163 | | |
164 | 80.5M | unsigned getGlobalValueSubClassData() const { |
165 | 80.5M | return SubClassData; |
166 | 80.5M | } |
167 | 10.5M | void setGlobalValueSubClassData(unsigned V) { |
168 | 10.5M | assert(V < (1 << GlobalValueSubClassDataBits) && "It will not fit"); |
169 | 10.5M | SubClassData = V; |
170 | 10.5M | } |
171 | | |
172 | | Module *Parent; // The containing module. |
173 | | |
174 | | // Used by SymbolTableListTraits. |
175 | 4.71M | void setParent(Module *parent) { |
176 | 4.71M | Parent = parent; |
177 | 4.71M | } |
178 | | |
179 | 1.91M | ~GlobalValue() { |
180 | 1.91M | removeDeadConstantUsers(); // remove any dead constants using this. |
181 | 1.91M | } |
182 | | |
183 | | public: |
184 | | enum ThreadLocalMode { |
185 | | NotThreadLocal = 0, |
186 | | GeneralDynamicTLSModel, |
187 | | LocalDynamicTLSModel, |
188 | | InitialExecTLSModel, |
189 | | LocalExecTLSModel |
190 | | }; |
191 | | |
192 | | GlobalValue(const GlobalValue &) = delete; |
193 | | |
194 | | unsigned getAlignment() const; |
195 | | unsigned getAddressSpace() const; |
196 | | |
197 | | enum class UnnamedAddr { |
198 | | None, |
199 | | Local, |
200 | | Global, |
201 | | }; |
202 | | |
203 | 3.04M | bool hasGlobalUnnamedAddr() const { |
204 | 3.04M | return getUnnamedAddr() == UnnamedAddr::Global; |
205 | 3.04M | } |
206 | | |
207 | | /// Returns true if this value's address is not significant in this module. |
208 | | /// This attribute is intended to be used only by the code generator and LTO |
209 | | /// to allow the linker to decide whether the global needs to be in the symbol |
210 | | /// table. It should probably not be used in optimizations, as the value may |
211 | | /// have uses outside the module; use hasGlobalUnnamedAddr() instead. |
212 | 10.6k | bool hasAtLeastLocalUnnamedAddr() const { |
213 | 10.6k | return getUnnamedAddr() != UnnamedAddr::None; |
214 | 10.6k | } |
215 | | |
216 | 5.29M | UnnamedAddr getUnnamedAddr() const { |
217 | 5.29M | return UnnamedAddr(UnnamedAddrVal); |
218 | 5.29M | } |
219 | 2.41M | void setUnnamedAddr(UnnamedAddr Val) { UnnamedAddrVal = unsigned(Val); } |
220 | | |
221 | 223 | static UnnamedAddr getMinUnnamedAddr(UnnamedAddr A, UnnamedAddr B) { |
222 | 223 | if (A == UnnamedAddr::None || B == UnnamedAddr::None8 ) |
223 | 218 | return UnnamedAddr::None; |
224 | 5 | if (A == UnnamedAddr::Local || B == UnnamedAddr::Local) |
225 | 0 | return UnnamedAddr::Local; |
226 | 5 | return UnnamedAddr::Global; |
227 | 5 | } |
228 | | |
229 | 371k | bool hasComdat() const { return getComdat() != nullptr; } |
230 | | const Comdat *getComdat() const; |
231 | 5.53M | Comdat *getComdat() { |
232 | 5.53M | return const_cast<Comdat *>( |
233 | 5.53M | static_cast<const GlobalValue *>(this)->getComdat()); |
234 | 5.53M | } |
235 | | |
236 | 1.62M | VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); } |
237 | 11.9M | bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; } |
238 | 18.5k | bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; } |
239 | 499 | bool hasProtectedVisibility() const { |
240 | 499 | return Visibility == ProtectedVisibility; |
241 | 499 | } |
242 | 2.54M | void setVisibility(VisibilityTypes V) { |
243 | 2.54M | assert((!hasLocalLinkage() || V == DefaultVisibility) && |
244 | 2.54M | "local linkage requires default visibility"); |
245 | 2.54M | Visibility = V; |
246 | 2.54M | maybeSetDsoLocal(); |
247 | 2.54M | } |
248 | | |
249 | | /// If the value is "Thread Local", its value isn't shared by the threads. |
250 | 3.29M | bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; } |
251 | 57 | void setThreadLocal(bool Val) { |
252 | 57 | setThreadLocalMode(Val ? GeneralDynamicTLSModel39 : NotThreadLocal18 ); |
253 | 57 | } |
254 | 645k | void setThreadLocalMode(ThreadLocalMode Val) { |
255 | 645k | assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal); |
256 | 645k | ThreadLocal = Val; |
257 | 645k | } |
258 | 3.39M | ThreadLocalMode getThreadLocalMode() const { |
259 | 3.39M | return static_cast<ThreadLocalMode>(ThreadLocal); |
260 | 3.39M | } |
261 | | |
262 | 390k | DLLStorageClassTypes getDLLStorageClass() const { |
263 | 390k | return DLLStorageClassTypes(DllStorageClass); |
264 | 390k | } |
265 | 6.49M | bool hasDLLImportStorageClass() const { |
266 | 6.49M | return DllStorageClass == DLLImportStorageClass; |
267 | 6.49M | } |
268 | 7.78k | bool hasDLLExportStorageClass() const { |
269 | 7.78k | return DllStorageClass == DLLExportStorageClass; |
270 | 7.78k | } |
271 | 3.03M | void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; } |
272 | | |
273 | 861k | bool hasSection() const { return !getSection().empty(); } |
274 | | StringRef getSection() const; |
275 | | |
276 | | /// Global values are always pointers. |
277 | 174M | PointerType *getType() const { return cast<PointerType>(User::getType()); } |
278 | | |
279 | 48.1M | Type *getValueType() const { return ValueType; } |
280 | | |
281 | 3.97M | void setDSOLocal(bool Local) { IsDSOLocal = Local; } |
282 | | |
283 | 2.31M | bool isDSOLocal() const { |
284 | 2.31M | return IsDSOLocal; |
285 | 2.31M | } |
286 | | |
287 | 2.30M | bool hasPartition() const { |
288 | 2.30M | return HasPartition; |
289 | 2.30M | } |
290 | | StringRef getPartition() const; |
291 | | void setPartition(StringRef Part); |
292 | | |
293 | 0 | static LinkageTypes getLinkOnceLinkage(bool ODR) { |
294 | 0 | return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage; |
295 | 0 | } |
296 | 124 | static LinkageTypes getWeakLinkage(bool ODR) { |
297 | 124 | return ODR ? WeakODRLinkage85 : WeakAnyLinkage39 ; |
298 | 124 | } |
299 | | |
300 | 748k | static bool isExternalLinkage(LinkageTypes Linkage) { |
301 | 748k | return Linkage == ExternalLinkage; |
302 | 748k | } |
303 | 61.5M | static bool isAvailableExternallyLinkage(LinkageTypes Linkage) { |
304 | 61.5M | return Linkage == AvailableExternallyLinkage; |
305 | 61.5M | } |
306 | 704k | static bool isLinkOnceODRLinkage(LinkageTypes Linkage) { |
307 | 704k | return Linkage == LinkOnceODRLinkage; |
308 | 704k | } |
309 | 8.26M | static bool isLinkOnceLinkage(LinkageTypes Linkage) { |
310 | 8.26M | return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage7.59M ; |
311 | 8.26M | } |
312 | 28.5k | static bool isWeakAnyLinkage(LinkageTypes Linkage) { |
313 | 28.5k | return Linkage == WeakAnyLinkage; |
314 | 28.5k | } |
315 | 24.7k | static bool isWeakODRLinkage(LinkageTypes Linkage) { |
316 | 24.7k | return Linkage == WeakODRLinkage; |
317 | 24.7k | } |
318 | 27.5k | static bool isWeakLinkage(LinkageTypes Linkage) { |
319 | 27.5k | return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage)24.4k ; |
320 | 27.5k | } |
321 | 3.36M | static bool isAppendingLinkage(LinkageTypes Linkage) { |
322 | 3.36M | return Linkage == AppendingLinkage; |
323 | 3.36M | } |
324 | 56.1M | static bool isInternalLinkage(LinkageTypes Linkage) { |
325 | 56.1M | return Linkage == InternalLinkage; |
326 | 56.1M | } |
327 | 59.6M | static bool isPrivateLinkage(LinkageTypes Linkage) { |
328 | 59.6M | return Linkage == PrivateLinkage; |
329 | 59.6M | } |
330 | 54.1M | static bool isLocalLinkage(LinkageTypes Linkage) { |
331 | 54.1M | return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage)50.9M ; |
332 | 54.1M | } |
333 | 5.27M | static bool isExternalWeakLinkage(LinkageTypes Linkage) { |
334 | 5.27M | return Linkage == ExternalWeakLinkage; |
335 | 5.27M | } |
336 | 2.11M | static bool isCommonLinkage(LinkageTypes Linkage) { |
337 | 2.11M | return Linkage == CommonLinkage; |
338 | 2.11M | } |
339 | 389k | static bool isValidDeclarationLinkage(LinkageTypes Linkage) { |
340 | 389k | return isExternalWeakLinkage(Linkage) || isExternalLinkage(Linkage)385k ; |
341 | 389k | } |
342 | | |
343 | | /// Whether the definition of this global may be replaced by something |
344 | | /// non-equivalent at link time. For example, if a function has weak linkage |
345 | | /// then the code defining it may be replaced by different code. |
346 | 22.5M | static bool isInterposableLinkage(LinkageTypes Linkage) { |
347 | 22.5M | switch (Linkage) { |
348 | 22.5M | case WeakAnyLinkage: |
349 | 15.7M | case LinkOnceAnyLinkage: |
350 | 15.7M | case CommonLinkage: |
351 | 15.7M | case ExternalWeakLinkage: |
352 | 15.7M | return true; |
353 | 15.7M | |
354 | 15.7M | case AvailableExternallyLinkage: |
355 | 6.86M | case LinkOnceODRLinkage: |
356 | 6.86M | case WeakODRLinkage: |
357 | 6.86M | // The above three cannot be overridden but can be de-refined. |
358 | 6.86M | |
359 | 6.86M | case ExternalLinkage: |
360 | 6.86M | case AppendingLinkage: |
361 | 6.86M | case InternalLinkage: |
362 | 6.86M | case PrivateLinkage: |
363 | 6.86M | return false; |
364 | 0 | } |
365 | 0 | llvm_unreachable("Fully covered switch above!"); |
366 | 0 | } |
367 | | |
368 | | /// Whether the definition of this global may be discarded if it is not used |
369 | | /// in its compilation unit. |
370 | 6.42M | static bool isDiscardableIfUnused(LinkageTypes Linkage) { |
371 | 6.42M | return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage)5.36M || |
372 | 6.42M | isAvailableExternallyLinkage(Linkage)2.83M ; |
373 | 6.42M | } |
374 | | |
375 | | /// Whether the definition of this global may be replaced at link time. NB: |
376 | | /// Using this method outside of the code generators is almost always a |
377 | | /// mistake: when working at the IR level use isInterposable instead as it |
378 | | /// knows about ODR semantics. |
379 | 1.79M | static bool isWeakForLinker(LinkageTypes Linkage) { |
380 | 1.79M | return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage1.64M || |
381 | 1.79M | Linkage == LinkOnceAnyLinkage1.63M || Linkage == LinkOnceODRLinkage1.39M || |
382 | 1.79M | Linkage == CommonLinkage1.34M || Linkage == ExternalWeakLinkage1.30M ; |
383 | 1.79M | } |
384 | | |
385 | | /// Return true if the currently visible definition of this global (if any) is |
386 | | /// exactly the definition we will see at runtime. |
387 | | /// |
388 | | /// Non-exact linkage types inhibits most non-inlining IPO, since a |
389 | | /// differently optimized variant of the same function can have different |
390 | | /// observable or undefined behavior than in the variant currently visible. |
391 | | /// For instance, we could have started with |
392 | | /// |
393 | | /// void foo(int *v) { |
394 | | /// int t = 5 / v[0]; |
395 | | /// (void) t; |
396 | | /// } |
397 | | /// |
398 | | /// and "refined" it to |
399 | | /// |
400 | | /// void foo(int *v) { } |
401 | | /// |
402 | | /// However, we cannot infer readnone for `foo`, since that would justify |
403 | | /// DSE'ing a store to `v[0]` across a call to `foo`, which can cause |
404 | | /// undefined behavior if the linker replaces the actual call destination with |
405 | | /// the unoptimized `foo`. |
406 | | /// |
407 | | /// Inlining is okay across non-exact linkage types as long as they're not |
408 | | /// interposable (see \c isInterposable), since in such cases the currently |
409 | | /// visible variant is *a* correct implementation of the original source |
410 | | /// function; it just isn't the *only* correct implementation. |
411 | 9.22M | bool isDefinitionExact() const { |
412 | 9.22M | return !mayBeDerefined(); |
413 | 9.22M | } |
414 | | |
415 | | /// Return true if this global has an exact defintion. |
416 | 10.9M | bool hasExactDefinition() const { |
417 | 10.9M | // While this computes exactly the same thing as |
418 | 10.9M | // isStrongDefinitionForLinker, the intended uses are different. This |
419 | 10.9M | // function is intended to help decide if specific inter-procedural |
420 | 10.9M | // transforms are correct, while isStrongDefinitionForLinker's intended use |
421 | 10.9M | // is in low level code generation. |
422 | 10.9M | return !isDeclaration() && isDefinitionExact()8.17M ; |
423 | 10.9M | } |
424 | | |
425 | | /// Return true if this global's definition can be substituted with an |
426 | | /// *arbitrary* definition at link time. We cannot do any IPO or inlinining |
427 | | /// across interposable call edges, since the callee can be replaced with |
428 | | /// something arbitrary at link time. |
429 | 22.5M | bool isInterposable() const { return isInterposableLinkage(getLinkage()); } |
430 | | |
431 | 356k | bool hasExternalLinkage() const { return isExternalLinkage(getLinkage()); } |
432 | 58.6M | bool hasAvailableExternallyLinkage() const { |
433 | 58.6M | return isAvailableExternallyLinkage(getLinkage()); |
434 | 58.6M | } |
435 | 1.84M | bool hasLinkOnceLinkage() const { return isLinkOnceLinkage(getLinkage()); } |
436 | 704k | bool hasLinkOnceODRLinkage() const { |
437 | 704k | return isLinkOnceODRLinkage(getLinkage()); |
438 | 704k | } |
439 | 26.2k | bool hasWeakLinkage() const { return isWeakLinkage(getLinkage()); } |
440 | 958 | bool hasWeakAnyLinkage() const { return isWeakAnyLinkage(getLinkage()); } |
441 | 163 | bool hasWeakODRLinkage() const { return isWeakODRLinkage(getLinkage()); } |
442 | 3.36M | bool hasAppendingLinkage() const { return isAppendingLinkage(getLinkage()); } |
443 | 1.99M | bool hasInternalLinkage() const { return isInternalLinkage(getLinkage()); } |
444 | 8.69M | bool hasPrivateLinkage() const { return isPrivateLinkage(getLinkage()); } |
445 | 43.3M | bool hasLocalLinkage() const { return isLocalLinkage(getLinkage()); } |
446 | 4.88M | bool hasExternalWeakLinkage() const { |
447 | 4.88M | return isExternalWeakLinkage(getLinkage()); |
448 | 4.88M | } |
449 | 2.11M | bool hasCommonLinkage() const { return isCommonLinkage(getLinkage()); } |
450 | 370k | bool hasValidDeclarationLinkage() const { |
451 | 370k | return isValidDeclarationLinkage(getLinkage()); |
452 | 370k | } |
453 | | |
454 | 4.80M | void setLinkage(LinkageTypes LT) { |
455 | 4.80M | if (isLocalLinkage(LT)) |
456 | 563k | Visibility = DefaultVisibility; |
457 | 4.80M | Linkage = LT; |
458 | 4.80M | maybeSetDsoLocal(); |
459 | 4.80M | } |
460 | 167M | LinkageTypes getLinkage() const { return LinkageTypes(Linkage); } |
461 | | |
462 | 6.42M | bool isDiscardableIfUnused() const { |
463 | 6.42M | return isDiscardableIfUnused(getLinkage()); |
464 | 6.42M | } |
465 | | |
466 | 1.79M | bool isWeakForLinker() const { return isWeakForLinker(getLinkage()); } |
467 | | |
468 | | protected: |
469 | | /// Copy all additional attributes (those not needed to create a GlobalValue) |
470 | | /// from the GlobalValue Src to this one. |
471 | | void copyAttributesFrom(const GlobalValue *Src); |
472 | | |
473 | | public: |
474 | | /// If the given string begins with the GlobalValue name mangling escape |
475 | | /// character '\1', drop it. |
476 | | /// |
477 | | /// This function applies a specific mangling that is used in PGO profiles, |
478 | | /// among other things. If you're trying to get a symbol name for an |
479 | | /// arbitrary GlobalValue, this is not the function you're looking for; see |
480 | | /// Mangler.h. |
481 | 46.8M | static StringRef dropLLVMManglingEscape(StringRef Name) { |
482 | 46.8M | if (!Name.empty() && Name[0] == '\1'46.8M ) |
483 | 552k | return Name.substr(1); |
484 | 46.3M | return Name; |
485 | 46.3M | } |
486 | | |
487 | | /// Return the modified name for a global value suitable to be |
488 | | /// used as the key for a global lookup (e.g. profile or ThinLTO). |
489 | | /// The value's original name is \c Name and has linkage of type |
490 | | /// \c Linkage. The value is defined in module \c FileName. |
491 | | static std::string getGlobalIdentifier(StringRef Name, |
492 | | GlobalValue::LinkageTypes Linkage, |
493 | | StringRef FileName); |
494 | | |
495 | | /// Return the modified name for this global value suitable to be |
496 | | /// used as the key for a global lookup (e.g. profile or ThinLTO). |
497 | | std::string getGlobalIdentifier() const; |
498 | | |
499 | | /// Declare a type to represent a global unique identifier for a global value. |
500 | | /// This is a 64 bits hash that is used by PGO and ThinLTO to have a compact |
501 | | /// unique way to identify a symbol. |
502 | | using GUID = uint64_t; |
503 | | |
504 | | /// Return a 64-bit global unique ID constructed from global value name |
505 | | /// (i.e. returned by getGlobalIdentifier()). |
506 | 20.3k | static GUID getGUID(StringRef GlobalName) { return MD5Hash(GlobalName); } |
507 | | |
508 | | /// Return a 64-bit global unique ID constructed from global value name |
509 | | /// (i.e. returned by getGlobalIdentifier()). |
510 | 8.47k | GUID getGUID() const { return getGUID(getGlobalIdentifier()); } |
511 | | |
512 | | /// @name Materialization |
513 | | /// Materialization is used to construct functions only as they're needed. |
514 | | /// This |
515 | | /// is useful to reduce memory usage in LLVM or parsing work done by the |
516 | | /// BitcodeReader to load the Module. |
517 | | /// @{ |
518 | | |
519 | | /// If this function's Module is being lazily streamed in functions from disk |
520 | | /// or some other source, this method can be used to check to see if the |
521 | | /// function has been read in yet or not. |
522 | | bool isMaterializable() const; |
523 | | |
524 | | /// Make sure this GlobalValue is fully read. |
525 | | Error materialize(); |
526 | | |
527 | | /// @} |
528 | | |
529 | | /// Return true if the primary definition of this global value is outside of |
530 | | /// the current translation unit. |
531 | | bool isDeclaration() const; |
532 | | |
533 | 4.29M | bool isDeclarationForLinker() const { |
534 | 4.29M | if (hasAvailableExternallyLinkage()) |
535 | 8.48k | return true; |
536 | 4.28M | |
537 | 4.28M | return isDeclaration(); |
538 | 4.28M | } |
539 | | |
540 | | /// Returns true if this global's definition will be the one chosen by the |
541 | | /// linker. |
542 | | /// |
543 | | /// NB! Ideally this should not be used at the IR level at all. If you're |
544 | | /// interested in optimization constraints implied by the linker's ability to |
545 | | /// choose an implementation, prefer using \c hasExactDefinition. |
546 | 698k | bool isStrongDefinitionForLinker() const { |
547 | 698k | return !(isDeclarationForLinker() || isWeakForLinker()312k ); |
548 | 698k | } |
549 | | |
550 | | // Returns true if the alignment of the value can be unilaterally |
551 | | // increased. |
552 | | bool canIncreaseAlignment() const; |
553 | | |
554 | | const GlobalObject *getBaseObject() const; |
555 | 45.3k | GlobalObject *getBaseObject() { |
556 | 45.3k | return const_cast<GlobalObject *>( |
557 | 45.3k | static_cast<const GlobalValue *>(this)->getBaseObject()); |
558 | 45.3k | } |
559 | | |
560 | | /// Returns whether this is a reference to an absolute symbol. |
561 | | bool isAbsoluteSymbolRef() const; |
562 | | |
563 | | /// If this is an absolute symbol reference, returns the range of the symbol, |
564 | | /// otherwise returns None. |
565 | | Optional<ConstantRange> getAbsoluteSymbolRange() const; |
566 | | |
567 | | /// This method unlinks 'this' from the containing module, but does not delete |
568 | | /// it. |
569 | | void removeFromParent(); |
570 | | |
571 | | /// This method unlinks 'this' from the containing module and deletes it. |
572 | | void eraseFromParent(); |
573 | | |
574 | | /// Get the module that this global value is contained inside of... |
575 | 237M | Module *getParent() { return Parent; } |
576 | 455M | const Module *getParent() const { return Parent; } |
577 | | |
578 | | // Methods for support type inquiry through isa, cast, and dyn_cast: |
579 | 27.5M | static bool classof(const Value *V) { |
580 | 27.5M | return V->getValueID() == Value::FunctionVal || |
581 | 27.5M | V->getValueID() == Value::GlobalVariableVal25.3M || |
582 | 27.5M | V->getValueID() == Value::GlobalAliasVal20.6M || |
583 | 27.5M | V->getValueID() == Value::GlobalIFuncVal20.6M ; |
584 | 27.5M | } |
585 | | |
586 | | /// True if GV can be left out of the object symbol table. This is the case |
587 | | /// for linkonce_odr values whose address is not significant. While legal, it |
588 | | /// is not normally profitable to omit them from the .o symbol table. Using |
589 | | /// this analysis makes sense when the information can be passed down to the |
590 | | /// linker or we are in LTO. |
591 | | bool canBeOmittedFromSymbolTable() const; |
592 | | }; |
593 | | |
594 | | } // end namespace llvm |
595 | | |
596 | | #endif // LLVM_IR_GLOBALVALUE_H |