1 //===--- ConstantEmitter.h - IR constant emission ---------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // A helper class for emitting expressions and values as llvm::Constants
10 // and as initializers for global variables.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H
15 #define LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H
17 #include "CodeGenFunction.h"
18 #include "CodeGenModule.h"
23 class ConstantEmitter
{
26 CodeGenFunction
*const CGF
;
29 bool Abstract
= false;
31 /// Whether non-abstract components of the emitter have been initialized.
32 bool InitializedNonAbstract
= false;
34 /// Whether the emitter has been finalized.
35 bool Finalized
= false;
37 /// Whether the constant-emission failed.
40 /// Whether we're in a constant context.
41 bool InConstantContext
= false;
43 /// The AST address space where this (non-abstract) initializer is going.
44 /// Used for generating appropriate placeholders.
45 LangAS DestAddressSpace
= LangAS::Default
;
47 llvm::SmallVector
<std::pair
<llvm::Constant
*, llvm::GlobalVariable
*>, 4>
51 ConstantEmitter(CodeGenModule
&CGM
, CodeGenFunction
*CGF
= nullptr)
52 : CGM(CGM
), CGF(CGF
) {}
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 : CGM(CGF
.CGM
), CGF(&CGF
) {}
60 ConstantEmitter(const ConstantEmitter
&other
) = delete;
61 ConstantEmitter
&operator=(const ConstantEmitter
&other
) = delete;
65 /// Is the current emission context abstract?
66 bool isAbstract() const {
70 bool isInConstantContext() const { return InConstantContext
; }
71 void setInConstantContext(bool var
) { InConstantContext
= var
; }
73 /// Try to emit the initiaizer of the given declaration as an abstract
74 /// constant. If this succeeds, the emission must be finalized.
75 llvm::Constant
*tryEmitForInitializer(const VarDecl
&D
);
76 llvm::Constant
*tryEmitForInitializer(const Expr
*E
, LangAS destAddrSpace
,
78 llvm::Constant
*emitForInitializer(const APValue
&value
, LangAS destAddrSpace
,
81 void finalize(llvm::GlobalVariable
*global
);
83 // All of the "abstract" emission methods below permit the emission to
84 // be immediately discarded without finalizing anything. Therefore, they
85 // must also promise not to do anything that will, in the future, require
88 // - using the CGF (if present) for anything other than establishing
89 // semantic context; for example, an expression with ignored
90 // side-effects must not be emitted as an abstract expression
92 // - doing anything that would not be safe to duplicate within an
93 // initializer or to propagate to another context; for example,
94 // side effects, or emitting an initialization that requires a
95 // reference to its current location.
97 /// Try to emit the initializer of the given declaration as an abstract
99 llvm::Constant
*tryEmitAbstractForInitializer(const VarDecl
&D
);
101 /// Emit the result of the given expression as an abstract constant,
102 /// asserting that it succeeded. This is only safe to do when the
103 /// expression is known to be a constant expression with either a fairly
104 /// simple type or a known simple form.
105 llvm::Constant
*emitAbstract(const Expr
*E
, QualType T
);
107 emitAbstract(SourceLocation loc
, const APValue
&value
, QualType T
,
108 bool EnablePtrAuthFunctionTypeDiscrimination
= true);
110 /// Try to emit the result of the given expression as an abstract constant.
111 llvm::Constant
*tryEmitAbstract(const Expr
*E
, QualType T
);
112 llvm::Constant
*tryEmitAbstractForMemory(const Expr
*E
, QualType T
);
114 llvm::Constant
*tryEmitAbstract(const APValue
&value
, QualType T
);
115 llvm::Constant
*tryEmitAbstractForMemory(const APValue
&value
, QualType T
);
117 llvm::Constant
*tryEmitConstantSignedPointer(llvm::Constant
*Ptr
,
118 PointerAuthQualifier Auth
);
120 llvm::Constant
*tryEmitConstantExpr(const ConstantExpr
*CE
);
122 llvm::Constant
*emitNullForMemory(QualType T
) {
123 return emitNullForMemory(CGM
, T
);
125 llvm::Constant
*emitForMemory(llvm::Constant
*C
, QualType T
) {
126 return emitForMemory(CGM
, C
, T
);
129 static llvm::Constant
*emitNullForMemory(CodeGenModule
&CGM
, QualType T
);
130 static llvm::Constant
*emitForMemory(CodeGenModule
&CGM
, llvm::Constant
*C
,
133 // These are private helper routines of the constant emitter that
134 // can't actually be private because things are split out into helper
135 // functions and classes.
137 llvm::Constant
*tryEmitPrivateForVarInit(const VarDecl
&D
);
139 llvm::Constant
*tryEmitPrivate(const Expr
*E
, QualType T
);
140 llvm::Constant
*tryEmitPrivateForMemory(const Expr
*E
, QualType T
);
143 tryEmitPrivate(const APValue
&value
, QualType T
,
144 bool EnablePtrAuthFunctionTypeDiscrimination
= true);
145 llvm::Constant
*tryEmitPrivateForMemory(const APValue
&value
, QualType T
);
147 /// Get the address of the current location. This is a constant
148 /// that will resolve, after finalization, to the address of the
149 /// 'signal' value that is registered with the emitter later.
150 llvm::GlobalValue
*getCurrentAddrPrivate();
152 /// Register a 'signal' value with the emitter to inform it where to
153 /// resolve a placeholder. The signal value must be unique in the
154 /// initializer; it might, for example, be the address of a global that
155 /// refers to the current-address value in its own initializer.
157 /// Uses of the placeholder must be properly anchored before finalizing
158 /// the emitter, e.g. by being installed as the initializer of a global
159 /// variable. That is, it must be possible to replaceAllUsesWith
160 /// the placeholder with the proper address of the signal.
161 void registerCurrentAddrPrivate(llvm::Constant
*signal
,
162 llvm::GlobalValue
*placeholder
);
165 void initializeNonAbstract(LangAS destAS
) {
166 assert(!InitializedNonAbstract
);
167 InitializedNonAbstract
= true;
168 DestAddressSpace
= destAS
;
170 llvm::Constant
*markIfFailed(llvm::Constant
*init
) {
176 struct AbstractState
{
178 size_t OldPlaceholdersSize
;
180 AbstractState
pushAbstract() {
181 AbstractState saved
= { Abstract
, PlaceholderAddresses
.size() };
185 llvm::Constant
*validateAndPopAbstract(llvm::Constant
*C
, AbstractState save
);