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