Skip to content

Commit 85de949

Browse files
committed
LICM: teach hoistMinMax about samesign
Follow up on 4a0d53a (PatternMatch: migrate to CmpPredicate) to get rid of one of the FIXMEs it introduced by replacing a predicate comparison with CmpPredicate::getMatching.
1 parent 798e9c7 commit 85de949

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,16 +2453,16 @@ static bool hoistMinMax(Instruction &I, Loop &L, ICFLoopSafetyInfo &SafetyInfo,
24532453
if (!MatchICmpAgainstInvariant(Cond1, P1, LHS1, RHS1) ||
24542454
!MatchICmpAgainstInvariant(Cond2, P2, LHS2, RHS2))
24552455
return false;
2456-
// FIXME: Use CmpPredicate::getMatching here.
2457-
if (P1 != static_cast<CmpInst::Predicate>(P2) || LHS1 != LHS2)
2456+
auto MatchingPred = CmpPredicate::getMatching(P1, P2);
2457+
if (!MatchingPred || LHS1 != LHS2)
24582458
return false;
24592459

24602460
// Everything is fine, we can do the transform.
24612461
bool UseMin = ICmpInst::isLT(P1) || ICmpInst::isLE(P1);
24622462
assert(
24632463
(UseMin || ICmpInst::isGT(P1) || ICmpInst::isGE(P1)) &&
24642464
"Relational predicate is either less (or equal) or greater (or equal)!");
2465-
Intrinsic::ID id = ICmpInst::isSigned(P1)
2465+
Intrinsic::ID id = ICmpInst::isSigned(*MatchingPred)
24662466
? (UseMin ? Intrinsic::smin : Intrinsic::smax)
24672467
: (UseMin ? Intrinsic::umin : Intrinsic::umax);
24682468
auto *Preheader = L.getLoopPreheader();
@@ -2479,7 +2479,7 @@ static bool hoistMinMax(Instruction &I, Loop &L, ICFLoopSafetyInfo &SafetyInfo,
24792479
(ICmpInst::isSigned(P1) ? "s" : "u") +
24802480
(UseMin ? "min" : "max"));
24812481
Builder.SetInsertPoint(&I);
2482-
ICmpInst::Predicate P = P1;
2482+
ICmpInst::Predicate P = *MatchingPred;
24832483
if (Inverse)
24842484
P = ICmpInst::getInversePredicate(P);
24852485
Value *NewCond = Builder.CreateICmp(P, LHS1, NewRHS);

llvm/test/Transforms/LICM/min_max.ll

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,11 @@ exit:
245245
define i32 @test_sgt_samesign(i32 %start, i32 %inv_1, i32 %inv_2) {
246246
; CHECK-LABEL: @test_sgt_samesign(
247247
; CHECK-NEXT: entry:
248+
; CHECK-NEXT: [[INVARIANT_UMAX:%.*]] = call i32 @llvm.smax.i32(i32 [[INV_1:%.*]], i32 [[INV_2:%.*]])
248249
; CHECK-NEXT: br label [[LOOP:%.*]]
249250
; CHECK: loop:
250251
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
251-
; CHECK-NEXT: [[CMP_1:%.*]] = icmp samesign ugt i32 [[IV]], [[INV_1:%.*]]
252-
; CHECK-NEXT: [[CMP_2:%.*]] = icmp sgt i32 [[IV]], [[INV_2:%.*]]
253-
; CHECK-NEXT: [[LOOP_COND:%.*]] = and i1 [[CMP_1]], [[CMP_2]]
252+
; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp sgt i32 [[IV]], [[INVARIANT_UMAX]]
254253
; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
255254
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
256255
; CHECK: exit:
@@ -305,14 +304,13 @@ exit:
305304
define i32 @test_sge_samesign(i32 %start, i32 %inv_1, i32 %inv_2) {
306305
; CHECK-LABEL: @test_sge_samesign(
307306
; CHECK-NEXT: entry:
307+
; CHECK-NEXT: [[INV_1:%.*]] = call i32 @llvm.smax.i32(i32 [[INV_3:%.*]], i32 [[INV_2:%.*]])
308308
; CHECK-NEXT: br label [[LOOP:%.*]]
309309
; CHECK: loop:
310310
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
311-
; CHECK-NEXT: [[CMP_1:%.*]] = icmp sge i32 [[IV]], [[INV_1:%.*]]
312-
; CHECK-NEXT: [[CMP_2:%.*]] = icmp samesign uge i32 [[IV]], [[INV_2:%.*]]
313-
; CHECK-NEXT: [[LOOP_COND:%.*]] = and i1 [[CMP_1]], [[CMP_2]]
311+
; CHECK-NEXT: [[CMP_1:%.*]] = icmp sge i32 [[IV]], [[INV_1]]
314312
; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
315-
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
313+
; CHECK-NEXT: br i1 [[CMP_1]], label [[LOOP]], label [[EXIT:%.*]]
316314
; CHECK: exit:
317315
; CHECK-NEXT: [[IV_LCSSA:%.*]] = phi i32 [ [[IV]], [[LOOP]] ]
318316
; CHECK-NEXT: ret i32 [[IV_LCSSA]]

0 commit comments

Comments
 (0)