|
23 | 23 | #include "swift/SIL/DebugUtils.h"
|
24 | 24 | #include "swift/SIL/FieldSensitivePrunedLiveness.h"
|
25 | 25 | #include "swift/SIL/SILArgument.h"
|
| 26 | +#include "swift/SILOptimizer/Utils/VariableNameUtils.h" |
26 | 27 | #include "llvm/Support/Debug.h"
|
27 | 28 |
|
28 | 29 | #include "MoveOnlyTypeUtils.h"
|
@@ -73,181 +74,12 @@ static void diagnose(ASTContext &context, SourceLoc loc, Diag<T...> diag,
|
73 | 74 | context.Diags.diagnose(loc, diag, std::forward<U>(args)...);
|
74 | 75 | }
|
75 | 76 |
|
76 |
| -/// Helper function that actually implements getVariableNameForValue. Do not |
77 |
| -/// call it directly! Call the unary variants instead. |
78 |
| -static void getVariableNameForValue(SILValue value2, |
79 |
| - SILValue searchValue, |
80 |
| - SmallString<64> &resultingString) { |
81 |
| - // Before we do anything, lets see if we have an exact debug_value on our |
82 |
| - // mmci. In such a case, we can end early and are done. |
83 |
| - if (auto *use = getAnyDebugUse(value2)) { |
84 |
| - if (auto debugVar = DebugVarCarryingInst(use->getUser())) { |
85 |
| - assert(debugVar.getKind() == DebugVarCarryingInst::Kind::DebugValue); |
86 |
| - resultingString += debugVar.getName(); |
87 |
| - return; |
88 |
| - } |
89 |
| - } |
90 |
| - |
91 |
| - // Otherwise, we need to look at our mark_unresolved_non_copyable_value's |
92 |
| - // operand. |
93 |
| - StackList<llvm::PointerUnion<SILInstruction *, SILValue>> variableNamePath( |
94 |
| - value2->getFunction()); |
95 |
| - while (searchValue) { |
96 |
| - if (auto *allocInst = dyn_cast<AllocationInst>(searchValue)) { |
97 |
| - // If the instruction itself doesn't carry any variable info, see whether |
98 |
| - // it's copied from another place that does. |
99 |
| - if (!allocInst->getDecl()) { |
100 |
| - if (auto copy = allocInst->getSingleUserOfType<CopyAddrInst>()) { |
101 |
| - if (copy->getDest() == allocInst |
102 |
| - && !copy->isTakeOfSrc() |
103 |
| - && copy->isInitializationOfDest()) { |
104 |
| - searchValue = copy->getSrc(); |
105 |
| - continue; |
106 |
| - } |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - variableNamePath.push_back(allocInst); |
111 |
| - break; |
112 |
| - } |
113 |
| - |
114 |
| - if (auto *globalAddrInst = dyn_cast<GlobalAddrInst>(searchValue)) { |
115 |
| - variableNamePath.push_back(globalAddrInst); |
116 |
| - break; |
117 |
| - } |
118 |
| - |
119 |
| - if (auto *oeInst = dyn_cast<OpenExistentialAddrInst>(searchValue)) { |
120 |
| - searchValue = oeInst->getOperand(); |
121 |
| - continue; |
122 |
| - } |
123 |
| - |
124 |
| - if (auto *rei = dyn_cast<RefElementAddrInst>(searchValue)) { |
125 |
| - variableNamePath.push_back(rei); |
126 |
| - searchValue = rei->getOperand(); |
127 |
| - continue; |
128 |
| - } |
129 |
| - |
130 |
| - if (auto *fArg = dyn_cast<SILFunctionArgument>(searchValue)) { |
131 |
| - variableNamePath.push_back({fArg}); |
132 |
| - break; |
133 |
| - } |
134 |
| - |
135 |
| - auto getNamePathComponentFromCallee = [&](FullApplySite call) -> llvm::Optional<SILValue> { |
136 |
| - // Use the name of the property being accessed if we can get to it. |
137 |
| - if (isa<FunctionRefBaseInst>(call.getCallee()) |
138 |
| - || isa<MethodInst>(call.getCallee())) { |
139 |
| - variableNamePath.push_back(call.getCallee()->getDefiningInstruction()); |
140 |
| - // Try to name the base of the property if this is a method. |
141 |
| - if (call.getSubstCalleeType()->hasSelfParam()) { |
142 |
| - return call.getSelfArgument(); |
143 |
| - } else { |
144 |
| - return SILValue(); |
145 |
| - } |
146 |
| - } |
147 |
| - return llvm::None; |
148 |
| - }; |
149 |
| - |
150 |
| - // Read or modify accessor. |
151 |
| - if (auto bai = dyn_cast_or_null<BeginApplyInst>(searchValue->getDefiningInstruction())) { |
152 |
| - if (auto selfParam = getNamePathComponentFromCallee(bai)) { |
153 |
| - searchValue = *selfParam; |
154 |
| - continue; |
155 |
| - } |
156 |
| - } |
157 |
| - |
158 |
| - // Addressor accessor. |
159 |
| - if (auto ptrToAddr = dyn_cast<PointerToAddressInst>(stripAccessMarkers(searchValue))) { |
160 |
| - // The addressor can either produce the raw pointer itself or an `UnsafePointer` stdlib type wrapping it. |
161 |
| - ApplyInst *addressorInvocation; |
162 |
| - if (auto structExtract = dyn_cast<StructExtractInst>(ptrToAddr->getOperand())) { |
163 |
| - addressorInvocation = dyn_cast<ApplyInst>(structExtract->getOperand()); |
164 |
| - } else { |
165 |
| - addressorInvocation = dyn_cast<ApplyInst>(ptrToAddr->getOperand()); |
166 |
| - } |
167 |
| - |
168 |
| - if (addressorInvocation) { |
169 |
| - if (auto selfParam = getNamePathComponentFromCallee(addressorInvocation)) { |
170 |
| - searchValue = *selfParam; |
171 |
| - continue; |
172 |
| - } |
173 |
| - } |
174 |
| - } |
175 |
| - |
176 |
| - // If we do not do an exact match, see if we can find a debug_var inst. If |
177 |
| - // we do, we always break since we have a root value. |
178 |
| - if (auto *use = getAnyDebugUse(searchValue)) { |
179 |
| - if (auto debugVar = DebugVarCarryingInst(use->getUser())) { |
180 |
| - assert(debugVar.getKind() == DebugVarCarryingInst::Kind::DebugValue); |
181 |
| - variableNamePath.push_back(use->getUser()); |
182 |
| - break; |
183 |
| - } |
184 |
| - } |
185 |
| - |
186 |
| - // Otherwise, try to see if we have a single value instruction we can look |
187 |
| - // through. |
188 |
| - if (isa<BeginBorrowInst>(searchValue) || isa<LoadInst>(searchValue) || |
189 |
| - isa<LoadBorrowInst>(searchValue) || isa<BeginAccessInst>(searchValue) || |
190 |
| - isa<MarkUnresolvedNonCopyableValueInst>(searchValue) || |
191 |
| - isa<ProjectBoxInst>(searchValue) || isa<CopyValueInst>(searchValue)) { |
192 |
| - searchValue = cast<SingleValueInstruction>(searchValue)->getOperand(0); |
193 |
| - continue; |
194 |
| - } |
195 |
| - |
196 |
| - // If we do not pattern match successfully, just set resulting string to |
197 |
| - // unknown and return early. |
198 |
| - resultingString += "unknown"; |
199 |
| - return; |
200 |
| - } |
201 |
| - |
202 |
| - auto nameFromDecl = [&](Decl *d) -> StringRef { |
203 |
| - if (d) { |
204 |
| - if (auto accessor = dyn_cast<AccessorDecl>(d)) { |
205 |
| - return accessor->getStorage()->getBaseName().userFacingName(); |
206 |
| - } |
207 |
| - if (auto vd = dyn_cast<ValueDecl>(d)) { |
208 |
| - return vd->getBaseName().userFacingName(); |
209 |
| - } |
210 |
| - } |
211 |
| - |
212 |
| - return "<unknown decl>"; |
213 |
| - }; |
214 |
| - |
215 |
| - // Walk backwards, constructing our string. |
216 |
| - while (true) { |
217 |
| - auto next = variableNamePath.pop_back_val(); |
218 |
| - |
219 |
| - if (auto *inst = next.dyn_cast<SILInstruction *>()) { |
220 |
| - if (auto i = DebugVarCarryingInst(inst)) { |
221 |
| - resultingString += i.getName(); |
222 |
| - } else if (auto i = VarDeclCarryingInst(inst)) { |
223 |
| - resultingString += i.getName(); |
224 |
| - } else if (auto f = dyn_cast<FunctionRefBaseInst>(inst)) { |
225 |
| - if (auto dc = f->getInitiallyReferencedFunction()->getDeclContext()) { |
226 |
| - resultingString += nameFromDecl(dc->getAsDecl()); |
227 |
| - } else { |
228 |
| - resultingString += "<unknown decl>"; |
229 |
| - } |
230 |
| - } else if (auto m = dyn_cast<MethodInst>(inst)) { |
231 |
| - resultingString += nameFromDecl(m->getMember().getDecl()); |
232 |
| - } else { |
233 |
| - resultingString += "<unknown decl>"; |
234 |
| - } |
235 |
| - } else { |
236 |
| - auto value = next.get<SILValue>(); |
237 |
| - if (auto *fArg = dyn_cast<SILFunctionArgument>(value)) |
238 |
| - resultingString += fArg->getDecl()->getBaseName().userFacingName(); |
239 |
| - } |
240 |
| - |
241 |
| - if (variableNamePath.empty()) |
242 |
| - return; |
243 |
| - |
244 |
| - resultingString += '.'; |
245 |
| - } |
246 |
| -} |
247 |
| - |
248 | 77 | static void getVariableNameForValue(MarkUnresolvedNonCopyableValueInst *mmci,
|
249 | 78 | SmallString<64> &resultingString) {
|
250 |
| - return getVariableNameForValue(mmci, mmci->getOperand(), resultingString); |
| 79 | + VariableNameInferrer inferrer(mmci->getFunction(), resultingString); |
| 80 | + if (inferrer.tryInferNameFromUses(mmci)) |
| 81 | + return; |
| 82 | + inferrer.inferByWalkingUsesToDefs(mmci->getOperand()); |
251 | 83 | }
|
252 | 84 |
|
253 | 85 | //===----------------------------------------------------------------------===//
|
|
0 commit comments