1 //===- ARMConstantPoolValue.cpp - ARM constantpool value ------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the ARM specific constantpool value class.
11 //===----------------------------------------------------------------------===//
13 #include "ARMConstantPoolValue.h"
14 #include "llvm/ADT/FoldingSet.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/IR/Constant.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
28 //===----------------------------------------------------------------------===//
29 // ARMConstantPoolValue
30 //===----------------------------------------------------------------------===//
32 ARMConstantPoolValue::ARMConstantPoolValue(Type
*Ty
, unsigned id
,
33 ARMCP::ARMCPKind kind
,
35 ARMCP::ARMCPModifier modifier
,
36 bool addCurrentAddress
)
37 : MachineConstantPoolValue(Ty
), LabelId(id
), Kind(kind
),
38 PCAdjust(PCAdj
), Modifier(modifier
),
39 AddCurrentAddress(addCurrentAddress
) {}
41 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext
&C
, unsigned id
,
42 ARMCP::ARMCPKind kind
,
44 ARMCP::ARMCPModifier modifier
,
45 bool addCurrentAddress
)
46 : MachineConstantPoolValue((Type
*)Type::getInt32Ty(C
)),
47 LabelId(id
), Kind(kind
), PCAdjust(PCAdj
), Modifier(modifier
),
48 AddCurrentAddress(addCurrentAddress
) {}
50 ARMConstantPoolValue::~ARMConstantPoolValue() = default;
52 StringRef
ARMConstantPoolValue::getModifierText() const {
54 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
55 // strings if that's legal.
56 case ARMCP::no_modifier
:
71 llvm_unreachable("Unknown modifier!");
74 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool
*CP
,
76 llvm_unreachable("Shouldn't be calling this directly!");
80 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID
&ID
) {
81 ID
.AddInteger(LabelId
);
82 ID
.AddInteger(PCAdjust
);
86 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue
*ACPV
) {
87 if (ACPV
->Kind
== Kind
&&
88 ACPV
->PCAdjust
== PCAdjust
&&
89 ACPV
->Modifier
== Modifier
&&
90 ACPV
->LabelId
== LabelId
&&
91 ACPV
->AddCurrentAddress
== AddCurrentAddress
) {
92 // Two PC relative constpool entries containing the same GV address or
93 // external symbols. FIXME: What about blockaddress?
94 if (Kind
== ARMCP::CPValue
|| Kind
== ARMCP::CPExtSymbol
)
100 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
101 LLVM_DUMP_METHOD
void ARMConstantPoolValue::dump() const {
102 errs() << " " << *this;
106 void ARMConstantPoolValue::print(raw_ostream
&O
) const {
107 if (Modifier
) O
<< "(" << getModifierText() << ")";
109 O
<< "-(LPC" << LabelId
<< "+" << (unsigned)PCAdjust
;
110 if (AddCurrentAddress
) O
<< "-.";
115 //===----------------------------------------------------------------------===//
116 // ARMConstantPoolConstant
117 //===----------------------------------------------------------------------===//
119 ARMConstantPoolConstant::ARMConstantPoolConstant(Type
*Ty
,
122 ARMCP::ARMCPKind Kind
,
124 ARMCP::ARMCPModifier Modifier
,
125 bool AddCurrentAddress
)
126 : ARMConstantPoolValue(Ty
, ID
, Kind
, PCAdj
, Modifier
, AddCurrentAddress
),
129 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant
*C
,
131 ARMCP::ARMCPKind Kind
,
133 ARMCP::ARMCPModifier Modifier
,
134 bool AddCurrentAddress
)
135 : ARMConstantPoolValue((Type
*)C
->getType(), ID
, Kind
, PCAdj
, Modifier
,
139 ARMConstantPoolConstant::ARMConstantPoolConstant(const GlobalVariable
*GV
,
141 : ARMConstantPoolValue((Type
*)C
->getType(), 0, ARMCP::CPPromotedGlobal
, 0,
142 ARMCP::no_modifier
, false), CVal(C
) {
146 ARMConstantPoolConstant
*
147 ARMConstantPoolConstant::Create(const Constant
*C
, unsigned ID
) {
148 return new ARMConstantPoolConstant(C
, ID
, ARMCP::CPValue
, 0,
149 ARMCP::no_modifier
, false);
152 ARMConstantPoolConstant
*
153 ARMConstantPoolConstant::Create(const GlobalVariable
*GVar
,
154 const Constant
*Initializer
) {
155 return new ARMConstantPoolConstant(GVar
, Initializer
);
158 ARMConstantPoolConstant
*
159 ARMConstantPoolConstant::Create(const GlobalValue
*GV
,
160 ARMCP::ARMCPModifier Modifier
) {
161 return new ARMConstantPoolConstant((Type
*)Type::getInt32Ty(GV
->getContext()),
162 GV
, 0, ARMCP::CPValue
, 0,
166 ARMConstantPoolConstant
*
167 ARMConstantPoolConstant::Create(const Constant
*C
, unsigned ID
,
168 ARMCP::ARMCPKind Kind
, unsigned char PCAdj
) {
169 return new ARMConstantPoolConstant(C
, ID
, Kind
, PCAdj
,
170 ARMCP::no_modifier
, false);
173 ARMConstantPoolConstant
*
174 ARMConstantPoolConstant::Create(const Constant
*C
, unsigned ID
,
175 ARMCP::ARMCPKind Kind
, unsigned char PCAdj
,
176 ARMCP::ARMCPModifier Modifier
,
177 bool AddCurrentAddress
) {
178 return new ARMConstantPoolConstant(C
, ID
, Kind
, PCAdj
, Modifier
,
182 const GlobalValue
*ARMConstantPoolConstant::getGV() const {
183 return dyn_cast_or_null
<GlobalValue
>(CVal
);
186 const BlockAddress
*ARMConstantPoolConstant::getBlockAddress() const {
187 return dyn_cast_or_null
<BlockAddress
>(CVal
);
190 int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool
*CP
,
191 unsigned Alignment
) {
193 getExistingMachineCPValueImpl
<ARMConstantPoolConstant
>(CP
, Alignment
);
195 auto *CPV
= static_cast<ARMConstantPoolValue
*>(
196 CP
->getConstants()[index
].Val
.MachineCPVal
);
197 auto *Constant
= cast
<ARMConstantPoolConstant
>(CPV
);
198 Constant
->GVars
.insert(GVars
.begin(), GVars
.end());
203 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue
*ACPV
) {
204 const ARMConstantPoolConstant
*ACPC
= dyn_cast
<ARMConstantPoolConstant
>(ACPV
);
205 return ACPC
&& ACPC
->CVal
== CVal
&& ARMConstantPoolValue::hasSameValue(ACPV
);
208 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID
&ID
) {
210 for (const auto *GV
: GVars
)
212 ARMConstantPoolValue::addSelectionDAGCSEId(ID
);
215 void ARMConstantPoolConstant::print(raw_ostream
&O
) const {
216 O
<< CVal
->getName();
217 ARMConstantPoolValue::print(O
);
220 //===----------------------------------------------------------------------===//
221 // ARMConstantPoolSymbol
222 //===----------------------------------------------------------------------===//
224 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext
&C
, StringRef s
,
225 unsigned id
, unsigned char PCAdj
,
226 ARMCP::ARMCPModifier Modifier
,
227 bool AddCurrentAddress
)
228 : ARMConstantPoolValue(C
, id
, ARMCP::CPExtSymbol
, PCAdj
, Modifier
,
232 ARMConstantPoolSymbol
*ARMConstantPoolSymbol::Create(LLVMContext
&C
,
233 StringRef s
, unsigned ID
,
234 unsigned char PCAdj
) {
235 return new ARMConstantPoolSymbol(C
, s
, ID
, PCAdj
, ARMCP::no_modifier
, false);
238 int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool
*CP
,
239 unsigned Alignment
) {
240 return getExistingMachineCPValueImpl
<ARMConstantPoolSymbol
>(CP
, Alignment
);
243 bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue
*ACPV
) {
244 const ARMConstantPoolSymbol
*ACPS
= dyn_cast
<ARMConstantPoolSymbol
>(ACPV
);
245 return ACPS
&& ACPS
->S
== S
&& ARMConstantPoolValue::hasSameValue(ACPV
);
248 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID
&ID
) {
250 ARMConstantPoolValue::addSelectionDAGCSEId(ID
);
253 void ARMConstantPoolSymbol::print(raw_ostream
&O
) const {
255 ARMConstantPoolValue::print(O
);
258 //===----------------------------------------------------------------------===//
259 // ARMConstantPoolMBB
260 //===----------------------------------------------------------------------===//
262 ARMConstantPoolMBB::ARMConstantPoolMBB(LLVMContext
&C
,
263 const MachineBasicBlock
*mbb
,
264 unsigned id
, unsigned char PCAdj
,
265 ARMCP::ARMCPModifier Modifier
,
266 bool AddCurrentAddress
)
267 : ARMConstantPoolValue(C
, id
, ARMCP::CPMachineBasicBlock
, PCAdj
,
268 Modifier
, AddCurrentAddress
),
271 ARMConstantPoolMBB
*ARMConstantPoolMBB::Create(LLVMContext
&C
,
272 const MachineBasicBlock
*mbb
,
274 unsigned char PCAdj
) {
275 return new ARMConstantPoolMBB(C
, mbb
, ID
, PCAdj
, ARMCP::no_modifier
, false);
278 int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool
*CP
,
279 unsigned Alignment
) {
280 return getExistingMachineCPValueImpl
<ARMConstantPoolMBB
>(CP
, Alignment
);
283 bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue
*ACPV
) {
284 const ARMConstantPoolMBB
*ACPMBB
= dyn_cast
<ARMConstantPoolMBB
>(ACPV
);
285 return ACPMBB
&& ACPMBB
->MBB
== MBB
&&
286 ARMConstantPoolValue::hasSameValue(ACPV
);
289 void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID
&ID
) {
291 ARMConstantPoolValue::addSelectionDAGCSEId(ID
);
294 void ARMConstantPoolMBB::print(raw_ostream
&O
) const {
295 O
<< printMBBReference(*MBB
);
296 ARMConstantPoolValue::print(O
);