xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Xtensa/XtensaConstantPoolValue.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
XtensaConstantPoolValue(Type * Ty,unsigned ID,XtensaCP::XtensaCPKind Kind,XtensaCP::XtensaCPModifier modifier)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 
XtensaConstantPoolValue(LLVMContext & C,unsigned ID,XtensaCP::XtensaCPKind Kind,XtensaCP::XtensaCPModifier Modifier)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 
~XtensaConstantPoolValue()36 XtensaConstantPoolValue::~XtensaConstantPoolValue() {}
37 
getModifierText() const38 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("Unknown modifier!");
46 }
47 
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)48 int XtensaConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
49                                                        Align Alignment) {
50   report_fatal_error("Shouldn't be calling this directly!");
51 }
52 
addSelectionDAGCSEId(FoldingSetNodeID & ID)53 void XtensaConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
54   ID.AddInteger(LabelId);
55 }
56 
hasSameValue(XtensaConstantPoolValue * ACPV)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)
dump() const66 void XtensaConstantPoolValue::dump() const { errs() << "  " << *this; }
67 #endif
68 
print(raw_ostream & O) const69 void XtensaConstantPoolValue::print(raw_ostream &O) const {}
70 
71 //===----------------------------------------------------------------------===//
72 // XtensaConstantPoolConstant
73 //===----------------------------------------------------------------------===//
74 
XtensaConstantPoolConstant(const Constant * C,unsigned ID,XtensaCP::XtensaCPKind Kind)75 XtensaConstantPoolConstant::XtensaConstantPoolConstant(
76     const Constant *C, unsigned ID, XtensaCP::XtensaCPKind Kind)
77     : XtensaConstantPoolValue(C->getType(), ID, Kind), CVal(C) {}
78 
79 XtensaConstantPoolConstant *
Create(const Constant * C,unsigned ID,XtensaCP::XtensaCPKind Kind)80 XtensaConstantPoolConstant::Create(const Constant *C, unsigned ID,
81                                    XtensaCP::XtensaCPKind Kind) {
82   return new XtensaConstantPoolConstant(C, ID, Kind);
83 }
84 
getBlockAddress() const85 const BlockAddress *XtensaConstantPoolConstant::getBlockAddress() const {
86   return dyn_cast_or_null<BlockAddress>(CVal);
87 }
88 
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)89 int XtensaConstantPoolConstant::getExistingMachineCPValue(
90     MachineConstantPool *CP, Align Alignment) {
91   return getExistingMachineCPValueImpl<XtensaConstantPoolConstant>(CP,
92                                                                    Alignment);
93 }
94 
hasSameValue(XtensaConstantPoolValue * ACPV)95 bool XtensaConstantPoolConstant::hasSameValue(XtensaConstantPoolValue *ACPV) {
96   const XtensaConstantPoolConstant *ACPC =
97       dyn_cast<XtensaConstantPoolConstant>(ACPV);
98   return ACPC && ACPC->CVal == CVal &&
99          XtensaConstantPoolValue::hasSameValue(ACPV);
100 }
101 
addSelectionDAGCSEId(FoldingSetNodeID & ID)102 void XtensaConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
103   ID.AddPointer(CVal);
104   XtensaConstantPoolValue::addSelectionDAGCSEId(ID);
105 }
106 
print(raw_ostream & O) const107 void XtensaConstantPoolConstant::print(raw_ostream &O) const {
108   O << CVal->getName();
109   XtensaConstantPoolValue::print(O);
110 }
111 
XtensaConstantPoolSymbol(LLVMContext & C,const char * Str,unsigned ID,bool PrivLinkage,XtensaCP::XtensaCPModifier Modifier)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 *
Create(LLVMContext & C,const char * Str,unsigned ID,bool PrivLinkage,XtensaCP::XtensaCPModifier Modifier)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 
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)127 int XtensaConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
128                                                         Align Alignment) {
129   return getExistingMachineCPValueImpl<XtensaConstantPoolSymbol>(CP, Alignment);
130 }
131 
hasSameValue(XtensaConstantPoolValue * ACPV)132 bool XtensaConstantPoolSymbol::hasSameValue(XtensaConstantPoolValue *ACPV) {
133   const XtensaConstantPoolSymbol *ACPS =
134       dyn_cast<XtensaConstantPoolSymbol>(ACPV);
135   return ACPS && ACPS->S == S && XtensaConstantPoolValue::hasSameValue(ACPV);
136 }
137 
addSelectionDAGCSEId(FoldingSetNodeID & ID)138 void XtensaConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
139   ID.AddString(S);
140   XtensaConstantPoolValue::addSelectionDAGCSEId(ID);
141 }
142 
print(raw_ostream & O) const143 void XtensaConstantPoolSymbol::print(raw_ostream &O) const {
144   O << S;
145   XtensaConstantPoolValue::print(O);
146 }
147 
XtensaConstantPoolMBB(LLVMContext & C,const MachineBasicBlock * M,unsigned Id)148 XtensaConstantPoolMBB::XtensaConstantPoolMBB(LLVMContext &C,
149                                              const MachineBasicBlock *M,
150                                              unsigned Id)
151     : XtensaConstantPoolValue(C, 0, XtensaCP::CPMachineBasicBlock), MBB(M) {}
152 
Create(LLVMContext & C,const MachineBasicBlock * M,unsigned Idx)153 XtensaConstantPoolMBB *XtensaConstantPoolMBB::Create(LLVMContext &C,
154                                                      const MachineBasicBlock *M,
155                                                      unsigned Idx) {
156   return new XtensaConstantPoolMBB(C, M, Idx);
157 }
158 
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)159 int XtensaConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
160                                                      Align Alignment) {
161   return getExistingMachineCPValueImpl<XtensaConstantPoolMBB>(CP, Alignment);
162 }
163 
hasSameValue(XtensaConstantPoolValue * ACPV)164 bool XtensaConstantPoolMBB::hasSameValue(XtensaConstantPoolValue *ACPV) {
165   const XtensaConstantPoolMBB *ACPMBB = dyn_cast<XtensaConstantPoolMBB>(ACPV);
166   return ACPMBB && ACPMBB->MBB == MBB &&
167          XtensaConstantPoolValue::hasSameValue(ACPV);
168 }
169 
addSelectionDAGCSEId(FoldingSetNodeID & ID)170 void XtensaConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
171   ID.AddPointer(MBB);
172   XtensaConstantPoolValue::addSelectionDAGCSEId(ID);
173 }
174 
print(raw_ostream & O) const175 void XtensaConstantPoolMBB::print(raw_ostream &O) const {
176   O << "BB#" << MBB->getNumber();
177   XtensaConstantPoolValue::print(O);
178 }
179 
XtensaConstantPoolJumpTable(LLVMContext & C,unsigned Index)180 XtensaConstantPoolJumpTable::XtensaConstantPoolJumpTable(LLVMContext &C,
181                                                          unsigned Index)
182     : XtensaConstantPoolValue(C, 0, XtensaCP::CPJumpTable), Idx(Index) {}
183 
Create(LLVMContext & C,unsigned Idx)184 XtensaConstantPoolJumpTable *XtensaConstantPoolJumpTable::Create(LLVMContext &C,
185                                                                  unsigned Idx) {
186   return new XtensaConstantPoolJumpTable(C, Idx);
187 }
188 
getExistingMachineCPValue(MachineConstantPool * CP,Align Alignment)189 int XtensaConstantPoolJumpTable::getExistingMachineCPValue(
190     MachineConstantPool *CP, Align Alignment) {
191   return getExistingMachineCPValueImpl<XtensaConstantPoolJumpTable>(CP,
192                                                                     Alignment);
193 }
194 
hasSameValue(XtensaConstantPoolValue * ACPV)195 bool XtensaConstantPoolJumpTable::hasSameValue(XtensaConstantPoolValue *ACPV) {
196   const XtensaConstantPoolJumpTable *ACPJT =
197       dyn_cast<XtensaConstantPoolJumpTable>(ACPV);
198   return ACPJT && ACPJT->Idx == Idx &&
199          XtensaConstantPoolValue::hasSameValue(ACPV);
200 }
201 
addSelectionDAGCSEId(FoldingSetNodeID & ID)202 void XtensaConstantPoolJumpTable::addSelectionDAGCSEId(FoldingSetNodeID &ID) {}
203 
print(raw_ostream & O) const204 void XtensaConstantPoolJumpTable::print(raw_ostream &O) const {
205   O << "JT" << Idx;
206   XtensaConstantPoolValue::print(O);
207 }
208