Skip to content

Commit d068eac

Browse files
committed
Get rid of address to pointer conversion in LoopRotate which is present just to avoid the address phi absence assert
1 parent 28f7001 commit d068eac

File tree

1 file changed

+12
-55
lines changed

1 file changed

+12
-55
lines changed

lib/SILOptimizer/LoopTransforms/LoopRotate.cpp

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ static void mapOperands(SILInstruction *inst,
146146
static void updateSSAForUseOfValue(
147147
SILSSAUpdater &updater, SmallVectorImpl<SILPhiArgument *> &insertedPhis,
148148
const llvm::DenseMap<ValueBase *, SILValue> &valueMap,
149-
SILBasicBlock *Header, SILBasicBlock *EntryCheckBlock, SILValue Res,
150-
SmallVectorImpl<std::pair<SILBasicBlock *, unsigned>>
151-
&accumulatedAddressPhis) {
149+
SILBasicBlock *Header, SILBasicBlock *EntryCheckBlock, SILValue Res) {
152150
// Find the mapped instruction.
153151
assert(valueMap.count(Res) && "Expected to find value in map!");
154152
SILValue MappedValue = valueMap.find(Res)->second;
@@ -178,14 +176,13 @@ static void updateSSAForUseOfValue(
178176
if (user->getParent() == Header)
179177
continue;
180178

181-
assert(user->getParent() != EntryCheckBlock
182-
&& "The entry check block should dominate the header");
179+
assert(user->getParent() != EntryCheckBlock &&
180+
"The entry check block should dominate the header");
183181
updater.rewriteUse(*use);
184182
}
185183

186184
// Canonicalize inserted phis to avoid extra BB Args and if we find an address
187185
// phi, stash it so we can handle it after we are done rewriting.
188-
bool hasOwnership = Header->getParent()->hasOwnership();
189186
for (SILPhiArgument *arg : insertedPhis) {
190187
if (SILValue inst = replaceBBArgWithCast(arg)) {
191188
arg->replaceAllUsesWith(inst);
@@ -194,30 +191,24 @@ static void updateSSAForUseOfValue(
194191
// SimplifyCFG deletes the dead BB arg.
195192
continue;
196193
}
197-
198-
// If we didn't simplify and have an address phi, stash the value so we can
199-
// fix it up.
200-
if (hasOwnership && arg->getType().isAddress())
201-
accumulatedAddressPhis.emplace_back(arg->getParent(), arg->getIndex());
202194
}
203195
}
204196

205-
static void updateSSAForUseOfInst(
206-
SILSSAUpdater &updater, SmallVectorImpl<SILPhiArgument *> &insertedPhis,
207-
const llvm::DenseMap<ValueBase *, SILValue> &valueMap,
208-
SILBasicBlock *header, SILBasicBlock *entryCheckBlock, SILInstruction *inst,
209-
SmallVectorImpl<std::pair<SILBasicBlock *, unsigned>>
210-
&accumulatedAddressPhis) {
197+
static void
198+
updateSSAForUseOfInst(SILSSAUpdater &updater,
199+
SmallVectorImpl<SILPhiArgument *> &insertedPhis,
200+
const llvm::DenseMap<ValueBase *, SILValue> &valueMap,
201+
SILBasicBlock *header, SILBasicBlock *entryCheckBlock,
202+
SILInstruction *inst) {
211203
for (auto result : inst->getResults())
212204
updateSSAForUseOfValue(updater, insertedPhis, valueMap, header,
213-
entryCheckBlock, result, accumulatedAddressPhis);
205+
entryCheckBlock, result);
214206
}
215207

216208
/// Rewrite the code we just created in the preheader and update SSA form.
217209
static void rewriteNewLoopEntryCheckBlock(
218210
SILBasicBlock *header, SILBasicBlock *entryCheckBlock,
219211
const llvm::DenseMap<ValueBase *, SILValue> &valueMap) {
220-
SmallVector<std::pair<SILBasicBlock *, unsigned>, 8> accumulatedAddressPhis;
221212
SmallVector<SILPhiArgument *, 8> insertedPhis;
222213
SILSSAUpdater updater(&insertedPhis);
223214

@@ -226,7 +217,7 @@ static void rewriteNewLoopEntryCheckBlock(
226217
for (unsigned i : range(header->getNumArguments())) {
227218
auto *arg = header->getArguments()[i];
228219
updateSSAForUseOfValue(updater, insertedPhis, valueMap, header,
229-
entryCheckBlock, arg, accumulatedAddressPhis);
220+
entryCheckBlock, arg);
230221
}
231222

232223
auto instIter = header->begin();
@@ -235,43 +226,9 @@ static void rewriteNewLoopEntryCheckBlock(
235226
while (instIter != header->end()) {
236227
auto &inst = *instIter;
237228
updateSSAForUseOfInst(updater, insertedPhis, valueMap, header,
238-
entryCheckBlock, &inst, accumulatedAddressPhis);
229+
entryCheckBlock, &inst);
239230
++instIter;
240231
}
241-
242-
// Then see if any of our phis were address phis. In such a case, rewrite the
243-
// address to be a smuggled through raw pointer. We do this late to
244-
// conservatively not interfere with the previous code's invariants.
245-
//
246-
// We also translate the phis into a BasicBlock, Index form so we are careful
247-
// with invalidation issues around branches/args.
248-
auto rawPointerTy =
249-
SILType::getRawPointerType(header->getParent()->getASTContext());
250-
auto rawPointerUndef = SILUndef::get(rawPointerTy, header->getModule());
251-
auto loc = RegularLocation::getAutoGeneratedLocation();
252-
while (!accumulatedAddressPhis.empty()) {
253-
SILBasicBlock *block;
254-
unsigned argIndex;
255-
std::tie(block, argIndex) = accumulatedAddressPhis.pop_back_val();
256-
auto *arg = cast<SILPhiArgument>(block->getArgument(argIndex));
257-
assert(arg->getType().isAddress() && "Not an address phi?!");
258-
for (auto *predBlock : block->getPredecessorBlocks()) {
259-
Operand *predUse = arg->getIncomingPhiOperand(predBlock);
260-
SILBuilderWithScope builder(predUse->getUser());
261-
auto *newIncomingValue =
262-
builder.createAddressToPointer(loc, predUse->get(), rawPointerTy);
263-
predUse->set(newIncomingValue);
264-
}
265-
SILBuilderWithScope builder(arg->getNextInstruction());
266-
SILType oldArgType = arg->getType();
267-
auto *phiShim = builder.createPointerToAddress(
268-
loc, rawPointerUndef, oldArgType, true /*isStrict*/,
269-
false /*is invariant*/);
270-
arg->replaceAllUsesWith(phiShim);
271-
SILArgument *newArg = block->replacePhiArgument(
272-
argIndex, rawPointerTy, OwnershipKind::None, nullptr);
273-
phiShim->setOperand(newArg);
274-
}
275232
}
276233

277234
/// Update the dominator tree after rotating the loop.

0 commit comments

Comments
 (0)