[PATCH] D63652: [InstCombine] (1 << (C - x)) -> ((1 << C) >> x) if C is bitwidth - 1

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 21 08:19:17 PDT 2019


xbolva00 created this revision.
xbolva00 added a reviewer: spatel.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

  %a = sub i32 31, %x
  %r = shl i32 1, %a
    =>
  %d = shl i32 1, 31
  %r = lshr i32 %d, %x
  
  Done: 1
  Optimization is correct!

https://rise4fun.com/Alive/btZm


Repository:
  rL LLVM

https://reviews.llvm.org/D63652

Files:
  lib/Transforms/InstCombine/InstCombineShifts.cpp
  test/Transforms/InstCombine/shl-sub.ll


Index: test/Transforms/InstCombine/shl-sub.ll
===================================================================
--- test/Transforms/InstCombine/shl-sub.ll
+++ test/Transforms/InstCombine/shl-sub.ll
@@ -3,8 +3,7 @@
 
 define i32 @shl_sub_i32(i32 %x) {
 ; CHECK-LABEL: @shl_sub_i32(
-; CHECK-NEXT:    [[S:%.*]] = sub i32 31, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = shl i32 1, [[S]]
+; CHECK-NEXT:    [[R:%.*]] = lshr i32 -2147483648, [[X:%.*]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %s = sub i32 31, %x
@@ -16,7 +15,7 @@
 ; CHECK-LABEL: @shl_sub_multiuse_i32(
 ; CHECK-NEXT:    [[S:%.*]] = sub i32 31, [[X:%.*]]
 ; CHECK-NEXT:    call void @use(i32 [[S]])
-; CHECK-NEXT:    [[R:%.*]] = shl i32 1, [[S]]
+; CHECK-NEXT:    [[R:%.*]] = lshr i32 -2147483648, [[X]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %s = sub i32 31, %x
@@ -27,8 +26,7 @@
 
 define i8 @shl_sub_i8(i8 %x) {
 ; CHECK-LABEL: @shl_sub_i8(
-; CHECK-NEXT:    [[S:%.*]] = sub i8 7, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = shl i8 1, [[S]]
+; CHECK-NEXT:    [[R:%.*]] = lshr i8 -128, [[X:%.*]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %s = sub i8 7, %x
@@ -38,8 +36,7 @@
 
 define i64 @shl_sub_i64(i64 %x) {
 ; CHECK-LABEL: @shl_sub_i64(
-; CHECK-NEXT:    [[S:%.*]] = sub i64 63, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = shl i64 1, [[S]]
+; CHECK-NEXT:    [[R:%.*]] = lshr i64 -9223372036854775808, [[X:%.*]]
 ; CHECK-NEXT:    ret i64 [[R]]
 ;
   %s = sub i64 63, %x
@@ -49,8 +46,7 @@
 
 define <2 x i64> @shl_sub_i64_vec(<2 x i64> %x) {
 ; CHECK-LABEL: @shl_sub_i64_vec(
-; CHECK-NEXT:    [[S:%.*]] = sub <2 x i64> <i64 63, i64 63>, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = shl <2 x i64> <i64 1, i64 1>, [[S]]
+; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i64> <i64 -9223372036854775808, i64 -9223372036854775808>, [[X:%.*]]
 ; CHECK-NEXT:    ret <2 x i64> [[R]]
 ;
   %s = sub <2 x i64> <i64 63, i64 63>, %x
Index: lib/Transforms/InstCombine/InstCombineShifts.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -582,6 +582,8 @@
 
   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
   Type *Ty = I.getType();
+  unsigned BitWidth = Ty->getScalarSizeInBits();
+
   const APInt *ShAmtAPInt;
   if (match(Op1, m_APInt(ShAmtAPInt))) {
     unsigned ShAmt = ShAmtAPInt->getZExtValue();
@@ -669,6 +671,13 @@
     if (match(Op0, m_Mul(m_Value(X), m_Constant(C2))))
       return BinaryOperator::CreateMul(X, ConstantExpr::getShl(C2, C1));
   }
+  const APInt *ShlOp0;
+  const APInt *C;
+  // (1 << (C - x)) -> ((1 << C) >> x) if C is bitwidth - 1
+  if (match(Op0, m_APInt(ShlOp0)) && ShlOp0->getSExtValue() == 1 &&
+      match(Op1, m_Sub(m_APInt(C), m_Value(X))) &&
+      C->getSExtValue() == BitWidth - 1)
+    return BinaryOperator::CreateLShr(ConstantInt::get(Ty, ShlOp0->shl(*C)), X);
 
   return nullptr;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63652.206003.patch
Type: text/x-patch
Size: 2890 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190621/bd743b3b/attachment.bin>


More information about the llvm-commits mailing list