blob: aeff5f8d3e4e8e057ce0a328dc152377736444a5 [file] [log] [blame]
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +00001//===- HexagonVectorLoopCarriedReuse.cpp ----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eugene Zelenko3b873362017-09-28 22:27:31 +00009//
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +000010// This pass removes the computation of provably redundant expressions that have
11// been computed earlier in a previous iteration. It relies on the use of PHIs
12// to identify loop carried dependences. This is scalar replacement for vector
13// types.
14//
15//-----------------------------------------------------------------------------
16// Motivation: Consider the case where we have the following loop structure.
17//
18// Loop:
19// t0 = a[i];
20// t1 = f(t0);
21// t2 = g(t1);
22// ...
23// t3 = a[i+1];
24// t4 = f(t3);
25// t5 = g(t4);
26// t6 = op(t2, t5)
27// cond_branch <Loop>
28//
29// This can be converted to
30// t00 = a[0];
31// t10 = f(t00);
32// t20 = g(t10);
33// Loop:
34// t2 = t20;
35// t3 = a[i+1];
36// t4 = f(t3);
37// t5 = g(t4);
38// t6 = op(t2, t5)
39// t20 = t5
40// cond_branch <Loop>
41//
42// SROA does a good job of reusing a[i+1] as a[i] in the next iteration.
43// Such a loop comes to this pass in the following form.
44//
45// LoopPreheader:
46// X0 = a[0];
47// Loop:
48// X2 = PHI<(X0, LoopPreheader), (X1, Loop)>
49// t1 = f(X2) <-- I1
50// t2 = g(t1)
51// ...
52// X1 = a[i+1]
53// t4 = f(X1) <-- I2
54// t5 = g(t4)
55// t6 = op(t2, t5)
56// cond_branch <Loop>
57//
58// In this pass, we look for PHIs such as X2 whose incoming values come only
59// from the Loop Preheader and over the backedge and additionaly, both these
60// values are the results of the same operation in terms of opcode. We call such
61// a PHI node a dependence chain or DepChain. In this case, the dependence of X2
62// over X1 is carried over only one iteration and so the DepChain is only one
63// PHI node long.
64//
65// Then, we traverse the uses of the PHI (X2) and the uses of the value of the
66// PHI coming over the backedge (X1). We stop at the first pair of such users
67// I1 (of X2) and I2 (of X1) that meet the following conditions.
68// 1. I1 and I2 are the same operation, but with different operands.
69// 2. X2 and X1 are used at the same operand number in the two instructions.
70// 3. All other operands Op1 of I1 and Op2 of I2 are also such that there is a
71// a DepChain from Op1 to Op2 of the same length as that between X2 and X1.
72//
73// We then make the following transformation
74// LoopPreheader:
75// X0 = a[0];
76// Y0 = f(X0);
77// Loop:
78// X2 = PHI<(X0, LoopPreheader), (X1, Loop)>
79// Y2 = PHI<(Y0, LoopPreheader), (t4, Loop)>
80// t1 = f(X2) <-- Will be removed by DCE.
81// t2 = g(Y2)
82// ...
83// X1 = a[i+1]
84// t4 = f(X1)
85// t5 = g(t4)
86// t6 = op(t2, t5)
87// cond_branch <Loop>
88//
89// We proceed until we cannot find any more such instructions I1 and I2.
90//
91// --- DepChains & Loop carried dependences ---
92// Consider a single basic block loop such as
93//
94// LoopPreheader:
95// X0 = ...
96// Y0 = ...
97// Loop:
98// X2 = PHI<(X0, LoopPreheader), (X1, Loop)>
99// Y2 = PHI<(Y0, LoopPreheader), (X2, Loop)>
100// ...
101// X1 = ...
102// ...
103// cond_branch <Loop>
104//
105// Then there is a dependence between X2 and X1 that goes back one iteration,
106// i.e. X1 is used as X2 in the very next iteration. We represent this as a
107// DepChain from X2 to X1 (X2->X1).
108// Similarly, there is a dependence between Y2 and X1 that goes back two
109// iterations. X1 is used as Y2 two iterations after it is computed. This is
110// represented by a DepChain as (Y2->X2->X1).
111//
112// A DepChain has the following properties.
113// 1. Num of edges in DepChain = Number of Instructions in DepChain = Number of
114// iterations of carried dependence + 1.
115// 2. All instructions in the DepChain except the last are PHIs.
Eugene Zelenko3b873362017-09-28 22:27:31 +0000116//
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000117//===----------------------------------------------------------------------===//
118
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000119#include "llvm/ADT/SetVector.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +0000120#include "llvm/ADT/SmallVector.h"
121#include "llvm/ADT/Statistic.h"
122#include "llvm/Analysis/LoopInfo.h"
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000123#include "llvm/Analysis/LoopPass.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +0000124#include "llvm/IR/BasicBlock.h"
125#include "llvm/IR/DerivedTypes.h"
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000126#include "llvm/IR/IRBuilder.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +0000127#include "llvm/IR/Instruction.h"
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000128#include "llvm/IR/Instructions.h"
129#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +0000130#include "llvm/IR/Intrinsics.h"
131#include "llvm/IR/Use.h"
132#include "llvm/IR/User.h"
133#include "llvm/IR/Value.h"
134#include "llvm/Pass.h"
135#include "llvm/Support/Casting.h"
136#include "llvm/Support/CommandLine.h"
137#include "llvm/Support/Compiler.h"
138#include "llvm/Support/Debug.h"
139#include "llvm/Support/raw_ostream.h"
140#include "llvm/Transforms/Scalar.h"
141#include <algorithm>
142#include <cassert>
143#include <cstddef>
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000144#include <map>
Eugene Zelenko3b873362017-09-28 22:27:31 +0000145#include <memory>
146#include <set>
147
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000148using namespace llvm;
149
Eugene Zelenko3b873362017-09-28 22:27:31 +0000150#define DEBUG_TYPE "hexagon-vlcr"
151
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000152STATISTIC(HexagonNumVectorLoopCarriedReuse,
153 "Number of values that were reused from a previous iteration.");
154
155static cl::opt<int> HexagonVLCRIterationLim("hexagon-vlcr-iteration-lim",
156 cl::Hidden,
157 cl::desc("Maximum distance of loop carried dependences that are handled"),
158 cl::init(2), cl::ZeroOrMore);
Eugene Zelenko3b873362017-09-28 22:27:31 +0000159
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000160namespace llvm {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000161
162void initializeHexagonVectorLoopCarriedReusePass(PassRegistry&);
163Pass *createHexagonVectorLoopCarriedReusePass();
164
165} // end namespace llvm
166
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000167namespace {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000168
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000169 // See info about DepChain in the comments at the top of this file.
Eugene Zelenko3b873362017-09-28 22:27:31 +0000170 using ChainOfDependences = SmallVector<Instruction *, 4>;
171
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000172 class DepChain {
173 ChainOfDependences Chain;
Eugene Zelenko3b873362017-09-28 22:27:31 +0000174
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000175 public:
Eugene Zelenko3b873362017-09-28 22:27:31 +0000176 bool isIdentical(DepChain &Other) const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000177 if (Other.size() != size())
178 return false;
179 ChainOfDependences &OtherChain = Other.getChain();
180 for (int i = 0; i < size(); ++i) {
181 if (Chain[i] != OtherChain[i])
182 return false;
183 }
184 return true;
185 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000186
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000187 ChainOfDependences &getChain() {
188 return Chain;
189 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000190
191 int size() const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000192 return Chain.size();
193 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000194
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000195 void clear() {
196 Chain.clear();
197 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000198
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000199 void push_back(Instruction *I) {
200 Chain.push_back(I);
201 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000202
203 int iterations() const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000204 return size() - 1;
205 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000206
207 Instruction *front() const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000208 return Chain.front();
209 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000210
211 Instruction *back() const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000212 return Chain.back();
213 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000214
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000215 Instruction *&operator[](const int index) {
216 return Chain[index];
217 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000218
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000219 friend raw_ostream &operator<< (raw_ostream &OS, const DepChain &D);
220 };
221
NAKAMURA Takumifec5e102017-09-22 01:01:33 +0000222 LLVM_ATTRIBUTE_UNUSED
NAKAMURA Takumi05f60152017-09-22 01:01:31 +0000223 raw_ostream &operator<<(raw_ostream &OS, const DepChain &D) {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000224 const ChainOfDependences &CD = D.Chain;
225 int ChainSize = CD.size();
226 OS << "**DepChain Start::**\n";
227 for (int i = 0; i < ChainSize -1; ++i) {
228 OS << *(CD[i]) << " -->\n";
229 }
230 OS << *CD[ChainSize-1] << "\n";
231 return OS;
232 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000233
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000234 struct ReuseValue {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000235 Instruction *Inst2Replace = nullptr;
236
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000237 // In the new PHI node that we'll construct this is the value that'll be
238 // used over the backedge. This is teh value that gets reused from a
239 // previous iteration.
Eugene Zelenko3b873362017-09-28 22:27:31 +0000240 Instruction *BackedgeInst = nullptr;
241
242 ReuseValue() = default;
243
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000244 void reset() { Inst2Replace = nullptr; BackedgeInst = nullptr; }
245 bool isDefined() { return Inst2Replace != nullptr; }
246 };
Eugene Zelenko3b873362017-09-28 22:27:31 +0000247
NAKAMURA Takumifec5e102017-09-22 01:01:33 +0000248 LLVM_ATTRIBUTE_UNUSED
NAKAMURA Takumi05f60152017-09-22 01:01:31 +0000249 raw_ostream &operator<<(raw_ostream &OS, const ReuseValue &RU) {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000250 OS << "** ReuseValue ***\n";
251 OS << "Instruction to Replace: " << *(RU.Inst2Replace) << "\n";
252 OS << "Backedge Instruction: " << *(RU.BackedgeInst) << "\n";
253 return OS;
254 }
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000255
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000256 class HexagonVectorLoopCarriedReuse : public LoopPass {
257 public:
258 static char ID;
Eugene Zelenko3b873362017-09-28 22:27:31 +0000259
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000260 explicit HexagonVectorLoopCarriedReuse() : LoopPass(ID) {
261 PassRegistry *PR = PassRegistry::getPassRegistry();
262 initializeHexagonVectorLoopCarriedReusePass(*PR);
263 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000264
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000265 StringRef getPassName() const override {
266 return "Hexagon-specific loop carried reuse for HVX vectors";
267 }
268
Eugene Zelenko3b873362017-09-28 22:27:31 +0000269 void getAnalysisUsage(AnalysisUsage &AU) const override {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000270 AU.addRequired<LoopInfoWrapperPass>();
271 AU.addRequiredID(LoopSimplifyID);
272 AU.addRequiredID(LCSSAID);
273 AU.addPreservedID(LCSSAID);
274 AU.setPreservesCFG();
275 }
276
277 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
278
279 private:
280 SetVector<DepChain *> Dependences;
281 std::set<Instruction *> ReplacedInsts;
282 Loop *CurLoop;
283 ReuseValue ReuseCandidate;
284
285 bool doVLCR();
286 void findLoopCarriedDeps();
287 void findValueToReuse();
288 void findDepChainFromPHI(Instruction *I, DepChain &D);
289 void reuseValue();
290 Value *findValueInBlock(Value *Op, BasicBlock *BB);
291 bool isDepChainBtwn(Instruction *I1, Instruction *I2, int Iters);
292 DepChain *getDepChainBtwn(Instruction *I1, Instruction *I2);
293 bool isEquivalentOperation(Instruction *I1, Instruction *I2);
294 bool canReplace(Instruction *I);
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000295 };
Eugene Zelenko3b873362017-09-28 22:27:31 +0000296
297} // end anonymous namespace
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000298
299char HexagonVectorLoopCarriedReuse::ID = 0;
300
301INITIALIZE_PASS_BEGIN(HexagonVectorLoopCarriedReuse, "hexagon-vlcr",
302 "Hexagon-specific predictive commoning for HVX vectors", false, false)
303INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
304INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
305INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
306INITIALIZE_PASS_END(HexagonVectorLoopCarriedReuse, "hexagon-vlcr",
307 "Hexagon-specific predictive commoning for HVX vectors", false, false)
308
309bool HexagonVectorLoopCarriedReuse::runOnLoop(Loop *L, LPPassManager &LPM) {
310 if (skipLoop(L))
311 return false;
312
313 if (!L->getLoopPreheader())
314 return false;
315
316 // Work only on innermost loops.
Eugene Zelenko3b873362017-09-28 22:27:31 +0000317 if (!L->getSubLoops().empty())
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000318 return false;
319
320 // Work only on single basic blocks loops.
321 if (L->getNumBlocks() != 1)
322 return false;
323
324 CurLoop = L;
325
326 return doVLCR();
327}
328
329bool HexagonVectorLoopCarriedReuse::isEquivalentOperation(Instruction *I1,
330 Instruction *I2) {
331 if (!I1->isSameOperationAs(I2))
332 return false;
333 // This check is in place specifically for intrinsics. isSameOperationAs will
334 // return two for any two hexagon intrinsics because they are essentially the
335 // same instruciton (CallInst). We need to scratch the surface to see if they
336 // are calls to the same function.
337 if (CallInst *C1 = dyn_cast<CallInst>(I1)) {
338 if (CallInst *C2 = dyn_cast<CallInst>(I2)) {
339 if (C1->getCalledFunction() != C2->getCalledFunction())
340 return false;
341 }
342 }
343 return true;
344}
345
346bool HexagonVectorLoopCarriedReuse::canReplace(Instruction *I) {
347 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
348 if (II &&
349 (II->getIntrinsicID() == Intrinsic::hexagon_V6_hi ||
350 II->getIntrinsicID() == Intrinsic::hexagon_V6_lo)) {
351 DEBUG(dbgs() << "Not considering for reuse: " << *II << "\n");
352 return false;
353 }
354 return true;
355}
356void HexagonVectorLoopCarriedReuse::findValueToReuse() {
357 for (auto *D : Dependences) {
358 DEBUG(dbgs() << "Processing dependence " << *(D->front()) << "\n");
359 if (D->iterations() > HexagonVLCRIterationLim) {
360 DEBUG(dbgs() <<
361 ".. Skipping because number of iterations > than the limit\n");
362 continue;
363 }
364
365 PHINode *PN = cast<PHINode>(D->front());
366 Instruction *BEInst = D->back();
367 int Iters = D->iterations();
368 BasicBlock *BB = PN->getParent();
369 DEBUG(dbgs() << "Checking if any uses of " << *PN << " can be reused\n");
370
371 SmallVector<Instruction *, 4> PNUsers;
372 for (auto UI = PN->use_begin(), E = PN->use_end(); UI != E; ++UI) {
373 Use &U = *UI;
374 Instruction *User = cast<Instruction>(U.getUser());
375
376 if (User->getParent() != BB)
377 continue;
378 if (ReplacedInsts.count(User)) {
379 DEBUG(dbgs() << *User << " has already been replaced. Skipping...\n");
380 continue;
381 }
382 if (isa<PHINode>(User))
383 continue;
384 if (User->mayHaveSideEffects())
385 continue;
386 if (!canReplace(User))
387 continue;
388
389 PNUsers.push_back(User);
390 }
391 DEBUG(dbgs() << PNUsers.size() << " use(s) of the PHI in the block\n");
392
393 // For each interesting use I of PN, find an Instruction BEUser that
394 // performs the same operation as I on BEInst and whose other operands,
395 // if any, can also be rematerialized in OtherBB. We stop when we find the
396 // first such Instruction BEUser. This is because once BEUser is
397 // rematerialized in OtherBB, we may find more such "fixup" opportunities
398 // in this block. So, we'll start over again.
399 for (Instruction *I : PNUsers) {
400 for (auto UI = BEInst->use_begin(), E = BEInst->use_end(); UI != E;
401 ++UI) {
402 Use &U = *UI;
403 Instruction *BEUser = cast<Instruction>(U.getUser());
404
405 if (BEUser->getParent() != BB)
406 continue;
407 if (!isEquivalentOperation(I, BEUser))
408 continue;
409
410 int NumOperands = I->getNumOperands();
411
412 for (int OpNo = 0; OpNo < NumOperands; ++OpNo) {
413 Value *Op = I->getOperand(OpNo);
414 Instruction *OpInst = dyn_cast<Instruction>(Op);
415 if (!OpInst)
416 continue;
417
418 Value *BEOp = BEUser->getOperand(OpNo);
419 Instruction *BEOpInst = dyn_cast<Instruction>(BEOp);
420
421 if (!isDepChainBtwn(OpInst, BEOpInst, Iters)) {
422 BEUser = nullptr;
423 break;
424 }
425 }
426 if (BEUser) {
427 DEBUG(dbgs() << "Found Value for reuse.\n");
428 ReuseCandidate.Inst2Replace = I;
429 ReuseCandidate.BackedgeInst = BEUser;
430 return;
431 } else
432 ReuseCandidate.reset();
433 }
434 }
435 }
436 ReuseCandidate.reset();
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000437}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000438
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000439Value *HexagonVectorLoopCarriedReuse::findValueInBlock(Value *Op,
440 BasicBlock *BB) {
441 PHINode *PN = dyn_cast<PHINode>(Op);
442 assert(PN);
443 Value *ValueInBlock = PN->getIncomingValueForBlock(BB);
444 return ValueInBlock;
445}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000446
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000447void HexagonVectorLoopCarriedReuse::reuseValue() {
448 DEBUG(dbgs() << ReuseCandidate);
449 Instruction *Inst2Replace = ReuseCandidate.Inst2Replace;
450 Instruction *BEInst = ReuseCandidate.BackedgeInst;
451 int NumOperands = Inst2Replace->getNumOperands();
452 std::map<Instruction *, DepChain *> DepChains;
453 int Iterations = -1;
454 BasicBlock *LoopPH = CurLoop->getLoopPreheader();
455
456 for (int i = 0; i < NumOperands; ++i) {
457 Instruction *I = dyn_cast<Instruction>(Inst2Replace->getOperand(i));
458 if(!I)
459 continue;
460 else {
461 Instruction *J = cast<Instruction>(BEInst->getOperand(i));
462 DepChain *D = getDepChainBtwn(I, J);
463
464 assert(D &&
465 "No DepChain between corresponding operands in ReuseCandidate\n");
466 if (Iterations == -1)
467 Iterations = D->iterations();
468 assert(Iterations == D->iterations() && "Iterations mismatch");
469 DepChains[I] = D;
470 }
471 }
472
473 DEBUG(dbgs() << "reuseValue is making the following changes\n");
474
475 SmallVector<Instruction *, 4> InstsInPreheader;
476 for (int i = 0; i < Iterations; ++i) {
477 Instruction *InstInPreheader = Inst2Replace->clone();
478 SmallVector<Value *, 4> Ops;
479 for (int j = 0; j < NumOperands; ++j) {
480 Instruction *I = dyn_cast<Instruction>(Inst2Replace->getOperand(j));
481 if (!I)
482 continue;
483 // Get the DepChain corresponding to this operand.
484 DepChain &D = *DepChains[I];
485 // Get the PHI for the iteration number and find
486 // the incoming value from the Loop Preheader for
487 // that PHI.
488 Value *ValInPreheader = findValueInBlock(D[i], LoopPH);
489 InstInPreheader->setOperand(j, ValInPreheader);
490 }
491 InstsInPreheader.push_back(InstInPreheader);
492 InstInPreheader->setName(Inst2Replace->getName() + ".hexagon.vlcr");
493 InstInPreheader->insertBefore(LoopPH->getTerminator());
494 DEBUG(dbgs() << "Added " << *InstInPreheader << " to " << LoopPH->getName()
495 << "\n");
496 }
497 BasicBlock *BB = BEInst->getParent();
498 IRBuilder<> IRB(BB);
499 IRB.SetInsertPoint(BB->getFirstNonPHI());
500 Value *BEVal = BEInst;
501 PHINode *NewPhi;
502 for (int i = Iterations-1; i >=0 ; --i) {
503 Instruction *InstInPreheader = InstsInPreheader[i];
504 NewPhi = IRB.CreatePHI(InstInPreheader->getType(), 2);
505 NewPhi->addIncoming(InstInPreheader, LoopPH);
506 NewPhi->addIncoming(BEVal, BB);
507 DEBUG(dbgs() << "Adding " << *NewPhi << " to " << BB->getName() << "\n");
508 BEVal = NewPhi;
509 }
510 // We are in LCSSA form. So, a value defined inside the Loop is used only
511 // inside the loop. So, the following is safe.
512 Inst2Replace->replaceAllUsesWith(NewPhi);
513 ReplacedInsts.insert(Inst2Replace);
514 ++HexagonNumVectorLoopCarriedReuse;
515}
516
517bool HexagonVectorLoopCarriedReuse::doVLCR() {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000518 assert(CurLoop->getSubLoops().empty() &&
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000519 "Can do VLCR on the innermost loop only");
520 assert((CurLoop->getNumBlocks() == 1) &&
521 "Can do VLCR only on single block loops");
522
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000523 bool Changed;
524 bool Continue;
525
Richard Trieucc10e632017-09-21 23:48:01 +0000526 DEBUG(dbgs() << "Working on Loop: " << *CurLoop->getHeader() << "\n");
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000527 do {
528 // Reset datastructures.
529 Dependences.clear();
530 Continue = false;
531
532 findLoopCarriedDeps();
533 findValueToReuse();
534 if (ReuseCandidate.isDefined()) {
535 reuseValue();
536 Changed = true;
537 Continue = true;
538 }
539 std::for_each(Dependences.begin(), Dependences.end(),
540 std::default_delete<DepChain>());
541 } while (Continue);
542 return Changed;
543}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000544
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000545void HexagonVectorLoopCarriedReuse::findDepChainFromPHI(Instruction *I,
546 DepChain &D) {
547 PHINode *PN = dyn_cast<PHINode>(I);
548 if (!PN) {
549 D.push_back(I);
550 return;
551 } else {
552 auto NumIncomingValues = PN->getNumIncomingValues();
553 if (NumIncomingValues != 2) {
554 D.clear();
555 return;
556 }
557
558 BasicBlock *BB = PN->getParent();
559 if (BB != CurLoop->getHeader()) {
560 D.clear();
561 return;
562 }
563
564 Value *BEVal = PN->getIncomingValueForBlock(BB);
565 Instruction *BEInst = dyn_cast<Instruction>(BEVal);
566 // This is a single block loop with a preheader, so at least
567 // one value should come over the backedge.
568 assert(BEInst && "There should be a value over the backedge");
569
570 Value *PreHdrVal =
571 PN->getIncomingValueForBlock(CurLoop->getLoopPreheader());
572 if(!PreHdrVal || !isa<Instruction>(PreHdrVal)) {
573 D.clear();
574 return;
575 }
576 D.push_back(PN);
577 findDepChainFromPHI(BEInst, D);
578 }
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000579}
580
581bool HexagonVectorLoopCarriedReuse::isDepChainBtwn(Instruction *I1,
582 Instruction *I2,
583 int Iters) {
584 for (auto *D : Dependences) {
585 if (D->front() == I1 && D->back() == I2 && D->iterations() == Iters)
586 return true;
587 }
588 return false;
589}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000590
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000591DepChain *HexagonVectorLoopCarriedReuse::getDepChainBtwn(Instruction *I1,
592 Instruction *I2) {
593 for (auto *D : Dependences) {
594 if (D->front() == I1 && D->back() == I2)
595 return D;
596 }
597 return nullptr;
598}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000599
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000600void HexagonVectorLoopCarriedReuse::findLoopCarriedDeps() {
601 BasicBlock *BB = CurLoop->getHeader();
602 for (auto I = BB->begin(), E = BB->end(); I != E && isa<PHINode>(I); ++I) {
603 auto *PN = cast<PHINode>(I);
604 if (!isa<VectorType>(PN->getType()))
605 continue;
606
607 DepChain *D = new DepChain();
608 findDepChainFromPHI(PN, *D);
609 if (D->size() != 0)
610 Dependences.insert(D);
611 else
612 delete D;
613 }
614 DEBUG(dbgs() << "Found " << Dependences.size() << " dependences\n");
615 DEBUG(for (size_t i = 0; i < Dependences.size(); ++i) {
616 dbgs() << *Dependences[i] << "\n";
617 });
618}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000619
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000620Pass *llvm::createHexagonVectorLoopCarriedReusePass() {
621 return new HexagonVectorLoopCarriedReuse();
622}