Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1 | //===---- MachineOutliner.cpp - Outline instructions -----------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// Replaces repeated sequences of instructions with function calls. |
| 11 | /// |
| 12 | /// This works by placing every instruction from every basic block in a |
| 13 | /// suffix tree, and repeatedly querying that tree for repeated sequences of |
| 14 | /// instructions. If a sequence of instructions appears often, then it ought |
| 15 | /// to be beneficial to pull out into a function. |
| 16 | /// |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 17 | /// The MachineOutliner communicates with a given target using hooks defined in |
| 18 | /// TargetInstrInfo.h. The target supplies the outliner with information on how |
| 19 | /// a specific sequence of instructions should be outlined. This information |
| 20 | /// is used to deduce the number of instructions necessary to |
| 21 | /// |
| 22 | /// * Create an outlined function |
| 23 | /// * Call that outlined function |
| 24 | /// |
| 25 | /// Targets must implement |
| 26 | /// * getOutliningCandidateInfo |
Jessica Paquette | 32de26d | 2018-06-19 21:14:48 +0000 | [diff] [blame] | 27 | /// * buildOutlinedFrame |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 28 | /// * insertOutlinedCall |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 29 | /// * isFunctionSafeToOutlineFrom |
| 30 | /// |
| 31 | /// in order to make use of the MachineOutliner. |
| 32 | /// |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 33 | /// This was originally presented at the 2016 LLVM Developers' Meeting in the |
| 34 | /// talk "Reducing Code Size Using Outlining". For a high-level overview of |
| 35 | /// how this pass works, the talk is available on YouTube at |
| 36 | /// |
| 37 | /// https://www.youtube.com/watch?v=yorld-WSOeU |
| 38 | /// |
| 39 | /// The slides for the talk are available at |
| 40 | /// |
| 41 | /// http://www.llvm.org/devmtg/2016-11/Slides/Paquette-Outliner.pdf |
| 42 | /// |
| 43 | /// The talk provides an overview of how the outliner finds candidates and |
| 44 | /// ultimately outlines them. It describes how the main data structure for this |
| 45 | /// pass, the suffix tree, is queried and purged for candidates. It also gives |
| 46 | /// a simplified suffix tree construction algorithm for suffix trees based off |
| 47 | /// of the algorithm actually used here, Ukkonen's algorithm. |
| 48 | /// |
| 49 | /// For the original RFC for this pass, please see |
| 50 | /// |
| 51 | /// http://lists.llvm.org/pipermail/llvm-dev/2016-August/104170.html |
| 52 | /// |
| 53 | /// For more information on the suffix tree data structure, please see |
| 54 | /// https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf |
| 55 | /// |
| 56 | //===----------------------------------------------------------------------===// |
Jessica Paquette | aa08732 | 2018-06-04 21:14:16 +0000 | [diff] [blame] | 57 | #include "llvm/CodeGen/MachineOutliner.h" |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/DenseMap.h" |
| 59 | #include "llvm/ADT/Statistic.h" |
| 60 | #include "llvm/ADT/Twine.h" |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 61 | #include "llvm/CodeGen/MachineFunction.h" |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 62 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Jessica Paquette | ffe4abc | 2017-08-31 21:02:45 +0000 | [diff] [blame] | 63 | #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
Geoff Berry | 82203c4 | 2018-01-31 20:15:16 +0000 | [diff] [blame] | 64 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 65 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 66 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 67 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Jessica Paquette | 729e686 | 2018-01-18 00:00:58 +0000 | [diff] [blame] | 68 | #include "llvm/IR/DIBuilder.h" |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 69 | #include "llvm/IR/IRBuilder.h" |
Jessica Paquette | a499c3c | 2018-01-19 21:21:49 +0000 | [diff] [blame] | 70 | #include "llvm/IR/Mangler.h" |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 71 | #include "llvm/Support/Allocator.h" |
Jessica Paquette | 1eca23b | 2018-04-19 22:17:07 +0000 | [diff] [blame] | 72 | #include "llvm/Support/CommandLine.h" |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 73 | #include "llvm/Support/Debug.h" |
| 74 | #include "llvm/Support/raw_ostream.h" |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 75 | #include <functional> |
| 76 | #include <map> |
| 77 | #include <sstream> |
| 78 | #include <tuple> |
| 79 | #include <vector> |
| 80 | |
| 81 | #define DEBUG_TYPE "machine-outliner" |
| 82 | |
| 83 | using namespace llvm; |
Jessica Paquette | ffe4abc | 2017-08-31 21:02:45 +0000 | [diff] [blame] | 84 | using namespace ore; |
Jessica Paquette | aa08732 | 2018-06-04 21:14:16 +0000 | [diff] [blame] | 85 | using namespace outliner; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 86 | |
| 87 | STATISTIC(NumOutlined, "Number of candidates outlined"); |
| 88 | STATISTIC(FunctionsCreated, "Number of functions created"); |
| 89 | |
Jessica Paquette | 1eca23b | 2018-04-19 22:17:07 +0000 | [diff] [blame] | 90 | // Set to true if the user wants the outliner to run on linkonceodr linkage |
| 91 | // functions. This is false by default because the linker can dedupe linkonceodr |
| 92 | // functions. Since the outliner is confined to a single module (modulo LTO), |
| 93 | // this is off by default. It should, however, be the default behaviour in |
| 94 | // LTO. |
| 95 | static cl::opt<bool> EnableLinkOnceODROutlining( |
| 96 | "enable-linkonceodr-outlining", |
| 97 | cl::Hidden, |
| 98 | cl::desc("Enable the machine outliner on linkonceodr functions"), |
| 99 | cl::init(false)); |
| 100 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 101 | namespace { |
| 102 | |
| 103 | /// Represents an undefined index in the suffix tree. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 104 | const unsigned EmptyIdx = -1; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 105 | |
| 106 | /// A node in a suffix tree which represents a substring or suffix. |
| 107 | /// |
| 108 | /// Each node has either no children or at least two children, with the root |
| 109 | /// being a exception in the empty tree. |
| 110 | /// |
| 111 | /// Children are represented as a map between unsigned integers and nodes. If |
| 112 | /// a node N has a child M on unsigned integer k, then the mapping represented |
| 113 | /// by N is a proper prefix of the mapping represented by M. Note that this, |
| 114 | /// although similar to a trie is somewhat different: each node stores a full |
| 115 | /// substring of the full mapping rather than a single character state. |
| 116 | /// |
| 117 | /// Each internal node contains a pointer to the internal node representing |
| 118 | /// the same string, but with the first character chopped off. This is stored |
| 119 | /// in \p Link. Each leaf node stores the start index of its respective |
| 120 | /// suffix in \p SuffixIdx. |
| 121 | struct SuffixTreeNode { |
| 122 | |
| 123 | /// The children of this node. |
| 124 | /// |
| 125 | /// A child existing on an unsigned integer implies that from the mapping |
| 126 | /// represented by the current node, there is a way to reach another |
| 127 | /// mapping by tacking that character on the end of the current string. |
| 128 | DenseMap<unsigned, SuffixTreeNode *> Children; |
| 129 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 130 | /// The start index of this node's substring in the main string. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 131 | unsigned StartIdx = EmptyIdx; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 132 | |
| 133 | /// The end index of this node's substring in the main string. |
| 134 | /// |
| 135 | /// Every leaf node must have its \p EndIdx incremented at the end of every |
| 136 | /// step in the construction algorithm. To avoid having to update O(N) |
| 137 | /// nodes individually at the end of every step, the end index is stored |
| 138 | /// as a pointer. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 139 | unsigned *EndIdx = nullptr; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 140 | |
| 141 | /// For leaves, the start index of the suffix represented by this node. |
| 142 | /// |
| 143 | /// For all other nodes, this is ignored. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 144 | unsigned SuffixIdx = EmptyIdx; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 145 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 146 | /// For internal nodes, a pointer to the internal node representing |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 147 | /// the same sequence with the first character chopped off. |
| 148 | /// |
Jessica Paquette | 4602c34 | 2017-07-28 05:59:30 +0000 | [diff] [blame] | 149 | /// This acts as a shortcut in Ukkonen's algorithm. One of the things that |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 150 | /// Ukkonen's algorithm does to achieve linear-time construction is |
| 151 | /// keep track of which node the next insert should be at. This makes each |
| 152 | /// insert O(1), and there are a total of O(N) inserts. The suffix link |
| 153 | /// helps with inserting children of internal nodes. |
| 154 | /// |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 155 | /// Say we add a child to an internal node with associated mapping S. The |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 156 | /// next insertion must be at the node representing S - its first character. |
| 157 | /// This is given by the way that we iteratively build the tree in Ukkonen's |
| 158 | /// algorithm. The main idea is to look at the suffixes of each prefix in the |
| 159 | /// string, starting with the longest suffix of the prefix, and ending with |
| 160 | /// the shortest. Therefore, if we keep pointers between such nodes, we can |
| 161 | /// move to the next insertion point in O(1) time. If we don't, then we'd |
| 162 | /// have to query from the root, which takes O(N) time. This would make the |
| 163 | /// construction algorithm O(N^2) rather than O(N). |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 164 | SuffixTreeNode *Link = nullptr; |
| 165 | |
Jessica Paquette | acffa28 | 2017-03-23 21:27:38 +0000 | [diff] [blame] | 166 | /// The length of the string formed by concatenating the edge labels from the |
| 167 | /// root to this node. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 168 | unsigned ConcatLen = 0; |
Jessica Paquette | acffa28 | 2017-03-23 21:27:38 +0000 | [diff] [blame] | 169 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 170 | /// Returns true if this node is a leaf. |
| 171 | bool isLeaf() const { return SuffixIdx != EmptyIdx; } |
| 172 | |
| 173 | /// Returns true if this node is the root of its owning \p SuffixTree. |
| 174 | bool isRoot() const { return StartIdx == EmptyIdx; } |
| 175 | |
| 176 | /// Return the number of elements in the substring associated with this node. |
| 177 | size_t size() const { |
| 178 | |
| 179 | // Is it the root? If so, it's the empty string so return 0. |
| 180 | if (isRoot()) |
| 181 | return 0; |
| 182 | |
| 183 | assert(*EndIdx != EmptyIdx && "EndIdx is undefined!"); |
| 184 | |
| 185 | // Size = the number of elements in the string. |
| 186 | // For example, [0 1 2 3] has length 4, not 3. 3-0 = 3, so we have 3-0+1. |
| 187 | return *EndIdx - StartIdx + 1; |
| 188 | } |
| 189 | |
Jessica Paquette | df5b09b | 2018-11-07 19:56:13 +0000 | [diff] [blame] | 190 | SuffixTreeNode(unsigned StartIdx, unsigned *EndIdx, SuffixTreeNode *Link) |
| 191 | : StartIdx(StartIdx), EndIdx(EndIdx), Link(Link) {} |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 192 | |
| 193 | SuffixTreeNode() {} |
| 194 | }; |
| 195 | |
| 196 | /// A data structure for fast substring queries. |
| 197 | /// |
| 198 | /// Suffix trees represent the suffixes of their input strings in their leaves. |
| 199 | /// A suffix tree is a type of compressed trie structure where each node |
| 200 | /// represents an entire substring rather than a single character. Each leaf |
| 201 | /// of the tree is a suffix. |
| 202 | /// |
| 203 | /// A suffix tree can be seen as a type of state machine where each state is a |
| 204 | /// substring of the full string. The tree is structured so that, for a string |
| 205 | /// of length N, there are exactly N leaves in the tree. This structure allows |
| 206 | /// us to quickly find repeated substrings of the input string. |
| 207 | /// |
| 208 | /// In this implementation, a "string" is a vector of unsigned integers. |
| 209 | /// These integers may result from hashing some data type. A suffix tree can |
| 210 | /// contain 1 or many strings, which can then be queried as one large string. |
| 211 | /// |
| 212 | /// The suffix tree is implemented using Ukkonen's algorithm for linear-time |
| 213 | /// suffix tree construction. Ukkonen's algorithm is explained in more detail |
| 214 | /// in the paper by Esko Ukkonen "On-line construction of suffix trees. The |
| 215 | /// paper is available at |
| 216 | /// |
| 217 | /// https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf |
| 218 | class SuffixTree { |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 219 | public: |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 220 | /// Each element is an integer representing an instruction in the module. |
| 221 | ArrayRef<unsigned> Str; |
| 222 | |
Jessica Paquette | 4e54ef8 | 2018-11-06 21:46:41 +0000 | [diff] [blame] | 223 | /// A repeated substring in the tree. |
| 224 | struct RepeatedSubstring { |
| 225 | /// The length of the string. |
| 226 | unsigned Length; |
| 227 | |
| 228 | /// The start indices of each occurrence. |
| 229 | std::vector<unsigned> StartIndices; |
| 230 | }; |
| 231 | |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 232 | private: |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 233 | /// Maintains each node in the tree. |
Jessica Paquette | d4cb9c6 | 2017-03-08 23:55:33 +0000 | [diff] [blame] | 234 | SpecificBumpPtrAllocator<SuffixTreeNode> NodeAllocator; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 235 | |
| 236 | /// The root of the suffix tree. |
| 237 | /// |
| 238 | /// The root represents the empty string. It is maintained by the |
| 239 | /// \p NodeAllocator like every other node in the tree. |
| 240 | SuffixTreeNode *Root = nullptr; |
| 241 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 242 | /// Maintains the end indices of the internal nodes in the tree. |
| 243 | /// |
| 244 | /// Each internal node is guaranteed to never have its end index change |
| 245 | /// during the construction algorithm; however, leaves must be updated at |
| 246 | /// every step. Therefore, we need to store leaf end indices by reference |
| 247 | /// to avoid updating O(N) leaves at every step of construction. Thus, |
| 248 | /// every internal node must be allocated its own end index. |
| 249 | BumpPtrAllocator InternalEndIdxAllocator; |
| 250 | |
| 251 | /// The end index of each leaf in the tree. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 252 | unsigned LeafEndIdx = -1; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 253 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 254 | /// Helper struct which keeps track of the next insertion point in |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 255 | /// Ukkonen's algorithm. |
| 256 | struct ActiveState { |
| 257 | /// The next node to insert at. |
| 258 | SuffixTreeNode *Node; |
| 259 | |
| 260 | /// The index of the first character in the substring currently being added. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 261 | unsigned Idx = EmptyIdx; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 262 | |
| 263 | /// The length of the substring we have to add at the current step. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 264 | unsigned Len = 0; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 267 | /// The point the next insertion will take place at in the |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 268 | /// construction algorithm. |
| 269 | ActiveState Active; |
| 270 | |
| 271 | /// Allocate a leaf node and add it to the tree. |
| 272 | /// |
| 273 | /// \param Parent The parent of this node. |
| 274 | /// \param StartIdx The start index of this node's associated string. |
| 275 | /// \param Edge The label on the edge leaving \p Parent to this node. |
| 276 | /// |
| 277 | /// \returns A pointer to the allocated leaf node. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 278 | SuffixTreeNode *insertLeaf(SuffixTreeNode &Parent, unsigned StartIdx, |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 279 | unsigned Edge) { |
| 280 | |
| 281 | assert(StartIdx <= LeafEndIdx && "String can't start after it ends!"); |
| 282 | |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 283 | SuffixTreeNode *N = new (NodeAllocator.Allocate()) |
Jessica Paquette | df5b09b | 2018-11-07 19:56:13 +0000 | [diff] [blame] | 284 | SuffixTreeNode(StartIdx, &LeafEndIdx, nullptr); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 285 | Parent.Children[Edge] = N; |
| 286 | |
| 287 | return N; |
| 288 | } |
| 289 | |
| 290 | /// Allocate an internal node and add it to the tree. |
| 291 | /// |
| 292 | /// \param Parent The parent of this node. Only null when allocating the root. |
| 293 | /// \param StartIdx The start index of this node's associated string. |
| 294 | /// \param EndIdx The end index of this node's associated string. |
| 295 | /// \param Edge The label on the edge leaving \p Parent to this node. |
| 296 | /// |
| 297 | /// \returns A pointer to the allocated internal node. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 298 | SuffixTreeNode *insertInternalNode(SuffixTreeNode *Parent, unsigned StartIdx, |
| 299 | unsigned EndIdx, unsigned Edge) { |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 300 | |
| 301 | assert(StartIdx <= EndIdx && "String can't start after it ends!"); |
| 302 | assert(!(!Parent && StartIdx != EmptyIdx) && |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 303 | "Non-root internal nodes must have parents!"); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 304 | |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 305 | unsigned *E = new (InternalEndIdxAllocator) unsigned(EndIdx); |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 306 | SuffixTreeNode *N = new (NodeAllocator.Allocate()) |
Jessica Paquette | df5b09b | 2018-11-07 19:56:13 +0000 | [diff] [blame] | 307 | SuffixTreeNode(StartIdx, E, Root); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 308 | if (Parent) |
| 309 | Parent->Children[Edge] = N; |
| 310 | |
| 311 | return N; |
| 312 | } |
| 313 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 314 | /// Set the suffix indices of the leaves to the start indices of their |
Jessica Paquette | 4e54ef8 | 2018-11-06 21:46:41 +0000 | [diff] [blame] | 315 | /// respective suffixes. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 316 | /// |
| 317 | /// \param[in] CurrNode The node currently being visited. |
Jessica Paquette | df5b09b | 2018-11-07 19:56:13 +0000 | [diff] [blame] | 318 | /// \param CurrNodeLen The concatenation of all node sizes from the root to |
| 319 | /// this node. Used to produce suffix indices. |
| 320 | void setSuffixIndices(SuffixTreeNode &CurrNode, unsigned CurrNodeLen) { |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 321 | |
| 322 | bool IsLeaf = CurrNode.Children.size() == 0 && !CurrNode.isRoot(); |
| 323 | |
Jessica Paquette | df5b09b | 2018-11-07 19:56:13 +0000 | [diff] [blame] | 324 | // Store the concatenation of lengths down from the root. |
| 325 | CurrNode.ConcatLen = CurrNodeLen; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 326 | // Traverse the tree depth-first. |
| 327 | for (auto &ChildPair : CurrNode.Children) { |
| 328 | assert(ChildPair.second && "Node had a null child!"); |
Jessica Paquette | df5b09b | 2018-11-07 19:56:13 +0000 | [diff] [blame] | 329 | setSuffixIndices(*ChildPair.second, |
| 330 | CurrNodeLen + ChildPair.second->size()); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Jessica Paquette | df5b09b | 2018-11-07 19:56:13 +0000 | [diff] [blame] | 333 | // Is this node a leaf? If it is, give it a suffix index. |
| 334 | if (IsLeaf) |
| 335 | CurrNode.SuffixIdx = Str.size() - CurrNodeLen; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 338 | /// Construct the suffix tree for the prefix of the input ending at |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 339 | /// \p EndIdx. |
| 340 | /// |
| 341 | /// Used to construct the full suffix tree iteratively. At the end of each |
| 342 | /// step, the constructed suffix tree is either a valid suffix tree, or a |
| 343 | /// suffix tree with implicit suffixes. At the end of the final step, the |
| 344 | /// suffix tree is a valid tree. |
| 345 | /// |
| 346 | /// \param EndIdx The end index of the current prefix in the main string. |
| 347 | /// \param SuffixesToAdd The number of suffixes that must be added |
| 348 | /// to complete the suffix tree at the current phase. |
| 349 | /// |
| 350 | /// \returns The number of suffixes that have not been added at the end of |
| 351 | /// this step. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 352 | unsigned extend(unsigned EndIdx, unsigned SuffixesToAdd) { |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 353 | SuffixTreeNode *NeedsLink = nullptr; |
| 354 | |
| 355 | while (SuffixesToAdd > 0) { |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 356 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 357 | // Are we waiting to add anything other than just the last character? |
| 358 | if (Active.Len == 0) { |
| 359 | // If not, then say the active index is the end index. |
| 360 | Active.Idx = EndIdx; |
| 361 | } |
| 362 | |
| 363 | assert(Active.Idx <= EndIdx && "Start index can't be after end index!"); |
| 364 | |
| 365 | // The first character in the current substring we're looking at. |
| 366 | unsigned FirstChar = Str[Active.Idx]; |
| 367 | |
| 368 | // Have we inserted anything starting with FirstChar at the current node? |
| 369 | if (Active.Node->Children.count(FirstChar) == 0) { |
| 370 | // If not, then we can just insert a leaf and move too the next step. |
| 371 | insertLeaf(*Active.Node, EndIdx, FirstChar); |
| 372 | |
| 373 | // The active node is an internal node, and we visited it, so it must |
| 374 | // need a link if it doesn't have one. |
| 375 | if (NeedsLink) { |
| 376 | NeedsLink->Link = Active.Node; |
| 377 | NeedsLink = nullptr; |
| 378 | } |
| 379 | } else { |
| 380 | // There's a match with FirstChar, so look for the point in the tree to |
| 381 | // insert a new node. |
| 382 | SuffixTreeNode *NextNode = Active.Node->Children[FirstChar]; |
| 383 | |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 384 | unsigned SubstringLen = NextNode->size(); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 385 | |
| 386 | // Is the current suffix we're trying to insert longer than the size of |
| 387 | // the child we want to move to? |
| 388 | if (Active.Len >= SubstringLen) { |
| 389 | // If yes, then consume the characters we've seen and move to the next |
| 390 | // node. |
| 391 | Active.Idx += SubstringLen; |
| 392 | Active.Len -= SubstringLen; |
| 393 | Active.Node = NextNode; |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | // Otherwise, the suffix we're trying to insert must be contained in the |
| 398 | // next node we want to move to. |
| 399 | unsigned LastChar = Str[EndIdx]; |
| 400 | |
| 401 | // Is the string we're trying to insert a substring of the next node? |
| 402 | if (Str[NextNode->StartIdx + Active.Len] == LastChar) { |
| 403 | // If yes, then we're done for this step. Remember our insertion point |
| 404 | // and move to the next end index. At this point, we have an implicit |
| 405 | // suffix tree. |
| 406 | if (NeedsLink && !Active.Node->isRoot()) { |
| 407 | NeedsLink->Link = Active.Node; |
| 408 | NeedsLink = nullptr; |
| 409 | } |
| 410 | |
| 411 | Active.Len++; |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | // The string we're trying to insert isn't a substring of the next node, |
| 416 | // but matches up to a point. Split the node. |
| 417 | // |
| 418 | // For example, say we ended our search at a node n and we're trying to |
| 419 | // insert ABD. Then we'll create a new node s for AB, reduce n to just |
| 420 | // representing C, and insert a new leaf node l to represent d. This |
| 421 | // allows us to ensure that if n was a leaf, it remains a leaf. |
| 422 | // |
| 423 | // | ABC ---split---> | AB |
| 424 | // n s |
| 425 | // C / \ D |
| 426 | // n l |
| 427 | |
| 428 | // The node s from the diagram |
| 429 | SuffixTreeNode *SplitNode = |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 430 | insertInternalNode(Active.Node, NextNode->StartIdx, |
| 431 | NextNode->StartIdx + Active.Len - 1, FirstChar); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 432 | |
| 433 | // Insert the new node representing the new substring into the tree as |
| 434 | // a child of the split node. This is the node l from the diagram. |
| 435 | insertLeaf(*SplitNode, EndIdx, LastChar); |
| 436 | |
| 437 | // Make the old node a child of the split node and update its start |
| 438 | // index. This is the node n from the diagram. |
| 439 | NextNode->StartIdx += Active.Len; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 440 | SplitNode->Children[Str[NextNode->StartIdx]] = NextNode; |
| 441 | |
| 442 | // SplitNode is an internal node, update the suffix link. |
| 443 | if (NeedsLink) |
| 444 | NeedsLink->Link = SplitNode; |
| 445 | |
| 446 | NeedsLink = SplitNode; |
| 447 | } |
| 448 | |
| 449 | // We've added something new to the tree, so there's one less suffix to |
| 450 | // add. |
| 451 | SuffixesToAdd--; |
| 452 | |
| 453 | if (Active.Node->isRoot()) { |
| 454 | if (Active.Len > 0) { |
| 455 | Active.Len--; |
| 456 | Active.Idx = EndIdx - SuffixesToAdd + 1; |
| 457 | } |
| 458 | } else { |
| 459 | // Start the next phase at the next smallest suffix. |
| 460 | Active.Node = Active.Node->Link; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | return SuffixesToAdd; |
| 465 | } |
| 466 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 467 | public: |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 468 | /// Construct a suffix tree from a sequence of unsigned integers. |
| 469 | /// |
| 470 | /// \param Str The string to construct the suffix tree for. |
| 471 | SuffixTree(const std::vector<unsigned> &Str) : Str(Str) { |
| 472 | Root = insertInternalNode(nullptr, EmptyIdx, EmptyIdx, 0); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 473 | Active.Node = Root; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 474 | |
| 475 | // Keep track of the number of suffixes we have to add of the current |
| 476 | // prefix. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 477 | unsigned SuffixesToAdd = 0; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 478 | Active.Node = Root; |
| 479 | |
| 480 | // Construct the suffix tree iteratively on each prefix of the string. |
| 481 | // PfxEndIdx is the end index of the current prefix. |
| 482 | // End is one past the last element in the string. |
Jessica Paquette | 4cf187b | 2017-09-27 20:47:39 +0000 | [diff] [blame] | 483 | for (unsigned PfxEndIdx = 0, End = Str.size(); PfxEndIdx < End; |
| 484 | PfxEndIdx++) { |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 485 | SuffixesToAdd++; |
| 486 | LeafEndIdx = PfxEndIdx; // Extend each of the leaves. |
| 487 | SuffixesToAdd = extend(PfxEndIdx, SuffixesToAdd); |
| 488 | } |
| 489 | |
| 490 | // Set the suffix indices of each leaf. |
| 491 | assert(Root && "Root node can't be nullptr!"); |
| 492 | setSuffixIndices(*Root, 0); |
| 493 | } |
Jessica Paquette | 4e54ef8 | 2018-11-06 21:46:41 +0000 | [diff] [blame] | 494 | |
Jessica Paquette | a409cc9 | 2018-11-07 19:20:55 +0000 | [diff] [blame] | 495 | |
| 496 | /// Iterator for finding all repeated substrings in the suffix tree. |
| 497 | struct RepeatedSubstringIterator { |
| 498 | private: |
| 499 | /// The current node we're visiting. |
| 500 | SuffixTreeNode *N = nullptr; |
| 501 | |
| 502 | /// The repeated substring associated with this node. |
| 503 | RepeatedSubstring RS; |
| 504 | |
| 505 | /// The nodes left to visit. |
| 506 | std::vector<SuffixTreeNode *> ToVisit; |
| 507 | |
| 508 | /// The minimum length of a repeated substring to find. |
| 509 | /// Since we're outlining, we want at least two instructions in the range. |
| 510 | /// FIXME: This may not be true for targets like X86 which support many |
| 511 | /// instruction lengths. |
| 512 | const unsigned MinLength = 2; |
| 513 | |
| 514 | /// Move the iterator to the next repeated substring. |
| 515 | void advance() { |
| 516 | // Clear the current state. If we're at the end of the range, then this |
| 517 | // is the state we want to be in. |
| 518 | RS = RepeatedSubstring(); |
| 519 | N = nullptr; |
| 520 | |
Jessica Paquette | 3cd70b3 | 2018-12-06 00:26:21 +0000 | [diff] [blame] | 521 | // Each leaf node represents a repeat of a string. |
| 522 | std::vector<SuffixTreeNode *> LeafChildren; |
| 523 | |
Jessica Paquette | a409cc9 | 2018-11-07 19:20:55 +0000 | [diff] [blame] | 524 | // Continue visiting nodes until we find one which repeats more than once. |
| 525 | while (!ToVisit.empty()) { |
| 526 | SuffixTreeNode *Curr = ToVisit.back(); |
| 527 | ToVisit.pop_back(); |
Jessica Paquette | 3cd70b3 | 2018-12-06 00:26:21 +0000 | [diff] [blame] | 528 | LeafChildren.clear(); |
Jessica Paquette | a409cc9 | 2018-11-07 19:20:55 +0000 | [diff] [blame] | 529 | |
| 530 | // Keep track of the length of the string associated with the node. If |
| 531 | // it's too short, we'll quit. |
| 532 | unsigned Length = Curr->ConcatLen; |
| 533 | |
Jessica Paquette | a409cc9 | 2018-11-07 19:20:55 +0000 | [diff] [blame] | 534 | // Iterate over each child, saving internal nodes for visiting, and |
| 535 | // leaf nodes in LeafChildren. Internal nodes represent individual |
| 536 | // strings, which may repeat. |
| 537 | for (auto &ChildPair : Curr->Children) { |
| 538 | // Save all of this node's children for processing. |
| 539 | if (!ChildPair.second->isLeaf()) |
| 540 | ToVisit.push_back(ChildPair.second); |
| 541 | |
| 542 | // It's not an internal node, so it must be a leaf. If we have a |
| 543 | // long enough string, then save the leaf children. |
| 544 | else if (Length >= MinLength) |
| 545 | LeafChildren.push_back(ChildPair.second); |
| 546 | } |
| 547 | |
| 548 | // The root never represents a repeated substring. If we're looking at |
| 549 | // that, then skip it. |
| 550 | if (Curr->isRoot()) |
| 551 | continue; |
| 552 | |
| 553 | // Do we have any repeated substrings? |
| 554 | if (LeafChildren.size() >= 2) { |
| 555 | // Yes. Update the state to reflect this, and then bail out. |
| 556 | N = Curr; |
| 557 | RS.Length = Length; |
| 558 | for (SuffixTreeNode *Leaf : LeafChildren) |
| 559 | RS.StartIndices.push_back(Leaf->SuffixIdx); |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // At this point, either NewRS is an empty RepeatedSubstring, or it was |
| 565 | // set in the above loop. Similarly, N is either nullptr, or the node |
| 566 | // associated with NewRS. |
| 567 | } |
| 568 | |
| 569 | public: |
| 570 | /// Return the current repeated substring. |
| 571 | RepeatedSubstring &operator*() { return RS; } |
| 572 | |
| 573 | RepeatedSubstringIterator &operator++() { |
| 574 | advance(); |
| 575 | return *this; |
| 576 | } |
| 577 | |
| 578 | RepeatedSubstringIterator operator++(int I) { |
| 579 | RepeatedSubstringIterator It(*this); |
| 580 | advance(); |
| 581 | return It; |
| 582 | } |
| 583 | |
| 584 | bool operator==(const RepeatedSubstringIterator &Other) { |
| 585 | return N == Other.N; |
| 586 | } |
| 587 | bool operator!=(const RepeatedSubstringIterator &Other) { |
| 588 | return !(*this == Other); |
| 589 | } |
| 590 | |
| 591 | RepeatedSubstringIterator(SuffixTreeNode *N) : N(N) { |
| 592 | // Do we have a non-null node? |
| 593 | if (N) { |
| 594 | // Yes. At the first step, we need to visit all of N's children. |
| 595 | // Note: This means that we visit N last. |
| 596 | ToVisit.push_back(N); |
| 597 | advance(); |
| 598 | } |
| 599 | } |
| 600 | }; |
| 601 | |
| 602 | typedef RepeatedSubstringIterator iterator; |
| 603 | iterator begin() { return iterator(Root); } |
| 604 | iterator end() { return iterator(nullptr); } |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 605 | }; |
| 606 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 607 | /// Maps \p MachineInstrs to unsigned integers and stores the mappings. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 608 | struct InstructionMapper { |
| 609 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 610 | /// The next available integer to assign to a \p MachineInstr that |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 611 | /// cannot be outlined. |
| 612 | /// |
| 613 | /// Set to -3 for compatability with \p DenseMapInfo<unsigned>. |
| 614 | unsigned IllegalInstrNumber = -3; |
| 615 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 616 | /// The next available integer to assign to a \p MachineInstr that can |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 617 | /// be outlined. |
| 618 | unsigned LegalInstrNumber = 0; |
| 619 | |
| 620 | /// Correspondence from \p MachineInstrs to unsigned integers. |
| 621 | DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait> |
| 622 | InstructionIntegerMap; |
| 623 | |
Jessica Paquette | cad864d | 2018-11-13 23:01:34 +0000 | [diff] [blame] | 624 | /// Correspondence between \p MachineBasicBlocks and target-defined flags. |
| 625 | DenseMap<MachineBasicBlock *, unsigned> MBBFlagsMap; |
| 626 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 627 | /// The vector of unsigned integers that the module is mapped to. |
| 628 | std::vector<unsigned> UnsignedVec; |
| 629 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 630 | /// Stores the location of the instruction associated with the integer |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 631 | /// at index i in \p UnsignedVec for each index i. |
| 632 | std::vector<MachineBasicBlock::iterator> InstrList; |
| 633 | |
Jessica Paquette | c991cf3 | 2018-11-01 23:09:06 +0000 | [diff] [blame] | 634 | // Set if we added an illegal number in the previous step. |
| 635 | // Since each illegal number is unique, we only need one of them between |
| 636 | // each range of legal numbers. This lets us make sure we don't add more |
| 637 | // than one illegal number per range. |
| 638 | bool AddedIllegalLastTime = false; |
| 639 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 640 | /// Maps \p *It to a legal integer. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 641 | /// |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 642 | /// Updates \p CanOutlineWithPrevInstr, \p HaveLegalRange, \p InstrListForMBB, |
Jessica Paquette | ca3ed96 | 2018-12-06 00:01:51 +0000 | [diff] [blame] | 643 | /// \p UnsignedVecForMBB, \p InstructionIntegerMap, and \p LegalInstrNumber. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 644 | /// |
| 645 | /// \returns The integer that \p *It was mapped to. |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 646 | unsigned mapToLegalUnsigned( |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 647 | MachineBasicBlock::iterator &It, bool &CanOutlineWithPrevInstr, |
| 648 | bool &HaveLegalRange, unsigned &NumLegalInBlock, |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 649 | std::vector<unsigned> &UnsignedVecForMBB, |
| 650 | std::vector<MachineBasicBlock::iterator> &InstrListForMBB) { |
Jessica Paquette | c991cf3 | 2018-11-01 23:09:06 +0000 | [diff] [blame] | 651 | // We added something legal, so we should unset the AddedLegalLastTime |
| 652 | // flag. |
| 653 | AddedIllegalLastTime = false; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 654 | |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 655 | // If we have at least two adjacent legal instructions (which may have |
| 656 | // invisible instructions in between), remember that. |
| 657 | if (CanOutlineWithPrevInstr) |
| 658 | HaveLegalRange = true; |
| 659 | CanOutlineWithPrevInstr = true; |
| 660 | |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 661 | // Keep track of the number of legal instructions we insert. |
| 662 | NumLegalInBlock++; |
| 663 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 664 | // Get the integer for this instruction or give it the current |
| 665 | // LegalInstrNumber. |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 666 | InstrListForMBB.push_back(It); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 667 | MachineInstr &MI = *It; |
| 668 | bool WasInserted; |
| 669 | DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>::iterator |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 670 | ResultIt; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 671 | std::tie(ResultIt, WasInserted) = |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 672 | InstructionIntegerMap.insert(std::make_pair(&MI, LegalInstrNumber)); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 673 | unsigned MINumber = ResultIt->second; |
| 674 | |
| 675 | // There was an insertion. |
Jessica Paquette | ca3ed96 | 2018-12-06 00:01:51 +0000 | [diff] [blame] | 676 | if (WasInserted) |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 677 | LegalInstrNumber++; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 678 | |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 679 | UnsignedVecForMBB.push_back(MINumber); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 680 | |
| 681 | // Make sure we don't overflow or use any integers reserved by the DenseMap. |
| 682 | if (LegalInstrNumber >= IllegalInstrNumber) |
| 683 | report_fatal_error("Instruction mapping overflow!"); |
| 684 | |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 685 | assert(LegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() && |
| 686 | "Tried to assign DenseMap tombstone or empty key to instruction."); |
| 687 | assert(LegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() && |
| 688 | "Tried to assign DenseMap tombstone or empty key to instruction."); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 689 | |
| 690 | return MINumber; |
| 691 | } |
| 692 | |
| 693 | /// Maps \p *It to an illegal integer. |
| 694 | /// |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 695 | /// Updates \p InstrListForMBB, \p UnsignedVecForMBB, and \p |
| 696 | /// IllegalInstrNumber. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 697 | /// |
| 698 | /// \returns The integer that \p *It was mapped to. |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 699 | unsigned mapToIllegalUnsigned(MachineBasicBlock::iterator &It, |
| 700 | bool &CanOutlineWithPrevInstr, std::vector<unsigned> &UnsignedVecForMBB, |
| 701 | std::vector<MachineBasicBlock::iterator> &InstrListForMBB) { |
| 702 | // Can't outline an illegal instruction. Set the flag. |
| 703 | CanOutlineWithPrevInstr = false; |
| 704 | |
Jessica Paquette | c991cf3 | 2018-11-01 23:09:06 +0000 | [diff] [blame] | 705 | // Only add one illegal number per range of legal numbers. |
| 706 | if (AddedIllegalLastTime) |
| 707 | return IllegalInstrNumber; |
| 708 | |
| 709 | // Remember that we added an illegal number last time. |
| 710 | AddedIllegalLastTime = true; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 711 | unsigned MINumber = IllegalInstrNumber; |
| 712 | |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 713 | InstrListForMBB.push_back(It); |
| 714 | UnsignedVecForMBB.push_back(IllegalInstrNumber); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 715 | IllegalInstrNumber--; |
| 716 | |
| 717 | assert(LegalInstrNumber < IllegalInstrNumber && |
| 718 | "Instruction mapping overflow!"); |
| 719 | |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 720 | assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() && |
| 721 | "IllegalInstrNumber cannot be DenseMap tombstone or empty key!"); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 722 | |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 723 | assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() && |
| 724 | "IllegalInstrNumber cannot be DenseMap tombstone or empty key!"); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 725 | |
| 726 | return MINumber; |
| 727 | } |
| 728 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 729 | /// Transforms a \p MachineBasicBlock into a \p vector of \p unsigneds |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 730 | /// and appends it to \p UnsignedVec and \p InstrList. |
| 731 | /// |
| 732 | /// Two instructions are assigned the same integer if they are identical. |
| 733 | /// If an instruction is deemed unsafe to outline, then it will be assigned an |
| 734 | /// unique integer. The resulting mapping is placed into a suffix tree and |
| 735 | /// queried for candidates. |
| 736 | /// |
| 737 | /// \param MBB The \p MachineBasicBlock to be translated into integers. |
Eli Friedman | da08078 | 2018-08-01 00:37:20 +0000 | [diff] [blame] | 738 | /// \param TII \p TargetInstrInfo for the function. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 739 | void convertToUnsignedVec(MachineBasicBlock &MBB, |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 740 | const TargetInstrInfo &TII) { |
Alexander Kornienko | 3635c89 | 2018-11-13 16:41:05 +0000 | [diff] [blame] | 741 | unsigned Flags = 0; |
Jessica Paquette | 82d9c0a | 2018-11-12 23:51:32 +0000 | [diff] [blame] | 742 | |
| 743 | // Don't even map in this case. |
| 744 | if (!TII.isMBBSafeToOutlineFrom(MBB, Flags)) |
| 745 | return; |
| 746 | |
Jessica Paquette | cad864d | 2018-11-13 23:01:34 +0000 | [diff] [blame] | 747 | // Store info for the MBB for later outlining. |
| 748 | MBBFlagsMap[&MBB] = Flags; |
| 749 | |
Jessica Paquette | c991cf3 | 2018-11-01 23:09:06 +0000 | [diff] [blame] | 750 | MachineBasicBlock::iterator It = MBB.begin(); |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 751 | |
| 752 | // The number of instructions in this block that will be considered for |
| 753 | // outlining. |
| 754 | unsigned NumLegalInBlock = 0; |
| 755 | |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 756 | // True if we have at least two legal instructions which aren't separated |
| 757 | // by an illegal instruction. |
| 758 | bool HaveLegalRange = false; |
| 759 | |
| 760 | // True if we can perform outlining given the last mapped (non-invisible) |
| 761 | // instruction. This lets us know if we have a legal range. |
| 762 | bool CanOutlineWithPrevInstr = false; |
| 763 | |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 764 | // FIXME: Should this all just be handled in the target, rather than using |
| 765 | // repeated calls to getOutliningType? |
| 766 | std::vector<unsigned> UnsignedVecForMBB; |
| 767 | std::vector<MachineBasicBlock::iterator> InstrListForMBB; |
| 768 | |
Jessica Paquette | c991cf3 | 2018-11-01 23:09:06 +0000 | [diff] [blame] | 769 | for (MachineBasicBlock::iterator Et = MBB.end(); It != Et; It++) { |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 770 | // Keep track of where this instruction is in the module. |
Jessica Paquette | 3291e73 | 2018-01-09 00:26:18 +0000 | [diff] [blame] | 771 | switch (TII.getOutliningType(It, Flags)) { |
Jessica Paquette | aa08732 | 2018-06-04 21:14:16 +0000 | [diff] [blame] | 772 | case InstrType::Illegal: |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 773 | mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, |
| 774 | UnsignedVecForMBB, InstrListForMBB); |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 775 | break; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 776 | |
Jessica Paquette | aa08732 | 2018-06-04 21:14:16 +0000 | [diff] [blame] | 777 | case InstrType::Legal: |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 778 | mapToLegalUnsigned(It, CanOutlineWithPrevInstr, HaveLegalRange, |
| 779 | NumLegalInBlock, UnsignedVecForMBB, InstrListForMBB); |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 780 | break; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 781 | |
Jessica Paquette | aa08732 | 2018-06-04 21:14:16 +0000 | [diff] [blame] | 782 | case InstrType::LegalTerminator: |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 783 | mapToLegalUnsigned(It, CanOutlineWithPrevInstr, HaveLegalRange, |
| 784 | NumLegalInBlock, UnsignedVecForMBB, InstrListForMBB); |
Jessica Paquette | c991cf3 | 2018-11-01 23:09:06 +0000 | [diff] [blame] | 785 | // The instruction also acts as a terminator, so we have to record that |
| 786 | // in the string. |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 787 | mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB, |
| 788 | InstrListForMBB); |
Eli Friedman | 042dc9e | 2018-05-22 19:11:06 +0000 | [diff] [blame] | 789 | break; |
| 790 | |
Jessica Paquette | aa08732 | 2018-06-04 21:14:16 +0000 | [diff] [blame] | 791 | case InstrType::Invisible: |
Jessica Paquette | c991cf3 | 2018-11-01 23:09:06 +0000 | [diff] [blame] | 792 | // Normally this is set by mapTo(Blah)Unsigned, but we just want to |
| 793 | // skip this instruction. So, unset the flag here. |
Jessica Paquette | bd72988 | 2018-09-17 18:40:21 +0000 | [diff] [blame] | 794 | AddedIllegalLastTime = false; |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 795 | break; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 799 | // Are there enough legal instructions in the block for outlining to be |
| 800 | // possible? |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 801 | if (HaveLegalRange) { |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 802 | // After we're done every insertion, uniquely terminate this part of the |
| 803 | // "string". This makes sure we won't match across basic block or function |
| 804 | // boundaries since the "end" is encoded uniquely and thus appears in no |
| 805 | // repeated substring. |
Jessica Paquette | c4cf775 | 2018-11-08 00:33:38 +0000 | [diff] [blame] | 806 | mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB, |
| 807 | InstrListForMBB); |
Jessica Paquette | 267d266 | 2018-11-08 00:02:11 +0000 | [diff] [blame] | 808 | InstrList.insert(InstrList.end(), InstrListForMBB.begin(), |
| 809 | InstrListForMBB.end()); |
| 810 | UnsignedVec.insert(UnsignedVec.end(), UnsignedVecForMBB.begin(), |
| 811 | UnsignedVecForMBB.end()); |
| 812 | } |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | InstructionMapper() { |
| 816 | // Make sure that the implementation of DenseMapInfo<unsigned> hasn't |
| 817 | // changed. |
| 818 | assert(DenseMapInfo<unsigned>::getEmptyKey() == (unsigned)-1 && |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 819 | "DenseMapInfo<unsigned>'s empty key isn't -1!"); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 820 | assert(DenseMapInfo<unsigned>::getTombstoneKey() == (unsigned)-2 && |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 821 | "DenseMapInfo<unsigned>'s tombstone key isn't -2!"); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 822 | } |
| 823 | }; |
| 824 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 825 | /// An interprocedural pass which finds repeated sequences of |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 826 | /// instructions and replaces them with calls to functions. |
| 827 | /// |
| 828 | /// Each instruction is mapped to an unsigned integer and placed in a string. |
| 829 | /// The resulting mapping is then placed in a \p SuffixTree. The \p SuffixTree |
| 830 | /// is then repeatedly queried for repeated sequences of instructions. Each |
| 831 | /// non-overlapping repeated sequence is then placed in its own |
| 832 | /// \p MachineFunction and each instance is then replaced with a call to that |
| 833 | /// function. |
| 834 | struct MachineOutliner : public ModulePass { |
| 835 | |
| 836 | static char ID; |
| 837 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 838 | /// Set to true if the outliner should consider functions with |
Jessica Paquette | 1359384 | 2017-10-07 00:16:34 +0000 | [diff] [blame] | 839 | /// linkonceodr linkage. |
| 840 | bool OutlineFromLinkOnceODRs = false; |
| 841 | |
Jessica Paquette | 8bda188 | 2018-06-30 03:56:03 +0000 | [diff] [blame] | 842 | /// Set to true if the outliner should run on all functions in the module |
| 843 | /// considered safe for outlining. |
| 844 | /// Set to true by default for compatibility with llc's -run-pass option. |
| 845 | /// Set when the pass is constructed in TargetPassConfig. |
| 846 | bool RunOnAllFunctions = true; |
| 847 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 848 | StringRef getPassName() const override { return "Machine Outliner"; } |
| 849 | |
| 850 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 851 | AU.addRequired<MachineModuleInfo>(); |
| 852 | AU.addPreserved<MachineModuleInfo>(); |
| 853 | AU.setPreservesAll(); |
| 854 | ModulePass::getAnalysisUsage(AU); |
| 855 | } |
| 856 | |
Jessica Paquette | 1eca23b | 2018-04-19 22:17:07 +0000 | [diff] [blame] | 857 | MachineOutliner() : ModulePass(ID) { |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 858 | initializeMachineOutlinerPass(*PassRegistry::getPassRegistry()); |
| 859 | } |
| 860 | |
Jessica Paquette | 1cc52a0 | 2018-07-24 17:37:28 +0000 | [diff] [blame] | 861 | /// Remark output explaining that not outlining a set of candidates would be |
| 862 | /// better than outlining that set. |
| 863 | void emitNotOutliningCheaperRemark( |
| 864 | unsigned StringLen, std::vector<Candidate> &CandidatesForRepeatedSeq, |
| 865 | OutlinedFunction &OF); |
| 866 | |
Jessica Paquette | 58e706a | 2018-07-24 20:20:45 +0000 | [diff] [blame] | 867 | /// Remark output explaining that a function was outlined. |
| 868 | void emitOutlinedFunctionRemark(OutlinedFunction &OF); |
| 869 | |
Jessica Paquette | ce3a2dc | 2018-12-05 23:39:07 +0000 | [diff] [blame] | 870 | /// Find all repeated substrings that satisfy the outlining cost model by |
| 871 | /// constructing a suffix tree. |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 872 | /// |
| 873 | /// If a substring appears at least twice, then it must be represented by |
Jessica Paquette | 1cc52a0 | 2018-07-24 17:37:28 +0000 | [diff] [blame] | 874 | /// an internal node which appears in at least two suffixes. Each suffix |
| 875 | /// is represented by a leaf node. To do this, we visit each internal node |
| 876 | /// in the tree, using the leaf children of each internal node. If an |
| 877 | /// internal node represents a beneficial substring, then we use each of |
| 878 | /// its leaf children to find the locations of its substring. |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 879 | /// |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 880 | /// \param Mapper Contains outlining mapping information. |
Jessica Paquette | 1cc52a0 | 2018-07-24 17:37:28 +0000 | [diff] [blame] | 881 | /// \param[out] FunctionList Filled with a list of \p OutlinedFunctions |
| 882 | /// each type of candidate. |
Jessica Paquette | ce3a2dc | 2018-12-05 23:39:07 +0000 | [diff] [blame] | 883 | void findCandidates(InstructionMapper &Mapper, |
| 884 | std::vector<OutlinedFunction> &FunctionList); |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 885 | |
Jessica Paquette | 4ae3b71 | 2018-12-05 22:50:26 +0000 | [diff] [blame] | 886 | /// Replace the sequences of instructions represented by \p OutlinedFunctions |
| 887 | /// with calls to functions. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 888 | /// |
| 889 | /// \param M The module we are outlining from. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 890 | /// \param FunctionList A list of functions to be inserted into the module. |
| 891 | /// \param Mapper Contains the instruction mappings for the module. |
Jessica Paquette | 4ae3b71 | 2018-12-05 22:50:26 +0000 | [diff] [blame] | 892 | bool outline(Module &M, std::vector<OutlinedFunction> &FunctionList, |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 893 | InstructionMapper &Mapper); |
| 894 | |
| 895 | /// Creates a function for \p OF and inserts it into the module. |
Jessica Paquette | e18d6ff | 2018-12-05 23:24:22 +0000 | [diff] [blame] | 896 | MachineFunction *createOutlinedFunction(Module &M, OutlinedFunction &OF, |
Jessica Paquette | a3eb0fa | 2018-11-07 18:36:43 +0000 | [diff] [blame] | 897 | InstructionMapper &Mapper, |
| 898 | unsigned Name); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 899 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 900 | /// Construct a suffix tree on the instructions in \p M and outline repeated |
| 901 | /// strings from that tree. |
| 902 | bool runOnModule(Module &M) override; |
Jessica Paquette | aa08732 | 2018-06-04 21:14:16 +0000 | [diff] [blame] | 903 | |
| 904 | /// Return a DISubprogram for OF if one exists, and null otherwise. Helper |
| 905 | /// function for remark emission. |
| 906 | DISubprogram *getSubprogramOrNull(const OutlinedFunction &OF) { |
| 907 | DISubprogram *SP; |
Jessica Paquette | e18d6ff | 2018-12-05 23:24:22 +0000 | [diff] [blame] | 908 | for (const Candidate &C : OF.Candidates) |
| 909 | if (C.getMF() && (SP = C.getMF()->getFunction().getSubprogram())) |
Jessica Paquette | aa08732 | 2018-06-04 21:14:16 +0000 | [diff] [blame] | 910 | return SP; |
| 911 | return nullptr; |
| 912 | } |
Jessica Paquette | 050d1ac | 2018-09-11 16:33:46 +0000 | [diff] [blame] | 913 | |
| 914 | /// Populate and \p InstructionMapper with instruction-to-integer mappings. |
| 915 | /// These are used to construct a suffix tree. |
| 916 | void populateMapper(InstructionMapper &Mapper, Module &M, |
| 917 | MachineModuleInfo &MMI); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 918 | |
Jessica Paquette | 2386eab | 2018-09-11 23:05:34 +0000 | [diff] [blame] | 919 | /// Initialize information necessary to output a size remark. |
| 920 | /// FIXME: This should be handled by the pass manager, not the outliner. |
| 921 | /// FIXME: This is nearly identical to the initSizeRemarkInfo in the legacy |
| 922 | /// pass manager. |
| 923 | void initSizeRemarkInfo( |
| 924 | const Module &M, const MachineModuleInfo &MMI, |
| 925 | StringMap<unsigned> &FunctionToInstrCount); |
| 926 | |
| 927 | /// Emit the remark. |
| 928 | // FIXME: This should be handled by the pass manager, not the outliner. |
| 929 | void emitInstrCountChangedRemark( |
| 930 | const Module &M, const MachineModuleInfo &MMI, |
| 931 | const StringMap<unsigned> &FunctionToInstrCount); |
| 932 | }; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 933 | } // Anonymous namespace. |
| 934 | |
| 935 | char MachineOutliner::ID = 0; |
| 936 | |
| 937 | namespace llvm { |
Jessica Paquette | 8bda188 | 2018-06-30 03:56:03 +0000 | [diff] [blame] | 938 | ModulePass *createMachineOutlinerPass(bool RunOnAllFunctions) { |
| 939 | MachineOutliner *OL = new MachineOutliner(); |
| 940 | OL->RunOnAllFunctions = RunOnAllFunctions; |
| 941 | return OL; |
Jessica Paquette | 1359384 | 2017-10-07 00:16:34 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 944 | } // namespace llvm |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 945 | |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 946 | INITIALIZE_PASS(MachineOutliner, DEBUG_TYPE, "Machine Function Outliner", false, |
| 947 | false) |
| 948 | |
Jessica Paquette | 1cc52a0 | 2018-07-24 17:37:28 +0000 | [diff] [blame] | 949 | void MachineOutliner::emitNotOutliningCheaperRemark( |
| 950 | unsigned StringLen, std::vector<Candidate> &CandidatesForRepeatedSeq, |
| 951 | OutlinedFunction &OF) { |
Jessica Paquette | c991cf3 | 2018-11-01 23:09:06 +0000 | [diff] [blame] | 952 | // FIXME: Right now, we arbitrarily choose some Candidate from the |
| 953 | // OutlinedFunction. This isn't necessarily fixed, nor does it have to be. |
| 954 | // We should probably sort these by function name or something to make sure |
| 955 | // the remarks are stable. |
Jessica Paquette | 1cc52a0 | 2018-07-24 17:37:28 +0000 | [diff] [blame] | 956 | Candidate &C = CandidatesForRepeatedSeq.front(); |
| 957 | MachineOptimizationRemarkEmitter MORE(*(C.getMF()), nullptr); |
| 958 | MORE.emit([&]() { |
| 959 | MachineOptimizationRemarkMissed R(DEBUG_TYPE, "NotOutliningCheaper", |
| 960 | C.front()->getDebugLoc(), C.getMBB()); |
| 961 | R << "Did not outline " << NV("Length", StringLen) << " instructions" |
| 962 | << " from " << NV("NumOccurrences", CandidatesForRepeatedSeq.size()) |
| 963 | << " locations." |
| 964 | << " Bytes from outlining all occurrences (" |
| 965 | << NV("OutliningCost", OF.getOutliningCost()) << ")" |
| 966 | << " >= Unoutlined instruction bytes (" |
| 967 | << NV("NotOutliningCost", OF.getNotOutlinedCost()) << ")" |
| 968 | << " (Also found at: "; |
| 969 | |
| 970 | // Tell the user the other places the candidate was found. |
| 971 | for (unsigned i = 1, e = CandidatesForRepeatedSeq.size(); i < e; i++) { |
| 972 | R << NV((Twine("OtherStartLoc") + Twine(i)).str(), |
| 973 | CandidatesForRepeatedSeq[i].front()->getDebugLoc()); |
| 974 | if (i != e - 1) |
| 975 | R << ", "; |
| 976 | } |
| 977 | |
| 978 | R << ")"; |
| 979 | return R; |
| 980 | }); |
| 981 | } |
| 982 | |
Jessica Paquette | 58e706a | 2018-07-24 20:20:45 +0000 | [diff] [blame] | 983 | void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction &OF) { |
| 984 | MachineBasicBlock *MBB = &*OF.MF->begin(); |
| 985 | MachineOptimizationRemarkEmitter MORE(*OF.MF, nullptr); |
| 986 | MachineOptimizationRemark R(DEBUG_TYPE, "OutlinedFunction", |
| 987 | MBB->findDebugLoc(MBB->begin()), MBB); |
| 988 | R << "Saved " << NV("OutliningBenefit", OF.getBenefit()) << " bytes by " |
Jessica Paquette | 34b618b | 2018-12-05 17:57:33 +0000 | [diff] [blame] | 989 | << "outlining " << NV("Length", OF.getNumInstrs()) << " instructions " |
Jessica Paquette | 58e706a | 2018-07-24 20:20:45 +0000 | [diff] [blame] | 990 | << "from " << NV("NumOccurrences", OF.getOccurrenceCount()) |
| 991 | << " locations. " |
| 992 | << "(Found at: "; |
| 993 | |
| 994 | // Tell the user the other places the candidate was found. |
| 995 | for (size_t i = 0, e = OF.Candidates.size(); i < e; i++) { |
| 996 | |
Jessica Paquette | 58e706a | 2018-07-24 20:20:45 +0000 | [diff] [blame] | 997 | R << NV((Twine("StartLoc") + Twine(i)).str(), |
Jessica Paquette | e18d6ff | 2018-12-05 23:24:22 +0000 | [diff] [blame] | 998 | OF.Candidates[i].front()->getDebugLoc()); |
Jessica Paquette | 58e706a | 2018-07-24 20:20:45 +0000 | [diff] [blame] | 999 | if (i != e - 1) |
| 1000 | R << ", "; |
| 1001 | } |
| 1002 | |
| 1003 | R << ")"; |
| 1004 | |
| 1005 | MORE.emit(R); |
| 1006 | } |
| 1007 | |
Jessica Paquette | ce3a2dc | 2018-12-05 23:39:07 +0000 | [diff] [blame] | 1008 | void |
| 1009 | MachineOutliner::findCandidates(InstructionMapper &Mapper, |
| 1010 | std::vector<OutlinedFunction> &FunctionList) { |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 1011 | FunctionList.clear(); |
Jessica Paquette | ce3a2dc | 2018-12-05 23:39:07 +0000 | [diff] [blame] | 1012 | SuffixTree ST(Mapper.UnsignedVec); |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 1013 | |
Jessica Paquette | 4e54ef8 | 2018-11-06 21:46:41 +0000 | [diff] [blame] | 1014 | // First, find dall of the repeated substrings in the tree of minimum length |
| 1015 | // 2. |
Jessica Paquette | d4e7d07 | 2018-12-06 00:04:03 +0000 | [diff] [blame] | 1016 | std::vector<Candidate> CandidatesForRepeatedSeq; |
Jessica Paquette | a409cc9 | 2018-11-07 19:20:55 +0000 | [diff] [blame] | 1017 | for (auto It = ST.begin(), Et = ST.end(); It != Et; ++It) { |
Jessica Paquette | d4e7d07 | 2018-12-06 00:04:03 +0000 | [diff] [blame] | 1018 | CandidatesForRepeatedSeq.clear(); |
Jessica Paquette | a409cc9 | 2018-11-07 19:20:55 +0000 | [diff] [blame] | 1019 | SuffixTree::RepeatedSubstring RS = *It; |
Jessica Paquette | 4e54ef8 | 2018-11-06 21:46:41 +0000 | [diff] [blame] | 1020 | unsigned StringLen = RS.Length; |
| 1021 | for (const unsigned &StartIdx : RS.StartIndices) { |
| 1022 | unsigned EndIdx = StartIdx + StringLen - 1; |
| 1023 | // Trick: Discard some candidates that would be incompatible with the |
| 1024 | // ones we've already found for this sequence. This will save us some |
| 1025 | // work in candidate selection. |
| 1026 | // |
| 1027 | // If two candidates overlap, then we can't outline them both. This |
| 1028 | // happens when we have candidates that look like, say |
| 1029 | // |
| 1030 | // AA (where each "A" is an instruction). |
| 1031 | // |
| 1032 | // We might have some portion of the module that looks like this: |
| 1033 | // AAAAAA (6 A's) |
| 1034 | // |
| 1035 | // In this case, there are 5 different copies of "AA" in this range, but |
| 1036 | // at most 3 can be outlined. If only outlining 3 of these is going to |
| 1037 | // be unbeneficial, then we ought to not bother. |
| 1038 | // |
| 1039 | // Note that two things DON'T overlap when they look like this: |
| 1040 | // start1...end1 .... start2...end2 |
| 1041 | // That is, one must either |
| 1042 | // * End before the other starts |
| 1043 | // * Start after the other ends |
| 1044 | if (std::all_of( |
| 1045 | CandidatesForRepeatedSeq.begin(), CandidatesForRepeatedSeq.end(), |
| 1046 | [&StartIdx, &EndIdx](const Candidate &C) { |
| 1047 | return (EndIdx < C.getStartIdx() || StartIdx > C.getEndIdx()); |
| 1048 | })) { |
| 1049 | // It doesn't overlap with anything, so we can outline it. |
| 1050 | // Each sequence is over [StartIt, EndIt]. |
| 1051 | // Save the candidate and its location. |
Jessica Paquette | d87f544 | 2017-07-29 02:55:46 +0000 | [diff] [blame] | 1052 | |
Jessica Paquette | 4e54ef8 | 2018-11-06 21:46:41 +0000 | [diff] [blame] | 1053 | MachineBasicBlock::iterator StartIt = Mapper.InstrList[StartIdx]; |
| 1054 | MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx]; |
Jessica Paquette | cad864d | 2018-11-13 23:01:34 +0000 | [diff] [blame] | 1055 | MachineBasicBlock *MBB = StartIt->getParent(); |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 1056 | |
Jessica Paquette | 4e54ef8 | 2018-11-06 21:46:41 +0000 | [diff] [blame] | 1057 | CandidatesForRepeatedSeq.emplace_back(StartIdx, StringLen, StartIt, |
Jessica Paquette | cad864d | 2018-11-13 23:01:34 +0000 | [diff] [blame] | 1058 | EndIt, MBB, FunctionList.size(), |
| 1059 | Mapper.MBBFlagsMap[MBB]); |
Jessica Paquette | 809d708 | 2017-07-28 03:21:58 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
Jessica Paquette | acc15e1 | 2017-10-03 20:32:55 +0000 | [diff] [blame] | 1063 | // We've found something we might want to outline. |
| 1064 | // Create an OutlinedFunction to store it and check if it'd be beneficial |
| 1065 | // to outline. |
Jessica Paquette | ddb039a | 2018-11-15 00:02:24 +0000 | [diff] [blame] | 1066 | if (CandidatesForRepeatedSeq.size() < 2) |
Eli Friedman | da08078 | 2018-08-01 00:37:20 +0000 | [diff] [blame] | 1067 | continue; |
| 1068 | |
| 1069 | // Arbitrarily choose a TII from the first candidate. |
| 1070 | // FIXME: Should getOutliningCandidateInfo move to TargetMachine? |
| 1071 | const TargetInstrInfo *TII = |
| 1072 | CandidatesForRepeatedSeq[0].getMF()->getSubtarget().getInstrInfo(); |
| 1073 | |
Jessica Paquette | 9d93c60 | 2018-07-27 18:21:57 +0000 | [diff] [blame] | 1074 | OutlinedFunction OF = |
Eli Friedman | da08078 | 2018-08-01 00:37:20 +0000 | [diff] [blame] | 1075 | TII->getOutliningCandidateInfo(CandidatesForRepeatedSeq); |
Jessica Paquette | 9d93c60 | 2018-07-27 18:21:57 +0000 | [diff] [blame] | 1076 | |
Jessica Paquette | b2d53c5 | 2018-11-13 22:16:27 +0000 | [diff] [blame] | 1077 | // If we deleted too many candidates, then there's nothing worth outlining. |
| 1078 | // FIXME: This should take target-specified instruction sizes into account. |
| 1079 | if (OF.Candidates.size() < 2) |
Jessica Paquette | 9d93c60 | 2018-07-27 18:21:57 +0000 | [diff] [blame] | 1080 | continue; |
| 1081 | |
Jessica Paquette | ffe4abc | 2017-08-31 21:02:45 +0000 | [diff] [blame] | 1082 | // Is it better to outline this candidate than not? |
Jessica Paquette | f94d1d2 | 2018-07-24 17:36:13 +0000 | [diff] [blame] | 1083 | if (OF.getBenefit() < 1) { |
Jessica Paquette | 1cc52a0 | 2018-07-24 17:37:28 +0000 | [diff] [blame] | 1084 | emitNotOutliningCheaperRemark(StringLen, CandidatesForRepeatedSeq, OF); |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 1085 | continue; |
Jessica Paquette | ffe4abc | 2017-08-31 21:02:45 +0000 | [diff] [blame] | 1086 | } |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 1087 | |
Jessica Paquette | acc15e1 | 2017-10-03 20:32:55 +0000 | [diff] [blame] | 1088 | FunctionList.push_back(OF); |
Jessica Paquette | 78681be | 2017-07-27 23:24:43 +0000 | [diff] [blame] | 1089 | } |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | MachineFunction * |
Jessica Paquette | e18d6ff | 2018-12-05 23:24:22 +0000 | [diff] [blame] | 1093 | MachineOutliner::createOutlinedFunction(Module &M, OutlinedFunction &OF, |
Jessica Paquette | a3eb0fa | 2018-11-07 18:36:43 +0000 | [diff] [blame] | 1094 | InstructionMapper &Mapper, |
| 1095 | unsigned Name) { |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1096 | |
| 1097 | // Create the function name. This should be unique. For now, just hash the |
| 1098 | // module name and include it in the function name plus the number of this |
| 1099 | // function. |
| 1100 | std::ostringstream NameStream; |
Jessica Paquette | a3eb0fa | 2018-11-07 18:36:43 +0000 | [diff] [blame] | 1101 | // FIXME: We should have a better naming scheme. This should be stable, |
| 1102 | // regardless of changes to the outliner's cost model/traversal order. |
| 1103 | NameStream << "OUTLINED_FUNCTION_" << Name; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Create the function using an IR-level function. |
| 1106 | LLVMContext &C = M.getContext(); |
James Y Knight | f47d6b3 | 2019-01-31 20:35:56 +0000 | [diff] [blame^] | 1107 | Function *F = |
| 1108 | Function::Create(FunctionType::get(Type::getVoidTy(C), false), |
| 1109 | Function::ExternalLinkage, NameStream.str(), M); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1110 | |
| 1111 | // NOTE: If this is linkonceodr, then we can take advantage of linker deduping |
| 1112 | // which gives us better results when we outline from linkonceodr functions. |
Jessica Paquette | d506bf8 | 2018-04-03 21:36:00 +0000 | [diff] [blame] | 1113 | F->setLinkage(GlobalValue::InternalLinkage); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1114 | F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
| 1115 | |
Eli Friedman | 25bef20 | 2018-05-15 23:36:46 +0000 | [diff] [blame] | 1116 | // FIXME: Set nounwind, so we don't generate eh_frame? Haven't verified it's |
| 1117 | // necessary. |
| 1118 | |
| 1119 | // Set optsize/minsize, so we don't insert padding between outlined |
| 1120 | // functions. |
| 1121 | F->addFnAttr(Attribute::OptimizeForSize); |
| 1122 | F->addFnAttr(Attribute::MinSize); |
| 1123 | |
Jessica Paquette | e3932ee | 2018-10-29 20:27:07 +0000 | [diff] [blame] | 1124 | // Include target features from an arbitrary candidate for the outlined |
| 1125 | // function. This makes sure the outlined function knows what kinds of |
| 1126 | // instructions are going into it. This is fine, since all parent functions |
| 1127 | // must necessarily support the instructions that are in the outlined region. |
Jessica Paquette | e18d6ff | 2018-12-05 23:24:22 +0000 | [diff] [blame] | 1128 | Candidate &FirstCand = OF.Candidates.front(); |
Jessica Paquette | 34b618b | 2018-12-05 17:57:33 +0000 | [diff] [blame] | 1129 | const Function &ParentFn = FirstCand.getMF()->getFunction(); |
Jessica Paquette | e3932ee | 2018-10-29 20:27:07 +0000 | [diff] [blame] | 1130 | if (ParentFn.hasFnAttribute("target-features")) |
| 1131 | F->addFnAttr(ParentFn.getFnAttribute("target-features")); |
| 1132 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1133 | BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F); |
| 1134 | IRBuilder<> Builder(EntryBB); |
| 1135 | Builder.CreateRetVoid(); |
| 1136 | |
| 1137 | MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>(); |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 1138 | MachineFunction &MF = MMI.getOrCreateMachineFunction(*F); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1139 | MachineBasicBlock &MBB = *MF.CreateMachineBasicBlock(); |
| 1140 | const TargetSubtargetInfo &STI = MF.getSubtarget(); |
| 1141 | const TargetInstrInfo &TII = *STI.getInstrInfo(); |
| 1142 | |
| 1143 | // Insert the new function into the module. |
| 1144 | MF.insert(MF.begin(), &MBB); |
| 1145 | |
Jessica Paquette | 34b618b | 2018-12-05 17:57:33 +0000 | [diff] [blame] | 1146 | for (auto I = FirstCand.front(), E = std::next(FirstCand.back()); I != E; |
| 1147 | ++I) { |
| 1148 | MachineInstr *NewMI = MF.CloneMachineInstr(&*I); |
Chandler Carruth | c73c030 | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 1149 | NewMI->dropMemRefs(MF); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1150 | |
| 1151 | // Don't keep debug information for outlined instructions. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1152 | NewMI->setDebugLoc(DebugLoc()); |
| 1153 | MBB.insert(MBB.end(), NewMI); |
| 1154 | } |
| 1155 | |
Jessica Paquette | 69f517d | 2018-07-24 20:13:10 +0000 | [diff] [blame] | 1156 | TII.buildOutlinedFrame(MBB, MF, OF); |
Jessica Paquette | 729e686 | 2018-01-18 00:00:58 +0000 | [diff] [blame] | 1157 | |
Jessica Paquette | cc06a78 | 2018-09-20 18:53:53 +0000 | [diff] [blame] | 1158 | // Outlined functions shouldn't preserve liveness. |
| 1159 | MF.getProperties().reset(MachineFunctionProperties::Property::TracksLiveness); |
| 1160 | MF.getRegInfo().freezeReservedRegs(MF); |
| 1161 | |
Jessica Paquette | a499c3c | 2018-01-19 21:21:49 +0000 | [diff] [blame] | 1162 | // If there's a DISubprogram associated with this outlined function, then |
| 1163 | // emit debug info for the outlined function. |
Jessica Paquette | aa08732 | 2018-06-04 21:14:16 +0000 | [diff] [blame] | 1164 | if (DISubprogram *SP = getSubprogramOrNull(OF)) { |
Jessica Paquette | a499c3c | 2018-01-19 21:21:49 +0000 | [diff] [blame] | 1165 | // We have a DISubprogram. Get its DICompileUnit. |
| 1166 | DICompileUnit *CU = SP->getUnit(); |
| 1167 | DIBuilder DB(M, true, CU); |
| 1168 | DIFile *Unit = SP->getFile(); |
| 1169 | Mangler Mg; |
Jessica Paquette | cc06a78 | 2018-09-20 18:53:53 +0000 | [diff] [blame] | 1170 | // Get the mangled name of the function for the linkage name. |
| 1171 | std::string Dummy; |
| 1172 | llvm::raw_string_ostream MangledNameStream(Dummy); |
| 1173 | Mg.getNameWithPrefix(MangledNameStream, F, false); |
Jessica Paquette | a499c3c | 2018-01-19 21:21:49 +0000 | [diff] [blame] | 1174 | |
Jessica Paquette | cc06a78 | 2018-09-20 18:53:53 +0000 | [diff] [blame] | 1175 | DISubprogram *OutlinedSP = DB.createFunction( |
| 1176 | Unit /* Context */, F->getName(), StringRef(MangledNameStream.str()), |
| 1177 | Unit /* File */, |
| 1178 | 0 /* Line 0 is reserved for compiler-generated code. */, |
| 1179 | DB.createSubroutineType(DB.getOrCreateTypeArray(None)), /* void type */ |
Paul Robinson | cda5421 | 2018-11-19 18:29:28 +0000 | [diff] [blame] | 1180 | 0, /* Line 0 is reserved for compiler-generated code. */ |
Jessica Paquette | cc06a78 | 2018-09-20 18:53:53 +0000 | [diff] [blame] | 1181 | DINode::DIFlags::FlagArtificial /* Compiler-generated code. */, |
Paul Robinson | cda5421 | 2018-11-19 18:29:28 +0000 | [diff] [blame] | 1182 | /* Outlined code is optimized code by definition. */ |
| 1183 | DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized); |
Jessica Paquette | a499c3c | 2018-01-19 21:21:49 +0000 | [diff] [blame] | 1184 | |
Jessica Paquette | cc06a78 | 2018-09-20 18:53:53 +0000 | [diff] [blame] | 1185 | // Don't add any new variables to the subprogram. |
| 1186 | DB.finalizeSubprogram(OutlinedSP); |
Jessica Paquette | a499c3c | 2018-01-19 21:21:49 +0000 | [diff] [blame] | 1187 | |
Jessica Paquette | cc06a78 | 2018-09-20 18:53:53 +0000 | [diff] [blame] | 1188 | // Attach subprogram to the function. |
| 1189 | F->setSubprogram(OutlinedSP); |
Jessica Paquette | a499c3c | 2018-01-19 21:21:49 +0000 | [diff] [blame] | 1190 | // We're done with the DIBuilder. |
| 1191 | DB.finalize(); |
| 1192 | } |
| 1193 | |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1194 | return &MF; |
| 1195 | } |
| 1196 | |
Jessica Paquette | 4ae3b71 | 2018-12-05 22:50:26 +0000 | [diff] [blame] | 1197 | bool MachineOutliner::outline(Module &M, |
| 1198 | std::vector<OutlinedFunction> &FunctionList, |
| 1199 | InstructionMapper &Mapper) { |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1200 | |
| 1201 | bool OutlinedSomething = false; |
Jessica Paquette | a3eb0fa | 2018-11-07 18:36:43 +0000 | [diff] [blame] | 1202 | |
| 1203 | // Number to append to the current outlined function. |
| 1204 | unsigned OutlinedFunctionNum = 0; |
| 1205 | |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1206 | // Sort by benefit. The most beneficial functions should be outlined first. |
| 1207 | std::stable_sort( |
| 1208 | FunctionList.begin(), FunctionList.end(), |
| 1209 | [](const OutlinedFunction &LHS, const OutlinedFunction &RHS) { |
| 1210 | return LHS.getBenefit() > RHS.getBenefit(); |
| 1211 | }); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1212 | |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1213 | // Walk over each function, outlining them as we go along. Functions are |
| 1214 | // outlined greedily, based off the sort above. |
| 1215 | for (OutlinedFunction &OF : FunctionList) { |
| 1216 | // If we outlined something that overlapped with a candidate in a previous |
| 1217 | // step, then we can't outline from it. |
Jessica Paquette | e18d6ff | 2018-12-05 23:24:22 +0000 | [diff] [blame] | 1218 | erase_if(OF.Candidates, [&Mapper](Candidate &C) { |
Jessica Paquette | d9d9309 | 2018-12-05 22:47:25 +0000 | [diff] [blame] | 1219 | return std::any_of( |
Jessica Paquette | e18d6ff | 2018-12-05 23:24:22 +0000 | [diff] [blame] | 1220 | Mapper.UnsignedVec.begin() + C.getStartIdx(), |
| 1221 | Mapper.UnsignedVec.begin() + C.getEndIdx() + 1, |
Jessica Paquette | d9d9309 | 2018-12-05 22:47:25 +0000 | [diff] [blame] | 1222 | [](unsigned I) { return (I == static_cast<unsigned>(-1)); }); |
Jessica Paquette | 235d877 | 2018-12-05 22:27:38 +0000 | [diff] [blame] | 1223 | }); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1224 | |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1225 | // If we made it unbeneficial to outline this function, skip it. |
Jessica Paquette | 85af63d | 2017-10-17 19:03:23 +0000 | [diff] [blame] | 1226 | if (OF.getBenefit() < 1) |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1227 | continue; |
| 1228 | |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1229 | // It's beneficial. Create the function and outline its sequence's |
| 1230 | // occurrences. |
| 1231 | OF.MF = createOutlinedFunction(M, OF, Mapper, OutlinedFunctionNum); |
| 1232 | emitOutlinedFunctionRemark(OF); |
| 1233 | FunctionsCreated++; |
| 1234 | OutlinedFunctionNum++; // Created a function, move to the next name. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1235 | MachineFunction *MF = OF.MF; |
| 1236 | const TargetSubtargetInfo &STI = MF->getSubtarget(); |
| 1237 | const TargetInstrInfo &TII = *STI.getInstrInfo(); |
| 1238 | |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1239 | // Replace occurrences of the sequence with calls to the new function. |
Jessica Paquette | e18d6ff | 2018-12-05 23:24:22 +0000 | [diff] [blame] | 1240 | for (Candidate &C : OF.Candidates) { |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1241 | MachineBasicBlock &MBB = *C.getMBB(); |
| 1242 | MachineBasicBlock::iterator StartIt = C.front(); |
| 1243 | MachineBasicBlock::iterator EndIt = C.back(); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1244 | |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1245 | // Insert the call. |
| 1246 | auto CallInst = TII.insertOutlinedCall(M, MBB, StartIt, *MF, C); |
Jessica Paquette | 0b67249 | 2018-04-27 23:36:35 +0000 | [diff] [blame] | 1247 | |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1248 | // If the caller tracks liveness, then we need to make sure that |
| 1249 | // anything we outline doesn't break liveness assumptions. The outlined |
| 1250 | // functions themselves currently don't track liveness, but we should |
| 1251 | // make sure that the ranges we yank things out of aren't wrong. |
| 1252 | if (MBB.getParent()->getProperties().hasProperty( |
| 1253 | MachineFunctionProperties::Property::TracksLiveness)) { |
| 1254 | // Helper lambda for adding implicit def operands to the call |
| 1255 | // instruction. |
| 1256 | auto CopyDefs = [&CallInst](MachineInstr &MI) { |
| 1257 | for (MachineOperand &MOP : MI.operands()) { |
| 1258 | // Skip over anything that isn't a register. |
| 1259 | if (!MOP.isReg()) |
| 1260 | continue; |
Jessica Paquette | 0b67249 | 2018-04-27 23:36:35 +0000 | [diff] [blame] | 1261 | |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1262 | // If it's a def, add it to the call instruction. |
| 1263 | if (MOP.isDef()) |
| 1264 | CallInst->addOperand(MachineOperand::CreateReg( |
| 1265 | MOP.getReg(), true, /* isDef = true */ |
| 1266 | true /* isImp = true */)); |
| 1267 | } |
| 1268 | }; |
| 1269 | // Copy over the defs in the outlined range. |
| 1270 | // First inst in outlined range <-- Anything that's defined in this |
| 1271 | // ... .. range has to be added as an |
| 1272 | // implicit Last inst in outlined range <-- def to the call |
| 1273 | // instruction. |
| 1274 | std::for_each(CallInst, std::next(EndIt), CopyDefs); |
| 1275 | } |
| 1276 | |
| 1277 | // Erase from the point after where the call was inserted up to, and |
| 1278 | // including, the final instruction in the sequence. |
| 1279 | // Erase needs one past the end, so we need std::next there too. |
| 1280 | MBB.erase(std::next(StartIt), std::next(EndIt)); |
Jessica Paquette | 235d877 | 2018-12-05 22:27:38 +0000 | [diff] [blame] | 1281 | |
Jessica Paquette | d9d9309 | 2018-12-05 22:47:25 +0000 | [diff] [blame] | 1282 | // Keep track of what we removed by marking them all as -1. |
Jessica Paquette | 235d877 | 2018-12-05 22:27:38 +0000 | [diff] [blame] | 1283 | std::for_each(Mapper.UnsignedVec.begin() + C.getStartIdx(), |
| 1284 | Mapper.UnsignedVec.begin() + C.getEndIdx() + 1, |
Jessica Paquette | d9d9309 | 2018-12-05 22:47:25 +0000 | [diff] [blame] | 1285 | [](unsigned &I) { I = static_cast<unsigned>(-1); }); |
Jessica Paquette | 962b3ae | 2018-12-05 21:36:04 +0000 | [diff] [blame] | 1286 | OutlinedSomething = true; |
| 1287 | |
| 1288 | // Statistics. |
| 1289 | NumOutlined++; |
Jessica Paquette | 0b67249 | 2018-04-27 23:36:35 +0000 | [diff] [blame] | 1290 | } |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1293 | LLVM_DEBUG(dbgs() << "OutlinedSomething = " << OutlinedSomething << "\n";); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1294 | |
| 1295 | return OutlinedSomething; |
| 1296 | } |
| 1297 | |
Jessica Paquette | 050d1ac | 2018-09-11 16:33:46 +0000 | [diff] [blame] | 1298 | void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M, |
| 1299 | MachineModuleInfo &MMI) { |
Jessica Paquette | df82274 | 2018-03-22 21:07:09 +0000 | [diff] [blame] | 1300 | // Build instruction mappings for each function in the module. Start by |
| 1301 | // iterating over each Function in M. |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1302 | for (Function &F : M) { |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1303 | |
Jessica Paquette | df82274 | 2018-03-22 21:07:09 +0000 | [diff] [blame] | 1304 | // If there's nothing in F, then there's no reason to try and outline from |
| 1305 | // it. |
| 1306 | if (F.empty()) |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1307 | continue; |
| 1308 | |
Jessica Paquette | df82274 | 2018-03-22 21:07:09 +0000 | [diff] [blame] | 1309 | // There's something in F. Check if it has a MachineFunction associated with |
| 1310 | // it. |
| 1311 | MachineFunction *MF = MMI.getMachineFunction(F); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1312 | |
Jessica Paquette | df82274 | 2018-03-22 21:07:09 +0000 | [diff] [blame] | 1313 | // If it doesn't, then there's nothing to outline from. Move to the next |
| 1314 | // Function. |
| 1315 | if (!MF) |
| 1316 | continue; |
| 1317 | |
Eli Friedman | da08078 | 2018-08-01 00:37:20 +0000 | [diff] [blame] | 1318 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
| 1319 | |
Jessica Paquette | 8bda188 | 2018-06-30 03:56:03 +0000 | [diff] [blame] | 1320 | if (!RunOnAllFunctions && !TII->shouldOutlineFromFunctionByDefault(*MF)) |
| 1321 | continue; |
| 1322 | |
Jessica Paquette | df82274 | 2018-03-22 21:07:09 +0000 | [diff] [blame] | 1323 | // We have a MachineFunction. Ask the target if it's suitable for outlining. |
| 1324 | // If it isn't, then move on to the next Function in the module. |
| 1325 | if (!TII->isFunctionSafeToOutlineFrom(*MF, OutlineFromLinkOnceODRs)) |
| 1326 | continue; |
| 1327 | |
| 1328 | // We have a function suitable for outlining. Iterate over every |
| 1329 | // MachineBasicBlock in MF and try to map its instructions to a list of |
| 1330 | // unsigned integers. |
| 1331 | for (MachineBasicBlock &MBB : *MF) { |
| 1332 | // If there isn't anything in MBB, then there's no point in outlining from |
| 1333 | // it. |
Jessica Paquette | b320ca2 | 2018-09-20 21:53:25 +0000 | [diff] [blame] | 1334 | // If there are fewer than 2 instructions in the MBB, then it can't ever |
| 1335 | // contain something worth outlining. |
| 1336 | // FIXME: This should be based off of the maximum size in B of an outlined |
| 1337 | // call versus the size in B of the MBB. |
| 1338 | if (MBB.empty() || MBB.size() < 2) |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1339 | continue; |
| 1340 | |
Jessica Paquette | df82274 | 2018-03-22 21:07:09 +0000 | [diff] [blame] | 1341 | // Check if MBB could be the target of an indirect branch. If it is, then |
| 1342 | // we don't want to outline from it. |
| 1343 | if (MBB.hasAddressTaken()) |
| 1344 | continue; |
| 1345 | |
| 1346 | // MBB is suitable for outlining. Map it to a list of unsigneds. |
Eli Friedman | da08078 | 2018-08-01 00:37:20 +0000 | [diff] [blame] | 1347 | Mapper.convertToUnsignedVec(MBB, *TII); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } |
Jessica Paquette | 050d1ac | 2018-09-11 16:33:46 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Jessica Paquette | 2386eab | 2018-09-11 23:05:34 +0000 | [diff] [blame] | 1352 | void MachineOutliner::initSizeRemarkInfo( |
| 1353 | const Module &M, const MachineModuleInfo &MMI, |
| 1354 | StringMap<unsigned> &FunctionToInstrCount) { |
| 1355 | // Collect instruction counts for every function. We'll use this to emit |
| 1356 | // per-function size remarks later. |
| 1357 | for (const Function &F : M) { |
| 1358 | MachineFunction *MF = MMI.getMachineFunction(F); |
| 1359 | |
| 1360 | // We only care about MI counts here. If there's no MachineFunction at this |
| 1361 | // point, then there won't be after the outliner runs, so let's move on. |
| 1362 | if (!MF) |
| 1363 | continue; |
| 1364 | FunctionToInstrCount[F.getName().str()] = MF->getInstructionCount(); |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | void MachineOutliner::emitInstrCountChangedRemark( |
| 1369 | const Module &M, const MachineModuleInfo &MMI, |
| 1370 | const StringMap<unsigned> &FunctionToInstrCount) { |
| 1371 | // Iterate over each function in the module and emit remarks. |
| 1372 | // Note that we won't miss anything by doing this, because the outliner never |
| 1373 | // deletes functions. |
| 1374 | for (const Function &F : M) { |
| 1375 | MachineFunction *MF = MMI.getMachineFunction(F); |
| 1376 | |
| 1377 | // The outliner never deletes functions. If we don't have a MF here, then we |
| 1378 | // didn't have one prior to outlining either. |
| 1379 | if (!MF) |
| 1380 | continue; |
| 1381 | |
| 1382 | std::string Fname = F.getName(); |
| 1383 | unsigned FnCountAfter = MF->getInstructionCount(); |
| 1384 | unsigned FnCountBefore = 0; |
| 1385 | |
| 1386 | // Check if the function was recorded before. |
| 1387 | auto It = FunctionToInstrCount.find(Fname); |
| 1388 | |
| 1389 | // Did we have a previously-recorded size? If yes, then set FnCountBefore |
| 1390 | // to that. |
| 1391 | if (It != FunctionToInstrCount.end()) |
| 1392 | FnCountBefore = It->second; |
| 1393 | |
| 1394 | // Compute the delta and emit a remark if there was a change. |
| 1395 | int64_t FnDelta = static_cast<int64_t>(FnCountAfter) - |
| 1396 | static_cast<int64_t>(FnCountBefore); |
| 1397 | if (FnDelta == 0) |
| 1398 | continue; |
| 1399 | |
| 1400 | MachineOptimizationRemarkEmitter MORE(*MF, nullptr); |
| 1401 | MORE.emit([&]() { |
| 1402 | MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange", |
| 1403 | DiagnosticLocation(), |
| 1404 | &MF->front()); |
| 1405 | R << DiagnosticInfoOptimizationBase::Argument("Pass", "Machine Outliner") |
| 1406 | << ": Function: " |
| 1407 | << DiagnosticInfoOptimizationBase::Argument("Function", F.getName()) |
| 1408 | << ": MI instruction count changed from " |
| 1409 | << DiagnosticInfoOptimizationBase::Argument("MIInstrsBefore", |
| 1410 | FnCountBefore) |
| 1411 | << " to " |
| 1412 | << DiagnosticInfoOptimizationBase::Argument("MIInstrsAfter", |
| 1413 | FnCountAfter) |
| 1414 | << "; Delta: " |
| 1415 | << DiagnosticInfoOptimizationBase::Argument("Delta", FnDelta); |
| 1416 | return R; |
| 1417 | }); |
| 1418 | } |
| 1419 | } |
| 1420 | |
Jessica Paquette | 050d1ac | 2018-09-11 16:33:46 +0000 | [diff] [blame] | 1421 | bool MachineOutliner::runOnModule(Module &M) { |
| 1422 | // Check if there's anything in the module. If it's empty, then there's |
| 1423 | // nothing to outline. |
| 1424 | if (M.empty()) |
| 1425 | return false; |
| 1426 | |
| 1427 | MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>(); |
| 1428 | |
| 1429 | // If the user passed -enable-machine-outliner=always or |
| 1430 | // -enable-machine-outliner, the pass will run on all functions in the module. |
| 1431 | // Otherwise, if the target supports default outlining, it will run on all |
| 1432 | // functions deemed by the target to be worth outlining from by default. Tell |
| 1433 | // the user how the outliner is running. |
| 1434 | LLVM_DEBUG( |
| 1435 | dbgs() << "Machine Outliner: Running on "; |
| 1436 | if (RunOnAllFunctions) |
| 1437 | dbgs() << "all functions"; |
| 1438 | else |
| 1439 | dbgs() << "target-default functions"; |
| 1440 | dbgs() << "\n" |
| 1441 | ); |
| 1442 | |
| 1443 | // If the user specifies that they want to outline from linkonceodrs, set |
| 1444 | // it here. |
| 1445 | OutlineFromLinkOnceODRs = EnableLinkOnceODROutlining; |
| 1446 | InstructionMapper Mapper; |
| 1447 | |
| 1448 | // Prepare instruction mappings for the suffix tree. |
| 1449 | populateMapper(Mapper, M, MMI); |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1450 | std::vector<OutlinedFunction> FunctionList; |
| 1451 | |
Jessica Paquette | acffa28 | 2017-03-23 21:27:38 +0000 | [diff] [blame] | 1452 | // Find all of the outlining candidates. |
Jessica Paquette | ce3a2dc | 2018-12-05 23:39:07 +0000 | [diff] [blame] | 1453 | findCandidates(Mapper, FunctionList); |
Jessica Paquette | acffa28 | 2017-03-23 21:27:38 +0000 | [diff] [blame] | 1454 | |
Jessica Paquette | 2386eab | 2018-09-11 23:05:34 +0000 | [diff] [blame] | 1455 | // If we've requested size remarks, then collect the MI counts of every |
| 1456 | // function before outlining, and the MI counts after outlining. |
| 1457 | // FIXME: This shouldn't be in the outliner at all; it should ultimately be |
| 1458 | // the pass manager's responsibility. |
| 1459 | // This could pretty easily be placed in outline instead, but because we |
| 1460 | // really ultimately *don't* want this here, it's done like this for now |
| 1461 | // instead. |
| 1462 | |
| 1463 | // Check if we want size remarks. |
| 1464 | bool ShouldEmitSizeRemarks = M.shouldEmitInstrCountChangedRemark(); |
| 1465 | StringMap<unsigned> FunctionToInstrCount; |
| 1466 | if (ShouldEmitSizeRemarks) |
| 1467 | initSizeRemarkInfo(M, MMI, FunctionToInstrCount); |
| 1468 | |
Jessica Paquette | acffa28 | 2017-03-23 21:27:38 +0000 | [diff] [blame] | 1469 | // Outline each of the candidates and return true if something was outlined. |
Jessica Paquette | 4ae3b71 | 2018-12-05 22:50:26 +0000 | [diff] [blame] | 1470 | bool OutlinedSomething = outline(M, FunctionList, Mapper); |
Jessica Paquette | 729e686 | 2018-01-18 00:00:58 +0000 | [diff] [blame] | 1471 | |
Jessica Paquette | 2386eab | 2018-09-11 23:05:34 +0000 | [diff] [blame] | 1472 | // If we outlined something, we definitely changed the MI count of the |
| 1473 | // module. If we've asked for size remarks, then output them. |
| 1474 | // FIXME: This should be in the pass manager. |
| 1475 | if (ShouldEmitSizeRemarks && OutlinedSomething) |
| 1476 | emitInstrCountChangedRemark(M, MMI, FunctionToInstrCount); |
| 1477 | |
Jessica Paquette | 729e686 | 2018-01-18 00:00:58 +0000 | [diff] [blame] | 1478 | return OutlinedSomething; |
Jessica Paquette | 596f483 | 2017-03-06 21:31:18 +0000 | [diff] [blame] | 1479 | } |