[llvm] r237968 - [InstCombine] X - 0 is equal to X, not undef
David Majnemer
david.majnemer at gmail.com
Thu May 21 16:04:21 PDT 2015
Author: majnemer
Date: Thu May 21 18:04:21 2015
New Revision: 237968
URL: http://llvm.org/viewvc/llvm-project?rev=237968&view=rev
Log:
[InstCombine] X - 0 is equal to X, not undef
A refactoring made @llvm.ssub.with.overflow.i32(i32 %X, i32 0) transform
into undef instead of %X.
This fixes PR23624.
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/trunk/test/Transforms/InstCombine/intrinsics.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=237968&r1=237967&r2=237968&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Thu May 21 18:04:21 2015
@@ -2141,13 +2141,11 @@ bool InstCombiner::OptimizeOverflowCheck
case OCF_SIGNED_ADD: {
// X + undef -> undef
if (isa<UndefValue>(RHS))
- return SetResult(UndefValue::get(RHS->getType()),
- UndefValue::get(Builder->getInt1Ty()), false);
+ return SetResult(RHS, UndefValue::get(Builder->getInt1Ty()), false);
- if (ConstantInt *ConstRHS = dyn_cast<ConstantInt>(RHS))
- // X + 0 -> {X, false}
- if (ConstRHS->isZero())
- return SetResult(LHS, Builder->getFalse(), false);
+ // X + 0 -> {X, false}
+ if (match(RHS, m_Zero()))
+ return SetResult(LHS, Builder->getFalse(), false);
// We can strength reduce this signed add into a regular add if we can prove
// that it will never overflow.
@@ -2160,16 +2158,16 @@ bool InstCombiner::OptimizeOverflowCheck
case OCF_UNSIGNED_SUB:
case OCF_SIGNED_SUB: {
// undef - X -> undef
+ if (isa<UndefValue>(LHS))
+ return SetResult(LHS, UndefValue::get(Builder->getInt1Ty()), false);
+
// X - undef -> undef
- if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS))
- return SetResult(UndefValue::get(LHS->getType()),
- UndefValue::get(Builder->getInt1Ty()), false);
-
- if (ConstantInt *ConstRHS = dyn_cast<ConstantInt>(RHS))
- // X - 0 -> {X, false}
- if (ConstRHS->isZero())
- return SetResult(UndefValue::get(LHS->getType()), Builder->getFalse(),
- false);
+ if (isa<UndefValue>(RHS))
+ return SetResult(RHS, UndefValue::get(Builder->getInt1Ty()), false);
+
+ // X - 0 -> {X, false}
+ if (match(RHS, m_Zero()))
+ return SetResult(LHS, Builder->getFalse(), false);
if (OCF == OCF_SIGNED_SUB) {
if (WillNotOverflowSignedSub(LHS, RHS, OrigI))
@@ -2194,19 +2192,15 @@ bool InstCombiner::OptimizeOverflowCheck
case OCF_SIGNED_MUL:
// X * undef -> undef
if (isa<UndefValue>(RHS))
- return SetResult(UndefValue::get(LHS->getType()),
- UndefValue::get(Builder->getInt1Ty()), false);
+ return SetResult(RHS, UndefValue::get(Builder->getInt1Ty()), false);
- if (ConstantInt *RHSI = dyn_cast<ConstantInt>(RHS)) {
- // X * 0 -> {0, false}
- if (RHSI->isZero())
- return SetResult(Constant::getNullValue(RHS->getType()),
- Builder->getFalse(), false);
-
- // X * 1 -> {X, false}
- if (RHSI->equalsInt(1))
- return SetResult(LHS, Builder->getFalse(), false);
- }
+ // X * 0 -> {0, false}
+ if (match(RHS, m_Zero()))
+ return SetResult(RHS, Builder->getFalse(), false);
+
+ // X * 1 -> {X, false}
+ if (match(RHS, m_One()))
+ return SetResult(LHS, Builder->getFalse(), false);
if (OCF == OCF_SIGNED_MUL)
if (WillNotOverflowSignedMul(LHS, RHS, OrigI))
Modified: llvm/trunk/test/Transforms/InstCombine/intrinsics.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/intrinsics.ll?rev=237968&r1=237967&r2=237968&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/intrinsics.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/intrinsics.ll Thu May 21 18:04:21 2015
@@ -417,3 +417,11 @@ define %ov.result.32 @ssubtest_reorder(i
; CHECK-NEXT: %1 = insertvalue %ov.result.32 { i32 undef, i1 false }, i32 %x, 0
; CHECK-NEXT: ret %ov.result.32 %1
}
+
+define %ov.result.32 @never_overflows_ssub(i32 %a) {
+ %x = call %ov.result.32 @llvm.ssub.with.overflow.i32(i32 %a, i32 0)
+ ret %ov.result.32 %x
+; CHECK-LABEL: @never_overflows_ssub
+; CHECK-NEXT: %[[x:.*]] = insertvalue %ov.result.32 { i32 undef, i1 false }, i32 %a, 0
+; CHECK-NEXT: ret %ov.result.32 %[[x]]
+}
More information about the llvm-commits
mailing list