Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 1 | //===- IntervalPartition.cpp - Interval Partition module code ----*- C++ -*--=// |
| 2 | // |
| 3 | // This file contains the definition of the cfg::IntervalPartition class, which |
| 4 | // calculates and represent the interval partition of a method. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #include "llvm/Analysis/IntervalIterator.h" |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 9 | #include "llvm/Support/STLExtras.h" |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 10 | |
| 11 | using namespace cfg; |
| 12 | |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | // IntervalPartition Implementation |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 17 | // Destructor - Free memory |
| 18 | IntervalPartition::~IntervalPartition() { |
| 19 | for_each(begin(), end(), deleter<cfg::Interval>); |
| 20 | } |
| 21 | |
Chris Lattner | 23e3662 | 2001-06-25 03:55:04 +0000 | [diff] [blame] | 22 | // addIntervalToPartition - Add an interval to the internal list of intervals, |
| 23 | // and then add mappings from all of the basic blocks in the interval to the |
| 24 | // interval itself (in the IntervalMap). |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 25 | // |
Chris Lattner | 23e3662 | 2001-06-25 03:55:04 +0000 | [diff] [blame] | 26 | void IntervalPartition::addIntervalToPartition(Interval *I) { |
Chris Lattner | e685023 | 2001-07-03 15:28:35 +0000 | [diff] [blame] | 27 | push_back(I); |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 28 | |
| 29 | // Add mappings for all of the basic blocks in I to the IntervalPartition |
| 30 | for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end(); |
| 31 | It != End; ++It) |
Chris Lattner | 23e3662 | 2001-06-25 03:55:04 +0000 | [diff] [blame] | 32 | IntervalMap.insert(make_pair(*It, I)); |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 35 | // updatePredecessors - Interval generation only sets the successor fields of |
| 36 | // the interval data structures. After interval generation is complete, |
| 37 | // run through all of the intervals and propogate successor info as |
| 38 | // predecessor info. |
| 39 | // |
| 40 | void IntervalPartition::updatePredecessors(cfg::Interval *Int) { |
| 41 | BasicBlock *Header = Int->getHeaderNode(); |
| 42 | for (Interval::succ_iterator I = Int->Successors.begin(), |
| 43 | E = Int->Successors.end(); I != E; ++I) |
| 44 | getBlockInterval(*I)->Predecessors.push_back(Header); |
| 45 | } |
| 46 | |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 47 | // IntervalPartition ctor - Build the first level interval partition for the |
| 48 | // specified method... |
| 49 | // |
| 50 | IntervalPartition::IntervalPartition(Method *M) { |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 51 | assert(M->front() && "Cannot operate on prototypes!"); |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 23e3662 | 2001-06-25 03:55:04 +0000 | [diff] [blame] | 53 | // Pass false to intervals_begin because we take ownership of it's memory |
| 54 | method_interval_iterator I = intervals_begin(M, false); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 55 | assert(I != intervals_end(M) && "No intervals in method!?!?!"); |
Chris Lattner | 23e3662 | 2001-06-25 03:55:04 +0000 | [diff] [blame] | 56 | |
| 57 | addIntervalToPartition(RootInterval = *I); |
| 58 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 59 | ++I; // After the first one... |
| 60 | |
| 61 | // Add the rest of the intervals to the partition... |
| 62 | for_each(I, intervals_end(M), |
| 63 | bind_obj(this, &IntervalPartition::addIntervalToPartition)); |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 64 | |
| 65 | // Now that we know all of the successor information, propogate this to the |
| 66 | // predecessors for each block... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 67 | for_each(begin(), end(), |
| 68 | bind_obj(this, &IntervalPartition::updatePredecessors)); |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | // IntervalPartition ctor - Build a reduced interval partition from an |
| 73 | // existing interval graph. This takes an additional boolean parameter to |
| 74 | // distinguish it from a copy constructor. Always pass in false for now. |
| 75 | // |
Chris Lattner | 23e3662 | 2001-06-25 03:55:04 +0000 | [diff] [blame] | 76 | IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) { |
| 77 | Interval *MethodStart = IP.getRootInterval(); |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 78 | assert(MethodStart && "Cannot operate on empty IntervalPartitions!"); |
| 79 | |
Chris Lattner | 23e3662 | 2001-06-25 03:55:04 +0000 | [diff] [blame] | 80 | // Pass false to intervals_begin because we take ownership of it's memory |
| 81 | interval_part_interval_iterator I = intervals_begin(IP, false); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 82 | assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!"); |
Chris Lattner | 23e3662 | 2001-06-25 03:55:04 +0000 | [diff] [blame] | 83 | |
| 84 | addIntervalToPartition(RootInterval = *I); |
| 85 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 86 | ++I; // After the first one... |
| 87 | |
| 88 | // Add the rest of the intervals to the partition... |
| 89 | for_each(I, intervals_end(IP), |
| 90 | bind_obj(this, &IntervalPartition::addIntervalToPartition)); |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 91 | |
| 92 | // Now that we know all of the successor information, propogate this to the |
| 93 | // predecessors for each block... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 94 | for_each(begin(), end(), |
| 95 | bind_obj(this, &IntervalPartition::updatePredecessors)); |
Chris Lattner | 2d676c9 | 2001-06-24 04:07:44 +0000 | [diff] [blame] | 96 | } |