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,
39 const MachineBasicBlock *mbb,
43 ARMCP::ARMCPModifier Modif,
45 : MachineConstantPoolValue((Type*)Type::getInt8PtrTy(C)),
46 MBB(mbb), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
47 Modifier(Modif), AddCurrentAddress(AddCA) {}
49 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
50 const char *s, unsigned id,
52 ARMCP::ARMCPModifier Modif,
54 : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
55 S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
56 PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
58 const MachineBasicBlock *ARMConstantPoolValue::getMBB() const {
62 const char *ARMConstantPoolValue::getModifierText() const {
64 default: llvm_unreachable("Unknown modifier!");
65 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
66 // strings if that's legal.
67 case ARMCP::no_modifier: return "none";
68 case ARMCP::TLSGD: return "tlsgd";
69 case ARMCP::GOT: return "GOT";
70 case ARMCP::GOTOFF: return "GOTOFF";
71 case ARMCP::GOTTPOFF: return "gottpoff";
72 case ARMCP::TPOFF: return "tpoff";
76 static bool CPV_streq(const char *S1, const char *S2) {
79 if (S1 && S2 && strcmp(S1, S2) == 0)
84 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
86 unsigned AlignMask = Alignment - 1;
87 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
88 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
89 if (Constants[i].isMachineConstantPoolEntry() &&
90 (Constants[i].getAlignment() & AlignMask) == 0) {
91 ARMConstantPoolValue *CPV =
92 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
93 if (CPV->LabelId == LabelId &&
94 CPV->PCAdjust == PCAdjust &&
95 CPV_streq(CPV->S, S) &&
96 CPV->Modifier == Modifier)
104 ARMConstantPoolValue::~ARMConstantPoolValue() {
109 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
111 ID.AddInteger(LabelId);
112 ID.AddInteger(PCAdjust);
116 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
117 if (ACPV->Kind == Kind &&
118 ACPV->PCAdjust == PCAdjust &&
119 CPV_streq(ACPV->S, S) &&
120 ACPV->Modifier == Modifier) {
121 if (ACPV->LabelId == LabelId)
123 // Two PC relative constpool entries containing the same GV address or
124 // external symbols. FIXME: What about blockaddress?
125 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
131 void ARMConstantPoolValue::dump() const {
132 errs() << " " << *this;
135 void ARMConstantPoolValue::print(raw_ostream &O) const {
140 if (Modifier) O << "(" << getModifierText() << ")";
142 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
143 if (AddCurrentAddress) O << "-.";
148 //===----------------------------------------------------------------------===//
149 // ARMConstantPoolConstant
150 //===----------------------------------------------------------------------===//
152 ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
155 ARMCP::ARMCPKind Kind,
157 ARMCP::ARMCPModifier Modifier,
158 bool AddCurrentAddress)
159 : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
162 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
164 ARMCP::ARMCPKind Kind,
166 ARMCP::ARMCPModifier Modifier,
167 bool AddCurrentAddress)
168 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
172 ARMConstantPoolConstant *
173 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
174 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
175 ARMCP::no_modifier, false);
178 ARMConstantPoolConstant *
179 ARMConstantPoolConstant::Create(const GlobalValue *GV,
180 ARMCP::ARMCPModifier Modifier) {
181 return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()),
182 GV, 0, ARMCP::CPValue, 0,
186 ARMConstantPoolConstant *
187 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
188 ARMCP::ARMCPKind Kind, unsigned char PCAdj) {
189 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj,
190 ARMCP::no_modifier, false);
193 ARMConstantPoolConstant *
194 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
195 ARMCP::ARMCPKind Kind, unsigned char PCAdj,
196 ARMCP::ARMCPModifier Modifier,
197 bool AddCurrentAddress) {
198 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier,
202 const GlobalValue *ARMConstantPoolConstant::getGV() const {
203 return dyn_cast_or_null<GlobalValue>(CVal);
206 const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
207 return dyn_cast_or_null<BlockAddress>(CVal);
210 int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
211 unsigned Alignment) {
212 unsigned AlignMask = Alignment - 1;
213 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
214 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
215 if (Constants[i].isMachineConstantPoolEntry() &&
216 (Constants[i].getAlignment() & AlignMask) == 0) {
217 ARMConstantPoolValue *CPV =
218 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
219 ARMConstantPoolConstant *APC = dyn_cast<ARMConstantPoolConstant>(CPV);
222 if (APC->getGV() == this->CVal &&
223 APC->getLabelId() == this->getLabelId() &&
224 APC->getPCAdjustment() == this->getPCAdjustment() &&
225 CPV_streq(APC->getSymbol(), this->getSymbol()) &&
226 APC->getModifier() == this->getModifier())
234 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
235 const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
236 return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
239 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
241 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
244 void ARMConstantPoolConstant::print(raw_ostream &O) const {
245 O << CVal->getName();
246 ARMConstantPoolValue::print(O);