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), S(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 S(NULL), 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), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
56 Modifier(Modif), AddCurrentAddress(AddCA) {}
58 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
59 const char *s, unsigned id,
61 ARMCP::ARMCPModifier Modif,
63 : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
64 S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
65 PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
67 ARMConstantPoolValue::~ARMConstantPoolValue() {
71 const MachineBasicBlock *ARMConstantPoolValue::getMBB() const {
75 const char *ARMConstantPoolValue::getModifierText() const {
77 default: llvm_unreachable("Unknown modifier!");
78 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
79 // strings if that's legal.
80 case ARMCP::no_modifier: return "none";
81 case ARMCP::TLSGD: return "tlsgd";
82 case ARMCP::GOT: return "GOT";
83 case ARMCP::GOTOFF: return "GOTOFF";
84 case ARMCP::GOTTPOFF: return "gottpoff";
85 case ARMCP::TPOFF: return "tpoff";
89 static bool CPV_streq(const char *S1, const char *S2) {
92 if (S1 && S2 && strcmp(S1, S2) == 0)
97 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
99 unsigned AlignMask = Alignment - 1;
100 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
101 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
102 if (Constants[i].isMachineConstantPoolEntry() &&
103 (Constants[i].getAlignment() & AlignMask) == 0) {
104 ARMConstantPoolValue *CPV =
105 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
106 if (CPV->LabelId == LabelId &&
107 CPV->PCAdjust == PCAdjust &&
108 CPV_streq(CPV->S, S) &&
109 CPV->Modifier == Modifier)
118 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
120 ID.AddInteger(LabelId);
121 ID.AddInteger(PCAdjust);
125 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
126 if (ACPV->Kind == Kind &&
127 ACPV->PCAdjust == PCAdjust &&
128 CPV_streq(ACPV->S, S) &&
129 ACPV->Modifier == Modifier) {
130 if (ACPV->LabelId == LabelId)
132 // Two PC relative constpool entries containing the same GV address or
133 // external symbols. FIXME: What about blockaddress?
134 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
140 void ARMConstantPoolValue::dump() const {
141 errs() << " " << *this;
144 void ARMConstantPoolValue::print(raw_ostream &O) const {
149 if (Modifier) O << "(" << getModifierText() << ")";
151 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
152 if (AddCurrentAddress) O << "-.";
157 //===----------------------------------------------------------------------===//
158 // ARMConstantPoolConstant
159 //===----------------------------------------------------------------------===//
161 ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
164 ARMCP::ARMCPKind Kind,
166 ARMCP::ARMCPModifier Modifier,
167 bool AddCurrentAddress)
168 : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
171 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
173 ARMCP::ARMCPKind Kind,
175 ARMCP::ARMCPModifier Modifier,
176 bool AddCurrentAddress)
177 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
181 ARMConstantPoolConstant *
182 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
183 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
184 ARMCP::no_modifier, false);
187 ARMConstantPoolConstant *
188 ARMConstantPoolConstant::Create(const GlobalValue *GV,
189 ARMCP::ARMCPModifier Modifier) {
190 return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()),
191 GV, 0, ARMCP::CPValue, 0,
195 ARMConstantPoolConstant *
196 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
197 ARMCP::ARMCPKind Kind, unsigned char PCAdj) {
198 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj,
199 ARMCP::no_modifier, false);
202 ARMConstantPoolConstant *
203 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
204 ARMCP::ARMCPKind Kind, unsigned char PCAdj,
205 ARMCP::ARMCPModifier Modifier,
206 bool AddCurrentAddress) {
207 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier,
211 const GlobalValue *ARMConstantPoolConstant::getGV() const {
212 return dyn_cast_or_null<GlobalValue>(CVal);
215 const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
216 return dyn_cast_or_null<BlockAddress>(CVal);
219 int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
220 unsigned Alignment) {
221 unsigned AlignMask = Alignment - 1;
222 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
223 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
224 if (Constants[i].isMachineConstantPoolEntry() &&
225 (Constants[i].getAlignment() & AlignMask) == 0) {
226 ARMConstantPoolValue *CPV =
227 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
228 ARMConstantPoolConstant *APC = dyn_cast<ARMConstantPoolConstant>(CPV);
231 if (APC->getGV() == this->CVal &&
232 APC->getLabelId() == this->getLabelId() &&
233 APC->getPCAdjustment() == this->getPCAdjustment() &&
234 APC->getModifier() == this->getModifier())
242 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
243 const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
244 return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
247 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
249 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
252 void ARMConstantPoolConstant::print(raw_ostream &O) const {
253 O << CVal->getName();
254 ARMConstantPoolValue::print(O);
257 //===----------------------------------------------------------------------===//
258 // ARMConstantPoolSymbol
259 //===----------------------------------------------------------------------===//
261 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
264 ARMCP::ARMCPModifier Modifier,
265 bool AddCurrentAddress)
266 : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
270 ARMConstantPoolSymbol::~ARMConstantPoolSymbol() {
274 ARMConstantPoolSymbol *
275 ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
276 unsigned ID, unsigned char PCAdj) {
277 return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false);
280 ARMConstantPoolSymbol *
281 ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
282 unsigned ID, unsigned char PCAdj,
283 ARMCP::ARMCPModifier Modifier,
284 bool AddCurrentAddress) {
285 return new ARMConstantPoolSymbol(C, s, ID, PCAdj, Modifier,
289 int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
290 unsigned Alignment) {
291 unsigned AlignMask = Alignment - 1;
292 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
293 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
294 if (Constants[i].isMachineConstantPoolEntry() &&
295 (Constants[i].getAlignment() & AlignMask) == 0) {
296 ARMConstantPoolValue *CPV =
297 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
298 ARMConstantPoolSymbol *APS = dyn_cast<ARMConstantPoolSymbol>(CPV);
301 if (APS->getLabelId() == this->getLabelId() &&
302 APS->getPCAdjustment() == this->getPCAdjustment() &&
303 CPV_streq(APS->getSymbol(), this->getSymbol()) &&
304 APS->getModifier() == this->getModifier())
312 bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) {
313 const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV);
314 return ACPS && CPV_streq(ACPS->S, S) &&
315 ARMConstantPoolValue::hasSameValue(ACPV);
318 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
320 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
323 void ARMConstantPoolSymbol::print(raw_ostream &O) const {
325 ARMConstantPoolValue::print(O);