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