blob: 5d17e5f8d52d82714f3af5d79edcdc592a16f2e3 [file] [log] [blame]
Chris Lattnerdbe0dec2007-03-31 04:06:36 +00001//===- CodeGenPrepare.cpp - Prepare a function for code generation --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbe0dec2007-03-31 04:06:36 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass munges the code in the input function to better prepare it for
Gordon Henriksena8a118b2008-05-08 17:46:35 +000011// SelectionDAG-based code generation. This works around limitations in it's
12// basic-block-at-a-time approach. It should eventually be removed.
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000013//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "codegenprepare"
17#include "llvm/Transforms/Scalar.h"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
Evan Cheng9bf12b52008-02-26 02:42:37 +000021#include "llvm/InlineAsm.h"
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000022#include "llvm/Instructions.h"
Dale Johannesen6aae1d62009-03-26 01:15:07 +000023#include "llvm/IntrinsicInst.h"
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000024#include "llvm/Pass.h"
Cameron Zwarich80f6a502011-01-08 17:01:52 +000025#include "llvm/Analysis/Dominators.h"
Owen Andersond5f86842010-12-23 20:57:35 +000026#include "llvm/Analysis/InstructionSimplify.h"
Andreas Neustifterad809812009-09-16 09:26:52 +000027#include "llvm/Analysis/ProfileInfo.h"
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000028#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Evan Chenga1fd5b32009-02-20 18:24:38 +000030#include "llvm/Transforms/Utils/AddrModeMatcher.h"
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000031#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerdd77df32007-04-13 20:30:56 +000032#include "llvm/Transforms/Utils/Local.h"
Eric Christopher040056f2010-03-11 02:41:03 +000033#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chris Lattnerdd77df32007-04-13 20:30:56 +000034#include "llvm/ADT/DenseMap.h"
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000035#include "llvm/ADT/SmallSet.h"
Jakob Stoklund Olesen7eb589d2010-09-30 20:51:52 +000036#include "llvm/ADT/Statistic.h"
Dan Gohman03ce0422009-02-13 17:45:12 +000037#include "llvm/Assembly/Writer.h"
Evan Cheng9bf12b52008-02-26 02:42:37 +000038#include "llvm/Support/CallSite.h"
Evan Chenge1bcb442010-08-17 01:34:49 +000039#include "llvm/Support/CommandLine.h"
Evan Chengbdcb7262007-12-05 23:58:20 +000040#include "llvm/Support/Debug.h"
Chris Lattnerdd77df32007-04-13 20:30:56 +000041#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner088a1e82008-11-25 04:42:10 +000042#include "llvm/Support/PatternMatch.h"
Dan Gohman6c1980b2009-07-25 01:13:51 +000043#include "llvm/Support/raw_ostream.h"
Eric Christopher040056f2010-03-11 02:41:03 +000044#include "llvm/Support/IRBuilder.h"
Chris Lattner94e8e0c2011-01-15 07:25:29 +000045#include "llvm/Support/ValueHandle.h"
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000046using namespace llvm;
Chris Lattner088a1e82008-11-25 04:42:10 +000047using namespace llvm::PatternMatch;
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000048
Cameron Zwarich31ff1332011-01-05 17:27:27 +000049STATISTIC(NumBlocksElim, "Number of blocks eliminated");
Cameron Zwarich073057f2011-01-05 17:47:38 +000050STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated");
51STATISTIC(NumGEPsElim, "Number of GEPs converted to casts");
Cameron Zwarich31ff1332011-01-05 17:27:27 +000052STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of "
53 "sunken Cmps");
54STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses "
55 "of sunken Casts");
56STATISTIC(NumMemoryInsts, "Number of memory instructions whose address "
57 "computations were sunk");
58STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads");
59STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized");
Jakob Stoklund Olesen7eb589d2010-09-30 20:51:52 +000060
Evan Chenge1bcb442010-08-17 01:34:49 +000061static cl::opt<bool>
62CriticalEdgeSplit("cgp-critical-edge-splitting",
63 cl::desc("Split critical edges during codegen prepare"),
Jakob Stoklund Olesen7eb589d2010-09-30 20:51:52 +000064 cl::init(false), cl::Hidden);
Evan Chenge1bcb442010-08-17 01:34:49 +000065
Eric Christopher692bf6b2008-09-24 05:32:41 +000066namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000067 class CodeGenPrepare : public FunctionPass {
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000068 /// TLI - Keep a pointer of a TargetLowering to consult for determining
69 /// transformation profitability.
70 const TargetLowering *TLI;
Cameron Zwarich80f6a502011-01-08 17:01:52 +000071 DominatorTree *DT;
Evan Cheng04149f72009-12-17 09:39:49 +000072 ProfileInfo *PFI;
Chris Lattner75796092011-01-15 07:14:54 +000073
74 /// CurInstIterator - As we scan instructions optimizing them, this is the
75 /// next instruction to optimize. Xforms that can invalidate this should
76 /// update it.
77 BasicBlock::iterator CurInstIterator;
Evan Chengab631522008-12-19 18:03:11 +000078
79 /// BackEdges - Keep a set of all the loop back edges.
80 ///
Mike Stumpfe095f32009-05-04 18:40:41 +000081 SmallSet<std::pair<const BasicBlock*, const BasicBlock*>, 8> BackEdges;
Cameron Zwarich8c3527e2011-01-06 00:42:50 +000082
83 // Keeps track of non-local addresses that have been sunk into a block. This
84 // allows us to avoid inserting duplicate code for blocks with multiple
85 // load/stores of the same address.
86 DenseMap<Value*, Value*> SunkAddrs;
87
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000088 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000089 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000090 explicit CodeGenPrepare(const TargetLowering *tli = 0)
Owen Anderson081c34b2010-10-19 17:21:58 +000091 : FunctionPass(ID), TLI(tli) {
92 initializeCodeGenPreparePass(*PassRegistry::getPassRegistry());
93 }
Chris Lattnerdbe0dec2007-03-31 04:06:36 +000094 bool runOnFunction(Function &F);
Eric Christopher692bf6b2008-09-24 05:32:41 +000095
Andreas Neustifterad809812009-09-16 09:26:52 +000096 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Cameron Zwarich80f6a502011-01-08 17:01:52 +000097 AU.addPreserved<DominatorTree>();
Andreas Neustifterad809812009-09-16 09:26:52 +000098 AU.addPreserved<ProfileInfo>();
99 }
100
Dan Gohmanaa0e5232010-02-05 19:24:11 +0000101 virtual void releaseMemory() {
102 BackEdges.clear();
103 }
104
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000105 private:
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000106 bool EliminateMostlyEmptyBlocks(Function &F);
107 bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
108 void EliminateMostlyEmptyBlock(BasicBlock *BB);
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000109 bool OptimizeBlock(BasicBlock &BB);
Cameron Zwarichc0611012011-01-06 02:37:26 +0000110 bool OptimizeInst(Instruction *I);
Chris Lattner1a8943a2011-01-15 07:29:01 +0000111 bool OptimizeMemoryInst(Instruction *I, Value *Addr, const Type *AccessTy);
Chris Lattner75796092011-01-15 07:14:54 +0000112 bool OptimizeInlineAsmInst(CallInst *CS);
Eric Christopher040056f2010-03-11 02:41:03 +0000113 bool OptimizeCallInst(CallInst *CI);
Dan Gohmanb00f2362009-10-16 20:59:35 +0000114 bool MoveExtToFormExtLoad(Instruction *I);
Evan Chengbdcb7262007-12-05 23:58:20 +0000115 bool OptimizeExtUses(Instruction *I);
Mike Stumpfe095f32009-05-04 18:40:41 +0000116 void findLoopBackEdges(const Function &F);
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000117 };
118}
Devang Patel794fd752007-05-01 21:15:47 +0000119
Devang Patel19974732007-05-03 01:11:54 +0000120char CodeGenPrepare::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000121INITIALIZE_PASS(CodeGenPrepare, "codegenprepare",
Owen Andersonce665bd2010-10-07 22:25:06 +0000122 "Optimize for code generation", false, false)
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000123
124FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) {
125 return new CodeGenPrepare(TLI);
126}
127
Evan Chengab631522008-12-19 18:03:11 +0000128/// findLoopBackEdges - Do a DFS walk to find loop back edges.
129///
Mike Stumpfe095f32009-05-04 18:40:41 +0000130void CodeGenPrepare::findLoopBackEdges(const Function &F) {
131 SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges;
132 FindFunctionBackedges(F, Edges);
133
134 BackEdges.insert(Edges.begin(), Edges.end());
Evan Chengab631522008-12-19 18:03:11 +0000135}
136
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000137
138bool CodeGenPrepare::runOnFunction(Function &F) {
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000139 bool EverMadeChange = false;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000140
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000141 DT = getAnalysisIfAvailable<DominatorTree>();
Evan Cheng04149f72009-12-17 09:39:49 +0000142 PFI = getAnalysisIfAvailable<ProfileInfo>();
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000143 // First pass, eliminate blocks that contain only PHI nodes and an
144 // unconditional branch.
145 EverMadeChange |= EliminateMostlyEmptyBlocks(F);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000146
Cameron Zwarich95bb0042011-01-04 04:43:31 +0000147 // Now find loop back edges, but only if they are being used to decide which
148 // critical edges to split.
149 if (CriticalEdgeSplit)
150 findLoopBackEdges(F);
Evan Cheng7e66c0d2009-01-05 21:17:27 +0000151
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000152 bool MadeChange = true;
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000153 while (MadeChange) {
154 MadeChange = false;
155 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
156 MadeChange |= OptimizeBlock(*BB);
157 EverMadeChange |= MadeChange;
158 }
Cameron Zwarich8c3527e2011-01-06 00:42:50 +0000159
160 SunkAddrs.clear();
161
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000162 return EverMadeChange;
163}
164
Dale Johannesen2d697242009-03-27 01:13:37 +0000165/// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes,
166/// debug info directives, and an unconditional branch. Passes before isel
167/// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for
168/// isel. Start by eliminating these blocks so we can split them the way we
169/// want them.
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000170bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) {
171 bool MadeChange = false;
172 // Note that this intentionally skips the entry block.
173 for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) {
174 BasicBlock *BB = I++;
175
176 // If this block doesn't end with an uncond branch, ignore it.
177 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
178 if (!BI || !BI->isUnconditional())
179 continue;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000180
Dale Johannesen2d697242009-03-27 01:13:37 +0000181 // If the instruction before the branch (skipping debug info) isn't a phi
182 // node, then other stuff is happening here.
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000183 BasicBlock::iterator BBI = BI;
184 if (BBI != BB->begin()) {
185 --BBI;
Dale Johannesen2d697242009-03-27 01:13:37 +0000186 while (isa<DbgInfoIntrinsic>(BBI)) {
187 if (BBI == BB->begin())
188 break;
189 --BBI;
190 }
191 if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
192 continue;
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000193 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000194
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000195 // Do not break infinite loops.
196 BasicBlock *DestBB = BI->getSuccessor(0);
197 if (DestBB == BB)
198 continue;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000199
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000200 if (!CanMergeBlocks(BB, DestBB))
201 continue;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000202
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000203 EliminateMostlyEmptyBlock(BB);
204 MadeChange = true;
205 }
206 return MadeChange;
207}
208
209/// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a
210/// single uncond branch between them, and BB contains no other non-phi
211/// instructions.
212bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB,
213 const BasicBlock *DestBB) const {
214 // We only want to eliminate blocks whose phi nodes are used by phi nodes in
215 // the successor. If there are more complex condition (e.g. preheaders),
216 // don't mess around with them.
217 BasicBlock::const_iterator BBI = BB->begin();
218 while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
Gabor Greif60ad7812010-03-25 23:06:16 +0000219 for (Value::const_use_iterator UI = PN->use_begin(), E = PN->use_end();
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000220 UI != E; ++UI) {
221 const Instruction *User = cast<Instruction>(*UI);
222 if (User->getParent() != DestBB || !isa<PHINode>(User))
223 return false;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000224 // If User is inside DestBB block and it is a PHINode then check
225 // incoming value. If incoming value is not from BB then this is
Devang Patel75abc1e2007-04-25 00:37:04 +0000226 // a complex condition (e.g. preheaders) we want to avoid here.
227 if (User->getParent() == DestBB) {
228 if (const PHINode *UPN = dyn_cast<PHINode>(User))
229 for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
230 Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
231 if (Insn && Insn->getParent() == BB &&
232 Insn->getParent() != UPN->getIncomingBlock(I))
233 return false;
234 }
235 }
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000236 }
237 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000238
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000239 // If BB and DestBB contain any common predecessors, then the phi nodes in BB
240 // and DestBB may have conflicting incoming values for the block. If so, we
241 // can't merge the block.
242 const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
243 if (!DestBBPN) return true; // no conflict.
Eric Christopher692bf6b2008-09-24 05:32:41 +0000244
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000245 // Collect the preds of BB.
Chris Lattnerf67f73a2007-11-06 22:07:40 +0000246 SmallPtrSet<const BasicBlock*, 16> BBPreds;
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000247 if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
248 // It is faster to get preds from a PHI than with pred_iterator.
249 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
250 BBPreds.insert(BBPN->getIncomingBlock(i));
251 } else {
252 BBPreds.insert(pred_begin(BB), pred_end(BB));
253 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000254
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000255 // Walk the preds of DestBB.
256 for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
257 BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
258 if (BBPreds.count(Pred)) { // Common predecessor?
259 BBI = DestBB->begin();
260 while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
261 const Value *V1 = PN->getIncomingValueForBlock(Pred);
262 const Value *V2 = PN->getIncomingValueForBlock(BB);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000263
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000264 // If V2 is a phi node in BB, look up what the mapped value will be.
265 if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
266 if (V2PN->getParent() == BB)
267 V2 = V2PN->getIncomingValueForBlock(Pred);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000268
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000269 // If there is a conflict, bail out.
270 if (V1 != V2) return false;
271 }
272 }
273 }
274
275 return true;
276}
277
278
279/// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and
280/// an unconditional branch in it.
281void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
282 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
283 BasicBlock *DestBB = BI->getSuccessor(0);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000284
David Greene68d67fd2010-01-05 01:27:11 +0000285 DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000286
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000287 // If the destination block has a single pred, then this is a trivial edge,
288 // just collapse it.
Chris Lattner9918fb52008-11-27 19:29:14 +0000289 if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
Chris Lattnerf5102a02008-11-28 19:54:49 +0000290 if (SinglePred != DestBB) {
291 // Remember if SinglePred was the entry block of the function. If so, we
292 // will need to move BB back to the entry position.
293 bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
Andreas Neustifterad809812009-09-16 09:26:52 +0000294 MergeBasicBlockIntoOnlyPred(DestBB, this);
Chris Lattner9918fb52008-11-27 19:29:14 +0000295
Chris Lattnerf5102a02008-11-28 19:54:49 +0000296 if (isEntry && BB != &BB->getParent()->getEntryBlock())
297 BB->moveBefore(&BB->getParent()->getEntryBlock());
298
David Greene68d67fd2010-01-05 01:27:11 +0000299 DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
Chris Lattnerf5102a02008-11-28 19:54:49 +0000300 return;
301 }
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000302 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000303
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000304 // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB
305 // to handle the new incoming edges it is about to have.
306 PHINode *PN;
307 for (BasicBlock::iterator BBI = DestBB->begin();
308 (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
309 // Remove the incoming value for BB, and remember it.
310 Value *InVal = PN->removeIncomingValue(BB, false);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000311
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000312 // Two options: either the InVal is a phi node defined in BB or it is some
313 // value that dominates BB.
314 PHINode *InValPhi = dyn_cast<PHINode>(InVal);
315 if (InValPhi && InValPhi->getParent() == BB) {
316 // Add all of the input values of the input PHI as inputs of this phi.
317 for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
318 PN->addIncoming(InValPhi->getIncomingValue(i),
319 InValPhi->getIncomingBlock(i));
320 } else {
321 // Otherwise, add one instance of the dominating value for each edge that
322 // we will be adding.
323 if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
324 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
325 PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
326 } else {
327 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
328 PN->addIncoming(InVal, *PI);
329 }
330 }
331 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000332
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000333 // The PHIs are now updated, change everything that refers to BB to use
334 // DestBB and remove BB.
335 BB->replaceAllUsesWith(DestBB);
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000336 if (DT) {
337 BasicBlock *BBIDom = DT->getNode(BB)->getIDom()->getBlock();
338 BasicBlock *DestBBIDom = DT->getNode(DestBB)->getIDom()->getBlock();
339 BasicBlock *NewIDom = DT->findNearestCommonDominator(BBIDom, DestBBIDom);
340 DT->changeImmediateDominator(DestBB, NewIDom);
341 DT->eraseNode(BB);
342 }
Evan Cheng04149f72009-12-17 09:39:49 +0000343 if (PFI) {
344 PFI->replaceAllUses(BB, DestBB);
345 PFI->removeEdge(ProfileInfo::getEdge(BB, DestBB));
Andreas Neustifterad809812009-09-16 09:26:52 +0000346 }
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000347 BB->eraseFromParent();
Cameron Zwarich31ff1332011-01-05 17:27:27 +0000348 ++NumBlocksElim;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000349
David Greene68d67fd2010-01-05 01:27:11 +0000350 DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000351}
352
Chris Lattner98d5c312010-02-13 05:35:08 +0000353/// FindReusablePredBB - Check all of the predecessors of the block DestPHI
354/// lives in to see if there is a block that we can reuse as a critical edge
355/// from TIBB.
356static BasicBlock *FindReusablePredBB(PHINode *DestPHI, BasicBlock *TIBB) {
357 BasicBlock *Dest = DestPHI->getParent();
358
359 /// TIPHIValues - This array is lazily computed to determine the values of
360 /// PHIs in Dest that TI would provide.
361 SmallVector<Value*, 32> TIPHIValues;
362
363 /// TIBBEntryNo - This is a cache to speed up pred queries for TIBB.
364 unsigned TIBBEntryNo = 0;
365
366 // Check to see if Dest has any blocks that can be used as a split edge for
367 // this terminator.
368 for (unsigned pi = 0, e = DestPHI->getNumIncomingValues(); pi != e; ++pi) {
369 BasicBlock *Pred = DestPHI->getIncomingBlock(pi);
370 // To be usable, the pred has to end with an uncond branch to the dest.
371 BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator());
372 if (!PredBr || !PredBr->isUnconditional())
373 continue;
374 // Must be empty other than the branch and debug info.
375 BasicBlock::iterator I = Pred->begin();
376 while (isa<DbgInfoIntrinsic>(I))
377 I++;
378 if (&*I != PredBr)
379 continue;
380 // Cannot be the entry block; its label does not get emitted.
381 if (Pred == &Dest->getParent()->getEntryBlock())
382 continue;
383
384 // Finally, since we know that Dest has phi nodes in it, we have to make
385 // sure that jumping to Pred will have the same effect as going to Dest in
386 // terms of PHI values.
387 PHINode *PN;
388 unsigned PHINo = 0;
389 unsigned PredEntryNo = pi;
390
391 bool FoundMatch = true;
392 for (BasicBlock::iterator I = Dest->begin();
393 (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) {
394 if (PHINo == TIPHIValues.size()) {
395 if (PN->getIncomingBlock(TIBBEntryNo) != TIBB)
396 TIBBEntryNo = PN->getBasicBlockIndex(TIBB);
397 TIPHIValues.push_back(PN->getIncomingValue(TIBBEntryNo));
398 }
399
400 // If the PHI entry doesn't work, we can't use this pred.
401 if (PN->getIncomingBlock(PredEntryNo) != Pred)
402 PredEntryNo = PN->getBasicBlockIndex(Pred);
403
404 if (TIPHIValues[PHINo] != PN->getIncomingValue(PredEntryNo)) {
405 FoundMatch = false;
406 break;
407 }
408 }
409
410 // If we found a workable predecessor, change TI to branch to Succ.
411 if (FoundMatch)
412 return Pred;
413 }
414 return 0;
415}
416
Chris Lattnerd9c3a0d2007-04-02 01:35:34 +0000417
Chris Lattnerebe80752007-12-24 19:32:55 +0000418/// SplitEdgeNicely - Split the critical edge from TI to its specified
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000419/// successor if it will improve codegen. We only do this if the successor has
420/// phi nodes (otherwise critical edges are ok). If there is already another
421/// predecessor of the succ that is empty (and thus has no phi nodes), use it
422/// instead of introducing a new block.
Evan Chengab631522008-12-19 18:03:11 +0000423static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum,
Mike Stumpfe095f32009-05-04 18:40:41 +0000424 SmallSet<std::pair<const BasicBlock*,
425 const BasicBlock*>, 8> &BackEdges,
Evan Chengab631522008-12-19 18:03:11 +0000426 Pass *P) {
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000427 BasicBlock *TIBB = TI->getParent();
428 BasicBlock *Dest = TI->getSuccessor(SuccNum);
429 assert(isa<PHINode>(Dest->begin()) &&
430 "This should only be called if Dest has a PHI!");
Chris Lattner3f65b5e2010-02-13 04:04:42 +0000431 PHINode *DestPHI = cast<PHINode>(Dest->begin());
Eric Christopher692bf6b2008-09-24 05:32:41 +0000432
Evan Chengfc0b80d2009-03-13 22:59:14 +0000433 // Do not split edges to EH landing pads.
Chris Lattner3f65b5e2010-02-13 04:04:42 +0000434 if (InvokeInst *Invoke = dyn_cast<InvokeInst>(TI))
Evan Chengfc0b80d2009-03-13 22:59:14 +0000435 if (Invoke->getSuccessor(1) == Dest)
436 return;
Evan Chengfc0b80d2009-03-13 22:59:14 +0000437
Chris Lattnerebe80752007-12-24 19:32:55 +0000438 // As a hack, never split backedges of loops. Even though the copy for any
439 // PHIs inserted on the backedge would be dead for exits from the loop, we
440 // assume that the cost of *splitting* the backedge would be too high.
Evan Chengab631522008-12-19 18:03:11 +0000441 if (BackEdges.count(std::make_pair(TIBB, Dest)))
Chris Lattnerebe80752007-12-24 19:32:55 +0000442 return;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000443
Chris Lattnerc09687b2010-02-13 19:07:06 +0000444 if (BasicBlock *ReuseBB = FindReusablePredBB(DestPHI, TIBB)) {
445 ProfileInfo *PFI = P->getAnalysisIfAvailable<ProfileInfo>();
446 if (PFI)
447 PFI->splitEdge(TIBB, Dest, ReuseBB);
448 Dest->removePredecessor(TIBB);
449 TI->setSuccessor(SuccNum, ReuseBB);
Evan Chengab631522008-12-19 18:03:11 +0000450 return;
451 }
452
Chris Lattnerc09687b2010-02-13 19:07:06 +0000453 SplitCriticalEdge(TI, SuccNum, P, true);
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000454}
455
Evan Chengab631522008-12-19 18:03:11 +0000456
Chris Lattnerdd77df32007-04-13 20:30:56 +0000457/// OptimizeNoopCopyExpression - If the specified cast instruction is a noop
Dan Gohmana119de82009-06-14 23:30:43 +0000458/// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC),
459/// sink it into user blocks to reduce the number of virtual
Dale Johannesence0b2372007-06-12 16:50:17 +0000460/// registers that must be created and coalesced.
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000461///
462/// Return true if any changes are made.
Chris Lattner85fa13c2008-11-24 22:44:16 +0000463///
Chris Lattnerdd77df32007-04-13 20:30:56 +0000464static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
Eric Christopher692bf6b2008-09-24 05:32:41 +0000465 // If this is a noop copy,
Owen Andersone50ed302009-08-10 22:56:29 +0000466 EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType());
467 EVT DstVT = TLI.getValueType(CI->getType());
Eric Christopher692bf6b2008-09-24 05:32:41 +0000468
Chris Lattnerdd77df32007-04-13 20:30:56 +0000469 // This is an fp<->int conversion?
Duncan Sands83ec4b62008-06-06 12:08:01 +0000470 if (SrcVT.isInteger() != DstVT.isInteger())
Chris Lattnerdd77df32007-04-13 20:30:56 +0000471 return false;
Duncan Sands8e4eb092008-06-08 20:54:56 +0000472
Chris Lattnerdd77df32007-04-13 20:30:56 +0000473 // If this is an extension, it will be a zero or sign extension, which
474 // isn't a noop.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000475 if (SrcVT.bitsLT(DstVT)) return false;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000476
Chris Lattnerdd77df32007-04-13 20:30:56 +0000477 // If these values will be promoted, find out what they will be promoted
478 // to. This helps us consider truncates on PPC as noop copies when they
479 // are.
Chris Lattneraafe6262010-08-25 23:00:45 +0000480 if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote)
Owen Anderson23b9b192009-08-12 00:36:31 +0000481 SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
Chris Lattneraafe6262010-08-25 23:00:45 +0000482 if (TLI.getTypeAction(DstVT) == TargetLowering::Promote)
Owen Anderson23b9b192009-08-12 00:36:31 +0000483 DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000484
Chris Lattnerdd77df32007-04-13 20:30:56 +0000485 // If, after promotion, these are the same types, this is a noop copy.
486 if (SrcVT != DstVT)
487 return false;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000488
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000489 BasicBlock *DefBB = CI->getParent();
Eric Christopher692bf6b2008-09-24 05:32:41 +0000490
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000491 /// InsertedCasts - Only insert a cast in each block once.
Dale Johannesence0b2372007-06-12 16:50:17 +0000492 DenseMap<BasicBlock*, CastInst*> InsertedCasts;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000493
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000494 bool MadeChange = false;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000495 for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000496 UI != E; ) {
497 Use &TheUse = UI.getUse();
498 Instruction *User = cast<Instruction>(*UI);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000499
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000500 // Figure out which BB this cast is used in. For PHI's this is the
501 // appropriate predecessor block.
502 BasicBlock *UserBB = User->getParent();
503 if (PHINode *PN = dyn_cast<PHINode>(User)) {
Gabor Greifa36791d2009-01-23 19:40:15 +0000504 UserBB = PN->getIncomingBlock(UI);
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000505 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000506
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000507 // Preincrement use iterator so we don't invalidate it.
508 ++UI;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000509
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000510 // If this user is in the same block as the cast, don't change the cast.
511 if (UserBB == DefBB) continue;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000512
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000513 // If we have already inserted a cast into this block, use it.
514 CastInst *&InsertedCast = InsertedCasts[UserBB];
515
516 if (!InsertedCast) {
Dan Gohman02dea8b2008-05-23 21:05:58 +0000517 BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
Eric Christopher692bf6b2008-09-24 05:32:41 +0000518
519 InsertedCast =
520 CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000521 InsertPt);
522 MadeChange = true;
523 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000524
Dale Johannesence0b2372007-06-12 16:50:17 +0000525 // Replace a use of the cast with a use of the new cast.
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000526 TheUse = InsertedCast;
Cameron Zwarich31ff1332011-01-05 17:27:27 +0000527 ++NumCastUses;
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000528 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000529
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000530 // If we removed all uses, nuke the cast.
Duncan Sandse0038132008-01-20 16:51:46 +0000531 if (CI->use_empty()) {
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000532 CI->eraseFromParent();
Duncan Sandse0038132008-01-20 16:51:46 +0000533 MadeChange = true;
534 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000535
Chris Lattnerdbe0dec2007-03-31 04:06:36 +0000536 return MadeChange;
537}
538
Eric Christopher692bf6b2008-09-24 05:32:41 +0000539/// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce
Dale Johannesence0b2372007-06-12 16:50:17 +0000540/// the number of virtual registers that must be created and coalesced. This is
Chris Lattner684b22d2007-08-02 16:53:43 +0000541/// a clear win except on targets with multiple condition code registers
542/// (PowerPC), where it might lose; some adjustment may be wanted there.
Dale Johannesence0b2372007-06-12 16:50:17 +0000543///
544/// Return true if any changes are made.
Chris Lattner85fa13c2008-11-24 22:44:16 +0000545static bool OptimizeCmpExpression(CmpInst *CI) {
Dale Johannesence0b2372007-06-12 16:50:17 +0000546 BasicBlock *DefBB = CI->getParent();
Eric Christopher692bf6b2008-09-24 05:32:41 +0000547
Dale Johannesence0b2372007-06-12 16:50:17 +0000548 /// InsertedCmp - Only insert a cmp in each block once.
549 DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000550
Dale Johannesence0b2372007-06-12 16:50:17 +0000551 bool MadeChange = false;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000552 for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
Dale Johannesence0b2372007-06-12 16:50:17 +0000553 UI != E; ) {
554 Use &TheUse = UI.getUse();
555 Instruction *User = cast<Instruction>(*UI);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000556
Dale Johannesence0b2372007-06-12 16:50:17 +0000557 // Preincrement use iterator so we don't invalidate it.
558 ++UI;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000559
Dale Johannesence0b2372007-06-12 16:50:17 +0000560 // Don't bother for PHI nodes.
561 if (isa<PHINode>(User))
562 continue;
563
564 // Figure out which BB this cmp is used in.
565 BasicBlock *UserBB = User->getParent();
Eric Christopher692bf6b2008-09-24 05:32:41 +0000566
Dale Johannesence0b2372007-06-12 16:50:17 +0000567 // If this user is in the same block as the cmp, don't change the cmp.
568 if (UserBB == DefBB) continue;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000569
Dale Johannesence0b2372007-06-12 16:50:17 +0000570 // If we have already inserted a cmp into this block, use it.
571 CmpInst *&InsertedCmp = InsertedCmps[UserBB];
572
573 if (!InsertedCmp) {
Dan Gohman02dea8b2008-05-23 21:05:58 +0000574 BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
Eric Christopher692bf6b2008-09-24 05:32:41 +0000575
576 InsertedCmp =
Dan Gohman1c8a23c2009-08-25 23:17:54 +0000577 CmpInst::Create(CI->getOpcode(),
Owen Anderson333c4002009-07-09 23:48:35 +0000578 CI->getPredicate(), CI->getOperand(0),
Dale Johannesence0b2372007-06-12 16:50:17 +0000579 CI->getOperand(1), "", InsertPt);
580 MadeChange = true;
581 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000582
Dale Johannesence0b2372007-06-12 16:50:17 +0000583 // Replace a use of the cmp with a use of the new cmp.
584 TheUse = InsertedCmp;
Cameron Zwarich31ff1332011-01-05 17:27:27 +0000585 ++NumCmpUses;
Dale Johannesence0b2372007-06-12 16:50:17 +0000586 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000587
Dale Johannesence0b2372007-06-12 16:50:17 +0000588 // If we removed all uses, nuke the cmp.
589 if (CI->use_empty())
590 CI->eraseFromParent();
Eric Christopher692bf6b2008-09-24 05:32:41 +0000591
Dale Johannesence0b2372007-06-12 16:50:17 +0000592 return MadeChange;
593}
594
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000595namespace {
596class CodeGenPrepareFortifiedLibCalls : public SimplifyFortifiedLibCalls {
597protected:
598 void replaceCall(Value *With) {
599 CI->replaceAllUsesWith(With);
600 CI->eraseFromParent();
601 }
602 bool isFoldable(unsigned SizeCIOp, unsigned, bool) const {
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000603 if (ConstantInt *SizeCI =
604 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp)))
605 return SizeCI->isAllOnesValue();
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000606 return false;
607 }
608};
609} // end anonymous namespace
610
Eric Christopher040056f2010-03-11 02:41:03 +0000611bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) {
Chris Lattner75796092011-01-15 07:14:54 +0000612 BasicBlock *BB = CI->getParent();
613
614 // Lower inline assembly if we can.
615 // If we found an inline asm expession, and if the target knows how to
616 // lower it to normal LLVM code, do so now.
617 if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
618 if (TLI->ExpandInlineAsm(CI)) {
619 // Avoid invalidating the iterator.
620 CurInstIterator = BB->begin();
621 // Avoid processing instructions out of order, which could cause
622 // reuse before a value is defined.
623 SunkAddrs.clear();
624 return true;
625 }
626 // Sink address computing for memory operands into the block.
627 if (OptimizeInlineAsmInst(CI))
628 return true;
629 }
630
Eric Christopher040056f2010-03-11 02:41:03 +0000631 // Lower all uses of llvm.objectsize.*
632 IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
633 if (II && II->getIntrinsicID() == Intrinsic::objectsize) {
Gabor Greifde9f5452010-06-24 00:44:01 +0000634 bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1);
Eric Christopher040056f2010-03-11 02:41:03 +0000635 const Type *ReturnTy = CI->getType();
636 Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
Chris Lattner94e8e0c2011-01-15 07:25:29 +0000637
638 // Substituting this can cause recursive simplifications, which can
639 // invalidate our iterator. Use a WeakVH to hold onto it in case this
640 // happens.
641 WeakVH IterHandle(CurInstIterator);
642
643 ReplaceAndSimplifyAllUses(CI, RetVal, TLI ? TLI->getTargetData() : 0, DT);
644
645 // If the iterator instruction was recursively deleted, start over at the
646 // start of the block.
Chris Lattner435b4d22011-01-18 20:53:04 +0000647 if (IterHandle != CurInstIterator) {
Chris Lattner94e8e0c2011-01-15 07:25:29 +0000648 CurInstIterator = BB->begin();
Chris Lattner435b4d22011-01-18 20:53:04 +0000649 SunkAddrs.clear();
650 }
Eric Christopher040056f2010-03-11 02:41:03 +0000651 return true;
652 }
653
654 // From here on out we're working with named functions.
655 if (CI->getCalledFunction() == 0) return false;
656
657 // We'll need TargetData from here on out.
658 const TargetData *TD = TLI ? TLI->getTargetData() : 0;
659 if (!TD) return false;
660
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000661 // Lower all default uses of _chk calls. This is very similar
662 // to what InstCombineCalls does, but here we are only lowering calls
Eric Christopher040056f2010-03-11 02:41:03 +0000663 // that have the default "don't know" as the objectsize. Anything else
664 // should be left alone.
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000665 CodeGenPrepareFortifiedLibCalls Simplifier;
666 return Simplifier.fold(CI, TD);
Eric Christopher040056f2010-03-11 02:41:03 +0000667}
Chris Lattner94e8e0c2011-01-15 07:25:29 +0000668
Chris Lattner88a5c832008-11-25 07:09:13 +0000669//===----------------------------------------------------------------------===//
Chris Lattner88a5c832008-11-25 07:09:13 +0000670// Memory Optimization
671//===----------------------------------------------------------------------===//
672
Chris Lattnerdd77df32007-04-13 20:30:56 +0000673/// IsNonLocalValue - Return true if the specified values are defined in a
674/// different basic block than BB.
675static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
676 if (Instruction *I = dyn_cast<Instruction>(V))
677 return I->getParent() != BB;
678 return false;
679}
680
Bob Wilson4a8ee232009-12-03 21:47:07 +0000681/// OptimizeMemoryInst - Load and Store Instructions often have
Chris Lattnerdd77df32007-04-13 20:30:56 +0000682/// addressing modes that can do significant amounts of computation. As such,
683/// instruction selection will try to get the load or store to do as much
684/// computation as possible for the program. The problem is that isel can only
685/// see within a single block. As such, we sink as much legal addressing mode
686/// stuff into the block as possible.
Chris Lattner88a5c832008-11-25 07:09:13 +0000687///
688/// This method is used to optimize both load/store and inline asms with memory
689/// operands.
Chris Lattner896617b2008-11-26 03:20:37 +0000690bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
Chris Lattner1a8943a2011-01-15 07:29:01 +0000691 const Type *AccessTy) {
Owen Anderson35bf4d62010-11-27 08:15:55 +0000692 Value *Repl = Addr;
693
Owen Andersond2f41742010-11-19 22:15:03 +0000694 // Try to collapse single-value PHI nodes. This is necessary to undo
695 // unprofitable PRE transformations.
Cameron Zwarich7cb4fa22011-01-03 06:33:01 +0000696 SmallVector<Value*, 8> worklist;
697 SmallPtrSet<Value*, 16> Visited;
Owen Anderson35bf4d62010-11-27 08:15:55 +0000698 worklist.push_back(Addr);
699
700 // Use a worklist to iteratively look through PHI nodes, and ensure that
701 // the addressing mode obtained from the non-PHI roots of the graph
702 // are equivalent.
703 Value *Consensus = 0;
Cameron Zwarich4c078f02011-03-01 21:13:53 +0000704 unsigned NumUsesConsensus = 0;
Owen Anderson35bf4d62010-11-27 08:15:55 +0000705 SmallVector<Instruction*, 16> AddrModeInsts;
706 ExtAddrMode AddrMode;
707 while (!worklist.empty()) {
708 Value *V = worklist.back();
709 worklist.pop_back();
710
711 // Break use-def graph loops.
712 if (Visited.count(V)) {
713 Consensus = 0;
714 break;
Owen Andersond2f41742010-11-19 22:15:03 +0000715 }
716
Owen Anderson35bf4d62010-11-27 08:15:55 +0000717 Visited.insert(V);
718
719 // For a PHI node, push all of its incoming values.
720 if (PHINode *P = dyn_cast<PHINode>(V)) {
721 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i)
722 worklist.push_back(P->getIncomingValue(i));
723 continue;
724 }
725
726 // For non-PHIs, determine the addressing mode being computed.
727 SmallVector<Instruction*, 16> NewAddrModeInsts;
728 ExtAddrMode NewAddrMode =
729 AddressingModeMatcher::Match(V, AccessTy,MemoryInst,
730 NewAddrModeInsts, *TLI);
731
732 // Ensure that the obtained addressing mode is equivalent to that obtained
733 // for all other roots of the PHI traversal. Also, when choosing one
734 // such root as representative, select the one with the most uses in order
735 // to keep the cost modeling heuristics in AddressingModeMatcher applicable.
736 if (!Consensus || NewAddrMode == AddrMode) {
Cameron Zwarich4c078f02011-03-01 21:13:53 +0000737 unsigned NumUses = V->getNumUses();
738 if (NumUses > NumUsesConsensus) {
Owen Anderson35bf4d62010-11-27 08:15:55 +0000739 Consensus = V;
Cameron Zwarich4c078f02011-03-01 21:13:53 +0000740 NumUsesConsensus = NumUses;
Owen Anderson35bf4d62010-11-27 08:15:55 +0000741 AddrMode = NewAddrMode;
742 AddrModeInsts = NewAddrModeInsts;
743 }
744 continue;
745 }
746
747 Consensus = 0;
748 break;
Owen Andersond2f41742010-11-19 22:15:03 +0000749 }
750
Owen Anderson35bf4d62010-11-27 08:15:55 +0000751 // If the addressing mode couldn't be determined, or if multiple different
752 // ones were determined, bail out now.
753 if (!Consensus) return false;
754
Chris Lattnerdd77df32007-04-13 20:30:56 +0000755 // Check to see if any of the instructions supersumed by this addr mode are
756 // non-local to I's BB.
757 bool AnyNonLocal = false;
758 for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) {
Chris Lattner896617b2008-11-26 03:20:37 +0000759 if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) {
Chris Lattnerdd77df32007-04-13 20:30:56 +0000760 AnyNonLocal = true;
761 break;
762 }
763 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000764
Chris Lattnerdd77df32007-04-13 20:30:56 +0000765 // If all the instructions matched are already in this BB, don't do anything.
766 if (!AnyNonLocal) {
David Greene68d67fd2010-01-05 01:27:11 +0000767 DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n");
Chris Lattnerdd77df32007-04-13 20:30:56 +0000768 return false;
769 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000770
Chris Lattnerdd77df32007-04-13 20:30:56 +0000771 // Insert this computation right after this user. Since our caller is
772 // scanning from the top of the BB to the bottom, reuse of the expr are
773 // guaranteed to happen later.
Chris Lattner896617b2008-11-26 03:20:37 +0000774 BasicBlock::iterator InsertPt = MemoryInst;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000775
Chris Lattnerdd77df32007-04-13 20:30:56 +0000776 // Now that we determined the addressing expression we want to use and know
777 // that we have to sink it into this block. Check to see if we have already
778 // done this for some other load/store instr in this block. If so, reuse the
779 // computation.
780 Value *&SunkAddr = SunkAddrs[Addr];
781 if (SunkAddr) {
David Greene68d67fd2010-01-05 01:27:11 +0000782 DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for "
Dan Gohman6c1980b2009-07-25 01:13:51 +0000783 << *MemoryInst);
Chris Lattnerdd77df32007-04-13 20:30:56 +0000784 if (SunkAddr->getType() != Addr->getType())
785 SunkAddr = new BitCastInst(SunkAddr, Addr->getType(), "tmp", InsertPt);
786 } else {
David Greene68d67fd2010-01-05 01:27:11 +0000787 DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
Dan Gohman6c1980b2009-07-25 01:13:51 +0000788 << *MemoryInst);
Owen Anderson1d0be152009-08-13 21:58:54 +0000789 const Type *IntPtrTy =
790 TLI->getTargetData()->getIntPtrType(AccessTy->getContext());
Eric Christopher692bf6b2008-09-24 05:32:41 +0000791
Chris Lattnerdd77df32007-04-13 20:30:56 +0000792 Value *Result = 0;
Dan Gohmand8d0b6a2010-01-19 22:45:06 +0000793
794 // Start with the base register. Do this first so that subsequent address
795 // matching finds it last, which will prevent it from trying to match it
796 // as the scaled value in case it happens to be a mul. That would be
797 // problematic if we've sunk a different mul for the scale, because then
798 // we'd end up sinking both muls.
799 if (AddrMode.BaseReg) {
800 Value *V = AddrMode.BaseReg;
Duncan Sands1df98592010-02-16 11:11:14 +0000801 if (V->getType()->isPointerTy())
Dan Gohmand8d0b6a2010-01-19 22:45:06 +0000802 V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
803 if (V->getType() != IntPtrTy)
804 V = CastInst::CreateIntegerCast(V, IntPtrTy, /*isSigned=*/true,
805 "sunkaddr", InsertPt);
806 Result = V;
807 }
808
809 // Add the scale value.
Chris Lattnerdd77df32007-04-13 20:30:56 +0000810 if (AddrMode.Scale) {
811 Value *V = AddrMode.ScaledReg;
812 if (V->getType() == IntPtrTy) {
813 // done.
Duncan Sands1df98592010-02-16 11:11:14 +0000814 } else if (V->getType()->isPointerTy()) {
Chris Lattnerdd77df32007-04-13 20:30:56 +0000815 V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
816 } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
817 cast<IntegerType>(V->getType())->getBitWidth()) {
818 V = new TruncInst(V, IntPtrTy, "sunkaddr", InsertPt);
819 } else {
820 V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt);
821 }
822 if (AddrMode.Scale != 1)
Owen Andersoneed707b2009-07-24 23:12:02 +0000823 V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy,
Owen Andersond672ecb2009-07-03 00:17:18 +0000824 AddrMode.Scale),
Chris Lattnerdd77df32007-04-13 20:30:56 +0000825 "sunkaddr", InsertPt);
Chris Lattnerdd77df32007-04-13 20:30:56 +0000826 if (Result)
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000827 Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
Chris Lattnerdd77df32007-04-13 20:30:56 +0000828 else
829 Result = V;
830 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000831
Chris Lattnerdd77df32007-04-13 20:30:56 +0000832 // Add in the BaseGV if present.
833 if (AddrMode.BaseGV) {
834 Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr",
835 InsertPt);
836 if (Result)
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000837 Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
Chris Lattnerdd77df32007-04-13 20:30:56 +0000838 else
839 Result = V;
840 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000841
Chris Lattnerdd77df32007-04-13 20:30:56 +0000842 // Add in the Base Offset if present.
843 if (AddrMode.BaseOffs) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000844 Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
Chris Lattnerdd77df32007-04-13 20:30:56 +0000845 if (Result)
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000846 Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
Chris Lattnerdd77df32007-04-13 20:30:56 +0000847 else
848 Result = V;
849 }
850
851 if (Result == 0)
Owen Andersona7235ea2009-07-31 20:28:14 +0000852 SunkAddr = Constant::getNullValue(Addr->getType());
Chris Lattnerdd77df32007-04-13 20:30:56 +0000853 else
854 SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt);
855 }
Eric Christopher692bf6b2008-09-24 05:32:41 +0000856
Owen Andersond2f41742010-11-19 22:15:03 +0000857 MemoryInst->replaceUsesOfWith(Repl, SunkAddr);
Eric Christopher692bf6b2008-09-24 05:32:41 +0000858
Owen Andersond2f41742010-11-19 22:15:03 +0000859 if (Repl->use_empty()) {
860 RecursivelyDeleteTriviallyDeadInstructions(Repl);
Dale Johannesen536d31b2010-03-31 20:37:15 +0000861 // This address is now available for reassignment, so erase the table entry;
862 // we don't want to match some completely different instruction.
863 SunkAddrs[Addr] = 0;
864 }
Cameron Zwarich31ff1332011-01-05 17:27:27 +0000865 ++NumMemoryInsts;
Chris Lattnerdd77df32007-04-13 20:30:56 +0000866 return true;
867}
868
Evan Cheng9bf12b52008-02-26 02:42:37 +0000869/// OptimizeInlineAsmInst - If there are any memory operands, use
Chris Lattner88a5c832008-11-25 07:09:13 +0000870/// OptimizeMemoryInst to sink their address computing into the block when
Evan Cheng9bf12b52008-02-26 02:42:37 +0000871/// possible / profitable.
Chris Lattner75796092011-01-15 07:14:54 +0000872bool CodeGenPrepare::OptimizeInlineAsmInst(CallInst *CS) {
Evan Cheng9bf12b52008-02-26 02:42:37 +0000873 bool MadeChange = false;
Evan Cheng9bf12b52008-02-26 02:42:37 +0000874
Chris Lattner75796092011-01-15 07:14:54 +0000875 TargetLowering::AsmOperandInfoVector
876 TargetConstraints = TLI->ParseConstraints(CS);
Dale Johannesen677c6ec2010-09-16 18:30:55 +0000877 unsigned ArgNo = 0;
John Thompsoneac6e1d2010-09-13 18:15:37 +0000878 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
879 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
880
Evan Cheng9bf12b52008-02-26 02:42:37 +0000881 // Compute the constraint code and ConstraintType to use.
Dale Johannesen1784d162010-06-25 21:55:36 +0000882 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Evan Cheng9bf12b52008-02-26 02:42:37 +0000883
Eli Friedman9ec80952008-02-26 18:37:49 +0000884 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
885 OpInfo.isIndirect) {
Chris Lattner75796092011-01-15 07:14:54 +0000886 Value *OpVal = CS->getArgOperand(ArgNo++);
Chris Lattner1a8943a2011-01-15 07:29:01 +0000887 MadeChange |= OptimizeMemoryInst(CS, OpVal, OpVal->getType());
Dale Johannesen677c6ec2010-09-16 18:30:55 +0000888 } else if (OpInfo.Type == InlineAsm::isInput)
889 ArgNo++;
Evan Cheng9bf12b52008-02-26 02:42:37 +0000890 }
891
892 return MadeChange;
893}
894
Dan Gohmanb00f2362009-10-16 20:59:35 +0000895/// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same
896/// basic block as the load, unless conditions are unfavorable. This allows
897/// SelectionDAG to fold the extend into the load.
898///
899bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) {
900 // Look for a load being extended.
901 LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0));
902 if (!LI) return false;
903
904 // If they're already in the same block, there's nothing to do.
905 if (LI->getParent() == I->getParent())
906 return false;
907
908 // If the load has other users and the truncate is not free, this probably
909 // isn't worthwhile.
910 if (!LI->hasOneUse() &&
Bob Wilsonec57a1a2010-09-22 18:44:56 +0000911 TLI && (TLI->isTypeLegal(TLI->getValueType(LI->getType())) ||
912 !TLI->isTypeLegal(TLI->getValueType(I->getType()))) &&
Bob Wilson71dc4d92010-09-21 21:54:27 +0000913 !TLI->isTruncateFree(I->getType(), LI->getType()))
Dan Gohmanb00f2362009-10-16 20:59:35 +0000914 return false;
915
916 // Check whether the target supports casts folded into loads.
917 unsigned LType;
918 if (isa<ZExtInst>(I))
919 LType = ISD::ZEXTLOAD;
920 else {
921 assert(isa<SExtInst>(I) && "Unexpected ext type!");
922 LType = ISD::SEXTLOAD;
923 }
924 if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType())))
925 return false;
926
927 // Move the extend into the same block as the load, so that SelectionDAG
928 // can fold it.
929 I->removeFromParent();
930 I->insertAfter(LI);
Cameron Zwarich31ff1332011-01-05 17:27:27 +0000931 ++NumExtsMoved;
Dan Gohmanb00f2362009-10-16 20:59:35 +0000932 return true;
933}
934
Evan Chengbdcb7262007-12-05 23:58:20 +0000935bool CodeGenPrepare::OptimizeExtUses(Instruction *I) {
936 BasicBlock *DefBB = I->getParent();
937
Bob Wilson9120f5c2010-09-21 21:44:14 +0000938 // If the result of a {s|z}ext and its source are both live out, rewrite all
Evan Chengbdcb7262007-12-05 23:58:20 +0000939 // other uses of the source with result of extension.
940 Value *Src = I->getOperand(0);
941 if (Src->hasOneUse())
942 return false;
943
Evan Cheng696e5c02007-12-13 07:50:36 +0000944 // Only do this xform if truncating is free.
Gabor Greif53bdbd72008-02-26 19:13:21 +0000945 if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
Evan Chengf9785f92007-12-13 03:32:53 +0000946 return false;
947
Evan Cheng772de512007-12-12 00:51:06 +0000948 // Only safe to perform the optimization if the source is also defined in
Evan Cheng765dff22007-12-12 02:53:41 +0000949 // this block.
950 if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
Evan Cheng772de512007-12-12 00:51:06 +0000951 return false;
952
Evan Chengbdcb7262007-12-05 23:58:20 +0000953 bool DefIsLiveOut = false;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000954 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
Evan Chengbdcb7262007-12-05 23:58:20 +0000955 UI != E; ++UI) {
956 Instruction *User = cast<Instruction>(*UI);
957
958 // Figure out which BB this ext is used in.
959 BasicBlock *UserBB = User->getParent();
960 if (UserBB == DefBB) continue;
961 DefIsLiveOut = true;
962 break;
963 }
964 if (!DefIsLiveOut)
965 return false;
966
Evan Cheng765dff22007-12-12 02:53:41 +0000967 // Make sure non of the uses are PHI nodes.
Eric Christopher692bf6b2008-09-24 05:32:41 +0000968 for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
Evan Cheng765dff22007-12-12 02:53:41 +0000969 UI != E; ++UI) {
970 Instruction *User = cast<Instruction>(*UI);
Evan Chengf9785f92007-12-13 03:32:53 +0000971 BasicBlock *UserBB = User->getParent();
972 if (UserBB == DefBB) continue;
973 // Be conservative. We don't want this xform to end up introducing
974 // reloads just before load / store instructions.
975 if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User))
Evan Cheng765dff22007-12-12 02:53:41 +0000976 return false;
977 }
978
Evan Chengbdcb7262007-12-05 23:58:20 +0000979 // InsertedTruncs - Only insert one trunc in each block once.
980 DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
981
982 bool MadeChange = false;
Eric Christopher692bf6b2008-09-24 05:32:41 +0000983 for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
Evan Chengbdcb7262007-12-05 23:58:20 +0000984 UI != E; ++UI) {
985 Use &TheUse = UI.getUse();
986 Instruction *User = cast<Instruction>(*UI);
987
988 // Figure out which BB this ext is used in.
989 BasicBlock *UserBB = User->getParent();
990 if (UserBB == DefBB) continue;
991
992 // Both src and def are live in this block. Rewrite the use.
993 Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
994
995 if (!InsertedTrunc) {
Dan Gohman02dea8b2008-05-23 21:05:58 +0000996 BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
Eric Christopher692bf6b2008-09-24 05:32:41 +0000997
Evan Chengbdcb7262007-12-05 23:58:20 +0000998 InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt);
999 }
1000
1001 // Replace a use of the {s|z}ext source with a use of the result.
1002 TheUse = InsertedTrunc;
Cameron Zwarich31ff1332011-01-05 17:27:27 +00001003 ++NumExtUses;
Evan Chengbdcb7262007-12-05 23:58:20 +00001004 MadeChange = true;
1005 }
1006
1007 return MadeChange;
1008}
1009
Cameron Zwarichc0611012011-01-06 02:37:26 +00001010bool CodeGenPrepare::OptimizeInst(Instruction *I) {
Cameron Zwarichc0611012011-01-06 02:37:26 +00001011 if (PHINode *P = dyn_cast<PHINode>(I)) {
1012 // It is possible for very late stage optimizations (such as SimplifyCFG)
1013 // to introduce PHI nodes too late to be cleaned up. If we detect such a
1014 // trivial PHI, go ahead and zap it here.
1015 if (Value *V = SimplifyInstruction(P)) {
1016 P->replaceAllUsesWith(V);
1017 P->eraseFromParent();
1018 ++NumPHIsElim;
Chris Lattner1a8943a2011-01-15 07:29:01 +00001019 return true;
Cameron Zwarichc0611012011-01-06 02:37:26 +00001020 }
Chris Lattner1a8943a2011-01-15 07:29:01 +00001021 return false;
1022 }
1023
1024 if (CastInst *CI = dyn_cast<CastInst>(I)) {
Cameron Zwarichc0611012011-01-06 02:37:26 +00001025 // If the source of the cast is a constant, then this should have
1026 // already been constant folded. The only reason NOT to constant fold
1027 // it is if something (e.g. LSR) was careful to place the constant
1028 // evaluation in a block other than then one that uses it (e.g. to hoist
1029 // the address of globals out of a loop). If this is the case, we don't
1030 // want to forward-subst the cast.
1031 if (isa<Constant>(CI->getOperand(0)))
1032 return false;
1033
Chris Lattner1a8943a2011-01-15 07:29:01 +00001034 if (TLI && OptimizeNoopCopyExpression(CI, *TLI))
1035 return true;
Cameron Zwarichc0611012011-01-06 02:37:26 +00001036
Chris Lattner1a8943a2011-01-15 07:29:01 +00001037 if (isa<ZExtInst>(I) || isa<SExtInst>(I)) {
1038 bool MadeChange = MoveExtToFormExtLoad(I);
1039 return MadeChange | OptimizeExtUses(I);
Cameron Zwarichc0611012011-01-06 02:37:26 +00001040 }
Chris Lattner1a8943a2011-01-15 07:29:01 +00001041 return false;
1042 }
1043
1044 if (CmpInst *CI = dyn_cast<CmpInst>(I))
1045 return OptimizeCmpExpression(CI);
1046
1047 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Cameron Zwarichc0611012011-01-06 02:37:26 +00001048 if (TLI)
Chris Lattner1a8943a2011-01-15 07:29:01 +00001049 return OptimizeMemoryInst(I, I->getOperand(0), LI->getType());
1050 return false;
1051 }
1052
1053 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Cameron Zwarichc0611012011-01-06 02:37:26 +00001054 if (TLI)
Chris Lattner1a8943a2011-01-15 07:29:01 +00001055 return OptimizeMemoryInst(I, SI->getOperand(1),
1056 SI->getOperand(0)->getType());
1057 return false;
1058 }
1059
1060 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
Cameron Zwarich865ae1a2011-01-06 02:44:52 +00001061 if (GEPI->hasAllZeroIndices()) {
1062 /// The GEP operand must be a pointer, so must its result -> BitCast
1063 Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
1064 GEPI->getName(), GEPI);
1065 GEPI->replaceAllUsesWith(NC);
1066 GEPI->eraseFromParent();
1067 ++NumGEPsElim;
Cameron Zwarich865ae1a2011-01-06 02:44:52 +00001068 OptimizeInst(NC);
Chris Lattner1a8943a2011-01-15 07:29:01 +00001069 return true;
Cameron Zwarich865ae1a2011-01-06 02:44:52 +00001070 }
Chris Lattner1a8943a2011-01-15 07:29:01 +00001071 return false;
Cameron Zwarichc0611012011-01-06 02:37:26 +00001072 }
Chris Lattner1a8943a2011-01-15 07:29:01 +00001073
1074 if (CallInst *CI = dyn_cast<CallInst>(I))
1075 return OptimizeCallInst(CI);
Cameron Zwarichc0611012011-01-06 02:37:26 +00001076
Chris Lattner1a8943a2011-01-15 07:29:01 +00001077 return false;
Cameron Zwarichc0611012011-01-06 02:37:26 +00001078}
1079
Chris Lattnerdbe0dec2007-03-31 04:06:36 +00001080// In this pass we look for GEP and cast instructions that are used
1081// across basic blocks and rewrite them to improve basic-block-at-a-time
1082// selection.
1083bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) {
1084 bool MadeChange = false;
Eric Christopher692bf6b2008-09-24 05:32:41 +00001085
Evan Chengab631522008-12-19 18:03:11 +00001086 // Split all critical edges where the dest block has a PHI.
Evan Chenge1bcb442010-08-17 01:34:49 +00001087 if (CriticalEdgeSplit) {
1088 TerminatorInst *BBTI = BB.getTerminator();
1089 if (BBTI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(BBTI)) {
1090 for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i) {
1091 BasicBlock *SuccBB = BBTI->getSuccessor(i);
1092 if (isa<PHINode>(SuccBB->begin()) && isCriticalEdge(BBTI, i, true))
1093 SplitEdgeNicely(BBTI, i, BackEdges, this);
1094 }
Evan Chengab631522008-12-19 18:03:11 +00001095 }
Chris Lattnerdbe0dec2007-03-31 04:06:36 +00001096 }
Eric Christopher692bf6b2008-09-24 05:32:41 +00001097
Cameron Zwarich8c3527e2011-01-06 00:42:50 +00001098 SunkAddrs.clear();
Eric Christopher692bf6b2008-09-24 05:32:41 +00001099
Chris Lattner75796092011-01-15 07:14:54 +00001100 CurInstIterator = BB.begin();
Chris Lattner94e8e0c2011-01-15 07:25:29 +00001101 for (BasicBlock::iterator E = BB.end(); CurInstIterator != E; )
1102 MadeChange |= OptimizeInst(CurInstIterator++);
Eric Christopher692bf6b2008-09-24 05:32:41 +00001103
Chris Lattnerdbe0dec2007-03-31 04:06:36 +00001104 return MadeChange;
1105}