CGAtomic.cpp 89 KB

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