/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/clang/lib/CodeGen/ConstantEmitter.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- ConstantEmitter.h - IR constant emission ---------------*- C++ -*-===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | // |
10 | | // A helper class for emitting expressions and values as llvm::Constants |
11 | | // and as initializers for global variables. |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #ifndef LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H |
16 | | #define LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H |
17 | | |
18 | | #include "CodeGenFunction.h" |
19 | | #include "CodeGenModule.h" |
20 | | |
21 | | namespace clang { |
22 | | namespace CodeGen { |
23 | | |
24 | | class ConstantEmitter { |
25 | | public: |
26 | | CodeGenModule &CGM; |
27 | | CodeGenFunction *CGF; |
28 | | |
29 | | private: |
30 | | bool Abstract = false; |
31 | | |
32 | | /// Whether non-abstract components of the emitter have been initialized. |
33 | | bool InitializedNonAbstract = false; |
34 | | |
35 | | /// Whether the emitter has been finalized. |
36 | | bool Finalized = false; |
37 | | |
38 | | /// Whether the constant-emission failed. |
39 | | bool Failed = false; |
40 | | |
41 | | /// The AST address space where this (non-abstract) initializer is going. |
42 | | /// Used for generating appropriate placeholders. |
43 | | unsigned DestAddressSpace; |
44 | | |
45 | | llvm::SmallVector<std::pair<llvm::Constant *, llvm::GlobalVariable*>, 4> |
46 | | PlaceholderAddresses; |
47 | | |
48 | | public: |
49 | | ConstantEmitter(CodeGenModule &CGM, CodeGenFunction *CGF = nullptr) |
50 | 22.1k | : CGM(CGM), CGF(CGF) {} |
51 | | |
52 | | /// Initialize this emission in the context of the given function. |
53 | | /// Use this if the expression might contain contextaul references like |
54 | | /// block addresses or PredefinedExprs. |
55 | | ConstantEmitter(CodeGenFunction &CGF) |
56 | 91.0k | : CGM(CGF.CGM), CGF(&CGF) {} |
57 | | |
58 | | ConstantEmitter(const ConstantEmitter &other) = delete; |
59 | | ConstantEmitter &operator=(const ConstantEmitter &other) = delete; |
60 | | |
61 | | ~ConstantEmitter(); |
62 | | |
63 | | /// Is the current emission context abstract? |
64 | 0 | bool isAbstract() const { |
65 | 0 | return Abstract; |
66 | 0 | } |
67 | | |
68 | | /// Try to emit the initiaizer of the given declaration as an abstract |
69 | | /// constant. If this succeeds, the emission must be finalized. |
70 | | llvm::Constant *tryEmitForInitializer(const VarDecl &D); |
71 | | llvm::Constant *tryEmitForInitializer(const Expr *E, |
72 | | unsigned destAddrSpace, |
73 | | QualType destType); |
74 | | llvm::Constant *emitForInitializer(const APValue &value, |
75 | | unsigned 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 | 32.2k | llvm::Constant *emitNullForMemory(QualType T) { |
114 | 32.2k | return emitNullForMemory(CGM, T); |
115 | 32.2k | } |
116 | 975k | llvm::Constant *emitForMemory(llvm::Constant *C, QualType T) { |
117 | 975k | return emitForMemory(CGM, C, T); |
118 | 975k | } |
119 | | |
120 | | static llvm::Constant *emitNullForMemory(CodeGenModule &CGM, QualType T); |
121 | | static llvm::Constant *emitForMemory(CodeGenModule &CGM, llvm::Constant *C, |
122 | | QualType T); |
123 | | |
124 | | // These are private helper routines of the constant emitter that |
125 | | // can't actually be private because things are split out into helper |
126 | | // functions and classes. |
127 | | |
128 | | llvm::Constant *tryEmitPrivateForVarInit(const VarDecl &D); |
129 | | |
130 | | llvm::Constant *tryEmitPrivate(const Expr *E, QualType T); |
131 | | llvm::Constant *tryEmitPrivateForMemory(const Expr *E, QualType T); |
132 | | |
133 | | llvm::Constant *tryEmitPrivate(const APValue &value, QualType T); |
134 | | llvm::Constant *tryEmitPrivateForMemory(const APValue &value, QualType T); |
135 | | |
136 | | /// Get the address of the current location. This is a constant |
137 | | /// that will resolve, after finalization, to the address of the |
138 | | /// 'signal' value that is registered with the emitter later. |
139 | | llvm::GlobalValue *getCurrentAddrPrivate(); |
140 | | |
141 | | /// Register a 'signal' value with the emitter to inform it where to |
142 | | /// resolve a placeholder. The signal value must be unique in the |
143 | | /// initializer; it might, for example, be the address of a global that |
144 | | /// refers to the current-address value in its own initializer. |
145 | | /// |
146 | | /// Uses of the placeholder must be properly anchored before finalizing |
147 | | /// the emitter, e.g. by being installed as the initializer of a global |
148 | | /// variable. That is, it must be possible to replaceAllUsesWith |
149 | | /// the placeholder with the proper address of the signal. |
150 | | void registerCurrentAddrPrivate(llvm::Constant *signal, |
151 | | llvm::GlobalValue *placeholder); |
152 | | |
153 | | private: |
154 | 24.5k | void initializeNonAbstract(unsigned destAS) { |
155 | 24.5k | assert(!InitializedNonAbstract); |
156 | 24.5k | InitializedNonAbstract = true; |
157 | 24.5k | DestAddressSpace = destAS; |
158 | 24.5k | } |
159 | 24.4k | llvm::Constant *markIfFailed(llvm::Constant *init) { |
160 | 24.4k | if (!init) |
161 | 1.86k | Failed = true; |
162 | 24.4k | return init; |
163 | 24.4k | } |
164 | | |
165 | | struct AbstractState { |
166 | | bool OldValue; |
167 | | size_t OldPlaceholdersSize; |
168 | | }; |
169 | 90.4k | AbstractState pushAbstract() { |
170 | 90.4k | AbstractState saved = { Abstract, PlaceholderAddresses.size() }; |
171 | 90.4k | Abstract = true; |
172 | 90.4k | return saved; |
173 | 90.4k | } |
174 | | llvm::Constant *validateAndPopAbstract(llvm::Constant *C, AbstractState save); |
175 | | }; |
176 | | |
177 | | } |
178 | | } |
179 | | |
180 | | #endif |