Index: llvm/lib/Target/SystemZ/SystemZISelLowering.cpp =================================================================== --- llvm/lib/Target/SystemZ/SystemZISelLowering.cpp +++ llvm/lib/Target/SystemZ/SystemZISelLowering.cpp @@ -7564,7 +7564,6 @@ MachineBasicBlock * SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI, MachineBasicBlock *MBB) const { - MachineFunction &MF = *MBB->getParent(); const SystemZInstrInfo *TII = static_cast(Subtarget.getInstrInfo()); @@ -7574,7 +7573,7 @@ Register Dest = MI.getOperand(0).getReg(); MachineOperand Base = earlyUseOperand(MI.getOperand(1)); int64_t Disp = MI.getOperand(2).getImm(); - Register OrigCmpVal = MI.getOperand(3).getReg(); + Register CmpVal = MI.getOperand(3).getReg(); Register OrigSwapVal = MI.getOperand(4).getReg(); Register BitShift = MI.getOperand(5).getReg(); Register NegBitShift = MI.getOperand(6).getReg(); @@ -7583,19 +7582,19 @@ const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass; - // Get the right opcodes for the displacement. + // Get the right opcodes for the displacement and zero-extension. unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp); unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp); + unsigned ZExtOpcode = BitSize == 8 ? SystemZ::LLCR : SystemZ::LLHR; assert(LOpcode && CSOpcode && "Displacement out of range"); // Create virtual registers for temporary results. Register OrigOldVal = MRI.createVirtualRegister(RC); Register OldVal = MRI.createVirtualRegister(RC); - Register CmpVal = MRI.createVirtualRegister(RC); Register SwapVal = MRI.createVirtualRegister(RC); Register StoreVal = MRI.createVirtualRegister(RC); + Register OldValZext = MRI.createVirtualRegister(RC); Register RetryOldVal = MRI.createVirtualRegister(RC); - Register RetryCmpVal = MRI.createVirtualRegister(RC); Register RetrySwapVal = MRI.createVirtualRegister(RC); // Insert 2 basic blocks for the loop. @@ -7617,34 +7616,27 @@ // LoopMBB: // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ] - // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ] // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ] // %Dest = RLL %OldVal, BitSize(%BitShift) // ^^ The low BitSize bits contain the field // of interest. - // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0 - // ^^ Replace the upper 32-BitSize bits of the - // comparison value with those that we loaded, - // so that we can use a full word comparison. - // CR %Dest, %RetryCmpVal + // %OldValZext = LL[CH] %Dest + // CR %OldValZext, %CmpVal // JNE DoneMBB // # Fall through to SetMBB MBB = LoopMBB; BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) .addReg(OrigOldVal).addMBB(StartMBB) .addReg(RetryOldVal).addMBB(SetMBB); - BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal) - .addReg(OrigCmpVal).addMBB(StartMBB) - .addReg(RetryCmpVal).addMBB(SetMBB); BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal) .addReg(OrigSwapVal).addMBB(StartMBB) .addReg(RetrySwapVal).addMBB(SetMBB); BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest) .addReg(OldVal).addReg(BitShift).addImm(BitSize); - BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal) - .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); + BuildMI(MBB, DL, TII->get(ZExtOpcode), OldValZext) + .addReg(Dest); BuildMI(MBB, DL, TII->get(SystemZ::CR)) - .addReg(Dest).addReg(RetryCmpVal); + .addReg(OldValZext).addReg(CmpVal); BuildMI(MBB, DL, TII->get(SystemZ::BRC)) .addImm(SystemZ::CCMASK_ICMP) .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB); @@ -7653,11 +7645,11 @@ // SetMBB: // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0 - // ^^ Replace the upper 32-BitSize bits of the new - // value with those that we loaded. - // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift) + // ^^ Replace the upper 32-BitSize bits of the + // swap value with those that we loaded and rotated. + // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift) // ^^ Rotate the new field to its proper position. - // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base) + // %RetryOldVal = CS %OldVal, %StoreVal, Disp(%Base) // JNE LoopMBB // # fall through to ExitMMB MBB = SetMBB; Index: llvm/test/CodeGen/SystemZ/cmpxchg-01.ll =================================================================== --- llvm/test/CodeGen/SystemZ/cmpxchg-01.ll +++ llvm/test/CodeGen/SystemZ/cmpxchg-01.ll @@ -17,8 +17,8 @@ ; CHECK-MAIN-DAG: l [[OLD:%r[0-9]+]], 0([[RISBG]]) ; CHECK-MAIN: [[LOOP:\.[^ ]*]]: ; CHECK-MAIN: rll %r2, [[OLD]], 8(%r3) -; CHECK-MAIN: risbg %r4, %r2, 32, 55, 0 -; CHECK-MAIN: crjlh %r2, %r4, [[EXIT:\.[^ ]*]] +; CHECK-MAIN: llcr %r13, %r2 +; CHECK-MAIN: crjlh %r13, %r4, [[EXIT:\.[^ ]*]] ; CHECK-MAIN: risbg %r5, %r2, 32, 55, 0 ; CHECK-MAIN: rll [[NEW:%r[0-9]+]], %r5, -8({{%r[1-9]+}}) ; CHECK-MAIN: cs [[OLD]], [[NEW]], 0([[RISBG]]) @@ -64,8 +64,8 @@ ; CHECK-MAIN-DAG: l [[OLD:%r[0-9]+]], 0([[RISBG]]) ; CHECK-MAIN: [[LOOP:\.[^ ]*]]: ; CHECK-MAIN: rll [[TMP:%r[0-9]+]], [[OLD]], 8(%r3) -; CHECK-MAIN: risbg %r4, [[TMP]], 32, 55, 0 -; CHECK-MAIN: cr [[TMP]], %r4 +; CHECK-MAIN: llcr [[TMP2:%r[0-9]+]], [[TMP]] +; CHECK-MAIN: cr [[TMP2]], %r4 ; CHECK-MAIN: jlh [[EXIT:\.[^ ]*]] ; CHECK-MAIN: risbg %r5, [[TMP]], 32, 55, 0 ; CHECK-MAIN: rll [[NEW:%r[0-9]+]], %r5, -8({{%r[1-9]+}})