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