Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 1 | //===-- 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 12 | // |
| 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 17 | // |
| 18 | // Criteria for CTR loops: |
| 19 | // - Countable loops (w/ ind. var for a trip count) |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 20 | // - Try inner-most loops first |
| 21 | // - No nested CTR loops. |
| 22 | // - No function calls in loops. |
| 23 | // |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | #define DEBUG_TYPE "ctrloops" |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 27 | |
| 28 | #include "llvm/Transforms/Scalar.h" |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 30 | #include "llvm/ADT/STLExtras.h" |
| 31 | #include "llvm/Analysis/Dominators.h" |
| 32 | #include "llvm/Analysis/LoopInfo.h" |
| 33 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Constants.h" |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 35 | #include "llvm/IR/DerivedTypes.h" |
| 36 | #include "llvm/IR/Instructions.h" |
| 37 | #include "llvm/IR/IntrinsicInst.h" |
| 38 | #include "llvm/IR/Module.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/PassSupport.h" |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 40 | #include "llvm/Support/CommandLine.h" |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 42 | #include "llvm/Support/ValueHandle.h" |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 44 | #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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 50 | #include <algorithm> |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 51 | #include <vector> |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 52 | |
| 53 | using namespace llvm; |
| 54 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 55 | #ifndef NDEBUG |
| 56 | static cl::opt<int> CTRLoopLimit("ppc-max-ctrloop", cl::Hidden, cl::init(-1)); |
| 57 | #endif |
| 58 | |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 59 | STATISTIC(NumCTRLoops, "Number of loops converted to CTR loops"); |
| 60 | |
Krzysztof Parzyszek | 96848df | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 61 | namespace llvm { |
| 62 | void initializePPCCTRLoopsPass(PassRegistry&); |
| 63 | } |
| 64 | |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 65 | namespace { |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 66 | struct PPCCTRLoops : public FunctionPass { |
| 67 | |
| 68 | #ifndef NDEBUG |
| 69 | static int Counter; |
| 70 | #endif |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 71 | |
| 72 | public: |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 73 | static char ID; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 74 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 75 | PPCCTRLoops() : FunctionPass(ID), TM(0) { |
| 76 | initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry()); |
| 77 | } |
| 78 | PPCCTRLoops(PPCTargetMachine &TM) : FunctionPass(ID), TM(&TM) { |
Krzysztof Parzyszek | 96848df | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 79 | initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry()); |
| 80 | } |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 81 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 82 | virtual bool runOnFunction(Function &F); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 83 | |
| 84 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 85 | AU.addRequired<LoopInfo>(); |
| 86 | AU.addPreserved<LoopInfo>(); |
| 87 | AU.addRequired<DominatorTree>(); |
| 88 | AU.addPreserved<DominatorTree>(); |
| 89 | AU.addRequired<ScalarEvolution>(); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private: |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 93 | // FIXME: Copied from LoopSimplify. |
| 94 | BasicBlock *InsertPreheaderForLoop(Loop *L); |
| 95 | void PlaceSplitBlockCarefully(BasicBlock *NewBB, |
| 96 | SmallVectorImpl<BasicBlock*> &SplitPreds, |
| 97 | Loop *L); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 98 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 99 | 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | char PPCCTRLoops::ID = 0; |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 110 | #ifndef NDEBUG |
| 111 | int PPCCTRLoops::Counter = 0; |
| 112 | #endif |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 113 | } // end anonymous namespace |
| 114 | |
Krzysztof Parzyszek | 96848df | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 115 | INITIALIZE_PASS_BEGIN(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops", |
| 116 | false, false) |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 117 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 118 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 119 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
Krzysztof Parzyszek | 96848df | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 120 | INITIALIZE_PASS_END(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops", |
| 121 | false, false) |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 122 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 123 | FunctionPass *llvm::createPPCCTRLoops(PPCTargetMachine &TM) { |
| 124 | return new PPCCTRLoops(TM); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 127 | bool 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 133 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 134 | bool MadeChange = false; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 135 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 136 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 137 | I != E; ++I) { |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 138 | Loop *L = *I; |
| 139 | if (!L->getParentLoop()) |
| 140 | MadeChange |= convertToCTRLoop(L); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 143 | return MadeChange; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 146 | bool PPCCTRLoops::convertToCTRLoop(Loop *L) { |
| 147 | bool MadeChange = false; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 148 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 149 | Triple TT = Triple(L->getHeader()->getParent()->getParent()-> |
| 150 | getTargetTriple()); |
| 151 | if (!TT.isArch32Bit() && !TT.isArch64Bit()) |
| 152 | return MadeChange; // Unknown arch. type. |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 153 | |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 154 | // Process nested loops first. |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 155 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) { |
| 156 | MadeChange |= convertToCTRLoop(*I); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 157 | } |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 158 | |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 159 | // If a nested loop has been converted, then we can't convert this loop. |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 160 | if (MadeChange) |
| 161 | return MadeChange; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 162 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 163 | #ifndef NDEBUG |
| 164 | // Stop trying after reaching the limit (if any). |
| 165 | int Limit = CTRLoopLimit; |
| 166 | if (Limit >= 0) { |
| 167 | if (Counter >= CTRLoopLimit) |
Hal Finkel | 9887ec3 | 2013-03-18 17:40:44 +0000 | [diff] [blame] | 168 | return false; |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 169 | Counter++; |
Hal Finkel | 9887ec3 | 2013-03-18 17:40:44 +0000 | [diff] [blame] | 170 | } |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 171 | #endif |
Hal Finkel | 9887ec3 | 2013-03-18 17:40:44 +0000 | [diff] [blame] | 172 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 173 | // 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 183 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 184 | 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 214 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 215 | // 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 219 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 220 | 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 309 | SmallVector<BasicBlock*, 4> ExitingBlocks; |
| 310 | L->getExitingBlocks(ExitingBlocks); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 311 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 312 | 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 327 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 328 | // 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 Finkel | 2741d2c | 2012-06-16 20:34:07 +0000 | [diff] [blame] | 331 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 332 | // 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 345 | } |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 346 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 347 | if (NotAlways) |
| 348 | continue; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 349 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 350 | // 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 370 | if (!CountedExitBlock) |
| 371 | return MadeChange; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 372 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 373 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 374 | if (!Preheader) |
| 375 | Preheader = InsertPreheaderForLoop(L); |
| 376 | if (!Preheader) |
| 377 | return MadeChange; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 378 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 379 | DEBUG(dbgs() << "Preheader for exit count: " << Preheader->getName() << "\n"); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 380 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 381 | // Insert the count into the preheader and replace the condition used by the |
| 382 | // selected branch. |
| 383 | MadeChange = true; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 384 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 385 | 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 396 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 397 | 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 402 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 403 | 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 418 | |
| 419 | ++NumCTRLoops; |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame^] | 420 | return MadeChange; |
| 421 | } |
| 422 | |
| 423 | // FIXME: Copied from LoopSimplify. |
| 424 | BasicBlock *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 | |
| 467 | void 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 Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 499 | } |
| 500 | |