For PR1043:
[oota-llvm.git] / include / llvm / Support / StableBasicBlockNumbering.h
1 //===- StableBasicBlockNumbering.h - Provide BB identifiers -----*- 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 class provides a *stable* numbering of basic blocks that does not depend
11 // on their address in memory (which is nondeterministic).  When requested, this
12 // class simply provides a unique ID for each basic block in the function
13 // specified and the inverse mapping.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_SUPPORT_STABLEBASICBLOCKNUMBERING_H
18 #define LLVM_SUPPORT_STABLEBASICBLOCKNUMBERING_H
19
20 #include "llvm/Function.h"
21 #include <map>
22
23 namespace llvm {
24   class StableBasicBlockNumbering {
25     // BasicBlockNumbering - Holds a numbering of the basic blocks in the
26     // function in a stable order that does not depend on their address.
27     std::map<BasicBlock*, unsigned> BasicBlockNumbering;
28
29     // NumberedBasicBlock - Holds the inverse mapping of BasicBlockNumbering.
30     std::vector<BasicBlock*> NumberedBasicBlock;
31   public:
32
33     StableBasicBlockNumbering(Function *F = 0) {
34       if (F) compute(*F);
35     }
36
37     /// compute - If we have not computed a numbering for the function yet, do
38     /// so.
39     void compute(Function &F) {
40       if (NumberedBasicBlock.empty()) {
41         unsigned n = 0;
42         for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I, ++n) {
43           NumberedBasicBlock.push_back(I);
44           BasicBlockNumbering[I] = n;
45         }
46       }
47     }
48
49     /// getNumber - Return the ID number for the specified BasicBlock.
50     ///
51     unsigned getNumber(BasicBlock *BB) const {
52       std::map<BasicBlock*, unsigned>::const_iterator I =
53         BasicBlockNumbering.find(BB);
54       assert(I != BasicBlockNumbering.end() &&
55              "Invalid basic block or numbering not computed!");
56       return I->second;
57     }
58
59     /// getBlock - Return the BasicBlock corresponding to a particular ID.
60     ///
61     BasicBlock *getBlock(unsigned N) const {
62       assert(N < NumberedBasicBlock.size() &&
63              "Block ID out of range or numbering not computed!");
64       return NumberedBasicBlock[N];
65     }
66
67   };
68 }
69
70 #endif