aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/CGBuilder.h
blob: 33d868920dcbafaf0de8438c17287d1b69053613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//===-- CGBuilder.h - Choose IRBuilder implementation  ----------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef CLANG_CODEGEN_CGBUILDER_H
#define CLANG_CODEGEN_CGBUILDER_H

#include "llvm/Support/IRBuilder.h"

namespace clang {
namespace CodeGen {

// Don't preserve names on values in an optimized build.
#ifdef NDEBUG
typedef llvm::IRBuilder<false> CGBuilderSuperTy;
#else
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

#endif