blob: 4e457fd43f8c7aeb070612d9cfec1f5d7e51a2d4 [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()));
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
176 /// Cast the given pointer to an integer pointer suitable for
177 /// atomic operations.
John McCall7f416cc2015-09-08 08:05:57 +0000178 Address emitCastToAtomicIntPointer(Address addr) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000179
180 /// Turn an atomic-layout object into an r-value.
John McCall7f416cc2015-09-08 08:05:57 +0000181 RValue convertAtomicTempToRValue(Address addr, AggValueSlot resultSlot,
182 SourceLocation loc, bool AsValue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000183
Alexey Bataev452d8e12014-12-15 05:25:25 +0000184 /// \brief Converts a rvalue to integer value.
185 llvm::Value *convertRValueToInt(RValue RVal) const;
186
Alexey Bataevb8329262015-02-27 06:33:30 +0000187 RValue ConvertIntToValueOrAtomic(llvm::Value *IntVal,
188 AggValueSlot ResultSlot,
189 SourceLocation Loc, bool AsValue) const;
Alexey Bataev452d8e12014-12-15 05:25:25 +0000190
John McCalla8ec7eb2013-03-07 21:37:17 +0000191 /// Copy an atomic r-value into atomic-layout memory.
Alexey Bataevb57056f2015-01-22 06:17:56 +0000192 void emitCopyIntoMemory(RValue rvalue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000193
194 /// Project an l-value down to the value field.
Alexey Bataevb57056f2015-01-22 06:17:56 +0000195 LValue projectValue() const {
196 assert(LVal.isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000197 Address addr = getAtomicAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +0000198 if (hasPadding())
John McCall7f416cc2015-09-08 08:05:57 +0000199 addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
John McCalla8ec7eb2013-03-07 21:37:17 +0000200
John McCall7f416cc2015-09-08 08:05:57 +0000201 return LValue::MakeAddr(addr, getValueType(), CGF.getContext(),
202 LVal.getAlignmentSource(), LVal.getTBAAInfo());
John McCalla8ec7eb2013-03-07 21:37:17 +0000203 }
204
Alexey Bataevb8329262015-02-27 06:33:30 +0000205 /// \brief Emits atomic load.
206 /// \returns Loaded value.
207 RValue EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
208 bool AsValue, llvm::AtomicOrdering AO,
209 bool IsVolatile);
210
211 /// \brief Emits atomic compare-and-exchange sequence.
212 /// \param Expected Expected value.
213 /// \param Desired Desired value.
214 /// \param Success Atomic ordering for success operation.
215 /// \param Failure Atomic ordering for failed operation.
216 /// \param IsWeak true if atomic operation is weak, false otherwise.
217 /// \returns Pair of values: previous value from storage (value type) and
218 /// boolean flag (i1 type) with true if success and false otherwise.
Alexey Bataevb4505a72015-03-30 05:20:59 +0000219 std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange(
Alexey Bataevb8329262015-02-27 06:33:30 +0000220 RValue Expected, RValue Desired,
221 llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
222 llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
223 bool IsWeak = false);
224
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000225 /// \brief Emits atomic update.
NAKAMURA Takumid16af5d2015-05-15 13:47:52 +0000226 /// \param AO Atomic ordering.
227 /// \param UpdateOp Update operation for the current lvalue.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000228 void EmitAtomicUpdate(llvm::AtomicOrdering AO,
229 const llvm::function_ref<RValue(RValue)> &UpdateOp,
230 bool IsVolatile);
231 /// \brief Emits atomic update.
NAKAMURA Takumid16af5d2015-05-15 13:47:52 +0000232 /// \param AO Atomic ordering.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000233 void EmitAtomicUpdate(llvm::AtomicOrdering AO, RValue UpdateRVal,
234 bool IsVolatile);
235
John McCalla8ec7eb2013-03-07 21:37:17 +0000236 /// Materialize an atomic r-value in atomic-layout memory.
John McCall7f416cc2015-09-08 08:05:57 +0000237 Address materializeRValue(RValue rvalue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000238
Alexey Bataevb8329262015-02-27 06:33:30 +0000239 /// \brief Translates LLVM atomic ordering to GNU atomic ordering for
240 /// libcalls.
241 static AtomicExpr::AtomicOrderingKind
242 translateAtomicOrdering(const llvm::AtomicOrdering AO);
243
John McCalla8ec7eb2013-03-07 21:37:17 +0000244 private:
245 bool requiresMemSetZero(llvm::Type *type) const;
Alexey Bataevb8329262015-02-27 06:33:30 +0000246
247 /// \brief Creates temp alloca for intermediate operations on atomic value.
John McCall7f416cc2015-09-08 08:05:57 +0000248 Address CreateTempAlloca() const;
Alexey Bataevb8329262015-02-27 06:33:30 +0000249
250 /// \brief Emits atomic load as a libcall.
251 void EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
252 llvm::AtomicOrdering AO, bool IsVolatile);
253 /// \brief Emits atomic load as LLVM instruction.
254 llvm::Value *EmitAtomicLoadOp(llvm::AtomicOrdering AO, bool IsVolatile);
255 /// \brief Emits atomic compare-and-exchange op as a libcall.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000256 llvm::Value *EmitAtomicCompareExchangeLibcall(
257 llvm::Value *ExpectedAddr, llvm::Value *DesiredAddr,
Alexey Bataevb8329262015-02-27 06:33:30 +0000258 llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
259 llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent);
260 /// \brief Emits atomic compare-and-exchange op as LLVM instruction.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000261 std::pair<llvm::Value *, llvm::Value *> EmitAtomicCompareExchangeOp(
262 llvm::Value *ExpectedVal, llvm::Value *DesiredVal,
Alexey Bataevb8329262015-02-27 06:33:30 +0000263 llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
264 llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
265 bool IsWeak = false);
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000266 /// \brief Emit atomic update as libcalls.
267 void
268 EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO,
269 const llvm::function_ref<RValue(RValue)> &UpdateOp,
270 bool IsVolatile);
271 /// \brief Emit atomic update as LLVM instructions.
272 void EmitAtomicUpdateOp(llvm::AtomicOrdering AO,
273 const llvm::function_ref<RValue(RValue)> &UpdateOp,
274 bool IsVolatile);
275 /// \brief Emit atomic update as libcalls.
276 void EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO, RValue UpdateRVal,
277 bool IsVolatile);
278 /// \brief Emit atomic update as LLVM instructions.
279 void EmitAtomicUpdateOp(llvm::AtomicOrdering AO, RValue UpdateRal,
280 bool IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +0000281 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000282}
John McCalla8ec7eb2013-03-07 21:37:17 +0000283
Alexey Bataevb8329262015-02-27 06:33:30 +0000284AtomicExpr::AtomicOrderingKind
285AtomicInfo::translateAtomicOrdering(const llvm::AtomicOrdering AO) {
286 switch (AO) {
287 case llvm::Unordered:
288 case llvm::NotAtomic:
289 case llvm::Monotonic:
290 return AtomicExpr::AO_ABI_memory_order_relaxed;
291 case llvm::Acquire:
292 return AtomicExpr::AO_ABI_memory_order_acquire;
293 case llvm::Release:
294 return AtomicExpr::AO_ABI_memory_order_release;
295 case llvm::AcquireRelease:
296 return AtomicExpr::AO_ABI_memory_order_acq_rel;
297 case llvm::SequentiallyConsistent:
298 return AtomicExpr::AO_ABI_memory_order_seq_cst;
299 }
Aaron Ballman152ad172015-02-27 13:55:58 +0000300 llvm_unreachable("Unhandled AtomicOrdering");
Alexey Bataevb8329262015-02-27 06:33:30 +0000301}
302
John McCall7f416cc2015-09-08 08:05:57 +0000303Address AtomicInfo::CreateTempAlloca() const {
304 Address TempAlloca = CGF.CreateMemTemp(
Alexey Bataevb8329262015-02-27 06:33:30 +0000305 (LVal.isBitField() && ValueSizeInBits > AtomicSizeInBits) ? ValueTy
306 : AtomicTy,
John McCall7f416cc2015-09-08 08:05:57 +0000307 getAtomicAlignment(),
Alexey Bataevb8329262015-02-27 06:33:30 +0000308 "atomic-temp");
Alexey Bataevb8329262015-02-27 06:33:30 +0000309 // Cast to pointer to value type for bitfields.
310 if (LVal.isBitField())
311 return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +0000312 TempAlloca, getAtomicAddress().getType());
Alexey Bataevb8329262015-02-27 06:33:30 +0000313 return TempAlloca;
314}
315
John McCalla8ec7eb2013-03-07 21:37:17 +0000316static RValue emitAtomicLibcall(CodeGenFunction &CGF,
317 StringRef fnName,
318 QualType resultType,
319 CallArgList &args) {
320 const CGFunctionInfo &fnInfo =
321 CGF.CGM.getTypes().arrangeFreeFunctionCall(resultType, args,
322 FunctionType::ExtInfo(), RequiredArgs::All);
323 llvm::FunctionType *fnTy = CGF.CGM.getTypes().GetFunctionType(fnInfo);
324 llvm::Constant *fn = CGF.CGM.CreateRuntimeFunction(fnTy, fnName);
325 return CGF.EmitCall(fnInfo, fn, ReturnValueSlot(), args);
326}
327
328/// Does a store of the given IR type modify the full expected width?
329static bool isFullSizeType(CodeGenModule &CGM, llvm::Type *type,
330 uint64_t expectedSize) {
331 return (CGM.getDataLayout().getTypeStoreSize(type) * 8 == expectedSize);
332}
333
334/// Does the atomic type require memsetting to zero before initialization?
335///
336/// The IR type is provided as a way of making certain queries faster.
337bool AtomicInfo::requiresMemSetZero(llvm::Type *type) const {
338 // If the atomic type has size padding, we definitely need a memset.
339 if (hasPadding()) return true;
340
341 // Otherwise, do some simple heuristics to try to avoid it:
342 switch (getEvaluationKind()) {
343 // For scalars and complexes, check whether the store size of the
344 // type uses the full size.
345 case TEK_Scalar:
346 return !isFullSizeType(CGF.CGM, type, AtomicSizeInBits);
347 case TEK_Complex:
348 return !isFullSizeType(CGF.CGM, type->getStructElementType(0),
349 AtomicSizeInBits / 2);
350
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000351 // Padding in structs has an undefined bit pattern. User beware.
John McCalla8ec7eb2013-03-07 21:37:17 +0000352 case TEK_Aggregate:
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000353 return false;
John McCalla8ec7eb2013-03-07 21:37:17 +0000354 }
355 llvm_unreachable("bad evaluation kind");
356}
357
Alexey Bataevb57056f2015-01-22 06:17:56 +0000358bool AtomicInfo::emitMemSetZeroIfNecessary() const {
359 assert(LVal.isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000360 llvm::Value *addr = LVal.getPointer();
John McCalla8ec7eb2013-03-07 21:37:17 +0000361 if (!requiresMemSetZero(addr->getType()->getPointerElementType()))
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000362 return false;
John McCalla8ec7eb2013-03-07 21:37:17 +0000363
Alexey Bataevb8329262015-02-27 06:33:30 +0000364 CGF.Builder.CreateMemSet(
365 addr, llvm::ConstantInt::get(CGF.Int8Ty, 0),
366 CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits).getQuantity(),
367 LVal.getAlignment().getQuantity());
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000368 return true;
John McCalla8ec7eb2013-03-07 21:37:17 +0000369}
370
Tim Northovercadbbe12014-06-13 19:43:04 +0000371static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak,
John McCall7f416cc2015-09-08 08:05:57 +0000372 Address Dest, Address Ptr,
373 Address Val1, Address Val2,
374 uint64_t Size,
Tim Northover9c177222014-03-13 19:25:48 +0000375 llvm::AtomicOrdering SuccessOrder,
376 llvm::AtomicOrdering FailureOrder) {
377 // Note that cmpxchg doesn't support weak cmpxchg, at least at the moment.
John McCall7f416cc2015-09-08 08:05:57 +0000378 llvm::Value *Expected = CGF.Builder.CreateLoad(Val1);
379 llvm::Value *Desired = CGF.Builder.CreateLoad(Val2);
Tim Northover9c177222014-03-13 19:25:48 +0000380
Tim Northoverb49b04b2014-06-13 14:24:59 +0000381 llvm::AtomicCmpXchgInst *Pair = CGF.Builder.CreateAtomicCmpXchg(
John McCall7f416cc2015-09-08 08:05:57 +0000382 Ptr.getPointer(), Expected, Desired, SuccessOrder, FailureOrder);
Tim Northoverb49b04b2014-06-13 14:24:59 +0000383 Pair->setVolatile(E->isVolatile());
Tim Northovercadbbe12014-06-13 19:43:04 +0000384 Pair->setWeak(IsWeak);
Tim Northover9c177222014-03-13 19:25:48 +0000385
386 // Cmp holds the result of the compare-exchange operation: true on success,
387 // false on failure.
Tim Northoverb49b04b2014-06-13 14:24:59 +0000388 llvm::Value *Old = CGF.Builder.CreateExtractValue(Pair, 0);
389 llvm::Value *Cmp = CGF.Builder.CreateExtractValue(Pair, 1);
Tim Northover9c177222014-03-13 19:25:48 +0000390
391 // This basic block is used to hold the store instruction if the operation
392 // failed.
393 llvm::BasicBlock *StoreExpectedBB =
394 CGF.createBasicBlock("cmpxchg.store_expected", CGF.CurFn);
395
396 // This basic block is the exit point of the operation, we should end up
397 // here regardless of whether or not the operation succeeded.
398 llvm::BasicBlock *ContinueBB =
399 CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
400
401 // Update Expected if Expected isn't equal to Old, otherwise branch to the
402 // exit point.
403 CGF.Builder.CreateCondBr(Cmp, ContinueBB, StoreExpectedBB);
404
405 CGF.Builder.SetInsertPoint(StoreExpectedBB);
406 // Update the memory at Expected with Old's value.
John McCall7f416cc2015-09-08 08:05:57 +0000407 CGF.Builder.CreateStore(Old, Val1);
Tim Northover9c177222014-03-13 19:25:48 +0000408 // Finally, branch to the exit point.
409 CGF.Builder.CreateBr(ContinueBB);
410
411 CGF.Builder.SetInsertPoint(ContinueBB);
412 // Update the memory at Dest with Cmp's value.
413 CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType()));
Tim Northover9c177222014-03-13 19:25:48 +0000414}
415
416/// Given an ordering required on success, emit all possible cmpxchg
417/// instructions to cope with the provided (but possibly only dynamically known)
418/// FailureOrder.
419static void emitAtomicCmpXchgFailureSet(CodeGenFunction &CGF, AtomicExpr *E,
John McCall7f416cc2015-09-08 08:05:57 +0000420 bool IsWeak, Address Dest,
421 Address Ptr, Address Val1,
422 Address Val2,
Tim Northover9c177222014-03-13 19:25:48 +0000423 llvm::Value *FailureOrderVal,
John McCall7f416cc2015-09-08 08:05:57 +0000424 uint64_t Size,
Tim Northover9c177222014-03-13 19:25:48 +0000425 llvm::AtomicOrdering SuccessOrder) {
426 llvm::AtomicOrdering FailureOrder;
427 if (llvm::ConstantInt *FO = dyn_cast<llvm::ConstantInt>(FailureOrderVal)) {
428 switch (FO->getSExtValue()) {
429 default:
430 FailureOrder = llvm::Monotonic;
431 break;
432 case AtomicExpr::AO_ABI_memory_order_consume:
433 case AtomicExpr::AO_ABI_memory_order_acquire:
434 FailureOrder = llvm::Acquire;
435 break;
436 case AtomicExpr::AO_ABI_memory_order_seq_cst:
437 FailureOrder = llvm::SequentiallyConsistent;
438 break;
439 }
440 if (FailureOrder >= SuccessOrder) {
441 // Don't assert on undefined behaviour.
442 FailureOrder =
443 llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrder);
444 }
John McCall7f416cc2015-09-08 08:05:57 +0000445 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2, Size,
Tim Northovercadbbe12014-06-13 19:43:04 +0000446 SuccessOrder, FailureOrder);
Tim Northover9c177222014-03-13 19:25:48 +0000447 return;
448 }
449
450 // Create all the relevant BB's
Craig Topper8a13c412014-05-21 05:09:00 +0000451 llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
452 *SeqCstBB = nullptr;
Tim Northover9c177222014-03-13 19:25:48 +0000453 MonotonicBB = CGF.createBasicBlock("monotonic_fail", CGF.CurFn);
454 if (SuccessOrder != llvm::Monotonic && SuccessOrder != llvm::Release)
455 AcquireBB = CGF.createBasicBlock("acquire_fail", CGF.CurFn);
456 if (SuccessOrder == llvm::SequentiallyConsistent)
457 SeqCstBB = CGF.createBasicBlock("seqcst_fail", CGF.CurFn);
458
459 llvm::BasicBlock *ContBB = CGF.createBasicBlock("atomic.continue", CGF.CurFn);
460
461 llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(FailureOrderVal, MonotonicBB);
462
463 // Emit all the different atomics
464
465 // MonotonicBB is arbitrarily chosen as the default case; in practice, this
466 // doesn't matter unless someone is crazy enough to use something that
467 // doesn't fold to a constant for the ordering.
468 CGF.Builder.SetInsertPoint(MonotonicBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000469 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000470 Size, SuccessOrder, llvm::Monotonic);
Tim Northover9c177222014-03-13 19:25:48 +0000471 CGF.Builder.CreateBr(ContBB);
472
473 if (AcquireBB) {
474 CGF.Builder.SetInsertPoint(AcquireBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000475 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000476 Size, SuccessOrder, llvm::Acquire);
Tim Northover9c177222014-03-13 19:25:48 +0000477 CGF.Builder.CreateBr(ContBB);
478 SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_consume),
479 AcquireBB);
480 SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acquire),
481 AcquireBB);
482 }
483 if (SeqCstBB) {
484 CGF.Builder.SetInsertPoint(SeqCstBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000485 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000486 Size, SuccessOrder, llvm::SequentiallyConsistent);
Tim Northover9c177222014-03-13 19:25:48 +0000487 CGF.Builder.CreateBr(ContBB);
488 SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_seq_cst),
489 SeqCstBB);
490 }
491
492 CGF.Builder.SetInsertPoint(ContBB);
493}
494
John McCall7f416cc2015-09-08 08:05:57 +0000495static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, Address Dest,
496 Address Ptr, Address Val1, Address Val2,
Tim Northovercadbbe12014-06-13 19:43:04 +0000497 llvm::Value *IsWeak, llvm::Value *FailureOrder,
John McCall7f416cc2015-09-08 08:05:57 +0000498 uint64_t Size, llvm::AtomicOrdering Order) {
John McCallfc207f22013-03-07 21:37:12 +0000499 llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add;
500 llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0;
501
502 switch (E->getOp()) {
503 case AtomicExpr::AO__c11_atomic_init:
504 llvm_unreachable("Already handled!");
505
506 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
Tim Northovercadbbe12014-06-13 19:43:04 +0000507 emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000508 FailureOrder, Size, Order);
John McCallfc207f22013-03-07 21:37:12 +0000509 return;
Tim Northovercadbbe12014-06-13 19:43:04 +0000510 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
511 emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000512 FailureOrder, Size, Order);
Tim Northovercadbbe12014-06-13 19:43:04 +0000513 return;
514 case AtomicExpr::AO__atomic_compare_exchange:
515 case AtomicExpr::AO__atomic_compare_exchange_n: {
516 if (llvm::ConstantInt *IsWeakC = dyn_cast<llvm::ConstantInt>(IsWeak)) {
517 emitAtomicCmpXchgFailureSet(CGF, E, IsWeakC->getZExtValue(), Dest, Ptr,
John McCall7f416cc2015-09-08 08:05:57 +0000518 Val1, Val2, FailureOrder, Size, Order);
Tim Northovercadbbe12014-06-13 19:43:04 +0000519 } else {
520 // Create all the relevant BB's
521 llvm::BasicBlock *StrongBB =
522 CGF.createBasicBlock("cmpxchg.strong", CGF.CurFn);
523 llvm::BasicBlock *WeakBB = CGF.createBasicBlock("cmxchg.weak", CGF.CurFn);
524 llvm::BasicBlock *ContBB =
525 CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
526
527 llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(IsWeak, WeakBB);
528 SI->addCase(CGF.Builder.getInt1(false), StrongBB);
529
530 CGF.Builder.SetInsertPoint(StrongBB);
531 emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000532 FailureOrder, Size, Order);
Tim Northovercadbbe12014-06-13 19:43:04 +0000533 CGF.Builder.CreateBr(ContBB);
534
535 CGF.Builder.SetInsertPoint(WeakBB);
536 emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
John McCall7f416cc2015-09-08 08:05:57 +0000537 FailureOrder, Size, Order);
Tim Northovercadbbe12014-06-13 19:43:04 +0000538 CGF.Builder.CreateBr(ContBB);
539
540 CGF.Builder.SetInsertPoint(ContBB);
541 }
542 return;
543 }
John McCallfc207f22013-03-07 21:37:12 +0000544 case AtomicExpr::AO__c11_atomic_load:
545 case AtomicExpr::AO__atomic_load_n:
546 case AtomicExpr::AO__atomic_load: {
547 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr);
548 Load->setAtomic(Order);
John McCallfc207f22013-03-07 21:37:12 +0000549 Load->setVolatile(E->isVolatile());
John McCall7f416cc2015-09-08 08:05:57 +0000550 CGF.Builder.CreateStore(Load, Dest);
John McCallfc207f22013-03-07 21:37:12 +0000551 return;
552 }
553
554 case AtomicExpr::AO__c11_atomic_store:
555 case AtomicExpr::AO__atomic_store:
556 case AtomicExpr::AO__atomic_store_n: {
John McCall7f416cc2015-09-08 08:05:57 +0000557 assert(!Dest.isValid() && "Store does not return a value");
558 llvm::Value *LoadVal1 = CGF.Builder.CreateLoad(Val1);
John McCallfc207f22013-03-07 21:37:12 +0000559 llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr);
560 Store->setAtomic(Order);
John McCallfc207f22013-03-07 21:37:12 +0000561 Store->setVolatile(E->isVolatile());
562 return;
563 }
564
565 case AtomicExpr::AO__c11_atomic_exchange:
566 case AtomicExpr::AO__atomic_exchange_n:
567 case AtomicExpr::AO__atomic_exchange:
568 Op = llvm::AtomicRMWInst::Xchg;
569 break;
570
571 case AtomicExpr::AO__atomic_add_fetch:
572 PostOp = llvm::Instruction::Add;
573 // Fall through.
574 case AtomicExpr::AO__c11_atomic_fetch_add:
575 case AtomicExpr::AO__atomic_fetch_add:
576 Op = llvm::AtomicRMWInst::Add;
577 break;
578
579 case AtomicExpr::AO__atomic_sub_fetch:
580 PostOp = llvm::Instruction::Sub;
581 // Fall through.
582 case AtomicExpr::AO__c11_atomic_fetch_sub:
583 case AtomicExpr::AO__atomic_fetch_sub:
584 Op = llvm::AtomicRMWInst::Sub;
585 break;
586
587 case AtomicExpr::AO__atomic_and_fetch:
588 PostOp = llvm::Instruction::And;
589 // Fall through.
590 case AtomicExpr::AO__c11_atomic_fetch_and:
591 case AtomicExpr::AO__atomic_fetch_and:
592 Op = llvm::AtomicRMWInst::And;
593 break;
594
595 case AtomicExpr::AO__atomic_or_fetch:
596 PostOp = llvm::Instruction::Or;
597 // Fall through.
598 case AtomicExpr::AO__c11_atomic_fetch_or:
599 case AtomicExpr::AO__atomic_fetch_or:
600 Op = llvm::AtomicRMWInst::Or;
601 break;
602
603 case AtomicExpr::AO__atomic_xor_fetch:
604 PostOp = llvm::Instruction::Xor;
605 // Fall through.
606 case AtomicExpr::AO__c11_atomic_fetch_xor:
607 case AtomicExpr::AO__atomic_fetch_xor:
608 Op = llvm::AtomicRMWInst::Xor;
609 break;
610
611 case AtomicExpr::AO__atomic_nand_fetch:
612 PostOp = llvm::Instruction::And;
613 // Fall through.
614 case AtomicExpr::AO__atomic_fetch_nand:
615 Op = llvm::AtomicRMWInst::Nand;
616 break;
617 }
618
John McCall7f416cc2015-09-08 08:05:57 +0000619 llvm::Value *LoadVal1 = CGF.Builder.CreateLoad(Val1);
John McCallfc207f22013-03-07 21:37:12 +0000620 llvm::AtomicRMWInst *RMWI =
John McCall7f416cc2015-09-08 08:05:57 +0000621 CGF.Builder.CreateAtomicRMW(Op, Ptr.getPointer(), LoadVal1, Order);
John McCallfc207f22013-03-07 21:37:12 +0000622 RMWI->setVolatile(E->isVolatile());
623
624 // For __atomic_*_fetch operations, perform the operation again to
625 // determine the value which was written.
626 llvm::Value *Result = RMWI;
627 if (PostOp)
628 Result = CGF.Builder.CreateBinOp(PostOp, RMWI, LoadVal1);
629 if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch)
630 Result = CGF.Builder.CreateNot(Result);
John McCall7f416cc2015-09-08 08:05:57 +0000631 CGF.Builder.CreateStore(Result, Dest);
John McCallfc207f22013-03-07 21:37:12 +0000632}
633
634// This function emits any expression (scalar, complex, or aggregate)
635// into a temporary alloca.
John McCall7f416cc2015-09-08 08:05:57 +0000636static Address
John McCallfc207f22013-03-07 21:37:12 +0000637EmitValToTemp(CodeGenFunction &CGF, Expr *E) {
John McCall7f416cc2015-09-08 08:05:57 +0000638 Address DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp");
John McCallfc207f22013-03-07 21:37:12 +0000639 CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(),
640 /*Init*/ true);
641 return DeclPtr;
642}
643
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000644static void
645AddDirectArgument(CodeGenFunction &CGF, CallArgList &Args,
Nick Lewycky2d84e842013-10-02 02:29:49 +0000646 bool UseOptimizedLibcall, llvm::Value *Val, QualType ValTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000647 SourceLocation Loc, CharUnits SizeInChars) {
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000648 if (UseOptimizedLibcall) {
649 // Load value and pass it to the function directly.
John McCall7f416cc2015-09-08 08:05:57 +0000650 CharUnits Align = CGF.getContext().getTypeAlignInChars(ValTy);
David Majnemer0392cf82014-08-29 07:27:49 +0000651 int64_t SizeInBits = CGF.getContext().toBits(SizeInChars);
652 ValTy =
653 CGF.getContext().getIntTypeForBitwidth(SizeInBits, /*Signed=*/false);
654 llvm::Type *IPtrTy = llvm::IntegerType::get(CGF.getLLVMContext(),
655 SizeInBits)->getPointerTo();
John McCall7f416cc2015-09-08 08:05:57 +0000656 Address Ptr = Address(CGF.Builder.CreateBitCast(Val, IPtrTy), Align);
657 Val = CGF.EmitLoadOfScalar(Ptr, false,
658 CGF.getContext().getPointerType(ValTy),
David Majnemer0392cf82014-08-29 07:27:49 +0000659 Loc);
660 // Coerce the value into an appropriately sized integer type.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000661 Args.add(RValue::get(Val), ValTy);
662 } else {
663 // Non-optimized functions always take a reference.
664 Args.add(RValue::get(CGF.EmitCastToVoidPtr(Val)),
665 CGF.getContext().VoidPtrTy);
666 }
667}
668
John McCall7f416cc2015-09-08 08:05:57 +0000669RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, Address Dest) {
John McCallfc207f22013-03-07 21:37:12 +0000670 QualType AtomicTy = E->getPtr()->getType()->getPointeeType();
671 QualType MemTy = AtomicTy;
672 if (const AtomicType *AT = AtomicTy->getAs<AtomicType>())
673 MemTy = AT->getValueType();
John McCall7f416cc2015-09-08 08:05:57 +0000674 CharUnits sizeChars, alignChars;
675 std::tie(sizeChars, alignChars) = getContext().getTypeInfoInChars(AtomicTy);
John McCallfc207f22013-03-07 21:37:12 +0000676 uint64_t Size = sizeChars.getQuantity();
John McCall7f416cc2015-09-08 08:05:57 +0000677 unsigned MaxInlineWidthInBits = getTarget().getMaxAtomicInlineWidth();
678 bool UseLibcall = (sizeChars != alignChars ||
John McCallfc207f22013-03-07 21:37:12 +0000679 getContext().toBits(sizeChars) > MaxInlineWidthInBits);
680
John McCall7f416cc2015-09-08 08:05:57 +0000681 llvm::Value *IsWeak = nullptr, *OrderFail = nullptr;
682
683 Address Val1 = Address::invalid();
684 Address Val2 = Address::invalid();
685 Address Ptr(EmitScalarExpr(E->getPtr()), alignChars);
John McCallfc207f22013-03-07 21:37:12 +0000686
687 if (E->getOp() == AtomicExpr::AO__c11_atomic_init) {
John McCall7f416cc2015-09-08 08:05:57 +0000688 assert(!Dest.isValid() && "Init does not return a value");
689 LValue lvalue = MakeAddrLValue(Ptr, AtomicTy);
John McCalla8ec7eb2013-03-07 21:37:17 +0000690 EmitAtomicInit(E->getVal1(), lvalue);
Craig Topper8a13c412014-05-21 05:09:00 +0000691 return RValue::get(nullptr);
John McCallfc207f22013-03-07 21:37:12 +0000692 }
693
Craig Topper8a13c412014-05-21 05:09:00 +0000694 llvm::Value *Order = EmitScalarExpr(E->getOrder());
John McCallfc207f22013-03-07 21:37:12 +0000695
696 switch (E->getOp()) {
697 case AtomicExpr::AO__c11_atomic_init:
James Y Knight81167fb2015-08-05 16:57:36 +0000698 llvm_unreachable("Already handled above with EmitAtomicInit!");
John McCallfc207f22013-03-07 21:37:12 +0000699
700 case AtomicExpr::AO__c11_atomic_load:
701 case AtomicExpr::AO__atomic_load_n:
702 break;
703
704 case AtomicExpr::AO__atomic_load:
John McCall7f416cc2015-09-08 08:05:57 +0000705 Dest = EmitPointerWithAlignment(E->getVal1());
John McCallfc207f22013-03-07 21:37:12 +0000706 break;
707
708 case AtomicExpr::AO__atomic_store:
John McCall7f416cc2015-09-08 08:05:57 +0000709 Val1 = EmitPointerWithAlignment(E->getVal1());
John McCallfc207f22013-03-07 21:37:12 +0000710 break;
711
712 case AtomicExpr::AO__atomic_exchange:
John McCall7f416cc2015-09-08 08:05:57 +0000713 Val1 = EmitPointerWithAlignment(E->getVal1());
714 Dest = EmitPointerWithAlignment(E->getVal2());
John McCallfc207f22013-03-07 21:37:12 +0000715 break;
716
717 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
718 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
719 case AtomicExpr::AO__atomic_compare_exchange_n:
720 case AtomicExpr::AO__atomic_compare_exchange:
John McCall7f416cc2015-09-08 08:05:57 +0000721 Val1 = EmitPointerWithAlignment(E->getVal1());
John McCallfc207f22013-03-07 21:37:12 +0000722 if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange)
John McCall7f416cc2015-09-08 08:05:57 +0000723 Val2 = EmitPointerWithAlignment(E->getVal2());
John McCallfc207f22013-03-07 21:37:12 +0000724 else
725 Val2 = EmitValToTemp(*this, E->getVal2());
726 OrderFail = EmitScalarExpr(E->getOrderFail());
John McCallfc207f22013-03-07 21:37:12 +0000727 if (E->getNumSubExprs() == 6)
Tim Northovercadbbe12014-06-13 19:43:04 +0000728 IsWeak = EmitScalarExpr(E->getWeak());
John McCallfc207f22013-03-07 21:37:12 +0000729 break;
730
731 case AtomicExpr::AO__c11_atomic_fetch_add:
732 case AtomicExpr::AO__c11_atomic_fetch_sub:
733 if (MemTy->isPointerType()) {
734 // For pointer arithmetic, we're required to do a bit of math:
735 // adding 1 to an int* is not the same as adding 1 to a uintptr_t.
736 // ... but only for the C11 builtins. The GNU builtins expect the
737 // user to multiply by sizeof(T).
738 QualType Val1Ty = E->getVal1()->getType();
739 llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1());
740 CharUnits PointeeIncAmt =
741 getContext().getTypeSizeInChars(MemTy->getPointeeType());
742 Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt));
John McCall7f416cc2015-09-08 08:05:57 +0000743 auto Temp = CreateMemTemp(Val1Ty, ".atomictmp");
744 Val1 = Temp;
745 EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Temp, Val1Ty));
John McCallfc207f22013-03-07 21:37:12 +0000746 break;
747 }
748 // Fall through.
749 case AtomicExpr::AO__atomic_fetch_add:
750 case AtomicExpr::AO__atomic_fetch_sub:
751 case AtomicExpr::AO__atomic_add_fetch:
752 case AtomicExpr::AO__atomic_sub_fetch:
753 case AtomicExpr::AO__c11_atomic_store:
754 case AtomicExpr::AO__c11_atomic_exchange:
755 case AtomicExpr::AO__atomic_store_n:
756 case AtomicExpr::AO__atomic_exchange_n:
757 case AtomicExpr::AO__c11_atomic_fetch_and:
758 case AtomicExpr::AO__c11_atomic_fetch_or:
759 case AtomicExpr::AO__c11_atomic_fetch_xor:
760 case AtomicExpr::AO__atomic_fetch_and:
761 case AtomicExpr::AO__atomic_fetch_or:
762 case AtomicExpr::AO__atomic_fetch_xor:
763 case AtomicExpr::AO__atomic_fetch_nand:
764 case AtomicExpr::AO__atomic_and_fetch:
765 case AtomicExpr::AO__atomic_or_fetch:
766 case AtomicExpr::AO__atomic_xor_fetch:
767 case AtomicExpr::AO__atomic_nand_fetch:
768 Val1 = EmitValToTemp(*this, E->getVal1());
769 break;
770 }
771
David Majnemeree8d04d2014-12-12 08:16:09 +0000772 QualType RValTy = E->getType().getUnqualifiedType();
773
David Majnemer659be552014-11-25 23:44:32 +0000774 auto GetDest = [&] {
John McCall7f416cc2015-09-08 08:05:57 +0000775 if (!RValTy->isVoidType() && !Dest.isValid()) {
David Majnemeree8d04d2014-12-12 08:16:09 +0000776 Dest = CreateMemTemp(RValTy, ".atomicdst");
777 }
David Majnemer659be552014-11-25 23:44:32 +0000778 return Dest;
779 };
John McCallfc207f22013-03-07 21:37:12 +0000780
781 // Use a library call. See: http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary .
782 if (UseLibcall) {
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000783 bool UseOptimizedLibcall = false;
784 switch (E->getOp()) {
James Y Knight81167fb2015-08-05 16:57:36 +0000785 case AtomicExpr::AO__c11_atomic_init:
786 llvm_unreachable("Already handled above with EmitAtomicInit!");
787
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000788 case AtomicExpr::AO__c11_atomic_fetch_add:
789 case AtomicExpr::AO__atomic_fetch_add:
790 case AtomicExpr::AO__c11_atomic_fetch_and:
791 case AtomicExpr::AO__atomic_fetch_and:
792 case AtomicExpr::AO__c11_atomic_fetch_or:
793 case AtomicExpr::AO__atomic_fetch_or:
James Y Knight81167fb2015-08-05 16:57:36 +0000794 case AtomicExpr::AO__atomic_fetch_nand:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000795 case AtomicExpr::AO__c11_atomic_fetch_sub:
796 case AtomicExpr::AO__atomic_fetch_sub:
797 case AtomicExpr::AO__c11_atomic_fetch_xor:
798 case AtomicExpr::AO__atomic_fetch_xor:
James Y Knight81167fb2015-08-05 16:57:36 +0000799 case AtomicExpr::AO__atomic_add_fetch:
800 case AtomicExpr::AO__atomic_and_fetch:
801 case AtomicExpr::AO__atomic_nand_fetch:
802 case AtomicExpr::AO__atomic_or_fetch:
803 case AtomicExpr::AO__atomic_sub_fetch:
804 case AtomicExpr::AO__atomic_xor_fetch:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000805 // For these, only library calls for certain sizes exist.
806 UseOptimizedLibcall = true;
807 break;
James Y Knight81167fb2015-08-05 16:57:36 +0000808
809 case AtomicExpr::AO__c11_atomic_load:
810 case AtomicExpr::AO__c11_atomic_store:
811 case AtomicExpr::AO__c11_atomic_exchange:
812 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
813 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
814 case AtomicExpr::AO__atomic_load_n:
815 case AtomicExpr::AO__atomic_load:
816 case AtomicExpr::AO__atomic_store_n:
817 case AtomicExpr::AO__atomic_store:
818 case AtomicExpr::AO__atomic_exchange_n:
819 case AtomicExpr::AO__atomic_exchange:
820 case AtomicExpr::AO__atomic_compare_exchange_n:
821 case AtomicExpr::AO__atomic_compare_exchange:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000822 // Only use optimized library calls for sizes for which they exist.
823 if (Size == 1 || Size == 2 || Size == 4 || Size == 8)
824 UseOptimizedLibcall = true;
825 break;
826 }
John McCallfc207f22013-03-07 21:37:12 +0000827
John McCallfc207f22013-03-07 21:37:12 +0000828 CallArgList Args;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000829 if (!UseOptimizedLibcall) {
830 // For non-optimized library calls, the size is the first parameter
831 Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)),
832 getContext().getSizeType());
833 }
834 // Atomic address is the first or second parameter
John McCall7f416cc2015-09-08 08:05:57 +0000835 Args.add(RValue::get(EmitCastToVoidPtr(Ptr.getPointer())),
836 getContext().VoidPtrTy);
John McCallfc207f22013-03-07 21:37:12 +0000837
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000838 std::string LibCallName;
Logan Chien74798a32014-03-26 17:35:01 +0000839 QualType LoweredMemTy =
840 MemTy->isPointerType() ? getContext().getIntPtrType() : MemTy;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000841 QualType RetTy;
842 bool HaveRetTy = false;
John McCallfc207f22013-03-07 21:37:12 +0000843 switch (E->getOp()) {
James Y Knight81167fb2015-08-05 16:57:36 +0000844 case AtomicExpr::AO__c11_atomic_init:
845 llvm_unreachable("Already handled!");
846
John McCallfc207f22013-03-07 21:37:12 +0000847 // There is only one libcall for compare an exchange, because there is no
848 // optimisation benefit possible from a libcall version of a weak compare
849 // and exchange.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000850 // bool __atomic_compare_exchange(size_t size, void *mem, void *expected,
John McCallfc207f22013-03-07 21:37:12 +0000851 // void *desired, int success, int failure)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000852 // bool __atomic_compare_exchange_N(T *mem, T *expected, T desired,
853 // int success, int failure)
John McCallfc207f22013-03-07 21:37:12 +0000854 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
855 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
856 case AtomicExpr::AO__atomic_compare_exchange:
857 case AtomicExpr::AO__atomic_compare_exchange_n:
858 LibCallName = "__atomic_compare_exchange";
859 RetTy = getContext().BoolTy;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000860 HaveRetTy = true;
John McCall7f416cc2015-09-08 08:05:57 +0000861 Args.add(RValue::get(EmitCastToVoidPtr(Val1.getPointer())),
862 getContext().VoidPtrTy);
863 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val2.getPointer(),
864 MemTy, E->getExprLoc(), sizeChars);
Nick Lewycky5fa40c32013-10-01 21:51:38 +0000865 Args.add(RValue::get(Order), getContext().IntTy);
John McCallfc207f22013-03-07 21:37:12 +0000866 Order = OrderFail;
867 break;
868 // void __atomic_exchange(size_t size, void *mem, void *val, void *return,
869 // int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000870 // T __atomic_exchange_N(T *mem, T val, int order)
John McCallfc207f22013-03-07 21:37:12 +0000871 case AtomicExpr::AO__c11_atomic_exchange:
872 case AtomicExpr::AO__atomic_exchange_n:
873 case AtomicExpr::AO__atomic_exchange:
874 LibCallName = "__atomic_exchange";
John McCall7f416cc2015-09-08 08:05:57 +0000875 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
876 MemTy, E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000877 break;
878 // void __atomic_store(size_t size, void *mem, void *val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000879 // void __atomic_store_N(T *mem, T val, int order)
John McCallfc207f22013-03-07 21:37:12 +0000880 case AtomicExpr::AO__c11_atomic_store:
881 case AtomicExpr::AO__atomic_store:
882 case AtomicExpr::AO__atomic_store_n:
883 LibCallName = "__atomic_store";
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000884 RetTy = getContext().VoidTy;
885 HaveRetTy = true;
John McCall7f416cc2015-09-08 08:05:57 +0000886 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
887 MemTy, E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000888 break;
889 // void __atomic_load(size_t size, void *mem, void *return, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000890 // T __atomic_load_N(T *mem, int order)
John McCallfc207f22013-03-07 21:37:12 +0000891 case AtomicExpr::AO__c11_atomic_load:
892 case AtomicExpr::AO__atomic_load:
893 case AtomicExpr::AO__atomic_load_n:
894 LibCallName = "__atomic_load";
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000895 break;
896 // T __atomic_fetch_add_N(T *mem, T val, int order)
897 case AtomicExpr::AO__c11_atomic_fetch_add:
898 case AtomicExpr::AO__atomic_fetch_add:
899 LibCallName = "__atomic_fetch_add";
John McCall7f416cc2015-09-08 08:05:57 +0000900 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
901 LoweredMemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000902 break;
903 // T __atomic_fetch_and_N(T *mem, T val, int order)
904 case AtomicExpr::AO__c11_atomic_fetch_and:
905 case AtomicExpr::AO__atomic_fetch_and:
906 LibCallName = "__atomic_fetch_and";
John McCall7f416cc2015-09-08 08:05:57 +0000907 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
908 MemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000909 break;
910 // T __atomic_fetch_or_N(T *mem, T val, int order)
911 case AtomicExpr::AO__c11_atomic_fetch_or:
912 case AtomicExpr::AO__atomic_fetch_or:
913 LibCallName = "__atomic_fetch_or";
John McCall7f416cc2015-09-08 08:05:57 +0000914 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
915 MemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000916 break;
917 // T __atomic_fetch_sub_N(T *mem, T val, int order)
918 case AtomicExpr::AO__c11_atomic_fetch_sub:
919 case AtomicExpr::AO__atomic_fetch_sub:
920 LibCallName = "__atomic_fetch_sub";
John McCall7f416cc2015-09-08 08:05:57 +0000921 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
922 LoweredMemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000923 break;
924 // T __atomic_fetch_xor_N(T *mem, T val, int order)
925 case AtomicExpr::AO__c11_atomic_fetch_xor:
926 case AtomicExpr::AO__atomic_fetch_xor:
927 LibCallName = "__atomic_fetch_xor";
John McCall7f416cc2015-09-08 08:05:57 +0000928 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
929 MemTy, E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000930 break;
James Y Knight81167fb2015-08-05 16:57:36 +0000931 // T __atomic_fetch_nand_N(T *mem, T val, int order)
932 case AtomicExpr::AO__atomic_fetch_nand:
933 LibCallName = "__atomic_fetch_nand";
John McCall7f416cc2015-09-08 08:05:57 +0000934 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
935 MemTy, E->getExprLoc(), sizeChars);
James Y Knight81167fb2015-08-05 16:57:36 +0000936 break;
937
938 // T __atomic_add_fetch_N(T *mem, T val, int order)
939 case AtomicExpr::AO__atomic_add_fetch:
940 LibCallName = "__atomic_add_fetch";
John McCall7f416cc2015-09-08 08:05:57 +0000941 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
942 LoweredMemTy, E->getExprLoc(), sizeChars);
James Y Knight81167fb2015-08-05 16:57:36 +0000943 break;
944 // T __atomic_and_fetch_N(T *mem, T val, int order)
945 case AtomicExpr::AO__atomic_and_fetch:
946 LibCallName = "__atomic_and_fetch";
John McCall7f416cc2015-09-08 08:05:57 +0000947 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
948 MemTy, E->getExprLoc(), sizeChars);
James Y Knight81167fb2015-08-05 16:57:36 +0000949 break;
950 // T __atomic_or_fetch_N(T *mem, T val, int order)
951 case AtomicExpr::AO__atomic_or_fetch:
952 LibCallName = "__atomic_or_fetch";
John McCall7f416cc2015-09-08 08:05:57 +0000953 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
954 MemTy, E->getExprLoc(), sizeChars);
James Y Knight81167fb2015-08-05 16:57:36 +0000955 break;
956 // T __atomic_sub_fetch_N(T *mem, T val, int order)
957 case AtomicExpr::AO__atomic_sub_fetch:
958 LibCallName = "__atomic_sub_fetch";
John McCall7f416cc2015-09-08 08:05:57 +0000959 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
960 LoweredMemTy, E->getExprLoc(), sizeChars);
James Y Knight81167fb2015-08-05 16:57:36 +0000961 break;
962 // T __atomic_xor_fetch_N(T *mem, T val, int order)
963 case AtomicExpr::AO__atomic_xor_fetch:
964 LibCallName = "__atomic_xor_fetch";
John McCall7f416cc2015-09-08 08:05:57 +0000965 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
966 MemTy, E->getExprLoc(), sizeChars);
James Y Knight81167fb2015-08-05 16:57:36 +0000967 break;
968 // T __atomic_nand_fetch_N(T *mem, T val, int order)
969 case AtomicExpr::AO__atomic_nand_fetch:
970 LibCallName = "__atomic_nand_fetch";
John McCall7f416cc2015-09-08 08:05:57 +0000971 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
972 MemTy, E->getExprLoc(), sizeChars);
James Y Knight81167fb2015-08-05 16:57:36 +0000973 break;
John McCallfc207f22013-03-07 21:37:12 +0000974 }
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000975
976 // Optimized functions have the size in their name.
977 if (UseOptimizedLibcall)
978 LibCallName += "_" + llvm::utostr(Size);
979 // By default, assume we return a value of the atomic type.
980 if (!HaveRetTy) {
981 if (UseOptimizedLibcall) {
982 // Value is returned directly.
David Majnemer0392cf82014-08-29 07:27:49 +0000983 // The function returns an appropriately sized integer type.
984 RetTy = getContext().getIntTypeForBitwidth(
985 getContext().toBits(sizeChars), /*Signed=*/false);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000986 } else {
987 // Value is returned through parameter before the order.
988 RetTy = getContext().VoidTy;
John McCall7f416cc2015-09-08 08:05:57 +0000989 Args.add(RValue::get(EmitCastToVoidPtr(Dest.getPointer())),
990 getContext().VoidPtrTy);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000991 }
992 }
John McCallfc207f22013-03-07 21:37:12 +0000993 // order is always the last parameter
994 Args.add(RValue::get(Order),
995 getContext().IntTy);
996
David Majnemer659be552014-11-25 23:44:32 +0000997 RValue Res = emitAtomicLibcall(*this, LibCallName, RetTy, Args);
998 // The value is returned directly from the libcall.
999 if (HaveRetTy && !RetTy->isVoidType())
1000 return Res;
1001 // The value is returned via an explicit out param.
1002 if (RetTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +00001003 return RValue::get(nullptr);
David Majnemer659be552014-11-25 23:44:32 +00001004 // The value is returned directly for optimized libcalls but the caller is
1005 // expected an out-param.
1006 if (UseOptimizedLibcall) {
1007 llvm::Value *ResVal = Res.getScalarVal();
John McCall7f416cc2015-09-08 08:05:57 +00001008 Builder.CreateStore(ResVal,
David Majnemer659be552014-11-25 23:44:32 +00001009 Builder.CreateBitCast(GetDest(), ResVal->getType()->getPointerTo()));
David Majnemer659be552014-11-25 23:44:32 +00001010 }
David Majnemeree8d04d2014-12-12 08:16:09 +00001011 return convertTempToRValue(Dest, RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001012 }
1013
1014 bool IsStore = E->getOp() == AtomicExpr::AO__c11_atomic_store ||
1015 E->getOp() == AtomicExpr::AO__atomic_store ||
1016 E->getOp() == AtomicExpr::AO__atomic_store_n;
1017 bool IsLoad = E->getOp() == AtomicExpr::AO__c11_atomic_load ||
1018 E->getOp() == AtomicExpr::AO__atomic_load ||
1019 E->getOp() == AtomicExpr::AO__atomic_load_n;
1020
David Majnemerd8cd8f72014-11-22 10:44:12 +00001021 llvm::Type *ITy =
1022 llvm::IntegerType::get(getLLVMContext(), Size * 8);
John McCall7f416cc2015-09-08 08:05:57 +00001023 Address OrigDest = GetDest();
David Majnemerd8cd8f72014-11-22 10:44:12 +00001024 Ptr = Builder.CreateBitCast(
John McCall7f416cc2015-09-08 08:05:57 +00001025 Ptr, ITy->getPointerTo(Ptr.getType()->getPointerAddressSpace()));
1026 if (Val1.isValid()) Val1 = Builder.CreateBitCast(Val1, ITy->getPointerTo());
1027 if (Val2.isValid()) Val2 = Builder.CreateBitCast(Val2, ITy->getPointerTo());
1028 if (Dest.isValid() && !E->isCmpXChg())
David Majnemerd8cd8f72014-11-22 10:44:12 +00001029 Dest = Builder.CreateBitCast(Dest, ITy->getPointerTo());
John McCallfc207f22013-03-07 21:37:12 +00001030
1031 if (isa<llvm::ConstantInt>(Order)) {
1032 int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
1033 switch (ord) {
Tim Northovere94a34c2014-03-11 10:49:14 +00001034 case AtomicExpr::AO_ABI_memory_order_relaxed:
Tim Northovercadbbe12014-06-13 19:43:04 +00001035 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001036 Size, llvm::Monotonic);
John McCallfc207f22013-03-07 21:37:12 +00001037 break;
Tim Northovere94a34c2014-03-11 10:49:14 +00001038 case AtomicExpr::AO_ABI_memory_order_consume:
1039 case AtomicExpr::AO_ABI_memory_order_acquire:
John McCallfc207f22013-03-07 21:37:12 +00001040 if (IsStore)
1041 break; // Avoid crashing on code with undefined behavior
Tim Northovercadbbe12014-06-13 19:43:04 +00001042 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001043 Size, llvm::Acquire);
John McCallfc207f22013-03-07 21:37:12 +00001044 break;
Tim Northovere94a34c2014-03-11 10:49:14 +00001045 case AtomicExpr::AO_ABI_memory_order_release:
John McCallfc207f22013-03-07 21:37:12 +00001046 if (IsLoad)
1047 break; // Avoid crashing on code with undefined behavior
Tim Northovercadbbe12014-06-13 19:43:04 +00001048 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001049 Size, llvm::Release);
John McCallfc207f22013-03-07 21:37:12 +00001050 break;
Tim Northovere94a34c2014-03-11 10:49:14 +00001051 case AtomicExpr::AO_ABI_memory_order_acq_rel:
John McCallfc207f22013-03-07 21:37:12 +00001052 if (IsLoad || IsStore)
1053 break; // Avoid crashing on code with undefined behavior
Tim Northovercadbbe12014-06-13 19:43:04 +00001054 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001055 Size, llvm::AcquireRelease);
John McCallfc207f22013-03-07 21:37:12 +00001056 break;
Tim Northovere94a34c2014-03-11 10:49:14 +00001057 case AtomicExpr::AO_ABI_memory_order_seq_cst:
Tim Northovercadbbe12014-06-13 19:43:04 +00001058 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001059 Size, llvm::SequentiallyConsistent);
John McCallfc207f22013-03-07 21:37:12 +00001060 break;
1061 default: // invalid order
1062 // We should not ever get here normally, but it's hard to
1063 // enforce that in general.
1064 break;
1065 }
David Majnemeree8d04d2014-12-12 08:16:09 +00001066 if (RValTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +00001067 return RValue::get(nullptr);
David Majnemeree8d04d2014-12-12 08:16:09 +00001068 return convertTempToRValue(OrigDest, RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001069 }
1070
1071 // Long case, when Order isn't obviously constant.
1072
1073 // Create all the relevant BB's
Craig Topper8a13c412014-05-21 05:09:00 +00001074 llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
1075 *ReleaseBB = nullptr, *AcqRelBB = nullptr,
1076 *SeqCstBB = nullptr;
John McCallfc207f22013-03-07 21:37:12 +00001077 MonotonicBB = createBasicBlock("monotonic", CurFn);
1078 if (!IsStore)
1079 AcquireBB = createBasicBlock("acquire", CurFn);
1080 if (!IsLoad)
1081 ReleaseBB = createBasicBlock("release", CurFn);
1082 if (!IsLoad && !IsStore)
1083 AcqRelBB = createBasicBlock("acqrel", CurFn);
1084 SeqCstBB = createBasicBlock("seqcst", CurFn);
1085 llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
1086
1087 // Create the switch for the split
1088 // MonotonicBB is arbitrarily chosen as the default case; in practice, this
1089 // doesn't matter unless someone is crazy enough to use something that
1090 // doesn't fold to a constant for the ordering.
1091 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
1092 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB);
1093
1094 // Emit all the different atomics
1095 Builder.SetInsertPoint(MonotonicBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001096 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001097 Size, llvm::Monotonic);
John McCallfc207f22013-03-07 21:37:12 +00001098 Builder.CreateBr(ContBB);
1099 if (!IsStore) {
1100 Builder.SetInsertPoint(AcquireBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001101 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001102 Size, llvm::Acquire);
John McCallfc207f22013-03-07 21:37:12 +00001103 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001104 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_consume),
1105 AcquireBB);
1106 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acquire),
1107 AcquireBB);
John McCallfc207f22013-03-07 21:37:12 +00001108 }
1109 if (!IsLoad) {
1110 Builder.SetInsertPoint(ReleaseBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001111 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001112 Size, llvm::Release);
John McCallfc207f22013-03-07 21:37:12 +00001113 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001114 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_release),
1115 ReleaseBB);
John McCallfc207f22013-03-07 21:37:12 +00001116 }
1117 if (!IsLoad && !IsStore) {
1118 Builder.SetInsertPoint(AcqRelBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001119 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001120 Size, llvm::AcquireRelease);
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_acq_rel),
1123 AcqRelBB);
John McCallfc207f22013-03-07 21:37:12 +00001124 }
1125 Builder.SetInsertPoint(SeqCstBB);
Tim Northovercadbbe12014-06-13 19:43:04 +00001126 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
John McCall7f416cc2015-09-08 08:05:57 +00001127 Size, llvm::SequentiallyConsistent);
John McCallfc207f22013-03-07 21:37:12 +00001128 Builder.CreateBr(ContBB);
Tim Northover514fc612014-03-13 19:25:52 +00001129 SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_seq_cst),
1130 SeqCstBB);
John McCallfc207f22013-03-07 21:37:12 +00001131
1132 // Cleanup and return
1133 Builder.SetInsertPoint(ContBB);
David Majnemeree8d04d2014-12-12 08:16:09 +00001134 if (RValTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +00001135 return RValue::get(nullptr);
David Majnemeree8d04d2014-12-12 08:16:09 +00001136 return convertTempToRValue(OrigDest, RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001137}
John McCalla8ec7eb2013-03-07 21:37:17 +00001138
John McCall7f416cc2015-09-08 08:05:57 +00001139Address AtomicInfo::emitCastToAtomicIntPointer(Address addr) const {
John McCalla8ec7eb2013-03-07 21:37:17 +00001140 unsigned addrspace =
John McCall7f416cc2015-09-08 08:05:57 +00001141 cast<llvm::PointerType>(addr.getPointer()->getType())->getAddressSpace();
John McCalla8ec7eb2013-03-07 21:37:17 +00001142 llvm::IntegerType *ty =
1143 llvm::IntegerType::get(CGF.getLLVMContext(), AtomicSizeInBits);
1144 return CGF.Builder.CreateBitCast(addr, ty->getPointerTo(addrspace));
1145}
1146
John McCall7f416cc2015-09-08 08:05:57 +00001147RValue AtomicInfo::convertAtomicTempToRValue(Address addr,
1148 AggValueSlot resultSlot,
1149 SourceLocation loc,
1150 bool asValue) const {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001151 if (LVal.isSimple()) {
1152 if (EvaluationKind == TEK_Aggregate)
1153 return resultSlot.asRValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001154
Alexey Bataevb57056f2015-01-22 06:17:56 +00001155 // Drill into the padding structure if we have one.
1156 if (hasPadding())
John McCall7f416cc2015-09-08 08:05:57 +00001157 addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
John McCalla8ec7eb2013-03-07 21:37:17 +00001158
Alexey Bataevb57056f2015-01-22 06:17:56 +00001159 // Otherwise, just convert the temporary to an r-value using the
1160 // normal conversion routine.
1161 return CGF.convertTempToRValue(addr, getValueType(), loc);
David Blaikie1ed728c2015-04-05 22:45:47 +00001162 }
John McCall7f416cc2015-09-08 08:05:57 +00001163 if (!asValue)
Alexey Bataevb8329262015-02-27 06:33:30 +00001164 // Get RValue from temp memory as atomic for non-simple lvalues
John McCall7f416cc2015-09-08 08:05:57 +00001165 return RValue::get(CGF.Builder.CreateLoad(addr));
David Blaikie1ed728c2015-04-05 22:45:47 +00001166 if (LVal.isBitField())
John McCall7f416cc2015-09-08 08:05:57 +00001167 return CGF.EmitLoadOfBitfieldLValue(
1168 LValue::MakeBitfield(addr, LVal.getBitFieldInfo(), LVal.getType(),
1169 LVal.getAlignmentSource()));
David Blaikie1ed728c2015-04-05 22:45:47 +00001170 if (LVal.isVectorElt())
John McCall7f416cc2015-09-08 08:05:57 +00001171 return CGF.EmitLoadOfLValue(
1172 LValue::MakeVectorElt(addr, LVal.getVectorIdx(), LVal.getType(),
1173 LVal.getAlignmentSource()), loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001174 assert(LVal.isExtVectorElt());
1175 return CGF.EmitLoadOfExtVectorElementLValue(LValue::MakeExtVectorElt(
John McCall7f416cc2015-09-08 08:05:57 +00001176 addr, LVal.getExtVectorElts(), LVal.getType(),
1177 LVal.getAlignmentSource()));
John McCalla8ec7eb2013-03-07 21:37:17 +00001178}
1179
Alexey Bataevb8329262015-02-27 06:33:30 +00001180RValue AtomicInfo::ConvertIntToValueOrAtomic(llvm::Value *IntVal,
1181 AggValueSlot ResultSlot,
1182 SourceLocation Loc,
1183 bool AsValue) const {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001184 // Try not to in some easy cases.
1185 assert(IntVal->getType()->isIntegerTy() && "Expected integer value");
Alexey Bataevb8329262015-02-27 06:33:30 +00001186 if (getEvaluationKind() == TEK_Scalar &&
1187 (((!LVal.isBitField() ||
1188 LVal.getBitFieldInfo().Size == ValueSizeInBits) &&
1189 !hasPadding()) ||
1190 !AsValue)) {
1191 auto *ValTy = AsValue
1192 ? CGF.ConvertTypeForMem(ValueTy)
John McCall7f416cc2015-09-08 08:05:57 +00001193 : getAtomicAddress().getType()->getPointerElementType();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001194 if (ValTy->isIntegerTy()) {
1195 assert(IntVal->getType() == ValTy && "Different integer types.");
David Majnemereeaec262015-02-14 02:18:14 +00001196 return RValue::get(CGF.EmitFromMemory(IntVal, ValueTy));
Alexey Bataev452d8e12014-12-15 05:25:25 +00001197 } else if (ValTy->isPointerTy())
1198 return RValue::get(CGF.Builder.CreateIntToPtr(IntVal, ValTy));
1199 else if (llvm::CastInst::isBitCastable(IntVal->getType(), ValTy))
1200 return RValue::get(CGF.Builder.CreateBitCast(IntVal, ValTy));
1201 }
1202
1203 // Create a temporary. This needs to be big enough to hold the
1204 // atomic integer.
John McCall7f416cc2015-09-08 08:05:57 +00001205 Address Temp = Address::invalid();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001206 bool TempIsVolatile = false;
Alexey Bataevb8329262015-02-27 06:33:30 +00001207 if (AsValue && getEvaluationKind() == TEK_Aggregate) {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001208 assert(!ResultSlot.isIgnored());
John McCall7f416cc2015-09-08 08:05:57 +00001209 Temp = ResultSlot.getAddress();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001210 TempIsVolatile = ResultSlot.isVolatile();
1211 } else {
Alexey Bataevb8329262015-02-27 06:33:30 +00001212 Temp = CreateTempAlloca();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001213 }
1214
1215 // Slam the integer into the temporary.
John McCall7f416cc2015-09-08 08:05:57 +00001216 Address CastTemp = emitCastToAtomicIntPointer(Temp);
1217 CGF.Builder.CreateStore(IntVal, CastTemp)
Alexey Bataev452d8e12014-12-15 05:25:25 +00001218 ->setVolatile(TempIsVolatile);
1219
John McCall7f416cc2015-09-08 08:05:57 +00001220 return convertAtomicTempToRValue(Temp, ResultSlot, Loc, AsValue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001221}
1222
1223void AtomicInfo::EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
1224 llvm::AtomicOrdering AO, bool) {
1225 // void __atomic_load(size_t size, void *mem, void *return, int order);
1226 CallArgList Args;
1227 Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
John McCall7f416cc2015-09-08 08:05:57 +00001228 Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
Alexey Bataevb8329262015-02-27 06:33:30 +00001229 CGF.getContext().VoidPtrTy);
1230 Args.add(RValue::get(CGF.EmitCastToVoidPtr(AddForLoaded)),
1231 CGF.getContext().VoidPtrTy);
1232 Args.add(RValue::get(
1233 llvm::ConstantInt::get(CGF.IntTy, translateAtomicOrdering(AO))),
1234 CGF.getContext().IntTy);
1235 emitAtomicLibcall(CGF, "__atomic_load", CGF.getContext().VoidTy, Args);
1236}
1237
1238llvm::Value *AtomicInfo::EmitAtomicLoadOp(llvm::AtomicOrdering AO,
1239 bool IsVolatile) {
1240 // Okay, we're doing this natively.
John McCall7f416cc2015-09-08 08:05:57 +00001241 Address Addr = getAtomicAddressAsAtomicIntPointer();
Alexey Bataevb8329262015-02-27 06:33:30 +00001242 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Addr, "atomic-load");
1243 Load->setAtomic(AO);
1244
1245 // Other decoration.
Alexey Bataevb8329262015-02-27 06:33:30 +00001246 if (IsVolatile)
1247 Load->setVolatile(true);
1248 if (LVal.getTBAAInfo())
1249 CGF.CGM.DecorateInstruction(Load, LVal.getTBAAInfo());
1250 return Load;
Alexey Bataev452d8e12014-12-15 05:25:25 +00001251}
1252
David Majnemera5b195a2015-02-14 01:35:12 +00001253/// An LValue is a candidate for having its loads and stores be made atomic if
1254/// we are operating under /volatile:ms *and* the LValue itself is volatile and
1255/// performing such an operation can be performed without a libcall.
1256bool CodeGenFunction::LValueIsSuitableForInlineAtomic(LValue LV) {
John McCall7f416cc2015-09-08 08:05:57 +00001257 if (!CGM.getCodeGenOpts().MSVolatile) return false;
David Majnemera5b195a2015-02-14 01:35:12 +00001258 AtomicInfo AI(*this, LV);
1259 bool IsVolatile = LV.isVolatile() || hasVolatileMember(LV.getType());
1260 // An atomic is inline if we don't need to use a libcall.
1261 bool AtomicIsInline = !AI.shouldUseLibcall();
John McCall7f416cc2015-09-08 08:05:57 +00001262 return IsVolatile && AtomicIsInline;
David Majnemera5b195a2015-02-14 01:35:12 +00001263}
1264
1265/// An type is a candidate for having its loads and stores be made atomic if
1266/// we are operating under /volatile:ms *and* we know the access is volatile and
1267/// performing such an operation can be performed without a libcall.
1268bool CodeGenFunction::typeIsSuitableForInlineAtomic(QualType Ty,
1269 bool IsVolatile) const {
1270 // An atomic is inline if we don't need to use a libcall (e.g. it is builtin).
1271 bool AtomicIsInline = getContext().getTargetInfo().hasBuiltinAtomic(
1272 getContext().getTypeSize(Ty), getContext().getTypeAlign(Ty));
1273 return CGM.getCodeGenOpts().MSVolatile && IsVolatile && AtomicIsInline;
1274}
1275
1276RValue CodeGenFunction::EmitAtomicLoad(LValue LV, SourceLocation SL,
1277 AggValueSlot Slot) {
1278 llvm::AtomicOrdering AO;
1279 bool IsVolatile = LV.isVolatileQualified();
1280 if (LV.getType()->isAtomicType()) {
1281 AO = llvm::SequentiallyConsistent;
1282 } else {
1283 AO = llvm::Acquire;
1284 IsVolatile = true;
1285 }
1286 return EmitAtomicLoad(LV, SL, AO, IsVolatile, Slot);
1287}
1288
Alexey Bataevb8329262015-02-27 06:33:30 +00001289RValue AtomicInfo::EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
1290 bool AsValue, llvm::AtomicOrdering AO,
1291 bool IsVolatile) {
1292 // Check whether we should use a library call.
1293 if (shouldUseLibcall()) {
John McCall7f416cc2015-09-08 08:05:57 +00001294 Address TempAddr = Address::invalid();
Alexey Bataevb8329262015-02-27 06:33:30 +00001295 if (LVal.isSimple() && !ResultSlot.isIgnored()) {
1296 assert(getEvaluationKind() == TEK_Aggregate);
John McCall7f416cc2015-09-08 08:05:57 +00001297 TempAddr = ResultSlot.getAddress();
Alexey Bataevb8329262015-02-27 06:33:30 +00001298 } else
1299 TempAddr = CreateTempAlloca();
1300
John McCall7f416cc2015-09-08 08:05:57 +00001301 EmitAtomicLoadLibcall(TempAddr.getPointer(), AO, IsVolatile);
Alexey Bataevb8329262015-02-27 06:33:30 +00001302
1303 // Okay, turn that back into the original value or whole atomic (for
1304 // non-simple lvalues) type.
John McCall7f416cc2015-09-08 08:05:57 +00001305 return convertAtomicTempToRValue(TempAddr, ResultSlot, Loc, AsValue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001306 }
1307
1308 // Okay, we're doing this natively.
1309 auto *Load = EmitAtomicLoadOp(AO, IsVolatile);
1310
1311 // If we're ignoring an aggregate return, don't do anything.
1312 if (getEvaluationKind() == TEK_Aggregate && ResultSlot.isIgnored())
John McCall7f416cc2015-09-08 08:05:57 +00001313 return RValue::getAggregate(Address::invalid(), false);
Alexey Bataevb8329262015-02-27 06:33:30 +00001314
1315 // Okay, turn that back into the original value or atomic (for non-simple
1316 // lvalues) type.
1317 return ConvertIntToValueOrAtomic(Load, ResultSlot, Loc, AsValue);
1318}
1319
John McCalla8ec7eb2013-03-07 21:37:17 +00001320/// Emit a load from an l-value of atomic type. Note that the r-value
1321/// we produce is an r-value of the atomic *value* type.
Nick Lewycky2d84e842013-10-02 02:29:49 +00001322RValue CodeGenFunction::EmitAtomicLoad(LValue src, SourceLocation loc,
David Majnemera5b195a2015-02-14 01:35:12 +00001323 llvm::AtomicOrdering AO, bool IsVolatile,
Nick Lewycky2d84e842013-10-02 02:29:49 +00001324 AggValueSlot resultSlot) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001325 AtomicInfo Atomics(*this, src);
1326 return Atomics.EmitAtomicLoad(resultSlot, loc, /*AsValue=*/true, AO,
1327 IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +00001328}
1329
John McCalla8ec7eb2013-03-07 21:37:17 +00001330/// Copy an r-value into memory as part of storing to an atomic type.
1331/// This needs to create a bit-pattern suitable for atomic operations.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001332void AtomicInfo::emitCopyIntoMemory(RValue rvalue) const {
1333 assert(LVal.isSimple());
John McCalla8ec7eb2013-03-07 21:37:17 +00001334 // If we have an r-value, the rvalue should be of the atomic type,
1335 // which means that the caller is responsible for having zeroed
1336 // any padding. Just do an aggregate copy of that type.
1337 if (rvalue.isAggregate()) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001338 CGF.EmitAggregateCopy(getAtomicAddress(),
John McCall7f416cc2015-09-08 08:05:57 +00001339 rvalue.getAggregateAddress(),
John McCalla8ec7eb2013-03-07 21:37:17 +00001340 getAtomicType(),
1341 (rvalue.isVolatileQualified()
John McCall7f416cc2015-09-08 08:05:57 +00001342 || LVal.isVolatileQualified()));
John McCalla8ec7eb2013-03-07 21:37:17 +00001343 return;
1344 }
1345
1346 // Okay, otherwise we're copying stuff.
1347
1348 // Zero out the buffer if necessary.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001349 emitMemSetZeroIfNecessary();
John McCalla8ec7eb2013-03-07 21:37:17 +00001350
1351 // Drill past the padding if present.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001352 LValue TempLVal = projectValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001353
1354 // Okay, store the rvalue in.
1355 if (rvalue.isScalar()) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001356 CGF.EmitStoreOfScalar(rvalue.getScalarVal(), TempLVal, /*init*/ true);
John McCalla8ec7eb2013-03-07 21:37:17 +00001357 } else {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001358 CGF.EmitStoreOfComplex(rvalue.getComplexVal(), TempLVal, /*init*/ true);
John McCalla8ec7eb2013-03-07 21:37:17 +00001359 }
1360}
1361
1362
1363/// Materialize an r-value into memory for the purposes of storing it
1364/// to an atomic type.
John McCall7f416cc2015-09-08 08:05:57 +00001365Address AtomicInfo::materializeRValue(RValue rvalue) const {
John McCalla8ec7eb2013-03-07 21:37:17 +00001366 // Aggregate r-values are already in memory, and EmitAtomicStore
1367 // requires them to be values of the atomic type.
1368 if (rvalue.isAggregate())
John McCall7f416cc2015-09-08 08:05:57 +00001369 return rvalue.getAggregateAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +00001370
1371 // Otherwise, make a temporary and materialize into it.
John McCall7f416cc2015-09-08 08:05:57 +00001372 LValue TempLV = CGF.MakeAddrLValue(CreateTempAlloca(), getAtomicType());
Alexey Bataevb8329262015-02-27 06:33:30 +00001373 AtomicInfo Atomics(CGF, TempLV);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001374 Atomics.emitCopyIntoMemory(rvalue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001375 return TempLV.getAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +00001376}
1377
Alexey Bataev452d8e12014-12-15 05:25:25 +00001378llvm::Value *AtomicInfo::convertRValueToInt(RValue RVal) const {
1379 // If we've got a scalar value of the right size, try to avoid going
1380 // through memory.
Alexey Bataevb8329262015-02-27 06:33:30 +00001381 if (RVal.isScalar() && (!hasPadding() || !LVal.isSimple())) {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001382 llvm::Value *Value = RVal.getScalarVal();
1383 if (isa<llvm::IntegerType>(Value->getType()))
Alexey Bataevb4505a72015-03-30 05:20:59 +00001384 return CGF.EmitToMemory(Value, ValueTy);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001385 else {
Alexey Bataevb8329262015-02-27 06:33:30 +00001386 llvm::IntegerType *InputIntTy = llvm::IntegerType::get(
1387 CGF.getLLVMContext(),
1388 LVal.isSimple() ? getValueSizeInBits() : getAtomicSizeInBits());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001389 if (isa<llvm::PointerType>(Value->getType()))
1390 return CGF.Builder.CreatePtrToInt(Value, InputIntTy);
1391 else if (llvm::BitCastInst::isBitCastable(Value->getType(), InputIntTy))
1392 return CGF.Builder.CreateBitCast(Value, InputIntTy);
1393 }
1394 }
1395 // Otherwise, we need to go through memory.
1396 // Put the r-value in memory.
John McCall7f416cc2015-09-08 08:05:57 +00001397 Address Addr = materializeRValue(RVal);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001398
1399 // Cast the temporary to the atomic int type and pull a value out.
1400 Addr = emitCastToAtomicIntPointer(Addr);
John McCall7f416cc2015-09-08 08:05:57 +00001401 return CGF.Builder.CreateLoad(Addr);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001402}
1403
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001404std::pair<llvm::Value *, llvm::Value *> AtomicInfo::EmitAtomicCompareExchangeOp(
1405 llvm::Value *ExpectedVal, llvm::Value *DesiredVal,
1406 llvm::AtomicOrdering Success, llvm::AtomicOrdering Failure, bool IsWeak) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001407 // Do the atomic store.
John McCall7f416cc2015-09-08 08:05:57 +00001408 Address Addr = getAtomicAddressAsAtomicIntPointer();
1409 auto *Inst = CGF.Builder.CreateAtomicCmpXchg(Addr.getPointer(),
1410 ExpectedVal, DesiredVal,
Alexey Bataevb4505a72015-03-30 05:20:59 +00001411 Success, Failure);
Alexey Bataevb8329262015-02-27 06:33:30 +00001412 // Other decoration.
1413 Inst->setVolatile(LVal.isVolatileQualified());
1414 Inst->setWeak(IsWeak);
1415
1416 // Okay, turn that back into the original value type.
1417 auto *PreviousVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/0);
1418 auto *SuccessFailureVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/1);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001419 return std::make_pair(PreviousVal, SuccessFailureVal);
Alexey Bataevb8329262015-02-27 06:33:30 +00001420}
1421
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001422llvm::Value *
1423AtomicInfo::EmitAtomicCompareExchangeLibcall(llvm::Value *ExpectedAddr,
1424 llvm::Value *DesiredAddr,
Alexey Bataevb8329262015-02-27 06:33:30 +00001425 llvm::AtomicOrdering Success,
1426 llvm::AtomicOrdering Failure) {
1427 // bool __atomic_compare_exchange(size_t size, void *obj, void *expected,
1428 // void *desired, int success, int failure);
1429 CallArgList Args;
1430 Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
John McCall7f416cc2015-09-08 08:05:57 +00001431 Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
Alexey Bataevb8329262015-02-27 06:33:30 +00001432 CGF.getContext().VoidPtrTy);
1433 Args.add(RValue::get(CGF.EmitCastToVoidPtr(ExpectedAddr)),
1434 CGF.getContext().VoidPtrTy);
1435 Args.add(RValue::get(CGF.EmitCastToVoidPtr(DesiredAddr)),
1436 CGF.getContext().VoidPtrTy);
1437 Args.add(RValue::get(llvm::ConstantInt::get(
1438 CGF.IntTy, translateAtomicOrdering(Success))),
1439 CGF.getContext().IntTy);
1440 Args.add(RValue::get(llvm::ConstantInt::get(
1441 CGF.IntTy, translateAtomicOrdering(Failure))),
1442 CGF.getContext().IntTy);
1443 auto SuccessFailureRVal = emitAtomicLibcall(CGF, "__atomic_compare_exchange",
1444 CGF.getContext().BoolTy, Args);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001445
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001446 return SuccessFailureRVal.getScalarVal();
Alexey Bataevb8329262015-02-27 06:33:30 +00001447}
1448
Alexey Bataevb4505a72015-03-30 05:20:59 +00001449std::pair<RValue, llvm::Value *> AtomicInfo::EmitAtomicCompareExchange(
Alexey Bataevb8329262015-02-27 06:33:30 +00001450 RValue Expected, RValue Desired, llvm::AtomicOrdering Success,
1451 llvm::AtomicOrdering Failure, bool IsWeak) {
1452 if (Failure >= Success)
1453 // Don't assert on undefined behavior.
1454 Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(Success);
1455
1456 // Check whether we should use a library call.
1457 if (shouldUseLibcall()) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001458 // Produce a source address.
John McCall7f416cc2015-09-08 08:05:57 +00001459 Address ExpectedAddr = materializeRValue(Expected);
1460 Address DesiredAddr = materializeRValue(Desired);
1461 auto *Res = EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1462 DesiredAddr.getPointer(),
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001463 Success, Failure);
1464 return std::make_pair(
John McCall7f416cc2015-09-08 08:05:57 +00001465 convertAtomicTempToRValue(ExpectedAddr, AggValueSlot::ignored(),
1466 SourceLocation(), /*AsValue=*/false),
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001467 Res);
Alexey Bataevb8329262015-02-27 06:33:30 +00001468 }
1469
1470 // If we've got a scalar value of the right size, try to avoid going
1471 // through memory.
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001472 auto *ExpectedVal = convertRValueToInt(Expected);
1473 auto *DesiredVal = convertRValueToInt(Desired);
1474 auto Res = EmitAtomicCompareExchangeOp(ExpectedVal, DesiredVal, Success,
1475 Failure, IsWeak);
1476 return std::make_pair(
1477 ConvertIntToValueOrAtomic(Res.first, AggValueSlot::ignored(),
1478 SourceLocation(), /*AsValue=*/false),
1479 Res.second);
1480}
1481
1482static void
1483EmitAtomicUpdateValue(CodeGenFunction &CGF, AtomicInfo &Atomics, RValue OldRVal,
1484 const llvm::function_ref<RValue(RValue)> &UpdateOp,
John McCall7f416cc2015-09-08 08:05:57 +00001485 Address DesiredAddr) {
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001486 RValue UpRVal;
1487 LValue AtomicLVal = Atomics.getAtomicLValue();
1488 LValue DesiredLVal;
1489 if (AtomicLVal.isSimple()) {
1490 UpRVal = OldRVal;
John McCall7f416cc2015-09-08 08:05:57 +00001491 DesiredLVal = CGF.MakeAddrLValue(DesiredAddr, AtomicLVal.getType());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001492 } else {
1493 // Build new lvalue for temp address
John McCall7f416cc2015-09-08 08:05:57 +00001494 Address Ptr = Atomics.materializeRValue(OldRVal);
1495 LValue UpdateLVal;
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001496 if (AtomicLVal.isBitField()) {
1497 UpdateLVal =
1498 LValue::MakeBitfield(Ptr, AtomicLVal.getBitFieldInfo(),
John McCall7f416cc2015-09-08 08:05:57 +00001499 AtomicLVal.getType(),
1500 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001501 DesiredLVal =
1502 LValue::MakeBitfield(DesiredAddr, AtomicLVal.getBitFieldInfo(),
John McCall7f416cc2015-09-08 08:05:57 +00001503 AtomicLVal.getType(),
1504 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001505 } else if (AtomicLVal.isVectorElt()) {
1506 UpdateLVal = LValue::MakeVectorElt(Ptr, AtomicLVal.getVectorIdx(),
1507 AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001508 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001509 DesiredLVal = LValue::MakeVectorElt(
1510 DesiredAddr, AtomicLVal.getVectorIdx(), AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001511 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001512 } else {
1513 assert(AtomicLVal.isExtVectorElt());
1514 UpdateLVal = LValue::MakeExtVectorElt(Ptr, AtomicLVal.getExtVectorElts(),
1515 AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001516 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001517 DesiredLVal = LValue::MakeExtVectorElt(
1518 DesiredAddr, AtomicLVal.getExtVectorElts(), AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001519 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001520 }
1521 UpdateLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1522 DesiredLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1523 UpRVal = CGF.EmitLoadOfLValue(UpdateLVal, SourceLocation());
1524 }
1525 // Store new value in the corresponding memory area
1526 RValue NewRVal = UpdateOp(UpRVal);
1527 if (NewRVal.isScalar()) {
1528 CGF.EmitStoreThroughLValue(NewRVal, DesiredLVal);
1529 } else {
1530 assert(NewRVal.isComplex());
1531 CGF.EmitStoreOfComplex(NewRVal.getComplexVal(), DesiredLVal,
1532 /*isInit=*/false);
1533 }
1534}
1535
1536void AtomicInfo::EmitAtomicUpdateLibcall(
1537 llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1538 bool IsVolatile) {
1539 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1540
John McCall7f416cc2015-09-08 08:05:57 +00001541 Address ExpectedAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001542
John McCall7f416cc2015-09-08 08:05:57 +00001543 EmitAtomicLoadLibcall(ExpectedAddr.getPointer(), AO, IsVolatile);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001544 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1545 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1546 CGF.EmitBlock(ContBB);
John McCall7f416cc2015-09-08 08:05:57 +00001547 Address DesiredAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001548 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001549 requiresMemSetZero(getAtomicAddress().getElementType())) {
1550 auto *OldVal = CGF.Builder.CreateLoad(ExpectedAddr);
1551 CGF.Builder.CreateStore(OldVal, DesiredAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001552 }
John McCall7f416cc2015-09-08 08:05:57 +00001553 auto OldRVal = convertAtomicTempToRValue(ExpectedAddr,
1554 AggValueSlot::ignored(),
1555 SourceLocation(), /*AsValue=*/false);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001556 EmitAtomicUpdateValue(CGF, *this, OldRVal, UpdateOp, DesiredAddr);
1557 auto *Res =
John McCall7f416cc2015-09-08 08:05:57 +00001558 EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1559 DesiredAddr.getPointer(),
1560 AO, Failure);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001561 CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
1562 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1563}
1564
1565void AtomicInfo::EmitAtomicUpdateOp(
1566 llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1567 bool IsVolatile) {
1568 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1569
1570 // Do the atomic load.
1571 auto *OldVal = EmitAtomicLoadOp(AO, IsVolatile);
1572 // For non-simple lvalues perform compare-and-swap procedure.
1573 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1574 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1575 auto *CurBB = CGF.Builder.GetInsertBlock();
1576 CGF.EmitBlock(ContBB);
1577 llvm::PHINode *PHI = CGF.Builder.CreatePHI(OldVal->getType(),
1578 /*NumReservedValues=*/2);
1579 PHI->addIncoming(OldVal, CurBB);
John McCall7f416cc2015-09-08 08:05:57 +00001580 Address NewAtomicAddr = CreateTempAlloca();
1581 Address NewAtomicIntAddr = emitCastToAtomicIntPointer(NewAtomicAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001582 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001583 requiresMemSetZero(getAtomicAddress().getElementType())) {
1584 CGF.Builder.CreateStore(PHI, NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001585 }
1586 auto OldRVal = ConvertIntToValueOrAtomic(PHI, AggValueSlot::ignored(),
1587 SourceLocation(), /*AsValue=*/false);
1588 EmitAtomicUpdateValue(CGF, *this, OldRVal, UpdateOp, NewAtomicAddr);
John McCall7f416cc2015-09-08 08:05:57 +00001589 auto *DesiredVal = CGF.Builder.CreateLoad(NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001590 // Try to write new value using cmpxchg operation
1591 auto Res = EmitAtomicCompareExchangeOp(PHI, DesiredVal, AO, Failure);
1592 PHI->addIncoming(Res.first, CGF.Builder.GetInsertBlock());
1593 CGF.Builder.CreateCondBr(Res.second, ExitBB, ContBB);
1594 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1595}
1596
1597static void EmitAtomicUpdateValue(CodeGenFunction &CGF, AtomicInfo &Atomics,
John McCall7f416cc2015-09-08 08:05:57 +00001598 RValue UpdateRVal, Address DesiredAddr) {
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001599 LValue AtomicLVal = Atomics.getAtomicLValue();
1600 LValue DesiredLVal;
1601 // Build new lvalue for temp address
1602 if (AtomicLVal.isBitField()) {
1603 DesiredLVal =
1604 LValue::MakeBitfield(DesiredAddr, AtomicLVal.getBitFieldInfo(),
John McCall7f416cc2015-09-08 08:05:57 +00001605 AtomicLVal.getType(),
1606 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001607 } else if (AtomicLVal.isVectorElt()) {
1608 DesiredLVal =
1609 LValue::MakeVectorElt(DesiredAddr, AtomicLVal.getVectorIdx(),
John McCall7f416cc2015-09-08 08:05:57 +00001610 AtomicLVal.getType(),
1611 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001612 } else {
1613 assert(AtomicLVal.isExtVectorElt());
1614 DesiredLVal = LValue::MakeExtVectorElt(
1615 DesiredAddr, AtomicLVal.getExtVectorElts(), AtomicLVal.getType(),
John McCall7f416cc2015-09-08 08:05:57 +00001616 AtomicLVal.getAlignmentSource());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001617 }
1618 DesiredLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1619 // Store new value in the corresponding memory area
1620 assert(UpdateRVal.isScalar());
1621 CGF.EmitStoreThroughLValue(UpdateRVal, DesiredLVal);
1622}
1623
1624void AtomicInfo::EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO,
1625 RValue UpdateRVal, bool IsVolatile) {
1626 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1627
John McCall7f416cc2015-09-08 08:05:57 +00001628 Address ExpectedAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001629
John McCall7f416cc2015-09-08 08:05:57 +00001630 EmitAtomicLoadLibcall(ExpectedAddr.getPointer(), AO, IsVolatile);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001631 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1632 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1633 CGF.EmitBlock(ContBB);
John McCall7f416cc2015-09-08 08:05:57 +00001634 Address DesiredAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001635 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001636 requiresMemSetZero(getAtomicAddress().getElementType())) {
1637 auto *OldVal = CGF.Builder.CreateLoad(ExpectedAddr);
1638 CGF.Builder.CreateStore(OldVal, DesiredAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001639 }
1640 EmitAtomicUpdateValue(CGF, *this, UpdateRVal, DesiredAddr);
1641 auto *Res =
John McCall7f416cc2015-09-08 08:05:57 +00001642 EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1643 DesiredAddr.getPointer(),
1644 AO, Failure);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001645 CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
1646 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1647}
1648
1649void AtomicInfo::EmitAtomicUpdateOp(llvm::AtomicOrdering AO, RValue UpdateRVal,
1650 bool IsVolatile) {
1651 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1652
1653 // Do the atomic load.
1654 auto *OldVal = EmitAtomicLoadOp(AO, IsVolatile);
1655 // For non-simple lvalues perform compare-and-swap procedure.
1656 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1657 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1658 auto *CurBB = CGF.Builder.GetInsertBlock();
1659 CGF.EmitBlock(ContBB);
1660 llvm::PHINode *PHI = CGF.Builder.CreatePHI(OldVal->getType(),
1661 /*NumReservedValues=*/2);
1662 PHI->addIncoming(OldVal, CurBB);
John McCall7f416cc2015-09-08 08:05:57 +00001663 Address NewAtomicAddr = CreateTempAlloca();
1664 Address NewAtomicIntAddr = emitCastToAtomicIntPointer(NewAtomicAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001665 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001666 requiresMemSetZero(getAtomicAddress().getElementType())) {
1667 CGF.Builder.CreateStore(PHI, NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001668 }
1669 EmitAtomicUpdateValue(CGF, *this, UpdateRVal, NewAtomicAddr);
John McCall7f416cc2015-09-08 08:05:57 +00001670 auto *DesiredVal = CGF.Builder.CreateLoad(NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001671 // Try to write new value using cmpxchg operation
1672 auto Res = EmitAtomicCompareExchangeOp(PHI, DesiredVal, AO, Failure);
1673 PHI->addIncoming(Res.first, CGF.Builder.GetInsertBlock());
1674 CGF.Builder.CreateCondBr(Res.second, ExitBB, ContBB);
1675 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1676}
1677
1678void AtomicInfo::EmitAtomicUpdate(
1679 llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1680 bool IsVolatile) {
1681 if (shouldUseLibcall()) {
1682 EmitAtomicUpdateLibcall(AO, UpdateOp, IsVolatile);
1683 } else {
1684 EmitAtomicUpdateOp(AO, UpdateOp, IsVolatile);
1685 }
1686}
1687
1688void AtomicInfo::EmitAtomicUpdate(llvm::AtomicOrdering AO, RValue UpdateRVal,
1689 bool IsVolatile) {
1690 if (shouldUseLibcall()) {
1691 EmitAtomicUpdateLibcall(AO, UpdateRVal, IsVolatile);
1692 } else {
1693 EmitAtomicUpdateOp(AO, UpdateRVal, IsVolatile);
1694 }
Alexey Bataevb8329262015-02-27 06:33:30 +00001695}
1696
David Majnemera5b195a2015-02-14 01:35:12 +00001697void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue lvalue,
1698 bool isInit) {
1699 bool IsVolatile = lvalue.isVolatileQualified();
1700 llvm::AtomicOrdering AO;
1701 if (lvalue.getType()->isAtomicType()) {
1702 AO = llvm::SequentiallyConsistent;
1703 } else {
1704 AO = llvm::Release;
1705 IsVolatile = true;
1706 }
1707 return EmitAtomicStore(rvalue, lvalue, AO, IsVolatile, isInit);
1708}
1709
John McCalla8ec7eb2013-03-07 21:37:17 +00001710/// Emit a store to an l-value of atomic type.
1711///
1712/// Note that the r-value is expected to be an r-value *of the atomic
1713/// type*; this means that for aggregate r-values, it should include
1714/// storage for any padding that was necessary.
David Majnemera5b195a2015-02-14 01:35:12 +00001715void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue dest,
1716 llvm::AtomicOrdering AO, bool IsVolatile,
1717 bool isInit) {
John McCalla8ec7eb2013-03-07 21:37:17 +00001718 // If this is an aggregate r-value, it should agree in type except
1719 // maybe for address-space qualification.
1720 assert(!rvalue.isAggregate() ||
John McCall7f416cc2015-09-08 08:05:57 +00001721 rvalue.getAggregateAddress().getElementType()
1722 == dest.getAddress().getElementType());
John McCalla8ec7eb2013-03-07 21:37:17 +00001723
1724 AtomicInfo atomics(*this, dest);
Alexey Bataevb8329262015-02-27 06:33:30 +00001725 LValue LVal = atomics.getAtomicLValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001726
1727 // If this is an initialization, just put the value there normally.
Alexey Bataevb8329262015-02-27 06:33:30 +00001728 if (LVal.isSimple()) {
1729 if (isInit) {
1730 atomics.emitCopyIntoMemory(rvalue);
1731 return;
1732 }
1733
1734 // Check whether we should use a library call.
1735 if (atomics.shouldUseLibcall()) {
1736 // Produce a source address.
John McCall7f416cc2015-09-08 08:05:57 +00001737 Address srcAddr = atomics.materializeRValue(rvalue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001738
1739 // void __atomic_store(size_t size, void *mem, void *val, int order)
1740 CallArgList args;
1741 args.add(RValue::get(atomics.getAtomicSizeValue()),
1742 getContext().getSizeType());
John McCall7f416cc2015-09-08 08:05:57 +00001743 args.add(RValue::get(EmitCastToVoidPtr(atomics.getAtomicPointer())),
Alexey Bataevb8329262015-02-27 06:33:30 +00001744 getContext().VoidPtrTy);
John McCall7f416cc2015-09-08 08:05:57 +00001745 args.add(RValue::get(EmitCastToVoidPtr(srcAddr.getPointer())),
1746 getContext().VoidPtrTy);
Alexey Bataevb8329262015-02-27 06:33:30 +00001747 args.add(RValue::get(llvm::ConstantInt::get(
1748 IntTy, AtomicInfo::translateAtomicOrdering(AO))),
1749 getContext().IntTy);
1750 emitAtomicLibcall(*this, "__atomic_store", getContext().VoidTy, args);
1751 return;
1752 }
1753
1754 // Okay, we're doing this natively.
1755 llvm::Value *intValue = atomics.convertRValueToInt(rvalue);
1756
1757 // Do the atomic store.
John McCall7f416cc2015-09-08 08:05:57 +00001758 Address addr =
Alexey Bataevb8329262015-02-27 06:33:30 +00001759 atomics.emitCastToAtomicIntPointer(atomics.getAtomicAddress());
1760 intValue = Builder.CreateIntCast(
John McCall7f416cc2015-09-08 08:05:57 +00001761 intValue, addr.getElementType(), /*isSigned=*/false);
Alexey Bataevb8329262015-02-27 06:33:30 +00001762 llvm::StoreInst *store = Builder.CreateStore(intValue, addr);
1763
1764 // Initializations don't need to be atomic.
1765 if (!isInit)
1766 store->setAtomic(AO);
1767
1768 // Other decoration.
Alexey Bataevb8329262015-02-27 06:33:30 +00001769 if (IsVolatile)
1770 store->setVolatile(true);
1771 if (dest.getTBAAInfo())
1772 CGM.DecorateInstruction(store, dest.getTBAAInfo());
John McCalla8ec7eb2013-03-07 21:37:17 +00001773 return;
1774 }
1775
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001776 // Emit simple atomic update operation.
1777 atomics.EmitAtomicUpdate(AO, rvalue, IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +00001778}
1779
Alexey Bataev452d8e12014-12-15 05:25:25 +00001780/// Emit a compare-and-exchange op for atomic type.
1781///
Alexey Bataevb4505a72015-03-30 05:20:59 +00001782std::pair<RValue, llvm::Value *> CodeGenFunction::EmitAtomicCompareExchange(
Alexey Bataev452d8e12014-12-15 05:25:25 +00001783 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
1784 llvm::AtomicOrdering Success, llvm::AtomicOrdering Failure, bool IsWeak,
1785 AggValueSlot Slot) {
1786 // If this is an aggregate r-value, it should agree in type except
1787 // maybe for address-space qualification.
1788 assert(!Expected.isAggregate() ||
John McCall7f416cc2015-09-08 08:05:57 +00001789 Expected.getAggregateAddress().getElementType() ==
1790 Obj.getAddress().getElementType());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001791 assert(!Desired.isAggregate() ||
John McCall7f416cc2015-09-08 08:05:57 +00001792 Desired.getAggregateAddress().getElementType() ==
1793 Obj.getAddress().getElementType());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001794 AtomicInfo Atomics(*this, Obj);
1795
Alexey Bataevb4505a72015-03-30 05:20:59 +00001796 return Atomics.EmitAtomicCompareExchange(Expected, Desired, Success, Failure,
1797 IsWeak);
1798}
1799
1800void CodeGenFunction::EmitAtomicUpdate(
1801 LValue LVal, llvm::AtomicOrdering AO,
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001802 const llvm::function_ref<RValue(RValue)> &UpdateOp, bool IsVolatile) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00001803 AtomicInfo Atomics(*this, LVal);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001804 Atomics.EmitAtomicUpdate(AO, UpdateOp, IsVolatile);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001805}
1806
John McCalla8ec7eb2013-03-07 21:37:17 +00001807void CodeGenFunction::EmitAtomicInit(Expr *init, LValue dest) {
1808 AtomicInfo atomics(*this, dest);
1809
1810 switch (atomics.getEvaluationKind()) {
1811 case TEK_Scalar: {
1812 llvm::Value *value = EmitScalarExpr(init);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001813 atomics.emitCopyIntoMemory(RValue::get(value));
John McCalla8ec7eb2013-03-07 21:37:17 +00001814 return;
1815 }
1816
1817 case TEK_Complex: {
1818 ComplexPairTy value = EmitComplexExpr(init);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001819 atomics.emitCopyIntoMemory(RValue::getComplex(value));
John McCalla8ec7eb2013-03-07 21:37:17 +00001820 return;
1821 }
1822
1823 case TEK_Aggregate: {
Eli Friedmanbe4504d2013-07-11 01:32:21 +00001824 // Fix up the destination if the initializer isn't an expression
1825 // of atomic type.
1826 bool Zeroed = false;
John McCalla8ec7eb2013-03-07 21:37:17 +00001827 if (!init->getType()->isAtomicType()) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001828 Zeroed = atomics.emitMemSetZeroIfNecessary();
1829 dest = atomics.projectValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001830 }
1831
1832 // Evaluate the expression directly into the destination.
1833 AggValueSlot slot = AggValueSlot::forLValue(dest,
1834 AggValueSlot::IsNotDestructed,
1835 AggValueSlot::DoesNotNeedGCBarriers,
Eli Friedmanbe4504d2013-07-11 01:32:21 +00001836 AggValueSlot::IsNotAliased,
1837 Zeroed ? AggValueSlot::IsZeroed :
1838 AggValueSlot::IsNotZeroed);
1839
John McCalla8ec7eb2013-03-07 21:37:17 +00001840 EmitAggExpr(init, slot);
1841 return;
1842 }
1843 }
1844 llvm_unreachable("bad evaluation kind");
1845}