blob: bb969a635ec673ab6c2a192e25c6a4760eca7a54 [file] [log] [blame]
Hal Finkel99f823f2012-06-08 15:38:21 +00001//===-- PPCCTRLoops.cpp - Identify and generate CTR loops -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass identifies loops where we can generate the PPC branch instructions
11// that decrement and test the count register (CTR) (bdnz and friends).
Hal Finkel99f823f2012-06-08 15:38:21 +000012//
13// The pattern that defines the induction variable can changed depending on
14// prior optimizations. For example, the IndVarSimplify phase run by 'opt'
15// normalizes induction variables, and the Loop Strength Reduction pass
16// run by 'llc' may also make changes to the induction variable.
Hal Finkel99f823f2012-06-08 15:38:21 +000017//
18// Criteria for CTR loops:
19// - Countable loops (w/ ind. var for a trip count)
Hal Finkel99f823f2012-06-08 15:38:21 +000020// - Try inner-most loops first
21// - No nested CTR loops.
22// - No function calls in loops.
23//
Hal Finkel99f823f2012-06-08 15:38:21 +000024//===----------------------------------------------------------------------===//
25
26#define DEBUG_TYPE "ctrloops"
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000027
28#include "llvm/Transforms/Scalar.h"
Hal Finkel99f823f2012-06-08 15:38:21 +000029#include "llvm/ADT/Statistic.h"
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000030#include "llvm/ADT/STLExtras.h"
31#include "llvm/Analysis/Dominators.h"
32#include "llvm/Analysis/LoopInfo.h"
33#include "llvm/Analysis/ScalarEvolutionExpander.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000034#include "llvm/IR/Constants.h"
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000035#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000039#include "llvm/PassSupport.h"
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000040#include "llvm/Support/CommandLine.h"
Hal Finkel99f823f2012-06-08 15:38:21 +000041#include "llvm/Support/Debug.h"
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000042#include "llvm/Support/ValueHandle.h"
Hal Finkel99f823f2012-06-08 15:38:21 +000043#include "llvm/Support/raw_ostream.h"
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000044#include "llvm/Transforms/Utils/BasicBlockUtils.h"
45#include "llvm/Transforms/Utils/Local.h"
46#include "llvm/Target/TargetLibraryInfo.h"
47#include "PPCTargetMachine.h"
48#include "PPC.h"
49
Hal Finkel99f823f2012-06-08 15:38:21 +000050#include <algorithm>
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000051#include <vector>
Hal Finkel99f823f2012-06-08 15:38:21 +000052
53using namespace llvm;
54
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000055#ifndef NDEBUG
56static cl::opt<int> CTRLoopLimit("ppc-max-ctrloop", cl::Hidden, cl::init(-1));
57#endif
58
Hal Finkel99f823f2012-06-08 15:38:21 +000059STATISTIC(NumCTRLoops, "Number of loops converted to CTR loops");
60
Krzysztof Parzyszek96848df2013-02-13 17:40:07 +000061namespace llvm {
62 void initializePPCCTRLoopsPass(PassRegistry&);
63}
64
Hal Finkel99f823f2012-06-08 15:38:21 +000065namespace {
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000066 struct PPCCTRLoops : public FunctionPass {
67
68#ifndef NDEBUG
69 static int Counter;
70#endif
Hal Finkel99f823f2012-06-08 15:38:21 +000071
72 public:
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000073 static char ID;
Hal Finkel99f823f2012-06-08 15:38:21 +000074
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000075 PPCCTRLoops() : FunctionPass(ID), TM(0) {
76 initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry());
77 }
78 PPCCTRLoops(PPCTargetMachine &TM) : FunctionPass(ID), TM(&TM) {
Krzysztof Parzyszek96848df2013-02-13 17:40:07 +000079 initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry());
80 }
Hal Finkel99f823f2012-06-08 15:38:21 +000081
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000082 virtual bool runOnFunction(Function &F);
Hal Finkel99f823f2012-06-08 15:38:21 +000083
84 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000085 AU.addRequired<LoopInfo>();
86 AU.addPreserved<LoopInfo>();
87 AU.addRequired<DominatorTree>();
88 AU.addPreserved<DominatorTree>();
89 AU.addRequired<ScalarEvolution>();
Hal Finkel99f823f2012-06-08 15:38:21 +000090 }
91
92 private:
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000093 // FIXME: Copied from LoopSimplify.
94 BasicBlock *InsertPreheaderForLoop(Loop *L);
95 void PlaceSplitBlockCarefully(BasicBlock *NewBB,
96 SmallVectorImpl<BasicBlock*> &SplitPreds,
97 Loop *L);
Hal Finkel99f823f2012-06-08 15:38:21 +000098
Hal Finkelb1fd3cd2013-05-15 21:37:41 +000099 bool convertToCTRLoop(Loop *L);
100 private:
101 PPCTargetMachine *TM;
102 LoopInfo *LI;
103 ScalarEvolution *SE;
104 DataLayout *TD;
105 DominatorTree *DT;
106 const TargetLibraryInfo *LibInfo;
Hal Finkel99f823f2012-06-08 15:38:21 +0000107 };
108
109 char PPCCTRLoops::ID = 0;
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000110#ifndef NDEBUG
111 int PPCCTRLoops::Counter = 0;
112#endif
Hal Finkel99f823f2012-06-08 15:38:21 +0000113} // end anonymous namespace
114
Krzysztof Parzyszek96848df2013-02-13 17:40:07 +0000115INITIALIZE_PASS_BEGIN(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops",
116 false, false)
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000117INITIALIZE_PASS_DEPENDENCY(DominatorTree)
118INITIALIZE_PASS_DEPENDENCY(LoopInfo)
119INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
Krzysztof Parzyszek96848df2013-02-13 17:40:07 +0000120INITIALIZE_PASS_END(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops",
121 false, false)
Hal Finkel99f823f2012-06-08 15:38:21 +0000122
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000123FunctionPass *llvm::createPPCCTRLoops(PPCTargetMachine &TM) {
124 return new PPCCTRLoops(TM);
Hal Finkel99f823f2012-06-08 15:38:21 +0000125}
126
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000127bool PPCCTRLoops::runOnFunction(Function &F) {
128 LI = &getAnalysis<LoopInfo>();
129 SE = &getAnalysis<ScalarEvolution>();
130 DT = &getAnalysis<DominatorTree>();
131 TD = getAnalysisIfAvailable<DataLayout>();
132 LibInfo = getAnalysisIfAvailable<TargetLibraryInfo>();
Hal Finkel99f823f2012-06-08 15:38:21 +0000133
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000134 bool MadeChange = false;
Hal Finkel99f823f2012-06-08 15:38:21 +0000135
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000136 for (LoopInfo::iterator I = LI->begin(), E = LI->end();
Hal Finkel99f823f2012-06-08 15:38:21 +0000137 I != E; ++I) {
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000138 Loop *L = *I;
139 if (!L->getParentLoop())
140 MadeChange |= convertToCTRLoop(L);
Hal Finkel99f823f2012-06-08 15:38:21 +0000141 }
142
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000143 return MadeChange;
Hal Finkel99f823f2012-06-08 15:38:21 +0000144}
145
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000146bool PPCCTRLoops::convertToCTRLoop(Loop *L) {
147 bool MadeChange = false;
Hal Finkel99f823f2012-06-08 15:38:21 +0000148
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000149 Triple TT = Triple(L->getHeader()->getParent()->getParent()->
150 getTargetTriple());
151 if (!TT.isArch32Bit() && !TT.isArch64Bit())
152 return MadeChange; // Unknown arch. type.
Hal Finkel99f823f2012-06-08 15:38:21 +0000153
Hal Finkel99f823f2012-06-08 15:38:21 +0000154 // Process nested loops first.
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000155 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) {
156 MadeChange |= convertToCTRLoop(*I);
Hal Finkel99f823f2012-06-08 15:38:21 +0000157 }
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000158
Hal Finkel99f823f2012-06-08 15:38:21 +0000159 // If a nested loop has been converted, then we can't convert this loop.
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000160 if (MadeChange)
161 return MadeChange;
Hal Finkel99f823f2012-06-08 15:38:21 +0000162
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000163#ifndef NDEBUG
164 // Stop trying after reaching the limit (if any).
165 int Limit = CTRLoopLimit;
166 if (Limit >= 0) {
167 if (Counter >= CTRLoopLimit)
Hal Finkel9887ec32013-03-18 17:40:44 +0000168 return false;
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000169 Counter++;
Hal Finkel9887ec32013-03-18 17:40:44 +0000170 }
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000171#endif
Hal Finkel9887ec32013-03-18 17:40:44 +0000172
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000173 // We don't want to spill/restore the counter register, and so we don't
174 // want to use the counter register if the loop contains calls.
175 for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
176 I != IE; ++I) {
177 for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end();
178 J != JE; ++J) {
179 if (CallInst *CI = dyn_cast<CallInst>(J)) {
180 if (!TM)
181 return MadeChange;
182 const TargetLowering *TLI = TM->getTargetLowering();
Hal Finkel99f823f2012-06-08 15:38:21 +0000183
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000184 if (Function *F = CI->getCalledFunction()) {
185 // Most intrinsics don't become function calls, but some might.
186 // sin, cos, exp and log are always calls.
187 unsigned Opcode;
188 if (F->getIntrinsicID() != Intrinsic::not_intrinsic) {
189 switch (F->getIntrinsicID()) {
190 default: continue;
191 case Intrinsic::setjmp:
192 case Intrinsic::longjmp:
193 case Intrinsic::memcpy:
194 case Intrinsic::memmove:
195 case Intrinsic::memset:
196 case Intrinsic::powi:
197 case Intrinsic::log:
198 case Intrinsic::log2:
199 case Intrinsic::log10:
200 case Intrinsic::exp:
201 case Intrinsic::exp2:
202 case Intrinsic::pow:
203 case Intrinsic::sin:
204 case Intrinsic::cos:
205 return MadeChange;
206 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
207 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
208 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
209 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
210 case Intrinsic::rint: Opcode = ISD::FRINT; break;
211 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
212 }
213 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000214
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000215 // PowerPC does not use [US]DIVREM or other library calls for
216 // operations on regular types which are not otherwise library calls
217 // (i.e. soft float or atomics). If adapting for targets that do,
218 // additional care is required here.
Hal Finkel99f823f2012-06-08 15:38:21 +0000219
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000220 LibFunc::Func Func;
221 if (!F->hasLocalLinkage() && F->hasName() && LibInfo &&
222 LibInfo->getLibFunc(F->getName(), Func) &&
223 LibInfo->hasOptimizedCodeGen(Func)) {
224 // Non-read-only functions are never treated as intrinsics.
225 if (!CI->onlyReadsMemory())
226 return MadeChange;
227
228 // Conversion happens only for FP calls.
229 if (!CI->getArgOperand(0)->getType()->isFloatingPointTy())
230 return MadeChange;
231
232 switch (Func) {
233 default: return MadeChange;
234 case LibFunc::copysign:
235 case LibFunc::copysignf:
236 case LibFunc::copysignl:
237 continue; // ISD::FCOPYSIGN is never a library call.
238 case LibFunc::fabs:
239 case LibFunc::fabsf:
240 case LibFunc::fabsl:
241 continue; // ISD::FABS is never a library call.
242 case LibFunc::sqrt:
243 case LibFunc::sqrtf:
244 case LibFunc::sqrtl:
245 Opcode = ISD::FSQRT; break;
246 case LibFunc::floor:
247 case LibFunc::floorf:
248 case LibFunc::floorl:
249 Opcode = ISD::FFLOOR; break;
250 case LibFunc::nearbyint:
251 case LibFunc::nearbyintf:
252 case LibFunc::nearbyintl:
253 Opcode = ISD::FNEARBYINT; break;
254 case LibFunc::ceil:
255 case LibFunc::ceilf:
256 case LibFunc::ceill:
257 Opcode = ISD::FCEIL; break;
258 case LibFunc::rint:
259 case LibFunc::rintf:
260 case LibFunc::rintl:
261 Opcode = ISD::FRINT; break;
262 case LibFunc::trunc:
263 case LibFunc::truncf:
264 case LibFunc::truncl:
265 Opcode = ISD::FTRUNC; break;
266 }
267
268 MVT VTy =
269 TLI->getSimpleValueType(CI->getArgOperand(0)->getType(), true);
270 if (VTy == MVT::Other)
271 return MadeChange;
272
273 if (TLI->isOperationLegalOrCustom(Opcode, VTy))
274 continue;
275 else if (VTy.isVector() &&
276 TLI->isOperationLegalOrCustom(Opcode, VTy.getScalarType()))
277 continue;
278
279 return MadeChange;
280 }
281 }
282
283 return MadeChange;
284 } else if (isa<BinaryOperator>(J) &&
285 J->getType()->getScalarType()->isPPC_FP128Ty()) {
286 // Most operations on ppc_f128 values become calls.
287 return MadeChange;
288 } else if (isa<UIToFPInst>(J) || isa<SIToFPInst>(J) ||
289 isa<FPToUIInst>(J) || isa<FPToSIInst>(J)) {
290 CastInst *CI = cast<CastInst>(J);
291 if (CI->getSrcTy()->getScalarType()->isPPC_FP128Ty() ||
292 CI->getDestTy()->getScalarType()->isPPC_FP128Ty())
293 return MadeChange;
294 } else if (isa<IndirectBrInst>(J) || isa<InvokeInst>(J)) {
295 // On PowerPC, indirect jumps use the counter register.
296 return MadeChange;
297 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(J)) {
298 if (!TM)
299 return MadeChange;
300 const TargetLowering *TLI = TM->getTargetLowering();
301
302 if (TLI->supportJumpTables() &&
303 SI->getNumCases()+1 >= (unsigned) TLI->getMinimumJumpTableEntries())
304 return MadeChange;
305 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000306 }
307 }
308
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000309 SmallVector<BasicBlock*, 4> ExitingBlocks;
310 L->getExitingBlocks(ExitingBlocks);
Hal Finkel99f823f2012-06-08 15:38:21 +0000311
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000312 BasicBlock *CountedExitBlock = 0;
313 const SCEV *ExitCount = 0;
314 BranchInst *CountedExitBranch = 0;
315 for (SmallVector<BasicBlock*, 4>::iterator I = ExitingBlocks.begin(),
316 IE = ExitingBlocks.end(); I != IE; ++I) {
317 const SCEV *EC = SE->getExitCount(L, *I);
318 DEBUG(dbgs() << "Exit Count for " << *L << " from block " <<
319 (*I)->getName() << ": " << *EC << "\n");
320 if (isa<SCEVCouldNotCompute>(EC))
321 continue;
322 if (const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
323 if (ConstEC->getValue()->isZero())
324 continue;
325 } else if (!SE->isLoopInvariant(EC, L))
326 continue;
Hal Finkel99f823f2012-06-08 15:38:21 +0000327
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000328 // We now have a loop-invariant count of loop iterations (which is not the
329 // constant zero) for which we know that this loop will not exit via this
330 // exisiting block.
Hal Finkel2741d2c2012-06-16 20:34:07 +0000331
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000332 // We need to make sure that this block will run on every loop iteration.
333 // For this to be true, we must dominate all blocks with backedges. Such
334 // blocks are in-loop predecessors to the header block.
335 bool NotAlways = false;
336 for (pred_iterator PI = pred_begin(L->getHeader()),
337 PIE = pred_end(L->getHeader()); PI != PIE; ++PI) {
338 if (!L->contains(*PI))
339 continue;
340
341 if (!DT->dominates(*I, *PI)) {
342 NotAlways = true;
343 break;
344 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000345 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000346
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000347 if (NotAlways)
348 continue;
Hal Finkel99f823f2012-06-08 15:38:21 +0000349
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000350 // Make sure this blocks ends with a conditional branch.
351 Instruction *TI = (*I)->getTerminator();
352 if (!TI)
353 continue;
354
355 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
356 if (!BI->isConditional())
357 continue;
358
359 CountedExitBranch = BI;
360 } else
361 continue;
362
363 // Note that this block may not be the loop latch block, even if the loop
364 // has a latch block.
365 CountedExitBlock = *I;
366 ExitCount = EC;
367 break;
Hal Finkel99f823f2012-06-08 15:38:21 +0000368 }
369
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000370 if (!CountedExitBlock)
371 return MadeChange;
Hal Finkel99f823f2012-06-08 15:38:21 +0000372
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000373 BasicBlock *Preheader = L->getLoopPreheader();
374 if (!Preheader)
375 Preheader = InsertPreheaderForLoop(L);
376 if (!Preheader)
377 return MadeChange;
Hal Finkel99f823f2012-06-08 15:38:21 +0000378
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000379 DEBUG(dbgs() << "Preheader for exit count: " << Preheader->getName() << "\n");
Hal Finkel99f823f2012-06-08 15:38:21 +0000380
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000381 // Insert the count into the preheader and replace the condition used by the
382 // selected branch.
383 MadeChange = true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000384
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000385 SCEVExpander SCEVE(*SE, "loopcnt");
386 LLVMContext &C = SE->getContext();
387 Type *CountType = TT.isArch64Bit() ? Type::getInt64Ty(C) :
388 Type::getInt32Ty(C);
389 if (!ExitCount->getType()->isPointerTy() &&
390 ExitCount->getType() != CountType)
391 ExitCount = SE->getZeroExtendExpr(ExitCount, CountType);
392 ExitCount = SE->getAddExpr(ExitCount,
393 SE->getConstant(CountType, 1));
394 Value *ECValue = SCEVE.expandCodeFor(ExitCount, CountType,
395 Preheader->getTerminator());
Hal Finkel99f823f2012-06-08 15:38:21 +0000396
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000397 IRBuilder<> CountBuilder(Preheader->getTerminator());
398 Module *M = Preheader->getParent()->getParent();
399 Value *MTCTRFunc = Intrinsic::getDeclaration(M, Intrinsic::ppc_mtctr,
400 CountType);
401 CountBuilder.CreateCall(MTCTRFunc, ECValue);
Hal Finkel99f823f2012-06-08 15:38:21 +0000402
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000403 IRBuilder<> CondBuilder(CountedExitBranch);
404 Value *DecFunc =
405 Intrinsic::getDeclaration(M, Intrinsic::ppc_is_decremented_ctr_nonzero);
406 Value *NewCond = CondBuilder.CreateCall(DecFunc);
407 Value *OldCond = CountedExitBranch->getCondition();
408 CountedExitBranch->setCondition(NewCond);
409
410 // The false branch must exit the loop.
411 if (!L->contains(CountedExitBranch->getSuccessor(0)))
412 CountedExitBranch->swapSuccessors();
413
414 // The old condition may be dead now, and may have even created a dead PHI
415 // (the original induction variable).
416 RecursivelyDeleteTriviallyDeadInstructions(OldCond);
417 DeleteDeadPHIs(CountedExitBlock);
Hal Finkel99f823f2012-06-08 15:38:21 +0000418
419 ++NumCTRLoops;
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000420 return MadeChange;
421}
422
423// FIXME: Copied from LoopSimplify.
424BasicBlock *PPCCTRLoops::InsertPreheaderForLoop(Loop *L) {
425 BasicBlock *Header = L->getHeader();
426
427 // Compute the set of predecessors of the loop that are not in the loop.
428 SmallVector<BasicBlock*, 8> OutsideBlocks;
429 for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
430 PI != PE; ++PI) {
431 BasicBlock *P = *PI;
432 if (!L->contains(P)) { // Coming in from outside the loop?
433 // If the loop is branched to from an indirect branch, we won't
434 // be able to fully transform the loop, because it prohibits
435 // edge splitting.
436 if (isa<IndirectBrInst>(P->getTerminator())) return 0;
437
438 // Keep track of it.
439 OutsideBlocks.push_back(P);
440 }
441 }
442
443 // Split out the loop pre-header.
444 BasicBlock *PreheaderBB;
445 if (!Header->isLandingPad()) {
446 PreheaderBB = SplitBlockPredecessors(Header, OutsideBlocks, ".preheader",
447 this);
448 } else {
449 SmallVector<BasicBlock*, 2> NewBBs;
450 SplitLandingPadPredecessors(Header, OutsideBlocks, ".preheader",
451 ".split-lp", this, NewBBs);
452 PreheaderBB = NewBBs[0];
453 }
454
455 PreheaderBB->getTerminator()->setDebugLoc(
456 Header->getFirstNonPHI()->getDebugLoc());
457 DEBUG(dbgs() << "Creating pre-header "
458 << PreheaderBB->getName() << "\n");
459
460 // Make sure that NewBB is put someplace intelligent, which doesn't mess up
461 // code layout too horribly.
462 PlaceSplitBlockCarefully(PreheaderBB, OutsideBlocks, L);
463
464 return PreheaderBB;
465}
466
467void PPCCTRLoops::PlaceSplitBlockCarefully(BasicBlock *NewBB,
468 SmallVectorImpl<BasicBlock*> &SplitPreds,
469 Loop *L) {
470 // Check to see if NewBB is already well placed.
471 Function::iterator BBI = NewBB; --BBI;
472 for (unsigned i = 0, e = SplitPreds.size(); i != e; ++i) {
473 if (&*BBI == SplitPreds[i])
474 return;
475 }
476
477 // If it isn't already after an outside block, move it after one. This is
478 // always good as it makes the uncond branch from the outside block into a
479 // fall-through.
480
481 // Figure out *which* outside block to put this after. Prefer an outside
482 // block that neighbors a BB actually in the loop.
483 BasicBlock *FoundBB = 0;
484 for (unsigned i = 0, e = SplitPreds.size(); i != e; ++i) {
485 Function::iterator BBI = SplitPreds[i];
486 if (++BBI != NewBB->getParent()->end() &&
487 L->contains(BBI)) {
488 FoundBB = SplitPreds[i];
489 break;
490 }
491 }
492
493 // If our heuristic for a *good* bb to place this after doesn't find
494 // anything, just pick something. It's likely better than leaving it within
495 // the loop.
496 if (!FoundBB)
497 FoundBB = SplitPreds[0];
498 NewBB->moveAfter(FoundBB);
Hal Finkel99f823f2012-06-08 15:38:21 +0000499}
500