CGAtomic.cpp 83 KB

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