/src/llvm-project/llvm/lib/Target/ARM/ARMConstantPoolValue.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- ARMConstantPoolValue.cpp - ARM constantpool value ------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements the ARM specific constantpool value class. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
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" |
26 | | |
27 | | using namespace llvm; |
28 | | |
29 | | //===----------------------------------------------------------------------===// |
30 | | // ARMConstantPoolValue |
31 | | //===----------------------------------------------------------------------===// |
32 | | |
33 | | ARMConstantPoolValue::ARMConstantPoolValue(Type *Ty, unsigned id, |
34 | | ARMCP::ARMCPKind kind, |
35 | | unsigned char PCAdj, |
36 | | ARMCP::ARMCPModifier modifier, |
37 | | bool addCurrentAddress) |
38 | | : MachineConstantPoolValue(Ty), LabelId(id), Kind(kind), |
39 | | PCAdjust(PCAdj), Modifier(modifier), |
40 | 0 | AddCurrentAddress(addCurrentAddress) {} |
41 | | |
42 | | ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C, unsigned id, |
43 | | ARMCP::ARMCPKind kind, |
44 | | unsigned char PCAdj, |
45 | | ARMCP::ARMCPModifier modifier, |
46 | | bool addCurrentAddress) |
47 | | : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)), |
48 | | LabelId(id), Kind(kind), PCAdjust(PCAdj), Modifier(modifier), |
49 | 0 | AddCurrentAddress(addCurrentAddress) {} |
50 | | |
51 | 0 | ARMConstantPoolValue::~ARMConstantPoolValue() = default; |
52 | | |
53 | 0 | StringRef ARMConstantPoolValue::getModifierText() const { |
54 | 0 | switch (Modifier) { |
55 | | // FIXME: Are these case sensitive? It'd be nice to lower-case all the |
56 | | // strings if that's legal. |
57 | 0 | case ARMCP::no_modifier: |
58 | 0 | return "none"; |
59 | 0 | case ARMCP::TLSGD: |
60 | 0 | return "tlsgd"; |
61 | 0 | case ARMCP::GOT_PREL: |
62 | 0 | return "GOT_PREL"; |
63 | 0 | case ARMCP::GOTTPOFF: |
64 | 0 | return "gottpoff"; |
65 | 0 | case ARMCP::TPOFF: |
66 | 0 | return "tpoff"; |
67 | 0 | case ARMCP::SBREL: |
68 | 0 | return "SBREL"; |
69 | 0 | case ARMCP::SECREL: |
70 | 0 | return "secrel32"; |
71 | 0 | } |
72 | 0 | llvm_unreachable("Unknown modifier!"); |
73 | 0 | } |
74 | | |
75 | | int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP, |
76 | 0 | Align Alignment) { |
77 | 0 | llvm_unreachable("Shouldn't be calling this directly!"); |
78 | 0 | } |
79 | | |
80 | | void |
81 | 0 | ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) { |
82 | 0 | ID.AddInteger(LabelId); |
83 | 0 | ID.AddInteger(PCAdjust); |
84 | 0 | } |
85 | | |
86 | | bool |
87 | 0 | ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) { |
88 | 0 | if (ACPV->Kind == Kind && |
89 | 0 | ACPV->PCAdjust == PCAdjust && |
90 | 0 | ACPV->Modifier == Modifier && |
91 | 0 | ACPV->LabelId == LabelId && |
92 | 0 | ACPV->AddCurrentAddress == AddCurrentAddress) { |
93 | | // Two PC relative constpool entries containing the same GV address or |
94 | | // external symbols. FIXME: What about blockaddress? |
95 | 0 | if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol) |
96 | 0 | return true; |
97 | 0 | } |
98 | 0 | return false; |
99 | 0 | } |
100 | | |
101 | | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
102 | 0 | LLVM_DUMP_METHOD void ARMConstantPoolValue::dump() const { |
103 | 0 | errs() << " " << *this; |
104 | 0 | } |
105 | | #endif |
106 | | |
107 | 0 | void ARMConstantPoolValue::print(raw_ostream &O) const { |
108 | 0 | if (Modifier) O << "(" << getModifierText() << ")"; |
109 | 0 | if (PCAdjust != 0) { |
110 | 0 | O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust; |
111 | 0 | if (AddCurrentAddress) O << "-."; |
112 | 0 | O << ")"; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | //===----------------------------------------------------------------------===// |
117 | | // ARMConstantPoolConstant |
118 | | //===----------------------------------------------------------------------===// |
119 | | |
120 | | ARMConstantPoolConstant::ARMConstantPoolConstant(Type *Ty, |
121 | | const Constant *C, |
122 | | unsigned ID, |
123 | | ARMCP::ARMCPKind Kind, |
124 | | unsigned char PCAdj, |
125 | | ARMCP::ARMCPModifier Modifier, |
126 | | bool AddCurrentAddress) |
127 | | : ARMConstantPoolValue(Ty, ID, Kind, PCAdj, Modifier, AddCurrentAddress), |
128 | 0 | CVal(C) {} |
129 | | |
130 | | ARMConstantPoolConstant::ARMConstantPoolConstant(const Constant *C, |
131 | | unsigned ID, |
132 | | ARMCP::ARMCPKind Kind, |
133 | | unsigned char PCAdj, |
134 | | ARMCP::ARMCPModifier Modifier, |
135 | | bool AddCurrentAddress) |
136 | | : ARMConstantPoolValue((Type*)C->getType(), ID, Kind, PCAdj, Modifier, |
137 | | AddCurrentAddress), |
138 | 0 | CVal(C) {} |
139 | | |
140 | | ARMConstantPoolConstant::ARMConstantPoolConstant(const GlobalVariable *GV, |
141 | | const Constant *C) |
142 | | : ARMConstantPoolValue((Type *)C->getType(), 0, ARMCP::CPPromotedGlobal, 0, |
143 | 0 | ARMCP::no_modifier, false), CVal(C) { |
144 | 0 | GVars.insert(GV); |
145 | 0 | } |
146 | | |
147 | | ARMConstantPoolConstant * |
148 | 0 | ARMConstantPoolConstant::Create(const Constant *C, unsigned ID) { |
149 | 0 | return new ARMConstantPoolConstant(C, ID, ARMCP::CPValue, 0, |
150 | 0 | ARMCP::no_modifier, false); |
151 | 0 | } |
152 | | |
153 | | ARMConstantPoolConstant * |
154 | | ARMConstantPoolConstant::Create(const GlobalVariable *GVar, |
155 | 0 | const Constant *Initializer) { |
156 | 0 | return new ARMConstantPoolConstant(GVar, Initializer); |
157 | 0 | } |
158 | | |
159 | | ARMConstantPoolConstant * |
160 | | ARMConstantPoolConstant::Create(const GlobalValue *GV, |
161 | 0 | ARMCP::ARMCPModifier Modifier) { |
162 | 0 | return new ARMConstantPoolConstant((Type*)Type::getInt32Ty(GV->getContext()), |
163 | 0 | GV, 0, ARMCP::CPValue, 0, |
164 | 0 | Modifier, false); |
165 | 0 | } |
166 | | |
167 | | ARMConstantPoolConstant * |
168 | | ARMConstantPoolConstant::Create(const Constant *C, unsigned ID, |
169 | 0 | ARMCP::ARMCPKind Kind, unsigned char PCAdj) { |
170 | 0 | return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, |
171 | 0 | ARMCP::no_modifier, false); |
172 | 0 | } |
173 | | |
174 | | ARMConstantPoolConstant * |
175 | | ARMConstantPoolConstant::Create(const Constant *C, unsigned ID, |
176 | | ARMCP::ARMCPKind Kind, unsigned char PCAdj, |
177 | | ARMCP::ARMCPModifier Modifier, |
178 | 0 | bool AddCurrentAddress) { |
179 | 0 | return new ARMConstantPoolConstant(C, ID, Kind, PCAdj, Modifier, |
180 | 0 | AddCurrentAddress); |
181 | 0 | } |
182 | | |
183 | 0 | const GlobalValue *ARMConstantPoolConstant::getGV() const { |
184 | 0 | return dyn_cast_or_null<GlobalValue>(CVal); |
185 | 0 | } |
186 | | |
187 | 0 | const BlockAddress *ARMConstantPoolConstant::getBlockAddress() const { |
188 | 0 | return dyn_cast_or_null<BlockAddress>(CVal); |
189 | 0 | } |
190 | | |
191 | | int ARMConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP, |
192 | 0 | Align Alignment) { |
193 | 0 | int index = |
194 | 0 | getExistingMachineCPValueImpl<ARMConstantPoolConstant>(CP, Alignment); |
195 | 0 | if (index != -1) { |
196 | 0 | auto *CPV = static_cast<ARMConstantPoolValue*>( |
197 | 0 | CP->getConstants()[index].Val.MachineCPVal); |
198 | 0 | auto *Constant = cast<ARMConstantPoolConstant>(CPV); |
199 | 0 | Constant->GVars.insert(GVars.begin(), GVars.end()); |
200 | 0 | } |
201 | 0 | return index; |
202 | 0 | } |
203 | | |
204 | 0 | bool ARMConstantPoolConstant::hasSameValue(ARMConstantPoolValue *ACPV) { |
205 | 0 | const ARMConstantPoolConstant *ACPC = dyn_cast<ARMConstantPoolConstant>(ACPV); |
206 | 0 | return ACPC && ACPC->CVal == CVal && ARMConstantPoolValue::hasSameValue(ACPV); |
207 | 0 | } |
208 | | |
209 | 0 | void ARMConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) { |
210 | 0 | ID.AddPointer(CVal); |
211 | 0 | for (const auto *GV : GVars) |
212 | 0 | ID.AddPointer(GV); |
213 | 0 | ARMConstantPoolValue::addSelectionDAGCSEId(ID); |
214 | 0 | } |
215 | | |
216 | 0 | void ARMConstantPoolConstant::print(raw_ostream &O) const { |
217 | 0 | O << CVal->getName(); |
218 | 0 | ARMConstantPoolValue::print(O); |
219 | 0 | } |
220 | | |
221 | | //===----------------------------------------------------------------------===// |
222 | | // ARMConstantPoolSymbol |
223 | | //===----------------------------------------------------------------------===// |
224 | | |
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, |
230 | | AddCurrentAddress), |
231 | 0 | S(std::string(s)) {} |
232 | | |
233 | | ARMConstantPoolSymbol *ARMConstantPoolSymbol::Create(LLVMContext &C, |
234 | | StringRef s, unsigned ID, |
235 | 0 | unsigned char PCAdj) { |
236 | 0 | return new ARMConstantPoolSymbol(C, s, ID, PCAdj, ARMCP::no_modifier, false); |
237 | 0 | } |
238 | | |
239 | | int ARMConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP, |
240 | 0 | Align Alignment) { |
241 | 0 | return getExistingMachineCPValueImpl<ARMConstantPoolSymbol>(CP, Alignment); |
242 | 0 | } |
243 | | |
244 | 0 | bool ARMConstantPoolSymbol::hasSameValue(ARMConstantPoolValue *ACPV) { |
245 | 0 | const ARMConstantPoolSymbol *ACPS = dyn_cast<ARMConstantPoolSymbol>(ACPV); |
246 | 0 | return ACPS && ACPS->S == S && ARMConstantPoolValue::hasSameValue(ACPV); |
247 | 0 | } |
248 | | |
249 | 0 | void ARMConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) { |
250 | 0 | ID.AddString(S); |
251 | 0 | ARMConstantPoolValue::addSelectionDAGCSEId(ID); |
252 | 0 | } |
253 | | |
254 | 0 | void ARMConstantPoolSymbol::print(raw_ostream &O) const { |
255 | 0 | O << S; |
256 | 0 | ARMConstantPoolValue::print(O); |
257 | 0 | } |
258 | | |
259 | | //===----------------------------------------------------------------------===// |
260 | | // ARMConstantPoolMBB |
261 | | //===----------------------------------------------------------------------===// |
262 | | |
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), |
270 | 0 | MBB(mbb) {} |
271 | | |
272 | | ARMConstantPoolMBB *ARMConstantPoolMBB::Create(LLVMContext &C, |
273 | | const MachineBasicBlock *mbb, |
274 | | unsigned ID, |
275 | 0 | unsigned char PCAdj) { |
276 | 0 | return new ARMConstantPoolMBB(C, mbb, ID, PCAdj, ARMCP::no_modifier, false); |
277 | 0 | } |
278 | | |
279 | | int ARMConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP, |
280 | 0 | Align Alignment) { |
281 | 0 | return getExistingMachineCPValueImpl<ARMConstantPoolMBB>(CP, Alignment); |
282 | 0 | } |
283 | | |
284 | 0 | bool ARMConstantPoolMBB::hasSameValue(ARMConstantPoolValue *ACPV) { |
285 | 0 | const ARMConstantPoolMBB *ACPMBB = dyn_cast<ARMConstantPoolMBB>(ACPV); |
286 | 0 | return ACPMBB && ACPMBB->MBB == MBB && |
287 | 0 | ARMConstantPoolValue::hasSameValue(ACPV); |
288 | 0 | } |
289 | | |
290 | 0 | void ARMConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) { |
291 | 0 | ID.AddPointer(MBB); |
292 | 0 | ARMConstantPoolValue::addSelectionDAGCSEId(ID); |
293 | 0 | } |
294 | | |
295 | 0 | void ARMConstantPoolMBB::print(raw_ostream &O) const { |
296 | 0 | O << printMBBReference(*MBB); |
297 | 0 | ARMConstantPoolValue::print(O); |
298 | 0 | } |