blob: 6a1322424ba7b2e755e29c3bf98429cee03098ea [file] [log] [blame]
John McCallfc207f22013-03-07 21:37:12 +00001//===--- CGAtomic.cpp - Emit LLVM IR for atomic operations ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the code for emitting atomic operations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CGCall.h"
Alexey Bataevb57056f2015-01-22 06:17:56 +000016#include "CGRecordLayout.h"
John McCallfc207f22013-03-07 21:37:12 +000017#include "CodeGenModule.h"
18#include "clang/AST/ASTContext.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000019#include "clang/CodeGen/CGFunctionInfo.h"
Ed Schoutenc7e82bd2013-05-31 19:27:59 +000020#include "llvm/ADT/StringExtras.h"
John McCallfc207f22013-03-07 21:37:12 +000021#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/Intrinsics.h"
John McCalla8ec7eb2013-03-07 21:37:17 +000023#include "llvm/IR/Operator.h"
John McCallfc207f22013-03-07 21:37:12 +000024
25using namespace clang;
26using namespace CodeGen;
27
John McCalla8ec7eb2013-03-07 21:37:17 +000028namespace {
29 class AtomicInfo {
30 CodeGenFunction &CGF;
31 QualType AtomicTy;
32 QualType ValueTy;
33 uint64_t AtomicSizeInBits;
34 uint64_t ValueSizeInBits;
35 CharUnits AtomicAlign;
36 CharUnits ValueAlign;
37 CharUnits LValueAlign;
38 TypeEvaluationKind EvaluationKind;
39 bool UseLibcall;
Alexey Bataevb57056f2015-01-22 06:17:56 +000040 LValue LVal;
41 CGBitFieldInfo BFI;
John McCalla8ec7eb2013-03-07 21:37:17 +000042 public:
Alexey Bataevb57056f2015-01-22 06:17:56 +000043 AtomicInfo(CodeGenFunction &CGF, LValue &lvalue)
44 : CGF(CGF), AtomicSizeInBits(0), ValueSizeInBits(0),
45 EvaluationKind(TEK_Scalar), UseLibcall(true) {
46 assert(!lvalue.isGlobalReg());
John McCalla8ec7eb2013-03-07 21:37:17 +000047 ASTContext &C = CGF.getContext();
Alexey Bataevb57056f2015-01-22 06:17:56 +000048 if (lvalue.isSimple()) {
49 AtomicTy = lvalue.getType();
50 if (auto *ATy = AtomicTy->getAs<AtomicType>())
51 ValueTy = ATy->getValueType();
52 else
53 ValueTy = AtomicTy;
54 EvaluationKind = CGF.getEvaluationKind(ValueTy);
John McCalla8ec7eb2013-03-07 21:37:17 +000055
Alexey Bataevb57056f2015-01-22 06:17:56 +000056 uint64_t ValueAlignInBits;
57 uint64_t AtomicAlignInBits;
58 TypeInfo ValueTI = C.getTypeInfo(ValueTy);
59 ValueSizeInBits = ValueTI.Width;
60 ValueAlignInBits = ValueTI.Align;
John McCalla8ec7eb2013-03-07 21:37:17 +000061
Alexey Bataevb57056f2015-01-22 06:17:56 +000062 TypeInfo AtomicTI = C.getTypeInfo(AtomicTy);
63 AtomicSizeInBits = AtomicTI.Width;
64 AtomicAlignInBits = AtomicTI.Align;
John McCalla8ec7eb2013-03-07 21:37:17 +000065
Alexey Bataevb57056f2015-01-22 06:17:56 +000066 assert(ValueSizeInBits <= AtomicSizeInBits);
67 assert(ValueAlignInBits <= AtomicAlignInBits);
John McCalla8ec7eb2013-03-07 21:37:17 +000068
Alexey Bataevb57056f2015-01-22 06:17:56 +000069 AtomicAlign = C.toCharUnitsFromBits(AtomicAlignInBits);
70 ValueAlign = C.toCharUnitsFromBits(ValueAlignInBits);
71 if (lvalue.getAlignment().isZero())
72 lvalue.setAlignment(AtomicAlign);
John McCalla8ec7eb2013-03-07 21:37:17 +000073
Alexey Bataevb57056f2015-01-22 06:17:56 +000074 LVal = lvalue;
75 } else if (lvalue.isBitField()) {
Alexey Bataevb8329262015-02-27 06:33:30 +000076 ValueTy = lvalue.getType();
77 ValueSizeInBits = C.getTypeSize(ValueTy);
Alexey Bataevb57056f2015-01-22 06:17:56 +000078 auto &OrigBFI = lvalue.getBitFieldInfo();
79 auto Offset = OrigBFI.Offset % C.toBits(lvalue.getAlignment());
80 AtomicSizeInBits = C.toBits(
81 C.toCharUnitsFromBits(Offset + OrigBFI.Size + C.getCharWidth() - 1)
Rui Ueyama83aa9792016-01-14 21:00:27 +000082 .alignTo(lvalue.getAlignment()));
John McCall7f416cc2015-09-08 08:05:57 +000083 auto VoidPtrAddr = CGF.EmitCastToVoidPtr(lvalue.getBitFieldPointer());
Alexey Bataevb57056f2015-01-22 06:17:56 +000084 auto OffsetInChars =
85 (C.toCharUnitsFromBits(OrigBFI.Offset) / lvalue.getAlignment()) *
86 lvalue.getAlignment();
87 VoidPtrAddr = CGF.Builder.CreateConstGEP1_64(
88 VoidPtrAddr, OffsetInChars.getQuantity());
89 auto Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
90 VoidPtrAddr,
91 CGF.Builder.getIntNTy(AtomicSizeInBits)->getPointerTo(),
92 "atomic_bitfield_base");
93 BFI = OrigBFI;
94 BFI.Offset = Offset;
95 BFI.StorageSize = AtomicSizeInBits;
Ulrich Weigand03ce2a12015-07-10 17:30:00 +000096 BFI.StorageOffset += OffsetInChars;
John McCall7f416cc2015-09-08 08:05:57 +000097 LVal = LValue::MakeBitfield(Address(Addr, lvalue.getAlignment()),
98 BFI, lvalue.getType(),
99 lvalue.getAlignmentSource());
Alexey Bataevb8329262015-02-27 06:33:30 +0000100 LVal.setTBAAInfo(lvalue.getTBAAInfo());
101 AtomicTy = C.getIntTypeForBitwidth(AtomicSizeInBits, OrigBFI.IsSigned);
102 if (AtomicTy.isNull()) {
103 llvm::APInt Size(
104 /*numBits=*/32,
105 C.toCharUnitsFromBits(AtomicSizeInBits).getQuantity());
106 AtomicTy = C.getConstantArrayType(C.CharTy, Size, ArrayType::Normal,
107 /*IndexTypeQuals=*/0);
108 }
109 AtomicAlign = ValueAlign = lvalue.getAlignment();
Alexey Bataevb57056f2015-01-22 06:17:56 +0000110 } else if (lvalue.isVectorElt()) {
Alexey Bataevb8329262015-02-27 06:33:30 +0000111 ValueTy = lvalue.getType()->getAs<VectorType>()->getElementType();
112 ValueSizeInBits = C.getTypeSize(ValueTy);
113 AtomicTy = lvalue.getType();
114 AtomicSizeInBits = C.getTypeSize(AtomicTy);
115 AtomicAlign = ValueAlign = lvalue.getAlignment();
Alexey Bataevb57056f2015-01-22 06:17:56 +0000116 LVal = lvalue;
117 } else {
118 assert(lvalue.isExtVectorElt());
Alexey Bataevb8329262015-02-27 06:33:30 +0000119 ValueTy = lvalue.getType();
120 ValueSizeInBits = C.getTypeSize(ValueTy);
121 AtomicTy = ValueTy = CGF.getContext().getExtVectorType(
John McCall7f416cc2015-09-08 08:05:57 +0000122 lvalue.getType(), lvalue.getExtVectorAddress()
123 .getElementType()->getVectorNumElements());
Alexey Bataevb8329262015-02-27 06:33:30 +0000124 AtomicSizeInBits = C.getTypeSize(AtomicTy);
125 AtomicAlign = ValueAlign = lvalue.getAlignment();
Alexey Bataevb57056f2015-01-22 06:17:56 +0000126 LVal = lvalue;
127 }
Alexey Bataev452d8e12014-12-15 05:25:25 +0000128 UseLibcall = !C.getTargetInfo().hasBuiltinAtomic(
129 AtomicSizeInBits, C.toBits(lvalue.getAlignment()));
John McCalla8ec7eb2013-03-07 21:37:17 +0000130 }
131
132 QualType getAtomicType() const { return AtomicTy; }
133 QualType getValueType() const { return ValueTy; }
134 CharUnits getAtomicAlignment() const { return AtomicAlign; }
135 CharUnits getValueAlignment() const { return ValueAlign; }
136 uint64_t getAtomicSizeInBits() const { return AtomicSizeInBits; }
Alexey Bataev452d8e12014-12-15 05:25:25 +0000137 uint64_t getValueSizeInBits() const { return ValueSizeInBits; }
John McCalla8ec7eb2013-03-07 21:37:17 +0000138 TypeEvaluationKind getEvaluationKind() const { return EvaluationKind; }
139 bool shouldUseLibcall() const { return UseLibcall; }
Alexey Bataevb57056f2015-01-22 06:17:56 +0000140 const LValue &getAtomicLValue() const { return LVal; }
John McCall7f416cc2015-09-08 08:05:57 +0000141 llvm::Value *getAtomicPointer() const {
Alexey Bataevb8329262015-02-27 06:33:30 +0000142 if (LVal.isSimple())
John McCall7f416cc2015-09-08 08:05:57 +0000143 return LVal.getPointer();
Alexey Bataevb8329262015-02-27 06:33:30 +0000144 else if (LVal.isBitField())
John McCall7f416cc2015-09-08 08:05:57 +0000145 return LVal.getBitFieldPointer();
Alexey Bataevb8329262015-02-27 06:33:30 +0000146 else if (LVal.isVectorElt())
John McCall7f416cc2015-09-08 08:05:57 +0000147 return LVal.getVectorPointer();
Alexey Bataevb8329262015-02-27 06:33:30 +0000148 assert(LVal.isExtVectorElt());
John McCall7f416cc2015-09-08 08:05:57 +0000149 return LVal.getExtVectorPointer();
150 }
151 Address getAtomicAddress() const {
152 return Address(getAtomicPointer(), getAtomicAlignment());
153 }
154
155 Address getAtomicAddressAsAtomicIntPointer() const {
156 return emitCastToAtomicIntPointer(getAtomicAddress());
Alexey Bataevb8329262015-02-27 06:33:30 +0000157 }
John McCalla8ec7eb2013-03-07 21:37:17 +0000158
159 /// Is the atomic size larger than the underlying value type?
160 ///
161 /// Note that the absence of padding does not mean that atomic
162 /// objects are completely interchangeable with non-atomic
163 /// objects: we might have promoted the alignment of a type
164 /// without making it bigger.
165 bool hasPadding() const {
166 return (ValueSizeInBits != AtomicSizeInBits);
167 }
168
Alexey Bataevb57056f2015-01-22 06:17:56 +0000169 bool emitMemSetZeroIfNecessary() const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000170
171 llvm::Value *getAtomicSizeValue() const {
172 CharUnits size = CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits);
173 return CGF.CGM.getSize(size);
174 }
175
Tim Northovercc2a6e02015-11-09 19:56:35 +0000176 /// Cast the given pointer to an integer pointer suitable for atomic
177 /// operations if the source.
178 Address emitCastToAtomicIntPointer(Address Addr) const;
179
180 /// If Addr is compatible with the iN that will be used for an atomic
181 /// operation, bitcast it. Otherwise, create a temporary that is suitable
182 /// and copy the value across.
183 Address convertToAtomicIntPointer(Address Addr) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000184
185 /// Turn an atomic-layout object into an r-value.
John McCall7f416cc2015-09-08 08:05:57 +0000186 RValue convertAtomicTempToRValue(Address addr, AggValueSlot resultSlot,
187 SourceLocation loc, bool AsValue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000188
Alexey Bataev452d8e12014-12-15 05:25:25 +0000189 /// \brief Converts a rvalue to integer value.
190 llvm::Value *convertRValueToInt(RValue RVal) const;
191
Alexey Bataevb8329262015-02-27 06:33:30 +0000192 RValue ConvertIntToValueOrAtomic(llvm::Value *IntVal,
193 AggValueSlot ResultSlot,
194 SourceLocation Loc, bool AsValue) const;
Alexey Bataev452d8e12014-12-15 05:25:25 +0000195
John McCalla8ec7eb2013-03-07 21:37:17 +0000196 /// Copy an atomic r-value into atomic-layout memory.
Alexey Bataevb57056f2015-01-22 06:17:56 +0000197 void emitCopyIntoMemory(RValue rvalue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000198
199 /// Project an l-value down to the value field.
Alexey Bataevb57056f2015-01-22 06:17:56 +0000200 LValue projectValue() const {
201 assert(LVal.isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000202 Address addr = getAtomicAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +0000203 if (hasPadding())
John McCall7f416cc2015-09-08 08:05:57 +0000204 addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
John McCalla8ec7eb2013-03-07 21:37:17 +0000205
John McCall7f416cc2015-09-08 08:05:57 +0000206 return LValue::MakeAddr(addr, getValueType(), CGF.getContext(),
207 LVal.getAlignmentSource(), LVal.getTBAAInfo());
John McCalla8ec7eb2013-03-07 21:37:17 +0000208 }
209
Alexey Bataevb8329262015-02-27 06:33:30 +0000210 /// \brief Emits atomic load.
211 /// \returns Loaded value.
212 RValue EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
213 bool AsValue, llvm::AtomicOrdering AO,
214 bool IsVolatile);
215
216 /// \brief Emits atomic compare-and-exchange sequence.
217 /// \param Expected Expected value.
218 /// \param Desired Desired value.
219 /// \param Success Atomic ordering for success operation.
220 /// \param Failure Atomic ordering for failed operation.
221 /// \param IsWeak true if atomic operation is weak, false otherwise.
222 /// \returns Pair of values: previous value from storage (value type) and
223 /// boolean flag (i1 type) with true if success and false otherwise.
JF Bastien92f4ef12016-04-06 17:26:42 +0000224 std::pair<RValue, llvm::Value *>
225 EmitAtomicCompareExchange(RValue Expected, RValue Desired,
226 llvm::AtomicOrdering Success =
227 llvm::AtomicOrdering::SequentiallyConsistent,
228 llvm::AtomicOrdering Failure =
229 llvm::AtomicOrdering::SequentiallyConsistent,
230 bool IsWeak = false);
Alexey Bataevb8329262015-02-27 06:33:30 +0000231
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000232 /// \brief Emits atomic update.
NAKAMURA Takumid16af5d2015-05-15 13:47:52 +0000233 /// \param AO Atomic ordering.
234 /// \param UpdateOp Update operation for the current lvalue.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000235 void EmitAtomicUpdate(llvm::AtomicOrdering AO,
236 const llvm::function_ref<RValue(RValue)> &UpdateOp,
237 bool IsVolatile);
238 /// \brief Emits atomic update.
NAKAMURA Takumid16af5d2015-05-15 13:47:52 +0000239 /// \param AO Atomic ordering.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000240 void EmitAtomicUpdate(llvm::AtomicOrdering AO, RValue UpdateRVal,
241 bool IsVolatile);
242
John McCalla8ec7eb2013-03-07 21:37:17 +0000243 /// Materialize an atomic r-value in atomic-layout memory.
John McCall7f416cc2015-09-08 08:05:57 +0000244 Address materializeRValue(RValue rvalue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000245
Alexey Bataevb8329262015-02-27 06:33:30 +0000246 /// \brief Translates LLVM atomic ordering to GNU atomic ordering for
247 /// libcalls.
248 static AtomicExpr::AtomicOrderingKind
249 translateAtomicOrdering(const llvm::AtomicOrdering AO);
250
Tim Northovercc2a6e02015-11-09 19:56:35 +0000251 /// \brief Creates temp alloca for intermediate operations on atomic value.
252 Address CreateTempAlloca() const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000253 private:
254 bool requiresMemSetZero(llvm::Type *type) const;
Alexey Bataevb8329262015-02-27 06:33:30 +0000255
Alexey Bataevb8329262015-02-27 06:33:30 +0000256
257 /// \brief Emits atomic load as a libcall.
258 void EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
259 llvm::AtomicOrdering AO, bool IsVolatile);
260 /// \brief Emits atomic load as LLVM instruction.
261 llvm::Value *EmitAtomicLoadOp(llvm::AtomicOrdering AO, bool IsVolatile);
262 /// \brief Emits atomic compare-and-exchange op as a libcall.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000263 llvm::Value *EmitAtomicCompareExchangeLibcall(
264 llvm::Value *ExpectedAddr, llvm::Value *DesiredAddr,
JF Bastien92f4ef12016-04-06 17:26:42 +0000265 llvm::AtomicOrdering Success =
266 llvm::AtomicOrdering::SequentiallyConsistent,
267 llvm::AtomicOrdering Failure =
268 llvm::AtomicOrdering::SequentiallyConsistent);
Alexey Bataevb8329262015-02-27 06:33:30 +0000269 /// \brief Emits atomic compare-and-exchange op as LLVM instruction.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000270 std::pair<llvm::Value *, llvm::Value *> EmitAtomicCompareExchangeOp(
271 llvm::Value *ExpectedVal, llvm::Value *DesiredVal,
JF Bastien92f4ef12016-04-06 17:26:42 +0000272 llvm::AtomicOrdering Success =
273 llvm::AtomicOrdering::SequentiallyConsistent,
274 llvm::AtomicOrdering Failure =
275 llvm::AtomicOrdering::SequentiallyConsistent,
Alexey Bataevb8329262015-02-27 06:33:30 +0000276 bool IsWeak = false);
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000277 /// \brief Emit atomic update as libcalls.
278 void
279 EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO,
280 const llvm::function_ref<RValue(RValue)> &UpdateOp,
281 bool IsVolatile);
282 /// \brief Emit atomic update as LLVM instructions.
283 void EmitAtomicUpdateOp(llvm::AtomicOrdering AO,
284 const llvm::function_ref<RValue(RValue)> &UpdateOp,
285 bool IsVolatile);
286 /// \brief Emit atomic update as libcalls.
287 void EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO, RValue UpdateRVal,
288 bool IsVolatile);
289 /// \brief Emit atomic update as LLVM instructions.
290 void EmitAtomicUpdateOp(llvm::AtomicOrdering AO, RValue UpdateRal,
291 bool IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +0000292 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000293}
John McCalla8ec7eb2013-03-07 21:37:17 +0000294
Alexey Bataevb8329262015-02-27 06:33:30 +0000295AtomicExpr::AtomicOrderingKind
296AtomicInfo::translateAtomicOrdering(const llvm::AtomicOrdering AO) {
297 switch (AO) {
JF Bastien92f4ef12016-04-06 17:26:42 +0000298 case llvm::AtomicOrdering::Unordered:
299 case llvm::AtomicOrdering::NotAtomic:
300 case llvm::AtomicOrdering::Monotonic:
Alexey Bataevb8329262015-02-27 06:33:30 +0000301 return AtomicExpr::AO_ABI_memory_order_relaxed;
JF Bastien92f4ef12016-04-06 17:26:42 +0000302 case llvm::AtomicOrdering::Acquire:
Alexey Bataevb8329262015-02-27 06:33:30 +0000303 return AtomicExpr::AO_ABI_memory_order_acquire;
JF Bastien92f4ef12016-04-06 17:26:42 +0000304 case llvm::AtomicOrdering::Release:
Alexey Bataevb8329262015-02-27 06:33:30 +0000305 return AtomicExpr::AO_ABI_memory_order_release;
JF Bastien92f4ef12016-04-06 17:26:42 +0000306 case llvm::AtomicOrdering::AcquireRelease:
Alexey Bataevb8329262015-02-27 06:33:30 +0000307 return AtomicExpr::AO_ABI_memory_order_acq_rel;
JF Bastien92f4ef12016-04-06 17:26:42 +0000308 case llvm::AtomicOrdering::SequentiallyConsistent:
Alexey Bataevb8329262015-02-27 06:33:30 +0000309 return AtomicExpr::AO_ABI_memory_order_seq_cst;
310 }
Aaron Ballman152ad172015-02-27 13:55:58 +0000311 llvm_unreachable("Unhandled AtomicOrdering");
Alexey Bataevb8329262015-02-27 06:33:30 +0000312}
313
John McCall7f416cc2015-09-08 08:05:57 +0000314Address AtomicInfo::CreateTempAlloca() const {
315 Address TempAlloca = CGF.CreateMemTemp(
Alexey Bataevb8329262015-02-27 06:33:30 +0000316 (LVal.isBitField() && ValueSizeInBits > AtomicSizeInBits) ? ValueTy
317 : AtomicTy,
John McCall7f416cc2015-09-08 08:05:57 +0000318 getAtomicAlignment(),
Alexey Bataevb8329262015-02-27 06:33:30 +0000319 "atomic-temp");
Alexey Bataevb8329262015-02-27 06:33:30 +0000320 // Cast to pointer to value type for bitfields.
321 if (LVal.isBitField())
322 return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +0000323 TempAlloca, getAtomicAddress().getType());
Alexey Bataevb8329262015-02-27 06:33:30 +0000324 return TempAlloca;
325}
326
John McCalla8ec7eb2013-03-07 21:37:17 +0000327static RValue emitAtomicLibcall(CodeGenFunction &CGF,
328 StringRef fnName,
329 QualType resultType,
330 CallArgList &args) {
331 const CGFunctionInfo &fnInfo =
John McCallc56a8b32016-03-11 04:30:31 +0000332 CGF.CGM.getTypes().arrangeBuiltinFunctionCall(resultType, args);
John McCalla8ec7eb2013-03-07 21:37:17 +0000333 llvm::FunctionType *fnTy = CGF.CGM.getTypes().GetFunctionType(fnInfo);
334 llvm::Constant *fn = CGF.CGM.CreateRuntimeFunction(fnTy, fnName);
335 return CGF.EmitCall(fnInfo, fn, ReturnValueSlot(), args);
336}
337
338/// Does a store of the given IR type modify the full expected width?
339static bool isFullSizeType(CodeGenModule &CGM, llvm::Type *type,
340 uint64_t expectedSize) {
341 return (CGM.getDataLayout().getTypeStoreSize(type) * 8 == expectedSize);
342}
343
344/// Does the atomic type require memsetting to zero before initialization?
345///
346/// The IR type is provided as a way of making certain queries faster.
347bool AtomicInfo::requiresMemSetZero(llvm::Type *type) const {
348 // If the atomic type has size padding, we definitely need a memset.
349 if (hasPadding()) return true;
350
351 // Otherwise, do some simple heuristics to try to avoid it:
352 switch (getEvaluationKind()) {
353 // For scalars and complexes, check whether the store size of the
354 // type uses the full size.
355 case TEK_Scalar:
356 return !isFullSizeType(CGF.CGM, type, AtomicSizeInBits);
357 case TEK_Complex:
358 return !isFullSizeType(CGF.CGM, type->getStructElementType(0),
359 AtomicSizeInBits / 2);
360
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000361 // Padding in structs has an undefined bit pattern. User beware.
John McCalla8ec7eb2013-03-07 21:37:17 +0000362 case TEK_Aggregate:
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000363 return false;
John McCalla8ec7eb2013-03-07 21:37:17 +0000364 }
365 llvm_unreachable("bad evaluation kind");
366}
367
Alexey Bataevb57056f2015-01-22 06:17:56 +0000368bool AtomicInfo::emitMemSetZeroIfNecessary() const {
369 assert(LVal.isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000370 llvm::Value *addr = LVal.getPointer();
John McCalla8ec7eb2013-03-07 21:37:17 +0000371 if (!requiresMemSetZero(addr->getType()->getPointerElementType()))
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000372 return false;
John McCalla8ec7eb2013-03-07 21:37:17 +0000373
Alexey Bataevb8329262015-02-27 06:33:30 +0000374 CGF.Builder.CreateMemSet(
375 addr, llvm::ConstantInt::get(CGF.Int8Ty, 0),
376 CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits).getQuantity(),
377 LVal.getAlignment().getQuantity());
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000378 return true;
John McCalla8ec7eb2013-03-07 21:37:17 +0000379}
380
Tim Northovercadbbe12014-06-13 19:43:04 +0000381static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak,
John McCall7f416cc2015-09-08 08:05:57 +0000382 Address Dest, Address Ptr,
383 Address Val1, Address Val2,
384 uint64_t Size,
Tim Northover9c177222014-03-13 19:25:48 +0000385 llvm::AtomicOrdering SuccessOrder,
386 llvm::AtomicOrdering FailureOrder) {
387 // Note that cmpxchg doesn't support weak cmpxchg, at least at the moment.
John McCall7f416cc2015-09-08 08:05:57 +0000388 llvm::Value *Expected = CGF.Builder.CreateLoad(Val1);
389 llvm::Value *Desired = CGF.Builder.CreateLoad(Val2);
Tim Northover9c177222014-03-13 19:25:48 +0000390
Tim Northoverb49b04b2014-06-13 14:24:59 +0000391 llvm::AtomicCmpXchgInst *Pair = CGF.Builder.CreateAtomicCmpXchg(
John McCall7f416cc2015-09-08 08:05:57 +0000392 Ptr.getPointer(), Expected, Desired, SuccessOrder, FailureOrder);
Tim Northoverb49b04b2014-06-13 14:24:59 +0000393 Pair->setVolatile(E->isVolatile());
Tim Northovercadbbe12014-06-13 19:43:04 +0000394 Pair->setWeak(IsWeak);
Tim Northover9c177222014-03-13 19:25:48 +0000395
396 // Cmp holds the result of the compare-exchange operation: true on success,
397 // false on failure.
Tim Northoverb49b04b2014-06-13 14:24:59 +0000398 llvm::Value *Old = CGF.Builder.CreateExtractValue(Pair, 0);
399 llvm::Value *Cmp = CGF.Builder.CreateExtractValue(Pair, 1);
Tim Northover9c177222014-03-13 19:25:48 +0000400
401 // This basic block is used to hold the store instruction if the operation
402 // failed.
403 llvm::BasicBlock *StoreExpectedBB =
404 CGF.createBasicBlock("cmpxchg.store_expected", CGF.CurFn);
405
406 // This basic block is the exit point of the operation, we should end up
407 // here regardless of whether or not the operation succeeded.
408 llvm::BasicBlock *ContinueBB =
409 CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
410
411 // Update Expected if Expected isn't equal to Old, otherwise branch to the
412 // exit point.
413 CGF.Builder.CreateCondBr(Cmp, ContinueBB, StoreExpectedBB);
414
415 CGF.Builder.SetInsertPoint(StoreExpectedBB);
416 // Update the memory at Expected with Old's value.
John McCall7f416cc2015-09-08 08:05:57 +0000417 CGF.Builder.CreateStore(Old, Val1);
Tim Northover9c177222014-03-13 19:25:48 +0000418 // Finally, branch to the exit point.
419 CGF.Builder.CreateBr(ContinueBB);
420
421 CGF.Builder.SetInsertPoint(ContinueBB);
422 // Update the memory at Dest with Cmp's value.
423 CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType()));
Tim Northover9c177222014-03-13 19:25:48 +0000424}
425
426/// Given an ordering required on success, emit all possible cmpxchg
427/// instructions to cope with the provided (but possibly only dynamically known)
428/// FailureOrder.
429static void emitAtomicCmpXchgFailureSet(CodeGenFunction &CGF, AtomicExpr *E,
John McCall7f416cc2015-09-08 08:05:57 +0000430 bool IsWeak, Address Dest,
431 Address Ptr, Address Val1,
432 Address Val2,
Tim Northover9c177222014-03-13 19:25:48 +0000433 llvm::Value *FailureOrderVal,
John McCall7f416cc2015-09-08 08:05:57 +0000434 uint64_t Size,
Tim Northover9c177222014-03-13 19:25:48 +0000435 llvm::AtomicOrdering SuccessOrder) {
436 llvm::AtomicOrdering FailureOrder;
437 if (llvm::ConstantInt *FO = dyn_cast<llvm::ConstantInt>(FailureOrderVal)) {
438 switch (FO->getSExtValue()) {
439 default:
JF Bastien92f4ef12016-04-06 17:26:42 +0000440 FailureOrder = llvm::AtomicOrdering::Monotonic;
Tim Northover9c177222014-03-13 19:25:48 +0000441 break;
442 case AtomicExpr::AO_ABI_memory_order_consume:
443 case AtomicExpr::AO_ABI_memory_order_acquire:
JF Bastien92f4ef12016-04-06 17:26:42 +0000444 FailureOrder = llvm::AtomicOrdering::Acquire;
Tim Northover9c177222014-03-13 19:25:48 +0000445 break;
446 case AtomicExpr::AO_ABI_memory_order_seq_cst:
JF Bastien92f4ef12016-04-06 17:26:42 +0000447 FailureOrder = llvm::AtomicOrdering::SequentiallyConsistent;
Tim Northover9c177222014-03-13 19:25:48 +0000448 break;
449 }
JF Bastiendd11ee72016-04-06 23:37:36 +0000450 if (isStrongerThan(FailureOrder, SuccessOrder)) {
451 // Don't assert on undefined behavior "failure argument shall be no
452 // stronger than the success argument".
Tim Northover9c177222014-03-13 19:25:48 +0000453 FailureOrder =
454 llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrder);
455 }
John McCall7f416cc2015-09-08 08:05:57 +0000456 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2, Size,
Tim Northovercadbbe12014-06-13 19:43:04 +0000457 SuccessOrder, FailureOrder);
Tim Northover9c177222014-03-13 19:25:48 +0000458 return;
459 }
460
461 // Create all the relevant BB's
Craig Topper8a13c412014-05-21 05:09:00 +0000462 llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
463 *SeqCstBB = nullptr;
Tim Northover9c177222014-03-13 19:25:48 +0000464 MonotonicBB = CGF.createBasicBlock("monotonic_fail", CGF.CurFn);
JF Bastien92f4ef12016-04-06 17:26:42 +0000465 if (SuccessOrder != llvm::AtomicOrdering::Monotonic &&
466 SuccessOrder != llvm::AtomicOrdering::Release)
Tim Northover9c177222014-03-13 19:25:48 +0000467 AcquireBB = CGF.createBasicBlock("acquire_fail", CGF.CurFn);
JF Bastien92f4ef12016-04-06 17:26:42 +0000468 if (SuccessOrder == llvm::AtomicOrdering::SequentiallyConsistent)
Tim Northover9c177222014-03-13 19:25:48 +0000469 SeqCstBB = CGF.createBasicBlock("seqcst_fail", CGF.CurFn);
470
471 llvm::BasicBlock *ContBB = CGF.createBasicBlock("atomic.continue", CGF.CurFn);
472
473 llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(FailureOrderVal, MonotonicBB);
474
475 // Emit all the different atomics
476
477 // MonotonicBB is arbitrarily chosen as the default case; in practice, this
478 // doesn't matter unless someone is crazy enough to use something that
479 // doesn't fold to a constant for the ordering.
480 CGF.Builder.SetInsertPoint(MonotonicBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000481 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
JF Bastien92f4ef12016-04-06 17:26:42 +0000482 Size, SuccessOrder, llvm::AtomicOrdering::Monotonic);
Tim Northover9c177222014-03-13 19:25:48 +0000483 CGF.Builder.CreateBr(ContBB);
484
485 if (AcquireBB) {
486 CGF.Builder.SetInsertPoint(AcquireBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000487 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
JF Bastien92f4ef12016-04-06 17:26:42 +0000488 Size, SuccessOrder, llvm::AtomicOrdering::Acquire);
Tim Northover9c177222014-03-13 19:25:48 +0000489 CGF.Builder.CreateBr(ContBB);
490 SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_consume),
491 AcquireBB);
492 SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acquire),
493 AcquireBB);
494 }
495 if (SeqCstBB) {
496 CGF.Builder.SetInsertPoint(SeqCstBB);
JF Bastien92f4ef12016-04-06 17:26:42 +0000497 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2, Size, SuccessOrder,
498 llvm::AtomicOrdering::SequentiallyConsistent);
Tim Northover9c177222014-03-13 19:25:48 +0000499 CGF.Builder.CreateBr(ContBB);
500 SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_seq_cst),
501 SeqCstBB);
502 }
503
504 CGF.Builder.SetInsertPoint(ContBB);
505}
506
John McCall7f416cc2015-09-08 08:05:57 +0000507static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, Address Dest,
508 Address Ptr, Address Val1, Address Val2,
Tim Northovercadbbe12014-06-13 19:43:04 +0000509 llvm::Value *IsWeak, llvm::Value *FailureOrder,
John McCall7f416cc2015-09-08 08:05:57 +0000510 uint64_t Size, llvm::AtomicOrdering Order) {
John McCallfc207f22013-03-07 21:37:12 +0000511 llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add;
512 llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0;
513
514 switch (E->getOp()) {
515 case AtomicExpr::AO__c11_atomic_init:
516 llvm_unreachable("Already handled!");
517
518 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
Tim Northovercadbbe12014-06-13 19:43:04 +0000519 emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000520 FailureOrder, Size, Order);
John McCallfc207f22013-03-07 21:37:12 +0000521 return;
Tim Northovercadbbe12014-06-13 19:43:04 +0000522 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
523 emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000524 FailureOrder, Size, Order);
Tim Northovercadbbe12014-06-13 19:43:04 +0000525 return;
526 case AtomicExpr::AO__atomic_compare_exchange:
527 case AtomicExpr::AO__atomic_compare_exchange_n: {
528 if (llvm::ConstantInt *IsWeakC = dyn_cast<llvm::ConstantInt>(IsWeak)) {
529 emitAtomicCmpXchgFailureSet(CGF, E, IsWeakC->getZExtValue(), Dest, Ptr,
John McCall7f416cc2015-09-08 08:05:57 +0000530 Val1, Val2, FailureOrder, Size, Order);
Tim Northovercadbbe12014-06-13 19:43:04 +0000531 } else {
532 // Create all the relevant BB's
533 llvm::BasicBlock *StrongBB =
534 CGF.createBasicBlock("cmpxchg.strong", CGF.CurFn);
535 llvm::BasicBlock *WeakBB = CGF.createBasicBlock("cmxchg.weak", CGF.CurFn);
536 llvm::BasicBlock *ContBB =
537 CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
538
539 llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(IsWeak, WeakBB);
540 SI->addCase(CGF.Builder.getInt1(false), StrongBB);
541
542 CGF.Builder.SetInsertPoint(StrongBB);
543 emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000544 FailureOrder, Size, Order);
Tim Northovercadbbe12014-06-13 19:43:04 +0000545 CGF.Builder.CreateBr(ContBB);
546
547 CGF.Builder.SetInsertPoint(WeakBB);
548 emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000549 FailureOrder, Size, Order);
Tim Northovercadbbe12014-06-13 19:43:04 +0000550 CGF.Builder.CreateBr(ContBB);
551
552 CGF.Builder.SetInsertPoint(ContBB);
553 }
554 return;
555 }
John McCallfc207f22013-03-07 21:37:12 +0000556 case AtomicExpr::AO__c11_atomic_load:
557 case AtomicExpr::AO__atomic_load_n:
558 case AtomicExpr::AO__atomic_load: {
559 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr);
560 Load->setAtomic(Order);
John McCallfc207f22013-03-07 21:37:12 +0000561 Load->setVolatile(E->isVolatile());
John McCall7f416cc2015-09-08 08:05:57 +0000562 CGF.Builder.CreateStore(Load, Dest);
John McCallfc207f22013-03-07 21:37:12 +0000563 return;
564 }
565
566 case AtomicExpr::AO__c11_atomic_store:
567 case AtomicExpr::AO__atomic_store:
568 case AtomicExpr::AO__atomic_store_n: {
John McCall7f416cc2015-09-08 08:05:57 +0000569 llvm::Value *LoadVal1 = CGF.Builder.CreateLoad(Val1);
John McCallfc207f22013-03-07 21:37:12 +0000570 llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr);
571 Store->setAtomic(Order);
John McCallfc207f22013-03-07 21:37:12 +0000572 Store->setVolatile(E->isVolatile());
573 return;
574 }
575
576 case AtomicExpr::AO__c11_atomic_exchange:
577 case AtomicExpr::AO__atomic_exchange_n:
578 case AtomicExpr::AO__atomic_exchange:
579 Op = llvm::AtomicRMWInst::Xchg;
580 break;
581
582 case AtomicExpr::AO__atomic_add_fetch:
583 PostOp = llvm::Instruction::Add;
584 // Fall through.
585 case AtomicExpr::AO__c11_atomic_fetch_add:
586 case AtomicExpr::AO__atomic_fetch_add:
587 Op = llvm::AtomicRMWInst::Add;
588 break;
589
590 case AtomicExpr::AO__atomic_sub_fetch:
591 PostOp = llvm::Instruction::Sub;
592 // Fall through.
593 case AtomicExpr::AO__c11_atomic_fetch_sub:
594 case AtomicExpr::AO__atomic_fetch_sub:
595 Op = llvm::AtomicRMWInst::Sub;
596 break;
597
598 case AtomicExpr::AO__atomic_and_fetch:
599 PostOp = llvm::Instruction::And;
600 // Fall through.
601 case AtomicExpr::AO__c11_atomic_fetch_and:
602 case AtomicExpr::AO__atomic_fetch_and:
603 Op = llvm::AtomicRMWInst::And;
604 break;
605
606 case AtomicExpr::AO__atomic_or_fetch:
607 PostOp = llvm::Instruction::Or;
608 // Fall through.
609 case AtomicExpr::AO__c11_atomic_fetch_or:
610 case AtomicExpr::AO__atomic_fetch_or:
611 Op = llvm::AtomicRMWInst::Or;
612 break;
613
614 case AtomicExpr::AO__atomic_xor_fetch:
615 PostOp = llvm::Instruction::Xor;
616 // Fall through.
617 case AtomicExpr::AO__c11_atomic_fetch_xor:
618 case AtomicExpr::AO__atomic_fetch_xor:
619 Op = llvm::AtomicRMWInst::Xor;
620 break;
621
622 case AtomicExpr::AO__atomic_nand_fetch:
James Y Knight7aefb5b2015-11-12 18:37:29 +0000623 PostOp = llvm::Instruction::And; // the NOT is special cased below
624 // Fall through.
John McCallfc207f22013-03-07 21:37:12 +0000625 case AtomicExpr::AO__atomic_fetch_nand:
626 Op = llvm::AtomicRMWInst::Nand;
627 break;
628 }
629
John McCall7f416cc2015-09-08 08:05:57 +0000630 llvm::Value *LoadVal1 = CGF.Builder.CreateLoad(Val1);
John McCallfc207f22013-03-07 21:37:12 +0000631 llvm::AtomicRMWInst *RMWI =
John McCall7f416cc2015-09-08 08:05:57 +0000632 CGF.Builder.CreateAtomicRMW(Op, Ptr.getPointer(), LoadVal1, Order);
John McCallfc207f22013-03-07 21:37:12 +0000633 RMWI->setVolatile(E->isVolatile());
634
635 // For __atomic_*_fetch operations, perform the operation again to
636 // determine the value which was written.
637 llvm::Value *Result = RMWI;
638 if (PostOp)
639 Result = CGF.Builder.CreateBinOp(PostOp, RMWI, LoadVal1);
640 if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch)
641 Result = CGF.Builder.CreateNot(Result);
John McCall7f416cc2015-09-08 08:05:57 +0000642 CGF.Builder.CreateStore(Result, Dest);
John McCallfc207f22013-03-07 21:37:12 +0000643}
644
645// This function emits any expression (scalar, complex, or aggregate)
646// into a temporary alloca.
John McCall7f416cc2015-09-08 08:05:57 +0000647static Address
John McCallfc207f22013-03-07 21:37:12 +0000648EmitValToTemp(CodeGenFunction &CGF, Expr *E) {
John McCall7f416cc2015-09-08 08:05:57 +0000649 Address DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp");
John McCallfc207f22013-03-07 21:37:12 +0000650 CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(),
651 /*Init*/ true);
652 return DeclPtr;
653}
654
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000655static void
656AddDirectArgument(CodeGenFunction &CGF, CallArgList &Args,
Nick Lewycky2d84e842013-10-02 02:29:49 +0000657 bool UseOptimizedLibcall, llvm::Value *Val, QualType ValTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000658 SourceLocation Loc, CharUnits SizeInChars) {
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000659 if (UseOptimizedLibcall) {
660 // Load value and pass it to the function directly.
John McCall7f416cc2015-09-08 08:05:57 +0000661 CharUnits Align = CGF.getContext().getTypeAlignInChars(ValTy);
David Majnemer0392cf82014-08-29 07:27:49 +0000662 int64_t SizeInBits = CGF.getContext().toBits(SizeInChars);
663 ValTy =
664 CGF.getContext().getIntTypeForBitwidth(SizeInBits, /*Signed=*/false);
665 llvm::Type *IPtrTy = llvm::IntegerType::get(CGF.getLLVMContext(),
666 SizeInBits)->getPointerTo();
John McCall7f416cc2015-09-08 08:05:57 +0000667 Address Ptr = Address(CGF.Builder.CreateBitCast(Val, IPtrTy), Align);
668 Val = CGF.EmitLoadOfScalar(Ptr, false,
669 CGF.getContext().getPointerType(ValTy),
David Majnemer0392cf82014-08-29 07:27:49 +0000670 Loc);
671 // Coerce the value into an appropriately sized integer type.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000672 Args.add(RValue::get(Val), ValTy);
673 } else {
674 // Non-optimized functions always take a reference.
675 Args.add(RValue::get(CGF.EmitCastToVoidPtr(Val)),
676 CGF.getContext().VoidPtrTy);
677 }
678}
679
Tim Northovercc2a6e02015-11-09 19:56:35 +0000680RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
John McCallfc207f22013-03-07 21:37:12 +0000681 QualType AtomicTy = E->getPtr()->getType()->getPointeeType();
682 QualType MemTy = AtomicTy;
683 if (const AtomicType *AT = AtomicTy->getAs<AtomicType>())
684 MemTy = AT->getValueType();
John McCall7f416cc2015-09-08 08:05:57 +0000685 CharUnits sizeChars, alignChars;
686 std::tie(sizeChars, alignChars) = getContext().getTypeInfoInChars(AtomicTy);
John McCallfc207f22013-03-07 21:37:12 +0000687 uint64_t Size = sizeChars.getQuantity();
John McCall7f416cc2015-09-08 08:05:57 +0000688 unsigned MaxInlineWidthInBits = getTarget().getMaxAtomicInlineWidth();
689 bool UseLibcall = (sizeChars != alignChars ||
John McCallfc207f22013-03-07 21:37:12 +0000690 getContext().toBits(sizeChars) > MaxInlineWidthInBits);
691
John McCall7f416cc2015-09-08 08:05:57 +0000692 llvm::Value *IsWeak = nullptr, *OrderFail = nullptr;
693
694 Address Val1 = Address::invalid();
695 Address Val2 = Address::invalid();
Tim Northovercc2a6e02015-11-09 19:56:35 +0000696 Address Dest = Address::invalid();
John McCall7f416cc2015-09-08 08:05:57 +0000697 Address Ptr(EmitScalarExpr(E->getPtr()), alignChars);
John McCallfc207f22013-03-07 21:37:12 +0000698
699 if (E->getOp() == AtomicExpr::AO__c11_atomic_init) {
John McCall7f416cc2015-09-08 08:05:57 +0000700 LValue lvalue = MakeAddrLValue(Ptr, AtomicTy);
John McCalla8ec7eb2013-03-07 21:37:17 +0000701 EmitAtomicInit(E->getVal1(), lvalue);
Craig Topper8a13c412014-05-21 05:09:00 +0000702 return RValue::get(nullptr);
John McCallfc207f22013-03-07 21:37:12 +0000703 }
704
Craig Topper8a13c412014-05-21 05:09:00 +0000705 llvm::Value *Order = EmitScalarExpr(E->getOrder());
John McCallfc207f22013-03-07 21:37:12 +0000706
707 switch (E->getOp()) {
708 case AtomicExpr::AO__c11_atomic_init:
James Y Knight81167fb2015-08-05 16:57:36 +0000709 llvm_unreachable("Already handled above with EmitAtomicInit!");
John McCallfc207f22013-03-07 21:37:12 +0000710
711 case AtomicExpr::AO__c11_atomic_load:
712 case AtomicExpr::AO__atomic_load_n:
713 break;
714
715 case AtomicExpr::AO__atomic_load:
John McCall7f416cc2015-09-08 08:05:57 +0000716 Dest = EmitPointerWithAlignment(E->getVal1());
John McCallfc207f22013-03-07 21:37:12 +0000717 break;
718
719 case AtomicExpr::AO__atomic_store:
John McCall7f416cc2015-09-08 08:05:57 +0000720 Val1 = EmitPointerWithAlignment(E->getVal1());
John McCallfc207f22013-03-07 21:37:12 +0000721 break;
722
723 case AtomicExpr::AO__atomic_exchange:
John McCall7f416cc2015-09-08 08:05:57 +0000724 Val1 = EmitPointerWithAlignment(E->getVal1());
725 Dest = EmitPointerWithAlignment(E->getVal2());
John McCallfc207f22013-03-07 21:37:12 +0000726 break;
727
728 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
729 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
730 case AtomicExpr::AO__atomic_compare_exchange_n:
731 case AtomicExpr::AO__atomic_compare_exchange:
John McCall7f416cc2015-09-08 08:05:57 +0000732 Val1 = EmitPointerWithAlignment(E->getVal1());
John McCallfc207f22013-03-07 21:37:12 +0000733 if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange)
John McCall7f416cc2015-09-08 08:05:57 +0000734 Val2 = EmitPointerWithAlignment(E->getVal2());
John McCallfc207f22013-03-07 21:37:12 +0000735 else
736 Val2 = EmitValToTemp(*this, E->getVal2());
737 OrderFail = EmitScalarExpr(E->getOrderFail());
John McCallfc207f22013-03-07 21:37:12 +0000738 if (E->getNumSubExprs() == 6)
Tim Northovercadbbe12014-06-13 19:43:04 +0000739 IsWeak = EmitScalarExpr(E->getWeak());
John McCallfc207f22013-03-07 21:37:12 +0000740 break;
741
742 case AtomicExpr::AO__c11_atomic_fetch_add:
743 case AtomicExpr::AO__c11_atomic_fetch_sub:
744 if (MemTy->isPointerType()) {
745 // For pointer arithmetic, we're required to do a bit of math:
746 // adding 1 to an int* is not the same as adding 1 to a uintptr_t.
747 // ... but only for the C11 builtins. The GNU builtins expect the
748 // user to multiply by sizeof(T).
749 QualType Val1Ty = E->getVal1()->getType();
750 llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1());
751 CharUnits PointeeIncAmt =
752 getContext().getTypeSizeInChars(MemTy->getPointeeType());
753 Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt));
John McCall7f416cc2015-09-08 08:05:57 +0000754 auto Temp = CreateMemTemp(Val1Ty, ".atomictmp");
755 Val1 = Temp;
756 EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Temp, Val1Ty));
John McCallfc207f22013-03-07 21:37:12 +0000757 break;
758 }
759 // Fall through.
760 case AtomicExpr::AO__atomic_fetch_add:
761 case AtomicExpr::AO__atomic_fetch_sub:
762 case AtomicExpr::AO__atomic_add_fetch:
763 case AtomicExpr::AO__atomic_sub_fetch:
764 case AtomicExpr::AO__c11_atomic_store:
765 case AtomicExpr::AO__c11_atomic_exchange:
766 case AtomicExpr::AO__atomic_store_n:
767 case AtomicExpr::AO__atomic_exchange_n:
768 case AtomicExpr::AO__c11_atomic_fetch_and:
769 case AtomicExpr::AO__c11_atomic_fetch_or:
770 case AtomicExpr::AO__c11_atomic_fetch_xor:
771 case AtomicExpr::AO__atomic_fetch_and:
772 case AtomicExpr::AO__atomic_fetch_or:
773 case AtomicExpr::AO__atomic_fetch_xor:
774 case AtomicExpr::AO__atomic_fetch_nand:
775 case AtomicExpr::AO__atomic_and_fetch:
776 case AtomicExpr::AO__atomic_or_fetch:
777 case AtomicExpr::AO__atomic_xor_fetch:
778 case AtomicExpr::AO__atomic_nand_fetch:
779 Val1 = EmitValToTemp(*this, E->getVal1());
780 break;
781 }
782
David Majnemeree8d04d2014-12-12 08:16:09 +0000783 QualType RValTy = E->getType().getUnqualifiedType();
784
Tim Northovercc2a6e02015-11-09 19:56:35 +0000785 // The inlined atomics only function on iN types, where N is a power of 2. We
786 // need to make sure (via temporaries if necessary) that all incoming values
787 // are compatible.
788 LValue AtomicVal = MakeAddrLValue(Ptr, AtomicTy);
789 AtomicInfo Atomics(*this, AtomicVal);
790
791 Ptr = Atomics.emitCastToAtomicIntPointer(Ptr);
792 if (Val1.isValid()) Val1 = Atomics.convertToAtomicIntPointer(Val1);
793 if (Val2.isValid()) Val2 = Atomics.convertToAtomicIntPointer(Val2);
794 if (Dest.isValid())
795 Dest = Atomics.emitCastToAtomicIntPointer(Dest);
796 else if (E->isCmpXChg())
797 Dest = CreateMemTemp(RValTy, "cmpxchg.bool");
798 else if (!RValTy->isVoidType())
799 Dest = Atomics.emitCastToAtomicIntPointer(Atomics.CreateTempAlloca());
John McCallfc207f22013-03-07 21:37:12 +0000800
801 // Use a library call. See: http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary .
802 if (UseLibcall) {
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000803 bool UseOptimizedLibcall = false;
804 switch (E->getOp()) {
James Y Knight81167fb2015-08-05 16:57:36 +0000805 case AtomicExpr::AO__c11_atomic_init:
806 llvm_unreachable("Already handled above with EmitAtomicInit!");
807
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000808 case AtomicExpr::AO__c11_atomic_fetch_add:
809 case AtomicExpr::AO__atomic_fetch_add:
810 case AtomicExpr::AO__c11_atomic_fetch_and:
811 case AtomicExpr::AO__atomic_fetch_and:
812 case AtomicExpr::AO__c11_atomic_fetch_or:
813 case AtomicExpr::AO__atomic_fetch_or:
James Y Knight81167fb2015-08-05 16:57:36 +0000814 case AtomicExpr::AO__atomic_fetch_nand:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000815 case AtomicExpr::AO__c11_atomic_fetch_sub:
816 case AtomicExpr::AO__atomic_fetch_sub:
817 case AtomicExpr::AO__c11_atomic_fetch_xor:
818 case AtomicExpr::AO__atomic_fetch_xor:
James Y Knight81167fb2015-08-05 16:57:36 +0000819 case AtomicExpr::AO__atomic_add_fetch:
820 case AtomicExpr::AO__atomic_and_fetch:
821 case AtomicExpr::AO__atomic_nand_fetch:
822 case AtomicExpr::AO__atomic_or_fetch:
823 case AtomicExpr::AO__atomic_sub_fetch:
824 case AtomicExpr::AO__atomic_xor_fetch:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000825 // For these, only library calls for certain sizes exist.
826 UseOptimizedLibcall = true;
827 break;
James Y Knight81167fb2015-08-05 16:57:36 +0000828
829 case AtomicExpr::AO__c11_atomic_load:
830 case AtomicExpr::AO__c11_atomic_store:
831 case AtomicExpr::AO__c11_atomic_exchange:
832 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
833 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
834 case AtomicExpr::AO__atomic_load_n:
835 case AtomicExpr::AO__atomic_load:
836 case AtomicExpr::AO__atomic_store_n:
837 case AtomicExpr::AO__atomic_store:
838 case AtomicExpr::AO__atomic_exchange_n:
839 case AtomicExpr::AO__atomic_exchange:
840 case AtomicExpr::AO__atomic_compare_exchange_n:
841 case AtomicExpr::AO__atomic_compare_exchange:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000842 // Only use optimized library calls for sizes for which they exist.
843 if (Size == 1 || Size == 2 || Size == 4 || Size == 8)
844 UseOptimizedLibcall = true;
845 break;
846 }
John McCallfc207f22013-03-07 21:37:12 +0000847
John McCallfc207f22013-03-07 21:37:12 +0000848 CallArgList Args;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000849 if (!UseOptimizedLibcall) {
850 // For non-optimized library calls, the size is the first parameter
851 Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)),
852 getContext().getSizeType());
853 }
854 // Atomic address is the first or second parameter
John McCall7f416cc2015-09-08 08:05:57 +0000855 Args.add(RValue::get(EmitCastToVoidPtr(Ptr.getPointer())),
856 getContext().VoidPtrTy);
John McCallfc207f22013-03-07 21:37:12 +0000857
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000858 std::string LibCallName;
Logan Chien74798a32014-03-26 17:35:01 +0000859 QualType LoweredMemTy =
860 MemTy->isPointerType() ? getContext().getIntPtrType() : MemTy;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000861 QualType RetTy;
862 bool HaveRetTy = false;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000863 llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0;
John McCallfc207f22013-03-07 21:37:12 +0000864 switch (E->getOp()) {
James Y Knight81167fb2015-08-05 16:57:36 +0000865 case AtomicExpr::AO__c11_atomic_init:
866 llvm_unreachable("Already handled!");
867
John McCallfc207f22013-03-07 21:37:12 +0000868 // There is only one libcall for compare an exchange, because there is no
869 // optimisation benefit possible from a libcall version of a weak compare
870 // and exchange.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000871 // bool __atomic_compare_exchange(size_t size, void *mem, void *expected,
John McCallfc207f22013-03-07 21:37:12 +0000872 // void *desired, int success, int failure)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000873 // bool __atomic_compare_exchange_N(T *mem, T *expected, T desired,
874 // int success, int failure)
John McCallfc207f22013-03-07 21:37:12 +0000875 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
876 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
877 case AtomicExpr::AO__atomic_compare_exchange:
878 case AtomicExpr::AO__atomic_compare_exchange_n:
879 LibCallName = "__atomic_compare_exchange";
880 RetTy = getContext().BoolTy;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000881 HaveRetTy = true;
John McCall7f416cc2015-09-08 08:05:57 +0000882 Args.add(RValue::get(EmitCastToVoidPtr(Val1.getPointer())),
883 getContext().VoidPtrTy);
884 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val2.getPointer(),
885 MemTy, E->getExprLoc(), sizeChars);
Nick Lewycky5fa40c32013-10-01 21:51:38 +0000886 Args.add(RValue::get(Order), getContext().IntTy);
John McCallfc207f22013-03-07 21:37:12 +0000887 Order = OrderFail;
888 break;
889 // void __atomic_exchange(size_t size, void *mem, void *val, void *return,
890 // int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000891 // T __atomic_exchange_N(T *mem, T val, int order)
John McCallfc207f22013-03-07 21:37:12 +0000892 case AtomicExpr::AO__c11_atomic_exchange:
893 case AtomicExpr::AO__atomic_exchange_n:
894 case AtomicExpr::AO__atomic_exchange:
895 LibCallName = "__atomic_exchange";
John McCall7f416cc2015-09-08 08:05:57 +0000896 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
897 MemTy, E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000898 break;
899 // void __atomic_store(size_t size, void *mem, void *val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000900 // void __atomic_store_N(T *mem, T val, int order)
John McCallfc207f22013-03-07 21:37:12 +0000901 case AtomicExpr::AO__c11_atomic_store:
902 case AtomicExpr::AO__atomic_store:
903 case AtomicExpr::AO__atomic_store_n:
904 LibCallName = "__atomic_store";
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000905 RetTy = getContext().VoidTy;
906 HaveRetTy = true;
John McCall7f416cc2015-09-08 08:05:57 +0000907 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
908 MemTy, E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000909 break;
910 // void __atomic_load(size_t size, void *mem, void *return, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000911 // T __atomic_load_N(T *mem, int order)
John McCallfc207f22013-03-07 21:37:12 +0000912 case AtomicExpr::AO__c11_atomic_load:
913 case AtomicExpr::AO__atomic_load:
914 case AtomicExpr::AO__atomic_load_n:
915 LibCallName = "__atomic_load";
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000916 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000917 // T __atomic_add_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000918 // T __atomic_fetch_add_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +0000919 case AtomicExpr::AO__atomic_add_fetch:
920 PostOp = llvm::Instruction::Add;
921 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000922 case AtomicExpr::AO__c11_atomic_fetch_add:
923 case AtomicExpr::AO__atomic_fetch_add:
924 LibCallName = "__atomic_fetch_add";
John McCall7f416cc2015-09-08 08:05:57 +0000925 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
926 LoweredMemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000927 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000928 // T __atomic_and_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000929 // T __atomic_fetch_and_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +0000930 case AtomicExpr::AO__atomic_and_fetch:
931 PostOp = llvm::Instruction::And;
932 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000933 case AtomicExpr::AO__c11_atomic_fetch_and:
934 case AtomicExpr::AO__atomic_fetch_and:
935 LibCallName = "__atomic_fetch_and";
John McCall7f416cc2015-09-08 08:05:57 +0000936 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
937 MemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000938 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000939 // T __atomic_or_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000940 // T __atomic_fetch_or_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +0000941 case AtomicExpr::AO__atomic_or_fetch:
942 PostOp = llvm::Instruction::Or;
943 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000944 case AtomicExpr::AO__c11_atomic_fetch_or:
945 case AtomicExpr::AO__atomic_fetch_or:
946 LibCallName = "__atomic_fetch_or";
John McCall7f416cc2015-09-08 08:05:57 +0000947 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
948 MemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000949 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000950 // T __atomic_sub_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000951 // T __atomic_fetch_sub_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +0000952 case AtomicExpr::AO__atomic_sub_fetch:
953 PostOp = llvm::Instruction::Sub;
954 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000955 case AtomicExpr::AO__c11_atomic_fetch_sub:
956 case AtomicExpr::AO__atomic_fetch_sub:
957 LibCallName = "__atomic_fetch_sub";
John McCall7f416cc2015-09-08 08:05:57 +0000958 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
959 LoweredMemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000960 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000961 // T __atomic_xor_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000962 // T __atomic_fetch_xor_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +0000963 case AtomicExpr::AO__atomic_xor_fetch:
964 PostOp = llvm::Instruction::Xor;
965 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000966 case AtomicExpr::AO__c11_atomic_fetch_xor:
967 case AtomicExpr::AO__atomic_fetch_xor:
968 LibCallName = "__atomic_fetch_xor";
John McCall7f416cc2015-09-08 08:05:57 +0000969 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
970 MemTy, E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000971 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000972 // T __atomic_nand_fetch_N(T *mem, T val, int order)
James Y Knight81167fb2015-08-05 16:57:36 +0000973 // T __atomic_fetch_nand_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +0000974 case AtomicExpr::AO__atomic_nand_fetch:
975 PostOp = llvm::Instruction::And; // the NOT is special cased below
976 // Fall through.
James Y Knight81167fb2015-08-05 16:57:36 +0000977 case AtomicExpr::AO__atomic_fetch_nand:
978 LibCallName = "__atomic_fetch_nand";
John McCall7f416cc2015-09-08 08:05:57 +0000979 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
980 MemTy, E->getExprLoc(), sizeChars);
James Y Knight81167fb2015-08-05 16:57:36 +0000981 break;
John McCallfc207f22013-03-07 21:37:12 +0000982 }
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000983
984 // Optimized functions have the size in their name.
985 if (UseOptimizedLibcall)
986 LibCallName += "_" + llvm::utostr(Size);
987 // By default, assume we return a value of the atomic type.
988 if (!HaveRetTy) {
989 if (UseOptimizedLibcall) {
990 // Value is returned directly.
David Majnemer0392cf82014-08-29 07:27:49 +0000991 // The function returns an appropriately sized integer type.
992 RetTy = getContext().getIntTypeForBitwidth(
993 getContext().toBits(sizeChars), /*Signed=*/false);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000994 } else {
995 // Value is returned through parameter before the order.
996 RetTy = getContext().VoidTy;
John McCall7f416cc2015-09-08 08:05:57 +0000997 Args.add(RValue::get(EmitCastToVoidPtr(Dest.getPointer())),
998 getContext().VoidPtrTy);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000999 }
1000 }
John McCallfc207f22013-03-07 21:37:12 +00001001 // order is always the last parameter
1002 Args.add(RValue::get(Order),
1003 getContext().IntTy);
1004
James Y Knight7aefb5b2015-11-12 18:37:29 +00001005 // PostOp is only needed for the atomic_*_fetch operations, and
1006 // thus is only needed for and implemented in the
1007 // UseOptimizedLibcall codepath.
1008 assert(UseOptimizedLibcall || !PostOp);
1009
David Majnemer659be552014-11-25 23:44:32 +00001010 RValue Res = emitAtomicLibcall(*this, LibCallName, RetTy, Args);
1011 // The value is returned directly from the libcall.
Tim Northovercc2a6e02015-11-09 19:56:35 +00001012 if (E->isCmpXChg())
David Majnemer659be552014-11-25 23:44:32 +00001013 return Res;
Tim Northovercc2a6e02015-11-09 19:56:35 +00001014
1015 // The value is returned directly for optimized libcalls but the expr
1016 // provided an out-param.
1017 if (UseOptimizedLibcall && Res.getScalarVal()) {
David Majnemer659be552014-11-25 23:44:32 +00001018 llvm::Value *ResVal = Res.getScalarVal();
James Y Knight7aefb5b2015-11-12 18:37:29 +00001019 if (PostOp) {
1020 llvm::Value *LoadVal1 = Args[1].RV.getScalarVal();
1021 ResVal = Builder.CreateBinOp(PostOp, ResVal, LoadVal1);
1022 }
1023 if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch)
1024 ResVal = Builder.CreateNot(ResVal);
1025
Tim Northovercc2a6e02015-11-09 19:56:35 +00001026 Builder.CreateStore(
1027 ResVal,
1028 Builder.CreateBitCast(Dest, ResVal->getType()->getPointerTo()));
David Majnemer659be552014-11-25 23:44:32 +00001029 }
Tim Northovercc2a6e02015-11-09 19:56:35 +00001030
1031 if (RValTy->isVoidType())
1032 return RValue::get(nullptr);
1033
1034 return convertTempToRValue(
1035 Builder.CreateBitCast(Dest, ConvertTypeForMem(RValTy)->getPointerTo()),
1036 RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001037 }
1038
1039 bool IsStore = E->getOp() == AtomicExpr::AO__c11_atomic_store ||
1040 E->getOp() == AtomicExpr::AO__atomic_store ||
1041 E->getOp() == AtomicExpr::AO__atomic_store_n;
1042 bool IsLoad = E->getOp() == AtomicExpr::AO__c11_atomic_load ||
1043 E->getOp() == AtomicExpr::AO__atomic_load ||
1044 E->getOp() == AtomicExpr::AO__atomic_load_n;
1045
John McCallfc207f22013-03-07 21:37:12 +00001046 if (isa<llvm::ConstantInt>(Order)) {
1047 int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
1048 switch (ord) {
Tim Northovere94a34c2014-03-11 10:49:14 +00001049 case AtomicExpr::AO_ABI_memory_order_relaxed:
Tim Northovercadbbe12014-06-13 19:43:04 +00001050 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001051 Size, llvm::AtomicOrdering::Monotonic);
John McCallfc207f22013-03-07 21:37:12 +00001052 break;
Tim Northovere94a34c2014-03-11 10:49:14 +00001053 case AtomicExpr::AO_ABI_memory_order_consume:
1054 case AtomicExpr::AO_ABI_memory_order_acquire:
John McCallfc207f22013-03-07 21:37:12 +00001055 if (IsStore)
1056 break; // Avoid crashing on code with undefined behavior
Tim Northovercadbbe12014-06-13 19:43:04 +00001057 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001058 Size, llvm::AtomicOrdering::Acquire);
John McCallfc207f22013-03-07 21:37:12 +00001059 break;
Tim Northovere94a34c2014-03-11 10:49:14 +00001060 case AtomicExpr::AO_ABI_memory_order_release:
John McCallfc207f22013-03-07 21:37:12 +00001061 if (IsLoad)
1062 break; // Avoid crashing on code with undefined behavior
Tim Northovercadbbe12014-06-13 19:43:04 +00001063 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001064 Size, llvm::AtomicOrdering::Release);
John McCallfc207f22013-03-07 21:37:12 +00001065 break;
Tim Northovere94a34c2014-03-11 10:49:14 +00001066 case AtomicExpr::AO_ABI_memory_order_acq_rel:
John McCallfc207f22013-03-07 21:37:12 +00001067 if (IsLoad || IsStore)
1068 break; // Avoid crashing on code with undefined behavior
Tim Northovercadbbe12014-06-13 19:43:04 +00001069 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001070 Size, llvm::AtomicOrdering::AcquireRelease);
John McCallfc207f22013-03-07 21:37:12 +00001071 break;
Tim Northovere94a34c2014-03-11 10:49:14 +00001072 case AtomicExpr::AO_ABI_memory_order_seq_cst:
Tim Northovercadbbe12014-06-13 19:43:04 +00001073 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001074 Size, llvm::AtomicOrdering::SequentiallyConsistent);
John McCallfc207f22013-03-07 21:37:12 +00001075 break;
1076 default: // invalid order
1077 // We should not ever get here normally, but it's hard to
1078 // enforce that in general.
1079 break;
1080 }
David Majnemeree8d04d2014-12-12 08:16:09 +00001081 if (RValTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +00001082 return RValue::get(nullptr);
Tim Northovercc2a6e02015-11-09 19:56:35 +00001083
1084 return convertTempToRValue(
1085 Builder.CreateBitCast(Dest, ConvertTypeForMem(RValTy)->getPointerTo()),
1086 RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001087 }
1088
1089 // Long case, when Order isn't obviously constant.
1090
1091 // Create all the relevant BB's
Craig Topper8a13c412014-05-21 05:09:00 +00001092 llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
1093 *ReleaseBB = nullptr, *AcqRelBB = nullptr,
1094 *SeqCstBB = nullptr;
John McCallfc207f22013-03-07 21:37:12 +00001095 MonotonicBB = createBasicBlock("monotonic", CurFn);
1096 if (!IsStore)
1097 AcquireBB = createBasicBlock("acquire", CurFn);
1098 if (!IsLoad)
1099 ReleaseBB = createBasicBlock("release", CurFn);
1100 if (!IsLoad && !IsStore)
1101 AcqRelBB = createBasicBlock("acqrel", CurFn);
1102 SeqCstBB = createBasicBlock("seqcst", CurFn);
1103 llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
1104
1105 // Create the switch for the split
1106 // MonotonicBB is arbitrarily chosen as the default case; in practice, this
1107 // doesn't matter unless someone is crazy enough to use something that
1108 // doesn't fold to a constant for the ordering.
1109 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
1110 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB);
1111
1112 // Emit all the different atomics
1113 Builder.SetInsertPoint(MonotonicBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001114 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001115 Size, llvm::AtomicOrdering::Monotonic);
John McCallfc207f22013-03-07 21:37:12 +00001116 Builder.CreateBr(ContBB);
1117 if (!IsStore) {
1118 Builder.SetInsertPoint(AcquireBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001119 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001120 Size, llvm::AtomicOrdering::Acquire);
John McCallfc207f22013-03-07 21:37:12 +00001121 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001122 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_consume),
1123 AcquireBB);
1124 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acquire),
1125 AcquireBB);
John McCallfc207f22013-03-07 21:37:12 +00001126 }
1127 if (!IsLoad) {
1128 Builder.SetInsertPoint(ReleaseBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001129 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001130 Size, llvm::AtomicOrdering::Release);
John McCallfc207f22013-03-07 21:37:12 +00001131 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001132 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_release),
1133 ReleaseBB);
John McCallfc207f22013-03-07 21:37:12 +00001134 }
1135 if (!IsLoad && !IsStore) {
1136 Builder.SetInsertPoint(AcqRelBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001137 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001138 Size, llvm::AtomicOrdering::AcquireRelease);
John McCallfc207f22013-03-07 21:37:12 +00001139 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001140 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acq_rel),
1141 AcqRelBB);
John McCallfc207f22013-03-07 21:37:12 +00001142 }
1143 Builder.SetInsertPoint(SeqCstBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001144 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
JF Bastien92f4ef12016-04-06 17:26:42 +00001145 Size, llvm::AtomicOrdering::SequentiallyConsistent);
John McCallfc207f22013-03-07 21:37:12 +00001146 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001147 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_seq_cst),
1148 SeqCstBB);
John McCallfc207f22013-03-07 21:37:12 +00001149
1150 // Cleanup and return
1151 Builder.SetInsertPoint(ContBB);
David Majnemeree8d04d2014-12-12 08:16:09 +00001152 if (RValTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +00001153 return RValue::get(nullptr);
Tim Northovercc2a6e02015-11-09 19:56:35 +00001154
1155 assert(Atomics.getValueSizeInBits() <= Atomics.getAtomicSizeInBits());
1156 return convertTempToRValue(
1157 Builder.CreateBitCast(Dest, ConvertTypeForMem(RValTy)->getPointerTo()),
1158 RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001159}
John McCalla8ec7eb2013-03-07 21:37:17 +00001160
John McCall7f416cc2015-09-08 08:05:57 +00001161Address AtomicInfo::emitCastToAtomicIntPointer(Address addr) const {
John McCalla8ec7eb2013-03-07 21:37:17 +00001162 unsigned addrspace =
John McCall7f416cc2015-09-08 08:05:57 +00001163 cast<llvm::PointerType>(addr.getPointer()->getType())->getAddressSpace();
John McCalla8ec7eb2013-03-07 21:37:17 +00001164 llvm::IntegerType *ty =
1165 llvm::IntegerType::get(CGF.getLLVMContext(), AtomicSizeInBits);
1166 return CGF.Builder.CreateBitCast(addr, ty->getPointerTo(addrspace));
1167}
1168
Tim Northovercc2a6e02015-11-09 19:56:35 +00001169Address AtomicInfo::convertToAtomicIntPointer(Address Addr) const {
1170 llvm::Type *Ty = Addr.getElementType();
1171 uint64_t SourceSizeInBits = CGF.CGM.getDataLayout().getTypeSizeInBits(Ty);
1172 if (SourceSizeInBits != AtomicSizeInBits) {
1173 Address Tmp = CreateTempAlloca();
1174 CGF.Builder.CreateMemCpy(Tmp, Addr,
1175 std::min(AtomicSizeInBits, SourceSizeInBits) / 8);
1176 Addr = Tmp;
1177 }
1178
1179 return emitCastToAtomicIntPointer(Addr);
1180}
1181
John McCall7f416cc2015-09-08 08:05:57 +00001182RValue AtomicInfo::convertAtomicTempToRValue(Address addr,
1183 AggValueSlot resultSlot,
1184 SourceLocation loc,
1185 bool asValue) const {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001186 if (LVal.isSimple()) {
1187 if (EvaluationKind == TEK_Aggregate)
1188 return resultSlot.asRValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001189
Alexey Bataevb57056f2015-01-22 06:17:56 +00001190 // Drill into the padding structure if we have one.
1191 if (hasPadding())
John McCall7f416cc2015-09-08 08:05:57 +00001192 addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
John McCalla8ec7eb2013-03-07 21:37:17 +00001193
Alexey Bataevb57056f2015-01-22 06:17:56 +00001194 // Otherwise, just convert the temporary to an r-value using the
1195 // normal conversion routine.
1196 return CGF.convertTempToRValue(addr, getValueType(), loc);
David Blaikie1ed728c2015-04-05 22:45:47 +00001197 }
John McCall7f416cc2015-09-08 08:05:57 +00001198 if (!asValue)
Alexey Bataevb8329262015-02-27 06:33:30 +00001199 // Get RValue from temp memory as atomic for non-simple lvalues
John McCall7f416cc2015-09-08 08:05:57 +00001200 return RValue::get(CGF.Builder.CreateLoad(addr));
David Blaikie1ed728c2015-04-05 22:45:47 +00001201 if (LVal.isBitField())
John McCall7f416cc2015-09-08 08:05:57 +00001202 return CGF.EmitLoadOfBitfieldLValue(
1203 LValue::MakeBitfield(addr, LVal.getBitFieldInfo(), LVal.getType(),
1204 LVal.getAlignmentSource()));
David Blaikie1ed728c2015-04-05 22:45:47 +00001205 if (LVal.isVectorElt())
John McCall7f416cc2015-09-08 08:05:57 +00001206 return CGF.EmitLoadOfLValue(
1207 LValue::MakeVectorElt(addr, LVal.getVectorIdx(), LVal.getType(),
1208 LVal.getAlignmentSource()), loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001209 assert(LVal.isExtVectorElt());
1210 return CGF.EmitLoadOfExtVectorElementLValue(LValue::MakeExtVectorElt(
John McCall7f416cc2015-09-08 08:05:57 +00001211 addr, LVal.getExtVectorElts(), LVal.getType(),
1212 LVal.getAlignmentSource()));
John McCalla8ec7eb2013-03-07 21:37:17 +00001213}
1214
Alexey Bataevb8329262015-02-27 06:33:30 +00001215RValue AtomicInfo::ConvertIntToValueOrAtomic(llvm::Value *IntVal,
1216 AggValueSlot ResultSlot,
1217 SourceLocation Loc,
1218 bool AsValue) const {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001219 // Try not to in some easy cases.
1220 assert(IntVal->getType()->isIntegerTy() && "Expected integer value");
Alexey Bataevb8329262015-02-27 06:33:30 +00001221 if (getEvaluationKind() == TEK_Scalar &&
1222 (((!LVal.isBitField() ||
1223 LVal.getBitFieldInfo().Size == ValueSizeInBits) &&
1224 !hasPadding()) ||
1225 !AsValue)) {
1226 auto *ValTy = AsValue
1227 ? CGF.ConvertTypeForMem(ValueTy)
John McCall7f416cc2015-09-08 08:05:57 +00001228 : getAtomicAddress().getType()->getPointerElementType();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001229 if (ValTy->isIntegerTy()) {
1230 assert(IntVal->getType() == ValTy && "Different integer types.");
David Majnemereeaec262015-02-14 02:18:14 +00001231 return RValue::get(CGF.EmitFromMemory(IntVal, ValueTy));
Alexey Bataev452d8e12014-12-15 05:25:25 +00001232 } else if (ValTy->isPointerTy())
1233 return RValue::get(CGF.Builder.CreateIntToPtr(IntVal, ValTy));
1234 else if (llvm::CastInst::isBitCastable(IntVal->getType(), ValTy))
1235 return RValue::get(CGF.Builder.CreateBitCast(IntVal, ValTy));
1236 }
1237
1238 // Create a temporary. This needs to be big enough to hold the
1239 // atomic integer.
John McCall7f416cc2015-09-08 08:05:57 +00001240 Address Temp = Address::invalid();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001241 bool TempIsVolatile = false;
Alexey Bataevb8329262015-02-27 06:33:30 +00001242 if (AsValue && getEvaluationKind() == TEK_Aggregate) {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001243 assert(!ResultSlot.isIgnored());
John McCall7f416cc2015-09-08 08:05:57 +00001244 Temp = ResultSlot.getAddress();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001245 TempIsVolatile = ResultSlot.isVolatile();
1246 } else {
Alexey Bataevb8329262015-02-27 06:33:30 +00001247 Temp = CreateTempAlloca();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001248 }
1249
1250 // Slam the integer into the temporary.
John McCall7f416cc2015-09-08 08:05:57 +00001251 Address CastTemp = emitCastToAtomicIntPointer(Temp);
1252 CGF.Builder.CreateStore(IntVal, CastTemp)
Alexey Bataev452d8e12014-12-15 05:25:25 +00001253 ->setVolatile(TempIsVolatile);
1254
John McCall7f416cc2015-09-08 08:05:57 +00001255 return convertAtomicTempToRValue(Temp, ResultSlot, Loc, AsValue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001256}
1257
1258void AtomicInfo::EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
1259 llvm::AtomicOrdering AO, bool) {
1260 // void __atomic_load(size_t size, void *mem, void *return, int order);
1261 CallArgList Args;
1262 Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
John McCall7f416cc2015-09-08 08:05:57 +00001263 Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
Alexey Bataevb8329262015-02-27 06:33:30 +00001264 CGF.getContext().VoidPtrTy);
1265 Args.add(RValue::get(CGF.EmitCastToVoidPtr(AddForLoaded)),
1266 CGF.getContext().VoidPtrTy);
1267 Args.add(RValue::get(
1268 llvm::ConstantInt::get(CGF.IntTy, translateAtomicOrdering(AO))),
1269 CGF.getContext().IntTy);
1270 emitAtomicLibcall(CGF, "__atomic_load", CGF.getContext().VoidTy, Args);
1271}
1272
1273llvm::Value *AtomicInfo::EmitAtomicLoadOp(llvm::AtomicOrdering AO,
1274 bool IsVolatile) {
1275 // Okay, we're doing this natively.
John McCall7f416cc2015-09-08 08:05:57 +00001276 Address Addr = getAtomicAddressAsAtomicIntPointer();
Alexey Bataevb8329262015-02-27 06:33:30 +00001277 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Addr, "atomic-load");
1278 Load->setAtomic(AO);
1279
1280 // Other decoration.
Alexey Bataevb8329262015-02-27 06:33:30 +00001281 if (IsVolatile)
1282 Load->setVolatile(true);
1283 if (LVal.getTBAAInfo())
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001284 CGF.CGM.DecorateInstructionWithTBAA(Load, LVal.getTBAAInfo());
Alexey Bataevb8329262015-02-27 06:33:30 +00001285 return Load;
Alexey Bataev452d8e12014-12-15 05:25:25 +00001286}
1287
David Majnemera5b195a2015-02-14 01:35:12 +00001288/// An LValue is a candidate for having its loads and stores be made atomic if
1289/// we are operating under /volatile:ms *and* the LValue itself is volatile and
1290/// performing such an operation can be performed without a libcall.
1291bool CodeGenFunction::LValueIsSuitableForInlineAtomic(LValue LV) {
John McCall7f416cc2015-09-08 08:05:57 +00001292 if (!CGM.getCodeGenOpts().MSVolatile) return false;
David Majnemera5b195a2015-02-14 01:35:12 +00001293 AtomicInfo AI(*this, LV);
1294 bool IsVolatile = LV.isVolatile() || hasVolatileMember(LV.getType());
1295 // An atomic is inline if we don't need to use a libcall.
1296 bool AtomicIsInline = !AI.shouldUseLibcall();
John McCall7f416cc2015-09-08 08:05:57 +00001297 return IsVolatile && AtomicIsInline;
David Majnemera5b195a2015-02-14 01:35:12 +00001298}
1299
1300/// An type is a candidate for having its loads and stores be made atomic if
1301/// we are operating under /volatile:ms *and* we know the access is volatile and
1302/// performing such an operation can be performed without a libcall.
1303bool CodeGenFunction::typeIsSuitableForInlineAtomic(QualType Ty,
1304 bool IsVolatile) const {
David Majnemerfc80b6e2016-01-22 16:36:44 +00001305 // The operation must be volatile for us to make it atomic.
1306 if (!IsVolatile)
1307 return false;
1308 // The -fms-volatile flag must be passed for us to adopt this behavior.
1309 if (!CGM.getCodeGenOpts().MSVolatile)
1310 return false;
1311
David Majnemera5b195a2015-02-14 01:35:12 +00001312 // An atomic is inline if we don't need to use a libcall (e.g. it is builtin).
David Majnemerfc80b6e2016-01-22 16:36:44 +00001313 if (!getContext().getTargetInfo().hasBuiltinAtomic(
1314 getContext().getTypeSize(Ty), getContext().getTypeAlign(Ty)))
1315 return false;
1316
1317 // MSVC doesn't seem to do this for types wider than a pointer.
1318 if (getContext().getTypeSize(Ty) >
1319 getContext().getTypeSize(getContext().getIntPtrType()))
1320 return false;
1321 return true;
David Majnemera5b195a2015-02-14 01:35:12 +00001322}
1323
1324RValue CodeGenFunction::EmitAtomicLoad(LValue LV, SourceLocation SL,
1325 AggValueSlot Slot) {
1326 llvm::AtomicOrdering AO;
1327 bool IsVolatile = LV.isVolatileQualified();
1328 if (LV.getType()->isAtomicType()) {
JF Bastien92f4ef12016-04-06 17:26:42 +00001329 AO = llvm::AtomicOrdering::SequentiallyConsistent;
David Majnemera5b195a2015-02-14 01:35:12 +00001330 } else {
JF Bastien92f4ef12016-04-06 17:26:42 +00001331 AO = llvm::AtomicOrdering::Acquire;
David Majnemera5b195a2015-02-14 01:35:12 +00001332 IsVolatile = true;
1333 }
1334 return EmitAtomicLoad(LV, SL, AO, IsVolatile, Slot);
1335}
1336
Alexey Bataevb8329262015-02-27 06:33:30 +00001337RValue AtomicInfo::EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
1338 bool AsValue, llvm::AtomicOrdering AO,
1339 bool IsVolatile) {
1340 // Check whether we should use a library call.
1341 if (shouldUseLibcall()) {
John McCall7f416cc2015-09-08 08:05:57 +00001342 Address TempAddr = Address::invalid();
Alexey Bataevb8329262015-02-27 06:33:30 +00001343 if (LVal.isSimple() && !ResultSlot.isIgnored()) {
1344 assert(getEvaluationKind() == TEK_Aggregate);
John McCall7f416cc2015-09-08 08:05:57 +00001345 TempAddr = ResultSlot.getAddress();
Alexey Bataevb8329262015-02-27 06:33:30 +00001346 } else
1347 TempAddr = CreateTempAlloca();
1348
John McCall7f416cc2015-09-08 08:05:57 +00001349 EmitAtomicLoadLibcall(TempAddr.getPointer(), AO, IsVolatile);
Alexey Bataevb8329262015-02-27 06:33:30 +00001350
1351 // Okay, turn that back into the original value or whole atomic (for
1352 // non-simple lvalues) type.
John McCall7f416cc2015-09-08 08:05:57 +00001353 return convertAtomicTempToRValue(TempAddr, ResultSlot, Loc, AsValue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001354 }
1355
1356 // Okay, we're doing this natively.
1357 auto *Load = EmitAtomicLoadOp(AO, IsVolatile);
1358
1359 // If we're ignoring an aggregate return, don't do anything.
1360 if (getEvaluationKind() == TEK_Aggregate && ResultSlot.isIgnored())
John McCall7f416cc2015-09-08 08:05:57 +00001361 return RValue::getAggregate(Address::invalid(), false);
Alexey Bataevb8329262015-02-27 06:33:30 +00001362
1363 // Okay, turn that back into the original value or atomic (for non-simple
1364 // lvalues) type.
1365 return ConvertIntToValueOrAtomic(Load, ResultSlot, Loc, AsValue);
1366}
1367
John McCalla8ec7eb2013-03-07 21:37:17 +00001368/// Emit a load from an l-value of atomic type. Note that the r-value
1369/// we produce is an r-value of the atomic *value* type.
Nick Lewycky2d84e842013-10-02 02:29:49 +00001370RValue CodeGenFunction::EmitAtomicLoad(LValue src, SourceLocation loc,
David Majnemera5b195a2015-02-14 01:35:12 +00001371 llvm::AtomicOrdering AO, bool IsVolatile,
Nick Lewycky2d84e842013-10-02 02:29:49 +00001372 AggValueSlot resultSlot) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001373 AtomicInfo Atomics(*this, src);
1374 return Atomics.EmitAtomicLoad(resultSlot, loc, /*AsValue=*/true, AO,
1375 IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +00001376}
1377
John McCalla8ec7eb2013-03-07 21:37:17 +00001378/// Copy an r-value into memory as part of storing to an atomic type.
1379/// This needs to create a bit-pattern suitable for atomic operations.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001380void AtomicInfo::emitCopyIntoMemory(RValue rvalue) const {
1381 assert(LVal.isSimple());
John McCalla8ec7eb2013-03-07 21:37:17 +00001382 // If we have an r-value, the rvalue should be of the atomic type,
1383 // which means that the caller is responsible for having zeroed
1384 // any padding. Just do an aggregate copy of that type.
1385 if (rvalue.isAggregate()) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001386 CGF.EmitAggregateCopy(getAtomicAddress(),
John McCall7f416cc2015-09-08 08:05:57 +00001387 rvalue.getAggregateAddress(),
John McCalla8ec7eb2013-03-07 21:37:17 +00001388 getAtomicType(),
1389 (rvalue.isVolatileQualified()
John McCall7f416cc2015-09-08 08:05:57 +00001390 || LVal.isVolatileQualified()));
John McCalla8ec7eb2013-03-07 21:37:17 +00001391 return;
1392 }
1393
1394 // Okay, otherwise we're copying stuff.
1395
1396 // Zero out the buffer if necessary.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001397 emitMemSetZeroIfNecessary();
John McCalla8ec7eb2013-03-07 21:37:17 +00001398
1399 // Drill past the padding if present.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001400 LValue TempLVal = projectValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001401
1402 // Okay, store the rvalue in.
1403 if (rvalue.isScalar()) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001404 CGF.EmitStoreOfScalar(rvalue.getScalarVal(), TempLVal, /*init*/ true);
John McCalla8ec7eb2013-03-07 21:37:17 +00001405 } else {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001406 CGF.EmitStoreOfComplex(rvalue.getComplexVal(), TempLVal, /*init*/ true);
John McCalla8ec7eb2013-03-07 21:37:17 +00001407 }
1408}
1409
1410
1411/// Materialize an r-value into memory for the purposes of storing it
1412/// to an atomic type.
John McCall7f416cc2015-09-08 08:05:57 +00001413Address AtomicInfo::materializeRValue(RValue rvalue) const {
John McCalla8ec7eb2013-03-07 21:37:17 +00001414 // Aggregate r-values are already in memory, and EmitAtomicStore
1415 // requires them to be values of the atomic type.
1416 if (rvalue.isAggregate())
John McCall7f416cc2015-09-08 08:05:57 +00001417 return rvalue.getAggregateAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +00001418
1419 // Otherwise, make a temporary and materialize into it.
John McCall7f416cc2015-09-08 08:05:57 +00001420 LValue TempLV = CGF.MakeAddrLValue(CreateTempAlloca(), getAtomicType());
Alexey Bataevb8329262015-02-27 06:33:30 +00001421 AtomicInfo Atomics(CGF, TempLV);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001422 Atomics.emitCopyIntoMemory(rvalue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001423 return TempLV.getAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +00001424}
1425
Alexey Bataev452d8e12014-12-15 05:25:25 +00001426llvm::Value *AtomicInfo::convertRValueToInt(RValue RVal) const {
1427 // If we've got a scalar value of the right size, try to avoid going
1428 // through memory.
Alexey Bataevb8329262015-02-27 06:33:30 +00001429 if (RVal.isScalar() && (!hasPadding() || !LVal.isSimple())) {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001430 llvm::Value *Value = RVal.getScalarVal();
1431 if (isa<llvm::IntegerType>(Value->getType()))
Alexey Bataevb4505a72015-03-30 05:20:59 +00001432 return CGF.EmitToMemory(Value, ValueTy);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001433 else {
Alexey Bataevb8329262015-02-27 06:33:30 +00001434 llvm::IntegerType *InputIntTy = llvm::IntegerType::get(
1435 CGF.getLLVMContext(),
1436 LVal.isSimple() ? getValueSizeInBits() : getAtomicSizeInBits());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001437 if (isa<llvm::PointerType>(Value->getType()))
1438 return CGF.Builder.CreatePtrToInt(Value, InputIntTy);
1439 else if (llvm::BitCastInst::isBitCastable(Value->getType(), InputIntTy))
1440 return CGF.Builder.CreateBitCast(Value, InputIntTy);
1441 }
1442 }
1443 // Otherwise, we need to go through memory.
1444 // Put the r-value in memory.
John McCall7f416cc2015-09-08 08:05:57 +00001445 Address Addr = materializeRValue(RVal);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001446
1447 // Cast the temporary to the atomic int type and pull a value out.
1448 Addr = emitCastToAtomicIntPointer(Addr);
John McCall7f416cc2015-09-08 08:05:57 +00001449 return CGF.Builder.CreateLoad(Addr);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001450}
1451
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001452std::pair<llvm::Value *, llvm::Value *> AtomicInfo::EmitAtomicCompareExchangeOp(
1453 llvm::Value *ExpectedVal, llvm::Value *DesiredVal,
1454 llvm::AtomicOrdering Success, llvm::AtomicOrdering Failure, bool IsWeak) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001455 // Do the atomic store.
John McCall7f416cc2015-09-08 08:05:57 +00001456 Address Addr = getAtomicAddressAsAtomicIntPointer();
1457 auto *Inst = CGF.Builder.CreateAtomicCmpXchg(Addr.getPointer(),
1458 ExpectedVal, DesiredVal,
Alexey Bataevb4505a72015-03-30 05:20:59 +00001459 Success, Failure);
Alexey Bataevb8329262015-02-27 06:33:30 +00001460 // Other decoration.
1461 Inst->setVolatile(LVal.isVolatileQualified());
1462 Inst->setWeak(IsWeak);
1463
1464 // Okay, turn that back into the original value type.
1465 auto *PreviousVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/0);
1466 auto *SuccessFailureVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/1);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001467 return std::make_pair(PreviousVal, SuccessFailureVal);
Alexey Bataevb8329262015-02-27 06:33:30 +00001468}
1469
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001470llvm::Value *
1471AtomicInfo::EmitAtomicCompareExchangeLibcall(llvm::Value *ExpectedAddr,
1472 llvm::Value *DesiredAddr,
Alexey Bataevb8329262015-02-27 06:33:30 +00001473 llvm::AtomicOrdering Success,
1474 llvm::AtomicOrdering Failure) {
1475 // bool __atomic_compare_exchange(size_t size, void *obj, void *expected,
1476 // void *desired, int success, int failure);
1477 CallArgList Args;
1478 Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
John McCall7f416cc2015-09-08 08:05:57 +00001479 Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
Alexey Bataevb8329262015-02-27 06:33:30 +00001480 CGF.getContext().VoidPtrTy);
1481 Args.add(RValue::get(CGF.EmitCastToVoidPtr(ExpectedAddr)),
1482 CGF.getContext().VoidPtrTy);
1483 Args.add(RValue::get(CGF.EmitCastToVoidPtr(DesiredAddr)),
1484 CGF.getContext().VoidPtrTy);
1485 Args.add(RValue::get(llvm::ConstantInt::get(
1486 CGF.IntTy, translateAtomicOrdering(Success))),
1487 CGF.getContext().IntTy);
1488 Args.add(RValue::get(llvm::ConstantInt::get(
1489 CGF.IntTy, translateAtomicOrdering(Failure))),
1490 CGF.getContext().IntTy);
1491 auto SuccessFailureRVal = emitAtomicLibcall(CGF, "__atomic_compare_exchange",
1492 CGF.getContext().BoolTy, Args);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001493
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001494 return SuccessFailureRVal.getScalarVal();
Alexey Bataevb8329262015-02-27 06:33:30 +00001495}
1496
Alexey Bataevb4505a72015-03-30 05:20:59 +00001497std::pair<RValue, llvm::Value *> AtomicInfo::EmitAtomicCompareExchange(
Alexey Bataevb8329262015-02-27 06:33:30 +00001498 RValue Expected, RValue Desired, llvm::AtomicOrdering Success,
1499 llvm::AtomicOrdering Failure, bool IsWeak) {
JF Bastiendd11ee72016-04-06 23:37:36 +00001500 if (isStrongerThan(Failure, Success))
1501 // Don't assert on undefined behavior "failure argument shall be no stronger
1502 // than the success argument".
Alexey Bataevb8329262015-02-27 06:33:30 +00001503 Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(Success);
1504
1505 // Check whether we should use a library call.
1506 if (shouldUseLibcall()) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001507 // Produce a source address.
John McCall7f416cc2015-09-08 08:05:57 +00001508 Address ExpectedAddr = materializeRValue(Expected);
1509 Address DesiredAddr = materializeRValue(Desired);
1510 auto *Res = EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1511 DesiredAddr.getPointer(),
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001512 Success, Failure);
1513 return std::make_pair(
John McCall7f416cc2015-09-08 08:05:57 +00001514 convertAtomicTempToRValue(ExpectedAddr, AggValueSlot::ignored(),
1515 SourceLocation(), /*AsValue=*/false),
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001516 Res);
Alexey Bataevb8329262015-02-27 06:33:30 +00001517 }
1518
1519 // If we've got a scalar value of the right size, try to avoid going
1520 // through memory.
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001521 auto *ExpectedVal = convertRValueToInt(Expected);
1522 auto *DesiredVal = convertRValueToInt(Desired);
1523 auto Res = EmitAtomicCompareExchangeOp(ExpectedVal, DesiredVal, Success,
1524 Failure, IsWeak);
1525 return std::make_pair(
1526 ConvertIntToValueOrAtomic(Res.first, AggValueSlot::ignored(),
1527 SourceLocation(), /*AsValue=*/false),
1528 Res.second);
1529}
1530
1531static void
1532EmitAtomicUpdateValue(CodeGenFunction &CGF, AtomicInfo &Atomics, RValue OldRVal,
1533 const llvm::function_ref<RValue(RValue)> &UpdateOp,
John McCall7f416cc2015-09-08 08:05:57 +00001534 Address DesiredAddr) {
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001535 RValue UpRVal;
1536 LValue AtomicLVal = Atomics.getAtomicLValue();
1537 LValue DesiredLVal;
1538 if (AtomicLVal.isSimple()) {
1539 UpRVal = OldRVal;
John McCall7f416cc2015-09-08 08:05:57 +00001540 DesiredLVal = CGF.MakeAddrLValue(DesiredAddr, AtomicLVal.getType());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001541 } else {
1542 // Build new lvalue for temp address
John McCall7f416cc2015-09-08 08:05:57 +00001543 Address Ptr = Atomics.materializeRValue(OldRVal);
1544 LValue UpdateLVal;
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001545 if (AtomicLVal.isBitField()) {
1546 UpdateLVal =
1547 LValue::MakeBitfield(Ptr, AtomicLVal.getBitFieldInfo(),
John McCall7f416cc2015-09-08 08:05:57 +00001548 AtomicLVal.getType(),
1549 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001550 DesiredLVal =
1551 LValue::MakeBitfield(DesiredAddr, AtomicLVal.getBitFieldInfo(),
John McCall7f416cc2015-09-08 08:05:57 +00001552 AtomicLVal.getType(),
1553 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001554 } else if (AtomicLVal.isVectorElt()) {
1555 UpdateLVal = LValue::MakeVectorElt(Ptr, AtomicLVal.getVectorIdx(),
1556 AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001557 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001558 DesiredLVal = LValue::MakeVectorElt(
1559 DesiredAddr, AtomicLVal.getVectorIdx(), AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001560 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001561 } else {
1562 assert(AtomicLVal.isExtVectorElt());
1563 UpdateLVal = LValue::MakeExtVectorElt(Ptr, AtomicLVal.getExtVectorElts(),
1564 AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001565 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001566 DesiredLVal = LValue::MakeExtVectorElt(
1567 DesiredAddr, AtomicLVal.getExtVectorElts(), AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001568 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001569 }
1570 UpdateLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1571 DesiredLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1572 UpRVal = CGF.EmitLoadOfLValue(UpdateLVal, SourceLocation());
1573 }
1574 // Store new value in the corresponding memory area
1575 RValue NewRVal = UpdateOp(UpRVal);
1576 if (NewRVal.isScalar()) {
1577 CGF.EmitStoreThroughLValue(NewRVal, DesiredLVal);
1578 } else {
1579 assert(NewRVal.isComplex());
1580 CGF.EmitStoreOfComplex(NewRVal.getComplexVal(), DesiredLVal,
1581 /*isInit=*/false);
1582 }
1583}
1584
1585void AtomicInfo::EmitAtomicUpdateLibcall(
1586 llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1587 bool IsVolatile) {
1588 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1589
John McCall7f416cc2015-09-08 08:05:57 +00001590 Address ExpectedAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001591
John McCall7f416cc2015-09-08 08:05:57 +00001592 EmitAtomicLoadLibcall(ExpectedAddr.getPointer(), AO, IsVolatile);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001593 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1594 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1595 CGF.EmitBlock(ContBB);
John McCall7f416cc2015-09-08 08:05:57 +00001596 Address DesiredAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001597 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001598 requiresMemSetZero(getAtomicAddress().getElementType())) {
1599 auto *OldVal = CGF.Builder.CreateLoad(ExpectedAddr);
1600 CGF.Builder.CreateStore(OldVal, DesiredAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001601 }
John McCall7f416cc2015-09-08 08:05:57 +00001602 auto OldRVal = convertAtomicTempToRValue(ExpectedAddr,
1603 AggValueSlot::ignored(),
1604 SourceLocation(), /*AsValue=*/false);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001605 EmitAtomicUpdateValue(CGF, *this, OldRVal, UpdateOp, DesiredAddr);
1606 auto *Res =
John McCall7f416cc2015-09-08 08:05:57 +00001607 EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1608 DesiredAddr.getPointer(),
1609 AO, Failure);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001610 CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
1611 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1612}
1613
1614void AtomicInfo::EmitAtomicUpdateOp(
1615 llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1616 bool IsVolatile) {
1617 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1618
1619 // Do the atomic load.
1620 auto *OldVal = EmitAtomicLoadOp(AO, IsVolatile);
1621 // For non-simple lvalues perform compare-and-swap procedure.
1622 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1623 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1624 auto *CurBB = CGF.Builder.GetInsertBlock();
1625 CGF.EmitBlock(ContBB);
1626 llvm::PHINode *PHI = CGF.Builder.CreatePHI(OldVal->getType(),
1627 /*NumReservedValues=*/2);
1628 PHI->addIncoming(OldVal, CurBB);
John McCall7f416cc2015-09-08 08:05:57 +00001629 Address NewAtomicAddr = CreateTempAlloca();
1630 Address NewAtomicIntAddr = emitCastToAtomicIntPointer(NewAtomicAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001631 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001632 requiresMemSetZero(getAtomicAddress().getElementType())) {
1633 CGF.Builder.CreateStore(PHI, NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001634 }
1635 auto OldRVal = ConvertIntToValueOrAtomic(PHI, AggValueSlot::ignored(),
1636 SourceLocation(), /*AsValue=*/false);
1637 EmitAtomicUpdateValue(CGF, *this, OldRVal, UpdateOp, NewAtomicAddr);
John McCall7f416cc2015-09-08 08:05:57 +00001638 auto *DesiredVal = CGF.Builder.CreateLoad(NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001639 // Try to write new value using cmpxchg operation
1640 auto Res = EmitAtomicCompareExchangeOp(PHI, DesiredVal, AO, Failure);
1641 PHI->addIncoming(Res.first, CGF.Builder.GetInsertBlock());
1642 CGF.Builder.CreateCondBr(Res.second, ExitBB, ContBB);
1643 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1644}
1645
1646static void EmitAtomicUpdateValue(CodeGenFunction &CGF, AtomicInfo &Atomics,
John McCall7f416cc2015-09-08 08:05:57 +00001647 RValue UpdateRVal, Address DesiredAddr) {
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001648 LValue AtomicLVal = Atomics.getAtomicLValue();
1649 LValue DesiredLVal;
1650 // Build new lvalue for temp address
1651 if (AtomicLVal.isBitField()) {
1652 DesiredLVal =
1653 LValue::MakeBitfield(DesiredAddr, AtomicLVal.getBitFieldInfo(),
John McCall7f416cc2015-09-08 08:05:57 +00001654 AtomicLVal.getType(),
1655 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001656 } else if (AtomicLVal.isVectorElt()) {
1657 DesiredLVal =
1658 LValue::MakeVectorElt(DesiredAddr, AtomicLVal.getVectorIdx(),
John McCall7f416cc2015-09-08 08:05:57 +00001659 AtomicLVal.getType(),
1660 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001661 } else {
1662 assert(AtomicLVal.isExtVectorElt());
1663 DesiredLVal = LValue::MakeExtVectorElt(
1664 DesiredAddr, AtomicLVal.getExtVectorElts(), AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001665 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001666 }
1667 DesiredLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1668 // Store new value in the corresponding memory area
1669 assert(UpdateRVal.isScalar());
1670 CGF.EmitStoreThroughLValue(UpdateRVal, DesiredLVal);
1671}
1672
1673void AtomicInfo::EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO,
1674 RValue UpdateRVal, bool IsVolatile) {
1675 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1676
John McCall7f416cc2015-09-08 08:05:57 +00001677 Address ExpectedAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001678
John McCall7f416cc2015-09-08 08:05:57 +00001679 EmitAtomicLoadLibcall(ExpectedAddr.getPointer(), AO, IsVolatile);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001680 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1681 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1682 CGF.EmitBlock(ContBB);
John McCall7f416cc2015-09-08 08:05:57 +00001683 Address DesiredAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001684 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001685 requiresMemSetZero(getAtomicAddress().getElementType())) {
1686 auto *OldVal = CGF.Builder.CreateLoad(ExpectedAddr);
1687 CGF.Builder.CreateStore(OldVal, DesiredAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001688 }
1689 EmitAtomicUpdateValue(CGF, *this, UpdateRVal, DesiredAddr);
1690 auto *Res =
John McCall7f416cc2015-09-08 08:05:57 +00001691 EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1692 DesiredAddr.getPointer(),
1693 AO, Failure);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001694 CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
1695 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1696}
1697
1698void AtomicInfo::EmitAtomicUpdateOp(llvm::AtomicOrdering AO, RValue UpdateRVal,
1699 bool IsVolatile) {
1700 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1701
1702 // Do the atomic load.
1703 auto *OldVal = EmitAtomicLoadOp(AO, IsVolatile);
1704 // For non-simple lvalues perform compare-and-swap procedure.
1705 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1706 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1707 auto *CurBB = CGF.Builder.GetInsertBlock();
1708 CGF.EmitBlock(ContBB);
1709 llvm::PHINode *PHI = CGF.Builder.CreatePHI(OldVal->getType(),
1710 /*NumReservedValues=*/2);
1711 PHI->addIncoming(OldVal, CurBB);
John McCall7f416cc2015-09-08 08:05:57 +00001712 Address NewAtomicAddr = CreateTempAlloca();
1713 Address NewAtomicIntAddr = emitCastToAtomicIntPointer(NewAtomicAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001714 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001715 requiresMemSetZero(getAtomicAddress().getElementType())) {
1716 CGF.Builder.CreateStore(PHI, NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001717 }
1718 EmitAtomicUpdateValue(CGF, *this, UpdateRVal, NewAtomicAddr);
John McCall7f416cc2015-09-08 08:05:57 +00001719 auto *DesiredVal = CGF.Builder.CreateLoad(NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001720 // Try to write new value using cmpxchg operation
1721 auto Res = EmitAtomicCompareExchangeOp(PHI, DesiredVal, AO, Failure);
1722 PHI->addIncoming(Res.first, CGF.Builder.GetInsertBlock());
1723 CGF.Builder.CreateCondBr(Res.second, ExitBB, ContBB);
1724 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1725}
1726
1727void AtomicInfo::EmitAtomicUpdate(
1728 llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1729 bool IsVolatile) {
1730 if (shouldUseLibcall()) {
1731 EmitAtomicUpdateLibcall(AO, UpdateOp, IsVolatile);
1732 } else {
1733 EmitAtomicUpdateOp(AO, UpdateOp, IsVolatile);
1734 }
1735}
1736
1737void AtomicInfo::EmitAtomicUpdate(llvm::AtomicOrdering AO, RValue UpdateRVal,
1738 bool IsVolatile) {
1739 if (shouldUseLibcall()) {
1740 EmitAtomicUpdateLibcall(AO, UpdateRVal, IsVolatile);
1741 } else {
1742 EmitAtomicUpdateOp(AO, UpdateRVal, IsVolatile);
1743 }
Alexey Bataevb8329262015-02-27 06:33:30 +00001744}
1745
David Majnemera5b195a2015-02-14 01:35:12 +00001746void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue lvalue,
1747 bool isInit) {
1748 bool IsVolatile = lvalue.isVolatileQualified();
1749 llvm::AtomicOrdering AO;
1750 if (lvalue.getType()->isAtomicType()) {
JF Bastien92f4ef12016-04-06 17:26:42 +00001751 AO = llvm::AtomicOrdering::SequentiallyConsistent;
David Majnemera5b195a2015-02-14 01:35:12 +00001752 } else {
JF Bastien92f4ef12016-04-06 17:26:42 +00001753 AO = llvm::AtomicOrdering::Release;
David Majnemera5b195a2015-02-14 01:35:12 +00001754 IsVolatile = true;
1755 }
1756 return EmitAtomicStore(rvalue, lvalue, AO, IsVolatile, isInit);
1757}
1758
John McCalla8ec7eb2013-03-07 21:37:17 +00001759/// Emit a store to an l-value of atomic type.
1760///
1761/// Note that the r-value is expected to be an r-value *of the atomic
1762/// type*; this means that for aggregate r-values, it should include
1763/// storage for any padding that was necessary.
David Majnemera5b195a2015-02-14 01:35:12 +00001764void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue dest,
1765 llvm::AtomicOrdering AO, bool IsVolatile,
1766 bool isInit) {
John McCalla8ec7eb2013-03-07 21:37:17 +00001767 // If this is an aggregate r-value, it should agree in type except
1768 // maybe for address-space qualification.
1769 assert(!rvalue.isAggregate() ||
John McCall7f416cc2015-09-08 08:05:57 +00001770 rvalue.getAggregateAddress().getElementType()
1771 == dest.getAddress().getElementType());
John McCalla8ec7eb2013-03-07 21:37:17 +00001772
1773 AtomicInfo atomics(*this, dest);
Alexey Bataevb8329262015-02-27 06:33:30 +00001774 LValue LVal = atomics.getAtomicLValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001775
1776 // If this is an initialization, just put the value there normally.
Alexey Bataevb8329262015-02-27 06:33:30 +00001777 if (LVal.isSimple()) {
1778 if (isInit) {
1779 atomics.emitCopyIntoMemory(rvalue);
1780 return;
1781 }
1782
1783 // Check whether we should use a library call.
1784 if (atomics.shouldUseLibcall()) {
1785 // Produce a source address.
John McCall7f416cc2015-09-08 08:05:57 +00001786 Address srcAddr = atomics.materializeRValue(rvalue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001787
1788 // void __atomic_store(size_t size, void *mem, void *val, int order)
1789 CallArgList args;
1790 args.add(RValue::get(atomics.getAtomicSizeValue()),
1791 getContext().getSizeType());
John McCall7f416cc2015-09-08 08:05:57 +00001792 args.add(RValue::get(EmitCastToVoidPtr(atomics.getAtomicPointer())),
Alexey Bataevb8329262015-02-27 06:33:30 +00001793 getContext().VoidPtrTy);
John McCall7f416cc2015-09-08 08:05:57 +00001794 args.add(RValue::get(EmitCastToVoidPtr(srcAddr.getPointer())),
1795 getContext().VoidPtrTy);
Alexey Bataevb8329262015-02-27 06:33:30 +00001796 args.add(RValue::get(llvm::ConstantInt::get(
1797 IntTy, AtomicInfo::translateAtomicOrdering(AO))),
1798 getContext().IntTy);
1799 emitAtomicLibcall(*this, "__atomic_store", getContext().VoidTy, args);
1800 return;
1801 }
1802
1803 // Okay, we're doing this natively.
1804 llvm::Value *intValue = atomics.convertRValueToInt(rvalue);
1805
1806 // Do the atomic store.
John McCall7f416cc2015-09-08 08:05:57 +00001807 Address addr =
Alexey Bataevb8329262015-02-27 06:33:30 +00001808 atomics.emitCastToAtomicIntPointer(atomics.getAtomicAddress());
1809 intValue = Builder.CreateIntCast(
John McCall7f416cc2015-09-08 08:05:57 +00001810 intValue, addr.getElementType(), /*isSigned=*/false);
Alexey Bataevb8329262015-02-27 06:33:30 +00001811 llvm::StoreInst *store = Builder.CreateStore(intValue, addr);
1812
1813 // Initializations don't need to be atomic.
1814 if (!isInit)
1815 store->setAtomic(AO);
1816
1817 // Other decoration.
Alexey Bataevb8329262015-02-27 06:33:30 +00001818 if (IsVolatile)
1819 store->setVolatile(true);
1820 if (dest.getTBAAInfo())
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001821 CGM.DecorateInstructionWithTBAA(store, dest.getTBAAInfo());
John McCalla8ec7eb2013-03-07 21:37:17 +00001822 return;
1823 }
1824
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001825 // Emit simple atomic update operation.
1826 atomics.EmitAtomicUpdate(AO, rvalue, IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +00001827}
1828
Alexey Bataev452d8e12014-12-15 05:25:25 +00001829/// Emit a compare-and-exchange op for atomic type.
1830///
Alexey Bataevb4505a72015-03-30 05:20:59 +00001831std::pair<RValue, llvm::Value *> CodeGenFunction::EmitAtomicCompareExchange(
Alexey Bataev452d8e12014-12-15 05:25:25 +00001832 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
1833 llvm::AtomicOrdering Success, llvm::AtomicOrdering Failure, bool IsWeak,
1834 AggValueSlot Slot) {
1835 // If this is an aggregate r-value, it should agree in type except
1836 // maybe for address-space qualification.
1837 assert(!Expected.isAggregate() ||
John McCall7f416cc2015-09-08 08:05:57 +00001838 Expected.getAggregateAddress().getElementType() ==
1839 Obj.getAddress().getElementType());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001840 assert(!Desired.isAggregate() ||
John McCall7f416cc2015-09-08 08:05:57 +00001841 Desired.getAggregateAddress().getElementType() ==
1842 Obj.getAddress().getElementType());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001843 AtomicInfo Atomics(*this, Obj);
1844
Alexey Bataevb4505a72015-03-30 05:20:59 +00001845 return Atomics.EmitAtomicCompareExchange(Expected, Desired, Success, Failure,
1846 IsWeak);
1847}
1848
1849void CodeGenFunction::EmitAtomicUpdate(
1850 LValue LVal, llvm::AtomicOrdering AO,
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001851 const llvm::function_ref<RValue(RValue)> &UpdateOp, bool IsVolatile) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00001852 AtomicInfo Atomics(*this, LVal);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001853 Atomics.EmitAtomicUpdate(AO, UpdateOp, IsVolatile);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001854}
1855
John McCalla8ec7eb2013-03-07 21:37:17 +00001856void CodeGenFunction::EmitAtomicInit(Expr *init, LValue dest) {
1857 AtomicInfo atomics(*this, dest);
1858
1859 switch (atomics.getEvaluationKind()) {
1860 case TEK_Scalar: {
1861 llvm::Value *value = EmitScalarExpr(init);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001862 atomics.emitCopyIntoMemory(RValue::get(value));
John McCalla8ec7eb2013-03-07 21:37:17 +00001863 return;
1864 }
1865
1866 case TEK_Complex: {
1867 ComplexPairTy value = EmitComplexExpr(init);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001868 atomics.emitCopyIntoMemory(RValue::getComplex(value));
John McCalla8ec7eb2013-03-07 21:37:17 +00001869 return;
1870 }
1871
1872 case TEK_Aggregate: {
Eli Friedmanbe4504d2013-07-11 01:32:21 +00001873 // Fix up the destination if the initializer isn't an expression
1874 // of atomic type.
1875 bool Zeroed = false;
John McCalla8ec7eb2013-03-07 21:37:17 +00001876 if (!init->getType()->isAtomicType()) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001877 Zeroed = atomics.emitMemSetZeroIfNecessary();
1878 dest = atomics.projectValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001879 }
1880
1881 // Evaluate the expression directly into the destination.
1882 AggValueSlot slot = AggValueSlot::forLValue(dest,
1883 AggValueSlot::IsNotDestructed,
1884 AggValueSlot::DoesNotNeedGCBarriers,
Eli Friedmanbe4504d2013-07-11 01:32:21 +00001885 AggValueSlot::IsNotAliased,
1886 Zeroed ? AggValueSlot::IsZeroed :
1887 AggValueSlot::IsNotZeroed);
1888
John McCalla8ec7eb2013-03-07 21:37:17 +00001889 EmitAggExpr(init, slot);
1890 return;
1891 }
1892 }
1893 llvm_unreachable("bad evaluation kind");
1894}