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