ARMConstantPoolValue.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //===- ARMConstantPoolValue.h - ARM 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 ARM specific constantpool value class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_TARGET_ARM_ARMCONSTANTPOOLVALUE_H
  13. #define LLVM_LIB_TARGET_ARM_ARMCONSTANTPOOLVALUE_H
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/iterator_range.h"
  17. #include "llvm/CodeGen/MachineConstantPool.h"
  18. #include "llvm/Support/Casting.h"
  19. #include <string>
  20. #include <vector>
  21. namespace llvm {
  22. class BlockAddress;
  23. class Constant;
  24. class GlobalValue;
  25. class GlobalVariable;
  26. class LLVMContext;
  27. class MachineBasicBlock;
  28. class raw_ostream;
  29. class Type;
  30. namespace ARMCP {
  31. enum ARMCPKind {
  32. CPValue,
  33. CPExtSymbol,
  34. CPBlockAddress,
  35. CPLSDA,
  36. CPMachineBasicBlock,
  37. CPPromotedGlobal
  38. };
  39. enum ARMCPModifier {
  40. no_modifier, /// None
  41. TLSGD, /// Thread Local Storage (General Dynamic Mode)
  42. GOT_PREL, /// Global Offset Table, PC Relative
  43. GOTTPOFF, /// Global Offset Table, Thread Pointer Offset
  44. TPOFF, /// Thread Pointer Offset
  45. SECREL, /// Section Relative (Windows TLS)
  46. SBREL, /// Static Base Relative (RWPI)
  47. };
  48. } // end namespace ARMCP
  49. /// ARMConstantPoolValue - ARM specific constantpool value. This is used to
  50. /// represent PC-relative displacement between the address of the load
  51. /// instruction and the constant being loaded, i.e. (&GV-(LPIC+8)).
  52. class ARMConstantPoolValue : public MachineConstantPoolValue {
  53. unsigned LabelId; // Label id of the load.
  54. ARMCP::ARMCPKind Kind; // Kind of constant.
  55. unsigned char PCAdjust; // Extra adjustment if constantpool is pc-relative.
  56. // 8 for ARM, 4 for Thumb.
  57. ARMCP::ARMCPModifier Modifier; // GV modifier i.e. (&GV(modifier)-(LPIC+8))
  58. bool AddCurrentAddress;
  59. protected:
  60. ARMConstantPoolValue(Type *Ty, unsigned id, ARMCP::ARMCPKind Kind,
  61. unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
  62. bool AddCurrentAddress);
  63. ARMConstantPoolValue(LLVMContext &C, unsigned id, ARMCP::ARMCPKind Kind,
  64. unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
  65. bool AddCurrentAddress);
  66. template <typename Derived>
  67. int getExistingMachineCPValueImpl(MachineConstantPool *CP, Align Alignment) {
  68. const std::vector<MachineConstantPoolEntry> &Constants = CP->getConstants();
  69. for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
  70. if (Constants[i].isMachineConstantPoolEntry() &&
  71. Constants[i].getAlign() >= Alignment) {
  72. auto *CPV =
  73. static_cast<ARMConstantPoolValue*>(Constants[i].Val.MachineCPVal);
  74. if (Derived *APC = dyn_cast<Derived>(CPV))
  75. if (cast<Derived>(this)->equals(APC))
  76. return i;
  77. }
  78. }
  79. return -1;
  80. }
  81. public:
  82. ~ARMConstantPoolValue() override;
  83. ARMCP::ARMCPModifier getModifier() const { return Modifier; }
  84. StringRef getModifierText() const;
  85. bool hasModifier() const { return Modifier != ARMCP::no_modifier; }
  86. bool mustAddCurrentAddress() const { return AddCurrentAddress; }
  87. unsigned getLabelId() const { return LabelId; }
  88. unsigned char getPCAdjustment() const { return PCAdjust; }
  89. bool isGlobalValue() const { return Kind == ARMCP::CPValue; }
  90. bool isExtSymbol() const { return Kind == ARMCP::CPExtSymbol; }
  91. bool isBlockAddress() const { return Kind == ARMCP::CPBlockAddress; }
  92. bool isLSDA() const { return Kind == ARMCP::CPLSDA; }
  93. bool isMachineBasicBlock() const{ return Kind == ARMCP::CPMachineBasicBlock; }
  94. bool isPromotedGlobal() const{ return Kind == ARMCP::CPPromotedGlobal; }
  95. int getExistingMachineCPValue(MachineConstantPool *CP,
  96. Align Alignment) override;
  97. void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
  98. /// hasSameValue - Return true if this ARM constpool value can share the same
  99. /// constantpool entry as another ARM constpool value.
  100. virtual bool hasSameValue(ARMConstantPoolValue *ACPV);
  101. bool equals(const ARMConstantPoolValue *A) const {
  102. return this->LabelId == A->LabelId &&
  103. this->PCAdjust == A->PCAdjust &&
  104. this->Modifier == A->Modifier;
  105. }
  106. void print(raw_ostream &O) const override;
  107. void print(raw_ostream *O) const { if (O) print(*O); }
  108. void dump() const;
  109. };
  110. inline raw_ostream &operator<<(raw_ostream &O, const ARMConstantPoolValue &V) {
  111. V.print(O);
  112. return O;
  113. }
  114. /// ARMConstantPoolConstant - ARM-specific constant pool values for Constants,
  115. /// Functions, and BlockAddresses.
  116. class ARMConstantPoolConstant : public ARMConstantPoolValue {
  117. const Constant *CVal; // Constant being loaded.
  118. SmallPtrSet<const GlobalVariable*, 1> GVars;
  119. ARMConstantPoolConstant(const Constant *C,
  120. unsigned ID,
  121. ARMCP::ARMCPKind Kind,
  122. unsigned char PCAdj,
  123. ARMCP::ARMCPModifier Modifier,
  124. bool AddCurrentAddress);
  125. ARMConstantPoolConstant(Type *Ty, const Constant *C,
  126. unsigned ID,
  127. ARMCP::ARMCPKind Kind,
  128. unsigned char PCAdj,
  129. ARMCP::ARMCPModifier Modifier,
  130. bool AddCurrentAddress);
  131. ARMConstantPoolConstant(const GlobalVariable *GV, const Constant *Init);
  132. public:
  133. static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID);
  134. static ARMConstantPoolConstant *Create(const GlobalValue *GV,
  135. ARMCP::ARMCPModifier Modifier);
  136. static ARMConstantPoolConstant *Create(const GlobalVariable *GV,
  137. const Constant *Initializer);
  138. static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID,
  139. ARMCP::ARMCPKind Kind,
  140. unsigned char PCAdj);
  141. static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID,
  142. ARMCP::ARMCPKind Kind,
  143. unsigned char PCAdj,
  144. ARMCP::ARMCPModifier Modifier,
  145. bool AddCurrentAddress);
  146. const GlobalValue *getGV() const;
  147. const BlockAddress *getBlockAddress() const;
  148. using promoted_iterator = SmallPtrSet<const GlobalVariable *, 1>::iterator;
  149. iterator_range<promoted_iterator> promotedGlobals() {
  150. return iterator_range<promoted_iterator>(GVars.begin(), GVars.end());
  151. }
  152. const Constant *getPromotedGlobalInit() const {
  153. return CVal;
  154. }
  155. int getExistingMachineCPValue(MachineConstantPool *CP,
  156. Align Alignment) override;
  157. /// hasSameValue - Return true if this ARM constpool value can share the same
  158. /// constantpool entry as another ARM constpool value.
  159. bool hasSameValue(ARMConstantPoolValue *ACPV) override;
  160. void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
  161. void print(raw_ostream &O) const override;
  162. static bool classof(const ARMConstantPoolValue *APV) {
  163. return APV->isGlobalValue() || APV->isBlockAddress() || APV->isLSDA() ||
  164. APV->isPromotedGlobal();
  165. }
  166. bool equals(const ARMConstantPoolConstant *A) const {
  167. return CVal == A->CVal && ARMConstantPoolValue::equals(A);
  168. }
  169. };
  170. /// ARMConstantPoolSymbol - ARM-specific constantpool values for external
  171. /// symbols.
  172. class ARMConstantPoolSymbol : public ARMConstantPoolValue {
  173. const std::string S; // ExtSymbol being loaded.
  174. ARMConstantPoolSymbol(LLVMContext &C, StringRef s, unsigned id,
  175. unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
  176. bool AddCurrentAddress);
  177. public:
  178. static ARMConstantPoolSymbol *Create(LLVMContext &C, StringRef s, unsigned ID,
  179. unsigned char PCAdj);
  180. StringRef getSymbol() const { return S; }
  181. int getExistingMachineCPValue(MachineConstantPool *CP,
  182. Align Alignment) override;
  183. void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
  184. /// hasSameValue - Return true if this ARM constpool value can share the same
  185. /// constantpool entry as another ARM constpool value.
  186. bool hasSameValue(ARMConstantPoolValue *ACPV) override;
  187. void print(raw_ostream &O) const override;
  188. static bool classof(const ARMConstantPoolValue *ACPV) {
  189. return ACPV->isExtSymbol();
  190. }
  191. bool equals(const ARMConstantPoolSymbol *A) const {
  192. return S == A->S && ARMConstantPoolValue::equals(A);
  193. }
  194. };
  195. /// ARMConstantPoolMBB - ARM-specific constantpool value of a machine basic
  196. /// block.
  197. class ARMConstantPoolMBB : public ARMConstantPoolValue {
  198. const MachineBasicBlock *MBB; // Machine basic block.
  199. ARMConstantPoolMBB(LLVMContext &C, const MachineBasicBlock *mbb, unsigned id,
  200. unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
  201. bool AddCurrentAddress);
  202. public:
  203. static ARMConstantPoolMBB *Create(LLVMContext &C,
  204. const MachineBasicBlock *mbb,
  205. unsigned ID, unsigned char PCAdj);
  206. const MachineBasicBlock *getMBB() const { return MBB; }
  207. int getExistingMachineCPValue(MachineConstantPool *CP,
  208. Align Alignment) override;
  209. void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
  210. /// hasSameValue - Return true if this ARM constpool value can share the same
  211. /// constantpool entry as another ARM constpool value.
  212. bool hasSameValue(ARMConstantPoolValue *ACPV) override;
  213. void print(raw_ostream &O) const override;
  214. static bool classof(const ARMConstantPoolValue *ACPV) {
  215. return ACPV->isMachineBasicBlock();
  216. }
  217. bool equals(const ARMConstantPoolMBB *A) const {
  218. return MBB == A->MBB && ARMConstantPoolValue::equals(A);
  219. }
  220. };
  221. } // end namespace llvm
  222. #endif // LLVM_LIB_TARGET_ARM_ARMCONSTANTPOOLVALUE_H