ConstantEmitter.h 6.6 KB

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