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 (this->equals(CPV))
81 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
82 ID.AddInteger(LabelId);
83 ID.AddInteger(PCAdjust);
87 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
88 if (ACPV->Kind == Kind &&
89 ACPV->PCAdjust == PCAdjust &&
90 ACPV->Modifier == Modifier) {
91 if (ACPV->LabelId == LabelId)
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)
101 void ARMConstantPoolValue::dump() const {
102 errs() << " " << *this;
105 void ARMConstantPoolValue::print(raw_ostream &O) const {
106 if (Modifier) O << "(" << getModifierText() << ")";
108 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
109 if (AddCurrentAddress) O << "-.";
114 //===----------------------------------------------------------------------===//
115 // ARMConstantPoolConstant
116 //===----------------------------------------------------------------------===//
118 ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
121 ARMCP::ARMCPKind Kind,
123 ARMCP::ARMCPModifier Modifier,
124 bool AddCurrentAddress)
125 : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
128 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
130 ARMCP::ARMCPKind Kind,
132 ARMCP::ARMCPModifier Modifier,
133 bool AddCurrentAddress)
134 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
138 ARMConstantPoolConstant *
139 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
140 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
141 ARMCP::no_modifier, false);
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,
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);
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,
168 const GlobalValue *ARMConstantPoolConstant::getGV() const {
169 return dyn_cast_or_null<GlobalValue>(CVal);
172 const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
173 return dyn_cast_or_null<BlockAddress>(CVal);
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);
187 if (APC->CVal == CVal && equals(APC))
195 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
196 const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
197 return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
200 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
202 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
205 void ARMConstantPoolConstant::print(raw_ostream &O) const {
206 O << CVal->getName();
207 ARMConstantPoolValue::print(O);
210 //===----------------------------------------------------------------------===//
211 // ARMConstantPoolSymbol
212 //===----------------------------------------------------------------------===//
214 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
217 ARMCP::ARMCPModifier Modifier,
218 bool AddCurrentAddress)
219 : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
223 ARMConstantPoolSymbol::~ARMConstantPoolSymbol() {
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);
233 static bool CPV_streq(const char *S1, const char *S2) {
236 if (S1 && S2 && strcmp(S1, S2) == 0)
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);
253 if (CPV_streq(APS->S, S) && equals(APS))
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);
267 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
269 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
272 void ARMConstantPoolSymbol::print(raw_ostream &O) const {
274 ARMConstantPoolValue::print(O);
277 //===----------------------------------------------------------------------===//
278 // ARMConstantPoolMBB
279 //===----------------------------------------------------------------------===//
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),
290 ARMConstantPoolMBB *ARMConstantPoolMBB::Create(LLVMContext &C,
291 const MachineBasicBlock *mbb,
293 unsigned char PCAdj) {
294 return new ARMConstantPoolMBB(C, mbb, ID, PCAdj, ARMCP::no_modifier, false);
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;
309 if (APMBB->MBB == MBB && equals(APMBB))
317 bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue *ACPV) {
318 const ARMConstantPoolMBB *ACPMBB = dyn_cast<ARMConstantPoolMBB>(ACPV);
319 return ACPMBB && ACPMBB->MBB == MBB &&
320 ARMConstantPoolValue::hasSameValue(ACPV);
323 void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
325 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
328 void ARMConstantPoolMBB::print(raw_ostream &O) const {
329 ARMConstantPoolValue::print(O);