1 //===-- ARMConstantPoolValue.cpp - ARM constantpool value -----------------===//
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/ADT/StringRef.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/IR/Constant.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
29 //===----------------------------------------------------------------------===//
30 // ARMConstantPoolValue
31 //===----------------------------------------------------------------------===//
33 ARMConstantPoolValue::ARMConstantPoolValue(Type *Ty, unsigned id,
34 ARMCP::ARMCPKind kind,
36 ARMCP::ARMCPModifier modifier,
37 bool addCurrentAddress)
38 : MachineConstantPoolValue(Ty), LabelId(id), Kind(kind),
39 PCAdjust(PCAdj), Modifier(modifier),
40 AddCurrentAddress(addCurrentAddress) {}
42 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C, unsigned id,
43 ARMCP::ARMCPKind kind,
45 ARMCP::ARMCPModifier modifier,
46 bool addCurrentAddress)
47 : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
48 LabelId(id), Kind(kind), PCAdjust(PCAdj), Modifier(modifier),
49 AddCurrentAddress(addCurrentAddress) {}
51 ARMConstantPoolValue::~ARMConstantPoolValue() = default;
53 StringRef ARMConstantPoolValue::getModifierText() const {
55 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
56 // strings if that's legal.
57 case ARMCP::no_modifier:
72 llvm_unreachable("Unknown modifier!");
75 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
77 llvm_unreachable("Shouldn't be calling this directly!");
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 ACPV->LabelId == LabelId &&
92 ACPV->AddCurrentAddress == AddCurrentAddress) {
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 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
102 LLVM_DUMP_METHOD void ARMConstantPoolValue::dump() const {
103 errs() << " " << *this;
107 void ARMConstantPoolValue::print(raw_ostream &O) const {
108 if (Modifier) O << "(" << getModifierText() << ")";
110 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
111 if (AddCurrentAddress) O << "-.";
116 //===----------------------------------------------------------------------===//
117 // ARMConstantPoolConstant
118 //===----------------------------------------------------------------------===//
120 ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
123 ARMCP::ARMCPKind Kind,
125 ARMCP::ARMCPModifier Modifier,
126 bool AddCurrentAddress)
127 : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
130 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
132 ARMCP::ARMCPKind Kind,
134 ARMCP::ARMCPModifier Modifier,
135 bool AddCurrentAddress)
136 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
140 ARMConstantPoolConstant::ARMConstantPoolConstant(const GlobalVariable *GV,
142 : ARMConstantPoolValue((Type *)C->getType(), 0, ARMCP::CPPromotedGlobal, 0,
143 ARMCP::no_modifier, false),
146 ARMConstantPoolConstant *
147 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
148 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
149 ARMCP::no_modifier, false);
152 ARMConstantPoolConstant *
153 ARMConstantPoolConstant::Create(const GlobalVariable *GVar,
154 const Constant *Initializer) {
155 return new ARMConstantPoolConstant(GVar, Initializer);
158 ARMConstantPoolConstant *
159 ARMConstantPoolConstant::Create(const GlobalValue *GV,
160 ARMCP::ARMCPModifier Modifier) {
161 return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()),
162 GV, 0, ARMCP::CPValue, 0,
166 ARMConstantPoolConstant *
167 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
168 ARMCP::ARMCPKind Kind, unsigned char PCAdj) {
169 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj,
170 ARMCP::no_modifier, false);
173 ARMConstantPoolConstant *
174 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
175 ARMCP::ARMCPKind Kind, unsigned char PCAdj,
176 ARMCP::ARMCPModifier Modifier,
177 bool AddCurrentAddress) {
178 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier,
182 const GlobalValue *ARMConstantPoolConstant::getGV() const {
183 return dyn_cast_or_null<GlobalValue>(CVal);
186 const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
187 return dyn_cast_or_null<BlockAddress>(CVal);
190 int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
191 unsigned Alignment) {
192 return getExistingMachineCPValueImpl<ARMConstantPoolConstant>(CP, Alignment);
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, StringRef s,
215 unsigned id, unsigned char PCAdj,
216 ARMCP::ARMCPModifier Modifier,
217 bool AddCurrentAddress)
218 : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
222 ARMConstantPoolSymbol *ARMConstantPoolSymbol::Create(LLVMContext &C,
223 StringRef s, unsigned ID,
224 unsigned char PCAdj) {
225 return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false);
228 int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
229 unsigned Alignment) {
230 return getExistingMachineCPValueImpl<ARMConstantPoolSymbol>(CP, Alignment);
233 bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) {
234 const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV);
235 return ACPS && ACPS->S == S && ARMConstantPoolValue::hasSameValue(ACPV);
238 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
240 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
243 void ARMConstantPoolSymbol::print(raw_ostream &O) const {
245 ARMConstantPoolValue::print(O);
248 //===----------------------------------------------------------------------===//
249 // ARMConstantPoolMBB
250 //===----------------------------------------------------------------------===//
252 ARMConstantPoolMBB::ARMConstantPoolMBB(LLVMContext &C,
253 const MachineBasicBlock *mbb,
254 unsigned id, unsigned char PCAdj,
255 ARMCP::ARMCPModifier Modifier,
256 bool AddCurrentAddress)
257 : ARMConstantPoolValue(C, id, ARMCP::CPMachineBasicBlock, PCAdj,
258 Modifier, AddCurrentAddress),
261 ARMConstantPoolMBB *ARMConstantPoolMBB::Create(LLVMContext &C,
262 const MachineBasicBlock *mbb,
264 unsigned char PCAdj) {
265 return new ARMConstantPoolMBB(C, mbb, ID, PCAdj, ARMCP::no_modifier, false);
268 int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
269 unsigned Alignment) {
270 return getExistingMachineCPValueImpl<ARMConstantPoolMBB>(CP, Alignment);
273 bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue *ACPV) {
274 const ARMConstantPoolMBB *ACPMBB = dyn_cast<ARMConstantPoolMBB>(ACPV);
275 return ACPMBB && ACPMBB->MBB == MBB &&
276 ARMConstantPoolValue::hasSameValue(ACPV);
279 void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
281 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
284 void ARMConstantPoolMBB::print(raw_ostream &O) const {
285 O << "BB#" << MBB->getNumber();
286 ARMConstantPoolValue::print(O);