1 | //===- XtensaConstantPoolValue.cpp - Xtensa 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 Xtensa specific constantpool value class. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "XtensaConstantPoolValue.h" |
14 | #include "llvm/ADT/FoldingSet.h" |
15 | #include "llvm/CodeGen/MachineBasicBlock.h" |
16 | #include "llvm/IR/Constant.h" |
17 | #include "llvm/IR/Constants.h" |
18 | #include "llvm/IR/GlobalValue.h" |
19 | #include "llvm/IR/Type.h" |
20 | #include "llvm/Support/raw_ostream.h" |
21 | #include <cstdlib> |
22 | using namespace llvm; |
23 | |
24 | XtensaConstantPoolValue::XtensaConstantPoolValue( |
25 | Type *Ty, unsigned ID, XtensaCP::XtensaCPKind Kind, |
26 | XtensaCP::XtensaCPModifier modifier) |
27 | : MachineConstantPoolValue(Ty), LabelId(ID), Kind(Kind), |
28 | Modifier(modifier) {} |
29 | |
30 | XtensaConstantPoolValue::XtensaConstantPoolValue( |
31 | LLVMContext &C, unsigned ID, XtensaCP::XtensaCPKind Kind, |
32 | XtensaCP::XtensaCPModifier Modifier) |
33 | : MachineConstantPoolValue((Type *)Type::getInt32Ty(C)), LabelId(ID), |
34 | Kind(Kind), Modifier(Modifier) {} |
35 | |
36 | XtensaConstantPoolValue::~XtensaConstantPoolValue() {} |
37 | |
38 | StringRef XtensaConstantPoolValue::getModifierText() const { |
39 | switch (Modifier) { |
40 | case XtensaCP::no_modifier: |
41 | return "" ; |
42 | case XtensaCP::TPOFF: |
43 | return "@TPOFF" ; |
44 | } |
45 | report_fatal_error(reason: "Unknown modifier!" ); |
46 | } |
47 | |
48 | int XtensaConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP, |
49 | Align Alignment) { |
50 | report_fatal_error(reason: "Shouldn't be calling this directly!" ); |
51 | } |
52 | |
53 | void XtensaConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) { |
54 | ID.AddInteger(I: LabelId); |
55 | } |
56 | |
57 | bool XtensaConstantPoolValue::hasSameValue(XtensaConstantPoolValue *ACPV) { |
58 | if (ACPV->Kind == Kind) { |
59 | if (ACPV->LabelId == LabelId) |
60 | return true; |
61 | } |
62 | return false; |
63 | } |
64 | |
65 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
66 | void XtensaConstantPoolValue::dump() const { errs() << " " << *this; } |
67 | #endif |
68 | |
69 | void XtensaConstantPoolValue::print(raw_ostream &O) const {} |
70 | |
71 | //===----------------------------------------------------------------------===// |
72 | // XtensaConstantPoolConstant |
73 | //===----------------------------------------------------------------------===// |
74 | |
75 | XtensaConstantPoolConstant::XtensaConstantPoolConstant( |
76 | const Constant *C, unsigned ID, XtensaCP::XtensaCPKind Kind) |
77 | : XtensaConstantPoolValue(C->getType(), ID, Kind), CVal(C) {} |
78 | |
79 | XtensaConstantPoolConstant * |
80 | XtensaConstantPoolConstant::Create(const Constant *C, unsigned ID, |
81 | XtensaCP::XtensaCPKind Kind) { |
82 | return new XtensaConstantPoolConstant(C, ID, Kind); |
83 | } |
84 | |
85 | const BlockAddress *XtensaConstantPoolConstant::getBlockAddress() const { |
86 | return dyn_cast_or_null<BlockAddress>(Val: CVal); |
87 | } |
88 | |
89 | int XtensaConstantPoolConstant::getExistingMachineCPValue( |
90 | MachineConstantPool *CP, Align Alignment) { |
91 | return getExistingMachineCPValueImpl<XtensaConstantPoolConstant>(CP, |
92 | Alignment); |
93 | } |
94 | |
95 | bool XtensaConstantPoolConstant::hasSameValue(XtensaConstantPoolValue *ACPV) { |
96 | const XtensaConstantPoolConstant *ACPC = |
97 | dyn_cast<XtensaConstantPoolConstant>(Val: ACPV); |
98 | return ACPC && ACPC->CVal == CVal && |
99 | XtensaConstantPoolValue::hasSameValue(ACPV); |
100 | } |
101 | |
102 | void XtensaConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) { |
103 | ID.AddPointer(Ptr: CVal); |
104 | XtensaConstantPoolValue::addSelectionDAGCSEId(ID); |
105 | } |
106 | |
107 | void XtensaConstantPoolConstant::print(raw_ostream &O) const { |
108 | O << CVal->getName(); |
109 | XtensaConstantPoolValue::print(O); |
110 | } |
111 | |
112 | XtensaConstantPoolSymbol::XtensaConstantPoolSymbol( |
113 | LLVMContext &C, const char *Str, unsigned ID, bool PrivLinkage, |
114 | XtensaCP::XtensaCPModifier Modifier) |
115 | : XtensaConstantPoolValue(C, ID, XtensaCP::CPExtSymbol, Modifier), S(Str), |
116 | PrivateLinkage(PrivLinkage) {} |
117 | |
118 | XtensaConstantPoolSymbol * |
119 | XtensaConstantPoolSymbol::Create(LLVMContext &C, const char *Str, unsigned ID, |
120 | bool PrivLinkage, |
121 | XtensaCP::XtensaCPModifier Modifier) |
122 | |
123 | { |
124 | return new XtensaConstantPoolSymbol(C, Str, ID, PrivLinkage, Modifier); |
125 | } |
126 | |
127 | int XtensaConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP, |
128 | Align Alignment) { |
129 | return getExistingMachineCPValueImpl<XtensaConstantPoolSymbol>(CP, Alignment); |
130 | } |
131 | |
132 | bool XtensaConstantPoolSymbol::hasSameValue(XtensaConstantPoolValue *ACPV) { |
133 | const XtensaConstantPoolSymbol *ACPS = |
134 | dyn_cast<XtensaConstantPoolSymbol>(Val: ACPV); |
135 | return ACPS && ACPS->S == S && XtensaConstantPoolValue::hasSameValue(ACPV); |
136 | } |
137 | |
138 | void XtensaConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) { |
139 | ID.AddString(String: S); |
140 | XtensaConstantPoolValue::addSelectionDAGCSEId(ID); |
141 | } |
142 | |
143 | void XtensaConstantPoolSymbol::print(raw_ostream &O) const { |
144 | O << S; |
145 | XtensaConstantPoolValue::print(O); |
146 | } |
147 | |
148 | XtensaConstantPoolMBB::XtensaConstantPoolMBB(LLVMContext &C, |
149 | const MachineBasicBlock *M, |
150 | unsigned Id) |
151 | : XtensaConstantPoolValue(C, 0, XtensaCP::CPMachineBasicBlock), MBB(M) {} |
152 | |
153 | XtensaConstantPoolMBB *XtensaConstantPoolMBB::Create(LLVMContext &C, |
154 | const MachineBasicBlock *M, |
155 | unsigned Idx) { |
156 | return new XtensaConstantPoolMBB(C, M, Idx); |
157 | } |
158 | |
159 | int XtensaConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP, |
160 | Align Alignment) { |
161 | return getExistingMachineCPValueImpl<XtensaConstantPoolMBB>(CP, Alignment); |
162 | } |
163 | |
164 | bool XtensaConstantPoolMBB::hasSameValue(XtensaConstantPoolValue *ACPV) { |
165 | const XtensaConstantPoolMBB *ACPMBB = dyn_cast<XtensaConstantPoolMBB>(Val: ACPV); |
166 | return ACPMBB && ACPMBB->MBB == MBB && |
167 | XtensaConstantPoolValue::hasSameValue(ACPV); |
168 | } |
169 | |
170 | void XtensaConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) { |
171 | ID.AddPointer(Ptr: MBB); |
172 | XtensaConstantPoolValue::addSelectionDAGCSEId(ID); |
173 | } |
174 | |
175 | void XtensaConstantPoolMBB::print(raw_ostream &O) const { |
176 | O << "BB#" << MBB->getNumber(); |
177 | XtensaConstantPoolValue::print(O); |
178 | } |
179 | |
180 | XtensaConstantPoolJumpTable::XtensaConstantPoolJumpTable(LLVMContext &C, |
181 | unsigned Index) |
182 | : XtensaConstantPoolValue(C, 0, XtensaCP::CPJumpTable), Idx(Index) {} |
183 | |
184 | XtensaConstantPoolJumpTable *XtensaConstantPoolJumpTable::Create(LLVMContext &C, |
185 | unsigned Idx) { |
186 | return new XtensaConstantPoolJumpTable(C, Idx); |
187 | } |
188 | |
189 | int XtensaConstantPoolJumpTable::getExistingMachineCPValue( |
190 | MachineConstantPool *CP, Align Alignment) { |
191 | return getExistingMachineCPValueImpl<XtensaConstantPoolJumpTable>(CP, |
192 | Alignment); |
193 | } |
194 | |
195 | bool XtensaConstantPoolJumpTable::hasSameValue(XtensaConstantPoolValue *ACPV) { |
196 | const XtensaConstantPoolJumpTable *ACPJT = |
197 | dyn_cast<XtensaConstantPoolJumpTable>(Val: ACPV); |
198 | return ACPJT && ACPJT->Idx == Idx && |
199 | XtensaConstantPoolValue::hasSameValue(ACPV); |
200 | } |
201 | |
202 | void XtensaConstantPoolJumpTable::addSelectionDAGCSEId(FoldingSetNodeID &ID) {} |
203 | |
204 | void XtensaConstantPoolJumpTable::print(raw_ostream &O) const { |
205 | O << "JT" << Idx; |
206 | XtensaConstantPoolValue::print(O); |
207 | } |
208 | |