From 63b11ef6d40b719d555c1398ca05b1f477ecfe39 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 27 Mar 2014 17:49:27 +0000 Subject: [PATCH] InstCombine: Don't combine constants on unsigned icmps Fixes a miscompile introduced in r204912. It would miscompile code like (unsigned)(a + -49) <= 5U. The transform would turn this into (unsigned)a < 55U, which would return true for values in [0, 49], when it should not. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204948 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/InstCombine/InstCombineCompares.cpp | 3 ++- test/Transforms/InstCombine/icmp.ll | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index 9bf38189542..fea0d0245c8 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3010,7 +3010,8 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate()); ConstantInt *Cst2; - if (match(Op1, m_ConstantInt(Cst)) && + if (I.isSigned() && + match(Op1, m_ConstantInt(Cst)) && match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst2))) && cast(Op0)->hasNoSignedWrap()) { // icmp X+Cst2, Cst --> icmp X, Cst-Cst2 diff --git a/test/Transforms/InstCombine/icmp.ll b/test/Transforms/InstCombine/icmp.ll index 2e3ff24f6ba..63fefc08776 100644 --- a/test/Transforms/InstCombine/icmp.ll +++ b/test/Transforms/InstCombine/icmp.ll @@ -1409,3 +1409,13 @@ entry: %conv = zext i1 %cmp to i32 ret i32 %conv } + +; CHECK-LABEL: icmp_add_const_ult +; CHECK: %cmp = icmp ult i32 %add, 6 +define i32 @icmp_add_const_ult(i32 %a) #0 { +entry: + %add = add nsw i32 %a, -49 + %cmp = icmp ult i32 %add, 6 + %conv = zext i1 %cmp to i32 + ret i32 %conv +}