blob: f1737fc39af106f46a7eb83e1e3f952b76b632b0 [file] [log] [blame]
Chris Lattner2275c1d2001-06-20 20:09:55 +00001//===- Intervals.cpp - Interval partition Calculation ------------*- C++ -*--=//
2//
3// This file contains the declaration of the cfg::IntervalPartition class, which
4// calculates and represent the interval partition of a method.
5//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Analysis/Intervals.h"
9#include "llvm/Method.h"
10#include "llvm/BasicBlock.h"
11#include "llvm/CFG.h"
12
13void cfg::IntervalPartition::UpdateSuccessors(cfg::Interval *Int) {
14 BasicBlock *Header = Int->HeaderNode;
15 for (cfg::Interval::succ_iterator I = Int->Successors.begin(),
16 E = Int->Successors.end(); I != E; ++I)
17 getBlockInterval(*I)->Predecessors.push_back(Header);
18}
19
20// IntervalPartition ctor - Build the partition for the specified method
21cfg::IntervalPartition::IntervalPartition(Method *M) {
22 BasicBlock *MethodStart = M->getBasicBlocks().front();
23 assert(MethodStart && "Cannot operate on prototypes!");
24
25 ProcessInterval(MethodStart);
26 RootInterval = getBlockInterval(MethodStart);
27
28 // Now that we know all of the successor information, propogate this to the
29 // predecessors for each block...
30 for(iterator I = begin(), E = end(); I != E; ++I)
31 UpdateSuccessors(*I);
32}
33
34void cfg::IntervalPartition::ProcessInterval(BasicBlock *Header) {
35 if (getBlockInterval(Header)) return; // Interval already constructed
36
37 Interval *Int = new Interval(Header);
38 IntervalList.push_back(Int); // Add the interval to our current set
39 IntervalMap.insert(make_pair(Header, Int));
40
41 // Check all of our successors to see if they are in the interval...
42 for (succ_iterator I = succ_begin(Header), E = succ_end(Header); I != E; ++I)
43 ProcessBasicBlock(Int, *I);
44
45 // Build all of the successor intervals of this interval now...
46 for(Interval::succ_iterator I = Int->Successors.begin(),
47 E = Int->Successors.end(); I != E; ++I)
48 ProcessInterval(*I);
49}
50
51void cfg::IntervalPartition::ProcessBasicBlock(Interval *Int, BasicBlock *BB) {
52 assert(Int && "Null interval == bad!");
53 assert(BB && "Null interval == bad!");
54
55 Interval *CurInt = getBlockInterval(BB);
56 if (CurInt == Int) { // Already in this interval...
57 return;
58 } else if (CurInt != 0) { // In another interval, add as successor
59 if (!Int->isSuccessor(BB)) // Add only if not already in set
60 Int->Successors.push_back(BB);
61 } else { // Otherwise, not in interval yet
62 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
63 if (!Int->contains(*I)) { // If pred not in interval, we can't be
64 if (!Int->isSuccessor(BB)) // Add only if not already in set
65 Int->Successors.push_back(BB);
66 return; // See you later
67 }
68 }
69
70 // If we get here, then all of the predecessors of BB are in the interval
71 // already. In this case, we must add BB to the interval!
72 Int->Nodes.push_back(BB);
73 IntervalMap.insert(make_pair(BB, Int));
74
75 if (Int->isSuccessor(BB)) {
76 // If we were in the successor list from before... remove from succ list
77 remove(Int->Successors.begin(), Int->Successors.end(), BB);
78 }
79
80 // Now that we have discovered that BB is in the interval, perhaps some of
81 // its successors are as well?
82 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
83 ProcessBasicBlock(Int, *I);
84 }
85}