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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This Pass handles loop interchange transform. |
| 10 | // This pass interchanges loops to provide a more cache-friendly memory access |
| 11 | // patterns. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Florian Hahn | 6e00433 | 2018-04-05 10:39:23 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.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/DependenceAnalysis.h" |
| 20 | #include "llvm/Analysis/LoopInfo.h" |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopPass.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 | |
Florian Hahn | 6e00433 | 2018-04-05 10:39:23 +0000 | [diff] [blame] | 54 | STATISTIC(LoopsInterchanged, "Number of loops interchanged"); |
| 55 | |
Chad Rosier | 7243189 | 2016-09-14 17:07:13 +0000 | [diff] [blame] | 56 | static cl::opt<int> LoopInterchangeCostThreshold( |
| 57 | "loop-interchange-threshold", cl::init(0), cl::Hidden, |
| 58 | cl::desc("Interchange if you gain more than this number")); |
| 59 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 60 | namespace { |
| 61 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 62 | using LoopVector = SmallVector<Loop *, 8>; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 63 | |
| 64 | // TODO: Check if we can use a sparse matrix here. |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 65 | using CharMatrix = std::vector<std::vector<char>>; |
| 66 | |
| 67 | } // end anonymous namespace |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 68 | |
| 69 | // Maximum number of dependencies that can be handled in the dependency matrix. |
| 70 | static const unsigned MaxMemInstrCount = 100; |
| 71 | |
| 72 | // Maximum loop depth supported. |
| 73 | static const unsigned MaxLoopNestDepth = 10; |
| 74 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 75 | #ifdef DUMP_DEP_MATRICIES |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 76 | static void printDepMatrix(CharMatrix &DepMatrix) { |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 77 | for (auto &Row : DepMatrix) { |
| 78 | for (auto D : Row) |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 79 | LLVM_DEBUG(dbgs() << D << " "); |
| 80 | LLVM_DEBUG(dbgs() << "\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | #endif |
| 84 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 85 | static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level, |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 86 | Loop *L, DependenceInfo *DI) { |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 87 | using ValueVector = SmallVector<Value *, 16>; |
| 88 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 89 | ValueVector MemInstr; |
| 90 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 91 | // For each block. |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 92 | for (BasicBlock *BB : L->blocks()) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 93 | // Scan the BB and collect legal loads and stores. |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 94 | for (Instruction &I : *BB) { |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 95 | if (!isa<Instruction>(I)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 96 | return false; |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 97 | if (auto *Ld = dyn_cast<LoadInst>(&I)) { |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 98 | if (!Ld->isSimple()) |
| 99 | return false; |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 100 | MemInstr.push_back(&I); |
| 101 | } else if (auto *St = dyn_cast<StoreInst>(&I)) { |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 102 | if (!St->isSimple()) |
| 103 | return false; |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 104 | MemInstr.push_back(&I); |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 105 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 109 | LLVM_DEBUG(dbgs() << "Found " << MemInstr.size() |
| 110 | << " Loads and Stores to analyze\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 111 | |
| 112 | ValueVector::iterator I, IE, J, JE; |
| 113 | |
| 114 | for (I = MemInstr.begin(), IE = MemInstr.end(); I != IE; ++I) { |
| 115 | for (J = I, JE = MemInstr.end(); J != JE; ++J) { |
| 116 | std::vector<char> Dep; |
Chad Rosier | 09c1109 | 2016-09-13 12:56:04 +0000 | [diff] [blame] | 117 | Instruction *Src = cast<Instruction>(*I); |
| 118 | Instruction *Dst = cast<Instruction>(*J); |
Chad Rosier | 90bcb91 | 2016-09-07 16:07:17 +0000 | [diff] [blame] | 119 | if (Src == 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 | // Ignore Input dependencies. |
Chad Rosier | 90bcb91 | 2016-09-07 16:07:17 +0000 | [diff] [blame] | 122 | if (isa<LoadInst>(Src) && isa<LoadInst>(Dst)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 123 | continue; |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 124 | // Track Output, Flow, and Anti dependencies. |
Chad Rosier | 90bcb91 | 2016-09-07 16:07:17 +0000 | [diff] [blame] | 125 | if (auto D = DI->depends(Src, Dst, true)) { |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 126 | assert(D->isOrdered() && "Expected an output, flow or anti dep."); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 127 | LLVM_DEBUG(StringRef DepType = |
| 128 | D->isFlow() ? "flow" : D->isAnti() ? "anti" : "output"; |
| 129 | dbgs() << "Found " << DepType |
| 130 | << " dependency between Src and Dst\n" |
| 131 | << " Src:" << *Src << "\n Dst:" << *Dst << '\n'); |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 132 | unsigned Levels = D->getLevels(); |
| 133 | char Direction; |
| 134 | for (unsigned II = 1; II <= Levels; ++II) { |
| 135 | const SCEV *Distance = D->getDistance(II); |
| 136 | const SCEVConstant *SCEVConst = |
| 137 | dyn_cast_or_null<SCEVConstant>(Distance); |
| 138 | if (SCEVConst) { |
| 139 | const ConstantInt *CI = SCEVConst->getValue(); |
| 140 | if (CI->isNegative()) |
| 141 | Direction = '<'; |
| 142 | else if (CI->isZero()) |
| 143 | Direction = '='; |
| 144 | else |
| 145 | Direction = '>'; |
| 146 | Dep.push_back(Direction); |
| 147 | } else if (D->isScalar(II)) { |
| 148 | Direction = 'S'; |
| 149 | Dep.push_back(Direction); |
| 150 | } else { |
| 151 | unsigned Dir = D->getDirection(II); |
| 152 | if (Dir == Dependence::DVEntry::LT || |
| 153 | Dir == Dependence::DVEntry::LE) |
| 154 | Direction = '<'; |
| 155 | else if (Dir == Dependence::DVEntry::GT || |
| 156 | Dir == Dependence::DVEntry::GE) |
| 157 | Direction = '>'; |
| 158 | else if (Dir == Dependence::DVEntry::EQ) |
| 159 | Direction = '='; |
| 160 | else |
| 161 | Direction = '*'; |
| 162 | Dep.push_back(Direction); |
| 163 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 164 | } |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 165 | while (Dep.size() != Level) { |
| 166 | Dep.push_back('I'); |
| 167 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 168 | |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 169 | DepMatrix.push_back(Dep); |
| 170 | if (DepMatrix.size() > MaxMemInstrCount) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 171 | LLVM_DEBUG(dbgs() << "Cannot handle more than " << MaxMemInstrCount |
| 172 | << " dependencies inside loop\n"); |
Chad Rosier | 00eb8db | 2016-09-21 19:16:47 +0000 | [diff] [blame] | 173 | return false; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 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 | |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 273 | static LoopVector populateWorklist(Loop &L) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 274 | LLVM_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. |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 284 | if (Vec->size() != 1) |
| 285 | return {}; |
| 286 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 287 | LoopList.push_back(CurrentLoop); |
Benjamin Kramer | e448b5b | 2015-07-13 17:21:14 +0000 | [diff] [blame] | 288 | CurrentLoop = Vec->front(); |
| 289 | Vec = &CurrentLoop->getSubLoops(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 290 | } |
| 291 | LoopList.push_back(CurrentLoop); |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 292 | return LoopList; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static PHINode *getInductionVariable(Loop *L, ScalarEvolution *SE) { |
| 296 | PHINode *InnerIndexVar = L->getCanonicalInductionVariable(); |
| 297 | if (InnerIndexVar) |
| 298 | return InnerIndexVar; |
| 299 | if (L->getLoopLatch() == nullptr || L->getLoopPredecessor() == nullptr) |
| 300 | return nullptr; |
| 301 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) { |
| 302 | PHINode *PhiVar = cast<PHINode>(I); |
| 303 | Type *PhiTy = PhiVar->getType(); |
| 304 | if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() && |
| 305 | !PhiTy->isPointerTy()) |
| 306 | return nullptr; |
| 307 | const SCEVAddRecExpr *AddRec = |
| 308 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(PhiVar)); |
| 309 | if (!AddRec || !AddRec->isAffine()) |
| 310 | continue; |
| 311 | const SCEV *Step = AddRec->getStepRecurrence(*SE); |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 312 | if (!isa<SCEVConstant>(Step)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 313 | continue; |
| 314 | // Found the induction variable. |
| 315 | // FIXME: Handle loops with more than one induction variable. Note that, |
| 316 | // currently, legality makes sure we have only one induction variable. |
| 317 | return PhiVar; |
| 318 | } |
| 319 | return nullptr; |
| 320 | } |
| 321 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 322 | namespace { |
| 323 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 324 | /// LoopInterchangeLegality checks if it is legal to interchange the loop. |
| 325 | class LoopInterchangeLegality { |
| 326 | public: |
| 327 | LoopInterchangeLegality(Loop *Outer, Loop *Inner, ScalarEvolution *SE, |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 328 | OptimizationRemarkEmitter *ORE) |
Florian Hahn | c51d088 | 2018-09-06 10:41:01 +0000 | [diff] [blame] | 329 | : OuterLoop(Outer), InnerLoop(Inner), SE(SE), ORE(ORE) {} |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 330 | |
| 331 | /// Check if the loops can be interchanged. |
| 332 | bool canInterchangeLoops(unsigned InnerLoopId, unsigned OuterLoopId, |
| 333 | CharMatrix &DepMatrix); |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 334 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 335 | /// Check if the loop structure is understood. We do not handle triangular |
| 336 | /// loops for now. |
| 337 | bool isLoopStructureUnderstood(PHINode *InnerInductionVar); |
| 338 | |
| 339 | bool currentLimitations(); |
| 340 | |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 341 | const SmallPtrSetImpl<PHINode *> &getOuterInnerReductions() const { |
| 342 | return OuterInnerReductions; |
| 343 | } |
| 344 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 345 | private: |
| 346 | bool tightlyNested(Loop *Outer, Loop *Inner); |
Florian Hahn | c8bd6ea | 2018-11-01 19:25:00 +0000 | [diff] [blame] | 347 | bool containsUnsafeInstructions(BasicBlock *BB); |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 348 | |
| 349 | /// Discover induction and reduction PHIs in the header of \p L. Induction |
| 350 | /// PHIs are added to \p Inductions, reductions are added to |
| 351 | /// OuterInnerReductions. When the outer loop is passed, the inner loop needs |
| 352 | /// to be passed as \p InnerLoop. |
| 353 | bool findInductionAndReductions(Loop *L, |
| 354 | SmallVector<PHINode *, 8> &Inductions, |
| 355 | Loop *InnerLoop); |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 356 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 357 | Loop *OuterLoop; |
| 358 | Loop *InnerLoop; |
| 359 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 360 | ScalarEvolution *SE; |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 361 | |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 362 | /// Interface to emit optimization remarks. |
| 363 | OptimizationRemarkEmitter *ORE; |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 364 | |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 365 | /// Set of reduction PHIs taking part of a reduction across the inner and |
| 366 | /// outer loop. |
| 367 | SmallPtrSet<PHINode *, 4> OuterInnerReductions; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | /// LoopInterchangeProfitability checks if it is profitable to interchange the |
| 371 | /// loop. |
| 372 | class LoopInterchangeProfitability { |
| 373 | public: |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 374 | LoopInterchangeProfitability(Loop *Outer, Loop *Inner, ScalarEvolution *SE, |
| 375 | OptimizationRemarkEmitter *ORE) |
| 376 | : OuterLoop(Outer), InnerLoop(Inner), SE(SE), ORE(ORE) {} |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 377 | |
Vikram TV | 74b4111 | 2015-12-09 05:16:24 +0000 | [diff] [blame] | 378 | /// Check if the loop interchange is profitable. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 379 | bool isProfitable(unsigned InnerLoopId, unsigned OuterLoopId, |
| 380 | CharMatrix &DepMatrix); |
| 381 | |
| 382 | private: |
| 383 | int getInstrOrderCost(); |
| 384 | |
| 385 | Loop *OuterLoop; |
| 386 | Loop *InnerLoop; |
| 387 | |
| 388 | /// Scev analysis. |
| 389 | ScalarEvolution *SE; |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 390 | |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 391 | /// Interface to emit optimization remarks. |
| 392 | OptimizationRemarkEmitter *ORE; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 393 | }; |
| 394 | |
Vikram TV | 74b4111 | 2015-12-09 05:16:24 +0000 | [diff] [blame] | 395 | /// LoopInterchangeTransform interchanges the loop. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 396 | class LoopInterchangeTransform { |
| 397 | public: |
| 398 | LoopInterchangeTransform(Loop *Outer, Loop *Inner, ScalarEvolution *SE, |
| 399 | LoopInfo *LI, DominatorTree *DT, |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 400 | BasicBlock *LoopNestExit, |
| 401 | const LoopInterchangeLegality &LIL) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 402 | : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT), |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 403 | LoopExit(LoopNestExit), LIL(LIL) {} |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 404 | |
| 405 | /// Interchange OuterLoop and InnerLoop. |
| 406 | bool transform(); |
Florian Hahn | 831a757 | 2018-04-05 09:48:45 +0000 | [diff] [blame] | 407 | void restructureLoops(Loop *NewInner, Loop *NewOuter, |
| 408 | BasicBlock *OrigInnerPreHeader, |
| 409 | BasicBlock *OrigOuterPreHeader); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 410 | void removeChildLoop(Loop *OuterLoop, Loop *InnerLoop); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 411 | |
| 412 | private: |
| 413 | void splitInnerLoopLatch(Instruction *); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 414 | void splitInnerLoopHeader(); |
| 415 | bool adjustLoopLinks(); |
| 416 | void adjustLoopPreheaders(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 417 | bool adjustLoopBranches(); |
| 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; |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 428 | |
| 429 | const LoopInterchangeLegality &LIL; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 430 | }; |
| 431 | |
Vikram TV | 74b4111 | 2015-12-09 05:16:24 +0000 | [diff] [blame] | 432 | // Main LoopInterchange Pass. |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 433 | struct LoopInterchange : public LoopPass { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 434 | static char ID; |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 435 | ScalarEvolution *SE = nullptr; |
| 436 | LoopInfo *LI = nullptr; |
| 437 | DependenceInfo *DI = nullptr; |
| 438 | DominatorTree *DT = nullptr; |
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 | |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 443 | LoopInterchange() : LoopPass(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 | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 448 | AU.addRequired<DependenceAnalysisWrapperPass>(); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 449 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 450 | |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 451 | getLoopAnalysisUsage(AU); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 454 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { |
| 455 | if (skipLoop(L) || L->getParentLoop()) |
Florian Hahn | 8d72ecc | 2018-09-28 10:20:07 +0000 | [diff] [blame] | 456 | return false; |
Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 457 | |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 458 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 459 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 460 | DI = &getAnalysis<DependenceAnalysisWrapperPass>().getDI(); |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 461 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 462 | ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 463 | |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 464 | return processLoopList(populateWorklist(*L)); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | bool isComputableLoopNest(LoopVector LoopList) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 468 | for (Loop *L : LoopList) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 469 | const SCEV *ExitCountOuter = SE->getBackedgeTakenCount(L); |
| 470 | if (ExitCountOuter == SE->getCouldNotCompute()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 471 | LLVM_DEBUG(dbgs() << "Couldn't compute backedge count\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 472 | return false; |
| 473 | } |
| 474 | if (L->getNumBackEdges() != 1) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 475 | LLVM_DEBUG(dbgs() << "NumBackEdges is not equal to 1\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | if (!L->getExitingBlock()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 479 | LLVM_DEBUG(dbgs() << "Loop doesn't have unique exit block\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | return true; |
| 484 | } |
| 485 | |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 486 | unsigned selectLoopForInterchange(const LoopVector &LoopList) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 487 | // 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] | 488 | // on the dependence matrix. Currently we select the innermost loop. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 489 | return LoopList.size() - 1; |
| 490 | } |
| 491 | |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 492 | bool processLoopList(LoopVector LoopList) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 493 | bool Changed = false; |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 494 | unsigned LoopNestDepth = LoopList.size(); |
| 495 | if (LoopNestDepth < 2) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 496 | LLVM_DEBUG(dbgs() << "Loop doesn't contain minimum nesting level.\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 497 | return false; |
| 498 | } |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 499 | if (LoopNestDepth > MaxLoopNestDepth) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 500 | LLVM_DEBUG(dbgs() << "Cannot handle loops of depth greater than " |
| 501 | << MaxLoopNestDepth << "\n"); |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 502 | return false; |
| 503 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 504 | if (!isComputableLoopNest(LoopList)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 505 | LLVM_DEBUG(dbgs() << "Not valid loop candidate for interchange\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 506 | return false; |
| 507 | } |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 508 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 509 | LLVM_DEBUG(dbgs() << "Processing LoopList of size = " << LoopNestDepth |
| 510 | << "\n"); |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 511 | |
| 512 | CharMatrix DependencyMatrix; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 513 | Loop *OuterMostLoop = *(LoopList.begin()); |
Chad Rosier | 7ea0d39 | 2016-09-13 13:30:30 +0000 | [diff] [blame] | 514 | if (!populateDependencyMatrix(DependencyMatrix, LoopNestDepth, |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 515 | OuterMostLoop, DI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 516 | LLVM_DEBUG(dbgs() << "Populating dependency matrix failed\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | #ifdef DUMP_DEP_MATRICIES |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 520 | LLVM_DEBUG(dbgs() << "Dependence before interchange\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 521 | printDepMatrix(DependencyMatrix); |
| 522 | #endif |
| 523 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 524 | // Get the Outermost loop exit. |
Florian Hahn | 1da30c6 | 2018-04-25 09:35:54 +0000 | [diff] [blame] | 525 | BasicBlock *LoopNestExit = OuterMostLoop->getExitBlock(); |
| 526 | if (!LoopNestExit) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 527 | LLVM_DEBUG(dbgs() << "OuterMostLoop needs an unique exit block"); |
Florian Hahn | 1da30c6 | 2018-04-25 09:35:54 +0000 | [diff] [blame] | 528 | return false; |
| 529 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 530 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 531 | unsigned SelecLoopId = selectLoopForInterchange(LoopList); |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 532 | // Move the selected loop outwards to the best possible position. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 533 | for (unsigned i = SelecLoopId; i > 0; i--) { |
| 534 | bool Interchanged = |
| 535 | processLoop(LoopList, i, i - 1, LoopNestExit, DependencyMatrix); |
| 536 | if (!Interchanged) |
| 537 | return Changed; |
| 538 | // Loops interchanged reflect the same in LoopList |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 539 | std::swap(LoopList[i - 1], LoopList[i]); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 540 | |
| 541 | // Update the DependencyMatrix |
Chad Rosier | d18ea06 | 2016-09-13 13:00:29 +0000 | [diff] [blame] | 542 | interChangeDependencies(DependencyMatrix, i, i - 1); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 543 | #ifdef DUMP_DEP_MATRICIES |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 544 | LLVM_DEBUG(dbgs() << "Dependence after interchange\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 545 | printDepMatrix(DependencyMatrix); |
| 546 | #endif |
| 547 | Changed |= Interchanged; |
| 548 | } |
| 549 | return Changed; |
| 550 | } |
| 551 | |
| 552 | bool processLoop(LoopVector LoopList, unsigned InnerLoopId, |
| 553 | unsigned OuterLoopId, BasicBlock *LoopNestExit, |
| 554 | std::vector<std::vector<char>> &DependencyMatrix) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 555 | LLVM_DEBUG(dbgs() << "Processing Inner Loop Id = " << InnerLoopId |
| 556 | << " and OuterLoopId = " << OuterLoopId << "\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 557 | Loop *InnerLoop = LoopList[InnerLoopId]; |
| 558 | Loop *OuterLoop = LoopList[OuterLoopId]; |
| 559 | |
Florian Hahn | c51d088 | 2018-09-06 10:41:01 +0000 | [diff] [blame] | 560 | LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, ORE); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 561 | if (!LIL.canInterchangeLoops(InnerLoopId, OuterLoopId, DependencyMatrix)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 562 | LLVM_DEBUG(dbgs() << "Not interchanging loops. Cannot prove legality.\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 563 | return false; |
| 564 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 565 | LLVM_DEBUG(dbgs() << "Loops are legal to interchange\n"); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 566 | LoopInterchangeProfitability LIP(OuterLoop, InnerLoop, SE, ORE); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 567 | if (!LIP.isProfitable(InnerLoopId, OuterLoopId, DependencyMatrix)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 568 | LLVM_DEBUG(dbgs() << "Interchanging loops not profitable.\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 572 | ORE->emit([&]() { |
| 573 | return OptimizationRemark(DEBUG_TYPE, "Interchanged", |
| 574 | InnerLoop->getStartLoc(), |
| 575 | InnerLoop->getHeader()) |
| 576 | << "Loop interchanged with enclosing loop."; |
| 577 | }); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 578 | |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 579 | LoopInterchangeTransform LIT(OuterLoop, InnerLoop, SE, LI, DT, LoopNestExit, |
| 580 | LIL); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 581 | LIT.transform(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 582 | LLVM_DEBUG(dbgs() << "Loops interchanged.\n"); |
Florian Hahn | 6e00433 | 2018-04-05 10:39:23 +0000 | [diff] [blame] | 583 | LoopsInterchanged++; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 584 | return true; |
| 585 | } |
| 586 | }; |
| 587 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 588 | } // end anonymous namespace |
| 589 | |
Florian Hahn | c8bd6ea | 2018-11-01 19:25:00 +0000 | [diff] [blame] | 590 | bool LoopInterchangeLegality::containsUnsafeInstructions(BasicBlock *BB) { |
| 591 | return any_of(*BB, [](const Instruction &I) { |
| 592 | return I.mayHaveSideEffects() || I.mayReadFromMemory(); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 593 | }); |
| 594 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 595 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 596 | bool LoopInterchangeLegality::tightlyNested(Loop *OuterLoop, Loop *InnerLoop) { |
| 597 | BasicBlock *OuterLoopHeader = OuterLoop->getHeader(); |
| 598 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 599 | BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch(); |
| 600 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 601 | LLVM_DEBUG(dbgs() << "Checking if loops are tightly nested\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 602 | |
| 603 | // A perfectly nested loop will not have any branch in between the outer and |
| 604 | // inner block i.e. outer header will branch to either inner preheader and |
| 605 | // outerloop latch. |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 606 | BranchInst *OuterLoopHeaderBI = |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 607 | dyn_cast<BranchInst>(OuterLoopHeader->getTerminator()); |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 608 | if (!OuterLoopHeaderBI) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 609 | return false; |
Chad Rosier | f7c76f9 | 2016-09-21 13:28:41 +0000 | [diff] [blame] | 610 | |
Chandler Carruth | 96fc1de | 2018-08-26 08:41:15 +0000 | [diff] [blame] | 611 | for (BasicBlock *Succ : successors(OuterLoopHeaderBI)) |
Florian Hahn | 236f6fe | 2018-09-06 09:57:27 +0000 | [diff] [blame] | 612 | if (Succ != InnerLoopPreHeader && Succ != InnerLoop->getHeader() && |
| 613 | Succ != OuterLoopLatch) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 614 | return false; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 615 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 616 | LLVM_DEBUG(dbgs() << "Checking instructions in Loop header and Loop latch\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 617 | // 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] | 618 | // and outer loop latch doesn't contain any unsafe instructions. |
Florian Hahn | c8bd6ea | 2018-11-01 19:25:00 +0000 | [diff] [blame] | 619 | if (containsUnsafeInstructions(OuterLoopHeader) || |
| 620 | containsUnsafeInstructions(OuterLoopLatch)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 621 | return false; |
| 622 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 623 | LLVM_DEBUG(dbgs() << "Loops are perfectly nested\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 624 | // We have a perfect loop nest. |
| 625 | return true; |
| 626 | } |
| 627 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 628 | bool LoopInterchangeLegality::isLoopStructureUnderstood( |
| 629 | PHINode *InnerInduction) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 630 | unsigned Num = InnerInduction->getNumOperands(); |
| 631 | BasicBlock *InnerLoopPreheader = InnerLoop->getLoopPreheader(); |
| 632 | for (unsigned i = 0; i < Num; ++i) { |
| 633 | Value *Val = InnerInduction->getOperand(i); |
| 634 | if (isa<Constant>(Val)) |
| 635 | continue; |
| 636 | Instruction *I = dyn_cast<Instruction>(Val); |
| 637 | if (!I) |
| 638 | return false; |
| 639 | // TODO: Handle triangular loops. |
| 640 | // e.g. for(int i=0;i<N;i++) |
| 641 | // for(int j=i;j<N;j++) |
| 642 | unsigned IncomBlockIndx = PHINode::getIncomingValueNumForOperand(i); |
| 643 | if (InnerInduction->getIncomingBlock(IncomBlockIndx) == |
| 644 | InnerLoopPreheader && |
| 645 | !OuterLoop->isLoopInvariant(I)) { |
| 646 | return false; |
| 647 | } |
| 648 | } |
| 649 | return true; |
| 650 | } |
| 651 | |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 652 | // If SV is a LCSSA PHI node with a single incoming value, return the incoming |
| 653 | // value. |
| 654 | static Value *followLCSSA(Value *SV) { |
| 655 | PHINode *PHI = dyn_cast<PHINode>(SV); |
| 656 | if (!PHI) |
| 657 | return SV; |
| 658 | |
| 659 | if (PHI->getNumIncomingValues() != 1) |
| 660 | return SV; |
| 661 | return followLCSSA(PHI->getIncomingValue(0)); |
| 662 | } |
| 663 | |
| 664 | // Check V's users to see if it is involved in a reduction in L. |
| 665 | static PHINode *findInnerReductionPhi(Loop *L, Value *V) { |
| 666 | for (Value *User : V->users()) { |
| 667 | if (PHINode *PHI = dyn_cast<PHINode>(User)) { |
| 668 | if (PHI->getNumIncomingValues() == 1) |
| 669 | continue; |
| 670 | RecurrenceDescriptor RD; |
| 671 | if (RecurrenceDescriptor::isReductionPHI(PHI, L, RD)) |
| 672 | return PHI; |
| 673 | return nullptr; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | return nullptr; |
| 678 | } |
| 679 | |
| 680 | bool LoopInterchangeLegality::findInductionAndReductions( |
| 681 | Loop *L, SmallVector<PHINode *, 8> &Inductions, Loop *InnerLoop) { |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 682 | if (!L->getLoopLatch() || !L->getLoopPredecessor()) |
| 683 | return false; |
Florian Hahn | 5912c66 | 2018-05-02 10:53:04 +0000 | [diff] [blame] | 684 | for (PHINode &PHI : L->getHeader()->phis()) { |
Tyler Nowicki | 0a91310 | 2015-06-16 18:07:34 +0000 | [diff] [blame] | 685 | RecurrenceDescriptor RD; |
James Molloy | 1bbf15c | 2015-08-27 09:53:00 +0000 | [diff] [blame] | 686 | InductionDescriptor ID; |
Florian Hahn | 5912c66 | 2018-05-02 10:53:04 +0000 | [diff] [blame] | 687 | if (InductionDescriptor::isInductionPHI(&PHI, L, SE, ID)) |
| 688 | Inductions.push_back(&PHI); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 689 | else { |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 690 | // PHIs in inner loops need to be part of a reduction in the outer loop, |
| 691 | // discovered when checking the PHIs of the outer loop earlier. |
| 692 | if (!InnerLoop) { |
| 693 | if (OuterInnerReductions.find(&PHI) == OuterInnerReductions.end()) { |
| 694 | LLVM_DEBUG(dbgs() << "Inner loop PHI is not part of reductions " |
| 695 | "across the outer loop.\n"); |
| 696 | return false; |
| 697 | } |
| 698 | } else { |
| 699 | assert(PHI.getNumIncomingValues() == 2 && |
| 700 | "Phis in loop header should have exactly 2 incoming values"); |
| 701 | // Check if we have a PHI node in the outer loop that has a reduction |
| 702 | // result from the inner loop as an incoming value. |
| 703 | Value *V = followLCSSA(PHI.getIncomingValueForBlock(L->getLoopLatch())); |
| 704 | PHINode *InnerRedPhi = findInnerReductionPhi(InnerLoop, V); |
| 705 | if (!InnerRedPhi || |
| 706 | !llvm::any_of(InnerRedPhi->incoming_values(), |
| 707 | [&PHI](Value *V) { return V == &PHI; })) { |
| 708 | LLVM_DEBUG( |
| 709 | dbgs() |
| 710 | << "Failed to recognize PHI as an induction or reduction.\n"); |
| 711 | return false; |
| 712 | } |
| 713 | OuterInnerReductions.insert(&PHI); |
| 714 | OuterInnerReductions.insert(InnerRedPhi); |
| 715 | } |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | return true; |
| 719 | } |
| 720 | |
| 721 | static bool containsSafePHI(BasicBlock *Block, bool isOuterLoopExitBlock) { |
Florian Hahn | 5912c66 | 2018-05-02 10:53:04 +0000 | [diff] [blame] | 722 | for (PHINode &PHI : Block->phis()) { |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 723 | // Reduction lcssa phi will have only 1 incoming block that from loop latch. |
Florian Hahn | 5912c66 | 2018-05-02 10:53:04 +0000 | [diff] [blame] | 724 | if (PHI.getNumIncomingValues() > 1) |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 725 | return false; |
Florian Hahn | 5912c66 | 2018-05-02 10:53:04 +0000 | [diff] [blame] | 726 | Instruction *Ins = dyn_cast<Instruction>(PHI.getIncomingValue(0)); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 727 | if (!Ins) |
| 728 | return false; |
| 729 | // Incoming value for lcssa phi's in outer loop exit can only be inner loop |
| 730 | // exits lcssa phi else it would not be tightly nested. |
| 731 | if (!isa<PHINode>(Ins) && isOuterLoopExitBlock) |
| 732 | return false; |
| 733 | } |
| 734 | return true; |
| 735 | } |
| 736 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 737 | // This function indicates the current limitations in the transform as a result |
| 738 | // of which we do not proceed. |
| 739 | bool LoopInterchangeLegality::currentLimitations() { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 740 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 741 | BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch(); |
Florian Hahn | 1da30c6 | 2018-04-25 09:35:54 +0000 | [diff] [blame] | 742 | |
| 743 | // transform currently expects the loop latches to also be the exiting |
| 744 | // blocks. |
| 745 | if (InnerLoop->getExitingBlock() != InnerLoopLatch || |
| 746 | OuterLoop->getExitingBlock() != OuterLoop->getLoopLatch() || |
| 747 | !isa<BranchInst>(InnerLoopLatch->getTerminator()) || |
| 748 | !isa<BranchInst>(OuterLoop->getLoopLatch()->getTerminator())) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 749 | LLVM_DEBUG( |
| 750 | dbgs() << "Loops where the latch is not the exiting block are not" |
| 751 | << " supported currently.\n"); |
Florian Hahn | 1da30c6 | 2018-04-25 09:35:54 +0000 | [diff] [blame] | 752 | ORE->emit([&]() { |
| 753 | return OptimizationRemarkMissed(DEBUG_TYPE, "ExitingNotLatch", |
| 754 | OuterLoop->getStartLoc(), |
| 755 | OuterLoop->getHeader()) |
| 756 | << "Loops where the latch is not the exiting block cannot be" |
| 757 | " interchange currently."; |
| 758 | }); |
| 759 | return true; |
| 760 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 761 | |
| 762 | PHINode *InnerInductionVar; |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 763 | SmallVector<PHINode *, 8> Inductions; |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 764 | if (!findInductionAndReductions(OuterLoop, Inductions, InnerLoop)) { |
| 765 | LLVM_DEBUG( |
| 766 | dbgs() << "Only outer loops with induction or reduction PHI nodes " |
| 767 | << "are supported currently.\n"); |
| 768 | ORE->emit([&]() { |
| 769 | return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedPHIOuter", |
| 770 | OuterLoop->getStartLoc(), |
| 771 | OuterLoop->getHeader()) |
| 772 | << "Only outer loops with induction or reduction PHI nodes can be" |
| 773 | " interchanged currently."; |
| 774 | }); |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | // TODO: Currently we handle only loops with 1 induction variable. |
| 779 | if (Inductions.size() != 1) { |
| 780 | LLVM_DEBUG(dbgs() << "Loops with more than 1 induction variables are not " |
| 781 | << "supported currently.\n"); |
| 782 | ORE->emit([&]() { |
| 783 | return OptimizationRemarkMissed(DEBUG_TYPE, "MultiIndutionOuter", |
| 784 | OuterLoop->getStartLoc(), |
| 785 | OuterLoop->getHeader()) |
| 786 | << "Only outer loops with 1 induction variable can be " |
| 787 | "interchanged currently."; |
| 788 | }); |
| 789 | return true; |
| 790 | } |
| 791 | |
| 792 | Inductions.clear(); |
| 793 | if (!findInductionAndReductions(InnerLoop, Inductions, nullptr)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 794 | LLVM_DEBUG( |
| 795 | dbgs() << "Only inner loops with induction or reduction PHI nodes " |
| 796 | << "are supported currently.\n"); |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 797 | ORE->emit([&]() { |
| 798 | return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedPHIInner", |
| 799 | InnerLoop->getStartLoc(), |
| 800 | InnerLoop->getHeader()) |
| 801 | << "Only inner loops with induction or reduction PHI nodes can be" |
| 802 | " interchange currently."; |
| 803 | }); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 804 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 805 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 806 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 807 | // TODO: Currently we handle only loops with 1 induction variable. |
| 808 | if (Inductions.size() != 1) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 809 | LLVM_DEBUG( |
| 810 | dbgs() << "We currently only support loops with 1 induction variable." |
| 811 | << "Failed to interchange due to current limitation\n"); |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 812 | ORE->emit([&]() { |
| 813 | return OptimizationRemarkMissed(DEBUG_TYPE, "MultiInductionInner", |
| 814 | InnerLoop->getStartLoc(), |
| 815 | InnerLoop->getHeader()) |
| 816 | << "Only inner loops with 1 induction variable can be " |
| 817 | "interchanged currently."; |
| 818 | }); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 819 | return true; |
| 820 | } |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 821 | InnerInductionVar = Inductions.pop_back_val(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 822 | |
| 823 | // TODO: Triangular loops are not handled for now. |
| 824 | if (!isLoopStructureUnderstood(InnerInductionVar)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 825 | LLVM_DEBUG(dbgs() << "Loop structure not understood by pass\n"); |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 826 | ORE->emit([&]() { |
| 827 | return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedStructureInner", |
| 828 | InnerLoop->getStartLoc(), |
| 829 | InnerLoop->getHeader()) |
| 830 | << "Inner loop structure not understood currently."; |
| 831 | }); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 832 | return true; |
| 833 | } |
| 834 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 835 | // TODO: We only handle LCSSA PHI's corresponding to reduction for now. |
Florian Hahn | 1da30c6 | 2018-04-25 09:35:54 +0000 | [diff] [blame] | 836 | BasicBlock *InnerExit = InnerLoop->getExitBlock(); |
| 837 | if (!containsSafePHI(InnerExit, false)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 838 | LLVM_DEBUG( |
| 839 | dbgs() << "Can only handle LCSSA PHIs in inner loops currently.\n"); |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 840 | ORE->emit([&]() { |
| 841 | return OptimizationRemarkMissed(DEBUG_TYPE, "NoLCSSAPHIOuterInner", |
| 842 | InnerLoop->getStartLoc(), |
| 843 | InnerLoop->getHeader()) |
| 844 | << "Only inner loops with LCSSA PHIs can be interchange " |
| 845 | "currently."; |
| 846 | }); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +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: Current limitation: Since we split the inner loop latch at the point |
| 851 | // were induction variable is incremented (induction.next); We cannot have |
| 852 | // more than 1 user of induction.next since it would result in broken code |
| 853 | // after split. |
| 854 | // e.g. |
| 855 | // for(i=0;i<N;i++) { |
| 856 | // for(j = 0;j<M;j++) { |
| 857 | // A[j+1][i+2] = A[j][i]+k; |
| 858 | // } |
| 859 | // } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 860 | Instruction *InnerIndexVarInc = nullptr; |
| 861 | if (InnerInductionVar->getIncomingBlock(0) == InnerLoopPreHeader) |
| 862 | InnerIndexVarInc = |
| 863 | dyn_cast<Instruction>(InnerInductionVar->getIncomingValue(1)); |
| 864 | else |
| 865 | InnerIndexVarInc = |
| 866 | dyn_cast<Instruction>(InnerInductionVar->getIncomingValue(0)); |
| 867 | |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 868 | if (!InnerIndexVarInc) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 869 | LLVM_DEBUG( |
| 870 | dbgs() << "Did not find an instruction to increment the induction " |
| 871 | << "variable.\n"); |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 872 | ORE->emit([&]() { |
| 873 | return OptimizationRemarkMissed(DEBUG_TYPE, "NoIncrementInInner", |
| 874 | InnerLoop->getStartLoc(), |
| 875 | InnerLoop->getHeader()) |
| 876 | << "The inner loop does not increment the induction variable."; |
| 877 | }); |
Pete Cooper | 11bd958 | 2015-07-27 18:37:58 +0000 | [diff] [blame] | 878 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 879 | } |
Pete Cooper | 11bd958 | 2015-07-27 18:37:58 +0000 | [diff] [blame] | 880 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 881 | // Since we split the inner loop latch on this induction variable. Make sure |
| 882 | // we do not have any instruction between the induction variable and branch |
| 883 | // instruction. |
| 884 | |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 885 | bool FoundInduction = false; |
Florian Hahn | fd2bc11 | 2018-04-26 10:26:17 +0000 | [diff] [blame] | 886 | for (const Instruction &I : |
| 887 | llvm::reverse(InnerLoopLatch->instructionsWithoutDebug())) { |
Florian Hahn | cd78345 | 2017-08-25 16:52:29 +0000 | [diff] [blame] | 888 | if (isa<BranchInst>(I) || isa<CmpInst>(I) || isa<TruncInst>(I) || |
| 889 | isa<ZExtInst>(I)) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 890 | continue; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 891 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 892 | // We found an instruction. If this is not induction variable then it is not |
| 893 | // safe to split this loop latch. |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 894 | if (!I.isIdenticalTo(InnerIndexVarInc)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 895 | LLVM_DEBUG(dbgs() << "Found unsupported instructions between induction " |
| 896 | << "variable increment and branch.\n"); |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 897 | ORE->emit([&]() { |
| 898 | return OptimizationRemarkMissed( |
| 899 | DEBUG_TYPE, "UnsupportedInsBetweenInduction", |
| 900 | InnerLoop->getStartLoc(), InnerLoop->getHeader()) |
| 901 | << "Found unsupported instruction between induction variable " |
| 902 | "increment and branch."; |
| 903 | }); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 904 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 905 | } |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 906 | |
| 907 | FoundInduction = true; |
| 908 | break; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 909 | } |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 910 | // 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] | 911 | // current limitation. |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 912 | if (!FoundInduction) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 913 | LLVM_DEBUG(dbgs() << "Did not find the induction variable.\n"); |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 914 | ORE->emit([&]() { |
| 915 | return OptimizationRemarkMissed(DEBUG_TYPE, "NoIndutionVariable", |
| 916 | InnerLoop->getStartLoc(), |
| 917 | InnerLoop->getHeader()) |
| 918 | << "Did not find the induction variable."; |
| 919 | }); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 920 | return true; |
Florian Hahn | 4eeff39 | 2017-07-03 15:32:00 +0000 | [diff] [blame] | 921 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 922 | return false; |
| 923 | } |
| 924 | |
Florian Hahn | f3fea0f | 2018-04-27 13:52:51 +0000 | [diff] [blame] | 925 | // We currently support LCSSA PHI nodes in the outer loop exit, if their |
| 926 | // incoming values do not come from the outer loop latch or if the |
| 927 | // outer loop latch has a single predecessor. In that case, the value will |
| 928 | // be available if both the inner and outer loop conditions are true, which |
| 929 | // will still be true after interchanging. If we have multiple predecessor, |
| 930 | // that may not be the case, e.g. because the outer loop latch may be executed |
| 931 | // if the inner loop is not executed. |
| 932 | static bool areLoopExitPHIsSupported(Loop *OuterLoop, Loop *InnerLoop) { |
| 933 | BasicBlock *LoopNestExit = OuterLoop->getUniqueExitBlock(); |
| 934 | for (PHINode &PHI : LoopNestExit->phis()) { |
| 935 | // FIXME: We currently are not able to detect floating point reductions |
| 936 | // and have to use floating point PHIs as a proxy to prevent |
| 937 | // interchanging in the presence of floating point reductions. |
| 938 | if (PHI.getType()->isFloatingPointTy()) |
| 939 | return false; |
| 940 | for (unsigned i = 0; i < PHI.getNumIncomingValues(); i++) { |
| 941 | Instruction *IncomingI = dyn_cast<Instruction>(PHI.getIncomingValue(i)); |
| 942 | if (!IncomingI || IncomingI->getParent() != OuterLoop->getLoopLatch()) |
| 943 | continue; |
| 944 | |
| 945 | // The incoming value is defined in the outer loop latch. Currently we |
| 946 | // only support that in case the outer loop latch has a single predecessor. |
| 947 | // This guarantees that the outer loop latch is executed if and only if |
| 948 | // the inner loop is executed (because tightlyNested() guarantees that the |
| 949 | // outer loop header only branches to the inner loop or the outer loop |
| 950 | // latch). |
| 951 | // FIXME: We could weaken this logic and allow multiple predecessors, |
| 952 | // if the values are produced outside the loop latch. We would need |
| 953 | // additional logic to update the PHI nodes in the exit block as |
| 954 | // well. |
| 955 | if (OuterLoop->getLoopLatch()->getUniquePredecessor() == nullptr) |
| 956 | return false; |
| 957 | } |
| 958 | } |
| 959 | return true; |
| 960 | } |
| 961 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 962 | bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId, |
| 963 | unsigned OuterLoopId, |
| 964 | CharMatrix &DepMatrix) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 965 | if (!isLegalToInterChangeLoops(DepMatrix, InnerLoopId, OuterLoopId)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 966 | LLVM_DEBUG(dbgs() << "Failed interchange InnerLoopId = " << InnerLoopId |
| 967 | << " and OuterLoopId = " << OuterLoopId |
| 968 | << " due to dependence\n"); |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 969 | ORE->emit([&]() { |
| 970 | return OptimizationRemarkMissed(DEBUG_TYPE, "Dependence", |
| 971 | InnerLoop->getStartLoc(), |
| 972 | InnerLoop->getHeader()) |
| 973 | << "Cannot interchange loops due to dependences."; |
| 974 | }); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 975 | return false; |
| 976 | } |
Florian Hahn | 4284049 | 2017-07-31 09:00:52 +0000 | [diff] [blame] | 977 | // Check if outer and inner loop contain legal instructions only. |
| 978 | for (auto *BB : OuterLoop->blocks()) |
Florian Hahn | fd2bc11 | 2018-04-26 10:26:17 +0000 | [diff] [blame] | 979 | for (Instruction &I : BB->instructionsWithoutDebug()) |
Florian Hahn | 4284049 | 2017-07-31 09:00:52 +0000 | [diff] [blame] | 980 | if (CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 981 | // readnone functions do not prevent interchanging. |
| 982 | if (CI->doesNotReadMemory()) |
| 983 | continue; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 984 | LLVM_DEBUG( |
| 985 | dbgs() << "Loops with call instructions cannot be interchanged " |
| 986 | << "safely."); |
Florian Hahn | 9467ccf | 2018-04-03 20:54:04 +0000 | [diff] [blame] | 987 | ORE->emit([&]() { |
| 988 | return OptimizationRemarkMissed(DEBUG_TYPE, "CallInst", |
| 989 | CI->getDebugLoc(), |
| 990 | CI->getParent()) |
| 991 | << "Cannot interchange loops due to call instruction."; |
| 992 | }); |
| 993 | |
Florian Hahn | 4284049 | 2017-07-31 09:00:52 +0000 | [diff] [blame] | 994 | return false; |
| 995 | } |
| 996 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 997 | // TODO: The loops could not be interchanged due to current limitations in the |
| 998 | // transform module. |
| 999 | if (currentLimitations()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1000 | LLVM_DEBUG(dbgs() << "Not legal because of current transform limitation\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1001 | return false; |
| 1002 | } |
| 1003 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1004 | // Check if the loops are tightly nested. |
| 1005 | if (!tightlyNested(OuterLoop, InnerLoop)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1006 | LLVM_DEBUG(dbgs() << "Loops not tightly nested\n"); |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 1007 | ORE->emit([&]() { |
| 1008 | return OptimizationRemarkMissed(DEBUG_TYPE, "NotTightlyNested", |
| 1009 | InnerLoop->getStartLoc(), |
| 1010 | InnerLoop->getHeader()) |
| 1011 | << "Cannot interchange loops because they are not tightly " |
| 1012 | "nested."; |
| 1013 | }); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1014 | return false; |
| 1015 | } |
| 1016 | |
Florian Hahn | f3fea0f | 2018-04-27 13:52:51 +0000 | [diff] [blame] | 1017 | if (!areLoopExitPHIsSupported(OuterLoop, InnerLoop)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1018 | LLVM_DEBUG(dbgs() << "Found unsupported PHI nodes in outer loop exit.\n"); |
Florian Hahn | f3fea0f | 2018-04-27 13:52:51 +0000 | [diff] [blame] | 1019 | ORE->emit([&]() { |
| 1020 | return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedExitPHI", |
| 1021 | OuterLoop->getStartLoc(), |
| 1022 | OuterLoop->getHeader()) |
| 1023 | << "Found unsupported PHI node in loop exit."; |
| 1024 | }); |
| 1025 | return false; |
| 1026 | } |
| 1027 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1028 | return true; |
| 1029 | } |
| 1030 | |
| 1031 | int LoopInterchangeProfitability::getInstrOrderCost() { |
| 1032 | unsigned GoodOrder, BadOrder; |
| 1033 | BadOrder = GoodOrder = 0; |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 1034 | for (BasicBlock *BB : InnerLoop->blocks()) { |
| 1035 | for (Instruction &Ins : *BB) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1036 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Ins)) { |
| 1037 | unsigned NumOp = GEP->getNumOperands(); |
| 1038 | bool FoundInnerInduction = false; |
| 1039 | bool FoundOuterInduction = false; |
| 1040 | for (unsigned i = 0; i < NumOp; ++i) { |
| 1041 | const SCEV *OperandVal = SE->getSCEV(GEP->getOperand(i)); |
| 1042 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(OperandVal); |
| 1043 | if (!AR) |
| 1044 | continue; |
| 1045 | |
| 1046 | // If we find the inner induction after an outer induction e.g. |
| 1047 | // for(int i=0;i<N;i++) |
| 1048 | // for(int j=0;j<N;j++) |
| 1049 | // A[i][j] = A[i-1][j-1]+k; |
| 1050 | // then it is a good order. |
| 1051 | if (AR->getLoop() == InnerLoop) { |
| 1052 | // We found an InnerLoop induction after OuterLoop induction. It is |
| 1053 | // a good order. |
| 1054 | FoundInnerInduction = true; |
| 1055 | if (FoundOuterInduction) { |
| 1056 | GoodOrder++; |
| 1057 | break; |
| 1058 | } |
| 1059 | } |
| 1060 | // If we find the outer induction after an inner induction e.g. |
| 1061 | // for(int i=0;i<N;i++) |
| 1062 | // for(int j=0;j<N;j++) |
| 1063 | // A[j][i] = A[j-1][i-1]+k; |
| 1064 | // then it is a bad order. |
| 1065 | if (AR->getLoop() == OuterLoop) { |
| 1066 | // We found an OuterLoop induction after InnerLoop induction. It is |
| 1067 | // a bad order. |
| 1068 | FoundOuterInduction = true; |
| 1069 | if (FoundInnerInduction) { |
| 1070 | BadOrder++; |
| 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | return GoodOrder - BadOrder; |
| 1079 | } |
| 1080 | |
Chad Rosier | e6b3a63 | 2016-09-14 17:12:30 +0000 | [diff] [blame] | 1081 | static bool isProfitableForVectorization(unsigned InnerLoopId, |
| 1082 | unsigned OuterLoopId, |
| 1083 | CharMatrix &DepMatrix) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1084 | // TODO: Improve this heuristic to catch more cases. |
| 1085 | // If the inner loop is loop independent or doesn't carry any dependency it is |
| 1086 | // profitable to move this to outer position. |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 1087 | for (auto &Row : DepMatrix) { |
| 1088 | if (Row[InnerLoopId] != 'S' && Row[InnerLoopId] != 'I') |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1089 | return false; |
| 1090 | // TODO: We need to improve this heuristic. |
Florian Hahn | f66efd6 | 2017-07-24 11:41:30 +0000 | [diff] [blame] | 1091 | if (Row[OuterLoopId] != '=') |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1092 | return false; |
| 1093 | } |
| 1094 | // If outer loop has dependence and inner loop is loop independent then it is |
| 1095 | // profitable to interchange to enable parallelism. |
Florian Hahn | ceee788 | 2018-04-24 16:55:32 +0000 | [diff] [blame] | 1096 | // If there are no dependences, interchanging will not improve anything. |
| 1097 | return !DepMatrix.empty(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | bool LoopInterchangeProfitability::isProfitable(unsigned InnerLoopId, |
| 1101 | unsigned OuterLoopId, |
| 1102 | CharMatrix &DepMatrix) { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1103 | // TODO: Add better profitability checks. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1104 | // e.g |
| 1105 | // 1) Construct dependency matrix and move the one with no loop carried dep |
| 1106 | // inside to enable vectorization. |
| 1107 | |
| 1108 | // This is rough cost estimation algorithm. It counts the good and bad order |
| 1109 | // of induction variables in the instruction and allows reordering if number |
| 1110 | // of bad orders is more than good. |
Chad Rosier | 7243189 | 2016-09-14 17:07:13 +0000 | [diff] [blame] | 1111 | int Cost = getInstrOrderCost(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1112 | LLVM_DEBUG(dbgs() << "Cost = " << Cost << "\n"); |
Chad Rosier | 7243189 | 2016-09-14 17:07:13 +0000 | [diff] [blame] | 1113 | if (Cost < -LoopInterchangeCostThreshold) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1114 | return true; |
| 1115 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1116 | // 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] | 1117 | // we can move this loop outside to improve parallelism. |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 1118 | if (isProfitableForVectorization(InnerLoopId, OuterLoopId, DepMatrix)) |
| 1119 | return true; |
| 1120 | |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 1121 | ORE->emit([&]() { |
| 1122 | return OptimizationRemarkMissed(DEBUG_TYPE, "InterchangeNotProfitable", |
| 1123 | InnerLoop->getStartLoc(), |
| 1124 | InnerLoop->getHeader()) |
| 1125 | << "Interchanging loops is too costly (cost=" |
| 1126 | << ore::NV("Cost", Cost) << ", threshold=" |
| 1127 | << ore::NV("Threshold", LoopInterchangeCostThreshold) |
| 1128 | << ") and it does not improve parallelism."; |
| 1129 | }); |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 1130 | return false; |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | void LoopInterchangeTransform::removeChildLoop(Loop *OuterLoop, |
| 1134 | Loop *InnerLoop) { |
Florian Hahn | 5912c66 | 2018-05-02 10:53:04 +0000 | [diff] [blame] | 1135 | for (Loop *L : *OuterLoop) |
| 1136 | if (L == InnerLoop) { |
| 1137 | OuterLoop->removeChildLoop(L); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1138 | return; |
| 1139 | } |
Benjamin Kramer | 8ceb323 | 2015-10-25 22:28:27 +0000 | [diff] [blame] | 1140 | llvm_unreachable("Couldn't find loop"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1141 | } |
Daniel Jasper | 6adbd7a | 2015-03-06 10:39:14 +0000 | [diff] [blame] | 1142 | |
Florian Hahn | 831a757 | 2018-04-05 09:48:45 +0000 | [diff] [blame] | 1143 | /// Update LoopInfo, after interchanging. NewInner and NewOuter refer to the |
| 1144 | /// new inner and outer loop after interchanging: NewInner is the original |
| 1145 | /// outer loop and NewOuter is the original inner loop. |
| 1146 | /// |
| 1147 | /// Before interchanging, we have the following structure |
| 1148 | /// Outer preheader |
| 1149 | // Outer header |
| 1150 | // Inner preheader |
| 1151 | // Inner header |
| 1152 | // Inner body |
| 1153 | // Inner latch |
| 1154 | // outer bbs |
| 1155 | // Outer latch |
| 1156 | // |
| 1157 | // After interchanging: |
| 1158 | // Inner preheader |
| 1159 | // Inner header |
| 1160 | // Outer preheader |
| 1161 | // Outer header |
| 1162 | // Inner body |
| 1163 | // outer bbs |
| 1164 | // Outer latch |
| 1165 | // Inner latch |
| 1166 | void LoopInterchangeTransform::restructureLoops( |
| 1167 | Loop *NewInner, Loop *NewOuter, BasicBlock *OrigInnerPreHeader, |
| 1168 | BasicBlock *OrigOuterPreHeader) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1169 | Loop *OuterLoopParent = OuterLoop->getParentLoop(); |
Florian Hahn | 831a757 | 2018-04-05 09:48:45 +0000 | [diff] [blame] | 1170 | // The original inner loop preheader moves from the new inner loop to |
| 1171 | // the parent loop, if there is one. |
| 1172 | NewInner->removeBlockFromLoop(OrigInnerPreHeader); |
| 1173 | LI->changeLoopFor(OrigInnerPreHeader, OuterLoopParent); |
| 1174 | |
| 1175 | // Switch the loop levels. |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1176 | if (OuterLoopParent) { |
| 1177 | // Remove the loop from its parent loop. |
Florian Hahn | 831a757 | 2018-04-05 09:48:45 +0000 | [diff] [blame] | 1178 | removeChildLoop(OuterLoopParent, NewInner); |
| 1179 | removeChildLoop(NewInner, NewOuter); |
| 1180 | OuterLoopParent->addChildLoop(NewOuter); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1181 | } else { |
Florian Hahn | 831a757 | 2018-04-05 09:48:45 +0000 | [diff] [blame] | 1182 | removeChildLoop(NewInner, NewOuter); |
| 1183 | LI->changeTopLevelLoop(NewInner, NewOuter); |
| 1184 | } |
| 1185 | while (!NewOuter->empty()) |
| 1186 | NewInner->addChildLoop(NewOuter->removeChildLoop(NewOuter->begin())); |
| 1187 | NewOuter->addChildLoop(NewInner); |
| 1188 | |
| 1189 | // BBs from the original inner loop. |
| 1190 | SmallVector<BasicBlock *, 8> OrigInnerBBs(NewOuter->blocks()); |
| 1191 | |
| 1192 | // Add BBs from the original outer loop to the original inner loop (excluding |
| 1193 | // BBs already in inner loop) |
| 1194 | for (BasicBlock *BB : NewInner->blocks()) |
| 1195 | if (LI->getLoopFor(BB) == NewInner) |
| 1196 | NewOuter->addBlockEntry(BB); |
| 1197 | |
| 1198 | // Now remove inner loop header and latch from the new inner loop and move |
| 1199 | // other BBs (the loop body) to the new inner loop. |
| 1200 | BasicBlock *OuterHeader = NewOuter->getHeader(); |
| 1201 | BasicBlock *OuterLatch = NewOuter->getLoopLatch(); |
| 1202 | for (BasicBlock *BB : OrigInnerBBs) { |
Florian Hahn | 74418185 | 2018-04-23 21:38:19 +0000 | [diff] [blame] | 1203 | // Nothing will change for BBs in child loops. |
| 1204 | if (LI->getLoopFor(BB) != NewOuter) |
| 1205 | continue; |
Florian Hahn | 831a757 | 2018-04-05 09:48:45 +0000 | [diff] [blame] | 1206 | // Remove the new outer loop header and latch from the new inner loop. |
| 1207 | if (BB == OuterHeader || BB == OuterLatch) |
| 1208 | NewInner->removeBlockFromLoop(BB); |
| 1209 | else |
| 1210 | LI->changeLoopFor(BB, NewInner); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Florian Hahn | 831a757 | 2018-04-05 09:48:45 +0000 | [diff] [blame] | 1213 | // The preheader of the original outer loop becomes part of the new |
| 1214 | // outer loop. |
| 1215 | NewOuter->addBlockEntry(OrigOuterPreHeader); |
| 1216 | LI->changeLoopFor(OrigOuterPreHeader, NewOuter); |
Florian Hahn | 3afb974 | 2018-09-14 07:50:20 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Tell SE that we move the loops around. |
| 1219 | SE->forgetLoop(NewOuter); |
| 1220 | SE->forgetLoop(NewInner); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | bool LoopInterchangeTransform::transform() { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1224 | bool Transformed = false; |
| 1225 | Instruction *InnerIndexVar; |
| 1226 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 1227 | if (InnerLoop->getSubLoops().empty()) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1228 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1229 | LLVM_DEBUG(dbgs() << "Calling Split Inner Loop\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1230 | PHINode *InductionPHI = getInductionVariable(InnerLoop, SE); |
| 1231 | if (!InductionPHI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1232 | LLVM_DEBUG(dbgs() << "Failed to find the point to split loop latch \n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1233 | return false; |
| 1234 | } |
| 1235 | |
| 1236 | if (InductionPHI->getIncomingBlock(0) == InnerLoopPreHeader) |
| 1237 | InnerIndexVar = dyn_cast<Instruction>(InductionPHI->getIncomingValue(1)); |
| 1238 | else |
| 1239 | InnerIndexVar = dyn_cast<Instruction>(InductionPHI->getIncomingValue(0)); |
| 1240 | |
Florian Hahn | d8fcf0d | 2018-06-19 08:03:24 +0000 | [diff] [blame] | 1241 | // Ensure that InductionPHI is the first Phi node. |
David Green | 907b60f | 2017-10-21 13:58:37 +0000 | [diff] [blame] | 1242 | if (&InductionPHI->getParent()->front() != InductionPHI) |
| 1243 | InductionPHI->moveBefore(&InductionPHI->getParent()->front()); |
| 1244 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1245 | // Split at the place were the induction variable is |
| 1246 | // incremented/decremented. |
| 1247 | // TODO: This splitting logic may not work always. Fix this. |
| 1248 | splitInnerLoopLatch(InnerIndexVar); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1249 | LLVM_DEBUG(dbgs() << "splitInnerLoopLatch done\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1250 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1251 | // Splits the inner loops phi nodes out into a separate basic block. |
Florian Hahn | d8fcf0d | 2018-06-19 08:03:24 +0000 | [diff] [blame] | 1252 | BasicBlock *InnerLoopHeader = InnerLoop->getHeader(); |
| 1253 | SplitBlock(InnerLoopHeader, InnerLoopHeader->getFirstNonPHI(), DT, LI); |
| 1254 | LLVM_DEBUG(dbgs() << "splitting InnerLoopHeader done\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | Transformed |= adjustLoopLinks(); |
| 1258 | if (!Transformed) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1259 | LLVM_DEBUG(dbgs() << "adjustLoopLinks failed\n"); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1260 | return false; |
| 1261 | } |
| 1262 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1263 | return true; |
| 1264 | } |
| 1265 | |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1266 | void LoopInterchangeTransform::splitInnerLoopLatch(Instruction *Inc) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1267 | BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1268 | BasicBlock *InnerLoopLatchPred = InnerLoopLatch; |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1269 | InnerLoopLatch = SplitBlock(InnerLoopLatchPred, Inc, DT, LI); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Florian Hahn | d8fcf0d | 2018-06-19 08:03:24 +0000 | [diff] [blame] | 1272 | /// \brief Move all instructions except the terminator from FromBB right before |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1273 | /// InsertBefore |
| 1274 | static void moveBBContents(BasicBlock *FromBB, Instruction *InsertBefore) { |
| 1275 | auto &ToList = InsertBefore->getParent()->getInstList(); |
| 1276 | auto &FromList = FromBB->getInstList(); |
| 1277 | |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1278 | ToList.splice(InsertBefore->getIterator(), FromList, FromList.begin(), |
| 1279 | FromBB->getTerminator()->getIterator()); |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1282 | /// Update BI to jump to NewBB instead of OldBB. Records updates to |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 1283 | /// the dominator tree in DTUpdates, if DT should be preserved. |
| 1284 | static void updateSuccessor(BranchInst *BI, BasicBlock *OldBB, |
| 1285 | BasicBlock *NewBB, |
| 1286 | std::vector<DominatorTree::UpdateType> &DTUpdates) { |
Chandler Carruth | 96fc1de | 2018-08-26 08:41:15 +0000 | [diff] [blame] | 1287 | assert(llvm::count_if(successors(BI), |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 1288 | [OldBB](BasicBlock *BB) { return BB == OldBB; }) < 2 && |
| 1289 | "BI must jump to OldBB at most once."); |
| 1290 | for (unsigned i = 0, e = BI->getNumSuccessors(); i < e; ++i) { |
| 1291 | if (BI->getSuccessor(i) == OldBB) { |
| 1292 | BI->setSuccessor(i, NewBB); |
| 1293 | |
| 1294 | DTUpdates.push_back( |
| 1295 | {DominatorTree::UpdateKind::Insert, BI->getParent(), NewBB}); |
| 1296 | DTUpdates.push_back( |
| 1297 | {DominatorTree::UpdateKind::Delete, BI->getParent(), OldBB}); |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | |
Florian Hahn | 6feb637 | 2018-09-26 19:34:25 +0000 | [diff] [blame] | 1303 | // Move Lcssa PHIs to the right place. |
| 1304 | static void moveLCSSAPhis(BasicBlock *InnerExit, BasicBlock *InnerLatch, |
| 1305 | BasicBlock *OuterLatch) { |
| 1306 | SmallVector<PHINode *, 8> LcssaInnerExit; |
| 1307 | for (PHINode &P : InnerExit->phis()) |
| 1308 | LcssaInnerExit.push_back(&P); |
| 1309 | |
| 1310 | SmallVector<PHINode *, 8> LcssaInnerLatch; |
| 1311 | for (PHINode &P : InnerLatch->phis()) |
| 1312 | LcssaInnerLatch.push_back(&P); |
| 1313 | |
| 1314 | // Lcssa PHIs for values used outside the inner loop are in InnerExit. |
| 1315 | // If a PHI node has users outside of InnerExit, it has a use outside the |
| 1316 | // interchanged loop and we have to preserve it. We move these to |
| 1317 | // InnerLatch, which will become the new exit block for the innermost |
| 1318 | // loop after interchanging. For PHIs only used in InnerExit, we can just |
| 1319 | // replace them with the incoming value. |
| 1320 | for (PHINode *P : LcssaInnerExit) { |
| 1321 | bool hasUsersOutside = false; |
| 1322 | for (auto UI = P->use_begin(), E = P->use_end(); UI != E;) { |
| 1323 | Use &U = *UI; |
| 1324 | ++UI; |
| 1325 | auto *Usr = cast<Instruction>(U.getUser()); |
| 1326 | if (Usr->getParent() != InnerExit) { |
| 1327 | hasUsersOutside = true; |
| 1328 | continue; |
| 1329 | } |
| 1330 | U.set(P->getIncomingValueForBlock(InnerLatch)); |
| 1331 | } |
| 1332 | if (hasUsersOutside) |
| 1333 | P->moveBefore(InnerLatch->getFirstNonPHI()); |
| 1334 | else |
| 1335 | P->eraseFromParent(); |
| 1336 | } |
| 1337 | |
| 1338 | // If the inner loop latch contains LCSSA PHIs, those come from a child loop |
| 1339 | // and we have to move them to the new inner latch. |
| 1340 | for (PHINode *P : LcssaInnerLatch) |
| 1341 | P->moveBefore(InnerExit->getFirstNonPHI()); |
| 1342 | |
| 1343 | // Now adjust the incoming blocks for the LCSSA PHIs. |
| 1344 | // For PHIs moved from Inner's exit block, we need to replace Inner's latch |
| 1345 | // with the new latch. |
Roman Lebedev | 1a1b922 | 2019-05-05 18:59:39 +0000 | [diff] [blame] | 1346 | InnerLatch->replacePhiUsesWith(InnerLatch, OuterLatch); |
Florian Hahn | 6feb637 | 2018-09-26 19:34:25 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1349 | bool LoopInterchangeTransform::adjustLoopBranches() { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1350 | LLVM_DEBUG(dbgs() << "adjustLoopBranches called\n"); |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 1351 | std::vector<DominatorTree::UpdateType> DTUpdates; |
| 1352 | |
Florian Hahn | 236f6fe | 2018-09-06 09:57:27 +0000 | [diff] [blame] | 1353 | BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader(); |
| 1354 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 1355 | |
| 1356 | assert(OuterLoopPreHeader != OuterLoop->getHeader() && |
| 1357 | InnerLoopPreHeader != InnerLoop->getHeader() && OuterLoopPreHeader && |
| 1358 | InnerLoopPreHeader && "Guaranteed by loop-simplify form"); |
| 1359 | // Ensure that both preheaders do not contain PHI nodes and have single |
| 1360 | // predecessors. This allows us to move them easily. We use |
| 1361 | // InsertPreHeaderForLoop to create an 'extra' preheader, if the existing |
| 1362 | // preheaders do not satisfy those conditions. |
| 1363 | if (isa<PHINode>(OuterLoopPreHeader->begin()) || |
| 1364 | !OuterLoopPreHeader->getUniquePredecessor()) |
Alina Sbirlea | f31eba6 | 2019-05-08 17:05:36 +0000 | [diff] [blame^] | 1365 | OuterLoopPreHeader = |
| 1366 | InsertPreheaderForLoop(OuterLoop, DT, LI, nullptr, true); |
Florian Hahn | 236f6fe | 2018-09-06 09:57:27 +0000 | [diff] [blame] | 1367 | if (InnerLoopPreHeader == OuterLoop->getHeader()) |
Alina Sbirlea | f31eba6 | 2019-05-08 17:05:36 +0000 | [diff] [blame^] | 1368 | InnerLoopPreHeader = |
| 1369 | InsertPreheaderForLoop(InnerLoop, DT, LI, nullptr, true); |
Florian Hahn | 236f6fe | 2018-09-06 09:57:27 +0000 | [diff] [blame] | 1370 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1371 | // Adjust the loop preheader |
| 1372 | BasicBlock *InnerLoopHeader = InnerLoop->getHeader(); |
| 1373 | BasicBlock *OuterLoopHeader = OuterLoop->getHeader(); |
| 1374 | BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch(); |
| 1375 | BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch(); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1376 | BasicBlock *OuterLoopPredecessor = OuterLoopPreHeader->getUniquePredecessor(); |
| 1377 | BasicBlock *InnerLoopLatchPredecessor = |
| 1378 | InnerLoopLatch->getUniquePredecessor(); |
| 1379 | BasicBlock *InnerLoopLatchSuccessor; |
| 1380 | BasicBlock *OuterLoopLatchSuccessor; |
| 1381 | |
| 1382 | BranchInst *OuterLoopLatchBI = |
| 1383 | dyn_cast<BranchInst>(OuterLoopLatch->getTerminator()); |
| 1384 | BranchInst *InnerLoopLatchBI = |
| 1385 | dyn_cast<BranchInst>(InnerLoopLatch->getTerminator()); |
| 1386 | BranchInst *OuterLoopHeaderBI = |
| 1387 | dyn_cast<BranchInst>(OuterLoopHeader->getTerminator()); |
| 1388 | BranchInst *InnerLoopHeaderBI = |
| 1389 | dyn_cast<BranchInst>(InnerLoopHeader->getTerminator()); |
| 1390 | |
| 1391 | if (!OuterLoopPredecessor || !InnerLoopLatchPredecessor || |
| 1392 | !OuterLoopLatchBI || !InnerLoopLatchBI || !OuterLoopHeaderBI || |
| 1393 | !InnerLoopHeaderBI) |
| 1394 | return false; |
| 1395 | |
| 1396 | BranchInst *InnerLoopLatchPredecessorBI = |
| 1397 | dyn_cast<BranchInst>(InnerLoopLatchPredecessor->getTerminator()); |
| 1398 | BranchInst *OuterLoopPredecessorBI = |
| 1399 | dyn_cast<BranchInst>(OuterLoopPredecessor->getTerminator()); |
| 1400 | |
| 1401 | if (!OuterLoopPredecessorBI || !InnerLoopLatchPredecessorBI) |
| 1402 | return false; |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1403 | BasicBlock *InnerLoopHeaderSuccessor = InnerLoopHeader->getUniqueSuccessor(); |
| 1404 | if (!InnerLoopHeaderSuccessor) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1405 | return false; |
| 1406 | |
| 1407 | // Adjust Loop Preheader and headers |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 1408 | updateSuccessor(OuterLoopPredecessorBI, OuterLoopPreHeader, |
| 1409 | InnerLoopPreHeader, DTUpdates); |
| 1410 | updateSuccessor(OuterLoopHeaderBI, OuterLoopLatch, LoopExit, DTUpdates); |
| 1411 | updateSuccessor(OuterLoopHeaderBI, InnerLoopPreHeader, |
| 1412 | InnerLoopHeaderSuccessor, DTUpdates); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1413 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1414 | // Adjust reduction PHI's now that the incoming block has changed. |
Roman Lebedev | 1a1b922 | 2019-05-05 18:59:39 +0000 | [diff] [blame] | 1415 | InnerLoopHeaderSuccessor->replacePhiUsesWith(InnerLoopHeader, |
| 1416 | OuterLoopHeader); |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1417 | |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 1418 | updateSuccessor(InnerLoopHeaderBI, InnerLoopHeaderSuccessor, |
| 1419 | OuterLoopPreHeader, DTUpdates); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1420 | |
| 1421 | // -------------Adjust loop latches----------- |
| 1422 | if (InnerLoopLatchBI->getSuccessor(0) == InnerLoopHeader) |
| 1423 | InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(1); |
| 1424 | else |
| 1425 | InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(0); |
| 1426 | |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 1427 | updateSuccessor(InnerLoopLatchPredecessorBI, InnerLoopLatch, |
| 1428 | InnerLoopLatchSuccessor, DTUpdates); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1429 | |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1430 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1431 | if (OuterLoopLatchBI->getSuccessor(0) == OuterLoopHeader) |
| 1432 | OuterLoopLatchSuccessor = OuterLoopLatchBI->getSuccessor(1); |
| 1433 | else |
| 1434 | OuterLoopLatchSuccessor = OuterLoopLatchBI->getSuccessor(0); |
| 1435 | |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 1436 | updateSuccessor(InnerLoopLatchBI, InnerLoopLatchSuccessor, |
| 1437 | OuterLoopLatchSuccessor, DTUpdates); |
| 1438 | updateSuccessor(OuterLoopLatchBI, OuterLoopLatchSuccessor, InnerLoopLatch, |
| 1439 | DTUpdates); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1440 | |
Florian Hahn | c6296fe | 2018-02-14 13:13:15 +0000 | [diff] [blame] | 1441 | DT->applyUpdates(DTUpdates); |
Florian Hahn | 831a757 | 2018-04-05 09:48:45 +0000 | [diff] [blame] | 1442 | restructureLoops(OuterLoop, InnerLoop, InnerLoopPreHeader, |
| 1443 | OuterLoopPreHeader); |
| 1444 | |
Florian Hahn | 6feb637 | 2018-09-26 19:34:25 +0000 | [diff] [blame] | 1445 | moveLCSSAPhis(InnerLoopLatchSuccessor, InnerLoopLatch, OuterLoopLatch); |
| 1446 | // For PHIs in the exit block of the outer loop, outer's latch has been |
| 1447 | // replaced by Inners'. |
Roman Lebedev | 1a1b922 | 2019-05-05 18:59:39 +0000 | [diff] [blame] | 1448 | OuterLoopLatchSuccessor->replacePhiUsesWith(OuterLoopLatch, InnerLoopLatch); |
Florian Hahn | 6feb637 | 2018-09-26 19:34:25 +0000 | [diff] [blame] | 1449 | |
Florian Hahn | a684a99 | 2018-11-08 20:44:19 +0000 | [diff] [blame] | 1450 | // Now update the reduction PHIs in the inner and outer loop headers. |
| 1451 | SmallVector<PHINode *, 4> InnerLoopPHIs, OuterLoopPHIs; |
| 1452 | for (PHINode &PHI : drop_begin(InnerLoopHeader->phis(), 1)) |
| 1453 | InnerLoopPHIs.push_back(cast<PHINode>(&PHI)); |
| 1454 | for (PHINode &PHI : drop_begin(OuterLoopHeader->phis(), 1)) |
| 1455 | OuterLoopPHIs.push_back(cast<PHINode>(&PHI)); |
| 1456 | |
| 1457 | auto &OuterInnerReductions = LIL.getOuterInnerReductions(); |
| 1458 | (void)OuterInnerReductions; |
| 1459 | |
| 1460 | // Now move the remaining reduction PHIs from outer to inner loop header and |
| 1461 | // vice versa. The PHI nodes must be part of a reduction across the inner and |
| 1462 | // outer loop and all the remains to do is and updating the incoming blocks. |
| 1463 | for (PHINode *PHI : OuterLoopPHIs) { |
| 1464 | PHI->moveBefore(InnerLoopHeader->getFirstNonPHI()); |
| 1465 | assert(OuterInnerReductions.find(PHI) != OuterInnerReductions.end() && |
| 1466 | "Expected a reduction PHI node"); |
| 1467 | } |
| 1468 | for (PHINode *PHI : InnerLoopPHIs) { |
| 1469 | PHI->moveBefore(OuterLoopHeader->getFirstNonPHI()); |
| 1470 | assert(OuterInnerReductions.find(PHI) != OuterInnerReductions.end() && |
| 1471 | "Expected a reduction PHI node"); |
| 1472 | } |
Florian Hahn | d8fcf0d | 2018-06-19 08:03:24 +0000 | [diff] [blame] | 1473 | |
| 1474 | // Update the incoming blocks for moved PHI nodes. |
Roman Lebedev | 1a1b922 | 2019-05-05 18:59:39 +0000 | [diff] [blame] | 1475 | OuterLoopHeader->replacePhiUsesWith(InnerLoopPreHeader, OuterLoopPreHeader); |
| 1476 | OuterLoopHeader->replacePhiUsesWith(InnerLoopLatch, OuterLoopLatch); |
| 1477 | InnerLoopHeader->replacePhiUsesWith(OuterLoopPreHeader, InnerLoopPreHeader); |
| 1478 | InnerLoopHeader->replacePhiUsesWith(OuterLoopLatch, InnerLoopLatch); |
Florian Hahn | d8fcf0d | 2018-06-19 08:03:24 +0000 | [diff] [blame] | 1479 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1480 | return true; |
| 1481 | } |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1482 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 1483 | void LoopInterchangeTransform::adjustLoopPreheaders() { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1484 | // We have interchanged the preheaders so we need to interchange the data in |
| 1485 | // the preheader as well. |
| 1486 | // This is because the content of inner preheader was previously executed |
| 1487 | // inside the outer loop. |
| 1488 | BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader(); |
| 1489 | BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); |
| 1490 | BasicBlock *OuterLoopHeader = OuterLoop->getHeader(); |
| 1491 | BranchInst *InnerTermBI = |
| 1492 | cast<BranchInst>(InnerLoopPreHeader->getTerminator()); |
| 1493 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1494 | // These instructions should now be executed inside the loop. |
| 1495 | // Move instruction into a new block after outer header. |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 1496 | moveBBContents(InnerLoopPreHeader, OuterLoopHeader->getTerminator()); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1497 | // These instructions were not executed previously in the loop so move them to |
| 1498 | // the older inner loop preheader. |
Benjamin Kramer | 7944292 | 2015-03-06 18:59:14 +0000 | [diff] [blame] | 1499 | moveBBContents(OuterLoopPreHeader, InnerTermBI); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | bool LoopInterchangeTransform::adjustLoopLinks() { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1503 | // Adjust all branches in the inner and outer loop. |
| 1504 | bool Changed = adjustLoopBranches(); |
| 1505 | if (Changed) |
| 1506 | adjustLoopPreheaders(); |
| 1507 | return Changed; |
| 1508 | } |
| 1509 | |
| 1510 | char LoopInterchange::ID = 0; |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 1511 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1512 | INITIALIZE_PASS_BEGIN(LoopInterchange, "loop-interchange", |
| 1513 | "Interchanges loops for cache reuse", false, false) |
Florian Hahn | 8600fee | 2018-10-01 09:59:48 +0000 | [diff] [blame] | 1514 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 1515 | INITIALIZE_PASS_DEPENDENCY(DependenceAnalysisWrapperPass) |
Florian Hahn | ad99352 | 2017-07-15 13:13:19 +0000 | [diff] [blame] | 1516 | INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 1517 | |
| 1518 | INITIALIZE_PASS_END(LoopInterchange, "loop-interchange", |
| 1519 | "Interchanges loops for cache reuse", false, false) |
| 1520 | |
| 1521 | Pass *llvm::createLoopInterchangePass() { return new LoopInterchange(); } |