John McCall | fafaaef | 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" |
| 16 | #include "CodeGenModule.h" |
| 17 | #include "clang/AST/ASTContext.h" |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DataLayout.h" |
| 20 | #include "llvm/IR/Intrinsics.h" |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Operator.h" |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 26 | // The ABI values for various atomic memory orderings. |
| 27 | enum AtomicOrderingKind { |
| 28 | AO_ABI_memory_order_relaxed = 0, |
| 29 | AO_ABI_memory_order_consume = 1, |
| 30 | AO_ABI_memory_order_acquire = 2, |
| 31 | AO_ABI_memory_order_release = 3, |
| 32 | AO_ABI_memory_order_acq_rel = 4, |
| 33 | AO_ABI_memory_order_seq_cst = 5 |
| 34 | }; |
| 35 | |
| 36 | namespace { |
| 37 | class AtomicInfo { |
| 38 | CodeGenFunction &CGF; |
| 39 | QualType AtomicTy; |
| 40 | QualType ValueTy; |
| 41 | uint64_t AtomicSizeInBits; |
| 42 | uint64_t ValueSizeInBits; |
| 43 | CharUnits AtomicAlign; |
| 44 | CharUnits ValueAlign; |
| 45 | CharUnits LValueAlign; |
| 46 | TypeEvaluationKind EvaluationKind; |
| 47 | bool UseLibcall; |
| 48 | public: |
| 49 | AtomicInfo(CodeGenFunction &CGF, LValue &lvalue) : CGF(CGF) { |
| 50 | assert(lvalue.isSimple()); |
| 51 | |
| 52 | AtomicTy = lvalue.getType(); |
| 53 | ValueTy = AtomicTy->castAs<AtomicType>()->getValueType(); |
| 54 | EvaluationKind = CGF.getEvaluationKind(ValueTy); |
| 55 | |
| 56 | ASTContext &C = CGF.getContext(); |
| 57 | |
| 58 | uint64_t valueAlignInBits; |
| 59 | llvm::tie(ValueSizeInBits, valueAlignInBits) = C.getTypeInfo(ValueTy); |
| 60 | |
| 61 | uint64_t atomicAlignInBits; |
| 62 | llvm::tie(AtomicSizeInBits, atomicAlignInBits) = C.getTypeInfo(AtomicTy); |
| 63 | |
| 64 | assert(ValueSizeInBits <= AtomicSizeInBits); |
| 65 | assert(valueAlignInBits <= atomicAlignInBits); |
| 66 | |
| 67 | AtomicAlign = C.toCharUnitsFromBits(atomicAlignInBits); |
| 68 | ValueAlign = C.toCharUnitsFromBits(valueAlignInBits); |
| 69 | if (lvalue.getAlignment().isZero()) |
| 70 | lvalue.setAlignment(AtomicAlign); |
| 71 | |
| 72 | UseLibcall = |
| 73 | (AtomicSizeInBits > uint64_t(C.toBits(lvalue.getAlignment())) || |
| 74 | AtomicSizeInBits > C.getTargetInfo().getMaxAtomicInlineWidth()); |
| 75 | } |
| 76 | |
| 77 | QualType getAtomicType() const { return AtomicTy; } |
| 78 | QualType getValueType() const { return ValueTy; } |
| 79 | CharUnits getAtomicAlignment() const { return AtomicAlign; } |
| 80 | CharUnits getValueAlignment() const { return ValueAlign; } |
| 81 | uint64_t getAtomicSizeInBits() const { return AtomicSizeInBits; } |
| 82 | uint64_t getValueSizeInBits() const { return AtomicSizeInBits; } |
| 83 | TypeEvaluationKind getEvaluationKind() const { return EvaluationKind; } |
| 84 | bool shouldUseLibcall() const { return UseLibcall; } |
| 85 | |
| 86 | /// Is the atomic size larger than the underlying value type? |
| 87 | /// |
| 88 | /// Note that the absence of padding does not mean that atomic |
| 89 | /// objects are completely interchangeable with non-atomic |
| 90 | /// objects: we might have promoted the alignment of a type |
| 91 | /// without making it bigger. |
| 92 | bool hasPadding() const { |
| 93 | return (ValueSizeInBits != AtomicSizeInBits); |
| 94 | } |
| 95 | |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 96 | bool emitMemSetZeroIfNecessary(LValue dest) const; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 97 | |
| 98 | llvm::Value *getAtomicSizeValue() const { |
| 99 | CharUnits size = CGF.getContext().toCharUnitsFromBits(AtomicSizeInBits); |
| 100 | return CGF.CGM.getSize(size); |
| 101 | } |
| 102 | |
| 103 | /// Cast the given pointer to an integer pointer suitable for |
| 104 | /// atomic operations. |
| 105 | llvm::Value *emitCastToAtomicIntPointer(llvm::Value *addr) const; |
| 106 | |
| 107 | /// Turn an atomic-layout object into an r-value. |
| 108 | RValue convertTempToRValue(llvm::Value *addr, |
| 109 | AggValueSlot resultSlot) const; |
| 110 | |
| 111 | /// Copy an atomic r-value into atomic-layout memory. |
| 112 | void emitCopyIntoMemory(RValue rvalue, LValue lvalue) const; |
| 113 | |
| 114 | /// Project an l-value down to the value field. |
| 115 | LValue projectValue(LValue lvalue) const { |
| 116 | llvm::Value *addr = lvalue.getAddress(); |
| 117 | if (hasPadding()) |
| 118 | addr = CGF.Builder.CreateStructGEP(addr, 0); |
| 119 | |
| 120 | return LValue::MakeAddr(addr, getValueType(), lvalue.getAlignment(), |
| 121 | CGF.getContext(), lvalue.getTBAAInfo()); |
| 122 | } |
| 123 | |
| 124 | /// Materialize an atomic r-value in atomic-layout memory. |
| 125 | llvm::Value *materializeRValue(RValue rvalue) const; |
| 126 | |
| 127 | private: |
| 128 | bool requiresMemSetZero(llvm::Type *type) const; |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | static RValue emitAtomicLibcall(CodeGenFunction &CGF, |
| 133 | StringRef fnName, |
| 134 | QualType resultType, |
| 135 | CallArgList &args) { |
| 136 | const CGFunctionInfo &fnInfo = |
| 137 | CGF.CGM.getTypes().arrangeFreeFunctionCall(resultType, args, |
| 138 | FunctionType::ExtInfo(), RequiredArgs::All); |
| 139 | llvm::FunctionType *fnTy = CGF.CGM.getTypes().GetFunctionType(fnInfo); |
| 140 | llvm::Constant *fn = CGF.CGM.CreateRuntimeFunction(fnTy, fnName); |
| 141 | return CGF.EmitCall(fnInfo, fn, ReturnValueSlot(), args); |
| 142 | } |
| 143 | |
| 144 | /// Does a store of the given IR type modify the full expected width? |
| 145 | static bool isFullSizeType(CodeGenModule &CGM, llvm::Type *type, |
| 146 | uint64_t expectedSize) { |
| 147 | return (CGM.getDataLayout().getTypeStoreSize(type) * 8 == expectedSize); |
| 148 | } |
| 149 | |
| 150 | /// Does the atomic type require memsetting to zero before initialization? |
| 151 | /// |
| 152 | /// The IR type is provided as a way of making certain queries faster. |
| 153 | bool AtomicInfo::requiresMemSetZero(llvm::Type *type) const { |
| 154 | // If the atomic type has size padding, we definitely need a memset. |
| 155 | if (hasPadding()) return true; |
| 156 | |
| 157 | // Otherwise, do some simple heuristics to try to avoid it: |
| 158 | switch (getEvaluationKind()) { |
| 159 | // For scalars and complexes, check whether the store size of the |
| 160 | // type uses the full size. |
| 161 | case TEK_Scalar: |
| 162 | return !isFullSizeType(CGF.CGM, type, AtomicSizeInBits); |
| 163 | case TEK_Complex: |
| 164 | return !isFullSizeType(CGF.CGM, type->getStructElementType(0), |
| 165 | AtomicSizeInBits / 2); |
| 166 | |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 167 | // Padding in structs has an undefined bit pattern. User beware. |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 168 | case TEK_Aggregate: |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 169 | return false; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 170 | } |
| 171 | llvm_unreachable("bad evaluation kind"); |
| 172 | } |
| 173 | |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 174 | bool AtomicInfo::emitMemSetZeroIfNecessary(LValue dest) const { |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 175 | llvm::Value *addr = dest.getAddress(); |
| 176 | if (!requiresMemSetZero(addr->getType()->getPointerElementType())) |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 177 | return false; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 178 | |
| 179 | CGF.Builder.CreateMemSet(addr, llvm::ConstantInt::get(CGF.Int8Ty, 0), |
| 180 | AtomicSizeInBits / 8, |
| 181 | dest.getAlignment().getQuantity()); |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 182 | return true; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 183 | } |
| 184 | |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 185 | static void |
| 186 | EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, llvm::Value *Dest, |
| 187 | llvm::Value *Ptr, llvm::Value *Val1, llvm::Value *Val2, |
| 188 | uint64_t Size, unsigned Align, llvm::AtomicOrdering Order) { |
| 189 | llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add; |
| 190 | llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0; |
| 191 | |
| 192 | switch (E->getOp()) { |
| 193 | case AtomicExpr::AO__c11_atomic_init: |
| 194 | llvm_unreachable("Already handled!"); |
| 195 | |
| 196 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 197 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 198 | case AtomicExpr::AO__atomic_compare_exchange: |
| 199 | case AtomicExpr::AO__atomic_compare_exchange_n: { |
| 200 | // Note that cmpxchg only supports specifying one ordering and |
| 201 | // doesn't support weak cmpxchg, at least at the moment. |
| 202 | llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1); |
| 203 | LoadVal1->setAlignment(Align); |
| 204 | llvm::LoadInst *LoadVal2 = CGF.Builder.CreateLoad(Val2); |
| 205 | LoadVal2->setAlignment(Align); |
| 206 | llvm::AtomicCmpXchgInst *CXI = |
| 207 | CGF.Builder.CreateAtomicCmpXchg(Ptr, LoadVal1, LoadVal2, Order); |
| 208 | CXI->setVolatile(E->isVolatile()); |
| 209 | llvm::StoreInst *StoreVal1 = CGF.Builder.CreateStore(CXI, Val1); |
| 210 | StoreVal1->setAlignment(Align); |
| 211 | llvm::Value *Cmp = CGF.Builder.CreateICmpEQ(CXI, LoadVal1); |
| 212 | CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType())); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | case AtomicExpr::AO__c11_atomic_load: |
| 217 | case AtomicExpr::AO__atomic_load_n: |
| 218 | case AtomicExpr::AO__atomic_load: { |
| 219 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr); |
| 220 | Load->setAtomic(Order); |
| 221 | Load->setAlignment(Size); |
| 222 | Load->setVolatile(E->isVolatile()); |
| 223 | llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Load, Dest); |
| 224 | StoreDest->setAlignment(Align); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | case AtomicExpr::AO__c11_atomic_store: |
| 229 | case AtomicExpr::AO__atomic_store: |
| 230 | case AtomicExpr::AO__atomic_store_n: { |
| 231 | assert(!Dest && "Store does not return a value"); |
| 232 | llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1); |
| 233 | LoadVal1->setAlignment(Align); |
| 234 | llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr); |
| 235 | Store->setAtomic(Order); |
| 236 | Store->setAlignment(Size); |
| 237 | Store->setVolatile(E->isVolatile()); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | case AtomicExpr::AO__c11_atomic_exchange: |
| 242 | case AtomicExpr::AO__atomic_exchange_n: |
| 243 | case AtomicExpr::AO__atomic_exchange: |
| 244 | Op = llvm::AtomicRMWInst::Xchg; |
| 245 | break; |
| 246 | |
| 247 | case AtomicExpr::AO__atomic_add_fetch: |
| 248 | PostOp = llvm::Instruction::Add; |
| 249 | // Fall through. |
| 250 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 251 | case AtomicExpr::AO__atomic_fetch_add: |
| 252 | Op = llvm::AtomicRMWInst::Add; |
| 253 | break; |
| 254 | |
| 255 | case AtomicExpr::AO__atomic_sub_fetch: |
| 256 | PostOp = llvm::Instruction::Sub; |
| 257 | // Fall through. |
| 258 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 259 | case AtomicExpr::AO__atomic_fetch_sub: |
| 260 | Op = llvm::AtomicRMWInst::Sub; |
| 261 | break; |
| 262 | |
| 263 | case AtomicExpr::AO__atomic_and_fetch: |
| 264 | PostOp = llvm::Instruction::And; |
| 265 | // Fall through. |
| 266 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 267 | case AtomicExpr::AO__atomic_fetch_and: |
| 268 | Op = llvm::AtomicRMWInst::And; |
| 269 | break; |
| 270 | |
| 271 | case AtomicExpr::AO__atomic_or_fetch: |
| 272 | PostOp = llvm::Instruction::Or; |
| 273 | // Fall through. |
| 274 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 275 | case AtomicExpr::AO__atomic_fetch_or: |
| 276 | Op = llvm::AtomicRMWInst::Or; |
| 277 | break; |
| 278 | |
| 279 | case AtomicExpr::AO__atomic_xor_fetch: |
| 280 | PostOp = llvm::Instruction::Xor; |
| 281 | // Fall through. |
| 282 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 283 | case AtomicExpr::AO__atomic_fetch_xor: |
| 284 | Op = llvm::AtomicRMWInst::Xor; |
| 285 | break; |
| 286 | |
| 287 | case AtomicExpr::AO__atomic_nand_fetch: |
| 288 | PostOp = llvm::Instruction::And; |
| 289 | // Fall through. |
| 290 | case AtomicExpr::AO__atomic_fetch_nand: |
| 291 | Op = llvm::AtomicRMWInst::Nand; |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1); |
| 296 | LoadVal1->setAlignment(Align); |
| 297 | llvm::AtomicRMWInst *RMWI = |
| 298 | CGF.Builder.CreateAtomicRMW(Op, Ptr, LoadVal1, Order); |
| 299 | RMWI->setVolatile(E->isVolatile()); |
| 300 | |
| 301 | // For __atomic_*_fetch operations, perform the operation again to |
| 302 | // determine the value which was written. |
| 303 | llvm::Value *Result = RMWI; |
| 304 | if (PostOp) |
| 305 | Result = CGF.Builder.CreateBinOp(PostOp, RMWI, LoadVal1); |
| 306 | if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch) |
| 307 | Result = CGF.Builder.CreateNot(Result); |
| 308 | llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Result, Dest); |
| 309 | StoreDest->setAlignment(Align); |
| 310 | } |
| 311 | |
| 312 | // This function emits any expression (scalar, complex, or aggregate) |
| 313 | // into a temporary alloca. |
| 314 | static llvm::Value * |
| 315 | EmitValToTemp(CodeGenFunction &CGF, Expr *E) { |
| 316 | llvm::Value *DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp"); |
| 317 | CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(), |
| 318 | /*Init*/ true); |
| 319 | return DeclPtr; |
| 320 | } |
| 321 | |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 322 | static void |
| 323 | AddDirectArgument(CodeGenFunction &CGF, CallArgList &Args, |
Ed Schouten | c59cf0d | 2013-05-31 20:12:49 +0000 | [diff] [blame] | 324 | bool UseOptimizedLibcall, llvm::Value *Val, QualType ValTy) { |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 325 | if (UseOptimizedLibcall) { |
| 326 | // Load value and pass it to the function directly. |
| 327 | unsigned Align = CGF.getContext().getTypeAlignInChars(ValTy).getQuantity(); |
| 328 | Val = CGF.EmitLoadOfScalar(Val, false, Align, ValTy); |
| 329 | Args.add(RValue::get(Val), ValTy); |
| 330 | } else { |
| 331 | // Non-optimized functions always take a reference. |
| 332 | Args.add(RValue::get(CGF.EmitCastToVoidPtr(Val)), |
| 333 | CGF.getContext().VoidPtrTy); |
| 334 | } |
| 335 | } |
| 336 | |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 337 | RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) { |
| 338 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
| 339 | QualType MemTy = AtomicTy; |
| 340 | if (const AtomicType *AT = AtomicTy->getAs<AtomicType>()) |
| 341 | MemTy = AT->getValueType(); |
| 342 | CharUnits sizeChars = getContext().getTypeSizeInChars(AtomicTy); |
| 343 | uint64_t Size = sizeChars.getQuantity(); |
| 344 | CharUnits alignChars = getContext().getTypeAlignInChars(AtomicTy); |
| 345 | unsigned Align = alignChars.getQuantity(); |
| 346 | unsigned MaxInlineWidthInBits = |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 347 | getTarget().getMaxAtomicInlineWidth(); |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 348 | bool UseLibcall = (Size != Align || |
| 349 | getContext().toBits(sizeChars) > MaxInlineWidthInBits); |
| 350 | |
| 351 | llvm::Value *Ptr, *Order, *OrderFail = 0, *Val1 = 0, *Val2 = 0; |
| 352 | Ptr = EmitScalarExpr(E->getPtr()); |
| 353 | |
| 354 | if (E->getOp() == AtomicExpr::AO__c11_atomic_init) { |
| 355 | assert(!Dest && "Init does not return a value"); |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 356 | LValue lvalue = LValue::MakeAddr(Ptr, AtomicTy, alignChars, getContext()); |
| 357 | EmitAtomicInit(E->getVal1(), lvalue); |
| 358 | return RValue::get(0); |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | Order = EmitScalarExpr(E->getOrder()); |
| 362 | |
| 363 | switch (E->getOp()) { |
| 364 | case AtomicExpr::AO__c11_atomic_init: |
| 365 | llvm_unreachable("Already handled!"); |
| 366 | |
| 367 | case AtomicExpr::AO__c11_atomic_load: |
| 368 | case AtomicExpr::AO__atomic_load_n: |
| 369 | break; |
| 370 | |
| 371 | case AtomicExpr::AO__atomic_load: |
| 372 | Dest = EmitScalarExpr(E->getVal1()); |
| 373 | break; |
| 374 | |
| 375 | case AtomicExpr::AO__atomic_store: |
| 376 | Val1 = EmitScalarExpr(E->getVal1()); |
| 377 | break; |
| 378 | |
| 379 | case AtomicExpr::AO__atomic_exchange: |
| 380 | Val1 = EmitScalarExpr(E->getVal1()); |
| 381 | Dest = EmitScalarExpr(E->getVal2()); |
| 382 | break; |
| 383 | |
| 384 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 385 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 386 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 387 | case AtomicExpr::AO__atomic_compare_exchange: |
| 388 | Val1 = EmitScalarExpr(E->getVal1()); |
| 389 | if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange) |
| 390 | Val2 = EmitScalarExpr(E->getVal2()); |
| 391 | else |
| 392 | Val2 = EmitValToTemp(*this, E->getVal2()); |
| 393 | OrderFail = EmitScalarExpr(E->getOrderFail()); |
| 394 | // Evaluate and discard the 'weak' argument. |
| 395 | if (E->getNumSubExprs() == 6) |
| 396 | EmitScalarExpr(E->getWeak()); |
| 397 | break; |
| 398 | |
| 399 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 400 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 401 | if (MemTy->isPointerType()) { |
| 402 | // For pointer arithmetic, we're required to do a bit of math: |
| 403 | // adding 1 to an int* is not the same as adding 1 to a uintptr_t. |
| 404 | // ... but only for the C11 builtins. The GNU builtins expect the |
| 405 | // user to multiply by sizeof(T). |
| 406 | QualType Val1Ty = E->getVal1()->getType(); |
| 407 | llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1()); |
| 408 | CharUnits PointeeIncAmt = |
| 409 | getContext().getTypeSizeInChars(MemTy->getPointeeType()); |
| 410 | Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt)); |
| 411 | Val1 = CreateMemTemp(Val1Ty, ".atomictmp"); |
| 412 | EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Val1, Val1Ty)); |
| 413 | break; |
| 414 | } |
| 415 | // Fall through. |
| 416 | case AtomicExpr::AO__atomic_fetch_add: |
| 417 | case AtomicExpr::AO__atomic_fetch_sub: |
| 418 | case AtomicExpr::AO__atomic_add_fetch: |
| 419 | case AtomicExpr::AO__atomic_sub_fetch: |
| 420 | case AtomicExpr::AO__c11_atomic_store: |
| 421 | case AtomicExpr::AO__c11_atomic_exchange: |
| 422 | case AtomicExpr::AO__atomic_store_n: |
| 423 | case AtomicExpr::AO__atomic_exchange_n: |
| 424 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 425 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 426 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 427 | case AtomicExpr::AO__atomic_fetch_and: |
| 428 | case AtomicExpr::AO__atomic_fetch_or: |
| 429 | case AtomicExpr::AO__atomic_fetch_xor: |
| 430 | case AtomicExpr::AO__atomic_fetch_nand: |
| 431 | case AtomicExpr::AO__atomic_and_fetch: |
| 432 | case AtomicExpr::AO__atomic_or_fetch: |
| 433 | case AtomicExpr::AO__atomic_xor_fetch: |
| 434 | case AtomicExpr::AO__atomic_nand_fetch: |
| 435 | Val1 = EmitValToTemp(*this, E->getVal1()); |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | if (!E->getType()->isVoidType() && !Dest) |
| 440 | Dest = CreateMemTemp(E->getType(), ".atomicdst"); |
| 441 | |
| 442 | // Use a library call. See: http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary . |
| 443 | if (UseLibcall) { |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 444 | bool UseOptimizedLibcall = false; |
| 445 | switch (E->getOp()) { |
| 446 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 447 | case AtomicExpr::AO__atomic_fetch_add: |
| 448 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 449 | case AtomicExpr::AO__atomic_fetch_and: |
| 450 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 451 | case AtomicExpr::AO__atomic_fetch_or: |
| 452 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 453 | case AtomicExpr::AO__atomic_fetch_sub: |
| 454 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 455 | case AtomicExpr::AO__atomic_fetch_xor: |
| 456 | // For these, only library calls for certain sizes exist. |
| 457 | UseOptimizedLibcall = true; |
| 458 | break; |
| 459 | default: |
| 460 | // Only use optimized library calls for sizes for which they exist. |
| 461 | if (Size == 1 || Size == 2 || Size == 4 || Size == 8) |
| 462 | UseOptimizedLibcall = true; |
| 463 | break; |
| 464 | } |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 465 | |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 466 | CallArgList Args; |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 467 | if (!UseOptimizedLibcall) { |
| 468 | // For non-optimized library calls, the size is the first parameter |
| 469 | Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)), |
| 470 | getContext().getSizeType()); |
| 471 | } |
| 472 | // Atomic address is the first or second parameter |
Nick Lewycky | 5d4a755 | 2013-10-01 21:51:38 +0000 | [diff] [blame^] | 473 | Args.add(RValue::get(EmitCastToVoidPtr(Ptr)), getContext().VoidPtrTy); |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 474 | |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 475 | std::string LibCallName; |
| 476 | QualType RetTy; |
| 477 | bool HaveRetTy = false; |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 478 | switch (E->getOp()) { |
| 479 | // There is only one libcall for compare an exchange, because there is no |
| 480 | // optimisation benefit possible from a libcall version of a weak compare |
| 481 | // and exchange. |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 482 | // bool __atomic_compare_exchange(size_t size, void *mem, void *expected, |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 483 | // void *desired, int success, int failure) |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 484 | // bool __atomic_compare_exchange_N(T *mem, T *expected, T desired, |
| 485 | // int success, int failure) |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 486 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 487 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 488 | case AtomicExpr::AO__atomic_compare_exchange: |
| 489 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 490 | LibCallName = "__atomic_compare_exchange"; |
| 491 | RetTy = getContext().BoolTy; |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 492 | HaveRetTy = true; |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 493 | Args.add(RValue::get(EmitCastToVoidPtr(Val1)), |
| 494 | getContext().VoidPtrTy); |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 495 | AddDirectArgument(*this, Args, UseOptimizedLibcall, Val2, MemTy); |
Nick Lewycky | 5d4a755 | 2013-10-01 21:51:38 +0000 | [diff] [blame^] | 496 | Args.add(RValue::get(Order), getContext().IntTy); |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 497 | Order = OrderFail; |
| 498 | break; |
| 499 | // void __atomic_exchange(size_t size, void *mem, void *val, void *return, |
| 500 | // int order) |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 501 | // T __atomic_exchange_N(T *mem, T val, int order) |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 502 | case AtomicExpr::AO__c11_atomic_exchange: |
| 503 | case AtomicExpr::AO__atomic_exchange_n: |
| 504 | case AtomicExpr::AO__atomic_exchange: |
| 505 | LibCallName = "__atomic_exchange"; |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 506 | AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy); |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 507 | break; |
| 508 | // void __atomic_store(size_t size, void *mem, void *val, int order) |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 509 | // void __atomic_store_N(T *mem, T val, int order) |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 510 | case AtomicExpr::AO__c11_atomic_store: |
| 511 | case AtomicExpr::AO__atomic_store: |
| 512 | case AtomicExpr::AO__atomic_store_n: |
| 513 | LibCallName = "__atomic_store"; |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 514 | RetTy = getContext().VoidTy; |
| 515 | HaveRetTy = true; |
| 516 | AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy); |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 517 | break; |
| 518 | // void __atomic_load(size_t size, void *mem, void *return, int order) |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 519 | // T __atomic_load_N(T *mem, int order) |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 520 | case AtomicExpr::AO__c11_atomic_load: |
| 521 | case AtomicExpr::AO__atomic_load: |
| 522 | case AtomicExpr::AO__atomic_load_n: |
| 523 | LibCallName = "__atomic_load"; |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 524 | break; |
| 525 | // T __atomic_fetch_add_N(T *mem, T val, int order) |
| 526 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 527 | case AtomicExpr::AO__atomic_fetch_add: |
| 528 | LibCallName = "__atomic_fetch_add"; |
| 529 | AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy); |
| 530 | break; |
| 531 | // T __atomic_fetch_and_N(T *mem, T val, int order) |
| 532 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 533 | case AtomicExpr::AO__atomic_fetch_and: |
| 534 | LibCallName = "__atomic_fetch_and"; |
| 535 | AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy); |
| 536 | break; |
| 537 | // T __atomic_fetch_or_N(T *mem, T val, int order) |
| 538 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 539 | case AtomicExpr::AO__atomic_fetch_or: |
| 540 | LibCallName = "__atomic_fetch_or"; |
| 541 | AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy); |
| 542 | break; |
| 543 | // T __atomic_fetch_sub_N(T *mem, T val, int order) |
| 544 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 545 | case AtomicExpr::AO__atomic_fetch_sub: |
| 546 | LibCallName = "__atomic_fetch_sub"; |
| 547 | AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy); |
| 548 | break; |
| 549 | // T __atomic_fetch_xor_N(T *mem, T val, int order) |
| 550 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 551 | case AtomicExpr::AO__atomic_fetch_xor: |
| 552 | LibCallName = "__atomic_fetch_xor"; |
| 553 | AddDirectArgument(*this, Args, UseOptimizedLibcall, Val1, MemTy); |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 554 | break; |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 555 | default: return EmitUnsupportedRValue(E, "atomic library call"); |
| 556 | } |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 557 | |
| 558 | // Optimized functions have the size in their name. |
| 559 | if (UseOptimizedLibcall) |
| 560 | LibCallName += "_" + llvm::utostr(Size); |
| 561 | // By default, assume we return a value of the atomic type. |
| 562 | if (!HaveRetTy) { |
| 563 | if (UseOptimizedLibcall) { |
| 564 | // Value is returned directly. |
| 565 | RetTy = MemTy; |
| 566 | } else { |
| 567 | // Value is returned through parameter before the order. |
| 568 | RetTy = getContext().VoidTy; |
| 569 | Args.add(RValue::get(EmitCastToVoidPtr(Dest)), |
| 570 | getContext().VoidPtrTy); |
| 571 | } |
| 572 | } |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 573 | // order is always the last parameter |
| 574 | Args.add(RValue::get(Order), |
| 575 | getContext().IntTy); |
| 576 | |
| 577 | const CGFunctionInfo &FuncInfo = |
| 578 | CGM.getTypes().arrangeFreeFunctionCall(RetTy, Args, |
| 579 | FunctionType::ExtInfo(), RequiredArgs::All); |
| 580 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 581 | llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName); |
| 582 | RValue Res = EmitCall(FuncInfo, Func, ReturnValueSlot(), Args); |
Ed Schouten | e469249 | 2013-05-31 19:27:59 +0000 | [diff] [blame] | 583 | if (!RetTy->isVoidType()) |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 584 | return Res; |
| 585 | if (E->getType()->isVoidType()) |
| 586 | return RValue::get(0); |
| 587 | return convertTempToRValue(Dest, E->getType()); |
| 588 | } |
| 589 | |
| 590 | bool IsStore = E->getOp() == AtomicExpr::AO__c11_atomic_store || |
| 591 | E->getOp() == AtomicExpr::AO__atomic_store || |
| 592 | E->getOp() == AtomicExpr::AO__atomic_store_n; |
| 593 | bool IsLoad = E->getOp() == AtomicExpr::AO__c11_atomic_load || |
| 594 | E->getOp() == AtomicExpr::AO__atomic_load || |
| 595 | E->getOp() == AtomicExpr::AO__atomic_load_n; |
| 596 | |
| 597 | llvm::Type *IPtrTy = |
| 598 | llvm::IntegerType::get(getLLVMContext(), Size * 8)->getPointerTo(); |
| 599 | llvm::Value *OrigDest = Dest; |
| 600 | Ptr = Builder.CreateBitCast(Ptr, IPtrTy); |
| 601 | if (Val1) Val1 = Builder.CreateBitCast(Val1, IPtrTy); |
| 602 | if (Val2) Val2 = Builder.CreateBitCast(Val2, IPtrTy); |
| 603 | if (Dest && !E->isCmpXChg()) Dest = Builder.CreateBitCast(Dest, IPtrTy); |
| 604 | |
| 605 | if (isa<llvm::ConstantInt>(Order)) { |
| 606 | int ord = cast<llvm::ConstantInt>(Order)->getZExtValue(); |
| 607 | switch (ord) { |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 608 | case AO_ABI_memory_order_relaxed: |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 609 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 610 | llvm::Monotonic); |
| 611 | break; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 612 | case AO_ABI_memory_order_consume: |
| 613 | case AO_ABI_memory_order_acquire: |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 614 | if (IsStore) |
| 615 | break; // Avoid crashing on code with undefined behavior |
| 616 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 617 | llvm::Acquire); |
| 618 | break; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 619 | case AO_ABI_memory_order_release: |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 620 | if (IsLoad) |
| 621 | break; // Avoid crashing on code with undefined behavior |
| 622 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 623 | llvm::Release); |
| 624 | break; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 625 | case AO_ABI_memory_order_acq_rel: |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 626 | if (IsLoad || IsStore) |
| 627 | break; // Avoid crashing on code with undefined behavior |
| 628 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 629 | llvm::AcquireRelease); |
| 630 | break; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 631 | case AO_ABI_memory_order_seq_cst: |
John McCall | fafaaef | 2013-03-07 21:37:12 +0000 | [diff] [blame] | 632 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 633 | llvm::SequentiallyConsistent); |
| 634 | break; |
| 635 | default: // invalid order |
| 636 | // We should not ever get here normally, but it's hard to |
| 637 | // enforce that in general. |
| 638 | break; |
| 639 | } |
| 640 | if (E->getType()->isVoidType()) |
| 641 | return RValue::get(0); |
| 642 | return convertTempToRValue(OrigDest, E->getType()); |
| 643 | } |
| 644 | |
| 645 | // Long case, when Order isn't obviously constant. |
| 646 | |
| 647 | // Create all the relevant BB's |
| 648 | llvm::BasicBlock *MonotonicBB = 0, *AcquireBB = 0, *ReleaseBB = 0, |
| 649 | *AcqRelBB = 0, *SeqCstBB = 0; |
| 650 | MonotonicBB = createBasicBlock("monotonic", CurFn); |
| 651 | if (!IsStore) |
| 652 | AcquireBB = createBasicBlock("acquire", CurFn); |
| 653 | if (!IsLoad) |
| 654 | ReleaseBB = createBasicBlock("release", CurFn); |
| 655 | if (!IsLoad && !IsStore) |
| 656 | AcqRelBB = createBasicBlock("acqrel", CurFn); |
| 657 | SeqCstBB = createBasicBlock("seqcst", CurFn); |
| 658 | llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn); |
| 659 | |
| 660 | // Create the switch for the split |
| 661 | // MonotonicBB is arbitrarily chosen as the default case; in practice, this |
| 662 | // doesn't matter unless someone is crazy enough to use something that |
| 663 | // doesn't fold to a constant for the ordering. |
| 664 | Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false); |
| 665 | llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB); |
| 666 | |
| 667 | // Emit all the different atomics |
| 668 | Builder.SetInsertPoint(MonotonicBB); |
| 669 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 670 | llvm::Monotonic); |
| 671 | Builder.CreateBr(ContBB); |
| 672 | if (!IsStore) { |
| 673 | Builder.SetInsertPoint(AcquireBB); |
| 674 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 675 | llvm::Acquire); |
| 676 | Builder.CreateBr(ContBB); |
| 677 | SI->addCase(Builder.getInt32(1), AcquireBB); |
| 678 | SI->addCase(Builder.getInt32(2), AcquireBB); |
| 679 | } |
| 680 | if (!IsLoad) { |
| 681 | Builder.SetInsertPoint(ReleaseBB); |
| 682 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 683 | llvm::Release); |
| 684 | Builder.CreateBr(ContBB); |
| 685 | SI->addCase(Builder.getInt32(3), ReleaseBB); |
| 686 | } |
| 687 | if (!IsLoad && !IsStore) { |
| 688 | Builder.SetInsertPoint(AcqRelBB); |
| 689 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 690 | llvm::AcquireRelease); |
| 691 | Builder.CreateBr(ContBB); |
| 692 | SI->addCase(Builder.getInt32(4), AcqRelBB); |
| 693 | } |
| 694 | Builder.SetInsertPoint(SeqCstBB); |
| 695 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 696 | llvm::SequentiallyConsistent); |
| 697 | Builder.CreateBr(ContBB); |
| 698 | SI->addCase(Builder.getInt32(5), SeqCstBB); |
| 699 | |
| 700 | // Cleanup and return |
| 701 | Builder.SetInsertPoint(ContBB); |
| 702 | if (E->getType()->isVoidType()) |
| 703 | return RValue::get(0); |
| 704 | return convertTempToRValue(OrigDest, E->getType()); |
| 705 | } |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 706 | |
| 707 | llvm::Value *AtomicInfo::emitCastToAtomicIntPointer(llvm::Value *addr) const { |
| 708 | unsigned addrspace = |
| 709 | cast<llvm::PointerType>(addr->getType())->getAddressSpace(); |
| 710 | llvm::IntegerType *ty = |
| 711 | llvm::IntegerType::get(CGF.getLLVMContext(), AtomicSizeInBits); |
| 712 | return CGF.Builder.CreateBitCast(addr, ty->getPointerTo(addrspace)); |
| 713 | } |
| 714 | |
| 715 | RValue AtomicInfo::convertTempToRValue(llvm::Value *addr, |
| 716 | AggValueSlot resultSlot) const { |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 717 | if (EvaluationKind == TEK_Aggregate) |
| 718 | return resultSlot.asRValue(); |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 719 | |
| 720 | // Drill into the padding structure if we have one. |
| 721 | if (hasPadding()) |
| 722 | addr = CGF.Builder.CreateStructGEP(addr, 0); |
| 723 | |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 724 | // Otherwise, just convert the temporary to an r-value using the |
| 725 | // normal conversion routine. |
| 726 | return CGF.convertTempToRValue(addr, getValueType()); |
| 727 | } |
| 728 | |
| 729 | /// Emit a load from an l-value of atomic type. Note that the r-value |
| 730 | /// we produce is an r-value of the atomic *value* type. |
| 731 | RValue CodeGenFunction::EmitAtomicLoad(LValue src, AggValueSlot resultSlot) { |
| 732 | AtomicInfo atomics(*this, src); |
| 733 | |
| 734 | // Check whether we should use a library call. |
| 735 | if (atomics.shouldUseLibcall()) { |
| 736 | llvm::Value *tempAddr; |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 737 | if (!resultSlot.isIgnored()) { |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 738 | assert(atomics.getEvaluationKind() == TEK_Aggregate); |
| 739 | tempAddr = resultSlot.getAddr(); |
| 740 | } else { |
| 741 | tempAddr = CreateMemTemp(atomics.getAtomicType(), "atomic-load-temp"); |
| 742 | } |
| 743 | |
| 744 | // void __atomic_load(size_t size, void *mem, void *return, int order); |
| 745 | CallArgList args; |
| 746 | args.add(RValue::get(atomics.getAtomicSizeValue()), |
| 747 | getContext().getSizeType()); |
| 748 | args.add(RValue::get(EmitCastToVoidPtr(src.getAddress())), |
| 749 | getContext().VoidPtrTy); |
| 750 | args.add(RValue::get(EmitCastToVoidPtr(tempAddr)), |
| 751 | getContext().VoidPtrTy); |
| 752 | args.add(RValue::get(llvm::ConstantInt::get(IntTy, |
| 753 | AO_ABI_memory_order_seq_cst)), |
| 754 | getContext().IntTy); |
| 755 | emitAtomicLibcall(*this, "__atomic_load", getContext().VoidTy, args); |
| 756 | |
| 757 | // Produce the r-value. |
| 758 | return atomics.convertTempToRValue(tempAddr, resultSlot); |
| 759 | } |
| 760 | |
| 761 | // Okay, we're doing this natively. |
| 762 | llvm::Value *addr = atomics.emitCastToAtomicIntPointer(src.getAddress()); |
| 763 | llvm::LoadInst *load = Builder.CreateLoad(addr, "atomic-load"); |
| 764 | load->setAtomic(llvm::SequentiallyConsistent); |
| 765 | |
| 766 | // Other decoration. |
| 767 | load->setAlignment(src.getAlignment().getQuantity()); |
| 768 | if (src.isVolatileQualified()) |
| 769 | load->setVolatile(true); |
| 770 | if (src.getTBAAInfo()) |
| 771 | CGM.DecorateInstruction(load, src.getTBAAInfo()); |
| 772 | |
| 773 | // Okay, turn that back into the original value type. |
| 774 | QualType valueType = atomics.getValueType(); |
| 775 | llvm::Value *result = load; |
| 776 | |
| 777 | // If we're ignoring an aggregate return, don't do anything. |
| 778 | if (atomics.getEvaluationKind() == TEK_Aggregate && resultSlot.isIgnored()) |
| 779 | return RValue::getAggregate(0, false); |
| 780 | |
| 781 | // The easiest way to do this this is to go through memory, but we |
| 782 | // try not to in some easy cases. |
| 783 | if (atomics.getEvaluationKind() == TEK_Scalar && !atomics.hasPadding()) { |
| 784 | llvm::Type *resultTy = CGM.getTypes().ConvertTypeForMem(valueType); |
| 785 | if (isa<llvm::IntegerType>(resultTy)) { |
| 786 | assert(result->getType() == resultTy); |
| 787 | result = EmitFromMemory(result, valueType); |
| 788 | } else if (isa<llvm::PointerType>(resultTy)) { |
| 789 | result = Builder.CreateIntToPtr(result, resultTy); |
| 790 | } else { |
| 791 | result = Builder.CreateBitCast(result, resultTy); |
| 792 | } |
| 793 | return RValue::get(result); |
| 794 | } |
| 795 | |
| 796 | // Create a temporary. This needs to be big enough to hold the |
| 797 | // atomic integer. |
| 798 | llvm::Value *temp; |
| 799 | bool tempIsVolatile = false; |
| 800 | CharUnits tempAlignment; |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 801 | if (atomics.getEvaluationKind() == TEK_Aggregate) { |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 802 | assert(!resultSlot.isIgnored()); |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 803 | temp = resultSlot.getAddr(); |
| 804 | tempAlignment = atomics.getValueAlignment(); |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 805 | tempIsVolatile = resultSlot.isVolatile(); |
| 806 | } else { |
| 807 | temp = CreateMemTemp(atomics.getAtomicType(), "atomic-load-temp"); |
| 808 | tempAlignment = atomics.getAtomicAlignment(); |
| 809 | } |
| 810 | |
| 811 | // Slam the integer into the temporary. |
| 812 | llvm::Value *castTemp = atomics.emitCastToAtomicIntPointer(temp); |
| 813 | Builder.CreateAlignedStore(result, castTemp, tempAlignment.getQuantity()) |
| 814 | ->setVolatile(tempIsVolatile); |
| 815 | |
| 816 | return atomics.convertTempToRValue(temp, resultSlot); |
| 817 | } |
| 818 | |
| 819 | |
| 820 | |
| 821 | /// Copy an r-value into memory as part of storing to an atomic type. |
| 822 | /// This needs to create a bit-pattern suitable for atomic operations. |
| 823 | void AtomicInfo::emitCopyIntoMemory(RValue rvalue, LValue dest) const { |
| 824 | // If we have an r-value, the rvalue should be of the atomic type, |
| 825 | // which means that the caller is responsible for having zeroed |
| 826 | // any padding. Just do an aggregate copy of that type. |
| 827 | if (rvalue.isAggregate()) { |
| 828 | CGF.EmitAggregateCopy(dest.getAddress(), |
| 829 | rvalue.getAggregateAddr(), |
| 830 | getAtomicType(), |
| 831 | (rvalue.isVolatileQualified() |
| 832 | || dest.isVolatileQualified()), |
| 833 | dest.getAlignment()); |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | // Okay, otherwise we're copying stuff. |
| 838 | |
| 839 | // Zero out the buffer if necessary. |
| 840 | emitMemSetZeroIfNecessary(dest); |
| 841 | |
| 842 | // Drill past the padding if present. |
| 843 | dest = projectValue(dest); |
| 844 | |
| 845 | // Okay, store the rvalue in. |
| 846 | if (rvalue.isScalar()) { |
| 847 | CGF.EmitStoreOfScalar(rvalue.getScalarVal(), dest, /*init*/ true); |
| 848 | } else { |
| 849 | CGF.EmitStoreOfComplex(rvalue.getComplexVal(), dest, /*init*/ true); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | |
| 854 | /// Materialize an r-value into memory for the purposes of storing it |
| 855 | /// to an atomic type. |
| 856 | llvm::Value *AtomicInfo::materializeRValue(RValue rvalue) const { |
| 857 | // Aggregate r-values are already in memory, and EmitAtomicStore |
| 858 | // requires them to be values of the atomic type. |
| 859 | if (rvalue.isAggregate()) |
| 860 | return rvalue.getAggregateAddr(); |
| 861 | |
| 862 | // Otherwise, make a temporary and materialize into it. |
| 863 | llvm::Value *temp = CGF.CreateMemTemp(getAtomicType(), "atomic-store-temp"); |
| 864 | LValue tempLV = CGF.MakeAddrLValue(temp, getAtomicType(), getAtomicAlignment()); |
| 865 | emitCopyIntoMemory(rvalue, tempLV); |
| 866 | return temp; |
| 867 | } |
| 868 | |
| 869 | /// Emit a store to an l-value of atomic type. |
| 870 | /// |
| 871 | /// Note that the r-value is expected to be an r-value *of the atomic |
| 872 | /// type*; this means that for aggregate r-values, it should include |
| 873 | /// storage for any padding that was necessary. |
Nick Lewycky | 5d4a755 | 2013-10-01 21:51:38 +0000 | [diff] [blame^] | 874 | void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue dest, bool isInit) { |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 875 | // If this is an aggregate r-value, it should agree in type except |
| 876 | // maybe for address-space qualification. |
| 877 | assert(!rvalue.isAggregate() || |
| 878 | rvalue.getAggregateAddr()->getType()->getPointerElementType() |
| 879 | == dest.getAddress()->getType()->getPointerElementType()); |
| 880 | |
| 881 | AtomicInfo atomics(*this, dest); |
| 882 | |
| 883 | // If this is an initialization, just put the value there normally. |
| 884 | if (isInit) { |
| 885 | atomics.emitCopyIntoMemory(rvalue, dest); |
| 886 | return; |
| 887 | } |
| 888 | |
| 889 | // Check whether we should use a library call. |
| 890 | if (atomics.shouldUseLibcall()) { |
| 891 | // Produce a source address. |
| 892 | llvm::Value *srcAddr = atomics.materializeRValue(rvalue); |
| 893 | |
| 894 | // void __atomic_store(size_t size, void *mem, void *val, int order) |
| 895 | CallArgList args; |
| 896 | args.add(RValue::get(atomics.getAtomicSizeValue()), |
| 897 | getContext().getSizeType()); |
| 898 | args.add(RValue::get(EmitCastToVoidPtr(dest.getAddress())), |
| 899 | getContext().VoidPtrTy); |
| 900 | args.add(RValue::get(EmitCastToVoidPtr(srcAddr)), |
| 901 | getContext().VoidPtrTy); |
| 902 | args.add(RValue::get(llvm::ConstantInt::get(IntTy, |
| 903 | AO_ABI_memory_order_seq_cst)), |
| 904 | getContext().IntTy); |
| 905 | emitAtomicLibcall(*this, "__atomic_store", getContext().VoidTy, args); |
| 906 | return; |
| 907 | } |
| 908 | |
| 909 | // Okay, we're doing this natively. |
| 910 | llvm::Value *intValue; |
| 911 | |
| 912 | // If we've got a scalar value of the right size, try to avoid going |
| 913 | // through memory. |
| 914 | if (rvalue.isScalar() && !atomics.hasPadding()) { |
| 915 | llvm::Value *value = rvalue.getScalarVal(); |
| 916 | if (isa<llvm::IntegerType>(value->getType())) { |
| 917 | intValue = value; |
| 918 | } else { |
| 919 | llvm::IntegerType *inputIntTy = |
| 920 | llvm::IntegerType::get(getLLVMContext(), atomics.getValueSizeInBits()); |
| 921 | if (isa<llvm::PointerType>(value->getType())) { |
| 922 | intValue = Builder.CreatePtrToInt(value, inputIntTy); |
| 923 | } else { |
| 924 | intValue = Builder.CreateBitCast(value, inputIntTy); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | // Otherwise, we need to go through memory. |
| 929 | } else { |
| 930 | // Put the r-value in memory. |
| 931 | llvm::Value *addr = atomics.materializeRValue(rvalue); |
| 932 | |
| 933 | // Cast the temporary to the atomic int type and pull a value out. |
| 934 | addr = atomics.emitCastToAtomicIntPointer(addr); |
| 935 | intValue = Builder.CreateAlignedLoad(addr, |
| 936 | atomics.getAtomicAlignment().getQuantity()); |
| 937 | } |
| 938 | |
| 939 | // Do the atomic store. |
| 940 | llvm::Value *addr = atomics.emitCastToAtomicIntPointer(dest.getAddress()); |
| 941 | llvm::StoreInst *store = Builder.CreateStore(intValue, addr); |
| 942 | |
| 943 | // Initializations don't need to be atomic. |
| 944 | if (!isInit) store->setAtomic(llvm::SequentiallyConsistent); |
| 945 | |
| 946 | // Other decoration. |
| 947 | store->setAlignment(dest.getAlignment().getQuantity()); |
| 948 | if (dest.isVolatileQualified()) |
| 949 | store->setVolatile(true); |
| 950 | if (dest.getTBAAInfo()) |
| 951 | CGM.DecorateInstruction(store, dest.getTBAAInfo()); |
| 952 | } |
| 953 | |
| 954 | void CodeGenFunction::EmitAtomicInit(Expr *init, LValue dest) { |
| 955 | AtomicInfo atomics(*this, dest); |
| 956 | |
| 957 | switch (atomics.getEvaluationKind()) { |
| 958 | case TEK_Scalar: { |
| 959 | llvm::Value *value = EmitScalarExpr(init); |
| 960 | atomics.emitCopyIntoMemory(RValue::get(value), dest); |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | case TEK_Complex: { |
| 965 | ComplexPairTy value = EmitComplexExpr(init); |
| 966 | atomics.emitCopyIntoMemory(RValue::getComplex(value), dest); |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | case TEK_Aggregate: { |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 971 | // Fix up the destination if the initializer isn't an expression |
| 972 | // of atomic type. |
| 973 | bool Zeroed = false; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 974 | if (!init->getType()->isAtomicType()) { |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 975 | Zeroed = atomics.emitMemSetZeroIfNecessary(dest); |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 976 | dest = atomics.projectValue(dest); |
| 977 | } |
| 978 | |
| 979 | // Evaluate the expression directly into the destination. |
| 980 | AggValueSlot slot = AggValueSlot::forLValue(dest, |
| 981 | AggValueSlot::IsNotDestructed, |
| 982 | AggValueSlot::DoesNotNeedGCBarriers, |
Eli Friedman | 336d9df | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 983 | AggValueSlot::IsNotAliased, |
| 984 | Zeroed ? AggValueSlot::IsZeroed : |
| 985 | AggValueSlot::IsNotZeroed); |
| 986 | |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 987 | EmitAggExpr(init, slot); |
| 988 | return; |
| 989 | } |
| 990 | } |
| 991 | llvm_unreachable("bad evaluation kind"); |
| 992 | } |