blob: fae64dc2455cfd0621fca006c25f7239efbabb8b [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMConstantPoolValue.h - ARM constantpool value ---------*- C++ -*-===//
Evan Cheng10043e22007-01-19 07:51:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ARM specific constantpool value class.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_TARGET_ARM_ARMCONSTANTPOOLVALUE_H
15#define LLVM_LIB_TARGET_ARM_ARMCONSTANTPOOLVALUE_H
Evan Cheng10043e22007-01-19 07:51:42 +000016
17#include "llvm/CodeGen/MachineConstantPool.h"
Benjamin Kramer2ef689c2013-09-16 10:17:31 +000018#include "llvm/Support/Casting.h"
Jim Grosbacha942ad42010-11-09 21:36:17 +000019#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar09041342010-06-15 14:50:42 +000020#include <cstddef>
Evan Cheng10043e22007-01-19 07:51:42 +000021
22namespace llvm {
23
Bob Wilson433ab092009-11-02 16:59:06 +000024class BlockAddress;
Bill Wendlinga1127b22011-09-29 23:48:44 +000025class Constant;
Dan Gohman0597e5b2008-07-11 20:38:31 +000026class GlobalValue;
Owen Anderson55f1c092009-08-13 21:58:54 +000027class LLVMContext;
Bill Wendlinga1127b22011-09-29 23:48:44 +000028class MachineBasicBlock;
Dan Gohman0597e5b2008-07-11 20:38:31 +000029
Jim Grosbach20eac922009-09-01 01:57:56 +000030namespace ARMCP {
31 enum ARMCPKind {
32 CPValue,
Bob Wilson433ab092009-11-02 16:59:06 +000033 CPExtSymbol,
34 CPBlockAddress,
Bill Wendlinga1127b22011-09-29 23:48:44 +000035 CPLSDA,
36 CPMachineBasicBlock
Jim Grosbach20eac922009-09-01 01:57:56 +000037 };
Jim Grosbacha942ad42010-11-09 21:36:17 +000038
39 enum ARMCPModifier {
Saleem Abdulrasoolce4eee42016-06-07 03:15:01 +000040 no_modifier, /// None
41 TLSGD, /// Thread Local Storage (General Dynamic Mode)
42 GOT_PREL, /// Global Offset Table, PC Relative
43 GOTTPOFF, /// Global Offset Table, Thread Pointer Offset
44 TPOFF, /// Thread Pointer Offset
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +000045 SECREL, /// Section Relative (Windows TLS)
Oliver Stannard8331aae2016-08-08 15:28:31 +000046 SBREL, /// Static Base Relative (RWPI)
Jim Grosbacha942ad42010-11-09 21:36:17 +000047 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000048}
Jim Grosbach20eac922009-09-01 01:57:56 +000049
Evan Cheng10043e22007-01-19 07:51:42 +000050/// ARMConstantPoolValue - ARM specific constantpool value. This is used to
Bob Wilson4c00a522009-11-02 17:10:37 +000051/// represent PC-relative displacement between the address of the load
Bob Wilson433ab092009-11-02 16:59:06 +000052/// instruction and the constant being loaded, i.e. (&GV-(LPIC+8)).
Evan Cheng10043e22007-01-19 07:51:42 +000053class ARMConstantPoolValue : public MachineConstantPoolValue {
Evan Cheng10043e22007-01-19 07:51:42 +000054 unsigned LabelId; // Label id of the load.
Bob Wilson433ab092009-11-02 16:59:06 +000055 ARMCP::ARMCPKind Kind; // Kind of constant.
Bob Wilson4c00a522009-11-02 17:10:37 +000056 unsigned char PCAdjust; // Extra adjustment if constantpool is pc-relative.
Evan Cheng10043e22007-01-19 07:51:42 +000057 // 8 for ARM, 4 for Thumb.
Jim Grosbacha942ad42010-11-09 21:36:17 +000058 ARMCP::ARMCPModifier Modifier; // GV modifier i.e. (&GV(modifier)-(LPIC+8))
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +000059 bool AddCurrentAddress;
Evan Cheng10043e22007-01-19 07:51:42 +000060
Bill Wendling396c2112011-10-01 06:40:33 +000061protected:
62 ARMConstantPoolValue(Type *Ty, unsigned id, ARMCP::ARMCPKind Kind,
63 unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
64 bool AddCurrentAddress);
65
Bill Wendlingd7fa0162011-10-01 08:36:59 +000066 ARMConstantPoolValue(LLVMContext &C, unsigned id, ARMCP::ARMCPKind Kind,
67 unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
68 bool AddCurrentAddress);
Benjamin Kramer2ef689c2013-09-16 10:17:31 +000069
70 template <typename Derived>
71 int getExistingMachineCPValueImpl(MachineConstantPool *CP,
72 unsigned Alignment) {
73 unsigned AlignMask = Alignment - 1;
74 const std::vector<MachineConstantPoolEntry> &Constants = CP->getConstants();
75 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
76 if (Constants[i].isMachineConstantPoolEntry() &&
77 (Constants[i].getAlignment() & AlignMask) == 0) {
78 ARMConstantPoolValue *CPV =
79 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
80 if (Derived *APC = dyn_cast<Derived>(CPV))
81 if (cast<Derived>(this)->equals(APC))
82 return i;
83 }
84 }
85
86 return -1;
87 }
88
Evan Cheng10043e22007-01-19 07:51:42 +000089public:
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000090 ~ARMConstantPoolValue() override;
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +000091
Jim Grosbacha942ad42010-11-09 21:36:17 +000092 ARMCP::ARMCPModifier getModifier() const { return Modifier; }
Bill Wendlinge8e4dbf2011-09-30 18:42:06 +000093 const char *getModifierText() const;
Jim Grosbacha942ad42010-11-09 21:36:17 +000094 bool hasModifier() const { return Modifier != ARMCP::no_modifier; }
Bill Wendlinge8e4dbf2011-09-30 18:42:06 +000095
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +000096 bool mustAddCurrentAddress() const { return AddCurrentAddress; }
Bill Wendlinge8e4dbf2011-09-30 18:42:06 +000097
Evan Cheng10043e22007-01-19 07:51:42 +000098 unsigned getLabelId() const { return LabelId; }
Evan Cheng10043e22007-01-19 07:51:42 +000099 unsigned char getPCAdjustment() const { return PCAdjust; }
Bill Wendlinge8e4dbf2011-09-30 18:42:06 +0000100
Bob Wilson433ab092009-11-02 16:59:06 +0000101 bool isGlobalValue() const { return Kind == ARMCP::CPValue; }
102 bool isExtSymbol() const { return Kind == ARMCP::CPExtSymbol; }
Bill Wendling396c2112011-10-01 06:40:33 +0000103 bool isBlockAddress() const { return Kind == ARMCP::CPBlockAddress; }
Bill Wendlinge8e4dbf2011-09-30 18:42:06 +0000104 bool isLSDA() const { return Kind == ARMCP::CPLSDA; }
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000105 bool isMachineBasicBlock() const{ return Kind == ARMCP::CPMachineBasicBlock; }
Evan Cheng10043e22007-01-19 07:51:42 +0000106
Craig Topper6bc27bf2014-03-10 02:09:33 +0000107 int getExistingMachineCPValue(MachineConstantPool *CP,
108 unsigned Alignment) override;
Evan Cheng10043e22007-01-19 07:51:42 +0000109
Craig Topper6bc27bf2014-03-10 02:09:33 +0000110 void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
Evan Cheng10043e22007-01-19 07:51:42 +0000111
Bill Wendling396c2112011-10-01 06:40:33 +0000112 /// hasSameValue - Return true if this ARM constpool value can share the same
113 /// constantpool entry as another ARM constpool value.
114 virtual bool hasSameValue(ARMConstantPoolValue *ACPV);
Evan Cheng7ff83192009-11-07 03:52:02 +0000115
Bill Wendlingf977ff52011-10-01 12:44:28 +0000116 bool equals(const ARMConstantPoolValue *A) const {
117 return this->LabelId == A->LabelId &&
118 this->PCAdjust == A->PCAdjust &&
119 this->Modifier == A->Modifier;
120 }
121
Craig Topper6bc27bf2014-03-10 02:09:33 +0000122 void print(raw_ostream &O) const override;
Evan Chengde9dbc52008-10-29 23:55:17 +0000123 void print(raw_ostream *O) const { if (O) print(*O); }
Evan Chengde9dbc52008-10-29 23:55:17 +0000124 void dump() const;
Evan Cheng10043e22007-01-19 07:51:42 +0000125};
Evan Chengde9dbc52008-10-29 23:55:17 +0000126
Evan Chengde9dbc52008-10-29 23:55:17 +0000127inline raw_ostream &operator<<(raw_ostream &O, const ARMConstantPoolValue &V) {
128 V.print(O);
129 return O;
130}
131
Bill Wendling396c2112011-10-01 06:40:33 +0000132/// ARMConstantPoolConstant - ARM-specific constant pool values for Constants,
133/// Functions, and BlockAddresses.
134class ARMConstantPoolConstant : public ARMConstantPoolValue {
135 const Constant *CVal; // Constant being loaded.
136
137 ARMConstantPoolConstant(const Constant *C,
138 unsigned ID,
139 ARMCP::ARMCPKind Kind,
140 unsigned char PCAdj,
141 ARMCP::ARMCPModifier Modifier,
142 bool AddCurrentAddress);
Bill Wendlingf117a352011-10-01 07:52:37 +0000143 ARMConstantPoolConstant(Type *Ty, const Constant *C,
144 unsigned ID,
145 ARMCP::ARMCPKind Kind,
146 unsigned char PCAdj,
147 ARMCP::ARMCPModifier Modifier,
148 bool AddCurrentAddress);
149
Bill Wendling396c2112011-10-01 06:40:33 +0000150public:
151 static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID);
Bill Wendlingf117a352011-10-01 07:52:37 +0000152 static ARMConstantPoolConstant *Create(const GlobalValue *GV,
153 ARMCP::ARMCPModifier Modifier);
Bill Wendling67225562011-10-01 06:44:24 +0000154 static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID,
155 ARMCP::ARMCPKind Kind,
156 unsigned char PCAdj);
Bill Wendlingf117a352011-10-01 07:52:37 +0000157 static ARMConstantPoolConstant *Create(const Constant *C, unsigned ID,
158 ARMCP::ARMCPKind Kind,
159 unsigned char PCAdj,
160 ARMCP::ARMCPModifier Modifier,
161 bool AddCurrentAddress);
Bill Wendling396c2112011-10-01 06:40:33 +0000162
163 const GlobalValue *getGV() const;
Bill Wendlingf117a352011-10-01 07:52:37 +0000164 const BlockAddress *getBlockAddress() const;
165
Craig Topper6bc27bf2014-03-10 02:09:33 +0000166 int getExistingMachineCPValue(MachineConstantPool *CP,
167 unsigned Alignment) override;
Bill Wendling396c2112011-10-01 06:40:33 +0000168
169 /// hasSameValue - Return true if this ARM constpool value can share the same
170 /// constantpool entry as another ARM constpool value.
Craig Topper6bc27bf2014-03-10 02:09:33 +0000171 bool hasSameValue(ARMConstantPoolValue *ACPV) override;
Bill Wendling396c2112011-10-01 06:40:33 +0000172
Craig Topper6bc27bf2014-03-10 02:09:33 +0000173 void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
Bill Wendling396c2112011-10-01 06:40:33 +0000174
Craig Topper6bc27bf2014-03-10 02:09:33 +0000175 void print(raw_ostream &O) const override;
Bill Wendling396c2112011-10-01 06:40:33 +0000176 static bool classof(const ARMConstantPoolValue *APV) {
177 return APV->isGlobalValue() || APV->isBlockAddress() || APV->isLSDA();
178 }
Benjamin Kramer2ef689c2013-09-16 10:17:31 +0000179
180 bool equals(const ARMConstantPoolConstant *A) const {
181 return CVal == A->CVal && ARMConstantPoolValue::equals(A);
182 }
Bill Wendling396c2112011-10-01 06:40:33 +0000183};
184
Bill Wendlingd7fa0162011-10-01 08:36:59 +0000185/// ARMConstantPoolSymbol - ARM-specific constantpool values for external
186/// symbols.
187class ARMConstantPoolSymbol : public ARMConstantPoolValue {
Benjamin Kramer9d461102012-12-24 19:23:30 +0000188 const std::string S; // ExtSymbol being loaded.
Bill Wendlingd7fa0162011-10-01 08:36:59 +0000189
190 ARMConstantPoolSymbol(LLVMContext &C, const char *s, unsigned id,
191 unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
192 bool AddCurrentAddress);
193
194public:
Bill Wendlingd7fa0162011-10-01 08:36:59 +0000195 static ARMConstantPoolSymbol *Create(LLVMContext &C, const char *s,
Bill Wendlingc214cb02011-10-01 08:58:29 +0000196 unsigned ID, unsigned char PCAdj);
197
Benjamin Kramer9d461102012-12-24 19:23:30 +0000198 const char *getSymbol() const { return S.c_str(); }
Bill Wendlingd7fa0162011-10-01 08:36:59 +0000199
Craig Topper6bc27bf2014-03-10 02:09:33 +0000200 int getExistingMachineCPValue(MachineConstantPool *CP,
201 unsigned Alignment) override;
Bill Wendlingd7fa0162011-10-01 08:36:59 +0000202
Craig Topper6bc27bf2014-03-10 02:09:33 +0000203 void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
Bill Wendlingd7fa0162011-10-01 08:36:59 +0000204
205 /// hasSameValue - Return true if this ARM constpool value can share the same
206 /// constantpool entry as another ARM constpool value.
Craig Topper6bc27bf2014-03-10 02:09:33 +0000207 bool hasSameValue(ARMConstantPoolValue *ACPV) override;
Bill Wendlingd7fa0162011-10-01 08:36:59 +0000208
Craig Topper6bc27bf2014-03-10 02:09:33 +0000209 void print(raw_ostream &O) const override;
Bill Wendlingd7fa0162011-10-01 08:36:59 +0000210
211 static bool classof(const ARMConstantPoolValue *ACPV) {
212 return ACPV->isExtSymbol();
213 }
Benjamin Kramer2ef689c2013-09-16 10:17:31 +0000214
215 bool equals(const ARMConstantPoolSymbol *A) const {
216 return S == A->S && ARMConstantPoolValue::equals(A);
217 }
Bill Wendlingd7fa0162011-10-01 08:36:59 +0000218};
219
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000220/// ARMConstantPoolMBB - ARM-specific constantpool value of a machine basic
221/// block.
222class ARMConstantPoolMBB : public ARMConstantPoolValue {
Bill Wendling4a4772f2011-10-01 09:30:42 +0000223 const MachineBasicBlock *MBB; // Machine basic block.
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000224
Bill Wendling4a4772f2011-10-01 09:30:42 +0000225 ARMConstantPoolMBB(LLVMContext &C, const MachineBasicBlock *mbb, unsigned id,
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000226 unsigned char PCAdj, ARMCP::ARMCPModifier Modifier,
227 bool AddCurrentAddress);
228
229public:
Bill Wendling4a4772f2011-10-01 09:30:42 +0000230 static ARMConstantPoolMBB *Create(LLVMContext &C,
231 const MachineBasicBlock *mbb,
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000232 unsigned ID, unsigned char PCAdj);
233
234 const MachineBasicBlock *getMBB() const { return MBB; }
235
Craig Topper6bc27bf2014-03-10 02:09:33 +0000236 int getExistingMachineCPValue(MachineConstantPool *CP,
237 unsigned Alignment) override;
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000238
Craig Topper6bc27bf2014-03-10 02:09:33 +0000239 void addSelectionDAGCSEId(FoldingSetNodeID &ID) override;
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000240
241 /// hasSameValue - Return true if this ARM constpool value can share the same
242 /// constantpool entry as another ARM constpool value.
Craig Topper6bc27bf2014-03-10 02:09:33 +0000243 bool hasSameValue(ARMConstantPoolValue *ACPV) override;
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000244
Craig Topper6bc27bf2014-03-10 02:09:33 +0000245 void print(raw_ostream &O) const override;
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000246
247 static bool classof(const ARMConstantPoolValue *ACPV) {
248 return ACPV->isMachineBasicBlock();
249 }
Benjamin Kramer2ef689c2013-09-16 10:17:31 +0000250
251 bool equals(const ARMConstantPoolMBB *A) const {
252 return MBB == A->MBB && ARMConstantPoolValue::equals(A);
253 }
Bill Wendling6dbc9fe2011-10-01 09:19:10 +0000254};
255
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000256} // End llvm namespace
Evan Cheng10043e22007-01-19 07:51:42 +0000257
258#endif