45e282acdfd5b8f2dc72abbd1588a2bfa830848c
[oota-llvm.git] / include / llvm / Analysis / IntervalPartition.h
1 //===- IntervalPartition.h - Interval partition Calculation -----*- C++ -*-===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the IntervalPartition class, which
11 // calculates and represents the interval partition of a function, or a
12 // preexisting interval partition.
13 //
14 // In this way, the interval partition may be used to reduce a flow graph down
15 // to its degenerate single node interval partition (unless it is irreducible).
16 //
17 // TODO: The IntervalPartition class should take a bool parameter that tells
18 // whether it should add the "tails" of an interval to an interval itself or if
19 // they should be represented as distinct intervals.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #ifndef LLVM_INTERVAL_PARTITION_H
24 #define LLVM_INTERVAL_PARTITION_H
25
26 #include "llvm/Analysis/Interval.h"
27 #include "llvm/Pass.h"
28
29 //===----------------------------------------------------------------------===//
30 //
31 // IntervalPartition - This class builds and holds an "interval partition" for
32 // a function.  This partition divides the control flow graph into a set of
33 // maximal intervals, as defined with the properties above.  Intuitively, a
34 // BasicBlock is a (possibly nonexistent) loop with a "tail" of non looping
35 // nodes following it.
36 //
37 class IntervalPartition : public FunctionPass {
38   typedef std::map<BasicBlock*, Interval*> IntervalMapTy;
39   IntervalMapTy IntervalMap;
40
41   typedef std::vector<Interval*> IntervalListTy;
42   Interval *RootInterval;
43   std::vector<Interval*> Intervals;
44
45 public:
46   IntervalPartition() : RootInterval(0) {}
47
48   // run - Calculate the interval partition for this function
49   virtual bool runOnFunction(Function &F);
50
51   // IntervalPartition ctor - Build a reduced interval partition from an
52   // existing interval graph.  This takes an additional boolean parameter to
53   // distinguish it from a copy constructor.  Always pass in false for now.
54   //
55   IntervalPartition(IntervalPartition &I, bool);
56
57   // Destructor - Free memory
58   ~IntervalPartition() { destroy(); }
59
60   // print - Show contents in human readable format...
61   virtual void print(std::ostream &O) const;
62
63   // getRootInterval() - Return the root interval that contains the starting
64   // block of the function.
65   inline Interval *getRootInterval() { return RootInterval; }
66
67   // isDegeneratePartition() - Returns true if the interval partition contains
68   // a single interval, and thus cannot be simplified anymore.
69   bool isDegeneratePartition() { return Intervals.size() == 1; }
70
71   // TODO: isIrreducible - look for triangle graph.
72
73   // getBlockInterval - Return the interval that a basic block exists in.
74   inline Interval *getBlockInterval(BasicBlock *BB) {
75     IntervalMapTy::iterator I = IntervalMap.find(BB);
76     return I != IntervalMap.end() ? I->second : 0;
77   }
78
79   // getAnalysisUsage - Implement the Pass API
80   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
81     AU.setPreservesAll();
82   }
83
84   // Interface to Intervals vector...
85   const std::vector<Interval*> &getIntervals() const { return Intervals; }
86
87 private:
88   // destroy - Reset state back to before function was analyzed
89   void destroy();
90
91   // addIntervalToPartition - Add an interval to the internal list of intervals,
92   // and then add mappings from all of the basic blocks in the interval to the
93   // interval itself (in the IntervalMap).
94   //
95   void addIntervalToPartition(Interval *I);
96
97   // updatePredecessors - Interval generation only sets the successor fields of
98   // the interval data structures.  After interval generation is complete,
99   // run through all of the intervals and propagate successor info as
100   // predecessor info.
101   //
102   void updatePredecessors(Interval *Int);
103 };
104
105 #endif