77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===-- AtomicExpandPass.cpp - Expand atomic instructions -------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // This file contains a pass (at IR level) to replace atomic instructions with
|
83
|
11 // either (intrinsic-based) load-linked/store-conditional loops or AtomicCmpXchg.
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 #include "llvm/CodeGen/Passes.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include "llvm/IR/Function.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #include "llvm/IR/IRBuilder.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 #include "llvm/IR/InstIterator.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 #include "llvm/IR/Instructions.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 #include "llvm/IR/Intrinsics.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 #include "llvm/IR/Module.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 #include "llvm/Support/Debug.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 #include "llvm/Target/TargetLowering.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 #include "llvm/Target/TargetMachine.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 #include "llvm/Target/TargetSubtargetInfo.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 #define DEBUG_TYPE "atomic-expand"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 namespace {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 class AtomicExpand: public FunctionPass {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 const TargetMachine *TM;
|
83
|
34 const TargetLowering *TLI;
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 public:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 static char ID; // Pass identification, replacement for typeid
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 explicit AtomicExpand(const TargetMachine *TM = nullptr)
|
83
|
38 : FunctionPass(ID), TM(TM), TLI(nullptr) {
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 initializeAtomicExpandPass(*PassRegistry::getPassRegistry());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 bool runOnFunction(Function &F) override;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 private:
|
83
|
45 bool bracketInstWithFences(Instruction *I, AtomicOrdering Order,
|
|
46 bool IsStore, bool IsLoad);
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 bool expandAtomicLoad(LoadInst *LI);
|
83
|
48 bool expandAtomicLoadToLL(LoadInst *LI);
|
|
49 bool expandAtomicLoadToCmpXchg(LoadInst *LI);
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 bool expandAtomicStore(StoreInst *SI);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 bool expandAtomicRMW(AtomicRMWInst *AI);
|
83
|
52 bool expandAtomicRMWToLLSC(AtomicRMWInst *AI);
|
|
53 bool expandAtomicRMWToCmpXchg(AtomicRMWInst *AI);
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 bool expandAtomicCmpXchg(AtomicCmpXchgInst *CI);
|
83
|
55 bool isIdempotentRMW(AtomicRMWInst *AI);
|
|
56 bool simplifyIdempotentRMW(AtomicRMWInst *AI);
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 };
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 char AtomicExpand::ID = 0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 char &llvm::AtomicExpandID = AtomicExpand::ID;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 INITIALIZE_TM_PASS(AtomicExpand, "atomic-expand",
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 "Expand Atomic calls in terms of either load-linked & store-conditional or cmpxchg",
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 false, false)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 FunctionPass *llvm::createAtomicExpandPass(const TargetMachine *TM) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 return new AtomicExpand(TM);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 bool AtomicExpand::runOnFunction(Function &F) {
|
83
|
71 if (!TM || !TM->getSubtargetImpl(F)->enableAtomicExpand())
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 return false;
|
83
|
73 TLI = TM->getSubtargetImpl(F)->getTargetLowering();
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 SmallVector<Instruction *, 1> AtomicInsts;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 // Changing control-flow while iterating through it is a bad idea, so gather a
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 // list of all atomic instructions before we start.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 if (I->isAtomic())
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81 AtomicInsts.push_back(&*I);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 bool MadeChange = false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 for (auto I : AtomicInsts) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 auto LI = dyn_cast<LoadInst>(I);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 auto SI = dyn_cast<StoreInst>(I);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 auto RMWI = dyn_cast<AtomicRMWInst>(I);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
89 auto CASI = dyn_cast<AtomicCmpXchgInst>(I);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 assert((LI || SI || RMWI || CASI || isa<FenceInst>(I)) &&
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91 "Unknown atomic instruction");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92
|
83
|
93 auto FenceOrdering = Monotonic;
|
|
94 bool IsStore, IsLoad;
|
|
95 if (TLI->getInsertFencesForAtomic()) {
|
|
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;
|
|
111 } else if (CASI && !TLI->hasLoadLinkedStoreConditional() &&
|
|
112 (isAtLeastRelease(CASI->getSuccessOrdering()) ||
|
|
113 isAtLeastAcquire(CASI->getSuccessOrdering()))) {
|
|
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
|
|
129 if (LI && TLI->shouldExpandAtomicLoadInIR(LI)) {
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
130 MadeChange |= expandAtomicLoad(LI);
|
83
|
131 } else if (SI && TLI->shouldExpandAtomicStoreInIR(SI)) {
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
132 MadeChange |= expandAtomicStore(SI);
|
83
|
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.
|
|
138 MadeChange |=
|
|
139 (isIdempotentRMW(RMWI) && simplifyIdempotentRMW(RMWI)) ||
|
|
140 (TLI->shouldExpandAtomicRMWInIR(RMWI) && expandAtomicRMW(RMWI));
|
|
141 } else if (CASI && TLI->hasLoadLinkedStoreConditional()) {
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
142 MadeChange |= expandAtomicCmpXchg(CASI);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
143 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
144 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
145 return MadeChange;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
146 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
147
|
83
|
148 bool AtomicExpand::bracketInstWithFences(Instruction *I, AtomicOrdering Order,
|
|
149 bool IsStore, bool IsLoad) {
|
|
150 IRBuilder<> Builder(I);
|
|
151
|
|
152 auto LeadingFence = TLI->emitLeadingFence(Builder, Order, IsStore, IsLoad);
|
|
153
|
|
154 auto TrailingFence = TLI->emitTrailingFence(Builder, Order, IsStore, IsLoad);
|
|
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
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 bool AtomicExpand::expandAtomicLoad(LoadInst *LI) {
|
83
|
170 if (TLI->hasLoadLinkedStoreConditional())
|
|
171 return expandAtomicLoadToLL(LI);
|
|
172 else
|
|
173 return expandAtomicLoadToCmpXchg(LI);
|
|
174 }
|
|
175
|
|
176 bool AtomicExpand::expandAtomicLoadToLL(LoadInst *LI) {
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
177 IRBuilder<> Builder(LI);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
178
|
83
|
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).
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182 Value *Val =
|
83
|
183 TLI->emitLoadLinked(Builder, LI->getPointerOperand(), LI->getOrdering());
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
184
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
185 LI->replaceAllUsesWith(Val);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186 LI->eraseFromParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
187
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
189 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
190
|
83
|
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
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
209 bool AtomicExpand::expandAtomicStore(StoreInst *SI) {
|
83
|
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.
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
216 IRBuilder<> Builder(SI);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
217 AtomicRMWInst *AI =
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
218 Builder.CreateAtomicRMW(AtomicRMWInst::Xchg, SI->getPointerOperand(),
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
219 SI->getValueOperand(), SI->getOrdering());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
220 SI->eraseFromParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
221
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
222 // Now we have an appropriate swap instruction, lower it as usual.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
223 return expandAtomicRMW(AI);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
224 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
225
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
226 bool AtomicExpand::expandAtomicRMW(AtomicRMWInst *AI) {
|
83
|
227 if (TLI->hasLoadLinkedStoreConditional())
|
|
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) {
|
|
271 AtomicOrdering MemOpOrder = AI->getOrdering();
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
272 Value *Addr = AI->getPointerOperand();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
273 BasicBlock *BB = AI->getParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
274 Function *F = BB->getParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
275 LLVMContext &Ctx = F->getContext();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
276
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
277 // Given: atomicrmw some_op iN* %addr, iN %incr ordering
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
278 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
279 // The standard expansion we produce is:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
280 // [...]
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
281 // fence?
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
282 // atomicrmw.start:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
283 // %loaded = @load.linked(%addr)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
284 // %new = some_op iN %loaded, %incr
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
285 // %stored = @store_conditional(%new, %addr)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
286 // %try_again = icmp i32 ne %stored, 0
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
287 // br i1 %try_again, label %loop, label %atomicrmw.end
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
288 // atomicrmw.end:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
289 // fence?
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
290 // [...]
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
291 BasicBlock *ExitBB = BB->splitBasicBlock(AI, "atomicrmw.end");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
292 BasicBlock *LoopBB = BasicBlock::Create(Ctx, "atomicrmw.start", F, ExitBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
293
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
294 // This grabs the DebugLoc from AI.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
295 IRBuilder<> Builder(AI);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
296
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
297 // The split call above "helpfully" added a branch at the end of BB (to the
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
298 // wrong place), but we might want a fence too. It's easiest to just remove
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
299 // the branch entirely.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
300 std::prev(BB->end())->eraseFromParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
301 Builder.SetInsertPoint(BB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
302 Builder.CreateBr(LoopBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
303
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
304 // Start the main loop block now that we've taken care of the preliminaries.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
305 Builder.SetInsertPoint(LoopBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
306 Value *Loaded = TLI->emitLoadLinked(Builder, Addr, MemOpOrder);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
307
|
83
|
308 Value *NewVal =
|
|
309 performAtomicOp(AI->getOperation(), Builder, Loaded, AI->getValOperand());
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
310
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
311 Value *StoreSuccess =
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
312 TLI->emitStoreConditional(Builder, NewVal, Addr, MemOpOrder);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
313 Value *TryAgain = Builder.CreateICmpNE(
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
314 StoreSuccess, ConstantInt::get(IntegerType::get(Ctx, 32), 0), "tryagain");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
315 Builder.CreateCondBr(TryAgain, LoopBB, ExitBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
316
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
317 Builder.SetInsertPoint(ExitBB, ExitBB->begin());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
318
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
319 AI->replaceAllUsesWith(Loaded);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
320 AI->eraseFromParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
321
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
322 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
323 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
324
|
83
|
325 bool AtomicExpand::expandAtomicRMWToCmpXchg(AtomicRMWInst *AI) {
|
|
326 AtomicOrdering MemOpOrder =
|
|
327 AI->getOrdering() == Unordered ? Monotonic : AI->getOrdering();
|
|
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);
|
|
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());
|
|
382
|
|
383 AI->replaceAllUsesWith(NewLoaded);
|
|
384 AI->eraseFromParent();
|
|
385
|
|
386 return true;
|
|
387 }
|
|
388
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
389 bool AtomicExpand::expandAtomicCmpXchg(AtomicCmpXchgInst *CI) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
390 AtomicOrdering SuccessOrder = CI->getSuccessOrdering();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
391 AtomicOrdering FailureOrder = CI->getFailureOrdering();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
392 Value *Addr = CI->getPointerOperand();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
393 BasicBlock *BB = CI->getParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
394 Function *F = BB->getParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
395 LLVMContext &Ctx = F->getContext();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
396 // If getInsertFencesForAtomic() returns true, then the target does not want
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
397 // to deal with memory orders, and emitLeading/TrailingFence should take care
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
398 // of everything. Otherwise, emitLeading/TrailingFence are no-op and we
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
399 // should preserve the ordering.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
400 AtomicOrdering MemOpOrder =
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
401 TLI->getInsertFencesForAtomic() ? Monotonic : SuccessOrder;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
402
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
403 // Given: cmpxchg some_op iN* %addr, iN %desired, iN %new success_ord fail_ord
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
404 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
405 // The full expansion we produce is:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
406 // [...]
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
407 // fence?
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
408 // cmpxchg.start:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
409 // %loaded = @load.linked(%addr)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
410 // %should_store = icmp eq %loaded, %desired
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
411 // br i1 %should_store, label %cmpxchg.trystore,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
412 // label %cmpxchg.failure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
413 // cmpxchg.trystore:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
414 // %stored = @store_conditional(%new, %addr)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
415 // %success = icmp eq i32 %stored, 0
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
416 // br i1 %success, label %cmpxchg.success, label %loop/%cmpxchg.failure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
417 // cmpxchg.success:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
418 // fence?
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
419 // br label %cmpxchg.end
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
420 // cmpxchg.failure:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
421 // fence?
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
422 // br label %cmpxchg.end
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
423 // cmpxchg.end:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
424 // %success = phi i1 [true, %cmpxchg.success], [false, %cmpxchg.failure]
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
425 // %restmp = insertvalue { iN, i1 } undef, iN %loaded, 0
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
426 // %res = insertvalue { iN, i1 } %restmp, i1 %success, 1
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
427 // [...]
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
428 BasicBlock *ExitBB = BB->splitBasicBlock(CI, "cmpxchg.end");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
429 auto FailureBB = BasicBlock::Create(Ctx, "cmpxchg.failure", F, ExitBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
430 auto SuccessBB = BasicBlock::Create(Ctx, "cmpxchg.success", F, FailureBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
431 auto TryStoreBB = BasicBlock::Create(Ctx, "cmpxchg.trystore", F, SuccessBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
432 auto LoopBB = BasicBlock::Create(Ctx, "cmpxchg.start", F, TryStoreBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
433
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
434 // This grabs the DebugLoc from CI
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
435 IRBuilder<> Builder(CI);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
436
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
437 // The split call above "helpfully" added a branch at the end of BB (to the
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
438 // wrong place), but we might want a fence too. It's easiest to just remove
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
439 // the branch entirely.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
440 std::prev(BB->end())->eraseFromParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
441 Builder.SetInsertPoint(BB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
442 TLI->emitLeadingFence(Builder, SuccessOrder, /*IsStore=*/true,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
443 /*IsLoad=*/true);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
444 Builder.CreateBr(LoopBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
445
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
446 // Start the main loop block now that we've taken care of the preliminaries.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
447 Builder.SetInsertPoint(LoopBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
448 Value *Loaded = TLI->emitLoadLinked(Builder, Addr, MemOpOrder);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
449 Value *ShouldStore =
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
450 Builder.CreateICmpEQ(Loaded, CI->getCompareOperand(), "should_store");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
451
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
452 // If the the cmpxchg doesn't actually need any ordering when it fails, we can
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
453 // jump straight past that fence instruction (if it exists).
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
454 Builder.CreateCondBr(ShouldStore, TryStoreBB, FailureBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
455
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
456 Builder.SetInsertPoint(TryStoreBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
457 Value *StoreSuccess = TLI->emitStoreConditional(
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
458 Builder, CI->getNewValOperand(), Addr, MemOpOrder);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
459 StoreSuccess = Builder.CreateICmpEQ(
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
460 StoreSuccess, ConstantInt::get(Type::getInt32Ty(Ctx), 0), "success");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
461 Builder.CreateCondBr(StoreSuccess, SuccessBB,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
462 CI->isWeak() ? FailureBB : LoopBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
463
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
464 // Make sure later instructions don't get reordered with a fence if necessary.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
465 Builder.SetInsertPoint(SuccessBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
466 TLI->emitTrailingFence(Builder, SuccessOrder, /*IsStore=*/true,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
467 /*IsLoad=*/true);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
468 Builder.CreateBr(ExitBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
469
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
470 Builder.SetInsertPoint(FailureBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
471 TLI->emitTrailingFence(Builder, FailureOrder, /*IsStore=*/true,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
472 /*IsLoad=*/true);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
473 Builder.CreateBr(ExitBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
474
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
475 // Finally, we have control-flow based knowledge of whether the cmpxchg
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
476 // succeeded or not. We expose this to later passes by converting any
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
477 // subsequent "icmp eq/ne %loaded, %oldval" into a use of an appropriate PHI.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
478
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
479 // Setup the builder so we can create any PHIs we need.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
480 Builder.SetInsertPoint(ExitBB, ExitBB->begin());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
481 PHINode *Success = Builder.CreatePHI(Type::getInt1Ty(Ctx), 2);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
482 Success->addIncoming(ConstantInt::getTrue(Ctx), SuccessBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
483 Success->addIncoming(ConstantInt::getFalse(Ctx), FailureBB);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
484
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
485 // Look for any users of the cmpxchg that are just comparing the loaded value
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
486 // against the desired one, and replace them with the CFG-derived version.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
487 SmallVector<ExtractValueInst *, 2> PrunedInsts;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
488 for (auto User : CI->users()) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
489 ExtractValueInst *EV = dyn_cast<ExtractValueInst>(User);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
490 if (!EV)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
491 continue;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
492
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
493 assert(EV->getNumIndices() == 1 && EV->getIndices()[0] <= 1 &&
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
494 "weird extraction from { iN, i1 }");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
495
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
496 if (EV->getIndices()[0] == 0)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
497 EV->replaceAllUsesWith(Loaded);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
498 else
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
499 EV->replaceAllUsesWith(Success);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
500
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
501 PrunedInsts.push_back(EV);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
502 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
503
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
504 // We can remove the instructions now we're no longer iterating through them.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
505 for (auto EV : PrunedInsts)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
506 EV->eraseFromParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
507
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
508 if (!CI->use_empty()) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
509 // Some use of the full struct return that we don't understand has happened,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
510 // so we've got to reconstruct it properly.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
511 Value *Res;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
512 Res = Builder.CreateInsertValue(UndefValue::get(CI->getType()), Loaded, 0);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
513 Res = Builder.CreateInsertValue(Res, Success, 1);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
514
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
515 CI->replaceAllUsesWith(Res);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
516 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
517
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
518 CI->eraseFromParent();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
519 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
520 }
|
83
|
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) {
|
|
543 if (auto ResultingLoad = TLI->lowerIdempotentRMWIntoFencedLoad(RMWI)) {
|
|
544 if (TLI->shouldExpandAtomicLoadInIR(ResultingLoad))
|
|
545 expandAtomicLoad(ResultingLoad);
|
|
546 return true;
|
|
547 }
|
|
548 return false;
|
|
549 }
|