diff options
author | John McCall <rjmccall@apple.com> | 2010-07-03 09:25:20 +0000 |
---|---|---|
committer | John McCall <rjmccall@apple.com> | 2010-07-03 09:25:20 +0000 |
commit | c7d209fb2442a63d328a25e1036b4726b0c4a49d (patch) | |
tree | 62fd01986472e4f01191e0a5ad2f8756e5e8ee66 | |
parent | 08778269cfbf803fb216870060c60eb2adb25ef6 (diff) |
Provide convenience routines to save and restore the current insertion
point.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@107570 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/CodeGen/CGBuilder.h | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/lib/CodeGen/CGBuilder.h b/lib/CodeGen/CGBuilder.h index ed56bd9137..33d868920d 100644 --- a/lib/CodeGen/CGBuilder.h +++ b/lib/CodeGen/CGBuilder.h @@ -14,12 +14,61 @@ namespace clang { namespace CodeGen { - // Don't preserve names on values in an optimized build. + +// Don't preserve names on values in an optimized build. #ifdef NDEBUG - typedef llvm::IRBuilder<false> CGBuilderTy; +typedef llvm::IRBuilder<false> CGBuilderSuperTy; #else - typedef llvm::IRBuilder<> CGBuilderTy; +typedef llvm::IRBuilder<> CGBuilderSuperTy; #endif + +/// IR generation's wrapper around an LLVM IRBuilder. +class CGBuilderTy : public CGBuilderSuperTy { +public: + CGBuilderTy(llvm::LLVMContext &Context) : CGBuilderSuperTy(Context) {} + CGBuilderTy(llvm::BasicBlock *Block) : CGBuilderSuperTy(Block) {} + CGBuilderTy(llvm::BasicBlock *Block, llvm::BasicBlock::iterator Point) + : CGBuilderSuperTy(Block, Point) {} + + CGBuilderTy(const CGBuilderTy &Builder) + : CGBuilderSuperTy(Builder.getContext()) { + + if (Builder.GetInsertBlock()) + SetInsertPoint(Builder.GetInsertBlock(), Builder.GetInsertPoint()); + } + + /// A saved insertion point. + class InsertPoint { + llvm::BasicBlock *Block; + llvm::BasicBlock::iterator Point; + + public: + InsertPoint(llvm::BasicBlock *Block, llvm::BasicBlock::iterator Point) + : Block(Block), Point(Point) {} + + bool isSet() const { return (Block != 0); } + llvm::BasicBlock *getBlock() const { return Block; } + llvm::BasicBlock::iterator getPoint() const { return Point; } + }; + + InsertPoint saveIP() const { + return InsertPoint(GetInsertBlock(), GetInsertPoint()); + } + + InsertPoint saveAndClearIP() { + InsertPoint IP(GetInsertBlock(), GetInsertPoint()); + ClearInsertionPoint(); + return IP; + } + + void restoreIP(InsertPoint IP) { + if (IP.isSet()) + SetInsertPoint(IP.getBlock(), IP.getPoint()); + else + ClearInsertionPoint(); + } +}; + } // end namespace CodeGen } // end namespace clang |