1 //===- StableBasicBlockNumbering.h - Provide BB identifiers -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_SUPPORT_STABLEBASICBLOCKNUMBERING_H
18 #define LLVM_SUPPORT_STABLEBASICBLOCKNUMBERING_H
20 #include "llvm/Function.h"
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;
29 // NumberedBasicBlock - Holds the inverse mapping of BasicBlockNumbering.
30 std::vector<BasicBlock*> NumberedBasicBlock;
33 StableBasicBlockNumbering(Function *F = 0) {
37 /// compute - If we have not computed a numbering for the function yet, do
39 void compute(Function &F) {
40 if (NumberedBasicBlock.empty()) {
42 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I, ++n) {
43 NumberedBasicBlock.push_back(I);
44 BasicBlockNumbering[I] = n;
49 /// getNumber - Return the ID number for the specified BasicBlock.
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!");
59 /// getBlock - Return the BasicBlock corresponding to a particular ID.
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];