blob: 984224f12a215b545704580652466a54fdc48e31 [file] [log] [blame]
Jessica Paquette596f4832017-03-06 21:31:18 +00001//===---- MachineOutliner.cpp - Outline instructions -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// Replaces repeated sequences of instructions with function calls.
12///
13/// This works by placing every instruction from every basic block in a
14/// suffix tree, and repeatedly querying that tree for repeated sequences of
15/// instructions. If a sequence of instructions appears often, then it ought
16/// to be beneficial to pull out into a function.
17///
Jessica Paquette4cf187b2017-09-27 20:47:39 +000018/// The MachineOutliner communicates with a given target using hooks defined in
19/// TargetInstrInfo.h. The target supplies the outliner with information on how
20/// a specific sequence of instructions should be outlined. This information
21/// is used to deduce the number of instructions necessary to
22///
23/// * Create an outlined function
24/// * Call that outlined function
25///
26/// Targets must implement
27/// * getOutliningCandidateInfo
28/// * insertOutlinerEpilogue
29/// * insertOutlinedCall
30/// * insertOutlinerPrologue
31/// * isFunctionSafeToOutlineFrom
32///
33/// in order to make use of the MachineOutliner.
34///
Jessica Paquette596f4832017-03-06 21:31:18 +000035/// This was originally presented at the 2016 LLVM Developers' Meeting in the
36/// talk "Reducing Code Size Using Outlining". For a high-level overview of
37/// how this pass works, the talk is available on YouTube at
38///
39/// https://www.youtube.com/watch?v=yorld-WSOeU
40///
41/// The slides for the talk are available at
42///
43/// http://www.llvm.org/devmtg/2016-11/Slides/Paquette-Outliner.pdf
44///
45/// The talk provides an overview of how the outliner finds candidates and
46/// ultimately outlines them. It describes how the main data structure for this
47/// pass, the suffix tree, is queried and purged for candidates. It also gives
48/// a simplified suffix tree construction algorithm for suffix trees based off
49/// of the algorithm actually used here, Ukkonen's algorithm.
50///
51/// For the original RFC for this pass, please see
52///
53/// http://lists.llvm.org/pipermail/llvm-dev/2016-August/104170.html
54///
55/// For more information on the suffix tree data structure, please see
56/// https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf
57///
58//===----------------------------------------------------------------------===//
59#include "llvm/ADT/DenseMap.h"
60#include "llvm/ADT/Statistic.h"
61#include "llvm/ADT/Twine.h"
Jessica Paquette596f4832017-03-06 21:31:18 +000062#include "llvm/CodeGen/MachineFunction.h"
Jessica Paquette596f4832017-03-06 21:31:18 +000063#include "llvm/CodeGen/MachineModuleInfo.h"
Jessica Paquetteffe4abc2017-08-31 21:02:45 +000064#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
Jessica Paquette596f4832017-03-06 21:31:18 +000065#include "llvm/CodeGen/Passes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000066#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000067#include "llvm/CodeGen/TargetRegisterInfo.h"
68#include "llvm/CodeGen/TargetSubtargetInfo.h"
Jessica Paquette729e6862018-01-18 00:00:58 +000069#include "llvm/IR/DIBuilder.h"
Jessica Paquette596f4832017-03-06 21:31:18 +000070#include "llvm/IR/IRBuilder.h"
Jessica Paquettea499c3c2018-01-19 21:21:49 +000071#include "llvm/IR/Mangler.h"
Jessica Paquette596f4832017-03-06 21:31:18 +000072#include "llvm/Support/Allocator.h"
73#include "llvm/Support/Debug.h"
74#include "llvm/Support/raw_ostream.h"
Jessica Paquette596f4832017-03-06 21:31:18 +000075#include <functional>
76#include <map>
77#include <sstream>
78#include <tuple>
79#include <vector>
80
81#define DEBUG_TYPE "machine-outliner"
82
83using namespace llvm;
Jessica Paquetteffe4abc2017-08-31 21:02:45 +000084using namespace ore;
Jessica Paquette596f4832017-03-06 21:31:18 +000085
86STATISTIC(NumOutlined, "Number of candidates outlined");
87STATISTIC(FunctionsCreated, "Number of functions created");
88
89namespace {
90
Jessica Paquetteacffa282017-03-23 21:27:38 +000091/// \brief An individual sequence of instructions to be replaced with a call to
92/// an outlined function.
93struct Candidate {
Jessica Paquettec9ab4c22017-10-17 18:43:15 +000094private:
95 /// The start index of this \p Candidate in the instruction list.
Jessica Paquette4cf187b2017-09-27 20:47:39 +000096 unsigned StartIdx;
Jessica Paquetteacffa282017-03-23 21:27:38 +000097
98 /// The number of instructions in this \p Candidate.
Jessica Paquette4cf187b2017-09-27 20:47:39 +000099 unsigned Len;
Jessica Paquetteacffa282017-03-23 21:27:38 +0000100
Jessica Paquettea499c3c2018-01-19 21:21:49 +0000101 /// The MachineFunction containing this \p Candidate.
102 MachineFunction *MF = nullptr;
103
Jessica Paquettec9ab4c22017-10-17 18:43:15 +0000104public:
105 /// Set to false if the candidate overlapped with another candidate.
106 bool InCandidateList = true;
107
108 /// \brief The index of this \p Candidate's \p OutlinedFunction in the list of
Jessica Paquetteacffa282017-03-23 21:27:38 +0000109 /// \p OutlinedFunctions.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000110 unsigned FunctionIdx;
Jessica Paquetteacffa282017-03-23 21:27:38 +0000111
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000112 /// Contains all target-specific information for this \p Candidate.
113 TargetInstrInfo::MachineOutlinerInfo MInfo;
Jessica Paquetted87f5442017-07-29 02:55:46 +0000114
Jessica Paquettea499c3c2018-01-19 21:21:49 +0000115 /// If there is a DISubprogram associated with the function that this
116 /// Candidate lives in, return it.
117 DISubprogram *getSubprogramOrNull() const {
118 assert(MF && "Candidate has no MF!");
119 if (DISubprogram *SP = MF->getFunction().getSubprogram())
120 return SP;
121 return nullptr;
122 }
123
Jessica Paquettec9ab4c22017-10-17 18:43:15 +0000124 /// Return the number of instructions in this Candidate.
Jessica Paquette1934fd22017-10-23 16:25:53 +0000125 unsigned getLength() const { return Len; }
Jessica Paquettec9ab4c22017-10-17 18:43:15 +0000126
127 /// Return the start index of this candidate.
Jessica Paquette1934fd22017-10-23 16:25:53 +0000128 unsigned getStartIdx() const { return StartIdx; }
Jessica Paquettec9ab4c22017-10-17 18:43:15 +0000129
130 // Return the end index of this candidate.
Jessica Paquette1934fd22017-10-23 16:25:53 +0000131 unsigned getEndIdx() const { return StartIdx + Len - 1; }
Jessica Paquettec9ab4c22017-10-17 18:43:15 +0000132
Jessica Paquetteacffa282017-03-23 21:27:38 +0000133 /// \brief The number of instructions that would be saved by outlining every
134 /// candidate of this type.
135 ///
136 /// This is a fixed value which is not updated during the candidate pruning
137 /// process. It is only used for deciding which candidate to keep if two
138 /// candidates overlap. The true benefit is stored in the OutlinedFunction
139 /// for some given candidate.
140 unsigned Benefit = 0;
141
Jessica Paquettea499c3c2018-01-19 21:21:49 +0000142 Candidate(unsigned StartIdx, unsigned Len, unsigned FunctionIdx,
143 MachineFunction *MF)
144 : StartIdx(StartIdx), Len(Len), MF(MF), FunctionIdx(FunctionIdx) {}
Jessica Paquetteacffa282017-03-23 21:27:38 +0000145
146 Candidate() {}
147
148 /// \brief Used to ensure that \p Candidates are outlined in an order that
149 /// preserves the start and end indices of other \p Candidates.
Jessica Paquettec9ab4c22017-10-17 18:43:15 +0000150 bool operator<(const Candidate &RHS) const {
Jessica Paquette1934fd22017-10-23 16:25:53 +0000151 return getStartIdx() > RHS.getStartIdx();
Jessica Paquettec9ab4c22017-10-17 18:43:15 +0000152 }
Jessica Paquetteacffa282017-03-23 21:27:38 +0000153};
154
155/// \brief The information necessary to create an outlined function for some
156/// class of candidate.
157struct OutlinedFunction {
158
Jessica Paquette85af63d2017-10-17 19:03:23 +0000159private:
160 /// The number of candidates for this \p OutlinedFunction.
161 unsigned OccurrenceCount = 0;
162
163public:
Jessica Paquette9df7fde2017-10-23 23:36:46 +0000164 std::vector<std::shared_ptr<Candidate>> Candidates;
165
Jessica Paquetteacffa282017-03-23 21:27:38 +0000166 /// The actual outlined function created.
167 /// This is initialized after we go through and create the actual function.
168 MachineFunction *MF = nullptr;
169
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000170 /// A number assigned to this function which appears at the end of its name.
171 unsigned Name;
Jessica Paquetteacffa282017-03-23 21:27:38 +0000172
Jessica Paquetteacffa282017-03-23 21:27:38 +0000173 /// \brief The sequence of integers corresponding to the instructions in this
174 /// function.
175 std::vector<unsigned> Sequence;
176
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000177 /// Contains all target-specific information for this \p OutlinedFunction.
178 TargetInstrInfo::MachineOutlinerInfo MInfo;
Jessica Paquetteacffa282017-03-23 21:27:38 +0000179
Jessica Paquettea499c3c2018-01-19 21:21:49 +0000180 /// If there is a DISubprogram for any Candidate for this outlined function,
181 /// then return it. Otherwise, return nullptr.
182 DISubprogram *getSubprogramOrNull() const {
183 for (const auto &C : Candidates)
184 if (DISubprogram *SP = C->getSubprogramOrNull())
185 return SP;
186 return nullptr;
187 }
188
Jessica Paquette85af63d2017-10-17 19:03:23 +0000189 /// Return the number of candidates for this \p OutlinedFunction.
Jessica Paquette60d31fc2017-10-17 21:11:58 +0000190 unsigned getOccurrenceCount() { return OccurrenceCount; }
Jessica Paquette85af63d2017-10-17 19:03:23 +0000191
192 /// Decrement the occurrence count of this OutlinedFunction and return the
193 /// new count.
194 unsigned decrement() {
195 assert(OccurrenceCount > 0 && "Can't decrement an empty function!");
196 OccurrenceCount--;
197 return getOccurrenceCount();
198 }
199
Jessica Paquetteacc15e12017-10-03 20:32:55 +0000200 /// \brief Return the number of instructions it would take to outline this
201 /// function.
202 unsigned getOutliningCost() {
203 return (OccurrenceCount * MInfo.CallOverhead) + Sequence.size() +
204 MInfo.FrameOverhead;
205 }
206
207 /// \brief Return the number of instructions that would be saved by outlining
208 /// this function.
209 unsigned getBenefit() {
210 unsigned NotOutlinedCost = OccurrenceCount * Sequence.size();
211 unsigned OutlinedCost = getOutliningCost();
212 return (NotOutlinedCost < OutlinedCost) ? 0
213 : NotOutlinedCost - OutlinedCost;
214 }
215
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000216 OutlinedFunction(unsigned Name, unsigned OccurrenceCount,
Jessica Paquetteacc15e12017-10-03 20:32:55 +0000217 const std::vector<unsigned> &Sequence,
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000218 TargetInstrInfo::MachineOutlinerInfo &MInfo)
Jessica Paquette85af63d2017-10-17 19:03:23 +0000219 : OccurrenceCount(OccurrenceCount), Name(Name), Sequence(Sequence),
Jessica Paquetteacc15e12017-10-03 20:32:55 +0000220 MInfo(MInfo) {}
Jessica Paquetteacffa282017-03-23 21:27:38 +0000221};
222
Jessica Paquette596f4832017-03-06 21:31:18 +0000223/// Represents an undefined index in the suffix tree.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000224const unsigned EmptyIdx = -1;
Jessica Paquette596f4832017-03-06 21:31:18 +0000225
226/// A node in a suffix tree which represents a substring or suffix.
227///
228/// Each node has either no children or at least two children, with the root
229/// being a exception in the empty tree.
230///
231/// Children are represented as a map between unsigned integers and nodes. If
232/// a node N has a child M on unsigned integer k, then the mapping represented
233/// by N is a proper prefix of the mapping represented by M. Note that this,
234/// although similar to a trie is somewhat different: each node stores a full
235/// substring of the full mapping rather than a single character state.
236///
237/// Each internal node contains a pointer to the internal node representing
238/// the same string, but with the first character chopped off. This is stored
239/// in \p Link. Each leaf node stores the start index of its respective
240/// suffix in \p SuffixIdx.
241struct SuffixTreeNode {
242
243 /// The children of this node.
244 ///
245 /// A child existing on an unsigned integer implies that from the mapping
246 /// represented by the current node, there is a way to reach another
247 /// mapping by tacking that character on the end of the current string.
248 DenseMap<unsigned, SuffixTreeNode *> Children;
249
250 /// A flag set to false if the node has been pruned from the tree.
251 bool IsInTree = true;
252
253 /// The start index of this node's substring in the main string.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000254 unsigned StartIdx = EmptyIdx;
Jessica Paquette596f4832017-03-06 21:31:18 +0000255
256 /// The end index of this node's substring in the main string.
257 ///
258 /// Every leaf node must have its \p EndIdx incremented at the end of every
259 /// step in the construction algorithm. To avoid having to update O(N)
260 /// nodes individually at the end of every step, the end index is stored
261 /// as a pointer.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000262 unsigned *EndIdx = nullptr;
Jessica Paquette596f4832017-03-06 21:31:18 +0000263
264 /// For leaves, the start index of the suffix represented by this node.
265 ///
266 /// For all other nodes, this is ignored.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000267 unsigned SuffixIdx = EmptyIdx;
Jessica Paquette596f4832017-03-06 21:31:18 +0000268
269 /// \brief For internal nodes, a pointer to the internal node representing
270 /// the same sequence with the first character chopped off.
271 ///
Jessica Paquette4602c342017-07-28 05:59:30 +0000272 /// This acts as a shortcut in Ukkonen's algorithm. One of the things that
Jessica Paquette596f4832017-03-06 21:31:18 +0000273 /// Ukkonen's algorithm does to achieve linear-time construction is
274 /// keep track of which node the next insert should be at. This makes each
275 /// insert O(1), and there are a total of O(N) inserts. The suffix link
276 /// helps with inserting children of internal nodes.
277 ///
Jessica Paquette78681be2017-07-27 23:24:43 +0000278 /// Say we add a child to an internal node with associated mapping S. The
Jessica Paquette596f4832017-03-06 21:31:18 +0000279 /// next insertion must be at the node representing S - its first character.
280 /// This is given by the way that we iteratively build the tree in Ukkonen's
281 /// algorithm. The main idea is to look at the suffixes of each prefix in the
282 /// string, starting with the longest suffix of the prefix, and ending with
283 /// the shortest. Therefore, if we keep pointers between such nodes, we can
284 /// move to the next insertion point in O(1) time. If we don't, then we'd
285 /// have to query from the root, which takes O(N) time. This would make the
286 /// construction algorithm O(N^2) rather than O(N).
Jessica Paquette596f4832017-03-06 21:31:18 +0000287 SuffixTreeNode *Link = nullptr;
288
289 /// The parent of this node. Every node except for the root has a parent.
290 SuffixTreeNode *Parent = nullptr;
291
292 /// The number of times this node's string appears in the tree.
293 ///
294 /// This is equal to the number of leaf children of the string. It represents
295 /// the number of suffixes that the node's string is a prefix of.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000296 unsigned OccurrenceCount = 0;
Jessica Paquette596f4832017-03-06 21:31:18 +0000297
Jessica Paquetteacffa282017-03-23 21:27:38 +0000298 /// The length of the string formed by concatenating the edge labels from the
299 /// root to this node.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000300 unsigned ConcatLen = 0;
Jessica Paquetteacffa282017-03-23 21:27:38 +0000301
Jessica Paquette596f4832017-03-06 21:31:18 +0000302 /// Returns true if this node is a leaf.
303 bool isLeaf() const { return SuffixIdx != EmptyIdx; }
304
305 /// Returns true if this node is the root of its owning \p SuffixTree.
306 bool isRoot() const { return StartIdx == EmptyIdx; }
307
308 /// Return the number of elements in the substring associated with this node.
309 size_t size() const {
310
311 // Is it the root? If so, it's the empty string so return 0.
312 if (isRoot())
313 return 0;
314
315 assert(*EndIdx != EmptyIdx && "EndIdx is undefined!");
316
317 // Size = the number of elements in the string.
318 // For example, [0 1 2 3] has length 4, not 3. 3-0 = 3, so we have 3-0+1.
319 return *EndIdx - StartIdx + 1;
320 }
321
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000322 SuffixTreeNode(unsigned StartIdx, unsigned *EndIdx, SuffixTreeNode *Link,
Jessica Paquette596f4832017-03-06 21:31:18 +0000323 SuffixTreeNode *Parent)
324 : StartIdx(StartIdx), EndIdx(EndIdx), Link(Link), Parent(Parent) {}
325
326 SuffixTreeNode() {}
327};
328
329/// A data structure for fast substring queries.
330///
331/// Suffix trees represent the suffixes of their input strings in their leaves.
332/// A suffix tree is a type of compressed trie structure where each node
333/// represents an entire substring rather than a single character. Each leaf
334/// of the tree is a suffix.
335///
336/// A suffix tree can be seen as a type of state machine where each state is a
337/// substring of the full string. The tree is structured so that, for a string
338/// of length N, there are exactly N leaves in the tree. This structure allows
339/// us to quickly find repeated substrings of the input string.
340///
341/// In this implementation, a "string" is a vector of unsigned integers.
342/// These integers may result from hashing some data type. A suffix tree can
343/// contain 1 or many strings, which can then be queried as one large string.
344///
345/// The suffix tree is implemented using Ukkonen's algorithm for linear-time
346/// suffix tree construction. Ukkonen's algorithm is explained in more detail
347/// in the paper by Esko Ukkonen "On-line construction of suffix trees. The
348/// paper is available at
349///
350/// https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf
351class SuffixTree {
Jessica Paquette78681be2017-07-27 23:24:43 +0000352public:
353 /// Stores each leaf node in the tree.
354 ///
355 /// This is used for finding outlining candidates.
356 std::vector<SuffixTreeNode *> LeafVector;
357
Jessica Paquette596f4832017-03-06 21:31:18 +0000358 /// Each element is an integer representing an instruction in the module.
359 ArrayRef<unsigned> Str;
360
Jessica Paquette78681be2017-07-27 23:24:43 +0000361private:
Jessica Paquette596f4832017-03-06 21:31:18 +0000362 /// Maintains each node in the tree.
Jessica Paquetted4cb9c62017-03-08 23:55:33 +0000363 SpecificBumpPtrAllocator<SuffixTreeNode> NodeAllocator;
Jessica Paquette596f4832017-03-06 21:31:18 +0000364
365 /// The root of the suffix tree.
366 ///
367 /// The root represents the empty string. It is maintained by the
368 /// \p NodeAllocator like every other node in the tree.
369 SuffixTreeNode *Root = nullptr;
370
Jessica Paquette596f4832017-03-06 21:31:18 +0000371 /// Maintains the end indices of the internal nodes in the tree.
372 ///
373 /// Each internal node is guaranteed to never have its end index change
374 /// during the construction algorithm; however, leaves must be updated at
375 /// every step. Therefore, we need to store leaf end indices by reference
376 /// to avoid updating O(N) leaves at every step of construction. Thus,
377 /// every internal node must be allocated its own end index.
378 BumpPtrAllocator InternalEndIdxAllocator;
379
380 /// The end index of each leaf in the tree.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000381 unsigned LeafEndIdx = -1;
Jessica Paquette596f4832017-03-06 21:31:18 +0000382
383 /// \brief Helper struct which keeps track of the next insertion point in
384 /// Ukkonen's algorithm.
385 struct ActiveState {
386 /// The next node to insert at.
387 SuffixTreeNode *Node;
388
389 /// The index of the first character in the substring currently being added.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000390 unsigned Idx = EmptyIdx;
Jessica Paquette596f4832017-03-06 21:31:18 +0000391
392 /// The length of the substring we have to add at the current step.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000393 unsigned Len = 0;
Jessica Paquette596f4832017-03-06 21:31:18 +0000394 };
395
396 /// \brief The point the next insertion will take place at in the
397 /// construction algorithm.
398 ActiveState Active;
399
400 /// Allocate a leaf node and add it to the tree.
401 ///
402 /// \param Parent The parent of this node.
403 /// \param StartIdx The start index of this node's associated string.
404 /// \param Edge The label on the edge leaving \p Parent to this node.
405 ///
406 /// \returns A pointer to the allocated leaf node.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000407 SuffixTreeNode *insertLeaf(SuffixTreeNode &Parent, unsigned StartIdx,
Jessica Paquette596f4832017-03-06 21:31:18 +0000408 unsigned Edge) {
409
410 assert(StartIdx <= LeafEndIdx && "String can't start after it ends!");
411
Jessica Paquette78681be2017-07-27 23:24:43 +0000412 SuffixTreeNode *N = new (NodeAllocator.Allocate())
413 SuffixTreeNode(StartIdx, &LeafEndIdx, nullptr, &Parent);
Jessica Paquette596f4832017-03-06 21:31:18 +0000414 Parent.Children[Edge] = N;
415
416 return N;
417 }
418
419 /// Allocate an internal node and add it to the tree.
420 ///
421 /// \param Parent The parent of this node. Only null when allocating the root.
422 /// \param StartIdx The start index of this node's associated string.
423 /// \param EndIdx The end index of this node's associated string.
424 /// \param Edge The label on the edge leaving \p Parent to this node.
425 ///
426 /// \returns A pointer to the allocated internal node.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000427 SuffixTreeNode *insertInternalNode(SuffixTreeNode *Parent, unsigned StartIdx,
428 unsigned EndIdx, unsigned Edge) {
Jessica Paquette596f4832017-03-06 21:31:18 +0000429
430 assert(StartIdx <= EndIdx && "String can't start after it ends!");
431 assert(!(!Parent && StartIdx != EmptyIdx) &&
Jessica Paquette78681be2017-07-27 23:24:43 +0000432 "Non-root internal nodes must have parents!");
Jessica Paquette596f4832017-03-06 21:31:18 +0000433
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000434 unsigned *E = new (InternalEndIdxAllocator) unsigned(EndIdx);
Jessica Paquette78681be2017-07-27 23:24:43 +0000435 SuffixTreeNode *N = new (NodeAllocator.Allocate())
436 SuffixTreeNode(StartIdx, E, Root, Parent);
Jessica Paquette596f4832017-03-06 21:31:18 +0000437 if (Parent)
438 Parent->Children[Edge] = N;
439
440 return N;
441 }
442
443 /// \brief Set the suffix indices of the leaves to the start indices of their
444 /// respective suffixes. Also stores each leaf in \p LeafVector at its
445 /// respective suffix index.
446 ///
447 /// \param[in] CurrNode The node currently being visited.
448 /// \param CurrIdx The current index of the string being visited.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000449 void setSuffixIndices(SuffixTreeNode &CurrNode, unsigned CurrIdx) {
Jessica Paquette596f4832017-03-06 21:31:18 +0000450
451 bool IsLeaf = CurrNode.Children.size() == 0 && !CurrNode.isRoot();
452
Jessica Paquetteacffa282017-03-23 21:27:38 +0000453 // Store the length of the concatenation of all strings from the root to
454 // this node.
455 if (!CurrNode.isRoot()) {
456 if (CurrNode.ConcatLen == 0)
457 CurrNode.ConcatLen = CurrNode.size();
458
459 if (CurrNode.Parent)
Jessica Paquette78681be2017-07-27 23:24:43 +0000460 CurrNode.ConcatLen += CurrNode.Parent->ConcatLen;
Jessica Paquetteacffa282017-03-23 21:27:38 +0000461 }
462
Jessica Paquette596f4832017-03-06 21:31:18 +0000463 // Traverse the tree depth-first.
464 for (auto &ChildPair : CurrNode.Children) {
465 assert(ChildPair.second && "Node had a null child!");
Jessica Paquette78681be2017-07-27 23:24:43 +0000466 setSuffixIndices(*ChildPair.second, CurrIdx + ChildPair.second->size());
Jessica Paquette596f4832017-03-06 21:31:18 +0000467 }
468
469 // Is this node a leaf?
470 if (IsLeaf) {
471 // If yes, give it a suffix index and bump its parent's occurrence count.
472 CurrNode.SuffixIdx = Str.size() - CurrIdx;
473 assert(CurrNode.Parent && "CurrNode had no parent!");
474 CurrNode.Parent->OccurrenceCount++;
475
476 // Store the leaf in the leaf vector for pruning later.
477 LeafVector[CurrNode.SuffixIdx] = &CurrNode;
478 }
479 }
480
481 /// \brief Construct the suffix tree for the prefix of the input ending at
482 /// \p EndIdx.
483 ///
484 /// Used to construct the full suffix tree iteratively. At the end of each
485 /// step, the constructed suffix tree is either a valid suffix tree, or a
486 /// suffix tree with implicit suffixes. At the end of the final step, the
487 /// suffix tree is a valid tree.
488 ///
489 /// \param EndIdx The end index of the current prefix in the main string.
490 /// \param SuffixesToAdd The number of suffixes that must be added
491 /// to complete the suffix tree at the current phase.
492 ///
493 /// \returns The number of suffixes that have not been added at the end of
494 /// this step.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000495 unsigned extend(unsigned EndIdx, unsigned SuffixesToAdd) {
Jessica Paquette596f4832017-03-06 21:31:18 +0000496 SuffixTreeNode *NeedsLink = nullptr;
497
498 while (SuffixesToAdd > 0) {
Jessica Paquette78681be2017-07-27 23:24:43 +0000499
Jessica Paquette596f4832017-03-06 21:31:18 +0000500 // Are we waiting to add anything other than just the last character?
501 if (Active.Len == 0) {
502 // If not, then say the active index is the end index.
503 Active.Idx = EndIdx;
504 }
505
506 assert(Active.Idx <= EndIdx && "Start index can't be after end index!");
507
508 // The first character in the current substring we're looking at.
509 unsigned FirstChar = Str[Active.Idx];
510
511 // Have we inserted anything starting with FirstChar at the current node?
512 if (Active.Node->Children.count(FirstChar) == 0) {
513 // If not, then we can just insert a leaf and move too the next step.
514 insertLeaf(*Active.Node, EndIdx, FirstChar);
515
516 // The active node is an internal node, and we visited it, so it must
517 // need a link if it doesn't have one.
518 if (NeedsLink) {
519 NeedsLink->Link = Active.Node;
520 NeedsLink = nullptr;
521 }
522 } else {
523 // There's a match with FirstChar, so look for the point in the tree to
524 // insert a new node.
525 SuffixTreeNode *NextNode = Active.Node->Children[FirstChar];
526
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000527 unsigned SubstringLen = NextNode->size();
Jessica Paquette596f4832017-03-06 21:31:18 +0000528
529 // Is the current suffix we're trying to insert longer than the size of
530 // the child we want to move to?
531 if (Active.Len >= SubstringLen) {
532 // If yes, then consume the characters we've seen and move to the next
533 // node.
534 Active.Idx += SubstringLen;
535 Active.Len -= SubstringLen;
536 Active.Node = NextNode;
537 continue;
538 }
539
540 // Otherwise, the suffix we're trying to insert must be contained in the
541 // next node we want to move to.
542 unsigned LastChar = Str[EndIdx];
543
544 // Is the string we're trying to insert a substring of the next node?
545 if (Str[NextNode->StartIdx + Active.Len] == LastChar) {
546 // If yes, then we're done for this step. Remember our insertion point
547 // and move to the next end index. At this point, we have an implicit
548 // suffix tree.
549 if (NeedsLink && !Active.Node->isRoot()) {
550 NeedsLink->Link = Active.Node;
551 NeedsLink = nullptr;
552 }
553
554 Active.Len++;
555 break;
556 }
557
558 // The string we're trying to insert isn't a substring of the next node,
559 // but matches up to a point. Split the node.
560 //
561 // For example, say we ended our search at a node n and we're trying to
562 // insert ABD. Then we'll create a new node s for AB, reduce n to just
563 // representing C, and insert a new leaf node l to represent d. This
564 // allows us to ensure that if n was a leaf, it remains a leaf.
565 //
566 // | ABC ---split---> | AB
567 // n s
568 // C / \ D
569 // n l
570
571 // The node s from the diagram
572 SuffixTreeNode *SplitNode =
Jessica Paquette78681be2017-07-27 23:24:43 +0000573 insertInternalNode(Active.Node, NextNode->StartIdx,
574 NextNode->StartIdx + Active.Len - 1, FirstChar);
Jessica Paquette596f4832017-03-06 21:31:18 +0000575
576 // Insert the new node representing the new substring into the tree as
577 // a child of the split node. This is the node l from the diagram.
578 insertLeaf(*SplitNode, EndIdx, LastChar);
579
580 // Make the old node a child of the split node and update its start
581 // index. This is the node n from the diagram.
582 NextNode->StartIdx += Active.Len;
583 NextNode->Parent = SplitNode;
584 SplitNode->Children[Str[NextNode->StartIdx]] = NextNode;
585
586 // SplitNode is an internal node, update the suffix link.
587 if (NeedsLink)
588 NeedsLink->Link = SplitNode;
589
590 NeedsLink = SplitNode;
591 }
592
593 // We've added something new to the tree, so there's one less suffix to
594 // add.
595 SuffixesToAdd--;
596
597 if (Active.Node->isRoot()) {
598 if (Active.Len > 0) {
599 Active.Len--;
600 Active.Idx = EndIdx - SuffixesToAdd + 1;
601 }
602 } else {
603 // Start the next phase at the next smallest suffix.
604 Active.Node = Active.Node->Link;
605 }
606 }
607
608 return SuffixesToAdd;
609 }
610
Jessica Paquette596f4832017-03-06 21:31:18 +0000611public:
Jessica Paquette596f4832017-03-06 21:31:18 +0000612 /// Construct a suffix tree from a sequence of unsigned integers.
613 ///
614 /// \param Str The string to construct the suffix tree for.
615 SuffixTree(const std::vector<unsigned> &Str) : Str(Str) {
616 Root = insertInternalNode(nullptr, EmptyIdx, EmptyIdx, 0);
617 Root->IsInTree = true;
618 Active.Node = Root;
Jessica Paquette78681be2017-07-27 23:24:43 +0000619 LeafVector = std::vector<SuffixTreeNode *>(Str.size());
Jessica Paquette596f4832017-03-06 21:31:18 +0000620
621 // Keep track of the number of suffixes we have to add of the current
622 // prefix.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000623 unsigned SuffixesToAdd = 0;
Jessica Paquette596f4832017-03-06 21:31:18 +0000624 Active.Node = Root;
625
626 // Construct the suffix tree iteratively on each prefix of the string.
627 // PfxEndIdx is the end index of the current prefix.
628 // End is one past the last element in the string.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000629 for (unsigned PfxEndIdx = 0, End = Str.size(); PfxEndIdx < End;
630 PfxEndIdx++) {
Jessica Paquette596f4832017-03-06 21:31:18 +0000631 SuffixesToAdd++;
632 LeafEndIdx = PfxEndIdx; // Extend each of the leaves.
633 SuffixesToAdd = extend(PfxEndIdx, SuffixesToAdd);
634 }
635
636 // Set the suffix indices of each leaf.
637 assert(Root && "Root node can't be nullptr!");
638 setSuffixIndices(*Root, 0);
639 }
640};
641
Jessica Paquette596f4832017-03-06 21:31:18 +0000642/// \brief Maps \p MachineInstrs to unsigned integers and stores the mappings.
643struct InstructionMapper {
644
645 /// \brief The next available integer to assign to a \p MachineInstr that
646 /// cannot be outlined.
647 ///
648 /// Set to -3 for compatability with \p DenseMapInfo<unsigned>.
649 unsigned IllegalInstrNumber = -3;
650
651 /// \brief The next available integer to assign to a \p MachineInstr that can
652 /// be outlined.
653 unsigned LegalInstrNumber = 0;
654
655 /// Correspondence from \p MachineInstrs to unsigned integers.
656 DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>
657 InstructionIntegerMap;
658
659 /// Corresponcence from unsigned integers to \p MachineInstrs.
660 /// Inverse of \p InstructionIntegerMap.
661 DenseMap<unsigned, MachineInstr *> IntegerInstructionMap;
662
663 /// The vector of unsigned integers that the module is mapped to.
664 std::vector<unsigned> UnsignedVec;
665
666 /// \brief Stores the location of the instruction associated with the integer
667 /// at index i in \p UnsignedVec for each index i.
668 std::vector<MachineBasicBlock::iterator> InstrList;
669
670 /// \brief Maps \p *It to a legal integer.
671 ///
672 /// Updates \p InstrList, \p UnsignedVec, \p InstructionIntegerMap,
673 /// \p IntegerInstructionMap, and \p LegalInstrNumber.
674 ///
675 /// \returns The integer that \p *It was mapped to.
676 unsigned mapToLegalUnsigned(MachineBasicBlock::iterator &It) {
677
678 // Get the integer for this instruction or give it the current
679 // LegalInstrNumber.
680 InstrList.push_back(It);
681 MachineInstr &MI = *It;
682 bool WasInserted;
683 DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>::iterator
Jessica Paquette78681be2017-07-27 23:24:43 +0000684 ResultIt;
Jessica Paquette596f4832017-03-06 21:31:18 +0000685 std::tie(ResultIt, WasInserted) =
Jessica Paquette78681be2017-07-27 23:24:43 +0000686 InstructionIntegerMap.insert(std::make_pair(&MI, LegalInstrNumber));
Jessica Paquette596f4832017-03-06 21:31:18 +0000687 unsigned MINumber = ResultIt->second;
688
689 // There was an insertion.
690 if (WasInserted) {
691 LegalInstrNumber++;
692 IntegerInstructionMap.insert(std::make_pair(MINumber, &MI));
693 }
694
695 UnsignedVec.push_back(MINumber);
696
697 // Make sure we don't overflow or use any integers reserved by the DenseMap.
698 if (LegalInstrNumber >= IllegalInstrNumber)
699 report_fatal_error("Instruction mapping overflow!");
700
Jessica Paquette78681be2017-07-27 23:24:43 +0000701 assert(LegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
702 "Tried to assign DenseMap tombstone or empty key to instruction.");
703 assert(LegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
704 "Tried to assign DenseMap tombstone or empty key to instruction.");
Jessica Paquette596f4832017-03-06 21:31:18 +0000705
706 return MINumber;
707 }
708
709 /// Maps \p *It to an illegal integer.
710 ///
711 /// Updates \p InstrList, \p UnsignedVec, and \p IllegalInstrNumber.
712 ///
713 /// \returns The integer that \p *It was mapped to.
714 unsigned mapToIllegalUnsigned(MachineBasicBlock::iterator &It) {
715 unsigned MINumber = IllegalInstrNumber;
716
717 InstrList.push_back(It);
718 UnsignedVec.push_back(IllegalInstrNumber);
719 IllegalInstrNumber--;
720
721 assert(LegalInstrNumber < IllegalInstrNumber &&
722 "Instruction mapping overflow!");
723
Jessica Paquette78681be2017-07-27 23:24:43 +0000724 assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
725 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
Jessica Paquette596f4832017-03-06 21:31:18 +0000726
Jessica Paquette78681be2017-07-27 23:24:43 +0000727 assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
728 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
Jessica Paquette596f4832017-03-06 21:31:18 +0000729
730 return MINumber;
731 }
732
733 /// \brief Transforms a \p MachineBasicBlock into a \p vector of \p unsigneds
734 /// and appends it to \p UnsignedVec and \p InstrList.
735 ///
736 /// Two instructions are assigned the same integer if they are identical.
737 /// If an instruction is deemed unsafe to outline, then it will be assigned an
738 /// unique integer. The resulting mapping is placed into a suffix tree and
739 /// queried for candidates.
740 ///
741 /// \param MBB The \p MachineBasicBlock to be translated into integers.
742 /// \param TRI \p TargetRegisterInfo for the module.
743 /// \param TII \p TargetInstrInfo for the module.
744 void convertToUnsignedVec(MachineBasicBlock &MBB,
745 const TargetRegisterInfo &TRI,
746 const TargetInstrInfo &TII) {
Jessica Paquette3291e732018-01-09 00:26:18 +0000747 unsigned Flags = TII.getMachineOutlinerMBBFlags(MBB);
748
Jessica Paquette596f4832017-03-06 21:31:18 +0000749 for (MachineBasicBlock::iterator It = MBB.begin(), Et = MBB.end(); It != Et;
750 It++) {
751
752 // Keep track of where this instruction is in the module.
Jessica Paquette3291e732018-01-09 00:26:18 +0000753 switch (TII.getOutliningType(It, Flags)) {
Jessica Paquette78681be2017-07-27 23:24:43 +0000754 case TargetInstrInfo::MachineOutlinerInstrType::Illegal:
755 mapToIllegalUnsigned(It);
756 break;
Jessica Paquette596f4832017-03-06 21:31:18 +0000757
Jessica Paquette78681be2017-07-27 23:24:43 +0000758 case TargetInstrInfo::MachineOutlinerInstrType::Legal:
759 mapToLegalUnsigned(It);
760 break;
Jessica Paquette596f4832017-03-06 21:31:18 +0000761
Jessica Paquette78681be2017-07-27 23:24:43 +0000762 case TargetInstrInfo::MachineOutlinerInstrType::Invisible:
763 break;
Jessica Paquette596f4832017-03-06 21:31:18 +0000764 }
765 }
766
767 // After we're done every insertion, uniquely terminate this part of the
768 // "string". This makes sure we won't match across basic block or function
769 // boundaries since the "end" is encoded uniquely and thus appears in no
770 // repeated substring.
771 InstrList.push_back(MBB.end());
772 UnsignedVec.push_back(IllegalInstrNumber);
773 IllegalInstrNumber--;
774 }
775
776 InstructionMapper() {
777 // Make sure that the implementation of DenseMapInfo<unsigned> hasn't
778 // changed.
779 assert(DenseMapInfo<unsigned>::getEmptyKey() == (unsigned)-1 &&
Jessica Paquette78681be2017-07-27 23:24:43 +0000780 "DenseMapInfo<unsigned>'s empty key isn't -1!");
Jessica Paquette596f4832017-03-06 21:31:18 +0000781 assert(DenseMapInfo<unsigned>::getTombstoneKey() == (unsigned)-2 &&
Jessica Paquette78681be2017-07-27 23:24:43 +0000782 "DenseMapInfo<unsigned>'s tombstone key isn't -2!");
Jessica Paquette596f4832017-03-06 21:31:18 +0000783 }
784};
785
786/// \brief An interprocedural pass which finds repeated sequences of
787/// instructions and replaces them with calls to functions.
788///
789/// Each instruction is mapped to an unsigned integer and placed in a string.
790/// The resulting mapping is then placed in a \p SuffixTree. The \p SuffixTree
791/// is then repeatedly queried for repeated sequences of instructions. Each
792/// non-overlapping repeated sequence is then placed in its own
793/// \p MachineFunction and each instance is then replaced with a call to that
794/// function.
795struct MachineOutliner : public ModulePass {
796
797 static char ID;
798
Jessica Paquette13593842017-10-07 00:16:34 +0000799 /// \brief Set to true if the outliner should consider functions with
800 /// linkonceodr linkage.
801 bool OutlineFromLinkOnceODRs = false;
802
Jessica Paquette729e6862018-01-18 00:00:58 +0000803 // Collection of IR functions created by the outliner.
804 std::vector<Function *> CreatedIRFunctions;
805
Jessica Paquette596f4832017-03-06 21:31:18 +0000806 StringRef getPassName() const override { return "Machine Outliner"; }
807
808 void getAnalysisUsage(AnalysisUsage &AU) const override {
809 AU.addRequired<MachineModuleInfo>();
810 AU.addPreserved<MachineModuleInfo>();
811 AU.setPreservesAll();
812 ModulePass::getAnalysisUsage(AU);
813 }
814
Jessica Paquettec9ab4c22017-10-17 18:43:15 +0000815 MachineOutliner(bool OutlineFromLinkOnceODRs = false)
816 : ModulePass(ID), OutlineFromLinkOnceODRs(OutlineFromLinkOnceODRs) {
Jessica Paquette596f4832017-03-06 21:31:18 +0000817 initializeMachineOutlinerPass(*PassRegistry::getPassRegistry());
818 }
819
Jessica Paquette78681be2017-07-27 23:24:43 +0000820 /// Find all repeated substrings that satisfy the outlining cost model.
821 ///
822 /// If a substring appears at least twice, then it must be represented by
823 /// an internal node which appears in at least two suffixes. Each suffix is
824 /// represented by a leaf node. To do this, we visit each internal node in
825 /// the tree, using the leaf children of each internal node. If an internal
826 /// node represents a beneficial substring, then we use each of its leaf
827 /// children to find the locations of its substring.
828 ///
829 /// \param ST A suffix tree to query.
830 /// \param TII TargetInstrInfo for the target.
831 /// \param Mapper Contains outlining mapping information.
832 /// \param[out] CandidateList Filled with candidates representing each
833 /// beneficial substring.
834 /// \param[out] FunctionList Filled with a list of \p OutlinedFunctions each
835 /// type of candidate.
836 ///
837 /// \returns The length of the longest candidate found.
Jessica Paquette9df7fde2017-10-23 23:36:46 +0000838 unsigned
839 findCandidates(SuffixTree &ST, const TargetInstrInfo &TII,
840 InstructionMapper &Mapper,
841 std::vector<std::shared_ptr<Candidate>> &CandidateList,
842 std::vector<OutlinedFunction> &FunctionList);
Jessica Paquette78681be2017-07-27 23:24:43 +0000843
Jessica Paquette596f4832017-03-06 21:31:18 +0000844 /// \brief Replace the sequences of instructions represented by the
845 /// \p Candidates in \p CandidateList with calls to \p MachineFunctions
846 /// described in \p FunctionList.
847 ///
848 /// \param M The module we are outlining from.
849 /// \param CandidateList A list of candidates to be outlined.
850 /// \param FunctionList A list of functions to be inserted into the module.
851 /// \param Mapper Contains the instruction mappings for the module.
Jessica Paquette9df7fde2017-10-23 23:36:46 +0000852 bool outline(Module &M,
853 const ArrayRef<std::shared_ptr<Candidate>> &CandidateList,
Jessica Paquette596f4832017-03-06 21:31:18 +0000854 std::vector<OutlinedFunction> &FunctionList,
855 InstructionMapper &Mapper);
856
857 /// Creates a function for \p OF and inserts it into the module.
858 MachineFunction *createOutlinedFunction(Module &M, const OutlinedFunction &OF,
859 InstructionMapper &Mapper);
860
861 /// Find potential outlining candidates and store them in \p CandidateList.
862 ///
863 /// For each type of potential candidate, also build an \p OutlinedFunction
864 /// struct containing the information to build the function for that
865 /// candidate.
866 ///
867 /// \param[out] CandidateList Filled with outlining candidates for the module.
868 /// \param[out] FunctionList Filled with functions corresponding to each type
869 /// of \p Candidate.
870 /// \param ST The suffix tree for the module.
871 /// \param TII TargetInstrInfo for the module.
872 ///
873 /// \returns The length of the longest candidate found. 0 if there are none.
Jessica Paquette9df7fde2017-10-23 23:36:46 +0000874 unsigned
875 buildCandidateList(std::vector<std::shared_ptr<Candidate>> &CandidateList,
876 std::vector<OutlinedFunction> &FunctionList,
877 SuffixTree &ST, InstructionMapper &Mapper,
878 const TargetInstrInfo &TII);
Jessica Paquette596f4832017-03-06 21:31:18 +0000879
Jessica Paquette60d31fc2017-10-17 21:11:58 +0000880 /// Helper function for pruneOverlaps.
881 /// Removes \p C from the candidate list, and updates its \p OutlinedFunction.
882 void prune(Candidate &C, std::vector<OutlinedFunction> &FunctionList);
883
Jessica Paquette596f4832017-03-06 21:31:18 +0000884 /// \brief Remove any overlapping candidates that weren't handled by the
885 /// suffix tree's pruning method.
886 ///
887 /// Pruning from the suffix tree doesn't necessarily remove all overlaps.
888 /// If a short candidate is chosen for outlining, then a longer candidate
889 /// which has that short candidate as a suffix is chosen, the tree's pruning
890 /// method will not find it. Thus, we need to prune before outlining as well.
891 ///
892 /// \param[in,out] CandidateList A list of outlining candidates.
893 /// \param[in,out] FunctionList A list of functions to be outlined.
Jessica Paquette809d7082017-07-28 03:21:58 +0000894 /// \param Mapper Contains instruction mapping info for outlining.
Jessica Paquette596f4832017-03-06 21:31:18 +0000895 /// \param MaxCandidateLen The length of the longest candidate.
896 /// \param TII TargetInstrInfo for the module.
Jessica Paquette9df7fde2017-10-23 23:36:46 +0000897 void pruneOverlaps(std::vector<std::shared_ptr<Candidate>> &CandidateList,
Jessica Paquette596f4832017-03-06 21:31:18 +0000898 std::vector<OutlinedFunction> &FunctionList,
Jessica Paquette809d7082017-07-28 03:21:58 +0000899 InstructionMapper &Mapper, unsigned MaxCandidateLen,
900 const TargetInstrInfo &TII);
Jessica Paquette596f4832017-03-06 21:31:18 +0000901
902 /// Construct a suffix tree on the instructions in \p M and outline repeated
903 /// strings from that tree.
904 bool runOnModule(Module &M) override;
905};
906
907} // Anonymous namespace.
908
909char MachineOutliner::ID = 0;
910
911namespace llvm {
Jessica Paquette13593842017-10-07 00:16:34 +0000912ModulePass *createMachineOutlinerPass(bool OutlineFromLinkOnceODRs) {
913 return new MachineOutliner(OutlineFromLinkOnceODRs);
914}
915
Jessica Paquette78681be2017-07-27 23:24:43 +0000916} // namespace llvm
Jessica Paquette596f4832017-03-06 21:31:18 +0000917
Jessica Paquette78681be2017-07-27 23:24:43 +0000918INITIALIZE_PASS(MachineOutliner, DEBUG_TYPE, "Machine Function Outliner", false,
919 false)
920
Jessica Paquette9df7fde2017-10-23 23:36:46 +0000921unsigned MachineOutliner::findCandidates(
922 SuffixTree &ST, const TargetInstrInfo &TII, InstructionMapper &Mapper,
923 std::vector<std::shared_ptr<Candidate>> &CandidateList,
924 std::vector<OutlinedFunction> &FunctionList) {
Jessica Paquette78681be2017-07-27 23:24:43 +0000925 CandidateList.clear();
926 FunctionList.clear();
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000927 unsigned MaxLen = 0;
Jessica Paquette78681be2017-07-27 23:24:43 +0000928
929 // FIXME: Visit internal nodes instead of leaves.
930 for (SuffixTreeNode *Leaf : ST.LeafVector) {
931 assert(Leaf && "Leaves in LeafVector cannot be null!");
932 if (!Leaf->IsInTree)
933 continue;
934
935 assert(Leaf->Parent && "All leaves must have parents!");
936 SuffixTreeNode &Parent = *(Leaf->Parent);
937
938 // If it doesn't appear enough, or we already outlined from it, skip it.
939 if (Parent.OccurrenceCount < 2 || Parent.isRoot() || !Parent.IsInTree)
940 continue;
941
Jessica Paquette809d7082017-07-28 03:21:58 +0000942 // Figure out if this candidate is beneficial.
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000943 unsigned StringLen = Leaf->ConcatLen - (unsigned)Leaf->size();
Jessica Paquette95c11072017-08-14 22:57:41 +0000944
945 // Too short to be beneficial; skip it.
946 // FIXME: This isn't necessarily true for, say, X86. If we factor in
947 // instruction lengths we need more information than this.
948 if (StringLen < 2)
949 continue;
950
Jessica Paquetted87f5442017-07-29 02:55:46 +0000951 // If this is a beneficial class of candidate, then every one is stored in
952 // this vector.
953 std::vector<Candidate> CandidatesForRepeatedSeq;
954
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000955 // Describes the start and end point of each candidate. This allows the
956 // target to infer some information about each occurrence of each repeated
957 // sequence.
Jessica Paquetted87f5442017-07-29 02:55:46 +0000958 // FIXME: CandidatesForRepeatedSeq and this should be combined.
959 std::vector<
960 std::pair<MachineBasicBlock::iterator, MachineBasicBlock::iterator>>
Jessica Paquette4cf187b2017-09-27 20:47:39 +0000961 RepeatedSequenceLocs;
Jessica Paquetted87f5442017-07-29 02:55:46 +0000962
Jessica Paquette809d7082017-07-28 03:21:58 +0000963 // Figure out the call overhead for each instance of the sequence.
964 for (auto &ChildPair : Parent.Children) {
965 SuffixTreeNode *M = ChildPair.second;
Jessica Paquette78681be2017-07-27 23:24:43 +0000966
Jessica Paquette809d7082017-07-28 03:21:58 +0000967 if (M && M->IsInTree && M->isLeaf()) {
Jessica Paquetted87f5442017-07-29 02:55:46 +0000968 // Never visit this leaf again.
969 M->IsInTree = false;
Jessica Paquette52df8012017-12-01 21:56:56 +0000970 unsigned StartIdx = M->SuffixIdx;
971 unsigned EndIdx = StartIdx + StringLen - 1;
972
973 // Trick: Discard some candidates that would be incompatible with the
974 // ones we've already found for this sequence. This will save us some
975 // work in candidate selection.
976 //
977 // If two candidates overlap, then we can't outline them both. This
978 // happens when we have candidates that look like, say
979 //
980 // AA (where each "A" is an instruction).
981 //
982 // We might have some portion of the module that looks like this:
983 // AAAAAA (6 A's)
984 //
985 // In this case, there are 5 different copies of "AA" in this range, but
986 // at most 3 can be outlined. If only outlining 3 of these is going to
987 // be unbeneficial, then we ought to not bother.
988 //
989 // Note that two things DON'T overlap when they look like this:
990 // start1...end1 .... start2...end2
991 // That is, one must either
992 // * End before the other starts
993 // * Start after the other ends
994 if (std::all_of(CandidatesForRepeatedSeq.begin(),
995 CandidatesForRepeatedSeq.end(),
996 [&StartIdx, &EndIdx](const Candidate &C) {
997 return (EndIdx < C.getStartIdx() ||
998 StartIdx > C.getEndIdx());
999 })) {
1000 // It doesn't overlap with anything, so we can outline it.
1001 // Each sequence is over [StartIt, EndIt].
1002 MachineBasicBlock::iterator StartIt = Mapper.InstrList[StartIdx];
1003 MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx];
1004
Jessica Paquettea499c3c2018-01-19 21:21:49 +00001005 // Save the MachineFunction containing the Candidate.
1006 MachineFunction *MF = StartIt->getParent()->getParent();
1007 assert(MF && "Candidate doesn't have a MF?");
1008
Jessica Paquette52df8012017-12-01 21:56:56 +00001009 // Save the candidate and its location.
1010 CandidatesForRepeatedSeq.emplace_back(StartIdx, StringLen,
Jessica Paquettea499c3c2018-01-19 21:21:49 +00001011 FunctionList.size(), MF);
Jessica Paquette52df8012017-12-01 21:56:56 +00001012 RepeatedSequenceLocs.emplace_back(std::make_pair(StartIt, EndIt));
1013 }
Jessica Paquette809d7082017-07-28 03:21:58 +00001014 }
1015 }
1016
Jessica Paquetteacc15e12017-10-03 20:32:55 +00001017 // We've found something we might want to outline.
1018 // Create an OutlinedFunction to store it and check if it'd be beneficial
1019 // to outline.
Jessica Paquette4cf187b2017-09-27 20:47:39 +00001020 TargetInstrInfo::MachineOutlinerInfo MInfo =
1021 TII.getOutlininingCandidateInfo(RepeatedSequenceLocs);
Jessica Paquetteacc15e12017-10-03 20:32:55 +00001022 std::vector<unsigned> Seq;
1023 for (unsigned i = Leaf->SuffixIdx; i < Leaf->SuffixIdx + StringLen; i++)
1024 Seq.push_back(ST.Str[i]);
Jessica Paquette52df8012017-12-01 21:56:56 +00001025 OutlinedFunction OF(FunctionList.size(), CandidatesForRepeatedSeq.size(),
1026 Seq, MInfo);
Jessica Paquetteacc15e12017-10-03 20:32:55 +00001027 unsigned Benefit = OF.getBenefit();
Jessica Paquette809d7082017-07-28 03:21:58 +00001028
Jessica Paquetteffe4abc2017-08-31 21:02:45 +00001029 // Is it better to outline this candidate than not?
Jessica Paquetteacc15e12017-10-03 20:32:55 +00001030 if (Benefit < 1) {
Jessica Paquetteffe4abc2017-08-31 21:02:45 +00001031 // Outlining this candidate would take more instructions than not
1032 // outlining.
1033 // Emit a remark explaining why we didn't outline this candidate.
1034 std::pair<MachineBasicBlock::iterator, MachineBasicBlock::iterator> C =
Jessica Paquette4cf187b2017-09-27 20:47:39 +00001035 RepeatedSequenceLocs[0];
Vivek Pandya95906582017-10-11 17:12:59 +00001036 MachineOptimizationRemarkEmitter MORE(
1037 *(C.first->getParent()->getParent()), nullptr);
1038 MORE.emit([&]() {
1039 MachineOptimizationRemarkMissed R(DEBUG_TYPE, "NotOutliningCheaper",
1040 C.first->getDebugLoc(),
1041 C.first->getParent());
1042 R << "Did not outline " << NV("Length", StringLen) << " instructions"
1043 << " from " << NV("NumOccurrences", RepeatedSequenceLocs.size())
1044 << " locations."
1045 << " Instructions from outlining all occurrences ("
1046 << NV("OutliningCost", OF.getOutliningCost()) << ")"
1047 << " >= Unoutlined instruction count ("
Jessica Paquette85af63d2017-10-17 19:03:23 +00001048 << NV("NotOutliningCost", StringLen * OF.getOccurrenceCount()) << ")"
Vivek Pandya95906582017-10-11 17:12:59 +00001049 << " (Also found at: ";
Jessica Paquetteffe4abc2017-08-31 21:02:45 +00001050
Vivek Pandya95906582017-10-11 17:12:59 +00001051 // Tell the user the other places the candidate was found.
1052 for (unsigned i = 1, e = RepeatedSequenceLocs.size(); i < e; i++) {
1053 R << NV((Twine("OtherStartLoc") + Twine(i)).str(),
1054 RepeatedSequenceLocs[i].first->getDebugLoc());
1055 if (i != e - 1)
1056 R << ", ";
1057 }
Jessica Paquetteffe4abc2017-08-31 21:02:45 +00001058
Vivek Pandya95906582017-10-11 17:12:59 +00001059 R << ")";
1060 return R;
1061 });
Jessica Paquetteffe4abc2017-08-31 21:02:45 +00001062
1063 // Move to the next candidate.
Jessica Paquette78681be2017-07-27 23:24:43 +00001064 continue;
Jessica Paquetteffe4abc2017-08-31 21:02:45 +00001065 }
Jessica Paquette78681be2017-07-27 23:24:43 +00001066
1067 if (StringLen > MaxLen)
1068 MaxLen = StringLen;
1069
Jessica Paquetted87f5442017-07-29 02:55:46 +00001070 // At this point, the candidate class is seen as beneficial. Set their
1071 // benefit values and save them in the candidate list.
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001072 std::vector<std::shared_ptr<Candidate>> CandidatesForFn;
Jessica Paquetted87f5442017-07-29 02:55:46 +00001073 for (Candidate &C : CandidatesForRepeatedSeq) {
1074 C.Benefit = Benefit;
Jessica Paquette4cf187b2017-09-27 20:47:39 +00001075 C.MInfo = MInfo;
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001076 std::shared_ptr<Candidate> Cptr = std::make_shared<Candidate>(C);
1077 CandidateList.push_back(Cptr);
1078 CandidatesForFn.push_back(Cptr);
Jessica Paquette78681be2017-07-27 23:24:43 +00001079 }
1080
Jessica Paquetteacc15e12017-10-03 20:32:55 +00001081 FunctionList.push_back(OF);
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001082 FunctionList.back().Candidates = CandidatesForFn;
Jessica Paquette78681be2017-07-27 23:24:43 +00001083
1084 // Move to the next function.
Jessica Paquette78681be2017-07-27 23:24:43 +00001085 Parent.IsInTree = false;
1086 }
1087
1088 return MaxLen;
1089}
Jessica Paquette596f4832017-03-06 21:31:18 +00001090
Jessica Paquette60d31fc2017-10-17 21:11:58 +00001091// Remove C from the candidate space, and update its OutlinedFunction.
1092void MachineOutliner::prune(Candidate &C,
1093 std::vector<OutlinedFunction> &FunctionList) {
1094 // Get the OutlinedFunction associated with this Candidate.
1095 OutlinedFunction &F = FunctionList[C.FunctionIdx];
1096
1097 // Update C's associated function's occurrence count.
1098 F.decrement();
1099
1100 // Remove C from the CandidateList.
1101 C.InCandidateList = false;
1102
1103 DEBUG(dbgs() << "- Removed a Candidate \n";
1104 dbgs() << "--- Num fns left for candidate: " << F.getOccurrenceCount()
1105 << "\n";
1106 dbgs() << "--- Candidate's functions's benefit: " << F.getBenefit()
1107 << "\n";);
1108}
1109
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001110void MachineOutliner::pruneOverlaps(
1111 std::vector<std::shared_ptr<Candidate>> &CandidateList,
1112 std::vector<OutlinedFunction> &FunctionList, InstructionMapper &Mapper,
1113 unsigned MaxCandidateLen, const TargetInstrInfo &TII) {
Jessica Paquette91999162017-09-28 23:39:36 +00001114
1115 // Return true if this candidate became unbeneficial for outlining in a
1116 // previous step.
Jessica Paquette60d31fc2017-10-17 21:11:58 +00001117 auto ShouldSkipCandidate = [&FunctionList, this](Candidate &C) {
Jessica Paquette91999162017-09-28 23:39:36 +00001118
1119 // Check if the candidate was removed in a previous step.
1120 if (!C.InCandidateList)
1121 return true;
1122
Jessica Paquette85af63d2017-10-17 19:03:23 +00001123 // C must be alive. Check if we should remove it.
Jessica Paquette60d31fc2017-10-17 21:11:58 +00001124 if (FunctionList[C.FunctionIdx].getBenefit() < 1) {
1125 prune(C, FunctionList);
Jessica Paquette91999162017-09-28 23:39:36 +00001126 return true;
1127 }
1128
1129 // C is in the list, and F is still beneficial.
1130 return false;
1131 };
1132
Jessica Paquetteacffa282017-03-23 21:27:38 +00001133 // TODO: Experiment with interval trees or other interval-checking structures
1134 // to lower the time complexity of this function.
1135 // TODO: Can we do better than the simple greedy choice?
1136 // Check for overlaps in the range.
1137 // This is O(MaxCandidateLen * CandidateList.size()).
Jessica Paquette596f4832017-03-06 21:31:18 +00001138 for (auto It = CandidateList.begin(), Et = CandidateList.end(); It != Et;
1139 It++) {
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001140 Candidate &C1 = **It;
Jessica Paquette596f4832017-03-06 21:31:18 +00001141
Jessica Paquette91999162017-09-28 23:39:36 +00001142 // If C1 was already pruned, or its function is no longer beneficial for
1143 // outlining, move to the next candidate.
1144 if (ShouldSkipCandidate(C1))
Jessica Paquette596f4832017-03-06 21:31:18 +00001145 continue;
1146
Jessica Paquette596f4832017-03-06 21:31:18 +00001147 // The minimum start index of any candidate that could overlap with this
1148 // one.
1149 unsigned FarthestPossibleIdx = 0;
1150
1151 // Either the index is 0, or it's at most MaxCandidateLen indices away.
Jessica Paquette1934fd22017-10-23 16:25:53 +00001152 if (C1.getStartIdx() > MaxCandidateLen)
1153 FarthestPossibleIdx = C1.getStartIdx() - MaxCandidateLen;
Jessica Paquette596f4832017-03-06 21:31:18 +00001154
Hiroshi Inoue0909ca12018-01-26 08:15:29 +00001155 // Compare against the candidates in the list that start at most
Jessica Paquetteacffa282017-03-23 21:27:38 +00001156 // FarthestPossibleIdx indices away from C1. There are at most
1157 // MaxCandidateLen of these.
Jessica Paquette596f4832017-03-06 21:31:18 +00001158 for (auto Sit = It + 1; Sit != Et; Sit++) {
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001159 Candidate &C2 = **Sit;
Jessica Paquette596f4832017-03-06 21:31:18 +00001160
1161 // Is this candidate too far away to overlap?
Jessica Paquette1934fd22017-10-23 16:25:53 +00001162 if (C2.getStartIdx() < FarthestPossibleIdx)
Jessica Paquette596f4832017-03-06 21:31:18 +00001163 break;
1164
Jessica Paquette91999162017-09-28 23:39:36 +00001165 // If C2 was already pruned, or its function is no longer beneficial for
1166 // outlining, move to the next candidate.
1167 if (ShouldSkipCandidate(C2))
Jessica Paquette596f4832017-03-06 21:31:18 +00001168 continue;
1169
Jessica Paquette596f4832017-03-06 21:31:18 +00001170 // Do C1 and C2 overlap?
1171 //
1172 // Not overlapping:
1173 // High indices... [C1End ... C1Start][C2End ... C2Start] ...Low indices
1174 //
1175 // We sorted our candidate list so C2Start <= C1Start. We know that
1176 // C2End > C2Start since each candidate has length >= 2. Therefore, all we
1177 // have to check is C2End < C2Start to see if we overlap.
Jessica Paquette1934fd22017-10-23 16:25:53 +00001178 if (C2.getEndIdx() < C1.getStartIdx())
Jessica Paquette596f4832017-03-06 21:31:18 +00001179 continue;
1180
Jessica Paquetteacffa282017-03-23 21:27:38 +00001181 // C1 and C2 overlap.
1182 // We need to choose the better of the two.
1183 //
1184 // Approximate this by picking the one which would have saved us the
1185 // most instructions before any pruning.
Jessica Paquette60d31fc2017-10-17 21:11:58 +00001186
1187 // Is C2 a better candidate?
1188 if (C2.Benefit > C1.Benefit) {
1189 // Yes, so prune C1. Since C1 is dead, we don't have to compare it
1190 // against anything anymore, so break.
1191 prune(C1, FunctionList);
Jessica Paquetteacffa282017-03-23 21:27:38 +00001192 break;
1193 }
Jessica Paquette60d31fc2017-10-17 21:11:58 +00001194
1195 // Prune C2 and move on to the next candidate.
1196 prune(C2, FunctionList);
Jessica Paquette596f4832017-03-06 21:31:18 +00001197 }
1198 }
1199}
1200
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001201unsigned MachineOutliner::buildCandidateList(
1202 std::vector<std::shared_ptr<Candidate>> &CandidateList,
1203 std::vector<OutlinedFunction> &FunctionList, SuffixTree &ST,
1204 InstructionMapper &Mapper, const TargetInstrInfo &TII) {
Jessica Paquette596f4832017-03-06 21:31:18 +00001205
1206 std::vector<unsigned> CandidateSequence; // Current outlining candidate.
Jessica Paquette4cf187b2017-09-27 20:47:39 +00001207 unsigned MaxCandidateLen = 0; // Length of the longest candidate.
Jessica Paquette596f4832017-03-06 21:31:18 +00001208
Jessica Paquette78681be2017-07-27 23:24:43 +00001209 MaxCandidateLen =
1210 findCandidates(ST, TII, Mapper, CandidateList, FunctionList);
Jessica Paquette596f4832017-03-06 21:31:18 +00001211
Jessica Paquette596f4832017-03-06 21:31:18 +00001212 // Sort the candidates in decending order. This will simplify the outlining
1213 // process when we have to remove the candidates from the mapping by
1214 // allowing us to cut them out without keeping track of an offset.
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001215 std::stable_sort(
1216 CandidateList.begin(), CandidateList.end(),
1217 [](const std::shared_ptr<Candidate> &LHS,
1218 const std::shared_ptr<Candidate> &RHS) { return *LHS < *RHS; });
Jessica Paquette596f4832017-03-06 21:31:18 +00001219
1220 return MaxCandidateLen;
1221}
1222
1223MachineFunction *
1224MachineOutliner::createOutlinedFunction(Module &M, const OutlinedFunction &OF,
Jessica Paquette78681be2017-07-27 23:24:43 +00001225 InstructionMapper &Mapper) {
Jessica Paquette596f4832017-03-06 21:31:18 +00001226
1227 // Create the function name. This should be unique. For now, just hash the
1228 // module name and include it in the function name plus the number of this
1229 // function.
1230 std::ostringstream NameStream;
Jessica Paquette78681be2017-07-27 23:24:43 +00001231 NameStream << "OUTLINED_FUNCTION_" << OF.Name;
Jessica Paquette596f4832017-03-06 21:31:18 +00001232
1233 // Create the function using an IR-level function.
1234 LLVMContext &C = M.getContext();
1235 Function *F = dyn_cast<Function>(
Serge Guelton59a2d7b2017-04-11 15:01:18 +00001236 M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C)));
Jessica Paquette596f4832017-03-06 21:31:18 +00001237 assert(F && "Function was null!");
1238
1239 // NOTE: If this is linkonceodr, then we can take advantage of linker deduping
1240 // which gives us better results when we outline from linkonceodr functions.
1241 F->setLinkage(GlobalValue::PrivateLinkage);
1242 F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1243
Jessica Paquette729e6862018-01-18 00:00:58 +00001244 // Save F so that we can add debug info later if we need to.
1245 CreatedIRFunctions.push_back(F);
1246
Jessica Paquette596f4832017-03-06 21:31:18 +00001247 BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
1248 IRBuilder<> Builder(EntryBB);
1249 Builder.CreateRetVoid();
1250
1251 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
Matthias Braun7bda1952017-06-06 00:44:35 +00001252 MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
Jessica Paquette596f4832017-03-06 21:31:18 +00001253 MachineBasicBlock &MBB = *MF.CreateMachineBasicBlock();
1254 const TargetSubtargetInfo &STI = MF.getSubtarget();
1255 const TargetInstrInfo &TII = *STI.getInstrInfo();
1256
1257 // Insert the new function into the module.
1258 MF.insert(MF.begin(), &MBB);
1259
Jessica Paquette4cf187b2017-09-27 20:47:39 +00001260 TII.insertOutlinerPrologue(MBB, MF, OF.MInfo);
Jessica Paquette596f4832017-03-06 21:31:18 +00001261
1262 // Copy over the instructions for the function using the integer mappings in
1263 // its sequence.
1264 for (unsigned Str : OF.Sequence) {
1265 MachineInstr *NewMI =
1266 MF.CloneMachineInstr(Mapper.IntegerInstructionMap.find(Str)->second);
1267 NewMI->dropMemRefs();
1268
1269 // Don't keep debug information for outlined instructions.
Jessica Paquette596f4832017-03-06 21:31:18 +00001270 NewMI->setDebugLoc(DebugLoc());
1271 MBB.insert(MBB.end(), NewMI);
1272 }
1273
Jessica Paquette4cf187b2017-09-27 20:47:39 +00001274 TII.insertOutlinerEpilogue(MBB, MF, OF.MInfo);
Jessica Paquette729e6862018-01-18 00:00:58 +00001275
Jessica Paquettea499c3c2018-01-19 21:21:49 +00001276 // If there's a DISubprogram associated with this outlined function, then
1277 // emit debug info for the outlined function.
1278 if (DISubprogram *SP = OF.getSubprogramOrNull()) {
1279 // We have a DISubprogram. Get its DICompileUnit.
1280 DICompileUnit *CU = SP->getUnit();
1281 DIBuilder DB(M, true, CU);
1282 DIFile *Unit = SP->getFile();
1283 Mangler Mg;
1284
1285 // Walk over each IR function we created in the outliner and create
1286 // DISubprograms for each function.
1287 for (Function *F : CreatedIRFunctions) {
1288 // Get the mangled name of the function for the linkage name.
1289 std::string Dummy;
1290 llvm::raw_string_ostream MangledNameStream(Dummy);
1291 Mg.getNameWithPrefix(MangledNameStream, F, false);
1292
1293 DISubprogram *SP = DB.createFunction(
1294 Unit /* Context */, F->getName(), StringRef(MangledNameStream.str()),
1295 Unit /* File */,
1296 0 /* Line 0 is reserved for compiler-generated code. */,
1297 DB.createSubroutineType(
1298 DB.getOrCreateTypeArray(None)), /* void type */
1299 false, true, 0, /* Line 0 is reserved for compiler-generated code. */
1300 DINode::DIFlags::FlagArtificial /* Compiler-generated code. */,
1301 true /* Outlined code is optimized code by definition. */);
1302
1303 // Don't add any new variables to the subprogram.
1304 DB.finalizeSubprogram(SP);
1305
1306 // Attach subprogram to the function.
1307 F->setSubprogram(SP);
1308 }
1309
1310 // We're done with the DIBuilder.
1311 DB.finalize();
1312 }
1313
Jessica Paquette596f4832017-03-06 21:31:18 +00001314 return &MF;
1315}
1316
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001317bool MachineOutliner::outline(
1318 Module &M, const ArrayRef<std::shared_ptr<Candidate>> &CandidateList,
1319 std::vector<OutlinedFunction> &FunctionList, InstructionMapper &Mapper) {
Jessica Paquette596f4832017-03-06 21:31:18 +00001320
1321 bool OutlinedSomething = false;
Jessica Paquette596f4832017-03-06 21:31:18 +00001322 // Replace the candidates with calls to their respective outlined functions.
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001323 for (const std::shared_ptr<Candidate> &Cptr : CandidateList) {
1324 Candidate &C = *Cptr;
Jessica Paquette596f4832017-03-06 21:31:18 +00001325 // Was the candidate removed during pruneOverlaps?
1326 if (!C.InCandidateList)
1327 continue;
1328
1329 // If not, then look at its OutlinedFunction.
1330 OutlinedFunction &OF = FunctionList[C.FunctionIdx];
1331
1332 // Was its OutlinedFunction made unbeneficial during pruneOverlaps?
Jessica Paquette85af63d2017-10-17 19:03:23 +00001333 if (OF.getBenefit() < 1)
Jessica Paquette596f4832017-03-06 21:31:18 +00001334 continue;
1335
1336 // If not, then outline it.
Jessica Paquette1934fd22017-10-23 16:25:53 +00001337 assert(C.getStartIdx() < Mapper.InstrList.size() &&
Jessica Paquettec9ab4c22017-10-17 18:43:15 +00001338 "Candidate out of bounds!");
Jessica Paquette1934fd22017-10-23 16:25:53 +00001339 MachineBasicBlock *MBB = (*Mapper.InstrList[C.getStartIdx()]).getParent();
1340 MachineBasicBlock::iterator StartIt = Mapper.InstrList[C.getStartIdx()];
1341 unsigned EndIdx = C.getEndIdx();
Jessica Paquette596f4832017-03-06 21:31:18 +00001342
1343 assert(EndIdx < Mapper.InstrList.size() && "Candidate out of bounds!");
1344 MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx];
1345 assert(EndIt != MBB->end() && "EndIt out of bounds!");
1346
1347 EndIt++; // Erase needs one past the end index.
1348
1349 // Does this candidate have a function yet?
Jessica Paquetteacffa282017-03-23 21:27:38 +00001350 if (!OF.MF) {
Jessica Paquette596f4832017-03-06 21:31:18 +00001351 OF.MF = createOutlinedFunction(M, OF, Mapper);
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001352 MachineBasicBlock *MBB = &*OF.MF->begin();
1353
1354 // Output a remark telling the user that an outlined function was created,
1355 // and explaining where it came from.
1356 MachineOptimizationRemarkEmitter MORE(*OF.MF, nullptr);
1357 MachineOptimizationRemark R(DEBUG_TYPE, "OutlinedFunction",
1358 MBB->findDebugLoc(MBB->begin()), MBB);
1359 R << "Saved " << NV("OutliningBenefit", OF.getBenefit())
1360 << " instructions by "
1361 << "outlining " << NV("Length", OF.Sequence.size()) << " instructions "
1362 << "from " << NV("NumOccurrences", OF.getOccurrenceCount())
1363 << " locations. "
1364 << "(Found at: ";
1365
1366 // Tell the user the other places the candidate was found.
1367 for (size_t i = 0, e = OF.Candidates.size(); i < e; i++) {
1368
1369 // Skip over things that were pruned.
1370 if (!OF.Candidates[i]->InCandidateList)
1371 continue;
1372
1373 R << NV(
1374 (Twine("StartLoc") + Twine(i)).str(),
1375 Mapper.InstrList[OF.Candidates[i]->getStartIdx()]->getDebugLoc());
1376 if (i != e - 1)
1377 R << ", ";
1378 }
1379
1380 R << ")";
1381
1382 MORE.emit(R);
Jessica Paquetteacffa282017-03-23 21:27:38 +00001383 FunctionsCreated++;
1384 }
Jessica Paquette596f4832017-03-06 21:31:18 +00001385
1386 MachineFunction *MF = OF.MF;
1387 const TargetSubtargetInfo &STI = MF->getSubtarget();
1388 const TargetInstrInfo &TII = *STI.getInstrInfo();
1389
1390 // Insert a call to the new function and erase the old sequence.
Jessica Paquette4cf187b2017-09-27 20:47:39 +00001391 TII.insertOutlinedCall(M, *MBB, StartIt, *MF, C.MInfo);
Jessica Paquette1934fd22017-10-23 16:25:53 +00001392 StartIt = Mapper.InstrList[C.getStartIdx()];
Jessica Paquette596f4832017-03-06 21:31:18 +00001393 MBB->erase(StartIt, EndIt);
1394
1395 OutlinedSomething = true;
1396
1397 // Statistics.
1398 NumOutlined++;
1399 }
1400
Jessica Paquette78681be2017-07-27 23:24:43 +00001401 DEBUG(dbgs() << "OutlinedSomething = " << OutlinedSomething << "\n";);
Jessica Paquette596f4832017-03-06 21:31:18 +00001402
1403 return OutlinedSomething;
1404}
1405
1406bool MachineOutliner::runOnModule(Module &M) {
1407
1408 // Is there anything in the module at all?
1409 if (M.empty())
1410 return false;
1411
1412 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
Jessica Paquette78681be2017-07-27 23:24:43 +00001413 const TargetSubtargetInfo &STI =
1414 MMI.getOrCreateMachineFunction(*M.begin()).getSubtarget();
Jessica Paquette596f4832017-03-06 21:31:18 +00001415 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
1416 const TargetInstrInfo *TII = STI.getInstrInfo();
Jessica Paquette3291e732018-01-09 00:26:18 +00001417
Jessica Paquette596f4832017-03-06 21:31:18 +00001418 InstructionMapper Mapper;
1419
1420 // Build instruction mappings for each function in the module.
1421 for (Function &F : M) {
Matthias Braun7bda1952017-06-06 00:44:35 +00001422 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
Jessica Paquette596f4832017-03-06 21:31:18 +00001423
1424 // Is the function empty? Safe to outline from?
Jessica Paquette13593842017-10-07 00:16:34 +00001425 if (F.empty() ||
1426 !TII->isFunctionSafeToOutlineFrom(MF, OutlineFromLinkOnceODRs))
Jessica Paquette596f4832017-03-06 21:31:18 +00001427 continue;
1428
1429 // If it is, look at each MachineBasicBlock in the function.
1430 for (MachineBasicBlock &MBB : MF) {
1431
Jessica Paquette757e1202018-01-13 00:42:28 +00001432 // Is there anything in MBB? And is it the target of an indirect branch?
1433 if (MBB.empty() || MBB.hasAddressTaken())
Jessica Paquette596f4832017-03-06 21:31:18 +00001434 continue;
1435
1436 // If yes, map it.
1437 Mapper.convertToUnsignedVec(MBB, *TRI, *TII);
1438 }
1439 }
1440
1441 // Construct a suffix tree, use it to find candidates, and then outline them.
1442 SuffixTree ST(Mapper.UnsignedVec);
Jessica Paquette9df7fde2017-10-23 23:36:46 +00001443 std::vector<std::shared_ptr<Candidate>> CandidateList;
Jessica Paquette596f4832017-03-06 21:31:18 +00001444 std::vector<OutlinedFunction> FunctionList;
1445
Jessica Paquetteacffa282017-03-23 21:27:38 +00001446 // Find all of the outlining candidates.
Jessica Paquette596f4832017-03-06 21:31:18 +00001447 unsigned MaxCandidateLen =
Jessica Paquettec984e212017-03-13 18:39:33 +00001448 buildCandidateList(CandidateList, FunctionList, ST, Mapper, *TII);
Jessica Paquette596f4832017-03-06 21:31:18 +00001449
Jessica Paquetteacffa282017-03-23 21:27:38 +00001450 // Remove candidates that overlap with other candidates.
Jessica Paquette809d7082017-07-28 03:21:58 +00001451 pruneOverlaps(CandidateList, FunctionList, Mapper, MaxCandidateLen, *TII);
Jessica Paquetteacffa282017-03-23 21:27:38 +00001452
1453 // Outline each of the candidates and return true if something was outlined.
Jessica Paquette729e6862018-01-18 00:00:58 +00001454 bool OutlinedSomething = outline(M, CandidateList, FunctionList, Mapper);
1455
Jessica Paquette729e6862018-01-18 00:00:58 +00001456 return OutlinedSomething;
Jessica Paquette596f4832017-03-06 21:31:18 +00001457}