blob: 5dfccd2c80c0f80901ee1f7f328790d1e5298aea [file] [log] [blame]
Chris Lattner2d676c92001-06-24 04:07:44 +00001//===- IntervalPartition.cpp - Interval Partition module code ----*- C++ -*--=//
2//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +00003// This file contains the definition of the IntervalPartition class, which
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00004// calculates and represent the interval partition of a function.
Chris Lattner2d676c92001-06-24 04:07:44 +00005//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Analysis/IntervalIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +00009#include "Support/STLExtras.h"
Chris Lattner2d676c92001-06-24 04:07:44 +000010
Chris Lattner697954c2002-01-20 22:54:45 +000011using std::make_pair;
Chris Lattner2d676c92001-06-24 04:07:44 +000012
Chris Lattner07a228d2002-05-06 19:32:07 +000013AnalysisID IntervalPartition::ID(AnalysisID::create<IntervalPartition>(), true);
Chris Lattner93193f82002-01-31 00:42:27 +000014
Chris Lattner2d676c92001-06-24 04:07:44 +000015//===----------------------------------------------------------------------===//
16// IntervalPartition Implementation
17//===----------------------------------------------------------------------===//
18
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000019// destroy - Reset state back to before function was analyzed
Chris Lattner93193f82002-01-31 00:42:27 +000020void IntervalPartition::destroy() {
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000021 for_each(begin(), end(), deleter<Interval>);
Chris Lattner93193f82002-01-31 00:42:27 +000022 IntervalMap.clear();
23 RootInterval = 0;
Chris Lattner2d676c92001-06-24 04:07:44 +000024}
25
Chris Lattner23e36622001-06-25 03:55:04 +000026// addIntervalToPartition - Add an interval to the internal list of intervals,
27// and then add mappings from all of the basic blocks in the interval to the
28// interval itself (in the IntervalMap).
Chris Lattner2d676c92001-06-24 04:07:44 +000029//
Chris Lattner23e36622001-06-25 03:55:04 +000030void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattnere6850232001-07-03 15:28:35 +000031 push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000032
33 // Add mappings for all of the basic blocks in I to the IntervalPartition
34 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
35 It != End; ++It)
Chris Lattner23e36622001-06-25 03:55:04 +000036 IntervalMap.insert(make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000037}
38
Chris Lattner2d676c92001-06-24 04:07:44 +000039// updatePredecessors - Interval generation only sets the successor fields of
40// the interval data structures. After interval generation is complete,
41// run through all of the intervals and propogate successor info as
42// predecessor info.
43//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000044void IntervalPartition::updatePredecessors(Interval *Int) {
Chris Lattner2d676c92001-06-24 04:07:44 +000045 BasicBlock *Header = Int->getHeaderNode();
46 for (Interval::succ_iterator I = Int->Successors.begin(),
47 E = Int->Successors.end(); I != E; ++I)
48 getBlockInterval(*I)->Predecessors.push_back(Header);
49}
50
Chris Lattner2d676c92001-06-24 04:07:44 +000051// IntervalPartition ctor - Build the first level interval partition for the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000052// specified function...
Chris Lattner2d676c92001-06-24 04:07:44 +000053//
Chris Lattnerf57b8452002-04-27 06:56:12 +000054bool IntervalPartition::runOnFunction(Function *F) {
55 assert(F->front() && "Cannot operate on prototypes!");
Chris Lattner2d676c92001-06-24 04:07:44 +000056
Chris Lattner23e36622001-06-25 03:55:04 +000057 // Pass false to intervals_begin because we take ownership of it's memory
Chris Lattnerf57b8452002-04-27 06:56:12 +000058 function_interval_iterator I = intervals_begin(F, false);
59 assert(I != intervals_end(F) && "No intervals in function!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000060
61 addIntervalToPartition(RootInterval = *I);
62
Chris Lattner7fc9fe32001-06-27 23:41:11 +000063 ++I; // After the first one...
64
65 // Add the rest of the intervals to the partition...
Chris Lattnerf57b8452002-04-27 06:56:12 +000066 for_each(I, intervals_end(F),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000067 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000068
69 // Now that we know all of the successor information, propogate this to the
70 // predecessors for each block...
Chris Lattner7fc9fe32001-06-27 23:41:11 +000071 for_each(begin(), end(),
72 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner93193f82002-01-31 00:42:27 +000073 return false;
Chris Lattner2d676c92001-06-24 04:07:44 +000074}
75
76
77// IntervalPartition ctor - Build a reduced interval partition from an
78// existing interval graph. This takes an additional boolean parameter to
79// distinguish it from a copy constructor. Always pass in false for now.
80//
Chris Lattner23e36622001-06-25 03:55:04 +000081IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000082 Interval *FunctionStart = IP.getRootInterval();
83 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
Chris Lattner2d676c92001-06-24 04:07:44 +000084
Chris Lattner23e36622001-06-25 03:55:04 +000085 // Pass false to intervals_begin because we take ownership of it's memory
86 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000087 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000088
89 addIntervalToPartition(RootInterval = *I);
90
Chris Lattner7fc9fe32001-06-27 23:41:11 +000091 ++I; // After the first one...
92
93 // Add the rest of the intervals to the partition...
94 for_each(I, intervals_end(IP),
95 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000096
97 // Now that we know all of the successor information, propogate this to the
98 // predecessors for each block...
Chris Lattner7fc9fe32001-06-27 23:41:11 +000099 for_each(begin(), end(),
100 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +0000101}