[PATCH] D20497: [ValueTracking, InstCombine] extend isKnownToBeAPowerOfTwo() to handle vector splat constants

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri May 20 15:34:53 PDT 2016


spatel created this revision.
spatel added reviewers: majnemer, sanjoy, hfinkel.
spatel added a subscriber: llvm-commits.
Herald added a subscriber: mcrosier.

We could try harder and handle non-splat vector constants too, but that seems much rarer to me.
Note that the div test isn't resolved because there's a check for isIntegerTy() guarding that transform.

http://reviews.llvm.org/D20497

Files:
  lib/Analysis/ValueTracking.cpp
  test/Transforms/InstCombine/div.ll
  test/Transforms/InstCombine/rem.ll
  test/Transforms/InstCombine/shift.ll

Index: test/Transforms/InstCombine/shift.ll
===================================================================
--- test/Transforms/InstCombine/shift.ll
+++ test/Transforms/InstCombine/shift.ll
@@ -700,11 +700,9 @@
   ret i32 %div2
 }
 
-; FIXME: Vector lshr should be treated the same as scalar. 
-
 define <2 x i32> @test42vec(<2 x i32> %a, <2 x i32> %b) {
 ; CHECK-LABEL: @test42vec(
-; CHECK-NEXT:    [[DIV:%.*]] = lshr <2 x i32> <i32 4096, i32 4096>, %b
+; CHECK-NEXT:    [[DIV:%.*]] = lshr exact <2 x i32> <i32 4096, i32 4096>, %b
 ; CHECK-NEXT:    [[DIV2:%.*]] = udiv <2 x i32> %a, [[DIV]]
 ; CHECK-NEXT:    ret <2 x i32> [[DIV2]]
 ;
Index: test/Transforms/InstCombine/rem.ll
===================================================================
--- test/Transforms/InstCombine/rem.ll
+++ test/Transforms/InstCombine/rem.ll
@@ -25,22 +25,18 @@
 	ret i32 %B
 }
 
-; FIXME: This could be an 'and' just like above.
-
 define <2 x i32> @vec_power_of_2_constant_splat_divisor(<2 x i32> %A) {
 ; CHECK-LABEL: @vec_power_of_2_constant_splat_divisor(
-; CHECK-NEXT:    [[B:%.*]] = urem <2 x i32> %A, <i32 8, i32 8>
+; CHECK-NEXT:    [[B:%.*]] = and <2 x i32> %A, <i32 7, i32 7>
 ; CHECK-NEXT:    ret <2 x i32> [[B]]
 ;
   %B = urem <2 x i32> %A, <i32 8, i32 8>
   ret <2 x i32> %B
 }
 
-; FIXME: And it shouldn't matter whether we have ConstantVector or ConstantDataVector.
-
 define <2 x i19> @weird_vec_power_of_2_constant_splat_divisor(<2 x i19> %A) {
 ; CHECK-LABEL: @weird_vec_power_of_2_constant_splat_divisor(
-; CHECK-NEXT:    [[B:%.*]] = urem <2 x i19> %A, <i19 8, i19 8>
+; CHECK-NEXT:    [[B:%.*]] = and <2 x i19> %A, <i19 7, i19 7>
 ; CHECK-NEXT:    ret <2 x i19> [[B]]
 ;
   %B = urem <2 x i19> %A, <i19 8, i19 8>
Index: test/Transforms/InstCombine/div.ll
===================================================================
--- test/Transforms/InstCombine/div.ll
+++ test/Transforms/InstCombine/div.ll
@@ -374,7 +374,7 @@
 define <2 x i32> @test36vec(<2 x i32> %A) {
 ; CHECK-LABEL: @test36vec(
 ; CHECK-NEXT:    [[AND:%.*]] = and <2 x i32> %A, <i32 2147483647, i32 2147483647>
-; CHECK-NEXT:    [[SHL:%.*]] = shl nsw <2 x i32> <i32 1, i32 1>, %A
+; CHECK-NEXT:    [[SHL:%.*]] = shl nuw nsw <2 x i32> <i32 1, i32 1>, %A
 ; CHECK-NEXT:    [[MUL:%.*]] = sdiv exact <2 x i32> [[AND]], [[SHL]]
 ; CHECK-NEXT:    ret <2 x i32> [[MUL]]
 ;
Index: lib/Analysis/ValueTracking.cpp
===================================================================
--- lib/Analysis/ValueTracking.cpp
+++ lib/Analysis/ValueTracking.cpp
@@ -1500,9 +1500,12 @@
   if (Constant *C = dyn_cast<Constant>(V)) {
     if (C->isNullValue())
       return OrZero;
-    if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
+
+    if (auto *CI = dyn_cast<ConstantInt>(C))
       return CI->getValue().isPowerOf2();
-    // TODO: Handle vector constants.
+
+    if (auto *Splat = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
+      return Splat->getValue().isPowerOf2();
   }
 
   // 1 << X is clearly a power of two if the one is not shifted off the end.  If


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20497.58008.patch
Type: text/x-patch
Size: 3031 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160520/b6c6aed5/attachment.bin>


More information about the llvm-commits mailing list