diff options
author | Chris Lattner <sabre@nondot.org> | 2004-06-19 08:41:59 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2004-06-19 08:41:59 +0000 |
commit | c8fd918aa169b1f193cfe24f413167dad0a91214 (patch) | |
tree | ace50a5c97626a56b31ea327ba8a2af78491165f /include/llvm/Support/StableBasicBlockNumbering.h | |
parent | 689835a2d98ddd1e3766f5ce6816c8d908e8fff1 (diff) |
Initial checkin of the StableBasicBlockNumbering, a little helper class for computing
(strangely enough) a stable (determinstic) numbering for basic blocks.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14246 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/Support/StableBasicBlockNumbering.h')
-rw-r--r-- | include/llvm/Support/StableBasicBlockNumbering.h | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/include/llvm/Support/StableBasicBlockNumbering.h b/include/llvm/Support/StableBasicBlockNumbering.h new file mode 100644 index 0000000000..4673ca9275 --- /dev/null +++ b/include/llvm/Support/StableBasicBlockNumbering.h @@ -0,0 +1,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 |