diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -872,6 +872,28 @@ SimplifyQuery Q = SQ.getWithInstruction(&I); Value *Cond, *True = nullptr, *False = nullptr; + + // Special-case for add/negate combination. Replace the zero in the negation + // with the trailing add operand: + // (Cond ? TVal : -N) + Z --> Cond ? True : (Z - N) + // (Cond ? -N : FVal) + Z --> Cond ? (Z - N) : False + auto foldAddNegate = [&](Value *TVal, Value *FVal, Value *Z) -> Value * { + // We need an 'add' and exactly 1 arm of the select to have been simplified. + if (Opcode != Instruction::Add || (!True && !False) || (True && False)) + return nullptr; + + Value *N; + if (True && match(FVal, m_Neg(m_Value(N)))) { + Value *Sub = Builder.CreateSub(Z, N); + return Builder.CreateSelect(Cond, True, Sub, I.getName()); + } + if (False && match(TVal, m_Neg(m_Value(N)))) { + Value *Sub = Builder.CreateSub(Z, N); + return Builder.CreateSelect(Cond, Sub, False, I.getName()); + } + return nullptr; + }; + if (LHSIsSelect && RHSIsSelect && A == D) { // (A ? B : C) op (A ? E : F) -> A ? (B op E) : (C op F) Cond = A; @@ -889,11 +911,15 @@ Cond = A; True = simplifyBinOp(Opcode, B, RHS, FMF, Q); False = simplifyBinOp(Opcode, C, RHS, FMF, Q); + if (Value *NewSel = foldAddNegate(B, C, RHS)) + return NewSel; } else if (RHSIsSelect && RHS->hasOneUse()) { // X op (D ? E : F) -> D ? (X op E) : (X op F) Cond = D; True = simplifyBinOp(Opcode, LHS, E, FMF, Q); False = simplifyBinOp(Opcode, LHS, F, FMF, Q); + if (Value *NewSel = foldAddNegate(E, F, LHS)) + return NewSel; } if (!True || !False) diff --git a/llvm/test/Transforms/InstCombine/add.ll b/llvm/test/Transforms/InstCombine/add.ll --- a/llvm/test/Transforms/InstCombine/add.ll +++ b/llvm/test/Transforms/InstCombine/add.ll @@ -1963,10 +1963,9 @@ define i8 @select_negate_or_zero(i1 %b, i8 %x, i8 %y) { ; CHECK-LABEL: @select_negate_or_zero( -; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]] -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i8 0, i8 [[NEGX]] -; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SEL]], [[Y:%.*]] -; CHECK-NEXT: ret i8 [[ADD]] +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], i8 0, i8 [[X:%.*]] +; CHECK-NEXT: [[ADD1:%.*]] = sub i8 [[Y:%.*]], [[TMP1]] +; CHECK-NEXT: ret i8 [[ADD1]] ; %negx = sub i8 0, %x %sel = select i1 %b, i8 0, i8 %negx @@ -1974,13 +1973,14 @@ ret i8 %add } +; commuted add operands - same result + define <2 x i8> @select_negate_or_zero_commute(<2 x i1> %b, <2 x i8> %x, <2 x i8> %p) { ; CHECK-LABEL: @select_negate_or_zero_commute( ; CHECK-NEXT: [[Y:%.*]] = mul <2 x i8> [[P:%.*]], [[P]] -; CHECK-NEXT: [[NEGX:%.*]] = sub <2 x i8> , [[X:%.*]] -; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[B:%.*]], <2 x i8> , <2 x i8> [[NEGX]] -; CHECK-NEXT: [[ADD:%.*]] = add <2 x i8> [[Y]], [[SEL]] -; CHECK-NEXT: ret <2 x i8> [[ADD]] +; CHECK-NEXT: [[TMP1:%.*]] = select <2 x i1> [[B:%.*]], <2 x i8> zeroinitializer, <2 x i8> [[X:%.*]] +; CHECK-NEXT: [[ADD1:%.*]] = sub <2 x i8> [[Y]], [[TMP1]] +; CHECK-NEXT: ret <2 x i8> [[ADD1]] ; %y = mul <2 x i8> %p, %p ; thwart complexity-based canonicalization %negx = sub <2 x i8> , %x @@ -1989,13 +1989,15 @@ ret <2 x i8> %add } +; swapped select operands and extra use are ok + define i8 @select_negate_or_zero_swap(i1 %b, i8 %x, i8 %y) { ; CHECK-LABEL: @select_negate_or_zero_swap( ; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]] ; CHECK-NEXT: call void @use(i8 [[NEGX]]) -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i8 [[NEGX]], i8 0 -; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SEL]], [[Y:%.*]] -; CHECK-NEXT: ret i8 [[ADD]] +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], i8 [[X]], i8 0 +; CHECK-NEXT: [[ADD1:%.*]] = sub i8 [[Y:%.*]], [[TMP1]] +; CHECK-NEXT: ret i8 [[ADD1]] ; %negx = sub i8 0, %x call void @use(i8 %negx) @@ -2004,13 +2006,14 @@ ret i8 %add } +; commuted add operands - same result + define i8 @select_negate_or_zero_swap_commute(i1 %b, i8 %x, i8 %p) { ; CHECK-LABEL: @select_negate_or_zero_swap_commute( ; CHECK-NEXT: [[Y:%.*]] = mul i8 [[P:%.*]], [[P]] -; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]] -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i8 [[NEGX]], i8 0 -; CHECK-NEXT: [[ADD:%.*]] = add i8 [[Y]], [[SEL]] -; CHECK-NEXT: ret i8 [[ADD]] +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], i8 [[X:%.*]], i8 0 +; CHECK-NEXT: [[ADD1:%.*]] = sub i8 [[Y]], [[TMP1]] +; CHECK-NEXT: ret i8 [[ADD1]] ; %y = mul i8 %p, %p ; thwart complexity-based canonicalization %negx = sub i8 0, %x @@ -2019,6 +2022,8 @@ ret i8 %add } +; negative test - one arm of the select must simplify + define i8 @select_negate_or_nonzero(i1 %b, i8 %x, i8 %y) { ; CHECK-LABEL: @select_negate_or_nonzero( ; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]] @@ -2032,6 +2037,8 @@ ret i8 %add } +; negative test - must have a negate, not any subtract + define i8 @select_nonnegate_or_zero(i1 %b, i8 %x, i8 %y) { ; CHECK-LABEL: @select_nonnegate_or_zero( ; CHECK-NEXT: [[NEGX:%.*]] = sub i8 42, [[X:%.*]] @@ -2045,6 +2052,8 @@ ret i8 %add } +; negative test - don't create an extra instruction + define i8 @select_negate_or_nonzero_use(i1 %b, i8 %x, i8 %y) { ; CHECK-LABEL: @select_negate_or_nonzero_use( ; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]] @@ -2060,13 +2069,13 @@ ret i8 %add } +; extra reduction because y + ~y -> -1 + define i5 @select_negate_not(i1 %b, i5 %x, i5 %y) { ; CHECK-LABEL: @select_negate_not( -; CHECK-NEXT: [[NEGX:%.*]] = sub i5 0, [[X:%.*]] -; CHECK-NEXT: [[NOTY:%.*]] = xor i5 [[Y:%.*]], -1 -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i5 [[NOTY]], i5 [[NEGX]] -; CHECK-NEXT: [[ADD:%.*]] = add i5 [[SEL]], [[Y]] -; CHECK-NEXT: ret i5 [[ADD]] +; CHECK-NEXT: [[TMP1:%.*]] = sub i5 [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[ADD1:%.*]] = select i1 [[B:%.*]], i5 -1, i5 [[TMP1]] +; CHECK-NEXT: ret i5 [[ADD1]] ; %negx = sub i5 0, %x %noty = xor i5 %y, -1 @@ -2078,11 +2087,9 @@ define i5 @select_negate_not_commute(i1 %b, i5 %x, i5 %p) { ; CHECK-LABEL: @select_negate_not_commute( ; CHECK-NEXT: [[Y:%.*]] = mul i5 [[P:%.*]], [[P]] -; CHECK-NEXT: [[NEGX:%.*]] = sub i5 0, [[X:%.*]] -; CHECK-NEXT: [[NOTY:%.*]] = xor i5 [[Y]], -1 -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i5 [[NOTY]], i5 [[NEGX]] -; CHECK-NEXT: [[ADD:%.*]] = add i5 [[Y]], [[SEL]] -; CHECK-NEXT: ret i5 [[ADD]] +; CHECK-NEXT: [[TMP1:%.*]] = sub i5 [[Y]], [[X:%.*]] +; CHECK-NEXT: [[ADD1:%.*]] = select i1 [[B:%.*]], i5 -1, i5 [[TMP1]] +; CHECK-NEXT: ret i5 [[ADD1]] ; %y = mul i5 %p, %p ; thwart complexity-based canonicalization %negx = sub i5 0, %x @@ -2094,11 +2101,9 @@ define i5 @select_negate_not_swap(i1 %b, i5 %x, i5 %y) { ; CHECK-LABEL: @select_negate_not_swap( -; CHECK-NEXT: [[NEGX:%.*]] = sub i5 0, [[X:%.*]] -; CHECK-NEXT: [[NOTY:%.*]] = xor i5 [[Y:%.*]], -1 -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i5 [[NEGX]], i5 [[NOTY]] -; CHECK-NEXT: [[ADD:%.*]] = add i5 [[SEL]], [[Y]] -; CHECK-NEXT: ret i5 [[ADD]] +; CHECK-NEXT: [[TMP1:%.*]] = sub i5 [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[ADD1:%.*]] = select i1 [[B:%.*]], i5 [[TMP1]], i5 -1 +; CHECK-NEXT: ret i5 [[ADD1]] ; %negx = sub i5 0, %x %noty = xor i5 %y, -1 @@ -2110,11 +2115,9 @@ define i5 @select_negate_not_swap_commute(i1 %b, i5 %x, i5 %p) { ; CHECK-LABEL: @select_negate_not_swap_commute( ; CHECK-NEXT: [[Y:%.*]] = mul i5 [[P:%.*]], [[P]] -; CHECK-NEXT: [[NEGX:%.*]] = sub i5 0, [[X:%.*]] -; CHECK-NEXT: [[NOTY:%.*]] = xor i5 [[Y]], -1 -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i5 [[NEGX]], i5 [[NOTY]] -; CHECK-NEXT: [[ADD:%.*]] = add i5 [[Y]], [[SEL]] -; CHECK-NEXT: ret i5 [[ADD]] +; CHECK-NEXT: [[TMP1:%.*]] = sub i5 [[Y]], [[X:%.*]] +; CHECK-NEXT: [[ADD1:%.*]] = select i1 [[B:%.*]], i5 [[TMP1]], i5 -1 +; CHECK-NEXT: ret i5 [[ADD1]] ; %y = mul i5 %p, %p ; thwart complexity-based canonicalization %negx = sub i5 0, %x