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