annotate lib/CodeGen/MachineOutliner.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
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===---- MachineOutliner.cpp - Outline instructions -----------*- C++ -*-===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 /// \file
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 /// Replaces repeated sequences of instructions with function calls.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 /// This works by placing every instruction from every basic block in a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 /// suffix tree, and repeatedly querying that tree for repeated sequences of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 /// instructions. If a sequence of instructions appears often, then it ought
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 /// to be beneficial to pull out into a function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 /// The MachineOutliner communicates with a given target using hooks defined in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 /// TargetInstrInfo.h. The target supplies the outliner with information on how
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 /// a specific sequence of instructions should be outlined. This information
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 /// is used to deduce the number of instructions necessary to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 /// * Create an outlined function
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 /// * Call that outlined function
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 /// Targets must implement
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 /// * getOutliningCandidateInfo
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 /// * insertOutlinerEpilogue
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 /// * insertOutlinedCall
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 /// * insertOutlinerPrologue
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 /// * isFunctionSafeToOutlineFrom
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 /// in order to make use of the MachineOutliner.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 /// This was originally presented at the 2016 LLVM Developers' Meeting in the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 /// talk "Reducing Code Size Using Outlining". For a high-level overview of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 /// how this pass works, the talk is available on YouTube at
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 /// https://www.youtube.com/watch?v=yorld-WSOeU
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 /// The slides for the talk are available at
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 /// http://www.llvm.org/devmtg/2016-11/Slides/Paquette-Outliner.pdf
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 /// The talk provides an overview of how the outliner finds candidates and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 /// ultimately outlines them. It describes how the main data structure for this
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 /// pass, the suffix tree, is queried and purged for candidates. It also gives
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 /// a simplified suffix tree construction algorithm for suffix trees based off
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 /// of the algorithm actually used here, Ukkonen's algorithm.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 /// For the original RFC for this pass, please see
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 /// http://lists.llvm.org/pipermail/llvm-dev/2016-August/104170.html
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 /// For more information on the suffix tree data structure, please see
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 /// https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 #include "llvm/ADT/DenseMap.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 #include "llvm/ADT/Statistic.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 #include "llvm/ADT/Twine.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 #include "llvm/CodeGen/MachineFunction.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 #include "llvm/CodeGen/MachineModuleInfo.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
65 #include "llvm/CodeGen/MachineRegisterInfo.h"
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 #include "llvm/CodeGen/Passes.h"
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
67 #include "llvm/CodeGen/TargetInstrInfo.h"
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
68 #include "llvm/CodeGen/TargetRegisterInfo.h"
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
69 #include "llvm/CodeGen/TargetSubtargetInfo.h"
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
70 #include "llvm/IR/DIBuilder.h"
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 #include "llvm/IR/IRBuilder.h"
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
72 #include "llvm/IR/Mangler.h"
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 #include "llvm/Support/Allocator.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 #include "llvm/Support/Debug.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 #include "llvm/Support/raw_ostream.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 #include <functional>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 #include <map>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 #include <sstream>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 #include <tuple>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 #include <vector>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 #define DEBUG_TYPE "machine-outliner"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 using namespace ore;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 STATISTIC(NumOutlined, "Number of candidates outlined");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 STATISTIC(FunctionsCreated, "Number of functions created");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 namespace {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 /// \brief An individual sequence of instructions to be replaced with a call to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 /// an outlined function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 struct Candidate {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 /// The start index of this \p Candidate in the instruction list.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 unsigned StartIdx;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 /// The number of instructions in this \p Candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100 unsigned Len;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
102 /// The MachineFunction containing this \p Candidate.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
103 MachineFunction *MF = nullptr;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
104
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 /// Set to false if the candidate overlapped with another candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 bool InCandidateList = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 /// \brief The index of this \p Candidate's \p OutlinedFunction in the list of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 /// \p OutlinedFunctions.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 unsigned FunctionIdx;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 /// Contains all target-specific information for this \p Candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 TargetInstrInfo::MachineOutlinerInfo MInfo;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
116 /// If there is a DISubprogram associated with the function that this
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
117 /// Candidate lives in, return it.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
118 DISubprogram *getSubprogramOrNull() const {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
119 assert(MF && "Candidate has no MF!");
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
120 if (DISubprogram *SP = MF->getFunction().getSubprogram())
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
121 return SP;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
122 return nullptr;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
123 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
124
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 /// Return the number of instructions in this Candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 unsigned getLength() const { return Len; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 /// Return the start index of this candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 unsigned getStartIdx() const { return StartIdx; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 // Return the end index of this candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 unsigned getEndIdx() const { return StartIdx + Len - 1; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 /// \brief The number of instructions that would be saved by outlining every
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 /// candidate of this type.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 /// This is a fixed value which is not updated during the candidate pruning
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 /// process. It is only used for deciding which candidate to keep if two
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 /// candidates overlap. The true benefit is stored in the OutlinedFunction
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 /// for some given candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 unsigned Benefit = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
143 Candidate(unsigned StartIdx, unsigned Len, unsigned FunctionIdx,
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
144 MachineFunction *MF)
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
145 : StartIdx(StartIdx), Len(Len), MF(MF), FunctionIdx(FunctionIdx) {}
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 Candidate() {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 /// \brief Used to ensure that \p Candidates are outlined in an order that
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 /// preserves the start and end indices of other \p Candidates.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 bool operator<(const Candidate &RHS) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 return getStartIdx() > RHS.getStartIdx();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 /// \brief The information necessary to create an outlined function for some
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 /// class of candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 struct OutlinedFunction {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 /// The number of candidates for this \p OutlinedFunction.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 unsigned OccurrenceCount = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 std::vector<std::shared_ptr<Candidate>> Candidates;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 /// The actual outlined function created.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 /// This is initialized after we go through and create the actual function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 MachineFunction *MF = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 /// A number assigned to this function which appears at the end of its name.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172 unsigned Name;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 /// \brief The sequence of integers corresponding to the instructions in this
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 /// function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 std::vector<unsigned> Sequence;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 /// Contains all target-specific information for this \p OutlinedFunction.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 TargetInstrInfo::MachineOutlinerInfo MInfo;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
181 /// If there is a DISubprogram for any Candidate for this outlined function,
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
182 /// then return it. Otherwise, return nullptr.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
183 DISubprogram *getSubprogramOrNull() const {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
184 for (const auto &C : Candidates)
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
185 if (DISubprogram *SP = C->getSubprogramOrNull())
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
186 return SP;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
187 return nullptr;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
188 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
189
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 /// Return the number of candidates for this \p OutlinedFunction.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 unsigned getOccurrenceCount() { return OccurrenceCount; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193 /// Decrement the occurrence count of this OutlinedFunction and return the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194 /// new count.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 unsigned decrement() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 assert(OccurrenceCount > 0 && "Can't decrement an empty function!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 OccurrenceCount--;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 return getOccurrenceCount();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 /// \brief Return the number of instructions it would take to outline this
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 /// function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 unsigned getOutliningCost() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 return (OccurrenceCount * MInfo.CallOverhead) + Sequence.size() +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 MInfo.FrameOverhead;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 /// \brief Return the number of instructions that would be saved by outlining
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209 /// this function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210 unsigned getBenefit() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211 unsigned NotOutlinedCost = OccurrenceCount * Sequence.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 unsigned OutlinedCost = getOutliningCost();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 return (NotOutlinedCost < OutlinedCost) ? 0
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214 : NotOutlinedCost - OutlinedCost;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 OutlinedFunction(unsigned Name, unsigned OccurrenceCount,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218 const std::vector<unsigned> &Sequence,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219 TargetInstrInfo::MachineOutlinerInfo &MInfo)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 : OccurrenceCount(OccurrenceCount), Name(Name), Sequence(Sequence),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221 MInfo(MInfo) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224 /// Represents an undefined index in the suffix tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225 const unsigned EmptyIdx = -1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227 /// A node in a suffix tree which represents a substring or suffix.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229 /// Each node has either no children or at least two children, with the root
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230 /// being a exception in the empty tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 /// Children are represented as a map between unsigned integers and nodes. If
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 /// a node N has a child M on unsigned integer k, then the mapping represented
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234 /// by N is a proper prefix of the mapping represented by M. Note that this,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235 /// although similar to a trie is somewhat different: each node stores a full
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236 /// substring of the full mapping rather than a single character state.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238 /// Each internal node contains a pointer to the internal node representing
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239 /// the same string, but with the first character chopped off. This is stored
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 /// in \p Link. Each leaf node stores the start index of its respective
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241 /// suffix in \p SuffixIdx.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242 struct SuffixTreeNode {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 /// The children of this node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246 /// A child existing on an unsigned integer implies that from the mapping
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247 /// represented by the current node, there is a way to reach another
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248 /// mapping by tacking that character on the end of the current string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249 DenseMap<unsigned, SuffixTreeNode *> Children;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251 /// A flag set to false if the node has been pruned from the tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252 bool IsInTree = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254 /// The start index of this node's substring in the main string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 unsigned StartIdx = EmptyIdx;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257 /// The end index of this node's substring in the main string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259 /// Every leaf node must have its \p EndIdx incremented at the end of every
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 /// step in the construction algorithm. To avoid having to update O(N)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261 /// nodes individually at the end of every step, the end index is stored
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 /// as a pointer.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263 unsigned *EndIdx = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 /// For leaves, the start index of the suffix represented by this node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
267 /// For all other nodes, this is ignored.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
268 unsigned SuffixIdx = EmptyIdx;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
269
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
270 /// \brief For internal nodes, a pointer to the internal node representing
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
271 /// the same sequence with the first character chopped off.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
272 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
273 /// This acts as a shortcut in Ukkonen's algorithm. One of the things that
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
274 /// Ukkonen's algorithm does to achieve linear-time construction is
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
275 /// keep track of which node the next insert should be at. This makes each
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
276 /// insert O(1), and there are a total of O(N) inserts. The suffix link
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
277 /// helps with inserting children of internal nodes.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
278 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
279 /// Say we add a child to an internal node with associated mapping S. The
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
280 /// next insertion must be at the node representing S - its first character.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
281 /// This is given by the way that we iteratively build the tree in Ukkonen's
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
282 /// algorithm. The main idea is to look at the suffixes of each prefix in the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
283 /// string, starting with the longest suffix of the prefix, and ending with
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
284 /// the shortest. Therefore, if we keep pointers between such nodes, we can
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
285 /// move to the next insertion point in O(1) time. If we don't, then we'd
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
286 /// have to query from the root, which takes O(N) time. This would make the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
287 /// construction algorithm O(N^2) rather than O(N).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
288 SuffixTreeNode *Link = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
289
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
290 /// The parent of this node. Every node except for the root has a parent.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
291 SuffixTreeNode *Parent = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
292
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
293 /// The number of times this node's string appears in the tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
294 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
295 /// This is equal to the number of leaf children of the string. It represents
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
296 /// the number of suffixes that the node's string is a prefix of.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
297 unsigned OccurrenceCount = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
298
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
299 /// The length of the string formed by concatenating the edge labels from the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
300 /// root to this node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
301 unsigned ConcatLen = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
302
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
303 /// Returns true if this node is a leaf.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
304 bool isLeaf() const { return SuffixIdx != EmptyIdx; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
305
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
306 /// Returns true if this node is the root of its owning \p SuffixTree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
307 bool isRoot() const { return StartIdx == EmptyIdx; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
308
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
309 /// Return the number of elements in the substring associated with this node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
310 size_t size() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
311
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
312 // Is it the root? If so, it's the empty string so return 0.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
313 if (isRoot())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
314 return 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
315
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
316 assert(*EndIdx != EmptyIdx && "EndIdx is undefined!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318 // Size = the number of elements in the string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 // For example, [0 1 2 3] has length 4, not 3. 3-0 = 3, so we have 3-0+1.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320 return *EndIdx - StartIdx + 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323 SuffixTreeNode(unsigned StartIdx, unsigned *EndIdx, SuffixTreeNode *Link,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324 SuffixTreeNode *Parent)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325 : StartIdx(StartIdx), EndIdx(EndIdx), Link(Link), Parent(Parent) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327 SuffixTreeNode() {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 /// A data structure for fast substring queries.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332 /// Suffix trees represent the suffixes of their input strings in their leaves.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 /// A suffix tree is a type of compressed trie structure where each node
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334 /// represents an entire substring rather than a single character. Each leaf
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335 /// of the tree is a suffix.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
336 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
337 /// A suffix tree can be seen as a type of state machine where each state is a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
338 /// substring of the full string. The tree is structured so that, for a string
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
339 /// of length N, there are exactly N leaves in the tree. This structure allows
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
340 /// us to quickly find repeated substrings of the input string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
341 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
342 /// In this implementation, a "string" is a vector of unsigned integers.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
343 /// These integers may result from hashing some data type. A suffix tree can
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
344 /// contain 1 or many strings, which can then be queried as one large string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
345 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
346 /// The suffix tree is implemented using Ukkonen's algorithm for linear-time
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
347 /// suffix tree construction. Ukkonen's algorithm is explained in more detail
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
348 /// in the paper by Esko Ukkonen "On-line construction of suffix trees. The
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
349 /// paper is available at
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
350 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
351 /// https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
352 class SuffixTree {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
353 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
354 /// Stores each leaf node in the tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
355 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
356 /// This is used for finding outlining candidates.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
357 std::vector<SuffixTreeNode *> LeafVector;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
358
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
359 /// Each element is an integer representing an instruction in the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
360 ArrayRef<unsigned> Str;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
361
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
362 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
363 /// Maintains each node in the tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
364 SpecificBumpPtrAllocator<SuffixTreeNode> NodeAllocator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
365
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
366 /// The root of the suffix tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
367 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
368 /// The root represents the empty string. It is maintained by the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
369 /// \p NodeAllocator like every other node in the tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
370 SuffixTreeNode *Root = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
371
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
372 /// Maintains the end indices of the internal nodes in the tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
373 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
374 /// Each internal node is guaranteed to never have its end index change
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
375 /// during the construction algorithm; however, leaves must be updated at
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
376 /// every step. Therefore, we need to store leaf end indices by reference
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
377 /// to avoid updating O(N) leaves at every step of construction. Thus,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
378 /// every internal node must be allocated its own end index.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
379 BumpPtrAllocator InternalEndIdxAllocator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
380
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
381 /// The end index of each leaf in the tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
382 unsigned LeafEndIdx = -1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
383
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
384 /// \brief Helper struct which keeps track of the next insertion point in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
385 /// Ukkonen's algorithm.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
386 struct ActiveState {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
387 /// The next node to insert at.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
388 SuffixTreeNode *Node;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
389
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
390 /// The index of the first character in the substring currently being added.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
391 unsigned Idx = EmptyIdx;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
392
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
393 /// The length of the substring we have to add at the current step.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
394 unsigned Len = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
395 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
396
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
397 /// \brief The point the next insertion will take place at in the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
398 /// construction algorithm.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
399 ActiveState Active;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
400
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
401 /// Allocate a leaf node and add it to the tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
402 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
403 /// \param Parent The parent of this node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
404 /// \param StartIdx The start index of this node's associated string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
405 /// \param Edge The label on the edge leaving \p Parent to this node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
406 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
407 /// \returns A pointer to the allocated leaf node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
408 SuffixTreeNode *insertLeaf(SuffixTreeNode &Parent, unsigned StartIdx,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
409 unsigned Edge) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
410
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
411 assert(StartIdx <= LeafEndIdx && "String can't start after it ends!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
412
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
413 SuffixTreeNode *N = new (NodeAllocator.Allocate())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
414 SuffixTreeNode(StartIdx, &LeafEndIdx, nullptr, &Parent);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
415 Parent.Children[Edge] = N;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
416
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
417 return N;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
418 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
419
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
420 /// Allocate an internal node and add it to the tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
421 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
422 /// \param Parent The parent of this node. Only null when allocating the root.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
423 /// \param StartIdx The start index of this node's associated string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
424 /// \param EndIdx The end index of this node's associated string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
425 /// \param Edge The label on the edge leaving \p Parent to this node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
426 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
427 /// \returns A pointer to the allocated internal node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
428 SuffixTreeNode *insertInternalNode(SuffixTreeNode *Parent, unsigned StartIdx,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
429 unsigned EndIdx, unsigned Edge) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
430
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
431 assert(StartIdx <= EndIdx && "String can't start after it ends!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
432 assert(!(!Parent && StartIdx != EmptyIdx) &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
433 "Non-root internal nodes must have parents!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
434
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
435 unsigned *E = new (InternalEndIdxAllocator) unsigned(EndIdx);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
436 SuffixTreeNode *N = new (NodeAllocator.Allocate())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
437 SuffixTreeNode(StartIdx, E, Root, Parent);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
438 if (Parent)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
439 Parent->Children[Edge] = N;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
440
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
441 return N;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
442 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
443
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
444 /// \brief Set the suffix indices of the leaves to the start indices of their
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
445 /// respective suffixes. Also stores each leaf in \p LeafVector at its
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
446 /// respective suffix index.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
447 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
448 /// \param[in] CurrNode The node currently being visited.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
449 /// \param CurrIdx The current index of the string being visited.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
450 void setSuffixIndices(SuffixTreeNode &CurrNode, unsigned CurrIdx) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
451
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
452 bool IsLeaf = CurrNode.Children.size() == 0 && !CurrNode.isRoot();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
453
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
454 // Store the length of the concatenation of all strings from the root to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
455 // this node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
456 if (!CurrNode.isRoot()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
457 if (CurrNode.ConcatLen == 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
458 CurrNode.ConcatLen = CurrNode.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
459
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
460 if (CurrNode.Parent)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
461 CurrNode.ConcatLen += CurrNode.Parent->ConcatLen;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
462 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
463
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
464 // Traverse the tree depth-first.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
465 for (auto &ChildPair : CurrNode.Children) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
466 assert(ChildPair.second && "Node had a null child!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
467 setSuffixIndices(*ChildPair.second, CurrIdx + ChildPair.second->size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
468 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
469
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
470 // Is this node a leaf?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
471 if (IsLeaf) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
472 // If yes, give it a suffix index and bump its parent's occurrence count.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
473 CurrNode.SuffixIdx = Str.size() - CurrIdx;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
474 assert(CurrNode.Parent && "CurrNode had no parent!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
475 CurrNode.Parent->OccurrenceCount++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
476
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
477 // Store the leaf in the leaf vector for pruning later.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
478 LeafVector[CurrNode.SuffixIdx] = &CurrNode;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
479 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
480 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
481
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
482 /// \brief Construct the suffix tree for the prefix of the input ending at
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
483 /// \p EndIdx.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
484 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
485 /// Used to construct the full suffix tree iteratively. At the end of each
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
486 /// step, the constructed suffix tree is either a valid suffix tree, or a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
487 /// suffix tree with implicit suffixes. At the end of the final step, the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
488 /// suffix tree is a valid tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
489 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
490 /// \param EndIdx The end index of the current prefix in the main string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
491 /// \param SuffixesToAdd The number of suffixes that must be added
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
492 /// to complete the suffix tree at the current phase.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
493 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
494 /// \returns The number of suffixes that have not been added at the end of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
495 /// this step.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
496 unsigned extend(unsigned EndIdx, unsigned SuffixesToAdd) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
497 SuffixTreeNode *NeedsLink = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
498
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
499 while (SuffixesToAdd > 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
500
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
501 // Are we waiting to add anything other than just the last character?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
502 if (Active.Len == 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
503 // If not, then say the active index is the end index.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
504 Active.Idx = EndIdx;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
505 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
506
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
507 assert(Active.Idx <= EndIdx && "Start index can't be after end index!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
508
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
509 // The first character in the current substring we're looking at.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
510 unsigned FirstChar = Str[Active.Idx];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
511
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
512 // Have we inserted anything starting with FirstChar at the current node?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
513 if (Active.Node->Children.count(FirstChar) == 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
514 // If not, then we can just insert a leaf and move too the next step.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
515 insertLeaf(*Active.Node, EndIdx, FirstChar);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
516
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
517 // The active node is an internal node, and we visited it, so it must
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
518 // need a link if it doesn't have one.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
519 if (NeedsLink) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
520 NeedsLink->Link = Active.Node;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
521 NeedsLink = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
522 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
523 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
524 // There's a match with FirstChar, so look for the point in the tree to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
525 // insert a new node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
526 SuffixTreeNode *NextNode = Active.Node->Children[FirstChar];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
527
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
528 unsigned SubstringLen = NextNode->size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
529
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
530 // Is the current suffix we're trying to insert longer than the size of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
531 // the child we want to move to?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
532 if (Active.Len >= SubstringLen) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
533 // If yes, then consume the characters we've seen and move to the next
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
534 // node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
535 Active.Idx += SubstringLen;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
536 Active.Len -= SubstringLen;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
537 Active.Node = NextNode;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
538 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
539 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
540
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
541 // Otherwise, the suffix we're trying to insert must be contained in the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
542 // next node we want to move to.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
543 unsigned LastChar = Str[EndIdx];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
544
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
545 // Is the string we're trying to insert a substring of the next node?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
546 if (Str[NextNode->StartIdx + Active.Len] == LastChar) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
547 // If yes, then we're done for this step. Remember our insertion point
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
548 // and move to the next end index. At this point, we have an implicit
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
549 // suffix tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
550 if (NeedsLink && !Active.Node->isRoot()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
551 NeedsLink->Link = Active.Node;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
552 NeedsLink = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
553 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
554
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
555 Active.Len++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
556 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
557 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
558
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
559 // The string we're trying to insert isn't a substring of the next node,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
560 // but matches up to a point. Split the node.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
561 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
562 // For example, say we ended our search at a node n and we're trying to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
563 // insert ABD. Then we'll create a new node s for AB, reduce n to just
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
564 // representing C, and insert a new leaf node l to represent d. This
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
565 // allows us to ensure that if n was a leaf, it remains a leaf.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
566 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
567 // | ABC ---split---> | AB
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
568 // n s
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
569 // C / \ D
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
570 // n l
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
571
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
572 // The node s from the diagram
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
573 SuffixTreeNode *SplitNode =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
574 insertInternalNode(Active.Node, NextNode->StartIdx,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
575 NextNode->StartIdx + Active.Len - 1, FirstChar);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
576
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
577 // Insert the new node representing the new substring into the tree as
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
578 // a child of the split node. This is the node l from the diagram.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
579 insertLeaf(*SplitNode, EndIdx, LastChar);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
580
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
581 // Make the old node a child of the split node and update its start
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
582 // index. This is the node n from the diagram.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
583 NextNode->StartIdx += Active.Len;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
584 NextNode->Parent = SplitNode;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
585 SplitNode->Children[Str[NextNode->StartIdx]] = NextNode;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
586
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
587 // SplitNode is an internal node, update the suffix link.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
588 if (NeedsLink)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
589 NeedsLink->Link = SplitNode;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
590
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
591 NeedsLink = SplitNode;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
592 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
593
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
594 // We've added something new to the tree, so there's one less suffix to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
595 // add.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
596 SuffixesToAdd--;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
597
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
598 if (Active.Node->isRoot()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
599 if (Active.Len > 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
600 Active.Len--;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
601 Active.Idx = EndIdx - SuffixesToAdd + 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
602 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
603 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
604 // Start the next phase at the next smallest suffix.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
605 Active.Node = Active.Node->Link;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
606 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
607 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
608
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
609 return SuffixesToAdd;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
610 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
611
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
612 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
613 /// Construct a suffix tree from a sequence of unsigned integers.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
614 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
615 /// \param Str The string to construct the suffix tree for.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
616 SuffixTree(const std::vector<unsigned> &Str) : Str(Str) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
617 Root = insertInternalNode(nullptr, EmptyIdx, EmptyIdx, 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
618 Root->IsInTree = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
619 Active.Node = Root;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
620 LeafVector = std::vector<SuffixTreeNode *>(Str.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
621
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
622 // Keep track of the number of suffixes we have to add of the current
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
623 // prefix.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
624 unsigned SuffixesToAdd = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
625 Active.Node = Root;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
626
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
627 // Construct the suffix tree iteratively on each prefix of the string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
628 // PfxEndIdx is the end index of the current prefix.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
629 // End is one past the last element in the string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
630 for (unsigned PfxEndIdx = 0, End = Str.size(); PfxEndIdx < End;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
631 PfxEndIdx++) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
632 SuffixesToAdd++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
633 LeafEndIdx = PfxEndIdx; // Extend each of the leaves.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
634 SuffixesToAdd = extend(PfxEndIdx, SuffixesToAdd);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
635 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
636
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
637 // Set the suffix indices of each leaf.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
638 assert(Root && "Root node can't be nullptr!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
639 setSuffixIndices(*Root, 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
640 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
641 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
642
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
643 /// \brief Maps \p MachineInstrs to unsigned integers and stores the mappings.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
644 struct InstructionMapper {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
645
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
646 /// \brief The next available integer to assign to a \p MachineInstr that
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
647 /// cannot be outlined.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
648 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
649 /// Set to -3 for compatability with \p DenseMapInfo<unsigned>.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
650 unsigned IllegalInstrNumber = -3;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
651
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
652 /// \brief The next available integer to assign to a \p MachineInstr that can
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
653 /// be outlined.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
654 unsigned LegalInstrNumber = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
655
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
656 /// Correspondence from \p MachineInstrs to unsigned integers.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
657 DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
658 InstructionIntegerMap;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
659
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
660 /// Corresponcence from unsigned integers to \p MachineInstrs.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
661 /// Inverse of \p InstructionIntegerMap.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
662 DenseMap<unsigned, MachineInstr *> IntegerInstructionMap;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
663
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
664 /// The vector of unsigned integers that the module is mapped to.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
665 std::vector<unsigned> UnsignedVec;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
666
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
667 /// \brief Stores the location of the instruction associated with the integer
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
668 /// at index i in \p UnsignedVec for each index i.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
669 std::vector<MachineBasicBlock::iterator> InstrList;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
670
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
671 /// \brief Maps \p *It to a legal integer.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
672 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
673 /// Updates \p InstrList, \p UnsignedVec, \p InstructionIntegerMap,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
674 /// \p IntegerInstructionMap, and \p LegalInstrNumber.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
675 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
676 /// \returns The integer that \p *It was mapped to.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
677 unsigned mapToLegalUnsigned(MachineBasicBlock::iterator &It) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
678
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
679 // Get the integer for this instruction or give it the current
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
680 // LegalInstrNumber.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
681 InstrList.push_back(It);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
682 MachineInstr &MI = *It;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
683 bool WasInserted;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
684 DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>::iterator
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
685 ResultIt;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
686 std::tie(ResultIt, WasInserted) =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
687 InstructionIntegerMap.insert(std::make_pair(&MI, LegalInstrNumber));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
688 unsigned MINumber = ResultIt->second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
689
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
690 // There was an insertion.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
691 if (WasInserted) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
692 LegalInstrNumber++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
693 IntegerInstructionMap.insert(std::make_pair(MINumber, &MI));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
694 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
695
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
696 UnsignedVec.push_back(MINumber);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
697
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
698 // Make sure we don't overflow or use any integers reserved by the DenseMap.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
699 if (LegalInstrNumber >= IllegalInstrNumber)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
700 report_fatal_error("Instruction mapping overflow!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
701
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
702 assert(LegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
703 "Tried to assign DenseMap tombstone or empty key to instruction.");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
704 assert(LegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
705 "Tried to assign DenseMap tombstone or empty key to instruction.");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
706
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
707 return MINumber;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
708 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
709
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
710 /// Maps \p *It to an illegal integer.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
711 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
712 /// Updates \p InstrList, \p UnsignedVec, and \p IllegalInstrNumber.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
713 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
714 /// \returns The integer that \p *It was mapped to.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
715 unsigned mapToIllegalUnsigned(MachineBasicBlock::iterator &It) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
716 unsigned MINumber = IllegalInstrNumber;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
717
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
718 InstrList.push_back(It);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
719 UnsignedVec.push_back(IllegalInstrNumber);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
720 IllegalInstrNumber--;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
721
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
722 assert(LegalInstrNumber < IllegalInstrNumber &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
723 "Instruction mapping overflow!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
724
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
725 assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
726 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
727
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
728 assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
729 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
730
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
731 return MINumber;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
732 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
733
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
734 /// \brief Transforms a \p MachineBasicBlock into a \p vector of \p unsigneds
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
735 /// and appends it to \p UnsignedVec and \p InstrList.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
736 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
737 /// Two instructions are assigned the same integer if they are identical.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
738 /// If an instruction is deemed unsafe to outline, then it will be assigned an
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
739 /// unique integer. The resulting mapping is placed into a suffix tree and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
740 /// queried for candidates.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
741 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
742 /// \param MBB The \p MachineBasicBlock to be translated into integers.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
743 /// \param TRI \p TargetRegisterInfo for the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
744 /// \param TII \p TargetInstrInfo for the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
745 void convertToUnsignedVec(MachineBasicBlock &MBB,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
746 const TargetRegisterInfo &TRI,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
747 const TargetInstrInfo &TII) {
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
748 unsigned Flags = TII.getMachineOutlinerMBBFlags(MBB);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
749
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
750 for (MachineBasicBlock::iterator It = MBB.begin(), Et = MBB.end(); It != Et;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
751 It++) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
752
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
753 // Keep track of where this instruction is in the module.
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
754 switch (TII.getOutliningType(It, Flags)) {
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
755 case TargetInstrInfo::MachineOutlinerInstrType::Illegal:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
756 mapToIllegalUnsigned(It);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
757 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
758
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
759 case TargetInstrInfo::MachineOutlinerInstrType::Legal:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
760 mapToLegalUnsigned(It);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
761 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
762
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
763 case TargetInstrInfo::MachineOutlinerInstrType::Invisible:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
764 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
765 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
766 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
767
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
768 // After we're done every insertion, uniquely terminate this part of the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
769 // "string". This makes sure we won't match across basic block or function
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
770 // boundaries since the "end" is encoded uniquely and thus appears in no
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
771 // repeated substring.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
772 InstrList.push_back(MBB.end());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
773 UnsignedVec.push_back(IllegalInstrNumber);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
774 IllegalInstrNumber--;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
775 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
776
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
777 InstructionMapper() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
778 // Make sure that the implementation of DenseMapInfo<unsigned> hasn't
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
779 // changed.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
780 assert(DenseMapInfo<unsigned>::getEmptyKey() == (unsigned)-1 &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
781 "DenseMapInfo<unsigned>'s empty key isn't -1!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
782 assert(DenseMapInfo<unsigned>::getTombstoneKey() == (unsigned)-2 &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
783 "DenseMapInfo<unsigned>'s tombstone key isn't -2!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
784 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
785 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
786
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
787 /// \brief An interprocedural pass which finds repeated sequences of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
788 /// instructions and replaces them with calls to functions.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
789 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
790 /// Each instruction is mapped to an unsigned integer and placed in a string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
791 /// The resulting mapping is then placed in a \p SuffixTree. The \p SuffixTree
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
792 /// is then repeatedly queried for repeated sequences of instructions. Each
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
793 /// non-overlapping repeated sequence is then placed in its own
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
794 /// \p MachineFunction and each instance is then replaced with a call to that
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
795 /// function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
796 struct MachineOutliner : public ModulePass {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
797
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
798 static char ID;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
799
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
800 /// \brief Set to true if the outliner should consider functions with
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
801 /// linkonceodr linkage.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
802 bool OutlineFromLinkOnceODRs = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
803
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
804 // Collection of IR functions created by the outliner.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
805 std::vector<Function *> CreatedIRFunctions;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
806
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
807 StringRef getPassName() const override { return "Machine Outliner"; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
808
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
809 void getAnalysisUsage(AnalysisUsage &AU) const override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
810 AU.addRequired<MachineModuleInfo>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
811 AU.addPreserved<MachineModuleInfo>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
812 AU.setPreservesAll();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
813 ModulePass::getAnalysisUsage(AU);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
814 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
815
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
816 MachineOutliner(bool OutlineFromLinkOnceODRs = false)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
817 : ModulePass(ID), OutlineFromLinkOnceODRs(OutlineFromLinkOnceODRs) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
818 initializeMachineOutlinerPass(*PassRegistry::getPassRegistry());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
819 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
820
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
821 /// Find all repeated substrings that satisfy the outlining cost model.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
822 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
823 /// If a substring appears at least twice, then it must be represented by
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
824 /// an internal node which appears in at least two suffixes. Each suffix is
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
825 /// represented by a leaf node. To do this, we visit each internal node in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
826 /// the tree, using the leaf children of each internal node. If an internal
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
827 /// node represents a beneficial substring, then we use each of its leaf
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
828 /// children to find the locations of its substring.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
829 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
830 /// \param ST A suffix tree to query.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
831 /// \param TII TargetInstrInfo for the target.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
832 /// \param Mapper Contains outlining mapping information.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
833 /// \param[out] CandidateList Filled with candidates representing each
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
834 /// beneficial substring.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
835 /// \param[out] FunctionList Filled with a list of \p OutlinedFunctions each
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
836 /// type of candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
837 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
838 /// \returns The length of the longest candidate found.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
839 unsigned
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
840 findCandidates(SuffixTree &ST, const TargetInstrInfo &TII,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
841 InstructionMapper &Mapper,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
842 std::vector<std::shared_ptr<Candidate>> &CandidateList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
843 std::vector<OutlinedFunction> &FunctionList);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
844
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
845 /// \brief Replace the sequences of instructions represented by the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
846 /// \p Candidates in \p CandidateList with calls to \p MachineFunctions
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
847 /// described in \p FunctionList.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
848 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
849 /// \param M The module we are outlining from.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
850 /// \param CandidateList A list of candidates to be outlined.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
851 /// \param FunctionList A list of functions to be inserted into the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
852 /// \param Mapper Contains the instruction mappings for the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
853 bool outline(Module &M,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
854 const ArrayRef<std::shared_ptr<Candidate>> &CandidateList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
855 std::vector<OutlinedFunction> &FunctionList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
856 InstructionMapper &Mapper);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
857
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
858 /// Creates a function for \p OF and inserts it into the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
859 MachineFunction *createOutlinedFunction(Module &M, const OutlinedFunction &OF,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
860 InstructionMapper &Mapper);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
861
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
862 /// Find potential outlining candidates and store them in \p CandidateList.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
863 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
864 /// For each type of potential candidate, also build an \p OutlinedFunction
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
865 /// struct containing the information to build the function for that
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
866 /// candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
867 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
868 /// \param[out] CandidateList Filled with outlining candidates for the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
869 /// \param[out] FunctionList Filled with functions corresponding to each type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
870 /// of \p Candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
871 /// \param ST The suffix tree for the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
872 /// \param TII TargetInstrInfo for the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
873 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
874 /// \returns The length of the longest candidate found. 0 if there are none.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
875 unsigned
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
876 buildCandidateList(std::vector<std::shared_ptr<Candidate>> &CandidateList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
877 std::vector<OutlinedFunction> &FunctionList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
878 SuffixTree &ST, InstructionMapper &Mapper,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
879 const TargetInstrInfo &TII);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
880
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
881 /// Helper function for pruneOverlaps.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
882 /// Removes \p C from the candidate list, and updates its \p OutlinedFunction.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
883 void prune(Candidate &C, std::vector<OutlinedFunction> &FunctionList);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
884
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
885 /// \brief Remove any overlapping candidates that weren't handled by the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
886 /// suffix tree's pruning method.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
887 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
888 /// Pruning from the suffix tree doesn't necessarily remove all overlaps.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
889 /// If a short candidate is chosen for outlining, then a longer candidate
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
890 /// which has that short candidate as a suffix is chosen, the tree's pruning
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
891 /// method will not find it. Thus, we need to prune before outlining as well.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
892 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
893 /// \param[in,out] CandidateList A list of outlining candidates.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
894 /// \param[in,out] FunctionList A list of functions to be outlined.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
895 /// \param Mapper Contains instruction mapping info for outlining.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
896 /// \param MaxCandidateLen The length of the longest candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
897 /// \param TII TargetInstrInfo for the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
898 void pruneOverlaps(std::vector<std::shared_ptr<Candidate>> &CandidateList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
899 std::vector<OutlinedFunction> &FunctionList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
900 InstructionMapper &Mapper, unsigned MaxCandidateLen,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
901 const TargetInstrInfo &TII);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
902
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
903 /// Construct a suffix tree on the instructions in \p M and outline repeated
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
904 /// strings from that tree.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
905 bool runOnModule(Module &M) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
906 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
907
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
908 } // Anonymous namespace.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
909
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
910 char MachineOutliner::ID = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
911
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
912 namespace llvm {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
913 ModulePass *createMachineOutlinerPass(bool OutlineFromLinkOnceODRs) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
914 return new MachineOutliner(OutlineFromLinkOnceODRs);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
915 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
916
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
917 } // namespace llvm
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
918
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
919 INITIALIZE_PASS(MachineOutliner, DEBUG_TYPE, "Machine Function Outliner", false,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
920 false)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
921
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
922 unsigned MachineOutliner::findCandidates(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
923 SuffixTree &ST, const TargetInstrInfo &TII, InstructionMapper &Mapper,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
924 std::vector<std::shared_ptr<Candidate>> &CandidateList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
925 std::vector<OutlinedFunction> &FunctionList) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
926 CandidateList.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
927 FunctionList.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
928 unsigned MaxLen = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
929
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
930 // FIXME: Visit internal nodes instead of leaves.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
931 for (SuffixTreeNode *Leaf : ST.LeafVector) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
932 assert(Leaf && "Leaves in LeafVector cannot be null!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
933 if (!Leaf->IsInTree)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
934 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
935
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
936 assert(Leaf->Parent && "All leaves must have parents!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
937 SuffixTreeNode &Parent = *(Leaf->Parent);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
938
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
939 // If it doesn't appear enough, or we already outlined from it, skip it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
940 if (Parent.OccurrenceCount < 2 || Parent.isRoot() || !Parent.IsInTree)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
941 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
942
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
943 // Figure out if this candidate is beneficial.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
944 unsigned StringLen = Leaf->ConcatLen - (unsigned)Leaf->size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
945
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
946 // Too short to be beneficial; skip it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
947 // FIXME: This isn't necessarily true for, say, X86. If we factor in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
948 // instruction lengths we need more information than this.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
949 if (StringLen < 2)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
950 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
951
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
952 // If this is a beneficial class of candidate, then every one is stored in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
953 // this vector.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
954 std::vector<Candidate> CandidatesForRepeatedSeq;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
955
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
956 // Describes the start and end point of each candidate. This allows the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
957 // target to infer some information about each occurrence of each repeated
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
958 // sequence.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
959 // FIXME: CandidatesForRepeatedSeq and this should be combined.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
960 std::vector<
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
961 std::pair<MachineBasicBlock::iterator, MachineBasicBlock::iterator>>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
962 RepeatedSequenceLocs;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
963
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
964 // Figure out the call overhead for each instance of the sequence.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
965 for (auto &ChildPair : Parent.Children) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
966 SuffixTreeNode *M = ChildPair.second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
967
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
968 if (M && M->IsInTree && M->isLeaf()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
969 // Never visit this leaf again.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
970 M->IsInTree = false;
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
971 unsigned StartIdx = M->SuffixIdx;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
972 unsigned EndIdx = StartIdx + StringLen - 1;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
973
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
974 // Trick: Discard some candidates that would be incompatible with the
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
975 // ones we've already found for this sequence. This will save us some
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
976 // work in candidate selection.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
977 //
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
978 // If two candidates overlap, then we can't outline them both. This
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
979 // happens when we have candidates that look like, say
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
980 //
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
981 // AA (where each "A" is an instruction).
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
982 //
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
983 // We might have some portion of the module that looks like this:
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
984 // AAAAAA (6 A's)
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
985 //
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
986 // In this case, there are 5 different copies of "AA" in this range, but
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
987 // at most 3 can be outlined. If only outlining 3 of these is going to
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
988 // be unbeneficial, then we ought to not bother.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
989 //
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
990 // Note that two things DON'T overlap when they look like this:
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
991 // start1...end1 .... start2...end2
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
992 // That is, one must either
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
993 // * End before the other starts
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
994 // * Start after the other ends
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
995 if (std::all_of(CandidatesForRepeatedSeq.begin(),
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
996 CandidatesForRepeatedSeq.end(),
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
997 [&StartIdx, &EndIdx](const Candidate &C) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
998 return (EndIdx < C.getStartIdx() ||
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
999 StartIdx > C.getEndIdx());
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1000 })) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1001 // It doesn't overlap with anything, so we can outline it.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1002 // Each sequence is over [StartIt, EndIt].
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1003 MachineBasicBlock::iterator StartIt = Mapper.InstrList[StartIdx];
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1004 MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx];
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1005
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1006 // Save the MachineFunction containing the Candidate.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1007 MachineFunction *MF = StartIt->getParent()->getParent();
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1008 assert(MF && "Candidate doesn't have a MF?");
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1009
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1010 // Save the candidate and its location.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1011 CandidatesForRepeatedSeq.emplace_back(StartIdx, StringLen,
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1012 FunctionList.size(), MF);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1013 RepeatedSequenceLocs.emplace_back(std::make_pair(StartIt, EndIt));
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1014 }
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1015 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1016 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1017
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1018 // We've found something we might want to outline.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1019 // Create an OutlinedFunction to store it and check if it'd be beneficial
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1020 // to outline.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1021 TargetInstrInfo::MachineOutlinerInfo MInfo =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1022 TII.getOutlininingCandidateInfo(RepeatedSequenceLocs);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1023 std::vector<unsigned> Seq;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1024 for (unsigned i = Leaf->SuffixIdx; i < Leaf->SuffixIdx + StringLen; i++)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1025 Seq.push_back(ST.Str[i]);
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1026 OutlinedFunction OF(FunctionList.size(), CandidatesForRepeatedSeq.size(),
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1027 Seq, MInfo);
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1028 unsigned Benefit = OF.getBenefit();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1029
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1030 // Is it better to outline this candidate than not?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1031 if (Benefit < 1) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1032 // Outlining this candidate would take more instructions than not
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1033 // outlining.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1034 // Emit a remark explaining why we didn't outline this candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1035 std::pair<MachineBasicBlock::iterator, MachineBasicBlock::iterator> C =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1036 RepeatedSequenceLocs[0];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1037 MachineOptimizationRemarkEmitter MORE(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1038 *(C.first->getParent()->getParent()), nullptr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1039 MORE.emit([&]() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1040 MachineOptimizationRemarkMissed R(DEBUG_TYPE, "NotOutliningCheaper",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1041 C.first->getDebugLoc(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1042 C.first->getParent());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1043 R << "Did not outline " << NV("Length", StringLen) << " instructions"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1044 << " from " << NV("NumOccurrences", RepeatedSequenceLocs.size())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1045 << " locations."
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1046 << " Instructions from outlining all occurrences ("
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1047 << NV("OutliningCost", OF.getOutliningCost()) << ")"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1048 << " >= Unoutlined instruction count ("
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1049 << NV("NotOutliningCost", StringLen * OF.getOccurrenceCount()) << ")"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1050 << " (Also found at: ";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1051
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1052 // Tell the user the other places the candidate was found.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1053 for (unsigned i = 1, e = RepeatedSequenceLocs.size(); i < e; i++) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1054 R << NV((Twine("OtherStartLoc") + Twine(i)).str(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1055 RepeatedSequenceLocs[i].first->getDebugLoc());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1056 if (i != e - 1)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1057 R << ", ";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1058 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1059
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1060 R << ")";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1061 return R;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1062 });
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1063
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1064 // Move to the next candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1065 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1066 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1067
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1068 if (StringLen > MaxLen)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1069 MaxLen = StringLen;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1070
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1071 // At this point, the candidate class is seen as beneficial. Set their
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1072 // benefit values and save them in the candidate list.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1073 std::vector<std::shared_ptr<Candidate>> CandidatesForFn;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1074 for (Candidate &C : CandidatesForRepeatedSeq) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1075 C.Benefit = Benefit;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1076 C.MInfo = MInfo;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1077 std::shared_ptr<Candidate> Cptr = std::make_shared<Candidate>(C);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1078 CandidateList.push_back(Cptr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1079 CandidatesForFn.push_back(Cptr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1080 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1081
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1082 FunctionList.push_back(OF);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1083 FunctionList.back().Candidates = CandidatesForFn;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1084
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1085 // Move to the next function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1086 Parent.IsInTree = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1087 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1088
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1089 return MaxLen;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1090 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1091
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1092 // Remove C from the candidate space, and update its OutlinedFunction.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1093 void MachineOutliner::prune(Candidate &C,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1094 std::vector<OutlinedFunction> &FunctionList) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1095 // Get the OutlinedFunction associated with this Candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1096 OutlinedFunction &F = FunctionList[C.FunctionIdx];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1097
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1098 // Update C's associated function's occurrence count.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1099 F.decrement();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1100
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1101 // Remove C from the CandidateList.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1102 C.InCandidateList = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1103
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1104 DEBUG(dbgs() << "- Removed a Candidate \n";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1105 dbgs() << "--- Num fns left for candidate: " << F.getOccurrenceCount()
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1106 << "\n";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1107 dbgs() << "--- Candidate's functions's benefit: " << F.getBenefit()
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1108 << "\n";);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1109 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1110
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1111 void MachineOutliner::pruneOverlaps(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1112 std::vector<std::shared_ptr<Candidate>> &CandidateList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1113 std::vector<OutlinedFunction> &FunctionList, InstructionMapper &Mapper,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1114 unsigned MaxCandidateLen, const TargetInstrInfo &TII) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1115
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1116 // Return true if this candidate became unbeneficial for outlining in a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1117 // previous step.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1118 auto ShouldSkipCandidate = [&FunctionList, this](Candidate &C) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1119
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1120 // Check if the candidate was removed in a previous step.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1121 if (!C.InCandidateList)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1122 return true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1123
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1124 // C must be alive. Check if we should remove it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1125 if (FunctionList[C.FunctionIdx].getBenefit() < 1) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1126 prune(C, FunctionList);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1127 return true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1128 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1129
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1130 // C is in the list, and F is still beneficial.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1131 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1132 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1133
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1134 // TODO: Experiment with interval trees or other interval-checking structures
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1135 // to lower the time complexity of this function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1136 // TODO: Can we do better than the simple greedy choice?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1137 // Check for overlaps in the range.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1138 // This is O(MaxCandidateLen * CandidateList.size()).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1139 for (auto It = CandidateList.begin(), Et = CandidateList.end(); It != Et;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1140 It++) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1141 Candidate &C1 = **It;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1142
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1143 // If C1 was already pruned, or its function is no longer beneficial for
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1144 // outlining, move to the next candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1145 if (ShouldSkipCandidate(C1))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1146 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1147
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1148 // The minimum start index of any candidate that could overlap with this
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1149 // one.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1150 unsigned FarthestPossibleIdx = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1151
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1152 // Either the index is 0, or it's at most MaxCandidateLen indices away.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1153 if (C1.getStartIdx() > MaxCandidateLen)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1154 FarthestPossibleIdx = C1.getStartIdx() - MaxCandidateLen;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1155
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1156 // Compare against the candidates in the list that start at most
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1157 // FarthestPossibleIdx indices away from C1. There are at most
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1158 // MaxCandidateLen of these.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1159 for (auto Sit = It + 1; Sit != Et; Sit++) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1160 Candidate &C2 = **Sit;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1161
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1162 // Is this candidate too far away to overlap?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1163 if (C2.getStartIdx() < FarthestPossibleIdx)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1164 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1165
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1166 // If C2 was already pruned, or its function is no longer beneficial for
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1167 // outlining, move to the next candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1168 if (ShouldSkipCandidate(C2))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1169 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1170
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1171 // Do C1 and C2 overlap?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1172 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1173 // Not overlapping:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1174 // High indices... [C1End ... C1Start][C2End ... C2Start] ...Low indices
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1175 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1176 // We sorted our candidate list so C2Start <= C1Start. We know that
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1177 // C2End > C2Start since each candidate has length >= 2. Therefore, all we
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1178 // have to check is C2End < C2Start to see if we overlap.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1179 if (C2.getEndIdx() < C1.getStartIdx())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1180 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1181
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1182 // C1 and C2 overlap.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1183 // We need to choose the better of the two.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1184 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1185 // Approximate this by picking the one which would have saved us the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1186 // most instructions before any pruning.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1187
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1188 // Is C2 a better candidate?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1189 if (C2.Benefit > C1.Benefit) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1190 // Yes, so prune C1. Since C1 is dead, we don't have to compare it
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1191 // against anything anymore, so break.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1192 prune(C1, FunctionList);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1193 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1194 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1195
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1196 // Prune C2 and move on to the next candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1197 prune(C2, FunctionList);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1198 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1199 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1200 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1201
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1202 unsigned MachineOutliner::buildCandidateList(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1203 std::vector<std::shared_ptr<Candidate>> &CandidateList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1204 std::vector<OutlinedFunction> &FunctionList, SuffixTree &ST,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1205 InstructionMapper &Mapper, const TargetInstrInfo &TII) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1206
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1207 std::vector<unsigned> CandidateSequence; // Current outlining candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1208 unsigned MaxCandidateLen = 0; // Length of the longest candidate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1209
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1210 MaxCandidateLen =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1211 findCandidates(ST, TII, Mapper, CandidateList, FunctionList);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1212
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1213 // Sort the candidates in decending order. This will simplify the outlining
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1214 // process when we have to remove the candidates from the mapping by
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1215 // allowing us to cut them out without keeping track of an offset.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1216 std::stable_sort(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1217 CandidateList.begin(), CandidateList.end(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1218 [](const std::shared_ptr<Candidate> &LHS,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1219 const std::shared_ptr<Candidate> &RHS) { return *LHS < *RHS; });
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1220
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1221 return MaxCandidateLen;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1222 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1223
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1224 MachineFunction *
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1225 MachineOutliner::createOutlinedFunction(Module &M, const OutlinedFunction &OF,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1226 InstructionMapper &Mapper) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1227
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1228 // Create the function name. This should be unique. For now, just hash the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1229 // module name and include it in the function name plus the number of this
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1230 // function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1231 std::ostringstream NameStream;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1232 NameStream << "OUTLINED_FUNCTION_" << OF.Name;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1233
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1234 // Create the function using an IR-level function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1235 LLVMContext &C = M.getContext();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1236 Function *F = dyn_cast<Function>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1237 M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C)));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1238 assert(F && "Function was null!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1239
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1240 // NOTE: If this is linkonceodr, then we can take advantage of linker deduping
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1241 // which gives us better results when we outline from linkonceodr functions.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1242 F->setLinkage(GlobalValue::PrivateLinkage);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1243 F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1244
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1245 // Save F so that we can add debug info later if we need to.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1246 CreatedIRFunctions.push_back(F);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1247
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1248 BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1249 IRBuilder<> Builder(EntryBB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1250 Builder.CreateRetVoid();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1251
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1252 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1253 MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1254 MachineBasicBlock &MBB = *MF.CreateMachineBasicBlock();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1255 const TargetSubtargetInfo &STI = MF.getSubtarget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1256 const TargetInstrInfo &TII = *STI.getInstrInfo();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1257
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1258 // Insert the new function into the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1259 MF.insert(MF.begin(), &MBB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1260
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1261 TII.insertOutlinerPrologue(MBB, MF, OF.MInfo);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1262
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1263 // Copy over the instructions for the function using the integer mappings in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1264 // its sequence.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1265 for (unsigned Str : OF.Sequence) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1266 MachineInstr *NewMI =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1267 MF.CloneMachineInstr(Mapper.IntegerInstructionMap.find(Str)->second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1268 NewMI->dropMemRefs();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1269
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1270 // Don't keep debug information for outlined instructions.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1271 NewMI->setDebugLoc(DebugLoc());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1272 MBB.insert(MBB.end(), NewMI);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1273 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1274
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1275 TII.insertOutlinerEpilogue(MBB, MF, OF.MInfo);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1276
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1277 // If there's a DISubprogram associated with this outlined function, then
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1278 // emit debug info for the outlined function.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1279 if (DISubprogram *SP = OF.getSubprogramOrNull()) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1280 // We have a DISubprogram. Get its DICompileUnit.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1281 DICompileUnit *CU = SP->getUnit();
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1282 DIBuilder DB(M, true, CU);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1283 DIFile *Unit = SP->getFile();
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1284 Mangler Mg;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1285
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1286 // Walk over each IR function we created in the outliner and create
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1287 // DISubprograms for each function.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1288 for (Function *F : CreatedIRFunctions) {
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1289 // Get the mangled name of the function for the linkage name.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1290 std::string Dummy;
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1291 llvm::raw_string_ostream MangledNameStream(Dummy);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1292 Mg.getNameWithPrefix(MangledNameStream, F, false);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1293
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1294 DISubprogram *SP = DB.createFunction(
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1295 Unit /* Context */, F->getName(), StringRef(MangledNameStream.str()),
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1296 Unit /* File */,
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1297 0 /* Line 0 is reserved for compiler-generated code. */,
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1298 DB.createSubroutineType(
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1299 DB.getOrCreateTypeArray(None)), /* void type */
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1300 false, true, 0, /* Line 0 is reserved for compiler-generated code. */
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1301 DINode::DIFlags::FlagArtificial /* Compiler-generated code. */,
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1302 true /* Outlined code is optimized code by definition. */);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1303
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1304 // Don't add any new variables to the subprogram.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1305 DB.finalizeSubprogram(SP);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1306
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1307 // Attach subprogram to the function.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1308 F->setSubprogram(SP);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1309 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1310
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1311 // We're done with the DIBuilder.
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1312 DB.finalize();
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1313 }
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1314
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1315 MF.getRegInfo().freezeReservedRegs(MF);
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1316 return &MF;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1317 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1318
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1319 bool MachineOutliner::outline(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1320 Module &M, const ArrayRef<std::shared_ptr<Candidate>> &CandidateList,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1321 std::vector<OutlinedFunction> &FunctionList, InstructionMapper &Mapper) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1322
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1323 bool OutlinedSomething = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1324 // Replace the candidates with calls to their respective outlined functions.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1325 for (const std::shared_ptr<Candidate> &Cptr : CandidateList) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1326 Candidate &C = *Cptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1327 // Was the candidate removed during pruneOverlaps?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1328 if (!C.InCandidateList)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1329 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1330
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1331 // If not, then look at its OutlinedFunction.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1332 OutlinedFunction &OF = FunctionList[C.FunctionIdx];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1333
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1334 // Was its OutlinedFunction made unbeneficial during pruneOverlaps?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1335 if (OF.getBenefit() < 1)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1336 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1337
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1338 // If not, then outline it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1339 assert(C.getStartIdx() < Mapper.InstrList.size() &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1340 "Candidate out of bounds!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1341 MachineBasicBlock *MBB = (*Mapper.InstrList[C.getStartIdx()]).getParent();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1342 MachineBasicBlock::iterator StartIt = Mapper.InstrList[C.getStartIdx()];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1343 unsigned EndIdx = C.getEndIdx();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1344
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1345 assert(EndIdx < Mapper.InstrList.size() && "Candidate out of bounds!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1346 MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1347 assert(EndIt != MBB->end() && "EndIt out of bounds!");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1348
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1349 EndIt++; // Erase needs one past the end index.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1350
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1351 // Does this candidate have a function yet?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1352 if (!OF.MF) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1353 OF.MF = createOutlinedFunction(M, OF, Mapper);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1354 MachineBasicBlock *MBB = &*OF.MF->begin();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1355
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1356 // Output a remark telling the user that an outlined function was created,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1357 // and explaining where it came from.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1358 MachineOptimizationRemarkEmitter MORE(*OF.MF, nullptr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1359 MachineOptimizationRemark R(DEBUG_TYPE, "OutlinedFunction",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1360 MBB->findDebugLoc(MBB->begin()), MBB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1361 R << "Saved " << NV("OutliningBenefit", OF.getBenefit())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1362 << " instructions by "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1363 << "outlining " << NV("Length", OF.Sequence.size()) << " instructions "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1364 << "from " << NV("NumOccurrences", OF.getOccurrenceCount())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1365 << " locations. "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1366 << "(Found at: ";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1367
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1368 // Tell the user the other places the candidate was found.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1369 for (size_t i = 0, e = OF.Candidates.size(); i < e; i++) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1370
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1371 // Skip over things that were pruned.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1372 if (!OF.Candidates[i]->InCandidateList)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1373 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1374
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1375 R << NV(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1376 (Twine("StartLoc") + Twine(i)).str(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1377 Mapper.InstrList[OF.Candidates[i]->getStartIdx()]->getDebugLoc());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1378 if (i != e - 1)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1379 R << ", ";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1380 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1381
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1382 R << ")";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1383
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1384 MORE.emit(R);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1385 FunctionsCreated++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1386 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1387
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1388 MachineFunction *MF = OF.MF;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1389 const TargetSubtargetInfo &STI = MF->getSubtarget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1390 const TargetInstrInfo &TII = *STI.getInstrInfo();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1391
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1392 // Insert a call to the new function and erase the old sequence.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1393 TII.insertOutlinedCall(M, *MBB, StartIt, *MF, C.MInfo);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1394 StartIt = Mapper.InstrList[C.getStartIdx()];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1395 MBB->erase(StartIt, EndIt);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1396
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1397 OutlinedSomething = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1398
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1399 // Statistics.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1400 NumOutlined++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1401 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1402
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1403 DEBUG(dbgs() << "OutlinedSomething = " << OutlinedSomething << "\n";);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1404
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1405 return OutlinedSomething;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1406 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1407
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1408 bool MachineOutliner::runOnModule(Module &M) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1409
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1410 // Is there anything in the module at all?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1411 if (M.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1412 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1413
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1414 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1415 const TargetSubtargetInfo &STI =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1416 MMI.getOrCreateMachineFunction(*M.begin()).getSubtarget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1417 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1418 const TargetInstrInfo *TII = STI.getInstrInfo();
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1419
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1420 InstructionMapper Mapper;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1421
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1422 // Build instruction mappings for each function in the module.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1423 for (Function &F : M) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1424 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1425
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1426 // Is the function empty? Safe to outline from?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1427 if (F.empty() ||
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1428 !TII->isFunctionSafeToOutlineFrom(MF, OutlineFromLinkOnceODRs))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1429 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1430
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1431 // If it is, look at each MachineBasicBlock in the function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1432 for (MachineBasicBlock &MBB : MF) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1433
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1434 // Is there anything in MBB? And is it the target of an indirect branch?
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1435 if (MBB.empty() || MBB.hasAddressTaken())
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1436 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1437
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1438 // If yes, map it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1439 Mapper.convertToUnsignedVec(MBB, *TRI, *TII);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1440 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1441 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1442
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1443 // Construct a suffix tree, use it to find candidates, and then outline them.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1444 SuffixTree ST(Mapper.UnsignedVec);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1445 std::vector<std::shared_ptr<Candidate>> CandidateList;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1446 std::vector<OutlinedFunction> FunctionList;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1447
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1448 // Find all of the outlining candidates.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1449 unsigned MaxCandidateLen =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1450 buildCandidateList(CandidateList, FunctionList, ST, Mapper, *TII);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1451
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1452 // Remove candidates that overlap with other candidates.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1453 pruneOverlaps(CandidateList, FunctionList, Mapper, MaxCandidateLen, *TII);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1454
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1455 // Outline each of the candidates and return true if something was outlined.
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1456 bool OutlinedSomething = outline(M, CandidateList, FunctionList, Mapper);
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1457
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
1458 return OutlinedSomething;
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1459 }