From 668c2aa51740da56f26a7488348362916fc77b2c Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Thu, 27 Mar 2014 11:16:05 +0000 Subject: [PATCH] InstCombine: merge constants in both operands of icmp. Transform: icmp X+Cst2, Cst into: icmp X, Cst-Cst2 when Cst-Cst2 does not overflow, and the add has nsw. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204912 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/README.txt | 12 ----- .../InstCombine/InstCombineCompares.cpp | 13 +++++ test/Transforms/InstCombine/icmp.ll | 53 +++++++++++++++++++ 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/lib/Target/README.txt b/lib/Target/README.txt index a9aab86abda..4ecacf9d010 100644 --- a/lib/Target/README.txt +++ b/lib/Target/README.txt @@ -930,18 +930,6 @@ optimized with "clang -emit-llvm-bc | opt -std-compile-opts". //===---------------------------------------------------------------------===// -int g(int x) { return (x - 10) < 0; } -Should combine to "x <= 9" (the sub has nsw). Currently not -optimized with "clang -emit-llvm-bc | opt -std-compile-opts". - -//===---------------------------------------------------------------------===// - -int g(int x) { return (x + 10) < 0; } -Should combine to "x < -10" (the add has nsw). Currently not -optimized with "clang -emit-llvm-bc | opt -std-compile-opts". - -//===---------------------------------------------------------------------===// - int f(int i, int j) { return i < j + 1; } int g(int i, int j) { return j > i - 1; } Should combine to "i <= j" (the add/sub has nsw). Currently not diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index 8c0ad525980..9bf38189542 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3008,6 +3008,19 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // icmp X, X+Cst if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X) return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate()); + + ConstantInt *Cst2; + if (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 + // iff Cst-Cst2 does not overflow + bool Overflow; + APInt NewCst = Cst->getValue().ssub_ov(Cst2->getValue(), Overflow); + if (!Overflow) + return new ICmpInst(I.getPredicate(), X, + ConstantInt::get(Cst->getType(), NewCst)); + } } return Changed ? &I : 0; } diff --git a/test/Transforms/InstCombine/icmp.ll b/test/Transforms/InstCombine/icmp.ll index 12a4744cc0f..2e3ff24f6ba 100644 --- a/test/Transforms/InstCombine/icmp.ll +++ b/test/Transforms/InstCombine/icmp.ll @@ -1356,3 +1356,56 @@ define i1 @icmp_ashr_ashr_ne(i32 %a, i32 %b) nounwind { %z = icmp ne i32 %x, %y ret i1 %z } + +; CHECK-LABEL: icmp_add_const_const1 +; CHECK: %cmp = icmp slt i32 %x, -10 +; CHECK-NOT: %add = add nsw i32 %x, 10 +define i32 @icmp_add_const_const1(i32 %x) nounwind ssp uwtable { +entry: + %add = add nsw i32 %x, 10 + %cmp = icmp slt i32 %add, 0 + %conv = zext i1 %cmp to i32 + ret i32 %conv +} + +; CHECK-LABEL: icmp_add_const_const2 +; CHECK: %cmp = icmp slt i32 %x, -10 +; CHECK-NOT: %add = add nsw i32 %x, 10 +define i32 @icmp_add_const_const2(i32 %x) nounwind ssp uwtable { +entry: + %add = add nsw i32 10, %x + %cmp = icmp sgt i32 0, %add + %conv = zext i1 %cmp to i32 + ret i32 %conv +} + +; CHECK-LABEL: icmp_add_const_const3 +; CHECK: %cmp = icmp slt i32 %x, 20 +; CHECK-NOT: %sub = add nsw i32 %x, -10 +define i32 @icmp_add_const_const3(i32 %x) nounwind ssp uwtable { +entry: + %add = add nsw i32 -10, %x + %cmp = icmp sgt i32 10, %add + %conv = zext i1 %cmp to i32 + ret i32 %conv +} + +; CHECK-LABEL: icmp_add_const_intmin +; CHECK: %cmp = icmp ne i32 %x, 2147483638 +define i32 @icmp_add_const_intmin(i32 %x) nounwind ssp uwtable { +entry: + %add = add nsw i32 %x, 10 + %cmp = icmp sgt i32 %add, -2147483648 + %conv = zext i1 %cmp to i32 + ret i32 %conv +} + +; CHECK-LABEL: icmp_add_const_intmax +; CHECK: %cmp = icmp ne i32 %x, 2147483637 +define i32 @icmp_add_const_intmax(i32 %x) nounwind ssp uwtable { +entry: + %add = add nsw i32 %x, 10 + %cmp = icmp slt i32 %add, 2147483647 + %conv = zext i1 %cmp to i32 + ret i32 %conv +} -- 2.34.1