blob: fa3226e37eb9a216174d1ab1bb50c1dfdf1fb55f [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- ARMConstantPoolValue.cpp - ARM constantpool value -----------------===//
Evan Chenga8e29892007-01-19 07:51:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
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"
Bob Wilson28989a82009-11-02 16:59:06 +000016#include "llvm/Constant.h"
17#include "llvm/Constants.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "llvm/GlobalValue.h"
Evan Chengc60e76d2007-01-30 20:37:08 +000019#include "llvm/Type.h"
Bill Wendling4dd9b092011-09-29 23:48:44 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner944fac72008-08-23 22:23:09 +000021#include "llvm/Support/raw_ostream.h"
Jim Grosbachf1287872009-08-11 15:26:27 +000022#include <cstdlib>
Evan Chenga8e29892007-01-19 07:51:42 +000023using namespace llvm;
24
Bill Wendlingf2b76aa2011-10-01 06:40:33 +000025//===----------------------------------------------------------------------===//
26// ARMConstantPoolValue
27//===----------------------------------------------------------------------===//
28
29ARMConstantPoolValue::ARMConstantPoolValue(Type *Ty, unsigned id,
30 ARMCP::ARMCPKind kind,
31 unsigned char PCAdj,
32 ARMCP::ARMCPModifier modifier,
33 bool addCurrentAddress)
Bill Wendling3320f2a2011-10-01 09:30:42 +000034 : MachineConstantPoolValue(Ty), LabelId(id), Kind(kind),
Bill Wendling3e944e32011-10-01 07:52:37 +000035 PCAdjust(PCAdj), Modifier(modifier),
36 AddCurrentAddress(addCurrentAddress) {}
Bill Wendlingf2b76aa2011-10-01 06:40:33 +000037
Bill Wendlingff4a8022011-10-01 08:36:59 +000038ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C, unsigned id,
39 ARMCP::ARMCPKind kind,
40 unsigned char PCAdj,
41 ARMCP::ARMCPModifier modifier,
42 bool addCurrentAddress)
43 : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
Bill Wendling9aca75c2011-10-01 09:04:18 +000044 LabelId(id), Kind(kind), PCAdjust(PCAdj), Modifier(modifier),
Bill Wendlingff4a8022011-10-01 08:36:59 +000045 AddCurrentAddress(addCurrentAddress) {}
46
Bill Wendling9aca75c2011-10-01 09:04:18 +000047ARMConstantPoolValue::~ARMConstantPoolValue() {}
Bill Wendlingff4a8022011-10-01 08:36:59 +000048
Bill Wendlingd98f8382011-09-30 18:42:06 +000049const char *ARMConstantPoolValue::getModifierText() const {
50 switch (Modifier) {
Bill Wendlingd98f8382011-09-30 18:42:06 +000051 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
52 // strings if that's legal.
53 case ARMCP::no_modifier: return "none";
54 case ARMCP::TLSGD: return "tlsgd";
55 case ARMCP::GOT: return "GOT";
56 case ARMCP::GOTOFF: return "GOTOFF";
57 case ARMCP::GOTTPOFF: return "gottpoff";
58 case ARMCP::TPOFF: return "tpoff";
59 }
David Blaikie2dd674f2012-01-16 23:24:27 +000060 llvm_unreachable("Unknown modifier!");
Bill Wendlingd98f8382011-09-30 18:42:06 +000061}
62
Evan Chenga8e29892007-01-19 07:51:42 +000063int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
64 unsigned Alignment) {
Craig Topperbc219812012-02-07 02:50:20 +000065 llvm_unreachable("Shouldn't be calling this directly!");
Evan Chenga8e29892007-01-19 07:51:42 +000066}
67
68void
Jim Grosbach5405d582011-09-27 20:59:33 +000069ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
Evan Chenga8e29892007-01-19 07:51:42 +000070 ID.AddInteger(LabelId);
Evan Chenga8e29892007-01-19 07:51:42 +000071 ID.AddInteger(PCAdjust);
72}
73
Evan Cheng78e5c112009-11-07 03:52:02 +000074bool
75ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
76 if (ACPV->Kind == Kind &&
Evan Cheng78e5c112009-11-07 03:52:02 +000077 ACPV->PCAdjust == PCAdjust &&
Jim Grosbach3a2429a2010-11-09 21:36:17 +000078 ACPV->Modifier == Modifier) {
Evan Cheng78e5c112009-11-07 03:52:02 +000079 if (ACPV->LabelId == LabelId)
80 return true;
81 // Two PC relative constpool entries containing the same GV address or
82 // external symbols. FIXME: What about blockaddress?
83 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
84 return true;
85 }
86 return false;
87}
88
Evan Cheng5be59ea2008-10-29 23:55:17 +000089void ARMConstantPoolValue::dump() const {
Chris Lattner705e07f2009-08-23 03:41:05 +000090 errs() << " " << *this;
Evan Cheng5be59ea2008-10-29 23:55:17 +000091}
92
Chris Lattner944fac72008-08-23 22:23:09 +000093void ARMConstantPoolValue::print(raw_ostream &O) const {
Jim Grosbach3a2429a2010-11-09 21:36:17 +000094 if (Modifier) O << "(" << getModifierText() << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000095 if (PCAdjust != 0) {
Evan Cheng25e04782008-11-04 00:50:32 +000096 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
97 if (AddCurrentAddress) O << "-.";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000098 O << ")";
99 }
Evan Chenga8e29892007-01-19 07:51:42 +0000100}
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000101
102//===----------------------------------------------------------------------===//
103// ARMConstantPoolConstant
104//===----------------------------------------------------------------------===//
105
Bill Wendling3e944e32011-10-01 07:52:37 +0000106ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
107 const Constant *C,
108 unsigned ID,
109 ARMCP::ARMCPKind Kind,
110 unsigned char PCAdj,
111 ARMCP::ARMCPModifier Modifier,
112 bool AddCurrentAddress)
113 : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
114 CVal(C) {}
115
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000116ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
117 unsigned ID,
118 ARMCP::ARMCPKind Kind,
119 unsigned char PCAdj,
120 ARMCP::ARMCPModifier Modifier,
121 bool AddCurrentAddress)
122 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
123 AddCurrentAddress),
124 CVal(C) {}
125
126ARMConstantPoolConstant *
127ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
128 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
129 ARMCP::no_modifier, false);
130}
131
Bill Wendling029e93882011-10-01 06:44:24 +0000132ARMConstantPoolConstant *
Bill Wendling3e944e32011-10-01 07:52:37 +0000133ARMConstantPoolConstant::Create(const GlobalValue *GV,
134 ARMCP::ARMCPModifier Modifier) {
135 return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()),
136 GV, 0, ARMCP::CPValue, 0,
137 Modifier, false);
138}
139
140ARMConstantPoolConstant *
Bill Wendling029e93882011-10-01 06:44:24 +0000141ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
142 ARMCP::ARMCPKind Kind, unsigned char PCAdj) {
143 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj,
144 ARMCP::no_modifier, false);
145}
146
Bill Wendling3e944e32011-10-01 07:52:37 +0000147ARMConstantPoolConstant *
148ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
149 ARMCP::ARMCPKind Kind, unsigned char PCAdj,
150 ARMCP::ARMCPModifier Modifier,
151 bool AddCurrentAddress) {
152 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier,
153 AddCurrentAddress);
154}
155
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000156const GlobalValue *ARMConstantPoolConstant::getGV() const {
Bill Wendling3e944e32011-10-01 07:52:37 +0000157 return dyn_cast_or_null<GlobalValue>(CVal);
158}
159
160const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
161 return dyn_cast_or_null<BlockAddress>(CVal);
162}
163
164int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
165 unsigned Alignment) {
166 unsigned AlignMask = Alignment - 1;
167 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
168 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
169 if (Constants[i].isMachineConstantPoolEntry() &&
170 (Constants[i].getAlignment() & AlignMask) == 0) {
171 ARMConstantPoolValue *CPV =
172 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
173 ARMConstantPoolConstant *APC = dyn_cast<ARMConstantPoolConstant>(CPV);
174 if (!APC) continue;
Bill Wendling405ca132011-10-01 12:44:28 +0000175 if (APC->CVal == CVal && equals(APC))
Bill Wendling3e944e32011-10-01 07:52:37 +0000176 return i;
177 }
178 }
179
180 return -1;
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000181}
182
183bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
184 const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
Bill Wendling3e944e32011-10-01 07:52:37 +0000185 return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000186}
187
188void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
189 ID.AddPointer(CVal);
190 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
191}
192
193void ARMConstantPoolConstant::print(raw_ostream &O) const {
194 O << CVal->getName();
195 ARMConstantPoolValue::print(O);
196}
Bill Wendlingff4a8022011-10-01 08:36:59 +0000197
198//===----------------------------------------------------------------------===//
199// ARMConstantPoolSymbol
200//===----------------------------------------------------------------------===//
201
202ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
203 unsigned id,
204 unsigned char PCAdj,
205 ARMCP::ARMCPModifier Modifier,
206 bool AddCurrentAddress)
207 : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
208 AddCurrentAddress),
209 S(strdup(s)) {}
210
211ARMConstantPoolSymbol::~ARMConstantPoolSymbol() {
212 free((void*)S);
213}
214
215ARMConstantPoolSymbol *
216ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
Bill Wendlingfe31e672011-10-01 08:58:29 +0000217 unsigned ID, unsigned char PCAdj) {
218 return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false);
219}
220
Bill Wendling9aca75c2011-10-01 09:04:18 +0000221static bool CPV_streq(const char *S1, const char *S2) {
222 if (S1 == S2)
223 return true;
224 if (S1 && S2 && strcmp(S1, S2) == 0)
225 return true;
226 return false;
227}
228
Bill Wendlingff4a8022011-10-01 08:36:59 +0000229int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
230 unsigned Alignment) {
231 unsigned AlignMask = Alignment - 1;
232 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
233 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
234 if (Constants[i].isMachineConstantPoolEntry() &&
235 (Constants[i].getAlignment() & AlignMask) == 0) {
236 ARMConstantPoolValue *CPV =
237 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
238 ARMConstantPoolSymbol *APS = dyn_cast<ARMConstantPoolSymbol>(CPV);
239 if (!APS) continue;
240
Bill Wendling405ca132011-10-01 12:44:28 +0000241 if (CPV_streq(APS->S, S) && equals(APS))
Bill Wendlingff4a8022011-10-01 08:36:59 +0000242 return i;
243 }
244 }
245
246 return -1;
247}
248
249bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) {
250 const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV);
251 return ACPS && CPV_streq(ACPS->S, S) &&
252 ARMConstantPoolValue::hasSameValue(ACPV);
253}
254
255void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
256 ID.AddPointer(S);
257 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
258}
259
260void ARMConstantPoolSymbol::print(raw_ostream &O) const {
261 O << S;
262 ARMConstantPoolValue::print(O);
263}
Bill Wendling9c18f512011-10-01 09:19:10 +0000264
265//===----------------------------------------------------------------------===//
266// ARMConstantPoolMBB
267//===----------------------------------------------------------------------===//
268
Bill Wendling3320f2a2011-10-01 09:30:42 +0000269ARMConstantPoolMBB::ARMConstantPoolMBB(LLVMContext &C,
270 const MachineBasicBlock *mbb,
Bill Wendling9c18f512011-10-01 09:19:10 +0000271 unsigned id, unsigned char PCAdj,
272 ARMCP::ARMCPModifier Modifier,
273 bool AddCurrentAddress)
Bill Wendling3320f2a2011-10-01 09:30:42 +0000274 : ARMConstantPoolValue(C, id, ARMCP::CPMachineBasicBlock, PCAdj,
Bill Wendling9c18f512011-10-01 09:19:10 +0000275 Modifier, AddCurrentAddress),
276 MBB(mbb) {}
277
278ARMConstantPoolMBB *ARMConstantPoolMBB::Create(LLVMContext &C,
Bill Wendling3320f2a2011-10-01 09:30:42 +0000279 const MachineBasicBlock *mbb,
Bill Wendling9c18f512011-10-01 09:19:10 +0000280 unsigned ID,
281 unsigned char PCAdj) {
282 return new ARMConstantPoolMBB(C, mbb, ID, PCAdj, ARMCP::no_modifier, false);
283}
284
285int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
286 unsigned Alignment) {
287 unsigned AlignMask = Alignment - 1;
288 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
289 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
290 if (Constants[i].isMachineConstantPoolEntry() &&
291 (Constants[i].getAlignment() & AlignMask) == 0) {
292 ARMConstantPoolValue *CPV =
293 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
294 ARMConstantPoolMBB *APMBB = dyn_cast<ARMConstantPoolMBB>(CPV);
295 if (!APMBB) continue;
296
Bill Wendling405ca132011-10-01 12:44:28 +0000297 if (APMBB->MBB == MBB && equals(APMBB))
Bill Wendling9c18f512011-10-01 09:19:10 +0000298 return i;
299 }
300 }
301
302 return -1;
303}
304
305bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue *ACPV) {
306 const ARMConstantPoolMBB *ACPMBB = dyn_cast<ARMConstantPoolMBB>(ACPV);
307 return ACPMBB && ACPMBB->MBB == MBB &&
308 ARMConstantPoolValue::hasSameValue(ACPV);
309}
310
311void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
312 ID.AddPointer(MBB);
313 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
314}
315
316void ARMConstantPoolMBB::print(raw_ostream &O) const {
Evan Chengaed49522011-10-24 23:01:03 +0000317 O << "BB#" << MBB->getNumber();
Bill Wendling9c18f512011-10-01 09:19:10 +0000318 ARMConstantPoolValue::print(O);
319}