From 611f9f92fcb1a0bdcfd481dbf7ee67be04dd54e0 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Thu, 27 Oct 2016 17:30:50 +0000 Subject: [PATCH] [InstCombine] handle simple vector integer constants in IsFreeToInvert llvm-svn: 285318 --- .../Transforms/InstCombine/InstCombineInternal.h | 18 ++++++++++++ llvm/test/Transforms/InstCombine/max-of-nots.ll | 32 ++++++++++++++++------ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h index e85b3a5..176b7a0 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -84,6 +84,24 @@ static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) { if (isa(V)) return true; + // A vector of constant integers can be inverted easily. + Constant *CV; + if (V->getType()->isVectorTy() && match(V, PatternMatch::m_Constant(CV))) { + unsigned NumElts = V->getType()->getVectorNumElements(); + for (unsigned i = 0; i != NumElts; ++i) { + Constant *Elt = CV->getAggregateElement(i); + if (!Elt) + return false; + + if (isa(Elt)) + continue; + + if (!isa(Elt)) + return false; + } + return true; + } + // Compares can be inverted if all of their uses are being modified to use the // ~V. if (isa(V)) diff --git a/llvm/test/Transforms/InstCombine/max-of-nots.ll b/llvm/test/Transforms/InstCombine/max-of-nots.ll index a329168..d9a13e5 100644 --- a/llvm/test/Transforms/InstCombine/max-of-nots.ll +++ b/llvm/test/Transforms/InstCombine/max-of-nots.ll @@ -88,16 +88,14 @@ define i32 @max_of_nots(i32 %x, i32 %y) { ret i32 %smax96 } -; FIXME - vectors should get the same folds define <2 x i32> @max_of_nots_vec(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @max_of_nots_vec( -; CHECK-NEXT: [[C0:%.*]] = icmp sgt <2 x i32> %y, zeroinitializer -; CHECK-NEXT: [[XOR_Y:%.*]] = xor <2 x i32> %y, -; CHECK-NEXT: [[S0:%.*]] = select <2 x i1> [[C0]], <2 x i32> [[XOR_Y]], <2 x i32> -; CHECK-NEXT: [[XOR_X:%.*]] = xor <2 x i32> %x, -; CHECK-NEXT: [[C1:%.*]] = icmp slt <2 x i32> [[S0]], [[XOR_X]] -; CHECK-NEXT: [[SMAX96:%.*]] = select <2 x i1> [[C1]], <2 x i32> [[XOR_X]], <2 x i32> [[S0]] -; CHECK-NEXT: ret <2 x i32> [[SMAX96]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i32> %y, zeroinitializer +; CHECK-NEXT: [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> %y, <2 x i32> zeroinitializer +; CHECK-NEXT: [[TMP3:%.*]] = icmp slt <2 x i32> [[TMP2]], %x +; CHECK-NEXT: [[TMP4:%.*]] = select <2 x i1> [[TMP3]], <2 x i32> [[TMP2]], <2 x i32> %x +; CHECK-NEXT: [[TMP5:%.*]] = xor <2 x i32> [[TMP4]], +; CHECK-NEXT: ret <2 x i32> [[TMP5]] ; %c0 = icmp sgt <2 x i32> %y, zeroinitializer %xor_y = xor <2 x i32> %y, @@ -108,3 +106,21 @@ define <2 x i32> @max_of_nots_vec(<2 x i32> %x, <2 x i32> %y) { ret <2 x i32> %smax96 } +define <2 x i37> @max_of_nots_weird_type_vec(<2 x i37> %x, <2 x i37> %y) { +; CHECK-LABEL: @max_of_nots_weird_type_vec( +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i37> %y, zeroinitializer +; CHECK-NEXT: [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i37> %y, <2 x i37> zeroinitializer +; CHECK-NEXT: [[TMP3:%.*]] = icmp slt <2 x i37> [[TMP2]], %x +; CHECK-NEXT: [[TMP4:%.*]] = select <2 x i1> [[TMP3]], <2 x i37> [[TMP2]], <2 x i37> %x +; CHECK-NEXT: [[TMP5:%.*]] = xor <2 x i37> [[TMP4]], +; CHECK-NEXT: ret <2 x i37> [[TMP5]] +; + %c0 = icmp sgt <2 x i37> %y, zeroinitializer + %xor_y = xor <2 x i37> %y, + %s0 = select <2 x i1> %c0, <2 x i37> %xor_y, <2 x i37> + %xor_x = xor <2 x i37> %x, + %c1 = icmp slt <2 x i37> %s0, %xor_x + %smax96 = select <2 x i1> %c1, <2 x i37> %xor_x, <2 x i37> %s0 + ret <2 x i37> %smax96 +} + -- 2.7.4