llvm.org GIT mirror llvm / 89f6d88
Merge LLVMBuilder and FoldingBuilder, calling the result IRBuilder. Patch by Dominic Hamon. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49604 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 11 years ago
19 changed file(s) with 679 addition(s) and 877 deletion(s). Raw diff Collapse all Expand all
161161 9F7794280C73CB6100551F9C /* MipsTargetMachine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MipsTargetMachine.h; sourceTree = ""; };
162162 9F77942F0C73CB7900551F9C /* MSILWriter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MSILWriter.cpp; sourceTree = ""; };
163163 9F7794300C73CB7900551F9C /* MSILWriter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSILWriter.h; sourceTree = ""; };
164 9F7794870C73D51000551F9C /* LLVMBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LLVMBuilder.h; sourceTree = ""; };
165164 9F7794880C73D51000551F9C /* MemoryBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryBuffer.h; sourceTree = ""; };
166165 9F7794890C73D51000551F9C /* Streams.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Streams.h; sourceTree = ""; };
167166 9F7C23E50CB81C2100498408 /* Analysis.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Analysis.h; sourceTree = ""; };
10701069 DE8170AB08CFB44D0093BDEF /* TableGenBackend.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = TableGenBackend.cpp; sourceTree = ""; };
10711070 DE8170AC08CFB44D0093BDEF /* TableGenBackend.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = TableGenBackend.h; sourceTree = ""; };
10721071 DEFAB19D0959E9A100E0AB42 /* DwarfWriter.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = DwarfWriter.h; path = ../include/llvm/CodeGen/DwarfWriter.h; sourceTree = SOURCE_ROOT; };
1072 F22627320DAE34D10008F441 /* index.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = index.html; sourceTree = ""; };
1073 F22627330DAE34D20008F441 /* JITTutorial1.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = JITTutorial1.html; sourceTree = ""; };
1074 F22627340DAE34D20008F441 /* JITTutorial2-1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "JITTutorial2-1.png"; sourceTree = ""; };
1075 F22627350DAE34D20008F441 /* JITTutorial2.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = JITTutorial2.html; sourceTree = ""; };
1076 F22627360DAE34D20008F441 /* LangImpl1.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LangImpl1.html; sourceTree = ""; };
1077 F22627370DAE34D20008F441 /* LangImpl2.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LangImpl2.html; sourceTree = ""; };
1078 F22627380DAE34D20008F441 /* LangImpl3.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LangImpl3.html; sourceTree = ""; };
1079 F22627390DAE34D20008F441 /* LangImpl4.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LangImpl4.html; sourceTree = ""; };
1080 F226273A0DAE34D20008F441 /* LangImpl5-cfg.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "LangImpl5-cfg.png"; sourceTree = ""; };
1081 F226273B0DAE34D20008F441 /* LangImpl5.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LangImpl5.html; sourceTree = ""; };
1082 F226273C0DAE34D20008F441 /* LangImpl6.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LangImpl6.html; sourceTree = ""; };
1083 F226273D0DAE34D20008F441 /* LangImpl7.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LangImpl7.html; sourceTree = ""; };
1084 F226273E0DAE34D20008F441 /* LangImpl8.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LangImpl8.html; sourceTree = ""; };
1085 F226273F0DAE34D20008F441 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; };
1086 F22627400DAE34D20008F441 /* OCamlLangImpl1.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = OCamlLangImpl1.html; sourceTree = ""; };
1087 F22627410DAE34D20008F441 /* OCamlLangImpl2.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = OCamlLangImpl2.html; sourceTree = ""; };
1088 F22627420DAE34D20008F441 /* OCamlLangImpl3.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = OCamlLangImpl3.html; sourceTree = ""; };
1089 F22627430DAE34D20008F441 /* OCamlLangImpl4.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = OCamlLangImpl4.html; sourceTree = ""; };
1090 F22627440DAE34D20008F441 /* OCamlLangImpl5.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = OCamlLangImpl5.html; sourceTree = ""; };
1091 F22627450DAE34D20008F441 /* OCamlLangImpl6.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = OCamlLangImpl6.html; sourceTree = ""; };
1092 F22627460DAE34D20008F441 /* OCamlLangImpl7.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = OCamlLangImpl7.html; sourceTree = ""; };
1093 F22761DF0DAD09CD003D8065 /* BrainF.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = BrainF.cpp; path = BrainF/BrainF.cpp; sourceTree = ""; };
1094 F22761E00DAD09CD003D8065 /* BrainF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BrainF.h; path = BrainF/BrainF.h; sourceTree = ""; };
1095 F22761E10DAD09CD003D8065 /* BrainFDriver.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = BrainFDriver.cpp; path = BrainF/BrainFDriver.cpp; sourceTree = ""; };
1096 F27C8CE90DAD2EF900A33844 /* IRBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IRBuilder.h; sourceTree = ""; };
1097 F27C8CFF0DAD307700A33844 /* ShadowStackCollector.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ShadowStackCollector.cpp; sourceTree = ""; };
10731098 /* End PBXFileReference section */
10741099
10751100 /* Begin PBXGroup section */
15791604 DE66ED3E08ABEC2A00323D32 /* lib/CodeGen */ = {
15801605 isa = PBXGroup;
15811606 children = (
1607 F27C8CFF0DAD307700A33844 /* ShadowStackCollector.cpp */,
15821608 754221420D171DFC00DDB61B /* MachineLICM.cpp */,
15831609 9FE450AB0C77AB6100C4FEA4 /* README.txt */,
15841610 DE66ED8308ABEC2B00323D32 /* SelectionDAG */,
24162442 DE66F26E08ABF03200323D32 /* Support */ = {
24172443 isa = PBXGroup;
24182444 children = (
2445 F27C8CE90DAD2EF900A33844 /* IRBuilder.h */,
24192446 DE66F27008ABF03200323D32 /* AIXDataTypesFix.h */,
24202447 9F5B90CE0D0CE89300CDFDEA /* AlignOf.h */,
24212448 CF8F1B430B64F74400BB4199 /* Allocator.h */,
24392466 DE66F28108ABF03200323D32 /* InstIterator.h */,
24402467 DE66F28208ABF03200323D32 /* InstVisitor.h */,
24412468 DE66F28308ABF03200323D32 /* LeakDetector.h */,
2442 9F7794870C73D51000551F9C /* LLVMBuilder.h */,
24432469 CF8F1B460B64F74400BB4199 /* ManagedStatic.h */,
24442470 DE66F28408ABF03200323D32 /* Mangler.h */,
24452471 DE66F28508ABF03200323D32 /* MathExtras.h */,
26302656 DE66F38D08ABF35C00323D32 /* docs */ = {
26312657 isa = PBXGroup;
26322658 children = (
2659 F22627310DAE34D10008F441 /* tutorial */,
26332660 DE66F38F08ABF35C00323D32 /* AliasAnalysis.html */,
26342661 DE66F39008ABF35C00323D32 /* Bugpoint.html */,
26352662 DE66F39208ABF35C00323D32 /* GCCFEBuildInstrs.html */,
27162743 DE66F3FD08ABF37000323D32 /* examples */ = {
27172744 isa = PBXGroup;
27182745 children = (
2746 F22761DF0DAD09CD003D8065 /* BrainF.cpp */,
2747 F22761E00DAD09CD003D8065 /* BrainF.h */,
2748 F22761E10DAD09CD003D8065 /* BrainFDriver.cpp */,
27192749 DE66F40E08ABF37000323D32 /* fibonacci.cpp */,
27202750 DE66F41508ABF37000323D32 /* HowToUseJIT.cpp */,
27212751 DE66F41E08ABF37000323D32 /* ModuleMaker.cpp */,
27702800 path = TableGen;
27712801 sourceTree = "";
27722802 };
2803 F22627310DAE34D10008F441 /* tutorial */ = {
2804 isa = PBXGroup;
2805 children = (
2806 F22627320DAE34D10008F441 /* index.html */,
2807 F22627330DAE34D20008F441 /* JITTutorial1.html */,
2808 F22627340DAE34D20008F441 /* JITTutorial2-1.png */,
2809 F22627350DAE34D20008F441 /* JITTutorial2.html */,
2810 F22627360DAE34D20008F441 /* LangImpl1.html */,
2811 F22627370DAE34D20008F441 /* LangImpl2.html */,
2812 F22627380DAE34D20008F441 /* LangImpl3.html */,
2813 F22627390DAE34D20008F441 /* LangImpl4.html */,
2814 F226273A0DAE34D20008F441 /* LangImpl5-cfg.png */,
2815 F226273B0DAE34D20008F441 /* LangImpl5.html */,
2816 F226273C0DAE34D20008F441 /* LangImpl6.html */,
2817 F226273D0DAE34D20008F441 /* LangImpl7.html */,
2818 F226273E0DAE34D20008F441 /* LangImpl8.html */,
2819 F226273F0DAE34D20008F441 /* Makefile */,
2820 F22627400DAE34D20008F441 /* OCamlLangImpl1.html */,
2821 F22627410DAE34D20008F441 /* OCamlLangImpl2.html */,
2822 F22627420DAE34D20008F441 /* OCamlLangImpl3.html */,
2823 F22627430DAE34D20008F441 /* OCamlLangImpl4.html */,
2824 F22627440DAE34D20008F441 /* OCamlLangImpl5.html */,
2825 F22627450DAE34D20008F441 /* OCamlLangImpl6.html */,
2826 F22627460DAE34D20008F441 /* OCamlLangImpl7.html */,
2827 );
2828 path = tutorial;
2829 sourceTree = "";
2830 };
27732831 /* End PBXGroup section */
27742832
27752833 /* Begin PBXLegacyTarget section */
775775 }
776776
777777 static struct custom_operations builder_ops = {
778 (char *) "LLVMBuilder",
778 (char *) "IRBuilder",
779779 llvm_finalize_builder,
780780 custom_compare_default,
781781 custom_hash_default,
5959 #include <llvm/CallingConv.h>
6060 #include <llvm/Analysis/Verifier.h>
6161 #include <llvm/Assembly/PrintModulePass.h>
62 #include <llvm/Support/LLVMBuilder.h>
62 #include <llvm/Support/IRBuilder.h>
6363
6464
6565
142142
143143

                  
                
144144 BasicBlock* block = new BasicBlock("entry", mul_add);
145 LLVMBuilder builder(block);
145 IRBuilder builder(block);
146146
147147
148148
149

We create a new basic block, as you might expect, by calling its constructor. All we need to tell it is its name and the function to which it belongs. In addition, we’re creating an LLVMBuilder object, which is a convenience interface for creating instructions and appending them to the end of a block. Instructions can be created through their constructors as well, but some of their interfaces are quite complicated. Unless you need a lot of control, using LLVMBuilder will make your life simpler.

149

We create a new basic block, as you might expect, by calling its constructor. All we need to tell it is its name and the function to which it belongs. In addition, we’re creating an IRBuilder object, which is a convenience interface for creating instructions and appending them to the end of a block. Instructions can be created through their constructors as well, but some of their interfaces are quite complicated. Unless you need a lot of control, using IRBuilder will make your life simpler.

150150
151151
152152

                  
                
162162
163163
164164
165

The final step in creating our function is to create the instructions that make it up. Our mul_add function is composed of just three instructions: a multiply, an add, and a return. LLVMBuilder gives us a simple interface for constructing these instructions and appending them to the “entry” block. Each of the calls to LLVMBuilder returns a Value* that represents the value yielded by the instruction. You’ll also notice that, above, x, y, and z are also Value*’s, so it’s clear that instructions operate on Value*’s.

165

The final step in creating our function is to create the instructions that make it up. Our mul_add function is composed of just three instructions: a multiply, an add, and a return. IRBuilder gives us a simple interface for constructing these instructions and appending them to the “entry” block. Each of the calls to IRBuilder returns a Value* that represents the value yielded by the instruction. You’ll also notice that, above, x, y, and z are also Value*’s, so it’s clear that instructions operate on Value*’s.

166166
167167

And that’s it! Now you can compile and run your code, and get a wonderful textual print out of the LLVM IR we saw at the beginning. To compile, use the following command line as a guide:

168168
5555 #include <llvm/PassManager.h>
5656 #include <llvm/Analysis/Verifier.h>
5757 #include <llvm/Assembly/PrintModulePass.h>
58 #include <llvm/Support/LLVMBuilder.h>
58 #include <llvm/Support/IRBuilder.h>
5959
6060 using namespace llvm;
6161
109109
110110
111111

                  
                
112 LLVMBuilder builder(entry);
112 IRBuilder builder(entry);
113113 Value* xEqualsY = builder.CreateICmpEQ(x, y, "tmp");
114114 builder.CreateCondBr(xEqualsY, ret, cond_false);
115115
116116
117117
118

Our next block, ret, is pretty simple: it just returns the value of x. Recall that this block is only reached if x == y, so this is the correct behavior. Notice that instead of creating a new LLVMBuilder for each block, we can use SetInsertPoint to retarget our existing one. This saves on construction and memory allocation costs.

118

Our next block, ret, is pretty simple: it just returns the value of x. Recall that this block is only reached if x == y, so this is the correct behavior. Notice that instead of creating a new IRBuilder for each block, we can use SetInsertPoint to retarget our existing one. This saves on construction and memory allocation costs.

119119
120120
121121

                  
                
110110 Value *ErrorV(const char *Str) { Error(Str); return 0; }
111111
112112 static Module *TheModule;
113 static LLVMBuilder Builder;
113 static IRBuilder Builder;
114114 static std::map<std::string, Value*> NamedValues;
115115
116116
122122
123123

The Builder object is a helper object that makes it easy to generate

124124 LLVM instructions. Instances of the
125 href="http://llvm.org/doxygen/LLVMBuilder_8h-source.html">LLVMBuilder
125 href="http://llvm.org/doxygen/IRBuilder_8h-source.html">IRBuilder
126126 class keep track of the current place to insert instructions and has methods to
127127 create new instructions.

128128
215215

216216
217217

In the example above, the LLVM builder class is starting to show its value.

218 LLVMBuilder knows where to insert the newly created instruction, all you have to
218 IRBuilder knows where to insert the newly created instruction, all you have to
219219 do is specify what instruction to create (e.g. with CreateAdd), which
220220 operands to use (L and R here) and optionally provide a name
221221 for the generated instruction.

679679 #include "llvm/DerivedTypes.h"
680680 #include "llvm/Module.h"
681681 #include "llvm/Analysis/Verifier.h"
682 #include "llvm/Support/LLVMBuilder.h"
682 #include "llvm/Support/IRBuilder.h"
683683 #include <cstdio>
684684 #include <string>
685685 #include <map>
10221022 //===----------------------------------------------------------------------===//
10231023
10241024 static Module *TheModule;
1025 static LLVMBuilder Builder;
1025 static IRBuilder Builder;
10261026 static std::map<std::string, Value*> NamedValues;
10271027
10281028 Value *ErrorV(const char *Str) { Error(Str); return 0; }
5555
5656

5757 Our demonstration for Chapter 3 is elegant and easy to extend. Unfortunately,
58 it does not produce wonderful code. For example, when compiling simple code,
59 we don't get obvious optimizations:

60
61
62

                  
                
63 ready> def test(x) 1+2+x;
64 Read function definition:
65 define double @test(double %x) {
66 entry:
67 %addtmp = add double 1.000000e+00, 2.000000e+00
68 %addtmp1 = add double %addtmp, %x
69 ret double %addtmp1
70 }
71
72
73
74

This code is a very, very literal transcription of the AST built by parsing

75 the input. As such, this transcription lacks optimizations like constant folding (we'd like to get "add x, 3.0" in the example above) as well as other more important
76 optimizations. Constant folding, in particular, is a very common and very
77 important optimization: so much so that many language implementors implement
78 constant folding support in their AST representation.

79
80

With LLVM, you don't need this support in the AST. Since all calls to build LLVM IR go through

81 the LLVM builder, it would be nice if the builder itself checked to see if there
82 was a constant folding opportunity when you call it. If so, it could just do
83 the constant fold and return the constant instead of creating an instruction.
84 This is exactly what the LLVMFoldingBuilder class does. Lets make one
85 change:
86
87
88

                  
                
89 static LLVMFoldingBuilder Builder;
90
91
92
93

All we did was switch from LLVMBuilder to

94 LLVMFoldingBuilder. Though we change no other code, we now have all of our
95 instructions implicitly constant folded without us having to do anything
96 about it. For example, the input above now compiles to:

58 it does not produce wonderful code. The IRBuilder, however, does give us
59 obvious optimizations when compiling simple code:

9760
9861
9962

                  
                
10770
10871
10972
73

This code is not a literal transcription of the AST built by parsing the

74 input. That would be:
75
76
77

                  
                
78 ready> def test(x) 1+2+x;
79 Read function definition:
80 define double @test(double %x) {
81 entry:
82 %addtmp = add double 2.000000e+00, 1.000000e+00
83 %addtmp1 = add double %addtmp, %x
84 ret double %addtmp1
85 }
86
87
88
89 Constant folding, as seen above, in particular, is a very common and very
90 important optimization: so much so that many language implementors implement
91 constant folding support in their AST representation.

92
93

With LLVM, you don't need this support in the AST. Since all calls to build

94 LLVM IR go through the LLVM IR builder, the builder itself checked to see if
95 there was a constant folding opportunity when you call it. If so, it just does
96 the constant fold and return the constant instead of creating an instruction.
97
11098

Well, that was easy :). In practice, we recommend always using

111 LLVMFoldingBuilder when generating code like this. It has no
99 IRBuilder when generating code like this. It has no
112100 "syntactic overhead" for its use (you don't have to uglify your compiler with
113101 constant checks everywhere) and it can dramatically reduce the amount of
114102 LLVM IR that is generated in some cases (particular for languages with a macro
115103 preprocessor or that use a lot of constants).

116104
117

On the other hand, the LLVMFoldingBuilder is limited by the fact

105

On the other hand, the IRBuilder is limited by the fact

118106 that it does all of its analysis inline with the code as it is built. If you
119107 take a slightly more complex example:

120108
524512 #include "llvm/Analysis/Verifier.h"
525513 #include "llvm/Target/TargetData.h"
526514 #include "llvm/Transforms/Scalar.h"
527 #include "llvm/Support/LLVMBuilder.h"
515 #include "llvm/Support/IRBuilder.h"
528516 #include <cstdio>
529517 #include <string>
530518 #include <map>
867855 //===----------------------------------------------------------------------===//
868856
869857 static Module *TheModule;
870 static LLVMFoldingBuilder Builder;
858 static IRBuilder Builder;
871859 static std::map<std::string, Value*> NamedValues;
872860 static FunctionPassManager *TheFPM;
873861
399399
400400

Once the blocks are created, we can emit the conditional branch that chooses

401401 between them. Note that creating new blocks does not implicitly affect the
402 LLVMBuilder, so it is still inserting into the block that the condition
402 IRBuilder, so it is still inserting into the block that the condition
403403 went into. Also note that it is creating a branch to the "then" block and the
404404 "else" block, even though the "else" block isn't inserted into the function yet.
405405 This is all ok: it is the standard way that LLVM supports forward
906906 #include "llvm/Analysis/Verifier.h"
907907 #include "llvm/Target/TargetData.h"
908908 #include "llvm/Transforms/Scalar.h"
909 #include "llvm/Support/LLVMBuilder.h"
909 #include "llvm/Support/IRBuilder.h"
910910 #include <cstdio>
911911 #include <string>
912912 #include <map>
13511351 //===----------------------------------------------------------------------===//
13521352
13531353 static Module *TheModule;
1354 static LLVMFoldingBuilder Builder;
1354 static IRBuilder Builder;
13551355 static std::map<std::string, Value*> NamedValues;
13561356 static FunctionPassManager *TheFPM;
13571357
826826 #include "llvm/Analysis/Verifier.h"
827827 #include "llvm/Target/TargetData.h"
828828 #include "llvm/Transforms/Scalar.h"
829 #include "llvm/Support/LLVMBuilder.h"
829 #include "llvm/Support/IRBuilder.h"
830830 #include <cstdio>
831831 #include <string>
832832 #include <map>
13561356 //===----------------------------------------------------------------------===//
13571357
13581358 static Module *TheModule;
1359 static LLVMFoldingBuilder Builder;
1359 static IRBuilder Builder;
13601360 static std::map<std::string, Value*> NamedValues;
13611361 static FunctionPassManager *TheFPM;
13621362
421421 /// the function. This is used for mutable variables etc.
422422 static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
423423 const std::string &VarName) {
424 LLVMBuilder TmpB(&TheFunction->getEntryBlock(),
425 TheFunction->getEntryBlock().begin());
424 IRBuilder TmpB(&TheFunction->getEntryBlock(),
425 TheFunction->getEntryBlock().begin());
426426 return TmpB.CreateAlloca(Type::DoubleTy, 0, VarName.c_str());
427427 }
428428
429429
430430
431

This funny looking code creates an LLVMBuilder object that is pointing at

431

This funny looking code creates an IRBuilder object that is pointing at

432432 the first instruction (.begin()) of the entry block. It then creates an alloca
433433 with the expected name and returns it. Because all values in Kaleidoscope are
434434 doubles, there is no need to pass in a type to use.

10081008 #include "llvm/Analysis/Verifier.h"
10091009 #include "llvm/Target/TargetData.h"
10101010 #include "llvm/Transforms/Scalar.h"
1011 #include "llvm/Support/LLVMBuilder.h"
1011 #include "llvm/Support/IRBuilder.h"
10121012 #include <cstdio>
10131013 #include <string>
10141014 #include <map>
16041604 //===----------------------------------------------------------------------===//
16051605
16061606 static Module *TheModule;
1607 static LLVMFoldingBuilder Builder;
1607 static IRBuilder Builder;
16081608 static std::map<std::string, AllocaInst*> NamedValues;
16091609 static FunctionPassManager *TheFPM;
16101610
16141614 /// the function. This is used for mutable variables etc.
16151615 static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
16161616 const std::string &VarName) {
1617 LLVMBuilder TmpB(&TheFunction->getEntryBlock(),
1618 TheFunction->getEntryBlock().begin());
1617 IRBuilder TmpB(&TheFunction->getEntryBlock(),
1618 TheFunction->getEntryBlock().begin());
16191619 return TmpB.CreateAlloca(Type::DoubleTy, 0, VarName.c_str());
16201620 }
16211621
107107
108108

The Codegen.builder object is a helper object that makes it easy to

109109 generate LLVM instructions. Instances of the
110 href="http://llvm.org/doxygen/LLVMBuilder_8h-source.html">LLVMBuilder
110 href="http://llvm.org/doxygen/IRBuilder_8h-source.html">IRBuilder
111111 class keep track of the current place to insert instructions and has methods to
112112 create new instructions.

113113
193193

194194
195195

In the example above, the LLVM builder class is starting to show its value.

196 LLVMBuilder knows where to insert the newly created instruction, all you have to
196 IRBuilder knows where to insert the newly created instruction, all you have to
197197 do is specify what instruction to create (e.g. with Llvm.create_add),
198198 which operands to use (lhs and rhs here) and optionally
199199 provide a name for the generated instruction.

5757
5858
5959
60

Note: the ocaml bindings already use LLVMFoldingBuilder.

60

Note: the default IRBuilder now always includes the constant

61 folding optimisations below.

6162
6263

6364 Our demonstration for Chapter 3 is elegant and easy to extend. Unfortunately,
454454
455455

Once the blocks are created, we can emit the conditional branch that chooses

456456 between them. Note that creating new blocks does not implicitly affect the
457 LLVMBuilder, so it is still inserting into the block that the condition
457 IRBuilder, so it is still inserting into the block that the condition
458458 went into. This is why we needed to save the "start" block.

459459
460460
7070 brainf_func = cast(module->
7171 getOrInsertFunction("brainf", Type::VoidTy, NULL));
7272
73 builder = new LLVMBuilder(BasicBlock::Create(label, brainf_func));
73 builder = new IRBuilder(BasicBlock::Create(label, brainf_func));
7474
7575 //%arr = malloc i8, i32 %d
7676 ConstantInt *val_mem = ConstantInt::get(APInt(32, memtotal));
192192 Value *tape_0 = getchar_call;
193193
194194 //%tape.%d = trunc i32 %tape.%d to i8
195 TruncInst *tape_1 = builder->
195 Value *tape_1 = builder->
196196 CreateTrunc(tape_0, IntegerType::Int8Ty, tapereg);
197197
198198 //store i8 %tape.%d, i8 *%head.%d
206206 LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
207207
208208 //%tape.%d = sext i8 %tape.%d to i32
209 SExtInst *tape_1 = builder->
209 Value *tape_1 = builder->
210210 CreateSExt(tape_0, IntegerType::Int32Ty, tapereg);
211211
212212 //call i32 @putchar(i32 %tape.%d)
231231 if (comflag & flag_arraybounds)
232232 {
233233 //%test.%d = icmp uge i8 *%head.%d, %arrmax
234 ICmpInst *test_0 = builder->
234 Value *test_0 = builder->
235235 CreateICmpUGE(curhead, ptr_arrmax, testreg);
236236
237237 //%test.%d = icmp ult i8 *%head.%d, %arr
238 ICmpInst *test_1 = builder->
238 Value *test_1 = builder->
239239 CreateICmpULT(curhead, ptr_arr, testreg);
240240
241241 //%test.%d = or i1 %test.%d, %test.%d
242 BinaryOperator *test_2 = builder->
242 Value *test_2 = builder->
243243 CreateOr(test_0, test_1, testreg);
244244
245245 //br i1 %test.%d, label %main.%d, label %main.%d
258258 LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
259259
260260 //%tape.%d = add i8 %tape.%d, %d
261 BinaryOperator *tape_1 = builder->
261 Value *tape_1 = builder->
262262 CreateAdd(tape_0, ConstantInt::get(APInt(8, curvalue)), tapereg);
263263
264264 //store i8 %tape.%d, i8 *%head.%d\n"
1515 #define BRAINF_H
1616
1717 #include "llvm/Module.h"
18 #include "llvm/Support/LLVMBuilder.h"
18 #include "llvm/Support/IRBuilder.h"
1919
2020 using namespace llvm;
2121
8383 BasicBlock *aberrorbb;
8484
8585 /// Variables
86 LLVMBuilder *builder;
86 IRBuilder *builder;
8787 Value *curhead;
8888 };
8989
0 //===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the IRBuilder class, which is used as a convenient way
10 // to create LLVM instructions with a consistent and simplified interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_IRBUILDER_H
15 #define LLVM_SUPPORT_IRBUILDER_H
16
17 #include "llvm/BasicBlock.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Constants.h"
20
21 namespace llvm {
22
23 /// IRBuilder - This provides a uniform API for creating instructions and
24 /// inserting them into a basic block: either at the end of a BasicBlock, or
25 /// at a specific iterator location in a block.
26 ///
27 /// Note that the builder does not expose the full generality of LLVM
28 /// instructions. For example, it cannot be used to create instructions with
29 /// arbitrary names (specifically, names with nul characters in them) - It only
30 /// supports nul-terminated C strings. For fully generic names, use
31 /// I->setName(). For access to extra instruction properties, use the mutators
32 /// (e.g. setVolatile) on the instructions after they have been created.
33 class IRBuilder {
34 BasicBlock *BB;
35 BasicBlock::iterator InsertPt;
36 public:
37 IRBuilder() { ClearInsertionPoint(); }
38 explicit IRBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
39 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
40 SetInsertPoint(TheBB, IP);
41 }
42
43 //===--------------------------------------------------------------------===//
44 // Builder configuration methods
45 //===--------------------------------------------------------------------===//
46
47 /// ClearInsertionPoint - Clear the insertion point: created instructions will
48 /// not be inserted into a block.
49 void ClearInsertionPoint() {
50 BB = 0;
51 }
52
53 BasicBlock *GetInsertBlock() const { return BB; }
54
55 /// SetInsertPoint - This specifies that created instructions should be
56 /// appended to the end of the specified block.
57 void SetInsertPoint(BasicBlock *TheBB) {
58 BB = TheBB;
59 InsertPt = BB->end();
60 }
61
62 /// SetInsertPoint - This specifies that created instructions should be
63 /// inserted at the specified point.
64 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
65 BB = TheBB;
66 InsertPt = IP;
67 }
68
69 /// Insert - Insert and return the specified instruction.
70 template
71 InstTy *Insert(InstTy *I) const {
72 InsertHelper(I);
73 return I;
74 }
75
76 /// InsertHelper - Insert the specified instruction at the specified insertion
77 /// point. This is split out of Insert so that it isn't duplicated for every
78 /// template instantiation.
79 void InsertHelper(Instruction *I) const {
80 if (BB) BB->getInstList().insert(InsertPt, I);
81 }
82
83 //===--------------------------------------------------------------------===//
84 // Instruction creation methods: Terminators
85 //===--------------------------------------------------------------------===//
86
87 /// CreateRetVoid - Create a 'ret void' instruction.
88 ReturnInst *CreateRetVoid() {
89 return Insert(ReturnInst::Create());
90 }
91
92 /// @verbatim
93 /// CreateRet - Create a 'ret ' instruction.
94 /// @endverbatim
95 ReturnInst *CreateRet(Value *V) {
96 return Insert(ReturnInst::Create(V));
97 }
98
99 ReturnInst *CreateRet(Value * const* retVals, unsigned N) {
100 return Insert(ReturnInst::Create(retVals, N));
101 }
102
103 GetResultInst *CreateGetResult(Value *V, unsigned Index,
104 const char *Name = "") {
105 return Insert(new GetResultInst(V, Index, Name));
106 }
107
108 /// CreateBr - Create an unconditional 'br label X' instruction.
109 BranchInst *CreateBr(BasicBlock *Dest) {
110 return Insert(BranchInst::Create(Dest));
111 }
112
113 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
114 /// instruction.
115 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
116 return Insert(BranchInst::Create(True, False, Cond));
117 }
118
119 /// CreateSwitch - Create a switch instruction with the specified value,
120 /// default dest, and with a hint for the number of cases that will be added
121 /// (for efficient allocation).
122 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
123 return Insert(SwitchInst::Create(V, Dest, NumCases));
124 }
125
126 /// CreateInvoke - Create an invoke instruction.
127 template
128 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
129 BasicBlock *UnwindDest, InputIterator ArgBegin,
130 InputIterator ArgEnd, const char *Name = "") {
131 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
132 ArgBegin, ArgEnd, Name));
133 }
134
135 UnwindInst *CreateUnwind() {
136 return Insert(new UnwindInst());
137 }
138
139 UnreachableInst *CreateUnreachable() {
140 return Insert(new UnreachableInst());
141 }
142
143 //===--------------------------------------------------------------------===//
144 // Instruction creation methods: Binary Operators
145 //===--------------------------------------------------------------------===//
146
147 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
148 if (Constant *LC = dyn_cast(LHS))
149 if (Constant *RC = dyn_cast(RHS))
150 return ConstantExpr::getAdd(LC, RC);
151 return Insert(BinaryOperator::createAdd(LHS, RHS, Name));
152 }
153 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
154 if (Constant *LC = dyn_cast(LHS))
155 if (Constant *RC = dyn_cast(RHS))
156 return ConstantExpr::getSub(LC, RC);
157 return Insert(BinaryOperator::createSub(LHS, RHS, Name));
158 }
159 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
160 if (Constant *LC = dyn_cast(LHS))
161 if (Constant *RC = dyn_cast(RHS))
162 return ConstantExpr::getMul(LC, RC);
163 return Insert(BinaryOperator::createMul(LHS, RHS, Name));
164 }
165 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
166 if (Constant *LC = dyn_cast(LHS))
167 if (Constant *RC = dyn_cast(RHS))
168 return ConstantExpr::getUDiv(LC, RC);
169 return Insert(BinaryOperator::createUDiv(LHS, RHS, Name));
170 }
171 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
172 if (Constant *LC = dyn_cast(LHS))
173 if (Constant *RC = dyn_cast(RHS))
174 return ConstantExpr::getSDiv(LC, RC);
175 return Insert(BinaryOperator::createSDiv(LHS, RHS, Name));
176 }
177 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
178 if (Constant *LC = dyn_cast(LHS))
179 if (Constant *RC = dyn_cast(RHS))
180 return ConstantExpr::getFDiv(LC, RC);
181 return Insert(BinaryOperator::createFDiv(LHS, RHS, Name));
182 }
183 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
184 if (Constant *LC = dyn_cast(LHS))
185 if (Constant *RC = dyn_cast(RHS))
186 return ConstantExpr::getURem(LC, RC);
187 return Insert(BinaryOperator::createURem(LHS, RHS, Name));
188 }
189 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
190 if (Constant *LC = dyn_cast(LHS))
191 if (Constant *RC = dyn_cast(RHS))
192 return ConstantExpr::getSRem(LC, RC);
193 return Insert(BinaryOperator::createSRem(LHS, RHS, Name));
194 }
195 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
196 if (Constant *LC = dyn_cast(LHS))
197 if (Constant *RC = dyn_cast(RHS))
198 return ConstantExpr::getFRem(LC, RC);
199 return Insert(BinaryOperator::createFRem(LHS, RHS, Name));
200 }
201 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
202 if (Constant *LC = dyn_cast(LHS))
203 if (Constant *RC = dyn_cast(RHS))
204 return ConstantExpr::getShl(LC, RC);
205 return Insert(BinaryOperator::createShl(LHS, RHS, Name));
206 }
207 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
208 if (Constant *LC = dyn_cast(LHS))
209 if (Constant *RC = dyn_cast(RHS))
210 return ConstantExpr::getLShr(LC, RC);
211 return Insert(BinaryOperator::createLShr(LHS, RHS, Name));
212 }
213 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
214 if (Constant *LC = dyn_cast(LHS))
215 if (Constant *RC = dyn_cast(RHS))
216 return ConstantExpr::getAShr(LC, RC);
217 return Insert(BinaryOperator::createAShr(LHS, RHS, Name));
218 }
219 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
220 if (Constant *LC = dyn_cast(LHS))
221 if (Constant *RC = dyn_cast(RHS))
222 return ConstantExpr::getAnd(LC, RC);
223 return Insert(BinaryOperator::createAnd(LHS, RHS, Name));
224 }
225 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
226 if (Constant *LC = dyn_cast(LHS))
227 if (Constant *RC = dyn_cast(RHS))
228 return ConstantExpr::getOr(LC, RC);
229 return Insert(BinaryOperator::createOr(LHS, RHS, Name));
230 }
231 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
232 if (Constant *LC = dyn_cast(LHS))
233 if (Constant *RC = dyn_cast(RHS))
234 return ConstantExpr::getXor(LC, RC);
235 return Insert(BinaryOperator::createXor(LHS, RHS, Name));
236 }
237
238 BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
239 Value *LHS, Value *RHS, const char *Name = "") {
240 return Insert(BinaryOperator::create(Opc, LHS, RHS, Name));
241 }
242
243 BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
244 return Insert(BinaryOperator::createNeg(V, Name));
245 }
246 BinaryOperator *CreateNot(Value *V, const char *Name = "") {
247 return Insert(BinaryOperator::createNot(V, Name));
248 }
249
250 //===--------------------------------------------------------------------===//
251 // Instruction creation methods: Memory Instructions
252 //===--------------------------------------------------------------------===//
253
254 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
255 const char *Name = "") {
256 return Insert(new MallocInst(Ty, ArraySize, Name));
257 }
258 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
259 const char *Name = "") {
260 return Insert(new AllocaInst(Ty, ArraySize, Name));
261 }
262 FreeInst *CreateFree(Value *Ptr) {
263 return Insert(new FreeInst(Ptr));
264 }
265 LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
266 return Insert(new LoadInst(Ptr, Name));
267 }
268 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
269 return Insert(new LoadInst(Ptr, Name, isVolatile));
270 }
271 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
272 return Insert(new StoreInst(Val, Ptr, isVolatile));
273 }
274 template
275 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin,
276 InputIterator IdxEnd, const char *Name = "") {
277
278 if (Constant *PC = dyn_cast(Ptr)) {
279 // Every index must be constant.
280 InputIterator i;
281 for (i = IdxBegin; i < IdxEnd; ++i) {
282 if (!dyn_cast(*i))
283 break;
284 }
285 if (i == IdxEnd)
286 return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
287 }
288 return(Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd, Name)));
289 }
290 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
291 if (Constant *PC = dyn_cast(Ptr))
292 if (Constant *IC = dyn_cast(Idx))
293 return ConstantExpr::getGetElementPtr(PC, &IC, 1);
294 return Insert(GetElementPtrInst::Create(Ptr, Idx, Name));
295 }
296 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
297 llvm::Value *Idxs[] = {
298 ConstantInt::get(llvm::Type::Int32Ty, 0),
299 ConstantInt::get(llvm::Type::Int32Ty, Idx)
300 };
301
302 if (Constant *PC = dyn_cast(Ptr))
303 return ConstantExpr::getGetElementPtr(PC, Idxs, 2);
304
305 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2, Name));
306 }
307
308 //===--------------------------------------------------------------------===//
309 // Instruction creation methods: Cast/Conversion Operators
310 //===--------------------------------------------------------------------===//
311
312 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
313 return CreateCast(Instruction::Trunc, V, DestTy, Name);
314 }
315 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
316 return CreateCast(Instruction::ZExt, V, DestTy, Name);
317 }
318 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
319 return CreateCast(Instruction::SExt, V, DestTy, Name);
320 }
321 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
322 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
323 }
324 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
325 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
326 }
327 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
328 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
329 }
330 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
331 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
332 }
333 Value *CreateFPTrunc(Value *V, const Type *DestTy,
334 const char *Name = "") {
335 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
336 }
337 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
338 return CreateCast(Instruction::FPExt, V, DestTy, Name);
339 }
340 Value *CreatePtrToInt(Value *V, const Type *DestTy,
341 const char *Name = "") {
342 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
343 }
344 Value *CreateIntToPtr(Value *V, const Type *DestTy,
345 const char *Name = "") {
346 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
347 }
348 Value *CreateBitCast(Value *V, const Type *DestTy,
349 const char *Name = "") {
350 return CreateCast(Instruction::BitCast, V, DestTy, Name);
351 }
352
353 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
354 const char *Name = "") {
355 if (V->getType() == DestTy)
356 return V;
357 if (Constant *VC = dyn_cast(V))
358 return ConstantExpr::getCast(Op, VC, DestTy);
359 return Insert(CastInst::create(Op, V, DestTy, Name));
360 }
361 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
362 const char *Name = "") {
363 if (V->getType() == DestTy)
364 return V;
365 if (Constant *VC = dyn_cast(V))
366 return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
367 return Insert(CastInst::createIntegerCast(V, DestTy, isSigned, Name));
368 }
369
370 //===--------------------------------------------------------------------===//
371 // Instruction creation methods: Compare Instructions
372 //===--------------------------------------------------------------------===//
373
374 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
375 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
376 }
377 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
378 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
379 }
380 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
381 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
382 }
383 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
384 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
385 }
386 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
387 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
388 }
389 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
390 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
391 }
392 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
393 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
394 }
395 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
396 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
397 }
398 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
399 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
400 }
401 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
402 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
403 }
404
405 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
406 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
407 }
408 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
409 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
410 }
411 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
412 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
413 }
414 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
415 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
416 }
417 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
418 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
419 }
420 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
421 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
422 }
423 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
424 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
425 }
426 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
427 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
428 }
429 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
430 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
431 }
432 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
433 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
434 }
435 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
436 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
437 }
438 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
439 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
440 }
441 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
442 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
443 }
444 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
445 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
446 }
447
448 Value *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS,
449 const char *Name = "") {
450 if (Constant *LC = dyn_cast(LHS))
451 if (Constant *RC = dyn_cast(RHS))
452 return ConstantExpr::getCompare(P, LC, RC);
453 return Insert(new ICmpInst(P, LHS, RHS, Name));
454 }
455 Value *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS,
456 const char *Name = "") {
457 if (Constant *LC = dyn_cast(LHS))
458 if (Constant *RC = dyn_cast(RHS))
459 return ConstantExpr::getCompare(P, LC, RC);
460 return Insert(new FCmpInst(P, LHS, RHS, Name));
461 }
462
463 //===--------------------------------------------------------------------===//
464 // Instruction creation methods: Other Instructions
465 //===--------------------------------------------------------------------===//
466
467 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
468 return Insert(PHINode::Create(Ty, Name));
469 }
470
471 CallInst *CreateCall(Value *Callee, const char *Name = "") {
472 return Insert(CallInst::Create(Callee, Name));
473 }
474 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
475 return Insert(CallInst::Create(Callee, Arg, Name));
476 }
477
478 template
479 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
480 InputIterator ArgEnd, const char *Name = "") {
481 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd, Name));
482 }
483
484 Value *CreateSelect(Value *C, Value *True, Value *False,
485 const char *Name = "") {
486 if (Constant *CC = dyn_cast(C))
487 if (Constant *TC = dyn_cast(True))
488 if (Constant *FC = dyn_cast(False))
489 return ConstantExpr::getSelect(CC, TC, FC);
490 return Insert(SelectInst::Create(C, True, False, Name));
491 }
492
493 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
494 return Insert(new VAArgInst(List, Ty, Name));
495 }
496
497 Value *CreateExtractElement(Value *Vec, Value *Idx,
498 const char *Name = "") {
499 if (Constant *VC = dyn_cast(Vec))
500 if (Constant *IC = dyn_cast(Idx))
501 return ConstantExpr::getExtractElement(VC, IC);
502 return Insert(new ExtractElementInst(Vec, Idx, Name));
503 }
504
505 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
506 const char *Name = "") {
507 if (Constant *VC = dyn_cast(Vec))
508 if (Constant *NC = dyn_cast(NewElt))
509 if (Constant *IC = dyn_cast(Idx))
510 return ConstantExpr::getInsertElement(VC, NC, IC);
511 return Insert(InsertElementInst::Create(Vec, NewElt, Idx, Name));
512 }
513
514 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
515 const char *Name = "") {
516 if (Constant *V1C = dyn_cast(V1))
517 if (Constant *V2C = dyn_cast(V2))
518 if (Constant *MC = dyn_cast(Mask))
519 return ConstantExpr::getShuffleVector(V1C, V2C, MC);
520 return Insert(new ShuffleVectorInst(V1, V2, Mask, Name));
521 }
522 };
523
524 }
525
526 #endif
+0
-780
include/llvm/Support/LLVMBuilder.h less more
None //===-- llvm/Support/LLVMBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the LLVMBuilder class, which is used as a convenient way
10 // to create LLVM instructions with a consistent and simplified interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_LLVMBUILDER_H
15 #define LLVM_SUPPORT_LLVMBUILDER_H
16
17 #include "llvm/BasicBlock.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Constants.h"
20
21 namespace llvm {
22
23 /// LLVMBuilder - This provides a uniform API for creating instructions and
24 /// inserting them into a basic block: either at the end of a BasicBlock, or
25 /// at a specific iterator location in a block.
26 ///
27 /// Note that the builder does not expose the full generality of LLVM
28 /// instructions. For example, it cannot be used to create instructions with
29 /// arbitrary names (specifically, names with nul characters in them) - It only
30 /// supports nul-terminated C strings. For fully generic names, use
31 /// I->setName(). For access to extra instruction properties, use the mutators
32 /// (e.g. setVolatile) on the instructions after they have been created.
33 class LLVMBuilder {
34 BasicBlock *BB;
35 BasicBlock::iterator InsertPt;
36 public:
37 LLVMBuilder() { ClearInsertionPoint(); }
38 explicit LLVMBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
39 LLVMBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
40 SetInsertPoint(TheBB, IP);
41 }
42
43 //===--------------------------------------------------------------------===//
44 // Builder configuration methods
45 //===--------------------------------------------------------------------===//
46
47 /// ClearInsertionPoint - Clear the insertion point: created instructions will
48 /// not be inserted into a block.
49 void ClearInsertionPoint() {
50 BB = 0;
51 }
52
53 BasicBlock *GetInsertBlock() const { return BB; }
54
55 /// SetInsertPoint - This specifies that created instructions should be
56 /// appended to the end of the specified block.
57 void SetInsertPoint(BasicBlock *TheBB) {
58 BB = TheBB;
59 InsertPt = BB->end();
60 }
61
62 /// SetInsertPoint - This specifies that created instructions should be
63 /// inserted at the specified point.
64 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
65 BB = TheBB;
66 InsertPt = IP;
67 }
68
69 /// Insert - Insert and return the specified instruction.
70 template
71 InstTy *Insert(InstTy *I) const {
72 InsertHelper(I);
73 return I;
74 }
75
76 /// InsertHelper - Insert the specified instruction at the specified insertion
77 /// point. This is split out of Insert so that it isn't duplicated for every
78 /// template instantiation.
79 void InsertHelper(Instruction *I) const {
80 if (BB) BB->getInstList().insert(InsertPt, I);
81 }
82
83 //===--------------------------------------------------------------------===//
84 // Instruction creation methods: Terminators
85 //===--------------------------------------------------------------------===//
86
87 /// CreateRetVoid - Create a 'ret void' instruction.
88 ReturnInst *CreateRetVoid() {
89 return Insert(ReturnInst::Create());
90 }
91
92 /// @verbatim
93 /// CreateRet - Create a 'ret ' instruction.
94 /// @endverbatim
95 ReturnInst *CreateRet(Value *V) {
96 return Insert(ReturnInst::Create(V));
97 }
98
99 ReturnInst *CreateRet(Value * const* retVals, unsigned N) {
100 return Insert(ReturnInst::Create(retVals, N));
101 }
102
103 GetResultInst *CreateGetResult(Value *V, unsigned Index, const char *Name = "") {
104 return Insert(new GetResultInst(V, Index, Name));
105 }
106
107 /// CreateBr - Create an unconditional 'br label X' instruction.
108 BranchInst *CreateBr(BasicBlock *Dest) {
109 return Insert(BranchInst::Create(Dest));
110 }
111
112 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
113 /// instruction.
114 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
115 return Insert(BranchInst::Create(True, False, Cond));
116 }
117
118 /// CreateSwitch - Create a switch instruction with the specified value,
119 /// default dest, and with a hint for the number of cases that will be added
120 /// (for efficient allocation).
121 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
122 return Insert(SwitchInst::Create(V, Dest, NumCases));
123 }
124
125 /// CreateInvoke - Create an invoke instruction.
126 template
127 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
128 BasicBlock *UnwindDest, InputIterator ArgBegin,
129 InputIterator ArgEnd, const char *Name = "") {
130 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
131 ArgBegin, ArgEnd, Name));
132 }
133
134 UnwindInst *CreateUnwind() {
135 return Insert(new UnwindInst());
136 }
137
138 UnreachableInst *CreateUnreachable() {
139 return Insert(new UnreachableInst());
140 }
141
142 //===--------------------------------------------------------------------===//
143 // Instruction creation methods: Binary Operators
144 //===--------------------------------------------------------------------===//
145
146 BinaryOperator *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
147 return Insert(BinaryOperator::createAdd(LHS, RHS, Name));
148 }
149 BinaryOperator *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
150 return Insert(BinaryOperator::createSub(LHS, RHS, Name));
151 }
152 BinaryOperator *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
153 return Insert(BinaryOperator::createMul(LHS, RHS, Name));
154 }
155 BinaryOperator *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
156 return Insert(BinaryOperator::createUDiv(LHS, RHS, Name));
157 }
158 BinaryOperator *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
159 return Insert(BinaryOperator::createSDiv(LHS, RHS, Name));
160 }
161 BinaryOperator *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
162 return Insert(BinaryOperator::createFDiv(LHS, RHS, Name));
163 }
164 BinaryOperator *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
165 return Insert(BinaryOperator::createURem(LHS, RHS, Name));
166 }
167 BinaryOperator *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
168 return Insert(BinaryOperator::createSRem(LHS, RHS, Name));
169 }
170 BinaryOperator *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
171 return Insert(BinaryOperator::createFRem(LHS, RHS, Name));
172 }
173 BinaryOperator *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
174 return Insert(BinaryOperator::createShl(LHS, RHS, Name));
175 }
176 BinaryOperator *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
177 return Insert(BinaryOperator::createLShr(LHS, RHS, Name));
178 }
179 BinaryOperator *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
180 return Insert(BinaryOperator::createAShr(LHS, RHS, Name));
181 }
182 BinaryOperator *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
183 return Insert(BinaryOperator::createAnd(LHS, RHS, Name));
184 }
185 BinaryOperator *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
186 return Insert(BinaryOperator::createOr(LHS, RHS, Name));
187 }
188 BinaryOperator *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
189 return Insert(BinaryOperator::createXor(LHS, RHS, Name));
190 }
191
192 BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
193 Value *LHS, Value *RHS, const char *Name = "") {
194 return Insert(BinaryOperator::create(Opc, LHS, RHS, Name));
195 }
196
197 BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
198 return Insert(BinaryOperator::createNeg(V, Name));
199 }
200 BinaryOperator *CreateNot(Value *V, const char *Name = "") {
201 return Insert(BinaryOperator::createNot(V, Name));
202 }
203
204 //===--------------------------------------------------------------------===//
205 // Instruction creation methods: Memory Instructions
206 //===--------------------------------------------------------------------===//
207
208 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
209 const char *Name = "") {
210 return Insert(new MallocInst(Ty, ArraySize, Name));
211 }
212 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
213 const char *Name = "") {
214 return Insert(new AllocaInst(Ty, ArraySize, Name));
215 }
216 FreeInst *CreateFree(Value *Ptr) {
217 return Insert(new FreeInst(Ptr));
218 }
219 LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
220 return Insert(new LoadInst(Ptr, Name));
221 }
222 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
223 return Insert(new LoadInst(Ptr, Name, isVolatile));
224 }
225 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
226 return Insert(new StoreInst(Val, Ptr, isVolatile));
227 }
228 template
229 GetElementPtrInst *CreateGEP(Value *Ptr, InputIterator IdxBegin,
230 InputIterator IdxEnd, const char *Name = "") {
231 return(Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd, Name)));
232 }
233 GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
234 return Insert(GetElementPtrInst::Create(Ptr, Idx, Name));
235 }
236 GetElementPtrInst *CreateStructGEP(Value *Ptr, unsigned Idx,
237 const char *Name = "") {
238 llvm::Value *Idxs[] = {
239 ConstantInt::get(llvm::Type::Int32Ty, 0),
240 ConstantInt::get(llvm::Type::Int32Ty, Idx)
241 };
242 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2, Name));
243 }
244
245 //===--------------------------------------------------------------------===//
246 // Instruction creation methods: Cast/Conversion Operators
247 //===--------------------------------------------------------------------===//
248
249 TruncInst *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
250 return Insert(new TruncInst(V, DestTy, Name));
251 }
252 ZExtInst *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
253 return Insert(new ZExtInst(V, DestTy, Name));
254 }
255 SExtInst *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
256 return Insert(new SExtInst(V, DestTy, Name));
257 }
258 FPToUIInst *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
259 return Insert(new FPToUIInst(V, DestTy, Name));
260 }
261 FPToSIInst *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
262 return Insert(new FPToSIInst(V, DestTy, Name));
263 }
264 UIToFPInst *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
265 return Insert(new UIToFPInst(V, DestTy, Name));
266 }
267 SIToFPInst *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
268 return Insert(new SIToFPInst(V, DestTy, Name));
269 }
270 FPTruncInst *CreateFPTrunc(Value *V, const Type *DestTy,
271 const char *Name = "") {
272 return Insert(new FPTruncInst(V, DestTy, Name));
273 }
274 FPExtInst *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
275 return Insert(new FPExtInst(V, DestTy, Name));
276 }
277 PtrToIntInst *CreatePtrToInt(Value *V, const Type *DestTy,
278 const char *Name = "") {
279 return Insert(new PtrToIntInst(V, DestTy, Name));
280 }
281 IntToPtrInst *CreateIntToPtr(Value *V, const Type *DestTy,
282 const char *Name = "") {
283 return Insert(new IntToPtrInst(V, DestTy, Name));
284 }
285 BitCastInst *CreateBitCast(Value *V, const Type *DestTy,
286 const char *Name = "") {
287 return Insert(new BitCastInst(V, DestTy, Name));
288 }
289
290 CastInst *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
291 const char *Name = "") {
292 return Insert(CastInst::create(Op, V, DestTy, Name));
293 }
294 CastInst *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
295 const char *Name = "") {
296 return Insert(CastInst::createIntegerCast(V, DestTy, isSigned, Name));
297 }
298
299
300
301 //===--------------------------------------------------------------------===//
302 // Instruction creation methods: Compare Instructions
303 //===--------------------------------------------------------------------===//
304
305 ICmpInst *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
306 return Insert(new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS, Name));
307 }
308 ICmpInst *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
309 return Insert(new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS, Name));
310 }
311 ICmpInst *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
312 return Insert(new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, Name));
313 }
314 ICmpInst *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
315 return Insert(new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS, Name));
316 }
317 ICmpInst *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
318 return Insert(new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS, Name));
319 }
320 ICmpInst *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
321 return Insert(new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS, Name));
322 }
323 ICmpInst *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
324 return Insert(new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, Name));
325 }
326 ICmpInst *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
327 return Insert(new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS, Name));
328 }
329 ICmpInst *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
330 return Insert(new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS, Name));
331 }
332 ICmpInst *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
333 return Insert(new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS, Name));
334 }
335
336 FCmpInst *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
337 return Insert(new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS, Name));
338 }
339 FCmpInst *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
340 return Insert(new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS, Name));
341 }
342 FCmpInst *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
343 return Insert(new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS, Name));
344 }
345 FCmpInst *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
346 return Insert(new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS, Name));
347 }
348 FCmpInst *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
349 return Insert(new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS, Name));
350 }
351 FCmpInst *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
352 return Insert(new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS, Name));
353 }
354 FCmpInst *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
355 return Insert(new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS, Name));
356 }
357 FCmpInst *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
358 return Insert(new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS, Name));
359 }
360 FCmpInst *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
361 return Insert(new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS, Name));
362 }
363 FCmpInst *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
364 return Insert(new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS, Name));
365 }
366 FCmpInst *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
367 return Insert(new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS, Name));
368 }
369 FCmpInst *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
370 return Insert(new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS, Name));
371 }
372 FCmpInst *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
373 return Insert(new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS, Name));
374 }
375 FCmpInst *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
376 return Insert(new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS, Name));
377 }
378
379
380 ICmpInst *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS,
381 const char *Name = "") {
382 return Insert(new ICmpInst(P, LHS, RHS, Name));
383 }
384 FCmpInst *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS,
385 const char *Name = "") {
386 return Insert(new FCmpInst(P, LHS, RHS, Name));
387 }
388
389 //===--------------------------------------------------------------------===//
390 // Instruction creation methods: Other Instructions
391 //===--------------------------------------------------------------------===//
392
393 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
394 return Insert(PHINode::Create(Ty, Name));
395 }
396
397 CallInst *CreateCall(Value *Callee, const char *Name = "") {
398 return Insert(CallInst::Create(Callee, Name));
399 }
400 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
401 return Insert(CallInst::Create(Callee, Arg, Name));
402 }
403
404 template
405 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
406 InputIterator ArgEnd, const char *Name = "") {
407 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd, Name));
408 }
409
410 SelectInst *CreateSelect(Value *C, Value *True, Value *False,
411 const char *Name = "") {
412 return Insert(SelectInst::Create(C, True, False, Name));
413 }
414
415 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
416 return Insert(new VAArgInst(List, Ty, Name));
417 }
418
419 ExtractElementInst *CreateExtractElement(Value *Vec, Value *Idx,
420 const char *Name = "") {
421 return Insert(new ExtractElementInst(Vec, Idx, Name));
422 }
423
424 InsertElementInst *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
425 const char *Name = "") {
426 return Insert(InsertElementInst::Create(Vec, NewElt, Idx, Name));
427 }
428
429 ShuffleVectorInst *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
430 const char *Name = "") {
431 return Insert(new ShuffleVectorInst(V1, V2, Mask, Name));
432 }
433 };
434
435 /// LLVMFoldingBuilder - A version of LLVMBuilder that constant folds operands
436 /// as they come in.
437 class LLVMFoldingBuilder : public LLVMBuilder {
438
439 public:
440 LLVMFoldingBuilder() {}
441 explicit LLVMFoldingBuilder(BasicBlock *TheBB)
442 : LLVMBuilder(TheBB) {}
443 LLVMFoldingBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
444 : LLVMBuilder(TheBB, IP) {}
445
446 //===--------------------------------------------------------------------===//
447 // Instruction creation methods: Binary Operators
448 //===--------------------------------------------------------------------===//
449
450 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
451 if (Constant *LC = dyn_cast(LHS))
452 if (Constant *RC = dyn_cast(RHS))
453 return ConstantExpr::getAdd(LC, RC);
454 return LLVMBuilder::CreateAdd(LHS, RHS, Name);
455 }
456
457 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
458 if (Constant *LC = dyn_cast(LHS))
459 if (Constant *RC = dyn_cast(RHS))
460 return ConstantExpr::getSub(LC, RC);
461 return LLVMBuilder::CreateSub(LHS, RHS, Name);
462 }
463
464 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
465 if (Constant *LC = dyn_cast(LHS))
466 if (Constant *RC = dyn_cast(RHS))
467 return ConstantExpr::getMul(LC, RC);
468 return LLVMBuilder::CreateMul(LHS, RHS, Name);
469 }
470
471 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
472 if (Constant *LC = dyn_cast(LHS))
473 if (Constant *RC = dyn_cast(RHS))
474 return ConstantExpr::getUDiv(LC, RC);
475 return LLVMBuilder::CreateUDiv(LHS, RHS, Name);
476 }
477
478 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
479 if (Constant *LC = dyn_cast(LHS))
480 if (Constant *RC = dyn_cast(RHS))
481 return ConstantExpr::getSDiv(LC, RC);
482 return LLVMBuilder::CreateSDiv(LHS, RHS, Name);
483 }
484
485 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
486 if (Constant *LC = dyn_cast(LHS))
487 if (Constant *RC = dyn_cast(RHS))
488 return ConstantExpr::getFDiv(LC, RC);
489 return LLVMBuilder::CreateFDiv(LHS, RHS, Name);
490 }
491
492 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
493 if (Constant *LC = dyn_cast(LHS))
494 if (Constant *RC = dyn_cast(RHS))
495 return ConstantExpr::getURem(LC, RC);
496 return LLVMBuilder::CreateURem(LHS, RHS, Name);
497 }
498
499 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
500 if (Constant *LC = dyn_cast(LHS))
501 if (Constant *RC = dyn_cast(RHS))
502 return ConstantExpr::getSRem(LC, RC);
503 return LLVMBuilder::CreateSRem(LHS, RHS, Name);
504 }
505
506 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
507 if (Constant *LC = dyn_cast(LHS))
508 if (Constant *RC = dyn_cast(RHS))
509 return ConstantExpr::getFRem(LC, RC);
510 return LLVMBuilder::CreateFRem(LHS, RHS, Name);
511 }
512
513 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
514 if (Constant *LC = dyn_cast(LHS))
515 if (Constant *RC = dyn_cast(RHS))
516 return ConstantExpr::getAnd(LC, RC);
517 return LLVMBuilder::CreateAnd(LHS, RHS, Name);
518 }
519
520 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
521 if (Constant *LC = dyn_cast(LHS))
522 if (Constant *RC = dyn_cast(RHS))
523 return ConstantExpr::getOr(LC, RC);
524 return LLVMBuilder::CreateOr(LHS, RHS, Name);
525 }
526
527 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
528 if (Constant *LC = dyn_cast(LHS))
529 if (Constant *RC = dyn_cast(RHS))
530 return ConstantExpr::getXor(LC, RC);
531 return LLVMBuilder::CreateXor(LHS, RHS, Name);
532 }
533
534 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
535 if (Constant *LC = dyn_cast(LHS))
536 if (Constant *RC = dyn_cast(RHS))
537 return ConstantExpr::getShl(LC, RC);
538 return LLVMBuilder::CreateShl(LHS, RHS, Name);
539 }
540
541 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
542 if (Constant *LC = dyn_cast(LHS))
543 if (Constant *RC = dyn_cast(RHS))
544 return ConstantExpr::getLShr(LC, RC);
545 return LLVMBuilder::CreateLShr(LHS, RHS, Name);
546 }
547
548 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
549 if (Constant *LC = dyn_cast(LHS))
550 if (Constant *RC = dyn_cast(RHS))
551 return ConstantExpr::getAShr(LC, RC);
552 return LLVMBuilder::CreateAShr(LHS, RHS, Name);
553 }
554
555 //===--------------------------------------------------------------------===//
556 // Instruction creation methods: Memory Instructions
557 //===--------------------------------------------------------------------===//
558
559 template
560 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin,
561 InputIterator IdxEnd, const char *Name = "") {
562
563 if (Constant *PC = dyn_cast(Ptr)) {
564 // Every index must be constant.
565 InputIterator i;
566 for (i = IdxBegin; i < IdxEnd; ++i)
567 if (!dyn_cast(*i))
568 break;
569 if (i == IdxEnd)
570 return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
571 }
572 return LLVMBuilder::CreateGEP(Ptr, IdxBegin, IdxEnd, Name);
573 }
574 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
575 if (Constant *PC = dyn_cast(Ptr))
576 if (Constant *IC = dyn_cast(Idx))
577 return ConstantExpr::getGetElementPtr(PC, &IC, 1);
578 return LLVMBuilder::CreateGEP(Ptr, Idx, Name);
579 }
580
581 //===--------------------------------------------------------------------===//
582 // Instruction creation methods: Cast/Conversion Operators
583 //===--------------------------------------------------------------------===//
584
585 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
586 return CreateCast(Instruction::Trunc, V, DestTy, Name);
587 }
588 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
589 return CreateCast(Instruction::ZExt, V, DestTy, Name);
590 }
591 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
592 return CreateCast(Instruction::SExt, V, DestTy, Name);
593 }
594 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
595 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
596 }
597 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
598 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
599 }
600 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
601 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
602 }
603 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
604 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
605 }
606 Value *CreateFPTrunc(Value *V, const Type *DestTy,
607 const char *Name = "") {
608 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
609 }
610 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
611 return CreateCast(Instruction::FPExt, V, DestTy, Name);
612 }
613 Value *CreatePtrToInt(Value *V, const Type *DestTy,
614 const char *Name = "") {
615 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
616 }
617 Value *CreateIntToPtr(Value *V, const Type *DestTy,
618 const char *Name = "") {
619 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
620 }
621 Value *CreateBitCast(Value *V, const Type *DestTy,
622 const char *Name = "") {
623 return CreateCast(Instruction::BitCast, V, DestTy, Name);
624 }
625
626 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
627 const char *Name = "") {
628 if (V->getType() == DestTy)
629 return V;
630 if (Constant *VC = dyn_cast(V))
631 return ConstantExpr::getCast(Op, VC, DestTy);
632 return LLVMBuilder::CreateCast(Op, V, DestTy, Name);
633 }
634 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
635 const char *Name = "") {
636 if (V->getType() == DestTy)
637 return V;
638 if (Constant *VC = dyn_cast(V))
639 return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
640 return LLVMBuilder::CreateIntCast(V, DestTy, isSigned, Name);
641 }
642
643 //===--------------------------------------------------------------------===//
644 // Instruction creation methods: Compare Instructions
645 //===--------------------------------------------------------------------===//
646
647 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
648 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
649 }
650 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
651 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
652 }
653 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
654 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
655 }
656 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
657 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
658 }
659 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
660 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
661 }
662 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
663 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
664 }
665 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
666 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
667 }
668 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
669 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
670 }
671 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
672 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
673 }
674 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
675 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
676 }
677
678 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
679 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
680 }
681 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
682 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
683 }
684 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
685 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
686 }
687 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
688 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
689 }
690 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
691 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
692 }
693 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
694 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
695 }
696 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
697 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
698 }
699 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
700 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
701 }
702 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
703 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
704 }
705 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
706 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
707 }
708 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
709 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
710 }
711 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
712 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
713 }
714 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
715 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
716 }
717 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
718 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
719 }
720
721 Value *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS,
722 const char *Name = "") {
723 if (Constant *LC = dyn_cast(LHS))
724 if (Constant *RC = dyn_cast(RHS))
725 return ConstantExpr::getCompare(P, LC, RC);
726 return LLVMBuilder::CreateICmp(P, LHS, RHS, Name);
727 }
728
729 Value *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS,
730 const char *Name = "") {
731 if (Constant *LC = dyn_cast(LHS))
732 if (Constant *RC = dyn_cast(RHS))
733 return ConstantExpr::getCompare(P, LC, RC);
734 return LLVMBuilder::CreateFCmp(P, LHS, RHS, Name);
735 }
736
737 //===--------------------------------------------------------------------===//
738 // Instruction creation methods: Other Instructions
739 //===--------------------------------------------------------------------===//
740
741 Value *CreateSelect(Value *C, Value *True, Value *False,
742 const char *Name = "") {
743 if (Constant *CC = dyn_cast(C))
744 if (Constant *TC = dyn_cast(True))
745 if (Constant *FC = dyn_cast(False))
746 return ConstantExpr::getSelect(CC, TC, FC);
747 return LLVMBuilder::CreateSelect(C, True, False, Name);
748 }
749
750 Value *CreateExtractElement(Value *Vec, Value *Idx,
751 const char *Name = "") {
752 if (Constant *VC = dyn_cast(Vec))
753 if (Constant *IC = dyn_cast(Idx))
754 return ConstantExpr::getExtractElement(VC, IC);
755 return LLVMBuilder::CreateExtractElement(Vec, Idx, Name);
756 }
757
758 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
759 const char *Name = "") {
760 if (Constant *VC = dyn_cast(Vec))
761 if (Constant *NC = dyn_cast(NewElt))
762 if (Constant *IC = dyn_cast(Idx))
763 return ConstantExpr::getInsertElement(VC, NC, IC);
764 return LLVMBuilder::CreateInsertElement(Vec, NewElt, Idx, Name);
765 }
766
767 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
768 const char *Name = "") {
769 if (Constant *V1C = dyn_cast(V1))
770 if (Constant *V2C = dyn_cast(V2))
771 if (Constant *MC = dyn_cast(Mask))
772 return ConstantExpr::getShuffleVector(V1C, V2C, MC);
773 return LLVMBuilder::CreateShuffleVector(V1, V2, Mask, Name);
774 }
775 };
776
777 }
778
779 #endif
3737 /* Need these includes to support the LLVM 'cast' template for the C++ 'wrap'
3838 and 'unwrap' conversion functions. */
3939 #include "llvm/Module.h"
40 #include "llvm/Support/LLVMBuilder.h"
40 #include "llvm/Support/IRBuilder.h"
4141
4242 extern "C" {
4343 #endif
688688 DEFINE_ISA_CONVERSION_FUNCTIONS (Value, LLVMValueRef )
689689 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef )
690690 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef )
691 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMFoldingBuilder, LLVMBuilderRef )
691 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder, LLVMBuilderRef )
692692 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PATypeHolder, LLVMTypeHandleRef )
693693 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ModuleProvider, LLVMModuleProviderRef)
694694 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef )
3030 #include "llvm/CodeGen/Collector.h"
3131 #include "llvm/IntrinsicInst.h"
3232 #include "llvm/Module.h"
33 #include "llvm/Support/LLVMBuilder.h"
33 #include "llvm/Support/IRBuilder.h"
3434
3535 using namespace llvm;
3636
6060 Constant *GetFrameMap(Function &F);
6161 const Type* GetConcreteStackEntryType(Function &F);
6262 void CollectRoots(Function &F);
63 static GetElementPtrInst *CreateGEP(LLVMBuilder &B, Value *BasePtr,
63 static GetElementPtrInst *CreateGEP(IRBuilder &B, Value *BasePtr,
6464 int Idx1, const char *Name);
65 static GetElementPtrInst *CreateGEP(LLVMBuilder &B, Value *BasePtr,
65 static GetElementPtrInst *CreateGEP(IRBuilder &B, Value *BasePtr,
6666 int Idx1, int Idx2, const char *Name);
6767 };
6868
8585 // State.
8686 int State;
8787 Function::iterator StateBB, StateE;
88 LLVMBuilder Builder;
88 IRBuilder Builder;
8989
9090 public:
9191 EscapeEnumerator(Function &F, const char *N = "cleanup")
9292 : F(F), CleanupBBName(N), State(0) {}
9393
94 LLVMBuilder *Next() {
94 IRBuilder *Next() {
9595 switch (State) {
9696 default:
9797 return 0;
338338 }
339339
340340 GetElementPtrInst *
341 ShadowStackCollector::CreateGEP(LLVMBuilder &B, Value *BasePtr,
341 ShadowStackCollector::CreateGEP(IRBuilder &B, Value *BasePtr,
342342 int Idx, int Idx2, const char *Name) {
343343 Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0),
344344 ConstantInt::get(Type::Int32Ty, Idx),
345345 ConstantInt::get(Type::Int32Ty, Idx2) };
346 return B.CreateGEP(BasePtr, Indices, Indices + 3, Name);
346 Value* Val = B.CreateGEP(BasePtr, Indices, Indices + 3, Name);
347
348 assert(isa(Val) && "Unexpected folded constant");
349
350 return dyn_cast(Val);
347351 }
348352
349353 GetElementPtrInst *
350 ShadowStackCollector::CreateGEP(LLVMBuilder &B, Value *BasePtr,
354 ShadowStackCollector::CreateGEP(IRBuilder &B, Value *BasePtr,
351355 int Idx, const char *Name) {
352356 Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0),
353357 ConstantInt::get(Type::Int32Ty, Idx) };
354 return B.CreateGEP(BasePtr, Indices, Indices + 2, Name);
358 Value *Val = B.CreateGEP(BasePtr, Indices, Indices + 2, Name);
359
360 assert(isa(Val) && "Unexpected folded constant");
361
362 return dyn_cast(Val);
355363 }
356364
357365 /// runOnFunction - Insert code to maintain the shadow stack.
370378
371379 // Build the shadow stack entry at the very start of the function.
372380 BasicBlock::iterator IP = F.getEntryBlock().begin();
373 LLVMBuilder AtEntry(IP->getParent(), IP);
381 IRBuilder AtEntry(IP->getParent(), IP);
374382
375383 Instruction *StackEntry = AtEntry.CreateAlloca(ConcreteStackEntryTy, 0,
376384 "gc_frame");
408416
409417 // For each instruction that escapes...
410418 EscapeEnumerator EE(F, "gc_cleanup");
411 while (LLVMBuilder *AtExit = EE.Next()) {
419 while (IRBuilder *AtExit = EE.Next()) {
412420 // Pop the entry from the shadow stack. Don't reuse CurrentHead from
413421 // AtEntry, since that would make the value live for the entire function.
414422 Instruction *EntryNextPtr2 = CreateGEP(*AtExit, StackEntry, 0, 0,
961961 /*===-- Instruction builders ----------------------------------------------===*/
962962
963963 LLVMBuilderRef LLVMCreateBuilder() {
964 return wrap(new LLVMFoldingBuilder());
964 return wrap(new IRBuilder());
965965 }
966966
967967 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,