%r = add i8 %a, 2
ret i8 %r
}
+
+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]]
+;
+ %negx = sub i8 0, %x
+ %sel = select i1 %b, i8 0, i8 %negx
+ %add = add i8 %sel, %y
+ ret i8 %add
+}
+
+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> <i8 poison, i8 0>, [[X:%.*]]
+; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[B:%.*]], <2 x i8> <i8 poison, i8 0>, <2 x i8> [[NEGX]]
+; CHECK-NEXT: [[ADD:%.*]] = add <2 x i8> [[Y]], [[SEL]]
+; CHECK-NEXT: ret <2 x i8> [[ADD]]
+;
+ %y = mul <2 x i8> %p, %p ; thwart complexity-based canonicalization
+ %negx = sub <2 x i8> <i8 poison, i8 0>, %x
+ %sel = select <2 x i1> %b, <2 x i8> <i8 poison, i8 0>, <2 x i8> %negx
+ %add = add <2 x i8> %y, %sel
+ ret <2 x i8> %add
+}
+
+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]]
+;
+ %negx = sub i8 0, %x
+ call void @use(i8 %negx)
+ %sel = select i1 %b, i8 %negx, i8 0
+ %add = add i8 %sel, %y
+ ret i8 %add
+}
+
+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]]
+;
+ %y = mul i8 %p, %p ; thwart complexity-based canonicalization
+ %negx = sub i8 0, %x
+ %sel = select i1 %b, i8 %negx, i8 0
+ %add = add i8 %y, %sel
+ ret i8 %add
+}
+
+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:%.*]]
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i8 42, i8 [[NEGX]]
+; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SEL]], [[Y:%.*]]
+; CHECK-NEXT: ret i8 [[ADD]]
+;
+ %negx = sub i8 0, %x
+ %sel = select i1 %b, i8 42, i8 %negx
+ %add = add i8 %sel, %y
+ ret i8 %add
+}
+
+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:%.*]]
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i8 0, i8 [[NEGX]]
+; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SEL]], [[Y:%.*]]
+; CHECK-NEXT: ret i8 [[ADD]]
+;
+ %negx = sub i8 42, %x
+ %sel = select i1 %b, i8 0, i8 %negx
+ %add = add i8 %sel, %y
+ ret i8 %add
+}
+
+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:%.*]]
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i8 0, i8 [[NEGX]]
+; CHECK-NEXT: call void @use(i8 [[SEL]])
+; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SEL]], [[Y:%.*]]
+; CHECK-NEXT: ret i8 [[ADD]]
+;
+ %negx = sub i8 0, %x
+ %sel = select i1 %b, i8 0, i8 %negx
+ call void @use(i8 %sel)
+ %add = add i8 %sel, %y
+ ret i8 %add
+}