|
| 1 | +//===--- RISCVConstantPoolValue.h - RISC-V constantpool value ---*- 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 | +// This file implements the RISC-V specific constantpool value class. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef LLVM_LIB_TARGET_RISCV_RISCVCONSTANTPOOLVALUE_H |
| 14 | +#define LLVM_LIB_TARGET_RISCV_RISCVCONSTANTPOOLVALUE_H |
| 15 | + |
| 16 | +#include "llvm/CodeGen/MachineConstantPool.h" |
| 17 | +#include "llvm/Support/Casting.h" |
| 18 | +#include "llvm/Support/ErrorHandling.h" |
| 19 | + |
| 20 | +namespace llvm { |
| 21 | + |
| 22 | +class LLVMContext; |
| 23 | +class GlobalValue; |
| 24 | +class BlockAddress; |
| 25 | + |
| 26 | +namespace RISCVCP { |
| 27 | + |
| 28 | +enum RISCVCPKind { ExtSymbol, GlobalValue, BlockAddress }; |
| 29 | + |
| 30 | +enum RISCVCPModifier { |
| 31 | + None, |
| 32 | +}; |
| 33 | +} // end namespace RISCVCP |
| 34 | + |
| 35 | +/// A RISCV-specific constant pool value. |
| 36 | +class RISCVConstantPoolValue : public MachineConstantPoolValue { |
| 37 | + RISCVCP::RISCVCPKind Kind; |
| 38 | + RISCVCP::RISCVCPModifier Modifier; |
| 39 | + |
| 40 | +protected: |
| 41 | + RISCVConstantPoolValue(LLVMContext &C, RISCVCP::RISCVCPKind Kind, |
| 42 | + RISCVCP::RISCVCPModifier Modifier); |
| 43 | + |
| 44 | + RISCVConstantPoolValue(Type *Ty, RISCVCP::RISCVCPKind Kind, |
| 45 | + RISCVCP::RISCVCPModifier Modifier); |
| 46 | + |
| 47 | + template <typename Derived> |
| 48 | + int getExistingMachineCPValueImpl(MachineConstantPool *CP, Align Alignment) { |
| 49 | + const std::vector<MachineConstantPoolEntry> &Constants = CP->getConstants(); |
| 50 | + for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
| 51 | + if (Constants[i].isMachineConstantPoolEntry() && |
| 52 | + Constants[i].getAlign() >= Alignment) { |
| 53 | + auto *CPV = static_cast<RISCVConstantPoolValue *>( |
| 54 | + Constants[i].Val.MachineCPVal); |
| 55 | + if (Derived *APC = dyn_cast<Derived>(CPV)) |
| 56 | + if (cast<Derived>(this)->equals(APC)) |
| 57 | + return i; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return -1; |
| 62 | + } |
| 63 | + |
| 64 | +public: |
| 65 | + ~RISCVConstantPoolValue() = default; |
| 66 | + |
| 67 | + RISCVCP::RISCVCPModifier getModifier() const { return Modifier; } |
| 68 | + StringRef getModifierText() const; |
| 69 | + bool hasModifier() const { return Modifier != RISCVCP::None; } |
| 70 | + |
| 71 | + bool isExtSymbol() const { return Kind == RISCVCP::ExtSymbol; } |
| 72 | + bool isGlobalValue() const { return Kind == RISCVCP::GlobalValue; } |
| 73 | + bool isBlockAddress() const { return Kind == RISCVCP::BlockAddress; } |
| 74 | + |
| 75 | + int getExistingMachineCPValue(MachineConstantPool *CP, |
| 76 | + Align Alignment) override; |
| 77 | + |
| 78 | + void addSelectionDAGCSEId(FoldingSetNodeID &ID) override {} |
| 79 | + |
| 80 | + bool equals(const RISCVConstantPoolValue *A) const { |
| 81 | + return this->Modifier == A->Modifier; |
| 82 | + } |
| 83 | + |
| 84 | + void print(raw_ostream &O) const override; |
| 85 | +}; |
| 86 | + |
| 87 | +class RISCVConstantPoolConstant : public RISCVConstantPoolValue { |
| 88 | + const Constant *CVal; |
| 89 | + |
| 90 | + RISCVConstantPoolConstant(Type *Ty, const Constant *GV, |
| 91 | + RISCVCP::RISCVCPKind Kind); |
| 92 | + |
| 93 | +public: |
| 94 | + static RISCVConstantPoolConstant *Create(const GlobalValue *GV, |
| 95 | + RISCVCP::RISCVCPKind Kind); |
| 96 | + static RISCVConstantPoolConstant *Create(const Constant *C, |
| 97 | + RISCVCP::RISCVCPKind Kind); |
| 98 | + |
| 99 | + const GlobalValue *getGlobalValue() const; |
| 100 | + const BlockAddress *getBlockAddress() const; |
| 101 | + |
| 102 | + int getExistingMachineCPValue(MachineConstantPool *CP, |
| 103 | + Align Alignment) override; |
| 104 | + |
| 105 | + void addSelectionDAGCSEId(FoldingSetNodeID &ID) override; |
| 106 | + |
| 107 | + void print(raw_ostream &O) const override; |
| 108 | + |
| 109 | + bool equals(const RISCVConstantPoolConstant *A) const { |
| 110 | + return CVal == A->CVal && RISCVConstantPoolValue::equals(A); |
| 111 | + } |
| 112 | + |
| 113 | + static bool classof(const RISCVConstantPoolValue *RCPV) { |
| 114 | + return RCPV->isGlobalValue() || RCPV->isBlockAddress(); |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +class RISCVConstantPoolSymbol : public RISCVConstantPoolValue { |
| 119 | + const std::string S; |
| 120 | + |
| 121 | + RISCVConstantPoolSymbol(LLVMContext &C, StringRef s, |
| 122 | + RISCVCP::RISCVCPModifier Modifier); |
| 123 | + |
| 124 | +public: |
| 125 | + static RISCVConstantPoolSymbol *Create(LLVMContext &C, StringRef s, |
| 126 | + RISCVCP ::RISCVCPModifier Modifier); |
| 127 | + |
| 128 | + std::string getSymbol() const { return S; } |
| 129 | + |
| 130 | + int getExistingMachineCPValue(MachineConstantPool *CP, |
| 131 | + Align Alignment) override; |
| 132 | + |
| 133 | + void addSelectionDAGCSEId(FoldingSetNodeID &ID) override; |
| 134 | + |
| 135 | + void print(raw_ostream &O) const override; |
| 136 | + |
| 137 | + bool equals(const RISCVConstantPoolSymbol *A) const { |
| 138 | + return S == A->S && RISCVConstantPoolValue::equals(A); |
| 139 | + } |
| 140 | + static bool classof(const RISCVConstantPoolValue *RCPV) { |
| 141 | + return RCPV->isExtSymbol(); |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +} // end namespace llvm |
| 146 | + |
| 147 | +#endif |
0 commit comments