[InstSimplify] fold rotate of -1 to -1
authorSanjay Patel <spatel@rotateright.com>
Sun, 22 Aug 2021 13:13:59 +0000 (09:13 -0400)
committerSanjay Patel <spatel@rotateright.com>
Sun, 22 Aug 2021 13:15:48 +0000 (09:15 -0400)
This is part of solving more general rotate patterns seen in
bugs related to:
https://llvm.org/PR51575

https://alive2.llvm.org/ce/z/GpkFCt

llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/call.ll

index 9c51fe9..5536de5 100644 (file)
@@ -5860,6 +5860,10 @@ static Value *simplifyIntrinsic(CallBase *Call, const SimplifyQuery &Q) {
     if (match(Op0, m_Zero()) && match(Op1, m_Zero()))
       return ConstantInt::getNullValue(F->getReturnType());
 
+    // Rotating -1 by anything is -1.
+    if (match(Op0, m_AllOnes()) && match(Op1, m_AllOnes()))
+      return ConstantInt::getAllOnesValue(F->getReturnType());
+
     return nullptr;
   }
   case Intrinsic::experimental_constrained_fma: {
index b2c6f23..a4560f8 100644 (file)
@@ -976,8 +976,7 @@ define <2 x i8> @fshr_zero_vec(<2 x i8> %shamt) {
 
 define <2 x i7> @fshl_ones_vec(<2 x i7> %shamt) {
 ; CHECK-LABEL: @fshl_ones_vec(
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i7> @llvm.fshl.v2i7(<2 x i7> <i7 undef, i7 -1>, <2 x i7> <i7 -1, i7 undef>, <2 x i7> [[SHAMT:%.*]])
-; CHECK-NEXT:    ret <2 x i7> [[R]]
+; CHECK-NEXT:    ret <2 x i7> <i7 -1, i7 -1>
 ;
   %r = call <2 x i7> @llvm.fshl.v2i7(<2 x i7> <i7 undef, i7 -1>, <2 x i7> <i7 -1, i7 undef>, <2 x i7> %shamt)
   ret <2 x i7> %r
@@ -985,8 +984,7 @@ define <2 x i7> @fshl_ones_vec(<2 x i7> %shamt) {
 
 define i9 @fshr_ones(i9 %shamt) {
 ; CHECK-LABEL: @fshr_ones(
-; CHECK-NEXT:    [[R:%.*]] = call i9 @llvm.fshr.i9(i9 -1, i9 -1, i9 [[SHAMT:%.*]])
-; CHECK-NEXT:    ret i9 [[R]]
+; CHECK-NEXT:    ret i9 -1
 ;
   %r = call i9 @llvm.fshr.i9(i9 -1, i9 -1, i9 %shamt)
   ret i9 %r