Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3901,11 +3901,11 @@ APInt AP2Abs = C2->getValue().abs(); if (AP1Abs.uge(AP2Abs)) { ConstantInt *C3 = Builder.getInt(AP1 - AP2); - Value *NewAdd = Builder.CreateNSWAdd(A, C3); + Value *NewAdd = Builder.CreateAdd(A, C3,"",BO0->hasNoUnsignedWrap(),BO0->hasNoSignedWrap()); return new ICmpInst(Pred, NewAdd, C); } else { ConstantInt *C3 = Builder.getInt(AP2 - AP1); - Value *NewAdd = Builder.CreateNSWAdd(C, C3); + Value *NewAdd = Builder.CreateAdd(C, C3,"",BO1->hasNoUnsignedWrap(),BO1->hasNoSignedWrap()); return new ICmpInst(Pred, A, NewAdd); } } Index: llvm/test/Transforms/InstCombine/icmp-emit-nsw-nuw.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/InstCombine/icmp-emit-nsw-nuw.ll @@ -0,0 +1,78 @@ +; RUN: opt -instcombine -S < %s | FileCheck %s + +declare void @use_1(i1 ) +declare void @use_8(i8 ) + +define void @test-emit-without-nsw-nuw(i8 %x, i8 %y) { +; CHECK-LABEL: @test-emit-without-nsw-nuw( +; CHECK: %1 = add +; CHECK-NOT: nsw +; CHECK-NOT: nuw +; CHECK-SAME: i8 %x, 2 +; CHECK-NEXT: %tobool = icmp eq i8 %1, %y +%1=add i8 %x, 37 +%2=add i8 %y, 35 +%tobool=icmp eq i8 %2, %1 +call void @use_1(i1 %tobool) +ret void +} + +define void @test-emit-with-nsw-nuw(i8 %x, i8 %y) { +; CHECK-LABEL: @test-emit-with-nsw-nuw( +; CHECK: %1 = add nuw nsw i8 %x, 2 +; CHECK-NEXT: %tobool = icmp eq i8 %1, %y +%1=add nsw nuw i8 %x, 37 +%2=add i8 %y, 35 +%tobool=icmp eq i8 %2, %1 +call void @use_1(i1 %tobool) +ret void +} + +define void @test-emit-with-nsw-large(i8 %x, i8 %y) { +; CHECK-LABEL: @test-emit-with-nsw-large( +; CHECK: %1 = add nsw i8 %x, 2 +; CHECK-NEXT: %tobool = icmp eq i8 %1, %y +%1=add nsw i8 %x, 37 +%2=add i8 %y, 35 +%tobool=icmp eq i8 %2, %1 +call void @use_1(i1 %tobool) +ret void +} + +define void @test-emit-with-nsw-small(i8 %x, i8 %y) { +; CHECK-LABEL: @test-emit-with-nsw-small( +; CHECK: %1 = add +; CHECK-NOT: nsw +; CHECK-SAME: i8 %y, 2 +; CHECK-NEXT: %tobool = icmp eq i8 %1, %x +%1=add nsw i8 %x, 35 +%2=add i8 %y, 37 +%tobool=icmp eq i8 %2, %1 +call void @use_1(i1 %tobool) +ret void +} + +define void @test-emit-with-nuw-large(i8 %x, i8 %y) { +; CHECK-LABEL: @test-emit-with-nuw-large( +; CHECK: %1 = add nuw i8 %x, 2 +; CHECK-NEXT: %tobool = icmp eq i8 %1, %y +%1=add nuw i8 %x, 37 +%2=add i8 %y, 35 +%tobool=icmp eq i8 %2, %1 +call void @use_1(i1 %tobool) +ret void +} + +define void @test-emit-with-nuw-small(i8 %x, i8 %y) { +; CHECK-LABEL: @test-emit-with-nuw-small( +; CHECK: %1 = add +; CHECK-NOT: nuw +; CHECK-SAME: i8 %y, 2 +; CHECK-NEXT: %tobool = icmp eq i8 %1, %x +%1=add nuw i8 %x, 35 +%2=add i8 %y, 37 +%tobool=icmp eq i8 %2, %1 +call void @use_1(i1 %tobool) +ret void +} +