Refactoring: Separate out the ARM constant pool Constant from the ARM constant
[oota-llvm.git] / lib / Target / ARM / ARMConstantPoolValue.cpp
1 //===- ARMConstantPoolValue.cpp - ARM constantpool value --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ARM specific constantpool value class.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
22 #include <cstdlib>
23 using namespace llvm;
24
25 //===----------------------------------------------------------------------===//
26 // ARMConstantPoolValue
27 //===----------------------------------------------------------------------===//
28
29 ARMConstantPoolValue::ARMConstantPoolValue(Type *Ty, unsigned id,
30                                            ARMCP::ARMCPKind kind,
31                                            unsigned char PCAdj,
32                                            ARMCP::ARMCPModifier modifier,
33                                            bool addCurrentAddress)
34   : MachineConstantPoolValue(Ty), LabelId(id), Kind(kind), PCAdjust(PCAdj),
35     Modifier(modifier), AddCurrentAddress(addCurrentAddress) {}
36
37 ARMConstantPoolValue::ARMConstantPoolValue(const Constant *cval, unsigned id,
38                                            ARMCP::ARMCPKind K,
39                                            unsigned char PCAdj,
40                                            ARMCP::ARMCPModifier Modif,
41                                            bool AddCA)
42   : MachineConstantPoolValue((Type*)cval->getType()),
43     CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
44     Modifier(Modif), AddCurrentAddress(AddCA) {}
45
46 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
47                                            const MachineBasicBlock *mbb,
48                                            unsigned id,
49                                            ARMCP::ARMCPKind K,
50                                            unsigned char PCAdj,
51                                            ARMCP::ARMCPModifier Modif,
52                                            bool AddCA)
53   : MachineConstantPoolValue((Type*)Type::getInt8PtrTy(C)),
54     CVal(NULL), MBB(mbb), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
55     Modifier(Modif), AddCurrentAddress(AddCA) {}
56
57 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
58                                            const char *s, unsigned id,
59                                            unsigned char PCAdj,
60                                            ARMCP::ARMCPModifier Modif,
61                                            bool AddCA)
62   : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
63     CVal(NULL), S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
64     PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
65
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) {}
71
72 const GlobalValue *ARMConstantPoolValue::getGV() const {
73   return dyn_cast_or_null<GlobalValue>(CVal);
74 }
75
76 const BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
77   return dyn_cast_or_null<BlockAddress>(CVal);
78 }
79
80 const MachineBasicBlock *ARMConstantPoolValue::getMBB() const {
81   return MBB;
82 }
83
84 const char *ARMConstantPoolValue::getModifierText() const {
85   switch (Modifier) {
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";
95   }
96 }
97
98 static bool CPV_streq(const char *S1, const char *S2) {
99   if (S1 == S2)
100     return true;
101   if (S1 && S2 && strcmp(S1, S2) == 0)
102     return true;
103   return false;
104 }
105
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)
120         return i;
121     }
122   }
123
124   return -1;
125 }
126
127 ARMConstantPoolValue::~ARMConstantPoolValue() {
128   free((void*)S);
129 }
130
131 void
132 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
133   ID.AddPointer(CVal);
134   ID.AddPointer(S);
135   ID.AddInteger(LabelId);
136   ID.AddInteger(PCAdjust);
137 }
138
139 bool
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)
147       return true;
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)
151       return true;
152   }
153   return false;
154 }
155
156 void ARMConstantPoolValue::dump() const {
157   errs() << "  " << *this;
158 }
159
160 void ARMConstantPoolValue::print(raw_ostream &O) const {
161   if (CVal)
162     O << CVal->getName();
163   else if (MBB)
164     O << "";
165   else
166     O << S;
167   if (Modifier) O << "(" << getModifierText() << ")";
168   if (PCAdjust != 0) {
169     O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
170     if (AddCurrentAddress) O << "-.";
171     O << ")";
172   }
173 }
174
175 //===----------------------------------------------------------------------===//
176 // ARMConstantPoolConstant
177 //===----------------------------------------------------------------------===//
178
179 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
180                                                  unsigned ID,
181                                                  ARMCP::ARMCPKind Kind,
182                                                  unsigned char PCAdj,
183                                                  ARMCP::ARMCPModifier Modifier,
184                                                  bool AddCurrentAddress)
185   : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
186                          AddCurrentAddress),
187     CVal(C) {}
188
189 ARMConstantPoolConstant *
190 ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
191   return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
192                                      ARMCP::no_modifier, false);
193 }
194
195 const GlobalValue *ARMConstantPoolConstant::getGV() const {
196   return dyn_cast<GlobalValue>(CVal);
197 }
198
199 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
200   const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
201
202   return (ACPC ? ACPC->CVal == CVal : true) &&
203     ARMConstantPoolValue::hasSameValue(ACPV);
204 }
205
206 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
207   ID.AddPointer(CVal);
208   ARMConstantPoolValue::addSelectionDAGCSEId(ID);
209 }
210
211 void ARMConstantPoolConstant::print(raw_ostream &O) const {
212   O << CVal->getName();
213   ARMConstantPoolValue::print(O);
214 }