From 2f6c3f7e20d048decd8c61831be9ce0c7c7557cc Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Sun, 29 May 2016 00:31:18 +0000 Subject: [PATCH] [ValueTracking] ICmp instructions propagate poison This is a stripped down version of D19211, leaving out the questionable "branching in poison is UB" bit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271150 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ValueTracking.cpp | 5 ++++ .../ScalarEvolution/flags-from-poison.ll | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index 8a0695c325d..58f6cd187dd 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -3477,6 +3477,11 @@ bool llvm::propagatesFullPoison(const Instruction *I) { return false; } + case Instruction::ICmp: + // Comparing poison with any value yields poison. This is why, for + // instance, x s< (x +nsw 1) can be folded to true. + return true; + case Instruction::GetElementPtr: // A GEP implicitly represents a sequence of additions, subtractions, // truncations, sign extensions and multiplications. The multiplications diff --git a/test/Analysis/ScalarEvolution/flags-from-poison.ll b/test/Analysis/ScalarEvolution/flags-from-poison.ll index a9d55f1b3db..2fcb4c038f2 100644 --- a/test/Analysis/ScalarEvolution/flags-from-poison.ll +++ b/test/Analysis/ScalarEvolution/flags-from-poison.ll @@ -57,6 +57,30 @@ exit: ret void } +define void @test-add-nuw-from-icmp(float* %input, i32 %offset, + i32 %numIterations) { +; CHECK-LABEL: @test-add-nuw-from-icmp +entry: + br label %loop +loop: + %i = phi i32 [ %nexti, %loop ], [ 0, %entry ] + +; CHECK: %index32 = +; CHECK: --> {%offset,+,1} + %index32 = add nuw i32 %i, %offset + %cmp = icmp sgt i32 %index32, 0 + %cmp.idx = sext i1 %cmp to i32 + + %ptr = getelementptr inbounds float, float* %input, i32 %cmp.idx + %nexti = add nuw i32 %i, 1 + %f = load float, float* %ptr, align 4 + %exitcond = icmp eq i32 %nexti, %numIterations + br i1 %exitcond, label %exit, label %loop + +exit: + ret void +} + ; With no load to trigger UB from poison, we cannot infer nsw. define void @test-add-no-load(float* %input, i32 %offset, i32 %numIterations) { ; CHECK-LABEL: @test-add-no-load