[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
Tue Jul 19 15:17:03 PDT 2016
This revision was automatically updated to reflect the committed changes.
Closed by commit rL276035: [InstCombine] fold add(zext(xor X, C), C) --> sext X when C is INT_MIN in theā¦ (authored by spatel).
Changed prior to commit:
https://reviews.llvm.org/D22477?vs=64381&id=64575#toc
Repository:
rL LLVM
https://reviews.llvm.org/D22477
Files:
llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/trunk/test/Transforms/InstCombine/apint-add.ll
Index: llvm/trunk/test/Transforms/InstCombine/apint-add.ll
===================================================================
--- llvm/trunk/test/Transforms/InstCombine/apint-add.ll
+++ llvm/trunk/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,17 +67,36 @@
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>
%zext = zext <2 x i3> %xor to <2 x i10>
%add = add nsw <2 x i10> %zext, <i10 -4, i10 -4>
ret <2 x i10> %add
}
+; Multiple uses of the operands don't prevent the fold.
+
+define i4 @sext_multiuse(i4 %x) {
+; CHECK-LABEL: @sext_multiuse(
+; CHECK-NEXT: [[XOR:%.*]] = xor i4 %x, -8
+; CHECK-NEXT: [[ZEXT:%.*]] = zext i4 [[XOR]] to i7
+; CHECK-NEXT: [[ADD:%.*]] = sext i4 %x to i7
+; CHECK-NEXT: [[MUL:%.*]] = sdiv i7 [[ZEXT]], [[ADD]]
+; CHECK-NEXT: [[TRUNC:%.*]] = trunc i7 [[MUL]] to i4
+; CHECK-NEXT: [[DIV:%.*]] = sdiv i4 [[TRUNC]], [[XOR]]
+; CHECK-NEXT: ret i4 [[DIV]]
+;
+ %xor = xor i4 %x, -8
+ %zext = zext i4 %xor to i7
+ %add = add nsw i7 %zext, -8
+ %mul = sdiv i7 %zext, %add
+ %trunc = trunc i7 %mul to i4
+ %div = sdiv i4 %trunc, %xor
+ ret i4 %div
+}
+
; Tests for Integer BitWidth > 64 && BitWidth <= 1024.
;; Flip sign bit then add INT_MIN -> nop.
Index: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/trunk/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_ZExt(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.64575.patch
Type: text/x-patch
Size: 2809 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160719/e9f815c3/attachment.bin>
More information about the llvm-commits
mailing list