Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -3653,6 +3653,25 @@ } } + // umin(zext x, sext x) ==> zext x + // umax(zext x, sext x) ==> sext x + // TODO: generalize and combine with the above loop - may require a change to + // sorting order to be effective? + if (Ops.size() == 2 && !IsSigned) { + const SCEVSignExtendExpr *SExt = dyn_cast(Ops[0]); + const SCEVZeroExtendExpr *ZExt = dyn_cast(Ops[1]); + if (!SExt || !ZExt) { + SExt = dyn_cast(Ops[1]); + ZExt = dyn_cast(Ops[0]); + } + if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand()) { + // If operand >=s 0 then ZExt == SExt. If operand zext and umax(zext, sext) -> sext + return IsMax ? static_cast(SExt) : + static_cast(ZExt); + } + } + if (Ops.size() == 1) return Ops[0]; assert(!Ops.empty() && "Reduced smax down to nothing!"); Index: test/Analysis/ScalarEvolution/umin-umax-folds.ll =================================================================== --- test/Analysis/ScalarEvolution/umin-umax-folds.ll +++ test/Analysis/ScalarEvolution/umin-umax-folds.ll @@ -0,0 +1,107 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; RUN: opt -analyze -scalar-evolution < %s | FileCheck %s + +define void @umin_sext_x_zext_x(i32 %len) { +; CHECK-LABEL: 'umin_sext_x_zext_x' +; CHECK-NEXT: Classifying expressions for: @umin_sext_x_zext_x +; CHECK-NEXT: %len.zext = zext i32 %len to i64 +; CHECK-NEXT: --> (zext i32 %len to i64) U: [0,4294967296) S: [0,4294967296) +; CHECK-NEXT: %len.sext = sext i32 %len to i64 +; CHECK-NEXT: --> (sext i32 %len to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483648) +; CHECK-NEXT: %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ] +; CHECK-NEXT: --> {0,+,1}<%loop> U: [0,4294967296) S: [0,4294967296) Exits: (zext i32 %len to i64) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: %iv.next = add i64 %iv, 1 +; CHECK-NEXT: --> {1,+,1}<%loop> U: [1,4294967297) S: [1,4294967297) Exits: (1 + (zext i32 %len to i64)) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: %umin = select i1 %cmp1, i64 %len.zext, i64 %len.sext +; CHECK-NEXT: --> (zext i32 %len to i64) U: [0,4294967296) S: [0,4294967296) Exits: (zext i32 %len to i64) LoopDispositions: { %loop: Invariant } +; CHECK-NEXT: Determining loop execution counts for: @umin_sext_x_zext_x +; CHECK-NEXT: Loop %loop: backedge-taken count is (zext i32 %len to i64) +; CHECK-NEXT: Loop %loop: max backedge-taken count is 4294967295 +; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (zext i32 %len to i64) +; CHECK-NEXT: Predicates: +; CHECK: Loop %loop: Trip multiple is 1 +; +entry: + %len.zext = zext i32 %len to i64 + %len.sext = sext i32 %len to i64 + br label %loop +loop: + %iv = phi i64 [0, %entry], [%iv.next, %loop] + %iv.next = add i64 %iv, 1 + %cmp1 = icmp ult i64 %len.zext, %len.sext + %umin = select i1 %cmp1, i64 %len.zext, i64 %len.sext + %cmp2 = icmp ult i64 %iv, %umin + br i1 %cmp2, label %loop, label %exit +exit: + ret void +} + +define void @umax_sext_x_zext_x(i32 %len) { +; CHECK-LABEL: 'umax_sext_x_zext_x' +; CHECK-NEXT: Classifying expressions for: @umax_sext_x_zext_x +; CHECK-NEXT: %len.zext = zext i32 %len to i64 +; CHECK-NEXT: --> (zext i32 %len to i64) U: [0,4294967296) S: [0,4294967296) +; CHECK-NEXT: %len.sext = sext i32 %len to i64 +; CHECK-NEXT: --> (sext i32 %len to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483648) +; CHECK-NEXT: %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ] +; CHECK-NEXT: --> {0,+,1}<%loop> U: full-set S: full-set Exits: (sext i32 %len to i64) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: %iv.next = add i64 %iv, 1 +; CHECK-NEXT: --> {1,+,1}<%loop> U: full-set S: full-set Exits: (1 + (sext i32 %len to i64)) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: %umax = select i1 %cmp1, i64 %len.zext, i64 %len.sext +; CHECK-NEXT: --> (sext i32 %len to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483648) Exits: (sext i32 %len to i64) LoopDispositions: { %loop: Invariant } +; CHECK-NEXT: Determining loop execution counts for: @umax_sext_x_zext_x +; CHECK-NEXT: Loop %loop: backedge-taken count is (sext i32 %len to i64) +; CHECK-NEXT: Loop %loop: max backedge-taken count is -1 +; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (sext i32 %len to i64) +; CHECK-NEXT: Predicates: +; CHECK: Loop %loop: Trip multiple is 1 +; +entry: + %len.zext = zext i32 %len to i64 + %len.sext = sext i32 %len to i64 + br label %loop +loop: + %iv = phi i64 [0, %entry], [%iv.next, %loop] + %iv.next = add i64 %iv, 1 + %cmp1 = icmp ugt i64 %len.zext, %len.sext + %umax = select i1 %cmp1, i64 %len.zext, i64 %len.sext + %cmp2 = icmp ult i64 %iv, %umax + br i1 %cmp2, label %loop, label %exit +exit: + ret void +} + +define void @umin_sext_x_zext_x_2(i32 %len) { +; CHECK-LABEL: 'umin_sext_x_zext_x_2' +; CHECK-NEXT: Classifying expressions for: @umin_sext_x_zext_x_2 +; CHECK-NEXT: %len.zext = zext i32 %len to i64 +; CHECK-NEXT: --> (zext i32 %len to i64) U: [0,4294967296) S: [0,4294967296) +; CHECK-NEXT: %len.sext = sext i32 %len to i64 +; CHECK-NEXT: --> (sext i32 %len to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483648) +; CHECK-NEXT: %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ] +; CHECK-NEXT: --> {0,+,1}<%loop> U: [0,4294967296) S: [0,4294967296) Exits: (zext i32 %len to i64) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: %iv.next = add i64 %iv, 1 +; CHECK-NEXT: --> {1,+,1}<%loop> U: [1,4294967297) S: [1,4294967297) Exits: (1 + (zext i32 %len to i64)) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: %and = and i1 %cmp1, %cmp2 +; CHECK-NEXT: --> %and U: full-set S: full-set Exits: <> LoopDispositions: { %loop: Variant } +; CHECK-NEXT: Determining loop execution counts for: @umin_sext_x_zext_x_2 +; CHECK-NEXT: Loop %loop: backedge-taken count is (zext i32 %len to i64) +; CHECK-NEXT: Loop %loop: max backedge-taken count is 4294967295 +; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (zext i32 %len to i64) +; CHECK-NEXT: Predicates: +; CHECK: Loop %loop: Trip multiple is 1 +; +entry: + %len.zext = zext i32 %len to i64 + %len.sext = sext i32 %len to i64 + br label %loop +loop: + %iv = phi i64 [0, %entry], [%iv.next, %loop] + %iv.next = add i64 %iv, 1 + %cmp1 = icmp ult i64 %iv, %len.zext + %cmp2 = icmp ult i64 %iv, %len.sext + %and = and i1 %cmp1, %cmp2 + br i1 %and, label %loop, label %exit +exit: + ret void +}