blob: 33d868920dcbafaf0de8438c17287d1b69053613 [file] [log] [blame]
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001//===-- 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
15namespace clang {
16namespace CodeGen {
John McCallc7d209f2010-07-03 09:25:20 +000017
18// Don't preserve names on values in an optimized build.
Daniel Dunbar29ea6722008-11-12 00:01:12 +000019#ifdef NDEBUG
John McCallc7d209f2010-07-03 09:25:20 +000020typedef llvm::IRBuilder<false> CGBuilderSuperTy;
Daniel Dunbar29ea6722008-11-12 00:01:12 +000021#else
John McCallc7d209f2010-07-03 09:25:20 +000022typedef llvm::IRBuilder<> CGBuilderSuperTy;
Daniel Dunbar29ea6722008-11-12 00:01:12 +000023#endif
John McCallc7d209f2010-07-03 09:25:20 +000024
25/// IR generation's wrapper around an LLVM IRBuilder.
26class CGBuilderTy : public CGBuilderSuperTy {
27public:
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 Dunbar45d196b2008-11-01 01:53:16 +000072} // end namespace CodeGen
73} // end namespace clang
74
75#endif