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