Skip to content

Commit addf7f0

Browse files
authored
Merge pull request #21908 from gottesmm/pr-221110c36a5cf9783736078ebc1a40785eb10bdf
[pmo] Simplify/cleanup some code in preparation for available value c…
2 parents 14ce144 + 28dde12 commit addf7f0

File tree

1 file changed

+72
-45
lines changed

1 file changed

+72
-45
lines changed

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,9 +1017,8 @@ class AllocOptimize {
10171017
bool promoteLoad(SILInstruction *Inst);
10181018
void promoteDestroyAddr(DestroyAddrInst *DAI,
10191019
MutableArrayRef<AvailableValue> Values);
1020-
bool
1021-
canPromoteDestroyAddr(DestroyAddrInst *DAI,
1022-
llvm::SmallVectorImpl<AvailableValue> &AvailableValues);
1020+
bool canPromoteDestroyAddr(DestroyAddrInst *DAI,
1021+
SmallVectorImpl<AvailableValue> &AvailableValues);
10231022

10241023
bool tryToRemoveDeadAllocation();
10251024
};
@@ -1149,8 +1148,7 @@ bool AllocOptimize::promoteLoad(SILInstruction *Inst) {
11491148

11501149
/// Return true if we can promote the given destroy.
11511150
bool AllocOptimize::canPromoteDestroyAddr(
1152-
DestroyAddrInst *DAI,
1153-
llvm::SmallVectorImpl<AvailableValue> &AvailableValues) {
1151+
DestroyAddrInst *DAI, SmallVectorImpl<AvailableValue> &AvailableValues) {
11541152
SILValue Address = DAI->getOperand();
11551153

11561154
// We cannot promote destroys of address-only types, because we can't expose
@@ -1224,8 +1222,49 @@ void AllocOptimize::promoteDestroyAddr(
12241222
DAI->eraseFromParent();
12251223
}
12261224

1227-
/// tryToRemoveDeadAllocation - If the allocation is an autogenerated allocation
1228-
/// that is only stored to (after load promotion) then remove it completely.
1225+
namespace {
1226+
1227+
struct DestroyAddrPromotionState {
1228+
ArrayRef<SILInstruction *> destroys;
1229+
SmallVector<unsigned, 8> destroyAddrIndices;
1230+
SmallVector<AvailableValue, 32> availableValueList;
1231+
SmallVector<unsigned, 8> availableValueStartOffsets;
1232+
1233+
DestroyAddrPromotionState(ArrayRef<SILInstruction *> destroys)
1234+
: destroys(destroys) {}
1235+
1236+
unsigned size() const {
1237+
return destroyAddrIndices.size();
1238+
}
1239+
1240+
void initializeForDestroyAddr(unsigned destroyAddrIndex) {
1241+
availableValueStartOffsets.push_back(availableValueList.size());
1242+
destroyAddrIndices.push_back(destroyAddrIndex);
1243+
}
1244+
1245+
std::pair<DestroyAddrInst *, MutableArrayRef<AvailableValue>>
1246+
getData(unsigned index) {
1247+
unsigned destroyAddrIndex = destroyAddrIndices[index];
1248+
unsigned startOffset = availableValueStartOffsets[index];
1249+
unsigned count;
1250+
1251+
if ((availableValueStartOffsets.size() - 1) != index) {
1252+
count = availableValueStartOffsets[index + 1] - startOffset;
1253+
} else {
1254+
count = availableValueList.size() - startOffset;
1255+
}
1256+
1257+
MutableArrayRef<AvailableValue> values(&availableValueList[startOffset],
1258+
count);
1259+
auto *dai = cast<DestroyAddrInst>(destroys[destroyAddrIndex]);
1260+
return {dai, values};
1261+
}
1262+
};
1263+
1264+
} // end anonymous namespace
1265+
1266+
/// If the allocation is an autogenerated allocation that is only stored to
1267+
/// (after load promotion) then remove it completely.
12291268
bool AllocOptimize::tryToRemoveDeadAllocation() {
12301269
assert((isa<AllocBoxInst>(TheMemory) || isa<AllocStackInst>(TheMemory)) &&
12311270
"Unhandled allocation case");
@@ -1236,29 +1275,30 @@ bool AllocOptimize::tryToRemoveDeadAllocation() {
12361275
// 1. They are in a transparent function.
12371276
// 2. They are in a normal function, but didn't come from a VarDecl, or came
12381277
// from one that was autogenerated or inlined from a transparent function.
1239-
SILLocation Loc = TheMemory->getLoc();
1278+
SILLocation loc = TheMemory->getLoc();
12401279
if (!TheMemory->getFunction()->isTransparent() &&
1241-
Loc.getAsASTNode<VarDecl>() && !Loc.isAutoGenerated() &&
1242-
!Loc.is<MandatoryInlinedLocation>())
1280+
loc.getAsASTNode<VarDecl>() && !loc.isAutoGenerated() &&
1281+
!loc.is<MandatoryInlinedLocation>())
12431282
return false;
12441283

12451284
// Check the uses list to see if there are any non-store uses left over after
12461285
// load promotion and other things PMO does.
1247-
for (auto &U : Uses) {
1286+
for (auto &u : Uses) {
12481287
// Ignore removed instructions.
1249-
if (U.Inst == nullptr) continue;
1288+
if (u.Inst == nullptr)
1289+
continue;
12501290

1251-
switch (U.Kind) {
1291+
switch (u.Kind) {
12521292
case PMOUseKind::Assign:
12531293
case PMOUseKind::PartialStore:
12541294
case PMOUseKind::InitOrAssign:
12551295
break; // These don't prevent removal.
12561296
case PMOUseKind::Initialization:
1257-
if (!isa<ApplyInst>(U.Inst) &&
1297+
if (!isa<ApplyInst>(u.Inst) &&
12581298
// A copy_addr that is not a take affects the retain count
12591299
// of the source.
1260-
(!isa<CopyAddrInst>(U.Inst) ||
1261-
cast<CopyAddrInst>(U.Inst)->isTakeOfSrc()))
1300+
(!isa<CopyAddrInst>(u.Inst) ||
1301+
cast<CopyAddrInst>(u.Inst)->isTakeOfSrc()))
12621302
break;
12631303
// FALL THROUGH.
12641304
LLVM_FALLTHROUGH;
@@ -1267,7 +1307,8 @@ bool AllocOptimize::tryToRemoveDeadAllocation() {
12671307
case PMOUseKind::InOutUse:
12681308
case PMOUseKind::Escape:
12691309
LLVM_DEBUG(llvm::dbgs() << "*** Failed to remove autogenerated alloc: "
1270-
"kept alive by: " << *U.Inst);
1310+
"kept alive by: "
1311+
<< *u.Inst);
12711312
return false; // These do prevent removal.
12721313
}
12731314
}
@@ -1289,53 +1330,39 @@ bool AllocOptimize::tryToRemoveDeadAllocation() {
12891330

12901331
// Otherwise removing the deallocation will drop any releases. Check that
12911332
// there is nothing preventing removal.
1292-
llvm::SmallVector<unsigned, 8> DestroyAddrIndices;
1293-
llvm::SmallVector<AvailableValue, 32> AvailableValueList;
1294-
llvm::SmallVector<unsigned, 8> AvailableValueStartOffsets;
1333+
DestroyAddrPromotionState state(Releases);
12951334

1296-
for (auto P : llvm::enumerate(Releases)) {
1297-
auto *R = P.value();
1298-
if (R == nullptr)
1335+
for (auto p : llvm::enumerate(Releases)) {
1336+
auto *r = p.value();
1337+
if (r == nullptr)
12991338
continue;
13001339

13011340
// We stash all of the destroy_addr that we see.
1302-
if (auto *DAI = dyn_cast<DestroyAddrInst>(R)) {
1303-
AvailableValueStartOffsets.push_back(AvailableValueList.size());
1341+
if (auto *dai = dyn_cast<DestroyAddrInst>(r)) {
1342+
state.initializeForDestroyAddr(p.index() /*destroyAddrIndex*/);
13041343
// Make sure we can actually promote this destroy addr. If we can not,
13051344
// then we must bail. In order to not gather available values twice, we
13061345
// gather the available values here that we will use to promote the
13071346
// values.
1308-
if (!canPromoteDestroyAddr(DAI, AvailableValueList))
1347+
if (!canPromoteDestroyAddr(dai, state.availableValueList))
13091348
return false;
1310-
DestroyAddrIndices.push_back(P.index());
13111349
continue;
13121350
}
13131351

13141352
LLVM_DEBUG(llvm::dbgs()
13151353
<< "*** Failed to remove autogenerated non-trivial alloc: "
13161354
"kept alive by release: "
1317-
<< *R);
1355+
<< *r);
13181356
return false;
13191357
}
13201358

13211359
// If we reached this point, we can promote all of our destroy_addr.
1322-
for (auto P : llvm::enumerate(DestroyAddrIndices)) {
1323-
unsigned DestroyAddrIndex = P.value();
1324-
unsigned AvailableValueIndex = P.index();
1325-
unsigned StartOffset = AvailableValueStartOffsets[AvailableValueIndex];
1326-
unsigned Count;
1327-
1328-
if ((AvailableValueStartOffsets.size() - 1) != AvailableValueIndex) {
1329-
Count = AvailableValueStartOffsets[AvailableValueIndex + 1] - StartOffset;
1330-
} else {
1331-
Count = AvailableValueList.size() - StartOffset;
1332-
}
1333-
1334-
MutableArrayRef<AvailableValue> Values(&AvailableValueList[StartOffset],
1335-
Count);
1336-
auto *DAI = cast<DestroyAddrInst>(Releases[DestroyAddrIndex]);
1337-
promoteDestroyAddr(DAI, Values);
1338-
Releases[DestroyAddrIndex] = nullptr;
1360+
for (unsigned i : range(state.size())) {
1361+
DestroyAddrInst *dai;
1362+
MutableArrayRef<AvailableValue> values;
1363+
std::tie(dai, values) = state.getData(i);
1364+
promoteDestroyAddr(dai, values);
1365+
// We do not need to unset releases, since we are going to exit here.
13391366
}
13401367

13411368
LLVM_DEBUG(llvm::dbgs() << "*** Removing autogenerated non-trivial alloc: "

0 commit comments

Comments
 (0)