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