Skip to content

Commit 301ab4e

Browse files
committed
LoopRotate: remove the replace-arg-with-struct peephole optimization
This does not belong to loop-rotate and does not work with OSSA. This peephole is covered by other optimizations.
1 parent 8715c34 commit 301ab4e

File tree

5 files changed

+0
-176
lines changed

5 files changed

+0
-176
lines changed

include/swift/SILOptimizer/Utils/SILSSAUpdater.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ class SILBasicBlock;
3535
class SILType;
3636
class SILUndef;
3737

38-
/// Independent utility that canonicalizes BB arguments by reusing structurally
39-
/// equivalent arguments and replacing the original arguments with casts.
40-
SILValue replaceBBArgWithCast(SILPhiArgument *arg);
41-
4238
/// This class updates SSA for a set of SIL instructions defined in multiple
4339
/// blocks.
4440
class SILSSAUpdater {

lib/SILOptimizer/LoopTransforms/LoopRotate.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,6 @@ static void updateSSAForUseOfValue(
187187
updater.rewriteUse(*use);
188188
}
189189

190-
// Canonicalize inserted phis to avoid extra BB Args and if we find an address
191-
// phi, stash it so we can handle it after we are done rewriting.
192-
for (SILPhiArgument *arg : insertedPhis) {
193-
if (SILValue inst = replaceBBArgWithCast(arg)) {
194-
arg->replaceAllUsesWith(inst);
195-
// DCE+SimplifyCFG runs as a post-pass cleanup.
196-
// DCE replaces dead arg values with undef.
197-
// SimplifyCFG deletes the dead BB arg.
198-
continue;
199-
}
200-
}
201190
}
202191

203192
static void

lib/SILOptimizer/Utils/SILSSAUpdater.cpp

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -494,82 +494,6 @@ Operand *UseWrapper::getOperand() {
494494
llvm_unreachable("uninitialize use type");
495495
}
496496

497-
/// At least one value feeding the specified SILArgument is a Struct. Attempt to
498-
/// replace the Argument with a new Struct in the same block.
499-
///
500-
/// When we handle more types of casts, this can become a template.
501-
///
502-
/// ArgValues are the values feeding the specified Argument from each
503-
/// predecessor. They must be listed in order of Arg->getParent()->getPreds().
504-
static StructInst *
505-
replaceBBArgWithStruct(SILPhiArgument *phiArg,
506-
SmallVectorImpl<SILValue> &argValues) {
507-
508-
SILBasicBlock *phiBlock = phiArg->getParent();
509-
auto *firstSI = dyn_cast<StructInst>(argValues[0]);
510-
if (!firstSI)
511-
return nullptr;
512-
513-
// Collect the BBArg index of each struct oper.
514-
// e.g.
515-
// struct(A, B)
516-
// br (B, A)
517-
// : ArgIdxForOper => {1, 0}
518-
SmallVector<unsigned, 4> argIdxForOper;
519-
for (unsigned operIdx : indices(firstSI->getElements())) {
520-
bool foundMatchingArgIdx = false;
521-
for (unsigned argIdx : indices(phiBlock->getArguments())) {
522-
auto avIter = argValues.begin();
523-
bool tryNextArgIdx = false;
524-
for (SILBasicBlock *predBlock : phiBlock->getPredecessorBlocks()) {
525-
// All argument values must be StructInst.
526-
auto *predSI = dyn_cast<StructInst>(*avIter++);
527-
if (!predSI)
528-
return nullptr;
529-
OperandValueArrayRef edgeValues =
530-
getEdgeValuesForTerminator(predBlock->getTerminator(), phiBlock);
531-
if (edgeValues[argIdx] != predSI->getElements()[operIdx]) {
532-
tryNextArgIdx = true;
533-
break;
534-
}
535-
}
536-
if (!tryNextArgIdx) {
537-
assert(avIter == argValues.end() &&
538-
"# ArgValues does not match # BB preds");
539-
foundMatchingArgIdx = true;
540-
argIdxForOper.push_back(argIdx);
541-
break;
542-
}
543-
}
544-
if (!foundMatchingArgIdx)
545-
return nullptr;
546-
}
547-
548-
SmallVector<SILValue, 4> structArgs;
549-
for (auto argIdx : argIdxForOper)
550-
structArgs.push_back(phiBlock->getArgument(argIdx));
551-
552-
// TODO: We probably want to use a SILBuilderWithScope here. What should we
553-
// use?
554-
SILBuilder builder(phiBlock, phiBlock->begin());
555-
return builder.createStruct(cast<StructInst>(argValues[0])->getLoc(),
556-
phiArg->getType(), structArgs);
557-
}
558-
559-
/// Canonicalize BB arguments, replacing argument-of-casts with
560-
/// cast-of-arguments. This only eliminates existing arguments, replacing them
561-
/// with casts. No new arguments are created. This allows downstream pattern
562-
/// detection like induction variable analysis to succeed.
563-
///
564-
/// If Arg is replaced, return the cast instruction. Otherwise return nullptr.
565-
SILValue swift::replaceBBArgWithCast(SILPhiArgument *arg) {
566-
SmallVector<SILValue, 4> argValues;
567-
arg->getIncomingPhiValues(argValues);
568-
if (isa<StructInst>(argValues[0]))
569-
return replaceBBArgWithStruct(arg, argValues);
570-
return nullptr;
571-
}
572-
573497
namespace swift::test {
574498
// Arguments:
575499
// * the first arguments are values, which are added as "available values" to the SSA-updater

test/SILOptimizer/looprotate.sil

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -336,48 +336,6 @@ bb0(%a0 : $Int32):
336336
return %t1 : $()
337337
}
338338

339-
// Test the SSA Updater utility: replaceBBArgWithCast().
340-
//
341-
// CHECK-LABEL: struct_arg
342-
// CHECK: bb0:
343-
// CHECK: cond_br
344-
// CHECK: bb1:
345-
// CHECK: br bb3
346-
// CHECK: bb3({{%.*}} : $Builtin.Int32, [[WORD:%.*]] : $Builtin.Int32
347-
// CHECK: [[STRUCT:%[0-9]+]] = struct $Int32 ([[WORD]]
348-
// CHECK: apply {{.*}}([[STRUCT]])
349-
// CHECK: cond_br
350-
// CHECK: bb6({{.*}}):
351-
// CHECK: return
352-
sil @struct_arg : $@convention(thin) () -> () {
353-
bb0:
354-
%c0 = integer_literal $Builtin.Int32, 0
355-
%c100 = integer_literal $Builtin.Int32, 100
356-
br bb1(%c0 : $Builtin.Int32, %c0 : $Builtin.Int32)
357-
358-
bb1(%a1 : $Builtin.Int32, %a0 : $Builtin.Int32):
359-
%s0 = struct $Int32 (%a0 : $Builtin.Int32)
360-
%z1 = builtin "cmp_eq_Word"(%a0 : $Builtin.Int32, %c100 : $Builtin.Int32) : $Builtin.Int1
361-
cond_br %z1, bb2, bb3
362-
363-
bb3:
364-
%c1 = integer_literal $Builtin.Int32, 1
365-
%z2 = integer_literal $Builtin.Int1, 0
366-
%d1 = builtin "sadd_with_overflow_Word"(%a0 : $Builtin.Int32, %c1 : $Builtin.Int32, %z2 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
367-
%t1 = tuple_extract %d1 : $(Builtin.Int32, Builtin.Int1), 0
368-
%d2 = builtin "sadd_with_overflow_Word"(%a1 : $Builtin.Int32, %c1 : $Builtin.Int32, %z2 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
369-
%t2 = tuple_extract %d2 : $(Builtin.Int32, Builtin.Int1), 0
370-
%f1 = function_ref @readInt : $@convention(thin) (Int32) -> ()
371-
%l1 = apply %f1(%s0) : $@convention(thin) (Int32) -> ()
372-
br bb1(%t2 : $Builtin.Int32, %t1 : $Builtin.Int32)
373-
374-
bb2:
375-
%14 = struct $Int32 (%a1 : $Builtin.Int32)
376-
%t3 = tuple ()
377-
return %t3 : $()
378-
379-
}
380-
381339
// Test LoopRotate insertBackedgeBlock.
382340
//
383341
// CHECK-LABEL: insert_backedge_block

test/SILOptimizer/looprotate_ossa.sil

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -404,49 +404,6 @@ bb0(%a0 : $Int32):
404404
return %t1 : $()
405405
}
406406

407-
// Test the SSA Updater utility: replaceBBArgWithCast().
408-
//
409-
// CHECK-LABEL: sil [ossa] @struct_arg :
410-
// CHECK: bb0:
411-
// CHECK: cond_br
412-
// CHECK: bb1:
413-
// CHECK: br bb3
414-
// CHECK: bb3({{%.*}} : $Builtin.Int32, [[WORD:%.*]] : $Builtin.Int32
415-
// CHECK: [[STRUCT:%[0-9]+]] = struct $Int32 ([[WORD]]
416-
// CHECK: apply {{.*}}([[STRUCT]])
417-
// CHECK: cond_br
418-
// CHECK: bb6({{.*}}):
419-
// CHECK: return
420-
// CHECK-LABEL: } // end sil function 'struct_arg'
421-
sil [ossa] @struct_arg : $@convention(thin) () -> () {
422-
bb0:
423-
%c0 = integer_literal $Builtin.Int32, 0
424-
%c100 = integer_literal $Builtin.Int32, 100
425-
br bb1(%c0 : $Builtin.Int32, %c0 : $Builtin.Int32)
426-
427-
bb1(%a1 : $Builtin.Int32, %a0 : $Builtin.Int32):
428-
%s0 = struct $Int32 (%a0 : $Builtin.Int32)
429-
%z1 = builtin "cmp_eq_Word"(%a0 : $Builtin.Int32, %c100 : $Builtin.Int32) : $Builtin.Int1
430-
cond_br %z1, bb2, bb3
431-
432-
bb3:
433-
%c1 = integer_literal $Builtin.Int32, 1
434-
%z2 = integer_literal $Builtin.Int1, 0
435-
%d1 = builtin "sadd_with_overflow_Word"(%a0 : $Builtin.Int32, %c1 : $Builtin.Int32, %z2 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
436-
%t1 = tuple_extract %d1 : $(Builtin.Int32, Builtin.Int1), 0
437-
%d2 = builtin "sadd_with_overflow_Word"(%a1 : $Builtin.Int32, %c1 : $Builtin.Int32, %z2 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
438-
%t2 = tuple_extract %d2 : $(Builtin.Int32, Builtin.Int1), 0
439-
%f1 = function_ref @readInt : $@convention(thin) (Int32) -> ()
440-
%l1 = apply %f1(%s0) : $@convention(thin) (Int32) -> ()
441-
br bb1(%t2 : $Builtin.Int32, %t1 : $Builtin.Int32)
442-
443-
bb2:
444-
%14 = struct $Int32 (%a1 : $Builtin.Int32)
445-
%t3 = tuple ()
446-
return %t3 : $()
447-
448-
}
449-
450407
// Test LoopRotate insertBackedgeBlock.
451408
//
452409
// CHECK-LABEL: sil [ossa] @insert_backedge_block :

0 commit comments

Comments
 (0)