Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 1 | //===- LoopDistribute.cpp - Loop Distribution Pass ------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Loop Distribution Pass. Its main focus is to |
| 10 | // distribute loops that cannot be vectorized due to dependence cycles. It |
| 11 | // tries to isolate the offending dependences into a new loop allowing |
| 12 | // vectorization of the remaining parts. |
| 13 | // |
| 14 | // For dependence analysis, the pass uses the LoopVectorizer's |
| 15 | // LoopAccessAnalysis. Because this analysis presumes no change in the order of |
| 16 | // memory operations, special care is taken to preserve the lexical order of |
| 17 | // these operations. |
| 18 | // |
| 19 | // Similarly to the Vectorizer, the pass also supports loop versioning to |
| 20 | // run-time disambiguate potentially overlapping arrays. |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Scalar/LoopDistribute.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseMap.h" |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DepthFirstIterator.h" |
| 27 | #include "llvm/ADT/EquivalenceClasses.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Optional.h" |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
| 31 | #include "llvm/ADT/SmallVector.h" |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringRef.h" |
| 34 | #include "llvm/ADT/Twine.h" |
| 35 | #include "llvm/ADT/iterator_range.h" |
| 36 | #include "llvm/Analysis/AliasAnalysis.h" |
| 37 | #include "llvm/Analysis/AssumptionCache.h" |
Eli Friedman | 66fdba8 | 2016-09-16 18:01:48 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/GlobalsModRef.h" |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/LoopAccessAnalysis.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/LoopAnalysisManager.h" |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/LoopInfo.h" |
Adam Nemet | 0965da2 | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/ScalarEvolution.h" |
| 44 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 45 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 46 | #include "llvm/IR/BasicBlock.h" |
| 47 | #include "llvm/IR/Constants.h" |
Adam Nemet | 0ba164b | 2016-04-28 23:08:32 +0000 | [diff] [blame] | 48 | #include "llvm/IR/DiagnosticInfo.h" |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 49 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 50 | #include "llvm/IR/Function.h" |
| 51 | #include "llvm/IR/InstrTypes.h" |
| 52 | #include "llvm/IR/Instruction.h" |
| 53 | #include "llvm/IR/Instructions.h" |
| 54 | #include "llvm/IR/LLVMContext.h" |
| 55 | #include "llvm/IR/Metadata.h" |
| 56 | #include "llvm/IR/PassManager.h" |
| 57 | #include "llvm/IR/Value.h" |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 58 | #include "llvm/Pass.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 59 | #include "llvm/Support/Casting.h" |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 60 | #include "llvm/Support/CommandLine.h" |
| 61 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 62 | #include "llvm/Support/raw_ostream.h" |
| 63 | #include "llvm/Transforms/Scalar.h" |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 64 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 65 | #include "llvm/Transforms/Utils/Cloning.h" |
Ashutosh Nema | c5b7b55 | 2015-08-19 05:40:42 +0000 | [diff] [blame] | 66 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 67 | #include "llvm/Transforms/Utils/LoopVersioning.h" |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 68 | #include "llvm/Transforms/Utils/ValueMapper.h" |
| 69 | #include <cassert> |
| 70 | #include <functional> |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 71 | #include <list> |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 72 | #include <tuple> |
| 73 | #include <utility> |
| 74 | |
| 75 | using namespace llvm; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 76 | |
| 77 | #define LDIST_NAME "loop-distribute" |
| 78 | #define DEBUG_TYPE LDIST_NAME |
| 79 | |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 80 | /// @{ |
| 81 | /// Metadata attribute names |
| 82 | static const char *const LLVMLoopDistributeFollowupAll = |
| 83 | "llvm.loop.distribute.followup_all"; |
| 84 | static const char *const LLVMLoopDistributeFollowupCoincident = |
| 85 | "llvm.loop.distribute.followup_coincident"; |
| 86 | static const char *const LLVMLoopDistributeFollowupSequential = |
| 87 | "llvm.loop.distribute.followup_sequential"; |
| 88 | static const char *const LLVMLoopDistributeFollowupFallback = |
| 89 | "llvm.loop.distribute.followup_fallback"; |
| 90 | /// @} |
| 91 | |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 92 | static cl::opt<bool> |
| 93 | LDistVerify("loop-distribute-verify", cl::Hidden, |
| 94 | cl::desc("Turn on DominatorTree and LoopInfo verification " |
| 95 | "after Loop Distribution"), |
| 96 | cl::init(false)); |
| 97 | |
| 98 | static cl::opt<bool> DistributeNonIfConvertible( |
| 99 | "loop-distribute-non-if-convertible", cl::Hidden, |
| 100 | cl::desc("Whether to distribute into a loop that may not be " |
| 101 | "if-convertible by the loop vectorizer"), |
| 102 | cl::init(false)); |
| 103 | |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 104 | static cl::opt<unsigned> DistributeSCEVCheckThreshold( |
| 105 | "loop-distribute-scev-check-threshold", cl::init(8), cl::Hidden, |
| 106 | cl::desc("The maximum number of SCEV checks allowed for Loop " |
| 107 | "Distribution")); |
| 108 | |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 109 | static cl::opt<unsigned> PragmaDistributeSCEVCheckThreshold( |
| 110 | "loop-distribute-scev-check-threshold-with-pragma", cl::init(128), |
| 111 | cl::Hidden, |
| 112 | cl::desc( |
| 113 | "The maximum number of SCEV checks allowed for Loop " |
| 114 | "Distribution for loop marked with #pragma loop distribute(enable)")); |
| 115 | |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 116 | static cl::opt<bool> EnableLoopDistribute( |
| 117 | "enable-loop-distribute", cl::Hidden, |
Adam Nemet | 32e6a34 | 2016-12-21 04:07:40 +0000 | [diff] [blame] | 118 | cl::desc("Enable the new, experimental LoopDistribution Pass"), |
| 119 | cl::init(false)); |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 120 | |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 121 | STATISTIC(NumLoopsDistributed, "Number of loops distributed"); |
| 122 | |
Adam Nemet | 2f85b73 | 2015-05-14 12:33:32 +0000 | [diff] [blame] | 123 | namespace { |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 124 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 125 | /// Maintains the set of instructions of the loop for a partition before |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 126 | /// cloning. After cloning, it hosts the new loop. |
| 127 | class InstPartition { |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 128 | using InstructionSet = SmallPtrSet<Instruction *, 8>; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 129 | |
| 130 | public: |
| 131 | InstPartition(Instruction *I, Loop *L, bool DepCycle = false) |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 132 | : DepCycle(DepCycle), OrigLoop(L) { |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 133 | Set.insert(I); |
| 134 | } |
| 135 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 136 | /// Returns whether this partition contains a dependence cycle. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 137 | bool hasDepCycle() const { return DepCycle; } |
| 138 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 139 | /// Adds an instruction to this partition. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 140 | void add(Instruction *I) { Set.insert(I); } |
| 141 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 142 | /// Collection accessors. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 143 | InstructionSet::iterator begin() { return Set.begin(); } |
| 144 | InstructionSet::iterator end() { return Set.end(); } |
| 145 | InstructionSet::const_iterator begin() const { return Set.begin(); } |
| 146 | InstructionSet::const_iterator end() const { return Set.end(); } |
| 147 | bool empty() const { return Set.empty(); } |
| 148 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 149 | /// Moves this partition into \p Other. This partition becomes empty |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 150 | /// after this. |
| 151 | void moveTo(InstPartition &Other) { |
| 152 | Other.Set.insert(Set.begin(), Set.end()); |
| 153 | Set.clear(); |
| 154 | Other.DepCycle |= DepCycle; |
| 155 | } |
| 156 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 157 | /// Populates the partition with a transitive closure of all the |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 158 | /// instructions that the seeded instructions dependent on. |
| 159 | void populateUsedSet() { |
| 160 | // FIXME: We currently don't use control-dependence but simply include all |
| 161 | // blocks (possibly empty at the end) and let simplifycfg mostly clean this |
| 162 | // up. |
| 163 | for (auto *B : OrigLoop->getBlocks()) |
| 164 | Set.insert(B->getTerminator()); |
| 165 | |
| 166 | // Follow the use-def chains to form a transitive closure of all the |
| 167 | // instructions that the originally seeded instructions depend on. |
| 168 | SmallVector<Instruction *, 8> Worklist(Set.begin(), Set.end()); |
| 169 | while (!Worklist.empty()) { |
| 170 | Instruction *I = Worklist.pop_back_val(); |
| 171 | // Insert instructions from the loop that we depend on. |
| 172 | for (Value *V : I->operand_values()) { |
| 173 | auto *I = dyn_cast<Instruction>(V); |
| 174 | if (I && OrigLoop->contains(I->getParent()) && Set.insert(I).second) |
| 175 | Worklist.push_back(I); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 180 | /// Clones the original loop. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 181 | /// |
| 182 | /// Updates LoopInfo and DominatorTree using the information that block \p |
| 183 | /// LoopDomBB dominates the loop. |
| 184 | Loop *cloneLoopWithPreheader(BasicBlock *InsertBefore, BasicBlock *LoopDomBB, |
| 185 | unsigned Index, LoopInfo *LI, |
| 186 | DominatorTree *DT) { |
| 187 | ClonedLoop = ::cloneLoopWithPreheader(InsertBefore, LoopDomBB, OrigLoop, |
| 188 | VMap, Twine(".ldist") + Twine(Index), |
| 189 | LI, DT, ClonedLoopBlocks); |
| 190 | return ClonedLoop; |
| 191 | } |
| 192 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 193 | /// The cloned loop. If this partition is mapped to the original loop, |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 194 | /// this is null. |
| 195 | const Loop *getClonedLoop() const { return ClonedLoop; } |
| 196 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 197 | /// Returns the loop where this partition ends up after distribution. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 198 | /// If this partition is mapped to the original loop then use the block from |
| 199 | /// the loop. |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 200 | Loop *getDistributedLoop() const { |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 201 | return ClonedLoop ? ClonedLoop : OrigLoop; |
| 202 | } |
| 203 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 204 | /// The VMap that is populated by cloning and then used in |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 205 | /// remapinstruction to remap the cloned instructions. |
| 206 | ValueToValueMapTy &getVMap() { return VMap; } |
| 207 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 208 | /// Remaps the cloned instructions using VMap. |
Adam Nemet | 1a68918 | 2015-07-10 18:55:09 +0000 | [diff] [blame] | 209 | void remapInstructions() { |
| 210 | remapInstructionsInBlocks(ClonedLoopBlocks, VMap); |
| 211 | } |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 212 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 213 | /// Based on the set of instructions selected for this partition, |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 214 | /// removes the unnecessary ones. |
| 215 | void removeUnusedInsts() { |
| 216 | SmallVector<Instruction *, 8> Unused; |
| 217 | |
| 218 | for (auto *Block : OrigLoop->getBlocks()) |
| 219 | for (auto &Inst : *Block) |
| 220 | if (!Set.count(&Inst)) { |
| 221 | Instruction *NewInst = &Inst; |
| 222 | if (!VMap.empty()) |
| 223 | NewInst = cast<Instruction>(VMap[NewInst]); |
| 224 | |
| 225 | assert(!isa<BranchInst>(NewInst) && |
| 226 | "Branches are marked used early on"); |
| 227 | Unused.push_back(NewInst); |
| 228 | } |
| 229 | |
| 230 | // Delete the instructions backwards, as it has a reduced likelihood of |
| 231 | // having to update as many def-use and use-def chains. |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 232 | for (auto *Inst : reverse(Unused)) { |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 233 | if (!Inst->use_empty()) |
| 234 | Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); |
| 235 | Inst->eraseFromParent(); |
| 236 | } |
| 237 | } |
| 238 | |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 239 | void print() const { |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 240 | if (DepCycle) |
| 241 | dbgs() << " (cycle)\n"; |
| 242 | for (auto *I : Set) |
| 243 | // Prefix with the block name. |
| 244 | dbgs() << " " << I->getParent()->getName() << ":" << *I << "\n"; |
| 245 | } |
| 246 | |
| 247 | void printBlocks() const { |
| 248 | for (auto *BB : getDistributedLoop()->getBlocks()) |
| 249 | dbgs() << *BB; |
| 250 | } |
| 251 | |
| 252 | private: |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 253 | /// Instructions from OrigLoop selected for this partition. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 254 | InstructionSet Set; |
| 255 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 256 | /// Whether this partition contains a dependence cycle. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 257 | bool DepCycle; |
| 258 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 259 | /// The original loop. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 260 | Loop *OrigLoop; |
| 261 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 262 | /// The cloned loop. If this partition is mapped to the original loop, |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 263 | /// this is null. |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 264 | Loop *ClonedLoop = nullptr; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 265 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 266 | /// The blocks of ClonedLoop including the preheader. If this |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 267 | /// partition is mapped to the original loop, this is empty. |
| 268 | SmallVector<BasicBlock *, 8> ClonedLoopBlocks; |
| 269 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 270 | /// These gets populated once the set of instructions have been |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 271 | /// finalized. If this partition is mapped to the original loop, these are not |
| 272 | /// set. |
| 273 | ValueToValueMapTy VMap; |
| 274 | }; |
| 275 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 276 | /// Holds the set of Partitions. It populates them, merges them and then |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 277 | /// clones the loops. |
| 278 | class InstPartitionContainer { |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 279 | using InstToPartitionIdT = DenseMap<Instruction *, int>; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 280 | |
| 281 | public: |
| 282 | InstPartitionContainer(Loop *L, LoopInfo *LI, DominatorTree *DT) |
| 283 | : L(L), LI(LI), DT(DT) {} |
| 284 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 285 | /// Returns the number of partitions. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 286 | unsigned getSize() const { return PartitionContainer.size(); } |
| 287 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 288 | /// Adds \p Inst into the current partition if that is marked to |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 289 | /// contain cycles. Otherwise start a new partition for it. |
| 290 | void addToCyclicPartition(Instruction *Inst) { |
| 291 | // If the current partition is non-cyclic. Start a new one. |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 292 | if (PartitionContainer.empty() || !PartitionContainer.back().hasDepCycle()) |
| 293 | PartitionContainer.emplace_back(Inst, L, /*DepCycle=*/true); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 294 | else |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 295 | PartitionContainer.back().add(Inst); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 298 | /// Adds \p Inst into a partition that is not marked to contain |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 299 | /// dependence cycles. |
| 300 | /// |
| 301 | // Initially we isolate memory instructions into as many partitions as |
| 302 | // possible, then later we may merge them back together. |
| 303 | void addToNewNonCyclicPartition(Instruction *Inst) { |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 304 | PartitionContainer.emplace_back(Inst, L); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 307 | /// Merges adjacent non-cyclic partitions. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 308 | /// |
| 309 | /// The idea is that we currently only want to isolate the non-vectorizable |
| 310 | /// partition. We could later allow more distribution among these partition |
| 311 | /// too. |
| 312 | void mergeAdjacentNonCyclic() { |
| 313 | mergeAdjacentPartitionsIf( |
| 314 | [](const InstPartition *P) { return !P->hasDepCycle(); }); |
| 315 | } |
| 316 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 317 | /// If a partition contains only conditional stores, we won't vectorize |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 318 | /// it. Try to merge it with a previous cyclic partition. |
| 319 | void mergeNonIfConvertible() { |
| 320 | mergeAdjacentPartitionsIf([&](const InstPartition *Partition) { |
| 321 | if (Partition->hasDepCycle()) |
| 322 | return true; |
| 323 | |
| 324 | // Now, check if all stores are conditional in this partition. |
| 325 | bool seenStore = false; |
| 326 | |
| 327 | for (auto *Inst : *Partition) |
| 328 | if (isa<StoreInst>(Inst)) { |
| 329 | seenStore = true; |
| 330 | if (!LoopAccessInfo::blockNeedsPredication(Inst->getParent(), L, DT)) |
| 331 | return false; |
| 332 | } |
| 333 | return seenStore; |
| 334 | }); |
| 335 | } |
| 336 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 337 | /// Merges the partitions according to various heuristics. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 338 | void mergeBeforePopulating() { |
| 339 | mergeAdjacentNonCyclic(); |
| 340 | if (!DistributeNonIfConvertible) |
| 341 | mergeNonIfConvertible(); |
| 342 | } |
| 343 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 344 | /// Merges partitions in order to ensure that no loads are duplicated. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 345 | /// |
| 346 | /// We can't duplicate loads because that could potentially reorder them. |
| 347 | /// LoopAccessAnalysis provides dependency information with the context that |
| 348 | /// the order of memory operation is preserved. |
| 349 | /// |
| 350 | /// Return if any partitions were merged. |
| 351 | bool mergeToAvoidDuplicatedLoads() { |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 352 | using LoadToPartitionT = DenseMap<Instruction *, InstPartition *>; |
| 353 | using ToBeMergedT = EquivalenceClasses<InstPartition *>; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 354 | |
| 355 | LoadToPartitionT LoadToPartition; |
| 356 | ToBeMergedT ToBeMerged; |
| 357 | |
| 358 | // Step through the partitions and create equivalence between partitions |
| 359 | // that contain the same load. Also put partitions in between them in the |
| 360 | // same equivalence class to avoid reordering of memory operations. |
| 361 | for (PartitionContainerT::iterator I = PartitionContainer.begin(), |
| 362 | E = PartitionContainer.end(); |
| 363 | I != E; ++I) { |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 364 | auto *PartI = &*I; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 365 | |
| 366 | // If a load occurs in two partitions PartI and PartJ, merge all |
| 367 | // partitions (PartI, PartJ] into PartI. |
| 368 | for (Instruction *Inst : *PartI) |
| 369 | if (isa<LoadInst>(Inst)) { |
| 370 | bool NewElt; |
| 371 | LoadToPartitionT::iterator LoadToPart; |
| 372 | |
| 373 | std::tie(LoadToPart, NewElt) = |
| 374 | LoadToPartition.insert(std::make_pair(Inst, PartI)); |
| 375 | if (!NewElt) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 376 | LLVM_DEBUG(dbgs() |
| 377 | << "Merging partitions due to this load in multiple " |
| 378 | << "partitions: " << PartI << ", " << LoadToPart->second |
| 379 | << "\n" |
| 380 | << *Inst << "\n"); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 381 | |
| 382 | auto PartJ = I; |
| 383 | do { |
| 384 | --PartJ; |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 385 | ToBeMerged.unionSets(PartI, &*PartJ); |
| 386 | } while (&*PartJ != LoadToPart->second); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | if (ToBeMerged.empty()) |
| 391 | return false; |
| 392 | |
| 393 | // Merge the member of an equivalence class into its class leader. This |
| 394 | // makes the members empty. |
| 395 | for (ToBeMergedT::iterator I = ToBeMerged.begin(), E = ToBeMerged.end(); |
| 396 | I != E; ++I) { |
| 397 | if (!I->isLeader()) |
| 398 | continue; |
| 399 | |
| 400 | auto PartI = I->getData(); |
| 401 | for (auto PartJ : make_range(std::next(ToBeMerged.member_begin(I)), |
| 402 | ToBeMerged.member_end())) { |
| 403 | PartJ->moveTo(*PartI); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // Remove the empty partitions. |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 408 | PartitionContainer.remove_if( |
| 409 | [](const InstPartition &P) { return P.empty(); }); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 410 | |
| 411 | return true; |
| 412 | } |
| 413 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 414 | /// Sets up the mapping between instructions to partitions. If the |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 415 | /// instruction is duplicated across multiple partitions, set the entry to -1. |
| 416 | void setupPartitionIdOnInstructions() { |
| 417 | int PartitionID = 0; |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 418 | for (const auto &Partition : PartitionContainer) { |
| 419 | for (Instruction *Inst : Partition) { |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 420 | bool NewElt; |
| 421 | InstToPartitionIdT::iterator Iter; |
| 422 | |
| 423 | std::tie(Iter, NewElt) = |
| 424 | InstToPartitionId.insert(std::make_pair(Inst, PartitionID)); |
| 425 | if (!NewElt) |
| 426 | Iter->second = -1; |
| 427 | } |
| 428 | ++PartitionID; |
| 429 | } |
| 430 | } |
| 431 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 432 | /// Populates the partition with everything that the seeding |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 433 | /// instructions require. |
| 434 | void populateUsedSet() { |
| 435 | for (auto &P : PartitionContainer) |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 436 | P.populateUsedSet(); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 439 | /// This performs the main chunk of the work of cloning the loops for |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 440 | /// the partitions. |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 441 | void cloneLoops() { |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 442 | BasicBlock *OrigPH = L->getLoopPreheader(); |
| 443 | // At this point the predecessor of the preheader is either the memcheck |
| 444 | // block or the top part of the original preheader. |
| 445 | BasicBlock *Pred = OrigPH->getSinglePredecessor(); |
| 446 | assert(Pred && "Preheader does not have a single predecessor"); |
| 447 | BasicBlock *ExitBlock = L->getExitBlock(); |
| 448 | assert(ExitBlock && "No single exit block"); |
| 449 | Loop *NewLoop; |
| 450 | |
| 451 | assert(!PartitionContainer.empty() && "at least two partitions expected"); |
| 452 | // We're cloning the preheader along with the loop so we already made sure |
| 453 | // it was empty. |
| 454 | assert(&*OrigPH->begin() == OrigPH->getTerminator() && |
| 455 | "preheader not empty"); |
| 456 | |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 457 | // Preserve the original loop ID for use after the transformation. |
| 458 | MDNode *OrigLoopID = L->getLoopID(); |
| 459 | |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 460 | // Create a loop for each partition except the last. Clone the original |
| 461 | // loop before PH along with adding a preheader for the cloned loop. Then |
| 462 | // update PH to point to the newly added preheader. |
| 463 | BasicBlock *TopPH = OrigPH; |
| 464 | unsigned Index = getSize() - 1; |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 465 | for (auto I = std::next(PartitionContainer.rbegin()), |
| 466 | E = PartitionContainer.rend(); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 467 | I != E; ++I, --Index, TopPH = NewLoop->getLoopPreheader()) { |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 468 | auto *Part = &*I; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 469 | |
| 470 | NewLoop = Part->cloneLoopWithPreheader(TopPH, Pred, Index, LI, DT); |
| 471 | |
| 472 | Part->getVMap()[ExitBlock] = TopPH; |
| 473 | Part->remapInstructions(); |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 474 | setNewLoopID(OrigLoopID, Part); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 475 | } |
| 476 | Pred->getTerminator()->replaceUsesOfWith(OrigPH, TopPH); |
| 477 | |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 478 | // Also set a new loop ID for the last loop. |
| 479 | setNewLoopID(OrigLoopID, &PartitionContainer.back()); |
| 480 | |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 481 | // Now go in forward order and update the immediate dominator for the |
| 482 | // preheaders with the exiting block of the previous loop. Dominance |
| 483 | // within the loop is updated in cloneLoopWithPreheader. |
| 484 | for (auto Curr = PartitionContainer.cbegin(), |
| 485 | Next = std::next(PartitionContainer.cbegin()), |
| 486 | E = PartitionContainer.cend(); |
| 487 | Next != E; ++Curr, ++Next) |
| 488 | DT->changeImmediateDominator( |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 489 | Next->getDistributedLoop()->getLoopPreheader(), |
| 490 | Curr->getDistributedLoop()->getExitingBlock()); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 493 | /// Removes the dead instructions from the cloned loops. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 494 | void removeUnusedInsts() { |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 495 | for (auto &Partition : PartitionContainer) |
| 496 | Partition.removeUnusedInsts(); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 499 | /// For each memory pointer, it computes the partitionId the pointer is |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 500 | /// used in. |
| 501 | /// |
| 502 | /// This returns an array of int where the I-th entry corresponds to I-th |
| 503 | /// entry in LAI.getRuntimePointerCheck(). If the pointer is used in multiple |
| 504 | /// partitions its entry is set to -1. |
| 505 | SmallVector<int, 8> |
| 506 | computePartitionSetForPointers(const LoopAccessInfo &LAI) { |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 507 | const RuntimePointerChecking *RtPtrCheck = LAI.getRuntimePointerChecking(); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 508 | |
| 509 | unsigned N = RtPtrCheck->Pointers.size(); |
| 510 | SmallVector<int, 8> PtrToPartitions(N); |
| 511 | for (unsigned I = 0; I < N; ++I) { |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 512 | Value *Ptr = RtPtrCheck->Pointers[I].PointerValue; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 513 | auto Instructions = |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 514 | LAI.getInstructionsForAccess(Ptr, RtPtrCheck->Pointers[I].IsWritePtr); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 515 | |
| 516 | int &Partition = PtrToPartitions[I]; |
| 517 | // First set it to uninitialized. |
| 518 | Partition = -2; |
| 519 | for (Instruction *Inst : Instructions) { |
| 520 | // Note that this could be -1 if Inst is duplicated across multiple |
| 521 | // partitions. |
| 522 | int ThisPartition = this->InstToPartitionId[Inst]; |
| 523 | if (Partition == -2) |
| 524 | Partition = ThisPartition; |
| 525 | // -1 means belonging to multiple partitions. |
| 526 | else if (Partition == -1) |
| 527 | break; |
| 528 | else if (Partition != (int)ThisPartition) |
| 529 | Partition = -1; |
| 530 | } |
| 531 | assert(Partition != -2 && "Pointer not belonging to any partition"); |
| 532 | } |
| 533 | |
| 534 | return PtrToPartitions; |
| 535 | } |
| 536 | |
| 537 | void print(raw_ostream &OS) const { |
| 538 | unsigned Index = 0; |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 539 | for (const auto &P : PartitionContainer) { |
| 540 | OS << "Partition " << Index++ << " (" << &P << "):\n"; |
| 541 | P.print(); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | void dump() const { print(dbgs()); } |
| 546 | |
| 547 | #ifndef NDEBUG |
| 548 | friend raw_ostream &operator<<(raw_ostream &OS, |
| 549 | const InstPartitionContainer &Partitions) { |
| 550 | Partitions.print(OS); |
| 551 | return OS; |
| 552 | } |
| 553 | #endif |
| 554 | |
| 555 | void printBlocks() const { |
| 556 | unsigned Index = 0; |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 557 | for (const auto &P : PartitionContainer) { |
| 558 | dbgs() << "\nPartition " << Index++ << " (" << &P << "):\n"; |
| 559 | P.printBlocks(); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | private: |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 564 | using PartitionContainerT = std::list<InstPartition>; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 565 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 566 | /// List of partitions. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 567 | PartitionContainerT PartitionContainer; |
| 568 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 569 | /// Mapping from Instruction to partition Id. If the instruction |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 570 | /// belongs to multiple partitions the entry contains -1. |
| 571 | InstToPartitionIdT InstToPartitionId; |
| 572 | |
| 573 | Loop *L; |
| 574 | LoopInfo *LI; |
| 575 | DominatorTree *DT; |
| 576 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 577 | /// The control structure to merge adjacent partitions if both satisfy |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 578 | /// the \p Predicate. |
| 579 | template <class UnaryPredicate> |
| 580 | void mergeAdjacentPartitionsIf(UnaryPredicate Predicate) { |
| 581 | InstPartition *PrevMatch = nullptr; |
| 582 | for (auto I = PartitionContainer.begin(); I != PartitionContainer.end();) { |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 583 | auto DoesMatch = Predicate(&*I); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 584 | if (PrevMatch == nullptr && DoesMatch) { |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 585 | PrevMatch = &*I; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 586 | ++I; |
| 587 | } else if (PrevMatch != nullptr && DoesMatch) { |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 588 | I->moveTo(*PrevMatch); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 589 | I = PartitionContainer.erase(I); |
| 590 | } else { |
| 591 | PrevMatch = nullptr; |
| 592 | ++I; |
| 593 | } |
| 594 | } |
| 595 | } |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 596 | |
| 597 | /// Assign new LoopIDs for the partition's cloned loop. |
| 598 | void setNewLoopID(MDNode *OrigLoopID, InstPartition *Part) { |
| 599 | Optional<MDNode *> PartitionID = makeFollowupLoopID( |
| 600 | OrigLoopID, |
| 601 | {LLVMLoopDistributeFollowupAll, |
| 602 | Part->hasDepCycle() ? LLVMLoopDistributeFollowupSequential |
| 603 | : LLVMLoopDistributeFollowupCoincident}); |
| 604 | if (PartitionID.hasValue()) { |
| 605 | Loop *NewLoop = Part->getDistributedLoop(); |
| 606 | NewLoop->setLoopID(PartitionID.getValue()); |
| 607 | } |
| 608 | } |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 609 | }; |
| 610 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 611 | /// For each memory instruction, this class maintains difference of the |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 612 | /// number of unsafe dependences that start out from this instruction minus |
| 613 | /// those that end here. |
| 614 | /// |
| 615 | /// By traversing the memory instructions in program order and accumulating this |
| 616 | /// number, we know whether any unsafe dependence crosses over a program point. |
| 617 | class MemoryInstructionDependences { |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 618 | using Dependence = MemoryDepChecker::Dependence; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 619 | |
| 620 | public: |
| 621 | struct Entry { |
| 622 | Instruction *Inst; |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 623 | unsigned NumUnsafeDependencesStartOrEnd = 0; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 624 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 625 | Entry(Instruction *Inst) : Inst(Inst) {} |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 626 | }; |
| 627 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 628 | using AccessesType = SmallVector<Entry, 8>; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 629 | |
| 630 | AccessesType::const_iterator begin() const { return Accesses.begin(); } |
| 631 | AccessesType::const_iterator end() const { return Accesses.end(); } |
| 632 | |
| 633 | MemoryInstructionDependences( |
| 634 | const SmallVectorImpl<Instruction *> &Instructions, |
Adam Nemet | a2df750 | 2015-11-03 21:39:52 +0000 | [diff] [blame] | 635 | const SmallVectorImpl<Dependence> &Dependences) { |
Benjamin Kramer | e6987bf | 2015-05-21 18:32:07 +0000 | [diff] [blame] | 636 | Accesses.append(Instructions.begin(), Instructions.end()); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 637 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 638 | LLVM_DEBUG(dbgs() << "Backward dependences:\n"); |
Adam Nemet | a2df750 | 2015-11-03 21:39:52 +0000 | [diff] [blame] | 639 | for (auto &Dep : Dependences) |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 640 | if (Dep.isPossiblyBackward()) { |
| 641 | // Note that the designations source and destination follow the program |
| 642 | // order, i.e. source is always first. (The direction is given by the |
| 643 | // DepType.) |
| 644 | ++Accesses[Dep.Source].NumUnsafeDependencesStartOrEnd; |
| 645 | --Accesses[Dep.Destination].NumUnsafeDependencesStartOrEnd; |
| 646 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 647 | LLVM_DEBUG(Dep.print(dbgs(), 2, Instructions)); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | private: |
| 652 | AccessesType Accesses; |
| 653 | }; |
| 654 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 655 | /// The actual class performing the per-loop work. |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 656 | class LoopDistributeForLoop { |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 657 | public: |
Adam Nemet | eff7664 | 2016-05-13 04:20:31 +0000 | [diff] [blame] | 658 | LoopDistributeForLoop(Loop *L, Function *F, LoopInfo *LI, DominatorTree *DT, |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 659 | ScalarEvolution *SE, OptimizationRemarkEmitter *ORE) |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 660 | : L(L), F(F), LI(LI), DT(DT), SE(SE), ORE(ORE) { |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 661 | setForced(); |
| 662 | } |
Adam Nemet | c75ad69 | 2015-07-30 03:29:16 +0000 | [diff] [blame] | 663 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 664 | /// Try to distribute an inner-most loop. |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 665 | bool processLoop(std::function<const LoopAccessInfo &(Loop &)> &GetLAA) { |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 666 | assert(L->empty() && "Only process inner loops."); |
| 667 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 668 | LLVM_DEBUG(dbgs() << "\nLDist: In \"" |
| 669 | << L->getHeader()->getParent()->getName() |
| 670 | << "\" checking " << *L << "\n"); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 671 | |
Adam Nemet | 7f38e11 | 2016-04-28 23:08:27 +0000 | [diff] [blame] | 672 | if (!L->getExitBlock()) |
Adam Nemet | f744ad7 | 2016-09-30 04:56:25 +0000 | [diff] [blame] | 673 | return fail("MultipleExitBlocks", "multiple exit blocks"); |
Florian Hahn | 2e03213 | 2016-12-19 17:13:37 +0000 | [diff] [blame] | 674 | if (!L->isLoopSimplifyForm()) |
| 675 | return fail("NotLoopSimplifyForm", |
| 676 | "loop is not in loop-simplify form"); |
| 677 | |
| 678 | BasicBlock *PH = L->getLoopPreheader(); |
Adam Nemet | eff7664 | 2016-05-13 04:20:31 +0000 | [diff] [blame] | 679 | |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 680 | // LAA will check that we only have a single exiting block. |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 681 | LAI = &GetLAA(*L); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 682 | |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 683 | // Currently, we only distribute to isolate the part of the loop with |
| 684 | // dependence cycles to enable partial vectorization. |
Adam Nemet | eff7664 | 2016-05-13 04:20:31 +0000 | [diff] [blame] | 685 | if (LAI->canVectorizeMemory()) |
Adam Nemet | f744ad7 | 2016-09-30 04:56:25 +0000 | [diff] [blame] | 686 | return fail("MemOpsCanBeVectorized", |
| 687 | "memory operations are safe for vectorization"); |
Adam Nemet | 7f38e11 | 2016-04-28 23:08:27 +0000 | [diff] [blame] | 688 | |
Adam Nemet | eff7664 | 2016-05-13 04:20:31 +0000 | [diff] [blame] | 689 | auto *Dependences = LAI->getDepChecker().getDependences(); |
Adam Nemet | 7f38e11 | 2016-04-28 23:08:27 +0000 | [diff] [blame] | 690 | if (!Dependences || Dependences->empty()) |
Adam Nemet | f744ad7 | 2016-09-30 04:56:25 +0000 | [diff] [blame] | 691 | return fail("NoUnsafeDeps", "no unsafe dependences to isolate"); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 692 | |
| 693 | InstPartitionContainer Partitions(L, LI, DT); |
| 694 | |
| 695 | // First, go through each memory operation and assign them to consecutive |
| 696 | // partitions (the order of partitions follows program order). Put those |
| 697 | // with unsafe dependences into "cyclic" partition otherwise put each store |
| 698 | // in its own "non-cyclic" partition (we'll merge these later). |
| 699 | // |
| 700 | // Note that a memory operation (e.g. Load2 below) at a program point that |
| 701 | // has an unsafe dependence (Store3->Load1) spanning over it must be |
| 702 | // included in the same cyclic partition as the dependent operations. This |
| 703 | // is to preserve the original program order after distribution. E.g.: |
| 704 | // |
| 705 | // NumUnsafeDependencesStartOrEnd NumUnsafeDependencesActive |
| 706 | // Load1 -. 1 0->1 |
| 707 | // Load2 | /Unsafe/ 0 1 |
| 708 | // Store3 -' -1 1->0 |
| 709 | // Load4 0 0 |
| 710 | // |
| 711 | // NumUnsafeDependencesActive > 0 indicates this situation and in this case |
| 712 | // we just keep assigning to the same cyclic partition until |
| 713 | // NumUnsafeDependencesActive reaches 0. |
Adam Nemet | eff7664 | 2016-05-13 04:20:31 +0000 | [diff] [blame] | 714 | const MemoryDepChecker &DepChecker = LAI->getDepChecker(); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 715 | MemoryInstructionDependences MID(DepChecker.getMemoryInstructions(), |
Adam Nemet | a2df750 | 2015-11-03 21:39:52 +0000 | [diff] [blame] | 716 | *Dependences); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 717 | |
| 718 | int NumUnsafeDependencesActive = 0; |
| 719 | for (auto &InstDep : MID) { |
| 720 | Instruction *I = InstDep.Inst; |
| 721 | // We update NumUnsafeDependencesActive post-instruction, catch the |
| 722 | // start of a dependence directly via NumUnsafeDependencesStartOrEnd. |
| 723 | if (NumUnsafeDependencesActive || |
| 724 | InstDep.NumUnsafeDependencesStartOrEnd > 0) |
| 725 | Partitions.addToCyclicPartition(I); |
| 726 | else |
| 727 | Partitions.addToNewNonCyclicPartition(I); |
| 728 | NumUnsafeDependencesActive += InstDep.NumUnsafeDependencesStartOrEnd; |
| 729 | assert(NumUnsafeDependencesActive >= 0 && |
| 730 | "Negative number of dependences active"); |
| 731 | } |
| 732 | |
| 733 | // Add partitions for values used outside. These partitions can be out of |
| 734 | // order from the original program order. This is OK because if the |
| 735 | // partition uses a load we will merge this partition with the original |
| 736 | // partition of the load that we set up in the previous loop (see |
| 737 | // mergeToAvoidDuplicatedLoads). |
| 738 | auto DefsUsedOutside = findDefsUsedOutsideOfLoop(L); |
| 739 | for (auto *Inst : DefsUsedOutside) |
| 740 | Partitions.addToNewNonCyclicPartition(Inst); |
| 741 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 742 | LLVM_DEBUG(dbgs() << "Seeded partitions:\n" << Partitions); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 743 | if (Partitions.getSize() < 2) |
Adam Nemet | f744ad7 | 2016-09-30 04:56:25 +0000 | [diff] [blame] | 744 | return fail("CantIsolateUnsafeDeps", |
| 745 | "cannot isolate unsafe dependencies"); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 746 | |
| 747 | // Run the merge heuristics: Merge non-cyclic adjacent partitions since we |
| 748 | // should be able to vectorize these together. |
| 749 | Partitions.mergeBeforePopulating(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 750 | LLVM_DEBUG(dbgs() << "\nMerged partitions:\n" << Partitions); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 751 | if (Partitions.getSize() < 2) |
Adam Nemet | f744ad7 | 2016-09-30 04:56:25 +0000 | [diff] [blame] | 752 | return fail("CantIsolateUnsafeDeps", |
| 753 | "cannot isolate unsafe dependencies"); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 754 | |
| 755 | // Now, populate the partitions with non-memory operations. |
| 756 | Partitions.populateUsedSet(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 757 | LLVM_DEBUG(dbgs() << "\nPopulated partitions:\n" << Partitions); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 758 | |
| 759 | // In order to preserve original lexical order for loads, keep them in the |
| 760 | // partition that we set up in the MemoryInstructionDependences loop. |
| 761 | if (Partitions.mergeToAvoidDuplicatedLoads()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 762 | LLVM_DEBUG(dbgs() << "\nPartitions merged to ensure unique loads:\n" |
| 763 | << Partitions); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 764 | if (Partitions.getSize() < 2) |
Adam Nemet | f744ad7 | 2016-09-30 04:56:25 +0000 | [diff] [blame] | 765 | return fail("CantIsolateUnsafeDeps", |
| 766 | "cannot isolate unsafe dependencies"); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 769 | // Don't distribute the loop if we need too many SCEV run-time checks. |
Xinliang David Li | 94734ee | 2016-07-01 05:59:55 +0000 | [diff] [blame] | 770 | const SCEVUnionPredicate &Pred = LAI->getPSE().getUnionPredicate(); |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 771 | if (Pred.getComplexity() > (IsForced.getValueOr(false) |
| 772 | ? PragmaDistributeSCEVCheckThreshold |
Adam Nemet | 7f38e11 | 2016-04-28 23:08:27 +0000 | [diff] [blame] | 773 | : DistributeSCEVCheckThreshold)) |
Adam Nemet | f744ad7 | 2016-09-30 04:56:25 +0000 | [diff] [blame] | 774 | return fail("TooManySCEVRuntimeChecks", |
| 775 | "too many SCEV run-time checks needed.\n"); |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 776 | |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 777 | if (!IsForced.getValueOr(false) && hasDisableAllTransformsHint(L)) |
| 778 | return fail("HeuristicDisabled", "distribution heuristic disabled"); |
| 779 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 780 | LLVM_DEBUG(dbgs() << "\nDistributing loop: " << *L << "\n"); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 781 | // We're done forming the partitions set up the reverse mapping from |
| 782 | // instructions to partitions. |
| 783 | Partitions.setupPartitionIdOnInstructions(); |
| 784 | |
| 785 | // To keep things simple have an empty preheader before we version or clone |
| 786 | // the loop. (Also split if this has no predecessor, i.e. entry, because we |
| 787 | // rely on PH having a predecessor.) |
| 788 | if (!PH->getSinglePredecessor() || &*PH->begin() != PH->getTerminator()) |
| 789 | SplitBlock(PH, PH->getTerminator(), DT, LI); |
| 790 | |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 791 | // If we need run-time checks, version the loop now. |
Adam Nemet | eff7664 | 2016-05-13 04:20:31 +0000 | [diff] [blame] | 792 | auto PtrToPartition = Partitions.computePartitionSetForPointers(*LAI); |
| 793 | const auto *RtPtrChecking = LAI->getRuntimePointerChecking(); |
Adam Nemet | 1584039 | 2015-08-07 22:44:15 +0000 | [diff] [blame] | 794 | const auto &AllChecks = RtPtrChecking->getChecks(); |
Adam Nemet | c75ad69 | 2015-07-30 03:29:16 +0000 | [diff] [blame] | 795 | auto Checks = includeOnlyCrossPartitionChecks(AllChecks, PtrToPartition, |
| 796 | RtPtrChecking); |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 797 | |
| 798 | if (!Pred.isAlwaysTrue() || !Checks.empty()) { |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 799 | MDNode *OrigLoopID = L->getLoopID(); |
| 800 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 801 | LLVM_DEBUG(dbgs() << "\nPointers:\n"); |
| 802 | LLVM_DEBUG(LAI->getRuntimePointerChecking()->printChecks(dbgs(), Checks)); |
Adam Nemet | eff7664 | 2016-05-13 04:20:31 +0000 | [diff] [blame] | 803 | LoopVersioning LVer(*LAI, L, LI, DT, SE, false); |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 804 | LVer.setAliasChecks(std::move(Checks)); |
Xinliang David Li | 94734ee | 2016-07-01 05:59:55 +0000 | [diff] [blame] | 805 | LVer.setSCEVChecks(LAI->getPSE().getUnionPredicate()); |
Adam Nemet | e481340 | 2015-08-20 17:22:29 +0000 | [diff] [blame] | 806 | LVer.versionLoop(DefsUsedOutside); |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 807 | LVer.annotateLoopWithNoAlias(); |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 808 | |
| 809 | // The unversioned loop will not be changed, so we inherit all attributes |
| 810 | // from the original loop, but remove the loop distribution metadata to |
| 811 | // avoid to distribute it again. |
| 812 | MDNode *UnversionedLoopID = |
| 813 | makeFollowupLoopID(OrigLoopID, |
| 814 | {LLVMLoopDistributeFollowupAll, |
| 815 | LLVMLoopDistributeFollowupFallback}, |
| 816 | "llvm.loop.distribute.", true) |
| 817 | .getValue(); |
| 818 | LVer.getNonVersionedLoop()->setLoopID(UnversionedLoopID); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | // Create identical copies of the original loop for each partition and hook |
| 822 | // them up sequentially. |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 823 | Partitions.cloneLoops(); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 824 | |
| 825 | // Now, we remove the instruction from each loop that don't belong to that |
| 826 | // partition. |
| 827 | Partitions.removeUnusedInsts(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 828 | LLVM_DEBUG(dbgs() << "\nAfter removing unused Instrs:\n"); |
| 829 | LLVM_DEBUG(Partitions.printBlocks()); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 830 | |
| 831 | if (LDistVerify) { |
Michael Zolotukhin | e0b2d97 | 2016-08-31 19:26:19 +0000 | [diff] [blame] | 832 | LI->verify(*DT); |
David Green | 7c35de1 | 2018-02-28 11:00:08 +0000 | [diff] [blame] | 833 | assert(DT->verify(DominatorTree::VerificationLevel::Fast)); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | ++NumLoopsDistributed; |
Adam Nemet | 88ec491 | 2016-04-29 07:10:46 +0000 | [diff] [blame] | 837 | // Report the success. |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 838 | ORE->emit([&]() { |
| 839 | return OptimizationRemark(LDIST_NAME, "Distribute", L->getStartLoc(), |
| 840 | L->getHeader()) |
| 841 | << "distributed loop"; |
| 842 | }); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 843 | return true; |
| 844 | } |
| 845 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 846 | /// Provide diagnostics then \return with false. |
Adam Nemet | f744ad7 | 2016-09-30 04:56:25 +0000 | [diff] [blame] | 847 | bool fail(StringRef RemarkName, StringRef Message) { |
Adam Nemet | 0ba164b | 2016-04-28 23:08:32 +0000 | [diff] [blame] | 848 | LLVMContext &Ctx = F->getContext(); |
| 849 | bool Forced = isForced().getValueOr(false); |
| 850 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 851 | LLVM_DEBUG(dbgs() << "Skipping; " << Message << "\n"); |
Adam Nemet | 0ba164b | 2016-04-28 23:08:32 +0000 | [diff] [blame] | 852 | |
| 853 | // With Rpass-missed report that distribution failed. |
Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 854 | ORE->emit([&]() { |
| 855 | return OptimizationRemarkMissed(LDIST_NAME, "NotDistributed", |
| 856 | L->getStartLoc(), L->getHeader()) |
| 857 | << "loop not distributed: use -Rpass-analysis=loop-distribute for " |
| 858 | "more " |
| 859 | "info"; |
| 860 | }); |
Adam Nemet | 0ba164b | 2016-04-28 23:08:32 +0000 | [diff] [blame] | 861 | |
| 862 | // With Rpass-analysis report why. This is on by default if distribution |
| 863 | // was requested explicitly. |
Adam Nemet | f744ad7 | 2016-09-30 04:56:25 +0000 | [diff] [blame] | 864 | ORE->emit(OptimizationRemarkAnalysis( |
| 865 | Forced ? OptimizationRemarkAnalysis::AlwaysPrint : LDIST_NAME, |
| 866 | RemarkName, L->getStartLoc(), L->getHeader()) |
| 867 | << "loop not distributed: " << Message); |
Adam Nemet | 0ba164b | 2016-04-28 23:08:32 +0000 | [diff] [blame] | 868 | |
| 869 | // Also issue a warning if distribution was requested explicitly but it |
| 870 | // failed. |
| 871 | if (Forced) |
| 872 | Ctx.diagnose(DiagnosticInfoOptimizationFailure( |
Adam Nemet | 74730d9 | 2016-07-14 22:33:46 +0000 | [diff] [blame] | 873 | *F, L->getStartLoc(), "loop not distributed: failed " |
Adam Nemet | 0ba164b | 2016-04-28 23:08:32 +0000 | [diff] [blame] | 874 | "explicitly specified loop distribution")); |
| 875 | |
Adam Nemet | 7f38e11 | 2016-04-28 23:08:27 +0000 | [diff] [blame] | 876 | return false; |
| 877 | } |
| 878 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 879 | /// Return if distribution forced to be enabled/disabled for the loop. |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 880 | /// |
| 881 | /// If the optional has a value, it indicates whether distribution was forced |
| 882 | /// to be enabled (true) or disabled (false). If the optional has no value |
| 883 | /// distribution was not forced either way. |
| 884 | const Optional<bool> &isForced() const { return IsForced; } |
| 885 | |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 886 | private: |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 887 | /// Filter out checks between pointers from the same partition. |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 888 | /// |
| 889 | /// \p PtrToPartition contains the partition number for pointers. Partition |
| 890 | /// number -1 means that the pointer is used in multiple partitions. In this |
| 891 | /// case we can't safely omit the check. |
| 892 | SmallVector<RuntimePointerChecking::PointerCheck, 4> |
| 893 | includeOnlyCrossPartitionChecks( |
| 894 | const SmallVectorImpl<RuntimePointerChecking::PointerCheck> &AllChecks, |
| 895 | const SmallVectorImpl<int> &PtrToPartition, |
| 896 | const RuntimePointerChecking *RtPtrChecking) { |
| 897 | SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks; |
| 898 | |
Sanjoy Das | 9020872 | 2017-02-21 00:38:44 +0000 | [diff] [blame] | 899 | copy_if(AllChecks, std::back_inserter(Checks), |
| 900 | [&](const RuntimePointerChecking::PointerCheck &Check) { |
| 901 | for (unsigned PtrIdx1 : Check.first->Members) |
| 902 | for (unsigned PtrIdx2 : Check.second->Members) |
| 903 | // Only include this check if there is a pair of pointers |
| 904 | // that require checking and the pointers fall into |
| 905 | // separate partitions. |
| 906 | // |
| 907 | // (Note that we already know at this point that the two |
| 908 | // pointer groups need checking but it doesn't follow |
| 909 | // that each pair of pointers within the two groups need |
| 910 | // checking as well. |
| 911 | // |
| 912 | // In other words we don't want to include a check just |
| 913 | // because there is a pair of pointers between the two |
| 914 | // pointer groups that require checks and a different |
| 915 | // pair whose pointers fall into different partitions.) |
| 916 | if (RtPtrChecking->needsChecking(PtrIdx1, PtrIdx2) && |
| 917 | !RuntimePointerChecking::arePointersInSamePartition( |
| 918 | PtrToPartition, PtrIdx1, PtrIdx2)) |
| 919 | return true; |
| 920 | return false; |
| 921 | }); |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 922 | |
| 923 | return Checks; |
| 924 | } |
| 925 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 926 | /// Check whether the loop metadata is forcing distribution to be |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 927 | /// enabled/disabled. |
| 928 | void setForced() { |
| 929 | Optional<const MDOperand *> Value = |
| 930 | findStringMetadataForLoop(L, "llvm.loop.distribute.enable"); |
| 931 | if (!Value) |
| 932 | return; |
| 933 | |
| 934 | const MDOperand *Op = *Value; |
| 935 | assert(Op && mdconst::hasa<ConstantInt>(*Op) && "invalid metadata"); |
| 936 | IsForced = mdconst::extract<ConstantInt>(*Op)->getZExtValue(); |
| 937 | } |
| 938 | |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 939 | Loop *L; |
Adam Nemet | 4338d67 | 2016-04-29 07:10:39 +0000 | [diff] [blame] | 940 | Function *F; |
| 941 | |
| 942 | // Analyses used. |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 943 | LoopInfo *LI; |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 944 | const LoopAccessInfo *LAI = nullptr; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 945 | DominatorTree *DT; |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 946 | ScalarEvolution *SE; |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 947 | OptimizationRemarkEmitter *ORE; |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 948 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 949 | /// Indicates whether distribution is forced to be enabled/disabled for |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 950 | /// the loop. |
| 951 | /// |
| 952 | /// If the optional has a value, it indicates whether distribution was forced |
| 953 | /// to be enabled (true) or disabled (false). If the optional has no value |
| 954 | /// distribution was not forced either way. |
| 955 | Optional<bool> IsForced; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 956 | }; |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 957 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 958 | } // end anonymous namespace |
| 959 | |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 960 | /// Shared implementation between new and old PMs. |
| 961 | static bool runImpl(Function &F, LoopInfo *LI, DominatorTree *DT, |
| 962 | ScalarEvolution *SE, OptimizationRemarkEmitter *ORE, |
Adam Nemet | 32e6a34 | 2016-12-21 04:07:40 +0000 | [diff] [blame] | 963 | std::function<const LoopAccessInfo &(Loop &)> &GetLAA) { |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 964 | // Build up a worklist of inner-loops to vectorize. This is necessary as the |
| 965 | // act of distributing a loop creates new loops and can invalidate iterators |
| 966 | // across the loops. |
| 967 | SmallVector<Loop *, 8> Worklist; |
| 968 | |
| 969 | for (Loop *TopLevelLoop : *LI) |
| 970 | for (Loop *L : depth_first(TopLevelLoop)) |
| 971 | // We only handle inner-most loops. |
| 972 | if (L->empty()) |
| 973 | Worklist.push_back(L); |
| 974 | |
| 975 | // Now walk the identified inner loops. |
| 976 | bool Changed = false; |
| 977 | for (Loop *L : Worklist) { |
| 978 | LoopDistributeForLoop LDL(L, &F, LI, DT, SE, ORE); |
| 979 | |
| 980 | // If distribution was forced for the specific loop to be |
| 981 | // enabled/disabled, follow that. Otherwise use the global flag. |
Adam Nemet | 32e6a34 | 2016-12-21 04:07:40 +0000 | [diff] [blame] | 982 | if (LDL.isForced().getValueOr(EnableLoopDistribute)) |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 983 | Changed |= LDL.processLoop(GetLAA); |
| 984 | } |
| 985 | |
| 986 | // Process each loop nest in the function. |
| 987 | return Changed; |
| 988 | } |
| 989 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 990 | namespace { |
| 991 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 992 | /// The pass class. |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 993 | class LoopDistributeLegacy : public FunctionPass { |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 994 | public: |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 995 | static char ID; |
| 996 | |
Adam Nemet | 32e6a34 | 2016-12-21 04:07:40 +0000 | [diff] [blame] | 997 | LoopDistributeLegacy() : FunctionPass(ID) { |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 998 | // The default is set by the caller. |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 999 | initializeLoopDistributeLegacyPass(*PassRegistry::getPassRegistry()); |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 1003 | if (skipFunction(F)) |
| 1004 | return false; |
| 1005 | |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 1006 | auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 1007 | auto *LAA = &getAnalysis<LoopAccessLegacyAnalysis>(); |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 1008 | auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 1009 | auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 1010 | auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 1011 | std::function<const LoopAccessInfo &(Loop &)> GetLAA = |
| 1012 | [&](Loop &L) -> const LoopAccessInfo & { return LAA->getInfo(&L); }; |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 1013 | |
Adam Nemet | 32e6a34 | 2016-12-21 04:07:40 +0000 | [diff] [blame] | 1014 | return runImpl(F, LI, DT, SE, ORE, GetLAA); |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 1018 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 1019 | AU.addRequired<LoopInfoWrapperPass>(); |
| 1020 | AU.addPreserved<LoopInfoWrapperPass>(); |
Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 1021 | AU.addRequired<LoopAccessLegacyAnalysis>(); |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 1022 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 1023 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 1024 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Eli Friedman | 66fdba8 | 2016-09-16 18:01:48 +0000 | [diff] [blame] | 1025 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 1026 | } |
Adam Nemet | 61399ac | 2016-04-27 00:31:03 +0000 | [diff] [blame] | 1027 | }; |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 1028 | |
| 1029 | } // end anonymous namespace |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 1030 | |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 1031 | PreservedAnalyses LoopDistributePass::run(Function &F, |
| 1032 | FunctionAnalysisManager &AM) { |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 1033 | auto &LI = AM.getResult<LoopAnalysis>(F); |
| 1034 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 1035 | auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F); |
| 1036 | auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 1037 | |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 1038 | // We don't directly need these analyses but they're required for loop |
| 1039 | // analyses so provide them below. |
| 1040 | auto &AA = AM.getResult<AAManager>(F); |
| 1041 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
| 1042 | auto &TTI = AM.getResult<TargetIRAnalysis>(F); |
| 1043 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
| 1044 | |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 1045 | auto &LAM = AM.getResult<LoopAnalysisManagerFunctionProxy>(F).getManager(); |
| 1046 | std::function<const LoopAccessInfo &(Loop &)> GetLAA = |
| 1047 | [&](Loop &L) -> const LoopAccessInfo & { |
Alina Sbirlea | ff8b8ae | 2017-11-21 15:45:46 +0000 | [diff] [blame] | 1048 | LoopStandardAnalysisResults AR = {AA, AC, DT, LI, SE, TLI, TTI, nullptr}; |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 1049 | return LAM.getResult<LoopAccessAnalysis>(L, AR); |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 1050 | }; |
| 1051 | |
Adam Nemet | 32e6a34 | 2016-12-21 04:07:40 +0000 | [diff] [blame] | 1052 | bool Changed = runImpl(F, &LI, &DT, &SE, &ORE, GetLAA); |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 1053 | if (!Changed) |
| 1054 | return PreservedAnalyses::all(); |
| 1055 | PreservedAnalyses PA; |
| 1056 | PA.preserve<LoopAnalysis>(); |
| 1057 | PA.preserve<DominatorTreeAnalysis>(); |
Davide Italiano | 11a871b | 2016-11-08 19:52:32 +0000 | [diff] [blame] | 1058 | PA.preserve<GlobalsAA>(); |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 1059 | return PA; |
| 1060 | } |
| 1061 | |
| 1062 | char LoopDistributeLegacy::ID; |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 1063 | |
Michael Zolotukhin | 5cda89a | 2016-10-05 00:44:52 +0000 | [diff] [blame] | 1064 | static const char ldist_name[] = "Loop Distribution"; |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 1065 | |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 1066 | INITIALIZE_PASS_BEGIN(LoopDistributeLegacy, LDIST_NAME, ldist_name, false, |
| 1067 | false) |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 1068 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 1069 | INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis) |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 1070 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 1071 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 1072 | INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 1073 | INITIALIZE_PASS_END(LoopDistributeLegacy, LDIST_NAME, ldist_name, false, false) |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 1074 | |
Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 1075 | FunctionPass *llvm::createLoopDistributePass() { return new LoopDistributeLegacy(); } |