1 //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- 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_IR_IRBUILDER_H
16 #define LLVM_IR_IRBUILDER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/ConstantFolder.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "llvm/Support/CBindingWrapping.h"
35 /// \brief This provides the default implementation of the IRBuilder
36 /// 'InsertHelper' method that is called whenever an instruction is created by
37 /// IRBuilder and needs to be inserted.
39 /// By default, this inserts the instruction at the insertion point.
40 template <bool preserveNames = true>
41 class IRBuilderDefaultInserter {
43 void InsertHelper(Instruction *I, const Twine &Name,
44 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
45 if (BB) BB->getInstList().insert(InsertPt, I);
51 /// \brief Common base class shared among various IRBuilders.
53 DebugLoc CurDbgLocation;
56 BasicBlock::iterator InsertPt;
59 MDNode *DefaultFPMathTag;
63 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr)
64 : Context(context), DefaultFPMathTag(FPMathTag), FMF() {
65 ClearInsertionPoint();
68 //===--------------------------------------------------------------------===//
69 // Builder configuration methods
70 //===--------------------------------------------------------------------===//
72 /// \brief Clear the insertion point: created instructions will not be
73 /// inserted into a block.
74 void ClearInsertionPoint() {
79 BasicBlock *GetInsertBlock() const { return BB; }
80 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
81 LLVMContext &getContext() const { return Context; }
83 /// \brief This specifies that created instructions should be appended to the
84 /// end of the specified block.
85 void SetInsertPoint(BasicBlock *TheBB) {
90 /// \brief This specifies that created instructions should be inserted before
91 /// the specified instruction.
92 void SetInsertPoint(Instruction *I) {
95 assert(I != BB->end() && "Can't read debug loc from end()");
96 SetCurrentDebugLocation(I->getDebugLoc());
99 /// \brief This specifies that created instructions should be inserted at the
101 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
104 if (IP != TheBB->end())
105 SetCurrentDebugLocation(IP->getDebugLoc());
108 /// \brief Set location information used by debugging information.
109 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
111 /// \brief Get location information used by debugging information.
112 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
114 /// \brief If this builder has a current debug location, set it on the
115 /// specified instruction.
116 void SetInstDebugLocation(Instruction *I) const {
118 I->setDebugLoc(CurDbgLocation);
121 /// \brief Get the return type of the current function that we're emitting
123 Type *getCurrentFunctionReturnType() const;
125 /// InsertPoint - A saved insertion point.
128 BasicBlock::iterator Point;
131 /// \brief Creates a new insertion point which doesn't point to anything.
132 InsertPoint() : Block(nullptr) {}
134 /// \brief Creates a new insertion point at the given location.
135 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
136 : Block(InsertBlock), Point(InsertPoint) {}
138 /// \brief Returns true if this insert point is set.
139 bool isSet() const { return (Block != nullptr); }
141 llvm::BasicBlock *getBlock() const { return Block; }
142 llvm::BasicBlock::iterator getPoint() const { return Point; }
145 /// \brief Returns the current insert point.
146 InsertPoint saveIP() const {
147 return InsertPoint(GetInsertBlock(), GetInsertPoint());
150 /// \brief Returns the current insert point, clearing it in the process.
151 InsertPoint saveAndClearIP() {
152 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
153 ClearInsertionPoint();
157 /// \brief Sets the current insert point to a previously-saved location.
158 void restoreIP(InsertPoint IP) {
160 SetInsertPoint(IP.getBlock(), IP.getPoint());
162 ClearInsertionPoint();
165 /// \brief Get the floating point math metadata being used.
166 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
168 /// \brief Get the flags to be applied to created floating point ops
169 FastMathFlags getFastMathFlags() const { return FMF; }
171 /// \brief Clear the fast-math flags.
172 void clearFastMathFlags() { FMF.clear(); }
174 /// \brief Set the floating point math metadata to be used.
175 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
177 /// \brief Set the fast-math flags to be used with generated fp-math operators
178 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
180 //===--------------------------------------------------------------------===//
182 //===--------------------------------------------------------------------===//
184 // \brief RAII object that stores the current insertion point and restores it
185 // when the object is destroyed. This includes the debug location.
186 class InsertPointGuard {
187 IRBuilderBase &Builder;
188 AssertingVH<BasicBlock> Block;
189 BasicBlock::iterator Point;
192 InsertPointGuard(const InsertPointGuard &) = delete;
193 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
196 InsertPointGuard(IRBuilderBase &B)
197 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
198 DbgLoc(B.getCurrentDebugLocation()) {}
200 ~InsertPointGuard() {
201 Builder.restoreIP(InsertPoint(Block, Point));
202 Builder.SetCurrentDebugLocation(DbgLoc);
206 // \brief RAII object that stores the current fast math settings and restores
207 // them when the object is destroyed.
208 class FastMathFlagGuard {
209 IRBuilderBase &Builder;
213 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
214 FastMathFlagGuard &operator=(
215 const FastMathFlagGuard &) = delete;
218 FastMathFlagGuard(IRBuilderBase &B)
219 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
221 ~FastMathFlagGuard() {
223 Builder.DefaultFPMathTag = FPMathTag;
227 //===--------------------------------------------------------------------===//
228 // Miscellaneous creation methods.
229 //===--------------------------------------------------------------------===//
231 /// \brief Make a new global variable with initializer type i8*
233 /// Make a new global variable with an initializer that has array of i8 type
234 /// filled in with the null terminated string value specified. The new global
235 /// variable will be marked mergable with any others of the same contents. If
236 /// Name is specified, it is the name of the global variable created.
237 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
238 unsigned AddressSpace = 0);
240 /// \brief Get a constant value representing either true or false.
241 ConstantInt *getInt1(bool V) {
242 return ConstantInt::get(getInt1Ty(), V);
245 /// \brief Get the constant value for i1 true.
246 ConstantInt *getTrue() {
247 return ConstantInt::getTrue(Context);
250 /// \brief Get the constant value for i1 false.
251 ConstantInt *getFalse() {
252 return ConstantInt::getFalse(Context);
255 /// \brief Get a constant 8-bit value.
256 ConstantInt *getInt8(uint8_t C) {
257 return ConstantInt::get(getInt8Ty(), C);
260 /// \brief Get a constant 16-bit value.
261 ConstantInt *getInt16(uint16_t C) {
262 return ConstantInt::get(getInt16Ty(), C);
265 /// \brief Get a constant 32-bit value.
266 ConstantInt *getInt32(uint32_t C) {
267 return ConstantInt::get(getInt32Ty(), C);
270 /// \brief Get a constant 64-bit value.
271 ConstantInt *getInt64(uint64_t C) {
272 return ConstantInt::get(getInt64Ty(), C);
275 /// \brief Get a constant N-bit value, zero extended or truncated from
277 ConstantInt *getIntN(unsigned N, uint64_t C) {
278 return ConstantInt::get(getIntNTy(N), C);
281 /// \brief Get a constant integer value.
282 ConstantInt *getInt(const APInt &AI) {
283 return ConstantInt::get(Context, AI);
286 //===--------------------------------------------------------------------===//
287 // Type creation methods
288 //===--------------------------------------------------------------------===//
290 /// \brief Fetch the type representing a single bit
291 IntegerType *getInt1Ty() {
292 return Type::getInt1Ty(Context);
295 /// \brief Fetch the type representing an 8-bit integer.
296 IntegerType *getInt8Ty() {
297 return Type::getInt8Ty(Context);
300 /// \brief Fetch the type representing a 16-bit integer.
301 IntegerType *getInt16Ty() {
302 return Type::getInt16Ty(Context);
305 /// \brief Fetch the type representing a 32-bit integer.
306 IntegerType *getInt32Ty() {
307 return Type::getInt32Ty(Context);
310 /// \brief Fetch the type representing a 64-bit integer.
311 IntegerType *getInt64Ty() {
312 return Type::getInt64Ty(Context);
315 /// \brief Fetch the type representing a 128-bit integer.
316 IntegerType *getInt128Ty() {
317 return Type::getInt128Ty(Context);
320 /// \brief Fetch the type representing an N-bit integer.
321 IntegerType *getIntNTy(unsigned N) {
322 return Type::getIntNTy(Context, N);
325 /// \brief Fetch the type representing a 16-bit floating point value.
327 return Type::getHalfTy(Context);
330 /// \brief Fetch the type representing a 32-bit floating point value.
332 return Type::getFloatTy(Context);
335 /// \brief Fetch the type representing a 64-bit floating point value.
336 Type *getDoubleTy() {
337 return Type::getDoubleTy(Context);
340 /// \brief Fetch the type representing void.
342 return Type::getVoidTy(Context);
345 /// \brief Fetch the type representing a pointer to an 8-bit integer value.
346 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
347 return Type::getInt8PtrTy(Context, AddrSpace);
350 /// \brief Fetch the type representing a pointer to an integer value.
351 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
352 return DL.getIntPtrType(Context, AddrSpace);
355 //===--------------------------------------------------------------------===//
356 // Intrinsic creation methods
357 //===--------------------------------------------------------------------===//
359 /// \brief Create and insert a memset to the specified pointer and the
362 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
363 /// specified, it will be added to the instruction. Likewise with alias.scope
364 /// and noalias tags.
365 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
366 bool isVolatile = false, MDNode *TBAATag = nullptr,
367 MDNode *ScopeTag = nullptr,
368 MDNode *NoAliasTag = nullptr) {
369 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
370 TBAATag, ScopeTag, NoAliasTag);
373 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
374 bool isVolatile = false, MDNode *TBAATag = nullptr,
375 MDNode *ScopeTag = nullptr,
376 MDNode *NoAliasTag = nullptr);
378 /// \brief Create and insert a memcpy between the specified pointers.
380 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
381 /// specified, it will be added to the instruction. Likewise with alias.scope
382 /// and noalias tags.
383 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
384 bool isVolatile = false, MDNode *TBAATag = nullptr,
385 MDNode *TBAAStructTag = nullptr,
386 MDNode *ScopeTag = nullptr,
387 MDNode *NoAliasTag = nullptr) {
388 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
389 TBAAStructTag, ScopeTag, NoAliasTag);
392 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
393 bool isVolatile = false, MDNode *TBAATag = nullptr,
394 MDNode *TBAAStructTag = nullptr,
395 MDNode *ScopeTag = nullptr,
396 MDNode *NoAliasTag = nullptr);
398 /// \brief Create and insert a memmove between the specified
401 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
402 /// specified, it will be added to the instruction. Likewise with alias.scope
403 /// and noalias tags.
404 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
405 bool isVolatile = false, MDNode *TBAATag = nullptr,
406 MDNode *ScopeTag = nullptr,
407 MDNode *NoAliasTag = nullptr) {
408 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile,
409 TBAATag, ScopeTag, NoAliasTag);
412 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
413 bool isVolatile = false, MDNode *TBAATag = nullptr,
414 MDNode *ScopeTag = nullptr,
415 MDNode *NoAliasTag = nullptr);
417 /// \brief Create a lifetime.start intrinsic.
419 /// If the pointer isn't i8* it will be converted.
420 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
422 /// \brief Create a lifetime.end intrinsic.
424 /// If the pointer isn't i8* it will be converted.
425 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
427 /// \brief Create a call to Masked Load intrinsic
428 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
429 Value *PassThru = 0, const Twine &Name = "");
431 /// \brief Create a call to Masked Store intrinsic
432 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
435 /// \brief Create an assume intrinsic call that allows the optimizer to
436 /// assume that the provided condition will be true.
437 CallInst *CreateAssumption(Value *Cond);
439 /// \brief Create a call to the experimental.gc.statepoint intrinsic to
440 /// start a new statepoint sequence.
441 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
443 ArrayRef<Value *> CallArgs,
444 ArrayRef<Value *> DeoptArgs,
445 ArrayRef<Value *> GCArgs,
446 const Twine &Name = "");
448 // \brief Conveninence function for the common case when CallArgs are filled
449 // in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
450 // .get()'ed to get the Value pointer.
451 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
452 Value *ActualCallee, ArrayRef<Use> CallArgs,
453 ArrayRef<Value *> DeoptArgs,
454 ArrayRef<Value *> GCArgs,
455 const Twine &Name = "");
457 /// brief Create an invoke to the experimental.gc.statepoint intrinsic to
458 /// start a new statepoint sequence.
460 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
461 Value *ActualInvokee, BasicBlock *NormalDest,
462 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
463 ArrayRef<Value *> DeoptArgs,
464 ArrayRef<Value *> GCArgs, const Twine &Name = "");
466 // Conveninence function for the common case when CallArgs are filled in using
467 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
470 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
471 Value *ActualInvokee, BasicBlock *NormalDest,
472 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
473 ArrayRef<Value *> DeoptArgs,
474 ArrayRef<Value *> GCArgs, const Twine &Name = "");
476 /// \brief Create a call to the experimental.gc.result intrinsic to extract
477 /// the result from a call wrapped in a statepoint.
478 CallInst *CreateGCResult(Instruction *Statepoint,
480 const Twine &Name = "");
482 /// \brief Create a call to the experimental.gc.relocate intrinsics to
483 /// project the relocated value of one pointer from the statepoint.
484 CallInst *CreateGCRelocate(Instruction *Statepoint,
488 const Twine &Name = "");
491 /// \brief Create a call to a masked intrinsic with given Id.
492 /// Masked intrinsic has only one overloaded type - data type.
493 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
494 Type *DataTy, const Twine &Name = "");
496 Value *getCastedInt8PtrValue(Value *Ptr);
499 /// \brief This provides a uniform API for creating instructions and inserting
500 /// them into a basic block: either at the end of a BasicBlock, or at a specific
501 /// iterator location in a block.
503 /// Note that the builder does not expose the full generality of LLVM
504 /// instructions. For access to extra instruction properties, use the mutators
505 /// (e.g. setVolatile) on the instructions after they have been
506 /// created. Convenience state exists to specify fast-math flags and fp-math
509 /// The first template argument handles whether or not to preserve names in the
510 /// final instruction output. This defaults to on. The second template argument
511 /// specifies a class to use for creating constants. This defaults to creating
512 /// minimally folded constants. The third template argument allows clients to
513 /// specify custom insertion hooks that are called on every newly created
515 template<bool preserveNames = true, typename T = ConstantFolder,
516 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
517 class IRBuilder : public IRBuilderBase, public Inserter {
520 IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
521 MDNode *FPMathTag = nullptr)
522 : IRBuilderBase(C, FPMathTag), Inserter(std::move(I)), Folder(F) {}
524 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
525 : IRBuilderBase(C, FPMathTag), Folder() {
528 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
529 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
530 SetInsertPoint(TheBB);
533 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
534 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
535 SetInsertPoint(TheBB);
538 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
539 : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
543 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
544 MDNode *FPMathTag = nullptr)
545 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
546 SetInsertPoint(TheBB, IP);
549 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
550 MDNode *FPMathTag = nullptr)
551 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
552 SetInsertPoint(TheBB, IP);
555 /// \brief Get the constant folder being used.
556 const T &getFolder() { return Folder; }
558 /// \brief Return true if this builder is configured to actually add the
559 /// requested names to IR created through it.
560 bool isNamePreserving() const { return preserveNames; }
562 /// \brief Insert and return the specified instruction.
563 template<typename InstTy>
564 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
565 this->InsertHelper(I, Name, BB, InsertPt);
566 this->SetInstDebugLocation(I);
570 /// \brief No-op overload to handle constants.
571 Constant *Insert(Constant *C, const Twine& = "") const {
575 //===--------------------------------------------------------------------===//
576 // Instruction creation methods: Terminators
577 //===--------------------------------------------------------------------===//
580 /// \brief Helper to add branch weight metadata onto an instruction.
581 /// \returns The annotated instruction.
582 template <typename InstTy>
583 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
585 I->setMetadata(LLVMContext::MD_prof, Weights);
590 /// \brief Create a 'ret void' instruction.
591 ReturnInst *CreateRetVoid() {
592 return Insert(ReturnInst::Create(Context));
595 /// \brief Create a 'ret <val>' instruction.
596 ReturnInst *CreateRet(Value *V) {
597 return Insert(ReturnInst::Create(Context, V));
600 /// \brief Create a sequence of N insertvalue instructions,
601 /// with one Value from the retVals array each, that build a aggregate
602 /// return value one value at a time, and a ret instruction to return
603 /// the resulting aggregate value.
605 /// This is a convenience function for code that uses aggregate return values
606 /// as a vehicle for having multiple return values.
607 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
608 Value *V = UndefValue::get(getCurrentFunctionReturnType());
609 for (unsigned i = 0; i != N; ++i)
610 V = CreateInsertValue(V, retVals[i], i, "mrv");
611 return Insert(ReturnInst::Create(Context, V));
614 /// \brief Create an unconditional 'br label X' instruction.
615 BranchInst *CreateBr(BasicBlock *Dest) {
616 return Insert(BranchInst::Create(Dest));
619 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
621 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
622 MDNode *BranchWeights = nullptr) {
623 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
627 /// \brief Create a switch instruction with the specified value, default dest,
628 /// and with a hint for the number of cases that will be added (for efficient
630 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
631 MDNode *BranchWeights = nullptr) {
632 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
636 /// \brief Create an indirect branch instruction with the specified address
637 /// operand, with an optional hint for the number of destinations that will be
638 /// added (for efficient allocation).
639 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
640 return Insert(IndirectBrInst::Create(Addr, NumDests));
643 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
644 BasicBlock *UnwindDest, const Twine &Name = "") {
645 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, None),
648 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
649 BasicBlock *UnwindDest, Value *Arg1,
650 const Twine &Name = "") {
651 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
654 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
655 BasicBlock *UnwindDest, Value *Arg1,
656 Value *Arg2, Value *Arg3,
657 const Twine &Name = "") {
658 Value *Args[] = { Arg1, Arg2, Arg3 };
659 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
662 /// \brief Create an invoke instruction.
663 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
664 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
665 const Twine &Name = "") {
666 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
670 ResumeInst *CreateResume(Value *Exn) {
671 return Insert(ResumeInst::Create(Exn));
674 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
675 BasicBlock *UnwindBB = nullptr) {
676 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
679 CatchPadInst *CreateCatchPad(BasicBlock *NormalDest, BasicBlock *UnwindDest,
680 ArrayRef<Value *> Args, const Twine &Name = "") {
681 return Insert(CatchPadInst::Create(NormalDest, UnwindDest, Args), Name);
684 CatchEndPadInst *CreateCatchEndPad(BasicBlock *UnwindBB = nullptr) {
685 return Insert(CatchEndPadInst::Create(Context, UnwindBB));
688 TerminatePadInst *CreateTerminatePad(BasicBlock *UnwindBB = nullptr,
689 ArrayRef<Value *> Args = {},
690 const Twine &Name = "") {
691 return Insert(TerminatePadInst::Create(Context, UnwindBB, Args), Name);
694 CleanupPadInst *CreateCleanupPad(ArrayRef<Value *> Args,
695 const Twine &Name = "") {
696 return Insert(CleanupPadInst::Create(Context, Args), Name);
699 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
700 return Insert(CatchReturnInst::Create(CatchPad, BB));
703 UnreachableInst *CreateUnreachable() {
704 return Insert(new UnreachableInst(Context));
707 //===--------------------------------------------------------------------===//
708 // Instruction creation methods: Binary Operators
709 //===--------------------------------------------------------------------===//
711 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
712 Value *LHS, Value *RHS,
714 bool HasNUW, bool HasNSW) {
715 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
716 if (HasNUW) BO->setHasNoUnsignedWrap();
717 if (HasNSW) BO->setHasNoSignedWrap();
721 Instruction *AddFPMathAttributes(Instruction *I,
723 FastMathFlags FMF) const {
725 FPMathTag = DefaultFPMathTag;
727 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
728 I->setFastMathFlags(FMF);
732 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
733 bool HasNUW = false, bool HasNSW = false) {
734 if (Constant *LC = dyn_cast<Constant>(LHS))
735 if (Constant *RC = dyn_cast<Constant>(RHS))
736 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
737 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
740 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
741 return CreateAdd(LHS, RHS, Name, false, true);
743 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
744 return CreateAdd(LHS, RHS, Name, true, false);
746 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
747 MDNode *FPMathTag = nullptr) {
748 if (Constant *LC = dyn_cast<Constant>(LHS))
749 if (Constant *RC = dyn_cast<Constant>(RHS))
750 return Insert(Folder.CreateFAdd(LC, RC), Name);
751 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
752 FPMathTag, FMF), Name);
754 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
755 bool HasNUW = false, bool HasNSW = false) {
756 if (Constant *LC = dyn_cast<Constant>(LHS))
757 if (Constant *RC = dyn_cast<Constant>(RHS))
758 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
759 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
762 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
763 return CreateSub(LHS, RHS, Name, false, true);
765 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
766 return CreateSub(LHS, RHS, Name, true, false);
768 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
769 MDNode *FPMathTag = nullptr) {
770 if (Constant *LC = dyn_cast<Constant>(LHS))
771 if (Constant *RC = dyn_cast<Constant>(RHS))
772 return Insert(Folder.CreateFSub(LC, RC), Name);
773 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
774 FPMathTag, FMF), Name);
776 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
777 bool HasNUW = false, bool HasNSW = false) {
778 if (Constant *LC = dyn_cast<Constant>(LHS))
779 if (Constant *RC = dyn_cast<Constant>(RHS))
780 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
781 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
784 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
785 return CreateMul(LHS, RHS, Name, false, true);
787 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
788 return CreateMul(LHS, RHS, Name, true, false);
790 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
791 MDNode *FPMathTag = nullptr) {
792 if (Constant *LC = dyn_cast<Constant>(LHS))
793 if (Constant *RC = dyn_cast<Constant>(RHS))
794 return Insert(Folder.CreateFMul(LC, RC), Name);
795 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
796 FPMathTag, FMF), Name);
798 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
799 bool isExact = false) {
800 if (Constant *LC = dyn_cast<Constant>(LHS))
801 if (Constant *RC = dyn_cast<Constant>(RHS))
802 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
804 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
805 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
807 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
808 return CreateUDiv(LHS, RHS, Name, true);
810 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
811 bool isExact = false) {
812 if (Constant *LC = dyn_cast<Constant>(LHS))
813 if (Constant *RC = dyn_cast<Constant>(RHS))
814 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
816 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
817 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
819 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
820 return CreateSDiv(LHS, RHS, Name, true);
822 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
823 MDNode *FPMathTag = nullptr) {
824 if (Constant *LC = dyn_cast<Constant>(LHS))
825 if (Constant *RC = dyn_cast<Constant>(RHS))
826 return Insert(Folder.CreateFDiv(LC, RC), Name);
827 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
828 FPMathTag, FMF), Name);
830 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
831 if (Constant *LC = dyn_cast<Constant>(LHS))
832 if (Constant *RC = dyn_cast<Constant>(RHS))
833 return Insert(Folder.CreateURem(LC, RC), Name);
834 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
836 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
837 if (Constant *LC = dyn_cast<Constant>(LHS))
838 if (Constant *RC = dyn_cast<Constant>(RHS))
839 return Insert(Folder.CreateSRem(LC, RC), Name);
840 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
842 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
843 MDNode *FPMathTag = nullptr) {
844 if (Constant *LC = dyn_cast<Constant>(LHS))
845 if (Constant *RC = dyn_cast<Constant>(RHS))
846 return Insert(Folder.CreateFRem(LC, RC), Name);
847 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
848 FPMathTag, FMF), Name);
851 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
852 bool HasNUW = false, bool HasNSW = false) {
853 if (Constant *LC = dyn_cast<Constant>(LHS))
854 if (Constant *RC = dyn_cast<Constant>(RHS))
855 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
856 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
859 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
860 bool HasNUW = false, bool HasNSW = false) {
861 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
864 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
865 bool HasNUW = false, bool HasNSW = false) {
866 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
870 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
871 bool isExact = false) {
872 if (Constant *LC = dyn_cast<Constant>(LHS))
873 if (Constant *RC = dyn_cast<Constant>(RHS))
874 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
876 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
877 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
879 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
880 bool isExact = false) {
881 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
883 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
884 bool isExact = false) {
885 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
888 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
889 bool isExact = false) {
890 if (Constant *LC = dyn_cast<Constant>(LHS))
891 if (Constant *RC = dyn_cast<Constant>(RHS))
892 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
894 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
895 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
897 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
898 bool isExact = false) {
899 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
901 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
902 bool isExact = false) {
903 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
906 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
907 if (Constant *RC = dyn_cast<Constant>(RHS)) {
908 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
909 return LHS; // LHS & -1 -> LHS
910 if (Constant *LC = dyn_cast<Constant>(LHS))
911 return Insert(Folder.CreateAnd(LC, RC), Name);
913 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
915 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
916 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
918 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
919 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
922 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
923 if (Constant *RC = dyn_cast<Constant>(RHS)) {
924 if (RC->isNullValue())
925 return LHS; // LHS | 0 -> LHS
926 if (Constant *LC = dyn_cast<Constant>(LHS))
927 return Insert(Folder.CreateOr(LC, RC), Name);
929 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
931 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
932 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
934 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
935 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
938 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
939 if (Constant *LC = dyn_cast<Constant>(LHS))
940 if (Constant *RC = dyn_cast<Constant>(RHS))
941 return Insert(Folder.CreateXor(LC, RC), Name);
942 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
944 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
945 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
947 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
948 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
951 Value *CreateBinOp(Instruction::BinaryOps Opc,
952 Value *LHS, Value *RHS, const Twine &Name = "",
953 MDNode *FPMathTag = nullptr) {
954 if (Constant *LC = dyn_cast<Constant>(LHS))
955 if (Constant *RC = dyn_cast<Constant>(RHS))
956 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
957 llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
958 if (isa<FPMathOperator>(BinOp))
959 BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
960 return Insert(BinOp, Name);
963 Value *CreateNeg(Value *V, const Twine &Name = "",
964 bool HasNUW = false, bool HasNSW = false) {
965 if (Constant *VC = dyn_cast<Constant>(V))
966 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
967 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
968 if (HasNUW) BO->setHasNoUnsignedWrap();
969 if (HasNSW) BO->setHasNoSignedWrap();
972 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
973 return CreateNeg(V, Name, false, true);
975 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
976 return CreateNeg(V, Name, true, false);
978 Value *CreateFNeg(Value *V, const Twine &Name = "",
979 MDNode *FPMathTag = nullptr) {
980 if (Constant *VC = dyn_cast<Constant>(V))
981 return Insert(Folder.CreateFNeg(VC), Name);
982 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
983 FPMathTag, FMF), Name);
985 Value *CreateNot(Value *V, const Twine &Name = "") {
986 if (Constant *VC = dyn_cast<Constant>(V))
987 return Insert(Folder.CreateNot(VC), Name);
988 return Insert(BinaryOperator::CreateNot(V), Name);
991 //===--------------------------------------------------------------------===//
992 // Instruction creation methods: Memory Instructions
993 //===--------------------------------------------------------------------===//
995 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
996 const Twine &Name = "") {
997 return Insert(new AllocaInst(Ty, ArraySize), Name);
999 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
1000 // converting the string to 'bool' for the isVolatile parameter.
1001 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1002 return Insert(new LoadInst(Ptr), Name);
1004 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1005 return Insert(new LoadInst(Ptr), Name);
1007 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1008 return Insert(new LoadInst(Ty, Ptr), Name);
1010 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1011 return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
1013 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1014 return Insert(new StoreInst(Val, Ptr, isVolatile));
1016 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
1017 // correctly, instead of converting the string to 'bool' for the isVolatile
1019 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
1020 LoadInst *LI = CreateLoad(Ptr, Name);
1021 LI->setAlignment(Align);
1024 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
1025 const Twine &Name = "") {
1026 LoadInst *LI = CreateLoad(Ptr, Name);
1027 LI->setAlignment(Align);
1030 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
1031 const Twine &Name = "") {
1032 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
1033 LI->setAlignment(Align);
1036 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
1037 bool isVolatile = false) {
1038 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
1039 SI->setAlignment(Align);
1042 FenceInst *CreateFence(AtomicOrdering Ordering,
1043 SynchronizationScope SynchScope = CrossThread,
1044 const Twine &Name = "") {
1045 return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
1048 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1049 AtomicOrdering SuccessOrdering,
1050 AtomicOrdering FailureOrdering,
1051 SynchronizationScope SynchScope = CrossThread) {
1052 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1053 FailureOrdering, SynchScope));
1055 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1056 AtomicOrdering Ordering,
1057 SynchronizationScope SynchScope = CrossThread) {
1058 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
1060 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1061 const Twine &Name = "") {
1062 return CreateGEP(nullptr, Ptr, IdxList, Name);
1064 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1065 const Twine &Name = "") {
1066 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1067 // Every index must be constant.
1069 for (i = 0, e = IdxList.size(); i != e; ++i)
1070 if (!isa<Constant>(IdxList[i]))
1073 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1075 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1077 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1078 const Twine &Name = "") {
1079 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1081 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1082 const Twine &Name = "") {
1083 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1084 // Every index must be constant.
1086 for (i = 0, e = IdxList.size(); i != e; ++i)
1087 if (!isa<Constant>(IdxList[i]))
1090 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1093 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1095 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1096 return CreateGEP(nullptr, Ptr, Idx, Name);
1098 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1099 if (Constant *PC = dyn_cast<Constant>(Ptr))
1100 if (Constant *IC = dyn_cast<Constant>(Idx))
1101 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1102 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1104 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1105 const Twine &Name = "") {
1106 if (Constant *PC = dyn_cast<Constant>(Ptr))
1107 if (Constant *IC = dyn_cast<Constant>(Idx))
1108 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1109 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1111 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1112 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1114 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1115 const Twine &Name = "") {
1116 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1118 if (Constant *PC = dyn_cast<Constant>(Ptr))
1119 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1121 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1123 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1124 const Twine &Name = "") {
1125 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1127 if (Constant *PC = dyn_cast<Constant>(Ptr))
1128 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1130 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1132 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1133 const Twine &Name = "") {
1135 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1136 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1139 if (Constant *PC = dyn_cast<Constant>(Ptr))
1140 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1142 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1144 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1145 unsigned Idx1, const Twine &Name = "") {
1147 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1148 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1151 if (Constant *PC = dyn_cast<Constant>(Ptr))
1152 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1154 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1156 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1157 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1159 if (Constant *PC = dyn_cast<Constant>(Ptr))
1160 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
1162 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
1164 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1165 const Twine &Name = "") {
1166 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1168 if (Constant *PC = dyn_cast<Constant>(Ptr))
1169 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
1171 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
1173 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1174 const Twine &Name = "") {
1176 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1177 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1180 if (Constant *PC = dyn_cast<Constant>(Ptr))
1181 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
1183 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
1185 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1186 const Twine &Name = "") {
1188 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1189 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1192 if (Constant *PC = dyn_cast<Constant>(Ptr))
1193 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
1196 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
1198 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1199 const Twine &Name = "") {
1200 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1203 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1204 /// instead of a pointer to array of i8.
1205 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1206 unsigned AddressSpace = 0) {
1207 GlobalVariable *gv = CreateGlobalString(Str, Name, AddressSpace);
1208 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1209 Value *Args[] = { zero, zero };
1210 return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
1213 //===--------------------------------------------------------------------===//
1214 // Instruction creation methods: Cast/Conversion Operators
1215 //===--------------------------------------------------------------------===//
1217 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1218 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1220 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1221 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1223 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1224 return CreateCast(Instruction::SExt, V, DestTy, Name);
1226 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1227 /// the value untouched if the type of V is already DestTy.
1228 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1229 const Twine &Name = "") {
1230 assert(V->getType()->isIntOrIntVectorTy() &&
1231 DestTy->isIntOrIntVectorTy() &&
1232 "Can only zero extend/truncate integers!");
1233 Type *VTy = V->getType();
1234 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1235 return CreateZExt(V, DestTy, Name);
1236 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1237 return CreateTrunc(V, DestTy, Name);
1240 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1241 /// the value untouched if the type of V is already DestTy.
1242 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1243 const Twine &Name = "") {
1244 assert(V->getType()->isIntOrIntVectorTy() &&
1245 DestTy->isIntOrIntVectorTy() &&
1246 "Can only sign extend/truncate integers!");
1247 Type *VTy = V->getType();
1248 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1249 return CreateSExt(V, DestTy, Name);
1250 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1251 return CreateTrunc(V, DestTy, Name);
1254 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1255 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1257 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1258 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1260 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1261 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1263 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1264 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1266 Value *CreateFPTrunc(Value *V, Type *DestTy,
1267 const Twine &Name = "") {
1268 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1270 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1271 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1273 Value *CreatePtrToInt(Value *V, Type *DestTy,
1274 const Twine &Name = "") {
1275 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1277 Value *CreateIntToPtr(Value *V, Type *DestTy,
1278 const Twine &Name = "") {
1279 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1281 Value *CreateBitCast(Value *V, Type *DestTy,
1282 const Twine &Name = "") {
1283 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1285 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1286 const Twine &Name = "") {
1287 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1289 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1290 const Twine &Name = "") {
1291 if (V->getType() == DestTy)
1293 if (Constant *VC = dyn_cast<Constant>(V))
1294 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1295 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1297 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1298 const Twine &Name = "") {
1299 if (V->getType() == DestTy)
1301 if (Constant *VC = dyn_cast<Constant>(V))
1302 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1303 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1305 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1306 const Twine &Name = "") {
1307 if (V->getType() == DestTy)
1309 if (Constant *VC = dyn_cast<Constant>(V))
1310 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1311 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1313 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1314 const Twine &Name = "") {
1315 if (V->getType() == DestTy)
1317 if (Constant *VC = dyn_cast<Constant>(V))
1318 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1319 return Insert(CastInst::Create(Op, V, DestTy), Name);
1321 Value *CreatePointerCast(Value *V, Type *DestTy,
1322 const Twine &Name = "") {
1323 if (V->getType() == DestTy)
1325 if (Constant *VC = dyn_cast<Constant>(V))
1326 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1327 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1330 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1331 const Twine &Name = "") {
1332 if (V->getType() == DestTy)
1335 if (Constant *VC = dyn_cast<Constant>(V)) {
1336 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1340 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1344 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1345 const Twine &Name = "") {
1346 if (V->getType() == DestTy)
1348 if (Constant *VC = dyn_cast<Constant>(V))
1349 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1350 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1353 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
1354 const Twine &Name = "") {
1355 if (V->getType() == DestTy)
1357 if (V->getType()->isPointerTy() && DestTy->isIntegerTy())
1358 return CreatePtrToInt(V, DestTy, Name);
1359 if (V->getType()->isIntegerTy() && DestTy->isPointerTy())
1360 return CreateIntToPtr(V, DestTy, Name);
1362 return CreateBitCast(V, DestTy, Name);
1365 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1366 // compile time error, instead of converting the string to bool for the
1367 // isSigned parameter.
1368 Value *CreateIntCast(Value *, Type *, const char *) = delete;
1370 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1371 if (V->getType() == DestTy)
1373 if (Constant *VC = dyn_cast<Constant>(V))
1374 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1375 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1378 //===--------------------------------------------------------------------===//
1379 // Instruction creation methods: Compare Instructions
1380 //===--------------------------------------------------------------------===//
1382 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1383 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1385 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1386 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1388 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1389 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1391 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1392 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1394 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1395 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1397 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1398 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1400 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1401 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1403 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1404 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1406 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1407 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1409 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1410 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1413 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1414 MDNode *FPMathTag = nullptr) {
1415 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
1417 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
1418 MDNode *FPMathTag = nullptr) {
1419 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
1421 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
1422 MDNode *FPMathTag = nullptr) {
1423 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
1425 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
1426 MDNode *FPMathTag = nullptr) {
1427 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
1429 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
1430 MDNode *FPMathTag = nullptr) {
1431 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
1433 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
1434 MDNode *FPMathTag = nullptr) {
1435 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
1437 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
1438 MDNode *FPMathTag = nullptr) {
1439 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
1441 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
1442 MDNode *FPMathTag = nullptr) {
1443 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
1445 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1446 MDNode *FPMathTag = nullptr) {
1447 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
1449 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
1450 MDNode *FPMathTag = nullptr) {
1451 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
1453 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
1454 MDNode *FPMathTag = nullptr) {
1455 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
1457 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
1458 MDNode *FPMathTag = nullptr) {
1459 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
1461 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
1462 MDNode *FPMathTag = nullptr) {
1463 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
1465 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
1466 MDNode *FPMathTag = nullptr) {
1467 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
1470 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1471 const Twine &Name = "") {
1472 if (Constant *LC = dyn_cast<Constant>(LHS))
1473 if (Constant *RC = dyn_cast<Constant>(RHS))
1474 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1475 return Insert(new ICmpInst(P, LHS, RHS), Name);
1477 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1478 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1479 if (Constant *LC = dyn_cast<Constant>(LHS))
1480 if (Constant *RC = dyn_cast<Constant>(RHS))
1481 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1482 return Insert(AddFPMathAttributes(new FCmpInst(P, LHS, RHS),
1483 FPMathTag, FMF), Name);
1486 //===--------------------------------------------------------------------===//
1487 // Instruction creation methods: Other Instructions
1488 //===--------------------------------------------------------------------===//
1490 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1491 const Twine &Name = "") {
1492 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1495 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
1496 const Twine &Name = "") {
1497 return Insert(CallInst::Create(Callee, Args), Name);
1500 CallInst *CreateCall(llvm::FunctionType *FTy, Value *Callee,
1501 ArrayRef<Value *> Args, const Twine &Name = "") {
1502 return Insert(CallInst::Create(FTy, Callee, Args), Name);
1505 CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args,
1506 const Twine &Name = "") {
1507 return CreateCall(Callee->getFunctionType(), Callee, Args, Name);
1510 Value *CreateSelect(Value *C, Value *True, Value *False,
1511 const Twine &Name = "") {
1512 if (Constant *CC = dyn_cast<Constant>(C))
1513 if (Constant *TC = dyn_cast<Constant>(True))
1514 if (Constant *FC = dyn_cast<Constant>(False))
1515 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1516 return Insert(SelectInst::Create(C, True, False), Name);
1519 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1520 return Insert(new VAArgInst(List, Ty), Name);
1523 Value *CreateExtractElement(Value *Vec, Value *Idx,
1524 const Twine &Name = "") {
1525 if (Constant *VC = dyn_cast<Constant>(Vec))
1526 if (Constant *IC = dyn_cast<Constant>(Idx))
1527 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1528 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1531 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
1532 const Twine &Name = "") {
1533 return CreateExtractElement(Vec, getInt64(Idx), Name);
1536 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1537 const Twine &Name = "") {
1538 if (Constant *VC = dyn_cast<Constant>(Vec))
1539 if (Constant *NC = dyn_cast<Constant>(NewElt))
1540 if (Constant *IC = dyn_cast<Constant>(Idx))
1541 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1542 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1545 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
1546 const Twine &Name = "") {
1547 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
1550 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1551 const Twine &Name = "") {
1552 if (Constant *V1C = dyn_cast<Constant>(V1))
1553 if (Constant *V2C = dyn_cast<Constant>(V2))
1554 if (Constant *MC = dyn_cast<Constant>(Mask))
1555 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1556 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1559 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> IntMask,
1560 const Twine &Name = "") {
1561 size_t MaskSize = IntMask.size();
1562 SmallVector<Constant*, 8> MaskVec(MaskSize);
1563 for (size_t i = 0; i != MaskSize; ++i)
1564 MaskVec[i] = getInt32(IntMask[i]);
1565 Value *Mask = ConstantVector::get(MaskVec);
1566 return CreateShuffleVector(V1, V2, Mask, Name);
1569 Value *CreateExtractValue(Value *Agg,
1570 ArrayRef<unsigned> Idxs,
1571 const Twine &Name = "") {
1572 if (Constant *AggC = dyn_cast<Constant>(Agg))
1573 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1574 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1577 Value *CreateInsertValue(Value *Agg, Value *Val,
1578 ArrayRef<unsigned> Idxs,
1579 const Twine &Name = "") {
1580 if (Constant *AggC = dyn_cast<Constant>(Agg))
1581 if (Constant *ValC = dyn_cast<Constant>(Val))
1582 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1583 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1586 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
1587 const Twine &Name = "") {
1588 return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
1591 //===--------------------------------------------------------------------===//
1592 // Utility creation methods
1593 //===--------------------------------------------------------------------===//
1595 /// \brief Return an i1 value testing if \p Arg is null.
1596 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1597 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1601 /// \brief Return an i1 value testing if \p Arg is not null.
1602 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1603 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1607 /// \brief Return the i64 difference between two pointer values, dividing out
1608 /// the size of the pointed-to objects.
1610 /// This is intended to implement C-style pointer subtraction. As such, the
1611 /// pointers must be appropriately aligned for their element types and
1612 /// pointing into the same object.
1613 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1614 assert(LHS->getType() == RHS->getType() &&
1615 "Pointer subtraction operand types must match!");
1616 PointerType *ArgType = cast<PointerType>(LHS->getType());
1617 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1618 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1619 Value *Difference = CreateSub(LHS_int, RHS_int);
1620 return CreateExactSDiv(Difference,
1621 ConstantExpr::getSizeOf(ArgType->getElementType()),
1625 /// \brief Return a vector value that contains \arg V broadcasted to \p
1626 /// NumElts elements.
1627 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1628 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1630 // First insert it into an undef vector so we can shuffle it.
1631 Type *I32Ty = getInt32Ty();
1632 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1633 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1634 Name + ".splatinsert");
1636 // Shuffle the value across the desired number of elements.
1637 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1638 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1641 /// \brief Return a value that has been extracted from a larger integer type.
1642 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
1643 IntegerType *ExtractedTy, uint64_t Offset,
1644 const Twine &Name) {
1645 IntegerType *IntTy = cast<IntegerType>(From->getType());
1646 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
1647 DL.getTypeStoreSize(IntTy) &&
1648 "Element extends past full value");
1649 uint64_t ShAmt = 8 * Offset;
1651 if (DL.isBigEndian())
1652 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
1653 DL.getTypeStoreSize(ExtractedTy) - Offset);
1655 V = CreateLShr(V, ShAmt, Name + ".shift");
1657 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
1658 "Cannot extract to a larger integer!");
1659 if (ExtractedTy != IntTy) {
1660 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
1665 /// \brief Create an assume intrinsic call that represents an alignment
1666 /// assumption on the provided pointer.
1668 /// An optional offset can be provided, and if it is provided, the offset
1669 /// must be subtracted from the provided pointer to get the pointer with the
1670 /// specified alignment.
1671 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
1673 Value *OffsetValue = nullptr) {
1674 assert(isa<PointerType>(PtrValue->getType()) &&
1675 "trying to create an alignment assumption on a non-pointer?");
1677 PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
1678 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
1679 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
1681 Value *Mask = ConstantInt::get(IntPtrTy,
1682 Alignment > 0 ? Alignment - 1 : 0);
1684 bool IsOffsetZero = false;
1685 if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
1686 IsOffsetZero = CI->isZero();
1688 if (!IsOffsetZero) {
1689 if (OffsetValue->getType() != IntPtrTy)
1690 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
1692 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
1696 Value *Zero = ConstantInt::get(IntPtrTy, 0);
1697 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
1698 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
1700 return CreateAssumption(InvCond);
1704 // Create wrappers for C Binding types (see CBindingWrapping.h).
1705 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)