/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/ConstantEmitter.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- ConstantEmitter.h - IR constant emission ---------------*- 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 | | // A helper class for emitting expressions and values as llvm::Constants |
10 | | // and as initializers for global variables. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #ifndef LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H |
15 | | #define LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H |
16 | | |
17 | | #include "CodeGenFunction.h" |
18 | | #include "CodeGenModule.h" |
19 | | |
20 | | namespace clang { |
21 | | namespace CodeGen { |
22 | | |
23 | | class ConstantEmitter { |
24 | | public: |
25 | | CodeGenModule &CGM; |
26 | | CodeGenFunction *const CGF; |
27 | | |
28 | | private: |
29 | | bool Abstract = false; |
30 | | |
31 | | /// Whether non-abstract components of the emitter have been initialized. |
32 | | bool InitializedNonAbstract = false; |
33 | | |
34 | | /// Whether the emitter has been finalized. |
35 | | bool Finalized = false; |
36 | | |
37 | | /// Whether the constant-emission failed. |
38 | | bool Failed = false; |
39 | | |
40 | | /// Whether we're in a constant context. |
41 | | bool InConstantContext = false; |
42 | | |
43 | | /// The AST address space where this (non-abstract) initializer is going. |
44 | | /// Used for generating appropriate placeholders. |
45 | | LangAS DestAddressSpace; |
46 | | |
47 | | llvm::SmallVector<std::pair<llvm::Constant *, llvm::GlobalVariable*>, 4> |
48 | | PlaceholderAddresses; |
49 | | |
50 | | public: |
51 | | ConstantEmitter(CodeGenModule &CGM, CodeGenFunction *CGF = nullptr) |
52 | 16.2k | : CGM(CGM), CGF(CGF) {} |
53 | | |
54 | | /// Initialize this emission in the context of the given function. |
55 | | /// Use this if the expression might contain contextual references like |
56 | | /// block addresses or PredefinedExprs. |
57 | | ConstantEmitter(CodeGenFunction &CGF) |
58 | 33.6k | : CGM(CGF.CGM), CGF(&CGF) {} |
59 | | |
60 | | ConstantEmitter(const ConstantEmitter &other) = delete; |
61 | | ConstantEmitter &operator=(const ConstantEmitter &other) = delete; |
62 | | |
63 | | ~ConstantEmitter(); |
64 | | |
65 | | /// Is the current emission context abstract? |
66 | 0 | bool isAbstract() const { |
67 | 0 | return Abstract; |
68 | 0 | } |
69 | | |
70 | | /// Try to emit the initiaizer of the given declaration as an abstract |
71 | | /// constant. If this succeeds, the emission must be finalized. |
72 | | llvm::Constant *tryEmitForInitializer(const VarDecl &D); |
73 | | llvm::Constant *tryEmitForInitializer(const Expr *E, LangAS destAddrSpace, |
74 | | QualType destType); |
75 | | llvm::Constant *emitForInitializer(const APValue &value, LangAS destAddrSpace, |
76 | | QualType destType); |
77 | | |
78 | | void finalize(llvm::GlobalVariable *global); |
79 | | |
80 | | // All of the "abstract" emission methods below permit the emission to |
81 | | // be immediately discarded without finalizing anything. Therefore, they |
82 | | // must also promise not to do anything that will, in the future, require |
83 | | // finalization: |
84 | | // |
85 | | // - using the CGF (if present) for anything other than establishing |
86 | | // semantic context; for example, an expression with ignored |
87 | | // side-effects must not be emitted as an abstract expression |
88 | | // |
89 | | // - doing anything that would not be safe to duplicate within an |
90 | | // initializer or to propagate to another context; for example, |
91 | | // side effects, or emitting an initialization that requires a |
92 | | // reference to its current location. |
93 | | |
94 | | /// Try to emit the initializer of the given declaration as an abstract |
95 | | /// constant. |
96 | | llvm::Constant *tryEmitAbstractForInitializer(const VarDecl &D); |
97 | | |
98 | | /// Emit the result of the given expression as an abstract constant, |
99 | | /// asserting that it succeeded. This is only safe to do when the |
100 | | /// expression is known to be a constant expression with either a fairly |
101 | | /// simple type or a known simple form. |
102 | | llvm::Constant *emitAbstract(const Expr *E, QualType T); |
103 | | llvm::Constant *emitAbstract(SourceLocation loc, const APValue &value, |
104 | | QualType T); |
105 | | |
106 | | /// Try to emit the result of the given expression as an abstract constant. |
107 | | llvm::Constant *tryEmitAbstract(const Expr *E, QualType T); |
108 | | llvm::Constant *tryEmitAbstractForMemory(const Expr *E, QualType T); |
109 | | |
110 | | llvm::Constant *tryEmitAbstract(const APValue &value, QualType T); |
111 | | llvm::Constant *tryEmitAbstractForMemory(const APValue &value, QualType T); |
112 | | |
113 | | llvm::Constant *tryEmitConstantExpr(const ConstantExpr *CE); |
114 | | |
115 | 46 | llvm::Constant *emitNullForMemory(QualType T) { |
116 | 46 | return emitNullForMemory(CGM, T); |
117 | 46 | } |
118 | 42.9k | llvm::Constant *emitForMemory(llvm::Constant *C, QualType T) { |
119 | 42.9k | return emitForMemory(CGM, C, T); |
120 | 42.9k | } |
121 | | |
122 | | static llvm::Constant *emitNullForMemory(CodeGenModule &CGM, QualType T); |
123 | | static llvm::Constant *emitForMemory(CodeGenModule &CGM, llvm::Constant *C, |
124 | | QualType T); |
125 | | |
126 | | // These are private helper routines of the constant emitter that |
127 | | // can't actually be private because things are split out into helper |
128 | | // functions and classes. |
129 | | |
130 | | llvm::Constant *tryEmitPrivateForVarInit(const VarDecl &D); |
131 | | |
132 | | llvm::Constant *tryEmitPrivate(const Expr *E, QualType T); |
133 | | llvm::Constant *tryEmitPrivateForMemory(const Expr *E, QualType T); |
134 | | |
135 | | llvm::Constant *tryEmitPrivate(const APValue &value, QualType T); |
136 | | llvm::Constant *tryEmitPrivateForMemory(const APValue &value, QualType T); |
137 | | |
138 | | /// Get the address of the current location. This is a constant |
139 | | /// that will resolve, after finalization, to the address of the |
140 | | /// 'signal' value that is registered with the emitter later. |
141 | | llvm::GlobalValue *getCurrentAddrPrivate(); |
142 | | |
143 | | /// Register a 'signal' value with the emitter to inform it where to |
144 | | /// resolve a placeholder. The signal value must be unique in the |
145 | | /// initializer; it might, for example, be the address of a global that |
146 | | /// refers to the current-address value in its own initializer. |
147 | | /// |
148 | | /// Uses of the placeholder must be properly anchored before finalizing |
149 | | /// the emitter, e.g. by being installed as the initializer of a global |
150 | | /// variable. That is, it must be possible to replaceAllUsesWith |
151 | | /// the placeholder with the proper address of the signal. |
152 | | void registerCurrentAddrPrivate(llvm::Constant *signal, |
153 | | llvm::GlobalValue *placeholder); |
154 | | |
155 | | private: |
156 | 25.3k | void initializeNonAbstract(LangAS destAS) { |
157 | 25.3k | assert(!InitializedNonAbstract); |
158 | 25.3k | InitializedNonAbstract = true; |
159 | 25.3k | DestAddressSpace = destAS; |
160 | 25.3k | } |
161 | 25.2k | llvm::Constant *markIfFailed(llvm::Constant *init) { |
162 | 25.2k | if (!init) |
163 | 13.1k | Failed = true; |
164 | 25.2k | return init; |
165 | 25.2k | } |
166 | | |
167 | | struct AbstractState { |
168 | | bool OldValue; |
169 | | size_t OldPlaceholdersSize; |
170 | | }; |
171 | 25.1k | AbstractState pushAbstract() { |
172 | 25.1k | AbstractState saved = { Abstract, PlaceholderAddresses.size() }; |
173 | 25.1k | Abstract = true; |
174 | 25.1k | return saved; |
175 | 25.1k | } |
176 | | llvm::Constant *validateAndPopAbstract(llvm::Constant *C, AbstractState save); |
177 | | }; |
178 | | |
179 | | } |
180 | | } |
181 | | |
182 | | #endif |