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