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), PCAdjust(PCAdj),
35 Modifier(modifier), AddCurrentAddress(addCurrentAddress) {}
37 ARMConstantPoolValue::ARMConstantPoolValue(const Constant *cval, unsigned id,
40 ARMCP::ARMCPModifier Modif,
42 : MachineConstantPoolValue((Type*)cval->getType()),
43 CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
44 Modifier(Modif), AddCurrentAddress(AddCA) {}
46 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
47 const MachineBasicBlock *mbb,
51 ARMCP::ARMCPModifier Modif,
53 : MachineConstantPoolValue((Type*)Type::getInt8PtrTy(C)),
54 CVal(NULL), MBB(mbb), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
55 Modifier(Modif), AddCurrentAddress(AddCA) {}
57 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
58 const char *s, unsigned id,
60 ARMCP::ARMCPModifier Modif,
62 : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
63 CVal(NULL), S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
64 PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
66 ARMConstantPoolValue::ARMConstantPoolValue(const GlobalValue *gv,
67 ARMCP::ARMCPModifier Modif)
68 : MachineConstantPoolValue((Type*)Type::getInt32Ty(gv->getContext())),
69 CVal(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
70 Modifier(Modif), AddCurrentAddress(false) {}
72 const GlobalValue *ARMConstantPoolValue::getGV() const {
73 return dyn_cast_or_null<GlobalValue>(CVal);
76 const BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
77 return dyn_cast_or_null<BlockAddress>(CVal);
80 const MachineBasicBlock *ARMConstantPoolValue::getMBB() const {
84 const char *ARMConstantPoolValue::getModifierText() const {
86 default: llvm_unreachable("Unknown modifier!");
87 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
88 // strings if that's legal.
89 case ARMCP::no_modifier: return "none";
90 case ARMCP::TLSGD: return "tlsgd";
91 case ARMCP::GOT: return "GOT";
92 case ARMCP::GOTOFF: return "GOTOFF";
93 case ARMCP::GOTTPOFF: return "gottpoff";
94 case ARMCP::TPOFF: return "tpoff";
98 static bool CPV_streq(const char *S1, const char *S2) {
101 if (S1 && S2 && strcmp(S1, S2) == 0)
106 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
107 unsigned Alignment) {
108 unsigned AlignMask = Alignment - 1;
109 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
110 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
111 if (Constants[i].isMachineConstantPoolEntry() &&
112 (Constants[i].getAlignment() & AlignMask) == 0) {
113 ARMConstantPoolValue *CPV =
114 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
115 if (CPV->CVal == CVal &&
116 CPV->LabelId == LabelId &&
117 CPV->PCAdjust == PCAdjust &&
118 CPV_streq(CPV->S, S) &&
119 CPV->Modifier == Modifier)
127 ARMConstantPoolValue::~ARMConstantPoolValue() {
132 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
135 ID.AddInteger(LabelId);
136 ID.AddInteger(PCAdjust);
140 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
141 if (ACPV->Kind == Kind &&
142 ACPV->CVal == CVal &&
143 ACPV->PCAdjust == PCAdjust &&
144 CPV_streq(ACPV->S, S) &&
145 ACPV->Modifier == Modifier) {
146 if (ACPV->LabelId == LabelId)
148 // Two PC relative constpool entries containing the same GV address or
149 // external symbols. FIXME: What about blockaddress?
150 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
156 void ARMConstantPoolValue::dump() const {
157 errs() << " " << *this;
160 void ARMConstantPoolValue::print(raw_ostream &O) const {
162 O << CVal->getName();
167 if (Modifier) O << "(" << getModifierText() << ")";
169 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
170 if (AddCurrentAddress) O << "-.";
175 //===----------------------------------------------------------------------===//
176 // ARMConstantPoolConstant
177 //===----------------------------------------------------------------------===//
179 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
181 ARMCP::ARMCPKind Kind,
183 ARMCP::ARMCPModifier Modifier,
184 bool AddCurrentAddress)
185 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
189 ARMConstantPoolConstant *
190 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
191 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
192 ARMCP::no_modifier, false);
195 const GlobalValue *ARMConstantPoolConstant::getGV() const {
196 return dyn_cast<GlobalValue>(CVal);
199 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
200 const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
202 return (ACPC ? ACPC->CVal == CVal : true) &&
203 ARMConstantPoolValue::hasSameValue(ACPV);
206 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
208 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
211 void ARMConstantPoolConstant::print(raw_ostream &O) const {
212 O << CVal->getName();
213 ARMConstantPoolValue::print(O);