1 //===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the IRBuilder class, which is used as a convenient way
11 // to create LLVM instructions with a consistent and simplified interface.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_IRBUILDER_H
16 #define LLVM_SUPPORT_IRBUILDER_H
18 #include "llvm/Instructions.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ConstantFolder.h"
26 /// IRBuilderDefaultInserter - This provides the default implementation of the
27 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
28 /// created by IRBuilder and needs to be inserted. By default, this inserts the
29 /// instruction at the insertion point.
30 template <bool preserveNames = true>
31 class IRBuilderDefaultInserter {
33 void InsertHelper(Instruction *I, const Twine &Name,
34 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
35 if (BB) BB->getInstList().insert(InsertPt, I);
41 /// IRBuilderBase - Common base class shared among various IRBuilders.
43 DebugLoc CurDbgLocation;
46 BasicBlock::iterator InsertPt;
50 IRBuilderBase(LLVMContext &context)
52 ClearInsertionPoint();
55 //===--------------------------------------------------------------------===//
56 // Builder configuration methods
57 //===--------------------------------------------------------------------===//
59 /// ClearInsertionPoint - Clear the insertion point: created instructions will
60 /// not be inserted into a block.
61 void ClearInsertionPoint() {
65 BasicBlock *GetInsertBlock() const { return BB; }
66 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
67 LLVMContext &getContext() const { return Context; }
69 /// SetInsertPoint - This specifies that created instructions should be
70 /// appended to the end of the specified block.
71 void SetInsertPoint(BasicBlock *TheBB) {
76 /// SetInsertPoint - This specifies that created instructions should be
77 /// inserted before the specified instruction.
78 void SetInsertPoint(Instruction *I) {
83 /// SetInsertPoint - This specifies that created instructions should be
84 /// inserted at the specified point.
85 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
90 /// SetCurrentDebugLocation - Set location information used by debugging
92 void SetCurrentDebugLocation(const DebugLoc &L) {
96 /// getCurrentDebugLocation - Get location information used by debugging
98 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
100 /// SetInstDebugLocation - If this builder has a current debug location, set
101 /// it on the specified instruction.
102 void SetInstDebugLocation(Instruction *I) const {
103 if (!CurDbgLocation.isUnknown())
104 I->setDebugLoc(CurDbgLocation);
107 /// InsertPoint - A saved insertion point.
110 BasicBlock::iterator Point;
113 /// Creates a new insertion point which doesn't point to anything.
114 InsertPoint() : Block(0) {}
116 /// Creates a new insertion point at the given location.
117 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
118 : Block(InsertBlock), Point(InsertPoint) {}
120 /// isSet - Returns true if this insert point is set.
121 bool isSet() const { return (Block != 0); }
123 llvm::BasicBlock *getBlock() const { return Block; }
124 llvm::BasicBlock::iterator getPoint() const { return Point; }
127 /// saveIP - Returns the current insert point.
128 InsertPoint saveIP() const {
129 return InsertPoint(GetInsertBlock(), GetInsertPoint());
132 /// saveAndClearIP - Returns the current insert point, clearing it
134 InsertPoint saveAndClearIP() {
135 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
136 ClearInsertionPoint();
140 /// restoreIP - Sets the current insert point to a previously-saved
142 void restoreIP(InsertPoint IP) {
144 SetInsertPoint(IP.getBlock(), IP.getPoint());
146 ClearInsertionPoint();
149 //===--------------------------------------------------------------------===//
150 // Miscellaneous creation methods.
151 //===--------------------------------------------------------------------===//
153 /// CreateGlobalString - Make a new global variable with an initializer that
154 /// has array of i8 type filled in with the nul terminated string value
155 /// specified. If Name is specified, it is the name of the global variable
157 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
159 /// getInt1 - Get a constant value representing either true or false.
160 ConstantInt *getInt1(bool V) {
161 return ConstantInt::get(getInt1Ty(), V);
164 /// getTrue - Get the constant value for i1 true.
165 ConstantInt *getTrue() {
166 return ConstantInt::getTrue(Context);
169 /// getFalse - Get the constant value for i1 false.
170 ConstantInt *getFalse() {
171 return ConstantInt::getFalse(Context);
174 /// getInt8 - Get a constant 8-bit value.
175 ConstantInt *getInt8(uint8_t C) {
176 return ConstantInt::get(getInt8Ty(), C);
179 /// getInt16 - Get a constant 16-bit value.
180 ConstantInt *getInt16(uint16_t C) {
181 return ConstantInt::get(getInt16Ty(), C);
184 /// getInt32 - Get a constant 32-bit value.
185 ConstantInt *getInt32(uint32_t C) {
186 return ConstantInt::get(getInt32Ty(), C);
189 /// getInt64 - Get a constant 64-bit value.
190 ConstantInt *getInt64(uint64_t C) {
191 return ConstantInt::get(getInt64Ty(), C);
194 //===--------------------------------------------------------------------===//
195 // Type creation methods
196 //===--------------------------------------------------------------------===//
198 /// getInt1Ty - Fetch the type representing a single bit
199 const IntegerType *getInt1Ty() {
200 return Type::getInt1Ty(Context);
203 /// getInt8Ty - Fetch the type representing an 8-bit integer.
204 const IntegerType *getInt8Ty() {
205 return Type::getInt8Ty(Context);
208 /// getInt16Ty - Fetch the type representing a 16-bit integer.
209 const IntegerType *getInt16Ty() {
210 return Type::getInt16Ty(Context);
213 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
214 const IntegerType *getInt32Ty() {
215 return Type::getInt32Ty(Context);
218 /// getInt64Ty - Fetch the type representing a 64-bit integer.
219 const IntegerType *getInt64Ty() {
220 return Type::getInt64Ty(Context);
223 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
224 const Type *getFloatTy() {
225 return Type::getFloatTy(Context);
228 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
229 const Type *getDoubleTy() {
230 return Type::getDoubleTy(Context);
233 /// getVoidTy - Fetch the type representing void.
234 const Type *getVoidTy() {
235 return Type::getVoidTy(Context);
238 const PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
239 return Type::getInt8PtrTy(Context, AddrSpace);
242 /// getCurrentFunctionReturnType - Get the return type of the current function
243 /// that we're emitting into.
244 const Type *getCurrentFunctionReturnType() const;
246 /// CreateMemSet - Create and insert a memset to the specified pointer and the
247 /// specified value. If the pointer isn't an i8*, it will be converted. If a
248 /// TBAA tag is specified, it will be added to the instruction.
249 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
250 bool isVolatile = false, MDNode *TBAATag = 0) {
251 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
254 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
255 bool isVolatile = false, MDNode *TBAATag = 0);
257 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
258 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
259 /// specified, it will be added to the instruction.
260 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
261 bool isVolatile = false, MDNode *TBAATag = 0) {
262 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
265 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
266 bool isVolatile = false, MDNode *TBAATag = 0);
268 /// CreateMemMove - Create and insert a memmove between the specified
269 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
270 /// tag is specified, it will be added to the instruction.
271 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
272 bool isVolatile = false, MDNode *TBAATag = 0) {
273 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
276 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
277 bool isVolatile = false, MDNode *TBAATag = 0);
279 Value *getCastedInt8PtrValue(Value *Ptr);
282 /// IRBuilder - This provides a uniform API for creating instructions and
283 /// inserting them into a basic block: either at the end of a BasicBlock, or
284 /// at a specific iterator location in a block.
286 /// Note that the builder does not expose the full generality of LLVM
287 /// instructions. For access to extra instruction properties, use the mutators
288 /// (e.g. setVolatile) on the instructions after they have been created.
289 /// The first template argument handles whether or not to preserve names in the
290 /// final instruction output. This defaults to on. The second template argument
291 /// specifies a class to use for creating constants. This defaults to creating
292 /// minimally folded constants. The fourth template argument allows clients to
293 /// specify custom insertion hooks that are called on every newly created
295 template<bool preserveNames = true, typename T = ConstantFolder,
296 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
297 class IRBuilder : public IRBuilderBase, public Inserter {
300 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
301 : IRBuilderBase(C), Inserter(I), Folder(F) {
304 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
307 explicit IRBuilder(BasicBlock *TheBB, const T &F)
308 : IRBuilderBase(TheBB->getContext()), Folder(F) {
309 SetInsertPoint(TheBB);
312 explicit IRBuilder(BasicBlock *TheBB)
313 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
314 SetInsertPoint(TheBB);
317 explicit IRBuilder(Instruction *IP)
318 : IRBuilderBase(IP->getContext()), Folder(Context) {
322 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
323 : IRBuilderBase(TheBB->getContext()), Folder(F) {
324 SetInsertPoint(TheBB, IP);
327 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
328 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
329 SetInsertPoint(TheBB, IP);
332 /// getFolder - Get the constant folder being used.
333 const T &getFolder() { return Folder; }
335 /// isNamePreserving - Return true if this builder is configured to actually
336 /// add the requested names to IR created through it.
337 bool isNamePreserving() const { return preserveNames; }
339 /// Insert - Insert and return the specified instruction.
340 template<typename InstTy>
341 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
342 this->InsertHelper(I, Name, BB, InsertPt);
343 if (!getCurrentDebugLocation().isUnknown())
344 this->SetInstDebugLocation(I);
348 /// Insert - No-op overload to handle constants.
349 Constant *Insert(Constant *C, const Twine& = "") const {
353 //===--------------------------------------------------------------------===//
354 // Instruction creation methods: Terminators
355 //===--------------------------------------------------------------------===//
357 /// CreateRetVoid - Create a 'ret void' instruction.
358 ReturnInst *CreateRetVoid() {
359 return Insert(ReturnInst::Create(Context));
363 /// CreateRet - Create a 'ret <val>' instruction.
365 ReturnInst *CreateRet(Value *V) {
366 return Insert(ReturnInst::Create(Context, V));
369 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
370 /// with one Value from the retVals array each, that build a aggregate
371 /// return value one value at a time, and a ret instruction to return
372 /// the resulting aggregate value. This is a convenience function for
373 /// code that uses aggregate return values as a vehicle for having
374 /// multiple return values.
376 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
377 Value *V = UndefValue::get(getCurrentFunctionReturnType());
378 for (unsigned i = 0; i != N; ++i)
379 V = CreateInsertValue(V, retVals[i], i, "mrv");
380 return Insert(ReturnInst::Create(Context, V));
383 /// CreateBr - Create an unconditional 'br label X' instruction.
384 BranchInst *CreateBr(BasicBlock *Dest) {
385 return Insert(BranchInst::Create(Dest));
388 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
390 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
391 return Insert(BranchInst::Create(True, False, Cond));
394 /// CreateSwitch - Create a switch instruction with the specified value,
395 /// default dest, and with a hint for the number of cases that will be added
396 /// (for efficient allocation).
397 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
398 return Insert(SwitchInst::Create(V, Dest, NumCases));
401 /// CreateIndirectBr - Create an indirect branch instruction with the
402 /// specified address operand, with an optional hint for the number of
403 /// destinations that will be added (for efficient allocation).
404 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
405 return Insert(IndirectBrInst::Create(Addr, NumDests));
408 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
409 BasicBlock *UnwindDest, const Twine &Name = "") {
410 Value *Args[] = { 0 };
411 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
414 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
415 BasicBlock *UnwindDest, Value *Arg1,
416 const Twine &Name = "") {
417 Value *Args[] = { Arg1 };
418 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
421 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
422 BasicBlock *UnwindDest, Value *Arg1,
423 Value *Arg2, Value *Arg3,
424 const Twine &Name = "") {
425 Value *Args[] = { Arg1, Arg2, Arg3 };
426 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
429 /// CreateInvoke - Create an invoke instruction.
430 template<typename RandomAccessIterator>
431 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
432 BasicBlock *UnwindDest,
433 RandomAccessIterator ArgBegin,
434 RandomAccessIterator ArgEnd,
435 const Twine &Name = "") {
436 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
437 ArgBegin, ArgEnd), Name);
440 UnwindInst *CreateUnwind() {
441 return Insert(new UnwindInst(Context));
444 UnreachableInst *CreateUnreachable() {
445 return Insert(new UnreachableInst(Context));
448 //===--------------------------------------------------------------------===//
449 // Instruction creation methods: Binary Operators
450 //===--------------------------------------------------------------------===//
452 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
453 if (Constant *LC = dyn_cast<Constant>(LHS))
454 if (Constant *RC = dyn_cast<Constant>(RHS))
455 return Insert(Folder.CreateAdd(LC, RC), Name);
456 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
458 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
459 if (Constant *LC = dyn_cast<Constant>(LHS))
460 if (Constant *RC = dyn_cast<Constant>(RHS))
461 return Insert(Folder.CreateNSWAdd(LC, RC), Name);
462 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
464 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
465 if (Constant *LC = dyn_cast<Constant>(LHS))
466 if (Constant *RC = dyn_cast<Constant>(RHS))
467 return Insert(Folder.CreateNUWAdd(LC, RC), Name);
468 return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
470 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
471 if (Constant *LC = dyn_cast<Constant>(LHS))
472 if (Constant *RC = dyn_cast<Constant>(RHS))
473 return Insert(Folder.CreateFAdd(LC, RC), Name);
474 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
476 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
477 if (Constant *LC = dyn_cast<Constant>(LHS))
478 if (Constant *RC = dyn_cast<Constant>(RHS))
479 return Insert(Folder.CreateSub(LC, RC), Name);
480 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
482 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
483 if (Constant *LC = dyn_cast<Constant>(LHS))
484 if (Constant *RC = dyn_cast<Constant>(RHS))
485 return Insert(Folder.CreateNSWSub(LC, RC), Name);
486 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
488 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
489 if (Constant *LC = dyn_cast<Constant>(LHS))
490 if (Constant *RC = dyn_cast<Constant>(RHS))
491 return Insert(Folder.CreateNUWSub(LC, RC), Name);
492 return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
494 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
495 if (Constant *LC = dyn_cast<Constant>(LHS))
496 if (Constant *RC = dyn_cast<Constant>(RHS))
497 return Insert(Folder.CreateFSub(LC, RC), Name);
498 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
500 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
501 if (Constant *LC = dyn_cast<Constant>(LHS))
502 if (Constant *RC = dyn_cast<Constant>(RHS))
503 return Insert(Folder.CreateMul(LC, RC), Name);
504 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
506 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
507 if (Constant *LC = dyn_cast<Constant>(LHS))
508 if (Constant *RC = dyn_cast<Constant>(RHS))
509 return Insert(Folder.CreateNSWMul(LC, RC), Name);
510 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
512 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
513 if (Constant *LC = dyn_cast<Constant>(LHS))
514 if (Constant *RC = dyn_cast<Constant>(RHS))
515 return Insert(Folder.CreateNUWMul(LC, RC), Name);
516 return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
518 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
519 if (Constant *LC = dyn_cast<Constant>(LHS))
520 if (Constant *RC = dyn_cast<Constant>(RHS))
521 return Insert(Folder.CreateFMul(LC, RC), Name);
522 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
524 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
525 if (Constant *LC = dyn_cast<Constant>(LHS))
526 if (Constant *RC = dyn_cast<Constant>(RHS))
527 return Insert(Folder.CreateUDiv(LC, RC), Name);
528 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
530 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
531 if (Constant *LC = dyn_cast<Constant>(LHS))
532 if (Constant *RC = dyn_cast<Constant>(RHS))
533 return Insert(Folder.CreateExactUDiv(LC, RC), Name);
534 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
536 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
537 if (Constant *LC = dyn_cast<Constant>(LHS))
538 if (Constant *RC = dyn_cast<Constant>(RHS))
539 return Insert(Folder.CreateSDiv(LC, RC), Name);
540 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
542 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
543 if (Constant *LC = dyn_cast<Constant>(LHS))
544 if (Constant *RC = dyn_cast<Constant>(RHS))
545 return Insert(Folder.CreateExactSDiv(LC, RC), Name);
546 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
548 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
549 if (Constant *LC = dyn_cast<Constant>(LHS))
550 if (Constant *RC = dyn_cast<Constant>(RHS))
551 return Insert(Folder.CreateFDiv(LC, RC), Name);
552 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
554 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
555 if (Constant *LC = dyn_cast<Constant>(LHS))
556 if (Constant *RC = dyn_cast<Constant>(RHS))
557 return Insert(Folder.CreateURem(LC, RC), Name);
558 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
560 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
561 if (Constant *LC = dyn_cast<Constant>(LHS))
562 if (Constant *RC = dyn_cast<Constant>(RHS))
563 return Insert(Folder.CreateSRem(LC, RC), Name);
564 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
566 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
567 if (Constant *LC = dyn_cast<Constant>(LHS))
568 if (Constant *RC = dyn_cast<Constant>(RHS))
569 return Insert(Folder.CreateFRem(LC, RC), Name);
570 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
573 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
574 if (Constant *LC = dyn_cast<Constant>(LHS))
575 if (Constant *RC = dyn_cast<Constant>(RHS))
576 return Insert(Folder.CreateShl(LC, RC), Name);
577 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
579 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "") {
580 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
581 if (Constant *LC = dyn_cast<Constant>(LHS))
582 return Insert(Folder.CreateShl(LC, RHSC), Name);
583 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
585 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
586 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
587 if (Constant *LC = dyn_cast<Constant>(LHS))
588 return Insert(Folder.CreateShl(LC, RHSC), Name);
589 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
592 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
593 if (Constant *LC = dyn_cast<Constant>(LHS))
594 if (Constant *RC = dyn_cast<Constant>(RHS))
595 return Insert(Folder.CreateLShr(LC, RC), Name);
596 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
598 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
599 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
600 if (Constant *LC = dyn_cast<Constant>(LHS))
601 return Insert(Folder.CreateLShr(LC, RHSC), Name);
602 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
604 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
605 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
606 if (Constant *LC = dyn_cast<Constant>(LHS))
607 return Insert(Folder.CreateLShr(LC, RHSC), Name);
608 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
611 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
612 if (Constant *LC = dyn_cast<Constant>(LHS))
613 if (Constant *RC = dyn_cast<Constant>(RHS))
614 return Insert(Folder.CreateAShr(LC, RC), Name);
615 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
617 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
618 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
619 if (Constant *LC = dyn_cast<Constant>(LHS))
620 return Insert(Folder.CreateAShr(LC, RHSC), Name);
621 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
623 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
624 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
625 if (Constant *LC = dyn_cast<Constant>(LHS))
626 return Insert(Folder.CreateAShr(LC, RHSC), Name);
627 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
630 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
631 if (Constant *RC = dyn_cast<Constant>(RHS)) {
632 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
633 return LHS; // LHS & -1 -> LHS
634 if (Constant *LC = dyn_cast<Constant>(LHS))
635 return Insert(Folder.CreateAnd(LC, RC), Name);
637 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
639 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
640 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
641 if (Constant *LC = dyn_cast<Constant>(LHS))
642 return Insert(Folder.CreateAnd(LC, RHSC), Name);
643 return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
645 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
646 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
647 if (Constant *LC = dyn_cast<Constant>(LHS))
648 return Insert(Folder.CreateAnd(LC, RHSC), Name);
649 return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
652 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
653 if (Constant *RC = dyn_cast<Constant>(RHS)) {
654 if (RC->isNullValue())
655 return LHS; // LHS | 0 -> LHS
656 if (Constant *LC = dyn_cast<Constant>(LHS))
657 return Insert(Folder.CreateOr(LC, RC), Name);
659 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
661 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
662 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
663 if (Constant *LC = dyn_cast<Constant>(LHS))
664 return Insert(Folder.CreateOr(LC, RHSC), Name);
665 return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
667 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
668 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
669 if (Constant *LC = dyn_cast<Constant>(LHS))
670 return Insert(Folder.CreateOr(LC, RHSC), Name);
671 return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
674 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
675 if (Constant *LC = dyn_cast<Constant>(LHS))
676 if (Constant *RC = dyn_cast<Constant>(RHS))
677 return Insert(Folder.CreateXor(LC, RC), Name);
678 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
680 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
681 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
682 if (Constant *LC = dyn_cast<Constant>(LHS))
683 return Insert(Folder.CreateXor(LC, RHSC), Name);
684 return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
686 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
687 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
688 if (Constant *LC = dyn_cast<Constant>(LHS))
689 return Insert(Folder.CreateXor(LC, RHSC), Name);
690 return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
693 Value *CreateBinOp(Instruction::BinaryOps Opc,
694 Value *LHS, Value *RHS, const Twine &Name = "") {
695 if (Constant *LC = dyn_cast<Constant>(LHS))
696 if (Constant *RC = dyn_cast<Constant>(RHS))
697 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
698 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
701 Value *CreateNeg(Value *V, const Twine &Name = "") {
702 if (Constant *VC = dyn_cast<Constant>(V))
703 return Insert(Folder.CreateNeg(VC), Name);
704 return Insert(BinaryOperator::CreateNeg(V), Name);
706 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
707 if (Constant *VC = dyn_cast<Constant>(V))
708 return Insert(Folder.CreateNSWNeg(VC), Name);
709 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
711 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
712 if (Constant *VC = dyn_cast<Constant>(V))
713 return Insert(Folder.CreateNUWNeg(VC), Name);
714 return Insert(BinaryOperator::CreateNUWNeg(V), Name);
716 Value *CreateFNeg(Value *V, const Twine &Name = "") {
717 if (Constant *VC = dyn_cast<Constant>(V))
718 return Insert(Folder.CreateFNeg(VC), Name);
719 return Insert(BinaryOperator::CreateFNeg(V), Name);
721 Value *CreateNot(Value *V, const Twine &Name = "") {
722 if (Constant *VC = dyn_cast<Constant>(V))
723 return Insert(Folder.CreateNot(VC), Name);
724 return Insert(BinaryOperator::CreateNot(V), Name);
727 //===--------------------------------------------------------------------===//
728 // Instruction creation methods: Memory Instructions
729 //===--------------------------------------------------------------------===//
731 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
732 const Twine &Name = "") {
733 return Insert(new AllocaInst(Ty, ArraySize), Name);
735 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
736 // converting the string to 'bool' for the isVolatile parameter.
737 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
738 return Insert(new LoadInst(Ptr), Name);
740 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
741 return Insert(new LoadInst(Ptr), Name);
743 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
744 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
746 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
747 return Insert(new StoreInst(Val, Ptr, isVolatile));
749 template<typename RandomAccessIterator>
750 Value *CreateGEP(Value *Ptr,
751 RandomAccessIterator IdxBegin,
752 RandomAccessIterator IdxEnd,
753 const Twine &Name = "") {
754 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
755 // Every index must be constant.
756 RandomAccessIterator i;
757 for (i = IdxBegin; i < IdxEnd; ++i)
758 if (!isa<Constant>(*i))
761 return Insert(Folder.CreateGetElementPtr(PC, &IdxBegin[0],
765 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
767 template<typename RandomAccessIterator>
768 Value *CreateInBoundsGEP(Value *Ptr, RandomAccessIterator IdxBegin,
769 RandomAccessIterator IdxEnd,
770 const Twine &Name = "") {
771 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
772 // Every index must be constant.
773 RandomAccessIterator i;
774 for (i = IdxBegin; i < IdxEnd; ++i)
775 if (!isa<Constant>(*i))
778 return Insert(Folder.CreateInBoundsGetElementPtr(PC,
783 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
786 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
787 if (Constant *PC = dyn_cast<Constant>(Ptr))
788 if (Constant *IC = dyn_cast<Constant>(Idx))
789 return Insert(Folder.CreateGetElementPtr(PC, &IC, 1), Name);
790 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
792 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
793 if (Constant *PC = dyn_cast<Constant>(Ptr))
794 if (Constant *IC = dyn_cast<Constant>(Idx))
795 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &IC, 1), Name);
796 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
798 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
799 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
801 if (Constant *PC = dyn_cast<Constant>(Ptr))
802 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
804 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
806 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
807 const Twine &Name = "") {
808 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
810 if (Constant *PC = dyn_cast<Constant>(Ptr))
811 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
813 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
815 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
816 const Twine &Name = "") {
818 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
819 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
822 if (Constant *PC = dyn_cast<Constant>(Ptr))
823 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
825 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
827 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
828 const Twine &Name = "") {
830 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
831 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
834 if (Constant *PC = dyn_cast<Constant>(Ptr))
835 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
837 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
839 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
840 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
842 if (Constant *PC = dyn_cast<Constant>(Ptr))
843 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
845 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
847 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
848 const Twine &Name = "") {
849 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
851 if (Constant *PC = dyn_cast<Constant>(Ptr))
852 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
854 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
856 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
857 const Twine &Name = "") {
859 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
860 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
863 if (Constant *PC = dyn_cast<Constant>(Ptr))
864 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
866 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
868 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
869 const Twine &Name = "") {
871 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
872 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
875 if (Constant *PC = dyn_cast<Constant>(Ptr))
876 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
878 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
880 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
881 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
884 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
885 /// with "i8*" type instead of a pointer to array of i8.
886 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
887 Value *gv = CreateGlobalString(Str, Name);
888 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
889 Value *Args[] = { zero, zero };
890 return CreateInBoundsGEP(gv, Args, Args+2, Name);
893 //===--------------------------------------------------------------------===//
894 // Instruction creation methods: Cast/Conversion Operators
895 //===--------------------------------------------------------------------===//
897 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
898 return CreateCast(Instruction::Trunc, V, DestTy, Name);
900 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
901 return CreateCast(Instruction::ZExt, V, DestTy, Name);
903 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
904 return CreateCast(Instruction::SExt, V, DestTy, Name);
906 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
907 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
909 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
910 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
912 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
913 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
915 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
916 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
918 Value *CreateFPTrunc(Value *V, const Type *DestTy,
919 const Twine &Name = "") {
920 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
922 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
923 return CreateCast(Instruction::FPExt, V, DestTy, Name);
925 Value *CreatePtrToInt(Value *V, const Type *DestTy,
926 const Twine &Name = "") {
927 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
929 Value *CreateIntToPtr(Value *V, const Type *DestTy,
930 const Twine &Name = "") {
931 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
933 Value *CreateBitCast(Value *V, const Type *DestTy,
934 const Twine &Name = "") {
935 return CreateCast(Instruction::BitCast, V, DestTy, Name);
937 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
938 const Twine &Name = "") {
939 if (V->getType() == DestTy)
941 if (Constant *VC = dyn_cast<Constant>(V))
942 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
943 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
945 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
946 const Twine &Name = "") {
947 if (V->getType() == DestTy)
949 if (Constant *VC = dyn_cast<Constant>(V))
950 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
951 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
953 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
954 const Twine &Name = "") {
955 if (V->getType() == DestTy)
957 if (Constant *VC = dyn_cast<Constant>(V))
958 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
959 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
961 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
962 const Twine &Name = "") {
963 if (V->getType() == DestTy)
965 if (Constant *VC = dyn_cast<Constant>(V))
966 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
967 return Insert(CastInst::Create(Op, V, DestTy), Name);
969 Value *CreatePointerCast(Value *V, const Type *DestTy,
970 const Twine &Name = "") {
971 if (V->getType() == DestTy)
973 if (Constant *VC = dyn_cast<Constant>(V))
974 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
975 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
977 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
978 const Twine &Name = "") {
979 if (V->getType() == DestTy)
981 if (Constant *VC = dyn_cast<Constant>(V))
982 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
983 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
986 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
987 // error, instead of converting the string to bool for the isSigned parameter.
988 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
990 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
991 if (V->getType() == DestTy)
993 if (Constant *VC = dyn_cast<Constant>(V))
994 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
995 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
998 //===--------------------------------------------------------------------===//
999 // Instruction creation methods: Compare Instructions
1000 //===--------------------------------------------------------------------===//
1002 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1003 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1005 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1006 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1008 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1009 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1011 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1012 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1014 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1015 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1017 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1018 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1020 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1021 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1023 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1024 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1026 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1027 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1029 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1030 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1033 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1034 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1036 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1037 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1039 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1040 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1042 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1043 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1045 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1046 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1048 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1049 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1051 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1052 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1054 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1055 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1057 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1058 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1060 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1061 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1063 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1064 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1066 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1067 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1069 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1070 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1072 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1073 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1076 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1077 const Twine &Name = "") {
1078 if (Constant *LC = dyn_cast<Constant>(LHS))
1079 if (Constant *RC = dyn_cast<Constant>(RHS))
1080 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1081 return Insert(new ICmpInst(P, LHS, RHS), Name);
1083 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1084 const Twine &Name = "") {
1085 if (Constant *LC = dyn_cast<Constant>(LHS))
1086 if (Constant *RC = dyn_cast<Constant>(RHS))
1087 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1088 return Insert(new FCmpInst(P, LHS, RHS), Name);
1091 //===--------------------------------------------------------------------===//
1092 // Instruction creation methods: Other Instructions
1093 //===--------------------------------------------------------------------===//
1095 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
1096 return Insert(PHINode::Create(Ty), Name);
1099 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1100 return Insert(CallInst::Create(Callee), Name);
1102 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1103 return Insert(CallInst::Create(Callee, Arg), Name);
1105 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1106 const Twine &Name = "") {
1107 Value *Args[] = { Arg1, Arg2 };
1108 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
1110 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1111 const Twine &Name = "") {
1112 Value *Args[] = { Arg1, Arg2, Arg3 };
1113 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
1115 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1116 Value *Arg4, const Twine &Name = "") {
1117 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1118 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
1120 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1121 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1122 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1123 return Insert(CallInst::Create(Callee, Args, Args+5), Name);
1126 template<typename RandomAccessIterator>
1127 CallInst *CreateCall(Value *Callee, RandomAccessIterator ArgBegin,
1128 RandomAccessIterator ArgEnd, const Twine &Name = "") {
1129 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
1132 Value *CreateSelect(Value *C, Value *True, Value *False,
1133 const Twine &Name = "") {
1134 if (Constant *CC = dyn_cast<Constant>(C))
1135 if (Constant *TC = dyn_cast<Constant>(True))
1136 if (Constant *FC = dyn_cast<Constant>(False))
1137 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1138 return Insert(SelectInst::Create(C, True, False), Name);
1141 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
1142 return Insert(new VAArgInst(List, Ty), Name);
1145 Value *CreateExtractElement(Value *Vec, Value *Idx,
1146 const Twine &Name = "") {
1147 if (Constant *VC = dyn_cast<Constant>(Vec))
1148 if (Constant *IC = dyn_cast<Constant>(Idx))
1149 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1150 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1153 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1154 const Twine &Name = "") {
1155 if (Constant *VC = dyn_cast<Constant>(Vec))
1156 if (Constant *NC = dyn_cast<Constant>(NewElt))
1157 if (Constant *IC = dyn_cast<Constant>(Idx))
1158 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1159 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1162 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1163 const Twine &Name = "") {
1164 if (Constant *V1C = dyn_cast<Constant>(V1))
1165 if (Constant *V2C = dyn_cast<Constant>(V2))
1166 if (Constant *MC = dyn_cast<Constant>(Mask))
1167 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1168 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1171 Value *CreateExtractValue(Value *Agg, unsigned Idx,
1172 const Twine &Name = "") {
1173 if (Constant *AggC = dyn_cast<Constant>(Agg))
1174 return Insert(Folder.CreateExtractValue(AggC, &Idx, 1), Name);
1175 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1178 template<typename RandomAccessIterator>
1179 Value *CreateExtractValue(Value *Agg,
1180 RandomAccessIterator IdxBegin,
1181 RandomAccessIterator IdxEnd,
1182 const Twine &Name = "") {
1183 if (Constant *AggC = dyn_cast<Constant>(Agg))
1184 return Insert(Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd-IdxBegin),
1186 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1189 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1190 const Twine &Name = "") {
1191 if (Constant *AggC = dyn_cast<Constant>(Agg))
1192 if (Constant *ValC = dyn_cast<Constant>(Val))
1193 return Insert(Folder.CreateInsertValue(AggC, ValC, &Idx, 1), Name);
1194 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1197 template<typename RandomAccessIterator>
1198 Value *CreateInsertValue(Value *Agg, Value *Val,
1199 RandomAccessIterator IdxBegin,
1200 RandomAccessIterator IdxEnd,
1201 const Twine &Name = "") {
1202 if (Constant *AggC = dyn_cast<Constant>(Agg))
1203 if (Constant *ValC = dyn_cast<Constant>(Val))
1204 return Insert(Folder.CreateInsertValue(AggC, ValC, IdxBegin,
1207 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1210 //===--------------------------------------------------------------------===//
1211 // Utility creation methods
1212 //===--------------------------------------------------------------------===//
1214 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1215 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1216 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1220 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1221 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1222 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1226 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1227 /// dividing out the size of the pointed-to objects. This is intended to
1228 /// implement C-style pointer subtraction. As such, the pointers must be
1229 /// appropriately aligned for their element types and pointing into the
1231 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1232 assert(LHS->getType() == RHS->getType() &&
1233 "Pointer subtraction operand types must match!");
1234 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1235 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1236 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1237 Value *Difference = CreateSub(LHS_int, RHS_int);
1238 return CreateExactSDiv(Difference,
1239 ConstantExpr::getSizeOf(ArgType->getElementType()),