1 //===-- llvm/CodeGen/PseudoSourceValue.cpp ----------------------*- C++ -*-===//
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 PseudoSourceValue class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/CodeGen/PseudoSourceValue.h"
14 #include "llvm/CodeGen/MachineFrameInfo.h"
15 #include "llvm/CodeGen/TargetInstrInfo.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
18 using namespace llvm;
19 
20 static const char *const PSVNames[] = {
21     "Stack", "GOT", "JumpTable", "ConstantPool", "FixedStack",
22     "GlobalValueCallEntry", "ExternalSymbolCallEntry"};
23 
24 PseudoSourceValue::PseudoSourceValue(unsigned Kind, const TargetInstrInfo &TII)
25     : Kind(Kind) {
26   AddressSpace = TII.getAddressSpaceForPseudoSourceKind(Kind);
27 }
28 
29 PseudoSourceValue::~PseudoSourceValue() = default;
30 
31 void PseudoSourceValue::printCustom(raw_ostream &O) const {
32   if (Kind < TargetCustom)
33     O << PSVNames[Kind];
34   else
35     O << "TargetCustom" << Kind;
36 }
37 
38 bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
39   if (isStack())
40     return false;
41   if (isGOT() || isConstantPool() || isJumpTable())
42     return true;
43   llvm_unreachable("Unknown PseudoSourceValue!");
44 }
45 
46 bool PseudoSourceValue::isAliased(const MachineFrameInfo *) const {
47   if (isStack() || isGOT() || isConstantPool() || isJumpTable())
48     return false;
49   llvm_unreachable("Unknown PseudoSourceValue!");
50 }
51 
52 bool PseudoSourceValue::mayAlias(const MachineFrameInfo *) const {
53   return !(isGOT() || isConstantPool() || isJumpTable());
54 }
55 
56 bool FixedStackPseudoSourceValue::isConstant(
57     const MachineFrameInfo *MFI) const {
58   return MFI && MFI->isImmutableObjectIndex(FI);
59 }
60 
61 bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
62   if (!MFI)
63     return true;
64   return MFI->isAliasedObjectIndex(FI);
65 }
66 
67 bool FixedStackPseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
68   if (!MFI)
69     return true;
70   // Spill slots will not alias any LLVM IR value.
71   return !MFI->isSpillSlotObjectIndex(FI);
72 }
73 
74 void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const {
75   OS << "FixedStack" << FI;
76 }
77 
78 CallEntryPseudoSourceValue::CallEntryPseudoSourceValue(
79     unsigned Kind, const TargetInstrInfo &TII)
80     : PseudoSourceValue(Kind, TII) {}
81 
82 bool CallEntryPseudoSourceValue::isConstant(const MachineFrameInfo *) const {
83   return false;
84 }
85 
86 bool CallEntryPseudoSourceValue::isAliased(const MachineFrameInfo *) const {
87   return false;
88 }
89 
90 bool CallEntryPseudoSourceValue::mayAlias(const MachineFrameInfo *) const {
91   return false;
92 }
93 
94 GlobalValuePseudoSourceValue::GlobalValuePseudoSourceValue(
95     const GlobalValue *GV,
96     const TargetInstrInfo &TII)
97     : CallEntryPseudoSourceValue(GlobalValueCallEntry, TII), GV(GV) {}
98 ExternalSymbolPseudoSourceValue::ExternalSymbolPseudoSourceValue(
99     const char *ES, const TargetInstrInfo &TII)
100     : CallEntryPseudoSourceValue(ExternalSymbolCallEntry, TII), ES(ES) {}
101 
102 PseudoSourceValueManager::PseudoSourceValueManager(
103     const TargetInstrInfo &TIInfo)
104     : TII(TIInfo),
105       StackPSV(PseudoSourceValue::Stack, TII),
106       GOTPSV(PseudoSourceValue::GOT, TII),
107       JumpTablePSV(PseudoSourceValue::JumpTable, TII),
108       ConstantPoolPSV(PseudoSourceValue::ConstantPool, TII) {}
109 
110 const PseudoSourceValue *PseudoSourceValueManager::getStack() {
111   return &StackPSV;
112 }
113 
114 const PseudoSourceValue *PseudoSourceValueManager::getGOT() { return &GOTPSV; }
115 
116 const PseudoSourceValue *PseudoSourceValueManager::getConstantPool() {
117   return &ConstantPoolPSV;
118 }
119 
120 const PseudoSourceValue *PseudoSourceValueManager::getJumpTable() {
121   return &JumpTablePSV;
122 }
123 
124 const PseudoSourceValue *
125 PseudoSourceValueManager::getFixedStack(int FI) {
126   std::unique_ptr<FixedStackPseudoSourceValue> &V = FSValues[FI];
127   if (!V)
128     V = std::make_unique<FixedStackPseudoSourceValue>(FI, TII);
129   return V.get();
130 }
131 
132 const PseudoSourceValue *
133 PseudoSourceValueManager::getGlobalValueCallEntry(const GlobalValue *GV) {
134   std::unique_ptr<const GlobalValuePseudoSourceValue> &E =
135       GlobalCallEntries[GV];
136   if (!E)
137     E = std::make_unique<GlobalValuePseudoSourceValue>(GV, TII);
138   return E.get();
139 }
140 
141 const PseudoSourceValue *
142 PseudoSourceValueManager::getExternalSymbolCallEntry(const char *ES) {
143   std::unique_ptr<const ExternalSymbolPseudoSourceValue> &E =
144       ExternalCallEntries[ES];
145   if (!E)
146     E = std::make_unique<ExternalSymbolPseudoSourceValue>(ES, TII);
147   return E.get();
148 }
149