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