llvm.org GIT mirror llvm / 9026ca1
[SCEV] Teach isLoopBackedgeGuardedByCond to exploit trip counts. Summary: If the trip count of a specific backedge is `N`, then we know that backedge is effectively guarded by the condition `{0,+,1} u< N`. This change teaches SCEV to use this condition to prove things in `isLoopBackedgeGuardedByCond`. Depends on D12948 Depends on D12949 Reviewers: atrick, reames, majnemer, hfinkel Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D12950 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@248608 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 5 years ago
3 changed file(s) with 55 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
69936993
69946994 SaveAndRestore ClearOnExit(WalkingBEDominatingConds, true);
69956995
6996 // See if we can exploit a trip count to prove the predicate.
6997 const auto &BETakenInfo = getBackedgeTakenInfo(L);
6998 const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this);
6999 if (LatchBECount != getCouldNotCompute()) {
7000 // We know that Latch branches back to the loop header exactly
7001 // LatchBECount times. This means the backdege condition at Latch is
7002 // equivalent to "{0,+,1} u< LatchBECount".
7003 Type *Ty = LatchBECount->getType();
7004 auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW);
7005 const SCEV *LoopCounter =
7006 getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags);
7007 if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter,
7008 LatchBECount))
7009 return true;
7010 }
7011
69967012 // Check conditions due to any @llvm.assume intrinsics.
69977013 for (auto &AssumeVH : AC.assumptions()) {
69987014 if (!AssumeVH)
99
1010 ; This cast shouldn't be folded into the addrec.
1111 ; CHECK: %tmp = zext i8 %l_95.0.i1 to i16
12 ; CHECK: --> (zext i8 {0,+,-1}<%bb.i> to i16){{ U: [^ ]+ S: [^ ]+}}{{ *}}Exits: 2
12 ; CHECK: --> (zext i8 {0,+,-1}<nw><%bb.i> to i16){{ U: [^ ]+ S: [^ ]+}}{{ *}}Exits: 2
1313
1414 %tmp = zext i8 %l_95.0.i1 to i16
1515
355355 ret void
356356 }
357357
358 define i1 @func_18(i16* %tmp20, i32* %len.addr) {
359 ; CHECK-LABEL: @func_18(
360 entry:
361 %len = load i32, i32* %len.addr, !range !0
362 %tmp18 = icmp eq i32 %len, 0
363 br i1 %tmp18, label %bb2, label %bb0.preheader
364
365 bb0.preheader:
366 br label %bb0
367
368 bb0:
369 ; CHECK: bb0:
370 %var_0.in = phi i32 [ %var_0, %bb1 ], [ %len, %bb0.preheader ]
371 %var_1 = phi i32 [ %tmp30, %bb1 ], [ 0, %bb0.preheader ]
372 %var_0 = add nsw i32 %var_0.in, -1
373 %tmp23 = icmp ult i32 %var_1, %len
374 ; CHECK: br i1 true, label %stay, label %bb2.loopexit
375 br i1 %tmp23, label %stay, label %bb2
376
377 stay:
378 ; CHECK: stay:
379 %tmp25 = getelementptr inbounds i16, i16* %tmp20, i32 %var_1
380 %tmp26 = load i16, i16* %tmp25
381 %tmp29 = icmp eq i16 %tmp26, 0
382 br i1 %tmp29, label %bb1, label %bb2
383
384 bb1:
385 %tmp30 = add i32 %var_1, 1
386 %tmp31 = icmp eq i32 %var_0, 0
387 br i1 %tmp31, label %bb3, label %bb0
388
389 bb2:
390 ret i1 false
391
392 bb3:
393 ret i1 true
394 }
395
358396 !0 = !{i32 0, i32 2147483647}
359