@@ -522,8 +522,15 @@ Value *EmitAtomicCmpXchgForMSIntrin(CodeGenFunction &CGF, const CallExpr *E,
522
522
523
523
Address DestAddr = CheckAtomicAlignment(CGF, E);
524
524
525
- auto *Comparand = CGF.EmitScalarExpr(E->getArg(2));
526
525
auto *Exchange = CGF.EmitScalarExpr(E->getArg(1));
526
+ auto *RTy = Exchange->getType();
527
+
528
+ auto *Comparand = CGF.EmitScalarExpr(E->getArg(2));
529
+
530
+ if (RTy->isPointerTy()) {
531
+ Exchange = CGF.Builder.CreatePtrToInt(Exchange, CGF.IntPtrTy);
532
+ Comparand = CGF.Builder.CreatePtrToInt(Comparand, CGF.IntPtrTy);
533
+ }
527
534
528
535
// For Release ordering, the failure ordering should be Monotonic.
529
536
auto FailureOrdering = SuccessOrdering == AtomicOrdering::Release ?
@@ -534,10 +541,16 @@ Value *EmitAtomicCmpXchgForMSIntrin(CodeGenFunction &CGF, const CallExpr *E,
534
541
// blocks the few atomics optimizations that LLVM has. If we want to optimize
535
542
// _Interlocked* operations in the future, we will have to remove the volatile
536
543
// marker.
537
- auto *Result = CGF.Builder.CreateAtomicCmpXchg(
544
+ auto *CmpXchg = CGF.Builder.CreateAtomicCmpXchg(
538
545
DestAddr, Comparand, Exchange, SuccessOrdering, FailureOrdering);
539
- Result->setVolatile(true);
540
- return CGF.Builder.CreateExtractValue(Result, 0);
546
+ CmpXchg->setVolatile(true);
547
+
548
+ auto *Result = CGF.Builder.CreateExtractValue(CmpXchg, 0);
549
+ if (RTy->isPointerTy()) {
550
+ Result = CGF.Builder.CreateIntToPtr(Result, RTy);
551
+ }
552
+
553
+ return Result;
541
554
}
542
555
543
556
// 64-bit Microsoft platforms support 128 bit cmpxchg operations. They are
@@ -1620,6 +1633,7 @@ enum class CodeGenFunction::MSVCIntrin {
1620
1633
_BitScanForward,
1621
1634
_BitScanReverse,
1622
1635
_InterlockedAnd,
1636
+ _InterlockedCompareExchange,
1623
1637
_InterlockedDecrement,
1624
1638
_InterlockedExchange,
1625
1639
_InterlockedExchangeAdd,
@@ -1705,26 +1719,31 @@ translateArmToMsvcIntrin(unsigned BuiltinID) {
1705
1719
case clang::ARM::BI_InterlockedExchange16_acq:
1706
1720
case clang::ARM::BI_InterlockedExchange_acq:
1707
1721
case clang::ARM::BI_InterlockedExchange64_acq:
1722
+ case clang::ARM::BI_InterlockedExchangePointer_acq:
1708
1723
return MSVCIntrin::_InterlockedExchange_acq;
1709
1724
case clang::ARM::BI_InterlockedExchange8_rel:
1710
1725
case clang::ARM::BI_InterlockedExchange16_rel:
1711
1726
case clang::ARM::BI_InterlockedExchange_rel:
1712
1727
case clang::ARM::BI_InterlockedExchange64_rel:
1728
+ case clang::ARM::BI_InterlockedExchangePointer_rel:
1713
1729
return MSVCIntrin::_InterlockedExchange_rel;
1714
1730
case clang::ARM::BI_InterlockedExchange8_nf:
1715
1731
case clang::ARM::BI_InterlockedExchange16_nf:
1716
1732
case clang::ARM::BI_InterlockedExchange_nf:
1717
1733
case clang::ARM::BI_InterlockedExchange64_nf:
1734
+ case clang::ARM::BI_InterlockedExchangePointer_nf:
1718
1735
return MSVCIntrin::_InterlockedExchange_nf;
1719
1736
case clang::ARM::BI_InterlockedCompareExchange8_acq:
1720
1737
case clang::ARM::BI_InterlockedCompareExchange16_acq:
1721
1738
case clang::ARM::BI_InterlockedCompareExchange_acq:
1722
1739
case clang::ARM::BI_InterlockedCompareExchange64_acq:
1740
+ case clang::ARM::BI_InterlockedCompareExchangePointer_acq:
1723
1741
return MSVCIntrin::_InterlockedCompareExchange_acq;
1724
1742
case clang::ARM::BI_InterlockedCompareExchange8_rel:
1725
1743
case clang::ARM::BI_InterlockedCompareExchange16_rel:
1726
1744
case clang::ARM::BI_InterlockedCompareExchange_rel:
1727
1745
case clang::ARM::BI_InterlockedCompareExchange64_rel:
1746
+ case clang::ARM::BI_InterlockedCompareExchangePointer_rel:
1728
1747
return MSVCIntrin::_InterlockedCompareExchange_rel;
1729
1748
case clang::ARM::BI_InterlockedCompareExchange8_nf:
1730
1749
case clang::ARM::BI_InterlockedCompareExchange16_nf:
@@ -1851,26 +1870,31 @@ translateAarch64ToMsvcIntrin(unsigned BuiltinID) {
1851
1870
case clang::AArch64::BI_InterlockedExchange16_acq:
1852
1871
case clang::AArch64::BI_InterlockedExchange_acq:
1853
1872
case clang::AArch64::BI_InterlockedExchange64_acq:
1873
+ case clang::AArch64::BI_InterlockedExchangePointer_acq:
1854
1874
return MSVCIntrin::_InterlockedExchange_acq;
1855
1875
case clang::AArch64::BI_InterlockedExchange8_rel:
1856
1876
case clang::AArch64::BI_InterlockedExchange16_rel:
1857
1877
case clang::AArch64::BI_InterlockedExchange_rel:
1858
1878
case clang::AArch64::BI_InterlockedExchange64_rel:
1879
+ case clang::AArch64::BI_InterlockedExchangePointer_rel:
1859
1880
return MSVCIntrin::_InterlockedExchange_rel;
1860
1881
case clang::AArch64::BI_InterlockedExchange8_nf:
1861
1882
case clang::AArch64::BI_InterlockedExchange16_nf:
1862
1883
case clang::AArch64::BI_InterlockedExchange_nf:
1863
1884
case clang::AArch64::BI_InterlockedExchange64_nf:
1885
+ case clang::AArch64::BI_InterlockedExchangePointer_nf:
1864
1886
return MSVCIntrin::_InterlockedExchange_nf;
1865
1887
case clang::AArch64::BI_InterlockedCompareExchange8_acq:
1866
1888
case clang::AArch64::BI_InterlockedCompareExchange16_acq:
1867
1889
case clang::AArch64::BI_InterlockedCompareExchange_acq:
1868
1890
case clang::AArch64::BI_InterlockedCompareExchange64_acq:
1891
+ case clang::AArch64::BI_InterlockedCompareExchangePointer_acq:
1869
1892
return MSVCIntrin::_InterlockedCompareExchange_acq;
1870
1893
case clang::AArch64::BI_InterlockedCompareExchange8_rel:
1871
1894
case clang::AArch64::BI_InterlockedCompareExchange16_rel:
1872
1895
case clang::AArch64::BI_InterlockedCompareExchange_rel:
1873
1896
case clang::AArch64::BI_InterlockedCompareExchange64_rel:
1897
+ case clang::AArch64::BI_InterlockedCompareExchangePointer_rel:
1874
1898
return MSVCIntrin::_InterlockedCompareExchange_rel;
1875
1899
case clang::AArch64::BI_InterlockedCompareExchange8_nf:
1876
1900
case clang::AArch64::BI_InterlockedCompareExchange16_nf:
@@ -2073,6 +2097,8 @@ Value *CodeGenFunction::EmitMSVCBuiltinExpr(MSVCIntrin BuiltinID,
2073
2097
case MSVCIntrin::_InterlockedExchange_nf:
2074
2098
return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E,
2075
2099
AtomicOrdering::Monotonic);
2100
+ case MSVCIntrin::_InterlockedCompareExchange:
2101
+ return EmitAtomicCmpXchgForMSIntrin(*this, E);
2076
2102
case MSVCIntrin::_InterlockedCompareExchange_acq:
2077
2103
return EmitAtomicCmpXchgForMSIntrin(*this, E, AtomicOrdering::Acquire);
2078
2104
case MSVCIntrin::_InterlockedCompareExchange_rel:
@@ -5720,32 +5746,11 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
5720
5746
return RValue::get(
5721
5747
EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchange, E));
5722
5748
case Builtin::BI_InterlockedCompareExchangePointer:
5723
- case Builtin::BI_InterlockedCompareExchangePointer_nf: {
5724
- llvm::Type *RTy;
5725
- llvm::IntegerType *IntType = IntegerType::get(
5726
- getLLVMContext(), getContext().getTypeSize(E->getType()));
5727
-
5728
- Address DestAddr = CheckAtomicAlignment(*this, E);
5729
-
5730
- llvm::Value *Exchange = EmitScalarExpr(E->getArg(1));
5731
- RTy = Exchange->getType();
5732
- Exchange = Builder.CreatePtrToInt(Exchange, IntType);
5733
-
5734
- llvm::Value *Comparand =
5735
- Builder.CreatePtrToInt(EmitScalarExpr(E->getArg(2)), IntType);
5736
-
5737
- auto Ordering =
5738
- BuiltinID == Builtin::BI_InterlockedCompareExchangePointer_nf ?
5739
- AtomicOrdering::Monotonic : AtomicOrdering::SequentiallyConsistent;
5740
-
5741
- auto Result = Builder.CreateAtomicCmpXchg(DestAddr, Comparand, Exchange,
5742
- Ordering, Ordering);
5743
- Result->setVolatile(true);
5744
-
5745
- return RValue::get(Builder.CreateIntToPtr(Builder.CreateExtractValue(Result,
5746
- 0),
5747
- RTy));
5748
- }
5749
+ return RValue::get(
5750
+ EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedCompareExchange, E));
5751
+ case Builtin::BI_InterlockedCompareExchangePointer_nf:
5752
+ return RValue::get(
5753
+ EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedCompareExchange_nf, E));
5749
5754
case Builtin::BI_InterlockedCompareExchange8:
5750
5755
case Builtin::BI_InterlockedCompareExchange16:
5751
5756
case Builtin::BI_InterlockedCompareExchange:
0 commit comments