Index: lib/CodeGen/SelectionDAG/LegalizeDAG.cpp =================================================================== --- lib/CodeGen/SelectionDAG/LegalizeDAG.cpp +++ lib/CodeGen/SelectionDAG/LegalizeDAG.cpp @@ -2925,12 +2925,12 @@ case ISD::ZERO_EXTEND: LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res, DAG.getValueType(AtomicType)); - RHS = DAG.getNode(ISD::ZERO_EXTEND, dl, OuterType, Node->getOperand(2)); + RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); ExtRes = LHS; break; case ISD::ANY_EXTEND: LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType); - RHS = DAG.getNode(ISD::ZERO_EXTEND, dl, OuterType, Node->getOperand(2)); + RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); break; default: llvm_unreachable("Invalid atomic op extension"); Index: test/CodeGen/ARM/atomic-cmpxchg.ll =================================================================== --- test/CodeGen/ARM/atomic-cmpxchg.ll +++ test/CodeGen/ARM/atomic-cmpxchg.ll @@ -46,9 +46,10 @@ ; CHECK-ARMV6-NEXT: b [[TRY]] ; CHECK-THUMBV6-LABEL: test_cmpxchg_res_i8: -; CHECK-THUMBV6: mov [[EXPECTED:r[0-9]+]], r1 +; CHECK-THUMBV6: mov [[TMP:r[0-9]+]], r1 ; CHECK-THUMBV6-NEXT: bl __sync_val_compare_and_swap_1 ; CHECK-THUMBV6-NEXT: mov [[RES:r[0-9]+]], r0 +; CHECK-THUMBV6-NEXT: uxtb [[EXPECTED:r[0-9]+]], [[TMP]] ; CHECK-THUMBV6-NEXT: movs r0, #1 ; CHECK-THUMBV6-NEXT: movs [[ZERO:r[0-9]+]], #0 ; CHECK-THUMBV6-NEXT: cmp [[RES]], [[EXPECTED]] Index: test/CodeGen/ARM/cmpxchg-O0.ll =================================================================== --- test/CodeGen/ARM/cmpxchg-O0.ll +++ test/CodeGen/ARM/cmpxchg-O0.ll @@ -17,7 +17,8 @@ ; CHECK: cmp{{(\.w)?}} [[STATUS]], #0 ; CHECK: bne [[RETRY]] ; CHECK: [[DONE]]: -; CHECK: cmp{{(\.w)?}} [[OLD]], [[DESIRED]] +; CHECK: uxtb [[TMP:r[0-9]+]], [[DESIRED]] +; CHECK: cmp{{(\.w)?}} [[OLD]], [[TMP]] ; CHECK: {{moveq|movweq}} {{r[0-9]+}}, #1 ; CHECK: dmb ish %res = cmpxchg i8* %addr, i8 %desired, i8 %new seq_cst monotonic @@ -36,7 +37,8 @@ ; CHECK: cmp{{(\.w)?}} [[STATUS]], #0 ; CHECK: bne [[RETRY]] ; CHECK: [[DONE]]: -; CHECK: cmp{{(\.w)?}} [[OLD]], [[DESIRED]] +; CHECK: uxth [[TMP:r[0-9]+]], [[DESIRED]] +; CHECK: cmp{{(\.w)?}} [[OLD]], [[TMP]] ; CHECK: {{moveq|movweq}} {{r[0-9]+}}, #1 ; CHECK: dmb ish %res = cmpxchg i16* %addr, i16 %desired, i16 %new seq_cst monotonic Index: test/CodeGen/SystemZ/cmpxchg-05.ll =================================================================== --- test/CodeGen/SystemZ/cmpxchg-05.ll +++ test/CodeGen/SystemZ/cmpxchg-05.ll @@ -79,3 +79,17 @@ %xres = sext i1 %res to i8 ret i8 %xres } + +; Verify that computing the comparison result extends the input value +; CHECK-LABEL: f7 +; CHECK: llc [[VAL:%r[0-9]+]], 0(%r3) +; CHECK: llcr [[REG:%r[0-9]+]], [[RES:%r[0-9]+]] +; CHECK: cr [[REG]], [[VAL]] +define zeroext i8 @f7(i8* nocapture, i8 *, i8 zeroext) { + %val = load i8, i8 *%1 + %cx = cmpxchg i8* %0, i8 %val, i8 %2 seq_cst seq_cst + %res = extractvalue { i8, i1 } %cx, 1 + %xres = sext i1 %res to i8 + ret i8 %xres +} +