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