llvm.org GIT mirror llvm / 06cb8ed
Move llvm/Support/IRBuilder.h -> llvm/IRBuilder.h This was always part of the VMCore library out of necessity -- it deals entirely in the IR. The .cpp file in fact was already part of the VMCore library. This is just a mechanical move. I've tried to go through and re-apply the coding standard's preferred header sort, but at 40-ish files, I may have gotten some wrong. Please let me know if so. I'll be committing the corresponding updates to Clang and Polly, and Duncan has DragonEgg. Thanks to Bill and Eric for giving the green light for this bit of cleanup. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@159421 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 7 years ago
51 changed file(s) with 1476 addition(s) and 1473 deletion(s). Raw diff Collapse all Expand all
684684 // See example below.
685685
686686 #include "llvm/DerivedTypes.h"
687 #include "llvm/IRBuilder.h"
687688 #include "llvm/LLVMContext.h"
688689 #include "llvm/Module.h"
689690 #include "llvm/Analysis/Verifier.h"
690 #include "llvm/Support/IRBuilder.h"
691691 #include <cstdio>
692692 #include <string>
693693 #include <map>
516516 #include "llvm/DerivedTypes.h"
517517 #include "llvm/ExecutionEngine/ExecutionEngine.h"
518518 #include "llvm/ExecutionEngine/JIT.h"
519 #include "llvm/IRBuilder.h"
519520 #include "llvm/LLVMContext.h"
520521 #include "llvm/Module.h"
521522 #include "llvm/PassManager.h"
523524 #include "llvm/Analysis/Passes.h"
524525 #include "llvm/Target/TargetData.h"
525526 #include "llvm/Transforms/Scalar.h"
526 #include "llvm/Support/IRBuilder.h"
527527 #include "llvm/Support/TargetSelect.h"
528528 #include <cstdio>
529529 #include <string>
894894 #include "llvm/DerivedTypes.h"
895895 #include "llvm/ExecutionEngine/ExecutionEngine.h"
896896 #include "llvm/ExecutionEngine/JIT.h"
897 #include "llvm/IRBuilder.h"
897898 #include "llvm/LLVMContext.h"
898899 #include "llvm/Module.h"
899900 #include "llvm/PassManager.h"
901902 #include "llvm/Analysis/Passes.h"
902903 #include "llvm/Target/TargetData.h"
903904 #include "llvm/Transforms/Scalar.h"
904 #include "llvm/Support/IRBuilder.h"
905905 #include "llvm/Support/TargetSelect.h"
906906 #include <cstdio>
907907 #include <string>
833833 #include "llvm/DerivedTypes.h"
834834 #include "llvm/ExecutionEngine/ExecutionEngine.h"
835835 #include "llvm/ExecutionEngine/JIT.h"
836 #include "llvm/IRBuilder.h"
836837 #include "llvm/LLVMContext.h"
837838 #include "llvm/Module.h"
838839 #include "llvm/PassManager.h"
840841 #include "llvm/Analysis/Passes.h"
841842 #include "llvm/Target/TargetData.h"
842843 #include "llvm/Transforms/Scalar.h"
843 #include "llvm/Support/IRBuilder.h"
844844 #include "llvm/Support/TargetSelect.h"
845845 #include <cstdio>
846846 #include <string>
10011001 #include "llvm/DerivedTypes.h"
10021002 #include "llvm/ExecutionEngine/ExecutionEngine.h"
10031003 #include "llvm/ExecutionEngine/JIT.h"
1004 #include "llvm/IRBuilder.h"
10041005 #include "llvm/LLVMContext.h"
10051006 #include "llvm/Module.h"
10061007 #include "llvm/PassManager.h"
10081009 #include "llvm/Analysis/Passes.h"
10091010 #include "llvm/Target/TargetData.h"
10101011 #include "llvm/Transforms/Scalar.h"
1011 #include "llvm/Support/IRBuilder.h"
10121012 #include "llvm/Support/TargetSelect.h"
10131013 #include <cstdio>
10141014 #include <string>
1414 #ifndef BRAINF_H
1515 #define BRAINF_H
1616
17 #include "llvm/IRBuilder.h"
1718 #include "llvm/LLVMContext.h"
1819 #include "llvm/Module.h"
19 #include "llvm/Support/IRBuilder.h"
2020
2121 using namespace llvm;
2222
5151 #include "llvm/DerivedTypes.h"
5252 #include "llvm/ExecutionEngine/ExecutionEngine.h"
5353 #include "llvm/ExecutionEngine/JIT.h"
54 #include "llvm/IRBuilder.h"
5455 #include "llvm/Module.h"
5556 #include "llvm/PassManager.h"
5657 #include "llvm/Intrinsics.h"
5859 #include "llvm/Target/TargetData.h"
5960 #include "llvm/Target/TargetOptions.h"
6061 #include "llvm/Transforms/Scalar.h"
61 #include "llvm/Support/IRBuilder.h"
6262 #include "llvm/Support/Dwarf.h"
6363 #include "llvm/Support/TargetSelect.h"
6464
3333 //
3434 //===----------------------------------------------------------------------===//
3535
36 #include "llvm/Constants.h"
37 #include "llvm/DerivedTypes.h"
38 #include "llvm/IRBuilder.h"
39 #include "llvm/Instructions.h"
3640 #include "llvm/LLVMContext.h"
3741 #include "llvm/Module.h"
38 #include "llvm/Constants.h"
39 #include "llvm/DerivedTypes.h"
40 #include "llvm/Instructions.h"
4142 #include "llvm/ExecutionEngine/JIT.h"
4243 #include "llvm/ExecutionEngine/Interpreter.h"
4344 #include "llvm/ExecutionEngine/GenericValue.h"
4445 #include "llvm/Support/TargetSelect.h"
4546 #include "llvm/Support/ManagedStatic.h"
4647 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/IRBuilder.h"
4848
4949 using namespace llvm;
5050
0 #include "llvm/DerivedTypes.h"
1 #include "llvm/IRBuilder.h"
12 #include "llvm/LLVMContext.h"
23 #include "llvm/Module.h"
34 #include "llvm/Analysis/Verifier.h"
4 #include "llvm/Support/IRBuilder.h"
55 #include
66 #include
77 #include
0 #include "llvm/DerivedTypes.h"
11 #include "llvm/ExecutionEngine/ExecutionEngine.h"
22 #include "llvm/ExecutionEngine/JIT.h"
3 #include "llvm/IRBuilder.h"
34 #include "llvm/LLVMContext.h"
45 #include "llvm/Module.h"
56 #include "llvm/PassManager.h"
78 #include "llvm/Analysis/Passes.h"
89 #include "llvm/Target/TargetData.h"
910 #include "llvm/Transforms/Scalar.h"
10 #include "llvm/Support/IRBuilder.h"
1111 #include "llvm/Support/TargetSelect.h"
1212 #include
1313 #include
0 #include "llvm/DerivedTypes.h"
11 #include "llvm/ExecutionEngine/ExecutionEngine.h"
22 #include "llvm/ExecutionEngine/JIT.h"
3 #include "llvm/IRBuilder.h"
34 #include "llvm/LLVMContext.h"
45 #include "llvm/Module.h"
56 #include "llvm/PassManager.h"
78 #include "llvm/Analysis/Passes.h"
89 #include "llvm/Target/TargetData.h"
910 #include "llvm/Transforms/Scalar.h"
10 #include "llvm/Support/IRBuilder.h"
1111 #include "llvm/Support/TargetSelect.h"
1212 #include
1313 #include
0 #include "llvm/DerivedTypes.h"
11 #include "llvm/ExecutionEngine/ExecutionEngine.h"
22 #include "llvm/ExecutionEngine/JIT.h"
3 #include "llvm/IRBuilder.h"
34 #include "llvm/LLVMContext.h"
45 #include "llvm/Module.h"
56 #include "llvm/PassManager.h"
78 #include "llvm/Analysis/Passes.h"
89 #include "llvm/Target/TargetData.h"
910 #include "llvm/Transforms/Scalar.h"
10 #include "llvm/Support/IRBuilder.h"
1111 #include "llvm/Support/TargetSelect.h"
1212 #include
1313 #include
0 #include "llvm/DerivedTypes.h"
11 #include "llvm/ExecutionEngine/ExecutionEngine.h"
22 #include "llvm/ExecutionEngine/JIT.h"
3 #include "llvm/IRBuilder.h"
34 #include "llvm/LLVMContext.h"
45 #include "llvm/Module.h"
56 #include "llvm/PassManager.h"
78 #include "llvm/Analysis/Passes.h"
89 #include "llvm/Target/TargetData.h"
910 #include "llvm/Transforms/Scalar.h"
10 #include "llvm/Support/IRBuilder.h"
1111 #include "llvm/Support/TargetSelect.h"
1212 #include
1313 #include
1414 #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
1515 #define LLVM_ANALYSIS_MEMORYBUILTINS_H
1616
17 #include "llvm/IRBuilder.h"
18 #include "llvm/Operator.h"
1719 #include "llvm/ADT/DenseMap.h"
1820 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Operator.h"
2021 #include "llvm/Support/DataTypes.h"
2122 #include "llvm/Support/InstVisitor.h"
22 #include "llvm/Support/IRBuilder.h"
2323 #include "llvm/Support/TargetFolder.h"
2424
2525 namespace llvm {
1313 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
1414 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
1515
16 #include "llvm/IRBuilder.h"
1617 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
1718 #include "llvm/Analysis/ScalarEvolutionNormalization.h"
18 #include "llvm/Support/IRBuilder.h"
1919 #include "llvm/Support/TargetFolder.h"
2020 #include "llvm/Support/ValueHandle.h"
2121 #include
0 //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- 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_IRBUILDER_H
15 #define LLVM_IRBUILDER_H
16
17 #include "llvm/Instructions.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/LLVMContext.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/ConstantFolder.h"
24
25 namespace llvm {
26 class MDNode;
27
28 /// IRBuilderDefaultInserter - This provides the default implementation of the
29 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
30 /// created by IRBuilder and needs to be inserted. By default, this inserts the
31 /// instruction at the insertion point.
32 template
33 class IRBuilderDefaultInserter {
34 protected:
35 void InsertHelper(Instruction *I, const Twine &Name,
36 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
37 if (BB) BB->getInstList().insert(InsertPt, I);
38 if (preserveNames)
39 I->setName(Name);
40 }
41 };
42
43 /// IRBuilderBase - Common base class shared among various IRBuilders.
44 class IRBuilderBase {
45 DebugLoc CurDbgLocation;
46 protected:
47 BasicBlock *BB;
48 BasicBlock::iterator InsertPt;
49 LLVMContext &Context;
50 public:
51
52 IRBuilderBase(LLVMContext &context)
53 : Context(context) {
54 ClearInsertionPoint();
55 }
56
57 //===--------------------------------------------------------------------===//
58 // Builder configuration methods
59 //===--------------------------------------------------------------------===//
60
61 /// ClearInsertionPoint - Clear the insertion point: created instructions will
62 /// not be inserted into a block.
63 void ClearInsertionPoint() {
64 BB = 0;
65 }
66
67 BasicBlock *GetInsertBlock() const { return BB; }
68 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
69 LLVMContext &getContext() const { return Context; }
70
71 /// SetInsertPoint - This specifies that created instructions should be
72 /// appended to the end of the specified block.
73 void SetInsertPoint(BasicBlock *TheBB) {
74 BB = TheBB;
75 InsertPt = BB->end();
76 }
77
78 /// SetInsertPoint - This specifies that created instructions should be
79 /// inserted before the specified instruction.
80 void SetInsertPoint(Instruction *I) {
81 BB = I->getParent();
82 InsertPt = I;
83 SetCurrentDebugLocation(I->getDebugLoc());
84 }
85
86 /// SetInsertPoint - This specifies that created instructions should be
87 /// inserted at the specified point.
88 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
89 BB = TheBB;
90 InsertPt = IP;
91 }
92
93 /// SetInsertPoint(Use) - Find the nearest point that dominates this use, and
94 /// specify that created instructions should be inserted at this point.
95 void SetInsertPoint(Use &U) {
96 Instruction *UseInst = cast(U.getUser());
97 if (PHINode *Phi = dyn_cast(UseInst)) {
98 BasicBlock *PredBB = Phi->getIncomingBlock(U);
99 assert(U != PredBB->getTerminator() && "critical edge not split");
100 SetInsertPoint(PredBB, PredBB->getTerminator());
101 return;
102 }
103 SetInsertPoint(UseInst);
104 }
105
106 /// SetCurrentDebugLocation - Set location information used by debugging
107 /// information.
108 void SetCurrentDebugLocation(const DebugLoc &L) {
109 CurDbgLocation = L;
110 }
111
112 /// getCurrentDebugLocation - Get location information used by debugging
113 /// information.
114 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
115
116 /// SetInstDebugLocation - If this builder has a current debug location, set
117 /// it on the specified instruction.
118 void SetInstDebugLocation(Instruction *I) const {
119 if (!CurDbgLocation.isUnknown())
120 I->setDebugLoc(CurDbgLocation);
121 }
122
123 /// getCurrentFunctionReturnType - Get the return type of the current function
124 /// that we're emitting into.
125 Type *getCurrentFunctionReturnType() const;
126
127 /// InsertPoint - A saved insertion point.
128 class InsertPoint {
129 BasicBlock *Block;
130 BasicBlock::iterator Point;
131
132 public:
133 /// Creates a new insertion point which doesn't point to anything.
134 InsertPoint() : Block(0) {}
135
136 /// Creates a new insertion point at the given location.
137 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
138 : Block(InsertBlock), Point(InsertPoint) {}
139
140 /// isSet - Returns true if this insert point is set.
141 bool isSet() const { return (Block != 0); }
142
143 llvm::BasicBlock *getBlock() const { return Block; }
144 llvm::BasicBlock::iterator getPoint() const { return Point; }
145 };
146
147 /// saveIP - Returns the current insert point.
148 InsertPoint saveIP() const {
149 return InsertPoint(GetInsertBlock(), GetInsertPoint());
150 }
151
152 /// saveAndClearIP - Returns the current insert point, clearing it
153 /// in the process.
154 InsertPoint saveAndClearIP() {
155 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
156 ClearInsertionPoint();
157 return IP;
158 }
159
160 /// restoreIP - Sets the current insert point to a previously-saved
161 /// location.
162 void restoreIP(InsertPoint IP) {
163 if (IP.isSet())
164 SetInsertPoint(IP.getBlock(), IP.getPoint());
165 else
166 ClearInsertionPoint();
167 }
168
169 //===--------------------------------------------------------------------===//
170 // Miscellaneous creation methods.
171 //===--------------------------------------------------------------------===//
172
173 /// CreateGlobalString - Make a new global variable with an initializer that
174 /// has array of i8 type filled in with the nul terminated string value
175 /// specified. The new global variable will be marked mergable with any
176 /// others of the same contents. If Name is specified, it is the name of the
177 /// global variable created.
178 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
179
180 /// getInt1 - Get a constant value representing either true or false.
181 ConstantInt *getInt1(bool V) {
182 return ConstantInt::get(getInt1Ty(), V);
183 }
184
185 /// getTrue - Get the constant value for i1 true.
186 ConstantInt *getTrue() {
187 return ConstantInt::getTrue(Context);
188 }
189
190 /// getFalse - Get the constant value for i1 false.
191 ConstantInt *getFalse() {
192 return ConstantInt::getFalse(Context);
193 }
194
195 /// getInt8 - Get a constant 8-bit value.
196 ConstantInt *getInt8(uint8_t C) {
197 return ConstantInt::get(getInt8Ty(), C);
198 }
199
200 /// getInt16 - Get a constant 16-bit value.
201 ConstantInt *getInt16(uint16_t C) {
202 return ConstantInt::get(getInt16Ty(), C);
203 }
204
205 /// getInt32 - Get a constant 32-bit value.
206 ConstantInt *getInt32(uint32_t C) {
207 return ConstantInt::get(getInt32Ty(), C);
208 }
209
210 /// getInt64 - Get a constant 64-bit value.
211 ConstantInt *getInt64(uint64_t C) {
212 return ConstantInt::get(getInt64Ty(), C);
213 }
214
215 /// getInt - Get a constant integer value.
216 ConstantInt *getInt(const APInt &AI) {
217 return ConstantInt::get(Context, AI);
218 }
219
220 //===--------------------------------------------------------------------===//
221 // Type creation methods
222 //===--------------------------------------------------------------------===//
223
224 /// getInt1Ty - Fetch the type representing a single bit
225 IntegerType *getInt1Ty() {
226 return Type::getInt1Ty(Context);
227 }
228
229 /// getInt8Ty - Fetch the type representing an 8-bit integer.
230 IntegerType *getInt8Ty() {
231 return Type::getInt8Ty(Context);
232 }
233
234 /// getInt16Ty - Fetch the type representing a 16-bit integer.
235 IntegerType *getInt16Ty() {
236 return Type::getInt16Ty(Context);
237 }
238
239 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
240 IntegerType *getInt32Ty() {
241 return Type::getInt32Ty(Context);
242 }
243
244 /// getInt64Ty - Fetch the type representing a 64-bit integer.
245 IntegerType *getInt64Ty() {
246 return Type::getInt64Ty(Context);
247 }
248
249 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
250 Type *getFloatTy() {
251 return Type::getFloatTy(Context);
252 }
253
254 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
255 Type *getDoubleTy() {
256 return Type::getDoubleTy(Context);
257 }
258
259 /// getVoidTy - Fetch the type representing void.
260 Type *getVoidTy() {
261 return Type::getVoidTy(Context);
262 }
263
264 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
265 return Type::getInt8PtrTy(Context, AddrSpace);
266 }
267
268 //===--------------------------------------------------------------------===//
269 // Intrinsic creation methods
270 //===--------------------------------------------------------------------===//
271
272 /// CreateMemSet - Create and insert a memset to the specified pointer and the
273 /// specified value. If the pointer isn't an i8*, it will be converted. If a
274 /// TBAA tag is specified, it will be added to the instruction.
275 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
276 bool isVolatile = false, MDNode *TBAATag = 0) {
277 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
278 }
279
280 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
281 bool isVolatile = false, MDNode *TBAATag = 0);
282
283 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
284 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
285 /// specified, it will be added to the instruction.
286 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
287 bool isVolatile = false, MDNode *TBAATag = 0) {
288 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
289 }
290
291 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
292 bool isVolatile = false, MDNode *TBAATag = 0);
293
294 /// CreateMemMove - Create and insert a memmove between the specified
295 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
296 /// tag is specified, it will be added to the instruction.
297 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
298 bool isVolatile = false, MDNode *TBAATag = 0) {
299 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
300 }
301
302 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
303 bool isVolatile = false, MDNode *TBAATag = 0);
304
305 /// CreateLifetimeStart - Create a lifetime.start intrinsic. If the pointer
306 /// isn't i8* it will be converted.
307 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
308
309 /// CreateLifetimeEnd - Create a lifetime.end intrinsic. If the pointer isn't
310 /// i8* it will be converted.
311 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
312
313 private:
314 Value *getCastedInt8PtrValue(Value *Ptr);
315 };
316
317 /// IRBuilder - This provides a uniform API for creating instructions and
318 /// inserting them into a basic block: either at the end of a BasicBlock, or
319 /// at a specific iterator location in a block.
320 ///
321 /// Note that the builder does not expose the full generality of LLVM
322 /// instructions. For access to extra instruction properties, use the mutators
323 /// (e.g. setVolatile) on the instructions after they have been created.
324 /// The first template argument handles whether or not to preserve names in the
325 /// final instruction output. This defaults to on. The second template argument
326 /// specifies a class to use for creating constants. This defaults to creating
327 /// minimally folded constants. The fourth template argument allows clients to
328 /// specify custom insertion hooks that are called on every newly created
329 /// insertion.
330 template
331 typename Inserter = IRBuilderDefaultInserter >
332 class IRBuilder : public IRBuilderBase, public Inserter {
333 T Folder;
334 MDNode *DefaultFPMathTag;
335 public:
336 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
337 MDNode *FPMathTag = 0)
338 : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag) {
339 }
340
341 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0) : IRBuilderBase(C),
342 Folder(), DefaultFPMathTag(FPMathTag) {
343 }
344
345 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
346 : IRBuilderBase(TheBB->getContext()), Folder(F),
347 DefaultFPMathTag(FPMathTag) {
348 SetInsertPoint(TheBB);
349 }
350
351 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
352 : IRBuilderBase(TheBB->getContext()), Folder(),
353 DefaultFPMathTag(FPMathTag) {
354 SetInsertPoint(TheBB);
355 }
356
357 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
358 : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
359 SetInsertPoint(IP);
360 SetCurrentDebugLocation(IP->getDebugLoc());
361 }
362
363 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
364 : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
365 SetInsertPoint(U);
366 SetCurrentDebugLocation(cast(U.getUser())->getDebugLoc());
367 }
368
369 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
370 MDNode *FPMathTag = 0)
371 : IRBuilderBase(TheBB->getContext()), Folder(F),
372 DefaultFPMathTag(FPMathTag) {
373 SetInsertPoint(TheBB, IP);
374 }
375
376 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
377 : IRBuilderBase(TheBB->getContext()), Folder(),
378 DefaultFPMathTag(FPMathTag) {
379 SetInsertPoint(TheBB, IP);
380 }
381
382 /// getFolder - Get the constant folder being used.
383 const T &getFolder() { return Folder; }
384
385 /// getDefaultFPMathTag - Get the floating point math metadata being used.
386 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
387
388 /// SetDefaultFPMathTag - Set the floating point math metadata to be used.
389 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
390
391 /// isNamePreserving - Return true if this builder is configured to actually
392 /// add the requested names to IR created through it.
393 bool isNamePreserving() const { return preserveNames; }
394
395 /// Insert - Insert and return the specified instruction.
396 template
397 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
398 this->InsertHelper(I, Name, BB, InsertPt);
399 if (!getCurrentDebugLocation().isUnknown())
400 this->SetInstDebugLocation(I);
401 return I;
402 }
403
404 /// Insert - No-op overload to handle constants.
405 Constant *Insert(Constant *C, const Twine& = "") const {
406 return C;
407 }
408
409 //===--------------------------------------------------------------------===//
410 // Instruction creation methods: Terminators
411 //===--------------------------------------------------------------------===//
412
413 /// CreateRetVoid - Create a 'ret void' instruction.
414 ReturnInst *CreateRetVoid() {
415 return Insert(ReturnInst::Create(Context));
416 }
417
418 /// @verbatim
419 /// CreateRet - Create a 'ret ' instruction.
420 /// @endverbatim
421 ReturnInst *CreateRet(Value *V) {
422 return Insert(ReturnInst::Create(Context, V));
423 }
424
425 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
426 /// with one Value from the retVals array each, that build a aggregate
427 /// return value one value at a time, and a ret instruction to return
428 /// the resulting aggregate value. This is a convenience function for
429 /// code that uses aggregate return values as a vehicle for having
430 /// multiple return values.
431 ///
432 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
433 Value *V = UndefValue::get(getCurrentFunctionReturnType());
434 for (unsigned i = 0; i != N; ++i)
435 V = CreateInsertValue(V, retVals[i], i, "mrv");
436 return Insert(ReturnInst::Create(Context, V));
437 }
438
439 /// CreateBr - Create an unconditional 'br label X' instruction.
440 BranchInst *CreateBr(BasicBlock *Dest) {
441 return Insert(BranchInst::Create(Dest));
442 }
443
444 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
445 /// instruction.
446 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
447 return Insert(BranchInst::Create(True, False, Cond));
448 }
449
450 /// CreateSwitch - Create a switch instruction with the specified value,
451 /// default dest, and with a hint for the number of cases that will be added
452 /// (for efficient allocation).
453 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
454 return Insert(SwitchInst::Create(V, Dest, NumCases));
455 }
456
457 /// CreateIndirectBr - Create an indirect branch instruction with the
458 /// specified address operand, with an optional hint for the number of
459 /// destinations that will be added (for efficient allocation).
460 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
461 return Insert(IndirectBrInst::Create(Addr, NumDests));
462 }
463
464 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
465 BasicBlock *UnwindDest, const Twine &Name = "") {
466 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
467 ArrayRef()),
468 Name);
469 }
470 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
471 BasicBlock *UnwindDest, Value *Arg1,
472 const Twine &Name = "") {
473 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
474 Name);
475 }
476 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
477 BasicBlock *UnwindDest, Value *Arg1,
478 Value *Arg2, Value *Arg3,
479 const Twine &Name = "") {
480 Value *Args[] = { Arg1, Arg2, Arg3 };
481 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
482 Name);
483 }
484 /// CreateInvoke - Create an invoke instruction.
485 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
486 BasicBlock *UnwindDest, ArrayRef Args,
487 const Twine &Name = "") {
488 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
489 Name);
490 }
491
492 ResumeInst *CreateResume(Value *Exn) {
493 return Insert(ResumeInst::Create(Exn));
494 }
495
496 UnreachableInst *CreateUnreachable() {
497 return Insert(new UnreachableInst(Context));
498 }
499
500 //===--------------------------------------------------------------------===//
501 // Instruction creation methods: Binary Operators
502 //===--------------------------------------------------------------------===//
503 private:
504 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
505 Value *LHS, Value *RHS,
506 const Twine &Name,
507 bool HasNUW, bool HasNSW) {
508 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
509 if (HasNUW) BO->setHasNoUnsignedWrap();
510 if (HasNSW) BO->setHasNoSignedWrap();
511 return BO;
512 }
513
514 Instruction *AddFPMathTag(Instruction *I, MDNode *FPMathTag) const {
515 if (!FPMathTag)
516 FPMathTag = DefaultFPMathTag;
517 if (FPMathTag)
518 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
519 return I;
520 }
521 public:
522 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
523 bool HasNUW = false, bool HasNSW = false) {
524 if (Constant *LC = dyn_cast(LHS))
525 if (Constant *RC = dyn_cast(RHS))
526 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
527 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
528 HasNUW, HasNSW);
529 }
530 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
531 return CreateAdd(LHS, RHS, Name, false, true);
532 }
533 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
534 return CreateAdd(LHS, RHS, Name, true, false);
535 }
536 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
537 MDNode *FPMathTag = 0) {
538 if (Constant *LC = dyn_cast(LHS))
539 if (Constant *RC = dyn_cast(RHS))
540 return Insert(Folder.CreateFAdd(LC, RC), Name);
541 return Insert(AddFPMathTag(BinaryOperator::CreateFAdd(LHS, RHS),
542 FPMathTag), Name);
543 }
544 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
545 bool HasNUW = false, bool HasNSW = false) {
546 if (Constant *LC = dyn_cast(LHS))
547 if (Constant *RC = dyn_cast(RHS))
548 return Insert(Folder.CreateSub(LC, RC), Name);
549 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
550 HasNUW, HasNSW);
551 }
552 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
553 return CreateSub(LHS, RHS, Name, false, true);
554 }
555 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
556 return CreateSub(LHS, RHS, Name, true, false);
557 }
558 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
559 MDNode *FPMathTag = 0) {
560 if (Constant *LC = dyn_cast(LHS))
561 if (Constant *RC = dyn_cast(RHS))
562 return Insert(Folder.CreateFSub(LC, RC), Name);
563 return Insert(AddFPMathTag(BinaryOperator::CreateFSub(LHS, RHS),
564 FPMathTag), Name);
565 }
566 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
567 bool HasNUW = false, bool HasNSW = false) {
568 if (Constant *LC = dyn_cast(LHS))
569 if (Constant *RC = dyn_cast(RHS))
570 return Insert(Folder.CreateMul(LC, RC), Name);
571 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
572 HasNUW, HasNSW);
573 }
574 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
575 return CreateMul(LHS, RHS, Name, false, true);
576 }
577 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
578 return CreateMul(LHS, RHS, Name, true, false);
579 }
580 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
581 MDNode *FPMathTag = 0) {
582 if (Constant *LC = dyn_cast(LHS))
583 if (Constant *RC = dyn_cast(RHS))
584 return Insert(Folder.CreateFMul(LC, RC), Name);
585 return Insert(AddFPMathTag(BinaryOperator::CreateFMul(LHS, RHS),
586 FPMathTag), Name);
587 }
588 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
589 bool isExact = false) {
590 if (Constant *LC = dyn_cast(LHS))
591 if (Constant *RC = dyn_cast(RHS))
592 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
593 if (!isExact)
594 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
595 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
596 }
597 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
598 return CreateUDiv(LHS, RHS, Name, true);
599 }
600 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
601 bool isExact = false) {
602 if (Constant *LC = dyn_cast(LHS))
603 if (Constant *RC = dyn_cast(RHS))
604 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
605 if (!isExact)
606 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
607 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
608 }
609 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
610 return CreateSDiv(LHS, RHS, Name, true);
611 }
612 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
613 MDNode *FPMathTag = 0) {
614 if (Constant *LC = dyn_cast(LHS))
615 if (Constant *RC = dyn_cast(RHS))
616 return Insert(Folder.CreateFDiv(LC, RC), Name);
617 return Insert(AddFPMathTag(BinaryOperator::CreateFDiv(LHS, RHS),
618 FPMathTag), Name);
619 }
620 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
621 if (Constant *LC = dyn_cast(LHS))
622 if (Constant *RC = dyn_cast(RHS))
623 return Insert(Folder.CreateURem(LC, RC), Name);
624 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
625 }
626 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
627 if (Constant *LC = dyn_cast(LHS))
628 if (Constant *RC = dyn_cast(RHS))
629 return Insert(Folder.CreateSRem(LC, RC), Name);
630 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
631 }
632 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
633 MDNode *FPMathTag = 0) {
634 if (Constant *LC = dyn_cast(LHS))
635 if (Constant *RC = dyn_cast(RHS))
636 return Insert(Folder.CreateFRem(LC, RC), Name);
637 return Insert(AddFPMathTag(BinaryOperator::CreateFRem(LHS, RHS),
638 FPMathTag), Name);
639 }
640
641 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
642 bool HasNUW = false, bool HasNSW = false) {
643 if (Constant *LC = dyn_cast(LHS))
644 if (Constant *RC = dyn_cast(RHS))
645 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
646 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
647 HasNUW, HasNSW);
648 }
649 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
650 bool HasNUW = false, bool HasNSW = false) {
651 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
652 HasNUW, HasNSW);
653 }
654 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
655 bool HasNUW = false, bool HasNSW = false) {
656 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
657 HasNUW, HasNSW);
658 }
659
660 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
661 bool isExact = false) {
662 if (Constant *LC = dyn_cast(LHS))
663 if (Constant *RC = dyn_cast(RHS))
664 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
665 if (!isExact)
666 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
667 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
668 }
669 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
670 bool isExact = false) {
671 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
672 }
673 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
674 bool isExact = false) {
675 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
676 }
677
678 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
679 bool isExact = false) {
680 if (Constant *LC = dyn_cast(LHS))
681 if (Constant *RC = dyn_cast(RHS))
682 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
683 if (!isExact)
684 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
685 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
686 }
687 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
688 bool isExact = false) {
689 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
690 }
691 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
692 bool isExact = false) {
693 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
694 }
695
696 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
697 if (Constant *RC = dyn_cast(RHS)) {
698 if (isa(RC) && cast(RC)->isAllOnesValue())
699 return LHS; // LHS & -1 -> LHS
700 if (Constant *LC = dyn_cast(LHS))
701 return Insert(Folder.CreateAnd(LC, RC), Name);
702 }
703 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
704 }
705 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
706 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
707 }
708 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
709 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
710 }
711
712 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
713 if (Constant *RC = dyn_cast(RHS)) {
714 if (RC->isNullValue())
715 return LHS; // LHS | 0 -> LHS
716 if (Constant *LC = dyn_cast(LHS))
717 return Insert(Folder.CreateOr(LC, RC), Name);
718 }
719 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
720 }
721 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
722 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
723 }
724 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
725 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
726 }
727
728 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
729 if (Constant *LC = dyn_cast(LHS))
730 if (Constant *RC = dyn_cast(RHS))
731 return Insert(Folder.CreateXor(LC, RC), Name);
732 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
733 }
734 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
735 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
736 }
737 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
738 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
739 }
740
741 Value *CreateBinOp(Instruction::BinaryOps Opc,
742 Value *LHS, Value *RHS, const Twine &Name = "") {
743 if (Constant *LC = dyn_cast(LHS))
744 if (Constant *RC = dyn_cast(RHS))
745 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
746 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
747 }
748
749 Value *CreateNeg(Value *V, const Twine &Name = "",
750 bool HasNUW = false, bool HasNSW = false) {
751 if (Constant *VC = dyn_cast(V))
752 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
753 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
754 if (HasNUW) BO->setHasNoUnsignedWrap();
755 if (HasNSW) BO->setHasNoSignedWrap();
756 return BO;
757 }
758 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
759 return CreateNeg(V, Name, false, true);
760 }
761 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
762 return CreateNeg(V, Name, true, false);
763 }
764 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
765 if (Constant *VC = dyn_cast(V))
766 return Insert(Folder.CreateFNeg(VC), Name);
767 return Insert(AddFPMathTag(BinaryOperator::CreateFNeg(V), FPMathTag), Name);
768 }
769 Value *CreateNot(Value *V, const Twine &Name = "") {
770 if (Constant *VC = dyn_cast(V))
771 return Insert(Folder.CreateNot(VC), Name);
772 return Insert(BinaryOperator::CreateNot(V), Name);
773 }
774
775 //===--------------------------------------------------------------------===//
776 // Instruction creation methods: Memory Instructions
777 //===--------------------------------------------------------------------===//
778
779 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
780 const Twine &Name = "") {
781 return Insert(new AllocaInst(Ty, ArraySize), Name);
782 }
783 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
784 // converting the string to 'bool' for the isVolatile parameter.
785 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
786 return Insert(new LoadInst(Ptr), Name);
787 }
788 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
789 return Insert(new LoadInst(Ptr), Name);
790 }
791 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
792 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
793 }
794 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
795 return Insert(new StoreInst(Val, Ptr, isVolatile));
796 }
797 FenceInst *CreateFence(AtomicOrdering Ordering,
798 SynchronizationScope SynchScope = CrossThread) {
799 return Insert(new FenceInst(Context, Ordering, SynchScope));
800 }
801 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
802 AtomicOrdering Ordering,
803 SynchronizationScope SynchScope = CrossThread) {
804 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
805 }
806 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
807 AtomicOrdering Ordering,
808 SynchronizationScope SynchScope = CrossThread) {
809 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
810 }
811 Value *CreateGEP(Value *Ptr, ArrayRef IdxList,
812 const Twine &Name = "") {
813 if (Constant *PC = dyn_cast(Ptr)) {
814 // Every index must be constant.
815 size_t i, e;
816 for (i = 0, e = IdxList.size(); i != e; ++i)
817 if (!isa(IdxList[i]))
818 break;
819 if (i == e)
820 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
821 }
822 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
823 }
824 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef IdxList,
825 const Twine &Name = "") {
826 if (Constant *PC = dyn_cast(Ptr)) {
827 // Every index must be constant.
828 size_t i, e;
829 for (i = 0, e = IdxList.size(); i != e; ++i)
830 if (!isa(IdxList[i]))
831 break;
832 if (i == e)
833 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
834 }
835 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
836 }
837 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
838 if (Constant *PC = dyn_cast(Ptr))
839 if (Constant *IC = dyn_cast(Idx))
840 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
841 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
842 }
843 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
844 if (Constant *PC = dyn_cast(Ptr))
845 if (Constant *IC = dyn_cast(Idx))
846 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
847 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
848 }
849 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
850 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
851
852 if (Constant *PC = dyn_cast(Ptr))
853 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
854
855 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
856 }
857 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
858 const Twine &Name = "") {
859 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
860
861 if (Constant *PC = dyn_cast(Ptr))
862 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
863
864 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
865 }
866 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
867 const Twine &Name = "") {
868 Value *Idxs[] = {
869 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
870 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
871 };
872
873 if (Constant *PC = dyn_cast(Ptr))
874 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
875
876 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
877 }
878 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
879 const Twine &Name = "") {
880 Value *Idxs[] = {
881 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
882 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
883 };
884
885 if (Constant *PC = dyn_cast(Ptr))
886 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
887
888 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
889 }
890 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
891 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
892
893 if (Constant *PC = dyn_cast(Ptr))
894 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
895
896 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
897 }
898 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
899 const Twine &Name = "") {
900 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
901
902 if (Constant *PC = dyn_cast(Ptr))
903 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
904
905 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
906 }
907 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
908 const Twine &Name = "") {
909 Value *Idxs[] = {
910 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
911 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
912 };
913
914 if (Constant *PC = dyn_cast(Ptr))
915 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
916
917 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
918 }
919 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
920 const Twine &Name = "") {
921 Value *Idxs[] = {
922 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
923 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
924 };
925
926 if (Constant *PC = dyn_cast(Ptr))
927 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
928
929 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
930 }
931 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
932 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
933 }
934
935 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
936 /// with "i8*" type instead of a pointer to array of i8.
937 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
938 Value *gv = CreateGlobalString(Str, Name);
939 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
940 Value *Args[] = { zero, zero };
941 return CreateInBoundsGEP(gv, Args, Name);
942 }
943
944 //===--------------------------------------------------------------------===//
945 // Instruction creation methods: Cast/Conversion Operators
946 //===--------------------------------------------------------------------===//
947
948 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
949 return CreateCast(Instruction::Trunc, V, DestTy, Name);
950 }
951 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
952 return CreateCast(Instruction::ZExt, V, DestTy, Name);
953 }
954 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
955 return CreateCast(Instruction::SExt, V, DestTy, Name);
956 }
957 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
958 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
959 }
960 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
961 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
962 }
963 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
964 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
965 }
966 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
967 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
968 }
969 Value *CreateFPTrunc(Value *V, Type *DestTy,
970 const Twine &Name = "") {
971 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
972 }
973 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
974 return CreateCast(Instruction::FPExt, V, DestTy, Name);
975 }
976 Value *CreatePtrToInt(Value *V, Type *DestTy,
977 const Twine &Name = "") {
978 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
979 }
980 Value *CreateIntToPtr(Value *V, Type *DestTy,
981 const Twine &Name = "") {
982 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
983 }
984 Value *CreateBitCast(Value *V, Type *DestTy,
985 const Twine &Name = "") {
986 return CreateCast(Instruction::BitCast, V, DestTy, Name);
987 }
988 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
989 const Twine &Name = "") {
990 if (V->getType() == DestTy)
991 return V;
992 if (Constant *VC = dyn_cast(V))
993 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
994 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
995 }
996 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
997 const Twine &Name = "") {
998 if (V->getType() == DestTy)
999 return V;
1000 if (Constant *VC = dyn_cast(V))
1001 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1002 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1003 }
1004 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1005 const Twine &Name = "") {
1006 if (V->getType() == DestTy)
1007 return V;
1008 if (Constant *VC = dyn_cast(V))
1009 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1010 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1011 }
1012 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1013 const Twine &Name = "") {
1014 if (V->getType() == DestTy)
1015 return V;
1016 if (Constant *VC = dyn_cast(V))
1017 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1018 return Insert(CastInst::Create(Op, V, DestTy), Name);
1019 }
1020 Value *CreatePointerCast(Value *V, Type *DestTy,
1021 const Twine &Name = "") {
1022 if (V->getType() == DestTy)
1023 return V;
1024 if (Constant *VC = dyn_cast(V))
1025 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1026 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1027 }
1028 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1029 const Twine &Name = "") {
1030 if (V->getType() == DestTy)
1031 return V;
1032 if (Constant *VC = dyn_cast(V))
1033 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1034 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1035 }
1036 private:
1037 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
1038 // error, instead of converting the string to bool for the isSigned parameter.
1039 Value *CreateIntCast(Value *, Type *, const char *); // DO NOT IMPLEMENT
1040 public:
1041 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1042 if (V->getType() == DestTy)
1043 return V;
1044 if (Constant *VC = dyn_cast(V))
1045 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1046 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1047 }
1048
1049 //===--------------------------------------------------------------------===//
1050 // Instruction creation methods: Compare Instructions
1051 //===--------------------------------------------------------------------===//
1052
1053 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1054 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1055 }
1056 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1057 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1058 }
1059 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1060 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1061 }
1062 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1063 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1064 }
1065 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1066 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1067 }
1068 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1069 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1070 }
1071 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1072 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1073 }
1074 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1075 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1076 }
1077 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1078 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1079 }
1080 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1081 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1082 }
1083
1084 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1085 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1086 }
1087 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1088 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1089 }
1090 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1091 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1092 }
1093 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1094 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1095 }
1096 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1097 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1098 }
1099 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1100 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1101 }
1102 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1103 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1104 }
1105 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1106 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1107 }
1108 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1109 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1110 }
1111 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1112 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1113 }
1114 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1115 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1116 }
1117 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1118 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1119 }
1120 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1121 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1122 }
1123 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1124 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1125 }
1126
1127 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1128 const Twine &Name = "") {
1129 if (Constant *LC = dyn_cast(LHS))
1130 if (Constant *RC = dyn_cast(RHS))
1131 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1132 return Insert(new ICmpInst(P, LHS, RHS), Name);
1133 }
1134 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1135 const Twine &Name = "") {
1136 if (Constant *LC = dyn_cast(LHS))
1137 if (Constant *RC = dyn_cast(RHS))
1138 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1139 return Insert(new FCmpInst(P, LHS, RHS), Name);
1140 }
1141
1142 //===--------------------------------------------------------------------===//
1143 // Instruction creation methods: Other Instructions
1144 //===--------------------------------------------------------------------===//
1145
1146 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1147 const Twine &Name = "") {
1148 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1149 }
1150
1151 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1152 return Insert(CallInst::Create(Callee), Name);
1153 }
1154 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1155 return Insert(CallInst::Create(Callee, Arg), Name);
1156 }
1157 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1158 const Twine &Name = "") {
1159 Value *Args[] = { Arg1, Arg2 };
1160 return Insert(CallInst::Create(Callee, Args), Name);
1161 }
1162 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1163 const Twine &Name = "") {
1164 Value *Args[] = { Arg1, Arg2, Arg3 };
1165 return Insert(CallInst::Create(Callee, Args), Name);
1166 }
1167 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1168 Value *Arg4, const Twine &Name = "") {
1169 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1170 return Insert(CallInst::Create(Callee, Args), Name);
1171 }
1172 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1173 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1174 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1175 return Insert(CallInst::Create(Callee, Args), Name);
1176 }
1177
1178 CallInst *CreateCall(Value *Callee, ArrayRef Args,
1179 const Twine &Name = "") {
1180 return Insert(CallInst::Create(Callee, Args), Name);
1181 }
1182
1183 Value *CreateSelect(Value *C, Value *True, Value *False,
1184 const Twine &Name = "") {
1185 if (Constant *CC = dyn_cast(C))
1186 if (Constant *TC = dyn_cast(True))
1187 if (Constant *FC = dyn_cast(False))
1188 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1189 return Insert(SelectInst::Create(C, True, False), Name);
1190 }
1191
1192 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1193 return Insert(new VAArgInst(List, Ty), Name);
1194 }
1195
1196 Value *CreateExtractElement(Value *Vec, Value *Idx,
1197 const Twine &Name = "") {
1198 if (Constant *VC = dyn_cast(Vec))
1199 if (Constant *IC = dyn_cast(Idx))
1200 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1201 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1202 }
1203
1204 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1205 const Twine &Name = "") {
1206 if (Constant *VC = dyn_cast(Vec))
1207 if (Constant *NC = dyn_cast(NewElt))
1208 if (Constant *IC = dyn_cast(Idx))
1209 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1210 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1211 }
1212
1213 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1214 const Twine &Name = "") {
1215 if (Constant *V1C = dyn_cast(V1))
1216 if (Constant *V2C = dyn_cast(V2))
1217 if (Constant *MC = dyn_cast(Mask))
1218 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1219 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1220 }
1221
1222 Value *CreateExtractValue(Value *Agg,
1223 ArrayRef Idxs,
1224 const Twine &Name = "") {
1225 if (Constant *AggC = dyn_cast(Agg))
1226 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1227 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1228 }
1229
1230 Value *CreateInsertValue(Value *Agg, Value *Val,
1231 ArrayRef Idxs,
1232 const Twine &Name = "") {
1233 if (Constant *AggC = dyn_cast(Agg))
1234 if (Constant *ValC = dyn_cast(Val))
1235 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1236 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1237 }
1238
1239 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1240 const Twine &Name = "") {
1241 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses, Name));
1242 }
1243
1244 //===--------------------------------------------------------------------===//
1245 // Utility creation methods
1246 //===--------------------------------------------------------------------===//
1247
1248 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1249 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1250 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1251 Name);
1252 }
1253
1254 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1255 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1256 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1257 Name);
1258 }
1259
1260 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1261 /// dividing out the size of the pointed-to objects. This is intended to
1262 /// implement C-style pointer subtraction. As such, the pointers must be
1263 /// appropriately aligned for their element types and pointing into the
1264 /// same object.
1265 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1266 assert(LHS->getType() == RHS->getType() &&
1267 "Pointer subtraction operand types must match!");
1268 PointerType *ArgType = cast(LHS->getType());
1269 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1270 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1271 Value *Difference = CreateSub(LHS_int, RHS_int);
1272 return CreateExactSDiv(Difference,
1273 ConstantExpr::getSizeOf(ArgType->getElementType()),
1274 Name);
1275 }
1276 };
1277
1278 }
1279
1280 #endif
+0
-1281
include/llvm/Support/IRBuilder.h less more
None //===---- 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/Instructions.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/LLVMContext.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/ConstantFolder.h"
24
25 namespace llvm {
26 class MDNode;
27
28 /// IRBuilderDefaultInserter - This provides the default implementation of the
29 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
30 /// created by IRBuilder and needs to be inserted. By default, this inserts the
31 /// instruction at the insertion point.
32 template
33 class IRBuilderDefaultInserter {
34 protected:
35 void InsertHelper(Instruction *I, const Twine &Name,
36 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
37 if (BB) BB->getInstList().insert(InsertPt, I);
38 if (preserveNames)
39 I->setName(Name);
40 }
41 };
42
43 /// IRBuilderBase - Common base class shared among various IRBuilders.
44 class IRBuilderBase {
45 DebugLoc CurDbgLocation;
46 protected:
47 BasicBlock *BB;
48 BasicBlock::iterator InsertPt;
49 LLVMContext &Context;
50 public:
51
52 IRBuilderBase(LLVMContext &context)
53 : Context(context) {
54 ClearInsertionPoint();
55 }
56
57 //===--------------------------------------------------------------------===//
58 // Builder configuration methods
59 //===--------------------------------------------------------------------===//
60
61 /// ClearInsertionPoint - Clear the insertion point: created instructions will
62 /// not be inserted into a block.
63 void ClearInsertionPoint() {
64 BB = 0;
65 }
66
67 BasicBlock *GetInsertBlock() const { return BB; }
68 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
69 LLVMContext &getContext() const { return Context; }
70
71 /// SetInsertPoint - This specifies that created instructions should be
72 /// appended to the end of the specified block.
73 void SetInsertPoint(BasicBlock *TheBB) {
74 BB = TheBB;
75 InsertPt = BB->end();
76 }
77
78 /// SetInsertPoint - This specifies that created instructions should be
79 /// inserted before the specified instruction.
80 void SetInsertPoint(Instruction *I) {
81 BB = I->getParent();
82 InsertPt = I;
83 SetCurrentDebugLocation(I->getDebugLoc());
84 }
85
86 /// SetInsertPoint - This specifies that created instructions should be
87 /// inserted at the specified point.
88 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
89 BB = TheBB;
90 InsertPt = IP;
91 }
92
93 /// SetInsertPoint(Use) - Find the nearest point that dominates this use, and
94 /// specify that created instructions should be inserted at this point.
95 void SetInsertPoint(Use &U) {
96 Instruction *UseInst = cast(U.getUser());
97 if (PHINode *Phi = dyn_cast(UseInst)) {
98 BasicBlock *PredBB = Phi->getIncomingBlock(U);
99 assert(U != PredBB->getTerminator() && "critical edge not split");
100 SetInsertPoint(PredBB, PredBB->getTerminator());
101 return;
102 }
103 SetInsertPoint(UseInst);
104 }
105
106 /// SetCurrentDebugLocation - Set location information used by debugging
107 /// information.
108 void SetCurrentDebugLocation(const DebugLoc &L) {
109 CurDbgLocation = L;
110 }
111
112 /// getCurrentDebugLocation - Get location information used by debugging
113 /// information.
114 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
115
116 /// SetInstDebugLocation - If this builder has a current debug location, set
117 /// it on the specified instruction.
118 void SetInstDebugLocation(Instruction *I) const {
119 if (!CurDbgLocation.isUnknown())
120 I->setDebugLoc(CurDbgLocation);
121 }
122
123 /// getCurrentFunctionReturnType - Get the return type of the current function
124 /// that we're emitting into.
125 Type *getCurrentFunctionReturnType() const;
126
127 /// InsertPoint - A saved insertion point.
128 class InsertPoint {
129 BasicBlock *Block;
130 BasicBlock::iterator Point;
131
132 public:
133 /// Creates a new insertion point which doesn't point to anything.
134 InsertPoint() : Block(0) {}
135
136 /// Creates a new insertion point at the given location.
137 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
138 : Block(InsertBlock), Point(InsertPoint) {}
139
140 /// isSet - Returns true if this insert point is set.
141 bool isSet() const { return (Block != 0); }
142
143 llvm::BasicBlock *getBlock() const { return Block; }
144 llvm::BasicBlock::iterator getPoint() const { return Point; }
145 };
146
147 /// saveIP - Returns the current insert point.
148 InsertPoint saveIP() const {
149 return InsertPoint(GetInsertBlock(), GetInsertPoint());
150 }
151
152 /// saveAndClearIP - Returns the current insert point, clearing it
153 /// in the process.
154 InsertPoint saveAndClearIP() {
155 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
156 ClearInsertionPoint();
157 return IP;
158 }
159
160 /// restoreIP - Sets the current insert point to a previously-saved
161 /// location.
162 void restoreIP(InsertPoint IP) {
163 if (IP.isSet())
164 SetInsertPoint(IP.getBlock(), IP.getPoint());
165 else
166 ClearInsertionPoint();
167 }
168
169 //===--------------------------------------------------------------------===//
170 // Miscellaneous creation methods.
171 //===--------------------------------------------------------------------===//
172
173 /// CreateGlobalString - Make a new global variable with an initializer that
174 /// has array of i8 type filled in with the nul terminated string value
175 /// specified. The new global variable will be marked mergable with any
176 /// others of the same contents. If Name is specified, it is the name of the
177 /// global variable created.
178 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
179
180 /// getInt1 - Get a constant value representing either true or false.
181 ConstantInt *getInt1(bool V) {
182 return ConstantInt::get(getInt1Ty(), V);
183 }
184
185 /// getTrue - Get the constant value for i1 true.
186 ConstantInt *getTrue() {
187 return ConstantInt::getTrue(Context);
188 }
189
190 /// getFalse - Get the constant value for i1 false.
191 ConstantInt *getFalse() {
192 return ConstantInt::getFalse(Context);
193 }
194
195 /// getInt8 - Get a constant 8-bit value.
196 ConstantInt *getInt8(uint8_t C) {
197 return ConstantInt::get(getInt8Ty(), C);
198 }
199
200 /// getInt16 - Get a constant 16-bit value.
201 ConstantInt *getInt16(uint16_t C) {
202 return ConstantInt::get(getInt16Ty(), C);
203 }
204
205 /// getInt32 - Get a constant 32-bit value.
206 ConstantInt *getInt32(uint32_t C) {
207 return ConstantInt::get(getInt32Ty(), C);
208 }
209
210 /// getInt64 - Get a constant 64-bit value.
211 ConstantInt *getInt64(uint64_t C) {
212 return ConstantInt::get(getInt64Ty(), C);
213 }
214
215 /// getInt - Get a constant integer value.
216 ConstantInt *getInt(const APInt &AI) {
217 return ConstantInt::get(Context, AI);
218 }
219
220 //===--------------------------------------------------------------------===//
221 // Type creation methods
222 //===--------------------------------------------------------------------===//
223
224 /// getInt1Ty - Fetch the type representing a single bit
225 IntegerType *getInt1Ty() {
226 return Type::getInt1Ty(Context);
227 }
228
229 /// getInt8Ty - Fetch the type representing an 8-bit integer.
230 IntegerType *getInt8Ty() {
231 return Type::getInt8Ty(Context);
232 }
233
234 /// getInt16Ty - Fetch the type representing a 16-bit integer.
235 IntegerType *getInt16Ty() {
236 return Type::getInt16Ty(Context);
237 }
238
239 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
240 IntegerType *getInt32Ty() {
241 return Type::getInt32Ty(Context);
242 }
243
244 /// getInt64Ty - Fetch the type representing a 64-bit integer.
245 IntegerType *getInt64Ty() {
246 return Type::getInt64Ty(Context);
247 }
248
249 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
250 Type *getFloatTy() {
251 return Type::getFloatTy(Context);
252 }
253
254 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
255 Type *getDoubleTy() {
256 return Type::getDoubleTy(Context);
257 }
258
259 /// getVoidTy - Fetch the type representing void.
260 Type *getVoidTy() {
261 return Type::getVoidTy(Context);
262 }
263
264 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
265 return Type::getInt8PtrTy(Context, AddrSpace);
266 }
267
268 //===--------------------------------------------------------------------===//
269 // Intrinsic creation methods
270 //===--------------------------------------------------------------------===//
271
272 /// CreateMemSet - Create and insert a memset to the specified pointer and the
273 /// specified value. If the pointer isn't an i8*, it will be converted. If a
274 /// TBAA tag is specified, it will be added to the instruction.
275 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
276 bool isVolatile = false, MDNode *TBAATag = 0) {
277 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
278 }
279
280 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
281 bool isVolatile = false, MDNode *TBAATag = 0);
282
283 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
284 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
285 /// specified, it will be added to the instruction.
286 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
287 bool isVolatile = false, MDNode *TBAATag = 0) {
288 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
289 }
290
291 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
292 bool isVolatile = false, MDNode *TBAATag = 0);
293
294 /// CreateMemMove - Create and insert a memmove between the specified
295 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
296 /// tag is specified, it will be added to the instruction.
297 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
298 bool isVolatile = false, MDNode *TBAATag = 0) {
299 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
300 }
301
302 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
303 bool isVolatile = false, MDNode *TBAATag = 0);
304
305 /// CreateLifetimeStart - Create a lifetime.start intrinsic. If the pointer
306 /// isn't i8* it will be converted.
307 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
308
309 /// CreateLifetimeEnd - Create a lifetime.end intrinsic. If the pointer isn't
310 /// i8* it will be converted.
311 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
312
313 private:
314 Value *getCastedInt8PtrValue(Value *Ptr);
315 };
316
317 /// IRBuilder - This provides a uniform API for creating instructions and
318 /// inserting them into a basic block: either at the end of a BasicBlock, or
319 /// at a specific iterator location in a block.
320 ///
321 /// Note that the builder does not expose the full generality of LLVM
322 /// instructions. For access to extra instruction properties, use the mutators
323 /// (e.g. setVolatile) on the instructions after they have been created.
324 /// The first template argument handles whether or not to preserve names in the
325 /// final instruction output. This defaults to on. The second template argument
326 /// specifies a class to use for creating constants. This defaults to creating
327 /// minimally folded constants. The fourth template argument allows clients to
328 /// specify custom insertion hooks that are called on every newly created
329 /// insertion.
330 template
331 typename Inserter = IRBuilderDefaultInserter >
332 class IRBuilder : public IRBuilderBase, public Inserter {
333 T Folder;
334 MDNode *DefaultFPMathTag;
335 public:
336 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
337 MDNode *FPMathTag = 0)
338 : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag) {
339 }
340
341 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0) : IRBuilderBase(C),
342 Folder(), DefaultFPMathTag(FPMathTag) {
343 }
344
345 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
346 : IRBuilderBase(TheBB->getContext()), Folder(F),
347 DefaultFPMathTag(FPMathTag) {
348 SetInsertPoint(TheBB);
349 }
350
351 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
352 : IRBuilderBase(TheBB->getContext()), Folder(),
353 DefaultFPMathTag(FPMathTag) {
354 SetInsertPoint(TheBB);
355 }
356
357 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
358 : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
359 SetInsertPoint(IP);
360 SetCurrentDebugLocation(IP->getDebugLoc());
361 }
362
363 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
364 : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
365 SetInsertPoint(U);
366 SetCurrentDebugLocation(cast(U.getUser())->getDebugLoc());
367 }
368
369 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
370 MDNode *FPMathTag = 0)
371 : IRBuilderBase(TheBB->getContext()), Folder(F),
372 DefaultFPMathTag(FPMathTag) {
373 SetInsertPoint(TheBB, IP);
374 }
375
376 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
377 : IRBuilderBase(TheBB->getContext()), Folder(),
378 DefaultFPMathTag(FPMathTag) {
379 SetInsertPoint(TheBB, IP);
380 }
381
382 /// getFolder - Get the constant folder being used.
383 const T &getFolder() { return Folder; }
384
385 /// getDefaultFPMathTag - Get the floating point math metadata being used.
386 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
387
388 /// SetDefaultFPMathTag - Set the floating point math metadata to be used.
389 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
390
391 /// isNamePreserving - Return true if this builder is configured to actually
392 /// add the requested names to IR created through it.
393 bool isNamePreserving() const { return preserveNames; }
394
395 /// Insert - Insert and return the specified instruction.
396 template
397 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
398 this->InsertHelper(I, Name, BB, InsertPt);
399 if (!getCurrentDebugLocation().isUnknown())
400 this->SetInstDebugLocation(I);
401 return I;
402 }
403
404 /// Insert - No-op overload to handle constants.
405 Constant *Insert(Constant *C, const Twine& = "") const {
406 return C;
407 }
408
409 //===--------------------------------------------------------------------===//
410 // Instruction creation methods: Terminators
411 //===--------------------------------------------------------------------===//
412
413 /// CreateRetVoid - Create a 'ret void' instruction.
414 ReturnInst *CreateRetVoid() {
415 return Insert(ReturnInst::Create(Context));
416 }
417
418 /// @verbatim
419 /// CreateRet - Create a 'ret ' instruction.
420 /// @endverbatim
421 ReturnInst *CreateRet(Value *V) {
422 return Insert(ReturnInst::Create(Context, V));
423 }
424
425 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
426 /// with one Value from the retVals array each, that build a aggregate
427 /// return value one value at a time, and a ret instruction to return
428 /// the resulting aggregate value. This is a convenience function for
429 /// code that uses aggregate return values as a vehicle for having
430 /// multiple return values.
431 ///
432 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
433 Value *V = UndefValue::get(getCurrentFunctionReturnType());
434 for (unsigned i = 0; i != N; ++i)
435 V = CreateInsertValue(V, retVals[i], i, "mrv");
436 return Insert(ReturnInst::Create(Context, V));
437 }
438
439 /// CreateBr - Create an unconditional 'br label X' instruction.
440 BranchInst *CreateBr(BasicBlock *Dest) {
441 return Insert(BranchInst::Create(Dest));
442 }
443
444 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
445 /// instruction.
446 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
447 return Insert(BranchInst::Create(True, False, Cond));
448 }
449
450 /// CreateSwitch - Create a switch instruction with the specified value,
451 /// default dest, and with a hint for the number of cases that will be added
452 /// (for efficient allocation).
453 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
454 return Insert(SwitchInst::Create(V, Dest, NumCases));
455 }
456
457 /// CreateIndirectBr - Create an indirect branch instruction with the
458 /// specified address operand, with an optional hint for the number of
459 /// destinations that will be added (for efficient allocation).
460 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
461 return Insert(IndirectBrInst::Create(Addr, NumDests));
462 }
463
464 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
465 BasicBlock *UnwindDest, const Twine &Name = "") {
466 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
467 ArrayRef()),
468 Name);
469 }
470 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
471 BasicBlock *UnwindDest, Value *Arg1,
472 const Twine &Name = "") {
473 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
474 Name);
475 }
476 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
477 BasicBlock *UnwindDest, Value *Arg1,
478 Value *Arg2, Value *Arg3,
479 const Twine &Name = "") {
480 Value *Args[] = { Arg1, Arg2, Arg3 };
481 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
482 Name);
483 }
484 /// CreateInvoke - Create an invoke instruction.
485 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
486 BasicBlock *UnwindDest, ArrayRef Args,
487 const Twine &Name = "") {
488 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
489 Name);
490 }
491
492 ResumeInst *CreateResume(Value *Exn) {
493 return Insert(ResumeInst::Create(Exn));
494 }
495
496 UnreachableInst *CreateUnreachable() {
497 return Insert(new UnreachableInst(Context));
498 }
499
500 //===--------------------------------------------------------------------===//
501 // Instruction creation methods: Binary Operators
502 //===--------------------------------------------------------------------===//
503 private:
504 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
505 Value *LHS, Value *RHS,
506 const Twine &Name,
507 bool HasNUW, bool HasNSW) {
508 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
509 if (HasNUW) BO->setHasNoUnsignedWrap();
510 if (HasNSW) BO->setHasNoSignedWrap();
511 return BO;
512 }
513
514 Instruction *AddFPMathTag(Instruction *I, MDNode *FPMathTag) const {
515 if (!FPMathTag)
516 FPMathTag = DefaultFPMathTag;
517 if (FPMathTag)
518 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
519 return I;
520 }
521 public:
522 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
523 bool HasNUW = false, bool HasNSW = false) {
524 if (Constant *LC = dyn_cast(LHS))
525 if (Constant *RC = dyn_cast(RHS))
526 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
527 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
528 HasNUW, HasNSW);
529 }
530 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
531 return CreateAdd(LHS, RHS, Name, false, true);
532 }
533 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
534 return CreateAdd(LHS, RHS, Name, true, false);
535 }
536 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
537 MDNode *FPMathTag = 0) {
538 if (Constant *LC = dyn_cast(LHS))
539 if (Constant *RC = dyn_cast(RHS))
540 return Insert(Folder.CreateFAdd(LC, RC), Name);
541 return Insert(AddFPMathTag(BinaryOperator::CreateFAdd(LHS, RHS),
542 FPMathTag), Name);
543 }
544 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
545 bool HasNUW = false, bool HasNSW = false) {
546 if (Constant *LC = dyn_cast(LHS))
547 if (Constant *RC = dyn_cast(RHS))
548 return Insert(Folder.CreateSub(LC, RC), Name);
549 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
550 HasNUW, HasNSW);
551 }
552 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
553 return CreateSub(LHS, RHS, Name, false, true);
554 }
555 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
556 return CreateSub(LHS, RHS, Name, true, false);
557 }
558 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
559 MDNode *FPMathTag = 0) {
560 if (Constant *LC = dyn_cast(LHS))
561 if (Constant *RC = dyn_cast(RHS))
562 return Insert(Folder.CreateFSub(LC, RC), Name);
563 return Insert(AddFPMathTag(BinaryOperator::CreateFSub(LHS, RHS),
564 FPMathTag), Name);
565 }
566 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
567 bool HasNUW = false, bool HasNSW = false) {
568 if (Constant *LC = dyn_cast(LHS))
569 if (Constant *RC = dyn_cast(RHS))
570 return Insert(Folder.CreateMul(LC, RC), Name);
571 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
572 HasNUW, HasNSW);
573 }
574 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
575 return CreateMul(LHS, RHS, Name, false, true);
576 }
577 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
578 return CreateMul(LHS, RHS, Name, true, false);
579 }
580 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
581 MDNode *FPMathTag = 0) {
582 if (Constant *LC = dyn_cast(LHS))
583 if (Constant *RC = dyn_cast(RHS))
584 return Insert(Folder.CreateFMul(LC, RC), Name);
585 return Insert(AddFPMathTag(BinaryOperator::CreateFMul(LHS, RHS),
586 FPMathTag), Name);
587 }
588 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
589 bool isExact = false) {
590 if (Constant *LC = dyn_cast(LHS))
591 if (Constant *RC = dyn_cast(RHS))
592 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
593 if (!isExact)
594 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
595 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
596 }
597 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
598 return CreateUDiv(LHS, RHS, Name, true);
599 }
600 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
601 bool isExact = false) {
602 if (Constant *LC = dyn_cast(LHS))
603 if (Constant *RC = dyn_cast(RHS))
604 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
605 if (!isExact)
606 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
607 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
608 }
609 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
610 return CreateSDiv(LHS, RHS, Name, true);
611 }
612 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
613 MDNode *FPMathTag = 0) {
614 if (Constant *LC = dyn_cast(LHS))
615 if (Constant *RC = dyn_cast(RHS))
616 return Insert(Folder.CreateFDiv(LC, RC), Name);
617 return Insert(AddFPMathTag(BinaryOperator::CreateFDiv(LHS, RHS),
618 FPMathTag), Name);
619 }
620 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
621 if (Constant *LC = dyn_cast(LHS))
622 if (Constant *RC = dyn_cast(RHS))
623 return Insert(Folder.CreateURem(LC, RC), Name);
624 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
625 }
626 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
627 if (Constant *LC = dyn_cast(LHS))
628 if (Constant *RC = dyn_cast(RHS))
629 return Insert(Folder.CreateSRem(LC, RC), Name);
630 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
631 }
632 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
633 MDNode *FPMathTag = 0) {
634 if (Constant *LC = dyn_cast(LHS))
635 if (Constant *RC = dyn_cast(RHS))
636 return Insert(Folder.CreateFRem(LC, RC), Name);
637 return Insert(AddFPMathTag(BinaryOperator::CreateFRem(LHS, RHS),
638 FPMathTag), Name);
639 }
640
641 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
642 bool HasNUW = false, bool HasNSW = false) {
643 if (Constant *LC = dyn_cast(LHS))
644 if (Constant *RC = dyn_cast(RHS))
645 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
646 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
647 HasNUW, HasNSW);
648 }
649 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
650 bool HasNUW = false, bool HasNSW = false) {
651 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
652 HasNUW, HasNSW);
653 }
654 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
655 bool HasNUW = false, bool HasNSW = false) {
656 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
657 HasNUW, HasNSW);
658 }
659
660 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
661 bool isExact = false) {
662 if (Constant *LC = dyn_cast(LHS))
663 if (Constant *RC = dyn_cast(RHS))
664 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
665 if (!isExact)
666 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
667 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
668 }
669 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
670 bool isExact = false) {
671 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
672 }
673 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
674 bool isExact = false) {
675 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
676 }
677
678 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
679 bool isExact = false) {
680 if (Constant *LC = dyn_cast(LHS))
681 if (Constant *RC = dyn_cast(RHS))
682 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
683 if (!isExact)
684 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
685 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
686 }
687 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
688 bool isExact = false) {
689 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
690 }
691 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
692 bool isExact = false) {
693 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
694 }
695
696 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
697 if (Constant *RC = dyn_cast(RHS)) {
698 if (isa(RC) && cast(RC)->isAllOnesValue())
699 return LHS; // LHS & -1 -> LHS
700 if (Constant *LC = dyn_cast(LHS))
701 return Insert(Folder.CreateAnd(LC, RC), Name);
702 }
703 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
704 }
705 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
706 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
707 }
708 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
709 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
710 }
711
712 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
713 if (Constant *RC = dyn_cast(RHS)) {
714 if (RC->isNullValue())
715 return LHS; // LHS | 0 -> LHS
716 if (Constant *LC = dyn_cast(LHS))
717 return Insert(Folder.CreateOr(LC, RC), Name);
718 }
719 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
720 }
721 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
722 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
723 }
724 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
725 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
726 }
727
728 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
729 if (Constant *LC = dyn_cast(LHS))
730 if (Constant *RC = dyn_cast(RHS))
731 return Insert(Folder.CreateXor(LC, RC), Name);
732 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
733 }
734 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
735 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
736 }
737 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
738 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
739 }
740
741 Value *CreateBinOp(Instruction::BinaryOps Opc,
742 Value *LHS, Value *RHS, const Twine &Name = "") {
743 if (Constant *LC = dyn_cast(LHS))
744 if (Constant *RC = dyn_cast(RHS))
745 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
746 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
747 }
748
749 Value *CreateNeg(Value *V, const Twine &Name = "",
750 bool HasNUW = false, bool HasNSW = false) {
751 if (Constant *VC = dyn_cast(V))
752 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
753 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
754 if (HasNUW) BO->setHasNoUnsignedWrap();
755 if (HasNSW) BO->setHasNoSignedWrap();
756 return BO;
757 }
758 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
759 return CreateNeg(V, Name, false, true);
760 }
761 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
762 return CreateNeg(V, Name, true, false);
763 }
764 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
765 if (Constant *VC = dyn_cast(V))
766 return Insert(Folder.CreateFNeg(VC), Name);
767 return Insert(AddFPMathTag(BinaryOperator::CreateFNeg(V), FPMathTag), Name);
768 }
769 Value *CreateNot(Value *V, const Twine &Name = "") {
770 if (Constant *VC = dyn_cast(V))
771 return Insert(Folder.CreateNot(VC), Name);
772 return Insert(BinaryOperator::CreateNot(V), Name);
773 }
774
775 //===--------------------------------------------------------------------===//
776 // Instruction creation methods: Memory Instructions
777 //===--------------------------------------------------------------------===//
778
779 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
780 const Twine &Name = "") {
781 return Insert(new AllocaInst(Ty, ArraySize), Name);
782 }
783 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
784 // converting the string to 'bool' for the isVolatile parameter.
785 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
786 return Insert(new LoadInst(Ptr), Name);
787 }
788 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
789 return Insert(new LoadInst(Ptr), Name);
790 }
791 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
792 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
793 }
794 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
795 return Insert(new StoreInst(Val, Ptr, isVolatile));
796 }
797 FenceInst *CreateFence(AtomicOrdering Ordering,
798 SynchronizationScope SynchScope = CrossThread) {
799 return Insert(new FenceInst(Context, Ordering, SynchScope));
800 }
801 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
802 AtomicOrdering Ordering,
803 SynchronizationScope SynchScope = CrossThread) {
804 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
805 }
806 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
807 AtomicOrdering Ordering,
808 SynchronizationScope SynchScope = CrossThread) {
809 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
810 }
811 Value *CreateGEP(Value *Ptr, ArrayRef IdxList,
812 const Twine &Name = "") {
813 if (Constant *PC = dyn_cast(Ptr)) {
814 // Every index must be constant.
815 size_t i, e;
816 for (i = 0, e = IdxList.size(); i != e; ++i)
817 if (!isa(IdxList[i]))
818 break;
819 if (i == e)
820 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
821 }
822 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
823 }
824 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef IdxList,
825 const Twine &Name = "") {
826 if (Constant *PC = dyn_cast(Ptr)) {
827 // Every index must be constant.
828 size_t i, e;
829 for (i = 0, e = IdxList.size(); i != e; ++i)
830 if (!isa(IdxList[i]))
831 break;
832 if (i == e)
833 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
834 }
835 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
836 }
837 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
838 if (Constant *PC = dyn_cast(Ptr))
839 if (Constant *IC = dyn_cast(Idx))
840 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
841 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
842 }
843 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
844 if (Constant *PC = dyn_cast(Ptr))
845 if (Constant *IC = dyn_cast(Idx))
846 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
847 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
848 }
849 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
850 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
851
852 if (Constant *PC = dyn_cast(Ptr))
853 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
854
855 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
856 }
857 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
858 const Twine &Name = "") {
859 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
860
861 if (Constant *PC = dyn_cast(Ptr))
862 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
863
864 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
865 }
866 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
867 const Twine &Name = "") {
868 Value *Idxs[] = {
869 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
870 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
871 };
872
873 if (Constant *PC = dyn_cast(Ptr))
874 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
875
876 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
877 }
878 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
879 const Twine &Name = "") {
880 Value *Idxs[] = {
881 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
882 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
883 };
884
885 if (Constant *PC = dyn_cast(Ptr))
886 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
887
888 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
889 }
890 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
891 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
892
893 if (Constant *PC = dyn_cast(Ptr))
894 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
895
896 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
897 }
898 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
899 const Twine &Name = "") {
900 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
901
902 if (Constant *PC = dyn_cast(Ptr))
903 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
904
905 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
906 }
907 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
908 const Twine &Name = "") {
909 Value *Idxs[] = {
910 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
911 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
912 };
913
914 if (Constant *PC = dyn_cast(Ptr))
915 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
916
917 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
918 }
919 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
920 const Twine &Name = "") {
921 Value *Idxs[] = {
922 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
923 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
924 };
925
926 if (Constant *PC = dyn_cast(Ptr))
927 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
928
929 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
930 }
931 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
932 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
933 }
934
935 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
936 /// with "i8*" type instead of a pointer to array of i8.
937 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
938 Value *gv = CreateGlobalString(Str, Name);
939 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
940 Value *Args[] = { zero, zero };
941 return CreateInBoundsGEP(gv, Args, Name);
942 }
943
944 //===--------------------------------------------------------------------===//
945 // Instruction creation methods: Cast/Conversion Operators
946 //===--------------------------------------------------------------------===//
947
948 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
949 return CreateCast(Instruction::Trunc, V, DestTy, Name);
950 }
951 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
952 return CreateCast(Instruction::ZExt, V, DestTy, Name);
953 }
954 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
955 return CreateCast(Instruction::SExt, V, DestTy, Name);
956 }
957 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
958 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
959 }
960 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
961 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
962 }
963 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
964 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
965 }
966 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
967 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
968 }
969 Value *CreateFPTrunc(Value *V, Type *DestTy,
970 const Twine &Name = "") {
971 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
972 }
973 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
974 return CreateCast(Instruction::FPExt, V, DestTy, Name);
975 }
976 Value *CreatePtrToInt(Value *V, Type *DestTy,
977 const Twine &Name = "") {
978 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
979 }
980 Value *CreateIntToPtr(Value *V, Type *DestTy,
981 const Twine &Name = "") {
982 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
983 }
984 Value *CreateBitCast(Value *V, Type *DestTy,
985 const Twine &Name = "") {
986 return CreateCast(Instruction::BitCast, V, DestTy, Name);
987 }
988 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
989 const Twine &Name = "") {
990 if (V->getType() == DestTy)
991 return V;
992 if (Constant *VC = dyn_cast(V))
993 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
994 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
995 }
996 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
997 const Twine &Name = "") {
998 if (V->getType() == DestTy)
999 return V;
1000 if (Constant *VC = dyn_cast(V))
1001 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1002 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1003 }
1004 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1005 const Twine &Name = "") {
1006 if (V->getType() == DestTy)
1007 return V;
1008 if (Constant *VC = dyn_cast(V))
1009 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1010 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1011 }
1012 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1013 const Twine &Name = "") {
1014 if (V->getType() == DestTy)
1015 return V;
1016 if (Constant *VC = dyn_cast(V))
1017 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1018 return Insert(CastInst::Create(Op, V, DestTy), Name);
1019 }
1020 Value *CreatePointerCast(Value *V, Type *DestTy,
1021 const Twine &Name = "") {
1022 if (V->getType() == DestTy)
1023 return V;
1024 if (Constant *VC = dyn_cast(V))
1025 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1026 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1027 }
1028 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1029 const Twine &Name = "") {
1030 if (V->getType() == DestTy)
1031 return V;
1032 if (Constant *VC = dyn_cast(V))
1033 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1034 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1035 }
1036 private:
1037 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
1038 // error, instead of converting the string to bool for the isSigned parameter.
1039 Value *CreateIntCast(Value *, Type *, const char *); // DO NOT IMPLEMENT
1040 public:
1041 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1042 if (V->getType() == DestTy)
1043 return V;
1044 if (Constant *VC = dyn_cast(V))
1045 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1046 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1047 }
1048
1049 //===--------------------------------------------------------------------===//
1050 // Instruction creation methods: Compare Instructions
1051 //===--------------------------------------------------------------------===//
1052
1053 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1054 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1055 }
1056 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1057 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1058 }
1059 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1060 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1061 }
1062 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1063 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1064 }
1065 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1066 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1067 }
1068 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1069 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1070 }
1071 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1072 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1073 }
1074 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1075 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1076 }
1077 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1078 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1079 }
1080 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1081 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1082 }
1083
1084 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1085 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1086 }
1087 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1088 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1089 }
1090 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1091 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1092 }
1093 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1094 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1095 }
1096 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1097 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1098 }
1099 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1100 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1101 }
1102 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1103 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1104 }
1105 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1106 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1107 }
1108 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1109 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1110 }
1111 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1112 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1113 }
1114 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1115 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1116 }
1117 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1118 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1119 }
1120 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1121 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1122 }
1123 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1124 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1125 }
1126
1127 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1128 const Twine &Name = "") {
1129 if (Constant *LC = dyn_cast(LHS))
1130 if (Constant *RC = dyn_cast(RHS))
1131 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1132 return Insert(new ICmpInst(P, LHS, RHS), Name);
1133 }
1134 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1135 const Twine &Name = "") {
1136 if (Constant *LC = dyn_cast(LHS))
1137 if (Constant *RC = dyn_cast(RHS))
1138 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1139 return Insert(new FCmpInst(P, LHS, RHS), Name);
1140 }
1141
1142 //===--------------------------------------------------------------------===//
1143 // Instruction creation methods: Other Instructions
1144 //===--------------------------------------------------------------------===//
1145
1146 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1147 const Twine &Name = "") {
1148 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1149 }
1150
1151 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1152 return Insert(CallInst::Create(Callee), Name);
1153 }
1154 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1155 return Insert(CallInst::Create(Callee, Arg), Name);
1156 }
1157 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1158 const Twine &Name = "") {
1159 Value *Args[] = { Arg1, Arg2 };
1160 return Insert(CallInst::Create(Callee, Args), Name);
1161 }
1162 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1163 const Twine &Name = "") {
1164 Value *Args[] = { Arg1, Arg2, Arg3 };
1165 return Insert(CallInst::Create(Callee, Args), Name);
1166 }
1167 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1168 Value *Arg4, const Twine &Name = "") {
1169 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1170 return Insert(CallInst::Create(Callee, Args), Name);
1171 }
1172 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1173 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1174 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1175 return Insert(CallInst::Create(Callee, Args), Name);
1176 }
1177
1178 CallInst *CreateCall(Value *Callee, ArrayRef Args,
1179 const Twine &Name = "") {
1180 return Insert(CallInst::Create(Callee, Args), Name);
1181 }
1182
1183 Value *CreateSelect(Value *C, Value *True, Value *False,
1184 const Twine &Name = "") {
1185 if (Constant *CC = dyn_cast(C))
1186 if (Constant *TC = dyn_cast(True))
1187 if (Constant *FC = dyn_cast(False))
1188 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1189 return Insert(SelectInst::Create(C, True, False), Name);
1190 }
1191
1192 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1193 return Insert(new VAArgInst(List, Ty), Name);
1194 }
1195
1196 Value *CreateExtractElement(Value *Vec, Value *Idx,
1197 const Twine &Name = "") {
1198 if (Constant *VC = dyn_cast(Vec))
1199 if (Constant *IC = dyn_cast(Idx))
1200 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1201 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1202 }
1203
1204 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1205 const Twine &Name = "") {
1206 if (Constant *VC = dyn_cast(Vec))
1207 if (Constant *NC = dyn_cast(NewElt))
1208 if (Constant *IC = dyn_cast(Idx))
1209 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1210 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1211 }
1212
1213 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1214 const Twine &Name = "") {
1215 if (Constant *V1C = dyn_cast(V1))
1216 if (Constant *V2C = dyn_cast(V2))
1217 if (Constant *MC = dyn_cast(Mask))
1218 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1219 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1220 }
1221
1222 Value *CreateExtractValue(Value *Agg,
1223 ArrayRef Idxs,
1224 const Twine &Name = "") {
1225 if (Constant *AggC = dyn_cast(Agg))
1226 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1227 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1228 }
1229
1230 Value *CreateInsertValue(Value *Agg, Value *Val,
1231 ArrayRef Idxs,
1232 const Twine &Name = "") {
1233 if (Constant *AggC = dyn_cast(Agg))
1234 if (Constant *ValC = dyn_cast(Val))
1235 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1236 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1237 }
1238
1239 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1240 const Twine &Name = "") {
1241 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses, Name));
1242 }
1243
1244 //===--------------------------------------------------------------------===//
1245 // Utility creation methods
1246 //===--------------------------------------------------------------------===//
1247
1248 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1249 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1250 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1251 Name);
1252 }
1253
1254 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1255 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1256 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1257 Name);
1258 }
1259
1260 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1261 /// dividing out the size of the pointed-to objects. This is intended to
1262 /// implement C-style pointer subtraction. As such, the pointers must be
1263 /// appropriately aligned for their element types and pointing into the
1264 /// same object.
1265 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1266 assert(LHS->getType() == RHS->getType() &&
1267 "Pointer subtraction operand types must match!");
1268 PointerType *ArgType = cast(LHS->getType());
1269 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1270 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1271 Value *Difference = CreateSub(LHS_int, RHS_int);
1272 return CreateExactSDiv(Difference,
1273 ConstantExpr::getSizeOf(ArgType->getElementType()),
1274 Name);
1275 }
1276 };
1277
1278 }
1279
1280 #endif
1414 #ifndef TRANSFORMS_UTILS_BUILDLIBCALLS_H
1515 #define TRANSFORMS_UTILS_BUILDLIBCALLS_H
1616
17 #include "llvm/Support/IRBuilder.h"
17 #include "llvm/IRBuilder.h"
1818
1919 namespace llvm {
2020 class Value;
1414 #ifndef LLVM_TRANSFORMS_UTILS_LOCAL_H
1515 #define LLVM_TRANSFORMS_UTILS_LOCAL_H
1616
17 #include "llvm/IRBuilder.h"
18 #include "llvm/Operator.h"
1719 #include "llvm/Support/GetElementPtrTypeIterator.h"
18 #include "llvm/Support/IRBuilder.h"
1920 #include "llvm/Target/TargetData.h"
20 #include "llvm/Operator.h"
2121
2222 namespace llvm {
2323
2020
2121 /* Need these includes to support the LLVM 'cast' template for the C++ 'wrap'
2222 and 'unwrap' conversion functions. */
23 #include "llvm/IRBuilder.h"
2324 #include "llvm/Module.h"
2425 #include "llvm/PassRegistry.h"
25 #include "llvm/Support/IRBuilder.h"
2626
2727 extern "C" {
2828 #endif
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/CodeGen/IntrinsicLowering.h"
1314 #include "llvm/Constants.h"
1415 #include "llvm/DerivedTypes.h"
16 #include "llvm/IRBuilder.h"
1517 #include "llvm/Module.h"
1618 #include "llvm/Type.h"
17 #include "llvm/CodeGen/IntrinsicLowering.h"
19 #include "llvm/ADT/SmallVector.h"
1820 #include "llvm/Support/CallSite.h"
1921 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/IRBuilder.h"
2122 #include "llvm/Support/raw_ostream.h"
2223 #include "llvm/Target/TargetData.h"
23 #include "llvm/ADT/SmallVector.h"
2424 using namespace llvm;
2525
2626 template
2525 //===----------------------------------------------------------------------===//
2626
2727 #define DEBUG_TYPE "shadowstackgc"
28 #include "llvm/CodeGen/GCs.h"
28 #include "llvm/IRBuilder.h"
29 #include "llvm/IntrinsicInst.h"
30 #include "llvm/Module.h"
2931 #include "llvm/ADT/StringExtras.h"
3032 #include "llvm/CodeGen/GCStrategy.h"
31 #include "llvm/IntrinsicInst.h"
32 #include "llvm/Module.h"
33 #include "llvm/CodeGen/GCs.h"
3334 #include "llvm/Support/CallSite.h"
34 #include "llvm/Support/IRBuilder.h"
3535
3636 using namespace llvm;
3737
1212 //===----------------------------------------------------------------------===//
1313
1414 #define DEBUG_TYPE "sjljehprepare"
15 #include "llvm/Transforms/Scalar.h"
1615 #include "llvm/Constants.h"
1716 #include "llvm/DerivedTypes.h"
17 #include "llvm/IRBuilder.h"
1818 #include "llvm/Instructions.h"
1919 #include "llvm/Intrinsics.h"
2020 #include "llvm/LLVMContext.h"
2121 #include "llvm/Module.h"
2222 #include "llvm/Pass.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Target/TargetLowering.h"
26 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 #include "llvm/Transforms/Utils/Local.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/IRBuilder.h"
31 #include "llvm/Support/raw_ostream.h"
3223 #include "llvm/ADT/DenseMap.h"
3324 #include "llvm/ADT/SetVector.h"
3425 #include "llvm/ADT/SmallPtrSet.h"
3526 #include "llvm/ADT/SmallVector.h"
3627 #include "llvm/ADT/Statistic.h"
28 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLowering.h"
34 #include "llvm/Transforms/Scalar.h"
35 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
36 #include "llvm/Transforms/Utils/Local.h"
3737 #include
3838 using namespace llvm;
3939
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "NVPTXLowerAggrCopies.h"
14 #include "llvm/Constants.h"
1315 #include "llvm/Function.h"
14 #include "llvm/Constants.h"
16 #include "llvm/IRBuilder.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/IntrinsicInst.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/LLVMContext.h"
1521 #include "llvm/Module.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/Intrinsics.h"
18 #include "llvm/IntrinsicInst.h"
1922 #include "llvm/Support/InstIterator.h"
20 #include "llvm/Support/IRBuilder.h"
21 #include "NVPTXLowerAggrCopies.h"
2223 #include "llvm/Target/TargetData.h"
23 #include "llvm/LLVMContext.h"
2424
2525 using namespace llvm;
2626
4444
4545 #define DEBUG_TYPE "mergefunc"
4646 #include "llvm/Transforms/IPO.h"
47 #include "llvm/ADT/DenseSet.h"
48 #include "llvm/ADT/FoldingSet.h"
49 #include "llvm/ADT/SmallSet.h"
50 #include "llvm/ADT/Statistic.h"
51 #include "llvm/ADT/STLExtras.h"
5247 #include "llvm/Constants.h"
48 #include "llvm/IRBuilder.h"
5349 #include "llvm/InlineAsm.h"
5450 #include "llvm/Instructions.h"
5551 #include "llvm/LLVMContext.h"
5652 #include "llvm/Module.h"
5753 #include "llvm/Operator.h"
5854 #include "llvm/Pass.h"
55 #include "llvm/ADT/DenseSet.h"
56 #include "llvm/ADT/FoldingSet.h"
57 #include "llvm/ADT/STLExtras.h"
58 #include "llvm/ADT/SmallSet.h"
59 #include "llvm/ADT/Statistic.h"
5960 #include "llvm/Support/CallSite.h"
6061 #include "llvm/Support/Debug.h"
6162 #include "llvm/Support/ErrorHandling.h"
62 #include "llvm/Support/IRBuilder.h"
6363 #include "llvm/Support/ValueHandle.h"
6464 #include "llvm/Support/raw_ostream.h"
6565 #include "llvm/Target/TargetData.h"
1010 #define INSTCOMBINE_INSTCOMBINE_H
1111
1212 #include "InstCombineWorklist.h"
13 #include "llvm/IRBuilder.h"
1314 #include "llvm/IntrinsicInst.h"
1415 #include "llvm/Operator.h"
1516 #include "llvm/Pass.h"
1617 #include "llvm/Analysis/ValueTracking.h"
17 #include "llvm/Support/IRBuilder.h"
1818 #include "llvm/Support/InstVisitor.h"
1919 #include "llvm/Support/TargetFolder.h"
2020
1515 #define DEBUG_TYPE "asan"
1616
1717 #include "FunctionBlackList.h"
18 #include "llvm/Function.h"
19 #include "llvm/IRBuilder.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Module.h"
23 #include "llvm/Type.h"
1824 #include "llvm/ADT/ArrayRef.h"
1925 #include "llvm/ADT/OwningPtr.h"
2026 #include "llvm/ADT/SmallSet.h"
2228 #include "llvm/ADT/SmallVector.h"
2329 #include "llvm/ADT/StringExtras.h"
2430 #include "llvm/ADT/Triple.h"
25 #include "llvm/Function.h"
26 #include "llvm/IntrinsicInst.h"
27 #include "llvm/LLVMContext.h"
28 #include "llvm/Module.h"
2931 #include "llvm/Support/CommandLine.h"
3032 #include "llvm/Support/DataTypes.h"
3133 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/IRBuilder.h"
3334 #include "llvm/Support/raw_ostream.h"
3435 #include "llvm/Support/system_error.h"
3536 #include "llvm/Target/TargetData.h"
3738 #include "llvm/Transforms/Instrumentation.h"
3839 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
3940 #include "llvm/Transforms/Utils/ModuleUtils.h"
40 #include "llvm/Type.h"
4141
4242 #include
4343 #include
1818 #include "ProfilingUtils.h"
1919 #include "llvm/Transforms/Instrumentation.h"
2020 #include "llvm/DebugInfo.h"
21 #include "llvm/IRBuilder.h"
22 #include "llvm/Instructions.h"
2123 #include "llvm/Module.h"
2224 #include "llvm/Pass.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Transforms/Utils/ModuleUtils.h"
25 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/UniqueVector.h"
2631 #include "llvm/Support/Debug.h"
2732 #include "llvm/Support/DebugLoc.h"
2833 #include "llvm/Support/InstIterator.h"
29 #include "llvm/Support/IRBuilder.h"
3034 #include "llvm/Support/PathV2.h"
31 #include "llvm/ADT/DenseMap.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/STLExtras.h"
34 #include "llvm/ADT/StringExtras.h"
35 #include "llvm/ADT/StringMap.h"
36 #include "llvm/ADT/UniqueVector.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Transforms/Utils/ModuleUtils.h"
3737 #include
3838 #include
3939 using namespace llvm;
2121 #define DEBUG_TYPE "tsan"
2222
2323 #include "FunctionBlackList.h"
24 #include "llvm/Function.h"
25 #include "llvm/IRBuilder.h"
26 #include "llvm/Intrinsics.h"
27 #include "llvm/LLVMContext.h"
28 #include "llvm/Metadata.h"
29 #include "llvm/Module.h"
30 #include "llvm/Type.h"
2431 #include "llvm/ADT/SmallSet.h"
2532 #include "llvm/ADT/SmallString.h"
2633 #include "llvm/ADT/SmallVector.h"
2734 #include "llvm/ADT/Statistic.h"
2835 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/Intrinsics.h"
30 #include "llvm/Function.h"
31 #include "llvm/LLVMContext.h"
32 #include "llvm/Metadata.h"
33 #include "llvm/Module.h"
3436 #include "llvm/Support/CommandLine.h"
3537 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/IRBuilder.h"
3738 #include "llvm/Support/MathExtras.h"
3839 #include "llvm/Support/raw_ostream.h"
3940 #include "llvm/Target/TargetData.h"
4041 #include "llvm/Transforms/Instrumentation.h"
4142 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
4243 #include "llvm/Transforms/Utils/ModuleUtils.h"
43 #include "llvm/Type.h"
4444
4545 using namespace llvm;
4646
1313
1414 #define DEBUG_TYPE "bounds-checking"
1515 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/IRBuilder.h"
17 #include "llvm/Intrinsics.h"
18 #include "llvm/Pass.h"
1619 #include "llvm/ADT/Statistic.h"
1720 #include "llvm/Analysis/MemoryBuiltins.h"
1821 #include "llvm/Support/CommandLine.h"
1922 #include "llvm/Support/Debug.h"
2023 #include "llvm/Support/InstIterator.h"
21 #include "llvm/Support/IRBuilder.h"
24 #include "llvm/Support/TargetFolder.h"
2225 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/TargetFolder.h"
2426 #include "llvm/Target/TargetData.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/Pass.h"
2727 using namespace llvm;
2828
2929 static cl::opt SingleTrapBB("bounds-checking-single-trap",
1717 #include "llvm/Constants.h"
1818 #include "llvm/DerivedTypes.h"
1919 #include "llvm/Function.h"
20 #include "llvm/IRBuilder.h"
2021 #include "llvm/InlineAsm.h"
2122 #include "llvm/Instructions.h"
2223 #include "llvm/IntrinsicInst.h"
2324 #include "llvm/Pass.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/SmallSet.h"
27 #include "llvm/ADT/Statistic.h"
2428 #include "llvm/Analysis/Dominators.h"
2529 #include "llvm/Analysis/InstructionSimplify.h"
2630 #include "llvm/Analysis/ProfileInfo.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetLibraryInfo.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Transforms/Utils/AddrModeMatcher.h"
31 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
32 #include "llvm/Transforms/Utils/Local.h"
33 #include "llvm/Transforms/Utils/BuildLibCalls.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/SmallSet.h"
36 #include "llvm/ADT/Statistic.h"
3731 #include "llvm/Assembly/Writer.h"
3832 #include "llvm/Support/CallSite.h"
3933 #include "llvm/Support/CommandLine.h"
4034 #include "llvm/Support/Debug.h"
4135 #include "llvm/Support/GetElementPtrTypeIterator.h"
4236 #include "llvm/Support/PatternMatch.h"
37 #include "llvm/Support/ValueHandle.h"
4338 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Support/IRBuilder.h"
45 #include "llvm/Support/ValueHandle.h"
39 #include "llvm/Target/TargetData.h"
40 #include "llvm/Target/TargetLibraryInfo.h"
41 #include "llvm/Target/TargetLowering.h"
42 #include "llvm/Transforms/Utils/AddrModeMatcher.h"
43 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
44 #include "llvm/Transforms/Utils/BuildLibCalls.h"
45 #include "llvm/Transforms/Utils/Local.h"
4646 using namespace llvm;
4747 using namespace llvm::PatternMatch;
4848
1717 #define DEBUG_TYPE "gvn"
1818 #include "llvm/Transforms/Scalar.h"
1919 #include "llvm/GlobalVariable.h"
20 #include "llvm/IRBuilder.h"
2021 #include "llvm/IntrinsicInst.h"
22 #include "llvm/LLVMContext.h"
2123 #include "llvm/Metadata.h"
22 #include "llvm/LLVMContext.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DepthFirstIterator.h"
26 #include "llvm/ADT/Hashing.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/Statistic.h"
2329 #include "llvm/Analysis/AliasAnalysis.h"
2430 #include "llvm/Analysis/ConstantFolding.h"
2531 #include "llvm/Analysis/Dominators.h"
3036 #include "llvm/Analysis/PHITransAddr.h"
3137 #include "llvm/Analysis/ValueTracking.h"
3238 #include "llvm/Assembly/Writer.h"
39 #include "llvm/Support/Allocator.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/PatternMatch.h"
3343 #include "llvm/Target/TargetData.h"
3444 #include "llvm/Target/TargetLibraryInfo.h"
3545 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
3646 #include "llvm/Transforms/Utils/SSAUpdater.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/DepthFirstIterator.h"
39 #include "llvm/ADT/Hashing.h"
40 #include "llvm/ADT/SmallPtrSet.h"
41 #include "llvm/ADT/Statistic.h"
42 #include "llvm/Support/Allocator.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/IRBuilder.h"
46 #include "llvm/Support/PatternMatch.h"
4747 using namespace llvm;
4848 using namespace PatternMatch;
4949
4242
4343 #define DEBUG_TYPE "loop-idiom"
4444 #include "llvm/Transforms/Scalar.h"
45 #include "llvm/IRBuilder.h"
4546 #include "llvm/IntrinsicInst.h"
4647 #include "llvm/Module.h"
48 #include "llvm/ADT/Statistic.h"
4749 #include "llvm/Analysis/AliasAnalysis.h"
4850 #include "llvm/Analysis/LoopPass.h"
51 #include "llvm/Analysis/ScalarEvolutionExpander.h"
4952 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
50 #include "llvm/Analysis/ScalarEvolutionExpander.h"
5153 #include "llvm/Analysis/ValueTracking.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/raw_ostream.h"
5256 #include "llvm/Target/TargetData.h"
5357 #include "llvm/Target/TargetLibraryInfo.h"
5458 #include "llvm/Transforms/Utils/Local.h"
55 #include "llvm/Support/Debug.h"
56 #include "llvm/Support/IRBuilder.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/ADT/Statistic.h"
5959 using namespace llvm;
6060
6161 STATISTIC(NumMemSet, "Number of memset's formed from loop stores");
1414 #define DEBUG_TYPE "loweratomic"
1515 #include "llvm/Transforms/Scalar.h"
1616 #include "llvm/Function.h"
17 #include "llvm/IRBuilder.h"
1718 #include "llvm/IntrinsicInst.h"
1819 #include "llvm/Pass.h"
19 #include "llvm/Support/IRBuilder.h"
2020 using namespace llvm;
2121
2222 static bool LowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI) {
1414 #define DEBUG_TYPE "memcpyopt"
1515 #include "llvm/Transforms/Scalar.h"
1616 #include "llvm/GlobalVariable.h"
17 #include "llvm/IRBuilder.h"
18 #include "llvm/Instructions.h"
1719 #include "llvm/IntrinsicInst.h"
18 #include "llvm/Instructions.h"
1920 #include "llvm/ADT/SmallVector.h"
2021 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
2123 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
2324 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
2425 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/Transforms/Utils/Local.h"
2626 #include "llvm/Support/Debug.h"
2727 #include "llvm/Support/GetElementPtrTypeIterator.h"
28 #include "llvm/Support/IRBuilder.h"
2928 #include "llvm/Support/raw_ostream.h"
3029 #include "llvm/Target/TargetData.h"
3130 #include "llvm/Target/TargetLibraryInfo.h"
31 #include "llvm/Transforms/Utils/Local.h"
3232 #include
3333 using namespace llvm;
3434
2525 #include "llvm/Constants.h"
2626 #include "llvm/DerivedTypes.h"
2727 #include "llvm/Function.h"
28 #include "llvm/IRBuilder.h"
2829 #include "llvm/Instructions.h"
2930 #include "llvm/IntrinsicInst.h"
3031 #include "llvm/Pass.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/PostOrderIterator.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/ADT/SetVector.h"
36 #include "llvm/ADT/Statistic.h"
3137 #include "llvm/Assembly/Writer.h"
3238 #include "llvm/Support/CFG.h"
33 #include "llvm/Support/IRBuilder.h"
3439 #include "llvm/Support/Debug.h"
3540 #include "llvm/Support/ValueHandle.h"
3641 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/PostOrderIterator.h"
39 #include "llvm/ADT/SetVector.h"
40 #include "llvm/ADT/STLExtras.h"
41 #include "llvm/ADT/Statistic.h"
4242 #include
4343 using namespace llvm;
4444
2121 #define DEBUG_TYPE "scalarrepl"
2222 #include "llvm/Transforms/Scalar.h"
2323 #include "llvm/Constants.h"
24 #include "llvm/DIBuilder.h"
2425 #include "llvm/DebugInfo.h"
2526 #include "llvm/DerivedTypes.h"
26 #include "llvm/DIBuilder.h"
2727 #include "llvm/Function.h"
2828 #include "llvm/GlobalVariable.h"
29 #include "llvm/IRBuilder.h"
2930 #include "llvm/Instructions.h"
3031 #include "llvm/IntrinsicInst.h"
3132 #include "llvm/LLVMContext.h"
3233 #include "llvm/Module.h"
3334 #include "llvm/Operator.h"
3435 #include "llvm/Pass.h"
36 #include "llvm/ADT/SetVector.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/Statistic.h"
3539 #include "llvm/Analysis/Dominators.h"
3640 #include "llvm/Analysis/Loads.h"
3741 #include "llvm/Analysis/ValueTracking.h"
38 #include "llvm/Target/TargetData.h"
39 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
40 #include "llvm/Transforms/Utils/Local.h"
41 #include "llvm/Transforms/Utils/SSAUpdater.h"
4242 #include "llvm/Support/CallSite.h"
4343 #include "llvm/Support/Debug.h"
4444 #include "llvm/Support/ErrorHandling.h"
4545 #include "llvm/Support/GetElementPtrTypeIterator.h"
46 #include "llvm/Support/IRBuilder.h"
4746 #include "llvm/Support/MathExtras.h"
4847 #include "llvm/Support/raw_ostream.h"
49 #include "llvm/ADT/SetVector.h"
50 #include "llvm/ADT/SmallVector.h"
51 #include "llvm/ADT/Statistic.h"
48 #include "llvm/Target/TargetData.h"
49 #include "llvm/Transforms/Utils/Local.h"
50 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
51 #include "llvm/Transforms/Utils/SSAUpdater.h"
5252 using namespace llvm;
5353
5454 STATISTIC(NumReplaced, "Number of allocas broken up");
1717 #define DEBUG_TYPE "simplify-libcalls"
1818 #include "llvm/Transforms/Scalar.h"
1919 #include "llvm/Transforms/Utils/BuildLibCalls.h"
20 #include "llvm/IRBuilder.h"
2021 #include "llvm/Intrinsics.h"
2122 #include "llvm/LLVMContext.h"
2223 #include "llvm/Module.h"
2324 #include "llvm/Pass.h"
24 #include "llvm/Support/IRBuilder.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/ADT/StringMap.h"
2529 #include "llvm/Analysis/ValueTracking.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
2632 #include "llvm/Target/TargetData.h"
2733 #include "llvm/Target/TargetLibraryInfo.h"
28 #include "llvm/ADT/SmallPtrSet.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
3434 #include "llvm/Config/config.h" // FIXME: Shouldn't depend on host!
3535 using namespace llvm;
3636
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Transforms/Utils/BuildLibCalls.h"
14 #include "llvm/Type.h"
1514 #include "llvm/Constants.h"
1615 #include "llvm/Function.h"
16 #include "llvm/IRBuilder.h"
17 #include "llvm/Intrinsics.h"
1718 #include "llvm/Intrinsics.h"
1819 #include "llvm/LLVMContext.h"
20 #include "llvm/LLVMContext.h"
1921 #include "llvm/Module.h"
20 #include "llvm/Support/IRBuilder.h"
22 #include "llvm/Type.h"
23 #include "llvm/ADT/SmallString.h"
2124 #include "llvm/Target/TargetData.h"
2225 #include "llvm/Target/TargetLibraryInfo.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/ADT/SmallString.h"
2626
2727 using namespace llvm;
2828
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "llvm/Transforms/Utils/Cloning.h"
15 #include "llvm/Attributes.h"
1516 #include "llvm/Constants.h"
1617 #include "llvm/DebugInfo.h"
1718 #include "llvm/DerivedTypes.h"
18 #include "llvm/Module.h"
19 #include "llvm/IRBuilder.h"
1920 #include "llvm/Instructions.h"
2021 #include "llvm/IntrinsicInst.h"
2122 #include "llvm/Intrinsics.h"
22 #include "llvm/Attributes.h"
23 #include "llvm/Module.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringExtras.h"
2326 #include "llvm/Analysis/CallGraph.h"
2427 #include "llvm/Analysis/InstructionSimplify.h"
28 #include "llvm/Support/CallSite.h"
2529 #include "llvm/Target/TargetData.h"
2630 #include "llvm/Transforms/Utils/Local.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/Support/CallSite.h"
30 #include "llvm/Support/IRBuilder.h"
3131 using namespace llvm;
3232
3333 bool llvm::InlineFunction(CallInst *CI, InlineFunctionInfo &IFI,
1313
1414 #include "llvm/Transforms/Utils/Local.h"
1515 #include "llvm/Constants.h"
16 #include "llvm/DIBuilder.h"
1617 #include "llvm/DebugInfo.h"
1718 #include "llvm/DerivedTypes.h"
18 #include "llvm/DIBuilder.h"
1919 #include "llvm/GlobalAlias.h"
2020 #include "llvm/GlobalVariable.h"
21 #include "llvm/IRBuilder.h"
2122 #include "llvm/Instructions.h"
23 #include "llvm/IntrinsicInst.h"
2224 #include "llvm/Intrinsics.h"
23 #include "llvm/IntrinsicInst.h"
2425 #include "llvm/Metadata.h"
2526 #include "llvm/Operator.h"
2627 #include "llvm/ADT/DenseMap.h"
3031 #include "llvm/Analysis/MemoryBuiltins.h"
3132 #include "llvm/Analysis/ProfileInfo.h"
3233 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/Target/TargetData.h"
3434 #include "llvm/Support/CFG.h"
3535 #include "llvm/Support/Debug.h"
3636 #include "llvm/Support/GetElementPtrTypeIterator.h"
37 #include "llvm/Support/IRBuilder.h"
3837 #include "llvm/Support/MathExtras.h"
3938 #include "llvm/Support/ValueHandle.h"
4039 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Target/TargetData.h"
4141 using namespace llvm;
42