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