1 //===- IntervalPartition.cpp - Interval Partition module code -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the definition of the IntervalPartition class, which 10 // calculates and represent the interval partition of a function. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/IntervalPartition.h" 15 #include "llvm/Analysis/Interval.h" 16 #include "llvm/Analysis/IntervalIterator.h" 17 #include "llvm/Pass.h" 18 #include <cassert> 19 #include <utility> 20 21 using namespace llvm; 22 23 char IntervalPartition::ID = 0; 24 25 INITIALIZE_PASS(IntervalPartition, "intervals", 26 "Interval Partition Construction", true, true) 27 28 //===----------------------------------------------------------------------===// 29 // IntervalPartition Implementation 30 //===----------------------------------------------------------------------===// 31 32 // releaseMemory - Reset state back to before function was analyzed 33 void IntervalPartition::releaseMemory() { 34 for (unsigned i = 0, e = Intervals.size(); i != e; ++i) 35 delete Intervals[i]; 36 IntervalMap.clear(); 37 Intervals.clear(); 38 RootInterval = nullptr; 39 } 40 41 void IntervalPartition::print(raw_ostream &O, const Module*) const { 42 for(unsigned i = 0, e = Intervals.size(); i != e; ++i) 43 Intervals[i]->print(O); 44 } 45 46 // addIntervalToPartition - Add an interval to the internal list of intervals, 47 // and then add mappings from all of the basic blocks in the interval to the 48 // interval itself (in the IntervalMap). 49 void IntervalPartition::addIntervalToPartition(Interval *I) { 50 Intervals.push_back(I); 51 52 // Add mappings for all of the basic blocks in I to the IntervalPartition 53 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end(); 54 It != End; ++It) 55 IntervalMap.insert(std::make_pair(*It, I)); 56 } 57 58 // updatePredecessors - Interval generation only sets the successor fields of 59 // the interval data structures. After interval generation is complete, 60 // run through all of the intervals and propagate successor info as 61 // predecessor info. 62 void IntervalPartition::updatePredecessors(Interval *Int) { 63 BasicBlock *Header = Int->getHeaderNode(); 64 for (BasicBlock *Successor : Int->Successors) 65 getBlockInterval(Successor)->Predecessors.push_back(Header); 66 } 67 68 // IntervalPartition ctor - Build the first level interval partition for the 69 // specified function... 70 bool IntervalPartition::runOnFunction(Function &F) { 71 // Pass false to intervals_begin because we take ownership of it's memory 72 function_interval_iterator I = intervals_begin(&F, false); 73 assert(I != intervals_end(&F) && "No intervals in function!?!?!"); 74 75 addIntervalToPartition(RootInterval = *I); 76 77 ++I; // After the first one... 78 79 // Add the rest of the intervals to the partition. 80 for (function_interval_iterator E = intervals_end(&F); I != E; ++I) 81 addIntervalToPartition(*I); 82 83 // Now that we know all of the successor information, propagate this to the 84 // predecessors for each block. 85 for (unsigned i = 0, e = Intervals.size(); i != e; ++i) 86 updatePredecessors(Intervals[i]); 87 return false; 88 } 89 90 // IntervalPartition ctor - Build a reduced interval partition from an 91 // existing interval graph. This takes an additional boolean parameter to 92 // distinguish it from a copy constructor. Always pass in false for now. 93 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) 94 : FunctionPass(ID) { 95 assert(IP.getRootInterval() && "Cannot operate on empty IntervalPartitions!"); 96 97 // Pass false to intervals_begin because we take ownership of it's memory 98 interval_part_interval_iterator I = intervals_begin(IP, false); 99 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!"); 100 101 addIntervalToPartition(RootInterval = *I); 102 103 ++I; // After the first one... 104 105 // Add the rest of the intervals to the partition. 106 for (interval_part_interval_iterator E = intervals_end(IP); I != E; ++I) 107 addIntervalToPartition(*I); 108 109 // Now that we know all of the successor information, propagate this to the 110 // predecessors for each block. 111 for (unsigned i = 0, e = Intervals.size(); i != e; ++i) 112 updatePredecessors(Intervals[i]); 113 } 114