blob: aadfd4779db36c877736e05126178a84934eabd2 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===- ARMConstantPoolValue.cpp - ARM constantpool value --------*- C++ -*-===//
2//
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) {
51 default: llvm_unreachable("Unknown modifier!");
52 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
53 // strings if that's legal.
54 case ARMCP::no_modifier: return "none";
55 case ARMCP::TLSGD: return "tlsgd";
56 case ARMCP::GOT: return "GOT";
57 case ARMCP::GOTOFF: return "GOTOFF";
58 case ARMCP::GOTTPOFF: return "gottpoff";
59 case ARMCP::TPOFF: return "tpoff";
60 }
61}
62
Evan Chenga8e29892007-01-19 07:51:42 +000063int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
64 unsigned Alignment) {
Bill Wendling2e6b97b2011-10-01 12:47:34 +000065 assert(false && "Shouldn't be calling this directly!");
Evan Chenga8e29892007-01-19 07:51:42 +000066 return -1;
67}
68
69void
Jim Grosbach5405d582011-09-27 20:59:33 +000070ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
Evan Chenga8e29892007-01-19 07:51:42 +000071 ID.AddInteger(LabelId);
Evan Chenga8e29892007-01-19 07:51:42 +000072 ID.AddInteger(PCAdjust);
73}
74
Evan Cheng78e5c112009-11-07 03:52:02 +000075bool
76ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
77 if (ACPV->Kind == Kind &&
Evan Cheng78e5c112009-11-07 03:52:02 +000078 ACPV->PCAdjust == PCAdjust &&
Jim Grosbach3a2429a2010-11-09 21:36:17 +000079 ACPV->Modifier == Modifier) {
Evan Cheng78e5c112009-11-07 03:52:02 +000080 if (ACPV->LabelId == LabelId)
81 return true;
82 // Two PC relative constpool entries containing the same GV address or
83 // external symbols. FIXME: What about blockaddress?
84 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
85 return true;
86 }
87 return false;
88}
89
Evan Cheng5be59ea2008-10-29 23:55:17 +000090void ARMConstantPoolValue::dump() const {
Chris Lattner705e07f2009-08-23 03:41:05 +000091 errs() << " " << *this;
Evan Cheng5be59ea2008-10-29 23:55:17 +000092}
93
Chris Lattner944fac72008-08-23 22:23:09 +000094void ARMConstantPoolValue::print(raw_ostream &O) const {
Jim Grosbach3a2429a2010-11-09 21:36:17 +000095 if (Modifier) O << "(" << getModifierText() << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000096 if (PCAdjust != 0) {
Evan Cheng25e04782008-11-04 00:50:32 +000097 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
98 if (AddCurrentAddress) O << "-.";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000099 O << ")";
100 }
Evan Chenga8e29892007-01-19 07:51:42 +0000101}
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000102
103//===----------------------------------------------------------------------===//
104// ARMConstantPoolConstant
105//===----------------------------------------------------------------------===//
106
Bill Wendling3e944e32011-10-01 07:52:37 +0000107ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty,
108 const Constant *C,
109 unsigned ID,
110 ARMCP::ARMCPKind Kind,
111 unsigned char PCAdj,
112 ARMCP::ARMCPModifier Modifier,
113 bool AddCurrentAddress)
114 : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress),
115 CVal(C) {}
116
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000117ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C,
118 unsigned ID,
119 ARMCP::ARMCPKind Kind,
120 unsigned char PCAdj,
121 ARMCP::ARMCPModifier Modifier,
122 bool AddCurrentAddress)
123 : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier,
124 AddCurrentAddress),
125 CVal(C) {}
126
127ARMConstantPoolConstant *
128ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) {
129 return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0,
130 ARMCP::no_modifier, false);
131}
132
Bill Wendling029e93882011-10-01 06:44:24 +0000133ARMConstantPoolConstant *
Bill Wendling3e944e32011-10-01 07:52:37 +0000134ARMConstantPoolConstant::Create(const GlobalValue *GV,
135 ARMCP::ARMCPModifier Modifier) {
136 return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()),
137 GV, 0, ARMCP::CPValue, 0,
138 Modifier, false);
139}
140
141ARMConstantPoolConstant *
Bill Wendling029e93882011-10-01 06:44:24 +0000142ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
143 ARMCP::ARMCPKind Kind, unsigned char PCAdj) {
144 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj,
145 ARMCP::no_modifier, false);
146}
147
Bill Wendling3e944e32011-10-01 07:52:37 +0000148ARMConstantPoolConstant *
149ARMConstantPoolConstant::Create(const Constant *C, unsigned ID,
150 ARMCP::ARMCPKind Kind, unsigned char PCAdj,
151 ARMCP::ARMCPModifier Modifier,
152 bool AddCurrentAddress) {
153 return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier,
154 AddCurrentAddress);
155}
156
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000157const GlobalValue *ARMConstantPoolConstant::getGV() const {
Bill Wendling3e944e32011-10-01 07:52:37 +0000158 return dyn_cast_or_null<GlobalValue>(CVal);
159}
160
161const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const {
162 return dyn_cast_or_null<BlockAddress>(CVal);
163}
164
165int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
166 unsigned Alignment) {
167 unsigned AlignMask = Alignment - 1;
168 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
169 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
170 if (Constants[i].isMachineConstantPoolEntry() &&
171 (Constants[i].getAlignment() & AlignMask) == 0) {
172 ARMConstantPoolValue *CPV =
173 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
174 ARMConstantPoolConstant *APC = dyn_cast<ARMConstantPoolConstant>(CPV);
175 if (!APC) continue;
Bill Wendling405ca132011-10-01 12:44:28 +0000176 if (APC->CVal == CVal && equals(APC))
Bill Wendling3e944e32011-10-01 07:52:37 +0000177 return i;
178 }
179 }
180
181 return -1;
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000182}
183
184bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) {
185 const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV);
Bill Wendling3e944e32011-10-01 07:52:37 +0000186 return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV);
Bill Wendlingf2b76aa2011-10-01 06:40:33 +0000187}
188
189void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
190 ID.AddPointer(CVal);
191 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
192}
193
194void ARMConstantPoolConstant::print(raw_ostream &O) const {
195 O << CVal->getName();
196 ARMConstantPoolValue::print(O);
197}
Bill Wendlingff4a8022011-10-01 08:36:59 +0000198
199//===----------------------------------------------------------------------===//
200// ARMConstantPoolSymbol
201//===----------------------------------------------------------------------===//
202
203ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext &C, const char *s,
204 unsigned id,
205 unsigned char PCAdj,
206 ARMCP::ARMCPModifier Modifier,
207 bool AddCurrentAddress)
208 : ARMConstantPoolValue(C, id, ARMCP::CPExtSymbol, PCAdj, Modifier,
209 AddCurrentAddress),
210 S(strdup(s)) {}
211
212ARMConstantPoolSymbol::~ARMConstantPoolSymbol() {
213 free((void*)S);
214}
215
216ARMConstantPoolSymbol *
217ARMConstantPoolSymbol::Create(LLVMContext &C, const char *s,
Bill Wendlingfe31e672011-10-01 08:58:29 +0000218 unsigned ID, unsigned char PCAdj) {
219 return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false);
220}
221
Bill Wendling9aca75c2011-10-01 09:04:18 +0000222static bool CPV_streq(const char *S1, const char *S2) {
223 if (S1 == S2)
224 return true;
225 if (S1 && S2 && strcmp(S1, S2) == 0)
226 return true;
227 return false;
228}
229
Bill Wendlingff4a8022011-10-01 08:36:59 +0000230int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
231 unsigned Alignment) {
232 unsigned AlignMask = Alignment - 1;
233 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
234 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
235 if (Constants[i].isMachineConstantPoolEntry() &&
236 (Constants[i].getAlignment() & AlignMask) == 0) {
237 ARMConstantPoolValue *CPV =
238 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
239 ARMConstantPoolSymbol *APS = dyn_cast<ARMConstantPoolSymbol>(CPV);
240 if (!APS) continue;
241
Bill Wendling405ca132011-10-01 12:44:28 +0000242 if (CPV_streq(APS->S, S) && equals(APS))
Bill Wendlingff4a8022011-10-01 08:36:59 +0000243 return i;
244 }
245 }
246
247 return -1;
248}
249
250bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) {
251 const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV);
252 return ACPS && CPV_streq(ACPS->S, S) &&
253 ARMConstantPoolValue::hasSameValue(ACPV);
254}
255
256void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
257 ID.AddPointer(S);
258 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
259}
260
261void ARMConstantPoolSymbol::print(raw_ostream &O) const {
262 O << S;
263 ARMConstantPoolValue::print(O);
264}
Bill Wendling9c18f512011-10-01 09:19:10 +0000265
266//===----------------------------------------------------------------------===//
267// ARMConstantPoolMBB
268//===----------------------------------------------------------------------===//
269
Bill Wendling3320f2a2011-10-01 09:30:42 +0000270ARMConstantPoolMBB::ARMConstantPoolMBB(LLVMContext &C,
271 const MachineBasicBlock *mbb,
Bill Wendling9c18f512011-10-01 09:19:10 +0000272 unsigned id, unsigned char PCAdj,
273 ARMCP::ARMCPModifier Modifier,
274 bool AddCurrentAddress)
Bill Wendling3320f2a2011-10-01 09:30:42 +0000275 : ARMConstantPoolValue(C, id, ARMCP::CPMachineBasicBlock, PCAdj,
Bill Wendling9c18f512011-10-01 09:19:10 +0000276 Modifier, AddCurrentAddress),
277 MBB(mbb) {}
278
279ARMConstantPoolMBB *ARMConstantPoolMBB::Create(LLVMContext &C,
Bill Wendling3320f2a2011-10-01 09:30:42 +0000280 const MachineBasicBlock *mbb,
Bill Wendling9c18f512011-10-01 09:19:10 +0000281 unsigned ID,
282 unsigned char PCAdj) {
283 return new ARMConstantPoolMBB(C, mbb, ID, PCAdj, ARMCP::no_modifier, false);
284}
285
286int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
287 unsigned Alignment) {
288 unsigned AlignMask = Alignment - 1;
289 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
290 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
291 if (Constants[i].isMachineConstantPoolEntry() &&
292 (Constants[i].getAlignment() & AlignMask) == 0) {
293 ARMConstantPoolValue *CPV =
294 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
295 ARMConstantPoolMBB *APMBB = dyn_cast<ARMConstantPoolMBB>(CPV);
296 if (!APMBB) continue;
297
Bill Wendling405ca132011-10-01 12:44:28 +0000298 if (APMBB->MBB == MBB && equals(APMBB))
Bill Wendling9c18f512011-10-01 09:19:10 +0000299 return i;
300 }
301 }
302
303 return -1;
304}
305
306bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue *ACPV) {
307 const ARMConstantPoolMBB *ACPMBB = dyn_cast<ARMConstantPoolMBB>(ACPV);
308 return ACPMBB && ACPMBB->MBB == MBB &&
309 ARMConstantPoolValue::hasSameValue(ACPV);
310}
311
312void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
313 ID.AddPointer(MBB);
314 ARMConstantPoolValue::addSelectionDAGCSEId(ID);
315}
316
317void ARMConstantPoolMBB::print(raw_ostream &O) const {
318 ARMConstantPoolValue::print(O);
319}