aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/Support/StableBasicBlockNumbering.h
blob: 7780b025a555166c1227a1ff0ebfd76d74904f25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//===- StableBasicBlockNumbering.h - Provide BB identifiers -----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This class provides a *stable* numbering of basic blocks that does not depend
// on their address in memory (which is nondeterministic).  When requested, this
// class simply provides a unique ID for each basic block in the function
// specified and the inverse mapping.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_STABLEBASICBLOCKNUMBERING_H
#define LLVM_SUPPORT_STABLEBASICBLOCKNUMBERING_H

#include "llvm/Function.h"
#include <map>

namespace llvm {
  class StableBasicBlockNumbering {
    // BasicBlockNumbering - Holds a numbering of the basic blocks in the
    // function in a stable order that does not depend on their address.
    std::map<BasicBlock*, unsigned> BasicBlockNumbering;

    // NumberedBasicBlock - Holds the inverse mapping of BasicBlockNumbering.
    std::vector<BasicBlock*> NumberedBasicBlock;
  public:

    StableBasicBlockNumbering(Function *F = 0) {
      if (F) compute(*F);
    }

    /// compute - If we have not computed a numbering for the function yet, do
    /// so.
    void compute(Function &F) {
      if (NumberedBasicBlock.empty()) {
        unsigned n = 0;
        for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I, ++n) {
          NumberedBasicBlock.push_back(I);
          BasicBlockNumbering[I] = n;
        }
      }
    }

    /// getNumber - Return the ID number for the specified BasicBlock.
    ///
    unsigned getNumber(BasicBlock *BB) const {
      std::map<BasicBlock*, unsigned>::const_iterator I =
        BasicBlockNumbering.find(BB);
      assert(I != BasicBlockNumbering.end() &&
             "Invalid basic block or numbering not computed!");
      return I->second;
    }

    /// getBlock - Return the BasicBlock corresponding to a particular ID.
    ///
    BasicBlock *getBlock(unsigned N) const {
      assert(N < NumberedBasicBlock.size() &&
             "Block ID out of range or numbering not computed!");
      return NumberedBasicBlock[N];
    }

  };
}

#endif