[InstSimplify] Make m_Not work for xor -1, X
authorCraig Topper <craig.topper@gmail.com>
Thu, 18 May 2017 20:27:32 +0000 (20:27 +0000)
committerCraig Topper <craig.topper@gmail.com>
Thu, 18 May 2017 20:27:32 +0000 (20:27 +0000)
Currently m_Not only works the canonical xor X, -1 form that InstCombine produces. InstSimplify can't rely on this canonicalization.

Differential Revision: https://reviews.llvm.org/D33331

llvm-svn: 303379

llvm/include/llvm/IR/PatternMatch.h
llvm/test/Transforms/InstSimplify/AndOrXor.ll

index 6b2b22e..072c6c5 100644 (file)
@@ -886,17 +886,21 @@ template <typename LHS_t> struct not_match {
 
   template <typename OpTy> bool match(OpTy *V) {
     if (auto *O = dyn_cast<Operator>(V))
-      if (O->getOpcode() == Instruction::Xor)
-        return matchIfNot(O->getOperand(0), O->getOperand(1));
+      if (O->getOpcode() == Instruction::Xor) {
+        if (isAllOnes(O->getOperand(1)))
+          return L.match(O->getOperand(0));
+        if (isAllOnes(O->getOperand(0)))
+          return L.match(O->getOperand(1));
+      }
     return false;
   }
 
 private:
-  bool matchIfNot(Value *LHS, Value *RHS) {
-    return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
+  bool isAllOnes(Value *V) {
+    return (isa<ConstantInt>(V) || isa<ConstantDataVector>(V) ||
             // FIXME: Remove CV.
-            isa<ConstantVector>(RHS)) &&
-           cast<Constant>(RHS)->isAllOnesValue() && L.match(LHS);
+            isa<ConstantVector>(V)) &&
+           cast<Constant>(V)->isAllOnesValue();
   }
 };
 
index 2d7eeae..a027c7e 100644 (file)
@@ -865,3 +865,11 @@ define <2 x i8> @shl_undersized_mask_splat(<2 x i8> %x) {
   ret <2 x i8> %mask
 }
 
+define i32 @reversed_not(i32 %a) {
+; CHECK-LABEL: @reversed_not(
+; CHECK-NEXT:    ret i32 -1
+;
+  %nega = xor i32 -1, %a
+  %or = or i32 %a, %nega
+  ret i32 %or
+}