blob: e94c248375115e97495630caa9249945cffbbaf7 [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"
David Blaikiea373d182018-03-28 17:44:36 +0000141#include "llvm/Transforms/Utils.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +0000142#include <algorithm>
143#include <cassert>
144#include <cstddef>
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000145#include <map>
Eugene Zelenko3b873362017-09-28 22:27:31 +0000146#include <memory>
147#include <set>
148
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000149using namespace llvm;
150
Eugene Zelenko3b873362017-09-28 22:27:31 +0000151#define DEBUG_TYPE "hexagon-vlcr"
152
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000153STATISTIC(HexagonNumVectorLoopCarriedReuse,
154 "Number of values that were reused from a previous iteration.");
155
156static cl::opt<int> HexagonVLCRIterationLim("hexagon-vlcr-iteration-lim",
157 cl::Hidden,
158 cl::desc("Maximum distance of loop carried dependences that are handled"),
159 cl::init(2), cl::ZeroOrMore);
Eugene Zelenko3b873362017-09-28 22:27:31 +0000160
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000161namespace llvm {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000162
163void initializeHexagonVectorLoopCarriedReusePass(PassRegistry&);
164Pass *createHexagonVectorLoopCarriedReusePass();
165
166} // end namespace llvm
167
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000168namespace {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000169
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000170 // See info about DepChain in the comments at the top of this file.
Eugene Zelenko3b873362017-09-28 22:27:31 +0000171 using ChainOfDependences = SmallVector<Instruction *, 4>;
172
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000173 class DepChain {
174 ChainOfDependences Chain;
Eugene Zelenko3b873362017-09-28 22:27:31 +0000175
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000176 public:
Eugene Zelenko3b873362017-09-28 22:27:31 +0000177 bool isIdentical(DepChain &Other) const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000178 if (Other.size() != size())
179 return false;
180 ChainOfDependences &OtherChain = Other.getChain();
181 for (int i = 0; i < size(); ++i) {
182 if (Chain[i] != OtherChain[i])
183 return false;
184 }
185 return true;
186 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000187
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000188 ChainOfDependences &getChain() {
189 return Chain;
190 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000191
192 int size() const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000193 return Chain.size();
194 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000195
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000196 void clear() {
197 Chain.clear();
198 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000199
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000200 void push_back(Instruction *I) {
201 Chain.push_back(I);
202 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000203
204 int iterations() const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000205 return size() - 1;
206 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000207
208 Instruction *front() const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000209 return Chain.front();
210 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000211
212 Instruction *back() const {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000213 return Chain.back();
214 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000215
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000216 Instruction *&operator[](const int index) {
217 return Chain[index];
218 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000219
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000220 friend raw_ostream &operator<< (raw_ostream &OS, const DepChain &D);
221 };
222
NAKAMURA Takumifec5e102017-09-22 01:01:33 +0000223 LLVM_ATTRIBUTE_UNUSED
NAKAMURA Takumi05f60152017-09-22 01:01:31 +0000224 raw_ostream &operator<<(raw_ostream &OS, const DepChain &D) {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000225 const ChainOfDependences &CD = D.Chain;
226 int ChainSize = CD.size();
227 OS << "**DepChain Start::**\n";
228 for (int i = 0; i < ChainSize -1; ++i) {
229 OS << *(CD[i]) << " -->\n";
230 }
231 OS << *CD[ChainSize-1] << "\n";
232 return OS;
233 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000234
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000235 struct ReuseValue {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000236 Instruction *Inst2Replace = nullptr;
237
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000238 // In the new PHI node that we'll construct this is the value that'll be
239 // used over the backedge. This is teh value that gets reused from a
240 // previous iteration.
Eugene Zelenko3b873362017-09-28 22:27:31 +0000241 Instruction *BackedgeInst = nullptr;
242
243 ReuseValue() = default;
244
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000245 void reset() { Inst2Replace = nullptr; BackedgeInst = nullptr; }
246 bool isDefined() { return Inst2Replace != nullptr; }
247 };
Eugene Zelenko3b873362017-09-28 22:27:31 +0000248
NAKAMURA Takumifec5e102017-09-22 01:01:33 +0000249 LLVM_ATTRIBUTE_UNUSED
NAKAMURA Takumi05f60152017-09-22 01:01:31 +0000250 raw_ostream &operator<<(raw_ostream &OS, const ReuseValue &RU) {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000251 OS << "** ReuseValue ***\n";
252 OS << "Instruction to Replace: " << *(RU.Inst2Replace) << "\n";
253 OS << "Backedge Instruction: " << *(RU.BackedgeInst) << "\n";
254 return OS;
255 }
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000256
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000257 class HexagonVectorLoopCarriedReuse : public LoopPass {
258 public:
259 static char ID;
Eugene Zelenko3b873362017-09-28 22:27:31 +0000260
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000261 explicit HexagonVectorLoopCarriedReuse() : LoopPass(ID) {
262 PassRegistry *PR = PassRegistry::getPassRegistry();
263 initializeHexagonVectorLoopCarriedReusePass(*PR);
264 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000265
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000266 StringRef getPassName() const override {
267 return "Hexagon-specific loop carried reuse for HVX vectors";
268 }
269
Eugene Zelenko3b873362017-09-28 22:27:31 +0000270 void getAnalysisUsage(AnalysisUsage &AU) const override {
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000271 AU.addRequired<LoopInfoWrapperPass>();
272 AU.addRequiredID(LoopSimplifyID);
273 AU.addRequiredID(LCSSAID);
274 AU.addPreservedID(LCSSAID);
275 AU.setPreservesCFG();
276 }
277
278 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
279
280 private:
281 SetVector<DepChain *> Dependences;
282 std::set<Instruction *> ReplacedInsts;
283 Loop *CurLoop;
284 ReuseValue ReuseCandidate;
285
286 bool doVLCR();
287 void findLoopCarriedDeps();
288 void findValueToReuse();
289 void findDepChainFromPHI(Instruction *I, DepChain &D);
290 void reuseValue();
291 Value *findValueInBlock(Value *Op, BasicBlock *BB);
292 bool isDepChainBtwn(Instruction *I1, Instruction *I2, int Iters);
293 DepChain *getDepChainBtwn(Instruction *I1, Instruction *I2);
294 bool isEquivalentOperation(Instruction *I1, Instruction *I2);
295 bool canReplace(Instruction *I);
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000296 };
Eugene Zelenko3b873362017-09-28 22:27:31 +0000297
298} // end anonymous namespace
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000299
300char HexagonVectorLoopCarriedReuse::ID = 0;
301
302INITIALIZE_PASS_BEGIN(HexagonVectorLoopCarriedReuse, "hexagon-vlcr",
303 "Hexagon-specific predictive commoning for HVX vectors", false, false)
304INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
305INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
306INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
307INITIALIZE_PASS_END(HexagonVectorLoopCarriedReuse, "hexagon-vlcr",
308 "Hexagon-specific predictive commoning for HVX vectors", false, false)
309
310bool HexagonVectorLoopCarriedReuse::runOnLoop(Loop *L, LPPassManager &LPM) {
311 if (skipLoop(L))
312 return false;
313
314 if (!L->getLoopPreheader())
315 return false;
316
317 // Work only on innermost loops.
Eugene Zelenko3b873362017-09-28 22:27:31 +0000318 if (!L->getSubLoops().empty())
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000319 return false;
320
321 // Work only on single basic blocks loops.
322 if (L->getNumBlocks() != 1)
323 return false;
324
325 CurLoop = L;
326
327 return doVLCR();
328}
329
330bool HexagonVectorLoopCarriedReuse::isEquivalentOperation(Instruction *I1,
331 Instruction *I2) {
332 if (!I1->isSameOperationAs(I2))
333 return false;
334 // This check is in place specifically for intrinsics. isSameOperationAs will
335 // return two for any two hexagon intrinsics because they are essentially the
336 // same instruciton (CallInst). We need to scratch the surface to see if they
337 // are calls to the same function.
338 if (CallInst *C1 = dyn_cast<CallInst>(I1)) {
339 if (CallInst *C2 = dyn_cast<CallInst>(I2)) {
340 if (C1->getCalledFunction() != C2->getCalledFunction())
341 return false;
342 }
343 }
Ron Lieberman9bcdd802017-10-02 00:34:07 +0000344
345 // If both the Instructions are of Vector Type and any of the element
346 // is integer constant, check their values too for equivalence.
347 if (I1->getType()->isVectorTy() && I2->getType()->isVectorTy()) {
348 unsigned NumOperands = I1->getNumOperands();
349 for (unsigned i = 0; i < NumOperands; ++i) {
350 ConstantInt *C1 = dyn_cast<ConstantInt>(I1->getOperand(i));
351 ConstantInt *C2 = dyn_cast<ConstantInt>(I2->getOperand(i));
352 if(!C1) continue;
353 assert(C2);
354 if (C1->getSExtValue() != C2->getSExtValue())
355 return false;
356 }
357 }
358
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000359 return true;
360}
361
362bool HexagonVectorLoopCarriedReuse::canReplace(Instruction *I) {
363 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
364 if (II &&
365 (II->getIntrinsicID() == Intrinsic::hexagon_V6_hi ||
366 II->getIntrinsicID() == Intrinsic::hexagon_V6_lo)) {
367 DEBUG(dbgs() << "Not considering for reuse: " << *II << "\n");
368 return false;
369 }
370 return true;
371}
372void HexagonVectorLoopCarriedReuse::findValueToReuse() {
373 for (auto *D : Dependences) {
374 DEBUG(dbgs() << "Processing dependence " << *(D->front()) << "\n");
375 if (D->iterations() > HexagonVLCRIterationLim) {
376 DEBUG(dbgs() <<
377 ".. Skipping because number of iterations > than the limit\n");
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();
385 DEBUG(dbgs() << "Checking if any uses of " << *PN << " can be reused\n");
386
387 SmallVector<Instruction *, 4> PNUsers;
388 for (auto UI = PN->use_begin(), E = PN->use_end(); UI != E; ++UI) {
389 Use &U = *UI;
390 Instruction *User = cast<Instruction>(U.getUser());
391
392 if (User->getParent() != BB)
393 continue;
394 if (ReplacedInsts.count(User)) {
395 DEBUG(dbgs() << *User << " has already been replaced. Skipping...\n");
396 continue;
397 }
398 if (isa<PHINode>(User))
399 continue;
400 if (User->mayHaveSideEffects())
401 continue;
402 if (!canReplace(User))
403 continue;
404
405 PNUsers.push_back(User);
406 }
407 DEBUG(dbgs() << PNUsers.size() << " use(s) of the PHI in the block\n");
408
409 // For each interesting use I of PN, find an Instruction BEUser that
410 // performs the same operation as I on BEInst and whose other operands,
411 // if any, can also be rematerialized in OtherBB. We stop when we find the
412 // first such Instruction BEUser. This is because once BEUser is
413 // rematerialized in OtherBB, we may find more such "fixup" opportunities
414 // in this block. So, we'll start over again.
415 for (Instruction *I : PNUsers) {
416 for (auto UI = BEInst->use_begin(), E = BEInst->use_end(); UI != E;
417 ++UI) {
418 Use &U = *UI;
419 Instruction *BEUser = cast<Instruction>(U.getUser());
420
421 if (BEUser->getParent() != BB)
422 continue;
423 if (!isEquivalentOperation(I, BEUser))
424 continue;
425
426 int NumOperands = I->getNumOperands();
427
428 for (int OpNo = 0; OpNo < NumOperands; ++OpNo) {
429 Value *Op = I->getOperand(OpNo);
430 Instruction *OpInst = dyn_cast<Instruction>(Op);
431 if (!OpInst)
432 continue;
433
434 Value *BEOp = BEUser->getOperand(OpNo);
435 Instruction *BEOpInst = dyn_cast<Instruction>(BEOp);
436
437 if (!isDepChainBtwn(OpInst, BEOpInst, Iters)) {
438 BEUser = nullptr;
439 break;
440 }
441 }
442 if (BEUser) {
443 DEBUG(dbgs() << "Found Value for reuse.\n");
444 ReuseCandidate.Inst2Replace = I;
445 ReuseCandidate.BackedgeInst = BEUser;
446 return;
447 } else
448 ReuseCandidate.reset();
449 }
450 }
451 }
452 ReuseCandidate.reset();
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000453}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000454
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000455Value *HexagonVectorLoopCarriedReuse::findValueInBlock(Value *Op,
456 BasicBlock *BB) {
457 PHINode *PN = dyn_cast<PHINode>(Op);
458 assert(PN);
459 Value *ValueInBlock = PN->getIncomingValueForBlock(BB);
460 return ValueInBlock;
461}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000462
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000463void HexagonVectorLoopCarriedReuse::reuseValue() {
464 DEBUG(dbgs() << ReuseCandidate);
465 Instruction *Inst2Replace = ReuseCandidate.Inst2Replace;
466 Instruction *BEInst = ReuseCandidate.BackedgeInst;
467 int NumOperands = Inst2Replace->getNumOperands();
468 std::map<Instruction *, DepChain *> DepChains;
469 int Iterations = -1;
470 BasicBlock *LoopPH = CurLoop->getLoopPreheader();
471
472 for (int i = 0; i < NumOperands; ++i) {
473 Instruction *I = dyn_cast<Instruction>(Inst2Replace->getOperand(i));
474 if(!I)
475 continue;
476 else {
477 Instruction *J = cast<Instruction>(BEInst->getOperand(i));
478 DepChain *D = getDepChainBtwn(I, J);
479
480 assert(D &&
481 "No DepChain between corresponding operands in ReuseCandidate\n");
482 if (Iterations == -1)
483 Iterations = D->iterations();
484 assert(Iterations == D->iterations() && "Iterations mismatch");
485 DepChains[I] = D;
486 }
487 }
488
489 DEBUG(dbgs() << "reuseValue is making the following changes\n");
490
491 SmallVector<Instruction *, 4> InstsInPreheader;
492 for (int i = 0; i < Iterations; ++i) {
493 Instruction *InstInPreheader = Inst2Replace->clone();
494 SmallVector<Value *, 4> Ops;
495 for (int j = 0; j < NumOperands; ++j) {
496 Instruction *I = dyn_cast<Instruction>(Inst2Replace->getOperand(j));
497 if (!I)
498 continue;
499 // Get the DepChain corresponding to this operand.
500 DepChain &D = *DepChains[I];
501 // Get the PHI for the iteration number and find
502 // the incoming value from the Loop Preheader for
503 // that PHI.
504 Value *ValInPreheader = findValueInBlock(D[i], LoopPH);
505 InstInPreheader->setOperand(j, ValInPreheader);
506 }
507 InstsInPreheader.push_back(InstInPreheader);
508 InstInPreheader->setName(Inst2Replace->getName() + ".hexagon.vlcr");
509 InstInPreheader->insertBefore(LoopPH->getTerminator());
510 DEBUG(dbgs() << "Added " << *InstInPreheader << " to " << LoopPH->getName()
511 << "\n");
512 }
513 BasicBlock *BB = BEInst->getParent();
514 IRBuilder<> IRB(BB);
515 IRB.SetInsertPoint(BB->getFirstNonPHI());
516 Value *BEVal = BEInst;
517 PHINode *NewPhi;
518 for (int i = Iterations-1; i >=0 ; --i) {
519 Instruction *InstInPreheader = InstsInPreheader[i];
520 NewPhi = IRB.CreatePHI(InstInPreheader->getType(), 2);
521 NewPhi->addIncoming(InstInPreheader, LoopPH);
522 NewPhi->addIncoming(BEVal, BB);
523 DEBUG(dbgs() << "Adding " << *NewPhi << " to " << BB->getName() << "\n");
524 BEVal = NewPhi;
525 }
526 // We are in LCSSA form. So, a value defined inside the Loop is used only
527 // inside the loop. So, the following is safe.
528 Inst2Replace->replaceAllUsesWith(NewPhi);
529 ReplacedInsts.insert(Inst2Replace);
530 ++HexagonNumVectorLoopCarriedReuse;
531}
532
533bool HexagonVectorLoopCarriedReuse::doVLCR() {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000534 assert(CurLoop->getSubLoops().empty() &&
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000535 "Can do VLCR on the innermost loop only");
536 assert((CurLoop->getNumBlocks() == 1) &&
537 "Can do VLCR only on single block loops");
538
Ron Lieberman9bcdd802017-10-02 00:34:07 +0000539 bool Changed = false;
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000540 bool Continue;
541
Richard Trieucc10e632017-09-21 23:48:01 +0000542 DEBUG(dbgs() << "Working on Loop: " << *CurLoop->getHeader() << "\n");
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000543 do {
544 // Reset datastructures.
545 Dependences.clear();
546 Continue = false;
547
548 findLoopCarriedDeps();
549 findValueToReuse();
550 if (ReuseCandidate.isDefined()) {
551 reuseValue();
Dimitry Andrice44dea92017-12-18 18:56:00 +0000552 Changed = true;
553 Continue = true;
554 }
555 llvm::for_each(Dependences, std::default_delete<DepChain>());
556 } while (Continue);
557 return Changed;
558}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000559
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000560void HexagonVectorLoopCarriedReuse::findDepChainFromPHI(Instruction *I,
561 DepChain &D) {
562 PHINode *PN = dyn_cast<PHINode>(I);
563 if (!PN) {
564 D.push_back(I);
565 return;
566 } else {
567 auto NumIncomingValues = PN->getNumIncomingValues();
568 if (NumIncomingValues != 2) {
569 D.clear();
570 return;
571 }
572
573 BasicBlock *BB = PN->getParent();
574 if (BB != CurLoop->getHeader()) {
575 D.clear();
576 return;
577 }
578
579 Value *BEVal = PN->getIncomingValueForBlock(BB);
580 Instruction *BEInst = dyn_cast<Instruction>(BEVal);
581 // This is a single block loop with a preheader, so at least
582 // one value should come over the backedge.
583 assert(BEInst && "There should be a value over the backedge");
584
585 Value *PreHdrVal =
586 PN->getIncomingValueForBlock(CurLoop->getLoopPreheader());
587 if(!PreHdrVal || !isa<Instruction>(PreHdrVal)) {
588 D.clear();
589 return;
590 }
591 D.push_back(PN);
592 findDepChainFromPHI(BEInst, D);
593 }
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000594}
595
596bool HexagonVectorLoopCarriedReuse::isDepChainBtwn(Instruction *I1,
597 Instruction *I2,
598 int Iters) {
599 for (auto *D : Dependences) {
600 if (D->front() == I1 && D->back() == I2 && D->iterations() == Iters)
601 return true;
602 }
603 return false;
604}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000605
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000606DepChain *HexagonVectorLoopCarriedReuse::getDepChainBtwn(Instruction *I1,
607 Instruction *I2) {
608 for (auto *D : Dependences) {
609 if (D->front() == I1 && D->back() == I2)
610 return D;
611 }
612 return nullptr;
613}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000614
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000615void HexagonVectorLoopCarriedReuse::findLoopCarriedDeps() {
616 BasicBlock *BB = CurLoop->getHeader();
617 for (auto I = BB->begin(), E = BB->end(); I != E && isa<PHINode>(I); ++I) {
618 auto *PN = cast<PHINode>(I);
619 if (!isa<VectorType>(PN->getType()))
620 continue;
621
622 DepChain *D = new DepChain();
623 findDepChainFromPHI(PN, *D);
624 if (D->size() != 0)
625 Dependences.insert(D);
626 else
627 delete D;
628 }
629 DEBUG(dbgs() << "Found " << Dependences.size() << " dependences\n");
630 DEBUG(for (size_t i = 0; i < Dependences.size(); ++i) {
631 dbgs() << *Dependences[i] << "\n";
632 });
633}
Eugene Zelenko3b873362017-09-28 22:27:31 +0000634
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000635Pass *llvm::createHexagonVectorLoopCarriedReusePass() {
636 return new HexagonVectorLoopCarriedReuse();
637}