[PATCH] D22477: [InstCombine] fold add(zext(xor X, C), C) --> sext X when C is INT_MIN in the source type
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 18 14:18:21 PDT 2016
spatel created this revision.
spatel added reviewers: majnemer, eli.friedman, sanjoy.
spatel added a subscriber: llvm-commits.
Herald added a subscriber: mcrosier.
The pattern may look more obviously like a sext if written as:
define i32 @g(i16 %x) {
%zext = zext i16 %x to i32
%xor = xor i32 %zext, 32768
%add = add i32 %xor, -32768
ret i32 %add
}
We already have that fold in visitAdd().
And that probably gives away how I got here: I was testing if we had any missing folds if we make D22271 more liberal about pulling logic ops ahead of zexts.
https://reviews.llvm.org/D22477
Files:
lib/Transforms/InstCombine/InstCombineAddSub.cpp
test/Transforms/InstCombine/apint-add.ll
Index: test/Transforms/InstCombine/apint-add.ll
===================================================================
--- test/Transforms/InstCombine/apint-add.ll
+++ test/Transforms/InstCombine/apint-add.ll
@@ -56,9 +56,7 @@
define i7 @sext(i4 %x) {
; CHECK-LABEL: @sext(
-; CHECK-NEXT: [[XOR:%.*]] = xor i4 %x, -8
-; CHECK-NEXT: [[ZEXT:%.*]] = zext i4 [[XOR]] to i7
-; CHECK-NEXT: [[ADD:%.*]] = add nsw i7 [[ZEXT]], -8
+; CHECK-NEXT: [[ADD:%.*]] = sext i4 %x to i7
; CHECK-NEXT: ret i7 [[ADD]]
;
%xor = xor i4 %x, -8
@@ -69,9 +67,7 @@
define <2 x i10> @sext_vec(<2 x i3> %x) {
; CHECK-LABEL: @sext_vec(
-; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i3> %x, <i3 -4, i3 -4>
-; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i3> [[XOR]] to <2 x i10>
-; CHECK-NEXT: [[ADD:%.*]] = add nsw <2 x i10> [[ZEXT]], <i10 -4, i10 -4>
+; CHECK-NEXT: [[ADD:%.*]] = sext <2 x i3> %x to <2 x i10>
; CHECK-NEXT: ret <2 x i10> [[ADD]]
;
%xor = xor <2 x i3> %x, <i3 -4, i3 -4>
Index: lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1047,6 +1047,16 @@
// X + (signbit) --> X ^ signbit
if (Val->isSignBit())
return BinaryOperator::CreateXor(LHS, RHS);
+
+ // Is this add the last step in a convoluted sext?
+ Value *X;
+ const APInt *C;
+ if (match(LHS, m_OneUse(m_ZExt(m_OneUse(m_Xor(m_Value(X), m_APInt(C)))))) &&
+ C->isMinSignedValue() &&
+ C->sext(LHS->getType()->getScalarSizeInBits()) == *Val) {
+ // add(zext(xor i16 X, -32768), -32768) --> sext X
+ return CastInst::Create(Instruction::SExt, X, LHS->getType());
+ }
}
// FIXME: Use the match above instead of dyn_cast to allow these transforms
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22477.64381.patch
Type: text/x-patch
Size: 1854 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160718/307be96c/attachment.bin>
More information about the llvm-commits
mailing list