[X86] Fold xor(zext(xor(x,c1)),c2) -> xor(zext(x),xor(zext(c1),c2))
authorSimon Pilgrim <llvm-dev@redking.me.uk>
Mon, 5 Apr 2021 10:40:29 +0000 (11:40 +0100)
committerSimon Pilgrim <llvm-dev@redking.me.uk>
Mon, 5 Apr 2021 10:40:37 +0000 (11:40 +0100)
Fixes PR47603 (second case) by extending rG89afec348dbd3e5078f176e978971ee2d3b5dec8

llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/test/CodeGen/X86/clz.ll

index 4818adb..3c81bc5 100644 (file)
@@ -46983,16 +46983,18 @@ static SDValue combineXor(SDNode *N, SelectionDAG &DAG,
   if (SDValue RV = foldXorTruncShiftIntoCmp(N, DAG))
     return RV;
 
+  // Fold xor(zext(xor(x,c1)),c2) -> xor(zext(x),xor(zext(c1),c2))
   // Fold xor(truncate(xor(x,c1)),c2) -> xor(truncate(x),xor(truncate(c1),c2))
   // TODO: Under what circumstances could this be performed in DAGCombine?
-  if (N->getOperand(0).getOpcode() == ISD::TRUNCATE &&
+  if ((N->getOperand(0).getOpcode() == ISD::TRUNCATE ||
+       N->getOperand(0).getOpcode() == ISD::ZERO_EXTEND) &&
       N->getOperand(0).getOperand(0).getOpcode() == N->getOpcode() &&
       isa<ConstantSDNode>(N->getOperand(1)) &&
       isa<ConstantSDNode>(N->getOperand(0).getOperand(0).getOperand(1))) {
     SDLoc DL(N);
-    SDValue TruncateSrc = N->getOperand(0).getOperand(0);
-    SDValue LHS = DAG.getNode(ISD::TRUNCATE, DL, VT, TruncateSrc.getOperand(0));
-    SDValue RHS = DAG.getNode(ISD::TRUNCATE, DL, VT, TruncateSrc.getOperand(1));
+    SDValue TruncExtSrc = N->getOperand(0).getOperand(0);
+    SDValue LHS = DAG.getZExtOrTrunc(TruncExtSrc.getOperand(0), DL, VT);
+    SDValue RHS = DAG.getZExtOrTrunc(TruncExtSrc.getOperand(1), DL, VT);
     return DAG.getNode(ISD::XOR, DL, VT, LHS,
                        DAG.getNode(ISD::XOR, DL, VT, RHS, N->getOperand(1)));
   }
index e11ab38..f8d0336 100644 (file)
@@ -1105,8 +1105,6 @@ define i32 @PR47603_zext(i32 %a0, [32 x i8]* %a1) {
 ; X64-LABEL: PR47603_zext:
 ; X64:       # %bb.0:
 ; X64-NEXT:    bsrl %edi, %eax
-; X64-NEXT:    xorl $31, %eax
-; X64-NEXT:    xorq $31, %rax
 ; X64-NEXT:    movsbl (%rsi,%rax), %eax
 ; X64-NEXT:    retq
 ;