blob: c777d91b67c6262e0f656700bc6c990881129604 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- IntervalPartition.cpp - Interval Partition module code -------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000015#include "llvm/Analysis/IntervalPartition.h"
16#include "llvm/Analysis/Interval.h"
Chris Lattner2d676c92001-06-24 04:07:44 +000017#include "llvm/Analysis/IntervalIterator.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000018#include "llvm/Pass.h"
19#include <cassert>
20#include <utility>
21
Chris Lattner13d01082005-02-22 23:27:21 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Devang Patel19974732007-05-03 01:11:54 +000024char IntervalPartition::ID = 0;
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000025
Owen Andersond13db2c2010-07-21 22:09:45 +000026INITIALIZE_PASS(IntervalPartition, "intervals",
Owen Andersonce665bd2010-10-07 22:25:06 +000027 "Interval Partition Construction", true, true)
Chris Lattner1e435162002-07-26 21:12:44 +000028
Chris Lattner2d676c92001-06-24 04:07:44 +000029//===----------------------------------------------------------------------===//
30// IntervalPartition Implementation
31//===----------------------------------------------------------------------===//
32
Chris Lattnerdb57ef12008-08-28 22:56:53 +000033// releaseMemory - Reset state back to before function was analyzed
34void IntervalPartition::releaseMemory() {
Chris Lattner13d01082005-02-22 23:27:21 +000035 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
36 delete Intervals[i];
Chris Lattner93193f82002-01-31 00:42:27 +000037 IntervalMap.clear();
Chris Lattner1e1ef3e2008-08-28 03:33:03 +000038 Intervals.clear();
Craig Topper570e52c2014-04-15 04:59:12 +000039 RootInterval = nullptr;
Chris Lattner2d676c92001-06-24 04:07:44 +000040}
41
Chris Lattner45cfe542009-08-23 06:03:38 +000042void IntervalPartition::print(raw_ostream &O, const Module*) const {
Chris Lattner318c1492005-04-26 14:48:28 +000043 for(unsigned i = 0, e = Intervals.size(); i != e; ++i)
44 Intervals[i]->print(O);
Chris Lattnera59cbb22002-07-27 01:12:17 +000045}
46
Chris Lattner23e36622001-06-25 03:55:04 +000047// addIntervalToPartition - Add an interval to the internal list of intervals,
48// and then add mappings from all of the basic blocks in the interval to the
49// interval itself (in the IntervalMap).
Chris Lattner23e36622001-06-25 03:55:04 +000050void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattner38969482002-08-09 22:52:08 +000051 Intervals.push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000052
53 // Add mappings for all of the basic blocks in I to the IntervalPartition
54 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
55 It != End; ++It)
Chris Lattnercf3056d2003-10-13 03:32:08 +000056 IntervalMap.insert(std::make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000057}
58
Chris Lattner2d676c92001-06-24 04:07:44 +000059// updatePredecessors - Interval generation only sets the successor fields of
60// the interval data structures. After interval generation is complete,
Misha Brukmana3bbcb52002-10-29 23:06:16 +000061// run through all of the intervals and propagate successor info as
Chris Lattner2d676c92001-06-24 04:07:44 +000062// predecessor info.
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000063void IntervalPartition::updatePredecessors(Interval *Int) {
Chris Lattner2d676c92001-06-24 04:07:44 +000064 BasicBlock *Header = Int->getHeaderNode();
Benjamin Kramer8d0d2b62016-06-26 17:27:42 +000065 for (BasicBlock *Successor : Int->Successors)
66 getBlockInterval(Successor)->Predecessors.push_back(Header);
Chris Lattner2d676c92001-06-24 04:07:44 +000067}
68
Chris Lattner2d676c92001-06-24 04:07:44 +000069// IntervalPartition ctor - Build the first level interval partition for the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000070// specified function...
Chris Lattner7e708292002-06-25 16:13:24 +000071bool IntervalPartition::runOnFunction(Function &F) {
Chris Lattner23e36622001-06-25 03:55:04 +000072 // Pass false to intervals_begin because we take ownership of it's memory
Chris Lattner7e708292002-06-25 16:13:24 +000073 function_interval_iterator I = intervals_begin(&F, false);
74 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000075
76 addIntervalToPartition(RootInterval = *I);
77
Chris Lattner7fc9fe32001-06-27 23:41:11 +000078 ++I; // After the first one...
79
Chris Lattner13d01082005-02-22 23:27:21 +000080 // Add the rest of the intervals to the partition.
81 for (function_interval_iterator E = intervals_end(&F); I != E; ++I)
82 addIntervalToPartition(*I);
Chris Lattner2d676c92001-06-24 04:07:44 +000083
Misha Brukmana3bbcb52002-10-29 23:06:16 +000084 // Now that we know all of the successor information, propagate this to the
Chris Lattner13d01082005-02-22 23:27:21 +000085 // predecessors for each block.
86 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
87 updatePredecessors(Intervals[i]);
Chris Lattner93193f82002-01-31 00:42:27 +000088 return false;
Chris Lattner2d676c92001-06-24 04:07:44 +000089}
90
Chris Lattner2d676c92001-06-24 04:07:44 +000091// IntervalPartition ctor - Build a reduced interval partition from an
92// existing interval graph. This takes an additional boolean parameter to
93// distinguish it from a copy constructor. Always pass in false for now.
Devang Patel794fd752007-05-01 21:15:47 +000094IntervalPartition::IntervalPartition(IntervalPartition &IP, bool)
Owen Anderson90c579d2010-08-06 18:33:48 +000095 : FunctionPass(ID) {
Chris Lattner9d2c9bd2008-06-21 19:48:22 +000096 assert(IP.getRootInterval() && "Cannot operate on empty IntervalPartitions!");
Chris Lattner2d676c92001-06-24 04:07:44 +000097
Chris Lattner23e36622001-06-25 03:55:04 +000098 // Pass false to intervals_begin because we take ownership of it's memory
99 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000100 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +0000101
102 addIntervalToPartition(RootInterval = *I);
103
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000104 ++I; // After the first one...
105
Chris Lattner13d01082005-02-22 23:27:21 +0000106 // Add the rest of the intervals to the partition.
107 for (interval_part_interval_iterator E = intervals_end(IP); I != E; ++I)
108 addIntervalToPartition(*I);
Chris Lattner2d676c92001-06-24 04:07:44 +0000109
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000110 // Now that we know all of the successor information, propagate this to the
Chris Lattner13d01082005-02-22 23:27:21 +0000111 // predecessors for each block.
112 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
113 updatePredecessors(Intervals[i]);
Chris Lattner2d676c92001-06-24 04:07:44 +0000114}