[InstCombine] restrict no-wrap propagation for i1/i2 to avoid miscompiles

This transform was added with 68c197f07e,
and the post-commit review noted the potential
for miscompiles at narrow bitwidths.

I'm not sure how to expose the i1 nuw bug because we
already simplify that, but other cases show that
there are missing transforms to add in follow-up
patches.
This commit is contained in:
Sanjay Patel committed 2023-01-18 10:32:12 -05:00
1 parent d35c7e413c
commit 1720ec6da0
2 files changed
+7 -6

No files matched your search

@@ -2379,9 +2379,9 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
auto *OBO0 = cast<OverflowingBinaryOperator>(Op0);
auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
bool PropagateNSW = I.hasNoSignedWrap() && OBO0->hasNoSignedWrap() &&
OBO1->hasNoSignedWrap();
OBO1->hasNoSignedWrap() && BitWidth > 2;
bool PropagateNUW = I.hasNoUnsignedWrap() && OBO0->hasNoUnsignedWrap() &&
OBO1->hasNoUnsignedWrap();
OBO1->hasNoUnsignedWrap() && BitWidth > 1;
Value *Add = Builder.CreateAdd(X, Y, "add", PropagateNUW, PropagateNSW);
Value *Sub = Builder.CreateSub(X, Y, "sub", PropagateNUW, PropagateNSW);
Value *Mul = Builder.CreateMul(Add, Sub, "", PropagateNUW, PropagateNSW);
+5 -4
View File
@@ -2489,13 +2489,14 @@ define i1 @diff_of_squares_nuw_i1(i1 %x, i1 %y) {
ret i1 %r
}
; FIXME: It is not correct to propagate nsw.
; It is not correct to propagate nsw.
; TODO: This should reduce more.
define i2 @diff_of_squares_nsw_i2(i2 %x, i2 %y) {
; CHECK-LABEL: @diff_of_squares_nsw_i2(
; CHECK-NEXT: [[ADD:%.*]] = add nsw i2 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i2 [[X]], [[Y]]
; CHECK-NEXT: [[R:%.*]] = mul nsw i2 [[ADD]], [[SUB]]
; CHECK-NEXT: [[ADD:%.*]] = add i2 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[SUB:%.*]] = sub i2 [[X]], [[Y]]
; CHECK-NEXT: [[R:%.*]] = mul i2 [[ADD]], [[SUB]]
; CHECK-NEXT: ret i2 [[R]]
;
%x2 = mul nsw i2 %x, %x