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