1 //===--- CGAtomic.cpp - Emit LLVM IR for atomic operations ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the code for emitting atomic operations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CGCall.h"
16 #include "CGRecordLayout.h"
17 #include "CodeGenModule.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/CodeGen/CGFunctionInfo.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/IR/Operator.h"
24 
25 using namespace clang;
26 using namespace CodeGen;
27 
28 namespace {
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;
40     LValue LVal;
41     CGBitFieldInfo BFI;
42   public:
43     AtomicInfo(CodeGenFunction &CGF, LValue &lvalue)
44         : CGF(CGF), AtomicSizeInBits(0), ValueSizeInBits(0),
45           EvaluationKind(TEK_Scalar), UseLibcall(true) {
46       assert(!lvalue.isGlobalReg());
47       ASTContext &C = CGF.getContext();
48       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);
55 
56         uint64_t ValueAlignInBits;
57         uint64_t AtomicAlignInBits;
58         TypeInfo ValueTI = C.getTypeInfo(ValueTy);
59         ValueSizeInBits = ValueTI.Width;
60         ValueAlignInBits = ValueTI.Align;
61 
62         TypeInfo AtomicTI = C.getTypeInfo(AtomicTy);
63         AtomicSizeInBits = AtomicTI.Width;
64         AtomicAlignInBits = AtomicTI.Align;
65 
66         assert(ValueSizeInBits <= AtomicSizeInBits);
67         assert(ValueAlignInBits <= AtomicAlignInBits);
68 
69         AtomicAlign = C.toCharUnitsFromBits(AtomicAlignInBits);
70         ValueAlign = C.toCharUnitsFromBits(ValueAlignInBits);
71         if (lvalue.getAlignment().isZero())
72           lvalue.setAlignment(AtomicAlign);
73 
74         LVal = lvalue;
75       } else if (lvalue.isBitField()) {
76         ValueTy = lvalue.getType();
77         ValueSizeInBits = C.getTypeSize(ValueTy);
78         auto &OrigBFI = lvalue.getBitFieldInfo();
79         auto Offset = OrigBFI.Offset % C.toBits(lvalue.getAlignment());
80         AtomicSizeInBits = C.toBits(
81             C.toCharUnitsFromBits(Offset + OrigBFI.Size + C.getCharWidth() - 1)
82                 .alignTo(lvalue.getAlignment()));
83         auto VoidPtrAddr = CGF.EmitCastToVoidPtr(lvalue.getBitFieldPointer());
84         auto OffsetInChars =
85             (C.toCharUnitsFromBits(OrigBFI.Offset) / lvalue.getAlignment()) *
86             lvalue.getAlignment();
87         VoidPtrAddr = CGF.Builder.CreateConstGEP1_64(
88             VoidPtrAddr, OffsetInChars.getQuantity());
89         auto Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
90             VoidPtrAddr,
91             CGF.Builder.getIntNTy(AtomicSizeInBits)->getPointerTo(),
92             "atomic_bitfield_base");
93         BFI = OrigBFI;
94         BFI.Offset = Offset;
95         BFI.StorageSize = AtomicSizeInBits;
96         BFI.StorageOffset += OffsetInChars;
97         LVal = LValue::MakeBitfield(Address(Addr, lvalue.getAlignment()),
98                                     BFI, lvalue.getType(),
99                                     lvalue.getAlignmentSource());
100         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();
110       } else if (lvalue.isVectorElt()) {
111         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();
116         LVal = lvalue;
117       } else {
118         assert(lvalue.isExtVectorElt());
119         ValueTy = lvalue.getType();
120         ValueSizeInBits = C.getTypeSize(ValueTy);
121         AtomicTy = ValueTy = CGF.getContext().getExtVectorType(
122             lvalue.getType(), lvalue.getExtVectorAddress()
123                                   .getElementType()->getVectorNumElements());
124         AtomicSizeInBits = C.getTypeSize(AtomicTy);
125         AtomicAlign = ValueAlign = lvalue.getAlignment();
126         LVal = lvalue;
127       }
128       UseLibcall = !C.getTargetInfo().hasBuiltinAtomic(
129           AtomicSizeInBits, C.toBits(lvalue.getAlignment()));
130     }
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; }
137     uint64_t getValueSizeInBits() const { return ValueSizeInBits; }
138     TypeEvaluationKind getEvaluationKind() const { return EvaluationKind; }
139     bool shouldUseLibcall() const { return UseLibcall; }
140     const LValue &getAtomicLValue() const { return LVal; }
141     llvm::Value *getAtomicPointer() const {
142       if (LVal.isSimple())
143         return LVal.getPointer();
144       else if (LVal.isBitField())
145         return LVal.getBitFieldPointer();
146       else if (LVal.isVectorElt())
147         return LVal.getVectorPointer();
148       assert(LVal.isExtVectorElt());
149       return LVal.getExtVectorPointer();
150     }
151     Address getAtomicAddress() const {
152       return Address(getAtomicPointer(), getAtomicAlignment());
153     }
154 
155     Address getAtomicAddressAsAtomicIntPointer() const {
156       return emitCastToAtomicIntPointer(getAtomicAddress());
157     }
158 
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 
169     bool emitMemSetZeroIfNecessary() const;
170 
171     llvm::Value *getAtomicSizeValue() const {
172       CharUnits size = CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits);
173       return CGF.CGM.getSize(size);
174     }
175 
176     /// Cast the given pointer to an integer pointer suitable for 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;
184 
185     /// Turn an atomic-layout object into an r-value.
186     RValue convertAtomicTempToRValue(Address addr, AggValueSlot resultSlot,
187                                      SourceLocation loc, bool AsValue) const;
188 
189     /// \brief Converts a rvalue to integer value.
190     llvm::Value *convertRValueToInt(RValue RVal) const;
191 
192     RValue ConvertIntToValueOrAtomic(llvm::Value *IntVal,
193                                      AggValueSlot ResultSlot,
194                                      SourceLocation Loc, bool AsValue) const;
195 
196     /// Copy an atomic r-value into atomic-layout memory.
197     void emitCopyIntoMemory(RValue rvalue) const;
198 
199     /// Project an l-value down to the value field.
200     LValue projectValue() const {
201       assert(LVal.isSimple());
202       Address addr = getAtomicAddress();
203       if (hasPadding())
204         addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
205 
206       return LValue::MakeAddr(addr, getValueType(), CGF.getContext(),
207                               LVal.getAlignmentSource(), LVal.getTBAAInfo());
208     }
209 
210     /// \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.
224     std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange(
225         RValue Expected, RValue Desired,
226         llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
227         llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
228         bool IsWeak = false);
229 
230     /// \brief Emits atomic update.
231     /// \param AO Atomic ordering.
232     /// \param UpdateOp Update operation for the current lvalue.
233     void EmitAtomicUpdate(llvm::AtomicOrdering AO,
234                           const llvm::function_ref<RValue(RValue)> &UpdateOp,
235                           bool IsVolatile);
236     /// \brief Emits atomic update.
237     /// \param AO Atomic ordering.
238     void EmitAtomicUpdate(llvm::AtomicOrdering AO, RValue UpdateRVal,
239                           bool IsVolatile);
240 
241     /// Materialize an atomic r-value in atomic-layout memory.
242     Address materializeRValue(RValue rvalue) const;
243 
244     /// \brief Translates LLVM atomic ordering to GNU atomic ordering for
245     /// libcalls.
246     static AtomicExpr::AtomicOrderingKind
247     translateAtomicOrdering(const llvm::AtomicOrdering AO);
248 
249     /// \brief Creates temp alloca for intermediate operations on atomic value.
250     Address CreateTempAlloca() const;
251   private:
252     bool requiresMemSetZero(llvm::Type *type) const;
253 
254 
255     /// \brief Emits atomic load as a libcall.
256     void EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
257                                llvm::AtomicOrdering AO, bool IsVolatile);
258     /// \brief Emits atomic load as LLVM instruction.
259     llvm::Value *EmitAtomicLoadOp(llvm::AtomicOrdering AO, bool IsVolatile);
260     /// \brief Emits atomic compare-and-exchange op as a libcall.
261     llvm::Value *EmitAtomicCompareExchangeLibcall(
262         llvm::Value *ExpectedAddr, llvm::Value *DesiredAddr,
263         llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
264         llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent);
265     /// \brief Emits atomic compare-and-exchange op as LLVM instruction.
266     std::pair<llvm::Value *, llvm::Value *> EmitAtomicCompareExchangeOp(
267         llvm::Value *ExpectedVal, llvm::Value *DesiredVal,
268         llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
269         llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
270         bool IsWeak = false);
271     /// \brief Emit atomic update as libcalls.
272     void
273     EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO,
274                             const llvm::function_ref<RValue(RValue)> &UpdateOp,
275                             bool IsVolatile);
276     /// \brief Emit atomic update as LLVM instructions.
277     void EmitAtomicUpdateOp(llvm::AtomicOrdering AO,
278                             const llvm::function_ref<RValue(RValue)> &UpdateOp,
279                             bool IsVolatile);
280     /// \brief Emit atomic update as libcalls.
281     void EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO, RValue UpdateRVal,
282                                  bool IsVolatile);
283     /// \brief Emit atomic update as LLVM instructions.
284     void EmitAtomicUpdateOp(llvm::AtomicOrdering AO, RValue UpdateRal,
285                             bool IsVolatile);
286   };
287 }
288 
289 AtomicExpr::AtomicOrderingKind
290 AtomicInfo::translateAtomicOrdering(const llvm::AtomicOrdering AO) {
291   switch (AO) {
292   case llvm::Unordered:
293   case llvm::NotAtomic:
294   case llvm::Monotonic:
295     return AtomicExpr::AO_ABI_memory_order_relaxed;
296   case llvm::Acquire:
297     return AtomicExpr::AO_ABI_memory_order_acquire;
298   case llvm::Release:
299     return AtomicExpr::AO_ABI_memory_order_release;
300   case llvm::AcquireRelease:
301     return AtomicExpr::AO_ABI_memory_order_acq_rel;
302   case llvm::SequentiallyConsistent:
303     return AtomicExpr::AO_ABI_memory_order_seq_cst;
304   }
305   llvm_unreachable("Unhandled AtomicOrdering");
306 }
307 
308 Address AtomicInfo::CreateTempAlloca() const {
309   Address TempAlloca = CGF.CreateMemTemp(
310       (LVal.isBitField() && ValueSizeInBits > AtomicSizeInBits) ? ValueTy
311                                                                 : AtomicTy,
312       getAtomicAlignment(),
313       "atomic-temp");
314   // Cast to pointer to value type for bitfields.
315   if (LVal.isBitField())
316     return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
317         TempAlloca, getAtomicAddress().getType());
318   return TempAlloca;
319 }
320 
321 static RValue emitAtomicLibcall(CodeGenFunction &CGF,
322                                 StringRef fnName,
323                                 QualType resultType,
324                                 CallArgList &args) {
325   const CGFunctionInfo &fnInfo =
326     CGF.CGM.getTypes().arrangeBuiltinFunctionCall(resultType, args);
327   llvm::FunctionType *fnTy = CGF.CGM.getTypes().GetFunctionType(fnInfo);
328   llvm::Constant *fn = CGF.CGM.CreateRuntimeFunction(fnTy, fnName);
329   return CGF.EmitCall(fnInfo, fn, ReturnValueSlot(), args);
330 }
331 
332 /// Does a store of the given IR type modify the full expected width?
333 static bool isFullSizeType(CodeGenModule &CGM, llvm::Type *type,
334                            uint64_t expectedSize) {
335   return (CGM.getDataLayout().getTypeStoreSize(type) * 8 == expectedSize);
336 }
337 
338 /// Does the atomic type require memsetting to zero before initialization?
339 ///
340 /// The IR type is provided as a way of making certain queries faster.
341 bool AtomicInfo::requiresMemSetZero(llvm::Type *type) const {
342   // If the atomic type has size padding, we definitely need a memset.
343   if (hasPadding()) return true;
344 
345   // Otherwise, do some simple heuristics to try to avoid it:
346   switch (getEvaluationKind()) {
347   // For scalars and complexes, check whether the store size of the
348   // type uses the full size.
349   case TEK_Scalar:
350     return !isFullSizeType(CGF.CGM, type, AtomicSizeInBits);
351   case TEK_Complex:
352     return !isFullSizeType(CGF.CGM, type->getStructElementType(0),
353                            AtomicSizeInBits / 2);
354 
355   // Padding in structs has an undefined bit pattern.  User beware.
356   case TEK_Aggregate:
357     return false;
358   }
359   llvm_unreachable("bad evaluation kind");
360 }
361 
362 bool AtomicInfo::emitMemSetZeroIfNecessary() const {
363   assert(LVal.isSimple());
364   llvm::Value *addr = LVal.getPointer();
365   if (!requiresMemSetZero(addr->getType()->getPointerElementType()))
366     return false;
367 
368   CGF.Builder.CreateMemSet(
369       addr, llvm::ConstantInt::get(CGF.Int8Ty, 0),
370       CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits).getQuantity(),
371       LVal.getAlignment().getQuantity());
372   return true;
373 }
374 
375 static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak,
376                               Address Dest, Address Ptr,
377                               Address Val1, Address Val2,
378                               uint64_t Size,
379                               llvm::AtomicOrdering SuccessOrder,
380                               llvm::AtomicOrdering FailureOrder) {
381   // Note that cmpxchg doesn't support weak cmpxchg, at least at the moment.
382   llvm::Value *Expected = CGF.Builder.CreateLoad(Val1);
383   llvm::Value *Desired = CGF.Builder.CreateLoad(Val2);
384 
385   llvm::AtomicCmpXchgInst *Pair = CGF.Builder.CreateAtomicCmpXchg(
386       Ptr.getPointer(), Expected, Desired, SuccessOrder, FailureOrder);
387   Pair->setVolatile(E->isVolatile());
388   Pair->setWeak(IsWeak);
389 
390   // Cmp holds the result of the compare-exchange operation: true on success,
391   // false on failure.
392   llvm::Value *Old = CGF.Builder.CreateExtractValue(Pair, 0);
393   llvm::Value *Cmp = CGF.Builder.CreateExtractValue(Pair, 1);
394 
395   // This basic block is used to hold the store instruction if the operation
396   // failed.
397   llvm::BasicBlock *StoreExpectedBB =
398       CGF.createBasicBlock("cmpxchg.store_expected", CGF.CurFn);
399 
400   // This basic block is the exit point of the operation, we should end up
401   // here regardless of whether or not the operation succeeded.
402   llvm::BasicBlock *ContinueBB =
403       CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
404 
405   // Update Expected if Expected isn't equal to Old, otherwise branch to the
406   // exit point.
407   CGF.Builder.CreateCondBr(Cmp, ContinueBB, StoreExpectedBB);
408 
409   CGF.Builder.SetInsertPoint(StoreExpectedBB);
410   // Update the memory at Expected with Old's value.
411   CGF.Builder.CreateStore(Old, Val1);
412   // Finally, branch to the exit point.
413   CGF.Builder.CreateBr(ContinueBB);
414 
415   CGF.Builder.SetInsertPoint(ContinueBB);
416   // Update the memory at Dest with Cmp's value.
417   CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType()));
418 }
419 
420 /// Given an ordering required on success, emit all possible cmpxchg
421 /// instructions to cope with the provided (but possibly only dynamically known)
422 /// FailureOrder.
423 static void emitAtomicCmpXchgFailureSet(CodeGenFunction &CGF, AtomicExpr *E,
424                                         bool IsWeak, Address Dest,
425                                         Address Ptr, Address Val1,
426                                         Address Val2,
427                                         llvm::Value *FailureOrderVal,
428                                         uint64_t Size,
429                                         llvm::AtomicOrdering SuccessOrder) {
430   llvm::AtomicOrdering FailureOrder;
431   if (llvm::ConstantInt *FO = dyn_cast<llvm::ConstantInt>(FailureOrderVal)) {
432     switch (FO->getSExtValue()) {
433     default:
434       FailureOrder = llvm::Monotonic;
435       break;
436     case AtomicExpr::AO_ABI_memory_order_consume:
437     case AtomicExpr::AO_ABI_memory_order_acquire:
438       FailureOrder = llvm::Acquire;
439       break;
440     case AtomicExpr::AO_ABI_memory_order_seq_cst:
441       FailureOrder = llvm::SequentiallyConsistent;
442       break;
443     }
444     if (FailureOrder >= SuccessOrder) {
445       // Don't assert on undefined behaviour.
446       FailureOrder =
447         llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrder);
448     }
449     emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2, Size,
450                       SuccessOrder, FailureOrder);
451     return;
452   }
453 
454   // Create all the relevant BB's
455   llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
456                    *SeqCstBB = nullptr;
457   MonotonicBB = CGF.createBasicBlock("monotonic_fail", CGF.CurFn);
458   if (SuccessOrder != llvm::Monotonic && SuccessOrder != llvm::Release)
459     AcquireBB = CGF.createBasicBlock("acquire_fail", CGF.CurFn);
460   if (SuccessOrder == llvm::SequentiallyConsistent)
461     SeqCstBB = CGF.createBasicBlock("seqcst_fail", CGF.CurFn);
462 
463   llvm::BasicBlock *ContBB = CGF.createBasicBlock("atomic.continue", CGF.CurFn);
464 
465   llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(FailureOrderVal, MonotonicBB);
466 
467   // Emit all the different atomics
468 
469   // MonotonicBB is arbitrarily chosen as the default case; in practice, this
470   // doesn't matter unless someone is crazy enough to use something that
471   // doesn't fold to a constant for the ordering.
472   CGF.Builder.SetInsertPoint(MonotonicBB);
473   emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
474                     Size, SuccessOrder, llvm::Monotonic);
475   CGF.Builder.CreateBr(ContBB);
476 
477   if (AcquireBB) {
478     CGF.Builder.SetInsertPoint(AcquireBB);
479     emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
480                       Size, SuccessOrder, llvm::Acquire);
481     CGF.Builder.CreateBr(ContBB);
482     SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_consume),
483                 AcquireBB);
484     SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acquire),
485                 AcquireBB);
486   }
487   if (SeqCstBB) {
488     CGF.Builder.SetInsertPoint(SeqCstBB);
489     emitAtomicCmpXchg(CGF, E, IsWeak, Dest, Ptr, Val1, Val2,
490                       Size, SuccessOrder, llvm::SequentiallyConsistent);
491     CGF.Builder.CreateBr(ContBB);
492     SI->addCase(CGF.Builder.getInt32(AtomicExpr::AO_ABI_memory_order_seq_cst),
493                 SeqCstBB);
494   }
495 
496   CGF.Builder.SetInsertPoint(ContBB);
497 }
498 
499 static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, Address Dest,
500                          Address Ptr, Address Val1, Address Val2,
501                          llvm::Value *IsWeak, llvm::Value *FailureOrder,
502                          uint64_t Size, llvm::AtomicOrdering Order) {
503   llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add;
504   llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0;
505 
506   switch (E->getOp()) {
507   case AtomicExpr::AO__c11_atomic_init:
508     llvm_unreachable("Already handled!");
509 
510   case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
511     emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
512                                 FailureOrder, Size, Order);
513     return;
514   case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
515     emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
516                                 FailureOrder, Size, Order);
517     return;
518   case AtomicExpr::AO__atomic_compare_exchange:
519   case AtomicExpr::AO__atomic_compare_exchange_n: {
520     if (llvm::ConstantInt *IsWeakC = dyn_cast<llvm::ConstantInt>(IsWeak)) {
521       emitAtomicCmpXchgFailureSet(CGF, E, IsWeakC->getZExtValue(), Dest, Ptr,
522                                   Val1, Val2, FailureOrder, Size, Order);
523     } else {
524       // Create all the relevant BB's
525       llvm::BasicBlock *StrongBB =
526           CGF.createBasicBlock("cmpxchg.strong", CGF.CurFn);
527       llvm::BasicBlock *WeakBB = CGF.createBasicBlock("cmxchg.weak", CGF.CurFn);
528       llvm::BasicBlock *ContBB =
529           CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
530 
531       llvm::SwitchInst *SI = CGF.Builder.CreateSwitch(IsWeak, WeakBB);
532       SI->addCase(CGF.Builder.getInt1(false), StrongBB);
533 
534       CGF.Builder.SetInsertPoint(StrongBB);
535       emitAtomicCmpXchgFailureSet(CGF, E, false, Dest, Ptr, Val1, Val2,
536                                   FailureOrder, Size, Order);
537       CGF.Builder.CreateBr(ContBB);
538 
539       CGF.Builder.SetInsertPoint(WeakBB);
540       emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
541                                   FailureOrder, Size, Order);
542       CGF.Builder.CreateBr(ContBB);
543 
544       CGF.Builder.SetInsertPoint(ContBB);
545     }
546     return;
547   }
548   case AtomicExpr::AO__c11_atomic_load:
549   case AtomicExpr::AO__atomic_load_n:
550   case AtomicExpr::AO__atomic_load: {
551     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr);
552     Load->setAtomic(Order);
553     Load->setVolatile(E->isVolatile());
554     CGF.Builder.CreateStore(Load, Dest);
555     return;
556   }
557 
558   case AtomicExpr::AO__c11_atomic_store:
559   case AtomicExpr::AO__atomic_store:
560   case AtomicExpr::AO__atomic_store_n: {
561     llvm::Value *LoadVal1 = CGF.Builder.CreateLoad(Val1);
562     llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr);
563     Store->setAtomic(Order);
564     Store->setVolatile(E->isVolatile());
565     return;
566   }
567 
568   case AtomicExpr::AO__c11_atomic_exchange:
569   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:
578   case AtomicExpr::AO__atomic_fetch_add:
579     Op = llvm::AtomicRMWInst::Add;
580     break;
581 
582   case AtomicExpr::AO__atomic_sub_fetch:
583     PostOp = llvm::Instruction::Sub;
584     // Fall through.
585   case AtomicExpr::AO__c11_atomic_fetch_sub:
586   case AtomicExpr::AO__atomic_fetch_sub:
587     Op = llvm::AtomicRMWInst::Sub;
588     break;
589 
590   case AtomicExpr::AO__atomic_and_fetch:
591     PostOp = llvm::Instruction::And;
592     // Fall through.
593   case AtomicExpr::AO__c11_atomic_fetch_and:
594   case AtomicExpr::AO__atomic_fetch_and:
595     Op = llvm::AtomicRMWInst::And;
596     break;
597 
598   case AtomicExpr::AO__atomic_or_fetch:
599     PostOp = llvm::Instruction::Or;
600     // Fall through.
601   case AtomicExpr::AO__c11_atomic_fetch_or:
602   case AtomicExpr::AO__atomic_fetch_or:
603     Op = llvm::AtomicRMWInst::Or;
604     break;
605 
606   case AtomicExpr::AO__atomic_xor_fetch:
607     PostOp = llvm::Instruction::Xor;
608     // Fall through.
609   case AtomicExpr::AO__c11_atomic_fetch_xor:
610   case AtomicExpr::AO__atomic_fetch_xor:
611     Op = llvm::AtomicRMWInst::Xor;
612     break;
613 
614   case AtomicExpr::AO__atomic_nand_fetch:
615     PostOp = llvm::Instruction::And; // the NOT is special cased below
616   // Fall through.
617   case AtomicExpr::AO__atomic_fetch_nand:
618     Op = llvm::AtomicRMWInst::Nand;
619     break;
620   }
621 
622   llvm::Value *LoadVal1 = CGF.Builder.CreateLoad(Val1);
623   llvm::AtomicRMWInst *RMWI =
624       CGF.Builder.CreateAtomicRMW(Op, Ptr.getPointer(), LoadVal1, Order);
625   RMWI->setVolatile(E->isVolatile());
626 
627   // For __atomic_*_fetch operations, perform the operation again to
628   // determine the value which was written.
629   llvm::Value *Result = RMWI;
630   if (PostOp)
631     Result = CGF.Builder.CreateBinOp(PostOp, RMWI, LoadVal1);
632   if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch)
633     Result = CGF.Builder.CreateNot(Result);
634   CGF.Builder.CreateStore(Result, Dest);
635 }
636 
637 // This function emits any expression (scalar, complex, or aggregate)
638 // into a temporary alloca.
639 static Address
640 EmitValToTemp(CodeGenFunction &CGF, Expr *E) {
641   Address DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp");
642   CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(),
643                        /*Init*/ true);
644   return DeclPtr;
645 }
646 
647 static void
648 AddDirectArgument(CodeGenFunction &CGF, CallArgList &Args,
649                   bool UseOptimizedLibcall, llvm::Value *Val, QualType ValTy,
650                   SourceLocation Loc, CharUnits SizeInChars) {
651   if (UseOptimizedLibcall) {
652     // Load value and pass it to the function directly.
653     CharUnits Align = CGF.getContext().getTypeAlignInChars(ValTy);
654     int64_t SizeInBits = CGF.getContext().toBits(SizeInChars);
655     ValTy =
656         CGF.getContext().getIntTypeForBitwidth(SizeInBits, /*Signed=*/false);
657     llvm::Type *IPtrTy = llvm::IntegerType::get(CGF.getLLVMContext(),
658                                                 SizeInBits)->getPointerTo();
659     Address Ptr = Address(CGF.Builder.CreateBitCast(Val, IPtrTy), Align);
660     Val = CGF.EmitLoadOfScalar(Ptr, false,
661                                CGF.getContext().getPointerType(ValTy),
662                                Loc);
663     // Coerce the value into an appropriately sized integer type.
664     Args.add(RValue::get(Val), ValTy);
665   } else {
666     // Non-optimized functions always take a reference.
667     Args.add(RValue::get(CGF.EmitCastToVoidPtr(Val)),
668                          CGF.getContext().VoidPtrTy);
669   }
670 }
671 
672 RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
673   QualType AtomicTy = E->getPtr()->getType()->getPointeeType();
674   QualType MemTy = AtomicTy;
675   if (const AtomicType *AT = AtomicTy->getAs<AtomicType>())
676     MemTy = AT->getValueType();
677   CharUnits sizeChars, alignChars;
678   std::tie(sizeChars, alignChars) = getContext().getTypeInfoInChars(AtomicTy);
679   uint64_t Size = sizeChars.getQuantity();
680   unsigned MaxInlineWidthInBits = getTarget().getMaxAtomicInlineWidth();
681   bool UseLibcall = (sizeChars != alignChars ||
682                      getContext().toBits(sizeChars) > MaxInlineWidthInBits);
683 
684   llvm::Value *IsWeak = nullptr, *OrderFail = nullptr;
685 
686   Address Val1 = Address::invalid();
687   Address Val2 = Address::invalid();
688   Address Dest = Address::invalid();
689   Address Ptr(EmitScalarExpr(E->getPtr()), alignChars);
690 
691   if (E->getOp() == AtomicExpr::AO__c11_atomic_init) {
692     LValue lvalue = MakeAddrLValue(Ptr, AtomicTy);
693     EmitAtomicInit(E->getVal1(), lvalue);
694     return RValue::get(nullptr);
695   }
696 
697   llvm::Value *Order = EmitScalarExpr(E->getOrder());
698 
699   switch (E->getOp()) {
700   case AtomicExpr::AO__c11_atomic_init:
701     llvm_unreachable("Already handled above with EmitAtomicInit!");
702 
703   case AtomicExpr::AO__c11_atomic_load:
704   case AtomicExpr::AO__atomic_load_n:
705     break;
706 
707   case AtomicExpr::AO__atomic_load:
708     Dest = EmitPointerWithAlignment(E->getVal1());
709     break;
710 
711   case AtomicExpr::AO__atomic_store:
712     Val1 = EmitPointerWithAlignment(E->getVal1());
713     break;
714 
715   case AtomicExpr::AO__atomic_exchange:
716     Val1 = EmitPointerWithAlignment(E->getVal1());
717     Dest = EmitPointerWithAlignment(E->getVal2());
718     break;
719 
720   case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
721   case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
722   case AtomicExpr::AO__atomic_compare_exchange_n:
723   case AtomicExpr::AO__atomic_compare_exchange:
724     Val1 = EmitPointerWithAlignment(E->getVal1());
725     if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange)
726       Val2 = EmitPointerWithAlignment(E->getVal2());
727     else
728       Val2 = EmitValToTemp(*this, E->getVal2());
729     OrderFail = EmitScalarExpr(E->getOrderFail());
730     if (E->getNumSubExprs() == 6)
731       IsWeak = EmitScalarExpr(E->getWeak());
732     break;
733 
734   case AtomicExpr::AO__c11_atomic_fetch_add:
735   case AtomicExpr::AO__c11_atomic_fetch_sub:
736     if (MemTy->isPointerType()) {
737       // For pointer arithmetic, we're required to do a bit of math:
738       // adding 1 to an int* is not the same as adding 1 to a uintptr_t.
739       // ... but only for the C11 builtins. The GNU builtins expect the
740       // user to multiply by sizeof(T).
741       QualType Val1Ty = E->getVal1()->getType();
742       llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1());
743       CharUnits PointeeIncAmt =
744           getContext().getTypeSizeInChars(MemTy->getPointeeType());
745       Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt));
746       auto Temp = CreateMemTemp(Val1Ty, ".atomictmp");
747       Val1 = Temp;
748       EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Temp, Val1Ty));
749       break;
750     }
751     // Fall through.
752   case AtomicExpr::AO__atomic_fetch_add:
753   case AtomicExpr::AO__atomic_fetch_sub:
754   case AtomicExpr::AO__atomic_add_fetch:
755   case AtomicExpr::AO__atomic_sub_fetch:
756   case AtomicExpr::AO__c11_atomic_store:
757   case AtomicExpr::AO__c11_atomic_exchange:
758   case AtomicExpr::AO__atomic_store_n:
759   case AtomicExpr::AO__atomic_exchange_n:
760   case AtomicExpr::AO__c11_atomic_fetch_and:
761   case AtomicExpr::AO__c11_atomic_fetch_or:
762   case AtomicExpr::AO__c11_atomic_fetch_xor:
763   case AtomicExpr::AO__atomic_fetch_and:
764   case AtomicExpr::AO__atomic_fetch_or:
765   case AtomicExpr::AO__atomic_fetch_xor:
766   case AtomicExpr::AO__atomic_fetch_nand:
767   case AtomicExpr::AO__atomic_and_fetch:
768   case AtomicExpr::AO__atomic_or_fetch:
769   case AtomicExpr::AO__atomic_xor_fetch:
770   case AtomicExpr::AO__atomic_nand_fetch:
771     Val1 = EmitValToTemp(*this, E->getVal1());
772     break;
773   }
774 
775   QualType RValTy = E->getType().getUnqualifiedType();
776 
777   // The inlined atomics only function on iN types, where N is a power of 2. We
778   // need to make sure (via temporaries if necessary) that all incoming values
779   // are compatible.
780   LValue AtomicVal = MakeAddrLValue(Ptr, AtomicTy);
781   AtomicInfo Atomics(*this, AtomicVal);
782 
783   Ptr = Atomics.emitCastToAtomicIntPointer(Ptr);
784   if (Val1.isValid()) Val1 = Atomics.convertToAtomicIntPointer(Val1);
785   if (Val2.isValid()) Val2 = Atomics.convertToAtomicIntPointer(Val2);
786   if (Dest.isValid())
787     Dest = Atomics.emitCastToAtomicIntPointer(Dest);
788   else if (E->isCmpXChg())
789     Dest = CreateMemTemp(RValTy, "cmpxchg.bool");
790   else if (!RValTy->isVoidType())
791     Dest = Atomics.emitCastToAtomicIntPointer(Atomics.CreateTempAlloca());
792 
793   // Use a library call.  See: http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary .
794   if (UseLibcall) {
795     bool UseOptimizedLibcall = false;
796     switch (E->getOp()) {
797     case AtomicExpr::AO__c11_atomic_init:
798       llvm_unreachable("Already handled above with EmitAtomicInit!");
799 
800     case AtomicExpr::AO__c11_atomic_fetch_add:
801     case AtomicExpr::AO__atomic_fetch_add:
802     case AtomicExpr::AO__c11_atomic_fetch_and:
803     case AtomicExpr::AO__atomic_fetch_and:
804     case AtomicExpr::AO__c11_atomic_fetch_or:
805     case AtomicExpr::AO__atomic_fetch_or:
806     case AtomicExpr::AO__atomic_fetch_nand:
807     case AtomicExpr::AO__c11_atomic_fetch_sub:
808     case AtomicExpr::AO__atomic_fetch_sub:
809     case AtomicExpr::AO__c11_atomic_fetch_xor:
810     case AtomicExpr::AO__atomic_fetch_xor:
811     case AtomicExpr::AO__atomic_add_fetch:
812     case AtomicExpr::AO__atomic_and_fetch:
813     case AtomicExpr::AO__atomic_nand_fetch:
814     case AtomicExpr::AO__atomic_or_fetch:
815     case AtomicExpr::AO__atomic_sub_fetch:
816     case AtomicExpr::AO__atomic_xor_fetch:
817       // For these, only library calls for certain sizes exist.
818       UseOptimizedLibcall = true;
819       break;
820 
821     case AtomicExpr::AO__c11_atomic_load:
822     case AtomicExpr::AO__c11_atomic_store:
823     case AtomicExpr::AO__c11_atomic_exchange:
824     case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
825     case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
826     case AtomicExpr::AO__atomic_load_n:
827     case AtomicExpr::AO__atomic_load:
828     case AtomicExpr::AO__atomic_store_n:
829     case AtomicExpr::AO__atomic_store:
830     case AtomicExpr::AO__atomic_exchange_n:
831     case AtomicExpr::AO__atomic_exchange:
832     case AtomicExpr::AO__atomic_compare_exchange_n:
833     case AtomicExpr::AO__atomic_compare_exchange:
834       // Only use optimized library calls for sizes for which they exist.
835       if (Size == 1 || Size == 2 || Size == 4 || Size == 8)
836         UseOptimizedLibcall = true;
837       break;
838     }
839 
840     CallArgList Args;
841     if (!UseOptimizedLibcall) {
842       // For non-optimized library calls, the size is the first parameter
843       Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)),
844                getContext().getSizeType());
845     }
846     // Atomic address is the first or second parameter
847     Args.add(RValue::get(EmitCastToVoidPtr(Ptr.getPointer())),
848              getContext().VoidPtrTy);
849 
850     std::string LibCallName;
851     QualType LoweredMemTy =
852       MemTy->isPointerType() ? getContext().getIntPtrType() : MemTy;
853     QualType RetTy;
854     bool HaveRetTy = false;
855     llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0;
856     switch (E->getOp()) {
857     case AtomicExpr::AO__c11_atomic_init:
858       llvm_unreachable("Already handled!");
859 
860     // There is only one libcall for compare an exchange, because there is no
861     // optimisation benefit possible from a libcall version of a weak compare
862     // and exchange.
863     // bool __atomic_compare_exchange(size_t size, void *mem, void *expected,
864     //                                void *desired, int success, int failure)
865     // bool __atomic_compare_exchange_N(T *mem, T *expected, T desired,
866     //                                  int success, int failure)
867     case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
868     case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
869     case AtomicExpr::AO__atomic_compare_exchange:
870     case AtomicExpr::AO__atomic_compare_exchange_n:
871       LibCallName = "__atomic_compare_exchange";
872       RetTy = getContext().BoolTy;
873       HaveRetTy = true;
874       Args.add(RValue::get(EmitCastToVoidPtr(Val1.getPointer())),
875                getContext().VoidPtrTy);
876       AddDirectArgument(*this, Args, UseOptimizedLibcall, Val2.getPointer(),
877                         MemTy, E->getExprLoc(), sizeChars);
878       Args.add(RValue::get(Order), getContext().IntTy);
879       Order = OrderFail;
880       break;
881     // void __atomic_exchange(size_t size, void *mem, void *val, void *return,
882     //                        int order)
883     // T __atomic_exchange_N(T *mem, T val, int order)
884     case AtomicExpr::AO__c11_atomic_exchange:
885     case AtomicExpr::AO__atomic_exchange_n:
886     case AtomicExpr::AO__atomic_exchange:
887       LibCallName = "__atomic_exchange";
888       AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
889                         MemTy, E->getExprLoc(), sizeChars);
890       break;
891     // void __atomic_store(size_t size, void *mem, void *val, int order)
892     // void __atomic_store_N(T *mem, T val, int order)
893     case AtomicExpr::AO__c11_atomic_store:
894     case AtomicExpr::AO__atomic_store:
895     case AtomicExpr::AO__atomic_store_n:
896       LibCallName = "__atomic_store";
897       RetTy = getContext().VoidTy;
898       HaveRetTy = true;
899       AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
900                         MemTy, E->getExprLoc(), sizeChars);
901       break;
902     // void __atomic_load(size_t size, void *mem, void *return, int order)
903     // T __atomic_load_N(T *mem, int order)
904     case AtomicExpr::AO__c11_atomic_load:
905     case AtomicExpr::AO__atomic_load:
906     case AtomicExpr::AO__atomic_load_n:
907       LibCallName = "__atomic_load";
908       break;
909     // T __atomic_add_fetch_N(T *mem, T val, int order)
910     // T __atomic_fetch_add_N(T *mem, T val, int order)
911     case AtomicExpr::AO__atomic_add_fetch:
912       PostOp = llvm::Instruction::Add;
913     // Fall through.
914     case AtomicExpr::AO__c11_atomic_fetch_add:
915     case AtomicExpr::AO__atomic_fetch_add:
916       LibCallName = "__atomic_fetch_add";
917       AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
918                         LoweredMemTy, E->getExprLoc(), sizeChars);
919       break;
920     // T __atomic_and_fetch_N(T *mem, T val, int order)
921     // T __atomic_fetch_and_N(T *mem, T val, int order)
922     case AtomicExpr::AO__atomic_and_fetch:
923       PostOp = llvm::Instruction::And;
924     // Fall through.
925     case AtomicExpr::AO__c11_atomic_fetch_and:
926     case AtomicExpr::AO__atomic_fetch_and:
927       LibCallName = "__atomic_fetch_and";
928       AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
929                         MemTy, E->getExprLoc(), sizeChars);
930       break;
931     // T __atomic_or_fetch_N(T *mem, T val, int order)
932     // T __atomic_fetch_or_N(T *mem, T val, int order)
933     case AtomicExpr::AO__atomic_or_fetch:
934       PostOp = llvm::Instruction::Or;
935     // Fall through.
936     case AtomicExpr::AO__c11_atomic_fetch_or:
937     case AtomicExpr::AO__atomic_fetch_or:
938       LibCallName = "__atomic_fetch_or";
939       AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
940                         MemTy, E->getExprLoc(), sizeChars);
941       break;
942     // T __atomic_sub_fetch_N(T *mem, T val, int order)
943     // T __atomic_fetch_sub_N(T *mem, T val, int order)
944     case AtomicExpr::AO__atomic_sub_fetch:
945       PostOp = llvm::Instruction::Sub;
946     // Fall through.
947     case AtomicExpr::AO__c11_atomic_fetch_sub:
948     case AtomicExpr::AO__atomic_fetch_sub:
949       LibCallName = "__atomic_fetch_sub";
950       AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
951                         LoweredMemTy, E->getExprLoc(), sizeChars);
952       break;
953     // T __atomic_xor_fetch_N(T *mem, T val, int order)
954     // T __atomic_fetch_xor_N(T *mem, T val, int order)
955     case AtomicExpr::AO__atomic_xor_fetch:
956       PostOp = llvm::Instruction::Xor;
957     // Fall through.
958     case AtomicExpr::AO__c11_atomic_fetch_xor:
959     case AtomicExpr::AO__atomic_fetch_xor:
960       LibCallName = "__atomic_fetch_xor";
961       AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
962                         MemTy, E->getExprLoc(), sizeChars);
963       break;
964     // T __atomic_nand_fetch_N(T *mem, T val, int order)
965     // T __atomic_fetch_nand_N(T *mem, T val, int order)
966     case AtomicExpr::AO__atomic_nand_fetch:
967       PostOp = llvm::Instruction::And; // the NOT is special cased below
968     // Fall through.
969     case AtomicExpr::AO__atomic_fetch_nand:
970       LibCallName = "__atomic_fetch_nand";
971       AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1.getPointer(),
972                         MemTy, E->getExprLoc(), sizeChars);
973       break;
974     }
975 
976     // Optimized functions have the size in their name.
977     if (UseOptimizedLibcall)
978       LibCallName += "_" + llvm::utostr(Size);
979     // By default, assume we return a value of the atomic type.
980     if (!HaveRetTy) {
981       if (UseOptimizedLibcall) {
982         // Value is returned directly.
983         // The function returns an appropriately sized integer type.
984         RetTy = getContext().getIntTypeForBitwidth(
985             getContext().toBits(sizeChars), /*Signed=*/false);
986       } else {
987         // Value is returned through parameter before the order.
988         RetTy = getContext().VoidTy;
989         Args.add(RValue::get(EmitCastToVoidPtr(Dest.getPointer())),
990                  getContext().VoidPtrTy);
991       }
992     }
993     // order is always the last parameter
994     Args.add(RValue::get(Order),
995              getContext().IntTy);
996 
997     // PostOp is only needed for the atomic_*_fetch operations, and
998     // thus is only needed for and implemented in the
999     // UseOptimizedLibcall codepath.
1000     assert(UseOptimizedLibcall || !PostOp);
1001 
1002     RValue Res = emitAtomicLibcall(*this, LibCallName, RetTy, Args);
1003     // The value is returned directly from the libcall.
1004     if (E->isCmpXChg())
1005       return Res;
1006 
1007     // The value is returned directly for optimized libcalls but the expr
1008     // provided an out-param.
1009     if (UseOptimizedLibcall && Res.getScalarVal()) {
1010       llvm::Value *ResVal = Res.getScalarVal();
1011       if (PostOp) {
1012         llvm::Value *LoadVal1 = Args[1].RV.getScalarVal();
1013         ResVal = Builder.CreateBinOp(PostOp, ResVal, LoadVal1);
1014       }
1015       if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch)
1016         ResVal = Builder.CreateNot(ResVal);
1017 
1018       Builder.CreateStore(
1019           ResVal,
1020           Builder.CreateBitCast(Dest, ResVal->getType()->getPointerTo()));
1021     }
1022 
1023     if (RValTy->isVoidType())
1024       return RValue::get(nullptr);
1025 
1026     return convertTempToRValue(
1027         Builder.CreateBitCast(Dest, ConvertTypeForMem(RValTy)->getPointerTo()),
1028         RValTy, E->getExprLoc());
1029   }
1030 
1031   bool IsStore = E->getOp() == AtomicExpr::AO__c11_atomic_store ||
1032                  E->getOp() == AtomicExpr::AO__atomic_store ||
1033                  E->getOp() == AtomicExpr::AO__atomic_store_n;
1034   bool IsLoad = E->getOp() == AtomicExpr::AO__c11_atomic_load ||
1035                 E->getOp() == AtomicExpr::AO__atomic_load ||
1036                 E->getOp() == AtomicExpr::AO__atomic_load_n;
1037 
1038   if (isa<llvm::ConstantInt>(Order)) {
1039     int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
1040     switch (ord) {
1041     case AtomicExpr::AO_ABI_memory_order_relaxed:
1042       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1043                    Size, llvm::Monotonic);
1044       break;
1045     case AtomicExpr::AO_ABI_memory_order_consume:
1046     case AtomicExpr::AO_ABI_memory_order_acquire:
1047       if (IsStore)
1048         break; // Avoid crashing on code with undefined behavior
1049       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1050                    Size, llvm::Acquire);
1051       break;
1052     case AtomicExpr::AO_ABI_memory_order_release:
1053       if (IsLoad)
1054         break; // Avoid crashing on code with undefined behavior
1055       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1056                    Size, llvm::Release);
1057       break;
1058     case AtomicExpr::AO_ABI_memory_order_acq_rel:
1059       if (IsLoad || IsStore)
1060         break; // Avoid crashing on code with undefined behavior
1061       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1062                    Size, llvm::AcquireRelease);
1063       break;
1064     case AtomicExpr::AO_ABI_memory_order_seq_cst:
1065       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1066                    Size, llvm::SequentiallyConsistent);
1067       break;
1068     default: // invalid order
1069       // We should not ever get here normally, but it's hard to
1070       // enforce that in general.
1071       break;
1072     }
1073     if (RValTy->isVoidType())
1074       return RValue::get(nullptr);
1075 
1076     return convertTempToRValue(
1077         Builder.CreateBitCast(Dest, ConvertTypeForMem(RValTy)->getPointerTo()),
1078         RValTy, E->getExprLoc());
1079   }
1080 
1081   // Long case, when Order isn't obviously constant.
1082 
1083   // Create all the relevant BB's
1084   llvm::BasicBlock *MonotonicBB = nullptr, *AcquireBB = nullptr,
1085                    *ReleaseBB = nullptr, *AcqRelBB = nullptr,
1086                    *SeqCstBB = nullptr;
1087   MonotonicBB = createBasicBlock("monotonic", CurFn);
1088   if (!IsStore)
1089     AcquireBB = createBasicBlock("acquire", CurFn);
1090   if (!IsLoad)
1091     ReleaseBB = createBasicBlock("release", CurFn);
1092   if (!IsLoad && !IsStore)
1093     AcqRelBB = createBasicBlock("acqrel", CurFn);
1094   SeqCstBB = createBasicBlock("seqcst", CurFn);
1095   llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
1096 
1097   // Create the switch for the split
1098   // MonotonicBB is arbitrarily chosen as the default case; in practice, this
1099   // doesn't matter unless someone is crazy enough to use something that
1100   // doesn't fold to a constant for the ordering.
1101   Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
1102   llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB);
1103 
1104   // Emit all the different atomics
1105   Builder.SetInsertPoint(MonotonicBB);
1106   EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1107                Size, llvm::Monotonic);
1108   Builder.CreateBr(ContBB);
1109   if (!IsStore) {
1110     Builder.SetInsertPoint(AcquireBB);
1111     EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1112                  Size, llvm::Acquire);
1113     Builder.CreateBr(ContBB);
1114     SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_consume),
1115                 AcquireBB);
1116     SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acquire),
1117                 AcquireBB);
1118   }
1119   if (!IsLoad) {
1120     Builder.SetInsertPoint(ReleaseBB);
1121     EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1122                  Size, llvm::Release);
1123     Builder.CreateBr(ContBB);
1124     SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_release),
1125                 ReleaseBB);
1126   }
1127   if (!IsLoad && !IsStore) {
1128     Builder.SetInsertPoint(AcqRelBB);
1129     EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1130                  Size, llvm::AcquireRelease);
1131     Builder.CreateBr(ContBB);
1132     SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_acq_rel),
1133                 AcqRelBB);
1134   }
1135   Builder.SetInsertPoint(SeqCstBB);
1136   EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, IsWeak, OrderFail,
1137                Size, llvm::SequentiallyConsistent);
1138   Builder.CreateBr(ContBB);
1139   SI->addCase(Builder.getInt32(AtomicExpr::AO_ABI_memory_order_seq_cst),
1140               SeqCstBB);
1141 
1142   // Cleanup and return
1143   Builder.SetInsertPoint(ContBB);
1144   if (RValTy->isVoidType())
1145     return RValue::get(nullptr);
1146 
1147   assert(Atomics.getValueSizeInBits() <= Atomics.getAtomicSizeInBits());
1148   return convertTempToRValue(
1149       Builder.CreateBitCast(Dest, ConvertTypeForMem(RValTy)->getPointerTo()),
1150       RValTy, E->getExprLoc());
1151 }
1152 
1153 Address AtomicInfo::emitCastToAtomicIntPointer(Address addr) const {
1154   unsigned addrspace =
1155     cast<llvm::PointerType>(addr.getPointer()->getType())->getAddressSpace();
1156   llvm::IntegerType *ty =
1157     llvm::IntegerType::get(CGF.getLLVMContext(), AtomicSizeInBits);
1158   return CGF.Builder.CreateBitCast(addr, ty->getPointerTo(addrspace));
1159 }
1160 
1161 Address AtomicInfo::convertToAtomicIntPointer(Address Addr) const {
1162   llvm::Type *Ty = Addr.getElementType();
1163   uint64_t SourceSizeInBits = CGF.CGM.getDataLayout().getTypeSizeInBits(Ty);
1164   if (SourceSizeInBits != AtomicSizeInBits) {
1165     Address Tmp = CreateTempAlloca();
1166     CGF.Builder.CreateMemCpy(Tmp, Addr,
1167                              std::min(AtomicSizeInBits, SourceSizeInBits) / 8);
1168     Addr = Tmp;
1169   }
1170 
1171   return emitCastToAtomicIntPointer(Addr);
1172 }
1173 
1174 RValue AtomicInfo::convertAtomicTempToRValue(Address addr,
1175                                              AggValueSlot resultSlot,
1176                                              SourceLocation loc,
1177                                              bool asValue) const {
1178   if (LVal.isSimple()) {
1179     if (EvaluationKind == TEK_Aggregate)
1180       return resultSlot.asRValue();
1181 
1182     // Drill into the padding structure if we have one.
1183     if (hasPadding())
1184       addr = CGF.Builder.CreateStructGEP(addr, 0, CharUnits());
1185 
1186     // Otherwise, just convert the temporary to an r-value using the
1187     // normal conversion routine.
1188     return CGF.convertTempToRValue(addr, getValueType(), loc);
1189   }
1190   if (!asValue)
1191     // Get RValue from temp memory as atomic for non-simple lvalues
1192     return RValue::get(CGF.Builder.CreateLoad(addr));
1193   if (LVal.isBitField())
1194     return CGF.EmitLoadOfBitfieldLValue(
1195         LValue::MakeBitfield(addr, LVal.getBitFieldInfo(), LVal.getType(),
1196                              LVal.getAlignmentSource()));
1197   if (LVal.isVectorElt())
1198     return CGF.EmitLoadOfLValue(
1199         LValue::MakeVectorElt(addr, LVal.getVectorIdx(), LVal.getType(),
1200                               LVal.getAlignmentSource()), loc);
1201   assert(LVal.isExtVectorElt());
1202   return CGF.EmitLoadOfExtVectorElementLValue(LValue::MakeExtVectorElt(
1203       addr, LVal.getExtVectorElts(), LVal.getType(),
1204       LVal.getAlignmentSource()));
1205 }
1206 
1207 RValue AtomicInfo::ConvertIntToValueOrAtomic(llvm::Value *IntVal,
1208                                              AggValueSlot ResultSlot,
1209                                              SourceLocation Loc,
1210                                              bool AsValue) const {
1211   // Try not to in some easy cases.
1212   assert(IntVal->getType()->isIntegerTy() && "Expected integer value");
1213   if (getEvaluationKind() == TEK_Scalar &&
1214       (((!LVal.isBitField() ||
1215          LVal.getBitFieldInfo().Size == ValueSizeInBits) &&
1216         !hasPadding()) ||
1217        !AsValue)) {
1218     auto *ValTy = AsValue
1219                       ? CGF.ConvertTypeForMem(ValueTy)
1220                       : getAtomicAddress().getType()->getPointerElementType();
1221     if (ValTy->isIntegerTy()) {
1222       assert(IntVal->getType() == ValTy && "Different integer types.");
1223       return RValue::get(CGF.EmitFromMemory(IntVal, ValueTy));
1224     } else if (ValTy->isPointerTy())
1225       return RValue::get(CGF.Builder.CreateIntToPtr(IntVal, ValTy));
1226     else if (llvm::CastInst::isBitCastable(IntVal->getType(), ValTy))
1227       return RValue::get(CGF.Builder.CreateBitCast(IntVal, ValTy));
1228   }
1229 
1230   // Create a temporary.  This needs to be big enough to hold the
1231   // atomic integer.
1232   Address Temp = Address::invalid();
1233   bool TempIsVolatile = false;
1234   if (AsValue && getEvaluationKind() == TEK_Aggregate) {
1235     assert(!ResultSlot.isIgnored());
1236     Temp = ResultSlot.getAddress();
1237     TempIsVolatile = ResultSlot.isVolatile();
1238   } else {
1239     Temp = CreateTempAlloca();
1240   }
1241 
1242   // Slam the integer into the temporary.
1243   Address CastTemp = emitCastToAtomicIntPointer(Temp);
1244   CGF.Builder.CreateStore(IntVal, CastTemp)
1245       ->setVolatile(TempIsVolatile);
1246 
1247   return convertAtomicTempToRValue(Temp, ResultSlot, Loc, AsValue);
1248 }
1249 
1250 void AtomicInfo::EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
1251                                        llvm::AtomicOrdering AO, bool) {
1252   // void __atomic_load(size_t size, void *mem, void *return, int order);
1253   CallArgList Args;
1254   Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
1255   Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
1256            CGF.getContext().VoidPtrTy);
1257   Args.add(RValue::get(CGF.EmitCastToVoidPtr(AddForLoaded)),
1258            CGF.getContext().VoidPtrTy);
1259   Args.add(RValue::get(
1260                llvm::ConstantInt::get(CGF.IntTy, translateAtomicOrdering(AO))),
1261            CGF.getContext().IntTy);
1262   emitAtomicLibcall(CGF, "__atomic_load", CGF.getContext().VoidTy, Args);
1263 }
1264 
1265 llvm::Value *AtomicInfo::EmitAtomicLoadOp(llvm::AtomicOrdering AO,
1266                                           bool IsVolatile) {
1267   // Okay, we're doing this natively.
1268   Address Addr = getAtomicAddressAsAtomicIntPointer();
1269   llvm::LoadInst *Load = CGF.Builder.CreateLoad(Addr, "atomic-load");
1270   Load->setAtomic(AO);
1271 
1272   // Other decoration.
1273   if (IsVolatile)
1274     Load->setVolatile(true);
1275   if (LVal.getTBAAInfo())
1276     CGF.CGM.DecorateInstructionWithTBAA(Load, LVal.getTBAAInfo());
1277   return Load;
1278 }
1279 
1280 /// An LValue is a candidate for having its loads and stores be made atomic if
1281 /// we are operating under /volatile:ms *and* the LValue itself is volatile and
1282 /// performing such an operation can be performed without a libcall.
1283 bool CodeGenFunction::LValueIsSuitableForInlineAtomic(LValue LV) {
1284   if (!CGM.getCodeGenOpts().MSVolatile) return false;
1285   AtomicInfo AI(*this, LV);
1286   bool IsVolatile = LV.isVolatile() || hasVolatileMember(LV.getType());
1287   // An atomic is inline if we don't need to use a libcall.
1288   bool AtomicIsInline = !AI.shouldUseLibcall();
1289   return IsVolatile && AtomicIsInline;
1290 }
1291 
1292 /// An type is a candidate for having its loads and stores be made atomic if
1293 /// we are operating under /volatile:ms *and* we know the access is volatile and
1294 /// performing such an operation can be performed without a libcall.
1295 bool CodeGenFunction::typeIsSuitableForInlineAtomic(QualType Ty,
1296                                                     bool IsVolatile) const {
1297   // The operation must be volatile for us to make it atomic.
1298   if (!IsVolatile)
1299     return false;
1300   // The -fms-volatile flag must be passed for us to adopt this behavior.
1301   if (!CGM.getCodeGenOpts().MSVolatile)
1302     return false;
1303 
1304   // An atomic is inline if we don't need to use a libcall (e.g. it is builtin).
1305   if (!getContext().getTargetInfo().hasBuiltinAtomic(
1306           getContext().getTypeSize(Ty), getContext().getTypeAlign(Ty)))
1307     return false;
1308 
1309   // MSVC doesn't seem to do this for types wider than a pointer.
1310   if (getContext().getTypeSize(Ty) >
1311       getContext().getTypeSize(getContext().getIntPtrType()))
1312     return false;
1313   return true;
1314 }
1315 
1316 RValue CodeGenFunction::EmitAtomicLoad(LValue LV, SourceLocation SL,
1317                                        AggValueSlot Slot) {
1318   llvm::AtomicOrdering AO;
1319   bool IsVolatile = LV.isVolatileQualified();
1320   if (LV.getType()->isAtomicType()) {
1321     AO = llvm::SequentiallyConsistent;
1322   } else {
1323     AO = llvm::Acquire;
1324     IsVolatile = true;
1325   }
1326   return EmitAtomicLoad(LV, SL, AO, IsVolatile, Slot);
1327 }
1328 
1329 RValue AtomicInfo::EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
1330                                   bool AsValue, llvm::AtomicOrdering AO,
1331                                   bool IsVolatile) {
1332   // Check whether we should use a library call.
1333   if (shouldUseLibcall()) {
1334     Address TempAddr = Address::invalid();
1335     if (LVal.isSimple() && !ResultSlot.isIgnored()) {
1336       assert(getEvaluationKind() == TEK_Aggregate);
1337       TempAddr = ResultSlot.getAddress();
1338     } else
1339       TempAddr = CreateTempAlloca();
1340 
1341     EmitAtomicLoadLibcall(TempAddr.getPointer(), AO, IsVolatile);
1342 
1343     // Okay, turn that back into the original value or whole atomic (for
1344     // non-simple lvalues) type.
1345     return convertAtomicTempToRValue(TempAddr, ResultSlot, Loc, AsValue);
1346   }
1347 
1348   // Okay, we're doing this natively.
1349   auto *Load = EmitAtomicLoadOp(AO, IsVolatile);
1350 
1351   // If we're ignoring an aggregate return, don't do anything.
1352   if (getEvaluationKind() == TEK_Aggregate && ResultSlot.isIgnored())
1353     return RValue::getAggregate(Address::invalid(), false);
1354 
1355   // Okay, turn that back into the original value or atomic (for non-simple
1356   // lvalues) type.
1357   return ConvertIntToValueOrAtomic(Load, ResultSlot, Loc, AsValue);
1358 }
1359 
1360 /// Emit a load from an l-value of atomic type.  Note that the r-value
1361 /// we produce is an r-value of the atomic *value* type.
1362 RValue CodeGenFunction::EmitAtomicLoad(LValue src, SourceLocation loc,
1363                                        llvm::AtomicOrdering AO, bool IsVolatile,
1364                                        AggValueSlot resultSlot) {
1365   AtomicInfo Atomics(*this, src);
1366   return Atomics.EmitAtomicLoad(resultSlot, loc, /*AsValue=*/true, AO,
1367                                 IsVolatile);
1368 }
1369 
1370 /// Copy an r-value into memory as part of storing to an atomic type.
1371 /// This needs to create a bit-pattern suitable for atomic operations.
1372 void AtomicInfo::emitCopyIntoMemory(RValue rvalue) const {
1373   assert(LVal.isSimple());
1374   // If we have an r-value, the rvalue should be of the atomic type,
1375   // which means that the caller is responsible for having zeroed
1376   // any padding.  Just do an aggregate copy of that type.
1377   if (rvalue.isAggregate()) {
1378     CGF.EmitAggregateCopy(getAtomicAddress(),
1379                           rvalue.getAggregateAddress(),
1380                           getAtomicType(),
1381                           (rvalue.isVolatileQualified()
1382                            || LVal.isVolatileQualified()));
1383     return;
1384   }
1385 
1386   // Okay, otherwise we're copying stuff.
1387 
1388   // Zero out the buffer if necessary.
1389   emitMemSetZeroIfNecessary();
1390 
1391   // Drill past the padding if present.
1392   LValue TempLVal = projectValue();
1393 
1394   // Okay, store the rvalue in.
1395   if (rvalue.isScalar()) {
1396     CGF.EmitStoreOfScalar(rvalue.getScalarVal(), TempLVal, /*init*/ true);
1397   } else {
1398     CGF.EmitStoreOfComplex(rvalue.getComplexVal(), TempLVal, /*init*/ true);
1399   }
1400 }
1401 
1402 
1403 /// Materialize an r-value into memory for the purposes of storing it
1404 /// to an atomic type.
1405 Address AtomicInfo::materializeRValue(RValue rvalue) const {
1406   // Aggregate r-values are already in memory, and EmitAtomicStore
1407   // requires them to be values of the atomic type.
1408   if (rvalue.isAggregate())
1409     return rvalue.getAggregateAddress();
1410 
1411   // Otherwise, make a temporary and materialize into it.
1412   LValue TempLV = CGF.MakeAddrLValue(CreateTempAlloca(), getAtomicType());
1413   AtomicInfo Atomics(CGF, TempLV);
1414   Atomics.emitCopyIntoMemory(rvalue);
1415   return TempLV.getAddress();
1416 }
1417 
1418 llvm::Value *AtomicInfo::convertRValueToInt(RValue RVal) const {
1419   // If we've got a scalar value of the right size, try to avoid going
1420   // through memory.
1421   if (RVal.isScalar() && (!hasPadding() || !LVal.isSimple())) {
1422     llvm::Value *Value = RVal.getScalarVal();
1423     if (isa<llvm::IntegerType>(Value->getType()))
1424       return CGF.EmitToMemory(Value, ValueTy);
1425     else {
1426       llvm::IntegerType *InputIntTy = llvm::IntegerType::get(
1427           CGF.getLLVMContext(),
1428           LVal.isSimple() ? getValueSizeInBits() : getAtomicSizeInBits());
1429       if (isa<llvm::PointerType>(Value->getType()))
1430         return CGF.Builder.CreatePtrToInt(Value, InputIntTy);
1431       else if (llvm::BitCastInst::isBitCastable(Value->getType(), InputIntTy))
1432         return CGF.Builder.CreateBitCast(Value, InputIntTy);
1433     }
1434   }
1435   // Otherwise, we need to go through memory.
1436   // Put the r-value in memory.
1437   Address Addr = materializeRValue(RVal);
1438 
1439   // Cast the temporary to the atomic int type and pull a value out.
1440   Addr = emitCastToAtomicIntPointer(Addr);
1441   return CGF.Builder.CreateLoad(Addr);
1442 }
1443 
1444 std::pair<llvm::Value *, llvm::Value *> AtomicInfo::EmitAtomicCompareExchangeOp(
1445     llvm::Value *ExpectedVal, llvm::Value *DesiredVal,
1446     llvm::AtomicOrdering Success, llvm::AtomicOrdering Failure, bool IsWeak) {
1447   // Do the atomic store.
1448   Address Addr = getAtomicAddressAsAtomicIntPointer();
1449   auto *Inst = CGF.Builder.CreateAtomicCmpXchg(Addr.getPointer(),
1450                                                ExpectedVal, DesiredVal,
1451                                                Success, Failure);
1452   // Other decoration.
1453   Inst->setVolatile(LVal.isVolatileQualified());
1454   Inst->setWeak(IsWeak);
1455 
1456   // Okay, turn that back into the original value type.
1457   auto *PreviousVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/0);
1458   auto *SuccessFailureVal = CGF.Builder.CreateExtractValue(Inst, /*Idxs=*/1);
1459   return std::make_pair(PreviousVal, SuccessFailureVal);
1460 }
1461 
1462 llvm::Value *
1463 AtomicInfo::EmitAtomicCompareExchangeLibcall(llvm::Value *ExpectedAddr,
1464                                              llvm::Value *DesiredAddr,
1465                                              llvm::AtomicOrdering Success,
1466                                              llvm::AtomicOrdering Failure) {
1467   // bool __atomic_compare_exchange(size_t size, void *obj, void *expected,
1468   // void *desired, int success, int failure);
1469   CallArgList Args;
1470   Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
1471   Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
1472            CGF.getContext().VoidPtrTy);
1473   Args.add(RValue::get(CGF.EmitCastToVoidPtr(ExpectedAddr)),
1474            CGF.getContext().VoidPtrTy);
1475   Args.add(RValue::get(CGF.EmitCastToVoidPtr(DesiredAddr)),
1476            CGF.getContext().VoidPtrTy);
1477   Args.add(RValue::get(llvm::ConstantInt::get(
1478                CGF.IntTy, translateAtomicOrdering(Success))),
1479            CGF.getContext().IntTy);
1480   Args.add(RValue::get(llvm::ConstantInt::get(
1481                CGF.IntTy, translateAtomicOrdering(Failure))),
1482            CGF.getContext().IntTy);
1483   auto SuccessFailureRVal = emitAtomicLibcall(CGF, "__atomic_compare_exchange",
1484                                               CGF.getContext().BoolTy, Args);
1485 
1486   return SuccessFailureRVal.getScalarVal();
1487 }
1488 
1489 std::pair<RValue, llvm::Value *> AtomicInfo::EmitAtomicCompareExchange(
1490     RValue Expected, RValue Desired, llvm::AtomicOrdering Success,
1491     llvm::AtomicOrdering Failure, bool IsWeak) {
1492   if (Failure >= Success)
1493     // Don't assert on undefined behavior.
1494     Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(Success);
1495 
1496   // Check whether we should use a library call.
1497   if (shouldUseLibcall()) {
1498     // Produce a source address.
1499     Address ExpectedAddr = materializeRValue(Expected);
1500     Address DesiredAddr = materializeRValue(Desired);
1501     auto *Res = EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1502                                                  DesiredAddr.getPointer(),
1503                                                  Success, Failure);
1504     return std::make_pair(
1505         convertAtomicTempToRValue(ExpectedAddr, AggValueSlot::ignored(),
1506                                   SourceLocation(), /*AsValue=*/false),
1507         Res);
1508   }
1509 
1510   // If we've got a scalar value of the right size, try to avoid going
1511   // through memory.
1512   auto *ExpectedVal = convertRValueToInt(Expected);
1513   auto *DesiredVal = convertRValueToInt(Desired);
1514   auto Res = EmitAtomicCompareExchangeOp(ExpectedVal, DesiredVal, Success,
1515                                          Failure, IsWeak);
1516   return std::make_pair(
1517       ConvertIntToValueOrAtomic(Res.first, AggValueSlot::ignored(),
1518                                 SourceLocation(), /*AsValue=*/false),
1519       Res.second);
1520 }
1521 
1522 static void
1523 EmitAtomicUpdateValue(CodeGenFunction &CGF, AtomicInfo &Atomics, RValue OldRVal,
1524                       const llvm::function_ref<RValue(RValue)> &UpdateOp,
1525                       Address DesiredAddr) {
1526   RValue UpRVal;
1527   LValue AtomicLVal = Atomics.getAtomicLValue();
1528   LValue DesiredLVal;
1529   if (AtomicLVal.isSimple()) {
1530     UpRVal = OldRVal;
1531     DesiredLVal = CGF.MakeAddrLValue(DesiredAddr, AtomicLVal.getType());
1532   } else {
1533     // Build new lvalue for temp address
1534     Address Ptr = Atomics.materializeRValue(OldRVal);
1535     LValue UpdateLVal;
1536     if (AtomicLVal.isBitField()) {
1537       UpdateLVal =
1538           LValue::MakeBitfield(Ptr, AtomicLVal.getBitFieldInfo(),
1539                                AtomicLVal.getType(),
1540                                AtomicLVal.getAlignmentSource());
1541       DesiredLVal =
1542           LValue::MakeBitfield(DesiredAddr, AtomicLVal.getBitFieldInfo(),
1543                                AtomicLVal.getType(),
1544                                AtomicLVal.getAlignmentSource());
1545     } else if (AtomicLVal.isVectorElt()) {
1546       UpdateLVal = LValue::MakeVectorElt(Ptr, AtomicLVal.getVectorIdx(),
1547                                          AtomicLVal.getType(),
1548                                          AtomicLVal.getAlignmentSource());
1549       DesiredLVal = LValue::MakeVectorElt(
1550           DesiredAddr, AtomicLVal.getVectorIdx(), AtomicLVal.getType(),
1551           AtomicLVal.getAlignmentSource());
1552     } else {
1553       assert(AtomicLVal.isExtVectorElt());
1554       UpdateLVal = LValue::MakeExtVectorElt(Ptr, AtomicLVal.getExtVectorElts(),
1555                                             AtomicLVal.getType(),
1556                                             AtomicLVal.getAlignmentSource());
1557       DesiredLVal = LValue::MakeExtVectorElt(
1558           DesiredAddr, AtomicLVal.getExtVectorElts(), AtomicLVal.getType(),
1559           AtomicLVal.getAlignmentSource());
1560     }
1561     UpdateLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1562     DesiredLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1563     UpRVal = CGF.EmitLoadOfLValue(UpdateLVal, SourceLocation());
1564   }
1565   // Store new value in the corresponding memory area
1566   RValue NewRVal = UpdateOp(UpRVal);
1567   if (NewRVal.isScalar()) {
1568     CGF.EmitStoreThroughLValue(NewRVal, DesiredLVal);
1569   } else {
1570     assert(NewRVal.isComplex());
1571     CGF.EmitStoreOfComplex(NewRVal.getComplexVal(), DesiredLVal,
1572                            /*isInit=*/false);
1573   }
1574 }
1575 
1576 void AtomicInfo::EmitAtomicUpdateLibcall(
1577     llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1578     bool IsVolatile) {
1579   auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1580 
1581   Address ExpectedAddr = CreateTempAlloca();
1582 
1583   EmitAtomicLoadLibcall(ExpectedAddr.getPointer(), AO, IsVolatile);
1584   auto *ContBB = CGF.createBasicBlock("atomic_cont");
1585   auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1586   CGF.EmitBlock(ContBB);
1587   Address DesiredAddr = CreateTempAlloca();
1588   if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
1589       requiresMemSetZero(getAtomicAddress().getElementType())) {
1590     auto *OldVal = CGF.Builder.CreateLoad(ExpectedAddr);
1591     CGF.Builder.CreateStore(OldVal, DesiredAddr);
1592   }
1593   auto OldRVal = convertAtomicTempToRValue(ExpectedAddr,
1594                                            AggValueSlot::ignored(),
1595                                            SourceLocation(), /*AsValue=*/false);
1596   EmitAtomicUpdateValue(CGF, *this, OldRVal, UpdateOp, DesiredAddr);
1597   auto *Res =
1598       EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1599                                        DesiredAddr.getPointer(),
1600                                        AO, Failure);
1601   CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
1602   CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1603 }
1604 
1605 void AtomicInfo::EmitAtomicUpdateOp(
1606     llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1607     bool IsVolatile) {
1608   auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1609 
1610   // Do the atomic load.
1611   auto *OldVal = EmitAtomicLoadOp(AO, IsVolatile);
1612   // For non-simple lvalues perform compare-and-swap procedure.
1613   auto *ContBB = CGF.createBasicBlock("atomic_cont");
1614   auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1615   auto *CurBB = CGF.Builder.GetInsertBlock();
1616   CGF.EmitBlock(ContBB);
1617   llvm::PHINode *PHI = CGF.Builder.CreatePHI(OldVal->getType(),
1618                                              /*NumReservedValues=*/2);
1619   PHI->addIncoming(OldVal, CurBB);
1620   Address NewAtomicAddr = CreateTempAlloca();
1621   Address NewAtomicIntAddr = emitCastToAtomicIntPointer(NewAtomicAddr);
1622   if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
1623       requiresMemSetZero(getAtomicAddress().getElementType())) {
1624     CGF.Builder.CreateStore(PHI, NewAtomicIntAddr);
1625   }
1626   auto OldRVal = ConvertIntToValueOrAtomic(PHI, AggValueSlot::ignored(),
1627                                            SourceLocation(), /*AsValue=*/false);
1628   EmitAtomicUpdateValue(CGF, *this, OldRVal, UpdateOp, NewAtomicAddr);
1629   auto *DesiredVal = CGF.Builder.CreateLoad(NewAtomicIntAddr);
1630   // Try to write new value using cmpxchg operation
1631   auto Res = EmitAtomicCompareExchangeOp(PHI, DesiredVal, AO, Failure);
1632   PHI->addIncoming(Res.first, CGF.Builder.GetInsertBlock());
1633   CGF.Builder.CreateCondBr(Res.second, ExitBB, ContBB);
1634   CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1635 }
1636 
1637 static void EmitAtomicUpdateValue(CodeGenFunction &CGF, AtomicInfo &Atomics,
1638                                   RValue UpdateRVal, Address DesiredAddr) {
1639   LValue AtomicLVal = Atomics.getAtomicLValue();
1640   LValue DesiredLVal;
1641   // Build new lvalue for temp address
1642   if (AtomicLVal.isBitField()) {
1643     DesiredLVal =
1644         LValue::MakeBitfield(DesiredAddr, AtomicLVal.getBitFieldInfo(),
1645                              AtomicLVal.getType(),
1646                              AtomicLVal.getAlignmentSource());
1647   } else if (AtomicLVal.isVectorElt()) {
1648     DesiredLVal =
1649         LValue::MakeVectorElt(DesiredAddr, AtomicLVal.getVectorIdx(),
1650                               AtomicLVal.getType(),
1651                               AtomicLVal.getAlignmentSource());
1652   } else {
1653     assert(AtomicLVal.isExtVectorElt());
1654     DesiredLVal = LValue::MakeExtVectorElt(
1655         DesiredAddr, AtomicLVal.getExtVectorElts(), AtomicLVal.getType(),
1656         AtomicLVal.getAlignmentSource());
1657   }
1658   DesiredLVal.setTBAAInfo(AtomicLVal.getTBAAInfo());
1659   // Store new value in the corresponding memory area
1660   assert(UpdateRVal.isScalar());
1661   CGF.EmitStoreThroughLValue(UpdateRVal, DesiredLVal);
1662 }
1663 
1664 void AtomicInfo::EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO,
1665                                          RValue UpdateRVal, bool IsVolatile) {
1666   auto Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
1667 
1668   Address ExpectedAddr = CreateTempAlloca();
1669 
1670   EmitAtomicLoadLibcall(ExpectedAddr.getPointer(), AO, IsVolatile);
1671   auto *ContBB = CGF.createBasicBlock("atomic_cont");
1672   auto *ExitBB = CGF.createBasicBlock("atomic_exit");
1673   CGF.EmitBlock(ContBB);
1674   Address DesiredAddr = CreateTempAlloca();
1675   if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
1676       requiresMemSetZero(getAtomicAddress().getElementType())) {
1677     auto *OldVal = CGF.Builder.CreateLoad(ExpectedAddr);
1678     CGF.Builder.CreateStore(OldVal, DesiredAddr);
1679   }
1680   EmitAtomicUpdateValue(CGF, *this, UpdateRVal, DesiredAddr);
1681   auto *Res =
1682       EmitAtomicCompareExchangeLibcall(ExpectedAddr.getPointer(),
1683                                        DesiredAddr.getPointer(),
1684                                        AO, Failure);
1685   CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
1686   CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1687 }
1688 
1689 void AtomicInfo::EmitAtomicUpdateOp(llvm::AtomicOrdering AO, RValue UpdateRVal,
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);
1703   Address NewAtomicAddr = CreateTempAlloca();
1704   Address NewAtomicIntAddr = emitCastToAtomicIntPointer(NewAtomicAddr);
1705   if ((LVal.isBitField() && BFI.Size != ValueSizeInBits) ||
1706       requiresMemSetZero(getAtomicAddress().getElementType())) {
1707     CGF.Builder.CreateStore(PHI, NewAtomicIntAddr);
1708   }
1709   EmitAtomicUpdateValue(CGF, *this, UpdateRVal, NewAtomicAddr);
1710   auto *DesiredVal = CGF.Builder.CreateLoad(NewAtomicIntAddr);
1711   // Try to write new value using cmpxchg operation
1712   auto Res = EmitAtomicCompareExchangeOp(PHI, DesiredVal, AO, Failure);
1713   PHI->addIncoming(Res.first, CGF.Builder.GetInsertBlock());
1714   CGF.Builder.CreateCondBr(Res.second, ExitBB, ContBB);
1715   CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1716 }
1717 
1718 void AtomicInfo::EmitAtomicUpdate(
1719     llvm::AtomicOrdering AO, const llvm::function_ref<RValue(RValue)> &UpdateOp,
1720     bool IsVolatile) {
1721   if (shouldUseLibcall()) {
1722     EmitAtomicUpdateLibcall(AO, UpdateOp, IsVolatile);
1723   } else {
1724     EmitAtomicUpdateOp(AO, UpdateOp, IsVolatile);
1725   }
1726 }
1727 
1728 void AtomicInfo::EmitAtomicUpdate(llvm::AtomicOrdering AO, RValue UpdateRVal,
1729                                   bool IsVolatile) {
1730   if (shouldUseLibcall()) {
1731     EmitAtomicUpdateLibcall(AO, UpdateRVal, IsVolatile);
1732   } else {
1733     EmitAtomicUpdateOp(AO, UpdateRVal, IsVolatile);
1734   }
1735 }
1736 
1737 void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue lvalue,
1738                                       bool isInit) {
1739   bool IsVolatile = lvalue.isVolatileQualified();
1740   llvm::AtomicOrdering AO;
1741   if (lvalue.getType()->isAtomicType()) {
1742     AO = llvm::SequentiallyConsistent;
1743   } else {
1744     AO = llvm::Release;
1745     IsVolatile = true;
1746   }
1747   return EmitAtomicStore(rvalue, lvalue, AO, IsVolatile, isInit);
1748 }
1749 
1750 /// Emit a store to an l-value of atomic type.
1751 ///
1752 /// Note that the r-value is expected to be an r-value *of the atomic
1753 /// type*; this means that for aggregate r-values, it should include
1754 /// storage for any padding that was necessary.
1755 void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue dest,
1756                                       llvm::AtomicOrdering AO, bool IsVolatile,
1757                                       bool isInit) {
1758   // If this is an aggregate r-value, it should agree in type except
1759   // maybe for address-space qualification.
1760   assert(!rvalue.isAggregate() ||
1761          rvalue.getAggregateAddress().getElementType()
1762            == dest.getAddress().getElementType());
1763 
1764   AtomicInfo atomics(*this, dest);
1765   LValue LVal = atomics.getAtomicLValue();
1766 
1767   // If this is an initialization, just put the value there normally.
1768   if (LVal.isSimple()) {
1769     if (isInit) {
1770       atomics.emitCopyIntoMemory(rvalue);
1771       return;
1772     }
1773 
1774     // Check whether we should use a library call.
1775     if (atomics.shouldUseLibcall()) {
1776       // Produce a source address.
1777       Address srcAddr = atomics.materializeRValue(rvalue);
1778 
1779       // void __atomic_store(size_t size, void *mem, void *val, int order)
1780       CallArgList args;
1781       args.add(RValue::get(atomics.getAtomicSizeValue()),
1782                getContext().getSizeType());
1783       args.add(RValue::get(EmitCastToVoidPtr(atomics.getAtomicPointer())),
1784                getContext().VoidPtrTy);
1785       args.add(RValue::get(EmitCastToVoidPtr(srcAddr.getPointer())),
1786                getContext().VoidPtrTy);
1787       args.add(RValue::get(llvm::ConstantInt::get(
1788                    IntTy, AtomicInfo::translateAtomicOrdering(AO))),
1789                getContext().IntTy);
1790       emitAtomicLibcall(*this, "__atomic_store", getContext().VoidTy, args);
1791       return;
1792     }
1793 
1794     // Okay, we're doing this natively.
1795     llvm::Value *intValue = atomics.convertRValueToInt(rvalue);
1796 
1797     // Do the atomic store.
1798     Address addr =
1799         atomics.emitCastToAtomicIntPointer(atomics.getAtomicAddress());
1800     intValue = Builder.CreateIntCast(
1801         intValue, addr.getElementType(), /*isSigned=*/false);
1802     llvm::StoreInst *store = Builder.CreateStore(intValue, addr);
1803 
1804     // Initializations don't need to be atomic.
1805     if (!isInit)
1806       store->setAtomic(AO);
1807 
1808     // Other decoration.
1809     if (IsVolatile)
1810       store->setVolatile(true);
1811     if (dest.getTBAAInfo())
1812       CGM.DecorateInstructionWithTBAA(store, dest.getTBAAInfo());
1813     return;
1814   }
1815 
1816   // Emit simple atomic update operation.
1817   atomics.EmitAtomicUpdate(AO, rvalue, IsVolatile);
1818 }
1819 
1820 /// Emit a compare-and-exchange op for atomic type.
1821 ///
1822 std::pair<RValue, llvm::Value *> CodeGenFunction::EmitAtomicCompareExchange(
1823     LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
1824     llvm::AtomicOrdering Success, llvm::AtomicOrdering Failure, bool IsWeak,
1825     AggValueSlot Slot) {
1826   // If this is an aggregate r-value, it should agree in type except
1827   // maybe for address-space qualification.
1828   assert(!Expected.isAggregate() ||
1829          Expected.getAggregateAddress().getElementType() ==
1830              Obj.getAddress().getElementType());
1831   assert(!Desired.isAggregate() ||
1832          Desired.getAggregateAddress().getElementType() ==
1833              Obj.getAddress().getElementType());
1834   AtomicInfo Atomics(*this, Obj);
1835 
1836   return Atomics.EmitAtomicCompareExchange(Expected, Desired, Success, Failure,
1837                                            IsWeak);
1838 }
1839 
1840 void CodeGenFunction::EmitAtomicUpdate(
1841     LValue LVal, llvm::AtomicOrdering AO,
1842     const llvm::function_ref<RValue(RValue)> &UpdateOp, bool IsVolatile) {
1843   AtomicInfo Atomics(*this, LVal);
1844   Atomics.EmitAtomicUpdate(AO, UpdateOp, IsVolatile);
1845 }
1846 
1847 void CodeGenFunction::EmitAtomicInit(Expr *init, LValue dest) {
1848   AtomicInfo atomics(*this, dest);
1849 
1850   switch (atomics.getEvaluationKind()) {
1851   case TEK_Scalar: {
1852     llvm::Value *value = EmitScalarExpr(init);
1853     atomics.emitCopyIntoMemory(RValue::get(value));
1854     return;
1855   }
1856 
1857   case TEK_Complex: {
1858     ComplexPairTy value = EmitComplexExpr(init);
1859     atomics.emitCopyIntoMemory(RValue::getComplex(value));
1860     return;
1861   }
1862 
1863   case TEK_Aggregate: {
1864     // Fix up the destination if the initializer isn't an expression
1865     // of atomic type.
1866     bool Zeroed = false;
1867     if (!init->getType()->isAtomicType()) {
1868       Zeroed = atomics.emitMemSetZeroIfNecessary();
1869       dest = atomics.projectValue();
1870     }
1871 
1872     // Evaluate the expression directly into the destination.
1873     AggValueSlot slot = AggValueSlot::forLValue(dest,
1874                                         AggValueSlot::IsNotDestructed,
1875                                         AggValueSlot::DoesNotNeedGCBarriers,
1876                                         AggValueSlot::IsNotAliased,
1877                                         Zeroed ? AggValueSlot::IsZeroed :
1878                                                  AggValueSlot::IsNotZeroed);
1879 
1880     EmitAggExpr(init, slot);
1881     return;
1882   }
1883   }
1884   llvm_unreachable("bad evaluation kind");
1885 }
1886