blob: 5e6633fff163597a5abac795c85dbdc74560d518 [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
John McCallfc207f22013-03-07 21:37:12 +000014#include "CGCall.h"
Alexey Bataevb57056f2015-01-22 06:17:56 +000015#include "CGRecordLayout.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000016#include "CodeGenFunction.h"
John McCallfc207f22013-03-07 21:37:12 +000017#include "CodeGenModule.h"
Yaxun Liu39195062017-08-04 18:16:31 +000018#include "TargetInfo.h"
John McCallfc207f22013-03-07 21:37:12 +000019#include "clang/AST/ASTContext.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000020#include "clang/CodeGen/CGFunctionInfo.h"
John McCallfc207f22013-03-07 21:37:12 +000021#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/Intrinsics.h"
John McCalla8ec7eb2013-03-07 21:37:17 +000023#include "llvm/IR/Operator.h"
John McCallfc207f22013-03-07 21:37:12 +000024
25using namespace clang;
26using namespace CodeGen;
27
John McCalla8ec7eb2013-03-07 21:37:17 +000028namespace {
29 class AtomicInfo {
30 CodeGenFunction &CGF;
31 QualType AtomicTy;
32 QualType ValueTy;
33 uint64_t AtomicSizeInBits;
34 uint64_t ValueSizeInBits;
35 CharUnits AtomicAlign;
36 CharUnits ValueAlign;
37 CharUnits LValueAlign;
38 TypeEvaluationKind EvaluationKind;
39 bool UseLibcall;
Alexey Bataevb57056f2015-01-22 06:17:56 +000040 LValue LVal;
41 CGBitFieldInfo BFI;
John McCalla8ec7eb2013-03-07 21:37:17 +000042 public:
Alexey Bataevb57056f2015-01-22 06:17:56 +000043 AtomicInfo(CodeGenFunction &CGF, LValue &lvalue)
44 : CGF(CGF), AtomicSizeInBits(0), ValueSizeInBits(0),
45 EvaluationKind(TEK_Scalar), UseLibcall(true) {
46 assert(!lvalue.isGlobalReg());
John McCalla8ec7eb2013-03-07 21:37:17 +000047 ASTContext &C = CGF.getContext();
Alexey Bataevb57056f2015-01-22 06:17:56 +000048 if (lvalue.isSimple()) {
49 AtomicTy = lvalue.getType();
50 if (auto *ATy = AtomicTy->getAs<AtomicType>())
51 ValueTy = ATy->getValueType();
52 else
53 ValueTy = AtomicTy;
54 EvaluationKind = CGF.getEvaluationKind(ValueTy);
John McCalla8ec7eb2013-03-07 21:37:17 +000055
Alexey Bataevb57056f2015-01-22 06:17:56 +000056 uint64_t ValueAlignInBits;
57 uint64_t AtomicAlignInBits;
58 TypeInfo ValueTI = C.getTypeInfo(ValueTy);
59 ValueSizeInBits = ValueTI.Width;
60 ValueAlignInBits = ValueTI.Align;
John McCalla8ec7eb2013-03-07 21:37:17 +000061
Alexey Bataevb57056f2015-01-22 06:17:56 +000062 TypeInfo AtomicTI = C.getTypeInfo(AtomicTy);
63 AtomicSizeInBits = AtomicTI.Width;
64 AtomicAlignInBits = AtomicTI.Align;
John McCalla8ec7eb2013-03-07 21:37:17 +000065
Alexey Bataevb57056f2015-01-22 06:17:56 +000066 assert(ValueSizeInBits <= AtomicSizeInBits);
67 assert(ValueAlignInBits <= AtomicAlignInBits);
John McCalla8ec7eb2013-03-07 21:37:17 +000068
Alexey Bataevb57056f2015-01-22 06:17:56 +000069 AtomicAlign = C.toCharUnitsFromBits(AtomicAlignInBits);
70 ValueAlign = C.toCharUnitsFromBits(ValueAlignInBits);
71 if (lvalue.getAlignment().isZero())
72 lvalue.setAlignment(AtomicAlign);
John McCalla8ec7eb2013-03-07 21:37:17 +000073
Alexey Bataevb57056f2015-01-22 06:17:56 +000074 LVal = lvalue;
75 } else if (lvalue.isBitField()) {
Alexey Bataevb8329262015-02-27 06:33:30 +000076 ValueTy = lvalue.getType();
77 ValueSizeInBits = C.getTypeSize(ValueTy);
Alexey Bataevb57056f2015-01-22 06:17:56 +000078 auto &OrigBFI = lvalue.getBitFieldInfo();
79 auto Offset = OrigBFI.Offset % C.toBits(lvalue.getAlignment());
80 AtomicSizeInBits = C.toBits(
81 C.toCharUnitsFromBits(Offset + OrigBFI.Size + C.getCharWidth() - 1)
Rui Ueyama83aa9792016-01-14 21:00:27 +000082 .alignTo(lvalue.getAlignment()));
John McCall7f416cc2015-09-08 08:05:57 +000083 auto VoidPtrAddr = CGF.EmitCastToVoidPtr(lvalue.getBitFieldPointer());
Alexey Bataevb57056f2015-01-22 06:17:56 +000084 auto OffsetInChars =
85 (C.toCharUnitsFromBits(OrigBFI.Offset) / lvalue.getAlignment()) *
86 lvalue.getAlignment();
87 VoidPtrAddr = CGF.Builder.CreateConstGEP1_64(
88 VoidPtrAddr, OffsetInChars.getQuantity());
89 auto Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
90 VoidPtrAddr,
91 CGF.Builder.getIntNTy(AtomicSizeInBits)->getPointerTo(),
92 "atomic_bitfield_base");
93 BFI = OrigBFI;
94 BFI.Offset = Offset;
95 BFI.StorageSize = AtomicSizeInBits;
Ulrich Weigand03ce2a12015-07-10 17:30:00 +000096 BFI.StorageOffset += OffsetInChars;
John McCall7f416cc2015-09-08 08:05:57 +000097 LVal = LValue::MakeBitfield(Address(Addr, lvalue.getAlignment()),
98 BFI, lvalue.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +000099 lvalue.getBaseInfo());
Alexey Bataevb8329262015-02-27 06:33:30 +0000100 LVal.setTBAAInfo(lvalue.getTBAAInfo());
101 AtomicTy = C.getIntTypeForBitwidth(AtomicSizeInBits, OrigBFI.IsSigned);
102 if (AtomicTy.isNull()) {
103 llvm::APInt Size(
104 /*numBits=*/32,
105 C.toCharUnitsFromBits(AtomicSizeInBits).getQuantity());
106 AtomicTy = C.getConstantArrayType(C.CharTy, Size, ArrayType::Normal,
107 /*IndexTypeQuals=*/0);
108 }
109 AtomicAlign = ValueAlign = lvalue.getAlignment();
Alexey Bataevb57056f2015-01-22 06:17:56 +0000110 } else if (lvalue.isVectorElt()) {
Alexey Bataevb8329262015-02-27 06:33:30 +0000111 ValueTy = lvalue.getType()->getAs<VectorType>()->getElementType();
112 ValueSizeInBits = C.getTypeSize(ValueTy);
113 AtomicTy = lvalue.getType();
114 AtomicSizeInBits = C.getTypeSize(AtomicTy);
115 AtomicAlign = ValueAlign = lvalue.getAlignment();
Alexey Bataevb57056f2015-01-22 06:17:56 +0000116 LVal = lvalue;
117 } else {
118 assert(lvalue.isExtVectorElt());
Alexey Bataevb8329262015-02-27 06:33:30 +0000119 ValueTy = lvalue.getType();
120 ValueSizeInBits = C.getTypeSize(ValueTy);
121 AtomicTy = ValueTy = CGF.getContext().getExtVectorType(
John McCall7f416cc2015-09-08 08:05:57 +0000122 lvalue.getType(), lvalue.getExtVectorAddress()
123 .getElementType()->getVectorNumElements());
Alexey Bataevb8329262015-02-27 06:33:30 +0000124 AtomicSizeInBits = C.getTypeSize(AtomicTy);
125 AtomicAlign = ValueAlign = lvalue.getAlignment();
Alexey Bataevb57056f2015-01-22 06:17:56 +0000126 LVal = lvalue;
127 }
Alexey Bataev452d8e12014-12-15 05:25:25 +0000128 UseLibcall = !C.getTargetInfo().hasBuiltinAtomic(
129 AtomicSizeInBits, C.toBits(lvalue.getAlignment()));
John McCalla8ec7eb2013-03-07 21:37:17 +0000130 }
131
132 QualType getAtomicType() const { return AtomicTy; }
133 QualType getValueType() const { return ValueTy; }
134 CharUnits getAtomicAlignment() const { return AtomicAlign; }
135 CharUnits getValueAlignment() const { return ValueAlign; }
136 uint64_t getAtomicSizeInBits() const { return AtomicSizeInBits; }
Alexey Bataev452d8e12014-12-15 05:25:25 +0000137 uint64_t getValueSizeInBits() const { return ValueSizeInBits; }
John McCalla8ec7eb2013-03-07 21:37:17 +0000138 TypeEvaluationKind getEvaluationKind() const { return EvaluationKind; }
139 bool shouldUseLibcall() const { return UseLibcall; }
Alexey Bataevb57056f2015-01-22 06:17:56 +0000140 const LValue &getAtomicLValue() const { return LVal; }
John McCall7f416cc2015-09-08 08:05:57 +0000141 llvm::Value *getAtomicPointer() const {
Alexey Bataevb8329262015-02-27 06:33:30 +0000142 if (LVal.isSimple())
John McCall7f416cc2015-09-08 08:05:57 +0000143 return LVal.getPointer();
Alexey Bataevb8329262015-02-27 06:33:30 +0000144 else if (LVal.isBitField())
John McCall7f416cc2015-09-08 08:05:57 +0000145 return LVal.getBitFieldPointer();
Alexey Bataevb8329262015-02-27 06:33:30 +0000146 else if (LVal.isVectorElt())
John McCall7f416cc2015-09-08 08:05:57 +0000147 return LVal.getVectorPointer();
Alexey Bataevb8329262015-02-27 06:33:30 +0000148 assert(LVal.isExtVectorElt());
John McCall7f416cc2015-09-08 08:05:57 +0000149 return LVal.getExtVectorPointer();
150 }
151 Address getAtomicAddress() const {
152 return Address(getAtomicPointer(), getAtomicAlignment());
153 }
154
155 Address getAtomicAddressAsAtomicIntPointer() const {
156 return emitCastToAtomicIntPointer(getAtomicAddress());
Alexey Bataevb8329262015-02-27 06:33:30 +0000157 }
John McCalla8ec7eb2013-03-07 21:37:17 +0000158
159 /// Is the atomic size larger than the underlying value type?
160 ///
161 /// Note that the absence of padding does not mean that atomic
162 /// objects are completely interchangeable with non-atomic
163 /// objects: we might have promoted the alignment of a type
164 /// without making it bigger.
165 bool hasPadding() const {
166 return (ValueSizeInBits != AtomicSizeInBits);
167 }
168
Alexey Bataevb57056f2015-01-22 06:17:56 +0000169 bool emitMemSetZeroIfNecessary() const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000170
171 llvm::Value *getAtomicSizeValue() const {
172 CharUnits size = CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits);
173 return CGF.CGM.getSize(size);
174 }
175
Tim Northovercc2a6e02015-11-09 19:56:35 +0000176 /// Cast the given pointer to an integer pointer suitable for atomic
177 /// operations if the source.
178 Address emitCastToAtomicIntPointer(Address Addr) const;
179
180 /// If Addr is compatible with the iN that will be used for an atomic
181 /// operation, bitcast it. Otherwise, create a temporary that is suitable
182 /// and copy the value across.
183 Address convertToAtomicIntPointer(Address Addr) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000184
185 /// Turn an atomic-layout object into an r-value.
John McCall7f416cc2015-09-08 08:05:57 +0000186 RValue convertAtomicTempToRValue(Address addr, AggValueSlot resultSlot,
187 SourceLocation loc, bool AsValue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000188
Alexey Bataev452d8e12014-12-15 05:25:25 +0000189 /// \brief Converts a rvalue to integer value.
190 llvm::Value *convertRValueToInt(RValue RVal) const;
191
Alexey Bataevb8329262015-02-27 06:33:30 +0000192 RValue ConvertIntToValueOrAtomic(llvm::Value *IntVal,
193 AggValueSlot ResultSlot,
194 SourceLocation Loc, bool AsValue) const;
Alexey Bataev452d8e12014-12-15 05:25:25 +0000195
John McCalla8ec7eb2013-03-07 21:37:17 +0000196 /// Copy an atomic r-value into atomic-layout memory.
Alexey Bataevb57056f2015-01-22 06:17:56 +0000197 void emitCopyIntoMemory(RValue rvalue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000198
199 /// Project an l-value down to the value field.
Alexey Bataevb57056f2015-01-22 06:17:56 +0000200 LValue projectValue() const {
201 assert(LVal.isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000202 Address addr = getAtomicAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +0000203 if (hasPadding())
John McCall7f416cc2015-09-08 08:05:57 +0000204 addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
John McCalla8ec7eb2013-03-07 21:37:17 +0000205
John McCall7f416cc2015-09-08 08:05:57 +0000206 return LValue::MakeAddr(addr, getValueType(), CGF.getContext(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000207 LVal.getBaseInfo(), LVal.getTBAAInfo());
John McCalla8ec7eb2013-03-07 21:37:17 +0000208 }
209
Alexey Bataevb8329262015-02-27 06:33:30 +0000210 /// \brief Emits atomic load.
211 /// \returns Loaded value.
212 RValue EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
213 bool AsValue, llvm::AtomicOrdering AO,
214 bool IsVolatile);
215
216 /// \brief Emits atomic compare-and-exchange sequence.
217 /// \param Expected Expected value.
218 /// \param Desired Desired value.
219 /// \param Success Atomic ordering for success operation.
220 /// \param Failure Atomic ordering for failed operation.
221 /// \param IsWeak true if atomic operation is weak, false otherwise.
222 /// \returns Pair of values: previous value from storage (value type) and
223 /// boolean flag (i1 type) with true if success and false otherwise.
JF Bastien92f4ef12016-04-06 17:26:42 +0000224 std::pair<RValue, llvm::Value *>
225 EmitAtomicCompareExchange(RValue Expected, RValue Desired,
226 llvm::AtomicOrdering Success =
227 llvm::AtomicOrdering::SequentiallyConsistent,
228 llvm::AtomicOrdering Failure =
229 llvm::AtomicOrdering::SequentiallyConsistent,
230 bool IsWeak = false);
Alexey Bataevb8329262015-02-27 06:33:30 +0000231
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000232 /// \brief Emits atomic update.
NAKAMURA Takumid16af5d2015-05-15 13:47:52 +0000233 /// \param AO Atomic ordering.
234 /// \param UpdateOp Update operation for the current lvalue.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000235 void EmitAtomicUpdate(llvm::AtomicOrdering AO,
236 const llvm::function_ref<RValue(RValue)> &UpdateOp,
237 bool IsVolatile);
238 /// \brief Emits atomic update.
NAKAMURA Takumid16af5d2015-05-15 13:47:52 +0000239 /// \param AO Atomic ordering.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000240 void EmitAtomicUpdate(llvm::AtomicOrdering AO, RValue UpdateRVal,
241 bool IsVolatile);
242
John McCalla8ec7eb2013-03-07 21:37:17 +0000243 /// Materialize an atomic r-value in atomic-layout memory.
John McCall7f416cc2015-09-08 08:05:57 +0000244 Address materializeRValue(RValue rvalue) const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000245
Tim Northovercc2a6e02015-11-09 19:56:35 +0000246 /// \brief Creates temp alloca for intermediate operations on atomic value.
247 Address CreateTempAlloca() const;
John McCalla8ec7eb2013-03-07 21:37:17 +0000248 private:
249 bool requiresMemSetZero(llvm::Type *type) const;
Alexey Bataevb8329262015-02-27 06:33:30 +0000250
Alexey Bataevb8329262015-02-27 06:33:30 +0000251
252 /// \brief Emits atomic load as a libcall.
253 void EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
254 llvm::AtomicOrdering AO, bool IsVolatile);
255 /// \brief Emits atomic load as LLVM instruction.
256 llvm::Value *EmitAtomicLoadOp(llvm::AtomicOrdering AO, bool IsVolatile);
257 /// \brief Emits atomic compare-and-exchange op as a libcall.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000258 llvm::Value *EmitAtomicCompareExchangeLibcall(
259 llvm::Value *ExpectedAddr, llvm::Value *DesiredAddr,
JF Bastien92f4ef12016-04-06 17:26:42 +0000260 llvm::AtomicOrdering Success =
261 llvm::AtomicOrdering::SequentiallyConsistent,
262 llvm::AtomicOrdering Failure =
263 llvm::AtomicOrdering::SequentiallyConsistent);
Alexey Bataevb8329262015-02-27 06:33:30 +0000264 /// \brief Emits atomic compare-and-exchange op as LLVM instruction.
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000265 std::pair<llvm::Value *, llvm::Value *> EmitAtomicCompareExchangeOp(
266 llvm::Value *ExpectedVal, llvm::Value *DesiredVal,
JF Bastien92f4ef12016-04-06 17:26:42 +0000267 llvm::AtomicOrdering Success =
268 llvm::AtomicOrdering::SequentiallyConsistent,
269 llvm::AtomicOrdering Failure =
270 llvm::AtomicOrdering::SequentiallyConsistent,
Alexey Bataevb8329262015-02-27 06:33:30 +0000271 bool IsWeak = false);
Alexey Bataevf0ab5532015-05-15 08:36:34 +0000272 /// \brief Emit atomic update as libcalls.
273 void
274 EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO,
275 const llvm::function_ref<RValue(RValue)> &UpdateOp,
276 bool IsVolatile);
277 /// \brief Emit atomic update as LLVM instructions.
278 void EmitAtomicUpdateOp(llvm::AtomicOrdering AO,
279 const llvm::function_ref<RValue(RValue)> &UpdateOp,
280 bool IsVolatile);
281 /// \brief Emit atomic update as libcalls.
282 void EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO, RValue UpdateRVal,
283 bool IsVolatile);
284 /// \brief Emit atomic update as LLVM instructions.
285 void EmitAtomicUpdateOp(llvm::AtomicOrdering AO, RValue UpdateRal,
286 bool IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +0000287 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000288}
John McCalla8ec7eb2013-03-07 21:37:17 +0000289
John McCall7f416cc2015-09-08 08:05:57 +0000290Address AtomicInfo::CreateTempAlloca() const {
291 Address TempAlloca = CGF.CreateMemTemp(
Alexey Bataevb8329262015-02-27 06:33:30 +0000292 (LVal.isBitField() && ValueSizeInBits > AtomicSizeInBits) ? ValueTy
293 : AtomicTy,
John McCall7f416cc2015-09-08 08:05:57 +0000294 getAtomicAlignment(),
Alexey Bataevb8329262015-02-27 06:33:30 +0000295 "atomic-temp");
Alexey Bataevb8329262015-02-27 06:33:30 +0000296 // Cast to pointer to value type for bitfields.
297 if (LVal.isBitField())
298 return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +0000299 TempAlloca, getAtomicAddress().getType());
Alexey Bataevb8329262015-02-27 06:33:30 +0000300 return TempAlloca;
301}
302
John McCalla8ec7eb2013-03-07 21:37:17 +0000303static RValue emitAtomicLibcall(CodeGenFunction &CGF,
304 StringRef fnName,
305 QualType resultType,
306 CallArgList &args) {
307 const CGFunctionInfo &fnInfo =
John McCallc56a8b32016-03-11 04:30:31 +0000308 CGF.CGM.getTypes().arrangeBuiltinFunctionCall(resultType, args);
John McCalla8ec7eb2013-03-07 21:37:17 +0000309 llvm::FunctionType *fnTy = CGF.CGM.getTypes().GetFunctionType(fnInfo);
310 llvm::Constant *fn = CGF.CGM.CreateRuntimeFunction(fnTy, fnName);
John McCallb92ab1a2016-10-26 23:46:34 +0000311 auto callee = CGCallee::forDirect(fn);
312 return CGF.EmitCall(fnInfo, callee, ReturnValueSlot(), args);
John McCalla8ec7eb2013-03-07 21:37:17 +0000313}
314
315/// Does a store of the given IR type modify the full expected width?
316static bool isFullSizeType(CodeGenModule &CGM, llvm::Type *type,
317 uint64_t expectedSize) {
318 return (CGM.getDataLayout().getTypeStoreSize(type) * 8 == expectedSize);
319}
320
321/// Does the atomic type require memsetting to zero before initialization?
322///
323/// The IR type is provided as a way of making certain queries faster.
324bool AtomicInfo::requiresMemSetZero(llvm::Type *type) const {
325 // If the atomic type has size padding, we definitely need a memset.
326 if (hasPadding()) return true;
327
328 // Otherwise, do some simple heuristics to try to avoid it:
329 switch (getEvaluationKind()) {
330 // For scalars and complexes, check whether the store size of the
331 // type uses the full size.
332 case TEK_Scalar:
333 return !isFullSizeType(CGF.CGM, type, AtomicSizeInBits);
334 case TEK_Complex:
335 return !isFullSizeType(CGF.CGM, type->getStructElementType(0),
336 AtomicSizeInBits / 2);
337
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000338 // Padding in structs has an undefined bit pattern. User beware.
John McCalla8ec7eb2013-03-07 21:37:17 +0000339 case TEK_Aggregate:
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000340 return false;
John McCalla8ec7eb2013-03-07 21:37:17 +0000341 }
342 llvm_unreachable("bad evaluation kind");
343}
344
Alexey Bataevb57056f2015-01-22 06:17:56 +0000345bool AtomicInfo::emitMemSetZeroIfNecessary() const {
346 assert(LVal.isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000347 llvm::Value *addr = LVal.getPointer();
John McCalla8ec7eb2013-03-07 21:37:17 +0000348 if (!requiresMemSetZero(addr->getType()->getPointerElementType()))
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000349 return false;
John McCalla8ec7eb2013-03-07 21:37:17 +0000350
Alexey Bataevb8329262015-02-27 06:33:30 +0000351 CGF.Builder.CreateMemSet(
352 addr, llvm::ConstantInt::get(CGF.Int8Ty, 0),
353 CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits).getQuantity(),
354 LVal.getAlignment().getQuantity());
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000355 return true;
John McCalla8ec7eb2013-03-07 21:37:17 +0000356}
357
Tim Northovercadbbe12014-06-13 19:43:04 +0000358static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak,
John McCall7f416cc2015-09-08 08:05:57 +0000359 Address Dest, Address Ptr,
360 Address Val1, Address Val2,
361 uint64_t Size,
Tim Northover9c177222014-03-13 19:25:48 +0000362 llvm::AtomicOrdering SuccessOrder,
Yaxun Liu39195062017-08-04 18:16:31 +0000363 llvm::AtomicOrdering FailureOrder,
364 llvm::SyncScope::ID Scope) {
Tim Northover9c177222014-03-13 19:25:48 +0000365 // Note that cmpxchg doesn't support weak cmpxchg, at least at the moment.
John McCall7f416cc2015-09-08 08:05:57 +0000366 llvm::Value *Expected = CGF.Builder.CreateLoad(Val1);
367 llvm::Value *Desired = CGF.Builder.CreateLoad(Val2);
Tim Northover9c177222014-03-13 19:25:48 +0000368
Tim Northoverb49b04b2014-06-13 14:24:59 +0000369 llvm::AtomicCmpXchgInst *Pair = CGF.Builder.CreateAtomicCmpXchg(
Yaxun Liu39195062017-08-04 18:16:31 +0000370 Ptr.getPointer(), Expected, Desired, SuccessOrder, FailureOrder,
371 Scope);
Tim Northoverb49b04b2014-06-13 14:24:59 +0000372 Pair->setVolatile(E->isVolatile());
Tim Northovercadbbe12014-06-13 19:43:04 +0000373 Pair->setWeak(IsWeak);
Tim Northover9c177222014-03-13 19:25:48 +0000374
375 // Cmp holds the result of the compare-exchange operation: true on success,
376 // false on failure.
Tim Northoverb49b04b2014-06-13 14:24:59 +0000377 llvm::Value *Old = CGF.Builder.CreateExtractValue(Pair, 0);
378 llvm::Value *Cmp = CGF.Builder.CreateExtractValue(Pair, 1);
Tim Northover9c177222014-03-13 19:25:48 +0000379
380 // This basic block is used to hold the store instruction if the operation
381 // failed.
382 llvm::BasicBlock *StoreExpectedBB =
383 CGF.createBasicBlock("cmpxchg.store_expected", CGF.CurFn);
384
385 // This basic block is the exit point of the operation, we should end up
386 // here regardless of whether or not the operation succeeded.
387 llvm::BasicBlock *ContinueBB =
388 CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
389
390 // Update Expected if Expected isn't equal to Old, otherwise branch to the
391 // exit point.
392 CGF.Builder.CreateCondBr(Cmp, ContinueBB, StoreExpectedBB);
393
394 CGF.Builder.SetInsertPoint(StoreExpectedBB);
395 // Update the memory at Expected with Old's value.
John McCall7f416cc2015-09-08 08:05:57 +0000396 CGF.Builder.CreateStore(Old, Val1);
Tim Northover9c177222014-03-13 19:25:48 +0000397 // Finally, branch to the exit point.
398 CGF.Builder.CreateBr(ContinueBB);
399
400 CGF.Builder.SetInsertPoint(ContinueBB);
401 // Update the memory at Dest with Cmp's value.
402 CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType()));
Tim Northover9c177222014-03-13 19:25:48 +0000403}
404
405/// Given an ordering required on success, emit all possible cmpxchg
406/// instructions to cope with the provided (but possibly only dynamically known)
407/// FailureOrder.
408static void emitAtomicCmpXchgFailureSet(CodeGenFunction &CGF, AtomicExpr *E,
JF Bastiendda2cb12016-04-18 18:01:49 +0000409 bool IsWeak, Address Dest, Address Ptr,
410 Address Val1, Address Val2,
Tim Northover9c177222014-03-13 19:25:48 +0000411 llvm::Value *FailureOrderVal,
John McCall7f416cc2015-09-08 08:05:57 +0000412 uint64_t Size,
Yaxun Liu39195062017-08-04 18:16:31 +0000413 llvm::AtomicOrdering SuccessOrder,
414 llvm::SyncScope::ID Scope) {
Tim Northover9c177222014-03-13 19:25:48 +0000415 llvm::AtomicOrdering FailureOrder;
416 if (llvm::ConstantInt *FO = dyn_cast<llvm::ConstantInt>(FailureOrderVal)) {
JF Bastiendda2cb12016-04-18 18:01:49 +0000417 auto FOS = FO->getSExtValue();
418 if (!llvm::isValidAtomicOrderingCABI(FOS))
JF Bastien92f4ef12016-04-06 17:26:42 +0000419 FailureOrder = llvm::AtomicOrdering::Monotonic;
JF Bastiendda2cb12016-04-18 18:01:49 +0000420 else
421 switch ((llvm::AtomicOrderingCABI)FOS) {
422 case llvm::AtomicOrderingCABI::relaxed:
423 case llvm::AtomicOrderingCABI::release:
424 case llvm::AtomicOrderingCABI::acq_rel:
425 FailureOrder = llvm::AtomicOrdering::Monotonic;
426 break;
427 case llvm::AtomicOrderingCABI::consume:
428 case llvm::AtomicOrderingCABI::acquire:
429 FailureOrder = llvm::AtomicOrdering::Acquire;
430 break;
431 case llvm::AtomicOrderingCABI::seq_cst:
432 FailureOrder = llvm::AtomicOrdering::SequentiallyConsistent;
433 break;
434 }
JF Bastiendd11ee72016-04-06 23:37:36 +0000435 if (isStrongerThan(FailureOrder, SuccessOrder)) {
436 // Don't assert on undefined behavior "failure argument shall be no
437 // stronger than the success argument".
Tim Northover9c177222014-03-13 19:25:48 +0000438 FailureOrder =
JF Bastiendda2cb12016-04-18 18:01:49 +0000439 llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrder);
Tim Northover9c177222014-03-13 19:25:48 +0000440 }
JF Bastiendda2cb12016-04-18 18:01:49 +0000441 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2, Size, SuccessOrder,
Yaxun Liu39195062017-08-04 18:16:31 +0000442 FailureOrder, Scope);
Tim Northover9c177222014-03-13 19:25:48 +0000443 return;
444 }
445
446 // Create all the relevant BB's
Craig Topper8a13c412014-05-21 05:09:00 +0000447 llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
448 *SeqCstBB = nullptr;
Tim Northover9c177222014-03-13 19:25:48 +0000449 MonotonicBB = CGF.createBasicBlock("monotonic_fail", CGF.CurFn);
JF Bastien92f4ef12016-04-06 17:26:42 +0000450 if (SuccessOrder != llvm::AtomicOrdering::Monotonic &&
451 SuccessOrder != llvm::AtomicOrdering::Release)
Tim Northover9c177222014-03-13 19:25:48 +0000452 AcquireBB = CGF.createBasicBlock("acquire_fail", CGF.CurFn);
JF Bastien92f4ef12016-04-06 17:26:42 +0000453 if (SuccessOrder == llvm::AtomicOrdering::SequentiallyConsistent)
Tim Northover9c177222014-03-13 19:25:48 +0000454 SeqCstBB = CGF.createBasicBlock("seqcst_fail", CGF.CurFn);
455
456 llvm::BasicBlock *ContBB = CGF.createBasicBlock("atomic.continue", CGF.CurFn);
457
458 llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(FailureOrderVal, MonotonicBB);
459
460 // Emit all the different atomics
461
462 // MonotonicBB is arbitrarily chosen as the default case; in practice, this
463 // doesn't matter unless someone is crazy enough to use something that
464 // doesn't fold to a constant for the ordering.
465 CGF.Builder.SetInsertPoint(MonotonicBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000466 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
Yaxun Liu39195062017-08-04 18:16:31 +0000467 Size, SuccessOrder, llvm::AtomicOrdering::Monotonic, Scope);
Tim Northover9c177222014-03-13 19:25:48 +0000468 CGF.Builder.CreateBr(ContBB);
469
470 if (AcquireBB) {
471 CGF.Builder.SetInsertPoint(AcquireBB);
Tim Northovercadbbe12014-06-13 19:43:04 +0000472 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
Yaxun Liu39195062017-08-04 18:16:31 +0000473 Size, SuccessOrder, llvm::AtomicOrdering::Acquire, Scope);
Tim Northover9c177222014-03-13 19:25:48 +0000474 CGF.Builder.CreateBr(ContBB);
JF Bastiendda2cb12016-04-18 18:01:49 +0000475 SI->addCase(CGF.Builder.getInt32((int)llvm::AtomicOrderingCABI::consume),
Tim Northover9c177222014-03-13 19:25:48 +0000476 AcquireBB);
JF Bastiendda2cb12016-04-18 18:01:49 +0000477 SI->addCase(CGF.Builder.getInt32((int)llvm::AtomicOrderingCABI::acquire),
Tim Northover9c177222014-03-13 19:25:48 +0000478 AcquireBB);
479 }
480 if (SeqCstBB) {
481 CGF.Builder.SetInsertPoint(SeqCstBB);
JF Bastien92f4ef12016-04-06 17:26:42 +0000482 emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2, Size, SuccessOrder,
Yaxun Liu39195062017-08-04 18:16:31 +0000483 llvm::AtomicOrdering::SequentiallyConsistent, Scope);
Tim Northover9c177222014-03-13 19:25:48 +0000484 CGF.Builder.CreateBr(ContBB);
JF Bastiendda2cb12016-04-18 18:01:49 +0000485 SI->addCase(CGF.Builder.getInt32((int)llvm::AtomicOrderingCABI::seq_cst),
Tim Northover9c177222014-03-13 19:25:48 +0000486 SeqCstBB);
487 }
488
489 CGF.Builder.SetInsertPoint(ContBB);
490}
491
John McCall7f416cc2015-09-08 08:05:57 +0000492static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, Address Dest,
493 Address Ptr, Address Val1, Address Val2,
Tim Northovercadbbe12014-06-13 19:43:04 +0000494 llvm::Value *IsWeak, llvm::Value *FailureOrder,
Yaxun Liu39195062017-08-04 18:16:31 +0000495 uint64_t Size, llvm::AtomicOrdering Order,
496 llvm::SyncScope::ID Scope) {
John McCallfc207f22013-03-07 21:37:12 +0000497 llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add;
498 llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0;
499
500 switch (E->getOp()) {
501 case AtomicExpr::AO__c11_atomic_init:
Yaxun Liu39195062017-08-04 18:16:31 +0000502 case AtomicExpr::AO__opencl_atomic_init:
John McCallfc207f22013-03-07 21:37:12 +0000503 llvm_unreachable("Already handled!");
504
505 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
Yaxun Liu39195062017-08-04 18:16:31 +0000506 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
Tim Northovercadbbe12014-06-13 19:43:04 +0000507 emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
Yaxun Liu39195062017-08-04 18:16:31 +0000508 FailureOrder, Size, Order, Scope);
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:
Yaxun Liu39195062017-08-04 18:16:31 +0000511 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
Tim Northovercadbbe12014-06-13 19:43:04 +0000512 emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
Yaxun Liu39195062017-08-04 18:16:31 +0000513 FailureOrder, Size, Order, Scope);
Tim Northovercadbbe12014-06-13 19:43:04 +0000514 return;
515 case AtomicExpr::AO__atomic_compare_exchange:
516 case AtomicExpr::AO__atomic_compare_exchange_n: {
517 if (llvm::ConstantInt *IsWeakC = dyn_cast<llvm::ConstantInt>(IsWeak)) {
518 emitAtomicCmpXchgFailureSet(CGF, E, IsWeakC->getZExtValue(), Dest, Ptr,
Yaxun Liu39195062017-08-04 18:16:31 +0000519 Val1, Val2, FailureOrder, Size, Order, Scope);
Tim Northovercadbbe12014-06-13 19:43:04 +0000520 } else {
521 // Create all the relevant BB's
522 llvm::BasicBlock *StrongBB =
523 CGF.createBasicBlock("cmpxchg.strong", CGF.CurFn);
524 llvm::BasicBlock *WeakBB = CGF.createBasicBlock("cmxchg.weak", CGF.CurFn);
525 llvm::BasicBlock *ContBB =
526 CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
527
528 llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(IsWeak, WeakBB);
529 SI->addCase(CGF.Builder.getInt1(false), StrongBB);
530
531 CGF.Builder.SetInsertPoint(StrongBB);
532 emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
Yaxun Liu39195062017-08-04 18:16:31 +0000533 FailureOrder, Size, Order, Scope);
Tim Northovercadbbe12014-06-13 19:43:04 +0000534 CGF.Builder.CreateBr(ContBB);
535
536 CGF.Builder.SetInsertPoint(WeakBB);
537 emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
Yaxun Liu39195062017-08-04 18:16:31 +0000538 FailureOrder, Size, Order, Scope);
Tim Northovercadbbe12014-06-13 19:43:04 +0000539 CGF.Builder.CreateBr(ContBB);
540
541 CGF.Builder.SetInsertPoint(ContBB);
542 }
543 return;
544 }
John McCallfc207f22013-03-07 21:37:12 +0000545 case AtomicExpr::AO__c11_atomic_load:
Yaxun Liu39195062017-08-04 18:16:31 +0000546 case AtomicExpr::AO__opencl_atomic_load:
John McCallfc207f22013-03-07 21:37:12 +0000547 case AtomicExpr::AO__atomic_load_n:
548 case AtomicExpr::AO__atomic_load: {
549 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr);
Yaxun Liu39195062017-08-04 18:16:31 +0000550 Load->setAtomic(Order, Scope);
John McCallfc207f22013-03-07 21:37:12 +0000551 Load->setVolatile(E->isVolatile());
John McCall7f416cc2015-09-08 08:05:57 +0000552 CGF.Builder.CreateStore(Load, Dest);
John McCallfc207f22013-03-07 21:37:12 +0000553 return;
554 }
555
556 case AtomicExpr::AO__c11_atomic_store:
Yaxun Liu39195062017-08-04 18:16:31 +0000557 case AtomicExpr::AO__opencl_atomic_store:
John McCallfc207f22013-03-07 21:37:12 +0000558 case AtomicExpr::AO__atomic_store:
559 case AtomicExpr::AO__atomic_store_n: {
John McCall7f416cc2015-09-08 08:05:57 +0000560 llvm::Value *LoadVal1 = CGF.Builder.CreateLoad(Val1);
John McCallfc207f22013-03-07 21:37:12 +0000561 llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr);
Yaxun Liu39195062017-08-04 18:16:31 +0000562 Store->setAtomic(Order, Scope);
John McCallfc207f22013-03-07 21:37:12 +0000563 Store->setVolatile(E->isVolatile());
564 return;
565 }
566
567 case AtomicExpr::AO__c11_atomic_exchange:
Yaxun Liu39195062017-08-04 18:16:31 +0000568 case AtomicExpr::AO__opencl_atomic_exchange:
John McCallfc207f22013-03-07 21:37:12 +0000569 case AtomicExpr::AO__atomic_exchange_n:
570 case AtomicExpr::AO__atomic_exchange:
571 Op = llvm::AtomicRMWInst::Xchg;
572 break;
573
574 case AtomicExpr::AO__atomic_add_fetch:
575 PostOp = llvm::Instruction::Add;
576 // Fall through.
577 case AtomicExpr::AO__c11_atomic_fetch_add:
Yaxun Liu39195062017-08-04 18:16:31 +0000578 case AtomicExpr::AO__opencl_atomic_fetch_add:
John McCallfc207f22013-03-07 21:37:12 +0000579 case AtomicExpr::AO__atomic_fetch_add:
580 Op = llvm::AtomicRMWInst::Add;
581 break;
582
583 case AtomicExpr::AO__atomic_sub_fetch:
584 PostOp = llvm::Instruction::Sub;
585 // Fall through.
586 case AtomicExpr::AO__c11_atomic_fetch_sub:
Yaxun Liu39195062017-08-04 18:16:31 +0000587 case AtomicExpr::AO__opencl_atomic_fetch_sub:
John McCallfc207f22013-03-07 21:37:12 +0000588 case AtomicExpr::AO__atomic_fetch_sub:
589 Op = llvm::AtomicRMWInst::Sub;
590 break;
591
Yaxun Liu39195062017-08-04 18:16:31 +0000592 case AtomicExpr::AO__opencl_atomic_fetch_min:
593 Op = E->getValueType()->isSignedIntegerType() ? llvm::AtomicRMWInst::Min
594 : llvm::AtomicRMWInst::UMin;
595 break;
596
597 case AtomicExpr::AO__opencl_atomic_fetch_max:
598 Op = E->getValueType()->isSignedIntegerType() ? llvm::AtomicRMWInst::Max
599 : llvm::AtomicRMWInst::UMax;
600 break;
601
John McCallfc207f22013-03-07 21:37:12 +0000602 case AtomicExpr::AO__atomic_and_fetch:
603 PostOp = llvm::Instruction::And;
604 // Fall through.
605 case AtomicExpr::AO__c11_atomic_fetch_and:
Yaxun Liu39195062017-08-04 18:16:31 +0000606 case AtomicExpr::AO__opencl_atomic_fetch_and:
John McCallfc207f22013-03-07 21:37:12 +0000607 case AtomicExpr::AO__atomic_fetch_and:
608 Op = llvm::AtomicRMWInst::And;
609 break;
610
611 case AtomicExpr::AO__atomic_or_fetch:
612 PostOp = llvm::Instruction::Or;
613 // Fall through.
614 case AtomicExpr::AO__c11_atomic_fetch_or:
Yaxun Liu39195062017-08-04 18:16:31 +0000615 case AtomicExpr::AO__opencl_atomic_fetch_or:
John McCallfc207f22013-03-07 21:37:12 +0000616 case AtomicExpr::AO__atomic_fetch_or:
617 Op = llvm::AtomicRMWInst::Or;
618 break;
619
620 case AtomicExpr::AO__atomic_xor_fetch:
621 PostOp = llvm::Instruction::Xor;
622 // Fall through.
623 case AtomicExpr::AO__c11_atomic_fetch_xor:
Yaxun Liu39195062017-08-04 18:16:31 +0000624 case AtomicExpr::AO__opencl_atomic_fetch_xor:
John McCallfc207f22013-03-07 21:37:12 +0000625 case AtomicExpr::AO__atomic_fetch_xor:
626 Op = llvm::AtomicRMWInst::Xor;
627 break;
628
629 case AtomicExpr::AO__atomic_nand_fetch:
James Y Knight7aefb5b2015-11-12 18:37:29 +0000630 PostOp = llvm::Instruction::And; // the NOT is special cased below
631 // Fall through.
John McCallfc207f22013-03-07 21:37:12 +0000632 case AtomicExpr::AO__atomic_fetch_nand:
633 Op = llvm::AtomicRMWInst::Nand;
634 break;
635 }
636
John McCall7f416cc2015-09-08 08:05:57 +0000637 llvm::Value *LoadVal1 = CGF.Builder.CreateLoad(Val1);
John McCallfc207f22013-03-07 21:37:12 +0000638 llvm::AtomicRMWInst *RMWI =
Yaxun Liu39195062017-08-04 18:16:31 +0000639 CGF.Builder.CreateAtomicRMW(Op, Ptr.getPointer(), LoadVal1, Order, Scope);
John McCallfc207f22013-03-07 21:37:12 +0000640 RMWI->setVolatile(E->isVolatile());
641
642 // For __atomic_*_fetch operations, perform the operation again to
643 // determine the value which was written.
644 llvm::Value *Result = RMWI;
645 if (PostOp)
646 Result = CGF.Builder.CreateBinOp(PostOp, RMWI, LoadVal1);
647 if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch)
648 Result = CGF.Builder.CreateNot(Result);
John McCall7f416cc2015-09-08 08:05:57 +0000649 CGF.Builder.CreateStore(Result, Dest);
John McCallfc207f22013-03-07 21:37:12 +0000650}
651
652// This function emits any expression (scalar, complex, or aggregate)
653// into a temporary alloca.
John McCall7f416cc2015-09-08 08:05:57 +0000654static Address
John McCallfc207f22013-03-07 21:37:12 +0000655EmitValToTemp(CodeGenFunction &CGF, Expr *E) {
John McCall7f416cc2015-09-08 08:05:57 +0000656 Address DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp");
John McCallfc207f22013-03-07 21:37:12 +0000657 CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(),
658 /*Init*/ true);
659 return DeclPtr;
660}
661
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000662static void
663AddDirectArgument(CodeGenFunction &CGF, CallArgList &Args,
Nick Lewycky2d84e842013-10-02 02:29:49 +0000664 bool UseOptimizedLibcall, llvm::Value *Val, QualType ValTy,
David Majnemer0392cf82014-08-29 07:27:49 +0000665 SourceLocation Loc, CharUnits SizeInChars) {
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000666 if (UseOptimizedLibcall) {
667 // Load value and pass it to the function directly.
John McCall7f416cc2015-09-08 08:05:57 +0000668 CharUnits Align = CGF.getContext().getTypeAlignInChars(ValTy);
David Majnemer0392cf82014-08-29 07:27:49 +0000669 int64_t SizeInBits = CGF.getContext().toBits(SizeInChars);
670 ValTy =
671 CGF.getContext().getIntTypeForBitwidth(SizeInBits, /*Signed=*/false);
672 llvm::Type *IPtrTy = llvm::IntegerType::get(CGF.getLLVMContext(),
673 SizeInBits)->getPointerTo();
John McCall7f416cc2015-09-08 08:05:57 +0000674 Address Ptr = Address(CGF.Builder.CreateBitCast(Val, IPtrTy), Align);
675 Val = CGF.EmitLoadOfScalar(Ptr, false,
676 CGF.getContext().getPointerType(ValTy),
David Majnemer0392cf82014-08-29 07:27:49 +0000677 Loc);
678 // Coerce the value into an appropriately sized integer type.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000679 Args.add(RValue::get(Val), ValTy);
680 } else {
681 // Non-optimized functions always take a reference.
682 Args.add(RValue::get(CGF.EmitCastToVoidPtr(Val)),
683 CGF.getContext().VoidPtrTy);
684 }
685}
686
Tim Northovercc2a6e02015-11-09 19:56:35 +0000687RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
John McCallfc207f22013-03-07 21:37:12 +0000688 QualType AtomicTy = E->getPtr()->getType()->getPointeeType();
689 QualType MemTy = AtomicTy;
690 if (const AtomicType *AT = AtomicTy->getAs<AtomicType>())
691 MemTy = AT->getValueType();
John McCall7f416cc2015-09-08 08:05:57 +0000692 CharUnits sizeChars, alignChars;
693 std::tie(sizeChars, alignChars) = getContext().getTypeInfoInChars(AtomicTy);
John McCallfc207f22013-03-07 21:37:12 +0000694 uint64_t Size = sizeChars.getQuantity();
John McCall7f416cc2015-09-08 08:05:57 +0000695 unsigned MaxInlineWidthInBits = getTarget().getMaxAtomicInlineWidth();
696 bool UseLibcall = (sizeChars != alignChars ||
John McCallfc207f22013-03-07 21:37:12 +0000697 getContext().toBits(sizeChars) > MaxInlineWidthInBits);
698
John McCall7f416cc2015-09-08 08:05:57 +0000699 llvm::Value *IsWeak = nullptr, *OrderFail = nullptr;
700
701 Address Val1 = Address::invalid();
702 Address Val2 = Address::invalid();
Tim Northovercc2a6e02015-11-09 19:56:35 +0000703 Address Dest = Address::invalid();
John McCall7f416cc2015-09-08 08:05:57 +0000704 Address Ptr(EmitScalarExpr(E->getPtr()), alignChars);
John McCallfc207f22013-03-07 21:37:12 +0000705
Yaxun Liu39195062017-08-04 18:16:31 +0000706 if (E->getOp() == AtomicExpr::AO__c11_atomic_init ||
707 E->getOp() == AtomicExpr::AO__opencl_atomic_init) {
John McCall7f416cc2015-09-08 08:05:57 +0000708 LValue lvalue = MakeAddrLValue(Ptr, AtomicTy);
John McCalla8ec7eb2013-03-07 21:37:17 +0000709 EmitAtomicInit(E->getVal1(), lvalue);
Craig Topper8a13c412014-05-21 05:09:00 +0000710 return RValue::get(nullptr);
John McCallfc207f22013-03-07 21:37:12 +0000711 }
712
Craig Topper8a13c412014-05-21 05:09:00 +0000713 llvm::Value *Order = EmitScalarExpr(E->getOrder());
Yaxun Liu39195062017-08-04 18:16:31 +0000714 llvm::Value *Scope = EmitScalarExpr(E->getScope());
John McCallfc207f22013-03-07 21:37:12 +0000715
716 switch (E->getOp()) {
717 case AtomicExpr::AO__c11_atomic_init:
Yaxun Liu39195062017-08-04 18:16:31 +0000718 case AtomicExpr::AO__opencl_atomic_init:
James Y Knight81167fb2015-08-05 16:57:36 +0000719 llvm_unreachable("Already handled above with EmitAtomicInit!");
John McCallfc207f22013-03-07 21:37:12 +0000720
721 case AtomicExpr::AO__c11_atomic_load:
Yaxun Liu39195062017-08-04 18:16:31 +0000722 case AtomicExpr::AO__opencl_atomic_load:
John McCallfc207f22013-03-07 21:37:12 +0000723 case AtomicExpr::AO__atomic_load_n:
724 break;
725
726 case AtomicExpr::AO__atomic_load:
John McCall7f416cc2015-09-08 08:05:57 +0000727 Dest = EmitPointerWithAlignment(E->getVal1());
John McCallfc207f22013-03-07 21:37:12 +0000728 break;
729
730 case AtomicExpr::AO__atomic_store:
John McCall7f416cc2015-09-08 08:05:57 +0000731 Val1 = EmitPointerWithAlignment(E->getVal1());
John McCallfc207f22013-03-07 21:37:12 +0000732 break;
733
734 case AtomicExpr::AO__atomic_exchange:
John McCall7f416cc2015-09-08 08:05:57 +0000735 Val1 = EmitPointerWithAlignment(E->getVal1());
736 Dest = EmitPointerWithAlignment(E->getVal2());
John McCallfc207f22013-03-07 21:37:12 +0000737 break;
738
739 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
740 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
Yaxun Liu39195062017-08-04 18:16:31 +0000741 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
742 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
John McCallfc207f22013-03-07 21:37:12 +0000743 case AtomicExpr::AO__atomic_compare_exchange_n:
744 case AtomicExpr::AO__atomic_compare_exchange:
John McCall7f416cc2015-09-08 08:05:57 +0000745 Val1 = EmitPointerWithAlignment(E->getVal1());
John McCallfc207f22013-03-07 21:37:12 +0000746 if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange)
John McCall7f416cc2015-09-08 08:05:57 +0000747 Val2 = EmitPointerWithAlignment(E->getVal2());
John McCallfc207f22013-03-07 21:37:12 +0000748 else
749 Val2 = EmitValToTemp(*this, E->getVal2());
750 OrderFail = EmitScalarExpr(E->getOrderFail());
Yaxun Liu39195062017-08-04 18:16:31 +0000751 if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange_n ||
752 E->getOp() == AtomicExpr::AO__atomic_compare_exchange)
Tim Northovercadbbe12014-06-13 19:43:04 +0000753 IsWeak = EmitScalarExpr(E->getWeak());
John McCallfc207f22013-03-07 21:37:12 +0000754 break;
755
756 case AtomicExpr::AO__c11_atomic_fetch_add:
757 case AtomicExpr::AO__c11_atomic_fetch_sub:
Yaxun Liu39195062017-08-04 18:16:31 +0000758 case AtomicExpr::AO__opencl_atomic_fetch_add:
759 case AtomicExpr::AO__opencl_atomic_fetch_sub:
John McCallfc207f22013-03-07 21:37:12 +0000760 if (MemTy->isPointerType()) {
761 // For pointer arithmetic, we're required to do a bit of math:
762 // adding 1 to an int* is not the same as adding 1 to a uintptr_t.
763 // ... but only for the C11 builtins. The GNU builtins expect the
764 // user to multiply by sizeof(T).
765 QualType Val1Ty = E->getVal1()->getType();
766 llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1());
767 CharUnits PointeeIncAmt =
768 getContext().getTypeSizeInChars(MemTy->getPointeeType());
769 Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt));
John McCall7f416cc2015-09-08 08:05:57 +0000770 auto Temp = CreateMemTemp(Val1Ty, ".atomictmp");
771 Val1 = Temp;
772 EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Temp, Val1Ty));
John McCallfc207f22013-03-07 21:37:12 +0000773 break;
774 }
775 // Fall through.
776 case AtomicExpr::AO__atomic_fetch_add:
777 case AtomicExpr::AO__atomic_fetch_sub:
778 case AtomicExpr::AO__atomic_add_fetch:
779 case AtomicExpr::AO__atomic_sub_fetch:
780 case AtomicExpr::AO__c11_atomic_store:
781 case AtomicExpr::AO__c11_atomic_exchange:
Yaxun Liu39195062017-08-04 18:16:31 +0000782 case AtomicExpr::AO__opencl_atomic_store:
783 case AtomicExpr::AO__opencl_atomic_exchange:
John McCallfc207f22013-03-07 21:37:12 +0000784 case AtomicExpr::AO__atomic_store_n:
785 case AtomicExpr::AO__atomic_exchange_n:
786 case AtomicExpr::AO__c11_atomic_fetch_and:
787 case AtomicExpr::AO__c11_atomic_fetch_or:
788 case AtomicExpr::AO__c11_atomic_fetch_xor:
Yaxun Liu39195062017-08-04 18:16:31 +0000789 case AtomicExpr::AO__opencl_atomic_fetch_and:
790 case AtomicExpr::AO__opencl_atomic_fetch_or:
791 case AtomicExpr::AO__opencl_atomic_fetch_xor:
792 case AtomicExpr::AO__opencl_atomic_fetch_min:
793 case AtomicExpr::AO__opencl_atomic_fetch_max:
John McCallfc207f22013-03-07 21:37:12 +0000794 case AtomicExpr::AO__atomic_fetch_and:
795 case AtomicExpr::AO__atomic_fetch_or:
796 case AtomicExpr::AO__atomic_fetch_xor:
797 case AtomicExpr::AO__atomic_fetch_nand:
798 case AtomicExpr::AO__atomic_and_fetch:
799 case AtomicExpr::AO__atomic_or_fetch:
800 case AtomicExpr::AO__atomic_xor_fetch:
801 case AtomicExpr::AO__atomic_nand_fetch:
802 Val1 = EmitValToTemp(*this, E->getVal1());
803 break;
804 }
805
David Majnemeree8d04d2014-12-12 08:16:09 +0000806 QualType RValTy = E->getType().getUnqualifiedType();
807
Tim Northovercc2a6e02015-11-09 19:56:35 +0000808 // The inlined atomics only function on iN types, where N is a power of 2. We
809 // need to make sure (via temporaries if necessary) that all incoming values
810 // are compatible.
811 LValue AtomicVal = MakeAddrLValue(Ptr, AtomicTy);
812 AtomicInfo Atomics(*this, AtomicVal);
813
814 Ptr = Atomics.emitCastToAtomicIntPointer(Ptr);
815 if (Val1.isValid()) Val1 = Atomics.convertToAtomicIntPointer(Val1);
816 if (Val2.isValid()) Val2 = Atomics.convertToAtomicIntPointer(Val2);
817 if (Dest.isValid())
818 Dest = Atomics.emitCastToAtomicIntPointer(Dest);
819 else if (E->isCmpXChg())
820 Dest = CreateMemTemp(RValTy, "cmpxchg.bool");
821 else if (!RValTy->isVoidType())
822 Dest = Atomics.emitCastToAtomicIntPointer(Atomics.CreateTempAlloca());
John McCallfc207f22013-03-07 21:37:12 +0000823
824 // Use a library call. See: http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary .
825 if (UseLibcall) {
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000826 bool UseOptimizedLibcall = false;
827 switch (E->getOp()) {
James Y Knight81167fb2015-08-05 16:57:36 +0000828 case AtomicExpr::AO__c11_atomic_init:
Yaxun Liu39195062017-08-04 18:16:31 +0000829 case AtomicExpr::AO__opencl_atomic_init:
James Y Knight81167fb2015-08-05 16:57:36 +0000830 llvm_unreachable("Already handled above with EmitAtomicInit!");
831
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000832 case AtomicExpr::AO__c11_atomic_fetch_add:
Yaxun Liu39195062017-08-04 18:16:31 +0000833 case AtomicExpr::AO__opencl_atomic_fetch_add:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000834 case AtomicExpr::AO__atomic_fetch_add:
835 case AtomicExpr::AO__c11_atomic_fetch_and:
Yaxun Liu39195062017-08-04 18:16:31 +0000836 case AtomicExpr::AO__opencl_atomic_fetch_and:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000837 case AtomicExpr::AO__atomic_fetch_and:
838 case AtomicExpr::AO__c11_atomic_fetch_or:
Yaxun Liu39195062017-08-04 18:16:31 +0000839 case AtomicExpr::AO__opencl_atomic_fetch_or:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000840 case AtomicExpr::AO__atomic_fetch_or:
James Y Knight81167fb2015-08-05 16:57:36 +0000841 case AtomicExpr::AO__atomic_fetch_nand:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000842 case AtomicExpr::AO__c11_atomic_fetch_sub:
Yaxun Liu39195062017-08-04 18:16:31 +0000843 case AtomicExpr::AO__opencl_atomic_fetch_sub:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000844 case AtomicExpr::AO__atomic_fetch_sub:
845 case AtomicExpr::AO__c11_atomic_fetch_xor:
Yaxun Liu39195062017-08-04 18:16:31 +0000846 case AtomicExpr::AO__opencl_atomic_fetch_xor:
847 case AtomicExpr::AO__opencl_atomic_fetch_min:
848 case AtomicExpr::AO__opencl_atomic_fetch_max:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000849 case AtomicExpr::AO__atomic_fetch_xor:
James Y Knight81167fb2015-08-05 16:57:36 +0000850 case AtomicExpr::AO__atomic_add_fetch:
851 case AtomicExpr::AO__atomic_and_fetch:
852 case AtomicExpr::AO__atomic_nand_fetch:
853 case AtomicExpr::AO__atomic_or_fetch:
854 case AtomicExpr::AO__atomic_sub_fetch:
855 case AtomicExpr::AO__atomic_xor_fetch:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000856 // For these, only library calls for certain sizes exist.
857 UseOptimizedLibcall = true;
858 break;
James Y Knight81167fb2015-08-05 16:57:36 +0000859
860 case AtomicExpr::AO__c11_atomic_load:
861 case AtomicExpr::AO__c11_atomic_store:
862 case AtomicExpr::AO__c11_atomic_exchange:
863 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
864 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
Yaxun Liu39195062017-08-04 18:16:31 +0000865 case AtomicExpr::AO__opencl_atomic_load:
866 case AtomicExpr::AO__opencl_atomic_store:
867 case AtomicExpr::AO__opencl_atomic_exchange:
868 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
869 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
James Y Knight81167fb2015-08-05 16:57:36 +0000870 case AtomicExpr::AO__atomic_load_n:
871 case AtomicExpr::AO__atomic_load:
872 case AtomicExpr::AO__atomic_store_n:
873 case AtomicExpr::AO__atomic_store:
874 case AtomicExpr::AO__atomic_exchange_n:
875 case AtomicExpr::AO__atomic_exchange:
876 case AtomicExpr::AO__atomic_compare_exchange_n:
877 case AtomicExpr::AO__atomic_compare_exchange:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000878 // Only use optimized library calls for sizes for which they exist.
879 if (Size == 1 || Size == 2 || Size == 4 || Size == 8)
880 UseOptimizedLibcall = true;
881 break;
882 }
John McCallfc207f22013-03-07 21:37:12 +0000883
John McCallfc207f22013-03-07 21:37:12 +0000884 CallArgList Args;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000885 if (!UseOptimizedLibcall) {
886 // For non-optimized library calls, the size is the first parameter
887 Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)),
888 getContext().getSizeType());
889 }
890 // Atomic address is the first or second parameter
Yaxun Liu39195062017-08-04 18:16:31 +0000891 // The OpenCL atomic library functions only accept pointer arguments to
892 // generic address space.
893 auto CastToGenericAddrSpace = [&](llvm::Value *V, QualType PT) {
894 if (!E->isOpenCL())
895 return V;
896 auto AS = PT->getAs<PointerType>()->getPointeeType().getAddressSpace();
897 if (AS == LangAS::opencl_generic)
898 return V;
899 auto DestAS = getContext().getTargetAddressSpace(LangAS::opencl_generic);
900 auto T = V->getType();
901 auto *DestType = T->getPointerElementType()->getPointerTo(DestAS);
902
903 return getTargetHooks().performAddrSpaceCast(
904 *this, V, AS, LangAS::opencl_generic, DestType, false);
905 };
906
907 Args.add(RValue::get(CastToGenericAddrSpace(
908 EmitCastToVoidPtr(Ptr.getPointer()), E->getPtr()->getType())),
John McCall7f416cc2015-09-08 08:05:57 +0000909 getContext().VoidPtrTy);
John McCallfc207f22013-03-07 21:37:12 +0000910
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000911 std::string LibCallName;
Logan Chien74798a32014-03-26 17:35:01 +0000912 QualType LoweredMemTy =
913 MemTy->isPointerType() ? getContext().getIntPtrType() : MemTy;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000914 QualType RetTy;
915 bool HaveRetTy = false;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000916 llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0;
John McCallfc207f22013-03-07 21:37:12 +0000917 switch (E->getOp()) {
James Y Knight81167fb2015-08-05 16:57:36 +0000918 case AtomicExpr::AO__c11_atomic_init:
Yaxun Liu39195062017-08-04 18:16:31 +0000919 case AtomicExpr::AO__opencl_atomic_init:
James Y Knight81167fb2015-08-05 16:57:36 +0000920 llvm_unreachable("Already handled!");
921
John McCallfc207f22013-03-07 21:37:12 +0000922 // There is only one libcall for compare an exchange, because there is no
923 // optimisation benefit possible from a libcall version of a weak compare
924 // and exchange.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000925 // bool __atomic_compare_exchange(size_t size, void *mem, void *expected,
John McCallfc207f22013-03-07 21:37:12 +0000926 // void *desired, int success, int failure)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000927 // bool __atomic_compare_exchange_N(T *mem, T *expected, T desired,
928 // int success, int failure)
John McCallfc207f22013-03-07 21:37:12 +0000929 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
930 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
Yaxun Liu39195062017-08-04 18:16:31 +0000931 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
932 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
John McCallfc207f22013-03-07 21:37:12 +0000933 case AtomicExpr::AO__atomic_compare_exchange:
934 case AtomicExpr::AO__atomic_compare_exchange_n:
935 LibCallName = "__atomic_compare_exchange";
936 RetTy = getContext().BoolTy;
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000937 HaveRetTy = true;
Yaxun Liu39195062017-08-04 18:16:31 +0000938 Args.add(
939 RValue::get(CastToGenericAddrSpace(
940 EmitCastToVoidPtr(Val1.getPointer()), E->getVal1()->getType())),
941 getContext().VoidPtrTy);
John McCall7f416cc2015-09-08 08:05:57 +0000942 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val2.getPointer(),
943 MemTy, E->getExprLoc(), sizeChars);
Nick Lewycky5fa40c32013-10-01 21:51:38 +0000944 Args.add(RValue::get(Order), getContext().IntTy);
John McCallfc207f22013-03-07 21:37:12 +0000945 Order = OrderFail;
946 break;
947 // void __atomic_exchange(size_t size, void *mem, void *val, void *return,
948 // int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000949 // T __atomic_exchange_N(T *mem, T val, int order)
John McCallfc207f22013-03-07 21:37:12 +0000950 case AtomicExpr::AO__c11_atomic_exchange:
Yaxun Liu39195062017-08-04 18:16:31 +0000951 case AtomicExpr::AO__opencl_atomic_exchange:
John McCallfc207f22013-03-07 21:37:12 +0000952 case AtomicExpr::AO__atomic_exchange_n:
953 case AtomicExpr::AO__atomic_exchange:
954 LibCallName = "__atomic_exchange";
John McCall7f416cc2015-09-08 08:05:57 +0000955 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
956 MemTy, E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000957 break;
958 // void __atomic_store(size_t size, void *mem, void *val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000959 // void __atomic_store_N(T *mem, T val, int order)
John McCallfc207f22013-03-07 21:37:12 +0000960 case AtomicExpr::AO__c11_atomic_store:
Yaxun Liu39195062017-08-04 18:16:31 +0000961 case AtomicExpr::AO__opencl_atomic_store:
John McCallfc207f22013-03-07 21:37:12 +0000962 case AtomicExpr::AO__atomic_store:
963 case AtomicExpr::AO__atomic_store_n:
964 LibCallName = "__atomic_store";
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000965 RetTy = getContext().VoidTy;
966 HaveRetTy = true;
John McCall7f416cc2015-09-08 08:05:57 +0000967 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
968 MemTy, E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +0000969 break;
970 // void __atomic_load(size_t size, void *mem, void *return, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000971 // T __atomic_load_N(T *mem, int order)
John McCallfc207f22013-03-07 21:37:12 +0000972 case AtomicExpr::AO__c11_atomic_load:
Yaxun Liu39195062017-08-04 18:16:31 +0000973 case AtomicExpr::AO__opencl_atomic_load:
John McCallfc207f22013-03-07 21:37:12 +0000974 case AtomicExpr::AO__atomic_load:
975 case AtomicExpr::AO__atomic_load_n:
976 LibCallName = "__atomic_load";
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000977 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000978 // T __atomic_add_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000979 // T __atomic_fetch_add_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +0000980 case AtomicExpr::AO__atomic_add_fetch:
981 PostOp = llvm::Instruction::Add;
982 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000983 case AtomicExpr::AO__c11_atomic_fetch_add:
Yaxun Liu39195062017-08-04 18:16:31 +0000984 case AtomicExpr::AO__opencl_atomic_fetch_add:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000985 case AtomicExpr::AO__atomic_fetch_add:
986 LibCallName = "__atomic_fetch_add";
John McCall7f416cc2015-09-08 08:05:57 +0000987 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
988 LoweredMemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000989 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +0000990 // T __atomic_and_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000991 // T __atomic_fetch_and_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +0000992 case AtomicExpr::AO__atomic_and_fetch:
993 PostOp = llvm::Instruction::And;
994 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000995 case AtomicExpr::AO__c11_atomic_fetch_and:
Yaxun Liu39195062017-08-04 18:16:31 +0000996 case AtomicExpr::AO__opencl_atomic_fetch_and:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +0000997 case AtomicExpr::AO__atomic_fetch_and:
998 LibCallName = "__atomic_fetch_and";
John McCall7f416cc2015-09-08 08:05:57 +0000999 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
1000 MemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001001 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +00001002 // T __atomic_or_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001003 // T __atomic_fetch_or_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +00001004 case AtomicExpr::AO__atomic_or_fetch:
1005 PostOp = llvm::Instruction::Or;
1006 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001007 case AtomicExpr::AO__c11_atomic_fetch_or:
Yaxun Liu39195062017-08-04 18:16:31 +00001008 case AtomicExpr::AO__opencl_atomic_fetch_or:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001009 case AtomicExpr::AO__atomic_fetch_or:
1010 LibCallName = "__atomic_fetch_or";
John McCall7f416cc2015-09-08 08:05:57 +00001011 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
1012 MemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001013 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +00001014 // T __atomic_sub_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001015 // T __atomic_fetch_sub_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +00001016 case AtomicExpr::AO__atomic_sub_fetch:
1017 PostOp = llvm::Instruction::Sub;
1018 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001019 case AtomicExpr::AO__c11_atomic_fetch_sub:
Yaxun Liu39195062017-08-04 18:16:31 +00001020 case AtomicExpr::AO__opencl_atomic_fetch_sub:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001021 case AtomicExpr::AO__atomic_fetch_sub:
1022 LibCallName = "__atomic_fetch_sub";
John McCall7f416cc2015-09-08 08:05:57 +00001023 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
1024 LoweredMemTy, E->getExprLoc(), sizeChars);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001025 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +00001026 // T __atomic_xor_fetch_N(T *mem, T val, int order)
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001027 // T __atomic_fetch_xor_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +00001028 case AtomicExpr::AO__atomic_xor_fetch:
1029 PostOp = llvm::Instruction::Xor;
1030 // Fall through.
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001031 case AtomicExpr::AO__c11_atomic_fetch_xor:
Yaxun Liu39195062017-08-04 18:16:31 +00001032 case AtomicExpr::AO__opencl_atomic_fetch_xor:
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001033 case AtomicExpr::AO__atomic_fetch_xor:
1034 LibCallName = "__atomic_fetch_xor";
John McCall7f416cc2015-09-08 08:05:57 +00001035 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
1036 MemTy, E->getExprLoc(), sizeChars);
John McCallfc207f22013-03-07 21:37:12 +00001037 break;
Yaxun Liu39195062017-08-04 18:16:31 +00001038 case AtomicExpr::AO__opencl_atomic_fetch_min:
1039 LibCallName = E->getValueType()->isSignedIntegerType()
1040 ? "__atomic_fetch_min"
1041 : "__atomic_fetch_umin";
1042 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
1043 LoweredMemTy, E->getExprLoc(), sizeChars);
1044 break;
1045 case AtomicExpr::AO__opencl_atomic_fetch_max:
1046 LibCallName = E->getValueType()->isSignedIntegerType()
1047 ? "__atomic_fetch_max"
1048 : "__atomic_fetch_umax";
1049 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
1050 LoweredMemTy, E->getExprLoc(), sizeChars);
1051 break;
James Y Knight7aefb5b2015-11-12 18:37:29 +00001052 // T __atomic_nand_fetch_N(T *mem, T val, int order)
James Y Knight81167fb2015-08-05 16:57:36 +00001053 // T __atomic_fetch_nand_N(T *mem, T val, int order)
James Y Knight7aefb5b2015-11-12 18:37:29 +00001054 case AtomicExpr::AO__atomic_nand_fetch:
1055 PostOp = llvm::Instruction::And; // the NOT is special cased below
1056 // Fall through.
James Y Knight81167fb2015-08-05 16:57:36 +00001057 case AtomicExpr::AO__atomic_fetch_nand:
1058 LibCallName = "__atomic_fetch_nand";
John McCall7f416cc2015-09-08 08:05:57 +00001059 AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
1060 MemTy, E->getExprLoc(), sizeChars);
James Y Knight81167fb2015-08-05 16:57:36 +00001061 break;
John McCallfc207f22013-03-07 21:37:12 +00001062 }
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001063
Yaxun Liu39195062017-08-04 18:16:31 +00001064 if (E->isOpenCL()) {
1065 LibCallName = std::string("__opencl") +
1066 StringRef(LibCallName).drop_front(1).str();
1067
1068 }
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001069 // Optimized functions have the size in their name.
1070 if (UseOptimizedLibcall)
1071 LibCallName += "_" + llvm::utostr(Size);
1072 // By default, assume we return a value of the atomic type.
1073 if (!HaveRetTy) {
1074 if (UseOptimizedLibcall) {
1075 // Value is returned directly.
David Majnemer0392cf82014-08-29 07:27:49 +00001076 // The function returns an appropriately sized integer type.
1077 RetTy = getContext().getIntTypeForBitwidth(
1078 getContext().toBits(sizeChars), /*Signed=*/false);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001079 } else {
1080 // Value is returned through parameter before the order.
1081 RetTy = getContext().VoidTy;
John McCall7f416cc2015-09-08 08:05:57 +00001082 Args.add(RValue::get(EmitCastToVoidPtr(Dest.getPointer())),
1083 getContext().VoidPtrTy);
Ed Schoutenc7e82bd2013-05-31 19:27:59 +00001084 }
1085 }
John McCallfc207f22013-03-07 21:37:12 +00001086 // order is always the last parameter
1087 Args.add(RValue::get(Order),
1088 getContext().IntTy);
Yaxun Liu39195062017-08-04 18:16:31 +00001089 if (E->isOpenCL())
1090 Args.add(RValue::get(Scope), getContext().IntTy);
John McCallfc207f22013-03-07 21:37:12 +00001091
James Y Knight7aefb5b2015-11-12 18:37:29 +00001092 // PostOp is only needed for the atomic_*_fetch operations, and
1093 // thus is only needed for and implemented in the
1094 // UseOptimizedLibcall codepath.
1095 assert(UseOptimizedLibcall || !PostOp);
1096
David Majnemer659be552014-11-25 23:44:32 +00001097 RValue Res = emitAtomicLibcall(*this, LibCallName, RetTy, Args);
1098 // The value is returned directly from the libcall.
Tim Northovercc2a6e02015-11-09 19:56:35 +00001099 if (E->isCmpXChg())
David Majnemer659be552014-11-25 23:44:32 +00001100 return Res;
Tim Northovercc2a6e02015-11-09 19:56:35 +00001101
1102 // The value is returned directly for optimized libcalls but the expr
1103 // provided an out-param.
1104 if (UseOptimizedLibcall && Res.getScalarVal()) {
David Majnemer659be552014-11-25 23:44:32 +00001105 llvm::Value *ResVal = Res.getScalarVal();
James Y Knight7aefb5b2015-11-12 18:37:29 +00001106 if (PostOp) {
1107 llvm::Value *LoadVal1 = Args[1].RV.getScalarVal();
1108 ResVal = Builder.CreateBinOp(PostOp, ResVal, LoadVal1);
1109 }
1110 if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch)
1111 ResVal = Builder.CreateNot(ResVal);
1112
Tim Northovercc2a6e02015-11-09 19:56:35 +00001113 Builder.CreateStore(
1114 ResVal,
1115 Builder.CreateBitCast(Dest, ResVal->getType()->getPointerTo()));
David Majnemer659be552014-11-25 23:44:32 +00001116 }
Tim Northovercc2a6e02015-11-09 19:56:35 +00001117
1118 if (RValTy->isVoidType())
1119 return RValue::get(nullptr);
1120
1121 return convertTempToRValue(
1122 Builder.CreateBitCast(Dest, ConvertTypeForMem(RValTy)->getPointerTo()),
1123 RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001124 }
1125
1126 bool IsStore = E->getOp() == AtomicExpr::AO__c11_atomic_store ||
Yaxun Liu39195062017-08-04 18:16:31 +00001127 E->getOp() == AtomicExpr::AO__opencl_atomic_store ||
John McCallfc207f22013-03-07 21:37:12 +00001128 E->getOp() == AtomicExpr::AO__atomic_store ||
1129 E->getOp() == AtomicExpr::AO__atomic_store_n;
1130 bool IsLoad = E->getOp() == AtomicExpr::AO__c11_atomic_load ||
Yaxun Liu39195062017-08-04 18:16:31 +00001131 E->getOp() == AtomicExpr::AO__opencl_atomic_load ||
John McCallfc207f22013-03-07 21:37:12 +00001132 E->getOp() == AtomicExpr::AO__atomic_load ||
1133 E->getOp() == AtomicExpr::AO__atomic_load_n;
1134
Yaxun Liu39195062017-08-04 18:16:31 +00001135 assert(isa<llvm::ConstantInt>(Scope) &&
1136 "Non-constant synchronization scope not supported");
1137 auto SCID = getTargetHooks().getLLVMSyncScopeID(
1138 static_cast<SyncScope>(cast<llvm::ConstantInt>(Scope)->getZExtValue()),
1139 getLLVMContext());
1140
John McCallfc207f22013-03-07 21:37:12 +00001141 if (isa<llvm::ConstantInt>(Order)) {
JF Bastiendda2cb12016-04-18 18:01:49 +00001142 auto ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
1143 // We should not ever get to a case where the ordering isn't a valid C ABI
1144 // value, but it's hard to enforce that in general.
1145 if (llvm::isValidAtomicOrderingCABI(ord))
1146 switch ((llvm::AtomicOrderingCABI)ord) {
1147 case llvm::AtomicOrderingCABI::relaxed:
1148 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
Yaxun Liu39195062017-08-04 18:16:31 +00001149 llvm::AtomicOrdering::Monotonic, SCID);
JF Bastiendda2cb12016-04-18 18:01:49 +00001150 break;
1151 case llvm::AtomicOrderingCABI::consume:
1152 case llvm::AtomicOrderingCABI::acquire:
1153 if (IsStore)
1154 break; // Avoid crashing on code with undefined behavior
1155 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
Yaxun Liu39195062017-08-04 18:16:31 +00001156 llvm::AtomicOrdering::Acquire, SCID);
JF Bastiendda2cb12016-04-18 18:01:49 +00001157 break;
1158 case llvm::AtomicOrderingCABI::release:
1159 if (IsLoad)
1160 break; // Avoid crashing on code with undefined behavior
1161 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
Yaxun Liu39195062017-08-04 18:16:31 +00001162 llvm::AtomicOrdering::Release, SCID);
JF Bastiendda2cb12016-04-18 18:01:49 +00001163 break;
1164 case llvm::AtomicOrderingCABI::acq_rel:
1165 if (IsLoad || IsStore)
1166 break; // Avoid crashing on code with undefined behavior
1167 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
Yaxun Liu39195062017-08-04 18:16:31 +00001168 llvm::AtomicOrdering::AcquireRelease, SCID);
JF Bastiendda2cb12016-04-18 18:01:49 +00001169 break;
1170 case llvm::AtomicOrderingCABI::seq_cst:
1171 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
Yaxun Liu39195062017-08-04 18:16:31 +00001172 llvm::AtomicOrdering::SequentiallyConsistent, SCID);
JF Bastiendda2cb12016-04-18 18:01:49 +00001173 break;
1174 }
David Majnemeree8d04d2014-12-12 08:16:09 +00001175 if (RValTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +00001176 return RValue::get(nullptr);
Tim Northovercc2a6e02015-11-09 19:56:35 +00001177
1178 return convertTempToRValue(
1179 Builder.CreateBitCast(Dest, ConvertTypeForMem(RValTy)->getPointerTo()),
1180 RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001181 }
1182
1183 // Long case, when Order isn't obviously constant.
1184
1185 // Create all the relevant BB's
Craig Topper8a13c412014-05-21 05:09:00 +00001186 llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
1187 *ReleaseBB = nullptr, *AcqRelBB = nullptr,
1188 *SeqCstBB = nullptr;
John McCallfc207f22013-03-07 21:37:12 +00001189 MonotonicBB = createBasicBlock("monotonic", CurFn);
1190 if (!IsStore)
1191 AcquireBB = createBasicBlock("acquire", CurFn);
1192 if (!IsLoad)
1193 ReleaseBB = createBasicBlock("release", CurFn);
1194 if (!IsLoad && !IsStore)
1195 AcqRelBB = createBasicBlock("acqrel", CurFn);
1196 SeqCstBB = createBasicBlock("seqcst", CurFn);
1197 llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
1198
1199 // Create the switch for the split
1200 // MonotonicBB is arbitrarily chosen as the default case; in practice, this
1201 // doesn't matter unless someone is crazy enough to use something that
1202 // doesn't fold to a constant for the ordering.
1203 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
1204 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB);
1205
1206 // Emit all the different atomics
1207 Builder.SetInsertPoint(MonotonicBB);
Yaxun Liu39195062017-08-04 18:16:31 +00001208 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
1209 llvm::AtomicOrdering::Monotonic, SCID);
John McCallfc207f22013-03-07 21:37:12 +00001210 Builder.CreateBr(ContBB);
1211 if (!IsStore) {
1212 Builder.SetInsertPoint(AcquireBB);
Yaxun Liu39195062017-08-04 18:16:31 +00001213 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
1214 llvm::AtomicOrdering::Acquire, SCID);
John McCallfc207f22013-03-07 21:37:12 +00001215 Builder.CreateBr(ContBB);
JF Bastiendda2cb12016-04-18 18:01:49 +00001216 SI->addCase(Builder.getInt32((int)llvm::AtomicOrderingCABI::consume),
Tim Northover514fc612014-03-13 19:25:52 +00001217 AcquireBB);
JF Bastiendda2cb12016-04-18 18:01:49 +00001218 SI->addCase(Builder.getInt32((int)llvm::AtomicOrderingCABI::acquire),
Tim Northover514fc612014-03-13 19:25:52 +00001219 AcquireBB);
John McCallfc207f22013-03-07 21:37:12 +00001220 }
1221 if (!IsLoad) {
1222 Builder.SetInsertPoint(ReleaseBB);
Yaxun Liu39195062017-08-04 18:16:31 +00001223 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
1224 llvm::AtomicOrdering::Release, SCID);
John McCallfc207f22013-03-07 21:37:12 +00001225 Builder.CreateBr(ContBB);
JF Bastiendda2cb12016-04-18 18:01:49 +00001226 SI->addCase(Builder.getInt32((int)llvm::AtomicOrderingCABI::release),
Tim Northover514fc612014-03-13 19:25:52 +00001227 ReleaseBB);
John McCallfc207f22013-03-07 21:37:12 +00001228 }
1229 if (!IsLoad && !IsStore) {
1230 Builder.SetInsertPoint(AcqRelBB);
Yaxun Liu39195062017-08-04 18:16:31 +00001231 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
1232 llvm::AtomicOrdering::AcquireRelease, SCID);
John McCallfc207f22013-03-07 21:37:12 +00001233 Builder.CreateBr(ContBB);
JF Bastiendda2cb12016-04-18 18:01:49 +00001234 SI->addCase(Builder.getInt32((int)llvm::AtomicOrderingCABI::acq_rel),
Tim Northover514fc612014-03-13 19:25:52 +00001235 AcqRelBB);
John McCallfc207f22013-03-07 21:37:12 +00001236 }
1237 Builder.SetInsertPoint(SeqCstBB);
Yaxun Liu39195062017-08-04 18:16:31 +00001238 EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail, Size,
1239 llvm::AtomicOrdering::SequentiallyConsistent, SCID);
John McCallfc207f22013-03-07 21:37:12 +00001240 Builder.CreateBr(ContBB);
JF Bastiendda2cb12016-04-18 18:01:49 +00001241 SI->addCase(Builder.getInt32((int)llvm::AtomicOrderingCABI::seq_cst),
Tim Northover514fc612014-03-13 19:25:52 +00001242 SeqCstBB);
John McCallfc207f22013-03-07 21:37:12 +00001243
1244 // Cleanup and return
1245 Builder.SetInsertPoint(ContBB);
David Majnemeree8d04d2014-12-12 08:16:09 +00001246 if (RValTy->isVoidType())
Craig Topper8a13c412014-05-21 05:09:00 +00001247 return RValue::get(nullptr);
Tim Northovercc2a6e02015-11-09 19:56:35 +00001248
1249 assert(Atomics.getValueSizeInBits() <= Atomics.getAtomicSizeInBits());
1250 return convertTempToRValue(
1251 Builder.CreateBitCast(Dest, ConvertTypeForMem(RValTy)->getPointerTo()),
1252 RValTy, E->getExprLoc());
John McCallfc207f22013-03-07 21:37:12 +00001253}
John McCalla8ec7eb2013-03-07 21:37:17 +00001254
John McCall7f416cc2015-09-08 08:05:57 +00001255Address AtomicInfo::emitCastToAtomicIntPointer(Address addr) const {
John McCalla8ec7eb2013-03-07 21:37:17 +00001256 unsigned addrspace =
John McCall7f416cc2015-09-08 08:05:57 +00001257 cast<llvm::PointerType>(addr.getPointer()->getType())->getAddressSpace();
John McCalla8ec7eb2013-03-07 21:37:17 +00001258 llvm::IntegerType *ty =
1259 llvm::IntegerType::get(CGF.getLLVMContext(), AtomicSizeInBits);
1260 return CGF.Builder.CreateBitCast(addr, ty->getPointerTo(addrspace));
1261}
1262
Tim Northovercc2a6e02015-11-09 19:56:35 +00001263Address AtomicInfo::convertToAtomicIntPointer(Address Addr) const {
1264 llvm::Type *Ty = Addr.getElementType();
1265 uint64_t SourceSizeInBits = CGF.CGM.getDataLayout().getTypeSizeInBits(Ty);
1266 if (SourceSizeInBits != AtomicSizeInBits) {
1267 Address Tmp = CreateTempAlloca();
1268 CGF.Builder.CreateMemCpy(Tmp, Addr,
1269 std::min(AtomicSizeInBits, SourceSizeInBits) / 8);
1270 Addr = Tmp;
1271 }
1272
1273 return emitCastToAtomicIntPointer(Addr);
1274}
1275
John McCall7f416cc2015-09-08 08:05:57 +00001276RValue AtomicInfo::convertAtomicTempToRValue(Address addr,
1277 AggValueSlot resultSlot,
1278 SourceLocation loc,
1279 bool asValue) const {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001280 if (LVal.isSimple()) {
1281 if (EvaluationKind == TEK_Aggregate)
1282 return resultSlot.asRValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001283
Alexey Bataevb57056f2015-01-22 06:17:56 +00001284 // Drill into the padding structure if we have one.
1285 if (hasPadding())
John McCall7f416cc2015-09-08 08:05:57 +00001286 addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
John McCalla8ec7eb2013-03-07 21:37:17 +00001287
Alexey Bataevb57056f2015-01-22 06:17:56 +00001288 // Otherwise, just convert the temporary to an r-value using the
1289 // normal conversion routine.
1290 return CGF.convertTempToRValue(addr, getValueType(), loc);
David Blaikie1ed728c2015-04-05 22:45:47 +00001291 }
John McCall7f416cc2015-09-08 08:05:57 +00001292 if (!asValue)
Alexey Bataevb8329262015-02-27 06:33:30 +00001293 // Get RValue from temp memory as atomic for non-simple lvalues
John McCall7f416cc2015-09-08 08:05:57 +00001294 return RValue::get(CGF.Builder.CreateLoad(addr));
David Blaikie1ed728c2015-04-05 22:45:47 +00001295 if (LVal.isBitField())
John McCall7f416cc2015-09-08 08:05:57 +00001296 return CGF.EmitLoadOfBitfieldLValue(
1297 LValue::MakeBitfield(addr, LVal.getBitFieldInfo(), LVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001298 LVal.getBaseInfo()), loc);
David Blaikie1ed728c2015-04-05 22:45:47 +00001299 if (LVal.isVectorElt())
John McCall7f416cc2015-09-08 08:05:57 +00001300 return CGF.EmitLoadOfLValue(
1301 LValue::MakeVectorElt(addr, LVal.getVectorIdx(), LVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001302 LVal.getBaseInfo()), loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001303 assert(LVal.isExtVectorElt());
1304 return CGF.EmitLoadOfExtVectorElementLValue(LValue::MakeExtVectorElt(
John McCall7f416cc2015-09-08 08:05:57 +00001305 addr, LVal.getExtVectorElts(), LVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001306 LVal.getBaseInfo()));
John McCalla8ec7eb2013-03-07 21:37:17 +00001307}
1308
Alexey Bataevb8329262015-02-27 06:33:30 +00001309RValue AtomicInfo::ConvertIntToValueOrAtomic(llvm::Value *IntVal,
1310 AggValueSlot ResultSlot,
1311 SourceLocation Loc,
1312 bool AsValue) const {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001313 // Try not to in some easy cases.
1314 assert(IntVal->getType()->isIntegerTy() && "Expected integer value");
Alexey Bataevb8329262015-02-27 06:33:30 +00001315 if (getEvaluationKind() == TEK_Scalar &&
1316 (((!LVal.isBitField() ||
1317 LVal.getBitFieldInfo().Size == ValueSizeInBits) &&
1318 !hasPadding()) ||
1319 !AsValue)) {
1320 auto *ValTy = AsValue
1321 ? CGF.ConvertTypeForMem(ValueTy)
John McCall7f416cc2015-09-08 08:05:57 +00001322 : getAtomicAddress().getType()->getPointerElementType();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001323 if (ValTy->isIntegerTy()) {
1324 assert(IntVal->getType() == ValTy && "Different integer types.");
David Majnemereeaec262015-02-14 02:18:14 +00001325 return RValue::get(CGF.EmitFromMemory(IntVal, ValueTy));
Alexey Bataev452d8e12014-12-15 05:25:25 +00001326 } else if (ValTy->isPointerTy())
1327 return RValue::get(CGF.Builder.CreateIntToPtr(IntVal, ValTy));
1328 else if (llvm::CastInst::isBitCastable(IntVal->getType(), ValTy))
1329 return RValue::get(CGF.Builder.CreateBitCast(IntVal, ValTy));
1330 }
1331
1332 // Create a temporary. This needs to be big enough to hold the
1333 // atomic integer.
John McCall7f416cc2015-09-08 08:05:57 +00001334 Address Temp = Address::invalid();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001335 bool TempIsVolatile = false;
Alexey Bataevb8329262015-02-27 06:33:30 +00001336 if (AsValue && getEvaluationKind() == TEK_Aggregate) {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001337 assert(!ResultSlot.isIgnored());
John McCall7f416cc2015-09-08 08:05:57 +00001338 Temp = ResultSlot.getAddress();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001339 TempIsVolatile = ResultSlot.isVolatile();
1340 } else {
Alexey Bataevb8329262015-02-27 06:33:30 +00001341 Temp = CreateTempAlloca();
Alexey Bataev452d8e12014-12-15 05:25:25 +00001342 }
1343
1344 // Slam the integer into the temporary.
John McCall7f416cc2015-09-08 08:05:57 +00001345 Address CastTemp = emitCastToAtomicIntPointer(Temp);
1346 CGF.Builder.CreateStore(IntVal, CastTemp)
Alexey Bataev452d8e12014-12-15 05:25:25 +00001347 ->setVolatile(TempIsVolatile);
1348
John McCall7f416cc2015-09-08 08:05:57 +00001349 return convertAtomicTempToRValue(Temp, ResultSlot, Loc, AsValue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001350}
1351
1352void AtomicInfo::EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
1353 llvm::AtomicOrdering AO, bool) {
1354 // void __atomic_load(size_t size, void *mem, void *return, int order);
1355 CallArgList Args;
1356 Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
John McCall7f416cc2015-09-08 08:05:57 +00001357 Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
Alexey Bataevb8329262015-02-27 06:33:30 +00001358 CGF.getContext().VoidPtrTy);
1359 Args.add(RValue::get(CGF.EmitCastToVoidPtr(AddForLoaded)),
1360 CGF.getContext().VoidPtrTy);
JF Bastiendda2cb12016-04-18 18:01:49 +00001361 Args.add(
1362 RValue::get(llvm::ConstantInt::get(CGF.IntTy, (int)llvm::toCABI(AO))),
1363 CGF.getContext().IntTy);
Alexey Bataevb8329262015-02-27 06:33:30 +00001364 emitAtomicLibcall(CGF, "__atomic_load", CGF.getContext().VoidTy, Args);
1365}
1366
1367llvm::Value *AtomicInfo::EmitAtomicLoadOp(llvm::AtomicOrdering AO,
1368 bool IsVolatile) {
1369 // Okay, we're doing this natively.
John McCall7f416cc2015-09-08 08:05:57 +00001370 Address Addr = getAtomicAddressAsAtomicIntPointer();
Alexey Bataevb8329262015-02-27 06:33:30 +00001371 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Addr, "atomic-load");
1372 Load->setAtomic(AO);
1373
1374 // Other decoration.
Alexey Bataevb8329262015-02-27 06:33:30 +00001375 if (IsVolatile)
1376 Load->setVolatile(true);
1377 if (LVal.getTBAAInfo())
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001378 CGF.CGM.DecorateInstructionWithTBAA(Load, LVal.getTBAAInfo());
Alexey Bataevb8329262015-02-27 06:33:30 +00001379 return Load;
Alexey Bataev452d8e12014-12-15 05:25:25 +00001380}
1381
David Majnemera5b195a2015-02-14 01:35:12 +00001382/// An LValue is a candidate for having its loads and stores be made atomic if
1383/// we are operating under /volatile:ms *and* the LValue itself is volatile and
1384/// performing such an operation can be performed without a libcall.
1385bool CodeGenFunction::LValueIsSuitableForInlineAtomic(LValue LV) {
John McCall7f416cc2015-09-08 08:05:57 +00001386 if (!CGM.getCodeGenOpts().MSVolatile) return false;
David Majnemera5b195a2015-02-14 01:35:12 +00001387 AtomicInfo AI(*this, LV);
1388 bool IsVolatile = LV.isVolatile() || hasVolatileMember(LV.getType());
1389 // An atomic is inline if we don't need to use a libcall.
1390 bool AtomicIsInline = !AI.shouldUseLibcall();
David Majnemerfc80b6e2016-01-22 16:36:44 +00001391 // MSVC doesn't seem to do this for types wider than a pointer.
David Majnemera38c9f12016-05-24 16:09:25 +00001392 if (getContext().getTypeSize(LV.getType()) >
David Majnemerfc80b6e2016-01-22 16:36:44 +00001393 getContext().getTypeSize(getContext().getIntPtrType()))
1394 return false;
David Majnemera38c9f12016-05-24 16:09:25 +00001395 return IsVolatile && AtomicIsInline;
David Majnemera5b195a2015-02-14 01:35:12 +00001396}
1397
1398RValue CodeGenFunction::EmitAtomicLoad(LValue LV, SourceLocation SL,
1399 AggValueSlot Slot) {
1400 llvm::AtomicOrdering AO;
1401 bool IsVolatile = LV.isVolatileQualified();
1402 if (LV.getType()->isAtomicType()) {
JF Bastien92f4ef12016-04-06 17:26:42 +00001403 AO = llvm::AtomicOrdering::SequentiallyConsistent;
David Majnemera5b195a2015-02-14 01:35:12 +00001404 } else {
JF Bastien92f4ef12016-04-06 17:26:42 +00001405 AO = llvm::AtomicOrdering::Acquire;
David Majnemera5b195a2015-02-14 01:35:12 +00001406 IsVolatile = true;
1407 }
1408 return EmitAtomicLoad(LV, SL, AO, IsVolatile, Slot);
1409}
1410
Alexey Bataevb8329262015-02-27 06:33:30 +00001411RValue AtomicInfo::EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
1412 bool AsValue, llvm::AtomicOrdering AO,
1413 bool IsVolatile) {
1414 // Check whether we should use a library call.
1415 if (shouldUseLibcall()) {
John McCall7f416cc2015-09-08 08:05:57 +00001416 Address TempAddr = Address::invalid();
Alexey Bataevb8329262015-02-27 06:33:30 +00001417 if (LVal.isSimple() && !ResultSlot.isIgnored()) {
1418 assert(getEvaluationKind() == TEK_Aggregate);
John McCall7f416cc2015-09-08 08:05:57 +00001419 TempAddr = ResultSlot.getAddress();
Alexey Bataevb8329262015-02-27 06:33:30 +00001420 } else
1421 TempAddr = CreateTempAlloca();
1422
John McCall7f416cc2015-09-08 08:05:57 +00001423 EmitAtomicLoadLibcall(TempAddr.getPointer(), AO, IsVolatile);
Alexey Bataevb8329262015-02-27 06:33:30 +00001424
1425 // Okay, turn that back into the original value or whole atomic (for
1426 // non-simple lvalues) type.
John McCall7f416cc2015-09-08 08:05:57 +00001427 return convertAtomicTempToRValue(TempAddr, ResultSlot, Loc, AsValue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001428 }
1429
1430 // Okay, we're doing this natively.
1431 auto *Load = EmitAtomicLoadOp(AO, IsVolatile);
1432
1433 // If we're ignoring an aggregate return, don't do anything.
1434 if (getEvaluationKind() == TEK_Aggregate && ResultSlot.isIgnored())
John McCall7f416cc2015-09-08 08:05:57 +00001435 return RValue::getAggregate(Address::invalid(), false);
Alexey Bataevb8329262015-02-27 06:33:30 +00001436
1437 // Okay, turn that back into the original value or atomic (for non-simple
1438 // lvalues) type.
1439 return ConvertIntToValueOrAtomic(Load, ResultSlot, Loc, AsValue);
1440}
1441
John McCalla8ec7eb2013-03-07 21:37:17 +00001442/// Emit a load from an l-value of atomic type. Note that the r-value
1443/// we produce is an r-value of the atomic *value* type.
Nick Lewycky2d84e842013-10-02 02:29:49 +00001444RValue CodeGenFunction::EmitAtomicLoad(LValue src, SourceLocation loc,
David Majnemera5b195a2015-02-14 01:35:12 +00001445 llvm::AtomicOrdering AO, bool IsVolatile,
Nick Lewycky2d84e842013-10-02 02:29:49 +00001446 AggValueSlot resultSlot) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001447 AtomicInfo Atomics(*this, src);
1448 return Atomics.EmitAtomicLoad(resultSlot, loc, /*AsValue=*/true, AO,
1449 IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +00001450}
1451
John McCalla8ec7eb2013-03-07 21:37:17 +00001452/// Copy an r-value into memory as part of storing to an atomic type.
1453/// This needs to create a bit-pattern suitable for atomic operations.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001454void AtomicInfo::emitCopyIntoMemory(RValue rvalue) const {
1455 assert(LVal.isSimple());
John McCalla8ec7eb2013-03-07 21:37:17 +00001456 // If we have an r-value, the rvalue should be of the atomic type,
1457 // which means that the caller is responsible for having zeroed
1458 // any padding. Just do an aggregate copy of that type.
1459 if (rvalue.isAggregate()) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001460 CGF.EmitAggregateCopy(getAtomicAddress(),
John McCall7f416cc2015-09-08 08:05:57 +00001461 rvalue.getAggregateAddress(),
John McCalla8ec7eb2013-03-07 21:37:17 +00001462 getAtomicType(),
1463 (rvalue.isVolatileQualified()
John McCall7f416cc2015-09-08 08:05:57 +00001464 || LVal.isVolatileQualified()));
John McCalla8ec7eb2013-03-07 21:37:17 +00001465 return;
1466 }
1467
1468 // Okay, otherwise we're copying stuff.
1469
1470 // Zero out the buffer if necessary.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001471 emitMemSetZeroIfNecessary();
John McCalla8ec7eb2013-03-07 21:37:17 +00001472
1473 // Drill past the padding if present.
Alexey Bataevb57056f2015-01-22 06:17:56 +00001474 LValue TempLVal = projectValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001475
1476 // Okay, store the rvalue in.
1477 if (rvalue.isScalar()) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001478 CGF.EmitStoreOfScalar(rvalue.getScalarVal(), TempLVal, /*init*/ true);
John McCalla8ec7eb2013-03-07 21:37:17 +00001479 } else {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001480 CGF.EmitStoreOfComplex(rvalue.getComplexVal(), TempLVal, /*init*/ true);
John McCalla8ec7eb2013-03-07 21:37:17 +00001481 }
1482}
1483
1484
1485/// Materialize an r-value into memory for the purposes of storing it
1486/// to an atomic type.
John McCall7f416cc2015-09-08 08:05:57 +00001487Address AtomicInfo::materializeRValue(RValue rvalue) const {
John McCalla8ec7eb2013-03-07 21:37:17 +00001488 // Aggregate r-values are already in memory, and EmitAtomicStore
1489 // requires them to be values of the atomic type.
1490 if (rvalue.isAggregate())
John McCall7f416cc2015-09-08 08:05:57 +00001491 return rvalue.getAggregateAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +00001492
1493 // Otherwise, make a temporary and materialize into it.
John McCall7f416cc2015-09-08 08:05:57 +00001494 LValue TempLV = CGF.MakeAddrLValue(CreateTempAlloca(), getAtomicType());
Alexey Bataevb8329262015-02-27 06:33:30 +00001495 AtomicInfo Atomics(CGF, TempLV);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001496 Atomics.emitCopyIntoMemory(rvalue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001497 return TempLV.getAddress();
John McCalla8ec7eb2013-03-07 21:37:17 +00001498}
1499
Alexey Bataev452d8e12014-12-15 05:25:25 +00001500llvm::Value *AtomicInfo::convertRValueToInt(RValue RVal) const {
1501 // If we've got a scalar value of the right size, try to avoid going
1502 // through memory.
Alexey Bataevb8329262015-02-27 06:33:30 +00001503 if (RVal.isScalar() && (!hasPadding() || !LVal.isSimple())) {
Alexey Bataev452d8e12014-12-15 05:25:25 +00001504 llvm::Value *Value = RVal.getScalarVal();
1505 if (isa<llvm::IntegerType>(Value->getType()))
Alexey Bataevb4505a72015-03-30 05:20:59 +00001506 return CGF.EmitToMemory(Value, ValueTy);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001507 else {
Alexey Bataevb8329262015-02-27 06:33:30 +00001508 llvm::IntegerType *InputIntTy = llvm::IntegerType::get(
1509 CGF.getLLVMContext(),
1510 LVal.isSimple() ? getValueSizeInBits() : getAtomicSizeInBits());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001511 if (isa<llvm::PointerType>(Value->getType()))
1512 return CGF.Builder.CreatePtrToInt(Value, InputIntTy);
1513 else if (llvm::BitCastInst::isBitCastable(Value->getType(), InputIntTy))
1514 return CGF.Builder.CreateBitCast(Value, InputIntTy);
1515 }
1516 }
1517 // Otherwise, we need to go through memory.
1518 // Put the r-value in memory.
John McCall7f416cc2015-09-08 08:05:57 +00001519 Address Addr = materializeRValue(RVal);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001520
1521 // Cast the temporary to the atomic int type and pull a value out.
1522 Addr = emitCastToAtomicIntPointer(Addr);
John McCall7f416cc2015-09-08 08:05:57 +00001523 return CGF.Builder.CreateLoad(Addr);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001524}
1525
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001526std::pair<llvm::Value *, llvm::Value *> AtomicInfo::EmitAtomicCompareExchangeOp(
1527 llvm::Value *ExpectedVal, llvm::Value *DesiredVal,
1528 llvm::AtomicOrdering Success, llvm::AtomicOrdering Failure, bool IsWeak) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001529 // Do the atomic store.
John McCall7f416cc2015-09-08 08:05:57 +00001530 Address Addr = getAtomicAddressAsAtomicIntPointer();
1531 auto *Inst = CGF.Builder.CreateAtomicCmpXchg(Addr.getPointer(),
1532 ExpectedVal, DesiredVal,
Alexey Bataevb4505a72015-03-30 05:20:59 +00001533 Success, Failure);
Alexey Bataevb8329262015-02-27 06:33:30 +00001534 // Other decoration.
1535 Inst->setVolatile(LVal.isVolatileQualified());
1536 Inst->setWeak(IsWeak);
1537
1538 // Okay, turn that back into the original value type.
1539 auto *PreviousVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/0);
1540 auto *SuccessFailureVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/1);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001541 return std::make_pair(PreviousVal, SuccessFailureVal);
Alexey Bataevb8329262015-02-27 06:33:30 +00001542}
1543
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001544llvm::Value *
1545AtomicInfo::EmitAtomicCompareExchangeLibcall(llvm::Value *ExpectedAddr,
1546 llvm::Value *DesiredAddr,
Alexey Bataevb8329262015-02-27 06:33:30 +00001547 llvm::AtomicOrdering Success,
1548 llvm::AtomicOrdering Failure) {
1549 // bool __atomic_compare_exchange(size_t size, void *obj, void *expected,
1550 // void *desired, int success, int failure);
1551 CallArgList Args;
1552 Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
John McCall7f416cc2015-09-08 08:05:57 +00001553 Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
Alexey Bataevb8329262015-02-27 06:33:30 +00001554 CGF.getContext().VoidPtrTy);
1555 Args.add(RValue::get(CGF.EmitCastToVoidPtr(ExpectedAddr)),
1556 CGF.getContext().VoidPtrTy);
1557 Args.add(RValue::get(CGF.EmitCastToVoidPtr(DesiredAddr)),
1558 CGF.getContext().VoidPtrTy);
JF Bastiendda2cb12016-04-18 18:01:49 +00001559 Args.add(RValue::get(
1560 llvm::ConstantInt::get(CGF.IntTy, (int)llvm::toCABI(Success))),
Alexey Bataevb8329262015-02-27 06:33:30 +00001561 CGF.getContext().IntTy);
JF Bastiendda2cb12016-04-18 18:01:49 +00001562 Args.add(RValue::get(
1563 llvm::ConstantInt::get(CGF.IntTy, (int)llvm::toCABI(Failure))),
Alexey Bataevb8329262015-02-27 06:33:30 +00001564 CGF.getContext().IntTy);
1565 auto SuccessFailureRVal = emitAtomicLibcall(CGF, "__atomic_compare_exchange",
1566 CGF.getContext().BoolTy, Args);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001567
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001568 return SuccessFailureRVal.getScalarVal();
Alexey Bataevb8329262015-02-27 06:33:30 +00001569}
1570
Alexey Bataevb4505a72015-03-30 05:20:59 +00001571std::pair<RValue, llvm::Value *> AtomicInfo::EmitAtomicCompareExchange(
Alexey Bataevb8329262015-02-27 06:33:30 +00001572 RValue Expected, RValue Desired, llvm::AtomicOrdering Success,
1573 llvm::AtomicOrdering Failure, bool IsWeak) {
JF Bastiendd11ee72016-04-06 23:37:36 +00001574 if (isStrongerThan(Failure, Success))
1575 // Don't assert on undefined behavior "failure argument shall be no stronger
1576 // than the success argument".
Alexey Bataevb8329262015-02-27 06:33:30 +00001577 Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(Success);
1578
1579 // Check whether we should use a library call.
1580 if (shouldUseLibcall()) {
Alexey Bataevb8329262015-02-27 06:33:30 +00001581 // Produce a source address.
John McCall7f416cc2015-09-08 08:05:57 +00001582 Address ExpectedAddr = materializeRValue(Expected);
1583 Address DesiredAddr = materializeRValue(Desired);
1584 auto *Res = EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1585 DesiredAddr.getPointer(),
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001586 Success, Failure);
1587 return std::make_pair(
John McCall7f416cc2015-09-08 08:05:57 +00001588 convertAtomicTempToRValue(ExpectedAddr, AggValueSlot::ignored(),
1589 SourceLocation(), /*AsValue=*/false),
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001590 Res);
Alexey Bataevb8329262015-02-27 06:33:30 +00001591 }
1592
1593 // If we've got a scalar value of the right size, try to avoid going
1594 // through memory.
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001595 auto *ExpectedVal = convertRValueToInt(Expected);
1596 auto *DesiredVal = convertRValueToInt(Desired);
1597 auto Res = EmitAtomicCompareExchangeOp(ExpectedVal, DesiredVal, Success,
1598 Failure, IsWeak);
1599 return std::make_pair(
1600 ConvertIntToValueOrAtomic(Res.first, AggValueSlot::ignored(),
1601 SourceLocation(), /*AsValue=*/false),
1602 Res.second);
1603}
1604
1605static void
1606EmitAtomicUpdateValue(CodeGenFunction &CGF, AtomicInfo &Atomics, RValue OldRVal,
1607 const llvm::function_ref<RValue(RValue)> &UpdateOp,
John McCall7f416cc2015-09-08 08:05:57 +00001608 Address DesiredAddr) {
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001609 RValue UpRVal;
1610 LValue AtomicLVal = Atomics.getAtomicLValue();
1611 LValue DesiredLVal;
1612 if (AtomicLVal.isSimple()) {
1613 UpRVal = OldRVal;
John McCall7f416cc2015-09-08 08:05:57 +00001614 DesiredLVal = CGF.MakeAddrLValue(DesiredAddr, AtomicLVal.getType());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001615 } else {
1616 // Build new lvalue for temp address
John McCall7f416cc2015-09-08 08:05:57 +00001617 Address Ptr = Atomics.materializeRValue(OldRVal);
1618 LValue UpdateLVal;
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001619 if (AtomicLVal.isBitField()) {
1620 UpdateLVal =
1621 LValue::MakeBitfield(Ptr, AtomicLVal.getBitFieldInfo(),
John McCall7f416cc2015-09-08 08:05:57 +00001622 AtomicLVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001623 AtomicLVal.getBaseInfo());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001624 DesiredLVal =
1625 LValue::MakeBitfield(DesiredAddr, AtomicLVal.getBitFieldInfo(),
John McCall7f416cc2015-09-08 08:05:57 +00001626 AtomicLVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001627 AtomicLVal.getBaseInfo());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001628 } else if (AtomicLVal.isVectorElt()) {
1629 UpdateLVal = LValue::MakeVectorElt(Ptr, AtomicLVal.getVectorIdx(),
1630 AtomicLVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001631 AtomicLVal.getBaseInfo());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001632 DesiredLVal = LValue::MakeVectorElt(
1633 DesiredAddr, AtomicLVal.getVectorIdx(), AtomicLVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001634 AtomicLVal.getBaseInfo());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001635 } else {
1636 assert(AtomicLVal.isExtVectorElt());
1637 UpdateLVal = LValue::MakeExtVectorElt(Ptr, AtomicLVal.getExtVectorElts(),
1638 AtomicLVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001639 AtomicLVal.getBaseInfo());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001640 DesiredLVal = LValue::MakeExtVectorElt(
1641 DesiredAddr, AtomicLVal.getExtVectorElts(), AtomicLVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001642 AtomicLVal.getBaseInfo());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001643 }
1644 UpdateLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1645 DesiredLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1646 UpRVal = CGF.EmitLoadOfLValue(UpdateLVal, SourceLocation());
1647 }
1648 // Store new value in the corresponding memory area
1649 RValue NewRVal = UpdateOp(UpRVal);
1650 if (NewRVal.isScalar()) {
1651 CGF.EmitStoreThroughLValue(NewRVal, DesiredLVal);
1652 } else {
1653 assert(NewRVal.isComplex());
1654 CGF.EmitStoreOfComplex(NewRVal.getComplexVal(), DesiredLVal,
1655 /*isInit=*/false);
1656 }
1657}
1658
1659void AtomicInfo::EmitAtomicUpdateLibcall(
1660 llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1661 bool IsVolatile) {
1662 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1663
John McCall7f416cc2015-09-08 08:05:57 +00001664 Address ExpectedAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001665
John McCall7f416cc2015-09-08 08:05:57 +00001666 EmitAtomicLoadLibcall(ExpectedAddr.getPointer(), AO, IsVolatile);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001667 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1668 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1669 CGF.EmitBlock(ContBB);
John McCall7f416cc2015-09-08 08:05:57 +00001670 Address DesiredAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001671 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001672 requiresMemSetZero(getAtomicAddress().getElementType())) {
1673 auto *OldVal = CGF.Builder.CreateLoad(ExpectedAddr);
1674 CGF.Builder.CreateStore(OldVal, DesiredAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001675 }
John McCall7f416cc2015-09-08 08:05:57 +00001676 auto OldRVal = convertAtomicTempToRValue(ExpectedAddr,
1677 AggValueSlot::ignored(),
1678 SourceLocation(), /*AsValue=*/false);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001679 EmitAtomicUpdateValue(CGF, *this, OldRVal, UpdateOp, DesiredAddr);
1680 auto *Res =
John McCall7f416cc2015-09-08 08:05:57 +00001681 EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1682 DesiredAddr.getPointer(),
1683 AO, Failure);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001684 CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
1685 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1686}
1687
1688void AtomicInfo::EmitAtomicUpdateOp(
1689 llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1690 bool IsVolatile) {
1691 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1692
1693 // Do the atomic load.
1694 auto *OldVal = EmitAtomicLoadOp(AO, IsVolatile);
1695 // For non-simple lvalues perform compare-and-swap procedure.
1696 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1697 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1698 auto *CurBB = CGF.Builder.GetInsertBlock();
1699 CGF.EmitBlock(ContBB);
1700 llvm::PHINode *PHI = CGF.Builder.CreatePHI(OldVal->getType(),
1701 /*NumReservedValues=*/2);
1702 PHI->addIncoming(OldVal, CurBB);
John McCall7f416cc2015-09-08 08:05:57 +00001703 Address NewAtomicAddr = CreateTempAlloca();
1704 Address NewAtomicIntAddr = emitCastToAtomicIntPointer(NewAtomicAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001705 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001706 requiresMemSetZero(getAtomicAddress().getElementType())) {
1707 CGF.Builder.CreateStore(PHI, NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001708 }
1709 auto OldRVal = ConvertIntToValueOrAtomic(PHI, AggValueSlot::ignored(),
1710 SourceLocation(), /*AsValue=*/false);
1711 EmitAtomicUpdateValue(CGF, *this, OldRVal, UpdateOp, NewAtomicAddr);
John McCall7f416cc2015-09-08 08:05:57 +00001712 auto *DesiredVal = CGF.Builder.CreateLoad(NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001713 // Try to write new value using cmpxchg operation
1714 auto Res = EmitAtomicCompareExchangeOp(PHI, DesiredVal, AO, Failure);
1715 PHI->addIncoming(Res.first, CGF.Builder.GetInsertBlock());
1716 CGF.Builder.CreateCondBr(Res.second, ExitBB, ContBB);
1717 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1718}
1719
1720static void EmitAtomicUpdateValue(CodeGenFunction &CGF, AtomicInfo &Atomics,
John McCall7f416cc2015-09-08 08:05:57 +00001721 RValue UpdateRVal, Address DesiredAddr) {
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001722 LValue AtomicLVal = Atomics.getAtomicLValue();
1723 LValue DesiredLVal;
1724 // Build new lvalue for temp address
1725 if (AtomicLVal.isBitField()) {
1726 DesiredLVal =
1727 LValue::MakeBitfield(DesiredAddr, AtomicLVal.getBitFieldInfo(),
John McCall7f416cc2015-09-08 08:05:57 +00001728 AtomicLVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001729 AtomicLVal.getBaseInfo());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001730 } else if (AtomicLVal.isVectorElt()) {
1731 DesiredLVal =
1732 LValue::MakeVectorElt(DesiredAddr, AtomicLVal.getVectorIdx(),
John McCall7f416cc2015-09-08 08:05:57 +00001733 AtomicLVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001734 AtomicLVal.getBaseInfo());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001735 } else {
1736 assert(AtomicLVal.isExtVectorElt());
1737 DesiredLVal = LValue::MakeExtVectorElt(
1738 DesiredAddr, AtomicLVal.getExtVectorElts(), AtomicLVal.getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +00001739 AtomicLVal.getBaseInfo());
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001740 }
1741 DesiredLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1742 // Store new value in the corresponding memory area
1743 assert(UpdateRVal.isScalar());
1744 CGF.EmitStoreThroughLValue(UpdateRVal, DesiredLVal);
1745}
1746
1747void AtomicInfo::EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO,
1748 RValue UpdateRVal, bool IsVolatile) {
1749 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1750
John McCall7f416cc2015-09-08 08:05:57 +00001751 Address ExpectedAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001752
John McCall7f416cc2015-09-08 08:05:57 +00001753 EmitAtomicLoadLibcall(ExpectedAddr.getPointer(), AO, IsVolatile);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001754 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1755 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1756 CGF.EmitBlock(ContBB);
John McCall7f416cc2015-09-08 08:05:57 +00001757 Address DesiredAddr = CreateTempAlloca();
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001758 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001759 requiresMemSetZero(getAtomicAddress().getElementType())) {
1760 auto *OldVal = CGF.Builder.CreateLoad(ExpectedAddr);
1761 CGF.Builder.CreateStore(OldVal, DesiredAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001762 }
1763 EmitAtomicUpdateValue(CGF, *this, UpdateRVal, DesiredAddr);
1764 auto *Res =
John McCall7f416cc2015-09-08 08:05:57 +00001765 EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1766 DesiredAddr.getPointer(),
1767 AO, Failure);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001768 CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
1769 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1770}
1771
1772void AtomicInfo::EmitAtomicUpdateOp(llvm::AtomicOrdering AO, RValue UpdateRVal,
1773 bool IsVolatile) {
1774 auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1775
1776 // Do the atomic load.
1777 auto *OldVal = EmitAtomicLoadOp(AO, IsVolatile);
1778 // For non-simple lvalues perform compare-and-swap procedure.
1779 auto *ContBB = CGF.createBasicBlock("atomic_cont");
1780 auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1781 auto *CurBB = CGF.Builder.GetInsertBlock();
1782 CGF.EmitBlock(ContBB);
1783 llvm::PHINode *PHI = CGF.Builder.CreatePHI(OldVal->getType(),
1784 /*NumReservedValues=*/2);
1785 PHI->addIncoming(OldVal, CurBB);
John McCall7f416cc2015-09-08 08:05:57 +00001786 Address NewAtomicAddr = CreateTempAlloca();
1787 Address NewAtomicIntAddr = emitCastToAtomicIntPointer(NewAtomicAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001788 if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
John McCall7f416cc2015-09-08 08:05:57 +00001789 requiresMemSetZero(getAtomicAddress().getElementType())) {
1790 CGF.Builder.CreateStore(PHI, NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001791 }
1792 EmitAtomicUpdateValue(CGF, *this, UpdateRVal, NewAtomicAddr);
John McCall7f416cc2015-09-08 08:05:57 +00001793 auto *DesiredVal = CGF.Builder.CreateLoad(NewAtomicIntAddr);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001794 // Try to write new value using cmpxchg operation
1795 auto Res = EmitAtomicCompareExchangeOp(PHI, DesiredVal, AO, Failure);
1796 PHI->addIncoming(Res.first, CGF.Builder.GetInsertBlock());
1797 CGF.Builder.CreateCondBr(Res.second, ExitBB, ContBB);
1798 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1799}
1800
1801void AtomicInfo::EmitAtomicUpdate(
1802 llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1803 bool IsVolatile) {
1804 if (shouldUseLibcall()) {
1805 EmitAtomicUpdateLibcall(AO, UpdateOp, IsVolatile);
1806 } else {
1807 EmitAtomicUpdateOp(AO, UpdateOp, IsVolatile);
1808 }
1809}
1810
1811void AtomicInfo::EmitAtomicUpdate(llvm::AtomicOrdering AO, RValue UpdateRVal,
1812 bool IsVolatile) {
1813 if (shouldUseLibcall()) {
1814 EmitAtomicUpdateLibcall(AO, UpdateRVal, IsVolatile);
1815 } else {
1816 EmitAtomicUpdateOp(AO, UpdateRVal, IsVolatile);
1817 }
Alexey Bataevb8329262015-02-27 06:33:30 +00001818}
1819
David Majnemera5b195a2015-02-14 01:35:12 +00001820void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue lvalue,
1821 bool isInit) {
1822 bool IsVolatile = lvalue.isVolatileQualified();
1823 llvm::AtomicOrdering AO;
1824 if (lvalue.getType()->isAtomicType()) {
JF Bastien92f4ef12016-04-06 17:26:42 +00001825 AO = llvm::AtomicOrdering::SequentiallyConsistent;
David Majnemera5b195a2015-02-14 01:35:12 +00001826 } else {
JF Bastien92f4ef12016-04-06 17:26:42 +00001827 AO = llvm::AtomicOrdering::Release;
David Majnemera5b195a2015-02-14 01:35:12 +00001828 IsVolatile = true;
1829 }
1830 return EmitAtomicStore(rvalue, lvalue, AO, IsVolatile, isInit);
1831}
1832
John McCalla8ec7eb2013-03-07 21:37:17 +00001833/// Emit a store to an l-value of atomic type.
1834///
1835/// Note that the r-value is expected to be an r-value *of the atomic
1836/// type*; this means that for aggregate r-values, it should include
1837/// storage for any padding that was necessary.
David Majnemera5b195a2015-02-14 01:35:12 +00001838void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue dest,
1839 llvm::AtomicOrdering AO, bool IsVolatile,
1840 bool isInit) {
John McCalla8ec7eb2013-03-07 21:37:17 +00001841 // If this is an aggregate r-value, it should agree in type except
1842 // maybe for address-space qualification.
1843 assert(!rvalue.isAggregate() ||
John McCall7f416cc2015-09-08 08:05:57 +00001844 rvalue.getAggregateAddress().getElementType()
1845 == dest.getAddress().getElementType());
John McCalla8ec7eb2013-03-07 21:37:17 +00001846
1847 AtomicInfo atomics(*this, dest);
Alexey Bataevb8329262015-02-27 06:33:30 +00001848 LValue LVal = atomics.getAtomicLValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001849
1850 // If this is an initialization, just put the value there normally.
Alexey Bataevb8329262015-02-27 06:33:30 +00001851 if (LVal.isSimple()) {
1852 if (isInit) {
1853 atomics.emitCopyIntoMemory(rvalue);
1854 return;
1855 }
1856
1857 // Check whether we should use a library call.
1858 if (atomics.shouldUseLibcall()) {
1859 // Produce a source address.
John McCall7f416cc2015-09-08 08:05:57 +00001860 Address srcAddr = atomics.materializeRValue(rvalue);
Alexey Bataevb8329262015-02-27 06:33:30 +00001861
1862 // void __atomic_store(size_t size, void *mem, void *val, int order)
1863 CallArgList args;
1864 args.add(RValue::get(atomics.getAtomicSizeValue()),
1865 getContext().getSizeType());
John McCall7f416cc2015-09-08 08:05:57 +00001866 args.add(RValue::get(EmitCastToVoidPtr(atomics.getAtomicPointer())),
Alexey Bataevb8329262015-02-27 06:33:30 +00001867 getContext().VoidPtrTy);
John McCall7f416cc2015-09-08 08:05:57 +00001868 args.add(RValue::get(EmitCastToVoidPtr(srcAddr.getPointer())),
1869 getContext().VoidPtrTy);
JF Bastiendda2cb12016-04-18 18:01:49 +00001870 args.add(
1871 RValue::get(llvm::ConstantInt::get(IntTy, (int)llvm::toCABI(AO))),
1872 getContext().IntTy);
Alexey Bataevb8329262015-02-27 06:33:30 +00001873 emitAtomicLibcall(*this, "__atomic_store", getContext().VoidTy, args);
1874 return;
1875 }
1876
1877 // Okay, we're doing this natively.
1878 llvm::Value *intValue = atomics.convertRValueToInt(rvalue);
1879
1880 // Do the atomic store.
John McCall7f416cc2015-09-08 08:05:57 +00001881 Address addr =
Alexey Bataevb8329262015-02-27 06:33:30 +00001882 atomics.emitCastToAtomicIntPointer(atomics.getAtomicAddress());
1883 intValue = Builder.CreateIntCast(
John McCall7f416cc2015-09-08 08:05:57 +00001884 intValue, addr.getElementType(), /*isSigned=*/false);
Alexey Bataevb8329262015-02-27 06:33:30 +00001885 llvm::StoreInst *store = Builder.CreateStore(intValue, addr);
1886
1887 // Initializations don't need to be atomic.
1888 if (!isInit)
1889 store->setAtomic(AO);
1890
1891 // Other decoration.
Alexey Bataevb8329262015-02-27 06:33:30 +00001892 if (IsVolatile)
1893 store->setVolatile(true);
1894 if (dest.getTBAAInfo())
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001895 CGM.DecorateInstructionWithTBAA(store, dest.getTBAAInfo());
John McCalla8ec7eb2013-03-07 21:37:17 +00001896 return;
1897 }
1898
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001899 // Emit simple atomic update operation.
1900 atomics.EmitAtomicUpdate(AO, rvalue, IsVolatile);
John McCalla8ec7eb2013-03-07 21:37:17 +00001901}
1902
Alexey Bataev452d8e12014-12-15 05:25:25 +00001903/// Emit a compare-and-exchange op for atomic type.
1904///
Alexey Bataevb4505a72015-03-30 05:20:59 +00001905std::pair<RValue, llvm::Value *> CodeGenFunction::EmitAtomicCompareExchange(
Alexey Bataev452d8e12014-12-15 05:25:25 +00001906 LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
1907 llvm::AtomicOrdering Success, llvm::AtomicOrdering Failure, bool IsWeak,
1908 AggValueSlot Slot) {
1909 // If this is an aggregate r-value, it should agree in type except
1910 // maybe for address-space qualification.
1911 assert(!Expected.isAggregate() ||
John McCall7f416cc2015-09-08 08:05:57 +00001912 Expected.getAggregateAddress().getElementType() ==
1913 Obj.getAddress().getElementType());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001914 assert(!Desired.isAggregate() ||
John McCall7f416cc2015-09-08 08:05:57 +00001915 Desired.getAggregateAddress().getElementType() ==
1916 Obj.getAddress().getElementType());
Alexey Bataev452d8e12014-12-15 05:25:25 +00001917 AtomicInfo Atomics(*this, Obj);
1918
Alexey Bataevb4505a72015-03-30 05:20:59 +00001919 return Atomics.EmitAtomicCompareExchange(Expected, Desired, Success, Failure,
1920 IsWeak);
1921}
1922
1923void CodeGenFunction::EmitAtomicUpdate(
1924 LValue LVal, llvm::AtomicOrdering AO,
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001925 const llvm::function_ref<RValue(RValue)> &UpdateOp, bool IsVolatile) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00001926 AtomicInfo Atomics(*this, LVal);
Alexey Bataevf0ab5532015-05-15 08:36:34 +00001927 Atomics.EmitAtomicUpdate(AO, UpdateOp, IsVolatile);
Alexey Bataev452d8e12014-12-15 05:25:25 +00001928}
1929
John McCalla8ec7eb2013-03-07 21:37:17 +00001930void CodeGenFunction::EmitAtomicInit(Expr *init, LValue dest) {
1931 AtomicInfo atomics(*this, dest);
1932
1933 switch (atomics.getEvaluationKind()) {
1934 case TEK_Scalar: {
1935 llvm::Value *value = EmitScalarExpr(init);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001936 atomics.emitCopyIntoMemory(RValue::get(value));
John McCalla8ec7eb2013-03-07 21:37:17 +00001937 return;
1938 }
1939
1940 case TEK_Complex: {
1941 ComplexPairTy value = EmitComplexExpr(init);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001942 atomics.emitCopyIntoMemory(RValue::getComplex(value));
John McCalla8ec7eb2013-03-07 21:37:17 +00001943 return;
1944 }
1945
1946 case TEK_Aggregate: {
Eli Friedmanbe4504d2013-07-11 01:32:21 +00001947 // Fix up the destination if the initializer isn't an expression
1948 // of atomic type.
1949 bool Zeroed = false;
John McCalla8ec7eb2013-03-07 21:37:17 +00001950 if (!init->getType()->isAtomicType()) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001951 Zeroed = atomics.emitMemSetZeroIfNecessary();
1952 dest = atomics.projectValue();
John McCalla8ec7eb2013-03-07 21:37:17 +00001953 }
1954
1955 // Evaluate the expression directly into the destination.
1956 AggValueSlot slot = AggValueSlot::forLValue(dest,
1957 AggValueSlot::IsNotDestructed,
1958 AggValueSlot::DoesNotNeedGCBarriers,
Eli Friedmanbe4504d2013-07-11 01:32:21 +00001959 AggValueSlot::IsNotAliased,
1960 Zeroed ? AggValueSlot::IsZeroed :
1961 AggValueSlot::IsNotZeroed);
1962
John McCalla8ec7eb2013-03-07 21:37:17 +00001963 EmitAggExpr(init, slot);
1964 return;
1965 }
1966 }
1967 llvm_unreachable("bad evaluation kind");
1968}