Use the new ARMConstantPoolSymbol class to handle external symbols.
[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     S(NULL), 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           APC->getModifier() == this->getModifier())
235         return i;
236     }
237   }
238
239   return -1;
240 }
241
242 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
243   const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
244   return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
245 }
246
247 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
248   ID.AddPointer(CVal);
249   ARMConstantPoolValue::addSelectionDAGCSEId(ID);
250 }
251
252 void ARMConstantPoolConstant::print(raw_ostream &O) const {
253   O << CVal->getName();
254   ARMConstantPoolValue::print(O);
255 }
256
257 //===----------------------------------------------------------------------===//
258 // ARMConstantPoolSymbol
259 //===----------------------------------------------------------------------===//
260
261 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
262                                              unsigned id,
263                                              unsigned char PCAdj,
264                                              ARMCP::ARMCPModifier Modifier,
265                                              bool AddCurrentAddress)
266   : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
267                          AddCurrentAddress),
268     S(strdup(s)) {}
269
270 ARMConstantPoolSymbol::~ARMConstantPoolSymbol() {
271   free((void*)S);
272 }
273
274 ARMConstantPoolSymbol *
275 ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
276                               unsigned ID, unsigned char PCAdj) {
277   return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false);
278 }
279
280 ARMConstantPoolSymbol *
281 ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
282                               unsigned ID, unsigned char PCAdj,
283                               ARMCP::ARMCPModifier Modifier,
284                               bool AddCurrentAddress) {
285   return new ARMConstantPoolSymbol(C, s, ID, PCAdj, Modifier,
286                                    AddCurrentAddress);
287 }
288
289 int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
290                                                      unsigned Alignment) {
291   unsigned AlignMask = Alignment - 1;
292   const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
293   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
294     if (Constants[i].isMachineConstantPoolEntry() &&
295         (Constants[i].getAlignment() & AlignMask) == 0) {
296       ARMConstantPoolValue *CPV =
297         (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
298       ARMConstantPoolSymbol *APS = dyn_cast<ARMConstantPoolSymbol>(CPV);
299       if (!APS) continue;
300
301       if (APS->getLabelId() == this->getLabelId() &&
302           APS->getPCAdjustment() == this->getPCAdjustment() &&
303           CPV_streq(APS->getSymbol(), this->getSymbol()) &&
304           APS->getModifier() == this->getModifier())
305         return i;
306     }
307   }
308
309   return -1;
310 }
311
312 bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) {
313   const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV);
314   return ACPS && CPV_streq(ACPS->S, S) &&
315     ARMConstantPoolValue::hasSameValue(ACPV);
316 }
317
318 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
319   ID.AddPointer(S);
320   ARMConstantPoolValue::addSelectionDAGCSEId(ID);
321 }
322
323 void ARMConstantPoolSymbol::print(raw_ostream &O) const {
324   O << S;
325   ARMConstantPoolValue::print(O);
326 }