blob: 4f178f4f64f8c2bf21c1e1a9af32913a58a8fc3f [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- IntervalPartition.cpp - Interval Partition module code -------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2d676c92001-06-24 04:07:44 +00009//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000010// This file contains the definition of the IntervalPartition class, which
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000011// calculates and represent the interval partition of a function.
Chris Lattner2d676c92001-06-24 04:07:44 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/IntervalIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000016#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000017#include <algorithm>
Chris Lattner2d676c92001-06-24 04:07:44 +000018
Brian Gaeked0fde302003-11-11 22:41:34 +000019namespace llvm {
20
Chris Lattner1e435162002-07-26 21:12:44 +000021static RegisterAnalysis<IntervalPartition>
Chris Lattner17689df2002-07-30 16:27:52 +000022X("intervals", "Interval Partition Construction", true);
Chris Lattner1e435162002-07-26 21:12:44 +000023
Chris Lattner2d676c92001-06-24 04:07:44 +000024//===----------------------------------------------------------------------===//
25// IntervalPartition Implementation
26//===----------------------------------------------------------------------===//
27
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000028// destroy - Reset state back to before function was analyzed
Chris Lattner93193f82002-01-31 00:42:27 +000029void IntervalPartition::destroy() {
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000030 std::for_each(Intervals.begin(), Intervals.end(), deleter<Interval>);
Chris Lattner93193f82002-01-31 00:42:27 +000031 IntervalMap.clear();
32 RootInterval = 0;
Chris Lattner2d676c92001-06-24 04:07:44 +000033}
34
Chris Lattner1b0a63f2002-07-31 19:32:01 +000035void IntervalPartition::print(std::ostream &O) const {
Chris Lattner38969482002-08-09 22:52:08 +000036 std::copy(Intervals.begin(), Intervals.end(),
Chris Lattnera59cbb22002-07-27 01:12:17 +000037 std::ostream_iterator<const Interval *>(O, "\n"));
38}
39
Chris Lattner23e36622001-06-25 03:55:04 +000040// addIntervalToPartition - Add an interval to the internal list of intervals,
41// and then add mappings from all of the basic blocks in the interval to the
42// interval itself (in the IntervalMap).
Chris Lattner2d676c92001-06-24 04:07:44 +000043//
Chris Lattner23e36622001-06-25 03:55:04 +000044void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattner38969482002-08-09 22:52:08 +000045 Intervals.push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000046
47 // Add mappings for all of the basic blocks in I to the IntervalPartition
48 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
49 It != End; ++It)
Chris Lattnercf3056d2003-10-13 03:32:08 +000050 IntervalMap.insert(std::make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000051}
52
Chris Lattner2d676c92001-06-24 04:07:44 +000053// updatePredecessors - Interval generation only sets the successor fields of
54// the interval data structures. After interval generation is complete,
Misha Brukmana3bbcb52002-10-29 23:06:16 +000055// run through all of the intervals and propagate successor info as
Chris Lattner2d676c92001-06-24 04:07:44 +000056// predecessor info.
57//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000058void IntervalPartition::updatePredecessors(Interval *Int) {
Chris Lattner2d676c92001-06-24 04:07:44 +000059 BasicBlock *Header = Int->getHeaderNode();
60 for (Interval::succ_iterator I = Int->Successors.begin(),
61 E = Int->Successors.end(); I != E; ++I)
62 getBlockInterval(*I)->Predecessors.push_back(Header);
63}
64
Chris Lattner2d676c92001-06-24 04:07:44 +000065// IntervalPartition ctor - Build the first level interval partition for the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000066// specified function...
Chris Lattner2d676c92001-06-24 04:07:44 +000067//
Chris Lattner7e708292002-06-25 16:13:24 +000068bool IntervalPartition::runOnFunction(Function &F) {
Chris Lattner23e36622001-06-25 03:55:04 +000069 // Pass false to intervals_begin because we take ownership of it's memory
Chris Lattner7e708292002-06-25 16:13:24 +000070 function_interval_iterator I = intervals_begin(&F, false);
71 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000072
73 addIntervalToPartition(RootInterval = *I);
74
Chris Lattner7fc9fe32001-06-27 23:41:11 +000075 ++I; // After the first one...
76
77 // Add the rest of the intervals to the partition...
Chris Lattner7e708292002-06-25 16:13:24 +000078 for_each(I, intervals_end(&F),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000079 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000080
Misha Brukmana3bbcb52002-10-29 23:06:16 +000081 // Now that we know all of the successor information, propagate this to the
Chris Lattner2d676c92001-06-24 04:07:44 +000082 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +000083 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000084 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner93193f82002-01-31 00:42:27 +000085 return false;
Chris Lattner2d676c92001-06-24 04:07:44 +000086}
87
88
89// IntervalPartition ctor - Build a reduced interval partition from an
90// existing interval graph. This takes an additional boolean parameter to
91// distinguish it from a copy constructor. Always pass in false for now.
92//
Chris Lattner23e36622001-06-25 03:55:04 +000093IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000094 Interval *FunctionStart = IP.getRootInterval();
95 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
Chris Lattner2d676c92001-06-24 04:07:44 +000096
Chris Lattner23e36622001-06-25 03:55:04 +000097 // Pass false to intervals_begin because we take ownership of it's memory
98 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000099 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +0000100
101 addIntervalToPartition(RootInterval = *I);
102
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000103 ++I; // After the first one...
104
105 // Add the rest of the intervals to the partition...
106 for_each(I, intervals_end(IP),
107 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +0000108
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000109 // Now that we know all of the successor information, propagate this to the
Chris Lattner2d676c92001-06-24 04:07:44 +0000110 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +0000111 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000112 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +0000113}
Brian Gaeked0fde302003-11-11 22:41:34 +0000114
115} // End llvm namespace