diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1920,8 +1920,10 @@ // urem Op0, (sext i1 X) --> (Op0 == -1) ? 0 : Op0 Value *X; if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { - Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty)); - return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Op0); + Value *FrozenOp0 = Builder.CreateFreeze(Op0, Op0->getName() + ".frozen"); + Value *Cmp = + Builder.CreateICmpEQ(FrozenOp0, ConstantInt::getAllOnesValue(Ty)); + return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), FrozenOp0); } // For "(X + 1) % Op1" and if (X u< Op1) => (X + 1) == Op1 ? 0 : X + 1 . diff --git a/llvm/test/Transforms/InstCombine/rem.ll b/llvm/test/Transforms/InstCombine/rem.ll --- a/llvm/test/Transforms/InstCombine/rem.ll +++ b/llvm/test/Transforms/InstCombine/rem.ll @@ -66,8 +66,9 @@ define i8 @urem_with_sext_bool_divisor(i1 %x, i8 %y) { ; CHECK-LABEL: @urem_with_sext_bool_divisor( -; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[Y:%.*]], -1 -; CHECK-NEXT: [[REM:%.*]] = select i1 [[TMP1]], i8 0, i8 [[Y]] +; CHECK-NEXT: [[Y_FROZEN:%.*]] = freeze i8 [[Y:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[Y_FROZEN]], -1 +; CHECK-NEXT: [[REM:%.*]] = select i1 [[TMP1]], i8 0, i8 [[Y_FROZEN]] ; CHECK-NEXT: ret i8 [[REM]] ; %s = sext i1 %x to i8 @@ -77,8 +78,9 @@ define <2 x i8> @urem_with_sext_bool_divisor_vec(<2 x i1> %x, <2 x i8> %y) { ; CHECK-LABEL: @urem_with_sext_bool_divisor_vec( -; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[Y:%.*]], -; CHECK-NEXT: [[REM:%.*]] = select <2 x i1> [[TMP1]], <2 x i8> zeroinitializer, <2 x i8> [[Y]] +; CHECK-NEXT: [[Y_FROZEN:%.*]] = freeze <2 x i8> [[Y:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[Y_FROZEN]], +; CHECK-NEXT: [[REM:%.*]] = select <2 x i1> [[TMP1]], <2 x i8> zeroinitializer, <2 x i8> [[Y_FROZEN]] ; CHECK-NEXT: ret <2 x i8> [[REM]] ; %s = sext <2 x i1> %x to <2 x i8> @@ -1020,3 +1022,16 @@ %r = urem i32 %x, %s ret i32 %r } + +; https://alive2.llvm.org/ce/z/bh2KHm +define <2 x i32> @PR62401(<2 x i1> %x, <2 x i32> %y) { +; CHECK-LABEL: @PR62401( +; CHECK-NEXT: [[Y_FROZEN:%.*]] = freeze <2 x i32> [[Y:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i32> [[Y_FROZEN]], +; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> zeroinitializer, <2 x i32> [[Y_FROZEN]] +; CHECK-NEXT: ret <2 x i32> [[R]] +; + %sext.i1 = sext <2 x i1> %x to <2 x i32> + %r = urem <2 x i32> %y, %sext.i1 + ret <2 x i32> %r +} diff --git a/llvm/test/Transforms/InstCombine/shift.ll b/llvm/test/Transforms/InstCombine/shift.ll --- a/llvm/test/Transforms/InstCombine/shift.ll +++ b/llvm/test/Transforms/InstCombine/shift.ll @@ -1713,12 +1713,13 @@ ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i64 -1, i64 -2 ; CHECK-NEXT: [[G11:%.*]] = getelementptr i177, ptr [[A]], i64 [[TMP2]] ; CHECK-NEXT: [[L7:%.*]] = load i177, ptr [[G11]], align 4 -; CHECK-NEXT: [[C171:%.*]] = icmp slt i177 [[L7]], 0 -; CHECK-NEXT: [[C17:%.*]] = select i1 [[TMP1]], i1 [[C171]], i1 false +; CHECK-NEXT: [[L7_FROZEN:%.*]] = freeze i177 [[L7]] +; CHECK-NEXT: [[C171:%.*]] = icmp slt i177 [[L7_FROZEN]], 0 +; CHECK-NEXT: [[C17:%.*]] = and i1 [[TMP1]], [[C171]] ; CHECK-NEXT: [[TMP3:%.*]] = sext i1 [[C17]] to i64 ; CHECK-NEXT: [[G62:%.*]] = getelementptr i177, ptr [[G11]], i64 [[TMP3]] -; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i177 [[L7]], -1 -; CHECK-NEXT: [[B28:%.*]] = select i1 [[TMP4]], i177 0, i177 [[L7]] +; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i177 [[L7_FROZEN]], -1 +; CHECK-NEXT: [[B28:%.*]] = select i1 [[TMP4]], i177 0, i177 [[L7_FROZEN]] ; CHECK-NEXT: store i177 [[B28]], ptr [[G62]], align 4 ; CHECK-NEXT: ret void ;