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/GlobalVariable.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
29 //===----------------------------------------------------------------------===//
30 // ARMConstantPoolValue
31 //===----------------------------------------------------------------------===//
33 ARMConstantPoolValue::ARMConstantPoolValue(Type
*Ty
, unsigned id
,
34 ARMCP::ARMCPKind kind
,
36 ARMCP::ARMCPModifier modifier
,
37 bool addCurrentAddress
)
38 : MachineConstantPoolValue(Ty
), LabelId(id
), Kind(kind
),
39 PCAdjust(PCAdj
), Modifier(modifier
),
40 AddCurrentAddress(addCurrentAddress
) {}
42 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext
&C
, unsigned id
,
43 ARMCP::ARMCPKind kind
,
45 ARMCP::ARMCPModifier modifier
,
46 bool addCurrentAddress
)
47 : MachineConstantPoolValue((Type
*)Type::getInt32Ty(C
)),
48 LabelId(id
), Kind(kind
), PCAdjust(PCAdj
), Modifier(modifier
),
49 AddCurrentAddress(addCurrentAddress
) {}
51 ARMConstantPoolValue::~ARMConstantPoolValue() = default;
53 StringRef
ARMConstantPoolValue::getModifierText() const {
55 // FIXME: Are these case sensitive? It'd be nice to lower-case all the
56 // strings if that's legal.
57 case ARMCP::no_modifier
:
72 llvm_unreachable("Unknown modifier!");
75 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool
*CP
,
77 llvm_unreachable("Shouldn't be calling this directly!");
81 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID
&ID
) {
82 ID
.AddInteger(LabelId
);
83 ID
.AddInteger(PCAdjust
);
87 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue
*ACPV
) {
88 if (ACPV
->Kind
== Kind
&&
89 ACPV
->PCAdjust
== PCAdjust
&&
90 ACPV
->Modifier
== Modifier
&&
91 ACPV
->LabelId
== LabelId
&&
92 ACPV
->AddCurrentAddress
== AddCurrentAddress
) {
93 // Two PC relative constpool entries containing the same GV address or
94 // external symbols. FIXME: What about blockaddress?
95 if (Kind
== ARMCP::CPValue
|| Kind
== ARMCP::CPExtSymbol
)
101 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
102 LLVM_DUMP_METHOD
void ARMConstantPoolValue::dump() const {
103 errs() << " " << *this;
107 void ARMConstantPoolValue::print(raw_ostream
&O
) const {
108 if (Modifier
) O
<< "(" << getModifierText() << ")";
110 O
<< "-(LPC" << LabelId
<< "+" << (unsigned)PCAdjust
;
111 if (AddCurrentAddress
) O
<< "-.";
116 //===----------------------------------------------------------------------===//
117 // ARMConstantPoolConstant
118 //===----------------------------------------------------------------------===//
120 ARMConstantPoolConstant::ARMConstantPoolConstant(Type
*Ty
,
123 ARMCP::ARMCPKind Kind
,
125 ARMCP::ARMCPModifier Modifier
,
126 bool AddCurrentAddress
)
127 : ARMConstantPoolValue(Ty
, ID
, Kind
, PCAdj
, Modifier
, AddCurrentAddress
),
130 ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant
*C
,
132 ARMCP::ARMCPKind Kind
,
134 ARMCP::ARMCPModifier Modifier
,
135 bool AddCurrentAddress
)
136 : ARMConstantPoolValue((Type
*)C
->getType(), ID
, Kind
, PCAdj
, Modifier
,
140 ARMConstantPoolConstant::ARMConstantPoolConstant(const GlobalVariable
*GV
,
142 : ARMConstantPoolValue((Type
*)C
->getType(), 0, ARMCP::CPPromotedGlobal
, 0,
143 ARMCP::no_modifier
, false), CVal(C
) {
147 ARMConstantPoolConstant
*
148 ARMConstantPoolConstant::Create(const Constant
*C
, unsigned ID
) {
149 return new ARMConstantPoolConstant(C
, ID
, ARMCP::CPValue
, 0,
150 ARMCP::no_modifier
, false);
153 ARMConstantPoolConstant
*
154 ARMConstantPoolConstant::Create(const GlobalVariable
*GVar
,
155 const Constant
*Initializer
) {
156 return new ARMConstantPoolConstant(GVar
, Initializer
);
159 ARMConstantPoolConstant
*
160 ARMConstantPoolConstant::Create(const GlobalValue
*GV
,
161 ARMCP::ARMCPModifier Modifier
) {
162 return new ARMConstantPoolConstant((Type
*)Type::getInt32Ty(GV
->getContext()),
163 GV
, 0, ARMCP::CPValue
, 0,
167 ARMConstantPoolConstant
*
168 ARMConstantPoolConstant::Create(const Constant
*C
, unsigned ID
,
169 ARMCP::ARMCPKind Kind
, unsigned char PCAdj
) {
170 return new ARMConstantPoolConstant(C
, ID
, Kind
, PCAdj
,
171 ARMCP::no_modifier
, false);
174 ARMConstantPoolConstant
*
175 ARMConstantPoolConstant::Create(const Constant
*C
, unsigned ID
,
176 ARMCP::ARMCPKind Kind
, unsigned char PCAdj
,
177 ARMCP::ARMCPModifier Modifier
,
178 bool AddCurrentAddress
) {
179 return new ARMConstantPoolConstant(C
, ID
, Kind
, PCAdj
, Modifier
,
183 const GlobalValue
*ARMConstantPoolConstant::getGV() const {
184 return dyn_cast_or_null
<GlobalValue
>(CVal
);
187 const BlockAddress
*ARMConstantPoolConstant::getBlockAddress() const {
188 return dyn_cast_or_null
<BlockAddress
>(CVal
);
191 int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool
*CP
,
192 unsigned Alignment
) {
194 getExistingMachineCPValueImpl
<ARMConstantPoolConstant
>(CP
, Alignment
);
196 auto *CPV
= static_cast<ARMConstantPoolValue
*>(
197 CP
->getConstants()[index
].Val
.MachineCPVal
);
198 auto *Constant
= cast
<ARMConstantPoolConstant
>(CPV
);
199 Constant
->GVars
.insert(GVars
.begin(), GVars
.end());
204 bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue
*ACPV
) {
205 const ARMConstantPoolConstant
*ACPC
= dyn_cast
<ARMConstantPoolConstant
>(ACPV
);
206 return ACPC
&& ACPC
->CVal
== CVal
&& ARMConstantPoolValue::hasSameValue(ACPV
);
209 void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID
&ID
) {
211 for (const auto *GV
: GVars
)
213 ARMConstantPoolValue::addSelectionDAGCSEId(ID
);
216 void ARMConstantPoolConstant::print(raw_ostream
&O
) const {
217 O
<< CVal
->getName();
218 ARMConstantPoolValue::print(O
);
221 //===----------------------------------------------------------------------===//
222 // ARMConstantPoolSymbol
223 //===----------------------------------------------------------------------===//
225 ARMConstantPoolSymbol::ARMConstantPoolSymbol(LLVMContext
&C
, StringRef s
,
226 unsigned id
, unsigned char PCAdj
,
227 ARMCP::ARMCPModifier Modifier
,
228 bool AddCurrentAddress
)
229 : ARMConstantPoolValue(C
, id
, ARMCP::CPExtSymbol
, PCAdj
, Modifier
,
233 ARMConstantPoolSymbol
*ARMConstantPoolSymbol::Create(LLVMContext
&C
,
234 StringRef s
, unsigned ID
,
235 unsigned char PCAdj
) {
236 return new ARMConstantPoolSymbol(C
, s
, ID
, PCAdj
, ARMCP::no_modifier
, false);
239 int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool
*CP
,
240 unsigned Alignment
) {
241 return getExistingMachineCPValueImpl
<ARMConstantPoolSymbol
>(CP
, Alignment
);
244 bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue
*ACPV
) {
245 const ARMConstantPoolSymbol
*ACPS
= dyn_cast
<ARMConstantPoolSymbol
>(ACPV
);
246 return ACPS
&& ACPS
->S
== S
&& ARMConstantPoolValue::hasSameValue(ACPV
);
249 void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID
&ID
) {
251 ARMConstantPoolValue::addSelectionDAGCSEId(ID
);
254 void ARMConstantPoolSymbol::print(raw_ostream
&O
) const {
256 ARMConstantPoolValue::print(O
);
259 //===----------------------------------------------------------------------===//
260 // ARMConstantPoolMBB
261 //===----------------------------------------------------------------------===//
263 ARMConstantPoolMBB::ARMConstantPoolMBB(LLVMContext
&C
,
264 const MachineBasicBlock
*mbb
,
265 unsigned id
, unsigned char PCAdj
,
266 ARMCP::ARMCPModifier Modifier
,
267 bool AddCurrentAddress
)
268 : ARMConstantPoolValue(C
, id
, ARMCP::CPMachineBasicBlock
, PCAdj
,
269 Modifier
, AddCurrentAddress
),
272 ARMConstantPoolMBB
*ARMConstantPoolMBB::Create(LLVMContext
&C
,
273 const MachineBasicBlock
*mbb
,
275 unsigned char PCAdj
) {
276 return new ARMConstantPoolMBB(C
, mbb
, ID
, PCAdj
, ARMCP::no_modifier
, false);
279 int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool
*CP
,
280 unsigned Alignment
) {
281 return getExistingMachineCPValueImpl
<ARMConstantPoolMBB
>(CP
, Alignment
);
284 bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue
*ACPV
) {
285 const ARMConstantPoolMBB
*ACPMBB
= dyn_cast
<ARMConstantPoolMBB
>(ACPV
);
286 return ACPMBB
&& ACPMBB
->MBB
== MBB
&&
287 ARMConstantPoolValue::hasSameValue(ACPV
);
290 void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID
&ID
) {
292 ARMConstantPoolValue::addSelectionDAGCSEId(ID
);
295 void ARMConstantPoolMBB::print(raw_ostream
&O
) const {
296 O
<< printMBBReference(*MBB
);
297 ARMConstantPoolValue::print(O
);