[PATCH] D23134: Make cltz and cttz zero undef when the operand cannot be zero in InstCombine
Amaury SECHET via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 3 14:27:37 PDT 2016
deadalnix updated this revision to Diff 66724.
deadalnix added a comment.
Split popcount change in https://reviews.llvm.org/D23139
https://reviews.llvm.org/D23134
Files:
lib/Transforms/InstCombine/InstCombineCalls.cpp
test/Transforms/InstCombine/intrinsics.ll
Index: test/Transforms/InstCombine/intrinsics.ll
===================================================================
--- test/Transforms/InstCombine/intrinsics.ll
+++ test/Transforms/InstCombine/intrinsics.ll
@@ -379,14 +379,38 @@
; CHECK-NEXT: ret i32 undef
}
+define i32 @ctlz_make_undef(i32 %a) {
+entry:
+ %or = or i32 %a, 8
+ %ctlz = tail call i32 @llvm.ctlz.i32(i32 %or, i1 false)
+ ret i32 %ctlz
+; CHECK-LABEL: @ctlz_make_undef(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: %or = or i32 %a, 8
+; CHECK-NEXT: %ctlz = tail call i32 @llvm.ctlz.i32(i32 %or, i1 true)
+; CHECK-NEXT: ret i32 %ctlz
+}
+
define i32 @cttz_undef(i32 %Value) nounwind {
%cttz = call i32 @llvm.cttz.i32(i32 0, i1 true)
ret i32 %cttz
; CHECK-LABEL: @cttz_undef(
; CHECK-NEXT: ret i32 undef
}
+define i32 @cttz_make_undef(i32 %a) {
+entry:
+ %or = or i32 %a, 8
+ %cttz = tail call i32 @llvm.cttz.i32(i32 %or, i1 false)
+ ret i32 %cttz
+; CHECK-LABEL: @cttz_make_undef(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: %or = or i32 %a, 8
+; CHECK-NEXT: %cttz = tail call i32 @llvm.cttz.i32(i32 %or, i1 true)
+; CHECK-NEXT: ret i32 %cttz
+}
+
define i32 @ctlz_select(i32 %Value) nounwind {
%tobool = icmp ne i32 %Value, 0
%ctlz = call i32 @llvm.ctlz.i32(i32 %Value, i1 true)
Index: lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1400,7 +1400,8 @@
case Intrinsic::cttz: {
// If all bits below the first known one are known zero,
// this value is constant.
- IntegerType *IT = dyn_cast<IntegerType>(II->getArgOperand(0)->getType());
+ auto Op0 = II->getArgOperand(0);
+ IntegerType *IT = dyn_cast<IntegerType>(Op0->getType());
// FIXME: Try to simplify vectors of integers.
if (!IT) break;
uint32_t BitWidth = IT->getBitWidth();
@@ -1412,13 +1413,15 @@
if ((Mask & KnownZero) == Mask)
return replaceInstUsesWith(CI, ConstantInt::get(IT,
APInt(BitWidth, TrailingZeros)));
-
+ if (KnownOne != 0 || isKnownNonZero(Op0, DL))
+ goto MakeCttzCltzUndef;
}
break;
case Intrinsic::ctlz: {
// If all bits above the first known one are known zero,
// this value is constant.
- IntegerType *IT = dyn_cast<IntegerType>(II->getArgOperand(0)->getType());
+ auto Op0 = II->getArgOperand(0);
+ IntegerType *IT = dyn_cast<IntegerType>(Op0->getType());
// FIXME: Try to simplify vectors of integers.
if (!IT) break;
uint32_t BitWidth = IT->getBitWidth();
@@ -1430,10 +1433,21 @@
if ((Mask & KnownZero) == Mask)
return replaceInstUsesWith(CI, ConstantInt::get(IT,
APInt(BitWidth, LeadingZeros)));
-
+ if (KnownOne != 0 || isKnownNonZero(Op0, DL))
+ goto MakeCttzCltzUndef;
}
break;
-
+ MakeCttzCltzUndef: {
+ bool IsZeroUndef = false;
+ auto Op1 = II->getArgOperand(1);
+ if (auto Op1C = dyn_cast<ConstantInt>(Op1))
+ IsZeroUndef = Op1C->getZExtValue() != 0;
+ if (!IsZeroUndef) {
+ Worklist.Add(II);
+ II->setOperand(1, ConstantInt::getAllOnesValue(Op1->getType()));
+ }
+ break;
+ }
case Intrinsic::uadd_with_overflow:
case Intrinsic::sadd_with_overflow:
case Intrinsic::umul_with_overflow:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23134.66724.patch
Type: text/x-patch
Size: 3381 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160803/6f54c7fa/attachment.bin>
More information about the llvm-commits
mailing list