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 at the specified point.
78 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
83 /// SetCurrentDebugLocation - Set location information used by debugging
85 void SetCurrentDebugLocation(const DebugLoc &L) {
89 /// getCurrentDebugLocation - Get location information used by debugging
91 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
93 /// SetInstDebugLocation - If this builder has a current debug location, set
94 /// it on the specified instruction.
95 void SetInstDebugLocation(Instruction *I) const {
96 if (!CurDbgLocation.isUnknown())
97 I->setDebugLoc(CurDbgLocation);
100 /// InsertPoint - A saved insertion point.
103 BasicBlock::iterator Point;
106 /// Creates a new insertion point which doesn't point to anything.
107 InsertPoint() : Block(0) {}
109 /// Creates a new insertion point at the given location.
110 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
111 : Block(InsertBlock), Point(InsertPoint) {}
113 /// isSet - Returns true if this insert point is set.
114 bool isSet() const { return (Block != 0); }
116 llvm::BasicBlock *getBlock() const { return Block; }
117 llvm::BasicBlock::iterator getPoint() const { return Point; }
120 /// saveIP - Returns the current insert point.
121 InsertPoint saveIP() const {
122 return InsertPoint(GetInsertBlock(), GetInsertPoint());
125 /// saveAndClearIP - Returns the current insert point, clearing it
127 InsertPoint saveAndClearIP() {
128 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
129 ClearInsertionPoint();
133 /// restoreIP - Sets the current insert point to a previously-saved
135 void restoreIP(InsertPoint IP) {
137 SetInsertPoint(IP.getBlock(), IP.getPoint());
139 ClearInsertionPoint();
142 //===--------------------------------------------------------------------===//
143 // Miscellaneous creation methods.
144 //===--------------------------------------------------------------------===//
146 /// CreateGlobalString - Make a new global variable with an initializer that
147 /// has array of i8 type filled in with the nul terminated string value
148 /// specified. If Name is specified, it is the name of the global variable
150 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
152 /// getInt1 - Get a constant value representing either true or false.
153 ConstantInt *getInt1(bool V) {
154 return ConstantInt::get(getInt1Ty(), V);
157 /// getTrue - Get the constant value for i1 true.
158 ConstantInt *getTrue() {
159 return ConstantInt::getTrue(Context);
162 /// getFalse - Get the constant value for i1 false.
163 ConstantInt *getFalse() {
164 return ConstantInt::getFalse(Context);
167 /// getInt8 - Get a constant 8-bit value.
168 ConstantInt *getInt8(uint8_t C) {
169 return ConstantInt::get(getInt8Ty(), C);
172 /// getInt16 - Get a constant 16-bit value.
173 ConstantInt *getInt16(uint16_t C) {
174 return ConstantInt::get(getInt16Ty(), C);
177 /// getInt32 - Get a constant 32-bit value.
178 ConstantInt *getInt32(uint32_t C) {
179 return ConstantInt::get(getInt32Ty(), C);
182 /// getInt64 - Get a constant 64-bit value.
183 ConstantInt *getInt64(uint64_t C) {
184 return ConstantInt::get(getInt64Ty(), C);
187 //===--------------------------------------------------------------------===//
188 // Type creation methods
189 //===--------------------------------------------------------------------===//
191 /// getInt1Ty - Fetch the type representing a single bit
192 const IntegerType *getInt1Ty() {
193 return Type::getInt1Ty(Context);
196 /// getInt8Ty - Fetch the type representing an 8-bit integer.
197 const IntegerType *getInt8Ty() {
198 return Type::getInt8Ty(Context);
201 /// getInt16Ty - Fetch the type representing a 16-bit integer.
202 const IntegerType *getInt16Ty() {
203 return Type::getInt16Ty(Context);
206 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
207 const IntegerType *getInt32Ty() {
208 return Type::getInt32Ty(Context);
211 /// getInt64Ty - Fetch the type representing a 64-bit integer.
212 const IntegerType *getInt64Ty() {
213 return Type::getInt64Ty(Context);
216 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
217 const Type *getFloatTy() {
218 return Type::getFloatTy(Context);
221 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
222 const Type *getDoubleTy() {
223 return Type::getDoubleTy(Context);
226 /// getVoidTy - Fetch the type representing void.
227 const Type *getVoidTy() {
228 return Type::getVoidTy(Context);
231 const PointerType *getInt8PtrTy() {
232 return Type::getInt8PtrTy(Context);
235 /// getCurrentFunctionReturnType - Get the return type of the current function
236 /// that we're emitting into.
237 const Type *getCurrentFunctionReturnType() const;
240 /// IRBuilder - This provides a uniform API for creating instructions and
241 /// inserting them into a basic block: either at the end of a BasicBlock, or
242 /// at a specific iterator location in a block.
244 /// Note that the builder does not expose the full generality of LLVM
245 /// instructions. For access to extra instruction properties, use the mutators
246 /// (e.g. setVolatile) on the instructions after they have been created.
247 /// The first template argument handles whether or not to preserve names in the
248 /// final instruction output. This defaults to on. The second template argument
249 /// specifies a class to use for creating constants. This defaults to creating
250 /// minimally folded constants. The fourth template argument allows clients to
251 /// specify custom insertion hooks that are called on every newly created
253 template<bool preserveNames = true, typename T = ConstantFolder,
254 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
255 class IRBuilder : public IRBuilderBase, public Inserter {
258 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
259 : IRBuilderBase(C), Inserter(I), Folder(F) {
262 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
265 explicit IRBuilder(BasicBlock *TheBB, const T &F)
266 : IRBuilderBase(TheBB->getContext()), Folder(F) {
267 SetInsertPoint(TheBB);
270 explicit IRBuilder(BasicBlock *TheBB)
271 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
272 SetInsertPoint(TheBB);
275 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
276 : IRBuilderBase(TheBB->getContext()), Folder(F) {
277 SetInsertPoint(TheBB, IP);
280 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
281 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
282 SetInsertPoint(TheBB, IP);
285 /// getFolder - Get the constant folder being used.
286 const T &getFolder() { return Folder; }
288 /// isNamePreserving - Return true if this builder is configured to actually
289 /// add the requested names to IR created through it.
290 bool isNamePreserving() const { return preserveNames; }
292 /// Insert - Insert and return the specified instruction.
293 template<typename InstTy>
294 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
295 this->InsertHelper(I, Name, BB, InsertPt);
296 if (!getCurrentDebugLocation().isUnknown())
297 this->SetInstDebugLocation(I);
301 /// Insert - No-op overload to handle constants.
302 Constant *Insert(Constant *C, const Twine& = "") const {
306 //===--------------------------------------------------------------------===//
307 // Instruction creation methods: Terminators
308 //===--------------------------------------------------------------------===//
310 /// CreateRetVoid - Create a 'ret void' instruction.
311 ReturnInst *CreateRetVoid() {
312 return Insert(ReturnInst::Create(Context));
316 /// CreateRet - Create a 'ret <val>' instruction.
318 ReturnInst *CreateRet(Value *V) {
319 return Insert(ReturnInst::Create(Context, V));
322 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
323 /// with one Value from the retVals array each, that build a aggregate
324 /// return value one value at a time, and a ret instruction to return
325 /// the resulting aggregate value. This is a convenience function for
326 /// code that uses aggregate return values as a vehicle for having
327 /// multiple return values.
329 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
330 Value *V = UndefValue::get(getCurrentFunctionReturnType());
331 for (unsigned i = 0; i != N; ++i)
332 V = CreateInsertValue(V, retVals[i], i, "mrv");
333 return Insert(ReturnInst::Create(Context, V));
336 /// CreateBr - Create an unconditional 'br label X' instruction.
337 BranchInst *CreateBr(BasicBlock *Dest) {
338 return Insert(BranchInst::Create(Dest));
341 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
343 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
344 return Insert(BranchInst::Create(True, False, Cond));
347 /// CreateSwitch - Create a switch instruction with the specified value,
348 /// default dest, and with a hint for the number of cases that will be added
349 /// (for efficient allocation).
350 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
351 return Insert(SwitchInst::Create(V, Dest, NumCases));
354 /// CreateIndirectBr - Create an indirect branch instruction with the
355 /// specified address operand, with an optional hint for the number of
356 /// destinations that will be added (for efficient allocation).
357 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
358 return Insert(IndirectBrInst::Create(Addr, NumDests));
361 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
362 BasicBlock *UnwindDest, const Twine &Name = "") {
363 Value *Args[] = { 0 };
364 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
367 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
368 BasicBlock *UnwindDest, Value *Arg1,
369 const Twine &Name = "") {
370 Value *Args[] = { Arg1 };
371 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
374 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
375 BasicBlock *UnwindDest, Value *Arg1,
376 Value *Arg2, Value *Arg3,
377 const Twine &Name = "") {
378 Value *Args[] = { Arg1, Arg2, Arg3 };
379 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
382 /// CreateInvoke - Create an invoke instruction.
383 template<typename RandomAccessIterator>
384 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
385 BasicBlock *UnwindDest,
386 RandomAccessIterator ArgBegin,
387 RandomAccessIterator ArgEnd,
388 const Twine &Name = "") {
389 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
390 ArgBegin, ArgEnd), Name);
393 UnwindInst *CreateUnwind() {
394 return Insert(new UnwindInst(Context));
397 UnreachableInst *CreateUnreachable() {
398 return Insert(new UnreachableInst(Context));
401 //===--------------------------------------------------------------------===//
402 // Instruction creation methods: Binary Operators
403 //===--------------------------------------------------------------------===//
405 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
406 if (Constant *LC = dyn_cast<Constant>(LHS))
407 if (Constant *RC = dyn_cast<Constant>(RHS))
408 return Insert(Folder.CreateAdd(LC, RC), Name);
409 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
411 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
412 if (Constant *LC = dyn_cast<Constant>(LHS))
413 if (Constant *RC = dyn_cast<Constant>(RHS))
414 return Insert(Folder.CreateNSWAdd(LC, RC), Name);
415 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
417 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
418 if (Constant *LC = dyn_cast<Constant>(LHS))
419 if (Constant *RC = dyn_cast<Constant>(RHS))
420 return Insert(Folder.CreateNUWAdd(LC, RC), Name);
421 return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
423 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
424 if (Constant *LC = dyn_cast<Constant>(LHS))
425 if (Constant *RC = dyn_cast<Constant>(RHS))
426 return Insert(Folder.CreateFAdd(LC, RC), Name);
427 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
429 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
430 if (Constant *LC = dyn_cast<Constant>(LHS))
431 if (Constant *RC = dyn_cast<Constant>(RHS))
432 return Insert(Folder.CreateSub(LC, RC), Name);
433 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
435 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
436 if (Constant *LC = dyn_cast<Constant>(LHS))
437 if (Constant *RC = dyn_cast<Constant>(RHS))
438 return Insert(Folder.CreateNSWSub(LC, RC), Name);
439 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
441 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
442 if (Constant *LC = dyn_cast<Constant>(LHS))
443 if (Constant *RC = dyn_cast<Constant>(RHS))
444 return Insert(Folder.CreateNUWSub(LC, RC), Name);
445 return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
447 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
448 if (Constant *LC = dyn_cast<Constant>(LHS))
449 if (Constant *RC = dyn_cast<Constant>(RHS))
450 return Insert(Folder.CreateFSub(LC, RC), Name);
451 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
453 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
454 if (Constant *LC = dyn_cast<Constant>(LHS))
455 if (Constant *RC = dyn_cast<Constant>(RHS))
456 return Insert(Folder.CreateMul(LC, RC), Name);
457 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
459 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
460 if (Constant *LC = dyn_cast<Constant>(LHS))
461 if (Constant *RC = dyn_cast<Constant>(RHS))
462 return Insert(Folder.CreateNSWMul(LC, RC), Name);
463 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
465 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
466 if (Constant *LC = dyn_cast<Constant>(LHS))
467 if (Constant *RC = dyn_cast<Constant>(RHS))
468 return Insert(Folder.CreateNUWMul(LC, RC), Name);
469 return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
471 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
472 if (Constant *LC = dyn_cast<Constant>(LHS))
473 if (Constant *RC = dyn_cast<Constant>(RHS))
474 return Insert(Folder.CreateFMul(LC, RC), Name);
475 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
477 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
478 if (Constant *LC = dyn_cast<Constant>(LHS))
479 if (Constant *RC = dyn_cast<Constant>(RHS))
480 return Insert(Folder.CreateUDiv(LC, RC), Name);
481 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
483 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
484 if (Constant *LC = dyn_cast<Constant>(LHS))
485 if (Constant *RC = dyn_cast<Constant>(RHS))
486 return Insert(Folder.CreateSDiv(LC, RC), Name);
487 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
489 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
490 if (Constant *LC = dyn_cast<Constant>(LHS))
491 if (Constant *RC = dyn_cast<Constant>(RHS))
492 return Insert(Folder.CreateExactSDiv(LC, RC), Name);
493 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
495 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
496 if (Constant *LC = dyn_cast<Constant>(LHS))
497 if (Constant *RC = dyn_cast<Constant>(RHS))
498 return Insert(Folder.CreateFDiv(LC, RC), Name);
499 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
501 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
502 if (Constant *LC = dyn_cast<Constant>(LHS))
503 if (Constant *RC = dyn_cast<Constant>(RHS))
504 return Insert(Folder.CreateURem(LC, RC), Name);
505 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
507 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
508 if (Constant *LC = dyn_cast<Constant>(LHS))
509 if (Constant *RC = dyn_cast<Constant>(RHS))
510 return Insert(Folder.CreateSRem(LC, RC), Name);
511 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
513 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
514 if (Constant *LC = dyn_cast<Constant>(LHS))
515 if (Constant *RC = dyn_cast<Constant>(RHS))
516 return Insert(Folder.CreateFRem(LC, RC), Name);
517 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
520 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
521 if (Constant *LC = dyn_cast<Constant>(LHS))
522 if (Constant *RC = dyn_cast<Constant>(RHS))
523 return Insert(Folder.CreateShl(LC, RC), Name);
524 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
526 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "") {
527 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
528 if (Constant *LC = dyn_cast<Constant>(LHS))
529 return Insert(Folder.CreateShl(LC, RHSC), Name);
530 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
532 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
533 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
534 if (Constant *LC = dyn_cast<Constant>(LHS))
535 return Insert(Folder.CreateShl(LC, RHSC), Name);
536 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
539 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
540 if (Constant *LC = dyn_cast<Constant>(LHS))
541 if (Constant *RC = dyn_cast<Constant>(RHS))
542 return Insert(Folder.CreateLShr(LC, RC), Name);
543 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
545 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
546 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
547 if (Constant *LC = dyn_cast<Constant>(LHS))
548 return Insert(Folder.CreateLShr(LC, RHSC), Name);
549 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
551 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
552 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
553 if (Constant *LC = dyn_cast<Constant>(LHS))
554 return Insert(Folder.CreateLShr(LC, RHSC), Name);
555 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
558 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
559 if (Constant *LC = dyn_cast<Constant>(LHS))
560 if (Constant *RC = dyn_cast<Constant>(RHS))
561 return Insert(Folder.CreateAShr(LC, RC), Name);
562 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
564 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
565 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
566 if (Constant *LC = dyn_cast<Constant>(LHS))
567 return Insert(Folder.CreateAShr(LC, RHSC), Name);
568 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
570 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
571 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
572 if (Constant *LC = dyn_cast<Constant>(LHS))
573 return Insert(Folder.CreateAShr(LC, RHSC), Name);
574 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
577 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
578 if (Constant *RC = dyn_cast<Constant>(RHS)) {
579 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
580 return LHS; // LHS & -1 -> LHS
581 if (Constant *LC = dyn_cast<Constant>(LHS))
582 return Insert(Folder.CreateAnd(LC, RC), Name);
584 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
586 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
587 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
588 if (Constant *LC = dyn_cast<Constant>(LHS))
589 return Insert(Folder.CreateAnd(LC, RHSC), Name);
590 return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
592 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
593 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
594 if (Constant *LC = dyn_cast<Constant>(LHS))
595 return Insert(Folder.CreateAnd(LC, RHSC), Name);
596 return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
599 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
600 if (Constant *RC = dyn_cast<Constant>(RHS)) {
601 if (RC->isNullValue())
602 return LHS; // LHS | 0 -> LHS
603 if (Constant *LC = dyn_cast<Constant>(LHS))
604 return Insert(Folder.CreateOr(LC, RC), Name);
606 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
608 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
609 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
610 if (Constant *LC = dyn_cast<Constant>(LHS))
611 return Insert(Folder.CreateOr(LC, RHSC), Name);
612 return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
614 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
615 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
616 if (Constant *LC = dyn_cast<Constant>(LHS))
617 return Insert(Folder.CreateOr(LC, RHSC), Name);
618 return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
621 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
622 if (Constant *LC = dyn_cast<Constant>(LHS))
623 if (Constant *RC = dyn_cast<Constant>(RHS))
624 return Insert(Folder.CreateXor(LC, RC), Name);
625 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
627 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
628 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
629 if (Constant *LC = dyn_cast<Constant>(LHS))
630 return Insert(Folder.CreateXor(LC, RHSC), Name);
631 return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
633 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
634 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
635 if (Constant *LC = dyn_cast<Constant>(LHS))
636 return Insert(Folder.CreateXor(LC, RHSC), Name);
637 return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
640 Value *CreateBinOp(Instruction::BinaryOps Opc,
641 Value *LHS, Value *RHS, const Twine &Name = "") {
642 if (Constant *LC = dyn_cast<Constant>(LHS))
643 if (Constant *RC = dyn_cast<Constant>(RHS))
644 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
645 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
648 Value *CreateNeg(Value *V, const Twine &Name = "") {
649 if (Constant *VC = dyn_cast<Constant>(V))
650 return Insert(Folder.CreateNeg(VC), Name);
651 return Insert(BinaryOperator::CreateNeg(V), Name);
653 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
654 if (Constant *VC = dyn_cast<Constant>(V))
655 return Insert(Folder.CreateNSWNeg(VC), Name);
656 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
658 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
659 if (Constant *VC = dyn_cast<Constant>(V))
660 return Insert(Folder.CreateNUWNeg(VC), Name);
661 return Insert(BinaryOperator::CreateNUWNeg(V), Name);
663 Value *CreateFNeg(Value *V, const Twine &Name = "") {
664 if (Constant *VC = dyn_cast<Constant>(V))
665 return Insert(Folder.CreateFNeg(VC), Name);
666 return Insert(BinaryOperator::CreateFNeg(V), Name);
668 Value *CreateNot(Value *V, const Twine &Name = "") {
669 if (Constant *VC = dyn_cast<Constant>(V))
670 return Insert(Folder.CreateNot(VC), Name);
671 return Insert(BinaryOperator::CreateNot(V), Name);
674 //===--------------------------------------------------------------------===//
675 // Instruction creation methods: Memory Instructions
676 //===--------------------------------------------------------------------===//
678 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
679 const Twine &Name = "") {
680 return Insert(new AllocaInst(Ty, ArraySize), Name);
682 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
683 // converting the string to 'bool' for the isVolatile parameter.
684 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
685 return Insert(new LoadInst(Ptr), Name);
687 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
688 return Insert(new LoadInst(Ptr), Name);
690 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
691 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
693 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
694 return Insert(new StoreInst(Val, Ptr, isVolatile));
696 template<typename RandomAccessIterator>
697 Value *CreateGEP(Value *Ptr,
698 RandomAccessIterator IdxBegin,
699 RandomAccessIterator IdxEnd,
700 const Twine &Name = "") {
701 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
702 // Every index must be constant.
703 RandomAccessIterator i;
704 for (i = IdxBegin; i < IdxEnd; ++i)
705 if (!isa<Constant>(*i))
708 return Insert(Folder.CreateGetElementPtr(PC, &IdxBegin[0],
712 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
714 template<typename RandomAccessIterator>
715 Value *CreateInBoundsGEP(Value *Ptr, RandomAccessIterator IdxBegin,
716 RandomAccessIterator IdxEnd,
717 const Twine &Name = "") {
718 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
719 // Every index must be constant.
720 RandomAccessIterator i;
721 for (i = IdxBegin; i < IdxEnd; ++i)
722 if (!isa<Constant>(*i))
725 return Insert(Folder.CreateInBoundsGetElementPtr(PC,
730 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
733 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
734 if (Constant *PC = dyn_cast<Constant>(Ptr))
735 if (Constant *IC = dyn_cast<Constant>(Idx))
736 return Insert(Folder.CreateGetElementPtr(PC, &IC, 1), Name);
737 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
739 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
740 if (Constant *PC = dyn_cast<Constant>(Ptr))
741 if (Constant *IC = dyn_cast<Constant>(Idx))
742 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &IC, 1), Name);
743 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
745 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
746 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
748 if (Constant *PC = dyn_cast<Constant>(Ptr))
749 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
751 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
753 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
754 const Twine &Name = "") {
755 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
757 if (Constant *PC = dyn_cast<Constant>(Ptr))
758 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
760 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
762 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
763 const Twine &Name = "") {
765 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
766 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
769 if (Constant *PC = dyn_cast<Constant>(Ptr))
770 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
772 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
774 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
775 const Twine &Name = "") {
777 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
778 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
781 if (Constant *PC = dyn_cast<Constant>(Ptr))
782 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
784 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
786 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
787 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
789 if (Constant *PC = dyn_cast<Constant>(Ptr))
790 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
792 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
794 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
795 const Twine &Name = "") {
796 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
798 if (Constant *PC = dyn_cast<Constant>(Ptr))
799 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
801 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
803 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
804 const Twine &Name = "") {
806 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
807 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
810 if (Constant *PC = dyn_cast<Constant>(Ptr))
811 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
813 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
815 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
816 const Twine &Name = "") {
818 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
819 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
822 if (Constant *PC = dyn_cast<Constant>(Ptr))
823 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
825 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
827 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
828 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
831 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
832 /// with "i8*" type instead of a pointer to array of i8.
833 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
834 Value *gv = CreateGlobalString(Str, Name);
835 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
836 Value *Args[] = { zero, zero };
837 return CreateInBoundsGEP(gv, Args, Args+2, Name);
840 //===--------------------------------------------------------------------===//
841 // Instruction creation methods: Cast/Conversion Operators
842 //===--------------------------------------------------------------------===//
844 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
845 return CreateCast(Instruction::Trunc, V, DestTy, Name);
847 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
848 return CreateCast(Instruction::ZExt, V, DestTy, Name);
850 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
851 return CreateCast(Instruction::SExt, V, DestTy, Name);
853 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
854 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
856 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
857 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
859 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
860 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
862 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
863 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
865 Value *CreateFPTrunc(Value *V, const Type *DestTy,
866 const Twine &Name = "") {
867 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
869 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
870 return CreateCast(Instruction::FPExt, V, DestTy, Name);
872 Value *CreatePtrToInt(Value *V, const Type *DestTy,
873 const Twine &Name = "") {
874 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
876 Value *CreateIntToPtr(Value *V, const Type *DestTy,
877 const Twine &Name = "") {
878 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
880 Value *CreateBitCast(Value *V, const Type *DestTy,
881 const Twine &Name = "") {
882 return CreateCast(Instruction::BitCast, V, DestTy, Name);
884 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
885 const Twine &Name = "") {
886 if (V->getType() == DestTy)
888 if (Constant *VC = dyn_cast<Constant>(V))
889 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
890 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
892 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
893 const Twine &Name = "") {
894 if (V->getType() == DestTy)
896 if (Constant *VC = dyn_cast<Constant>(V))
897 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
898 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
900 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
901 const Twine &Name = "") {
902 if (V->getType() == DestTy)
904 if (Constant *VC = dyn_cast<Constant>(V))
905 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
906 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
908 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
909 const Twine &Name = "") {
910 if (V->getType() == DestTy)
912 if (Constant *VC = dyn_cast<Constant>(V))
913 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
914 return Insert(CastInst::Create(Op, V, DestTy), Name);
916 Value *CreatePointerCast(Value *V, const Type *DestTy,
917 const Twine &Name = "") {
918 if (V->getType() == DestTy)
920 if (Constant *VC = dyn_cast<Constant>(V))
921 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
922 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
924 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
925 const Twine &Name = "") {
926 if (V->getType() == DestTy)
928 if (Constant *VC = dyn_cast<Constant>(V))
929 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
930 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
933 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
934 // error, instead of converting the string to bool for the isSigned parameter.
935 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
937 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
938 if (V->getType() == DestTy)
940 if (Constant *VC = dyn_cast<Constant>(V))
941 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
942 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
945 //===--------------------------------------------------------------------===//
946 // Instruction creation methods: Compare Instructions
947 //===--------------------------------------------------------------------===//
949 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
950 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
952 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
953 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
955 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
956 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
958 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
959 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
961 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
962 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
964 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
965 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
967 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
968 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
970 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
971 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
973 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
974 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
976 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
977 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
980 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
981 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
983 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
984 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
986 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
987 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
989 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
990 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
992 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
993 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
995 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
996 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
998 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
999 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1001 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1002 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1004 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1005 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1007 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1008 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1010 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1011 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1013 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1014 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1016 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1017 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1019 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1020 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1023 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1024 const Twine &Name = "") {
1025 if (Constant *LC = dyn_cast<Constant>(LHS))
1026 if (Constant *RC = dyn_cast<Constant>(RHS))
1027 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1028 return Insert(new ICmpInst(P, LHS, RHS), Name);
1030 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1031 const Twine &Name = "") {
1032 if (Constant *LC = dyn_cast<Constant>(LHS))
1033 if (Constant *RC = dyn_cast<Constant>(RHS))
1034 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1035 return Insert(new FCmpInst(P, LHS, RHS), Name);
1038 //===--------------------------------------------------------------------===//
1039 // Instruction creation methods: Other Instructions
1040 //===--------------------------------------------------------------------===//
1042 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
1043 return Insert(PHINode::Create(Ty), Name);
1046 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1047 return Insert(CallInst::Create(Callee), Name);
1049 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1050 return Insert(CallInst::Create(Callee, Arg), Name);
1052 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1053 const Twine &Name = "") {
1054 Value *Args[] = { Arg1, Arg2 };
1055 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
1057 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1058 const Twine &Name = "") {
1059 Value *Args[] = { Arg1, Arg2, Arg3 };
1060 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
1062 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1063 Value *Arg4, const Twine &Name = "") {
1064 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1065 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
1067 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1068 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1069 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1070 return Insert(CallInst::Create(Callee, Args, Args+5), Name);
1073 template<typename RandomAccessIterator>
1074 CallInst *CreateCall(Value *Callee, RandomAccessIterator ArgBegin,
1075 RandomAccessIterator ArgEnd, const Twine &Name = "") {
1076 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
1079 Value *CreateSelect(Value *C, Value *True, Value *False,
1080 const Twine &Name = "") {
1081 if (Constant *CC = dyn_cast<Constant>(C))
1082 if (Constant *TC = dyn_cast<Constant>(True))
1083 if (Constant *FC = dyn_cast<Constant>(False))
1084 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1085 return Insert(SelectInst::Create(C, True, False), Name);
1088 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
1089 return Insert(new VAArgInst(List, Ty), Name);
1092 Value *CreateExtractElement(Value *Vec, Value *Idx,
1093 const Twine &Name = "") {
1094 if (Constant *VC = dyn_cast<Constant>(Vec))
1095 if (Constant *IC = dyn_cast<Constant>(Idx))
1096 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1097 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1100 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1101 const Twine &Name = "") {
1102 if (Constant *VC = dyn_cast<Constant>(Vec))
1103 if (Constant *NC = dyn_cast<Constant>(NewElt))
1104 if (Constant *IC = dyn_cast<Constant>(Idx))
1105 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1106 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1109 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1110 const Twine &Name = "") {
1111 if (Constant *V1C = dyn_cast<Constant>(V1))
1112 if (Constant *V2C = dyn_cast<Constant>(V2))
1113 if (Constant *MC = dyn_cast<Constant>(Mask))
1114 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1115 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1118 Value *CreateExtractValue(Value *Agg, unsigned Idx,
1119 const Twine &Name = "") {
1120 if (Constant *AggC = dyn_cast<Constant>(Agg))
1121 return Insert(Folder.CreateExtractValue(AggC, &Idx, 1), Name);
1122 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1125 template<typename RandomAccessIterator>
1126 Value *CreateExtractValue(Value *Agg,
1127 RandomAccessIterator IdxBegin,
1128 RandomAccessIterator IdxEnd,
1129 const Twine &Name = "") {
1130 if (Constant *AggC = dyn_cast<Constant>(Agg))
1131 return Insert(Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd-IdxBegin),
1133 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1136 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1137 const Twine &Name = "") {
1138 if (Constant *AggC = dyn_cast<Constant>(Agg))
1139 if (Constant *ValC = dyn_cast<Constant>(Val))
1140 return Insert(Folder.CreateInsertValue(AggC, ValC, &Idx, 1), Name);
1141 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1144 template<typename RandomAccessIterator>
1145 Value *CreateInsertValue(Value *Agg, Value *Val,
1146 RandomAccessIterator IdxBegin,
1147 RandomAccessIterator IdxEnd,
1148 const Twine &Name = "") {
1149 if (Constant *AggC = dyn_cast<Constant>(Agg))
1150 if (Constant *ValC = dyn_cast<Constant>(Val))
1151 return Insert(Folder.CreateInsertValue(AggC, ValC, IdxBegin,
1154 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1157 //===--------------------------------------------------------------------===//
1158 // Utility creation methods
1159 //===--------------------------------------------------------------------===//
1161 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1162 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1163 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1167 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1168 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1169 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1173 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1174 /// dividing out the size of the pointed-to objects. This is intended to
1175 /// implement C-style pointer subtraction. As such, the pointers must be
1176 /// appropriately aligned for their element types and pointing into the
1178 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1179 assert(LHS->getType() == RHS->getType() &&
1180 "Pointer subtraction operand types must match!");
1181 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1182 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1183 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1184 Value *Difference = CreateSub(LHS_int, RHS_int);
1185 return CreateExactSDiv(Difference,
1186 ConstantExpr::getSizeOf(ArgType->getElementType()),