10b57cec5SDimitry Andric //===--- ConstantEmitter.h - IR constant emission ---------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // A helper class for emitting expressions and values as llvm::Constants 100b57cec5SDimitry Andric // and as initializers for global variables. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #ifndef LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H 150b57cec5SDimitry Andric #define LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "CodeGenFunction.h" 180b57cec5SDimitry Andric #include "CodeGenModule.h" 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric namespace clang { 210b57cec5SDimitry Andric namespace CodeGen { 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric class ConstantEmitter { 240b57cec5SDimitry Andric public: 250b57cec5SDimitry Andric CodeGenModule &CGM; 26480093f4SDimitry Andric CodeGenFunction *const CGF; 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric private: 290b57cec5SDimitry Andric bool Abstract = false; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric /// Whether non-abstract components of the emitter have been initialized. 320b57cec5SDimitry Andric bool InitializedNonAbstract = false; 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric /// Whether the emitter has been finalized. 350b57cec5SDimitry Andric bool Finalized = false; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric /// Whether the constant-emission failed. 380b57cec5SDimitry Andric bool Failed = false; 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric /// Whether we're in a constant context. 410b57cec5SDimitry Andric bool InConstantContext = false; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric /// The AST address space where this (non-abstract) initializer is going. 440b57cec5SDimitry Andric /// Used for generating appropriate placeholders. 4506c3fb27SDimitry Andric LangAS DestAddressSpace = LangAS::Default; 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric llvm::SmallVector<std::pair<llvm::Constant *, llvm::GlobalVariable*>, 4> 480b57cec5SDimitry Andric PlaceholderAddresses; 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric public: 510b57cec5SDimitry Andric ConstantEmitter(CodeGenModule &CGM, CodeGenFunction *CGF = nullptr) CGM(CGM)520b57cec5SDimitry Andric : CGM(CGM), CGF(CGF) {} 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric /// Initialize this emission in the context of the given function. 550b57cec5SDimitry Andric /// Use this if the expression might contain contextual references like 560b57cec5SDimitry Andric /// block addresses or PredefinedExprs. ConstantEmitter(CodeGenFunction & CGF)570b57cec5SDimitry Andric ConstantEmitter(CodeGenFunction &CGF) 580b57cec5SDimitry Andric : CGM(CGF.CGM), CGF(&CGF) {} 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric ConstantEmitter(const ConstantEmitter &other) = delete; 610b57cec5SDimitry Andric ConstantEmitter &operator=(const ConstantEmitter &other) = delete; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric ~ConstantEmitter(); 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric /// Is the current emission context abstract? isAbstract()660b57cec5SDimitry Andric bool isAbstract() const { 670b57cec5SDimitry Andric return Abstract; 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric isInConstantContext()70bdd1243dSDimitry Andric bool isInConstantContext() const { return InConstantContext; } setInConstantContext(bool var)71bdd1243dSDimitry Andric void setInConstantContext(bool var) { InConstantContext = var; } 72bdd1243dSDimitry Andric 730b57cec5SDimitry Andric /// Try to emit the initiaizer of the given declaration as an abstract 740b57cec5SDimitry Andric /// constant. If this succeeds, the emission must be finalized. 750b57cec5SDimitry Andric llvm::Constant *tryEmitForInitializer(const VarDecl &D); 760b57cec5SDimitry Andric llvm::Constant *tryEmitForInitializer(const Expr *E, LangAS destAddrSpace, 770b57cec5SDimitry Andric QualType destType); 780b57cec5SDimitry Andric llvm::Constant *emitForInitializer(const APValue &value, LangAS destAddrSpace, 790b57cec5SDimitry Andric QualType destType); 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric void finalize(llvm::GlobalVariable *global); 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric // All of the "abstract" emission methods below permit the emission to 840b57cec5SDimitry Andric // be immediately discarded without finalizing anything. Therefore, they 850b57cec5SDimitry Andric // must also promise not to do anything that will, in the future, require 860b57cec5SDimitry Andric // finalization: 870b57cec5SDimitry Andric // 880b57cec5SDimitry Andric // - using the CGF (if present) for anything other than establishing 890b57cec5SDimitry Andric // semantic context; for example, an expression with ignored 900b57cec5SDimitry Andric // side-effects must not be emitted as an abstract expression 910b57cec5SDimitry Andric // 920b57cec5SDimitry Andric // - doing anything that would not be safe to duplicate within an 930b57cec5SDimitry Andric // initializer or to propagate to another context; for example, 940b57cec5SDimitry Andric // side effects, or emitting an initialization that requires a 950b57cec5SDimitry Andric // reference to its current location. 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric /// Try to emit the initializer of the given declaration as an abstract 980b57cec5SDimitry Andric /// constant. 990b57cec5SDimitry Andric llvm::Constant *tryEmitAbstractForInitializer(const VarDecl &D); 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric /// Emit the result of the given expression as an abstract constant, 1020b57cec5SDimitry Andric /// asserting that it succeeded. This is only safe to do when the 1030b57cec5SDimitry Andric /// expression is known to be a constant expression with either a fairly 1040b57cec5SDimitry Andric /// simple type or a known simple form. 1050b57cec5SDimitry Andric llvm::Constant *emitAbstract(const Expr *E, QualType T); 1060b57cec5SDimitry Andric llvm::Constant *emitAbstract(SourceLocation loc, const APValue &value, 1070b57cec5SDimitry Andric QualType T); 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric /// Try to emit the result of the given expression as an abstract constant. 1100b57cec5SDimitry Andric llvm::Constant *tryEmitAbstract(const Expr *E, QualType T); 1110b57cec5SDimitry Andric llvm::Constant *tryEmitAbstractForMemory(const Expr *E, QualType T); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric llvm::Constant *tryEmitAbstract(const APValue &value, QualType T); 1140b57cec5SDimitry Andric llvm::Constant *tryEmitAbstractForMemory(const APValue &value, QualType T); 1150b57cec5SDimitry Andric 1165ffd83dbSDimitry Andric llvm::Constant *tryEmitConstantExpr(const ConstantExpr *CE); 1175ffd83dbSDimitry Andric emitNullForMemory(QualType T)1180b57cec5SDimitry Andric llvm::Constant *emitNullForMemory(QualType T) { 1190b57cec5SDimitry Andric return emitNullForMemory(CGM, T); 1200b57cec5SDimitry Andric } emitForMemory(llvm::Constant * C,QualType T)1210b57cec5SDimitry Andric llvm::Constant *emitForMemory(llvm::Constant *C, QualType T) { 1220b57cec5SDimitry Andric return emitForMemory(CGM, C, T); 1230b57cec5SDimitry Andric } 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric static llvm::Constant *emitNullForMemory(CodeGenModule &CGM, QualType T); 1260b57cec5SDimitry Andric static llvm::Constant *emitForMemory(CodeGenModule &CGM, llvm::Constant *C, 1270b57cec5SDimitry Andric QualType T); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric // These are private helper routines of the constant emitter that 1300b57cec5SDimitry Andric // can't actually be private because things are split out into helper 1310b57cec5SDimitry Andric // functions and classes. 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric llvm::Constant *tryEmitPrivateForVarInit(const VarDecl &D); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric llvm::Constant *tryEmitPrivate(const Expr *E, QualType T); 1360b57cec5SDimitry Andric llvm::Constant *tryEmitPrivateForMemory(const Expr *E, QualType T); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric llvm::Constant *tryEmitPrivate(const APValue &value, QualType T); 1390b57cec5SDimitry Andric llvm::Constant *tryEmitPrivateForMemory(const APValue &value, QualType T); 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric /// Get the address of the current location. This is a constant 1420b57cec5SDimitry Andric /// that will resolve, after finalization, to the address of the 1430b57cec5SDimitry Andric /// 'signal' value that is registered with the emitter later. 1440b57cec5SDimitry Andric llvm::GlobalValue *getCurrentAddrPrivate(); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric /// Register a 'signal' value with the emitter to inform it where to 1470b57cec5SDimitry Andric /// resolve a placeholder. The signal value must be unique in the 1480b57cec5SDimitry Andric /// initializer; it might, for example, be the address of a global that 1490b57cec5SDimitry Andric /// refers to the current-address value in its own initializer. 1500b57cec5SDimitry Andric /// 1510b57cec5SDimitry Andric /// Uses of the placeholder must be properly anchored before finalizing 1520b57cec5SDimitry Andric /// the emitter, e.g. by being installed as the initializer of a global 1530b57cec5SDimitry Andric /// variable. That is, it must be possible to replaceAllUsesWith 1540b57cec5SDimitry Andric /// the placeholder with the proper address of the signal. 1550b57cec5SDimitry Andric void registerCurrentAddrPrivate(llvm::Constant *signal, 1560b57cec5SDimitry Andric llvm::GlobalValue *placeholder); 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric private: initializeNonAbstract(LangAS destAS)1590b57cec5SDimitry Andric void initializeNonAbstract(LangAS destAS) { 1600b57cec5SDimitry Andric assert(!InitializedNonAbstract); 1610b57cec5SDimitry Andric InitializedNonAbstract = true; 1620b57cec5SDimitry Andric DestAddressSpace = destAS; 1630b57cec5SDimitry Andric } markIfFailed(llvm::Constant * init)1640b57cec5SDimitry Andric llvm::Constant *markIfFailed(llvm::Constant *init) { 1650b57cec5SDimitry Andric if (!init) 1660b57cec5SDimitry Andric Failed = true; 1670b57cec5SDimitry Andric return init; 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric struct AbstractState { 1710b57cec5SDimitry Andric bool OldValue; 1720b57cec5SDimitry Andric size_t OldPlaceholdersSize; 1730b57cec5SDimitry Andric }; pushAbstract()1740b57cec5SDimitry Andric AbstractState pushAbstract() { 1750b57cec5SDimitry Andric AbstractState saved = { Abstract, PlaceholderAddresses.size() }; 1760b57cec5SDimitry Andric Abstract = true; 1770b57cec5SDimitry Andric return saved; 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric llvm::Constant *validateAndPopAbstract(llvm::Constant *C, AbstractState save); 1800b57cec5SDimitry Andric }; 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric } 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric #endif 186