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 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
52 // strings if that's legal.
53 case ARMCP::no_modifier: return "none";
54 case ARMCP::TLSGD: return "tlsgd";
55 case ARMCP::GOT: return "GOT";
56 case ARMCP::GOTOFF: return "GOTOFF";
57 case ARMCP::GOTTPOFF: return "gottpoff";
58 case ARMCP::TPOFF: return "tpoff";
60 llvm_unreachable("Unknown modifier!");
63 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
65 llvm_unreachable("Shouldn't be calling this directly!");
69 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
70 ID.AddInteger(LabelId);
71 ID.AddInteger(PCAdjust);
75 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
76 if (ACPV->Kind == Kind &&
77 ACPV->PCAdjust == PCAdjust &&
78 ACPV->Modifier == Modifier) {
79 if (ACPV->LabelId == LabelId)
81 // Two PC relative constpool entries containing the same GV address or
82 // external symbols. FIXME: What about blockaddress?
83 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
89 void ARMConstantPoolValue::dump() const {
90 errs() << " " << *this;
93 void ARMConstantPoolValue::print(raw_ostream &O) const {
94 if (Modifier) O << "(" << getModifierText() << ")";
96 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
97 if (AddCurrentAddress) O << "-.";
102 //===----------------------------------------------------------------------===//
103 // ARMConstantPoolConstant
104 //===----------------------------------------------------------------------===//
106 ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
109 ARMCP::ARMCPKind Kind,
111 ARMCP::ARMCPModifier Modifier,
112 bool AddCurrentAddress)
113 : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
116 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
118 ARMCP::ARMCPKind Kind,
120 ARMCP::ARMCPModifier Modifier,
121 bool AddCurrentAddress)
122 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
126 ARMConstantPoolConstant *
127 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
128 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
129 ARMCP::no_modifier, false);
132 ARMConstantPoolConstant *
133 ARMConstantPoolConstant::Create(const GlobalValue *GV,
134 ARMCP::ARMCPModifier Modifier) {
135 return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()),
136 GV, 0, ARMCP::CPValue, 0,
140 ARMConstantPoolConstant *
141 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
142 ARMCP::ARMCPKind Kind, unsigned char PCAdj) {
143 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj,
144 ARMCP::no_modifier, false);
147 ARMConstantPoolConstant *
148 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
149 ARMCP::ARMCPKind Kind, unsigned char PCAdj,
150 ARMCP::ARMCPModifier Modifier,
151 bool AddCurrentAddress) {
152 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier,
156 const GlobalValue *ARMConstantPoolConstant::getGV() const {
157 return dyn_cast_or_null<GlobalValue>(CVal);
160 const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
161 return dyn_cast_or_null<BlockAddress>(CVal);
164 int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
165 unsigned Alignment) {
166 unsigned AlignMask = Alignment - 1;
167 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
168 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
169 if (Constants[i].isMachineConstantPoolEntry() &&
170 (Constants[i].getAlignment() & AlignMask) == 0) {
171 ARMConstantPoolValue *CPV =
172 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
173 ARMConstantPoolConstant *APC = dyn_cast<ARMConstantPoolConstant>(CPV);
175 if (APC->CVal == CVal && equals(APC))
183 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
184 const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
185 return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
188 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
190 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
193 void ARMConstantPoolConstant::print(raw_ostream &O) const {
194 O << CVal->getName();
195 ARMConstantPoolValue::print(O);
198 //===----------------------------------------------------------------------===//
199 // ARMConstantPoolSymbol
200 //===----------------------------------------------------------------------===//
202 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
205 ARMCP::ARMCPModifier Modifier,
206 bool AddCurrentAddress)
207 : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
211 ARMConstantPoolSymbol::~ARMConstantPoolSymbol() {
215 ARMConstantPoolSymbol *
216 ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
217 unsigned ID, unsigned char PCAdj) {
218 return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false);
221 static bool CPV_streq(const char *S1, const char *S2) {
224 if (S1 && S2 && strcmp(S1, S2) == 0)
229 int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
230 unsigned Alignment) {
231 unsigned AlignMask = Alignment - 1;
232 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
233 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
234 if (Constants[i].isMachineConstantPoolEntry() &&
235 (Constants[i].getAlignment() & AlignMask) == 0) {
236 ARMConstantPoolValue *CPV =
237 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
238 ARMConstantPoolSymbol *APS = dyn_cast<ARMConstantPoolSymbol>(CPV);
241 if (CPV_streq(APS->S, S) && equals(APS))
249 bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) {
250 const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV);
251 return ACPS && CPV_streq(ACPS->S, S) &&
252 ARMConstantPoolValue::hasSameValue(ACPV);
255 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
257 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
260 void ARMConstantPoolSymbol::print(raw_ostream &O) const {
262 ARMConstantPoolValue::print(O);
265 //===----------------------------------------------------------------------===//
266 // ARMConstantPoolMBB
267 //===----------------------------------------------------------------------===//
269 ARMConstantPoolMBB::ARMConstantPoolMBB(LLVMContext &C,
270 const MachineBasicBlock *mbb,
271 unsigned id, unsigned char PCAdj,
272 ARMCP::ARMCPModifier Modifier,
273 bool AddCurrentAddress)
274 : ARMConstantPoolValue(C, id, ARMCP::CPMachineBasicBlock, PCAdj,
275 Modifier, AddCurrentAddress),
278 ARMConstantPoolMBB *ARMConstantPoolMBB::Create(LLVMContext &C,
279 const MachineBasicBlock *mbb,
281 unsigned char PCAdj) {
282 return new ARMConstantPoolMBB(C, mbb, ID, PCAdj, ARMCP::no_modifier, false);
285 int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
286 unsigned Alignment) {
287 unsigned AlignMask = Alignment - 1;
288 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
289 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
290 if (Constants[i].isMachineConstantPoolEntry() &&
291 (Constants[i].getAlignment() & AlignMask) == 0) {
292 ARMConstantPoolValue *CPV =
293 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
294 ARMConstantPoolMBB *APMBB = dyn_cast<ARMConstantPoolMBB>(CPV);
295 if (!APMBB) continue;
297 if (APMBB->MBB == MBB && equals(APMBB))
305 bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue *ACPV) {
306 const ARMConstantPoolMBB *ACPMBB = dyn_cast<ARMConstantPoolMBB>(ACPV);
307 return ACPMBB && ACPMBB->MBB == MBB &&
308 ARMConstantPoolValue::hasSameValue(ACPV);
311 void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
313 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
316 void ARMConstantPoolMBB::print(raw_ostream &O) const {
317 O << "BB#" << MBB->getNumber();
318 ARMConstantPoolValue::print(O);