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