1 //===- ARMConstantPoolValue.cpp - ARM constantpool value --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ARM specific constantpool value class.
12 //===----------------------------------------------------------------------===//
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"
25 //===----------------------------------------------------------------------===//
26 // ARMConstantPoolValue
27 //===----------------------------------------------------------------------===//
29 ARMConstantPoolValue::ARMConstantPoolValue(Type *Ty, unsigned id,
30 ARMCP::ARMCPKind kind,
32 ARMCP::ARMCPModifier modifier,
33 bool addCurrentAddress)
34 : MachineConstantPoolValue(Ty), LabelId(id), Kind(kind),
35 PCAdjust(PCAdj), Modifier(modifier),
36 AddCurrentAddress(addCurrentAddress) {}
38 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C, unsigned id,
39 ARMCP::ARMCPKind kind,
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) {}
47 ARMConstantPoolValue::~ARMConstantPoolValue() {}
49 const char *ARMConstantPoolValue::getModifierText() const {
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";
63 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
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 (CPV->LabelId == LabelId &&
73 CPV->PCAdjust == PCAdjust &&
74 CPV->Modifier == Modifier)
83 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
84 ID.AddInteger(LabelId);
85 ID.AddInteger(PCAdjust);
89 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
90 if (ACPV->Kind == Kind &&
91 ACPV->PCAdjust == PCAdjust &&
92 ACPV->Modifier == Modifier) {
93 if (ACPV->LabelId == LabelId)
95 // Two PC relative constpool entries containing the same GV address or
96 // external symbols. FIXME: What about blockaddress?
97 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
103 void ARMConstantPoolValue::dump() const {
104 errs() << " " << *this;
107 void ARMConstantPoolValue::print(raw_ostream &O) const {
108 if (Modifier) O << "(" << getModifierText() << ")";
110 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
111 if (AddCurrentAddress) O << "-.";
116 //===----------------------------------------------------------------------===//
117 // ARMConstantPoolConstant
118 //===----------------------------------------------------------------------===//
120 ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
123 ARMCP::ARMCPKind Kind,
125 ARMCP::ARMCPModifier Modifier,
126 bool AddCurrentAddress)
127 : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
130 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
132 ARMCP::ARMCPKind Kind,
134 ARMCP::ARMCPModifier Modifier,
135 bool AddCurrentAddress)
136 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
140 ARMConstantPoolConstant *
141 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
142 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
143 ARMCP::no_modifier, false);
146 ARMConstantPoolConstant *
147 ARMConstantPoolConstant::Create(const GlobalValue *GV,
148 ARMCP::ARMCPModifier Modifier) {
149 return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()),
150 GV, 0, ARMCP::CPValue, 0,
154 ARMConstantPoolConstant *
155 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
156 ARMCP::ARMCPKind Kind, unsigned char PCAdj) {
157 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj,
158 ARMCP::no_modifier, false);
161 ARMConstantPoolConstant *
162 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
163 ARMCP::ARMCPKind Kind, unsigned char PCAdj,
164 ARMCP::ARMCPModifier Modifier,
165 bool AddCurrentAddress) {
166 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier,
170 const GlobalValue *ARMConstantPoolConstant::getGV() const {
171 return dyn_cast_or_null<GlobalValue>(CVal);
174 const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
175 return dyn_cast_or_null<BlockAddress>(CVal);
178 int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
179 unsigned Alignment) {
180 unsigned AlignMask = Alignment - 1;
181 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
182 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
183 if (Constants[i].isMachineConstantPoolEntry() &&
184 (Constants[i].getAlignment() & AlignMask) == 0) {
185 ARMConstantPoolValue *CPV =
186 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
187 ARMConstantPoolConstant *APC = dyn_cast<ARMConstantPoolConstant>(CPV);
190 if (APC->getGV() == this->CVal &&
191 APC->getLabelId() == this->getLabelId() &&
192 APC->getPCAdjustment() == this->getPCAdjustment() &&
193 APC->getModifier() == this->getModifier())
201 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
202 const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
203 return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
206 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
208 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
211 void ARMConstantPoolConstant::print(raw_ostream &O) const {
212 O << CVal->getName();
213 ARMConstantPoolValue::print(O);
216 //===----------------------------------------------------------------------===//
217 // ARMConstantPoolSymbol
218 //===----------------------------------------------------------------------===//
220 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
223 ARMCP::ARMCPModifier Modifier,
224 bool AddCurrentAddress)
225 : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
229 ARMConstantPoolSymbol::~ARMConstantPoolSymbol() {
233 ARMConstantPoolSymbol *
234 ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
235 unsigned ID, unsigned char PCAdj) {
236 return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false);
239 static bool CPV_streq(const char *S1, const char *S2) {
242 if (S1 && S2 && strcmp(S1, S2) == 0)
247 int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
248 unsigned Alignment) {
249 unsigned AlignMask = Alignment - 1;
250 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
251 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
252 if (Constants[i].isMachineConstantPoolEntry() &&
253 (Constants[i].getAlignment() & AlignMask) == 0) {
254 ARMConstantPoolValue *CPV =
255 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
256 ARMConstantPoolSymbol *APS = dyn_cast<ARMConstantPoolSymbol>(CPV);
259 if (APS->getLabelId() == this->getLabelId() &&
260 APS->getPCAdjustment() == this->getPCAdjustment() &&
261 CPV_streq(APS->getSymbol(), this->getSymbol()) &&
262 APS->getModifier() == this->getModifier())
270 bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) {
271 const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV);
272 return ACPS && CPV_streq(ACPS->S, S) &&
273 ARMConstantPoolValue::hasSameValue(ACPV);
276 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
278 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
281 void ARMConstantPoolSymbol::print(raw_ostream &O) const {
283 ARMConstantPoolValue::print(O);
286 //===----------------------------------------------------------------------===//
287 // ARMConstantPoolMBB
288 //===----------------------------------------------------------------------===//
290 ARMConstantPoolMBB::ARMConstantPoolMBB(LLVMContext &C,
291 const MachineBasicBlock *mbb,
292 unsigned id, unsigned char PCAdj,
293 ARMCP::ARMCPModifier Modifier,
294 bool AddCurrentAddress)
295 : ARMConstantPoolValue(C, id, ARMCP::CPMachineBasicBlock, PCAdj,
296 Modifier, AddCurrentAddress),
299 ARMConstantPoolMBB *ARMConstantPoolMBB::Create(LLVMContext &C,
300 const MachineBasicBlock *mbb,
302 unsigned char PCAdj) {
303 return new ARMConstantPoolMBB(C, mbb, ID, PCAdj, ARMCP::no_modifier, false);
306 int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
307 unsigned Alignment) {
308 unsigned AlignMask = Alignment - 1;
309 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
310 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
311 if (Constants[i].isMachineConstantPoolEntry() &&
312 (Constants[i].getAlignment() & AlignMask) == 0) {
313 ARMConstantPoolValue *CPV =
314 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
315 ARMConstantPoolMBB *APMBB = dyn_cast<ARMConstantPoolMBB>(CPV);
316 if (!APMBB) continue;
318 if (APMBB->getLabelId() == this->getLabelId() &&
319 APMBB->getPCAdjustment() == this->getPCAdjustment() &&
320 APMBB->getMBB() == this->getMBB() &&
321 APMBB->getModifier() == this->getModifier())
329 bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue *ACPV) {
330 const ARMConstantPoolMBB *ACPMBB = dyn_cast<ARMConstantPoolMBB>(ACPV);
331 return ACPMBB && ACPMBB->MBB == MBB &&
332 ARMConstantPoolValue::hasSameValue(ACPV);
335 void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
337 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
340 void ARMConstantPoolMBB::print(raw_ostream &O) const {
341 ARMConstantPoolValue::print(O);