1 //===-- ARMConstantPoolValue.cpp - ARM constantpool value -----------------===//
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 #include "ARMConstantPoolValue.h"
15 #include "llvm/ADT/FoldingSet.h"
16 #include "llvm/CodeGen/MachineBasicBlock.h"
17 #include "llvm/IR/Constant.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cstdlib>
23 using namespace llvm;
24 
25 //===----------------------------------------------------------------------===//
26 // ARMConstantPoolValue
27 //===----------------------------------------------------------------------===//
28 
29 ARMConstantPoolValue::ARMConstantPoolValue(Type *Ty, unsigned id,
30                                            ARMCP::ARMCPKind kind,
31                                            unsigned char PCAdj,
32                                            ARMCP::ARMCPModifier modifier,
33                                            bool addCurrentAddress)
34   : MachineConstantPoolValue(Ty), LabelId(id), Kind(kind),
35     PCAdjust(PCAdj), Modifier(modifier),
36     AddCurrentAddress(addCurrentAddress) {}
37 
38 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C, unsigned id,
39                                            ARMCP::ARMCPKind kind,
40                                            unsigned char PCAdj,
41                                            ARMCP::ARMCPModifier modifier,
42                                            bool addCurrentAddress)
43   : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
44     LabelId(id), Kind(kind), PCAdjust(PCAdj), Modifier(modifier),
45     AddCurrentAddress(addCurrentAddress) {}
46 
47 ARMConstantPoolValue::~ARMConstantPoolValue() {}
48 
49 const char *ARMConstantPoolValue::getModifierText() const {
50   switch (Modifier) {
51     // FIXME: Are these case sensitive? It'd be nice to lower-case all the
52     // strings if that's legal.
53   case ARMCP::no_modifier:
54     return "none";
55   case ARMCP::TLSGD:
56     return "tlsgd";
57   case ARMCP::GOT_PREL:
58     return "GOT_PREL";
59   case ARMCP::GOTTPOFF:
60     return "gottpoff";
61   case ARMCP::TPOFF:
62     return "tpoff";
63   case ARMCP::SBREL:
64     return "SBREL";
65   case ARMCP::SECREL:
66     return "secrel32";
67   }
68   llvm_unreachable("Unknown modifier!");
69 }
70 
71 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
72                                                     unsigned Alignment) {
73   llvm_unreachable("Shouldn't be calling this directly!");
74 }
75 
76 void
77 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
78   ID.AddInteger(LabelId);
79   ID.AddInteger(PCAdjust);
80 }
81 
82 bool
83 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
84   if (ACPV->Kind == Kind &&
85       ACPV->PCAdjust == PCAdjust &&
86       ACPV->Modifier == Modifier &&
87       ACPV->LabelId == LabelId &&
88       ACPV->AddCurrentAddress == AddCurrentAddress) {
89     // Two PC relative constpool entries containing the same GV address or
90     // external symbols. FIXME: What about blockaddress?
91     if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
92       return true;
93   }
94   return false;
95 }
96 
97 LLVM_DUMP_METHOD void ARMConstantPoolValue::dump() const {
98   errs() << "  " << *this;
99 }
100 
101 void ARMConstantPoolValue::print(raw_ostream &O) const {
102   if (Modifier) O << "(" << getModifierText() << ")";
103   if (PCAdjust != 0) {
104     O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
105     if (AddCurrentAddress) O << "-.";
106     O << ")";
107   }
108 }
109 
110 //===----------------------------------------------------------------------===//
111 // ARMConstantPoolConstant
112 //===----------------------------------------------------------------------===//
113 
114 ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
115                                                  const Constant *C,
116                                                  unsigned ID,
117                                                  ARMCP::ARMCPKind Kind,
118                                                  unsigned char PCAdj,
119                                                  ARMCP::ARMCPModifier Modifier,
120                                                  bool AddCurrentAddress)
121   : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
122     CVal(C) {}
123 
124 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
125                                                  unsigned ID,
126                                                  ARMCP::ARMCPKind Kind,
127                                                  unsigned char PCAdj,
128                                                  ARMCP::ARMCPModifier Modifier,
129                                                  bool AddCurrentAddress)
130   : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
131                          AddCurrentAddress),
132     CVal(C) {}
133 
134 ARMConstantPoolConstant *
135 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
136   return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
137                                      ARMCP::no_modifier, false);
138 }
139 
140 ARMConstantPoolConstant *
141 ARMConstantPoolConstant::Create(const GlobalValue *GV,
142                                 ARMCP::ARMCPModifier Modifier) {
143   return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()),
144                                      GV, 0, ARMCP::CPValue, 0,
145                                      Modifier, false);
146 }
147 
148 ARMConstantPoolConstant *
149 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
150                                 ARMCP::ARMCPKind Kind, unsigned char PCAdj) {
151   return new ARMConstantPoolConstant(C, ID, Kind, PCAdj,
152                                      ARMCP::no_modifier, false);
153 }
154 
155 ARMConstantPoolConstant *
156 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
157                                 ARMCP::ARMCPKind Kind, unsigned char PCAdj,
158                                 ARMCP::ARMCPModifier Modifier,
159                                 bool AddCurrentAddress) {
160   return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier,
161                                      AddCurrentAddress);
162 }
163 
164 const GlobalValue *ARMConstantPoolConstant::getGV() const {
165   return dyn_cast_or_null<GlobalValue>(CVal);
166 }
167 
168 const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
169   return dyn_cast_or_null<BlockAddress>(CVal);
170 }
171 
172 int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
173                                                        unsigned Alignment) {
174   return getExistingMachineCPValueImpl<ARMConstantPoolConstant>(CP, Alignment);
175 }
176 
177 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
178   const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
179   return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
180 }
181 
182 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
183   ID.AddPointer(CVal);
184   ARMConstantPoolValue::addSelectionDAGCSEId(ID);
185 }
186 
187 void ARMConstantPoolConstant::print(raw_ostream &O) const {
188   O << CVal->getName();
189   ARMConstantPoolValue::print(O);
190 }
191 
192 //===----------------------------------------------------------------------===//
193 // ARMConstantPoolSymbol
194 //===----------------------------------------------------------------------===//
195 
196 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
197                                              unsigned id,
198                                              unsigned char PCAdj,
199                                              ARMCP::ARMCPModifier Modifier,
200                                              bool AddCurrentAddress)
201   : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
202                          AddCurrentAddress),
203     S(s) {}
204 
205 ARMConstantPoolSymbol *
206 ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
207                               unsigned ID, unsigned char PCAdj) {
208   return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false);
209 }
210 
211 int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
212                                                      unsigned Alignment) {
213   return getExistingMachineCPValueImpl<ARMConstantPoolSymbol>(CP, Alignment);
214 }
215 
216 bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) {
217   const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV);
218   return ACPS && ACPS->S == S && ARMConstantPoolValue::hasSameValue(ACPV);
219 }
220 
221 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
222   ID.AddString(S);
223   ARMConstantPoolValue::addSelectionDAGCSEId(ID);
224 }
225 
226 void ARMConstantPoolSymbol::print(raw_ostream &O) const {
227   O << S;
228   ARMConstantPoolValue::print(O);
229 }
230 
231 //===----------------------------------------------------------------------===//
232 // ARMConstantPoolMBB
233 //===----------------------------------------------------------------------===//
234 
235 ARMConstantPoolMBB::ARMConstantPoolMBB(LLVMContext &C,
236                                        const MachineBasicBlock *mbb,
237                                        unsigned id, unsigned char PCAdj,
238                                        ARMCP::ARMCPModifier Modifier,
239                                        bool AddCurrentAddress)
240   : ARMConstantPoolValue(C, id, ARMCP::CPMachineBasicBlock, PCAdj,
241                          Modifier, AddCurrentAddress),
242     MBB(mbb) {}
243 
244 ARMConstantPoolMBB *ARMConstantPoolMBB::Create(LLVMContext &C,
245                                                const MachineBasicBlock *mbb,
246                                                unsigned ID,
247                                                unsigned char PCAdj) {
248   return new ARMConstantPoolMBB(C, mbb, ID, PCAdj, ARMCP::no_modifier, false);
249 }
250 
251 int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
252                                                   unsigned Alignment) {
253   return getExistingMachineCPValueImpl<ARMConstantPoolMBB>(CP, Alignment);
254 }
255 
256 bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue *ACPV) {
257   const ARMConstantPoolMBB *ACPMBB = dyn_cast<ARMConstantPoolMBB>(ACPV);
258   return ACPMBB && ACPMBB->MBB == MBB &&
259     ARMConstantPoolValue::hasSameValue(ACPV);
260 }
261 
262 void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
263   ID.AddPointer(MBB);
264   ARMConstantPoolValue::addSelectionDAGCSEId(ID);
265 }
266 
267 void ARMConstantPoolMBB::print(raw_ostream &O) const {
268   O << "BB#" << MBB->getNumber();
269   ARMConstantPoolValue::print(O);
270 }
271