]> granicus.if.org Git - clang/blob - lib/CodeGen/CGBuilder.h
Provide convenience routines to save and restore the current insertion
[clang] / lib / CodeGen / CGBuilder.h
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 {
17
18 // Don't preserve names on values in an optimized build.
19 #ifdef NDEBUG
20 typedef llvm::IRBuilder<false> CGBuilderSuperTy;
21 #else
22 typedef llvm::IRBuilder<> CGBuilderSuperTy;
23 #endif
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
72 }  // end namespace CodeGen
73 }  // end namespace clang
74
75 #endif