xref: /llvm-project/llvm/lib/Target/RISCV/RISCVConstantPoolValue.cpp (revision 82d5dd28b4de7245088f7ed40da37f8cf80461e4)
1*fef84c56SJim Lin //===------- RISCVConstantPoolValue.cpp - RISC-V constantpool value -------===//
2*fef84c56SJim Lin //
3*fef84c56SJim Lin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fef84c56SJim Lin // See https://llvm.org/LICENSE.txt for license information.
5*fef84c56SJim Lin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fef84c56SJim Lin //
7*fef84c56SJim Lin //===----------------------------------------------------------------------===//
8*fef84c56SJim Lin //
9*fef84c56SJim Lin // This file implements the RISC-V specific constantpool value class.
10*fef84c56SJim Lin //
11*fef84c56SJim Lin //===----------------------------------------------------------------------===//
12*fef84c56SJim Lin 
13*fef84c56SJim Lin #include "RISCVConstantPoolValue.h"
14*fef84c56SJim Lin #include "llvm/ADT/FoldingSet.h"
15*fef84c56SJim Lin #include "llvm/IR/DerivedTypes.h"
16*fef84c56SJim Lin #include "llvm/IR/GlobalValue.h"
17*fef84c56SJim Lin #include "llvm/IR/Type.h"
18*fef84c56SJim Lin #include "llvm/Support/raw_ostream.h"
19*fef84c56SJim Lin 
20*fef84c56SJim Lin using namespace llvm;
21*fef84c56SJim Lin 
22*fef84c56SJim Lin RISCVConstantPoolValue::RISCVConstantPoolValue(Type *Ty, const GlobalValue *GV)
23*fef84c56SJim Lin     : MachineConstantPoolValue(Ty), GV(GV), Kind(RISCVCPKind::GlobalValue) {}
24*fef84c56SJim Lin 
25*fef84c56SJim Lin RISCVConstantPoolValue::RISCVConstantPoolValue(LLVMContext &C, StringRef S)
26*fef84c56SJim Lin     : MachineConstantPoolValue(Type::getInt64Ty(C)), S(S),
27*fef84c56SJim Lin       Kind(RISCVCPKind::ExtSymbol) {}
28*fef84c56SJim Lin 
29*fef84c56SJim Lin RISCVConstantPoolValue *RISCVConstantPoolValue::Create(const GlobalValue *GV) {
30*fef84c56SJim Lin   return new RISCVConstantPoolValue(GV->getType(), GV);
31*fef84c56SJim Lin }
32*fef84c56SJim Lin 
33*fef84c56SJim Lin RISCVConstantPoolValue *RISCVConstantPoolValue::Create(LLVMContext &C,
34*fef84c56SJim Lin                                                        StringRef S) {
35*fef84c56SJim Lin   return new RISCVConstantPoolValue(C, S);
36*fef84c56SJim Lin }
37*fef84c56SJim Lin 
38*fef84c56SJim Lin int RISCVConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
39*fef84c56SJim Lin                                                       Align Alignment) {
40*fef84c56SJim Lin   const std::vector<MachineConstantPoolEntry> &Constants = CP->getConstants();
41*fef84c56SJim Lin   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
42*fef84c56SJim Lin     if (Constants[i].isMachineConstantPoolEntry() &&
43*fef84c56SJim Lin         Constants[i].getAlign() >= Alignment) {
44*fef84c56SJim Lin       auto *CPV =
45*fef84c56SJim Lin           static_cast<RISCVConstantPoolValue *>(Constants[i].Val.MachineCPVal);
46*fef84c56SJim Lin       if (equals(CPV))
47*fef84c56SJim Lin         return i;
48*fef84c56SJim Lin     }
49*fef84c56SJim Lin   }
50*fef84c56SJim Lin 
51*fef84c56SJim Lin   return -1;
52*fef84c56SJim Lin }
53*fef84c56SJim Lin 
54*fef84c56SJim Lin void RISCVConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
55*fef84c56SJim Lin   if (isGlobalValue())
56*fef84c56SJim Lin     ID.AddPointer(GV);
57*fef84c56SJim Lin   else {
58*fef84c56SJim Lin     assert(isExtSymbol() && "unrecognized constant pool type");
59*fef84c56SJim Lin     ID.AddString(S);
60*fef84c56SJim Lin   }
61*fef84c56SJim Lin }
62*fef84c56SJim Lin 
63*fef84c56SJim Lin void RISCVConstantPoolValue::print(raw_ostream &O) const {
64*fef84c56SJim Lin   if (isGlobalValue())
65*fef84c56SJim Lin     O << GV->getName();
66*fef84c56SJim Lin   else {
67*fef84c56SJim Lin     assert(isExtSymbol() && "unrecognized constant pool type");
68*fef84c56SJim Lin     O << S;
69*fef84c56SJim Lin   }
70*fef84c56SJim Lin }
71*fef84c56SJim Lin 
72*fef84c56SJim Lin bool RISCVConstantPoolValue::equals(const RISCVConstantPoolValue *A) const {
73*fef84c56SJim Lin   if (isGlobalValue() && A->isGlobalValue())
74*fef84c56SJim Lin     return GV == A->GV;
75*fef84c56SJim Lin   if (isExtSymbol() && A->isExtSymbol())
76*fef84c56SJim Lin     return S == A->S;
77*fef84c56SJim Lin 
78*fef84c56SJim Lin   return false;
79*fef84c56SJim Lin }
80