blob: c777d91b67c6262e0f656700bc6c990881129604 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===- IntervalPartition.cpp - Interval Partition module code -------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner60101272001-06-24 04:07:44 +00009//
Chris Lattner78dd56f2002-04-28 16:21:30 +000010// This file contains the definition of the IntervalPartition class, which
Chris Lattner62b7fd12002-04-07 20:49:59 +000011// calculates and represent the interval partition of a function.
Chris Lattner60101272001-06-24 04:07:44 +000012//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenko48666a62017-07-24 23:16:33 +000015#include "llvm/Analysis/IntervalPartition.h"
16#include "llvm/Analysis/Interval.h"
Chris Lattner60101272001-06-24 04:07:44 +000017#include "llvm/Analysis/IntervalIterator.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000018#include "llvm/Pass.h"
19#include <cassert>
20#include <utility>
21
Chris Lattner31664712005-02-22 23:27:21 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Devang Patel8c78a0b2007-05-03 01:11:54 +000024char IntervalPartition::ID = 0;
Eugene Zelenko48666a62017-07-24 23:16:33 +000025
Owen Andersona57b97e2010-07-21 22:09:45 +000026INITIALIZE_PASS(IntervalPartition, "intervals",
Owen Andersondf7a4f22010-10-07 22:25:06 +000027 "Interval Partition Construction", true, true)
Chris Lattnera2c09852002-07-26 21:12:44 +000028
Chris Lattner60101272001-06-24 04:07:44 +000029//===----------------------------------------------------------------------===//
30// IntervalPartition Implementation
31//===----------------------------------------------------------------------===//
32
Chris Lattnere6fe8832008-08-28 22:56:53 +000033// releaseMemory - Reset state back to before function was analyzed
34void IntervalPartition::releaseMemory() {
Chris Lattner31664712005-02-22 23:27:21 +000035 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
36 delete Intervals[i];
Chris Lattnerccf571a2002-01-31 00:42:27 +000037 IntervalMap.clear();
Chris Lattner65df1182008-08-28 03:33:03 +000038 Intervals.clear();
Craig Topper9f008862014-04-15 04:59:12 +000039 RootInterval = nullptr;
Chris Lattner60101272001-06-24 04:07:44 +000040}
41
Chris Lattner13626022009-08-23 06:03:38 +000042void IntervalPartition::print(raw_ostream &O, const Module*) const {
Chris Lattnereb2a4612005-04-26 14:48:28 +000043 for(unsigned i = 0, e = Intervals.size(); i != e; ++i)
44 Intervals[i]->print(O);
Chris Lattner26750072002-07-27 01:12:17 +000045}
46
Chris Lattnered590252001-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 Lattnered590252001-06-25 03:55:04 +000050void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattner0d92ff02002-08-09 22:52:08 +000051 Intervals.push_back(I);
Chris Lattner60101272001-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 Lattner44d2c352003-10-13 03:32:08 +000056 IntervalMap.insert(std::make_pair(*It, I));
Chris Lattner60101272001-06-24 04:07:44 +000057}
58
Chris Lattner60101272001-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 Brukman632df282002-10-29 23:06:16 +000061// run through all of the intervals and propagate successor info as
Chris Lattner60101272001-06-24 04:07:44 +000062// predecessor info.
Chris Lattner78dd56f2002-04-28 16:21:30 +000063void IntervalPartition::updatePredecessors(Interval *Int) {
Chris Lattner60101272001-06-24 04:07:44 +000064 BasicBlock *Header = Int->getHeaderNode();
Benjamin Krameraa209152016-06-26 17:27:42 +000065 for (BasicBlock *Successor : Int->Successors)
66 getBlockInterval(Successor)->Predecessors.push_back(Header);
Chris Lattner60101272001-06-24 04:07:44 +000067}
68
Chris Lattner60101272001-06-24 04:07:44 +000069// IntervalPartition ctor - Build the first level interval partition for the
Chris Lattner62b7fd12002-04-07 20:49:59 +000070// specified function...
Chris Lattner113f4f42002-06-25 16:13:24 +000071bool IntervalPartition::runOnFunction(Function &F) {
Chris Lattnered590252001-06-25 03:55:04 +000072 // Pass false to intervals_begin because we take ownership of it's memory
Chris Lattner113f4f42002-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 Lattnered590252001-06-25 03:55:04 +000075
76 addIntervalToPartition(RootInterval = *I);
77
Chris Lattner4cee8d82001-06-27 23:41:11 +000078 ++I; // After the first one...
79
Chris Lattner31664712005-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 Lattner60101272001-06-24 04:07:44 +000083
Misha Brukman632df282002-10-29 23:06:16 +000084 // Now that we know all of the successor information, propagate this to the
Chris Lattner31664712005-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 Lattnerccf571a2002-01-31 00:42:27 +000088 return false;
Chris Lattner60101272001-06-24 04:07:44 +000089}
90
Chris Lattner60101272001-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 Patel09f162c2007-05-01 21:15:47 +000094IntervalPartition::IntervalPartition(IntervalPartition &IP, bool)
Owen Andersona7aed182010-08-06 18:33:48 +000095 : FunctionPass(ID) {
Chris Lattnere635aca2008-06-21 19:48:22 +000096 assert(IP.getRootInterval() && "Cannot operate on empty IntervalPartitions!");
Chris Lattner60101272001-06-24 04:07:44 +000097
Chris Lattnered590252001-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 Lattner4cee8d82001-06-27 23:41:11 +0000100 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattnered590252001-06-25 03:55:04 +0000101
102 addIntervalToPartition(RootInterval = *I);
103
Chris Lattner4cee8d82001-06-27 23:41:11 +0000104 ++I; // After the first one...
105
Chris Lattner31664712005-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 Lattner60101272001-06-24 04:07:44 +0000109
Misha Brukman632df282002-10-29 23:06:16 +0000110 // Now that we know all of the successor information, propagate this to the
Chris Lattner31664712005-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 Lattner60101272001-06-24 04:07:44 +0000114}