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 ARMConstantPoolValue::ARMConstantPoolValue(const Constant *cval, unsigned id,
28 ARMCP::ARMCPModifier Modif,
30 : MachineConstantPoolValue((Type*)cval->getType()),
31 CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
32 Modifier(Modif), AddCurrentAddress(AddCA) {}
34 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
35 const MachineBasicBlock *mbb,
39 ARMCP::ARMCPModifier Modif,
41 : MachineConstantPoolValue((Type*)Type::getInt8PtrTy(C)),
42 CVal(NULL), MBB(mbb), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
43 Modifier(Modif), AddCurrentAddress(AddCA) {}
45 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
46 const char *s, unsigned id,
48 ARMCP::ARMCPModifier Modif,
50 : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
51 CVal(NULL), S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
52 PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
54 ARMConstantPoolValue::ARMConstantPoolValue(const GlobalValue *gv,
55 ARMCP::ARMCPModifier Modif)
56 : MachineConstantPoolValue((Type*)Type::getInt32Ty(gv->getContext())),
57 CVal(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
58 Modifier(Modif), AddCurrentAddress(false) {}
60 const GlobalValue *ARMConstantPoolValue::getGV() const {
61 return dyn_cast_or_null<GlobalValue>(CVal);
64 const BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
65 return dyn_cast_or_null<BlockAddress>(CVal);
68 const MachineBasicBlock *ARMConstantPoolValue::getMBB() const {
72 const char *ARMConstantPoolValue::getModifierText() const {
74 default: llvm_unreachable("Unknown modifier!");
75 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
76 // strings if that's legal.
77 case ARMCP::no_modifier: return "none";
78 case ARMCP::TLSGD: return "tlsgd";
79 case ARMCP::GOT: return "GOT";
80 case ARMCP::GOTOFF: return "GOTOFF";
81 case ARMCP::GOTTPOFF: return "gottpoff";
82 case ARMCP::TPOFF: return "tpoff";
86 static bool CPV_streq(const char *S1, const char *S2) {
89 if (S1 && S2 && strcmp(S1, S2) == 0)
94 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
96 unsigned AlignMask = Alignment - 1;
97 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
98 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
99 if (Constants[i].isMachineConstantPoolEntry() &&
100 (Constants[i].getAlignment() & AlignMask) == 0) {
101 ARMConstantPoolValue *CPV =
102 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
103 if (CPV->CVal == CVal &&
104 CPV->LabelId == LabelId &&
105 CPV->PCAdjust == PCAdjust &&
106 CPV_streq(CPV->S, S) &&
107 CPV->Modifier == Modifier)
115 ARMConstantPoolValue::~ARMConstantPoolValue() {
120 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
123 ID.AddInteger(LabelId);
124 ID.AddInteger(PCAdjust);
128 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
129 if (ACPV->Kind == Kind &&
130 ACPV->CVal == CVal &&
131 ACPV->PCAdjust == PCAdjust &&
132 CPV_streq(ACPV->S, S) &&
133 ACPV->Modifier == Modifier) {
134 if (ACPV->LabelId == LabelId)
136 // Two PC relative constpool entries containing the same GV address or
137 // external symbols. FIXME: What about blockaddress?
138 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
144 void ARMConstantPoolValue::dump() const {
145 errs() << " " << *this;
149 void ARMConstantPoolValue::print(raw_ostream &O) const {
151 O << CVal->getName();
156 if (Modifier) O << "(" << getModifierText() << ")";
158 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
159 if (AddCurrentAddress) O << "-.";