Index: include/clang/Basic/Builtins.def =================================================================== --- include/clang/Basic/Builtins.def +++ include/clang/Basic/Builtins.def @@ -683,9 +683,12 @@ LANGBUILTIN(__debugbreak, "v", "n", ALL_MS_LANGUAGES) LANGBUILTIN(__va_start, "vc**.", "nt", ALL_MS_LANGUAGES) LANGBUILTIN(_InterlockedCompareExchange, "LiLiD*LiLi", "n", ALL_MS_LANGUAGES) +LANGBUILTIN(_InterlockedCompareExchangePointer, "v*v*D*v*v*", "n", ALL_MS_LANGUAGES) LANGBUILTIN(_InterlockedIncrement, "LiLiD*", "n", ALL_MS_LANGUAGES) LANGBUILTIN(_InterlockedDecrement, "LiLiD*", "n", ALL_MS_LANGUAGES) LANGBUILTIN(_InterlockedExchangeAdd, "LiLiD*Li", "n", ALL_MS_LANGUAGES) +LANGBUILTIN(_InterlockedExchangePointer, "v*v*D*v*", "n", ALL_MS_LANGUAGES) +LANGBUILTIN(_InterlockedExchange, "LiLiD*Li", "n", ALL_MS_LANGUAGES) // C99 library functions // C99 stdlib.h Index: lib/CodeGen/CGBuiltin.cpp =================================================================== --- lib/CodeGen/CGBuiltin.cpp +++ lib/CodeGen/CGBuiltin.cpp @@ -1516,6 +1516,34 @@ E->getArg(0), true); case Builtin::BI__noop: return RValue::get(nullptr); + case Builtin::BI_InterlockedExchange: + case Builtin::BI_InterlockedExchangePointer: { + return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E); + } + case Builtin::BI_InterlockedCompareExchangePointer: { + llvm::Type *RTy; + llvm::IntegerType *IntType = + IntegerType::get(getLLVMContext(), + getContext().getTypeSize(E->getType())); + llvm::Type *IntPtrType = IntType->getPointerTo(); + + llvm::Value *Destination = + Builder.CreateBitCast(EmitScalarExpr(E->getArg(0)), IntPtrType); + + llvm::Value *Exchange = EmitScalarExpr(E->getArg(1)); + RTy = Exchange->getType(); + Exchange = Builder.CreatePtrToInt(Exchange, CGM.Int32Ty); + + llvm::Value *Comparend = + Builder.CreatePtrToInt(EmitScalarExpr(E->getArg(2)), CGM.Int32Ty); + + Value *Result = Builder.CreateAtomicCmpXchg(Destination, Comparend, + Exchange, + SequentiallyConsistent, + SequentiallyConsistent); + Result = Builder.CreateIntToPtr(Result, RTy); + return RValue::get(Result); + } case Builtin::BI_InterlockedCompareExchange: { AtomicCmpXchgInst *CXI = Builder.CreateAtomicCmpXchg( EmitScalarExpr(E->getArg(0)), Index: lib/Headers/Intrin.h =================================================================== --- lib/Headers/Intrin.h +++ lib/Headers/Intrin.h @@ -223,8 +223,7 @@ long __cdecl _InterlockedDecrement(long volatile *_Addend); static __inline__ short _InterlockedDecrement16(short volatile *_Addend); -static __inline__ -long __cdecl _InterlockedExchange(long volatile *_Target, long _Value); +long _InterlockedExchange(long volatile *_Target, long _Value); static __inline__ short _InterlockedExchange16(short volatile *_Target, short _Value); static __inline__ @@ -411,7 +410,6 @@ __int64); __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination, __int64 _Exchange, __int64 _Comparand); -static __inline__ void *_InterlockedCompareExchangePointer(void *volatile *_Destination, void *_Exchange, void *_Comparand); void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination, @@ -422,7 +420,6 @@ __int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value); static __inline__ __int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value); -static __inline__ void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value); static __inline__ __int64 _InterlockedIncrement64(__int64 volatile *_Addend); @@ -785,22 +782,12 @@ __atomic_exchange(_Target, &_Value, &_Value, 0); return _Value; } -static __inline__ long __attribute__((__always_inline__, __nodebug__)) -_InterlockedExchange(long volatile *_Target, long _Value) { - __atomic_exchange(_Target, &_Value, &_Value, 0); - return _Value; -} #ifdef __x86_64__ static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) { __atomic_exchange(_Target, &_Value, &_Value, 0); return _Value; } -static __inline__ void *__attribute__((__always_inline__, __nodebug__)) -_InterlockedExchangePointer(void *volatile *_Target, void *_Value) { - __atomic_exchange(_Target, &_Value, &_Value, 0); - return _Value; -} #endif /*----------------------------------------------------------------------------*\ |* Interlocked Compare Exchange @@ -817,14 +804,6 @@ __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0); return _Comparand; } -#ifdef __x86_64__ -static __inline__ void *__attribute__((__always_inline__, __nodebug__)) -_InterlockedCompareExchangePointer(void *volatile *_Destination, - void *_Exchange, void *_Comparand) { - __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0); - return _Comparand; -} -#endif static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) _InterlockedCompareExchange64(__int64 volatile *_Destination, __int64 _Exchange, __int64 _Comparand) { Index: test/CodeGen/ms-intrinsics.c =================================================================== --- /dev/null +++ test/CodeGen/ms-intrinsics.c @@ -0,0 +1,40 @@ +// RUN: %clang_cc1 -triple i686--windows -fms-compatibility -Oz -emit-llvm %s -o - | FileCheck %s +// RUN: %clang_cc1 -triple thumbv7--windows -fms-compatibility -Oz -emit-llvm %s -o - | FileCheck %s + +void *test_InterlockedExchangePointer(void * volatile *Target, void *Value) { + return _InterlockedExchangePointer(Target, Value); +} + +// CHECK: define{{.*}}i8* @test_InterlockedExchangePointer(i8** %Target, i8* %Value){{.*}}{ +// CHECK: entry: +// CHECK: %0 = bitcast i8** %Target to i32* +// CHECK: %1 = ptrtoint i8* %Value to i32 +// CHECK: %2 = atomicrmw xchg i32* %0, i32 %1 seq_cst +// CHECK: %3 = inttoptr i32 %2 to i8* +// CHECK: ret i8* %3 +// CHECK: } + +void *test_InterlockedCompareExchangePointer(void * volatile *Destination, + void *Exchange, void *Comparend) { + return _InterlockedCompareExchangePointer(Destination, Exchange, Comparend); +} + +// CHECK: define{{.*}}i8* @test_InterlockedCompareExchangePointer(i8** %Destination, i8* %Exchange, i8* %Comparend){{.*}}{ +// CHECK: entry: +// CHECK: %0 = bitcast i8** %Destination to i32* +// CHECK: %1 = ptrtoint i8* %Exchange to i32 +// CHECK: %2 = ptrtoint i8* %Comparend to i32 +// CHECK: %3 = cmpxchg i32* %0, i32 %2, i32 %1 seq_cst seq_cst +// CHECK: %4 = inttoptr i32 %3 to i8* +// CHECK: ret i8* %4 +// CHECK: } + +long test_InterlockedExchange(long *Target, long Value) { + return _InterlockedExchange(Target, Value); +} + +// CHECK: define{{.*}}i32 @test_InterlockedExchange(i32* %Target, i32 %Value){{.*}}{ +// CHECK: entry: +// CHECK: %0 = atomicrmw xchg i32* %Target, i32 %Value seq_cst +// CHECK: ret i32 %0 +// CHECK: }