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