Skip to content

Commit 9007fca

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 2e3be7d commit 9007fca

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
@@ -147,9 +147,7 @@ static void mapOperands(SILInstruction *inst,
147147
static void updateSSAForUseOfValue(
148148
SILSSAUpdater &updater, SmallVectorImpl<SILPhiArgument *> &insertedPhis,
149149
const llvm::DenseMap<ValueBase *, SILValue> &valueMap,
150-
SILBasicBlock *Header, SILBasicBlock *EntryCheckBlock, SILValue Res,
151-
SmallVectorImpl<std::pair<SILBasicBlock *, unsigned>>
152-
&accumulatedAddressPhis) {
150+
SILBasicBlock *Header, SILBasicBlock *EntryCheckBlock, SILValue Res) {
153151
// Find the mapped instruction.
154152
assert(valueMap.count(Res) && "Expected to find value in map!");
155153
SILValue MappedValue = valueMap.find(Res)->second;
@@ -179,14 +177,13 @@ static void updateSSAForUseOfValue(
179177
if (user->getParent() == Header)
180178
continue;
181179

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

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

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

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

@@ -227,7 +218,7 @@ static void rewriteNewLoopEntryCheckBlock(
227218
for (unsigned i : range(header->getNumArguments())) {
228219
auto *arg = header->getArguments()[i];
229220
updateSSAForUseOfValue(updater, insertedPhis, valueMap, header,
230-
entryCheckBlock, arg, accumulatedAddressPhis);
221+
entryCheckBlock, arg);
231222
}
232223

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

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

0 commit comments

Comments
 (0)