[InstSimplify] fold shifts by sext bool

https://rise4fun.com/Alive/c3Y

llvm-svn: 335633
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 4a80558..c15f649 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -1162,7 +1162,11 @@
     return Constant::getNullValue(Op0->getType());
 
   // X shift by 0 -> X
-  if (match(Op1, m_Zero()))
+  // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones
+  // would be poison.
+  Value *X;
+  if (match(Op1, m_Zero()) ||
+      (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
     return Op0;
 
   // Fold undefined shifts.
diff --git a/llvm/test/Transforms/InstSimplify/shift.ll b/llvm/test/Transforms/InstSimplify/shift.ll
index ad572a1..d080cbc 100644
--- a/llvm/test/Transforms/InstSimplify/shift.ll
+++ b/llvm/test/Transforms/InstSimplify/shift.ll
@@ -123,9 +123,7 @@
 
 define i8 @lshr_by_sext_bool(i1 %x, i8 %y) {
 ; CHECK-LABEL: @lshr_by_sext_bool(
-; CHECK-NEXT:    [[S:%.*]] = sext i1 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = lshr i8 [[Y:%.*]], [[S]]
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 [[Y:%.*]]
 ;
   %s = sext i1 %x to i8
   %r = lshr i8 %y, %s
@@ -134,9 +132,7 @@
 
 define <2 x i8> @lshr_by_sext_bool_vec(<2 x i1> %x, <2 x i8> %y) {
 ; CHECK-LABEL: @lshr_by_sext_bool_vec(
-; CHECK-NEXT:    [[S:%.*]] = sext <2 x i1> [[X:%.*]] to <2 x i8>
-; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i8> [[Y:%.*]], [[S]]
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    ret <2 x i8> [[Y:%.*]]
 ;
   %s = sext <2 x i1> %x to <2 x i8>
   %r = lshr <2 x i8> %y, %s
@@ -145,9 +141,7 @@
 
 define i8 @ashr_by_sext_bool(i1 %x, i8 %y) {
 ; CHECK-LABEL: @ashr_by_sext_bool(
-; CHECK-NEXT:    [[S:%.*]] = sext i1 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = ashr i8 [[Y:%.*]], [[S]]
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 [[Y:%.*]]
 ;
   %s = sext i1 %x to i8
   %r = ashr i8 %y, %s
@@ -156,9 +150,7 @@
 
 define <2 x i8> @ashr_by_sext_bool_vec(<2 x i1> %x, <2 x i8> %y) {
 ; CHECK-LABEL: @ashr_by_sext_bool_vec(
-; CHECK-NEXT:    [[S:%.*]] = sext <2 x i1> [[X:%.*]] to <2 x i8>
-; CHECK-NEXT:    [[R:%.*]] = ashr <2 x i8> [[Y:%.*]], [[S]]
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    ret <2 x i8> [[Y:%.*]]
 ;
   %s = sext <2 x i1> %x to <2 x i8>
   %r = ashr <2 x i8> %y, %s
@@ -167,9 +159,7 @@
 
 define i8 @shl_by_sext_bool(i1 %x, i8 %y) {
 ; CHECK-LABEL: @shl_by_sext_bool(
-; CHECK-NEXT:    [[S:%.*]] = sext i1 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = shl i8 [[Y:%.*]], [[S]]
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 [[Y:%.*]]
 ;
   %s = sext i1 %x to i8
   %r = shl i8 %y, %s
@@ -178,9 +168,7 @@
 
 define <2 x i8> @shl_by_sext_bool_vec(<2 x i1> %x, <2 x i8> %y) {
 ; CHECK-LABEL: @shl_by_sext_bool_vec(
-; CHECK-NEXT:    [[S:%.*]] = sext <2 x i1> [[X:%.*]] to <2 x i8>
-; CHECK-NEXT:    [[R:%.*]] = shl <2 x i8> [[Y:%.*]], [[S]]
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    ret <2 x i8> [[Y:%.*]]
 ;
   %s = sext <2 x i1> %x to <2 x i8>
   %r = shl <2 x i8> %y, %s