Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1 | //===- LoopInterchange.cpp - Loop interchange pass------------------------===// |
| 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 handles loop interchange transform. |
| 11 | // This pass interchanges loops to provide a more cache-friendly memory access |
| 12 | // patterns. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AssumptionCache.h" |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
| 20 | #include "llvm/Analysis/CodeMetrics.h" |
| 21 | #include "llvm/Analysis/DependenceAnalysis.h" |
| 22 | #include "llvm/Analysis/LoopInfo.h" |
| 23 | #include "llvm/Analysis/LoopIterator.h" |
| 24 | #include "llvm/Analysis/LoopPass.h" |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ScalarEvolution.h" |
| 27 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 28 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 29 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 30 | #include "llvm/Analysis/ValueTracking.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Dominators.h" |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Function.h" |
| 33 | #include "llvm/IR/IRBuilder.h" |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 34 | #include "llvm/IR/InstIterator.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 35 | #include "llvm/IR/IntrinsicInst.h" |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Module.h" |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 37 | #include "llvm/Pass.h" |
| 38 | #include "llvm/Support/Debug.h" |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 40 | #include "llvm/Transforms/Scalar.h" |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Davide Italiano | 9d8f6f8 | 2017-01-29 01:55:24 +0000 | [diff] [blame] | 43 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
| 46 | #define DEBUG_TYPE "loop-interchange" |
| 47 | |
Chad Rosier | 7243189 | 2016-09-14 17:07:13 +0000 | [diff] [blame] | 48 | static cl::opt<int> LoopInterchangeCostThreshold( |
| 49 | "loop-interchange-threshold", cl::init(0), cl::Hidden, |
| 50 | cl::desc("Interchange if you gain more than this number")); |
| 51 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 52 | namespace { |
| 53 | |
| 54 | typedef SmallVector<Loop *, 8> LoopVector; |
| 55 | |
| 56 | // TODO: Check if we can use a sparse matrix here. |
| 57 | typedef std::vector<std::vector<char>> CharMatrix; |
| 58 | |
| 59 | // Maximum number of dependencies that can be handled in the dependency matrix. |
| 60 | static const unsigned MaxMemInstrCount = 100; |
| 61 | |
| 62 | // Maximum loop depth supported. |
| 63 | static const unsigned MaxLoopNestDepth = 10; |
| 64 | |
| 65 | struct LoopInterchange; |
| 66 | |
| 67 | #ifdef DUMP_DEP_MATRICIES |
| 68 | void printDepMatrix(CharMatrix &DepMatrix) { |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 69 | for (auto &Row : DepMatrix) { |
| 70 | for (auto D : Row) |
| 71 | DEBUG(dbgs() << D << " "); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 72 | DEBUG(dbgs() << "\n"); |
| 73 | } |
| 74 | } |
| 75 | #endif |
| 76 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 77 | static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level, |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 78 | Loop *L, DependenceInfo *DI) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 79 | typedef SmallVector<Value *, 16> ValueVector; |
| 80 | ValueVector MemInstr; |
| 81 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 82 | // For each block. |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 83 | for (BasicBlock *BB : L->blocks()) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 84 | // Scan the BB and collect legal loads and stores. |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 85 | for (Instruction &I : *BB) { |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 86 | if (!isa<Instruction>(I)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 87 | return false; |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 88 | if (auto *Ld = dyn_cast<LoadInst>(&I)) { |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 89 | if (!Ld->isSimple()) |
| 90 | return false; |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 91 | MemInstr.push_back(&I); |
| 92 | } else if (auto *St = dyn_cast<StoreInst>(&I)) { |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 93 | if (!St->isSimple()) |
| 94 | return false; |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 95 | MemInstr.push_back(&I); |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 96 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | DEBUG(dbgs() << "Found " << MemInstr.size() |
| 101 | << " Loads and Stores to analyze\n"); |
| 102 | |
| 103 | ValueVector::iterator I, IE, J, JE; |
| 104 | |
| 105 | for (I = MemInstr.begin(), IE = MemInstr.end(); I != IE; ++I) { |
| 106 | for (J = I, JE = MemInstr.end(); J != JE; ++J) { |
| 107 | std::vector<char> Dep; |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 108 | Instruction *Src = cast<Instruction>(*I); |
| 109 | Instruction *Dst = cast<Instruction>(*J); |
Chad Rosier | 90bcb91 | 2016-09-07 16:07:17 +0000 | [diff] [blame] | 110 | if (Src == Dst) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 111 | continue; |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 112 | // Ignore Input dependencies. |
Chad Rosier | 90bcb91 | 2016-09-07 16:07:17 +0000 | [diff] [blame] | 113 | if (isa<LoadInst>(Src) && isa<LoadInst>(Dst)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 114 | continue; |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 115 | // Track Output, Flow, and Anti dependencies. |
Chad Rosier | 90bcb91 | 2016-09-07 16:07:17 +0000 | [diff] [blame] | 116 | if (auto D = DI->depends(Src, Dst, true)) { |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 117 | assert(D->isOrdered() && "Expected an output, flow or anti dep."); |
| 118 | DEBUG(StringRef DepType = |
| 119 | D->isFlow() ? "flow" : D->isAnti() ? "anti" : "output"; |
| 120 | dbgs() << "Found " << DepType |
| 121 | << " dependency between Src and Dst\n" |
Chad Rosier | 90bcb91 | 2016-09-07 16:07:17 +0000 | [diff] [blame] | 122 | << " Src:" << *Src << "\n Dst:" << *Dst << '\n'); |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 123 | unsigned Levels = D->getLevels(); |
| 124 | char Direction; |
| 125 | for (unsigned II = 1; II <= Levels; ++II) { |
| 126 | const SCEV *Distance = D->getDistance(II); |
| 127 | const SCEVConstant *SCEVConst = |
| 128 | dyn_cast_or_null<SCEVConstant>(Distance); |
| 129 | if (SCEVConst) { |
| 130 | const ConstantInt *CI = SCEVConst->getValue(); |
| 131 | if (CI->isNegative()) |
| 132 | Direction = '<'; |
| 133 | else if (CI->isZero()) |
| 134 | Direction = '='; |
| 135 | else |
| 136 | Direction = '>'; |
| 137 | Dep.push_back(Direction); |
| 138 | } else if (D->isScalar(II)) { |
| 139 | Direction = 'S'; |
| 140 | Dep.push_back(Direction); |
| 141 | } else { |
| 142 | unsigned Dir = D->getDirection(II); |
| 143 | if (Dir == Dependence::DVEntry::LT || |
| 144 | Dir == Dependence::DVEntry::LE) |
| 145 | Direction = '<'; |
| 146 | else if (Dir == Dependence::DVEntry::GT || |
| 147 | Dir == Dependence::DVEntry::GE) |
| 148 | Direction = '>'; |
| 149 | else if (Dir == Dependence::DVEntry::EQ) |
| 150 | Direction = '='; |
| 151 | else |
| 152 | Direction = '*'; |
| 153 | Dep.push_back(Direction); |
| 154 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 155 | } |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 156 | while (Dep.size() != Level) { |
| 157 | Dep.push_back('I'); |
| 158 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 159 | |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 160 | DepMatrix.push_back(Dep); |
| 161 | if (DepMatrix.size() > MaxMemInstrCount) { |
| 162 | DEBUG(dbgs() << "Cannot handle more than " << MaxMemInstrCount |
| 163 | << " dependencies inside loop\n"); |
| 164 | return false; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Vikram TV | 74b4111 | 2015-12-09 05:16:24 +0000 | [diff] [blame] | 170 | // We don't have a DepMatrix to check legality return false. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 171 | if (DepMatrix.size() == 0) |
| 172 | return false; |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | // A loop is moved from index 'from' to an index 'to'. Update the Dependence |
| 177 | // matrix by exchanging the two columns. |
Chad Rosier | d18ea06 | 2016-09-13 13:00:29 +0000 | [diff] [blame] | 178 | static void interChangeDependencies(CharMatrix &DepMatrix, unsigned FromIndx, |
| 179 | unsigned ToIndx) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 180 | unsigned numRows = DepMatrix.size(); |
| 181 | for (unsigned i = 0; i < numRows; ++i) { |
| 182 | char TmpVal = DepMatrix[i][ToIndx]; |
| 183 | DepMatrix[i][ToIndx] = DepMatrix[i][FromIndx]; |
| 184 | DepMatrix[i][FromIndx] = TmpVal; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Checks if outermost non '=','S'or'I' dependence in the dependence matrix is |
| 189 | // '>' |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 190 | static bool isOuterMostDepPositive(CharMatrix &DepMatrix, unsigned Row, |
| 191 | unsigned Column) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 192 | for (unsigned i = 0; i <= Column; ++i) { |
| 193 | if (DepMatrix[Row][i] == '<') |
| 194 | return false; |
| 195 | if (DepMatrix[Row][i] == '>') |
| 196 | return true; |
| 197 | } |
| 198 | // All dependencies were '=','S' or 'I' |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | // Checks if no dependence exist in the dependency matrix in Row before Column. |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 203 | static bool containsNoDependence(CharMatrix &DepMatrix, unsigned Row, |
| 204 | unsigned Column) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 205 | for (unsigned i = 0; i < Column; ++i) { |
Chandler Carruth | fca1ff0 | 2016-11-03 16:39:25 +0000 | [diff] [blame] | 206 | if (DepMatrix[Row][i] != '=' && DepMatrix[Row][i] != 'S' && |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 207 | DepMatrix[Row][i] != 'I') |
| 208 | return false; |
| 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 213 | static bool validDepInterchange(CharMatrix &DepMatrix, unsigned Row, |
| 214 | unsigned OuterLoopId, char InnerDep, |
| 215 | char OuterDep) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 216 | |
| 217 | if (isOuterMostDepPositive(DepMatrix, Row, OuterLoopId)) |
| 218 | return false; |
| 219 | |
| 220 | if (InnerDep == OuterDep) |
| 221 | return true; |
| 222 | |
| 223 | // It is legal to interchange if and only if after interchange no row has a |
| 224 | // '>' direction as the leftmost non-'='. |
| 225 | |
| 226 | if (InnerDep == '=' || InnerDep == 'S' || InnerDep == 'I') |
| 227 | return true; |
| 228 | |
| 229 | if (InnerDep == '<') |
| 230 | return true; |
| 231 | |
| 232 | if (InnerDep == '>') { |
| 233 | // If OuterLoopId represents outermost loop then interchanging will make the |
| 234 | // 1st dependency as '>' |
| 235 | if (OuterLoopId == 0) |
| 236 | return false; |
| 237 | |
| 238 | // If all dependencies before OuterloopId are '=','S'or 'I'. Then |
| 239 | // interchanging will result in this row having an outermost non '=' |
| 240 | // dependency of '>' |
| 241 | if (!containsNoDependence(DepMatrix, Row, OuterLoopId)) |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | // Checks if it is legal to interchange 2 loops. |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 249 | // [Theorem] A permutation of the loops in a perfect nest is legal if and only |
Chad Rosier | 61683a2 | 2016-09-13 13:08:53 +0000 | [diff] [blame] | 250 | // if the direction matrix, after the same permutation is applied to its |
| 251 | // columns, has no ">" direction as the leftmost non-"=" direction in any row. |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 252 | static bool isLegalToInterChangeLoops(CharMatrix &DepMatrix, |
| 253 | unsigned InnerLoopId, |
| 254 | unsigned OuterLoopId) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 255 | |
| 256 | unsigned NumRows = DepMatrix.size(); |
| 257 | // For each row check if it is valid to interchange. |
| 258 | for (unsigned Row = 0; Row < NumRows; ++Row) { |
| 259 | char InnerDep = DepMatrix[Row][InnerLoopId]; |
| 260 | char OuterDep = DepMatrix[Row][OuterLoopId]; |
| 261 | if (InnerDep == '*' || OuterDep == '*') |
| 262 | return false; |
Chad Rosier | 61683a2 | 2016-09-13 13:08:53 +0000 | [diff] [blame] | 263 | if (!validDepInterchange(DepMatrix, Row, OuterLoopId, InnerDep, OuterDep)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | static void populateWorklist(Loop &L, SmallVector<LoopVector, 8> &V) { |
| 270 | |
Chad Rosier | f5814f5 | 2016-09-07 15:56:59 +0000 | [diff] [blame] | 271 | DEBUG(dbgs() << "Calling populateWorklist on Func: " |
| 272 | << L.getHeader()->getParent()->getName() << " Loop: %" |
| 273 | << L.getHeader()->getName() << '\n'); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 274 | LoopVector LoopList; |
| 275 | Loop *CurrentLoop = &L; |
Benjamin Kramer | e448b5b | 2015-07-13 17:21:14 +0000 | [diff] [blame] | 276 | const std::vector<Loop *> *Vec = &CurrentLoop->getSubLoops(); |
| 277 | while (!Vec->empty()) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 278 | // The current loop has multiple subloops in it hence it is not tightly |
| 279 | // nested. |
| 280 | // Discard all loops above it added into Worklist. |
Benjamin Kramer | e448b5b | 2015-07-13 17:21:14 +0000 | [diff] [blame] | 281 | if (Vec->size() != 1) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 282 | LoopList.clear(); |
| 283 | return; |
| 284 | } |
| 285 | LoopList.push_back(CurrentLoop); |
Benjamin Kramer | e448b5b | 2015-07-13 17:21:14 +0000 | [diff] [blame] | 286 | CurrentLoop = Vec->front(); |
| 287 | Vec = &CurrentLoop->getSubLoops(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 288 | } |
| 289 | LoopList.push_back(CurrentLoop); |
Benjamin Kramer | e448b5b | 2015-07-13 17:21:14 +0000 | [diff] [blame] | 290 | V.push_back(std::move(LoopList)); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static PHINode *getInductionVariable(Loop *L, ScalarEvolution *SE) { |
| 294 | PHINode *InnerIndexVar = L->getCanonicalInductionVariable(); |
| 295 | if (InnerIndexVar) |
| 296 | return InnerIndexVar; |
| 297 | if (L->getLoopLatch() == nullptr || L->getLoopPredecessor() == nullptr) |
| 298 | return nullptr; |
| 299 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) { |
| 300 | PHINode *PhiVar = cast<PHINode>(I); |
| 301 | Type *PhiTy = PhiVar->getType(); |
| 302 | if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() && |
| 303 | !PhiTy->isPointerTy()) |
| 304 | return nullptr; |
| 305 | const SCEVAddRecExpr *AddRec = |
| 306 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(PhiVar)); |
| 307 | if (!AddRec || !AddRec->isAffine()) |
| 308 | continue; |
| 309 | const SCEV *Step = AddRec->getStepRecurrence(*SE); |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 310 | if (!isa<SCEVConstant>(Step)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 311 | continue; |
| 312 | // Found the induction variable. |
| 313 | // FIXME: Handle loops with more than one induction variable. Note that, |
| 314 | // currently, legality makes sure we have only one induction variable. |
| 315 | return PhiVar; |
| 316 | } |
| 317 | return nullptr; |
| 318 | } |
| 319 | |
| 320 | /// LoopInterchangeLegality checks if it is legal to interchange the loop. |
| 321 | class LoopInterchangeLegality { |
| 322 | public: |
| 323 | LoopInterchangeLegality(Loop *Outer, Loop *Inner, ScalarEvolution *SE, |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 324 | LoopInfo *LI, DominatorTree *DT, bool PreserveLCSSA, |
| 325 | OptimizationRemarkEmitter *ORE) |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 326 | : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT), |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 327 | PreserveLCSSA(PreserveLCSSA), ORE(ORE), InnerLoopHasReduction(false) {} |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 328 | |
| 329 | /// Check if the loops can be interchanged. |
| 330 | bool canInterchangeLoops(unsigned InnerLoopId, unsigned OuterLoopId, |
| 331 | CharMatrix &DepMatrix); |
| 332 | /// Check if the loop structure is understood. We do not handle triangular |
| 333 | /// loops for now. |
| 334 | bool isLoopStructureUnderstood(PHINode *InnerInductionVar); |
| 335 | |
| 336 | bool currentLimitations(); |
| 337 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 338 | bool hasInnerLoopReduction() { return InnerLoopHasReduction; } |
| 339 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 340 | private: |
| 341 | bool tightlyNested(Loop *Outer, Loop *Inner); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 342 | bool containsUnsafeInstructionsInHeader(BasicBlock *BB); |
| 343 | bool areAllUsesReductions(Instruction *Ins, Loop *L); |
| 344 | bool containsUnsafeInstructionsInLatch(BasicBlock *BB); |
| 345 | bool findInductionAndReductions(Loop *L, |
| 346 | SmallVector<PHINode *, 8> &Inductions, |
| 347 | SmallVector<PHINode *, 8> &Reductions); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 348 | Loop *OuterLoop; |
| 349 | Loop *InnerLoop; |
| 350 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 351 | ScalarEvolution *SE; |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 352 | LoopInfo *LI; |
| 353 | DominatorTree *DT; |
| 354 | bool PreserveLCSSA; |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 355 | /// Interface to emit optimization remarks. |
| 356 | OptimizationRemarkEmitter *ORE; |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 357 | |
| 358 | bool InnerLoopHasReduction; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | /// LoopInterchangeProfitability checks if it is profitable to interchange the |
| 362 | /// loop. |
| 363 | class LoopInterchangeProfitability { |
| 364 | public: |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 365 | LoopInterchangeProfitability(Loop *Outer, Loop *Inner, ScalarEvolution *SE, |
| 366 | OptimizationRemarkEmitter *ORE) |
| 367 | : OuterLoop(Outer), InnerLoop(Inner), SE(SE), ORE(ORE) {} |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 368 | |
Vikram TV | 74b4111 | 2015-12-09 05:16:24 +0000 | [diff] [blame] | 369 | /// Check if the loop interchange is profitable. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 370 | bool isProfitable(unsigned InnerLoopId, unsigned OuterLoopId, |
| 371 | CharMatrix &DepMatrix); |
| 372 | |
| 373 | private: |
| 374 | int getInstrOrderCost(); |
| 375 | |
| 376 | Loop *OuterLoop; |
| 377 | Loop *InnerLoop; |
| 378 | |
| 379 | /// Scev analysis. |
| 380 | ScalarEvolution *SE; |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 381 | /// Interface to emit optimization remarks. |
| 382 | OptimizationRemarkEmitter *ORE; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
Vikram TV | 74b4111 | 2015-12-09 05:16:24 +0000 | [diff] [blame] | 385 | /// LoopInterchangeTransform interchanges the loop. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 386 | class LoopInterchangeTransform { |
| 387 | public: |
| 388 | LoopInterchangeTransform(Loop *Outer, Loop *Inner, ScalarEvolution *SE, |
| 389 | LoopInfo *LI, DominatorTree *DT, |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 390 | BasicBlock *LoopNestExit, |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 391 | bool InnerLoopContainsReductions) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 392 | : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT), |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 393 | LoopExit(LoopNestExit), |
| 394 | InnerLoopHasReduction(InnerLoopContainsReductions) {} |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 395 | |
| 396 | /// Interchange OuterLoop and InnerLoop. |
| 397 | bool transform(); |
| 398 | void restructureLoops(Loop *InnerLoop, Loop *OuterLoop); |
| 399 | void removeChildLoop(Loop *OuterLoop, Loop *InnerLoop); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 400 | |
| 401 | private: |
| 402 | void splitInnerLoopLatch(Instruction *); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 403 | void splitInnerLoopHeader(); |
| 404 | bool adjustLoopLinks(); |
| 405 | void adjustLoopPreheaders(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 406 | bool adjustLoopBranches(); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 407 | void updateIncomingBlock(BasicBlock *CurrBlock, BasicBlock *OldPred, |
| 408 | BasicBlock *NewPred); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 409 | |
| 410 | Loop *OuterLoop; |
| 411 | Loop *InnerLoop; |
| 412 | |
| 413 | /// Scev analysis. |
| 414 | ScalarEvolution *SE; |
| 415 | LoopInfo *LI; |
| 416 | DominatorTree *DT; |
| 417 | BasicBlock *LoopExit; |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 418 | bool InnerLoopHasReduction; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 419 | }; |
| 420 | |
Vikram TV | 74b4111 | 2015-12-09 05:16:24 +0000 | [diff] [blame] | 421 | // Main LoopInterchange Pass. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 422 | struct LoopInterchange : public FunctionPass { |
| 423 | static char ID; |
| 424 | ScalarEvolution *SE; |
| 425 | LoopInfo *LI; |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 426 | DependenceInfo *DI; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 427 | DominatorTree *DT; |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 428 | bool PreserveLCSSA; |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 429 | /// Interface to emit optimization remarks. |
| 430 | OptimizationRemarkEmitter *ORE; |
| 431 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 432 | LoopInterchange() |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 433 | : FunctionPass(ID), SE(nullptr), LI(nullptr), DI(nullptr), DT(nullptr) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 434 | initializeLoopInterchangePass(*PassRegistry::getPassRegistry()); |
| 435 | } |
| 436 | |
| 437 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 438 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 439 | AU.addRequired<AAResultsWrapperPass>(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 440 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 441 | AU.addRequired<LoopInfoWrapperPass>(); |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 442 | AU.addRequired<DependenceAnalysisWrapperPass>(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 443 | AU.addRequiredID(LoopSimplifyID); |
| 444 | AU.addRequiredID(LCSSAID); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 445 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 449 | if (skipFunction(F)) |
| 450 | return false; |
| 451 | |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 452 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 453 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 454 | DI = &getAnalysis<DependenceAnalysisWrapperPass>().getDI(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 455 | auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 456 | DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 457 | ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 458 | PreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 459 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 460 | // Build up a worklist of loop pairs to analyze. |
| 461 | SmallVector<LoopVector, 8> Worklist; |
| 462 | |
| 463 | for (Loop *L : *LI) |
| 464 | populateWorklist(*L, Worklist); |
| 465 | |
Chad Rosier | a4c4246 | 2016-09-12 13:24:47 +0000 | [diff] [blame] | 466 | DEBUG(dbgs() << "Worklist size = " << Worklist.size() << "\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 467 | bool Changed = true; |
| 468 | while (!Worklist.empty()) { |
| 469 | LoopVector LoopList = Worklist.pop_back_val(); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 470 | Changed = processLoopList(LoopList, F); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 471 | } |
| 472 | return Changed; |
| 473 | } |
| 474 | |
| 475 | bool isComputableLoopNest(LoopVector LoopList) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 476 | for (Loop *L : LoopList) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 477 | const SCEV *ExitCountOuter = SE->getBackedgeTakenCount(L); |
| 478 | if (ExitCountOuter == SE->getCouldNotCompute()) { |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 479 | DEBUG(dbgs() << "Couldn't compute backedge count\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 480 | return false; |
| 481 | } |
| 482 | if (L->getNumBackEdges() != 1) { |
| 483 | DEBUG(dbgs() << "NumBackEdges is not equal to 1\n"); |
| 484 | return false; |
| 485 | } |
| 486 | if (!L->getExitingBlock()) { |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 487 | DEBUG(dbgs() << "Loop doesn't have unique exit block\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 488 | return false; |
| 489 | } |
| 490 | } |
| 491 | return true; |
| 492 | } |
| 493 | |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 494 | unsigned selectLoopForInterchange(const LoopVector &LoopList) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 495 | // TODO: Add a better heuristic to select the loop to be interchanged based |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 496 | // on the dependence matrix. Currently we select the innermost loop. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 497 | return LoopList.size() - 1; |
| 498 | } |
| 499 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 500 | bool processLoopList(LoopVector LoopList, Function &F) { |
| 501 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 502 | bool Changed = false; |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 503 | unsigned LoopNestDepth = LoopList.size(); |
| 504 | if (LoopNestDepth < 2) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 505 | DEBUG(dbgs() << "Loop doesn't contain minimum nesting level.\n"); |
| 506 | return false; |
| 507 | } |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 508 | if (LoopNestDepth > MaxLoopNestDepth) { |
| 509 | DEBUG(dbgs() << "Cannot handle loops of depth greater than " |
| 510 | << MaxLoopNestDepth << "\n"); |
| 511 | return false; |
| 512 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 513 | if (!isComputableLoopNest(LoopList)) { |
Chad Rosier | a4c4246 | 2016-09-12 13:24:47 +0000 | [diff] [blame] | 514 | DEBUG(dbgs() << "Not valid loop candidate for interchange\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 515 | return false; |
| 516 | } |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 517 | |
| 518 | DEBUG(dbgs() << "Processing LoopList of size = " << LoopNestDepth << "\n"); |
| 519 | |
| 520 | CharMatrix DependencyMatrix; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 521 | Loop *OuterMostLoop = *(LoopList.begin()); |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 522 | if (!populateDependencyMatrix(DependencyMatrix, LoopNestDepth, |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 523 | OuterMostLoop, DI)) { |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 524 | DEBUG(dbgs() << "Populating dependency matrix failed\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 525 | return false; |
| 526 | } |
| 527 | #ifdef DUMP_DEP_MATRICIES |
Chad Rosier | 58ede27 | 2016-09-14 16:43:19 +0000 | [diff] [blame] | 528 | DEBUG(dbgs() << "Dependence before interchange\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 529 | printDepMatrix(DependencyMatrix); |
| 530 | #endif |
| 531 | |
| 532 | BasicBlock *OuterMostLoopLatch = OuterMostLoop->getLoopLatch(); |
| 533 | BranchInst *OuterMostLoopLatchBI = |
| 534 | dyn_cast<BranchInst>(OuterMostLoopLatch->getTerminator()); |
| 535 | if (!OuterMostLoopLatchBI) |
| 536 | return false; |
| 537 | |
| 538 | // Since we currently do not handle LCSSA PHI's any failure in loop |
| 539 | // condition will now branch to LoopNestExit. |
| 540 | // TODO: This should be removed once we handle LCSSA PHI nodes. |
| 541 | |
| 542 | // Get the Outermost loop exit. |
| 543 | BasicBlock *LoopNestExit; |
| 544 | if (OuterMostLoopLatchBI->getSuccessor(0) == OuterMostLoop->getHeader()) |
| 545 | LoopNestExit = OuterMostLoopLatchBI->getSuccessor(1); |
| 546 | else |
| 547 | LoopNestExit = OuterMostLoopLatchBI->getSuccessor(0); |
| 548 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 549 | if (isa<PHINode>(LoopNestExit->begin())) { |
| 550 | DEBUG(dbgs() << "PHI Nodes in loop nest exit is not handled for now " |
| 551 | "since on failure all loops branch to loop nest exit.\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 552 | return false; |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 553 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 554 | |
| 555 | unsigned SelecLoopId = selectLoopForInterchange(LoopList); |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 556 | // Move the selected loop outwards to the best possible position. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 557 | for (unsigned i = SelecLoopId; i > 0; i--) { |
| 558 | bool Interchanged = |
| 559 | processLoop(LoopList, i, i - 1, LoopNestExit, DependencyMatrix); |
| 560 | if (!Interchanged) |
| 561 | return Changed; |
| 562 | // Loops interchanged reflect the same in LoopList |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 563 | std::swap(LoopList[i - 1], LoopList[i]); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 564 | |
| 565 | // Update the DependencyMatrix |
Chad Rosier | d18ea06 | 2016-09-13 13:00:29 +0000 | [diff] [blame] | 566 | interChangeDependencies(DependencyMatrix, i, i - 1); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 567 | DT->recalculate(F); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 568 | #ifdef DUMP_DEP_MATRICIES |
Chad Rosier | 58ede27 | 2016-09-14 16:43:19 +0000 | [diff] [blame] | 569 | DEBUG(dbgs() << "Dependence after interchange\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 570 | printDepMatrix(DependencyMatrix); |
| 571 | #endif |
| 572 | Changed |= Interchanged; |
| 573 | } |
| 574 | return Changed; |
| 575 | } |
| 576 | |
| 577 | bool processLoop(LoopVector LoopList, unsigned InnerLoopId, |
| 578 | unsigned OuterLoopId, BasicBlock *LoopNestExit, |
| 579 | std::vector<std::vector<char>> &DependencyMatrix) { |
| 580 | |
Chad Rosier | 13bc0d19 | 2016-09-07 18:15:12 +0000 | [diff] [blame] | 581 | DEBUG(dbgs() << "Processing Inner Loop Id = " << InnerLoopId |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 582 | << " and OuterLoopId = " << OuterLoopId << "\n"); |
| 583 | Loop *InnerLoop = LoopList[InnerLoopId]; |
| 584 | Loop *OuterLoop = LoopList[OuterLoopId]; |
| 585 | |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 586 | LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, LI, DT, |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 587 | PreserveLCSSA, ORE); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 588 | if (!LIL.canInterchangeLoops(InnerLoopId, OuterLoopId, DependencyMatrix)) { |
| 589 | DEBUG(dbgs() << "Not interchanging Loops. Cannot prove legality\n"); |
| 590 | return false; |
| 591 | } |
| 592 | DEBUG(dbgs() << "Loops are legal to interchange\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 593 | LoopInterchangeProfitability LIP(OuterLoop, InnerLoop, SE, ORE); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 594 | if (!LIP.isProfitable(InnerLoopId, OuterLoopId, DependencyMatrix)) { |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 595 | DEBUG(dbgs() << "Interchanging loops not profitable\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 596 | return false; |
| 597 | } |
| 598 | |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 599 | ORE->emit(OptimizationRemark(DEBUG_TYPE, "Interchanged", |
| 600 | InnerLoop->getStartLoc(), |
| 601 | InnerLoop->getHeader()) |
| 602 | << "Loop interchanged with enclosing loop."); |
| 603 | |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 604 | LoopInterchangeTransform LIT(OuterLoop, InnerLoop, SE, LI, DT, |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 605 | LoopNestExit, LIL.hasInnerLoopReduction()); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 606 | LIT.transform(); |
| 607 | DEBUG(dbgs() << "Loops interchanged\n"); |
| 608 | return true; |
| 609 | } |
| 610 | }; |
| 611 | |
| 612 | } // end of namespace |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 613 | bool LoopInterchangeLegality::areAllUsesReductions(Instruction *Ins, Loop *L) { |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 614 | return none_of(Ins->users(), [=](User *U) -> bool { |
| 615 | auto *UserIns = dyn_cast<PHINode>(U); |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 616 | RecurrenceDescriptor RD; |
| 617 | return !UserIns || !RecurrenceDescriptor::isReductionPHI(UserIns, L, RD); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 618 | }); |
| 619 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 620 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 621 | bool LoopInterchangeLegality::containsUnsafeInstructionsInHeader( |
| 622 | BasicBlock *BB) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 623 | for (auto I = BB->begin(), E = BB->end(); I != E; ++I) { |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 624 | // Load corresponding to reduction PHI's are safe while concluding if |
| 625 | // tightly nested. |
| 626 | if (LoadInst *L = dyn_cast<LoadInst>(I)) { |
| 627 | if (!areAllUsesReductions(L, InnerLoop)) |
| 628 | return true; |
| 629 | } else if (I->mayHaveSideEffects() || I->mayReadFromMemory()) |
| 630 | return true; |
| 631 | } |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | bool LoopInterchangeLegality::containsUnsafeInstructionsInLatch( |
| 636 | BasicBlock *BB) { |
| 637 | for (auto I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 638 | // Stores corresponding to reductions are safe while concluding if tightly |
| 639 | // nested. |
| 640 | if (StoreInst *L = dyn_cast<StoreInst>(I)) { |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 641 | if (!isa<PHINode>(L->getOperand(0))) |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 642 | return true; |
| 643 | } else if (I->mayHaveSideEffects() || I->mayReadFromMemory()) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 644 | return true; |
| 645 | } |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | bool LoopInterchangeLegality::tightlyNested(Loop *OuterLoop, Loop *InnerLoop) { |
| 650 | BasicBlock *OuterLoopHeader = OuterLoop->getHeader(); |
| 651 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 652 | BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch(); |
| 653 | |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 654 | DEBUG(dbgs() << "Checking if loops are tightly nested\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 655 | |
| 656 | // A perfectly nested loop will not have any branch in between the outer and |
| 657 | // inner block i.e. outer header will branch to either inner preheader and |
| 658 | // outerloop latch. |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 659 | BranchInst *OuterLoopHeaderBI = |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 660 | dyn_cast<BranchInst>(OuterLoopHeader->getTerminator()); |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 661 | if (!OuterLoopHeaderBI) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 662 | return false; |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 663 | |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 664 | for (BasicBlock *Succ : OuterLoopHeaderBI->successors()) |
| 665 | if (Succ != InnerLoopPreHeader && Succ != OuterLoopLatch) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 666 | return false; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 667 | |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 668 | DEBUG(dbgs() << "Checking instructions in Loop header and Loop latch\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 669 | // We do not have any basic block in between now make sure the outer header |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 670 | // and outer loop latch doesn't contain any unsafe instructions. |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 671 | if (containsUnsafeInstructionsInHeader(OuterLoopHeader) || |
| 672 | containsUnsafeInstructionsInLatch(OuterLoopLatch)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 673 | return false; |
| 674 | |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 675 | DEBUG(dbgs() << "Loops are perfectly nested\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 676 | // We have a perfect loop nest. |
| 677 | return true; |
| 678 | } |
| 679 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 680 | |
| 681 | bool LoopInterchangeLegality::isLoopStructureUnderstood( |
| 682 | PHINode *InnerInduction) { |
| 683 | |
| 684 | unsigned Num = InnerInduction->getNumOperands(); |
| 685 | BasicBlock *InnerLoopPreheader = InnerLoop->getLoopPreheader(); |
| 686 | for (unsigned i = 0; i < Num; ++i) { |
| 687 | Value *Val = InnerInduction->getOperand(i); |
| 688 | if (isa<Constant>(Val)) |
| 689 | continue; |
| 690 | Instruction *I = dyn_cast<Instruction>(Val); |
| 691 | if (!I) |
| 692 | return false; |
| 693 | // TODO: Handle triangular loops. |
| 694 | // e.g. for(int i=0;i<N;i++) |
| 695 | // for(int j=i;j<N;j++) |
| 696 | unsigned IncomBlockIndx = PHINode::getIncomingValueNumForOperand(i); |
| 697 | if (InnerInduction->getIncomingBlock(IncomBlockIndx) == |
| 698 | InnerLoopPreheader && |
| 699 | !OuterLoop->isLoopInvariant(I)) { |
| 700 | return false; |
| 701 | } |
| 702 | } |
| 703 | return true; |
| 704 | } |
| 705 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 706 | bool LoopInterchangeLegality::findInductionAndReductions( |
| 707 | Loop *L, SmallVector<PHINode *, 8> &Inductions, |
| 708 | SmallVector<PHINode *, 8> &Reductions) { |
| 709 | if (!L->getLoopLatch() || !L->getLoopPredecessor()) |
| 710 | return false; |
| 711 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) { |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 712 | RecurrenceDescriptor RD; |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 713 | InductionDescriptor ID; |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 714 | PHINode *PHI = cast<PHINode>(I); |
Elena Demikhovsky | 376a18b | 2016-07-24 07:24:54 +0000 | [diff] [blame] | 715 | if (InductionDescriptor::isInductionPHI(PHI, L, SE, ID)) |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 716 | Inductions.push_back(PHI); |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 717 | else if (RecurrenceDescriptor::isReductionPHI(PHI, L, RD)) |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 718 | Reductions.push_back(PHI); |
| 719 | else { |
| 720 | DEBUG( |
| 721 | dbgs() << "Failed to recognize PHI as an induction or reduction.\n"); |
| 722 | return false; |
| 723 | } |
| 724 | } |
| 725 | return true; |
| 726 | } |
| 727 | |
| 728 | static bool containsSafePHI(BasicBlock *Block, bool isOuterLoopExitBlock) { |
| 729 | for (auto I = Block->begin(); isa<PHINode>(I); ++I) { |
| 730 | PHINode *PHI = cast<PHINode>(I); |
| 731 | // Reduction lcssa phi will have only 1 incoming block that from loop latch. |
| 732 | if (PHI->getNumIncomingValues() > 1) |
| 733 | return false; |
| 734 | Instruction *Ins = dyn_cast<Instruction>(PHI->getIncomingValue(0)); |
| 735 | if (!Ins) |
| 736 | return false; |
| 737 | // Incoming value for lcssa phi's in outer loop exit can only be inner loop |
| 738 | // exits lcssa phi else it would not be tightly nested. |
| 739 | if (!isa<PHINode>(Ins) && isOuterLoopExitBlock) |
| 740 | return false; |
| 741 | } |
| 742 | return true; |
| 743 | } |
| 744 | |
| 745 | static BasicBlock *getLoopLatchExitBlock(BasicBlock *LatchBlock, |
| 746 | BasicBlock *LoopHeader) { |
| 747 | if (BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator())) { |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 748 | assert(BI->getNumSuccessors() == 2 && |
| 749 | "Branch leaving loop latch must have 2 successors"); |
| 750 | for (BasicBlock *Succ : BI->successors()) { |
| 751 | if (Succ == LoopHeader) |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 752 | continue; |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 753 | return Succ; |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | return nullptr; |
| 757 | } |
| 758 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 759 | // This function indicates the current limitations in the transform as a result |
| 760 | // of which we do not proceed. |
| 761 | bool LoopInterchangeLegality::currentLimitations() { |
| 762 | |
| 763 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 764 | BasicBlock *InnerLoopHeader = InnerLoop->getHeader(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 765 | BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch(); |
| 766 | BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch(); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 767 | BasicBlock *OuterLoopHeader = OuterLoop->getHeader(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 768 | |
| 769 | PHINode *InnerInductionVar; |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 770 | SmallVector<PHINode *, 8> Inductions; |
| 771 | SmallVector<PHINode *, 8> Reductions; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 772 | if (!findInductionAndReductions(InnerLoop, Inductions, Reductions)) { |
| 773 | DEBUG(dbgs() << "Only inner loops with induction or reduction PHI nodes " |
| 774 | << "are supported currently.\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 775 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 776 | "UnsupportedPHIInner", |
| 777 | InnerLoop->getStartLoc(), |
| 778 | InnerLoop->getHeader()) |
| 779 | << "Only inner loops with induction or reduction PHI nodes can be" |
| 780 | " interchange currently."); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 781 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 782 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 783 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 784 | // TODO: Currently we handle only loops with 1 induction variable. |
| 785 | if (Inductions.size() != 1) { |
| 786 | DEBUG(dbgs() << "We currently only support loops with 1 induction variable." |
| 787 | << "Failed to interchange due to current limitation\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 788 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 789 | "MultiInductionInner", |
| 790 | InnerLoop->getStartLoc(), |
| 791 | InnerLoop->getHeader()) |
| 792 | << "Only inner loops with 1 induction variable can be " |
| 793 | "interchanged currently."); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 794 | return true; |
| 795 | } |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 796 | if (Reductions.size() > 0) |
| 797 | InnerLoopHasReduction = true; |
| 798 | |
| 799 | InnerInductionVar = Inductions.pop_back_val(); |
| 800 | Reductions.clear(); |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 801 | if (!findInductionAndReductions(OuterLoop, Inductions, Reductions)) { |
| 802 | DEBUG(dbgs() << "Only outer loops with induction or reduction PHI nodes " |
| 803 | << "are supported currently.\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 804 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 805 | "UnsupportedPHIOuter", |
| 806 | OuterLoop->getStartLoc(), |
| 807 | OuterLoop->getHeader()) |
| 808 | << "Only outer loops with induction or reduction PHI nodes can be" |
| 809 | " interchanged currently."); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 810 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 811 | } |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 812 | |
| 813 | // Outer loop cannot have reduction because then loops will not be tightly |
| 814 | // nested. |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 815 | if (!Reductions.empty()) { |
| 816 | DEBUG(dbgs() << "Outer loops with reductions are not supported " |
| 817 | << "currently.\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 818 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 819 | "ReductionsOuter", |
| 820 | OuterLoop->getStartLoc(), |
| 821 | OuterLoop->getHeader()) |
| 822 | << "Outer loops with reductions cannot be interchangeed " |
| 823 | "currently."); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 824 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 825 | } |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 826 | // TODO: Currently we handle only loops with 1 induction variable. |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 827 | if (Inductions.size() != 1) { |
| 828 | DEBUG(dbgs() << "Loops with more than 1 induction variables are not " |
| 829 | << "supported currently.\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 830 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 831 | "MultiIndutionOuter", |
| 832 | OuterLoop->getStartLoc(), |
| 833 | OuterLoop->getHeader()) |
| 834 | << "Only outer loops with 1 induction variable can be " |
| 835 | "interchanged currently."); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 836 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 837 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 838 | |
| 839 | // TODO: Triangular loops are not handled for now. |
| 840 | if (!isLoopStructureUnderstood(InnerInductionVar)) { |
| 841 | DEBUG(dbgs() << "Loop structure not understood by pass\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 842 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 843 | "UnsupportedStructureInner", |
| 844 | InnerLoop->getStartLoc(), |
| 845 | InnerLoop->getHeader()) |
| 846 | << "Inner loop structure not understood currently."); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 847 | return true; |
| 848 | } |
| 849 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 850 | // TODO: We only handle LCSSA PHI's corresponding to reduction for now. |
| 851 | BasicBlock *LoopExitBlock = |
| 852 | getLoopLatchExitBlock(OuterLoopLatch, OuterLoopHeader); |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 853 | if (!LoopExitBlock || !containsSafePHI(LoopExitBlock, true)) { |
| 854 | DEBUG(dbgs() << "Can only handle LCSSA PHIs in outer loops currently.\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 855 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 856 | "NoLCSSAPHIOuter", |
| 857 | OuterLoop->getStartLoc(), |
| 858 | OuterLoop->getHeader()) |
| 859 | << "Only outer loops with LCSSA PHIs can be interchange " |
| 860 | "currently."); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 861 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 862 | } |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 863 | |
| 864 | LoopExitBlock = getLoopLatchExitBlock(InnerLoopLatch, InnerLoopHeader); |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 865 | if (!LoopExitBlock || !containsSafePHI(LoopExitBlock, false)) { |
| 866 | DEBUG(dbgs() << "Can only handle LCSSA PHIs in inner loops currently.\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 867 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 868 | "NoLCSSAPHIOuterInner", |
| 869 | InnerLoop->getStartLoc(), |
| 870 | InnerLoop->getHeader()) |
| 871 | << "Only inner loops with LCSSA PHIs can be interchange " |
| 872 | "currently."); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 873 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 874 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 875 | |
| 876 | // TODO: Current limitation: Since we split the inner loop latch at the point |
| 877 | // were induction variable is incremented (induction.next); We cannot have |
| 878 | // more than 1 user of induction.next since it would result in broken code |
| 879 | // after split. |
| 880 | // e.g. |
| 881 | // for(i=0;i<N;i++) { |
| 882 | // for(j = 0;j<M;j++) { |
| 883 | // A[j+1][i+2] = A[j][i]+k; |
| 884 | // } |
| 885 | // } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 886 | Instruction *InnerIndexVarInc = nullptr; |
| 887 | if (InnerInductionVar->getIncomingBlock(0) == InnerLoopPreHeader) |
| 888 | InnerIndexVarInc = |
| 889 | dyn_cast<Instruction>(InnerInductionVar->getIncomingValue(1)); |
| 890 | else |
| 891 | InnerIndexVarInc = |
| 892 | dyn_cast<Instruction>(InnerInductionVar->getIncomingValue(0)); |
| 893 | |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 894 | if (!InnerIndexVarInc) { |
| 895 | DEBUG(dbgs() << "Did not find an instruction to increment the induction " |
| 896 | << "variable.\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 897 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 898 | "NoIncrementInInner", |
| 899 | InnerLoop->getStartLoc(), |
| 900 | InnerLoop->getHeader()) |
| 901 | << "The inner loop does not increment the induction variable."); |
Pete Cooper | 11bd958 | 2015-07-27 18:37:58 +0000 | [diff] [blame] | 902 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 903 | } |
Pete Cooper | 11bd958 | 2015-07-27 18:37:58 +0000 | [diff] [blame] | 904 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 905 | // Since we split the inner loop latch on this induction variable. Make sure |
| 906 | // we do not have any instruction between the induction variable and branch |
| 907 | // instruction. |
| 908 | |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 909 | bool FoundInduction = false; |
| 910 | for (const Instruction &I : reverse(*InnerLoopLatch)) { |
Florian Hahn | cd78345 | 2017-08-25 16:52:29 +0000 | [diff] [blame^] | 911 | if (isa<BranchInst>(I) || isa<CmpInst>(I) || isa<TruncInst>(I) || |
| 912 | isa<ZExtInst>(I)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 913 | continue; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 914 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 915 | // We found an instruction. If this is not induction variable then it is not |
| 916 | // safe to split this loop latch. |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 917 | if (!I.isIdenticalTo(InnerIndexVarInc)) { |
| 918 | DEBUG(dbgs() << "Found unsupported instructions between induction " |
| 919 | << "variable increment and branch.\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 920 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 921 | "UnsupportedInsBetweenInduction", |
| 922 | InnerLoop->getStartLoc(), |
| 923 | InnerLoop->getHeader()) |
| 924 | << "Found unsupported instruction between induction variable " |
| 925 | "increment and branch."); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 926 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 927 | } |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 928 | |
| 929 | FoundInduction = true; |
| 930 | break; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 931 | } |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 932 | // The loop latch ended and we didn't find the induction variable return as |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 933 | // current limitation. |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 934 | if (!FoundInduction) { |
| 935 | DEBUG(dbgs() << "Did not find the induction variable.\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 936 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 937 | "NoIndutionVariable", |
| 938 | InnerLoop->getStartLoc(), |
| 939 | InnerLoop->getHeader()) |
| 940 | << "Did not find the induction variable."); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 941 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 942 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 943 | return false; |
| 944 | } |
| 945 | |
| 946 | bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId, |
| 947 | unsigned OuterLoopId, |
| 948 | CharMatrix &DepMatrix) { |
| 949 | |
| 950 | if (!isLegalToInterChangeLoops(DepMatrix, InnerLoopId, OuterLoopId)) { |
| 951 | DEBUG(dbgs() << "Failed interchange InnerLoopId = " << InnerLoopId |
Chad Rosier | 58ede27 | 2016-09-14 16:43:19 +0000 | [diff] [blame] | 952 | << " and OuterLoopId = " << OuterLoopId |
| 953 | << " due to dependence\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 954 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 955 | "Dependence", |
| 956 | InnerLoop->getStartLoc(), |
| 957 | InnerLoop->getHeader()) |
| 958 | << "Cannot interchange loops due to dependences."); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 959 | return false; |
| 960 | } |
| 961 | |
Florian Hahn | 4284049 | 2017-07-31 09:00:52 +0000 | [diff] [blame] | 962 | // Check if outer and inner loop contain legal instructions only. |
| 963 | for (auto *BB : OuterLoop->blocks()) |
| 964 | for (Instruction &I : *BB) |
| 965 | if (CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 966 | // readnone functions do not prevent interchanging. |
| 967 | if (CI->doesNotReadMemory()) |
| 968 | continue; |
| 969 | DEBUG(dbgs() << "Loops with call instructions cannot be interchanged " |
| 970 | << "safely."); |
| 971 | return false; |
| 972 | } |
| 973 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 974 | // Create unique Preheaders if we already do not have one. |
| 975 | BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader(); |
| 976 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 977 | |
| 978 | // Create a unique outer preheader - |
| 979 | // 1) If OuterLoop preheader is not present. |
| 980 | // 2) If OuterLoop Preheader is same as OuterLoop Header |
| 981 | // 3) If OuterLoop Preheader is same as Header of the previous loop. |
| 982 | // 4) If OuterLoop Preheader is Entry node. |
| 983 | if (!OuterLoopPreHeader || OuterLoopPreHeader == OuterLoop->getHeader() || |
| 984 | isa<PHINode>(OuterLoopPreHeader->begin()) || |
| 985 | !OuterLoopPreHeader->getUniquePredecessor()) { |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 986 | OuterLoopPreHeader = |
| 987 | InsertPreheaderForLoop(OuterLoop, DT, LI, PreserveLCSSA); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | if (!InnerLoopPreHeader || InnerLoopPreHeader == InnerLoop->getHeader() || |
| 991 | InnerLoopPreHeader == OuterLoop->getHeader()) { |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 992 | InnerLoopPreHeader = |
| 993 | InsertPreheaderForLoop(InnerLoop, DT, LI, PreserveLCSSA); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 996 | // TODO: The loops could not be interchanged due to current limitations in the |
| 997 | // transform module. |
| 998 | if (currentLimitations()) { |
| 999 | DEBUG(dbgs() << "Not legal because of current transform limitation\n"); |
| 1000 | return false; |
| 1001 | } |
| 1002 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1003 | // Check if the loops are tightly nested. |
| 1004 | if (!tightlyNested(OuterLoop, InnerLoop)) { |
| 1005 | DEBUG(dbgs() << "Loops not tightly nested\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 1006 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 1007 | "NotTightlyNested", |
| 1008 | InnerLoop->getStartLoc(), |
| 1009 | InnerLoop->getHeader()) |
| 1010 | << "Cannot interchange loops because they are not tightly " |
| 1011 | "nested."); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1012 | return false; |
| 1013 | } |
| 1014 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1015 | return true; |
| 1016 | } |
| 1017 | |
| 1018 | int LoopInterchangeProfitability::getInstrOrderCost() { |
| 1019 | unsigned GoodOrder, BadOrder; |
| 1020 | BadOrder = GoodOrder = 0; |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 1021 | for (BasicBlock *BB : InnerLoop->blocks()) { |
| 1022 | for (Instruction &Ins : *BB) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1023 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Ins)) { |
| 1024 | unsigned NumOp = GEP->getNumOperands(); |
| 1025 | bool FoundInnerInduction = false; |
| 1026 | bool FoundOuterInduction = false; |
| 1027 | for (unsigned i = 0; i < NumOp; ++i) { |
| 1028 | const SCEV *OperandVal = SE->getSCEV(GEP->getOperand(i)); |
| 1029 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(OperandVal); |
| 1030 | if (!AR) |
| 1031 | continue; |
| 1032 | |
| 1033 | // If we find the inner induction after an outer induction e.g. |
| 1034 | // for(int i=0;i<N;i++) |
| 1035 | // for(int j=0;j<N;j++) |
| 1036 | // A[i][j] = A[i-1][j-1]+k; |
| 1037 | // then it is a good order. |
| 1038 | if (AR->getLoop() == InnerLoop) { |
| 1039 | // We found an InnerLoop induction after OuterLoop induction. It is |
| 1040 | // a good order. |
| 1041 | FoundInnerInduction = true; |
| 1042 | if (FoundOuterInduction) { |
| 1043 | GoodOrder++; |
| 1044 | break; |
| 1045 | } |
| 1046 | } |
| 1047 | // If we find the outer induction after an inner induction e.g. |
| 1048 | // for(int i=0;i<N;i++) |
| 1049 | // for(int j=0;j<N;j++) |
| 1050 | // A[j][i] = A[j-1][i-1]+k; |
| 1051 | // then it is a bad order. |
| 1052 | if (AR->getLoop() == OuterLoop) { |
| 1053 | // We found an OuterLoop induction after InnerLoop induction. It is |
| 1054 | // a bad order. |
| 1055 | FoundOuterInduction = true; |
| 1056 | if (FoundInnerInduction) { |
| 1057 | BadOrder++; |
| 1058 | break; |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | return GoodOrder - BadOrder; |
| 1066 | } |
| 1067 | |
Chad Rosier | e6b3a63 | 2016-09-14 17:12:30 +0000 | [diff] [blame] | 1068 | static bool isProfitableForVectorization(unsigned InnerLoopId, |
| 1069 | unsigned OuterLoopId, |
| 1070 | CharMatrix &DepMatrix) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1071 | // TODO: Improve this heuristic to catch more cases. |
| 1072 | // If the inner loop is loop independent or doesn't carry any dependency it is |
| 1073 | // profitable to move this to outer position. |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 1074 | for (auto &Row : DepMatrix) { |
| 1075 | if (Row[InnerLoopId] != 'S' && Row[InnerLoopId] != 'I') |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1076 | return false; |
| 1077 | // TODO: We need to improve this heuristic. |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 1078 | if (Row[OuterLoopId] != '=') |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1079 | return false; |
| 1080 | } |
| 1081 | // If outer loop has dependence and inner loop is loop independent then it is |
| 1082 | // profitable to interchange to enable parallelism. |
| 1083 | return true; |
| 1084 | } |
| 1085 | |
| 1086 | bool LoopInterchangeProfitability::isProfitable(unsigned InnerLoopId, |
| 1087 | unsigned OuterLoopId, |
| 1088 | CharMatrix &DepMatrix) { |
| 1089 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1090 | // TODO: Add better profitability checks. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1091 | // e.g |
| 1092 | // 1) Construct dependency matrix and move the one with no loop carried dep |
| 1093 | // inside to enable vectorization. |
| 1094 | |
| 1095 | // This is rough cost estimation algorithm. It counts the good and bad order |
| 1096 | // of induction variables in the instruction and allows reordering if number |
| 1097 | // of bad orders is more than good. |
Chad Rosier | 7243189 | 2016-09-14 17:07:13 +0000 | [diff] [blame] | 1098 | int Cost = getInstrOrderCost(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1099 | DEBUG(dbgs() << "Cost = " << Cost << "\n"); |
Chad Rosier | 7243189 | 2016-09-14 17:07:13 +0000 | [diff] [blame] | 1100 | if (Cost < -LoopInterchangeCostThreshold) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1101 | return true; |
| 1102 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1103 | // It is not profitable as per current cache profitability model. But check if |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1104 | // we can move this loop outside to improve parallelism. |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 1105 | if (isProfitableForVectorization(InnerLoopId, OuterLoopId, DepMatrix)) |
| 1106 | return true; |
| 1107 | |
| 1108 | ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, |
| 1109 | "InterchangeNotProfitable", |
| 1110 | InnerLoop->getStartLoc(), |
| 1111 | InnerLoop->getHeader()) |
| 1112 | << "Interchanging loops is too costly (cost=" |
| 1113 | << ore::NV("Cost", Cost) << ", threshold=" |
| 1114 | << ore::NV("Threshold", LoopInterchangeCostThreshold) << |
| 1115 | ") and it does not improve parallelism."); |
| 1116 | return false; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | void LoopInterchangeTransform::removeChildLoop(Loop *OuterLoop, |
| 1120 | Loop *InnerLoop) { |
Daniel Jasper | 6adbd7a | 2015-03-06 10:39:14 +0000 | [diff] [blame] | 1121 | for (Loop::iterator I = OuterLoop->begin(), E = OuterLoop->end(); I != E; |
| 1122 | ++I) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1123 | if (*I == InnerLoop) { |
| 1124 | OuterLoop->removeChildLoop(I); |
| 1125 | return; |
| 1126 | } |
| 1127 | } |
Benjamin Kramer | 8ceb323 | 2015-10-25 22:28:27 +0000 | [diff] [blame] | 1128 | llvm_unreachable("Couldn't find loop"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1129 | } |
Daniel Jasper | 6adbd7a | 2015-03-06 10:39:14 +0000 | [diff] [blame] | 1130 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1131 | void LoopInterchangeTransform::restructureLoops(Loop *InnerLoop, |
| 1132 | Loop *OuterLoop) { |
| 1133 | Loop *OuterLoopParent = OuterLoop->getParentLoop(); |
| 1134 | if (OuterLoopParent) { |
| 1135 | // Remove the loop from its parent loop. |
| 1136 | removeChildLoop(OuterLoopParent, OuterLoop); |
| 1137 | removeChildLoop(OuterLoop, InnerLoop); |
| 1138 | OuterLoopParent->addChildLoop(InnerLoop); |
| 1139 | } else { |
| 1140 | removeChildLoop(OuterLoop, InnerLoop); |
| 1141 | LI->changeTopLevelLoop(OuterLoop, InnerLoop); |
| 1142 | } |
| 1143 | |
Andrew Kaylor | 08c5f1e | 2015-04-24 17:39:16 +0000 | [diff] [blame] | 1144 | while (!InnerLoop->empty()) |
| 1145 | OuterLoop->addChildLoop(InnerLoop->removeChildLoop(InnerLoop->begin())); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1146 | |
| 1147 | InnerLoop->addChildLoop(OuterLoop); |
| 1148 | } |
| 1149 | |
| 1150 | bool LoopInterchangeTransform::transform() { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1151 | bool Transformed = false; |
| 1152 | Instruction *InnerIndexVar; |
| 1153 | |
| 1154 | if (InnerLoop->getSubLoops().size() == 0) { |
| 1155 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 1156 | DEBUG(dbgs() << "Calling Split Inner Loop\n"); |
| 1157 | PHINode *InductionPHI = getInductionVariable(InnerLoop, SE); |
| 1158 | if (!InductionPHI) { |
| 1159 | DEBUG(dbgs() << "Failed to find the point to split loop latch \n"); |
| 1160 | return false; |
| 1161 | } |
| 1162 | |
| 1163 | if (InductionPHI->getIncomingBlock(0) == InnerLoopPreHeader) |
| 1164 | InnerIndexVar = dyn_cast<Instruction>(InductionPHI->getIncomingValue(1)); |
| 1165 | else |
| 1166 | InnerIndexVar = dyn_cast<Instruction>(InductionPHI->getIncomingValue(0)); |
| 1167 | |
| 1168 | // |
| 1169 | // Split at the place were the induction variable is |
| 1170 | // incremented/decremented. |
| 1171 | // TODO: This splitting logic may not work always. Fix this. |
| 1172 | splitInnerLoopLatch(InnerIndexVar); |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 1173 | DEBUG(dbgs() << "splitInnerLoopLatch done\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1174 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1175 | // Splits the inner loops phi nodes out into a separate basic block. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1176 | splitInnerLoopHeader(); |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 1177 | DEBUG(dbgs() << "splitInnerLoopHeader done\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | Transformed |= adjustLoopLinks(); |
| 1181 | if (!Transformed) { |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 1182 | DEBUG(dbgs() << "adjustLoopLinks failed\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1183 | return false; |
| 1184 | } |
| 1185 | |
| 1186 | restructureLoops(InnerLoop, OuterLoop); |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1190 | void LoopInterchangeTransform::splitInnerLoopLatch(Instruction *Inc) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1191 | BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1192 | BasicBlock *InnerLoopLatchPred = InnerLoopLatch; |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1193 | InnerLoopLatch = SplitBlock(InnerLoopLatchPred, Inc, DT, LI); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1196 | void LoopInterchangeTransform::splitInnerLoopHeader() { |
| 1197 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1198 | // Split the inner loop header out. Here make sure that the reduction PHI's |
| 1199 | // stay in the innerloop body. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1200 | BasicBlock *InnerLoopHeader = InnerLoop->getHeader(); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1201 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 1202 | if (InnerLoopHasReduction) { |
| 1203 | // FIXME: Check if the induction PHI will always be the first PHI. |
| 1204 | BasicBlock *New = InnerLoopHeader->splitBasicBlock( |
| 1205 | ++(InnerLoopHeader->begin()), InnerLoopHeader->getName() + ".split"); |
| 1206 | if (LI) |
| 1207 | if (Loop *L = LI->getLoopFor(InnerLoopHeader)) |
| 1208 | L->addBasicBlockToLoop(New, *LI); |
| 1209 | |
| 1210 | // Adjust Reduction PHI's in the block. |
| 1211 | SmallVector<PHINode *, 8> PHIVec; |
| 1212 | for (auto I = New->begin(); isa<PHINode>(I); ++I) { |
| 1213 | PHINode *PHI = dyn_cast<PHINode>(I); |
| 1214 | Value *V = PHI->getIncomingValueForBlock(InnerLoopPreHeader); |
| 1215 | PHI->replaceAllUsesWith(V); |
| 1216 | PHIVec.push_back((PHI)); |
| 1217 | } |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1218 | for (PHINode *P : PHIVec) { |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1219 | P->eraseFromParent(); |
| 1220 | } |
| 1221 | } else { |
| 1222 | SplitBlock(InnerLoopHeader, InnerLoopHeader->getFirstNonPHI(), DT, LI); |
| 1223 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1224 | |
| 1225 | DEBUG(dbgs() << "Output of splitInnerLoopHeader InnerLoopHeaderSucc & " |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 1226 | "InnerLoopHeader\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1229 | /// \brief Move all instructions except the terminator from FromBB right before |
| 1230 | /// InsertBefore |
| 1231 | static void moveBBContents(BasicBlock *FromBB, Instruction *InsertBefore) { |
| 1232 | auto &ToList = InsertBefore->getParent()->getInstList(); |
| 1233 | auto &FromList = FromBB->getInstList(); |
| 1234 | |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1235 | ToList.splice(InsertBefore->getIterator(), FromList, FromList.begin(), |
| 1236 | FromBB->getTerminator()->getIterator()); |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1239 | void LoopInterchangeTransform::updateIncomingBlock(BasicBlock *CurrBlock, |
| 1240 | BasicBlock *OldPred, |
| 1241 | BasicBlock *NewPred) { |
| 1242 | for (auto I = CurrBlock->begin(); isa<PHINode>(I); ++I) { |
| 1243 | PHINode *PHI = cast<PHINode>(I); |
| 1244 | unsigned Num = PHI->getNumIncomingValues(); |
| 1245 | for (unsigned i = 0; i < Num; ++i) { |
| 1246 | if (PHI->getIncomingBlock(i) == OldPred) |
| 1247 | PHI->setIncomingBlock(i, NewPred); |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1252 | bool LoopInterchangeTransform::adjustLoopBranches() { |
| 1253 | |
| 1254 | DEBUG(dbgs() << "adjustLoopBranches called\n"); |
| 1255 | // Adjust the loop preheader |
| 1256 | BasicBlock *InnerLoopHeader = InnerLoop->getHeader(); |
| 1257 | BasicBlock *OuterLoopHeader = OuterLoop->getHeader(); |
| 1258 | BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch(); |
| 1259 | BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch(); |
| 1260 | BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader(); |
| 1261 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 1262 | BasicBlock *OuterLoopPredecessor = OuterLoopPreHeader->getUniquePredecessor(); |
| 1263 | BasicBlock *InnerLoopLatchPredecessor = |
| 1264 | InnerLoopLatch->getUniquePredecessor(); |
| 1265 | BasicBlock *InnerLoopLatchSuccessor; |
| 1266 | BasicBlock *OuterLoopLatchSuccessor; |
| 1267 | |
| 1268 | BranchInst *OuterLoopLatchBI = |
| 1269 | dyn_cast<BranchInst>(OuterLoopLatch->getTerminator()); |
| 1270 | BranchInst *InnerLoopLatchBI = |
| 1271 | dyn_cast<BranchInst>(InnerLoopLatch->getTerminator()); |
| 1272 | BranchInst *OuterLoopHeaderBI = |
| 1273 | dyn_cast<BranchInst>(OuterLoopHeader->getTerminator()); |
| 1274 | BranchInst *InnerLoopHeaderBI = |
| 1275 | dyn_cast<BranchInst>(InnerLoopHeader->getTerminator()); |
| 1276 | |
| 1277 | if (!OuterLoopPredecessor || !InnerLoopLatchPredecessor || |
| 1278 | !OuterLoopLatchBI || !InnerLoopLatchBI || !OuterLoopHeaderBI || |
| 1279 | !InnerLoopHeaderBI) |
| 1280 | return false; |
| 1281 | |
| 1282 | BranchInst *InnerLoopLatchPredecessorBI = |
| 1283 | dyn_cast<BranchInst>(InnerLoopLatchPredecessor->getTerminator()); |
| 1284 | BranchInst *OuterLoopPredecessorBI = |
| 1285 | dyn_cast<BranchInst>(OuterLoopPredecessor->getTerminator()); |
| 1286 | |
| 1287 | if (!OuterLoopPredecessorBI || !InnerLoopLatchPredecessorBI) |
| 1288 | return false; |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1289 | BasicBlock *InnerLoopHeaderSuccessor = InnerLoopHeader->getUniqueSuccessor(); |
| 1290 | if (!InnerLoopHeaderSuccessor) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1291 | return false; |
| 1292 | |
| 1293 | // Adjust Loop Preheader and headers |
| 1294 | |
| 1295 | unsigned NumSucc = OuterLoopPredecessorBI->getNumSuccessors(); |
| 1296 | for (unsigned i = 0; i < NumSucc; ++i) { |
| 1297 | if (OuterLoopPredecessorBI->getSuccessor(i) == OuterLoopPreHeader) |
| 1298 | OuterLoopPredecessorBI->setSuccessor(i, InnerLoopPreHeader); |
| 1299 | } |
| 1300 | |
| 1301 | NumSucc = OuterLoopHeaderBI->getNumSuccessors(); |
| 1302 | for (unsigned i = 0; i < NumSucc; ++i) { |
| 1303 | if (OuterLoopHeaderBI->getSuccessor(i) == OuterLoopLatch) |
| 1304 | OuterLoopHeaderBI->setSuccessor(i, LoopExit); |
| 1305 | else if (OuterLoopHeaderBI->getSuccessor(i) == InnerLoopPreHeader) |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1306 | OuterLoopHeaderBI->setSuccessor(i, InnerLoopHeaderSuccessor); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1309 | // Adjust reduction PHI's now that the incoming block has changed. |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1310 | updateIncomingBlock(InnerLoopHeaderSuccessor, InnerLoopHeader, |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1311 | OuterLoopHeader); |
| 1312 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1313 | BranchInst::Create(OuterLoopPreHeader, InnerLoopHeaderBI); |
| 1314 | InnerLoopHeaderBI->eraseFromParent(); |
| 1315 | |
| 1316 | // -------------Adjust loop latches----------- |
| 1317 | if (InnerLoopLatchBI->getSuccessor(0) == InnerLoopHeader) |
| 1318 | InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(1); |
| 1319 | else |
| 1320 | InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(0); |
| 1321 | |
| 1322 | NumSucc = InnerLoopLatchPredecessorBI->getNumSuccessors(); |
| 1323 | for (unsigned i = 0; i < NumSucc; ++i) { |
| 1324 | if (InnerLoopLatchPredecessorBI->getSuccessor(i) == InnerLoopLatch) |
| 1325 | InnerLoopLatchPredecessorBI->setSuccessor(i, InnerLoopLatchSuccessor); |
| 1326 | } |
| 1327 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1328 | // Adjust PHI nodes in InnerLoopLatchSuccessor. Update all uses of PHI with |
| 1329 | // the value and remove this PHI node from inner loop. |
| 1330 | SmallVector<PHINode *, 8> LcssaVec; |
| 1331 | for (auto I = InnerLoopLatchSuccessor->begin(); isa<PHINode>(I); ++I) { |
| 1332 | PHINode *LcssaPhi = cast<PHINode>(I); |
| 1333 | LcssaVec.push_back(LcssaPhi); |
| 1334 | } |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1335 | for (PHINode *P : LcssaVec) { |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1336 | Value *Incoming = P->getIncomingValueForBlock(InnerLoopLatch); |
| 1337 | P->replaceAllUsesWith(Incoming); |
| 1338 | P->eraseFromParent(); |
| 1339 | } |
| 1340 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1341 | if (OuterLoopLatchBI->getSuccessor(0) == OuterLoopHeader) |
| 1342 | OuterLoopLatchSuccessor = OuterLoopLatchBI->getSuccessor(1); |
| 1343 | else |
| 1344 | OuterLoopLatchSuccessor = OuterLoopLatchBI->getSuccessor(0); |
| 1345 | |
| 1346 | if (InnerLoopLatchBI->getSuccessor(1) == InnerLoopLatchSuccessor) |
| 1347 | InnerLoopLatchBI->setSuccessor(1, OuterLoopLatchSuccessor); |
| 1348 | else |
| 1349 | InnerLoopLatchBI->setSuccessor(0, OuterLoopLatchSuccessor); |
| 1350 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1351 | updateIncomingBlock(OuterLoopLatchSuccessor, OuterLoopLatch, InnerLoopLatch); |
| 1352 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1353 | if (OuterLoopLatchBI->getSuccessor(0) == OuterLoopLatchSuccessor) { |
| 1354 | OuterLoopLatchBI->setSuccessor(0, InnerLoopLatch); |
| 1355 | } else { |
| 1356 | OuterLoopLatchBI->setSuccessor(1, InnerLoopLatch); |
| 1357 | } |
| 1358 | |
| 1359 | return true; |
| 1360 | } |
| 1361 | void LoopInterchangeTransform::adjustLoopPreheaders() { |
| 1362 | |
| 1363 | // We have interchanged the preheaders so we need to interchange the data in |
| 1364 | // the preheader as well. |
| 1365 | // This is because the content of inner preheader was previously executed |
| 1366 | // inside the outer loop. |
| 1367 | BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader(); |
| 1368 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 1369 | BasicBlock *OuterLoopHeader = OuterLoop->getHeader(); |
| 1370 | BranchInst *InnerTermBI = |
| 1371 | cast<BranchInst>(InnerLoopPreHeader->getTerminator()); |
| 1372 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1373 | // These instructions should now be executed inside the loop. |
| 1374 | // Move instruction into a new block after outer header. |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1375 | moveBBContents(InnerLoopPreHeader, OuterLoopHeader->getTerminator()); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1376 | // These instructions were not executed previously in the loop so move them to |
| 1377 | // the older inner loop preheader. |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1378 | moveBBContents(OuterLoopPreHeader, InnerTermBI); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | bool LoopInterchangeTransform::adjustLoopLinks() { |
| 1382 | |
| 1383 | // Adjust all branches in the inner and outer loop. |
| 1384 | bool Changed = adjustLoopBranches(); |
| 1385 | if (Changed) |
| 1386 | adjustLoopPreheaders(); |
| 1387 | return Changed; |
| 1388 | } |
| 1389 | |
| 1390 | char LoopInterchange::ID = 0; |
| 1391 | INITIALIZE_PASS_BEGIN(LoopInterchange, "loop-interchange", |
| 1392 | "Interchanges loops for cache reuse", false, false) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1393 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 1394 | INITIALIZE_PASS_DEPENDENCY(DependenceAnalysisWrapperPass) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1395 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 1396 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1397 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
Easwaran Raman | e12c487 | 2016-06-09 19:44:46 +0000 | [diff] [blame] | 1398 | INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1399 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 1400 | INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1401 | |
| 1402 | INITIALIZE_PASS_END(LoopInterchange, "loop-interchange", |
| 1403 | "Interchanges loops for cache reuse", false, false) |
| 1404 | |
| 1405 | Pass *llvm::createLoopInterchangePass() { return new LoopInterchange(); } |