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), MBB(NULL), 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(LLVMContext &C,
48 const MachineBasicBlock *mbb,
52 ARMCP::ARMCPModifier Modif,
54 : MachineConstantPoolValue((Type*)Type::getInt8PtrTy(C)),
55 MBB(mbb), LabelId(id), Kind(K), PCAdjust(PCAdj),
56 Modifier(Modif), AddCurrentAddress(AddCA) {}
58 ARMConstantPoolValue::~ARMConstantPoolValue() {}
60 const MachineBasicBlock *ARMConstantPoolValue::getMBB() const {
64 const char *ARMConstantPoolValue::getModifierText() const {
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";
78 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
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)
98 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
99 ID.AddInteger(LabelId);
100 ID.AddInteger(PCAdjust);
104 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
105 if (ACPV->Kind == Kind &&
106 ACPV->PCAdjust == PCAdjust &&
107 ACPV->Modifier == Modifier) {
108 if (ACPV->LabelId == LabelId)
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)
118 void ARMConstantPoolValue::dump() const {
119 errs() << " " << *this;
122 void ARMConstantPoolValue::print(raw_ostream &O) const {
125 if (Modifier) O << "(" << getModifierText() << ")";
127 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
128 if (AddCurrentAddress) O << "-.";
133 //===----------------------------------------------------------------------===//
134 // ARMConstantPoolConstant
135 //===----------------------------------------------------------------------===//
137 ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
140 ARMCP::ARMCPKind Kind,
142 ARMCP::ARMCPModifier Modifier,
143 bool AddCurrentAddress)
144 : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
147 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
149 ARMCP::ARMCPKind Kind,
151 ARMCP::ARMCPModifier Modifier,
152 bool AddCurrentAddress)
153 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
157 ARMConstantPoolConstant *
158 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
159 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
160 ARMCP::no_modifier, false);
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,
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);
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,
187 const GlobalValue *ARMConstantPoolConstant::getGV() const {
188 return dyn_cast_or_null<GlobalValue>(CVal);
191 const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
192 return dyn_cast_or_null<BlockAddress>(CVal);
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);
207 if (APC->getGV() == this->CVal &&
208 APC->getLabelId() == this->getLabelId() &&
209 APC->getPCAdjustment() == this->getPCAdjustment() &&
210 APC->getModifier() == this->getModifier())
218 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
219 const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
220 return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
223 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
225 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
228 void ARMConstantPoolConstant::print(raw_ostream &O) const {
229 O << CVal->getName();
230 ARMConstantPoolValue::print(O);
233 //===----------------------------------------------------------------------===//
234 // ARMConstantPoolSymbol
235 //===----------------------------------------------------------------------===//
237 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
240 ARMCP::ARMCPModifier Modifier,
241 bool AddCurrentAddress)
242 : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
246 ARMConstantPoolSymbol::~ARMConstantPoolSymbol() {
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);
256 static bool CPV_streq(const char *S1, const char *S2) {
259 if (S1 && S2 && strcmp(S1, S2) == 0)
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);
276 if (APS->getLabelId() == this->getLabelId() &&
277 APS->getPCAdjustment() == this->getPCAdjustment() &&
278 CPV_streq(APS->getSymbol(), this->getSymbol()) &&
279 APS->getModifier() == this->getModifier())
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);
293 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
295 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
298 void ARMConstantPoolSymbol::print(raw_ostream &O) const {
300 ARMConstantPoolValue::print(O);