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