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
20using namespace llvm;
21
22RISCVConstantPoolValue::RISCVConstantPoolValue(Type *Ty, const GlobalValue *GV)
23 : MachineConstantPoolValue(Ty), GV(GV), Kind(RISCVCPKind::GlobalValue) {}
24
25RISCVConstantPoolValue::RISCVConstantPoolValue(LLVMContext &C, StringRef S)
26 : MachineConstantPoolValue(Type::getInt64Ty(C)), S(S),
27 Kind(RISCVCPKind::ExtSymbol) {}
28
29RISCVConstantPoolValue *RISCVConstantPoolValue::Create(const GlobalValue *GV) {
30 return new RISCVConstantPoolValue(GV->getType(), GV);
31}
32
33RISCVConstantPoolValue *RISCVConstantPoolValue::Create(LLVMContext &C,
34 StringRef S) {
35 return new RISCVConstantPoolValue(C, S);
36}
37
38int 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(A: CPV))
47 return i;
48 }
49 }
50
51 return -1;
52}
53
54void RISCVConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
55 if (isGlobalValue())
56 ID.AddPointer(Ptr: GV);
57 else {
58 assert(isExtSymbol() && "unrecognized constant pool type");
59 ID.AddString(String: S);
60 }
61}
62
63void 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
72bool 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