diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 995d0b6a8db71c..dd4e48170f028d 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -622,7 +622,7 @@ Instruction *InstCombinerImpl::visitVAEndInst(VAEndInst &I) { return nullptr; } -static Instruction *canonicalizeConstantArg0ToArg1(CallInst &Call) { +static CallInst *canonicalizeConstantArg0ToArg1(CallInst &Call) { assert(Call.getNumArgOperands() > 1 && "Need at least 2 args to swap"); Value *Arg0 = Call.getArgOperand(0), *Arg1 = Call.getArgOperand(1); if (isa(Arg0) && !isa(Arg1)) { @@ -763,6 +763,11 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { } } + if (II->isCommutative()) { + if (CallInst *NewCall = canonicalizeConstantArg0ToArg1(CI)) + return NewCall; + } + Intrinsic::ID IID = II->getIntrinsicID(); switch (IID) { case Intrinsic::objectsize: @@ -905,8 +910,6 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { } case Intrinsic::uadd_with_overflow: case Intrinsic::sadd_with_overflow: { - if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) - return I; if (Instruction *I = foldIntrinsicWithOverflowCommon(II)) return I; @@ -934,10 +937,6 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { case Intrinsic::umul_with_overflow: case Intrinsic::smul_with_overflow: - if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) - return I; - LLVM_FALLTHROUGH; - case Intrinsic::usub_with_overflow: if (Instruction *I = foldIntrinsicWithOverflowCommon(II)) return I; @@ -968,9 +967,6 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { case Intrinsic::uadd_sat: case Intrinsic::sadd_sat: - if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) - return I; - LLVM_FALLTHROUGH; case Intrinsic::usub_sat: case Intrinsic::ssub_sat: { SaturatingInst *SI = cast(II); @@ -1051,8 +1047,6 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { case Intrinsic::maxnum: case Intrinsic::minimum: case Intrinsic::maximum: { - if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) - return I; Value *Arg0 = II->getArgOperand(0); Value *Arg1 = II->getArgOperand(1); Value *X, *Y; @@ -1161,9 +1155,6 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { LLVM_FALLTHROUGH; } case Intrinsic::fma: { - if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) - return I; - // fma fneg(x), fneg(y), z -> fma x, y, z Value *Src0 = II->getArgOperand(0); Value *Src1 = II->getArgOperand(1); diff --git a/llvm/test/Transforms/InstCombine/commutative-intrinsics.ll b/llvm/test/Transforms/InstCombine/commutative-intrinsics.ll index 92f85f51b44137..3622904fa07d7e 100644 --- a/llvm/test/Transforms/InstCombine/commutative-intrinsics.ll +++ b/llvm/test/Transforms/InstCombine/commutative-intrinsics.ll @@ -3,7 +3,7 @@ define i35 @smax(i35 %x) { ; CHECK-LABEL: @smax( -; CHECK-NEXT: [[R:%.*]] = call i35 @llvm.smax.i35(i35 42, i35 [[X:%.*]]) +; CHECK-NEXT: [[R:%.*]] = call i35 @llvm.smax.i35(i35 [[X:%.*]], i35 42) ; CHECK-NEXT: ret i35 [[R]] ; %r = call i35 @llvm.smax.i35(i35 42, i35 %x) @@ -12,7 +12,7 @@ define i35 @smax(i35 %x) { define i5 @smin(i5 %x) { ; CHECK-LABEL: @smin( -; CHECK-NEXT: [[R:%.*]] = call i5 @llvm.smin.i5(i5 10, i5 [[X:%.*]]) +; CHECK-NEXT: [[R:%.*]] = call i5 @llvm.smin.i5(i5 [[X:%.*]], i5 10) ; CHECK-NEXT: ret i5 [[R]] ; %r = call i5 @llvm.smin.i5(i5 42, i5 %x) @@ -21,7 +21,7 @@ define i5 @smin(i5 %x) { define <2 x i35> @umax(<2 x i35> %x) { ; CHECK-LABEL: @umax( -; CHECK-NEXT: [[R:%.*]] = call <2 x i35> @llvm.umax.v2i35(<2 x i35> , <2 x i35> [[X:%.*]]) +; CHECK-NEXT: [[R:%.*]] = call <2 x i35> @llvm.umax.v2i35(<2 x i35> [[X:%.*]], <2 x i35> ) ; CHECK-NEXT: ret <2 x i35> [[R]] ; %r = call <2 x i35> @llvm.umax.v2i35(<2 x i35> , <2 x i35> %x) @@ -30,7 +30,7 @@ define <2 x i35> @umax(<2 x i35> %x) { define <3 x i35> @umin(<3 x i35> %x) { ; CHECK-LABEL: @umin( -; CHECK-NEXT: [[R:%.*]] = call <3 x i35> @llvm.umin.v3i35(<3 x i35> , <3 x i35> [[X:%.*]]) +; CHECK-NEXT: [[R:%.*]] = call <3 x i35> @llvm.umin.v3i35(<3 x i35> [[X:%.*]], <3 x i35> ) ; CHECK-NEXT: ret <3 x i35> [[R]] ; %r = call <3 x i35> @llvm.umin.v3i35(<3 x i35> , <3 x i35> %x) @@ -39,7 +39,7 @@ define <3 x i35> @umin(<3 x i35> %x) { define i35 @smul_fix(i35 %x) { ; CHECK-LABEL: @smul_fix( -; CHECK-NEXT: [[R:%.*]] = call i35 @llvm.smul.fix.i35(i35 42, i35 [[X:%.*]], i32 2) +; CHECK-NEXT: [[R:%.*]] = call i35 @llvm.smul.fix.i35(i35 [[X:%.*]], i35 42, i32 2) ; CHECK-NEXT: ret i35 [[R]] ; %r = call i35 @llvm.smul.fix.i35(i35 42, i35 %x, i32 2) @@ -48,7 +48,7 @@ define i35 @smul_fix(i35 %x) { define i5 @umul_fix(i5 %x) { ; CHECK-LABEL: @umul_fix( -; CHECK-NEXT: [[R:%.*]] = call i5 @llvm.umul.fix.i5(i5 10, i5 [[X:%.*]], i32 3) +; CHECK-NEXT: [[R:%.*]] = call i5 @llvm.umul.fix.i5(i5 [[X:%.*]], i5 10, i32 3) ; CHECK-NEXT: ret i5 [[R]] ; %r = call i5 @llvm.umul.fix.i5(i5 42, i5 %x, i32 3) @@ -57,7 +57,7 @@ define i5 @umul_fix(i5 %x) { define <2 x i35> @smul_fix_sat(<2 x i35> %x) { ; CHECK-LABEL: @smul_fix_sat( -; CHECK-NEXT: [[R:%.*]] = call <2 x i35> @llvm.smul.fix.sat.v2i35(<2 x i35> , <2 x i35> [[X:%.*]], i32 4) +; CHECK-NEXT: [[R:%.*]] = call <2 x i35> @llvm.smul.fix.sat.v2i35(<2 x i35> [[X:%.*]], <2 x i35> , i32 4) ; CHECK-NEXT: ret <2 x i35> [[R]] ; %r = call <2 x i35> @llvm.smul.fix.sat.v2i35(<2 x i35> , <2 x i35> %x, i32 4) @@ -66,7 +66,7 @@ define <2 x i35> @smul_fix_sat(<2 x i35> %x) { define <3 x i35> @umul_fix_sat(<3 x i35> %x) { ; CHECK-LABEL: @umul_fix_sat( -; CHECK-NEXT: [[R:%.*]] = call <3 x i35> @llvm.umul.fix.sat.v3i35(<3 x i35> , <3 x i35> [[X:%.*]], i32 5) +; CHECK-NEXT: [[R:%.*]] = call <3 x i35> @llvm.umul.fix.sat.v3i35(<3 x i35> [[X:%.*]], <3 x i35> , i32 5) ; CHECK-NEXT: ret <3 x i35> [[R]] ; %r = call <3 x i35> @llvm.umul.fix.sat.v3i35(<3 x i35> , <3 x i35> %x, i32 5)