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