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