Skip to content

Commit ed721c6

Browse files
authored
Merge pull request #21647 from gottesmm/pr-9bf90eedff20acd7bd60f72b81666a8d2b60bdc7
2 parents 7f00541 + ecacc75 commit ed721c6

File tree

1 file changed

+23
-49
lines changed

1 file changed

+23
-49
lines changed

lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.cpp

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static SILValue scalarizeLoad(LoadInst *LI,
9393
//===----------------------------------------------------------------------===//
9494

9595
namespace {
96+
9697
class ElementUseCollector {
9798
SILModule &Module;
9899
const PMOMemoryObjectInfo &TheMemory;
@@ -118,9 +119,6 @@ class ElementUseCollector {
118119
private:
119120
LLVM_NODISCARD bool collectUses(SILValue Pointer);
120121
LLVM_NODISCARD bool collectContainerUses(AllocBoxInst *ABI);
121-
void addElementUses(SILInstruction *User, PMOUseKind Kind);
122-
LLVM_NODISCARD bool collectTupleElementUses(TupleElementAddrInst *TEAI);
123-
LLVM_NODISCARD bool collectStructElementUses(StructElementAddrInst *SEAI);
124122
};
125123
} // end anonymous namespace
126124

@@ -149,34 +147,6 @@ bool ElementUseCollector::collectFrom() {
149147
return true;
150148
}
151149

152-
/// addElementUses - An operation (e.g. load, store, inout use, etc) on a value
153-
/// acts on all of the aggregate elements in that value. For example, a load
154-
/// of $*(Int,Int) is a use of both Int elements of the tuple. This is a helper
155-
/// to keep the Uses data structure up to date for aggregate uses.
156-
void ElementUseCollector::addElementUses(SILInstruction *User,
157-
PMOUseKind Kind) {
158-
Uses.emplace_back(User, Kind);
159-
}
160-
161-
/// Given a tuple_element_addr or struct_element_addr, compute the new
162-
/// BaseEltNo implicit in the selected member, and recursively add uses of
163-
/// the instruction.
164-
bool ElementUseCollector::collectTupleElementUses(TupleElementAddrInst *TEAI) {
165-
// If we're walking into a tuple within a struct or enum, don't adjust the
166-
// BaseElt. The uses hanging off the tuple_element_addr are going to be
167-
// counted as uses of the struct or enum itself.
168-
return collectUses(TEAI);
169-
}
170-
171-
bool ElementUseCollector::collectStructElementUses(
172-
StructElementAddrInst *SEAI) {
173-
// Generally, we set the "InStructSubElement" flag and recursively process
174-
// the uses so that we know that we're looking at something within the
175-
// current element.
176-
llvm::SaveAndRestore<bool> X(InStructSubElement, true);
177-
return collectUses(SEAI);
178-
}
179-
180150
bool ElementUseCollector::collectContainerUses(AllocBoxInst *ABI) {
181151
for (Operand *UI : ABI->getUses()) {
182152
auto *User = UI->getUser();
@@ -200,7 +170,7 @@ bool ElementUseCollector::collectContainerUses(AllocBoxInst *ABI) {
200170
//
201171
// This will cause the dataflow to stop propagating any information at the
202172
// use block.
203-
addElementUses(User, PMOUseKind::Escape);
173+
Uses.emplace_back(User, PMOUseKind::Escape);
204174
}
205175

206176
return true;
@@ -221,22 +191,26 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
221191
auto *User = UI->getUser();
222192

223193
// struct_element_addr P, #field indexes into the current element.
224-
if (auto *SEAI = dyn_cast<StructElementAddrInst>(User)) {
225-
if (!collectStructElementUses(SEAI))
194+
if (auto *seai = dyn_cast<StructElementAddrInst>(User)) {
195+
// Generally, we set the "InStructSubElement" flag and recursively process
196+
// the uses so that we know that we're looking at something within the
197+
// current element.
198+
llvm::SaveAndRestore<bool> X(InStructSubElement, true);
199+
if (!collectUses(seai))
226200
return false;
227201
continue;
228202
}
229203

230204
// Instructions that compute a subelement are handled by a helper.
231-
if (auto *TEAI = dyn_cast<TupleElementAddrInst>(User)) {
232-
if (!collectTupleElementUses(TEAI))
205+
if (auto *teai = dyn_cast<TupleElementAddrInst>(User)) {
206+
if (!collectUses(teai))
233207
return false;
234208
continue;
235209
}
236210

237211
// Look through begin_access.
238-
if (auto I = dyn_cast<BeginAccessInst>(User)) {
239-
if (!collectUses(I))
212+
if (auto *bai = dyn_cast<BeginAccessInst>(User)) {
213+
if (!collectUses(bai))
240214
return false;
241215
continue;
242216
}
@@ -251,7 +225,7 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
251225
if (PointeeType.is<TupleType>())
252226
UsesToScalarize.push_back(User);
253227
else
254-
addElementUses(User, PMOUseKind::Load);
228+
Uses.emplace_back(User, PMOUseKind::Load);
255229
continue;
256230
}
257231

@@ -279,7 +253,7 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
279253
else
280254
Kind = PMOUseKind::Initialization;
281255

282-
addElementUses(User, Kind);
256+
Uses.emplace_back(User, Kind);
283257
continue;
284258
}
285259

@@ -321,7 +295,7 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
321295
else
322296
Kind = PMOUseKind::Assign;
323297

324-
addElementUses(User, Kind);
298+
Uses.emplace_back(User, Kind);
325299
continue;
326300
}
327301

@@ -346,7 +320,7 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
346320
if (InStructSubElement) {
347321
return false;
348322
}
349-
addElementUses(User, PMOUseKind::Initialization);
323+
Uses.emplace_back(User, PMOUseKind::Initialization);
350324
continue;
351325

352326
// Otherwise, adjust the argument index.
@@ -367,7 +341,7 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
367341
case ParameterConvention::Indirect_In:
368342
case ParameterConvention::Indirect_In_Constant:
369343
case ParameterConvention::Indirect_In_Guaranteed:
370-
addElementUses(User, PMOUseKind::IndirectIn);
344+
Uses.emplace_back(User, PMOUseKind::IndirectIn);
371345
continue;
372346

373347
// If this is an @inout parameter, it is like both a load and store.
@@ -377,7 +351,7 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
377351
// mutating method, we model that as an escape of self. If an
378352
// individual sub-member is passed as inout, then we model that as an
379353
// inout use.
380-
addElementUses(User, PMOUseKind::InOutUse);
354+
Uses.emplace_back(User, PMOUseKind::InOutUse);
381355
continue;
382356
}
383357
}
@@ -390,14 +364,14 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
390364
if (InStructSubElement) {
391365
return false;
392366
}
393-
Uses.push_back(PMOMemoryUse(User, PMOUseKind::Initialization));
367+
Uses.emplace_back(User, PMOUseKind::Initialization);
394368
continue;
395369
}
396370

397371
// open_existential_addr is a use of the protocol value,
398372
// so it is modeled as a load.
399373
if (isa<OpenExistentialAddrInst>(User)) {
400-
Uses.push_back(PMOMemoryUse(User, PMOUseKind::Load));
374+
Uses.emplace_back(User, PMOUseKind::Load);
401375
// TODO: Is it safe to ignore all uses of the open_existential_addr?
402376
continue;
403377
}
@@ -418,7 +392,7 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
418392
continue;
419393

420394
// Otherwise, the use is something complicated, it escapes.
421-
addElementUses(User, PMOUseKind::Escape);
395+
Uses.emplace_back(User, PMOUseKind::Escape);
422396
}
423397

424398
// Now that we've walked all of the immediate uses, scalarize any operations
@@ -483,8 +457,8 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
483457
// Now that we've scalarized some stuff, recurse down into the newly created
484458
// element address computations to recursively process it. This can cause
485459
// further scalarization.
486-
if (llvm::any_of(ElementAddrs, [&](SILValue V) {
487-
return !collectTupleElementUses(cast<TupleElementAddrInst>(V));
460+
if (llvm::any_of(ElementAddrs, [&](SILValue v) {
461+
return !collectUses(cast<TupleElementAddrInst>(v));
488462
})) {
489463
return false;
490464
}

0 commit comments

Comments
 (0)