1f985e37d6456d5dc299f6ec40dc0efa5a5e19e1
[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 namespace llvm {
30
31 //===----------------------------------------------------------------------===//
32 //
33 // IntervalPartition - This class builds and holds an "interval partition" for
34 // a function.  This partition divides the control flow graph into a set of
35 // maximal intervals, as defined with the properties above.  Intuitively, a
36 // BasicBlock is a (possibly nonexistent) loop with a "tail" of non looping
37 // nodes following it.
38 //
39 class IntervalPartition : public FunctionPass {
40   typedef std::map<BasicBlock*, Interval*> IntervalMapTy;
41   IntervalMapTy IntervalMap;
42
43   typedef std::vector<Interval*> IntervalListTy;
44   Interval *RootInterval;
45   std::vector<Interval*> Intervals;
46
47 public:
48   static char ID; // Pass identification, replacement for typeid
49
50   IntervalPartition() : FunctionPass((intptr_t)&ID), RootInterval(0) {}
51
52   // run - Calculate the interval partition for this function
53   virtual bool runOnFunction(Function &F);
54
55   // IntervalPartition ctor - Build a reduced interval partition from an
56   // existing interval graph.  This takes an additional boolean parameter to
57   // distinguish it from a copy constructor.  Always pass in false for now.
58   //
59   IntervalPartition(IntervalPartition &I, bool);
60
61   // Destructor - Free memory
62   ~IntervalPartition() { destroy(); }
63
64   // print - Show contents in human readable format...
65   virtual void print(std::ostream &O, const Module* = 0) const;
66   void print(std::ostream *O, const Module* M = 0) const {
67     if (O) print(*O, M);
68   }
69
70   // getRootInterval() - Return the root interval that contains the starting
71   // block of the function.
72   inline Interval *getRootInterval() { return RootInterval; }
73
74   // isDegeneratePartition() - Returns true if the interval partition contains
75   // a single interval, and thus cannot be simplified anymore.
76   bool isDegeneratePartition() { return Intervals.size() == 1; }
77
78   // TODO: isIrreducible - look for triangle graph.
79
80   // getBlockInterval - Return the interval that a basic block exists in.
81   inline Interval *getBlockInterval(BasicBlock *BB) {
82     IntervalMapTy::iterator I = IntervalMap.find(BB);
83     return I != IntervalMap.end() ? I->second : 0;
84   }
85
86   // getAnalysisUsage - Implement the Pass API
87   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
88     AU.setPreservesAll();
89   }
90
91   // Interface to Intervals vector...
92   const std::vector<Interval*> &getIntervals() const { return Intervals; }
93
94 private:
95   // destroy - Reset state back to before function was analyzed
96   void destroy();
97
98   // addIntervalToPartition - Add an interval to the internal list of intervals,
99   // and then add mappings from all of the basic blocks in the interval to the
100   // interval itself (in the IntervalMap).
101   //
102   void addIntervalToPartition(Interval *I);
103
104   // updatePredecessors - Interval generation only sets the successor fields of
105   // the interval data structures.  After interval generation is complete,
106   // run through all of the intervals and propagate successor info as
107   // predecessor info.
108   //
109   void updatePredecessors(Interval *Int);
110 };
111
112 } // End llvm namespace
113
114 #endif