Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1 | //===-- CGBuilder.h - Choose IRBuilder implementation ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef CLANG_CODEGEN_CGBUILDER_H |
| 11 | #define CLANG_CODEGEN_CGBUILDER_H |
| 12 | |
| 13 | #include "llvm/Support/IRBuilder.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace CodeGen { |
John McCall | c7d209f | 2010-07-03 09:25:20 +0000 | [diff] [blame^] | 17 | |
| 18 | // Don't preserve names on values in an optimized build. |
Daniel Dunbar | 29ea672 | 2008-11-12 00:01:12 +0000 | [diff] [blame] | 19 | #ifdef NDEBUG |
John McCall | c7d209f | 2010-07-03 09:25:20 +0000 | [diff] [blame^] | 20 | typedef llvm::IRBuilder<false> CGBuilderSuperTy; |
Daniel Dunbar | 29ea672 | 2008-11-12 00:01:12 +0000 | [diff] [blame] | 21 | #else |
John McCall | c7d209f | 2010-07-03 09:25:20 +0000 | [diff] [blame^] | 22 | typedef llvm::IRBuilder<> CGBuilderSuperTy; |
Daniel Dunbar | 29ea672 | 2008-11-12 00:01:12 +0000 | [diff] [blame] | 23 | #endif |
John McCall | c7d209f | 2010-07-03 09:25:20 +0000 | [diff] [blame^] | 24 | |
| 25 | /// IR generation's wrapper around an LLVM IRBuilder. |
| 26 | class CGBuilderTy : public CGBuilderSuperTy { |
| 27 | public: |
| 28 | CGBuilderTy(llvm::LLVMContext &Context) : CGBuilderSuperTy(Context) {} |
| 29 | CGBuilderTy(llvm::BasicBlock *Block) : CGBuilderSuperTy(Block) {} |
| 30 | CGBuilderTy(llvm::BasicBlock *Block, llvm::BasicBlock::iterator Point) |
| 31 | : CGBuilderSuperTy(Block, Point) {} |
| 32 | |
| 33 | CGBuilderTy(const CGBuilderTy &Builder) |
| 34 | : CGBuilderSuperTy(Builder.getContext()) { |
| 35 | |
| 36 | if (Builder.GetInsertBlock()) |
| 37 | SetInsertPoint(Builder.GetInsertBlock(), Builder.GetInsertPoint()); |
| 38 | } |
| 39 | |
| 40 | /// A saved insertion point. |
| 41 | class InsertPoint { |
| 42 | llvm::BasicBlock *Block; |
| 43 | llvm::BasicBlock::iterator Point; |
| 44 | |
| 45 | public: |
| 46 | InsertPoint(llvm::BasicBlock *Block, llvm::BasicBlock::iterator Point) |
| 47 | : Block(Block), Point(Point) {} |
| 48 | |
| 49 | bool isSet() const { return (Block != 0); } |
| 50 | llvm::BasicBlock *getBlock() const { return Block; } |
| 51 | llvm::BasicBlock::iterator getPoint() const { return Point; } |
| 52 | }; |
| 53 | |
| 54 | InsertPoint saveIP() const { |
| 55 | return InsertPoint(GetInsertBlock(), GetInsertPoint()); |
| 56 | } |
| 57 | |
| 58 | InsertPoint saveAndClearIP() { |
| 59 | InsertPoint IP(GetInsertBlock(), GetInsertPoint()); |
| 60 | ClearInsertionPoint(); |
| 61 | return IP; |
| 62 | } |
| 63 | |
| 64 | void restoreIP(InsertPoint IP) { |
| 65 | if (IP.isSet()) |
| 66 | SetInsertPoint(IP.getBlock(), IP.getPoint()); |
| 67 | else |
| 68 | ClearInsertionPoint(); |
| 69 | } |
| 70 | }; |
| 71 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 72 | } // end namespace CodeGen |
| 73 | } // end namespace clang |
| 74 | |
| 75 | #endif |