blob: d51c4fdb596e75d4b2097eb029a47e2237797876 [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)
82 .RoundUpToAlignment(lvalue.getAlignment()));
83 auto VoidPtrAddr = CGF.EmitCastToVoidPtr(lvalue.getBitFieldAddr());
84 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;
96 LVal = LValue::MakeBitfield(Addr, BFI, lvalue.getType(),
97 lvalue.getAlignment());
Alexey Bataevb8329262015-02-27 06:33:30 +000098 LVal.setTBAAInfo(lvalue.getTBAAInfo());
99 AtomicTy = C.getIntTypeForBitwidth(AtomicSizeInBits, OrigBFI.IsSigned);
100 if (AtomicTy.isNull()) {
101 llvm::APInt Size(
102 /*numBits=*/32,
103 C.toCharUnitsFromBits(AtomicSizeInBits).getQuantity());
104 AtomicTy = C.getConstantArrayType(C.CharTy, Size, ArrayType::Normal,
105 /*IndexTypeQuals=*/0);
106 }
107 AtomicAlign = ValueAlign = lvalue.getAlignment();
Alexey Bataevb57056f2015-01-22 06:17:56 +0000108 } else if (lvalue.isVectorElt()) {
Alexey Bataevb8329262015-02-27 06:33:30 +0000109 ValueTy = lvalue.getType()->getAs<VectorType>()->getElementType();
110 ValueSizeInBits = C.getTypeSize(ValueTy);
111 AtomicTy = lvalue.getType();
112 AtomicSizeInBits = C.getTypeSize(AtomicTy);
113 AtomicAlign = ValueAlign = lvalue.getAlignment();
Alexey Bataevb57056f2015-01-22 06:17:56 +0000114 LVal = lvalue;
115 } else {
116 assert(lvalue.isExtVectorElt());
Alexey Bataevb8329262015-02-27 06:33:30 +0000117 ValueTy = lvalue.getType();
118 ValueSizeInBits = C.getTypeSize(ValueTy);
119 AtomicTy = ValueTy = CGF.getContext().getExtVectorType(
120 lvalue.getType(), lvalue.getExtVectorAddr()
121 ->getType()
122 ->getPointerElementType()
123 ->getVectorNumElements());
124 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; }
Alexey Bataevb8329262015-02-27 06:33:30 +0000141 llvm::Value *getAtomicAddress() const {
142 if (LVal.isSimple())
143 return LVal.getAddress();
144 else if (LVal.isBitField())
145 return LVal.getBitFieldAddr();
146 else if (LVal.isVectorElt())
147 return LVal.getVectorAddr();
148 assert(LVal.isExtVectorElt());
149 return LVal.getExtVectorAddr();
150 }
John McCalla8ec7eb2013-03-07 21:37:17 +0000151
152 /// Is the atomic size larger than the underlying value type?
153 ///
154 /// Note that the absence of padding does not mean that atomic
155 /// objects are completely interchangeable with non-atomic
156 /// objects: we might have promoted the alignment of a type
157 /// without making it bigger.
158 bool hasPadding() const {
159 return (ValueSizeInBits != AtomicSizeInBits);
160 }
161
Alexey Bataevb57056f2015-01-22 06:17:56 +0000162 bool emitMemSetZeroIfNecessary() const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000163
164 llvm::Value *getAtomicSizeValue() const {
165 CharUnits size = CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits);
166 return CGF.CGM.getSize(size);
167 }
168
169 /// Cast the given pointer to an integer pointer suitable for
170 /// atomic operations.
171 llvm::Value *emitCastToAtomicIntPointer(llvm::Value *addr) const;
172
173 /// Turn an atomic-layout object into an r-value.
Alexey Bataevb8329262015-02-27 06:33:30 +0000174 RValue convertTempToRValue(llvm::Value *addr, AggValueSlot resultSlot,
175 SourceLocation loc, bool AsValue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000176
Alexey Bataev452d8e12014-12-15 05:25:25 +0000177 /// \brief Converts a rvalue to integer value.
178 llvm::Value *convertRValueToInt(RValue RVal) const;
179
Alexey Bataevb8329262015-02-27 06:33:30 +0000180 RValue ConvertIntToValueOrAtomic(llvm::Value *IntVal,
181 AggValueSlot ResultSlot,
182 SourceLocation Loc, bool AsValue) const;
Alexey Bataev452d8e12014-12-15 05:25:25 +0000183
John McCalla8ec7eb2013-03-07 21:37:17 +0000184 /// Copy an atomic r-value into atomic-layout memory.
Alexey Bataevb57056f2015-01-22 06:17:56 +0000185 void emitCopyIntoMemory(RValue rvalue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000186
187 /// Project an l-value down to the value field.
Alexey Bataevb57056f2015-01-22 06:17:56 +0000188 LValue projectValue() const {
189 assert(LVal.isSimple());
Alexey Bataevb8329262015-02-27 06:33:30 +0000190 llvm::Value *addr = getAtomicAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +0000191 if (hasPadding())
192 addr = CGF.Builder.CreateStructGEP(addr, 0);
193
Alexey Bataevb57056f2015-01-22 06:17:56 +0000194 return LValue::MakeAddr(addr, getValueType(), LVal.getAlignment(),
195 CGF.getContext(), LVal.getTBAAInfo());
John McCalla8ec7eb2013-03-07 21:37:17 +0000196 }
197
Alexey Bataevb8329262015-02-27 06:33:30 +0000198 /// \brief Emits atomic load.
199 /// \returns Loaded value.
200 RValue EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
201 bool AsValue, llvm::AtomicOrdering AO,
202 bool IsVolatile);
203
204 /// \brief Emits atomic compare-and-exchange sequence.
205 /// \param Expected Expected value.
206 /// \param Desired Desired value.
207 /// \param Success Atomic ordering for success operation.
208 /// \param Failure Atomic ordering for failed operation.
209 /// \param IsWeak true if atomic operation is weak, false otherwise.
210 /// \returns Pair of values: previous value from storage (value type) and
211 /// boolean flag (i1 type) with true if success and false otherwise.
Alexey Bataevb4505a72015-03-30 05:20:59 +0000212 std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange(
Alexey Bataevb8329262015-02-27 06:33:30 +0000213 RValue Expected, RValue Desired,
214 llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
215 llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
216 bool IsWeak = false);
217
John McCalla8ec7eb2013-03-07 21:37:17 +0000218 /// Materialize an atomic r-value in atomic-layout memory.
219 llvm::Value *materializeRValue(RValue rvalue) const;
220
Alexey Bataevb8329262015-02-27 06:33:30 +0000221 /// \brief Translates LLVM atomic ordering to GNU atomic ordering for
222 /// libcalls.
223 static AtomicExpr::AtomicOrderingKind
224 translateAtomicOrdering(const llvm::AtomicOrdering AO);
225
John McCalla8ec7eb2013-03-07 21:37:17 +0000226 private:
227 bool requiresMemSetZero(llvm::Type *type) const;
Alexey Bataevb8329262015-02-27 06:33:30 +0000228
229 /// \brief Creates temp alloca for intermediate operations on atomic value.
230 llvm::Value *CreateTempAlloca() const;
231
232 /// \brief Emits atomic load as a libcall.
233 void EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
234 llvm::AtomicOrdering AO, bool IsVolatile);
235 /// \brief Emits atomic load as LLVM instruction.
236 llvm::Value *EmitAtomicLoadOp(llvm::AtomicOrdering AO, bool IsVolatile);
237 /// \brief Emits atomic compare-and-exchange op as a libcall.
Alexey Bataevb4505a72015-03-30 05:20:59 +0000238 std::pair<RValue, llvm::Value *> EmitAtomicCompareExchangeLibcall(
239 RValue Expected, RValue DesiredAddr,
Alexey Bataevb8329262015-02-27 06:33:30 +0000240 llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
241 llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent);
242 /// \brief Emits atomic compare-and-exchange op as LLVM instruction.
Alexey Bataevb4505a72015-03-30 05:20:59 +0000243 std::pair<RValue, llvm::Value *> EmitAtomicCompareExchangeOp(
244 RValue Expected, RValue Desired,
Alexey Bataevb8329262015-02-27 06:33:30 +0000245 llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
246 llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
247 bool IsWeak = false);
John McCalla8ec7eb2013-03-07 21:37:17 +0000248 };
249}
250
Alexey Bataevb8329262015-02-27 06:33:30 +0000251AtomicExpr::AtomicOrderingKind
252AtomicInfo::translateAtomicOrdering(const llvm::AtomicOrdering AO) {
253 switch (AO) {
254 case llvm::Unordered:
255 case llvm::NotAtomic:
256 case llvm::Monotonic:
257 return AtomicExpr::AO_ABI_memory_order_relaxed;
258 case llvm::Acquire:
259 return AtomicExpr::AO_ABI_memory_order_acquire;
260 case llvm::Release:
261 return AtomicExpr::AO_ABI_memory_order_release;
262 case llvm::AcquireRelease:
263 return AtomicExpr::AO_ABI_memory_order_acq_rel;
264 case llvm::SequentiallyConsistent:
265 return AtomicExpr::AO_ABI_memory_order_seq_cst;
266 }
Aaron Ballman152ad172015-02-27 13:55:58 +0000267 llvm_unreachable("Unhandled AtomicOrdering");
Alexey Bataevb8329262015-02-27 06:33:30 +0000268}
269
270llvm::Value *AtomicInfo::CreateTempAlloca() const {
271 auto *TempAlloca = CGF.CreateMemTemp(
272 (LVal.isBitField() && ValueSizeInBits > AtomicSizeInBits) ? ValueTy
273 : AtomicTy,
274 "atomic-temp");
275 TempAlloca->setAlignment(getAtomicAlignment().getQuantity());
276 // Cast to pointer to value type for bitfields.
277 if (LVal.isBitField())
278 return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
279 TempAlloca, getAtomicAddress()->getType());
280 return TempAlloca;
281}
282
John McCalla8ec7eb2013-03-07 21:37:17 +0000283static RValue emitAtomicLibcall(CodeGenFunction &CGF,
284 StringRef fnName,
285 QualType resultType,
286 CallArgList &args) {
287 const CGFunctionInfo &fnInfo =
288 CGF.CGM.getTypes().arrangeFreeFunctionCall(resultType, args,
289 FunctionType::ExtInfo(), RequiredArgs::All);
290 llvm::FunctionType *fnTy = CGF.CGM.getTypes().GetFunctionType(fnInfo);
291 llvm::Constant *fn = CGF.CGM.CreateRuntimeFunction(fnTy, fnName);
292 return CGF.EmitCall(fnInfo, fn, ReturnValueSlot(), args);
293}
294
295/// Does a store of the given IR type modify the full expected width?
296static bool isFullSizeType(CodeGenModule &CGM, llvm::Type *type,
297 uint64_t expectedSize) {
298 return (CGM.getDataLayout().getTypeStoreSize(type) * 8 == expectedSize);
299}
300
301/// Does the atomic type require memsetting to zero before initialization?
302///
303/// The IR type is provided as a way of making certain queries faster.
304bool AtomicInfo::requiresMemSetZero(llvm::Type *type) const {
305 // If the atomic type has size padding, we definitely need a memset.
306 if (hasPadding()) return true;
307
308 // Otherwise, do some simple heuristics to try to avoid it:
309 switch (getEvaluationKind()) {
310 // For scalars and complexes, check whether the store size of the
311 // type uses the full size.
312 case TEK_Scalar:
313 return !isFullSizeType(CGF.CGM, type, AtomicSizeInBits);
314 case TEK_Complex:
315 return !isFullSizeType(CGF.CGM, type->getStructElementType(0),
316 AtomicSizeInBits / 2);
317
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000318 // Padding in structs has an undefined bit pattern. User beware.
John McCalla8ec7eb2013-03-07 21:37:17 +0000319 case TEK_Aggregate:
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000320 return false;
John McCalla8ec7eb2013-03-07 21:37:17 +0000321 }
322 llvm_unreachable("bad evaluation kind");
323}
324
Alexey Bataevb57056f2015-01-22 06:17:56 +0000325bool AtomicInfo::emitMemSetZeroIfNecessary() const {
326 assert(LVal.isSimple());
327 llvm::Value *addr = LVal.getAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +0000328 if (!requiresMemSetZero(addr->getType()->getPointerElementType()))
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000329 return false;
John McCalla8ec7eb2013-03-07 21:37:17 +0000330
Alexey Bataevb8329262015-02-27 06:33:30 +0000331 CGF.Builder.CreateMemSet(
332 addr, llvm::ConstantInt::get(CGF.Int8Ty, 0),
333 CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits).getQuantity(),
334 LVal.getAlignment().getQuantity());
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000335 return true;
John McCalla8ec7eb2013-03-07 21:37:17 +0000336}
337
Tim Northovercadbbe12014-06-13 19:43:04 +0000338static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak,
Tim Northover9c177222014-03-13 19:25:48 +0000339 llvm::Value *Dest, llvm::Value *Ptr,
340 llvm::Value *Val1, llvm::Value *Val2,
341 uint64_t Size, unsigned Align,
342 llvm::AtomicOrdering SuccessOrder,
343 llvm::AtomicOrdering FailureOrder) {
344 // Note that cmpxchg doesn't support weak cmpxchg, at least at the moment.
345 llvm::LoadInst *Expected = CGF.Builder.CreateLoad(Val1);
346 Expected->setAlignment(Align);
347 llvm::LoadInst *Desired = CGF.Builder.CreateLoad(Val2);
348 Desired->setAlignment(Align);
349
Tim Northoverb49b04b2014-06-13 14:24:59 +0000350 llvm::AtomicCmpXchgInst *Pair = CGF.Builder.CreateAtomicCmpXchg(
Tim Northover9c177222014-03-13 19:25:48 +0000351 Ptr, Expected, Desired, SuccessOrder, FailureOrder);
Tim Northoverb49b04b2014-06-13 14:24:59 +0000352 Pair->setVolatile(E->isVolatile());
Tim Northovercadbbe12014-06-13 19:43:04 +0000353 Pair->setWeak(IsWeak);
Tim Northover9c177222014-03-13 19:25:48 +0000354
355 // Cmp holds the result of the compare-exchange operation: true on success,
356 // false on failure.
Tim Northoverb49b04b2014-06-13 14:24:59 +0000357 llvm::Value *Old = CGF.Builder.CreateExtractValue(Pair, 0);
358 llvm::Value *Cmp = CGF.Builder.CreateExtractValue(Pair, 1);
Tim Northover9c177222014-03-13 19:25:48 +0000359
360 // This basic block is used to hold the store instruction if the operation
361 // failed.
362 llvm::BasicBlock *StoreExpectedBB =
363 CGF.createBasicBlock("cmpxchg.store_expected", CGF.CurFn);
364
365 // This basic block is the exit point of the operation, we should end up
366 // here regardless of whether or not the operation succeeded.
367 llvm::BasicBlock *ContinueBB =
368 CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
369
370 // Update Expected if Expected isn't equal to Old, otherwise branch to the
371 // exit point.
372 CGF.Builder.CreateCondBr(Cmp, ContinueBB, StoreExpectedBB);
373
374 CGF.Builder.SetInsertPoint(StoreExpectedBB);
375 // Update the memory at Expected with Old's value.
376 llvm::StoreInst *StoreExpected = CGF.Builder.CreateStore(Old, Val1);
377 StoreExpected->setAlignment(Align);
378 // Finally, branch to the exit point.
379 CGF.Builder.CreateBr(ContinueBB);
380
381 CGF.Builder.SetInsertPoint(ContinueBB);
382 // Update the memory at Dest with Cmp's value.
383 CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType()));
384 return;
385}
386
387/// Given an ordering required on success, emit all possible cmpxchg
388/// instructions to cope with the provided (but possibly only dynamically known)
389/// FailureOrder.
390static void emitAtomicCmpXchgFailureSet(CodeGenFunction &CGF, AtomicExpr *E,
Tim Northovercadbbe12014-06-13 19:43:04 +0000391 bool IsWeak, llvm::Value *Dest,
392 llvm::Value *Ptr, llvm::Value *Val1,
393 llvm::Value *Val2,
Tim Northover9c177222014-03-13 19:25:48 +0000394 llvm::Value *FailureOrderVal,
395 uint64_t Size, unsigned Align,
396 llvm::AtomicOrdering SuccessOrder) {
397 llvm::AtomicOrdering FailureOrder;
398 if (llvm::ConstantInt *FO = dyn_cast<llvm::ConstantInt>(FailureOrderVal)) {
399 switch (FO->getSExtValue()) {
400 default:
401 FailureOrder = llvm::Monotonic;
402 break;
403 case AtomicExpr::AO_ABI_memory_order_consume:
404 case AtomicExpr::AO_ABI_memory_order_acquire:
405 FailureOrder = llvm::Acquire;
406 break;
407 case AtomicExpr::AO_ABI_memory_order_seq_cst:
408 FailureOrder = llvm::SequentiallyConsistent;
409 break;
410 }
411 if (FailureOrder >= SuccessOrder) {
412 // Don't assert on undefined behaviour.
413 FailureOrder =
414 llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrder);
415 }
Tim Northovercadbbe12014-06-13 19:43:04 +0000416 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2, Size, Align,
417 SuccessOrder, FailureOrder);
Tim Northover9c177222014-03-13 19:25:48 +0000418 return;
419 }
420
421 // Create all the relevant BB's
Craig Topper8a13c412014-05-21 05:09:00 +0000422 llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
423 *SeqCstBB = nullptr;
Tim Northover9c177222014-03-13 19:25:48 +0000424 MonotonicBB = CGF.createBasicBlock("monotonic_fail", CGF.CurFn);
425 if (SuccessOrder != llvm::Monotonic && SuccessOrder != llvm::Release)
426 AcquireBB = CGF.createBasicBlock("acquire_fail", CGF.CurFn);
427 if (SuccessOrder == llvm::SequentiallyConsistent)
428 SeqCstBB = CGF.createBasicBlock("seqcst_fail", CGF.CurFn);
429
430 llvm::BasicBlock *ContBB = CGF.createBasicBlock("atomic.continue", CGF.CurFn);
431
432 llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(FailureOrderVal, MonotonicBB);
433
434 // Emit all the different atomics
435
436 // MonotonicBB is arbitrarily chosen as the default case; in practice, this
437 // doesn't matter unless someone is crazy enough to use something that
438 // doesn't fold to a constant for the ordering.
439 CGF.Builder.SetInsertPoint(MonotonicBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000440 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
Tim Northover9c177222014-03-13 19:25:48 +0000441 Size, Align, SuccessOrder, llvm::Monotonic);
442 CGF.Builder.CreateBr(ContBB);
443
444 if (AcquireBB) {
445 CGF.Builder.SetInsertPoint(AcquireBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000446 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
Tim Northover9c177222014-03-13 19:25:48 +0000447 Size, Align, SuccessOrder, llvm::Acquire);
448 CGF.Builder.CreateBr(ContBB);
449 SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_consume),
450 AcquireBB);
451 SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acquire),
452 AcquireBB);
453 }
454 if (SeqCstBB) {
455 CGF.Builder.SetInsertPoint(SeqCstBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000456 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
Tim Northover9c177222014-03-13 19:25:48 +0000457 Size, Align, SuccessOrder, llvm::SequentiallyConsistent);
458 CGF.Builder.CreateBr(ContBB);
459 SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_seq_cst),
460 SeqCstBB);
461 }
462
463 CGF.Builder.SetInsertPoint(ContBB);
464}
465
466static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, llvm::Value *Dest,
467 llvm::Value *Ptr, llvm::Value *Val1, llvm::Value *Val2,
Tim Northovercadbbe12014-06-13 19:43:04 +0000468 llvm::Value *IsWeak, llvm::Value *FailureOrder,
469 uint64_t Size, unsigned Align,
470 llvm::AtomicOrdering Order) {
John McCallfc207f22013-03-07 21:37:12 +0000471 llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add;
472 llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0;
473
474 switch (E->getOp()) {
475 case AtomicExpr::AO__c11_atomic_init:
476 llvm_unreachable("Already handled!");
477
478 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
Tim Northovercadbbe12014-06-13 19:43:04 +0000479 emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
480 FailureOrder, Size, Align, Order);
John McCallfc207f22013-03-07 21:37:12 +0000481 return;
Tim Northovercadbbe12014-06-13 19:43:04 +0000482 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
483 emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
484 FailureOrder, Size, Align, Order);
485 return;
486 case AtomicExpr::AO__atomic_compare_exchange:
487 case AtomicExpr::AO__atomic_compare_exchange_n: {
488 if (llvm::ConstantInt *IsWeakC = dyn_cast<llvm::ConstantInt>(IsWeak)) {
489 emitAtomicCmpXchgFailureSet(CGF, E, IsWeakC->getZExtValue(), Dest, Ptr,
490 Val1, Val2, FailureOrder, Size, Align, Order);
491 } else {
492 // Create all the relevant BB's
493 llvm::BasicBlock *StrongBB =
494 CGF.createBasicBlock("cmpxchg.strong", CGF.CurFn);
495 llvm::BasicBlock *WeakBB = CGF.createBasicBlock("cmxchg.weak", CGF.CurFn);
496 llvm::BasicBlock *ContBB =
497 CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
498
499 llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(IsWeak, WeakBB);
500 SI->addCase(CGF.Builder.getInt1(false), StrongBB);
501
502 CGF.Builder.SetInsertPoint(StrongBB);
503 emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
504 FailureOrder, Size, Align, Order);
505 CGF.Builder.CreateBr(ContBB);
506
507 CGF.Builder.SetInsertPoint(WeakBB);
508 emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
509 FailureOrder, Size, Align, Order);
510 CGF.Builder.CreateBr(ContBB);
511
512 CGF.Builder.SetInsertPoint(ContBB);
513 }
514 return;
515 }
John McCallfc207f22013-03-07 21:37:12 +0000516 case AtomicExpr::AO__c11_atomic_load:
517 case AtomicExpr::AO__atomic_load_n:
518 case AtomicExpr::AO__atomic_load: {
519 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr);
520 Load->setAtomic(Order);
521 Load->setAlignment(Size);
522 Load->setVolatile(E->isVolatile());
523 llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Load, Dest);
524 StoreDest->setAlignment(Align);
525 return;
526 }
527
528 case AtomicExpr::AO__c11_atomic_store:
529 case AtomicExpr::AO__atomic_store:
530 case AtomicExpr::AO__atomic_store_n: {
531 assert(!Dest && "Store does not return a value");
532 llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1);
533 LoadVal1->setAlignment(Align);
534 llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr);
535 Store->setAtomic(Order);
536 Store->setAlignment(Size);
537 Store->setVolatile(E->isVolatile());
538 return;
539 }
540
541 case AtomicExpr::AO__c11_atomic_exchange:
542 case AtomicExpr::AO__atomic_exchange_n:
543 case AtomicExpr::AO__atomic_exchange:
544 Op = llvm::AtomicRMWInst::Xchg;
545 break;
546
547 case AtomicExpr::AO__atomic_add_fetch:
548 PostOp = llvm::Instruction::Add;
549 // Fall through.
550 case AtomicExpr::AO__c11_atomic_fetch_add:
551 case AtomicExpr::AO__atomic_fetch_add:
552 Op = llvm::AtomicRMWInst::Add;
553 break;
554
555 case AtomicExpr::AO__atomic_sub_fetch:
556 PostOp = llvm::Instruction::Sub;
557 // Fall through.
558 case AtomicExpr::AO__c11_atomic_fetch_sub:
559 case AtomicExpr::AO__atomic_fetch_sub:
560 Op = llvm::AtomicRMWInst::Sub;
561 break;
562
563 case AtomicExpr::AO__atomic_and_fetch:
564 PostOp = llvm::Instruction::And;
565 // Fall through.
566 case AtomicExpr::AO__c11_atomic_fetch_and:
567 case AtomicExpr::AO__atomic_fetch_and:
568 Op = llvm::AtomicRMWInst::And;
569 break;
570
571 case AtomicExpr::AO__atomic_or_fetch:
572 PostOp = llvm::Instruction::Or;
573 // Fall through.
574 case AtomicExpr::AO__c11_atomic_fetch_or:
575 case AtomicExpr::AO__atomic_fetch_or:
576 Op = llvm::AtomicRMWInst::Or;
577 break;
578
579 case AtomicExpr::AO__atomic_xor_fetch:
580 PostOp = llvm::Instruction::Xor;
581 // Fall through.
582 case AtomicExpr::AO__c11_atomic_fetch_xor:
583 case AtomicExpr::AO__atomic_fetch_xor:
584 Op = llvm::AtomicRMWInst::Xor;
585 break;
586
587 case AtomicExpr::AO__atomic_nand_fetch:
588 PostOp = llvm::Instruction::And;
589 // Fall through.
590 case AtomicExpr::AO__atomic_fetch_nand:
591 Op = llvm::AtomicRMWInst::Nand;
592 break;
593 }
594
595 llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1);
596 LoadVal1->setAlignment(Align);
597 llvm::AtomicRMWInst *RMWI =
598 CGF.Builder.CreateAtomicRMW(Op, Ptr, LoadVal1, Order);
599 RMWI->setVolatile(E->isVolatile());
600
601 // For __atomic_*_fetch operations, perform the operation again to
602 // determine the value which was written.
603 llvm::Value *Result = RMWI;
604 if (PostOp)
605 Result = CGF.Builder.CreateBinOp(PostOp, RMWI, LoadVal1);
606 if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch)
607 Result = CGF.Builder.CreateNot(Result);
608 llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Result, Dest);
609 StoreDest->setAlignment(Align);
610}
611
612// This function emits any expression (scalar, complex, or aggregate)
613// into a temporary alloca.
614static llvm::Value *
615EmitValToTemp(CodeGenFunction &CGF, Expr *E) {
616 llvm::Value *DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp");
617 CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(),
618 /*Init*/ true);
619 return DeclPtr;
620}
621
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000622static void
623AddDirectArgument(CodeGenFunction &CGF, CallArgList &Args,
Nick Lewycky2d84e842013-10-02 02:29:49 +0000624 bool UseOptimizedLibcall, llvm::Value *Val, QualType ValTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000625 SourceLocation Loc, CharUnits SizeInChars) {
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000626 if (UseOptimizedLibcall) {
627 // Load value and pass it to the function directly.
628 unsigned Align = CGF.getContext().getTypeAlignInChars(ValTy).getQuantity();
David Majnemer0392cf82014-08-29 07:27:49 +0000629 int64_t SizeInBits = CGF.getContext().toBits(SizeInChars);
630 ValTy =
631 CGF.getContext().getIntTypeForBitwidth(SizeInBits, /*Signed=*/false);
632 llvm::Type *IPtrTy = llvm::IntegerType::get(CGF.getLLVMContext(),
633 SizeInBits)->getPointerTo();
634 Val = CGF.EmitLoadOfScalar(CGF.Builder.CreateBitCast(Val, IPtrTy), false,
635 Align, CGF.getContext().getPointerType(ValTy),
636 Loc);
637 // Coerce the value into an appropriately sized integer type.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000638 Args.add(RValue::get(Val), ValTy);
639 } else {
640 // Non-optimized functions always take a reference.
641 Args.add(RValue::get(CGF.EmitCastToVoidPtr(Val)),
642 CGF.getContext().VoidPtrTy);
643 }
644}
645
John McCallfc207f22013-03-07 21:37:12 +0000646RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) {
647 QualType AtomicTy = E->getPtr()->getType()->getPointeeType();
648 QualType MemTy = AtomicTy;
649 if (const AtomicType *AT = AtomicTy->getAs<AtomicType>())
650 MemTy = AT->getValueType();
651 CharUnits sizeChars = getContext().getTypeSizeInChars(AtomicTy);
652 uint64_t Size = sizeChars.getQuantity();
653 CharUnits alignChars = getContext().getTypeAlignInChars(AtomicTy);
654 unsigned Align = alignChars.getQuantity();
655 unsigned MaxInlineWidthInBits =
John McCallc8e01702013-04-16 22:48:15 +0000656 getTarget().getMaxAtomicInlineWidth();
John McCallfc207f22013-03-07 21:37:12 +0000657 bool UseLibcall = (Size != Align ||
658 getContext().toBits(sizeChars) > MaxInlineWidthInBits);
659
Tim Northovercadbbe12014-06-13 19:43:04 +0000660 llvm::Value *IsWeak = nullptr, *OrderFail = nullptr, *Val1 = nullptr,
661 *Val2 = nullptr;
Craig Topper8a13c412014-05-21 05:09:00 +0000662 llvm::Value *Ptr = EmitScalarExpr(E->getPtr());
John McCallfc207f22013-03-07 21:37:12 +0000663
664 if (E->getOp() == AtomicExpr::AO__c11_atomic_init) {
665 assert(!Dest && "Init does not return a value");
John McCalla8ec7eb2013-03-07 21:37:17 +0000666 LValue lvalue = LValue::MakeAddr(Ptr, AtomicTy, alignChars, getContext());
667 EmitAtomicInit(E->getVal1(), lvalue);
Craig Topper8a13c412014-05-21 05:09:00 +0000668 return RValue::get(nullptr);
John McCallfc207f22013-03-07 21:37:12 +0000669 }
670
Craig Topper8a13c412014-05-21 05:09:00 +0000671 llvm::Value *Order = EmitScalarExpr(E->getOrder());
John McCallfc207f22013-03-07 21:37:12 +0000672
673 switch (E->getOp()) {
674 case AtomicExpr::AO__c11_atomic_init:
675 llvm_unreachable("Already handled!");
676
677 case AtomicExpr::AO__c11_atomic_load:
678 case AtomicExpr::AO__atomic_load_n:
679 break;
680
681 case AtomicExpr::AO__atomic_load:
682 Dest = EmitScalarExpr(E->getVal1());
683 break;
684
685 case AtomicExpr::AO__atomic_store:
686 Val1 = EmitScalarExpr(E->getVal1());
687 break;
688
689 case AtomicExpr::AO__atomic_exchange:
690 Val1 = EmitScalarExpr(E->getVal1());
691 Dest = EmitScalarExpr(E->getVal2());
692 break;
693
694 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
695 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
696 case AtomicExpr::AO__atomic_compare_exchange_n:
697 case AtomicExpr::AO__atomic_compare_exchange:
698 Val1 = EmitScalarExpr(E->getVal1());
699 if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange)
700 Val2 = EmitScalarExpr(E->getVal2());
701 else
702 Val2 = EmitValToTemp(*this, E->getVal2());
703 OrderFail = EmitScalarExpr(E->getOrderFail());
John McCallfc207f22013-03-07 21:37:12 +0000704 if (E->getNumSubExprs() == 6)
Tim Northovercadbbe12014-06-13 19:43:04 +0000705 IsWeak = EmitScalarExpr(E->getWeak());
John McCallfc207f22013-03-07 21:37:12 +0000706 break;
707
708 case AtomicExpr::AO__c11_atomic_fetch_add:
709 case AtomicExpr::AO__c11_atomic_fetch_sub:
710 if (MemTy->isPointerType()) {
711 // For pointer arithmetic, we're required to do a bit of math:
712 // adding 1 to an int* is not the same as adding 1 to a uintptr_t.
713 // ... but only for the C11 builtins. The GNU builtins expect the
714 // user to multiply by sizeof(T).
715 QualType Val1Ty = E->getVal1()->getType();
716 llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1());
717 CharUnits PointeeIncAmt =
718 getContext().getTypeSizeInChars(MemTy->getPointeeType());
719 Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt));
720 Val1 = CreateMemTemp(Val1Ty, ".atomictmp");
721 EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Val1, Val1Ty));
722 break;
723 }
724 // Fall through.
725 case AtomicExpr::AO__atomic_fetch_add:
726 case AtomicExpr::AO__atomic_fetch_sub:
727 case AtomicExpr::AO__atomic_add_fetch:
728 case AtomicExpr::AO__atomic_sub_fetch:
729 case AtomicExpr::AO__c11_atomic_store:
730 case AtomicExpr::AO__c11_atomic_exchange:
731 case AtomicExpr::AO__atomic_store_n:
732 case AtomicExpr::AO__atomic_exchange_n:
733 case AtomicExpr::AO__c11_atomic_fetch_and:
734 case AtomicExpr::AO__c11_atomic_fetch_or:
735 case AtomicExpr::AO__c11_atomic_fetch_xor:
736 case AtomicExpr::AO__atomic_fetch_and:
737 case AtomicExpr::AO__atomic_fetch_or:
738 case AtomicExpr::AO__atomic_fetch_xor:
739 case AtomicExpr::AO__atomic_fetch_nand:
740 case AtomicExpr::AO__atomic_and_fetch:
741 case AtomicExpr::AO__atomic_or_fetch:
742 case AtomicExpr::AO__atomic_xor_fetch:
743 case AtomicExpr::AO__atomic_nand_fetch:
744 Val1 = EmitValToTemp(*this, E->getVal1());
745 break;
746 }
747
David Majnemeree8d04d2014-12-12 08:16:09 +0000748 QualType RValTy = E->getType().getUnqualifiedType();
749
David Majnemer659be552014-11-25 23:44:32 +0000750 auto GetDest = [&] {
David Majnemeree8d04d2014-12-12 08:16:09 +0000751 if (!RValTy->isVoidType() && !Dest) {
752 Dest = CreateMemTemp(RValTy, ".atomicdst");
753 }
David Majnemer659be552014-11-25 23:44:32 +0000754 return Dest;
755 };
John McCallfc207f22013-03-07 21:37:12 +0000756
757 // Use a library call. See: http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary .
758 if (UseLibcall) {
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000759 bool UseOptimizedLibcall = false;
760 switch (E->getOp()) {
761 case AtomicExpr::AO__c11_atomic_fetch_add:
762 case AtomicExpr::AO__atomic_fetch_add:
763 case AtomicExpr::AO__c11_atomic_fetch_and:
764 case AtomicExpr::AO__atomic_fetch_and:
765 case AtomicExpr::AO__c11_atomic_fetch_or:
766 case AtomicExpr::AO__atomic_fetch_or:
767 case AtomicExpr::AO__c11_atomic_fetch_sub:
768 case AtomicExpr::AO__atomic_fetch_sub:
769 case AtomicExpr::AO__c11_atomic_fetch_xor:
770 case AtomicExpr::AO__atomic_fetch_xor:
771 // For these, only library calls for certain sizes exist.
772 UseOptimizedLibcall = true;
773 break;
774 default:
775 // Only use optimized library calls for sizes for which they exist.
776 if (Size == 1 || Size == 2 || Size == 4 || Size == 8)
777 UseOptimizedLibcall = true;
778 break;
779 }
John McCallfc207f22013-03-07 21:37:12 +0000780
John McCallfc207f22013-03-07 21:37:12 +0000781 CallArgList Args;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000782 if (!UseOptimizedLibcall) {
783 // For non-optimized library calls, the size is the first parameter
784 Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)),
785 getContext().getSizeType());
786 }
787 // Atomic address is the first or second parameter
Nick Lewycky5fa40c32013-10-01 21:51:38 +0000788 Args.add(RValue::get(EmitCastToVoidPtr(Ptr)), getContext().VoidPtrTy);
John McCallfc207f22013-03-07 21:37:12 +0000789
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000790 std::string LibCallName;
Logan Chien74798a32014-03-26 17:35:01 +0000791 QualType LoweredMemTy =
792 MemTy->isPointerType() ? getContext().getIntPtrType() : MemTy;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000793 QualType RetTy;
794 bool HaveRetTy = false;
John McCallfc207f22013-03-07 21:37:12 +0000795 switch (E->getOp()) {
796 // There is only one libcall for compare an exchange, because there is no
797 // optimisation benefit possible from a libcall version of a weak compare
798 // and exchange.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000799 // bool __atomic_compare_exchange(size_t size, void *mem, void *expected,
John McCallfc207f22013-03-07 21:37:12 +0000800 // void *desired, int success, int failure)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000801 // bool __atomic_compare_exchange_N(T *mem, T *expected, T desired,
802 // int success, int failure)
John McCallfc207f22013-03-07 21:37:12 +0000803 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
804 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
805 case AtomicExpr::AO__atomic_compare_exchange:
806 case AtomicExpr::AO__atomic_compare_exchange_n:
807 LibCallName = "__atomic_compare_exchange";
808 RetTy = getContext().BoolTy;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000809 HaveRetTy = true;
Nick Lewycky2d84e842013-10-02 02:29:49 +0000810 Args.add(RValue::get(EmitCastToVoidPtr(Val1)), getContext().VoidPtrTy);
811 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val2, MemTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000812 E->getExprLoc(), sizeChars);
Nick Lewycky5fa40c32013-10-01 21:51:38 +0000813 Args.add(RValue::get(Order), getContext().IntTy);
John McCallfc207f22013-03-07 21:37:12 +0000814 Order = OrderFail;
815 break;
816 // void __atomic_exchange(size_t size, void *mem, void *val, void *return,
817 // int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000818 // T __atomic_exchange_N(T *mem, T val, int order)
John McCallfc207f22013-03-07 21:37:12 +0000819 case AtomicExpr::AO__c11_atomic_exchange:
820 case AtomicExpr::AO__atomic_exchange_n:
821 case AtomicExpr::AO__atomic_exchange:
822 LibCallName = "__atomic_exchange";
Nick Lewycky2d84e842013-10-02 02:29:49 +0000823 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000824 E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000825 break;
826 // void __atomic_store(size_t size, void *mem, void *val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000827 // void __atomic_store_N(T *mem, T val, int order)
John McCallfc207f22013-03-07 21:37:12 +0000828 case AtomicExpr::AO__c11_atomic_store:
829 case AtomicExpr::AO__atomic_store:
830 case AtomicExpr::AO__atomic_store_n:
831 LibCallName = "__atomic_store";
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000832 RetTy = getContext().VoidTy;
833 HaveRetTy = true;
Nick Lewycky2d84e842013-10-02 02:29:49 +0000834 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000835 E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000836 break;
837 // void __atomic_load(size_t size, void *mem, void *return, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000838 // T __atomic_load_N(T *mem, int order)
John McCallfc207f22013-03-07 21:37:12 +0000839 case AtomicExpr::AO__c11_atomic_load:
840 case AtomicExpr::AO__atomic_load:
841 case AtomicExpr::AO__atomic_load_n:
842 LibCallName = "__atomic_load";
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000843 break;
844 // T __atomic_fetch_add_N(T *mem, T val, int order)
845 case AtomicExpr::AO__c11_atomic_fetch_add:
846 case AtomicExpr::AO__atomic_fetch_add:
847 LibCallName = "__atomic_fetch_add";
Logan Chien74798a32014-03-26 17:35:01 +0000848 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, LoweredMemTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000849 E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000850 break;
851 // T __atomic_fetch_and_N(T *mem, T val, int order)
852 case AtomicExpr::AO__c11_atomic_fetch_and:
853 case AtomicExpr::AO__atomic_fetch_and:
854 LibCallName = "__atomic_fetch_and";
Nick Lewycky2d84e842013-10-02 02:29:49 +0000855 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000856 E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000857 break;
858 // T __atomic_fetch_or_N(T *mem, T val, int order)
859 case AtomicExpr::AO__c11_atomic_fetch_or:
860 case AtomicExpr::AO__atomic_fetch_or:
861 LibCallName = "__atomic_fetch_or";
Nick Lewycky2d84e842013-10-02 02:29:49 +0000862 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000863 E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000864 break;
865 // T __atomic_fetch_sub_N(T *mem, T val, int order)
866 case AtomicExpr::AO__c11_atomic_fetch_sub:
867 case AtomicExpr::AO__atomic_fetch_sub:
868 LibCallName = "__atomic_fetch_sub";
Logan Chien74798a32014-03-26 17:35:01 +0000869 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, LoweredMemTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000870 E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000871 break;
872 // T __atomic_fetch_xor_N(T *mem, T val, int order)
873 case AtomicExpr::AO__c11_atomic_fetch_xor:
874 case AtomicExpr::AO__atomic_fetch_xor:
875 LibCallName = "__atomic_fetch_xor";
Nick Lewycky2d84e842013-10-02 02:29:49 +0000876 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000877 E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000878 break;
John McCallfc207f22013-03-07 21:37:12 +0000879 default: return EmitUnsupportedRValue(E, "atomic library call");
880 }
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000881
882 // Optimized functions have the size in their name.
883 if (UseOptimizedLibcall)
884 LibCallName += "_" + llvm::utostr(Size);
885 // By default, assume we return a value of the atomic type.
886 if (!HaveRetTy) {
887 if (UseOptimizedLibcall) {
888 // Value is returned directly.
David Majnemer0392cf82014-08-29 07:27:49 +0000889 // The function returns an appropriately sized integer type.
890 RetTy = getContext().getIntTypeForBitwidth(
891 getContext().toBits(sizeChars), /*Signed=*/false);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000892 } else {
893 // Value is returned through parameter before the order.
894 RetTy = getContext().VoidTy;
David Majnemer659be552014-11-25 23:44:32 +0000895 Args.add(RValue::get(EmitCastToVoidPtr(Dest)), getContext().VoidPtrTy);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000896 }
897 }
John McCallfc207f22013-03-07 21:37:12 +0000898 // order is always the last parameter
899 Args.add(RValue::get(Order),
900 getContext().IntTy);
901
David Majnemer659be552014-11-25 23:44:32 +0000902 RValue Res = emitAtomicLibcall(*this, LibCallName, RetTy, Args);
903 // The value is returned directly from the libcall.
904 if (HaveRetTy && !RetTy->isVoidType())
905 return Res;
906 // The value is returned via an explicit out param.
907 if (RetTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +0000908 return RValue::get(nullptr);
David Majnemer659be552014-11-25 23:44:32 +0000909 // The value is returned directly for optimized libcalls but the caller is
910 // expected an out-param.
911 if (UseOptimizedLibcall) {
912 llvm::Value *ResVal = Res.getScalarVal();
913 llvm::StoreInst *StoreDest = Builder.CreateStore(
914 ResVal,
915 Builder.CreateBitCast(GetDest(), ResVal->getType()->getPointerTo()));
916 StoreDest->setAlignment(Align);
917 }
David Majnemeree8d04d2014-12-12 08:16:09 +0000918 return convertTempToRValue(Dest, RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +0000919 }
920
921 bool IsStore = E->getOp() == AtomicExpr::AO__c11_atomic_store ||
922 E->getOp() == AtomicExpr::AO__atomic_store ||
923 E->getOp() == AtomicExpr::AO__atomic_store_n;
924 bool IsLoad = E->getOp() == AtomicExpr::AO__c11_atomic_load ||
925 E->getOp() == AtomicExpr::AO__atomic_load ||
926 E->getOp() == AtomicExpr::AO__atomic_load_n;
927
David Majnemerd8cd8f72014-11-22 10:44:12 +0000928 llvm::Type *ITy =
929 llvm::IntegerType::get(getLLVMContext(), Size * 8);
David Majnemer659be552014-11-25 23:44:32 +0000930 llvm::Value *OrigDest = GetDest();
David Majnemerd8cd8f72014-11-22 10:44:12 +0000931 Ptr = Builder.CreateBitCast(
932 Ptr, ITy->getPointerTo(Ptr->getType()->getPointerAddressSpace()));
933 if (Val1) Val1 = Builder.CreateBitCast(Val1, ITy->getPointerTo());
934 if (Val2) Val2 = Builder.CreateBitCast(Val2, ITy->getPointerTo());
935 if (Dest && !E->isCmpXChg())
936 Dest = Builder.CreateBitCast(Dest, ITy->getPointerTo());
John McCallfc207f22013-03-07 21:37:12 +0000937
938 if (isa<llvm::ConstantInt>(Order)) {
939 int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
940 switch (ord) {
Tim Northovere94a34c2014-03-11 10:49:14 +0000941 case AtomicExpr::AO_ABI_memory_order_relaxed:
Tim Northovercadbbe12014-06-13 19:43:04 +0000942 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +0000943 Size, Align, llvm::Monotonic);
John McCallfc207f22013-03-07 21:37:12 +0000944 break;
Tim Northovere94a34c2014-03-11 10:49:14 +0000945 case AtomicExpr::AO_ABI_memory_order_consume:
946 case AtomicExpr::AO_ABI_memory_order_acquire:
John McCallfc207f22013-03-07 21:37:12 +0000947 if (IsStore)
948 break; // Avoid crashing on code with undefined behavior
Tim Northovercadbbe12014-06-13 19:43:04 +0000949 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +0000950 Size, Align, llvm::Acquire);
John McCallfc207f22013-03-07 21:37:12 +0000951 break;
Tim Northovere94a34c2014-03-11 10:49:14 +0000952 case AtomicExpr::AO_ABI_memory_order_release:
John McCallfc207f22013-03-07 21:37:12 +0000953 if (IsLoad)
954 break; // Avoid crashing on code with undefined behavior
Tim Northovercadbbe12014-06-13 19:43:04 +0000955 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +0000956 Size, Align, llvm::Release);
John McCallfc207f22013-03-07 21:37:12 +0000957 break;
Tim Northovere94a34c2014-03-11 10:49:14 +0000958 case AtomicExpr::AO_ABI_memory_order_acq_rel:
John McCallfc207f22013-03-07 21:37:12 +0000959 if (IsLoad || IsStore)
960 break; // Avoid crashing on code with undefined behavior
Tim Northovercadbbe12014-06-13 19:43:04 +0000961 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +0000962 Size, Align, llvm::AcquireRelease);
John McCallfc207f22013-03-07 21:37:12 +0000963 break;
Tim Northovere94a34c2014-03-11 10:49:14 +0000964 case AtomicExpr::AO_ABI_memory_order_seq_cst:
Tim Northovercadbbe12014-06-13 19:43:04 +0000965 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +0000966 Size, Align, llvm::SequentiallyConsistent);
John McCallfc207f22013-03-07 21:37:12 +0000967 break;
968 default: // invalid order
969 // We should not ever get here normally, but it's hard to
970 // enforce that in general.
971 break;
972 }
David Majnemeree8d04d2014-12-12 08:16:09 +0000973 if (RValTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +0000974 return RValue::get(nullptr);
David Majnemeree8d04d2014-12-12 08:16:09 +0000975 return convertTempToRValue(OrigDest, RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +0000976 }
977
978 // Long case, when Order isn't obviously constant.
979
980 // Create all the relevant BB's
Craig Topper8a13c412014-05-21 05:09:00 +0000981 llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
982 *ReleaseBB = nullptr, *AcqRelBB = nullptr,
983 *SeqCstBB = nullptr;
John McCallfc207f22013-03-07 21:37:12 +0000984 MonotonicBB = createBasicBlock("monotonic", CurFn);
985 if (!IsStore)
986 AcquireBB = createBasicBlock("acquire", CurFn);
987 if (!IsLoad)
988 ReleaseBB = createBasicBlock("release", CurFn);
989 if (!IsLoad && !IsStore)
990 AcqRelBB = createBasicBlock("acqrel", CurFn);
991 SeqCstBB = createBasicBlock("seqcst", CurFn);
992 llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
993
994 // Create the switch for the split
995 // MonotonicBB is arbitrarily chosen as the default case; in practice, this
996 // doesn't matter unless someone is crazy enough to use something that
997 // doesn't fold to a constant for the ordering.
998 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
999 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB);
1000
1001 // Emit all the different atomics
1002 Builder.SetInsertPoint(MonotonicBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001003 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +00001004 Size, Align, llvm::Monotonic);
John McCallfc207f22013-03-07 21:37:12 +00001005 Builder.CreateBr(ContBB);
1006 if (!IsStore) {
1007 Builder.SetInsertPoint(AcquireBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001008 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +00001009 Size, Align, llvm::Acquire);
John McCallfc207f22013-03-07 21:37:12 +00001010 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001011 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_consume),
1012 AcquireBB);
1013 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acquire),
1014 AcquireBB);
John McCallfc207f22013-03-07 21:37:12 +00001015 }
1016 if (!IsLoad) {
1017 Builder.SetInsertPoint(ReleaseBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001018 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +00001019 Size, Align, llvm::Release);
John McCallfc207f22013-03-07 21:37:12 +00001020 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001021 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_release),
1022 ReleaseBB);
John McCallfc207f22013-03-07 21:37:12 +00001023 }
1024 if (!IsLoad && !IsStore) {
1025 Builder.SetInsertPoint(AcqRelBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001026 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +00001027 Size, Align, llvm::AcquireRelease);
John McCallfc207f22013-03-07 21:37:12 +00001028 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001029 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acq_rel),
1030 AcqRelBB);
John McCallfc207f22013-03-07 21:37:12 +00001031 }
1032 Builder.SetInsertPoint(SeqCstBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001033 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
Tim Northover9c177222014-03-13 19:25:48 +00001034 Size, Align, llvm::SequentiallyConsistent);
John McCallfc207f22013-03-07 21:37:12 +00001035 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001036 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_seq_cst),
1037 SeqCstBB);
John McCallfc207f22013-03-07 21:37:12 +00001038
1039 // Cleanup and return
1040 Builder.SetInsertPoint(ContBB);
David Majnemeree8d04d2014-12-12 08:16:09 +00001041 if (RValTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +00001042 return RValue::get(nullptr);
David Majnemeree8d04d2014-12-12 08:16:09 +00001043 return convertTempToRValue(OrigDest, RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001044}
John McCalla8ec7eb2013-03-07 21:37:17 +00001045
1046llvm::Value *AtomicInfo::emitCastToAtomicIntPointer(llvm::Value *addr) const {
1047 unsigned addrspace =
1048 cast<llvm::PointerType>(addr->getType())->getAddressSpace();
1049 llvm::IntegerType *ty =
1050 llvm::IntegerType::get(CGF.getLLVMContext(), AtomicSizeInBits);
1051 return CGF.Builder.CreateBitCast(addr, ty->getPointerTo(addrspace));
1052}
1053
1054RValue AtomicInfo::convertTempToRValue(llvm::Value *addr,
Nick Lewycky2d84e842013-10-02 02:29:49 +00001055 AggValueSlot resultSlot,
Alexey Bataevb8329262015-02-27 06:33:30 +00001056 SourceLocation loc, bool AsValue) const {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001057 if (LVal.isSimple()) {
1058 if (EvaluationKind == TEK_Aggregate)
1059 return resultSlot.asRValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001060
Alexey Bataevb57056f2015-01-22 06:17:56 +00001061 // Drill into the padding structure if we have one.
1062 if (hasPadding())
1063 addr = CGF.Builder.CreateStructGEP(addr, 0);
John McCalla8ec7eb2013-03-07 21:37:17 +00001064
Alexey Bataevb57056f2015-01-22 06:17:56 +00001065 // Otherwise, just convert the temporary to an r-value using the
1066 // normal conversion routine.
1067 return CGF.convertTempToRValue(addr, getValueType(), loc);
Alexey Bataevb8329262015-02-27 06:33:30 +00001068 } else if (!AsValue)
1069 // Get RValue from temp memory as atomic for non-simple lvalues
1070 return RValue::get(
1071 CGF.Builder.CreateAlignedLoad(addr, AtomicAlign.getQuantity()));
1072 else if (LVal.isBitField())
Alexey Bataevb57056f2015-01-22 06:17:56 +00001073 return CGF.EmitLoadOfBitfieldLValue(LValue::MakeBitfield(
1074 addr, LVal.getBitFieldInfo(), LVal.getType(), LVal.getAlignment()));
1075 else if (LVal.isVectorElt())
1076 return CGF.EmitLoadOfLValue(LValue::MakeVectorElt(addr, LVal.getVectorIdx(),
1077 LVal.getType(),
1078 LVal.getAlignment()),
1079 loc);
1080 assert(LVal.isExtVectorElt());
1081 return CGF.EmitLoadOfExtVectorElementLValue(LValue::MakeExtVectorElt(
1082 addr, LVal.getExtVectorElts(), LVal.getType(), LVal.getAlignment()));
John McCalla8ec7eb2013-03-07 21:37:17 +00001083}
1084
Alexey Bataevb8329262015-02-27 06:33:30 +00001085RValue AtomicInfo::ConvertIntToValueOrAtomic(llvm::Value *IntVal,
1086 AggValueSlot ResultSlot,
1087 SourceLocation Loc,
1088 bool AsValue) const {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001089 // Try not to in some easy cases.
1090 assert(IntVal->getType()->isIntegerTy() && "Expected integer value");
Alexey Bataevb8329262015-02-27 06:33:30 +00001091 if (getEvaluationKind() == TEK_Scalar &&
1092 (((!LVal.isBitField() ||
1093 LVal.getBitFieldInfo().Size == ValueSizeInBits) &&
1094 !hasPadding()) ||
1095 !AsValue)) {
1096 auto *ValTy = AsValue
1097 ? CGF.ConvertTypeForMem(ValueTy)
1098 : getAtomicAddress()->getType()->getPointerElementType();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001099 if (ValTy->isIntegerTy()) {
1100 assert(IntVal->getType() == ValTy && "Different integer types.");
David Majnemereeaec262015-02-14 02:18:14 +00001101 return RValue::get(CGF.EmitFromMemory(IntVal, ValueTy));
Alexey Bataev452d8e12014-12-15 05:25:25 +00001102 } else if (ValTy->isPointerTy())
1103 return RValue::get(CGF.Builder.CreateIntToPtr(IntVal, ValTy));
1104 else if (llvm::CastInst::isBitCastable(IntVal->getType(), ValTy))
1105 return RValue::get(CGF.Builder.CreateBitCast(IntVal, ValTy));
1106 }
1107
1108 // Create a temporary. This needs to be big enough to hold the
1109 // atomic integer.
1110 llvm::Value *Temp;
1111 bool TempIsVolatile = false;
1112 CharUnits TempAlignment;
Alexey Bataevb8329262015-02-27 06:33:30 +00001113 if (AsValue && getEvaluationKind() == TEK_Aggregate) {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001114 assert(!ResultSlot.isIgnored());
1115 Temp = ResultSlot.getAddr();
1116 TempAlignment = getValueAlignment();
1117 TempIsVolatile = ResultSlot.isVolatile();
1118 } else {
Alexey Bataevb8329262015-02-27 06:33:30 +00001119 Temp = CreateTempAlloca();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001120 TempAlignment = getAtomicAlignment();
1121 }
1122
1123 // Slam the integer into the temporary.
1124 llvm::Value *CastTemp = emitCastToAtomicIntPointer(Temp);
1125 CGF.Builder.CreateAlignedStore(IntVal, CastTemp, TempAlignment.getQuantity())
1126 ->setVolatile(TempIsVolatile);
1127
Alexey Bataevb8329262015-02-27 06:33:30 +00001128 return convertTempToRValue(Temp, ResultSlot, Loc, AsValue);
1129}
1130
1131void AtomicInfo::EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
1132 llvm::AtomicOrdering AO, bool) {
1133 // void __atomic_load(size_t size, void *mem, void *return, int order);
1134 CallArgList Args;
1135 Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
1136 Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicAddress())),
1137 CGF.getContext().VoidPtrTy);
1138 Args.add(RValue::get(CGF.EmitCastToVoidPtr(AddForLoaded)),
1139 CGF.getContext().VoidPtrTy);
1140 Args.add(RValue::get(
1141 llvm::ConstantInt::get(CGF.IntTy, translateAtomicOrdering(AO))),
1142 CGF.getContext().IntTy);
1143 emitAtomicLibcall(CGF, "__atomic_load", CGF.getContext().VoidTy, Args);
1144}
1145
1146llvm::Value *AtomicInfo::EmitAtomicLoadOp(llvm::AtomicOrdering AO,
1147 bool IsVolatile) {
1148 // Okay, we're doing this natively.
1149 llvm::Value *Addr = emitCastToAtomicIntPointer(getAtomicAddress());
1150 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Addr, "atomic-load");
1151 Load->setAtomic(AO);
1152
1153 // Other decoration.
1154 Load->setAlignment(getAtomicAlignment().getQuantity());
1155 if (IsVolatile)
1156 Load->setVolatile(true);
1157 if (LVal.getTBAAInfo())
1158 CGF.CGM.DecorateInstruction(Load, LVal.getTBAAInfo());
1159 return Load;
Alexey Bataev452d8e12014-12-15 05:25:25 +00001160}
1161
David Majnemera5b195a2015-02-14 01:35:12 +00001162/// An LValue is a candidate for having its loads and stores be made atomic if
1163/// we are operating under /volatile:ms *and* the LValue itself is volatile and
1164/// performing such an operation can be performed without a libcall.
1165bool CodeGenFunction::LValueIsSuitableForInlineAtomic(LValue LV) {
1166 AtomicInfo AI(*this, LV);
1167 bool IsVolatile = LV.isVolatile() || hasVolatileMember(LV.getType());
1168 // An atomic is inline if we don't need to use a libcall.
1169 bool AtomicIsInline = !AI.shouldUseLibcall();
1170 return CGM.getCodeGenOpts().MSVolatile && IsVolatile && AtomicIsInline;
1171}
1172
1173/// An type is a candidate for having its loads and stores be made atomic if
1174/// we are operating under /volatile:ms *and* we know the access is volatile and
1175/// performing such an operation can be performed without a libcall.
1176bool CodeGenFunction::typeIsSuitableForInlineAtomic(QualType Ty,
1177 bool IsVolatile) const {
1178 // An atomic is inline if we don't need to use a libcall (e.g. it is builtin).
1179 bool AtomicIsInline = getContext().getTargetInfo().hasBuiltinAtomic(
1180 getContext().getTypeSize(Ty), getContext().getTypeAlign(Ty));
1181 return CGM.getCodeGenOpts().MSVolatile && IsVolatile && AtomicIsInline;
1182}
1183
1184RValue CodeGenFunction::EmitAtomicLoad(LValue LV, SourceLocation SL,
1185 AggValueSlot Slot) {
1186 llvm::AtomicOrdering AO;
1187 bool IsVolatile = LV.isVolatileQualified();
1188 if (LV.getType()->isAtomicType()) {
1189 AO = llvm::SequentiallyConsistent;
1190 } else {
1191 AO = llvm::Acquire;
1192 IsVolatile = true;
1193 }
1194 return EmitAtomicLoad(LV, SL, AO, IsVolatile, Slot);
1195}
1196
Alexey Bataevb8329262015-02-27 06:33:30 +00001197RValue AtomicInfo::EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
1198 bool AsValue, llvm::AtomicOrdering AO,
1199 bool IsVolatile) {
1200 // Check whether we should use a library call.
1201 if (shouldUseLibcall()) {
1202 llvm::Value *TempAddr;
1203 if (LVal.isSimple() && !ResultSlot.isIgnored()) {
1204 assert(getEvaluationKind() == TEK_Aggregate);
1205 TempAddr = ResultSlot.getAddr();
1206 } else
1207 TempAddr = CreateTempAlloca();
1208
1209 EmitAtomicLoadLibcall(TempAddr, AO, IsVolatile);
1210
1211 // Okay, turn that back into the original value or whole atomic (for
1212 // non-simple lvalues) type.
1213 return convertTempToRValue(TempAddr, ResultSlot, Loc, AsValue);
1214 }
1215
1216 // Okay, we're doing this natively.
1217 auto *Load = EmitAtomicLoadOp(AO, IsVolatile);
1218
1219 // If we're ignoring an aggregate return, don't do anything.
1220 if (getEvaluationKind() == TEK_Aggregate && ResultSlot.isIgnored())
1221 return RValue::getAggregate(nullptr, false);
1222
1223 // Okay, turn that back into the original value or atomic (for non-simple
1224 // lvalues) type.
1225 return ConvertIntToValueOrAtomic(Load, ResultSlot, Loc, AsValue);
1226}
1227
John McCalla8ec7eb2013-03-07 21:37:17 +00001228/// Emit a load from an l-value of atomic type. Note that the r-value
1229/// we produce is an r-value of the atomic *value* type.
Nick Lewycky2d84e842013-10-02 02:29:49 +00001230RValue CodeGenFunction::EmitAtomicLoad(LValue src, SourceLocation loc,
David Majnemera5b195a2015-02-14 01:35:12 +00001231 llvm::AtomicOrdering AO, bool IsVolatile,
Nick Lewycky2d84e842013-10-02 02:29:49 +00001232 AggValueSlot resultSlot) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001233 AtomicInfo Atomics(*this, src);
1234 return Atomics.EmitAtomicLoad(resultSlot, loc, /*AsValue=*/true, AO,
1235 IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +00001236}
1237
John McCalla8ec7eb2013-03-07 21:37:17 +00001238/// Copy an r-value into memory as part of storing to an atomic type.
1239/// This needs to create a bit-pattern suitable for atomic operations.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001240void AtomicInfo::emitCopyIntoMemory(RValue rvalue) const {
1241 assert(LVal.isSimple());
John McCalla8ec7eb2013-03-07 21:37:17 +00001242 // If we have an r-value, the rvalue should be of the atomic type,
1243 // which means that the caller is responsible for having zeroed
1244 // any padding. Just do an aggregate copy of that type.
1245 if (rvalue.isAggregate()) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001246 CGF.EmitAggregateCopy(getAtomicAddress(),
John McCalla8ec7eb2013-03-07 21:37:17 +00001247 rvalue.getAggregateAddr(),
1248 getAtomicType(),
1249 (rvalue.isVolatileQualified()
Alexey Bataevb57056f2015-01-22 06:17:56 +00001250 || LVal.isVolatileQualified()),
1251 LVal.getAlignment());
John McCalla8ec7eb2013-03-07 21:37:17 +00001252 return;
1253 }
1254
1255 // Okay, otherwise we're copying stuff.
1256
1257 // Zero out the buffer if necessary.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001258 emitMemSetZeroIfNecessary();
John McCalla8ec7eb2013-03-07 21:37:17 +00001259
1260 // Drill past the padding if present.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001261 LValue TempLVal = projectValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001262
1263 // Okay, store the rvalue in.
1264 if (rvalue.isScalar()) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001265 CGF.EmitStoreOfScalar(rvalue.getScalarVal(), TempLVal, /*init*/ true);
John McCalla8ec7eb2013-03-07 21:37:17 +00001266 } else {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001267 CGF.EmitStoreOfComplex(rvalue.getComplexVal(), TempLVal, /*init*/ true);
John McCalla8ec7eb2013-03-07 21:37:17 +00001268 }
1269}
1270
1271
1272/// Materialize an r-value into memory for the purposes of storing it
1273/// to an atomic type.
1274llvm::Value *AtomicInfo::materializeRValue(RValue rvalue) const {
1275 // Aggregate r-values are already in memory, and EmitAtomicStore
1276 // requires them to be values of the atomic type.
1277 if (rvalue.isAggregate())
1278 return rvalue.getAggregateAddr();
1279
1280 // Otherwise, make a temporary and materialize into it.
Alexey Bataevb8329262015-02-27 06:33:30 +00001281 LValue TempLV = CGF.MakeAddrLValue(CreateTempAlloca(), getAtomicType(),
1282 getAtomicAlignment());
1283 AtomicInfo Atomics(CGF, TempLV);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001284 Atomics.emitCopyIntoMemory(rvalue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001285 return TempLV.getAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +00001286}
1287
Alexey Bataev452d8e12014-12-15 05:25:25 +00001288llvm::Value *AtomicInfo::convertRValueToInt(RValue RVal) const {
1289 // If we've got a scalar value of the right size, try to avoid going
1290 // through memory.
Alexey Bataevb8329262015-02-27 06:33:30 +00001291 if (RVal.isScalar() && (!hasPadding() || !LVal.isSimple())) {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001292 llvm::Value *Value = RVal.getScalarVal();
1293 if (isa<llvm::IntegerType>(Value->getType()))
Alexey Bataevb4505a72015-03-30 05:20:59 +00001294 return CGF.EmitToMemory(Value, ValueTy);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001295 else {
Alexey Bataevb8329262015-02-27 06:33:30 +00001296 llvm::IntegerType *InputIntTy = llvm::IntegerType::get(
1297 CGF.getLLVMContext(),
1298 LVal.isSimple() ? getValueSizeInBits() : getAtomicSizeInBits());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001299 if (isa<llvm::PointerType>(Value->getType()))
1300 return CGF.Builder.CreatePtrToInt(Value, InputIntTy);
1301 else if (llvm::BitCastInst::isBitCastable(Value->getType(), InputIntTy))
1302 return CGF.Builder.CreateBitCast(Value, InputIntTy);
1303 }
1304 }
1305 // Otherwise, we need to go through memory.
1306 // Put the r-value in memory.
1307 llvm::Value *Addr = materializeRValue(RVal);
1308
1309 // Cast the temporary to the atomic int type and pull a value out.
1310 Addr = emitCastToAtomicIntPointer(Addr);
1311 return CGF.Builder.CreateAlignedLoad(Addr,
1312 getAtomicAlignment().getQuantity());
1313}
1314
Alexey Bataevb4505a72015-03-30 05:20:59 +00001315std::pair<RValue, llvm::Value *> AtomicInfo::EmitAtomicCompareExchangeOp(
1316 RValue Expected, RValue Desired, llvm::AtomicOrdering Success,
Alexey Bataevb8329262015-02-27 06:33:30 +00001317 llvm::AtomicOrdering Failure, bool IsWeak) {
1318 // Do the atomic store.
Alexey Bataevb4505a72015-03-30 05:20:59 +00001319 auto *ExpectedVal = convertRValueToInt(Expected);
1320 auto *DesiredVal = convertRValueToInt(Desired);
Alexey Bataevb8329262015-02-27 06:33:30 +00001321 auto *Addr = emitCastToAtomicIntPointer(getAtomicAddress());
Alexey Bataevb4505a72015-03-30 05:20:59 +00001322 auto *Inst = CGF.Builder.CreateAtomicCmpXchg(Addr, ExpectedVal, DesiredVal,
1323 Success, Failure);
Alexey Bataevb8329262015-02-27 06:33:30 +00001324 // Other decoration.
1325 Inst->setVolatile(LVal.isVolatileQualified());
1326 Inst->setWeak(IsWeak);
1327
1328 // Okay, turn that back into the original value type.
1329 auto *PreviousVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/0);
1330 auto *SuccessFailureVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/1);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001331 return std::make_pair(
1332 ConvertIntToValueOrAtomic(PreviousVal, AggValueSlot::ignored(),
1333 SourceLocation(), /*AsValue=*/false),
1334 SuccessFailureVal);
Alexey Bataevb8329262015-02-27 06:33:30 +00001335}
1336
Alexey Bataevb4505a72015-03-30 05:20:59 +00001337std::pair<RValue, llvm::Value *>
1338AtomicInfo::EmitAtomicCompareExchangeLibcall(RValue Expected, RValue Desired,
Alexey Bataevb8329262015-02-27 06:33:30 +00001339 llvm::AtomicOrdering Success,
1340 llvm::AtomicOrdering Failure) {
1341 // bool __atomic_compare_exchange(size_t size, void *obj, void *expected,
1342 // void *desired, int success, int failure);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001343 auto *ExpectedAddr = materializeRValue(Expected);
1344 auto *DesiredAddr = materializeRValue(Desired);
Alexey Bataevb8329262015-02-27 06:33:30 +00001345 CallArgList Args;
1346 Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
1347 Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicAddress())),
1348 CGF.getContext().VoidPtrTy);
1349 Args.add(RValue::get(CGF.EmitCastToVoidPtr(ExpectedAddr)),
1350 CGF.getContext().VoidPtrTy);
1351 Args.add(RValue::get(CGF.EmitCastToVoidPtr(DesiredAddr)),
1352 CGF.getContext().VoidPtrTy);
1353 Args.add(RValue::get(llvm::ConstantInt::get(
1354 CGF.IntTy, translateAtomicOrdering(Success))),
1355 CGF.getContext().IntTy);
1356 Args.add(RValue::get(llvm::ConstantInt::get(
1357 CGF.IntTy, translateAtomicOrdering(Failure))),
1358 CGF.getContext().IntTy);
1359 auto SuccessFailureRVal = emitAtomicLibcall(CGF, "__atomic_compare_exchange",
1360 CGF.getContext().BoolTy, Args);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001361
1362 return std::make_pair(
1363 convertTempToRValue(ExpectedAddr, AggValueSlot::ignored(),
1364 SourceLocation(), /*AsValue=*/false),
1365 SuccessFailureRVal.getScalarVal());
Alexey Bataevb8329262015-02-27 06:33:30 +00001366}
1367
Alexey Bataevb4505a72015-03-30 05:20:59 +00001368std::pair<RValue, llvm::Value *> AtomicInfo::EmitAtomicCompareExchange(
Alexey Bataevb8329262015-02-27 06:33:30 +00001369 RValue Expected, RValue Desired, llvm::AtomicOrdering Success,
1370 llvm::AtomicOrdering Failure, bool IsWeak) {
1371 if (Failure >= Success)
1372 // Don't assert on undefined behavior.
1373 Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(Success);
1374
1375 // Check whether we should use a library call.
1376 if (shouldUseLibcall()) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001377 // Produce a source address.
Alexey Bataevb4505a72015-03-30 05:20:59 +00001378 return EmitAtomicCompareExchangeLibcall(Expected, Desired, Success,
Alexey Bataevb8329262015-02-27 06:33:30 +00001379 Failure);
1380 }
1381
1382 // If we've got a scalar value of the right size, try to avoid going
1383 // through memory.
Alexey Bataevb4505a72015-03-30 05:20:59 +00001384 return EmitAtomicCompareExchangeOp(Expected, Desired, Success, Failure,
1385 IsWeak);
Alexey Bataevb8329262015-02-27 06:33:30 +00001386}
1387
David Majnemera5b195a2015-02-14 01:35:12 +00001388void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue lvalue,
1389 bool isInit) {
1390 bool IsVolatile = lvalue.isVolatileQualified();
1391 llvm::AtomicOrdering AO;
1392 if (lvalue.getType()->isAtomicType()) {
1393 AO = llvm::SequentiallyConsistent;
1394 } else {
1395 AO = llvm::Release;
1396 IsVolatile = true;
1397 }
1398 return EmitAtomicStore(rvalue, lvalue, AO, IsVolatile, isInit);
1399}
1400
John McCalla8ec7eb2013-03-07 21:37:17 +00001401/// Emit a store to an l-value of atomic type.
1402///
1403/// Note that the r-value is expected to be an r-value *of the atomic
1404/// type*; this means that for aggregate r-values, it should include
1405/// storage for any padding that was necessary.
David Majnemera5b195a2015-02-14 01:35:12 +00001406void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue dest,
1407 llvm::AtomicOrdering AO, bool IsVolatile,
1408 bool isInit) {
John McCalla8ec7eb2013-03-07 21:37:17 +00001409 // If this is an aggregate r-value, it should agree in type except
1410 // maybe for address-space qualification.
1411 assert(!rvalue.isAggregate() ||
1412 rvalue.getAggregateAddr()->getType()->getPointerElementType()
1413 == dest.getAddress()->getType()->getPointerElementType());
1414
1415 AtomicInfo atomics(*this, dest);
Alexey Bataevb8329262015-02-27 06:33:30 +00001416 LValue LVal = atomics.getAtomicLValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001417
1418 // If this is an initialization, just put the value there normally.
Alexey Bataevb8329262015-02-27 06:33:30 +00001419 if (LVal.isSimple()) {
1420 if (isInit) {
1421 atomics.emitCopyIntoMemory(rvalue);
1422 return;
1423 }
1424
1425 // Check whether we should use a library call.
1426 if (atomics.shouldUseLibcall()) {
1427 // Produce a source address.
1428 llvm::Value *srcAddr = atomics.materializeRValue(rvalue);
1429
1430 // void __atomic_store(size_t size, void *mem, void *val, int order)
1431 CallArgList args;
1432 args.add(RValue::get(atomics.getAtomicSizeValue()),
1433 getContext().getSizeType());
1434 args.add(RValue::get(EmitCastToVoidPtr(atomics.getAtomicAddress())),
1435 getContext().VoidPtrTy);
1436 args.add(RValue::get(EmitCastToVoidPtr(srcAddr)), getContext().VoidPtrTy);
1437 args.add(RValue::get(llvm::ConstantInt::get(
1438 IntTy, AtomicInfo::translateAtomicOrdering(AO))),
1439 getContext().IntTy);
1440 emitAtomicLibcall(*this, "__atomic_store", getContext().VoidTy, args);
1441 return;
1442 }
1443
1444 // Okay, we're doing this natively.
1445 llvm::Value *intValue = atomics.convertRValueToInt(rvalue);
1446
1447 // Do the atomic store.
1448 llvm::Value *addr =
1449 atomics.emitCastToAtomicIntPointer(atomics.getAtomicAddress());
1450 intValue = Builder.CreateIntCast(
1451 intValue, addr->getType()->getPointerElementType(), /*isSigned=*/false);
1452 llvm::StoreInst *store = Builder.CreateStore(intValue, addr);
1453
1454 // Initializations don't need to be atomic.
1455 if (!isInit)
1456 store->setAtomic(AO);
1457
1458 // Other decoration.
1459 store->setAlignment(dest.getAlignment().getQuantity());
1460 if (IsVolatile)
1461 store->setVolatile(true);
1462 if (dest.getTBAAInfo())
1463 CGM.DecorateInstruction(store, dest.getTBAAInfo());
John McCalla8ec7eb2013-03-07 21:37:17 +00001464 return;
1465 }
1466
Alexey Bataevb8329262015-02-27 06:33:30 +00001467 // Atomic load of prev value.
1468 RValue OldRVal =
1469 atomics.EmitAtomicLoad(AggValueSlot::ignored(), SourceLocation(),
1470 /*AsValue=*/false, AO, IsVolatile);
1471 // For non-simple lvalues perform compare-and-swap procedure.
1472 auto *ContBB = createBasicBlock("atomic_cont");
1473 auto *ExitBB = createBasicBlock("atomic_exit");
1474 auto *CurBB = Builder.GetInsertBlock();
1475 EmitBlock(ContBB);
1476 llvm::PHINode *PHI = Builder.CreatePHI(OldRVal.getScalarVal()->getType(),
1477 /*NumReservedValues=*/2);
1478 PHI->addIncoming(OldRVal.getScalarVal(), CurBB);
1479 RValue OriginalRValue = RValue::get(PHI);
1480 // Build new lvalue for temp address
1481 auto *Ptr = atomics.materializeRValue(OriginalRValue);
1482 // Build new lvalue for temp address
1483 LValue UpdateLVal;
1484 if (LVal.isBitField())
1485 UpdateLVal = LValue::MakeBitfield(Ptr, LVal.getBitFieldInfo(),
1486 LVal.getType(), LVal.getAlignment());
1487 else if (LVal.isVectorElt())
1488 UpdateLVal = LValue::MakeVectorElt(Ptr, LVal.getVectorIdx(), LVal.getType(),
1489 LVal.getAlignment());
1490 else {
1491 assert(LVal.isExtVectorElt());
1492 UpdateLVal = LValue::MakeExtVectorElt(Ptr, LVal.getExtVectorElts(),
1493 LVal.getType(), LVal.getAlignment());
John McCalla8ec7eb2013-03-07 21:37:17 +00001494 }
Alexey Bataevb8329262015-02-27 06:33:30 +00001495 UpdateLVal.setTBAAInfo(LVal.getTBAAInfo());
1496 // Store new value in the corresponding memory area
1497 EmitStoreThroughLValue(rvalue, UpdateLVal);
1498 // Load new value
1499 RValue NewRValue = RValue::get(EmitLoadOfScalar(
1500 Ptr, LVal.isVolatile(), atomics.getAtomicAlignment().getQuantity(),
1501 atomics.getAtomicType(), SourceLocation()));
1502 // Try to write new value using cmpxchg operation
1503 auto Pair = atomics.EmitAtomicCompareExchange(OriginalRValue, NewRValue, AO);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001504 PHI->addIncoming(Pair.first.getScalarVal(), ContBB);
Alexey Bataev87b13022015-03-19 08:44:10 +00001505 Builder.CreateCondBr(Pair.second, ExitBB, ContBB);
1506 EmitBlock(ExitBB, /*IsFinished=*/true);
John McCalla8ec7eb2013-03-07 21:37:17 +00001507}
1508
Alexey Bataev452d8e12014-12-15 05:25:25 +00001509/// Emit a compare-and-exchange op for atomic type.
1510///
Alexey Bataevb4505a72015-03-30 05:20:59 +00001511std::pair<RValue, llvm::Value *> CodeGenFunction::EmitAtomicCompareExchange(
Alexey Bataev452d8e12014-12-15 05:25:25 +00001512 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
1513 llvm::AtomicOrdering Success, llvm::AtomicOrdering Failure, bool IsWeak,
1514 AggValueSlot Slot) {
1515 // If this is an aggregate r-value, it should agree in type except
1516 // maybe for address-space qualification.
1517 assert(!Expected.isAggregate() ||
1518 Expected.getAggregateAddr()->getType()->getPointerElementType() ==
1519 Obj.getAddress()->getType()->getPointerElementType());
1520 assert(!Desired.isAggregate() ||
1521 Desired.getAggregateAddr()->getType()->getPointerElementType() ==
1522 Obj.getAddress()->getType()->getPointerElementType());
1523 AtomicInfo Atomics(*this, Obj);
1524
Alexey Bataevb4505a72015-03-30 05:20:59 +00001525 return Atomics.EmitAtomicCompareExchange(Expected, Desired, Success, Failure,
1526 IsWeak);
1527}
1528
1529void CodeGenFunction::EmitAtomicUpdate(
1530 LValue LVal, llvm::AtomicOrdering AO,
1531 const std::function<RValue(RValue)> &UpdateOp, bool IsVolatile) {
1532 AtomicInfo Atomics(*this, LVal);
1533 LValue AtomicLVal = Atomics.getAtomicLValue();
1534
1535 // Atomic load of prev value.
1536 RValue OldRVal =
1537 Atomics.EmitAtomicLoad(AggValueSlot::ignored(), SourceLocation(),
1538 /*AsValue=*/false, AO, IsVolatile);
1539 bool IsScalar = OldRVal.isScalar();
1540 auto *OldVal =
1541 IsScalar ? OldRVal.getScalarVal() : Atomics.convertRValueToInt(OldRVal);
1542 // For non-simple lvalues perform compare-and-swap procedure.
1543 auto *ContBB = createBasicBlock("atomic_cont");
1544 auto *ExitBB = createBasicBlock("atomic_exit");
1545 auto *CurBB = Builder.GetInsertBlock();
1546 EmitBlock(ContBB);
1547 llvm::PHINode *PHI = Builder.CreatePHI(OldVal->getType(),
1548 /*NumReservedValues=*/2);
1549 PHI->addIncoming(OldVal, CurBB);
1550 RValue OriginalRValue =
1551 IsScalar ? RValue::get(PHI) : Atomics.ConvertIntToValueOrAtomic(
1552 PHI, AggValueSlot::ignored(),
1553 SourceLocation(), /*AsValue=*/false);
1554 // Build new lvalue for temp address
1555 LValue UpdateLVal;
1556 llvm::Value *Ptr = nullptr;
1557 RValue UpRVal;
1558 if (AtomicLVal.isSimple()) {
1559 UpRVal = OriginalRValue;
1560 } else {
1561 // Build new lvalue for temp address
1562 Ptr = Atomics.materializeRValue(OriginalRValue);
1563 if (AtomicLVal.isBitField())
1564 UpdateLVal =
1565 LValue::MakeBitfield(Ptr, AtomicLVal.getBitFieldInfo(),
1566 AtomicLVal.getType(), AtomicLVal.getAlignment());
1567 else if (AtomicLVal.isVectorElt())
1568 UpdateLVal = LValue::MakeVectorElt(Ptr, AtomicLVal.getVectorIdx(),
1569 AtomicLVal.getType(),
1570 AtomicLVal.getAlignment());
1571 else {
1572 assert(AtomicLVal.isExtVectorElt());
1573 UpdateLVal = LValue::MakeExtVectorElt(Ptr, AtomicLVal.getExtVectorElts(),
1574 AtomicLVal.getType(),
1575 AtomicLVal.getAlignment());
1576 }
1577 UpdateLVal.setTBAAInfo(LVal.getTBAAInfo());
1578 UpRVal = EmitLoadOfLValue(UpdateLVal, SourceLocation());
1579 }
1580 // Store new value in the corresponding memory area
1581 RValue NewRVal = UpdateOp(UpRVal);
1582 if (!AtomicLVal.isSimple()) {
1583 EmitStoreThroughLValue(NewRVal, UpdateLVal);
1584 // Load new value
1585 NewRVal = RValue::get(
1586 EmitLoadOfScalar(Ptr, AtomicLVal.isVolatile(),
1587 Atomics.getAtomicAlignment().getQuantity(),
1588 Atomics.getAtomicType(), SourceLocation()));
1589 }
1590 // Try to write new value using cmpxchg operation
1591 auto Pair = Atomics.EmitAtomicCompareExchange(OriginalRValue, NewRVal, AO);
1592 OldVal = IsScalar ? Pair.first.getScalarVal()
1593 : Atomics.convertRValueToInt(Pair.first);
1594 PHI->addIncoming(OldVal, ContBB);
1595 Builder.CreateCondBr(Pair.second, ExitBB, ContBB);
1596 EmitBlock(ExitBB, /*IsFinished=*/true);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001597}
1598
John McCalla8ec7eb2013-03-07 21:37:17 +00001599void CodeGenFunction::EmitAtomicInit(Expr *init, LValue dest) {
1600 AtomicInfo atomics(*this, dest);
1601
1602 switch (atomics.getEvaluationKind()) {
1603 case TEK_Scalar: {
1604 llvm::Value *value = EmitScalarExpr(init);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001605 atomics.emitCopyIntoMemory(RValue::get(value));
John McCalla8ec7eb2013-03-07 21:37:17 +00001606 return;
1607 }
1608
1609 case TEK_Complex: {
1610 ComplexPairTy value = EmitComplexExpr(init);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001611 atomics.emitCopyIntoMemory(RValue::getComplex(value));
John McCalla8ec7eb2013-03-07 21:37:17 +00001612 return;
1613 }
1614
1615 case TEK_Aggregate: {
Eli Friedmanbe4504d2013-07-11 01:32:21 +00001616 // Fix up the destination if the initializer isn't an expression
1617 // of atomic type.
1618 bool Zeroed = false;
John McCalla8ec7eb2013-03-07 21:37:17 +00001619 if (!init->getType()->isAtomicType()) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001620 Zeroed = atomics.emitMemSetZeroIfNecessary();
1621 dest = atomics.projectValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001622 }
1623
1624 // Evaluate the expression directly into the destination.
1625 AggValueSlot slot = AggValueSlot::forLValue(dest,
1626 AggValueSlot::IsNotDestructed,
1627 AggValueSlot::DoesNotNeedGCBarriers,
Eli Friedmanbe4504d2013-07-11 01:32:21 +00001628 AggValueSlot::IsNotAliased,
1629 Zeroed ? AggValueSlot::IsZeroed :
1630 AggValueSlot::IsNotZeroed);
1631
John McCalla8ec7eb2013-03-07 21:37:17 +00001632 EmitAggExpr(init, slot);
1633 return;
1634 }
1635 }
1636 llvm_unreachable("bad evaluation kind");
1637}