LLVM API Documentation
00001 //===- StableBasicBlockNumbering.h - Provide BB identifiers -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // 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 "llvm/ADT/UniqueVector.h" 00022 00023 namespace llvm { 00024 class StableBasicBlockNumbering { 00025 // BBNumbering - Holds the numbering. 00026 UniqueVector<BasicBlock*> BBNumbering; 00027 public: 00028 StableBasicBlockNumbering(Function *F = 0) { 00029 if (F) compute(*F); 00030 } 00031 00032 /// compute - If we have not computed a numbering for the function yet, do 00033 /// so. 00034 void compute(Function &F) { 00035 if (BBNumbering.empty()) { 00036 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 00037 BBNumbering.insert(I); 00038 } 00039 } 00040 00041 /// getNumber - Return the ID number for the specified BasicBlock. 00042 /// 00043 unsigned getNumber(BasicBlock *BB) const { 00044 unsigned Idx = BBNumbering.idFor(BB); 00045 assert(Idx && "Invalid basic block or numbering not computed!"); 00046 return Idx-1; 00047 } 00048 00049 /// getBlock - Return the BasicBlock corresponding to a particular ID. 00050 /// 00051 BasicBlock *getBlock(unsigned N) const { 00052 assert(N < BBNumbering.size() && 00053 "Block ID out of range or numbering not computed!"); 00054 return BBNumbering[N+1]; 00055 } 00056 }; 00057 } 00058 00059 #endif