clang  16.0.0git
ConstantEmitter.h
Go to the documentation of this file.
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 
24 public:
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 
48  PlaceholderAddresses;
49 
50 public:
52  : 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.
58  : CGM(CGF.CGM), CGF(&CGF) {}
59 
60  ConstantEmitter(const ConstantEmitter &other) = delete;
61  ConstantEmitter &operator=(const ConstantEmitter &other) = delete;
62 
64 
65  /// Is the current emission context abstract?
66  bool isAbstract() const {
67  return Abstract;
68  }
69 
70  bool isInConstantContext() const { return InConstantContext; }
71  void setInConstantContext(bool var) { InConstantContext = var; }
72 
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,
77  QualType destType);
78  llvm::Constant *emitForInitializer(const APValue &value, LangAS destAddrSpace,
79  QualType destType);
80 
81  void finalize(llvm::GlobalVariable *global);
82 
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
86  // finalization:
87  //
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
91  //
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.
96 
97  /// Try to emit the initializer of the given declaration as an abstract
98  /// constant.
99  llvm::Constant *tryEmitAbstractForInitializer(const VarDecl &D);
100 
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);
106  llvm::Constant *emitAbstract(SourceLocation loc, const APValue &value,
107  QualType T);
108 
109  /// Try to emit the result of the given expression as an abstract constant.
110  llvm::Constant *tryEmitAbstract(const Expr *E, QualType T);
111  llvm::Constant *tryEmitAbstractForMemory(const Expr *E, QualType T);
112 
113  llvm::Constant *tryEmitAbstract(const APValue &value, QualType T);
114  llvm::Constant *tryEmitAbstractForMemory(const APValue &value, QualType T);
115 
116  llvm::Constant *tryEmitConstantExpr(const ConstantExpr *CE);
117 
118  llvm::Constant *emitNullForMemory(QualType T) {
119  return emitNullForMemory(CGM, T);
120  }
121  llvm::Constant *emitForMemory(llvm::Constant *C, QualType T) {
122  return emitForMemory(CGM, C, T);
123  }
124 
125  static llvm::Constant *emitNullForMemory(CodeGenModule &CGM, QualType T);
126  static llvm::Constant *emitForMemory(CodeGenModule &CGM, llvm::Constant *C,
127  QualType T);
128 
129  // These are private helper routines of the constant emitter that
130  // can't actually be private because things are split out into helper
131  // functions and classes.
132 
133  llvm::Constant *tryEmitPrivateForVarInit(const VarDecl &D);
134 
135  llvm::Constant *tryEmitPrivate(const Expr *E, QualType T);
136  llvm::Constant *tryEmitPrivateForMemory(const Expr *E, QualType T);
137 
138  llvm::Constant *tryEmitPrivate(const APValue &value, QualType T);
139  llvm::Constant *tryEmitPrivateForMemory(const APValue &value, QualType T);
140 
141  /// Get the address of the current location. This is a constant
142  /// that will resolve, after finalization, to the address of the
143  /// 'signal' value that is registered with the emitter later.
144  llvm::GlobalValue *getCurrentAddrPrivate();
145 
146  /// Register a 'signal' value with the emitter to inform it where to
147  /// resolve a placeholder. The signal value must be unique in the
148  /// initializer; it might, for example, be the address of a global that
149  /// refers to the current-address value in its own initializer.
150  ///
151  /// Uses of the placeholder must be properly anchored before finalizing
152  /// the emitter, e.g. by being installed as the initializer of a global
153  /// variable. That is, it must be possible to replaceAllUsesWith
154  /// the placeholder with the proper address of the signal.
155  void registerCurrentAddrPrivate(llvm::Constant *signal,
156  llvm::GlobalValue *placeholder);
157 
158 private:
159  void initializeNonAbstract(LangAS destAS) {
160  assert(!InitializedNonAbstract);
161  InitializedNonAbstract = true;
162  DestAddressSpace = destAS;
163  }
164  llvm::Constant *markIfFailed(llvm::Constant *init) {
165  if (!init)
166  Failed = true;
167  return init;
168  }
169 
170  struct AbstractState {
171  bool OldValue;
172  size_t OldPlaceholdersSize;
173  };
174  AbstractState pushAbstract() {
175  AbstractState saved = { Abstract, PlaceholderAddresses.size() };
176  Abstract = true;
177  return saved;
178  }
179  llvm::Constant *validateAndPopAbstract(llvm::Constant *C, AbstractState save);
180 };
181 
182 }
183 }
184 
185 #endif
clang::CodeGen::ConstantEmitter::emitForInitializer
llvm::Constant * emitForInitializer(const APValue &value, LangAS destAddrSpace, QualType destType)
Definition: CGExprConstant.cpp:1444
clang::CodeGen::ConstantEmitter::CGF
CodeGenFunction *const CGF
Definition: ConstantEmitter.h:26
clang::CodeGen::ConstantEmitter::tryEmitForInitializer
llvm::Constant * tryEmitForInitializer(const VarDecl &D)
Try to emit the initiaizer of the given declaration as an abstract constant.
Definition: CGExprConstant.cpp:1432
CodeGenFunction.h
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::CodeGen::ConstantEmitter::emitForMemory
llvm::Constant * emitForMemory(llvm::Constant *C, QualType T)
Definition: ConstantEmitter.h:121
clang::CodeGen::ConstantEmitter::isInConstantContext
bool isInConstantContext() const
Definition: ConstantEmitter.h:70
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:737
clang::CodeGen::ConstantEmitter::tryEmitPrivateForVarInit
llvm::Constant * tryEmitPrivateForVarInit(const VarDecl &D)
Definition: CGExprConstant.cpp:1638
clang::CodeGen::ConstantEmitter::CGM
CodeGenModule & CGM
Definition: ConstantEmitter.h:25
clang::CodeGen::ConstantEmitter::operator=
ConstantEmitter & operator=(const ConstantEmitter &other)=delete
clang::CodeGen::ConstantEmitter::tryEmitAbstractForInitializer
llvm::Constant * tryEmitAbstractForInitializer(const VarDecl &D)
Try to emit the initializer of the given declaration as an abstract constant.
Definition: CGExprConstant.cpp:1374
clang::CodeGen::ConstantEmitter::tryEmitPrivate
llvm::Constant * tryEmitPrivate(const Expr *E, QualType T)
Definition: CGExprConstant.cpp:1740
clang::CodeGen::ConstantEmitter::ConstantEmitter
ConstantEmitter(CodeGenModule &CGM, CodeGenFunction *CGF=nullptr)
Definition: ConstantEmitter.h:51
clang::CodeGen::ConstantEmitter::~ConstantEmitter
~ConstantEmitter()
Definition: CGExprConstant.cpp:1624
clang::CodeGen::ConstantEmitter::getCurrentAddrPrivate
llvm::GlobalValue * getCurrentAddrPrivate()
Get the address of the current location.
Definition: CGExprConstant.cpp:1453
clang::CodeGen::ConstantEmitter::ConstantEmitter
ConstantEmitter(CodeGenFunction &CGF)
Initialize this emission in the context of the given function.
Definition: ConstantEmitter.h:57
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:906
clang::dataflow::var
static constexpr Variable var(Literal L)
Returns the variable of L.
Definition: WatchedLiteralsSolver.cpp:71
CodeGenModule.h
clang::ConstantExpr
ConstantExpr - An expression that occurs in a constant context and optionally the result of evaluatin...
Definition: Expr.h:1041
clang::LangAS
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
clang::CodeGen::ConstantEmitter::tryEmitConstantExpr
llvm::Constant * tryEmitConstantExpr(const ConstantExpr *CE)
Definition: CGExprConstant.cpp:1394
clang::CodeGen::CodeGenFunction
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Definition: CodeGenFunction.h:231
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:279
clang::CodeGen::ConstantEmitter::emitAbstract
llvm::Constant * emitAbstract(const Expr *E, QualType T)
Emit the result of the given expression as an abstract constant, asserting that it succeeded.
Definition: CGExprConstant.cpp:1406
clang::CodeGen::ConstantEmitter::registerCurrentAddrPrivate
void registerCurrentAddrPrivate(llvm::Constant *signal, llvm::GlobalValue *placeholder)
Register a 'signal' value with the emitter to inform it where to resolve a placeholder.
Definition: CGExprConstant.cpp:1473
clang::CodeGen::ConstantEmitter::emitNullForMemory
llvm::Constant * emitNullForMemory(QualType T)
Definition: ConstantEmitter.h:118
clang
Definition: CalledOnceCheck.h:17
clang::CodeGen::ConstantEmitter::tryEmitPrivateForMemory
llvm::Constant * tryEmitPrivateForMemory(const Expr *E, QualType T)
Definition: CGExprConstant.cpp:1695
clang::CodeGen::ConstantEmitter::setInConstantContext
void setInConstantContext(bool var)
Definition: ConstantEmitter.h:71
clang::CodeGen::ConstantEmitter
Definition: ConstantEmitter.h:23
clang::CodeGen::ConstantEmitter::isAbstract
bool isAbstract() const
Is the current emission context abstract?
Definition: ConstantEmitter.h:66
clang::CodeGen::ConstantEmitter::tryEmitAbstract
llvm::Constant * tryEmitAbstract(const Expr *E, QualType T)
Try to emit the result of the given expression as an abstract constant.
Definition: CGExprConstant.cpp:1381
clang::APValue
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::CodeGen::ConstantEmitter::finalize
void finalize(llvm::GlobalVariable *global)
Definition: CGExprConstant.cpp:1608
clang::CodeGen::ConstantEmitter::tryEmitAbstractForMemory
llvm::Constant * tryEmitAbstractForMemory(const Expr *E, QualType T)
Definition: CGExprConstant.cpp:1681