From: Philip Reames Date: Wed, 3 Mar 2021 19:49:46 +0000 (-0800) Subject: [LSR] Unify scheduling of existing and inserted addrecs X-Git-Tag: llvmorg-14-init~13487 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=805115655ee4886d212ef5aebca852cb5ebedc72;p=platform%2Fupstream%2Fllvm.git [LSR] Unify scheduling of existing and inserted addrecs LSR goes to some lengths to schedule IV increments such that %iv and %iv.next never need to overlap. This is fairly fundamental to LSRs cost model. LSR assumes that an addrec can be represented with a single register. If %iv and %iv.next have to overlap, then that assumption does not hold. The bug - which this patch is fixing - is that LSR only does this scheduling for IVs which it inserts, but it's cost model assumes the same for existing IVs that it reuses. It will rewrite existing IV users such that the no-overlap property holds, but will not actually reschedule said IV increment. As you can see from the relatively lack of test updates, this doesn't actually impact codegen much. The main reason for doing it is to make a follow up patch series which improves post-increment use and scheduling easier to follow. Differential Revision: https://reviews.llvm.org/D97219 --- diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp index 7ae7520..50aed8c 100644 --- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -5561,6 +5561,46 @@ void LSRInstance::ImplementSolution( Changed |= RecursivelyDeleteTriviallyDeadInstructionsPermissive(DeadInsts, &TLI, MSSAU); + + // In our cost analysis above, we assume that each addrec consumes exactly + // one register, and arrange to have increments inserted just before the + // latch to maximimize the chance this is true. However, if we reused + // existing IVs, we now need to move the increments to match our + // expectations. Otherwise, our cost modeling results in us having a + // chosen a non-optimal result for the actual schedule. (And yes, this + // scheduling decision does impact later codegen.) + for (PHINode &PN : L->getHeader()->phis()) { + // First, filter out anything not an obvious addrec + if (!SE.isSCEVable(PN.getType()) || !isa(SE.getSCEV(&PN))) + continue; + Instruction *IncV = + dyn_cast(PN.getIncomingValueForBlock(L->getLoopLatch())); + if (!IncV) + continue; + + if (IncV->getOpcode() != Instruction::Add && + IncV->getOpcode() != Instruction::Sub) + continue; + + if (IncV->getOperand(0) != &PN && + !isa(IncV->getOperand(1))) + // If not a constant step, might increase register pressure + // (We assume constants have been canonicalized to RHS) + continue; + + if (IncV->getParent() == IVIncInsertPos->getParent()) + // Only bother moving across blocks. Isel can handle block local case. + continue; + + // Can we legally schedule inc at the desired point? + if (!llvm::all_of(IncV->uses(), + [&](Use &U) {return DT.dominates(IVIncInsertPos, U);})) + continue; + IncV->moveBefore(IVIncInsertPos); + Changed = true; + } + + } LSRInstance::LSRInstance(Loop *L, IVUsers &IU, ScalarEvolution &SE, diff --git a/llvm/test/Transforms/LoopStrengthReduce/post-increment-insertion.ll b/llvm/test/Transforms/LoopStrengthReduce/post-increment-insertion.ll index 26e1734..c2266c9 100644 --- a/llvm/test/Transforms/LoopStrengthReduce/post-increment-insertion.ll +++ b/llvm/test/Transforms/LoopStrengthReduce/post-increment-insertion.ll @@ -13,13 +13,13 @@ define i32 @test_01(i32* %p, i64 %len, i32 %x) { ; CHECK-NEXT: br label [[LOOP:%.*]] ; CHECK: loop: ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[LEN:%.*]], [[ENTRY:%.*]] ] -; CHECK-NEXT: [[IV_NEXT]] = add nsw i64 [[IV]], -1 ; CHECK-NEXT: [[COND_1:%.*]] = icmp eq i64 [[IV]], 0 ; CHECK-NEXT: br i1 [[COND_1]], label [[EXIT:%.*]], label [[BACKEDGE]] ; CHECK: backedge: ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i32, i32* [[SCEVGEP]], i64 [[IV]] ; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, i32* [[SCEVGEP1]] unordered, align 4 ; CHECK-NEXT: [[COND_2:%.*]] = icmp eq i32 [[LOADED]], [[X:%.*]] +; CHECK-NEXT: [[IV_NEXT]] = add nsw i64 [[IV]], -1 ; CHECK-NEXT: br i1 [[COND_2]], label [[FAILURE:%.*]], label [[LOOP]] ; CHECK: exit: ; CHECK-NEXT: ret i32 -1 @@ -145,13 +145,13 @@ define i32 @test_04(i32* %p, i64 %len, i32 %x) { ; CHECK-NEXT: br label [[LOOP:%.*]] ; CHECK: loop: ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[LEN:%.*]], [[ENTRY:%.*]] ] -; CHECK-NEXT: [[IV_NEXT]] = sub i64 [[IV]], 1 ; CHECK-NEXT: [[COND_1:%.*]] = icmp eq i64 [[IV]], 0 ; CHECK-NEXT: br i1 [[COND_1]], label [[EXIT:%.*]], label [[BACKEDGE]] ; CHECK: backedge: ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i32, i32* [[SCEVGEP]], i64 [[IV]] ; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, i32* [[SCEVGEP1]] unordered, align 4 ; CHECK-NEXT: [[COND_2:%.*]] = icmp eq i32 [[LOADED]], [[X:%.*]] +; CHECK-NEXT: [[IV_NEXT]] = sub i64 [[IV]], 1 ; CHECK-NEXT: br i1 [[COND_2]], label [[FAILURE:%.*]], label [[LOOP]] ; CHECK: exit: ; CHECK-NEXT: ret i32 -1