Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -5597,37 +5597,6 @@ } } -/// Check if the order of \p Op0 and \p Op1 as operands in an ICmpInst -/// should be swapped. -/// The decision is based on how many times these two operands are reused -/// as subtract operands and their positions in those instructions. -/// The rationale is that several architectures use the same instruction for -/// both subtract and cmp. Thus, it is better if the order of those operands -/// match. -/// TODO: Shouldn't this be part of CGP instead? -/// \return true if Op0 and Op1 should be swapped. -static bool swapMayExposeCSEOpportunities(const Value *Op0, const Value *Op1) { - // Filter out pointer values as those cannot appear directly in subtract. - // FIXME: we may want to go through inttoptrs or bitcasts. - if (Op0->getType()->isPointerTy() || isa(Op0)) - return false; - // If a subtract already has the same operands as a compare, swapping would be - // bad. If a subtract has the same operands as a compare but in reverse order, - // then swapping is good. - int GoodToSwap = 0; - unsigned NumInspected = 0; - for (const User *U : Op0->users()) { - // Avoid walking many users. - if (++NumInspected > 128) - return false; - if (match(U, m_Sub(m_Specific(Op1), m_Specific(Op0)))) - GoodToSwap++; - else if (match(U, m_Sub(m_Specific(Op0), m_Specific(Op1)))) - GoodToSwap--; - } - return GoodToSwap > 0; -} - /// Check that one use is in the same block as the definition and all /// other uses are in blocks dominated by a given block. /// @@ -6477,8 +6446,7 @@ /// Orders the operands of the compare so that they are listed from most /// complex to least complex. This puts constants before unary operators, /// before binary operators. - if (Op0Cplxity < Op1Cplxity || - (Op0Cplxity == Op1Cplxity && swapMayExposeCSEOpportunities(Op0, Op1))) { + if (Op0Cplxity < Op1Cplxity) { I.swapOperands(); std::swap(Op0, Op1); Changed = true; Index: llvm/test/Transforms/InstCombine/abs-intrinsic.ll =================================================================== --- llvm/test/Transforms/InstCombine/abs-intrinsic.ll +++ llvm/test/Transforms/InstCombine/abs-intrinsic.ll @@ -455,7 +455,7 @@ define i32 @sub_abs_lt(i32 %x, i32 %y) { ; CHECK-LABEL: @sub_abs_lt( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]] ; CHECK: cond.true: ; CHECK-NEXT: [[SUB_NEG:%.*]] = sub i32 [[Y]], [[X]] @@ -481,7 +481,7 @@ define i32 @sub_abs_lt_min_not_poison(i32 %x, i32 %y) { ; CHECK-LABEL: @sub_abs_lt_min_not_poison( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]] ; CHECK: cond.true: ; CHECK-NEXT: [[SUB_NEG:%.*]] = sub i32 [[Y]], [[X]] Index: llvm/test/Transforms/InstCombine/icmp.ll =================================================================== --- llvm/test/Transforms/InstCombine/icmp.ll +++ llvm/test/Transforms/InstCombine/icmp.ll @@ -2969,7 +2969,7 @@ ; CHECK-LABEL: @icmp_swap_operands_for_cse( ; CHECK-NEXT: entry: ; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X]], [[Y]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[Y]], [[X]] ; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]] ; CHECK: true: ; CHECK-NEXT: [[TMP0:%.*]] = and i32 [[SUB]], 1 @@ -3001,7 +3001,7 @@ define i1 @icmp_swap_operands_for_cse2(i32 %X, i32 %Y) { ; CHECK-LABEL: @icmp_swap_operands_for_cse2( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[Y:%.*]], [[X:%.*]] ; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]] ; CHECK: true: ; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[X]], [[Y]] Index: llvm/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll =================================================================== --- llvm/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll +++ llvm/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll @@ -20,11 +20,11 @@ ; CHECK-LABEL: @t0_noncanonical_ignoreme( ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ule i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[BASE]], [[OFFSET]] ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) -; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: ret i1 [[R]] ; %adjusted = sub i8 %base, %offset @@ -41,11 +41,11 @@ ; CHECK-LABEL: @t0_noncanonical_ignoreme_logical( ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ule i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[BASE]], [[OFFSET]] ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) -; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: ret i1 [[R]] ; %adjusted = sub i8 %base, %offset @@ -199,11 +199,11 @@ ; CHECK-LABEL: @t3_commutability0( ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ule i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[BASE]], [[OFFSET]] ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) -; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: ret i1 [[R]] ; %adjusted = sub i8 %base, %offset @@ -220,11 +220,11 @@ ; CHECK-LABEL: @t3_commutability0_logical( ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ule i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[BASE]], [[OFFSET]] ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) -; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: ret i1 [[R]] ; %adjusted = sub i8 %base, %offset @@ -281,7 +281,7 @@ ; CHECK-LABEL: @t5_commutability2( ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ule i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[BASE]], [[OFFSET]] ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) @@ -302,7 +302,7 @@ ; CHECK-LABEL: @t5_commutability2_logical( ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ule i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]]) ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[BASE]], [[OFFSET]] ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]]) @@ -509,11 +509,11 @@ ; CHECK-LABEL: @t9_commutative( ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[UNDERFLOW:%.*]] = icmp ult i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[UNDERFLOW:%.*]] = icmp ugt i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: call void @use1(i1 [[UNDERFLOW]]) ; CHECK-NEXT: [[NULL:%.*]] = icmp eq i8 [[BASE]], [[OFFSET]] ; CHECK-NEXT: call void @use1(i1 [[NULL]]) -; CHECK-NEXT: [[R:%.*]] = icmp ule i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: ret i1 [[R]] ; %adjusted = sub i8 %base, %offset @@ -530,11 +530,11 @@ ; CHECK-LABEL: @t9_commutative_logical( ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[UNDERFLOW:%.*]] = icmp ult i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[UNDERFLOW:%.*]] = icmp ugt i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: call void @use1(i1 [[UNDERFLOW]]) ; CHECK-NEXT: [[NULL:%.*]] = icmp eq i8 [[BASE]], [[OFFSET]] ; CHECK-NEXT: call void @use1(i1 [[NULL]]) -; CHECK-NEXT: [[R:%.*]] = icmp ule i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: ret i1 [[R]] ; %adjusted = sub i8 %base, %offset Index: llvm/test/Transforms/InstCombine/strict-sub-underflow-check-to-comparison-of-sub-operands.ll =================================================================== --- llvm/test/Transforms/InstCombine/strict-sub-underflow-check-to-comparison-of-sub-operands.ll +++ llvm/test/Transforms/InstCombine/strict-sub-underflow-check-to-comparison-of-sub-operands.ll @@ -11,7 +11,7 @@ ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]]) ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[RES:%.*]] = icmp uge i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[RES:%.*]] = icmp ule i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: ret i1 [[RES]] ; %cmp = icmp slt i8 %offset, 0 @@ -28,7 +28,7 @@ ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]]) ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]] ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]]) -; CHECK-NEXT: [[RES:%.*]] = icmp ult i8 [[BASE]], [[OFFSET]] +; CHECK-NEXT: [[RES:%.*]] = icmp ugt i8 [[OFFSET]], [[BASE]] ; CHECK-NEXT: ret i1 [[RES]] ; %cmp = icmp slt i8 %offset, 0 Index: llvm/test/Transforms/InstCombine/sub-ashr-or-to-icmp-select.ll =================================================================== --- llvm/test/Transforms/InstCombine/sub-ashr-or-to-icmp-select.ll +++ llvm/test/Transforms/InstCombine/sub-ashr-or-to-icmp-select.ll @@ -195,7 +195,7 @@ ; CHECK-LABEL: @sub_ashr_or_i32_extra_use_sub( ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]] ; CHECK-NEXT: store i32 [[SUB]], ptr [[P:%.*]], align 4 -; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[Y]], [[X]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X]], [[Y]] ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]] ; CHECK-NEXT: ret i32 [[OR]] ; Index: llvm/test/Transforms/InstCombine/unsigned-sub-lack-of-overflow-check.ll =================================================================== --- llvm/test/Transforms/InstCombine/unsigned-sub-lack-of-overflow-check.ll +++ llvm/test/Transforms/InstCombine/unsigned-sub-lack-of-overflow-check.ll @@ -46,7 +46,7 @@ ; CHECK-LABEL: @t3_extrause0( ; CHECK-NEXT: [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: call void @use8(i8 [[T0]]) -; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[X]], [[Y]] +; CHECK-NEXT: [[R:%.*]] = icmp ule i8 [[Y]], [[X]] ; CHECK-NEXT: ret i1 [[R]] ; %t0 = sub i8 %x, %y Index: llvm/test/Transforms/InstCombine/unsigned-sub-overflow-check.ll =================================================================== --- llvm/test/Transforms/InstCombine/unsigned-sub-overflow-check.ll +++ llvm/test/Transforms/InstCombine/unsigned-sub-overflow-check.ll @@ -46,7 +46,7 @@ ; CHECK-LABEL: @t3_extrause0( ; CHECK-NEXT: [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: call void @use8(i8 [[T0]]) -; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[X]], [[Y]] +; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[Y]], [[X]] ; CHECK-NEXT: ret i1 [[R]] ; %t0 = sub i8 %x, %y