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