Refactoring: Separate out the ARM constant pool Constant from the ARM constant
[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_TARGET_ARM_CONSTANTPOOLVALUE_H
15 #define LLVM_TARGET_ARM_CONSTANTPOOLVALUE_H
16
17 #include "llvm/CodeGen/MachineConstantPool.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include <cstddef>
20
21 namespace llvm {
22
23 class BlockAddress;
24 class Constant;
25 class GlobalValue;
26 class LLVMContext;
27 class MachineBasicBlock;
28
29 namespace ARMCP {
30   enum ARMCPKind {
31     CPValue,
32     CPExtSymbol,
33     CPBlockAddress,
34     CPLSDA,
35     CPMachineBasicBlock
36   };
37
38   enum ARMCPModifier {
39     no_modifier,
40     TLSGD,
41     GOT,
42     GOTOFF,
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   const Constant *CVal;    // Constant being loaded.
53   const MachineBasicBlock *MBB; // MachineBasicBlock being loaded.
54   const char *S;           // ExtSymbol being loaded.
55   unsigned LabelId;        // Label id of the load.
56   ARMCP::ARMCPKind Kind;   // Kind of constant.
57   unsigned char PCAdjust;  // Extra adjustment if constantpool is pc-relative.
58                            // 8 for ARM, 4 for Thumb.
59   ARMCP::ARMCPModifier Modifier;   // GV modifier i.e. (&GV(modifier)-(LPIC+8))
60   bool AddCurrentAddress;
61
62 protected:
63   ARMConstantPoolValue(Type *Ty, unsigned id, ARMCP::ARMCPKind Kind,
64                        unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
65                        bool AddCurrentAddress);
66
67 public:
68   ARMConstantPoolValue(const Constant *cval, unsigned id,
69                        ARMCP::ARMCPKind Kind = ARMCP::CPValue,
70                        unsigned char PCAdj = 0,
71                        ARMCP::ARMCPModifier Modifier = ARMCP::no_modifier,
72                        bool AddCurrentAddress = false);
73   ARMConstantPoolValue(LLVMContext &C, const MachineBasicBlock *mbb,unsigned id,
74                        ARMCP::ARMCPKind Kind = ARMCP::CPValue,
75                        unsigned char PCAdj = 0,
76                        ARMCP::ARMCPModifier Modifier = ARMCP::no_modifier,
77                        bool AddCurrentAddress = false);
78   ARMConstantPoolValue(LLVMContext &C, const char *s, unsigned id,
79                        unsigned char PCAdj = 0,
80                        ARMCP::ARMCPModifier Modifier = ARMCP::no_modifier,
81                        bool AddCurrentAddress = false);
82   ARMConstantPoolValue(const GlobalValue *GV, ARMCP::ARMCPModifier Modifier);
83   ARMConstantPoolValue();
84   ~ARMConstantPoolValue();
85
86   const GlobalValue *getGV() const;
87   const char *getSymbol() const { return S; }
88   const BlockAddress *getBlockAddress() const;
89   const MachineBasicBlock *getMBB() const;
90
91   ARMCP::ARMCPModifier getModifier() const { return Modifier; }
92   const char *getModifierText() const;
93   bool hasModifier() const { return Modifier != ARMCP::no_modifier; }
94
95   bool mustAddCurrentAddress() const { return AddCurrentAddress; }
96
97   unsigned getLabelId() const { return LabelId; }
98   unsigned char getPCAdjustment() const { return PCAdjust; }
99
100   bool isGlobalValue() const { return Kind == ARMCP::CPValue; }
101   bool isExtSymbol() const { return Kind == ARMCP::CPExtSymbol; }
102   bool isBlockAddress() const { return Kind == ARMCP::CPBlockAddress; }
103   bool isLSDA() const { return Kind == ARMCP::CPLSDA; }
104   bool isMachineBasicBlock() { return Kind == ARMCP::CPMachineBasicBlock; }
105
106   virtual unsigned getRelocationInfo() const { return 2; }
107
108   virtual int getExistingMachineCPValue(MachineConstantPool *CP,
109                                         unsigned Alignment);
110
111   virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID);
112
113   /// hasSameValue - Return true if this ARM constpool value can share the same
114   /// constantpool entry as another ARM constpool value.
115   virtual bool hasSameValue(ARMConstantPoolValue *ACPV);
116
117   virtual void print(raw_ostream &O) const;
118   void print(raw_ostream *O) const { if (O) print(*O); }
119   void dump() const;
120
121   static bool classof(const ARMConstantPoolValue *) { return true; }
122 };
123
124 inline raw_ostream &operator<<(raw_ostream &O, const ARMConstantPoolValue &V) {
125   V.print(O);
126   return O;
127 }
128
129 /// ARMConstantPoolConstant - ARM-specific constant pool values for Constants,
130 /// Functions, and BlockAddresses.
131 class ARMConstantPoolConstant : public ARMConstantPoolValue {
132   const Constant *CVal;         // Constant being loaded.
133
134   ARMConstantPoolConstant(const Constant *C,
135                           unsigned ID,
136                           ARMCP::ARMCPKind Kind,
137                           unsigned char PCAdj,
138                           ARMCP::ARMCPModifier Modifier,
139                           bool AddCurrentAddress);
140 public:
141   static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID);
142
143   const GlobalValue *getGV() const;
144
145   /// hasSameValue - Return true if this ARM constpool value can share the same
146   /// constantpool entry as another ARM constpool value.
147   virtual bool hasSameValue(ARMConstantPoolValue *ACPV);
148
149   virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID);
150
151   virtual void print(raw_ostream &O) const;
152   static bool classof(const ARMConstantPoolValue *APV) {
153     return APV->isGlobalValue() || APV->isBlockAddress() || APV->isLSDA();
154   }
155   static bool classof(const ARMConstantPoolConstant *) { return true; }
156 };
157
158 } // End llvm namespace
159
160 #endif