Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 1 | //===- HexagonVectorLoopCarriedReuse.cpp ----------------------------------===// |
| 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 |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 8 | // |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 9 | // This pass removes the computation of provably redundant expressions that have |
| 10 | // been computed earlier in a previous iteration. It relies on the use of PHIs |
| 11 | // to identify loop carried dependences. This is scalar replacement for vector |
| 12 | // types. |
| 13 | // |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 16 | #include "HexagonVectorLoopCarriedReuse.h" |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
| 20 | #include "llvm/Analysis/LoopInfo.h" |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopPass.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 22 | #include "llvm/IR/BasicBlock.h" |
| 23 | #include "llvm/IR/DerivedTypes.h" |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instruction.h" |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Instructions.h" |
| 27 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Intrinsics.h" |
Reid Kleckner | 5d98695 | 2019-12-11 07:55:26 -0800 | [diff] [blame] | 29 | #include "llvm/IR/IntrinsicsHexagon.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Use.h" |
| 31 | #include "llvm/IR/User.h" |
| 32 | #include "llvm/IR/Value.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 33 | #include "llvm/InitializePasses.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 34 | #include "llvm/Pass.h" |
| 35 | #include "llvm/Support/Casting.h" |
| 36 | #include "llvm/Support/CommandLine.h" |
| 37 | #include "llvm/Support/Compiler.h" |
| 38 | #include "llvm/Support/Debug.h" |
| 39 | #include "llvm/Support/raw_ostream.h" |
| 40 | #include "llvm/Transforms/Scalar.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Utils.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 42 | #include <algorithm> |
| 43 | #include <cassert> |
| 44 | #include <cstddef> |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 45 | #include <map> |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 46 | #include <memory> |
| 47 | #include <set> |
| 48 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 49 | using namespace llvm; |
| 50 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 51 | #define DEBUG_TYPE "hexagon-vlcr" |
| 52 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 53 | STATISTIC(HexagonNumVectorLoopCarriedReuse, |
| 54 | "Number of values that were reused from a previous iteration."); |
| 55 | |
| 56 | static cl::opt<int> HexagonVLCRIterationLim("hexagon-vlcr-iteration-lim", |
| 57 | cl::Hidden, |
| 58 | cl::desc("Maximum distance of loop carried dependences that are handled"), |
| 59 | cl::init(2), cl::ZeroOrMore); |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 60 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 61 | namespace llvm { |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 62 | |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 63 | void initializeHexagonVectorLoopCarriedReuseLegacyPassPass(PassRegistry &); |
| 64 | Pass *createHexagonVectorLoopCarriedReuseLegacyPass(); |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 65 | |
| 66 | } // end namespace llvm |
| 67 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 68 | namespace { |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 69 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 70 | // See info about DepChain in the comments at the top of this file. |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 71 | using ChainOfDependences = SmallVector<Instruction *, 4>; |
| 72 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 73 | class DepChain { |
| 74 | ChainOfDependences Chain; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 75 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 76 | public: |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 77 | bool isIdentical(DepChain &Other) const { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 78 | if (Other.size() != size()) |
| 79 | return false; |
| 80 | ChainOfDependences &OtherChain = Other.getChain(); |
| 81 | for (int i = 0; i < size(); ++i) { |
| 82 | if (Chain[i] != OtherChain[i]) |
| 83 | return false; |
| 84 | } |
| 85 | return true; |
| 86 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 87 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 88 | ChainOfDependences &getChain() { |
| 89 | return Chain; |
| 90 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 91 | |
| 92 | int size() const { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 93 | return Chain.size(); |
| 94 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 95 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 96 | void clear() { |
| 97 | Chain.clear(); |
| 98 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 99 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 100 | void push_back(Instruction *I) { |
| 101 | Chain.push_back(I); |
| 102 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 103 | |
| 104 | int iterations() const { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 105 | return size() - 1; |
| 106 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 107 | |
| 108 | Instruction *front() const { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 109 | return Chain.front(); |
| 110 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 111 | |
| 112 | Instruction *back() const { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 113 | return Chain.back(); |
| 114 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 115 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 116 | Instruction *&operator[](const int index) { |
| 117 | return Chain[index]; |
| 118 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 119 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 120 | friend raw_ostream &operator<< (raw_ostream &OS, const DepChain &D); |
| 121 | }; |
| 122 | |
NAKAMURA Takumi | fec5e10 | 2017-09-22 01:01:33 +0000 | [diff] [blame] | 123 | LLVM_ATTRIBUTE_UNUSED |
NAKAMURA Takumi | 05f6015 | 2017-09-22 01:01:31 +0000 | [diff] [blame] | 124 | raw_ostream &operator<<(raw_ostream &OS, const DepChain &D) { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 125 | const ChainOfDependences &CD = D.Chain; |
| 126 | int ChainSize = CD.size(); |
| 127 | OS << "**DepChain Start::**\n"; |
| 128 | for (int i = 0; i < ChainSize -1; ++i) { |
| 129 | OS << *(CD[i]) << " -->\n"; |
| 130 | } |
| 131 | OS << *CD[ChainSize-1] << "\n"; |
| 132 | return OS; |
| 133 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 134 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 135 | struct ReuseValue { |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 136 | Instruction *Inst2Replace = nullptr; |
| 137 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 138 | // In the new PHI node that we'll construct this is the value that'll be |
Shoaib Meenai | e3d26b4 | 2019-10-23 18:06:28 -0700 | [diff] [blame] | 139 | // used over the backedge. This is the value that gets reused from a |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 140 | // previous iteration. |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 141 | Instruction *BackedgeInst = nullptr; |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 142 | std::map<Instruction *, DepChain *> DepChains; |
| 143 | int Iterations = -1; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 144 | |
| 145 | ReuseValue() = default; |
| 146 | |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 147 | void reset() { |
| 148 | Inst2Replace = nullptr; |
| 149 | BackedgeInst = nullptr; |
| 150 | DepChains.clear(); |
| 151 | Iterations = -1; |
| 152 | } |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 153 | bool isDefined() { return Inst2Replace != nullptr; } |
| 154 | }; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 155 | |
NAKAMURA Takumi | fec5e10 | 2017-09-22 01:01:33 +0000 | [diff] [blame] | 156 | LLVM_ATTRIBUTE_UNUSED |
NAKAMURA Takumi | 05f6015 | 2017-09-22 01:01:31 +0000 | [diff] [blame] | 157 | raw_ostream &operator<<(raw_ostream &OS, const ReuseValue &RU) { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 158 | OS << "** ReuseValue ***\n"; |
| 159 | OS << "Instruction to Replace: " << *(RU.Inst2Replace) << "\n"; |
| 160 | OS << "Backedge Instruction: " << *(RU.BackedgeInst) << "\n"; |
| 161 | return OS; |
| 162 | } |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 163 | |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 164 | class HexagonVectorLoopCarriedReuseLegacyPass : public LoopPass { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 165 | public: |
| 166 | static char ID; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 167 | |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 168 | explicit HexagonVectorLoopCarriedReuseLegacyPass() : LoopPass(ID) { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 169 | PassRegistry *PR = PassRegistry::getPassRegistry(); |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 170 | initializeHexagonVectorLoopCarriedReuseLegacyPassPass(*PR); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 171 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 172 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 173 | StringRef getPassName() const override { |
| 174 | return "Hexagon-specific loop carried reuse for HVX vectors"; |
| 175 | } |
| 176 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 177 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 178 | AU.addRequiredID(LoopSimplifyID); |
| 179 | AU.addRequiredID(LCSSAID); |
| 180 | AU.addPreservedID(LCSSAID); |
| 181 | AU.setPreservesCFG(); |
| 182 | } |
| 183 | |
| 184 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | class HexagonVectorLoopCarriedReuse { |
| 188 | public: |
| 189 | HexagonVectorLoopCarriedReuse(Loop *L) : CurLoop(L){}; |
| 190 | |
| 191 | bool run(); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 192 | |
| 193 | private: |
| 194 | SetVector<DepChain *> Dependences; |
| 195 | std::set<Instruction *> ReplacedInsts; |
| 196 | Loop *CurLoop; |
| 197 | ReuseValue ReuseCandidate; |
| 198 | |
| 199 | bool doVLCR(); |
| 200 | void findLoopCarriedDeps(); |
| 201 | void findValueToReuse(); |
| 202 | void findDepChainFromPHI(Instruction *I, DepChain &D); |
| 203 | void reuseValue(); |
| 204 | Value *findValueInBlock(Value *Op, BasicBlock *BB); |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 205 | DepChain *getDepChainBtwn(Instruction *I1, Instruction *I2, int Iters); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 206 | bool isEquivalentOperation(Instruction *I1, Instruction *I2); |
| 207 | bool canReplace(Instruction *I); |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 208 | bool isCallInstCommutative(CallInst *C); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 209 | }; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 210 | |
| 211 | } // end anonymous namespace |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 212 | |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 213 | char HexagonVectorLoopCarriedReuseLegacyPass::ID = 0; |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 214 | |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 215 | INITIALIZE_PASS_BEGIN(HexagonVectorLoopCarriedReuseLegacyPass, "hexagon-vlcr", |
| 216 | "Hexagon-specific predictive commoning for HVX vectors", |
| 217 | false, false) |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 218 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 219 | INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass) |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 220 | INITIALIZE_PASS_END(HexagonVectorLoopCarriedReuseLegacyPass, "hexagon-vlcr", |
| 221 | "Hexagon-specific predictive commoning for HVX vectors", |
| 222 | false, false) |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 223 | |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 224 | PreservedAnalyses |
| 225 | HexagonVectorLoopCarriedReusePass::run(Loop &L, LoopAnalysisManager &LAM, |
| 226 | LoopStandardAnalysisResults &AR, |
| 227 | LPMUpdater &U) { |
| 228 | HexagonVectorLoopCarriedReuse Vlcr(&L); |
| 229 | if (!Vlcr.run()) |
| 230 | return PreservedAnalyses::all(); |
| 231 | PreservedAnalyses PA; |
| 232 | PA.preserveSet<CFGAnalyses>(); |
| 233 | return PA; |
| 234 | } |
| 235 | |
| 236 | bool HexagonVectorLoopCarriedReuseLegacyPass::runOnLoop(Loop *L, |
| 237 | LPPassManager &LPM) { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 238 | if (skipLoop(L)) |
| 239 | return false; |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 240 | HexagonVectorLoopCarriedReuse Vlcr(L); |
| 241 | return Vlcr.run(); |
| 242 | } |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 243 | |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 244 | bool HexagonVectorLoopCarriedReuse::run() { |
| 245 | if (!CurLoop->getLoopPreheader()) |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 246 | return false; |
| 247 | |
| 248 | // Work only on innermost loops. |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 249 | if (!CurLoop->getSubLoops().empty()) |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 250 | return false; |
| 251 | |
| 252 | // Work only on single basic blocks loops. |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 253 | if (CurLoop->getNumBlocks() != 1) |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 254 | return false; |
| 255 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 256 | return doVLCR(); |
| 257 | } |
| 258 | |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 259 | bool HexagonVectorLoopCarriedReuse::isCallInstCommutative(CallInst *C) { |
| 260 | switch (C->getCalledFunction()->getIntrinsicID()) { |
| 261 | case Intrinsic::hexagon_V6_vaddb: |
| 262 | case Intrinsic::hexagon_V6_vaddb_128B: |
| 263 | case Intrinsic::hexagon_V6_vaddh: |
| 264 | case Intrinsic::hexagon_V6_vaddh_128B: |
| 265 | case Intrinsic::hexagon_V6_vaddw: |
| 266 | case Intrinsic::hexagon_V6_vaddw_128B: |
| 267 | case Intrinsic::hexagon_V6_vaddubh: |
| 268 | case Intrinsic::hexagon_V6_vaddubh_128B: |
| 269 | case Intrinsic::hexagon_V6_vadduhw: |
| 270 | case Intrinsic::hexagon_V6_vadduhw_128B: |
| 271 | case Intrinsic::hexagon_V6_vaddhw: |
| 272 | case Intrinsic::hexagon_V6_vaddhw_128B: |
| 273 | case Intrinsic::hexagon_V6_vmaxb: |
| 274 | case Intrinsic::hexagon_V6_vmaxb_128B: |
| 275 | case Intrinsic::hexagon_V6_vmaxh: |
| 276 | case Intrinsic::hexagon_V6_vmaxh_128B: |
| 277 | case Intrinsic::hexagon_V6_vmaxw: |
| 278 | case Intrinsic::hexagon_V6_vmaxw_128B: |
| 279 | case Intrinsic::hexagon_V6_vmaxub: |
| 280 | case Intrinsic::hexagon_V6_vmaxub_128B: |
| 281 | case Intrinsic::hexagon_V6_vmaxuh: |
| 282 | case Intrinsic::hexagon_V6_vmaxuh_128B: |
| 283 | case Intrinsic::hexagon_V6_vminub: |
| 284 | case Intrinsic::hexagon_V6_vminub_128B: |
| 285 | case Intrinsic::hexagon_V6_vminuh: |
| 286 | case Intrinsic::hexagon_V6_vminuh_128B: |
| 287 | case Intrinsic::hexagon_V6_vminb: |
| 288 | case Intrinsic::hexagon_V6_vminb_128B: |
| 289 | case Intrinsic::hexagon_V6_vminh: |
| 290 | case Intrinsic::hexagon_V6_vminh_128B: |
| 291 | case Intrinsic::hexagon_V6_vminw: |
| 292 | case Intrinsic::hexagon_V6_vminw_128B: |
| 293 | case Intrinsic::hexagon_V6_vmpyub: |
| 294 | case Intrinsic::hexagon_V6_vmpyub_128B: |
| 295 | case Intrinsic::hexagon_V6_vmpyuh: |
| 296 | case Intrinsic::hexagon_V6_vmpyuh_128B: |
| 297 | case Intrinsic::hexagon_V6_vavgub: |
| 298 | case Intrinsic::hexagon_V6_vavgub_128B: |
| 299 | case Intrinsic::hexagon_V6_vavgh: |
| 300 | case Intrinsic::hexagon_V6_vavgh_128B: |
| 301 | case Intrinsic::hexagon_V6_vavguh: |
| 302 | case Intrinsic::hexagon_V6_vavguh_128B: |
| 303 | case Intrinsic::hexagon_V6_vavgw: |
| 304 | case Intrinsic::hexagon_V6_vavgw_128B: |
| 305 | case Intrinsic::hexagon_V6_vavgb: |
| 306 | case Intrinsic::hexagon_V6_vavgb_128B: |
| 307 | case Intrinsic::hexagon_V6_vavguw: |
| 308 | case Intrinsic::hexagon_V6_vavguw_128B: |
| 309 | case Intrinsic::hexagon_V6_vabsdiffh: |
| 310 | case Intrinsic::hexagon_V6_vabsdiffh_128B: |
| 311 | case Intrinsic::hexagon_V6_vabsdiffub: |
| 312 | case Intrinsic::hexagon_V6_vabsdiffub_128B: |
| 313 | case Intrinsic::hexagon_V6_vabsdiffuh: |
| 314 | case Intrinsic::hexagon_V6_vabsdiffuh_128B: |
| 315 | case Intrinsic::hexagon_V6_vabsdiffw: |
| 316 | case Intrinsic::hexagon_V6_vabsdiffw_128B: |
| 317 | return true; |
| 318 | default: |
| 319 | return false; |
| 320 | } |
| 321 | } |
| 322 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 323 | bool HexagonVectorLoopCarriedReuse::isEquivalentOperation(Instruction *I1, |
| 324 | Instruction *I2) { |
| 325 | if (!I1->isSameOperationAs(I2)) |
| 326 | return false; |
| 327 | // This check is in place specifically for intrinsics. isSameOperationAs will |
| 328 | // return two for any two hexagon intrinsics because they are essentially the |
| 329 | // same instruciton (CallInst). We need to scratch the surface to see if they |
| 330 | // are calls to the same function. |
| 331 | if (CallInst *C1 = dyn_cast<CallInst>(I1)) { |
| 332 | if (CallInst *C2 = dyn_cast<CallInst>(I2)) { |
| 333 | if (C1->getCalledFunction() != C2->getCalledFunction()) |
| 334 | return false; |
| 335 | } |
| 336 | } |
Ron Lieberman | 9bcdd80 | 2017-10-02 00:34:07 +0000 | [diff] [blame] | 337 | |
| 338 | // If both the Instructions are of Vector Type and any of the element |
| 339 | // is integer constant, check their values too for equivalence. |
| 340 | if (I1->getType()->isVectorTy() && I2->getType()->isVectorTy()) { |
| 341 | unsigned NumOperands = I1->getNumOperands(); |
| 342 | for (unsigned i = 0; i < NumOperands; ++i) { |
| 343 | ConstantInt *C1 = dyn_cast<ConstantInt>(I1->getOperand(i)); |
| 344 | ConstantInt *C2 = dyn_cast<ConstantInt>(I2->getOperand(i)); |
| 345 | if(!C1) continue; |
| 346 | assert(C2); |
| 347 | if (C1->getSExtValue() != C2->getSExtValue()) |
| 348 | return false; |
| 349 | } |
| 350 | } |
| 351 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | |
| 355 | bool HexagonVectorLoopCarriedReuse::canReplace(Instruction *I) { |
| 356 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 357 | if (!II) |
| 358 | return true; |
| 359 | |
| 360 | switch (II->getIntrinsicID()) { |
| 361 | case Intrinsic::hexagon_V6_hi: |
| 362 | case Intrinsic::hexagon_V6_lo: |
| 363 | case Intrinsic::hexagon_V6_hi_128B: |
| 364 | case Intrinsic::hexagon_V6_lo_128B: |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 365 | LLVM_DEBUG(dbgs() << "Not considering for reuse: " << *II << "\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 366 | return false; |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 367 | default: |
| 368 | return true; |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 369 | } |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 370 | } |
| 371 | void HexagonVectorLoopCarriedReuse::findValueToReuse() { |
| 372 | for (auto *D : Dependences) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 373 | LLVM_DEBUG(dbgs() << "Processing dependence " << *(D->front()) << "\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 374 | if (D->iterations() > HexagonVLCRIterationLim) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 375 | LLVM_DEBUG( |
| 376 | dbgs() |
| 377 | << ".. Skipping because number of iterations > than the limit\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 378 | continue; |
| 379 | } |
| 380 | |
| 381 | PHINode *PN = cast<PHINode>(D->front()); |
| 382 | Instruction *BEInst = D->back(); |
| 383 | int Iters = D->iterations(); |
| 384 | BasicBlock *BB = PN->getParent(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 385 | LLVM_DEBUG(dbgs() << "Checking if any uses of " << *PN |
| 386 | << " can be reused\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 387 | |
| 388 | SmallVector<Instruction *, 4> PNUsers; |
Kazu Hirata | 7ca14f6 | 2021-11-18 09:09:52 -0800 | [diff] [blame] | 389 | for (Use &U : PN->uses()) { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 390 | Instruction *User = cast<Instruction>(U.getUser()); |
| 391 | |
| 392 | if (User->getParent() != BB) |
| 393 | continue; |
| 394 | if (ReplacedInsts.count(User)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 395 | LLVM_DEBUG(dbgs() << *User |
| 396 | << " has already been replaced. Skipping...\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 397 | continue; |
| 398 | } |
| 399 | if (isa<PHINode>(User)) |
| 400 | continue; |
| 401 | if (User->mayHaveSideEffects()) |
| 402 | continue; |
| 403 | if (!canReplace(User)) |
| 404 | continue; |
| 405 | |
| 406 | PNUsers.push_back(User); |
| 407 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 408 | LLVM_DEBUG(dbgs() << PNUsers.size() << " use(s) of the PHI in the block\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 409 | |
| 410 | // For each interesting use I of PN, find an Instruction BEUser that |
| 411 | // performs the same operation as I on BEInst and whose other operands, |
| 412 | // if any, can also be rematerialized in OtherBB. We stop when we find the |
| 413 | // first such Instruction BEUser. This is because once BEUser is |
| 414 | // rematerialized in OtherBB, we may find more such "fixup" opportunities |
| 415 | // in this block. So, we'll start over again. |
| 416 | for (Instruction *I : PNUsers) { |
Kazu Hirata | 7ca14f6 | 2021-11-18 09:09:52 -0800 | [diff] [blame] | 417 | for (Use &U : BEInst->uses()) { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 418 | Instruction *BEUser = cast<Instruction>(U.getUser()); |
| 419 | |
| 420 | if (BEUser->getParent() != BB) |
| 421 | continue; |
| 422 | if (!isEquivalentOperation(I, BEUser)) |
| 423 | continue; |
| 424 | |
| 425 | int NumOperands = I->getNumOperands(); |
| 426 | |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 427 | // Take operands of each PNUser one by one and try to find DepChain |
| 428 | // with every operand of the BEUser. If any of the operands of BEUser |
| 429 | // has DepChain with current operand of the PNUser, break the matcher |
| 430 | // loop. Keep doing this for Every PNUser operand. If PNUser operand |
| 431 | // does not have DepChain with any of the BEUser operand, break the |
| 432 | // outer matcher loop, mark the BEUser as null and reset the ReuseCandidate. |
| 433 | // This ensures that DepChain exist for all the PNUser operand with |
| 434 | // BEUser operand. This also ensures that DepChains are independent of |
| 435 | // the positions in PNUser and BEUser. |
| 436 | std::map<Instruction *, DepChain *> DepChains; |
| 437 | CallInst *C1 = dyn_cast<CallInst>(I); |
| 438 | if ((I && I->isCommutative()) || (C1 && isCallInstCommutative(C1))) { |
| 439 | bool Found = false; |
| 440 | for (int OpNo = 0; OpNo < NumOperands; ++OpNo) { |
| 441 | Value *Op = I->getOperand(OpNo); |
| 442 | Instruction *OpInst = dyn_cast<Instruction>(Op); |
| 443 | Found = false; |
| 444 | for (int T = 0; T < NumOperands; ++T) { |
| 445 | Value *BEOp = BEUser->getOperand(T); |
| 446 | Instruction *BEOpInst = dyn_cast<Instruction>(BEOp); |
| 447 | if (!OpInst && !BEOpInst) { |
| 448 | if (Op == BEOp) { |
| 449 | Found = true; |
| 450 | break; |
| 451 | } |
| 452 | } |
Brendon Cahoon | 4df216c | 2019-04-12 16:37:12 +0000 | [diff] [blame] | 453 | |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 454 | if ((OpInst && !BEOpInst) || (!OpInst && BEOpInst)) |
| 455 | continue; |
| 456 | |
| 457 | DepChain *D = getDepChainBtwn(OpInst, BEOpInst, Iters); |
| 458 | |
| 459 | if (D) { |
| 460 | Found = true; |
| 461 | DepChains[OpInst] = D; |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | if (!Found) { |
| 466 | BEUser = nullptr; |
| 467 | break; |
| 468 | } |
Brendon Cahoon | 4df216c | 2019-04-12 16:37:12 +0000 | [diff] [blame] | 469 | } |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 470 | } else { |
Brendon Cahoon | 4df216c | 2019-04-12 16:37:12 +0000 | [diff] [blame] | 471 | |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 472 | for (int OpNo = 0; OpNo < NumOperands; ++OpNo) { |
| 473 | Value *Op = I->getOperand(OpNo); |
| 474 | Value *BEOp = BEUser->getOperand(OpNo); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 475 | |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 476 | Instruction *OpInst = dyn_cast<Instruction>(Op); |
| 477 | if (!OpInst) { |
| 478 | if (Op == BEOp) |
| 479 | continue; |
| 480 | // Do not allow reuse to occur when the operands may be different |
| 481 | // values. |
| 482 | BEUser = nullptr; |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | Instruction *BEOpInst = dyn_cast<Instruction>(BEOp); |
| 487 | DepChain *D = getDepChainBtwn(OpInst, BEOpInst, Iters); |
| 488 | |
| 489 | if (D) { |
| 490 | DepChains[OpInst] = D; |
| 491 | } else { |
| 492 | BEUser = nullptr; |
| 493 | break; |
| 494 | } |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | if (BEUser) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 498 | LLVM_DEBUG(dbgs() << "Found Value for reuse.\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 499 | ReuseCandidate.Inst2Replace = I; |
| 500 | ReuseCandidate.BackedgeInst = BEUser; |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 501 | ReuseCandidate.DepChains = DepChains; |
| 502 | ReuseCandidate.Iterations = Iters; |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 503 | return; |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 504 | } |
| 505 | ReuseCandidate.reset(); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
| 509 | ReuseCandidate.reset(); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 510 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 511 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 512 | Value *HexagonVectorLoopCarriedReuse::findValueInBlock(Value *Op, |
| 513 | BasicBlock *BB) { |
| 514 | PHINode *PN = dyn_cast<PHINode>(Op); |
| 515 | assert(PN); |
| 516 | Value *ValueInBlock = PN->getIncomingValueForBlock(BB); |
| 517 | return ValueInBlock; |
| 518 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 519 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 520 | void HexagonVectorLoopCarriedReuse::reuseValue() { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 521 | LLVM_DEBUG(dbgs() << ReuseCandidate); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 522 | Instruction *Inst2Replace = ReuseCandidate.Inst2Replace; |
| 523 | Instruction *BEInst = ReuseCandidate.BackedgeInst; |
| 524 | int NumOperands = Inst2Replace->getNumOperands(); |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 525 | std::map<Instruction *, DepChain *> &DepChains = ReuseCandidate.DepChains; |
| 526 | int Iterations = ReuseCandidate.Iterations; |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 527 | BasicBlock *LoopPH = CurLoop->getLoopPreheader(); |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 528 | assert(!DepChains.empty() && "No DepChains"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 529 | LLVM_DEBUG(dbgs() << "reuseValue is making the following changes\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 530 | |
| 531 | SmallVector<Instruction *, 4> InstsInPreheader; |
| 532 | for (int i = 0; i < Iterations; ++i) { |
| 533 | Instruction *InstInPreheader = Inst2Replace->clone(); |
| 534 | SmallVector<Value *, 4> Ops; |
| 535 | for (int j = 0; j < NumOperands; ++j) { |
| 536 | Instruction *I = dyn_cast<Instruction>(Inst2Replace->getOperand(j)); |
| 537 | if (!I) |
| 538 | continue; |
| 539 | // Get the DepChain corresponding to this operand. |
| 540 | DepChain &D = *DepChains[I]; |
| 541 | // Get the PHI for the iteration number and find |
| 542 | // the incoming value from the Loop Preheader for |
| 543 | // that PHI. |
| 544 | Value *ValInPreheader = findValueInBlock(D[i], LoopPH); |
| 545 | InstInPreheader->setOperand(j, ValInPreheader); |
| 546 | } |
| 547 | InstsInPreheader.push_back(InstInPreheader); |
| 548 | InstInPreheader->setName(Inst2Replace->getName() + ".hexagon.vlcr"); |
| 549 | InstInPreheader->insertBefore(LoopPH->getTerminator()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 550 | LLVM_DEBUG(dbgs() << "Added " << *InstInPreheader << " to " |
| 551 | << LoopPH->getName() << "\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 552 | } |
| 553 | BasicBlock *BB = BEInst->getParent(); |
| 554 | IRBuilder<> IRB(BB); |
| 555 | IRB.SetInsertPoint(BB->getFirstNonPHI()); |
| 556 | Value *BEVal = BEInst; |
| 557 | PHINode *NewPhi; |
| 558 | for (int i = Iterations-1; i >=0 ; --i) { |
| 559 | Instruction *InstInPreheader = InstsInPreheader[i]; |
| 560 | NewPhi = IRB.CreatePHI(InstInPreheader->getType(), 2); |
| 561 | NewPhi->addIncoming(InstInPreheader, LoopPH); |
| 562 | NewPhi->addIncoming(BEVal, BB); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 563 | LLVM_DEBUG(dbgs() << "Adding " << *NewPhi << " to " << BB->getName() |
| 564 | << "\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 565 | BEVal = NewPhi; |
| 566 | } |
| 567 | // We are in LCSSA form. So, a value defined inside the Loop is used only |
| 568 | // inside the loop. So, the following is safe. |
| 569 | Inst2Replace->replaceAllUsesWith(NewPhi); |
| 570 | ReplacedInsts.insert(Inst2Replace); |
| 571 | ++HexagonNumVectorLoopCarriedReuse; |
| 572 | } |
| 573 | |
| 574 | bool HexagonVectorLoopCarriedReuse::doVLCR() { |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 575 | assert(CurLoop->getSubLoops().empty() && |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 576 | "Can do VLCR on the innermost loop only"); |
| 577 | assert((CurLoop->getNumBlocks() == 1) && |
| 578 | "Can do VLCR only on single block loops"); |
| 579 | |
Ron Lieberman | 9bcdd80 | 2017-10-02 00:34:07 +0000 | [diff] [blame] | 580 | bool Changed = false; |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 581 | bool Continue; |
| 582 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 583 | LLVM_DEBUG(dbgs() << "Working on Loop: " << *CurLoop->getHeader() << "\n"); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 584 | do { |
| 585 | // Reset datastructures. |
| 586 | Dependences.clear(); |
| 587 | Continue = false; |
| 588 | |
| 589 | findLoopCarriedDeps(); |
| 590 | findValueToReuse(); |
| 591 | if (ReuseCandidate.isDefined()) { |
| 592 | reuseValue(); |
Dimitry Andric | e44dea9 | 2017-12-18 18:56:00 +0000 | [diff] [blame] | 593 | Changed = true; |
| 594 | Continue = true; |
| 595 | } |
| 596 | llvm::for_each(Dependences, std::default_delete<DepChain>()); |
| 597 | } while (Continue); |
| 598 | return Changed; |
| 599 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 600 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 601 | void HexagonVectorLoopCarriedReuse::findDepChainFromPHI(Instruction *I, |
| 602 | DepChain &D) { |
| 603 | PHINode *PN = dyn_cast<PHINode>(I); |
| 604 | if (!PN) { |
| 605 | D.push_back(I); |
| 606 | return; |
| 607 | } else { |
| 608 | auto NumIncomingValues = PN->getNumIncomingValues(); |
| 609 | if (NumIncomingValues != 2) { |
| 610 | D.clear(); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | BasicBlock *BB = PN->getParent(); |
| 615 | if (BB != CurLoop->getHeader()) { |
| 616 | D.clear(); |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | Value *BEVal = PN->getIncomingValueForBlock(BB); |
| 621 | Instruction *BEInst = dyn_cast<Instruction>(BEVal); |
| 622 | // This is a single block loop with a preheader, so at least |
| 623 | // one value should come over the backedge. |
| 624 | assert(BEInst && "There should be a value over the backedge"); |
| 625 | |
| 626 | Value *PreHdrVal = |
| 627 | PN->getIncomingValueForBlock(CurLoop->getLoopPreheader()); |
| 628 | if(!PreHdrVal || !isa<Instruction>(PreHdrVal)) { |
| 629 | D.clear(); |
| 630 | return; |
| 631 | } |
| 632 | D.push_back(PN); |
| 633 | findDepChainFromPHI(BEInst, D); |
| 634 | } |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Krzysztof Parzyszek | 511ad50 | 2019-07-01 13:50:47 +0000 | [diff] [blame] | 637 | DepChain *HexagonVectorLoopCarriedReuse::getDepChainBtwn(Instruction *I1, |
| 638 | Instruction *I2, |
| 639 | int Iters) { |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 640 | for (auto *D : Dependences) { |
| 641 | if (D->front() == I1 && D->back() == I2 && D->iterations() == Iters) |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 642 | return D; |
| 643 | } |
| 644 | return nullptr; |
| 645 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 646 | |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 647 | void HexagonVectorLoopCarriedReuse::findLoopCarriedDeps() { |
| 648 | BasicBlock *BB = CurLoop->getHeader(); |
| 649 | for (auto I = BB->begin(), E = BB->end(); I != E && isa<PHINode>(I); ++I) { |
| 650 | auto *PN = cast<PHINode>(I); |
| 651 | if (!isa<VectorType>(PN->getType())) |
| 652 | continue; |
| 653 | |
| 654 | DepChain *D = new DepChain(); |
| 655 | findDepChainFromPHI(PN, *D); |
| 656 | if (D->size() != 0) |
| 657 | Dependences.insert(D); |
| 658 | else |
| 659 | delete D; |
| 660 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 661 | LLVM_DEBUG(dbgs() << "Found " << Dependences.size() << " dependences\n"); |
| 662 | LLVM_DEBUG(for (size_t i = 0; i < Dependences.size(); |
| 663 | ++i) { dbgs() << *Dependences[i] << "\n"; }); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 664 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 665 | |
Pengxuan Zheng | e5fea37 | 2020-09-21 13:43:42 -0700 | [diff] [blame] | 666 | Pass *llvm::createHexagonVectorLoopCarriedReuseLegacyPass() { |
| 667 | return new HexagonVectorLoopCarriedReuseLegacyPass(); |
Pranav Bhandarkar | 931d0b7 | 2017-09-21 21:48:23 +0000 | [diff] [blame] | 668 | } |