Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp +++ lib/Transforms/InstCombine/InstructionCombining.cpp @@ -596,14 +596,21 @@ if (BinaryOperator *BO = dyn_cast(SimplifiedInst)) { if (isa(SimplifiedInst)) { bool HasNSW = false; - if (isa(&I)) + bool HasNUW = false; + if (isa(&I)) { HasNSW = I.hasNoSignedWrap(); + HasNUW = I.hasNoUnsignedWrap(); + } - if (auto *LOBO = dyn_cast(LHS)) + if (auto *LOBO = dyn_cast(LHS)) { HasNSW &= LOBO->hasNoSignedWrap(); + HasNUW &= LOBO->hasNoUnsignedWrap(); + } - if (auto *ROBO = dyn_cast(RHS)) + if (auto *ROBO = dyn_cast(RHS)) { HasNSW &= ROBO->hasNoSignedWrap(); + HasNUW &= ROBO->hasNoUnsignedWrap(); + } // We can propagate 'nsw' if we know that // %Y = mul nsw i16 %X, C @@ -615,8 +622,11 @@ const APInt *CInt; if (TopLevelOpcode == Instruction::Add && InnerOpcode == Instruction::Mul) - if (match(V, m_APInt(CInt)) && !CInt->isMinSignedValue()) - BO->setHasNoSignedWrap(HasNSW); + if (match(V, m_APInt(CInt))) { + if (!CInt->isMinSignedValue()) + BO->setHasNoSignedWrap(HasNSW); + BO->setHasNoUnsignedWrap(HasNUW); + } } } } Index: test/Transforms/InstCombine/reassociate-nuw.ll =================================================================== --- test/Transforms/InstCombine/reassociate-nuw.ll +++ test/Transforms/InstCombine/reassociate-nuw.ll @@ -92,7 +92,7 @@ define i32 @tryFactorization_add_nuw_mul_nuw(i32 %x) { ; CHECK-LABEL: @tryFactorization_add_nuw_mul_nuw( -; CHECK-NEXT: [[ADD2:%.*]] = shl i32 [[X:%.*]], 2 +; CHECK-NEXT: [[ADD2:%.*]] = shl nuw i32 [[X:%.*]], 2 ; CHECK-NEXT: ret i32 [[ADD2]] ; %mul1 = mul nuw i32 %x, 3 @@ -102,7 +102,7 @@ define i32 @tryFactorization_add_nuw_mul_nuw_int_max(i32 %x) { ; CHECK-LABEL: @tryFactorization_add_nuw_mul_nuw_int_max( -; CHECK-NEXT: [[ADD2:%.*]] = shl i32 [[X:%.*]], 31 +; CHECK-NEXT: [[ADD2:%.*]] = shl nuw i32 [[X:%.*]], 31 ; CHECK-NEXT: ret i32 [[ADD2]] ; %mul1 = mul nuw i32 %x, 2147483647