1 //===-- CSKYConstantPoolValue.cpp - CSKY 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 CSKY specific constantpool value class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "CSKYConstantPoolValue.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 using namespace llvm;
22
23 //===----------------------------------------------------------------------===//
24 // CSKYConstantPoolValue
25 //===----------------------------------------------------------------------===//
26
CSKYConstantPoolValue(Type * Ty,CSKYCP::CSKYCPKind Kind,unsigned PCAdjust,CSKYCP::CSKYCPModifier Modifier,bool AddCurrentAddress,unsigned ID)27 CSKYConstantPoolValue::CSKYConstantPoolValue(Type *Ty, CSKYCP::CSKYCPKind Kind,
28 unsigned PCAdjust,
29 CSKYCP::CSKYCPModifier Modifier,
30 bool AddCurrentAddress,
31 unsigned ID)
32 : MachineConstantPoolValue(Ty), Kind(Kind), PCAdjust(PCAdjust),
33 Modifier(Modifier), AddCurrentAddress(AddCurrentAddress), LabelId(ID) {}
34
getModifierText() const35 const char *CSKYConstantPoolValue::getModifierText() const {
36 switch (Modifier) {
37 case CSKYCP::ADDR:
38 return "ADDR";
39 case CSKYCP::GOT:
40 return "GOT";
41 case CSKYCP::GOTOFF:
42 return "GOTOFF";
43 case CSKYCP::PLT:
44 return "PLT";
45 case CSKYCP::TLSIE:
46 return "TLSIE";
47 case CSKYCP::TLSLE:
48 return "TLSLE";
49 case CSKYCP::TLSGD:
50 return "TLSGD";
51 case CSKYCP::NO_MOD:
52 return "";
53 }
54 llvm_unreachable("Unknown modifier!");
55 }
56
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)57 int CSKYConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
58 Align Alignment) {
59 llvm_unreachable("Shouldn't be calling this directly!");
60 }
61
addSelectionDAGCSEId(FoldingSetNodeID & ID)62 void CSKYConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
63 ID.AddInteger(LabelId);
64 ID.AddInteger(PCAdjust);
65 ID.AddInteger(Modifier);
66 }
67
print(raw_ostream & O) const68 void CSKYConstantPoolValue::print(raw_ostream &O) const {
69 if (Modifier)
70 O << "(" << getModifierText() << ")";
71 if (PCAdjust)
72 O << " + " << PCAdjust;
73 }
74
75 //===----------------------------------------------------------------------===//
76 // CSKYConstantPoolConstant
77 //===----------------------------------------------------------------------===//
78
CSKYConstantPoolConstant(const Constant * C,Type * Ty,CSKYCP::CSKYCPKind Kind,unsigned PCAdjust,CSKYCP::CSKYCPModifier Modifier,bool AddCurrentAddress,unsigned ID)79 CSKYConstantPoolConstant::CSKYConstantPoolConstant(
80 const Constant *C, Type *Ty, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
81 CSKYCP::CSKYCPModifier Modifier, bool AddCurrentAddress, unsigned ID)
82 : CSKYConstantPoolValue(Ty, Kind, PCAdjust, Modifier, AddCurrentAddress,
83 ID),
84 CVal(C) {}
85
Create(const Constant * C,CSKYCP::CSKYCPKind Kind,unsigned PCAdjust,CSKYCP::CSKYCPModifier Modifier,bool AddCurrentAddress,unsigned ID)86 CSKYConstantPoolConstant *CSKYConstantPoolConstant::Create(
87 const Constant *C, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
88 CSKYCP::CSKYCPModifier Modifier, bool AddCurrentAddress, unsigned ID) {
89 return new CSKYConstantPoolConstant(C, C->getType(), Kind, PCAdjust, Modifier,
90 AddCurrentAddress, ID);
91 }
92
Create(const Constant * C,Type * Ty,CSKYCP::CSKYCPKind Kind,unsigned PCAdjust,CSKYCP::CSKYCPModifier Modifier,bool AddCurrentAddress,unsigned ID)93 CSKYConstantPoolConstant *CSKYConstantPoolConstant::Create(
94 const Constant *C, Type *Ty, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
95 CSKYCP::CSKYCPModifier Modifier, bool AddCurrentAddress, unsigned ID) {
96 return new CSKYConstantPoolConstant(C, Ty, Kind, PCAdjust, Modifier,
97 AddCurrentAddress, ID);
98 }
99
getGV() const100 const GlobalValue *CSKYConstantPoolConstant::getGV() const {
101 assert(isa<GlobalValue>(CVal) && "CVal should be GlobalValue");
102 return cast<GlobalValue>(CVal);
103 }
104
getBlockAddress() const105 const BlockAddress *CSKYConstantPoolConstant::getBlockAddress() const {
106 assert(isa<BlockAddress>(CVal) && "CVal should be BlockAddress");
107 return cast<BlockAddress>(CVal);
108 }
109
getConstantPool() const110 const Constant *CSKYConstantPoolConstant::getConstantPool() const {
111 return CVal;
112 }
113
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)114 int CSKYConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
115 Align Alignment) {
116 return getExistingMachineCPValueImpl<CSKYConstantPoolConstant>(CP, Alignment);
117 }
118
addSelectionDAGCSEId(FoldingSetNodeID & ID)119 void CSKYConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
120 ID.AddPointer(CVal);
121
122 CSKYConstantPoolValue::addSelectionDAGCSEId(ID);
123 }
124
print(raw_ostream & O) const125 void CSKYConstantPoolConstant::print(raw_ostream &O) const {
126 O << CVal->getName();
127 CSKYConstantPoolValue::print(O);
128 }
129
130 //===----------------------------------------------------------------------===//
131 // CSKYConstantPoolSymbol
132 //===----------------------------------------------------------------------===//
133
CSKYConstantPoolSymbol(Type * Ty,const char * S,unsigned PCAdjust,CSKYCP::CSKYCPModifier Modifier,bool AddCurrentAddress)134 CSKYConstantPoolSymbol::CSKYConstantPoolSymbol(Type *Ty, const char *S,
135 unsigned PCAdjust,
136 CSKYCP::CSKYCPModifier Modifier,
137 bool AddCurrentAddress)
138 : CSKYConstantPoolValue(Ty, CSKYCP::CPExtSymbol, PCAdjust, Modifier,
139 AddCurrentAddress),
140 S(strdup(S)) {}
141
142 CSKYConstantPoolSymbol *
Create(Type * Ty,const char * S,unsigned PCAdjust,CSKYCP::CSKYCPModifier Modifier)143 CSKYConstantPoolSymbol::Create(Type *Ty, const char *S, unsigned PCAdjust,
144 CSKYCP::CSKYCPModifier Modifier) {
145 return new CSKYConstantPoolSymbol(Ty, S, PCAdjust, Modifier, false);
146 }
147
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)148 int CSKYConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
149 Align Alignment) {
150
151 return getExistingMachineCPValueImpl<CSKYConstantPoolSymbol>(CP, Alignment);
152 }
153
addSelectionDAGCSEId(FoldingSetNodeID & ID)154 void CSKYConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
155 ID.AddString(S);
156 CSKYConstantPoolValue::addSelectionDAGCSEId(ID);
157 }
158
print(raw_ostream & O) const159 void CSKYConstantPoolSymbol::print(raw_ostream &O) const {
160 O << S;
161 CSKYConstantPoolValue::print(O);
162 }
163
164 //===----------------------------------------------------------------------===//
165 // CSKYConstantPoolMBB
166 //===----------------------------------------------------------------------===//
167
CSKYConstantPoolMBB(Type * Ty,const MachineBasicBlock * Mbb,unsigned PCAdjust,CSKYCP::CSKYCPModifier Modifier,bool AddCurrentAddress)168 CSKYConstantPoolMBB::CSKYConstantPoolMBB(Type *Ty, const MachineBasicBlock *Mbb,
169 unsigned PCAdjust,
170 CSKYCP::CSKYCPModifier Modifier,
171 bool AddCurrentAddress)
172 : CSKYConstantPoolValue(Ty, CSKYCP::CPMachineBasicBlock, PCAdjust, Modifier,
173 AddCurrentAddress),
174 MBB(Mbb) {}
175
Create(Type * Ty,const MachineBasicBlock * Mbb,unsigned PCAdjust)176 CSKYConstantPoolMBB *CSKYConstantPoolMBB::Create(Type *Ty,
177 const MachineBasicBlock *Mbb,
178 unsigned PCAdjust) {
179 return new CSKYConstantPoolMBB(Ty, Mbb, PCAdjust, CSKYCP::ADDR, false);
180 }
181
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)182 int CSKYConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
183 Align Alignment) {
184 return getExistingMachineCPValueImpl<CSKYConstantPoolMBB>(CP, Alignment);
185 }
186
addSelectionDAGCSEId(FoldingSetNodeID & ID)187 void CSKYConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
188 ID.AddPointer(MBB);
189 CSKYConstantPoolValue::addSelectionDAGCSEId(ID);
190 }
191
print(raw_ostream & O) const192 void CSKYConstantPoolMBB::print(raw_ostream &O) const {
193 O << "BB#" << MBB->getNumber();
194 CSKYConstantPoolValue::print(O);
195 }
196
197 //===----------------------------------------------------------------------===//
198 // CSKYConstantPoolJT
199 //===----------------------------------------------------------------------===//
200
CSKYConstantPoolJT(Type * Ty,int JTIndex,unsigned PCAdj,CSKYCP::CSKYCPModifier Modifier,bool AddCurrentAddress)201 CSKYConstantPoolJT::CSKYConstantPoolJT(Type *Ty, int JTIndex, unsigned PCAdj,
202 CSKYCP::CSKYCPModifier Modifier,
203 bool AddCurrentAddress)
204 : CSKYConstantPoolValue(Ty, CSKYCP::CPJT, PCAdj, Modifier,
205 AddCurrentAddress),
206 JTI(JTIndex) {}
207
208 CSKYConstantPoolJT *
Create(Type * Ty,int JTI,unsigned PCAdj,CSKYCP::CSKYCPModifier Modifier)209 CSKYConstantPoolJT::Create(Type *Ty, int JTI, unsigned PCAdj,
210 CSKYCP::CSKYCPModifier Modifier) {
211 return new CSKYConstantPoolJT(Ty, JTI, PCAdj, Modifier, false);
212 }
213
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)214 int CSKYConstantPoolJT::getExistingMachineCPValue(MachineConstantPool *CP,
215 Align Alignment) {
216 return getExistingMachineCPValueImpl<CSKYConstantPoolJT>(CP, Alignment);
217 }
218
addSelectionDAGCSEId(FoldingSetNodeID & ID)219 void CSKYConstantPoolJT::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
220 ID.AddInteger(JTI);
221 CSKYConstantPoolValue::addSelectionDAGCSEId(ID);
222 }
223
print(raw_ostream & O) const224 void CSKYConstantPoolJT::print(raw_ostream &O) const {
225 O << "JTI#" << JTI;
226 CSKYConstantPoolValue::print(O);
227 }
228