2c200bdf2f34faa95aeb7457f55cd7a44c2ff8e4
[oota-llvm.git] / lib / Target / ARM / ARMConstantPoolValue.cpp
1 //===- ARMConstantPoolValue.cpp - 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 #include "ARMConstantPoolValue.h"
15 #include "llvm/ADT/FoldingSet.h"
16 #include "llvm/Constant.h"
17 #include "llvm/Constants.h"
18 #include "llvm/GlobalValue.h"
19 #include "llvm/Type.h"
20 #include "llvm/CodeGen/MachineBasicBlock.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   default: llvm_unreachable("Unknown modifier!");
52     // FIXME: Are these case sensitive? It'd be nice to lower-case all the
53     // strings if that's legal.
54   case ARMCP::no_modifier: return "none";
55   case ARMCP::TLSGD:       return "tlsgd";
56   case ARMCP::GOT:         return "GOT";
57   case ARMCP::GOTOFF:      return "GOTOFF";
58   case ARMCP::GOTTPOFF:    return "gottpoff";
59   case ARMCP::TPOFF:       return "tpoff";
60   }
61 }
62
63 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
64                                                     unsigned Alignment) {
65   unsigned AlignMask = Alignment - 1;
66   const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
67   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
68     if (Constants[i].isMachineConstantPoolEntry() &&
69         (Constants[i].getAlignment() & AlignMask) == 0) {
70       ARMConstantPoolValue *CPV =
71         (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
72       if (CPV->LabelId == LabelId &&
73           CPV->PCAdjust == PCAdjust &&
74           CPV->Modifier == Modifier)
75         return i;
76     }
77   }
78
79   return -1;
80 }
81
82 void
83 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
84   ID.AddInteger(LabelId);
85   ID.AddInteger(PCAdjust);
86 }
87
88 bool
89 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
90   if (ACPV->Kind == Kind &&
91       ACPV->PCAdjust == PCAdjust &&
92       ACPV->Modifier == Modifier) {
93     if (ACPV->LabelId == LabelId)
94       return true;
95     // Two PC relative constpool entries containing the same GV address or
96     // external symbols. FIXME: What about blockaddress?
97     if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
98       return true;
99   }
100   return false;
101 }
102
103 void ARMConstantPoolValue::dump() const {
104   errs() << "  " << *this;
105 }
106
107 void ARMConstantPoolValue::print(raw_ostream &O) const {
108   if (Modifier) O << "(" << getModifierText() << ")";
109   if (PCAdjust != 0) {
110     O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
111     if (AddCurrentAddress) O << "-.";
112     O << ")";
113   }
114 }
115
116 //===----------------------------------------------------------------------===//
117 // ARMConstantPoolConstant
118 //===----------------------------------------------------------------------===//
119
120 ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
121                                                  const Constant *C,
122                                                  unsigned ID,
123                                                  ARMCP::ARMCPKind Kind,
124                                                  unsigned char PCAdj,
125                                                  ARMCP::ARMCPModifier Modifier,
126                                                  bool AddCurrentAddress)
127   : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
128     CVal(C) {}
129
130 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
131                                                  unsigned ID,
132                                                  ARMCP::ARMCPKind Kind,
133                                                  unsigned char PCAdj,
134                                                  ARMCP::ARMCPModifier Modifier,
135                                                  bool AddCurrentAddress)
136   : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
137                          AddCurrentAddress),
138     CVal(C) {}
139
140 ARMConstantPoolConstant *
141 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
142   return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
143                                      ARMCP::no_modifier, false);
144 }
145
146 ARMConstantPoolConstant *
147 ARMConstantPoolConstant::Create(const GlobalValue *GV,
148                                 ARMCP::ARMCPModifier Modifier) {
149   return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()),
150                                      GV, 0, ARMCP::CPValue, 0,
151                                      Modifier, false);
152 }
153
154 ARMConstantPoolConstant *
155 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
156                                 ARMCP::ARMCPKind Kind, unsigned char PCAdj) {
157   return new ARMConstantPoolConstant(C, ID, Kind, PCAdj,
158                                      ARMCP::no_modifier, false);
159 }
160
161 ARMConstantPoolConstant *
162 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
163                                 ARMCP::ARMCPKind Kind, unsigned char PCAdj,
164                                 ARMCP::ARMCPModifier Modifier,
165                                 bool AddCurrentAddress) {
166   return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier,
167                                      AddCurrentAddress);
168 }
169
170 const GlobalValue *ARMConstantPoolConstant::getGV() const {
171   return dyn_cast_or_null<GlobalValue>(CVal);
172 }
173
174 const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
175   return dyn_cast_or_null<BlockAddress>(CVal);
176 }
177
178 int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
179                                                        unsigned Alignment) {
180   unsigned AlignMask = Alignment - 1;
181   const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
182   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
183     if (Constants[i].isMachineConstantPoolEntry() &&
184         (Constants[i].getAlignment() & AlignMask) == 0) {
185       ARMConstantPoolValue *CPV =
186         (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
187       ARMConstantPoolConstant *APC = dyn_cast<ARMConstantPoolConstant>(CPV);
188       if (!APC) continue;
189
190       if (APC->getGV() == this->CVal &&
191           APC->getLabelId() == this->getLabelId() &&
192           APC->getPCAdjustment() == this->getPCAdjustment() &&
193           APC->getModifier() == this->getModifier())
194         return i;
195     }
196   }
197
198   return -1;
199 }
200
201 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
202   const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
203   return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
204 }
205
206 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
207   ID.AddPointer(CVal);
208   ARMConstantPoolValue::addSelectionDAGCSEId(ID);
209 }
210
211 void ARMConstantPoolConstant::print(raw_ostream &O) const {
212   O << CVal->getName();
213   ARMConstantPoolValue::print(O);
214 }
215
216 //===----------------------------------------------------------------------===//
217 // ARMConstantPoolSymbol
218 //===----------------------------------------------------------------------===//
219
220 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
221                                              unsigned id,
222                                              unsigned char PCAdj,
223                                              ARMCP::ARMCPModifier Modifier,
224                                              bool AddCurrentAddress)
225   : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
226                          AddCurrentAddress),
227     S(strdup(s)) {}
228
229 ARMConstantPoolSymbol::~ARMConstantPoolSymbol() {
230   free((void*)S);
231 }
232
233 ARMConstantPoolSymbol *
234 ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
235                               unsigned ID, unsigned char PCAdj) {
236   return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false);
237 }
238
239 static bool CPV_streq(const char *S1, const char *S2) {
240   if (S1 == S2)
241     return true;
242   if (S1 && S2 && strcmp(S1, S2) == 0)
243     return true;
244   return false;
245 }
246
247 int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
248                                                      unsigned Alignment) {
249   unsigned AlignMask = Alignment - 1;
250   const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
251   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
252     if (Constants[i].isMachineConstantPoolEntry() &&
253         (Constants[i].getAlignment() & AlignMask) == 0) {
254       ARMConstantPoolValue *CPV =
255         (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
256       ARMConstantPoolSymbol *APS = dyn_cast<ARMConstantPoolSymbol>(CPV);
257       if (!APS) continue;
258
259       if (APS->getLabelId() == this->getLabelId() &&
260           APS->getPCAdjustment() == this->getPCAdjustment() &&
261           CPV_streq(APS->getSymbol(), this->getSymbol()) &&
262           APS->getModifier() == this->getModifier())
263         return i;
264     }
265   }
266
267   return -1;
268 }
269
270 bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) {
271   const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV);
272   return ACPS && CPV_streq(ACPS->S, S) &&
273     ARMConstantPoolValue::hasSameValue(ACPV);
274 }
275
276 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
277   ID.AddPointer(S);
278   ARMConstantPoolValue::addSelectionDAGCSEId(ID);
279 }
280
281 void ARMConstantPoolSymbol::print(raw_ostream &O) const {
282   O << S;
283   ARMConstantPoolValue::print(O);
284 }
285
286 //===----------------------------------------------------------------------===//
287 // ARMConstantPoolMBB
288 //===----------------------------------------------------------------------===//
289
290 ARMConstantPoolMBB::ARMConstantPoolMBB(LLVMContext &C,
291                                        const MachineBasicBlock *mbb,
292                                        unsigned id, unsigned char PCAdj,
293                                        ARMCP::ARMCPModifier Modifier,
294                                        bool AddCurrentAddress)
295   : ARMConstantPoolValue(C, id, ARMCP::CPMachineBasicBlock, PCAdj,
296                          Modifier, AddCurrentAddress),
297     MBB(mbb) {}
298
299 ARMConstantPoolMBB *ARMConstantPoolMBB::Create(LLVMContext &C,
300                                                const MachineBasicBlock *mbb,
301                                                unsigned ID,
302                                                unsigned char PCAdj) {
303   return new ARMConstantPoolMBB(C, mbb, ID, PCAdj, ARMCP::no_modifier, false);
304 }
305
306 int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
307                                                   unsigned Alignment) {
308   unsigned AlignMask = Alignment - 1;
309   const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
310   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
311     if (Constants[i].isMachineConstantPoolEntry() &&
312         (Constants[i].getAlignment() & AlignMask) == 0) {
313       ARMConstantPoolValue *CPV =
314         (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
315       ARMConstantPoolMBB *APMBB = dyn_cast<ARMConstantPoolMBB>(CPV);
316       if (!APMBB) continue;
317
318       if (APMBB->getLabelId() == this->getLabelId() &&
319           APMBB->getPCAdjustment() == this->getPCAdjustment() &&
320           APMBB->getMBB() == this->getMBB() &&
321           APMBB->getModifier() == this->getModifier())
322         return i;
323     }
324   }
325
326   return -1;
327 }
328
329 bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue *ACPV) {
330   const ARMConstantPoolMBB *ACPMBB = dyn_cast<ARMConstantPoolMBB>(ACPV);
331   return ACPMBB && ACPMBB->MBB == MBB &&
332     ARMConstantPoolValue::hasSameValue(ACPV);
333 }
334
335 void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
336   ID.AddPointer(MBB);
337   ARMConstantPoolValue::addSelectionDAGCSEId(ID);
338 }
339
340 void ARMConstantPoolMBB::print(raw_ostream &O) const {
341   ARMConstantPoolValue::print(O);
342 }