diff --git a/llvm/test/Transforms/InstCombine/binop-select.ll b/llvm/test/Transforms/InstCombine/binop-select.ll --- a/llvm/test/Transforms/InstCombine/binop-select.ll +++ b/llvm/test/Transforms/InstCombine/binop-select.ll @@ -321,3 +321,139 @@ %r = ashr i32 -2, %s ret i32 %r } + +define <2 x i32> @test_udiv_to_const_shr(i1 %c, <2 x i32> %x, <2 x i32> %yy) { +; CHECK-LABEL: @test_udiv_to_const_shr( +; CHECK-NEXT: [[Y:%.*]] = shl nuw <2 x i32> , [[YY:%.*]] +; CHECK-NEXT: [[COND:%.*]] = select i1 [[C:%.*]], <2 x i32> , <2 x i32> [[Y]] +; CHECK-NEXT: [[DIV:%.*]] = udiv <2 x i32> , [[COND]] +; CHECK-NEXT: ret <2 x i32> [[DIV]] +; + %y = shl <2 x i32> , %yy + %cond = select i1 %c, <2 x i32> , <2 x i32> %y + %div = udiv <2 x i32> , %cond + ret <2 x i32> %div +} + +define i32 @test_udiv_to_const_shr_fail_multiuse(i1 %c, i32 %x, i32 %yy) { +; CHECK-LABEL: @test_udiv_to_const_shr_fail_multiuse( +; CHECK-NEXT: [[Y:%.*]] = shl nuw i32 1, [[YY:%.*]] +; CHECK-NEXT: [[COND:%.*]] = select i1 [[C:%.*]], i32 7, i32 [[Y]] +; CHECK-NEXT: [[DIV:%.*]] = udiv i32 42, [[COND]] +; CHECK-NEXT: call void @use(i32 [[COND]]) +; CHECK-NEXT: ret i32 [[DIV]] +; + %y = shl i32 1, %yy + %cond = select i1 %c, i32 7, i32 %y + %div = udiv i32 42, %cond + call void @use(i32 %cond) + ret i32 %div +} + +define i32 @test_udiv_to_const_Cudiv(i32 %x) { +; CHECK-LABEL: @test_udiv_to_const_Cudiv( +; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 90 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[C]], i32 7, i32 19 +; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[X]], [[COND]] +; CHECK-NEXT: ret i32 [[DIV]] +; + %c = icmp eq i32 %x, 90 + %cond = select i1 %c, i32 7, i32 19 + %div = udiv i32 %x, %cond + ret i32 %div +} + +define i32 @test_udiv_to_const_Cudiv_fail_non_equality(i32 %x) { +; CHECK-LABEL: @test_udiv_to_const_Cudiv_fail_non_equality( +; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[X:%.*]], 91 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[C]], i32 7, i32 19 +; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[X]], [[COND]] +; CHECK-NEXT: ret i32 [[DIV]] +; + %c = icmp ule i32 %x, 90 + %cond = select i1 %c, i32 7, i32 19 + %div = udiv i32 %x, %cond + ret i32 %div +} + +define <2 x i32> @test_sdiv_to_const_Csdiv(<2 x i32> %x) { +; CHECK-LABEL: @test_sdiv_to_const_Csdiv( +; CHECK-NEXT: [[C_NOT:%.*]] = icmp eq <2 x i32> [[X:%.*]], +; CHECK-NEXT: [[COND:%.*]] = select <2 x i1> [[C_NOT]], <2 x i32> , <2 x i32> +; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[X]], [[COND]] +; CHECK-NEXT: ret <2 x i32> [[DIV]] +; + %c = icmp ne <2 x i32> %x, + %cond = select <2 x i1> %c, <2 x i32> , <2 x i32> + %div = sdiv <2 x i32> %x, %cond + ret <2 x i32> %div +} + +define i32 @test_srem_to_const_Csrem(i32 %x) { +; CHECK-LABEL: @test_srem_to_const_Csrem( +; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 24 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[C]], i32 7, i32 16 +; CHECK-NEXT: [[DIV:%.*]] = srem i32 [[X]], [[COND]] +; CHECK-NEXT: ret i32 [[DIV]] +; + %c = icmp eq i32 %x, 24 + %cond = select i1 %c, i32 7, i32 16 + %div = srem i32 %x, %cond + ret i32 %div +} + +define i32 @test_srem_to_const_Csrem_fail_multiuse(i32 %x) { +; CHECK-LABEL: @test_srem_to_const_Csrem_fail_multiuse( +; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 24 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[C]], i32 7, i32 16 +; CHECK-NEXT: [[DIV:%.*]] = srem i32 [[X]], [[COND]] +; CHECK-NEXT: call void @use(i32 [[COND]]) +; CHECK-NEXT: ret i32 [[DIV]] +; + %c = icmp eq i32 %x, 24 + %cond = select i1 %c, i32 7, i32 16 + %div = srem i32 %x, %cond + call void @use(i32 %cond) + ret i32 %div +} + +define i32 @test_urem_to_const_and_todo_need_to_accept_non_const_ops(i32 %x, i32 %yy) { +; CHECK-LABEL: @test_urem_to_const_and_todo_need_to_accept_non_const_ops( +; CHECK-NEXT: [[Y:%.*]] = shl nuw i32 1, [[YY:%.*]] +; CHECK-NEXT: [[C_NOT:%.*]] = icmp eq i32 [[X:%.*]], 24 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[C_NOT]], i32 19, i32 [[Y]] +; CHECK-NEXT: [[DIV:%.*]] = urem i32 [[X]], [[COND]] +; CHECK-NEXT: ret i32 [[DIV]] +; + %y = shl i32 1, %yy + %c = icmp ne i32 %x, 24 + %cond = select i1 %c, i32 %y, i32 19 + %div = urem i32 %x, %cond + ret i32 %div +} + +define i32 @test_urem_to_const_and(i1 %c, i32 %yy) { +; CHECK-LABEL: @test_urem_to_const_and( +; CHECK-NEXT: [[Y:%.*]] = shl nuw i32 1, [[YY:%.*]] +; CHECK-NEXT: [[COND:%.*]] = select i1 [[C:%.*]], i32 [[Y]], i32 19 +; CHECK-NEXT: [[DIV:%.*]] = urem i32 44, [[COND]] +; CHECK-NEXT: ret i32 [[DIV]] +; + %y = shl i32 1, %yy + %cond = select i1 %c, i32 %y, i32 19 + %div = urem i32 44, %cond + ret i32 %div +} + +define i32 @test_mul_to_const_Cmul(i32 %x) { +; CHECK-LABEL: @test_mul_to_const_Cmul( +; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 61 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[C]], i32 9, i32 14 +; CHECK-NEXT: [[DIV:%.*]] = mul i32 [[COND]], [[X]] +; CHECK-NEXT: ret i32 [[DIV]] +; + %c = icmp eq i32 %x, 61 + %cond = select i1 %c, i32 9, i32 14 + %div = mul i32 %x, %cond + ret i32 %div +}