Skip to content

Commit 50f9198

Browse files
toppercjiegec
authored andcommitted
In LowerShift*Parts, xor with bits-1 instead of -1.
If we start with an i128 shift, the initial shift amount would usually have zeros in bit 8 and above. xoring the shift amount with -1 will set those upper bits to 1. If DAGCombiner is able to prove those bits are now 1, then the shift that uses the xor will be replaced with undef. Which we don't want. Reduce the xor constant to VT.bits-1 where VT is half the size of the larger shift type. This avoids toggling the upper bits. The hardware shift instruction only uses the lower bits of the shift amount. I assume the code used NOT because the hardware doesn't use the upper bits, but that isn't compatible with the LLVM poison semantics. Fixes rust-lang#71142.
1 parent c282d83 commit 50f9198

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/llvm-project/llvm/lib/Target/Mips/MipsISelLowering.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -2593,12 +2593,13 @@ SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
25932593
SDValue Shamt = Op.getOperand(2);
25942594
// if shamt < (VT.bits):
25952595
// lo = (shl lo, shamt)
2596-
// hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2596+
// hi = (or (shl hi, shamt) (srl (srl lo, 1), (xor shamt, (VT.bits-1))))
25972597
// else:
25982598
// lo = 0
25992599
// hi = (shl lo, shamt[4:0])
2600-
SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2601-
DAG.getConstant(-1, DL, MVT::i32));
2600+
SDValue Not =
2601+
DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2602+
DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32));
26022603
SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
26032604
DAG.getConstant(1, DL, VT));
26042605
SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
@@ -2623,7 +2624,7 @@ SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
26232624
MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
26242625

26252626
// if shamt < (VT.bits):
2626-
// lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2627+
// lo = (or (shl (shl hi, 1), (xor shamt, (VT.bits-1))) (srl lo, shamt))
26272628
// if isSRA:
26282629
// hi = (sra hi, shamt)
26292630
// else:
@@ -2635,8 +2636,9 @@ SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
26352636
// else:
26362637
// lo = (srl hi, shamt[4:0])
26372638
// hi = 0
2638-
SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2639-
DAG.getConstant(-1, DL, MVT::i32));
2639+
SDValue Not =
2640+
DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2641+
DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32));
26402642
SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
26412643
DAG.getConstant(1, DL, VT));
26422644
SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);

0 commit comments

Comments
 (0)