Assume lane masks are always precise
[oota-llvm.git] / lib / Target / ARM / ARMConstantPoolValue.h
1 //===-- ARMConstantPoolValue.h - ARM constantpool value ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ARM specific constantpool value class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_TARGET_ARM_ARMCONSTANTPOOLVALUE_H
15 #define LLVM_LIB_TARGET_ARM_ARMCONSTANTPOOLVALUE_H
16
17 #include "llvm/CodeGen/MachineConstantPool.h"
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include <cstddef>
21
22 namespace llvm {
23
24 class BlockAddress;
25 class Constant;
26 class GlobalValue;
27 class LLVMContext;
28 class MachineBasicBlock;
29
30 namespace ARMCP {
31   enum ARMCPKind {
32     CPValue,
33     CPExtSymbol,
34     CPBlockAddress,
35     CPLSDA,
36     CPMachineBasicBlock
37   };
38
39   enum ARMCPModifier {
40     no_modifier,
41     TLSGD,
42     GOT_PREL,
43     GOTTPOFF,
44     TPOFF
45   };
46 }
47
48 /// ARMConstantPoolValue - ARM specific constantpool value. This is used to
49 /// represent PC-relative displacement between the address of the load
50 /// instruction and the constant being loaded, i.e. (&GV-(LPIC+8)).
51 class ARMConstantPoolValue : public MachineConstantPoolValue {
52   unsigned LabelId;        // Label id of the load.
53   ARMCP::ARMCPKind Kind;   // Kind of constant.
54   unsigned char PCAdjust;  // Extra adjustment if constantpool is pc-relative.
55                            // 8 for ARM, 4 for Thumb.
56   ARMCP::ARMCPModifier Modifier;   // GV modifier i.e. (&GV(modifier)-(LPIC+8))
57   bool AddCurrentAddress;
58
59 protected:
60   ARMConstantPoolValue(Type *Ty, unsigned id, ARMCP::ARMCPKind Kind,
61                        unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
62                        bool AddCurrentAddress);
63
64   ARMConstantPoolValue(LLVMContext &C, unsigned id, ARMCP::ARMCPKind Kind,
65                        unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
66                        bool AddCurrentAddress);
67
68   template <typename Derived>
69   int getExistingMachineCPValueImpl(MachineConstantPool *CP,
70                                     unsigned Alignment) {
71     unsigned AlignMask = Alignment - 1;
72     const std::vector<MachineConstantPoolEntry> &Constants = CP->getConstants();
73     for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
74       if (Constants[i].isMachineConstantPoolEntry() &&
75           (Constants[i].getAlignment() & AlignMask) == 0) {
76         ARMConstantPoolValue *CPV =
77             (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
78         if (Derived *APC = dyn_cast<Derived>(CPV))
79           if (cast<Derived>(this)->equals(APC))
80             return i;
81       }
82     }
83
84     return -1;
85   }
86
87 public:
88   ~ARMConstantPoolValue() override;
89
90   ARMCP::ARMCPModifier getModifier() const { return Modifier; }
91   const char *getModifierText() const;
92   bool hasModifier() const { return Modifier != ARMCP::no_modifier; }
93
94   bool mustAddCurrentAddress() const { return AddCurrentAddress; }
95
96   unsigned getLabelId() const { return LabelId; }
97   unsigned char getPCAdjustment() const { return PCAdjust; }
98
99   bool isGlobalValue() const { return Kind == ARMCP::CPValue; }
100   bool isExtSymbol() const { return Kind == ARMCP::CPExtSymbol; }
101   bool isBlockAddress() const { return Kind == ARMCP::CPBlockAddress; }
102   bool isLSDA() const { return Kind == ARMCP::CPLSDA; }
103   bool isMachineBasicBlock() const{ return Kind == ARMCP::CPMachineBasicBlock; }
104
105   unsigned getRelocationInfo() const override { return 2; }
106
107   int getExistingMachineCPValue(MachineConstantPool *CP,
108                                 unsigned Alignment) override;
109
110   void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
111
112   /// hasSameValue - Return true if this ARM constpool value can share the same
113   /// constantpool entry as another ARM constpool value.
114   virtual bool hasSameValue(ARMConstantPoolValue *ACPV);
115
116   bool equals(const ARMConstantPoolValue *A) const {
117     return this->LabelId == A->LabelId &&
118       this->PCAdjust == A->PCAdjust &&
119       this->Modifier == A->Modifier;
120   }
121
122   void print(raw_ostream &O) const override;
123   void print(raw_ostream *O) const { if (O) print(*O); }
124   void dump() const;
125 };
126
127 inline raw_ostream &operator<<(raw_ostream &O, const ARMConstantPoolValue &V) {
128   V.print(O);
129   return O;
130 }
131
132 /// ARMConstantPoolConstant - ARM-specific constant pool values for Constants,
133 /// Functions, and BlockAddresses.
134 class ARMConstantPoolConstant : public ARMConstantPoolValue {
135   const Constant *CVal;         // Constant being loaded.
136
137   ARMConstantPoolConstant(const Constant *C,
138                           unsigned ID,
139                           ARMCP::ARMCPKind Kind,
140                           unsigned char PCAdj,
141                           ARMCP::ARMCPModifier Modifier,
142                           bool AddCurrentAddress);
143   ARMConstantPoolConstant(Type *Ty, const Constant *C,
144                           unsigned ID,
145                           ARMCP::ARMCPKind Kind,
146                           unsigned char PCAdj,
147                           ARMCP::ARMCPModifier Modifier,
148                           bool AddCurrentAddress);
149
150 public:
151   static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID);
152   static ARMConstantPoolConstant *Create(const GlobalValue *GV,
153                                          ARMCP::ARMCPModifier Modifier);
154   static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID,
155                                          ARMCP::ARMCPKind Kind,
156                                          unsigned char PCAdj);
157   static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID,
158                                          ARMCP::ARMCPKind Kind,
159                                          unsigned char PCAdj,
160                                          ARMCP::ARMCPModifier Modifier,
161                                          bool AddCurrentAddress);
162
163   const GlobalValue *getGV() const;
164   const BlockAddress *getBlockAddress() const;
165
166   int getExistingMachineCPValue(MachineConstantPool *CP,
167                                 unsigned Alignment) override;
168
169   /// hasSameValue - Return true if this ARM constpool value can share the same
170   /// constantpool entry as another ARM constpool value.
171   bool hasSameValue(ARMConstantPoolValue *ACPV) override;
172
173   void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
174
175   void print(raw_ostream &O) const override;
176   static bool classof(const ARMConstantPoolValue *APV) {
177     return APV->isGlobalValue() || APV->isBlockAddress() || APV->isLSDA();
178   }
179
180   bool equals(const ARMConstantPoolConstant *A) const {
181     return CVal == A->CVal && ARMConstantPoolValue::equals(A);
182   }
183 };
184
185 /// ARMConstantPoolSymbol - ARM-specific constantpool values for external
186 /// symbols.
187 class ARMConstantPoolSymbol : public ARMConstantPoolValue {
188   const std::string S;          // ExtSymbol being loaded.
189
190   ARMConstantPoolSymbol(LLVMContext &C, const char *s, unsigned id,
191                         unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
192                         bool AddCurrentAddress);
193
194 public:
195   static ARMConstantPoolSymbol *Create(LLVMContext &C, const char *s,
196                                        unsigned ID, unsigned char PCAdj);
197
198   const char *getSymbol() const { return S.c_str(); }
199
200   int getExistingMachineCPValue(MachineConstantPool *CP,
201                                 unsigned Alignment) override;
202
203   void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
204
205   /// hasSameValue - Return true if this ARM constpool value can share the same
206   /// constantpool entry as another ARM constpool value.
207   bool hasSameValue(ARMConstantPoolValue *ACPV) override;
208
209   void print(raw_ostream &O) const override;
210
211   static bool classof(const ARMConstantPoolValue *ACPV) {
212     return ACPV->isExtSymbol();
213   }
214
215   bool equals(const ARMConstantPoolSymbol *A) const {
216     return S == A->S && ARMConstantPoolValue::equals(A);
217   }
218 };
219
220 /// ARMConstantPoolMBB - ARM-specific constantpool value of a machine basic
221 /// block.
222 class ARMConstantPoolMBB : public ARMConstantPoolValue {
223   const MachineBasicBlock *MBB; // Machine basic block.
224
225   ARMConstantPoolMBB(LLVMContext &C, const MachineBasicBlock *mbb, unsigned id,
226                      unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
227                      bool AddCurrentAddress);
228
229 public:
230   static ARMConstantPoolMBB *Create(LLVMContext &C,
231                                     const MachineBasicBlock *mbb,
232                                     unsigned ID, unsigned char PCAdj);
233
234   const MachineBasicBlock *getMBB() const { return MBB; }
235
236   int getExistingMachineCPValue(MachineConstantPool *CP,
237                                 unsigned Alignment) override;
238
239   void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
240
241   /// hasSameValue - Return true if this ARM constpool value can share the same
242   /// constantpool entry as another ARM constpool value.
243   bool hasSameValue(ARMConstantPoolValue *ACPV) override;
244
245   void print(raw_ostream &O) const override;
246
247   static bool classof(const ARMConstantPoolValue *ACPV) {
248     return ACPV->isMachineBasicBlock();
249   }
250
251   bool equals(const ARMConstantPoolMBB *A) const {
252     return MBB == A->MBB && ARMConstantPoolValue::equals(A);
253   }
254 };
255
256 } // End llvm namespace
257
258 #endif