blob: 6eb1ca1e53d56a1e79f2a680abc6452788bcf3ca [file] [log] [blame]
Robin Morisset59c23cd2014-08-21 21:50:01 +00001//===-- AtomicExpandPass.cpp - Expand atomic instructions -------===//
Tim Northoverc882eb02014-04-03 11:44:58 +00002//
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
11// appropriate (intrinsic-based) ldrex/strex loops.
12//
13//===----------------------------------------------------------------------===//
14
Tim Northoverc882eb02014-04-03 11:44:58 +000015#include "llvm/CodeGen/Passes.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/IRBuilder.h"
Robin Morisseted3d48f2014-09-03 21:29:59 +000018#include "llvm/IR/InstIterator.h"
Tim Northoverc882eb02014-04-03 11:44:58 +000019#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 Christopherc40e5ed2014-06-19 21:03:04 +000025#include "llvm/Target/TargetSubtargetInfo.h"
26
Tim Northoverc882eb02014-04-03 11:44:58 +000027using namespace llvm;
28
Robin Morisset59c23cd2014-08-21 21:50:01 +000029#define DEBUG_TYPE "atomic-expand"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030
Tim Northoverc882eb02014-04-03 11:44:58 +000031namespace {
Robin Morisset59c23cd2014-08-21 21:50:01 +000032 class AtomicExpand: public FunctionPass {
Eric Christopherc40e5ed2014-06-19 21:03:04 +000033 const TargetMachine *TM;
Tim Northoverc882eb02014-04-03 11:44:58 +000034 public:
35 static char ID; // Pass identification, replacement for typeid
Robin Morisset59c23cd2014-08-21 21:50:01 +000036 explicit AtomicExpand(const TargetMachine *TM = nullptr)
Eric Christopherc40e5ed2014-06-19 21:03:04 +000037 : FunctionPass(ID), TM(TM) {
Robin Morisset59c23cd2014-08-21 21:50:01 +000038 initializeAtomicExpandPass(*PassRegistry::getPassRegistry());
Tim Northover037f26f22014-04-17 18:22:47 +000039 }
Tim Northoverc882eb02014-04-03 11:44:58 +000040
41 bool runOnFunction(Function &F) override;
Tim Northoverc882eb02014-04-03 11:44:58 +000042
Robin Morisseted3d48f2014-09-03 21:29:59 +000043 private:
Tim Northoverc882eb02014-04-03 11:44:58 +000044 bool expandAtomicLoad(LoadInst *LI);
Robin Morisseted3d48f2014-09-03 21:29:59 +000045 bool expandAtomicStore(StoreInst *SI);
Tim Northoverc882eb02014-04-03 11:44:58 +000046 bool expandAtomicRMW(AtomicRMWInst *AI);
47 bool expandAtomicCmpXchg(AtomicCmpXchgInst *CI);
Tim Northoverc882eb02014-04-03 11:44:58 +000048 };
49}
50
Robin Morisset59c23cd2014-08-21 21:50:01 +000051char AtomicExpand::ID = 0;
52char &llvm::AtomicExpandID = AtomicExpand::ID;
53INITIALIZE_TM_PASS(AtomicExpand, "atomic-expand",
54 "Expand Atomic calls in terms of either load-linked & store-conditional or cmpxchg",
Jiangning Liud623c522014-06-11 07:04:37 +000055 false, false)
Tim Northover037f26f22014-04-17 18:22:47 +000056
Robin Morisset59c23cd2014-08-21 21:50:01 +000057FunctionPass *llvm::createAtomicExpandPass(const TargetMachine *TM) {
58 return new AtomicExpand(TM);
Tim Northover037f26f22014-04-17 18:22:47 +000059}
60
Robin Morisset59c23cd2014-08-21 21:50:01 +000061bool AtomicExpand::runOnFunction(Function &F) {
62 if (!TM || !TM->getSubtargetImpl()->enableAtomicExpand())
Tim Northover037f26f22014-04-17 18:22:47 +000063 return false;
Robin Morisseted3d48f2014-09-03 21:29:59 +000064 auto TargetLowering = TM->getSubtargetImpl()->getTargetLowering();
Tim Northover037f26f22014-04-17 18:22:47 +000065
Tim Northoverc882eb02014-04-03 11:44:58 +000066 SmallVector<Instruction *, 1> AtomicInsts;
67
68 // Changing control-flow while iterating through it is a bad idea, so gather a
69 // list of all atomic instructions before we start.
Robin Morisseted3d48f2014-09-03 21:29:59 +000070 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
71 if (I->isAtomic())
72 AtomicInsts.push_back(&*I);
Tim Northoverc882eb02014-04-03 11:44:58 +000073 }
74
Robin Morisseted3d48f2014-09-03 21:29:59 +000075 bool MadeChange = false;
76 for (auto I : AtomicInsts) {
77 auto LI = dyn_cast<LoadInst>(I);
78 auto SI = dyn_cast<StoreInst>(I);
79 auto RMWI = dyn_cast<AtomicRMWInst>(I);
80 auto CASI = dyn_cast<AtomicCmpXchgInst>(I);
81
82 assert((LI || SI || RMWI || CASI || isa<FenceInst>(I)) &&
83 "Unknown atomic instruction");
84
85 if (LI && TargetLowering->shouldExpandAtomicLoadInIR(LI)) {
86 MadeChange |= expandAtomicLoad(LI);
87 } else if (SI && TargetLowering->shouldExpandAtomicStoreInIR(SI)) {
88 MadeChange |= expandAtomicStore(SI);
89 } else if (RMWI && TargetLowering->shouldExpandAtomicRMWInIR(RMWI)) {
90 MadeChange |= expandAtomicRMW(RMWI);
91 } else if (CASI) {
92 MadeChange |= expandAtomicCmpXchg(CASI);
93 }
94 }
Tim Northoverc882eb02014-04-03 11:44:58 +000095 return MadeChange;
96}
97
Robin Morisset59c23cd2014-08-21 21:50:01 +000098bool AtomicExpand::expandAtomicLoad(LoadInst *LI) {
Robin Morisseta47cb412014-09-03 21:01:03 +000099 auto TLI = TM->getSubtargetImpl()->getTargetLowering();
100 // If getInsertFencesForAtomic() returns true, then the target does not want
101 // to deal with memory orders, and emitLeading/TrailingFence should take care
102 // of everything. Otherwise, emitLeading/TrailingFence are no-op and we
103 // should preserve the ordering.
Tim Northoverc882eb02014-04-03 11:44:58 +0000104 AtomicOrdering MemOpOrder =
Robin Morisseta47cb412014-09-03 21:01:03 +0000105 TLI->getInsertFencesForAtomic() ? Monotonic : LI->getOrdering();
Tim Northoverc882eb02014-04-03 11:44:58 +0000106 IRBuilder<> Builder(LI);
Tim Northoverc882eb02014-04-03 11:44:58 +0000107
Robin Morisseta47cb412014-09-03 21:01:03 +0000108 // Note that although no fence is required before atomic load on ARM, it is
109 // required before SequentiallyConsistent loads for the recommended Power
110 // mapping (see http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html).
111 // So we let the target choose what to emit.
112 TLI->emitLeadingFence(Builder, LI->getOrdering(),
113 /*IsStore=*/false, /*IsLoad=*/true);
114
115 // The only 64-bit load guaranteed to be single-copy atomic by ARM is
116 // an ldrexd (A3.5.3).
117 Value *Val =
118 TLI->emitLoadLinked(Builder, LI->getPointerOperand(), MemOpOrder);
119
120 TLI->emitTrailingFence(Builder, LI->getOrdering(),
121 /*IsStore=*/false, /*IsLoad=*/true);
Tim Northoverc882eb02014-04-03 11:44:58 +0000122
123 LI->replaceAllUsesWith(Val);
124 LI->eraseFromParent();
125
126 return true;
127}
128
Robin Morisset59c23cd2014-08-21 21:50:01 +0000129bool AtomicExpand::expandAtomicStore(StoreInst *SI) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000130 // The only atomic 64-bit store on ARM is an strexd that succeeds, which means
131 // we need a loop and the entire instruction is essentially an "atomicrmw
132 // xchg" that ignores the value loaded.
133 IRBuilder<> Builder(SI);
134 AtomicRMWInst *AI =
135 Builder.CreateAtomicRMW(AtomicRMWInst::Xchg, SI->getPointerOperand(),
136 SI->getValueOperand(), SI->getOrdering());
137 SI->eraseFromParent();
138
139 // Now we have an appropriate swap instruction, lower it as usual.
140 return expandAtomicRMW(AI);
141}
142
Robin Morisset59c23cd2014-08-21 21:50:01 +0000143bool AtomicExpand::expandAtomicRMW(AtomicRMWInst *AI) {
Robin Morisseta47cb412014-09-03 21:01:03 +0000144 auto TLI = TM->getSubtargetImpl()->getTargetLowering();
Tim Northoverc882eb02014-04-03 11:44:58 +0000145 AtomicOrdering Order = AI->getOrdering();
146 Value *Addr = AI->getPointerOperand();
147 BasicBlock *BB = AI->getParent();
148 Function *F = BB->getParent();
149 LLVMContext &Ctx = F->getContext();
Robin Morisseted3d48f2014-09-03 21:29:59 +0000150 // If getInsertFencesForAtomic() returns true, then the target does not want
151 // to deal with memory orders, and emitLeading/TrailingFence should take care
152 // of everything. Otherwise, emitLeading/TrailingFence are no-op and we
153 // should preserve the ordering.
Robin Morisseta47cb412014-09-03 21:01:03 +0000154 AtomicOrdering MemOpOrder =
155 TLI->getInsertFencesForAtomic() ? Monotonic : Order;
Tim Northoverc882eb02014-04-03 11:44:58 +0000156
157 // Given: atomicrmw some_op iN* %addr, iN %incr ordering
158 //
159 // The standard expansion we produce is:
160 // [...]
161 // fence?
162 // atomicrmw.start:
163 // %loaded = @load.linked(%addr)
164 // %new = some_op iN %loaded, %incr
165 // %stored = @store_conditional(%new, %addr)
166 // %try_again = icmp i32 ne %stored, 0
167 // br i1 %try_again, label %loop, label %atomicrmw.end
168 // atomicrmw.end:
169 // fence?
170 // [...]
171 BasicBlock *ExitBB = BB->splitBasicBlock(AI, "atomicrmw.end");
172 BasicBlock *LoopBB = BasicBlock::Create(Ctx, "atomicrmw.start", F, ExitBB);
173
174 // This grabs the DebugLoc from AI.
175 IRBuilder<> Builder(AI);
176
177 // The split call above "helpfully" added a branch at the end of BB (to the
178 // wrong place), but we might want a fence too. It's easiest to just remove
179 // the branch entirely.
180 std::prev(BB->end())->eraseFromParent();
181 Builder.SetInsertPoint(BB);
Robin Morisseta47cb412014-09-03 21:01:03 +0000182 TLI->emitLeadingFence(Builder, Order, /*IsStore=*/true, /*IsLoad=*/true);
Tim Northoverc882eb02014-04-03 11:44:58 +0000183 Builder.CreateBr(LoopBB);
184
185 // Start the main loop block now that we've taken care of the preliminaries.
186 Builder.SetInsertPoint(LoopBB);
Robin Morisseta47cb412014-09-03 21:01:03 +0000187 Value *Loaded = TLI->emitLoadLinked(Builder, Addr, MemOpOrder);
Tim Northoverc882eb02014-04-03 11:44:58 +0000188
189 Value *NewVal;
190 switch (AI->getOperation()) {
191 case AtomicRMWInst::Xchg:
192 NewVal = AI->getValOperand();
193 break;
194 case AtomicRMWInst::Add:
195 NewVal = Builder.CreateAdd(Loaded, AI->getValOperand(), "new");
196 break;
197 case AtomicRMWInst::Sub:
198 NewVal = Builder.CreateSub(Loaded, AI->getValOperand(), "new");
199 break;
200 case AtomicRMWInst::And:
201 NewVal = Builder.CreateAnd(Loaded, AI->getValOperand(), "new");
202 break;
203 case AtomicRMWInst::Nand:
Tim Northover55beb642014-07-07 09:06:35 +0000204 NewVal = Builder.CreateNot(Builder.CreateAnd(Loaded, AI->getValOperand()),
Tim Northoverc882eb02014-04-03 11:44:58 +0000205 "new");
206 break;
207 case AtomicRMWInst::Or:
208 NewVal = Builder.CreateOr(Loaded, AI->getValOperand(), "new");
209 break;
210 case AtomicRMWInst::Xor:
211 NewVal = Builder.CreateXor(Loaded, AI->getValOperand(), "new");
212 break;
213 case AtomicRMWInst::Max:
214 NewVal = Builder.CreateICmpSGT(Loaded, AI->getValOperand());
215 NewVal = Builder.CreateSelect(NewVal, Loaded, AI->getValOperand(), "new");
216 break;
217 case AtomicRMWInst::Min:
218 NewVal = Builder.CreateICmpSLE(Loaded, AI->getValOperand());
219 NewVal = Builder.CreateSelect(NewVal, Loaded, AI->getValOperand(), "new");
220 break;
221 case AtomicRMWInst::UMax:
222 NewVal = Builder.CreateICmpUGT(Loaded, AI->getValOperand());
223 NewVal = Builder.CreateSelect(NewVal, Loaded, AI->getValOperand(), "new");
224 break;
225 case AtomicRMWInst::UMin:
226 NewVal = Builder.CreateICmpULE(Loaded, AI->getValOperand());
227 NewVal = Builder.CreateSelect(NewVal, Loaded, AI->getValOperand(), "new");
228 break;
229 default:
230 llvm_unreachable("Unknown atomic op");
231 }
232
Eric Christopherd9134482014-08-04 21:25:23 +0000233 Value *StoreSuccess =
Robin Morisseta47cb412014-09-03 21:01:03 +0000234 TLI->emitStoreConditional(Builder, NewVal, Addr, MemOpOrder);
Tim Northoverc882eb02014-04-03 11:44:58 +0000235 Value *TryAgain = Builder.CreateICmpNE(
236 StoreSuccess, ConstantInt::get(IntegerType::get(Ctx, 32), 0), "tryagain");
237 Builder.CreateCondBr(TryAgain, LoopBB, ExitBB);
238
239 Builder.SetInsertPoint(ExitBB, ExitBB->begin());
Robin Morisseta47cb412014-09-03 21:01:03 +0000240 TLI->emitTrailingFence(Builder, Order, /*IsStore=*/true, /*IsLoad=*/true);
Tim Northoverc882eb02014-04-03 11:44:58 +0000241
242 AI->replaceAllUsesWith(Loaded);
243 AI->eraseFromParent();
244
245 return true;
246}
247
Robin Morisset59c23cd2014-08-21 21:50:01 +0000248bool AtomicExpand::expandAtomicCmpXchg(AtomicCmpXchgInst *CI) {
Robin Morisseta47cb412014-09-03 21:01:03 +0000249 auto TLI = TM->getSubtargetImpl()->getTargetLowering();
Tim Northover70450c52014-04-03 13:06:54 +0000250 AtomicOrdering SuccessOrder = CI->getSuccessOrdering();
251 AtomicOrdering FailureOrder = CI->getFailureOrdering();
Tim Northoverc882eb02014-04-03 11:44:58 +0000252 Value *Addr = CI->getPointerOperand();
253 BasicBlock *BB = CI->getParent();
254 Function *F = BB->getParent();
255 LLVMContext &Ctx = F->getContext();
Robin Morisseted3d48f2014-09-03 21:29:59 +0000256 // If getInsertFencesForAtomic() returns true, then the target does not want
257 // to deal with memory orders, and emitLeading/TrailingFence should take care
258 // of everything. Otherwise, emitLeading/TrailingFence are no-op and we
259 // should preserve the ordering.
Robin Morisseta47cb412014-09-03 21:01:03 +0000260 AtomicOrdering MemOpOrder =
261 TLI->getInsertFencesForAtomic() ? Monotonic : SuccessOrder;
Tim Northoverc882eb02014-04-03 11:44:58 +0000262
263 // Given: cmpxchg some_op iN* %addr, iN %desired, iN %new success_ord fail_ord
264 //
Tim Northover70450c52014-04-03 13:06:54 +0000265 // The full expansion we produce is:
Tim Northoverc882eb02014-04-03 11:44:58 +0000266 // [...]
267 // fence?
268 // cmpxchg.start:
269 // %loaded = @load.linked(%addr)
270 // %should_store = icmp eq %loaded, %desired
Tim Northover70450c52014-04-03 13:06:54 +0000271 // br i1 %should_store, label %cmpxchg.trystore,
Tim Northover20b9f732014-06-13 16:45:52 +0000272 // label %cmpxchg.failure
Tim Northoverc882eb02014-04-03 11:44:58 +0000273 // cmpxchg.trystore:
274 // %stored = @store_conditional(%new, %addr)
Tim Northover20b9f732014-06-13 16:45:52 +0000275 // %success = icmp eq i32 %stored, 0
276 // br i1 %success, label %cmpxchg.success, label %loop/%cmpxchg.failure
277 // cmpxchg.success:
278 // fence?
279 // br label %cmpxchg.end
280 // cmpxchg.failure:
Tim Northoverc882eb02014-04-03 11:44:58 +0000281 // fence?
Tim Northover70450c52014-04-03 13:06:54 +0000282 // br label %cmpxchg.end
283 // cmpxchg.end:
Tim Northover20b9f732014-06-13 16:45:52 +0000284 // %success = phi i1 [true, %cmpxchg.success], [false, %cmpxchg.failure]
285 // %restmp = insertvalue { iN, i1 } undef, iN %loaded, 0
286 // %res = insertvalue { iN, i1 } %restmp, i1 %success, 1
Tim Northoverc882eb02014-04-03 11:44:58 +0000287 // [...]
288 BasicBlock *ExitBB = BB->splitBasicBlock(CI, "cmpxchg.end");
Tim Northover20b9f732014-06-13 16:45:52 +0000289 auto FailureBB = BasicBlock::Create(Ctx, "cmpxchg.failure", F, ExitBB);
290 auto SuccessBB = BasicBlock::Create(Ctx, "cmpxchg.success", F, FailureBB);
291 auto TryStoreBB = BasicBlock::Create(Ctx, "cmpxchg.trystore", F, SuccessBB);
Tim Northover70450c52014-04-03 13:06:54 +0000292 auto LoopBB = BasicBlock::Create(Ctx, "cmpxchg.start", F, TryStoreBB);
Tim Northoverc882eb02014-04-03 11:44:58 +0000293
294 // This grabs the DebugLoc from CI
295 IRBuilder<> Builder(CI);
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);
Robin Morisseta47cb412014-09-03 21:01:03 +0000302 TLI->emitLeadingFence(Builder, SuccessOrder, /*IsStore=*/true,
303 /*IsLoad=*/true);
Tim Northoverc882eb02014-04-03 11:44:58 +0000304 Builder.CreateBr(LoopBB);
305
306 // Start the main loop block now that we've taken care of the preliminaries.
307 Builder.SetInsertPoint(LoopBB);
Robin Morisseta47cb412014-09-03 21:01:03 +0000308 Value *Loaded = TLI->emitLoadLinked(Builder, Addr, MemOpOrder);
Tim Northoverc882eb02014-04-03 11:44:58 +0000309 Value *ShouldStore =
310 Builder.CreateICmpEQ(Loaded, CI->getCompareOperand(), "should_store");
Tim Northover70450c52014-04-03 13:06:54 +0000311
312 // If the the cmpxchg doesn't actually need any ordering when it fails, we can
313 // jump straight past that fence instruction (if it exists).
Tim Northover70450c52014-04-03 13:06:54 +0000314 Builder.CreateCondBr(ShouldStore, TryStoreBB, FailureBB);
Tim Northoverc882eb02014-04-03 11:44:58 +0000315
316 Builder.SetInsertPoint(TryStoreBB);
Robin Morisseta47cb412014-09-03 21:01:03 +0000317 Value *StoreSuccess = TLI->emitStoreConditional(
318 Builder, CI->getNewValOperand(), Addr, MemOpOrder);
Tim Northoverd039abd2014-06-13 16:45:36 +0000319 StoreSuccess = Builder.CreateICmpEQ(
Tim Northoverc882eb02014-04-03 11:44:58 +0000320 StoreSuccess, ConstantInt::get(Type::getInt32Ty(Ctx), 0), "success");
Tim Northover20b9f732014-06-13 16:45:52 +0000321 Builder.CreateCondBr(StoreSuccess, SuccessBB,
322 CI->isWeak() ? FailureBB : LoopBB);
Tim Northoverc882eb02014-04-03 11:44:58 +0000323
Tim Northoverb4ddc082014-05-30 10:09:59 +0000324 // Make sure later instructions don't get reordered with a fence if necessary.
Tim Northover20b9f732014-06-13 16:45:52 +0000325 Builder.SetInsertPoint(SuccessBB);
Robin Morisseta47cb412014-09-03 21:01:03 +0000326 TLI->emitTrailingFence(Builder, SuccessOrder, /*IsStore=*/true,
327 /*IsLoad=*/true);
Tim Northover70450c52014-04-03 13:06:54 +0000328 Builder.CreateBr(ExitBB);
Tim Northoverc882eb02014-04-03 11:44:58 +0000329
Tim Northover20b9f732014-06-13 16:45:52 +0000330 Builder.SetInsertPoint(FailureBB);
Robin Morisseta47cb412014-09-03 21:01:03 +0000331 TLI->emitTrailingFence(Builder, FailureOrder, /*IsStore=*/true,
332 /*IsLoad=*/true);
Tim Northover20b9f732014-06-13 16:45:52 +0000333 Builder.CreateBr(ExitBB);
334
Tim Northoverb4ddc082014-05-30 10:09:59 +0000335 // Finally, we have control-flow based knowledge of whether the cmpxchg
336 // succeeded or not. We expose this to later passes by converting any
337 // subsequent "icmp eq/ne %loaded, %oldval" into a use of an appropriate PHI.
338
339 // Setup the builder so we can create any PHIs we need.
Tim Northover20b9f732014-06-13 16:45:52 +0000340 Builder.SetInsertPoint(ExitBB, ExitBB->begin());
Tim Northover420a2162014-06-13 14:24:07 +0000341 PHINode *Success = Builder.CreatePHI(Type::getInt1Ty(Ctx), 2);
342 Success->addIncoming(ConstantInt::getTrue(Ctx), SuccessBB);
Tim Northover20b9f732014-06-13 16:45:52 +0000343 Success->addIncoming(ConstantInt::getFalse(Ctx), FailureBB);
Tim Northoverb4ddc082014-05-30 10:09:59 +0000344
345 // Look for any users of the cmpxchg that are just comparing the loaded value
346 // against the desired one, and replace them with the CFG-derived version.
Tim Northover420a2162014-06-13 14:24:07 +0000347 SmallVector<ExtractValueInst *, 2> PrunedInsts;
Tim Northoverb4ddc082014-05-30 10:09:59 +0000348 for (auto User : CI->users()) {
Tim Northover420a2162014-06-13 14:24:07 +0000349 ExtractValueInst *EV = dyn_cast<ExtractValueInst>(User);
350 if (!EV)
Tim Northoverb4ddc082014-05-30 10:09:59 +0000351 continue;
352
Tim Northover420a2162014-06-13 14:24:07 +0000353 assert(EV->getNumIndices() == 1 && EV->getIndices()[0] <= 1 &&
354 "weird extraction from { iN, i1 }");
Tim Northoverb4ddc082014-05-30 10:09:59 +0000355
Tim Northover420a2162014-06-13 14:24:07 +0000356 if (EV->getIndices()[0] == 0)
357 EV->replaceAllUsesWith(Loaded);
358 else
359 EV->replaceAllUsesWith(Success);
360
361 PrunedInsts.push_back(EV);
Tim Northoverb4ddc082014-05-30 10:09:59 +0000362 }
363
Tim Northover420a2162014-06-13 14:24:07 +0000364 // We can remove the instructions now we're no longer iterating through them.
365 for (auto EV : PrunedInsts)
366 EV->eraseFromParent();
Tim Northoverc882eb02014-04-03 11:44:58 +0000367
Tim Northover420a2162014-06-13 14:24:07 +0000368 if (!CI->use_empty()) {
369 // Some use of the full struct return that we don't understand has happened,
370 // so we've got to reconstruct it properly.
371 Value *Res;
372 Res = Builder.CreateInsertValue(UndefValue::get(CI->getType()), Loaded, 0);
373 Res = Builder.CreateInsertValue(Res, Success, 1);
374
375 CI->replaceAllUsesWith(Res);
376 }
377
378 CI->eraseFromParent();
Tim Northoverc882eb02014-04-03 11:44:58 +0000379 return true;
380}