annotate lib/Analysis/LoopInfo.cpp @ 134:3a76565eade5 LLVM5.0.1

update 5.0.1
author mir3636
date Sat, 17 Feb 2018 09:57:20 +0900
parents 803732b1fca8
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // This file defines the LoopInfo class that is used to identify natural loops
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 // and determine the loop depth of various nodes of the CFG. Note that the
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 // loops identified may actually be several natural loops that share the same
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 // header node... not just a single natural loop.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "llvm/Analysis/LoopInfo.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include "llvm/ADT/DepthFirstIterator.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
19 #include "llvm/ADT/ScopeExit.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "llvm/ADT/SmallPtrSet.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "llvm/Analysis/LoopInfoImpl.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "llvm/Analysis/LoopIterator.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #include "llvm/Analysis/ValueTracking.h"
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
24 #include "llvm/IR/CFG.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #include "llvm/IR/Constants.h"
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
26 #include "llvm/IR/DebugLoc.h"
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
27 #include "llvm/IR/Dominators.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 #include "llvm/IR/Instructions.h"
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
29 #include "llvm/IR/LLVMContext.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 #include "llvm/IR/Metadata.h"
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
31 #include "llvm/IR/PassManager.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 #include "llvm/Support/CommandLine.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 #include "llvm/Support/Debug.h"
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
34 #include "llvm/Support/raw_ostream.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 #include <algorithm>
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 using namespace llvm;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 template class llvm::LoopBase<BasicBlock, Loop>;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 template class llvm::LoopInfoBase<BasicBlock, Loop>;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 // Always verify loopinfo if expensive checking is enabled.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
43 #ifdef EXPENSIVE_CHECKS
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
44 bool llvm::VerifyLoopInfo = true;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 #else
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
46 bool llvm::VerifyLoopInfo = false;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 #endif
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
48 static cl::opt<bool, true>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
49 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
50 cl::Hidden, cl::desc("Verify loop info (time consuming)"));
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 // Loop implementation
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
56 bool Loop::isLoopInvariant(const Value *V) const {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
57 if (const Instruction *I = dyn_cast<Instruction>(V))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 return !contains(I);
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
59 return true; // All non-instructions are loop invariant
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
62 bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
63 return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 bool Loop::makeLoopInvariant(Value *V, bool &Changed,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 Instruction *InsertPt) const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 if (Instruction *I = dyn_cast<Instruction>(V))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 return makeLoopInvariant(I, Changed, InsertPt);
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
70 return true; // All non-instructions are loop-invariant.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 Instruction *InsertPt) const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 // Test if the value is already loop-invariant.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 if (isLoopInvariant(I))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 if (!isSafeToSpeculativelyExecute(I))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 if (I->mayReadFromMemory())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 return false;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
82 // EH block instructions are immobile.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
83 if (I->isEHPad())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 // Determine the insertion point, unless one was given.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 if (!InsertPt) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 BasicBlock *Preheader = getLoopPreheader();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 // Without a preheader, hoisting is not feasible.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 if (!Preheader)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 InsertPt = Preheader->getTerminator();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 // Don't hoist instructions with loop-variant operands.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
94 for (Value *Operand : I->operands())
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
95 if (!makeLoopInvariant(Operand, Changed, InsertPt))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 // Hoist.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 I->moveBefore(InsertPt);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
101 // There is possibility of hoisting this instruction above some arbitrary
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
102 // condition. Any metadata defined on it can be control dependent on this
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
103 // condition. Conservatively strip it here so that we don't give any wrong
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
104 // information to the optimizer.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
105 I->dropUnknownNonDebugMetadata();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
106
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 Changed = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 PHINode *Loop::getCanonicalInductionVariable() const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 BasicBlock *H = getHeader();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
114 BasicBlock *Incoming = nullptr, *Backedge = nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 pred_iterator PI = pred_begin(H);
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
116 assert(PI != pred_end(H) && "Loop must have at least one backedge!");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 Backedge = *PI++;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
118 if (PI == pred_end(H))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
119 return nullptr; // dead loop
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 Incoming = *PI++;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
121 if (PI != pred_end(H))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
122 return nullptr; // multiple backedges?
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 if (contains(Incoming)) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 if (contains(Backedge))
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
126 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 std::swap(Incoming, Backedge);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 } else if (!contains(Backedge))
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
129 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 // Loop over all of the PHI nodes, looking for a canonical indvar.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 PHINode *PN = cast<PHINode>(I);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 if (ConstantInt *CI =
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
135 dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
136 if (CI->isZero())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 if (Instruction *Inc =
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
138 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
139 if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
141 if (CI->isOne())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 return PN;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 }
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
144 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
147 // Check that 'BB' doesn't have any uses outside of the 'L'
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
148 static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
149 DominatorTree &DT) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
150 for (const Instruction &I : BB) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
151 // Tokens can't be used in PHI nodes and live-out tokens prevent loop
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
152 // optimizations, so for the purposes of considered LCSSA form, we
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
153 // can ignore them.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
154 if (I.getType()->isTokenTy())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
155 continue;
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
156
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
157 for (const Use &U : I.uses()) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
158 const Instruction *UI = cast<Instruction>(U.getUser());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
159 const BasicBlock *UserBB = UI->getParent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
160 if (const PHINode *P = dyn_cast<PHINode>(UI))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
161 UserBB = P->getIncomingBlock(U);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
163 // Check the current block, as a fast-path, before checking whether
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
164 // the use is anywhere in the loop. Most values are used in the same
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
165 // block they are defined in. Also, blocks not reachable from the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
166 // entry are special; uses in them don't need to go through PHIs.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
167 if (UserBB != &BB && !L.contains(UserBB) &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
168 DT.isReachableFromEntry(UserBB))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
169 return false;
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
170 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
175 bool Loop::isLCSSAForm(DominatorTree &DT) const {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
176 // For each block we check that it doesn't have any uses outside of this loop.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
177 return all_of(this->blocks(), [&](const BasicBlock *BB) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
178 return isBlockInLCSSAForm(*this, *BB, DT);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
179 });
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
180 }
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
181
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
182 bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
183 // For each block we check that it doesn't have any uses outside of its
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
184 // innermost loop. This process will transitively guarantee that the current
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
185 // loop and all of the nested loops are in LCSSA form.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
186 return all_of(this->blocks(), [&](const BasicBlock *BB) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
187 return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
188 });
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
189 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
190
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 bool Loop::isLoopSimplifyForm() const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 // Normal-form loops have a preheader, a single backedge, and all of their
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 // exits have all their predecessors inside the loop.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
197 // Routines that reform the loop CFG and split edges often fail on indirectbr.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 bool Loop::isSafeToClone() const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 // Return false if any loop blocks contain indirectbrs, or there are any calls
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 // to noduplicate functions.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
201 for (BasicBlock *BB : this->blocks()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
202 if (isa<IndirectBrInst>(BB->getTerminator()))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
205 for (Instruction &I : *BB)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
206 if (auto CS = CallSite(&I))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
207 if (CS.cannotDuplicate())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 MDNode *Loop::getLoopID() const {
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
214 MDNode *LoopID = nullptr;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
215 if (BasicBlock *Latch = getLoopLatch()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
216 LoopID = Latch->getTerminator()->getMetadata(LLVMContext::MD_loop);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 } else {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
218 assert(!getLoopLatch() &&
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
219 "The loop should have no single latch at this point");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 // Go through each predecessor of the loop header and check the
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 // terminator for the metadata.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 BasicBlock *H = getHeader();
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
223 for (BasicBlock *BB : this->blocks()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
224 TerminatorInst *TI = BB->getTerminator();
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
225 MDNode *MD = nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 // Check if this terminator branches to the loop header.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
228 for (BasicBlock *Successor : TI->successors()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
229 if (Successor == H) {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
230 MD = TI->getMetadata(LLVMContext::MD_loop);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 if (!MD)
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
235 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 if (!LoopID)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 LoopID = MD;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 else if (MD != LoopID)
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
240 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 if (!LoopID || LoopID->getNumOperands() == 0 ||
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 LoopID->getOperand(0) != LoopID)
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
245 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 return LoopID;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 void Loop::setLoopID(MDNode *LoopID) const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 assert(LoopID && "Loop ID should not be null");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
253
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
254 if (BasicBlock *Latch = getLoopLatch()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
255 Latch->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 return;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
259 assert(!getLoopLatch() &&
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
260 "The loop should have no single latch at this point");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 BasicBlock *H = getHeader();
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
262 for (BasicBlock *BB : this->blocks()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
263 TerminatorInst *TI = BB->getTerminator();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
264 for (BasicBlock *Successor : TI->successors()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
265 if (Successor == H)
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
266 TI->setMetadata(LLVMContext::MD_loop, LoopID);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
271 void Loop::setLoopAlreadyUnrolled() {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
272 MDNode *LoopID = getLoopID();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
273 // First remove any existing loop unrolling metadata.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
274 SmallVector<Metadata *, 4> MDs;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
275 // Reserve first location for self reference to the LoopID metadata node.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
276 MDs.push_back(nullptr);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
277
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
278 if (LoopID) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
279 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
280 bool IsUnrollMetadata = false;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
281 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
282 if (MD) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
283 const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
284 IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
285 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
286 if (!IsUnrollMetadata)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
287 MDs.push_back(LoopID->getOperand(i));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
288 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
289 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
290
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
291 // Add unroll(disable) metadata to disable future unrolling.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
292 LLVMContext &Context = getHeader()->getContext();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
293 SmallVector<Metadata *, 1> DisableOperands;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
294 DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
295 MDNode *DisableNode = MDNode::get(Context, DisableOperands);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
296 MDs.push_back(DisableNode);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
297
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
298 MDNode *NewLoopID = MDNode::get(Context, MDs);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
299 // Set operand 0 to refer to the loop id itself.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
300 NewLoopID->replaceOperandWith(0, NewLoopID);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
301 setLoopID(NewLoopID);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
302 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
303
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 bool Loop::isAnnotatedParallel() const {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
305 MDNode *DesiredLoopIdMetadata = getLoopID();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
306
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
307 if (!DesiredLoopIdMetadata)
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
308 return false;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 // The loop branch contains the parallel loop metadata. In order to ensure
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 // that any parallel-loop-unaware optimization pass hasn't added loop-carried
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 // dependencies (thus converted the loop back to a sequential loop), check
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 // that all the memory instructions in the loop contain parallelism metadata
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 // that point to the same unique "loop id metadata" the loop branch does.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
315 for (BasicBlock *BB : this->blocks()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
316 for (Instruction &I : *BB) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
317 if (!I.mayReadOrWriteMemory())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
319
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 // The memory instruction can refer to the loop identifier metadata
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 // directly or indirectly through another list metadata (in case of
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 // nested parallel loops). The loop identifier metadata refers to
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
323 // itself so we can check both cases with the same routine.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
324 MDNode *LoopIdMD =
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
325 I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
326
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
327 if (!LoopIdMD)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
329
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
330 bool LoopIdMDFound = false;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
331 for (const MDOperand &MDOp : LoopIdMD->operands()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
332 if (MDOp == DesiredLoopIdMetadata) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
333 LoopIdMDFound = true;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
337
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
338 if (!LoopIdMDFound)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
344
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
345 DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
346
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
347 Loop::LocRange Loop::getLocRange() const {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
348 // If we have a debug location in the loop ID, then use it.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
349 if (MDNode *LoopID = getLoopID()) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
350 DebugLoc Start;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
351 // We use the first DebugLoc in the header as the start location of the loop
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
352 // and if there is a second DebugLoc in the header we use it as end location
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
353 // of the loop.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
354 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
355 if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
356 if (!Start)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
357 Start = DebugLoc(L);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
358 else
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
359 return LocRange(Start, DebugLoc(L));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
360 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
361 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
362
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
363 if (Start)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
364 return LocRange(Start);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
365 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
366
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
367 // Try the pre-header first.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
368 if (BasicBlock *PHeadBB = getLoopPreheader())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
369 if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
370 return LocRange(DL);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
371
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
372 // If we have no pre-header or there are no instructions with debug
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
373 // info in it, try the header.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
374 if (BasicBlock *HeadBB = getHeader())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
375 return LocRange(HeadBB->getTerminator()->getDebugLoc());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
376
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
377 return LocRange();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
378 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
379
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 bool Loop::hasDedicatedExits() const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 // Each predecessor of each exit block of a normal loop is contained
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
382 // within the loop.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
383 SmallVector<BasicBlock *, 4> ExitBlocks;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
384 getExitBlocks(ExitBlocks);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
385 for (BasicBlock *BB : ExitBlocks)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
386 for (BasicBlock *Predecessor : predecessors(BB))
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
387 if (!contains(Predecessor))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
388 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 // All the requirements are met.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
390 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
391 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
392
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
393 void Loop::getUniqueExitBlocks(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
394 SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
395 assert(hasDedicatedExits() &&
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
396 "getUniqueExitBlocks assumes the loop has canonical form exits!");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
397
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
398 SmallVector<BasicBlock *, 32> SwitchExitBlocks;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
399 for (BasicBlock *BB : this->blocks()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
400 SwitchExitBlocks.clear();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
401 for (BasicBlock *Successor : successors(BB)) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
402 // If block is inside the loop then it is not an exit block.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
403 if (contains(Successor))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
404 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
405
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
406 pred_iterator PI = pred_begin(Successor);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
407 BasicBlock *FirstPred = *PI;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
408
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 // If current basic block is this exit block's first predecessor
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 // then only insert exit block in to the output ExitBlocks vector.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
411 // This ensures that same exit block is not inserted twice into
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
412 // ExitBlocks vector.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
413 if (BB != FirstPred)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
414 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
415
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
416 // If a terminator has more then two successors, for example SwitchInst,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
417 // then it is possible that there are multiple edges from current block
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
418 // to one exit block.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
419 if (std::distance(succ_begin(BB), succ_end(BB)) <= 2) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
420 ExitBlocks.push_back(Successor);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
421 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
422 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
423
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
424 // In case of multiple edges from current block to exit block, collect
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
425 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
426 // duplicate edges.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
427 if (!is_contained(SwitchExitBlocks, Successor)) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
428 SwitchExitBlocks.push_back(Successor);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
429 ExitBlocks.push_back(Successor);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
430 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
431 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
432 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
433 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
434
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
435 BasicBlock *Loop::getUniqueExitBlock() const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
436 SmallVector<BasicBlock *, 8> UniqueExitBlocks;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
437 getUniqueExitBlocks(UniqueExitBlocks);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
438 if (UniqueExitBlocks.size() == 1)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
439 return UniqueExitBlocks[0];
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
440 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
441 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
442
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
443 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
444 LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
445
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
446 LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
447 print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
448 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
449 #endif
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
450
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
451 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
452 // UnloopUpdater implementation
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
453 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
454
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
455 namespace {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
456 /// Find the new parent loop for all blocks within the "unloop" whose last
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 /// backedges has just been removed.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 class UnloopUpdater {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
459 Loop &Unloop;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
460 LoopInfo *LI;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
461
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
462 LoopBlocksDFS DFS;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
463
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
464 // Map unloop's immediate subloops to their nearest reachable parents. Nested
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
465 // loops within these subloops will not change parents. However, an immediate
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
466 // subloop's new parent will be the nearest loop reachable from either its own
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
467 // exits *or* any of its nested loop's exits.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
468 DenseMap<Loop *, Loop *> SubloopParents;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
469
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
470 // Flag the presence of an irreducible backedge whose destination is a block
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
471 // directly contained by the original unloop.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
472 bool FoundIB;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
473
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
474 public:
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
475 UnloopUpdater(Loop *UL, LoopInfo *LInfo)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
476 : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
477
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
478 void updateBlockParents();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
479
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
480 void removeBlocksFromAncestors();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
481
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
482 void updateSubloopParents();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
483
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
484 protected:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
485 Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
486 };
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
487 } // end anonymous namespace
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
488
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
489 /// Update the parent loop for all blocks that are directly contained within the
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
490 /// original "unloop".
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
491 void UnloopUpdater::updateBlockParents() {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
492 if (Unloop.getNumBlocks()) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
493 // Perform a post order CFG traversal of all blocks within this loop,
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
494 // propagating the nearest loop from successors to predecessors.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
495 LoopBlocksTraversal Traversal(DFS, LI);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
496 for (BasicBlock *POI : Traversal) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
497
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
498 Loop *L = LI->getLoopFor(POI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
499 Loop *NL = getNearestLoop(POI, L);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
500
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
501 if (NL != L) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
502 // For reducible loops, NL is now an ancestor of Unloop.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
503 assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
504 "uninitialized successor");
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
505 LI->changeLoopFor(POI, NL);
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
506 } else {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
507 // Or the current block is part of a subloop, in which case its parent
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
508 // is unchanged.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
509 assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
510 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
511 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
512 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
513 // Each irreducible loop within the unloop induces a round of iteration using
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
514 // the DFS result cached by Traversal.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
515 bool Changed = FoundIB;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
516 for (unsigned NIters = 0; Changed; ++NIters) {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
517 assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
518
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
519 // Iterate over the postorder list of blocks, propagating the nearest loop
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
520 // from successors to predecessors as before.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
521 Changed = false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
522 for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
523 POE = DFS.endPostorder();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
524 POI != POE; ++POI) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
525
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
526 Loop *L = LI->getLoopFor(*POI);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
527 Loop *NL = getNearestLoop(*POI, L);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
528 if (NL != L) {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
529 assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
530 "uninitialized successor");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
531 LI->changeLoopFor(*POI, NL);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
532 Changed = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
533 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
534 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
535 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
536 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
537
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
538 /// Remove unloop's blocks from all ancestors below their new parents.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
539 void UnloopUpdater::removeBlocksFromAncestors() {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
540 // Remove all unloop's blocks (including those in nested subloops) from
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
541 // ancestors below the new parent loop.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
542 for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
543 BI != BE; ++BI) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
544 Loop *OuterParent = LI->getLoopFor(*BI);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
545 if (Unloop.contains(OuterParent)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
546 while (OuterParent->getParentLoop() != &Unloop)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
547 OuterParent = OuterParent->getParentLoop();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
548 OuterParent = SubloopParents[OuterParent];
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
549 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
550 // Remove blocks from former Ancestors except Unloop itself which will be
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
551 // deleted.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
552 for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
553 OldParent = OldParent->getParentLoop()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
554 assert(OldParent && "new loop is not an ancestor of the original");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
555 OldParent->removeBlockFromLoop(*BI);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
556 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
557 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
558 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
559
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
560 /// Update the parent loop for all subloops directly nested within unloop.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
561 void UnloopUpdater::updateSubloopParents() {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
562 while (!Unloop.empty()) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
563 Loop *Subloop = *std::prev(Unloop.end());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
564 Unloop.removeChildLoop(std::prev(Unloop.end()));
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
565
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
566 assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
567 if (Loop *Parent = SubloopParents[Subloop])
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
568 Parent->addChildLoop(Subloop);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
569 else
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 LI->addTopLevelLoop(Subloop);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
571 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
572 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
573
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
574 /// Return the nearest parent loop among this block's successors. If a successor
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
575 /// is a subloop header, consider its parent to be the nearest parent of the
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
576 /// subloop's exits.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
577 ///
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
578 /// For subloop blocks, simply update SubloopParents and return NULL.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
579 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
580
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
581 // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
582 // is considered uninitialized.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
583 Loop *NearLoop = BBLoop;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
584
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
585 Loop *Subloop = nullptr;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
586 if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
587 Subloop = NearLoop;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
588 // Find the subloop ancestor that is directly contained within Unloop.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
589 while (Subloop->getParentLoop() != &Unloop) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
590 Subloop = Subloop->getParentLoop();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
591 assert(Subloop && "subloop is not an ancestor of the original loop");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
592 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
593 // Get the current nearest parent of the Subloop exits, initially Unloop.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
594 NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
595 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
596
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
597 succ_iterator I = succ_begin(BB), E = succ_end(BB);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
598 if (I == E) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
599 assert(!Subloop && "subloop blocks must have a successor");
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
600 NearLoop = nullptr; // unloop blocks may now exit the function.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
601 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
602 for (; I != E; ++I) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
603 if (*I == BB)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
604 continue; // self loops are uninteresting
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
605
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
606 Loop *L = LI->getLoopFor(*I);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
607 if (L == &Unloop) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
608 // This successor has not been processed. This path must lead to an
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
609 // irreducible backedge.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
610 assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
611 FoundIB = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
612 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
613 if (L != &Unloop && Unloop.contains(L)) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
614 // Successor is in a subloop.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
615 if (Subloop)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
616 continue; // Branching within subloops. Ignore it.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
617
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
618 // BB branches from the original into a subloop header.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
619 assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
620
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
621 // Get the current nearest parent of the Subloop's exits.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
622 L = SubloopParents[L];
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
623 // L could be Unloop if the only exit was an irreducible backedge.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
624 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
625 if (L == &Unloop) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
626 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
627 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
628 // Handle critical edges from Unloop into a sibling loop.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
629 if (L && !L->contains(&Unloop)) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
630 L = L->getParentLoop();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
631 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
632 // Remember the nearest parent loop among successors or subloop exits.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
633 if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
634 NearLoop = L;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
635 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
636 if (Subloop) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
637 SubloopParents[Subloop] = NearLoop;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
638 return BBLoop;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
639 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
640 return NearLoop;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
641 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
642
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
643 LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
644
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
645 bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
646 FunctionAnalysisManager::Invalidator &) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
647 // Check whether the analysis, all analyses on functions, or the function's
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
648 // CFG have been preserved.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
649 auto PAC = PA.getChecker<LoopAnalysis>();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
650 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
651 PAC.preservedSet<CFGAnalyses>());
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
652 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
653
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
654 void LoopInfo::erase(Loop *Unloop) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
655 assert(!Unloop->isInvalid() && "Loop has already been erased!");
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
656
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
657 auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
658
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
659 // First handle the special case of no parent loop to simplify the algorithm.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
660 if (!Unloop->getParentLoop()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
661 // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
662 for (Loop::block_iterator I = Unloop->block_begin(),
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
663 E = Unloop->block_end();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
664 I != E; ++I) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
665
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
666 // Don't reparent blocks in subloops.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
667 if (getLoopFor(*I) != Unloop)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
668 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
669
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
670 // Blocks no longer have a parent but are still referenced by Unloop until
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
671 // the Unloop object is deleted.
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
672 changeLoopFor(*I, nullptr);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
673 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
674
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
675 // Remove the loop from the top-level LoopInfo object.
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
676 for (iterator I = begin();; ++I) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
677 assert(I != end() && "Couldn't find loop");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
678 if (*I == Unloop) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
679 removeLoop(I);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
680 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
681 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
682 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
683
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
684 // Move all of the subloops to the top-level.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
685 while (!Unloop->empty())
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
686 addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
687
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
688 return;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
689 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
690
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
691 // Update the parent loop for all blocks within the loop. Blocks within
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
692 // subloops will not change parents.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
693 UnloopUpdater Updater(Unloop, this);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
694 Updater.updateBlockParents();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
695
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
696 // Remove blocks from former ancestor loops.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
697 Updater.removeBlocksFromAncestors();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
698
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
699 // Add direct subloops as children in their new parent loop.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
700 Updater.updateSubloopParents();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
701
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
702 // Remove unloop from its parent loop.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
703 Loop *ParentLoop = Unloop->getParentLoop();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
704 for (Loop::iterator I = ParentLoop->begin();; ++I) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
705 assert(I != ParentLoop->end() && "Couldn't find loop");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
706 if (*I == Unloop) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
707 ParentLoop->removeChildLoop(I);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
708 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
709 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
710 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
711 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
712
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
713 AnalysisKey LoopAnalysis::Key;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
714
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
715 LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
716 // FIXME: Currently we create a LoopInfo from scratch for every function.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
717 // This may prove to be too wasteful due to deallocating and re-allocating
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
718 // memory each time for the underlying map and vector datastructures. At some
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
719 // point it may prove worthwhile to use a freelist and recycle LoopInfo
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
720 // objects. I don't want to add that kind of complexity until the scope of
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
721 // the problem is better understood.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
722 LoopInfo LI;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
723 LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
724 return LI;
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
725 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
726
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
727 PreservedAnalyses LoopPrinterPass::run(Function &F,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
728 FunctionAnalysisManager &AM) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
729 AM.getResult<LoopAnalysis>(F).print(OS);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
730 return PreservedAnalyses::all();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
731 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
732
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
733 void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
734
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
735 if (forcePrintModuleIR()) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
736 // handling -print-module-scope
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
737 OS << Banner << " (loop: ";
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
738 L.getHeader()->printAsOperand(OS, false);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
739 OS << ")\n";
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
740
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
741 // printing whole module
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
742 OS << *L.getHeader()->getModule();
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
743 return;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
744 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
745
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
746 OS << Banner;
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
747
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
748 auto *PreHeader = L.getLoopPreheader();
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
749 if (PreHeader) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
750 OS << "\n; Preheader:";
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
751 PreHeader->print(OS);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
752 OS << "\n; Loop:";
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
753 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
754
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
755 for (auto *Block : L.blocks())
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
756 if (Block)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
757 Block->print(OS);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
758 else
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
759 OS << "Printing <null> block";
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
760
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
761 SmallVector<BasicBlock *, 8> ExitBlocks;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
762 L.getExitBlocks(ExitBlocks);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
763 if (!ExitBlocks.empty()) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
764 OS << "\n; Exit blocks";
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
765 for (auto *Block : ExitBlocks)
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
766 if (Block)
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
767 Block->print(OS);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
768 else
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
769 OS << "Printing <null> block";
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
770 }
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
771 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
772
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
773 //===----------------------------------------------------------------------===//
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
774 // LoopInfo implementation
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
775 //
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
776
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
777 char LoopInfoWrapperPass::ID = 0;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
778 INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
779 true, true)
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
780 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
781 INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
782 true, true)
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
783
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
784 bool LoopInfoWrapperPass::runOnFunction(Function &) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
785 releaseMemory();
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
786 LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
787 return false;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
788 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
789
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
790 void LoopInfoWrapperPass::verifyAnalysis() const {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
791 // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
792 // function each time verifyAnalysis is called is very expensive. The
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
793 // -verify-loop-info option can enable this. In order to perform some
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
794 // checking by default, LoopPass has been taught to call verifyLoop manually
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
795 // during loop pass sequences.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
796 if (VerifyLoopInfo) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
797 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
798 LI.verify(DT);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
799 }
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
800 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
801
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
802 void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
803 AU.setPreservesAll();
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
804 AU.addRequired<DominatorTreeWrapperPass>();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
805 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
806
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
807 void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
808 LI.print(OS);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
809 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
810
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
811 PreservedAnalyses LoopVerifierPass::run(Function &F,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
812 FunctionAnalysisManager &AM) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
813 LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
814 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
815 LI.verify(DT);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
816 return PreservedAnalyses::all();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
817 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
818
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
819 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
820 // LoopBlocksDFS implementation
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
821 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
822
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
823 /// Traverse the loop blocks and store the DFS result.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
824 /// Useful for clients that just want the final DFS result and don't need to
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
825 /// visit blocks during the initial traversal.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
826 void LoopBlocksDFS::perform(LoopInfo *LI) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
827 LoopBlocksTraversal Traversal(*this, LI);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
828 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
829 POE = Traversal.end();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
830 POI != POE; ++POI)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
831 ;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
832 }