LLVM API Documentation

StableBasicBlockNumbering.h

Go to the documentation of this file.
00001 //===- StableBasicBlockNumbering.h - Provide BB identifiers -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class provides a *stable* numbering of basic blocks that does not depend
00011 // on their address in memory (which is nondeterministic).  When requested, this
00012 // class simply provides a unique ID for each basic block in the function
00013 // specified and the inverse mapping.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_SUPPORT_STABLEBASICBLOCKNUMBERING_H
00018 #define LLVM_SUPPORT_STABLEBASICBLOCKNUMBERING_H
00019 
00020 #include "llvm/Function.h"
00021 #include <map>
00022 
00023 namespace llvm {
00024   class StableBasicBlockNumbering {
00025     // BasicBlockNumbering - Holds a numbering of the basic blocks in the
00026     // function in a stable order that does not depend on their address.
00027     std::map<BasicBlock*, unsigned> BasicBlockNumbering;
00028 
00029     // NumberedBasicBlock - Holds the inverse mapping of BasicBlockNumbering.
00030     std::vector<BasicBlock*> NumberedBasicBlock;
00031   public:
00032 
00033     StableBasicBlockNumbering(Function *F = 0) {
00034       if (F) compute(*F);
00035     }
00036 
00037     /// compute - If we have not computed a numbering for the function yet, do
00038     /// so.
00039     void compute(Function &F) {
00040       if (NumberedBasicBlock.empty()) {
00041         unsigned n = 0;
00042         for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I, ++n) {
00043           NumberedBasicBlock.push_back(I);
00044           BasicBlockNumbering[I] = n;
00045         }
00046       }
00047     }
00048 
00049     /// getNumber - Return the ID number for the specified BasicBlock.
00050     ///
00051     unsigned getNumber(BasicBlock *BB) const {
00052       std::map<BasicBlock*, unsigned>::const_iterator I =
00053         BasicBlockNumbering.find(BB);
00054       assert(I != BasicBlockNumbering.end() &&
00055              "Invalid basic block or numbering not computed!");
00056       return I->second;
00057     }
00058 
00059     /// getBlock - Return the BasicBlock corresponding to a particular ID.
00060     ///
00061     BasicBlock *getBlock(unsigned N) const {
00062       assert(N < NumberedBasicBlock.size() &&
00063              "Block ID out of range or numbering not computed!");
00064       return NumberedBasicBlock[N];
00065     }
00066 
00067   };
00068 }
00069 
00070 #endif