Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 1 | //===-- AtomicExpandPass.cpp - Expand atomic instructions -------===// |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 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 a pass (at IR level) to replace atomic instructions with |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 11 | // either (intrinsic-based) load-linked/store-conditional loops or AtomicCmpXchg. |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/Passes.h" |
| 16 | #include "llvm/IR/Function.h" |
| 17 | #include "llvm/IR/IRBuilder.h" |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 18 | #include "llvm/IR/InstIterator.h" |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Instructions.h" |
| 20 | #include "llvm/IR/Intrinsics.h" |
| 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Target/TargetLowering.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | c40e5ed | 2014-06-19 21:03:04 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 26 | |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "atomic-expand" |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 30 | |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 31 | namespace { |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 32 | class AtomicExpand: public FunctionPass { |
Eric Christopher | c40e5ed | 2014-06-19 21:03:04 +0000 | [diff] [blame] | 33 | const TargetMachine *TM; |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 34 | const TargetLowering *TLI; |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 35 | public: |
| 36 | static char ID; // Pass identification, replacement for typeid |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 37 | explicit AtomicExpand(const TargetMachine *TM = nullptr) |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 38 | : FunctionPass(ID), TM(TM), TLI(nullptr) { |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 39 | initializeAtomicExpandPass(*PassRegistry::getPassRegistry()); |
Tim Northover | 037f26f2 | 2014-04-17 18:22:47 +0000 | [diff] [blame] | 40 | } |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 41 | |
| 42 | bool runOnFunction(Function &F) override; |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 43 | |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 44 | private: |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 45 | bool bracketInstWithFences(Instruction *I, AtomicOrdering Order, |
| 46 | bool IsStore, bool IsLoad); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 47 | bool expandAtomicLoad(LoadInst *LI); |
Robin Morisset | 6dbbbc2 | 2014-09-23 20:59:25 +0000 | [diff] [blame] | 48 | bool expandAtomicLoadToLL(LoadInst *LI); |
| 49 | bool expandAtomicLoadToCmpXchg(LoadInst *LI); |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 50 | bool expandAtomicStore(StoreInst *SI); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 51 | bool expandAtomicRMW(AtomicRMWInst *AI); |
Robin Morisset | 25c8e31 | 2014-09-17 00:06:58 +0000 | [diff] [blame] | 52 | bool expandAtomicRMWToLLSC(AtomicRMWInst *AI); |
| 53 | bool expandAtomicRMWToCmpXchg(AtomicRMWInst *AI); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 54 | bool expandAtomicCmpXchg(AtomicCmpXchgInst *CI); |
Robin Morisset | 810739d | 2014-09-25 17:27:43 +0000 | [diff] [blame] | 55 | bool isIdempotentRMW(AtomicRMWInst *AI); |
| 56 | bool simplifyIdempotentRMW(AtomicRMWInst *AI); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 57 | }; |
| 58 | } |
| 59 | |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 60 | char AtomicExpand::ID = 0; |
| 61 | char &llvm::AtomicExpandID = AtomicExpand::ID; |
| 62 | INITIALIZE_TM_PASS(AtomicExpand, "atomic-expand", |
| 63 | "Expand Atomic calls in terms of either load-linked & store-conditional or cmpxchg", |
Jiangning Liu | d623c52 | 2014-06-11 07:04:37 +0000 | [diff] [blame] | 64 | false, false) |
Tim Northover | 037f26f2 | 2014-04-17 18:22:47 +0000 | [diff] [blame] | 65 | |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 66 | FunctionPass *llvm::createAtomicExpandPass(const TargetMachine *TM) { |
| 67 | return new AtomicExpand(TM); |
Tim Northover | 037f26f2 | 2014-04-17 18:22:47 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 70 | bool AtomicExpand::runOnFunction(Function &F) { |
Eric Christopher | 4e048eeb | 2015-01-27 01:04:42 +0000 | [diff] [blame^] | 71 | if (!TM || !TM->getSubtargetImpl(F)->enableAtomicExpand()) |
Tim Northover | 037f26f2 | 2014-04-17 18:22:47 +0000 | [diff] [blame] | 72 | return false; |
Eric Christopher | 4e048eeb | 2015-01-27 01:04:42 +0000 | [diff] [blame^] | 73 | TLI = TM->getSubtargetImpl(F)->getTargetLowering(); |
Tim Northover | 037f26f2 | 2014-04-17 18:22:47 +0000 | [diff] [blame] | 74 | |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 75 | SmallVector<Instruction *, 1> AtomicInsts; |
| 76 | |
| 77 | // Changing control-flow while iterating through it is a bad idea, so gather a |
| 78 | // list of all atomic instructions before we start. |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 79 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
| 80 | if (I->isAtomic()) |
| 81 | AtomicInsts.push_back(&*I); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 84 | bool MadeChange = false; |
| 85 | for (auto I : AtomicInsts) { |
| 86 | auto LI = dyn_cast<LoadInst>(I); |
| 87 | auto SI = dyn_cast<StoreInst>(I); |
| 88 | auto RMWI = dyn_cast<AtomicRMWInst>(I); |
| 89 | auto CASI = dyn_cast<AtomicCmpXchgInst>(I); |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 90 | assert((LI || SI || RMWI || CASI || isa<FenceInst>(I)) && |
| 91 | "Unknown atomic instruction"); |
| 92 | |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 93 | auto FenceOrdering = Monotonic; |
| 94 | bool IsStore, IsLoad; |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 95 | if (TLI->getInsertFencesForAtomic()) { |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 96 | if (LI && isAtLeastAcquire(LI->getOrdering())) { |
| 97 | FenceOrdering = LI->getOrdering(); |
| 98 | LI->setOrdering(Monotonic); |
| 99 | IsStore = false; |
| 100 | IsLoad = true; |
| 101 | } else if (SI && isAtLeastRelease(SI->getOrdering())) { |
| 102 | FenceOrdering = SI->getOrdering(); |
| 103 | SI->setOrdering(Monotonic); |
| 104 | IsStore = true; |
| 105 | IsLoad = false; |
| 106 | } else if (RMWI && (isAtLeastRelease(RMWI->getOrdering()) || |
| 107 | isAtLeastAcquire(RMWI->getOrdering()))) { |
| 108 | FenceOrdering = RMWI->getOrdering(); |
| 109 | RMWI->setOrdering(Monotonic); |
| 110 | IsStore = IsLoad = true; |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 111 | } else if (CASI && !TLI->hasLoadLinkedStoreConditional() && |
| 112 | (isAtLeastRelease(CASI->getSuccessOrdering()) || |
| 113 | isAtLeastAcquire(CASI->getSuccessOrdering()))) { |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 114 | // If a compare and swap is lowered to LL/SC, we can do smarter fence |
| 115 | // insertion, with a stronger one on the success path than on the |
| 116 | // failure path. As a result, fence insertion is directly done by |
| 117 | // expandAtomicCmpXchg in that case. |
| 118 | FenceOrdering = CASI->getSuccessOrdering(); |
| 119 | CASI->setSuccessOrdering(Monotonic); |
| 120 | CASI->setFailureOrdering(Monotonic); |
| 121 | IsStore = IsLoad = true; |
| 122 | } |
| 123 | |
| 124 | if (FenceOrdering != Monotonic) { |
| 125 | MadeChange |= bracketInstWithFences(I, FenceOrdering, IsStore, IsLoad); |
| 126 | } |
| 127 | } |
| 128 | |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 129 | if (LI && TLI->shouldExpandAtomicLoadInIR(LI)) { |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 130 | MadeChange |= expandAtomicLoad(LI); |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 131 | } else if (SI && TLI->shouldExpandAtomicStoreInIR(SI)) { |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 132 | MadeChange |= expandAtomicStore(SI); |
Robin Morisset | 810739d | 2014-09-25 17:27:43 +0000 | [diff] [blame] | 133 | } else if (RMWI) { |
| 134 | // There are two different ways of expanding RMW instructions: |
| 135 | // - into a load if it is idempotent |
| 136 | // - into a Cmpxchg/LL-SC loop otherwise |
| 137 | // we try them in that order. |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 138 | MadeChange |= |
| 139 | (isIdempotentRMW(RMWI) && simplifyIdempotentRMW(RMWI)) || |
| 140 | (TLI->shouldExpandAtomicRMWInIR(RMWI) && expandAtomicRMW(RMWI)); |
| 141 | } else if (CASI && TLI->hasLoadLinkedStoreConditional()) { |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 142 | MadeChange |= expandAtomicCmpXchg(CASI); |
| 143 | } |
| 144 | } |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 145 | return MadeChange; |
| 146 | } |
| 147 | |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 148 | bool AtomicExpand::bracketInstWithFences(Instruction *I, AtomicOrdering Order, |
| 149 | bool IsStore, bool IsLoad) { |
| 150 | IRBuilder<> Builder(I); |
| 151 | |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 152 | auto LeadingFence = TLI->emitLeadingFence(Builder, Order, IsStore, IsLoad); |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 153 | |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 154 | auto TrailingFence = TLI->emitTrailingFence(Builder, Order, IsStore, IsLoad); |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 155 | // The trailing fence is emitted before the instruction instead of after |
| 156 | // because there is no easy way of setting Builder insertion point after |
| 157 | // an instruction. So we must erase it from the BB, and insert it back |
| 158 | // in the right place. |
| 159 | // We have a guard here because not every atomic operation generates a |
| 160 | // trailing fence. |
| 161 | if (TrailingFence) { |
| 162 | TrailingFence->removeFromParent(); |
| 163 | TrailingFence->insertAfter(I); |
| 164 | } |
| 165 | |
| 166 | return (LeadingFence || TrailingFence); |
| 167 | } |
| 168 | |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 169 | bool AtomicExpand::expandAtomicLoad(LoadInst *LI) { |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 170 | if (TLI->hasLoadLinkedStoreConditional()) |
Robin Morisset | 6dbbbc2 | 2014-09-23 20:59:25 +0000 | [diff] [blame] | 171 | return expandAtomicLoadToLL(LI); |
| 172 | else |
| 173 | return expandAtomicLoadToCmpXchg(LI); |
| 174 | } |
| 175 | |
| 176 | bool AtomicExpand::expandAtomicLoadToLL(LoadInst *LI) { |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 177 | IRBuilder<> Builder(LI); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 178 | |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 179 | // On some architectures, load-linked instructions are atomic for larger |
| 180 | // sizes than normal loads. For example, the only 64-bit load guaranteed |
| 181 | // to be single-copy atomic by ARM is an ldrexd (A3.5.3). |
Robin Morisset | a47cb41 | 2014-09-03 21:01:03 +0000 | [diff] [blame] | 182 | Value *Val = |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 183 | TLI->emitLoadLinked(Builder, LI->getPointerOperand(), LI->getOrdering()); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 184 | |
| 185 | LI->replaceAllUsesWith(Val); |
| 186 | LI->eraseFromParent(); |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | |
Robin Morisset | 6dbbbc2 | 2014-09-23 20:59:25 +0000 | [diff] [blame] | 191 | bool AtomicExpand::expandAtomicLoadToCmpXchg(LoadInst *LI) { |
| 192 | IRBuilder<> Builder(LI); |
| 193 | AtomicOrdering Order = LI->getOrdering(); |
| 194 | Value *Addr = LI->getPointerOperand(); |
| 195 | Type *Ty = cast<PointerType>(Addr->getType())->getElementType(); |
| 196 | Constant *DummyVal = Constant::getNullValue(Ty); |
| 197 | |
| 198 | Value *Pair = Builder.CreateAtomicCmpXchg( |
| 199 | Addr, DummyVal, DummyVal, Order, |
| 200 | AtomicCmpXchgInst::getStrongestFailureOrdering(Order)); |
| 201 | Value *Loaded = Builder.CreateExtractValue(Pair, 0, "loaded"); |
| 202 | |
| 203 | LI->replaceAllUsesWith(Loaded); |
| 204 | LI->eraseFromParent(); |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 209 | bool AtomicExpand::expandAtomicStore(StoreInst *SI) { |
Robin Morisset | 25c8e31 | 2014-09-17 00:06:58 +0000 | [diff] [blame] | 210 | // This function is only called on atomic stores that are too large to be |
| 211 | // atomic if implemented as a native store. So we replace them by an |
| 212 | // atomic swap, that can be implemented for example as a ldrex/strex on ARM |
| 213 | // or lock cmpxchg8/16b on X86, as these are atomic for larger sizes. |
| 214 | // It is the responsibility of the target to only return true in |
| 215 | // shouldExpandAtomicRMW in cases where this is required and possible. |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 216 | IRBuilder<> Builder(SI); |
| 217 | AtomicRMWInst *AI = |
| 218 | Builder.CreateAtomicRMW(AtomicRMWInst::Xchg, SI->getPointerOperand(), |
| 219 | SI->getValueOperand(), SI->getOrdering()); |
| 220 | SI->eraseFromParent(); |
| 221 | |
| 222 | // Now we have an appropriate swap instruction, lower it as usual. |
| 223 | return expandAtomicRMW(AI); |
| 224 | } |
| 225 | |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 226 | bool AtomicExpand::expandAtomicRMW(AtomicRMWInst *AI) { |
Eric Christopher | b11a1b7 | 2015-01-26 19:45:40 +0000 | [diff] [blame] | 227 | if (TLI->hasLoadLinkedStoreConditional()) |
Robin Morisset | 25c8e31 | 2014-09-17 00:06:58 +0000 | [diff] [blame] | 228 | return expandAtomicRMWToLLSC(AI); |
| 229 | else |
| 230 | return expandAtomicRMWToCmpXchg(AI); |
| 231 | } |
| 232 | |
| 233 | /// Emit IR to implement the given atomicrmw operation on values in registers, |
| 234 | /// returning the new value. |
| 235 | static Value *performAtomicOp(AtomicRMWInst::BinOp Op, IRBuilder<> &Builder, |
| 236 | Value *Loaded, Value *Inc) { |
| 237 | Value *NewVal; |
| 238 | switch (Op) { |
| 239 | case AtomicRMWInst::Xchg: |
| 240 | return Inc; |
| 241 | case AtomicRMWInst::Add: |
| 242 | return Builder.CreateAdd(Loaded, Inc, "new"); |
| 243 | case AtomicRMWInst::Sub: |
| 244 | return Builder.CreateSub(Loaded, Inc, "new"); |
| 245 | case AtomicRMWInst::And: |
| 246 | return Builder.CreateAnd(Loaded, Inc, "new"); |
| 247 | case AtomicRMWInst::Nand: |
| 248 | return Builder.CreateNot(Builder.CreateAnd(Loaded, Inc), "new"); |
| 249 | case AtomicRMWInst::Or: |
| 250 | return Builder.CreateOr(Loaded, Inc, "new"); |
| 251 | case AtomicRMWInst::Xor: |
| 252 | return Builder.CreateXor(Loaded, Inc, "new"); |
| 253 | case AtomicRMWInst::Max: |
| 254 | NewVal = Builder.CreateICmpSGT(Loaded, Inc); |
| 255 | return Builder.CreateSelect(NewVal, Loaded, Inc, "new"); |
| 256 | case AtomicRMWInst::Min: |
| 257 | NewVal = Builder.CreateICmpSLE(Loaded, Inc); |
| 258 | return Builder.CreateSelect(NewVal, Loaded, Inc, "new"); |
| 259 | case AtomicRMWInst::UMax: |
| 260 | NewVal = Builder.CreateICmpUGT(Loaded, Inc); |
| 261 | return Builder.CreateSelect(NewVal, Loaded, Inc, "new"); |
| 262 | case AtomicRMWInst::UMin: |
| 263 | NewVal = Builder.CreateICmpULE(Loaded, Inc); |
| 264 | return Builder.CreateSelect(NewVal, Loaded, Inc, "new"); |
| 265 | default: |
| 266 | llvm_unreachable("Unknown atomic op"); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | bool AtomicExpand::expandAtomicRMWToLLSC(AtomicRMWInst *AI) { |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 271 | AtomicOrdering MemOpOrder = AI->getOrdering(); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 272 | Value *Addr = AI->getPointerOperand(); |
| 273 | BasicBlock *BB = AI->getParent(); |
| 274 | Function *F = BB->getParent(); |
| 275 | LLVMContext &Ctx = F->getContext(); |
| 276 | |
| 277 | // Given: atomicrmw some_op iN* %addr, iN %incr ordering |
| 278 | // |
| 279 | // The standard expansion we produce is: |
| 280 | // [...] |
| 281 | // fence? |
| 282 | // atomicrmw.start: |
| 283 | // %loaded = @load.linked(%addr) |
| 284 | // %new = some_op iN %loaded, %incr |
| 285 | // %stored = @store_conditional(%new, %addr) |
| 286 | // %try_again = icmp i32 ne %stored, 0 |
| 287 | // br i1 %try_again, label %loop, label %atomicrmw.end |
| 288 | // atomicrmw.end: |
| 289 | // fence? |
| 290 | // [...] |
| 291 | BasicBlock *ExitBB = BB->splitBasicBlock(AI, "atomicrmw.end"); |
| 292 | BasicBlock *LoopBB = BasicBlock::Create(Ctx, "atomicrmw.start", F, ExitBB); |
| 293 | |
| 294 | // This grabs the DebugLoc from AI. |
| 295 | IRBuilder<> Builder(AI); |
| 296 | |
| 297 | // The split call above "helpfully" added a branch at the end of BB (to the |
| 298 | // wrong place), but we might want a fence too. It's easiest to just remove |
| 299 | // the branch entirely. |
| 300 | std::prev(BB->end())->eraseFromParent(); |
| 301 | Builder.SetInsertPoint(BB); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 302 | Builder.CreateBr(LoopBB); |
| 303 | |
| 304 | // Start the main loop block now that we've taken care of the preliminaries. |
| 305 | Builder.SetInsertPoint(LoopBB); |
Robin Morisset | a47cb41 | 2014-09-03 21:01:03 +0000 | [diff] [blame] | 306 | Value *Loaded = TLI->emitLoadLinked(Builder, Addr, MemOpOrder); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 307 | |
Robin Morisset | 25c8e31 | 2014-09-17 00:06:58 +0000 | [diff] [blame] | 308 | Value *NewVal = |
| 309 | performAtomicOp(AI->getOperation(), Builder, Loaded, AI->getValOperand()); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 310 | |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 311 | Value *StoreSuccess = |
Robin Morisset | a47cb41 | 2014-09-03 21:01:03 +0000 | [diff] [blame] | 312 | TLI->emitStoreConditional(Builder, NewVal, Addr, MemOpOrder); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 313 | Value *TryAgain = Builder.CreateICmpNE( |
| 314 | StoreSuccess, ConstantInt::get(IntegerType::get(Ctx, 32), 0), "tryagain"); |
| 315 | Builder.CreateCondBr(TryAgain, LoopBB, ExitBB); |
| 316 | |
| 317 | Builder.SetInsertPoint(ExitBB, ExitBB->begin()); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 318 | |
| 319 | AI->replaceAllUsesWith(Loaded); |
| 320 | AI->eraseFromParent(); |
| 321 | |
| 322 | return true; |
| 323 | } |
| 324 | |
Robin Morisset | 25c8e31 | 2014-09-17 00:06:58 +0000 | [diff] [blame] | 325 | bool AtomicExpand::expandAtomicRMWToCmpXchg(AtomicRMWInst *AI) { |
Robin Morisset | 25c8e31 | 2014-09-17 00:06:58 +0000 | [diff] [blame] | 326 | AtomicOrdering MemOpOrder = |
Robin Morisset | dedef33 | 2014-09-23 20:31:14 +0000 | [diff] [blame] | 327 | AI->getOrdering() == Unordered ? Monotonic : AI->getOrdering(); |
Robin Morisset | 25c8e31 | 2014-09-17 00:06:58 +0000 | [diff] [blame] | 328 | Value *Addr = AI->getPointerOperand(); |
| 329 | BasicBlock *BB = AI->getParent(); |
| 330 | Function *F = BB->getParent(); |
| 331 | LLVMContext &Ctx = F->getContext(); |
| 332 | |
| 333 | // Given: atomicrmw some_op iN* %addr, iN %incr ordering |
| 334 | // |
| 335 | // The standard expansion we produce is: |
| 336 | // [...] |
| 337 | // %init_loaded = load atomic iN* %addr |
| 338 | // br label %loop |
| 339 | // loop: |
| 340 | // %loaded = phi iN [ %init_loaded, %entry ], [ %new_loaded, %loop ] |
| 341 | // %new = some_op iN %loaded, %incr |
| 342 | // %pair = cmpxchg iN* %addr, iN %loaded, iN %new |
| 343 | // %new_loaded = extractvalue { iN, i1 } %pair, 0 |
| 344 | // %success = extractvalue { iN, i1 } %pair, 1 |
| 345 | // br i1 %success, label %atomicrmw.end, label %loop |
| 346 | // atomicrmw.end: |
| 347 | // [...] |
| 348 | BasicBlock *ExitBB = BB->splitBasicBlock(AI, "atomicrmw.end"); |
| 349 | BasicBlock *LoopBB = BasicBlock::Create(Ctx, "atomicrmw.start", F, ExitBB); |
| 350 | |
| 351 | // This grabs the DebugLoc from AI. |
| 352 | IRBuilder<> Builder(AI); |
| 353 | |
| 354 | // The split call above "helpfully" added a branch at the end of BB (to the |
| 355 | // wrong place), but we want a load. It's easiest to just remove |
| 356 | // the branch entirely. |
| 357 | std::prev(BB->end())->eraseFromParent(); |
| 358 | Builder.SetInsertPoint(BB); |
Robin Morisset | 25c8e31 | 2014-09-17 00:06:58 +0000 | [diff] [blame] | 359 | LoadInst *InitLoaded = Builder.CreateLoad(Addr); |
| 360 | // Atomics require at least natural alignment. |
| 361 | InitLoaded->setAlignment(AI->getType()->getPrimitiveSizeInBits()); |
| 362 | Builder.CreateBr(LoopBB); |
| 363 | |
| 364 | // Start the main loop block now that we've taken care of the preliminaries. |
| 365 | Builder.SetInsertPoint(LoopBB); |
| 366 | PHINode *Loaded = Builder.CreatePHI(AI->getType(), 2, "loaded"); |
| 367 | Loaded->addIncoming(InitLoaded, BB); |
| 368 | |
| 369 | Value *NewVal = |
| 370 | performAtomicOp(AI->getOperation(), Builder, Loaded, AI->getValOperand()); |
| 371 | |
| 372 | Value *Pair = Builder.CreateAtomicCmpXchg( |
| 373 | Addr, Loaded, NewVal, MemOpOrder, |
| 374 | AtomicCmpXchgInst::getStrongestFailureOrdering(MemOpOrder)); |
| 375 | Value *NewLoaded = Builder.CreateExtractValue(Pair, 0, "newloaded"); |
| 376 | Loaded->addIncoming(NewLoaded, LoopBB); |
| 377 | |
| 378 | Value *Success = Builder.CreateExtractValue(Pair, 1, "success"); |
| 379 | Builder.CreateCondBr(Success, ExitBB, LoopBB); |
| 380 | |
| 381 | Builder.SetInsertPoint(ExitBB, ExitBB->begin()); |
Robin Morisset | 25c8e31 | 2014-09-17 00:06:58 +0000 | [diff] [blame] | 382 | |
| 383 | AI->replaceAllUsesWith(NewLoaded); |
| 384 | AI->eraseFromParent(); |
| 385 | |
| 386 | return true; |
| 387 | } |
| 388 | |
Robin Morisset | 59c23cd | 2014-08-21 21:50:01 +0000 | [diff] [blame] | 389 | bool AtomicExpand::expandAtomicCmpXchg(AtomicCmpXchgInst *CI) { |
Tim Northover | 70450c5 | 2014-04-03 13:06:54 +0000 | [diff] [blame] | 390 | AtomicOrdering SuccessOrder = CI->getSuccessOrdering(); |
| 391 | AtomicOrdering FailureOrder = CI->getFailureOrdering(); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 392 | Value *Addr = CI->getPointerOperand(); |
| 393 | BasicBlock *BB = CI->getParent(); |
| 394 | Function *F = BB->getParent(); |
| 395 | LLVMContext &Ctx = F->getContext(); |
Robin Morisset | ed3d48f | 2014-09-03 21:29:59 +0000 | [diff] [blame] | 396 | // If getInsertFencesForAtomic() returns true, then the target does not want |
| 397 | // to deal with memory orders, and emitLeading/TrailingFence should take care |
| 398 | // of everything. Otherwise, emitLeading/TrailingFence are no-op and we |
| 399 | // should preserve the ordering. |
Robin Morisset | a47cb41 | 2014-09-03 21:01:03 +0000 | [diff] [blame] | 400 | AtomicOrdering MemOpOrder = |
| 401 | TLI->getInsertFencesForAtomic() ? Monotonic : SuccessOrder; |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 402 | |
| 403 | // Given: cmpxchg some_op iN* %addr, iN %desired, iN %new success_ord fail_ord |
| 404 | // |
Tim Northover | 70450c5 | 2014-04-03 13:06:54 +0000 | [diff] [blame] | 405 | // The full expansion we produce is: |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 406 | // [...] |
| 407 | // fence? |
| 408 | // cmpxchg.start: |
| 409 | // %loaded = @load.linked(%addr) |
| 410 | // %should_store = icmp eq %loaded, %desired |
Tim Northover | 70450c5 | 2014-04-03 13:06:54 +0000 | [diff] [blame] | 411 | // br i1 %should_store, label %cmpxchg.trystore, |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 412 | // label %cmpxchg.failure |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 413 | // cmpxchg.trystore: |
| 414 | // %stored = @store_conditional(%new, %addr) |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 415 | // %success = icmp eq i32 %stored, 0 |
| 416 | // br i1 %success, label %cmpxchg.success, label %loop/%cmpxchg.failure |
| 417 | // cmpxchg.success: |
| 418 | // fence? |
| 419 | // br label %cmpxchg.end |
| 420 | // cmpxchg.failure: |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 421 | // fence? |
Tim Northover | 70450c5 | 2014-04-03 13:06:54 +0000 | [diff] [blame] | 422 | // br label %cmpxchg.end |
| 423 | // cmpxchg.end: |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 424 | // %success = phi i1 [true, %cmpxchg.success], [false, %cmpxchg.failure] |
| 425 | // %restmp = insertvalue { iN, i1 } undef, iN %loaded, 0 |
| 426 | // %res = insertvalue { iN, i1 } %restmp, i1 %success, 1 |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 427 | // [...] |
| 428 | BasicBlock *ExitBB = BB->splitBasicBlock(CI, "cmpxchg.end"); |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 429 | auto FailureBB = BasicBlock::Create(Ctx, "cmpxchg.failure", F, ExitBB); |
| 430 | auto SuccessBB = BasicBlock::Create(Ctx, "cmpxchg.success", F, FailureBB); |
| 431 | auto TryStoreBB = BasicBlock::Create(Ctx, "cmpxchg.trystore", F, SuccessBB); |
Tim Northover | 70450c5 | 2014-04-03 13:06:54 +0000 | [diff] [blame] | 432 | auto LoopBB = BasicBlock::Create(Ctx, "cmpxchg.start", F, TryStoreBB); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 433 | |
| 434 | // This grabs the DebugLoc from CI |
| 435 | IRBuilder<> Builder(CI); |
| 436 | |
| 437 | // The split call above "helpfully" added a branch at the end of BB (to the |
| 438 | // wrong place), but we might want a fence too. It's easiest to just remove |
| 439 | // the branch entirely. |
| 440 | std::prev(BB->end())->eraseFromParent(); |
| 441 | Builder.SetInsertPoint(BB); |
Robin Morisset | a47cb41 | 2014-09-03 21:01:03 +0000 | [diff] [blame] | 442 | TLI->emitLeadingFence(Builder, SuccessOrder, /*IsStore=*/true, |
| 443 | /*IsLoad=*/true); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 444 | Builder.CreateBr(LoopBB); |
| 445 | |
| 446 | // Start the main loop block now that we've taken care of the preliminaries. |
| 447 | Builder.SetInsertPoint(LoopBB); |
Robin Morisset | a47cb41 | 2014-09-03 21:01:03 +0000 | [diff] [blame] | 448 | Value *Loaded = TLI->emitLoadLinked(Builder, Addr, MemOpOrder); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 449 | Value *ShouldStore = |
| 450 | Builder.CreateICmpEQ(Loaded, CI->getCompareOperand(), "should_store"); |
Tim Northover | 70450c5 | 2014-04-03 13:06:54 +0000 | [diff] [blame] | 451 | |
| 452 | // If the the cmpxchg doesn't actually need any ordering when it fails, we can |
| 453 | // jump straight past that fence instruction (if it exists). |
Tim Northover | 70450c5 | 2014-04-03 13:06:54 +0000 | [diff] [blame] | 454 | Builder.CreateCondBr(ShouldStore, TryStoreBB, FailureBB); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 455 | |
| 456 | Builder.SetInsertPoint(TryStoreBB); |
Robin Morisset | a47cb41 | 2014-09-03 21:01:03 +0000 | [diff] [blame] | 457 | Value *StoreSuccess = TLI->emitStoreConditional( |
| 458 | Builder, CI->getNewValOperand(), Addr, MemOpOrder); |
Tim Northover | d039abd | 2014-06-13 16:45:36 +0000 | [diff] [blame] | 459 | StoreSuccess = Builder.CreateICmpEQ( |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 460 | StoreSuccess, ConstantInt::get(Type::getInt32Ty(Ctx), 0), "success"); |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 461 | Builder.CreateCondBr(StoreSuccess, SuccessBB, |
| 462 | CI->isWeak() ? FailureBB : LoopBB); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 463 | |
Tim Northover | b4ddc08 | 2014-05-30 10:09:59 +0000 | [diff] [blame] | 464 | // Make sure later instructions don't get reordered with a fence if necessary. |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 465 | Builder.SetInsertPoint(SuccessBB); |
Robin Morisset | a47cb41 | 2014-09-03 21:01:03 +0000 | [diff] [blame] | 466 | TLI->emitTrailingFence(Builder, SuccessOrder, /*IsStore=*/true, |
| 467 | /*IsLoad=*/true); |
Tim Northover | 70450c5 | 2014-04-03 13:06:54 +0000 | [diff] [blame] | 468 | Builder.CreateBr(ExitBB); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 469 | |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 470 | Builder.SetInsertPoint(FailureBB); |
Robin Morisset | a47cb41 | 2014-09-03 21:01:03 +0000 | [diff] [blame] | 471 | TLI->emitTrailingFence(Builder, FailureOrder, /*IsStore=*/true, |
| 472 | /*IsLoad=*/true); |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 473 | Builder.CreateBr(ExitBB); |
| 474 | |
Tim Northover | b4ddc08 | 2014-05-30 10:09:59 +0000 | [diff] [blame] | 475 | // Finally, we have control-flow based knowledge of whether the cmpxchg |
| 476 | // succeeded or not. We expose this to later passes by converting any |
| 477 | // subsequent "icmp eq/ne %loaded, %oldval" into a use of an appropriate PHI. |
| 478 | |
| 479 | // Setup the builder so we can create any PHIs we need. |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 480 | Builder.SetInsertPoint(ExitBB, ExitBB->begin()); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 481 | PHINode *Success = Builder.CreatePHI(Type::getInt1Ty(Ctx), 2); |
| 482 | Success->addIncoming(ConstantInt::getTrue(Ctx), SuccessBB); |
Tim Northover | 20b9f73 | 2014-06-13 16:45:52 +0000 | [diff] [blame] | 483 | Success->addIncoming(ConstantInt::getFalse(Ctx), FailureBB); |
Tim Northover | b4ddc08 | 2014-05-30 10:09:59 +0000 | [diff] [blame] | 484 | |
| 485 | // Look for any users of the cmpxchg that are just comparing the loaded value |
| 486 | // against the desired one, and replace them with the CFG-derived version. |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 487 | SmallVector<ExtractValueInst *, 2> PrunedInsts; |
Tim Northover | b4ddc08 | 2014-05-30 10:09:59 +0000 | [diff] [blame] | 488 | for (auto User : CI->users()) { |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 489 | ExtractValueInst *EV = dyn_cast<ExtractValueInst>(User); |
| 490 | if (!EV) |
Tim Northover | b4ddc08 | 2014-05-30 10:09:59 +0000 | [diff] [blame] | 491 | continue; |
| 492 | |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 493 | assert(EV->getNumIndices() == 1 && EV->getIndices()[0] <= 1 && |
| 494 | "weird extraction from { iN, i1 }"); |
Tim Northover | b4ddc08 | 2014-05-30 10:09:59 +0000 | [diff] [blame] | 495 | |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 496 | if (EV->getIndices()[0] == 0) |
| 497 | EV->replaceAllUsesWith(Loaded); |
| 498 | else |
| 499 | EV->replaceAllUsesWith(Success); |
| 500 | |
| 501 | PrunedInsts.push_back(EV); |
Tim Northover | b4ddc08 | 2014-05-30 10:09:59 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 504 | // We can remove the instructions now we're no longer iterating through them. |
| 505 | for (auto EV : PrunedInsts) |
| 506 | EV->eraseFromParent(); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 507 | |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 508 | if (!CI->use_empty()) { |
| 509 | // Some use of the full struct return that we don't understand has happened, |
| 510 | // so we've got to reconstruct it properly. |
| 511 | Value *Res; |
| 512 | Res = Builder.CreateInsertValue(UndefValue::get(CI->getType()), Loaded, 0); |
| 513 | Res = Builder.CreateInsertValue(Res, Success, 1); |
| 514 | |
| 515 | CI->replaceAllUsesWith(Res); |
| 516 | } |
| 517 | |
| 518 | CI->eraseFromParent(); |
Tim Northover | c882eb0 | 2014-04-03 11:44:58 +0000 | [diff] [blame] | 519 | return true; |
| 520 | } |
Robin Morisset | 810739d | 2014-09-25 17:27:43 +0000 | [diff] [blame] | 521 | |
| 522 | bool AtomicExpand::isIdempotentRMW(AtomicRMWInst* RMWI) { |
| 523 | auto C = dyn_cast<ConstantInt>(RMWI->getValOperand()); |
| 524 | if(!C) |
| 525 | return false; |
| 526 | |
| 527 | AtomicRMWInst::BinOp Op = RMWI->getOperation(); |
| 528 | switch(Op) { |
| 529 | case AtomicRMWInst::Add: |
| 530 | case AtomicRMWInst::Sub: |
| 531 | case AtomicRMWInst::Or: |
| 532 | case AtomicRMWInst::Xor: |
| 533 | return C->isZero(); |
| 534 | case AtomicRMWInst::And: |
| 535 | return C->isMinusOne(); |
| 536 | // FIXME: we could also treat Min/Max/UMin/UMax by the INT_MIN/INT_MAX/... |
| 537 | default: |
| 538 | return false; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | bool AtomicExpand::simplifyIdempotentRMW(AtomicRMWInst* RMWI) { |
Robin Morisset | 810739d | 2014-09-25 17:27:43 +0000 | [diff] [blame] | 543 | if (auto ResultingLoad = TLI->lowerIdempotentRMWIntoFencedLoad(RMWI)) { |
| 544 | if (TLI->shouldExpandAtomicLoadInIR(ResultingLoad)) |
| 545 | expandAtomicLoad(ResultingLoad); |
| 546 | return true; |
| 547 | } |
Robin Morisset | 810739d | 2014-09-25 17:27:43 +0000 | [diff] [blame] | 548 | return false; |
| 549 | } |