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, const Inserter &I = Inserter(),
521 MDNode *FPMathTag = nullptr)
522 : IRBuilderBase(C, FPMathTag), Inserter(I), Folder(F) {
525 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
526 : IRBuilderBase(C, FPMathTag), Folder() {
529 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
530 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
531 SetInsertPoint(TheBB);
534 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
535 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
536 SetInsertPoint(TheBB);
539 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
540 : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
544 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
545 MDNode *FPMathTag = nullptr)
546 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
547 SetInsertPoint(TheBB, IP);
550 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
551 MDNode *FPMathTag = nullptr)
552 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
553 SetInsertPoint(TheBB, IP);
556 /// \brief Get the constant folder being used.
557 const T &getFolder() { return Folder; }
559 /// \brief Return true if this builder is configured to actually add the
560 /// requested names to IR created through it.
561 bool isNamePreserving() const { return preserveNames; }
563 /// \brief Insert and return the specified instruction.
564 template<typename InstTy>
565 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
566 this->InsertHelper(I, Name, BB, InsertPt);
567 this->SetInstDebugLocation(I);
571 /// \brief No-op overload to handle constants.
572 Constant *Insert(Constant *C, const Twine& = "") const {
576 //===--------------------------------------------------------------------===//
577 // Instruction creation methods: Terminators
578 //===--------------------------------------------------------------------===//
581 /// \brief Helper to add branch weight metadata onto an instruction.
582 /// \returns The annotated instruction.
583 template <typename InstTy>
584 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
586 I->setMetadata(LLVMContext::MD_prof, Weights);
591 /// \brief Create a 'ret void' instruction.
592 ReturnInst *CreateRetVoid() {
593 return Insert(ReturnInst::Create(Context));
596 /// \brief Create a 'ret <val>' instruction.
597 ReturnInst *CreateRet(Value *V) {
598 return Insert(ReturnInst::Create(Context, V));
601 /// \brief Create a sequence of N insertvalue instructions,
602 /// with one Value from the retVals array each, that build a aggregate
603 /// return value one value at a time, and a ret instruction to return
604 /// the resulting aggregate value.
606 /// This is a convenience function for code that uses aggregate return values
607 /// as a vehicle for having multiple return values.
608 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
609 Value *V = UndefValue::get(getCurrentFunctionReturnType());
610 for (unsigned i = 0; i != N; ++i)
611 V = CreateInsertValue(V, retVals[i], i, "mrv");
612 return Insert(ReturnInst::Create(Context, V));
615 /// \brief Create an unconditional 'br label X' instruction.
616 BranchInst *CreateBr(BasicBlock *Dest) {
617 return Insert(BranchInst::Create(Dest));
620 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
622 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
623 MDNode *BranchWeights = nullptr) {
624 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
628 /// \brief Create a switch instruction with the specified value, default dest,
629 /// and with a hint for the number of cases that will be added (for efficient
631 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
632 MDNode *BranchWeights = nullptr) {
633 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
637 /// \brief Create an indirect branch instruction with the specified address
638 /// operand, with an optional hint for the number of destinations that will be
639 /// added (for efficient allocation).
640 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
641 return Insert(IndirectBrInst::Create(Addr, NumDests));
644 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
645 BasicBlock *UnwindDest, const Twine &Name = "") {
646 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, None),
649 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
650 BasicBlock *UnwindDest, Value *Arg1,
651 const Twine &Name = "") {
652 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
655 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
656 BasicBlock *UnwindDest, Value *Arg1,
657 Value *Arg2, Value *Arg3,
658 const Twine &Name = "") {
659 Value *Args[] = { Arg1, Arg2, Arg3 };
660 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
663 /// \brief Create an invoke instruction.
664 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
665 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
666 const Twine &Name = "") {
667 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
671 ResumeInst *CreateResume(Value *Exn) {
672 return Insert(ResumeInst::Create(Exn));
675 CleanupReturnInst *CreateCleanupRet(BasicBlock *UnwindBB = nullptr,
676 Value *RetVal = nullptr) {
677 return Insert(CleanupReturnInst::Create(Context, RetVal, UnwindBB));
680 CatchBlockInst *CreateCatchBlock(Type *Ty, BasicBlock *NormalDest,
681 BasicBlock *UnwindDest,
682 ArrayRef<Value *> Args,
683 const Twine &Name = "") {
684 return Insert(CatchBlockInst::Create(Ty, NormalDest, UnwindDest, Args),
688 CatchEndBlockInst *CreateCatchEndBlock(BasicBlock *UnwindBB = nullptr) {
689 return Insert(CatchEndBlockInst::Create(Context, UnwindBB));
692 TerminateBlockInst *CreateTerminateBlock(BasicBlock *NormalDest = nullptr,
693 ArrayRef<Value *> Args = {},
694 const Twine &Name = "") {
695 return Insert(TerminateBlockInst::Create(Context, NormalDest, Args), Name);
698 CleanupBlockInst *CreateCleanupBlock(Type *Ty, ArrayRef<Value *> Args,
699 const Twine &Name = "") {
700 return Insert(CleanupBlockInst::Create(Ty, Args), Name);
703 CatchReturnInst *CreateCatchRet(BasicBlock *BB) {
704 return Insert(CatchReturnInst::Create(BB));
707 UnreachableInst *CreateUnreachable() {
708 return Insert(new UnreachableInst(Context));
711 //===--------------------------------------------------------------------===//
712 // Instruction creation methods: Binary Operators
713 //===--------------------------------------------------------------------===//
715 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
716 Value *LHS, Value *RHS,
718 bool HasNUW, bool HasNSW) {
719 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
720 if (HasNUW) BO->setHasNoUnsignedWrap();
721 if (HasNSW) BO->setHasNoSignedWrap();
725 Instruction *AddFPMathAttributes(Instruction *I,
727 FastMathFlags FMF) const {
729 FPMathTag = DefaultFPMathTag;
731 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
732 I->setFastMathFlags(FMF);
736 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
737 bool HasNUW = false, bool HasNSW = false) {
738 if (Constant *LC = dyn_cast<Constant>(LHS))
739 if (Constant *RC = dyn_cast<Constant>(RHS))
740 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
741 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
744 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
745 return CreateAdd(LHS, RHS, Name, false, true);
747 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
748 return CreateAdd(LHS, RHS, Name, true, false);
750 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
751 MDNode *FPMathTag = nullptr) {
752 if (Constant *LC = dyn_cast<Constant>(LHS))
753 if (Constant *RC = dyn_cast<Constant>(RHS))
754 return Insert(Folder.CreateFAdd(LC, RC), Name);
755 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
756 FPMathTag, FMF), Name);
758 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
759 bool HasNUW = false, bool HasNSW = false) {
760 if (Constant *LC = dyn_cast<Constant>(LHS))
761 if (Constant *RC = dyn_cast<Constant>(RHS))
762 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
763 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
766 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
767 return CreateSub(LHS, RHS, Name, false, true);
769 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
770 return CreateSub(LHS, RHS, Name, true, false);
772 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
773 MDNode *FPMathTag = nullptr) {
774 if (Constant *LC = dyn_cast<Constant>(LHS))
775 if (Constant *RC = dyn_cast<Constant>(RHS))
776 return Insert(Folder.CreateFSub(LC, RC), Name);
777 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
778 FPMathTag, FMF), Name);
780 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
781 bool HasNUW = false, bool HasNSW = false) {
782 if (Constant *LC = dyn_cast<Constant>(LHS))
783 if (Constant *RC = dyn_cast<Constant>(RHS))
784 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
785 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
788 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
789 return CreateMul(LHS, RHS, Name, false, true);
791 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
792 return CreateMul(LHS, RHS, Name, true, false);
794 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
795 MDNode *FPMathTag = nullptr) {
796 if (Constant *LC = dyn_cast<Constant>(LHS))
797 if (Constant *RC = dyn_cast<Constant>(RHS))
798 return Insert(Folder.CreateFMul(LC, RC), Name);
799 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
800 FPMathTag, FMF), Name);
802 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
803 bool isExact = false) {
804 if (Constant *LC = dyn_cast<Constant>(LHS))
805 if (Constant *RC = dyn_cast<Constant>(RHS))
806 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
808 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
809 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
811 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
812 return CreateUDiv(LHS, RHS, Name, true);
814 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
815 bool isExact = false) {
816 if (Constant *LC = dyn_cast<Constant>(LHS))
817 if (Constant *RC = dyn_cast<Constant>(RHS))
818 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
820 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
821 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
823 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
824 return CreateSDiv(LHS, RHS, Name, true);
826 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
827 MDNode *FPMathTag = nullptr) {
828 if (Constant *LC = dyn_cast<Constant>(LHS))
829 if (Constant *RC = dyn_cast<Constant>(RHS))
830 return Insert(Folder.CreateFDiv(LC, RC), Name);
831 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
832 FPMathTag, FMF), Name);
834 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
835 if (Constant *LC = dyn_cast<Constant>(LHS))
836 if (Constant *RC = dyn_cast<Constant>(RHS))
837 return Insert(Folder.CreateURem(LC, RC), Name);
838 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
840 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
841 if (Constant *LC = dyn_cast<Constant>(LHS))
842 if (Constant *RC = dyn_cast<Constant>(RHS))
843 return Insert(Folder.CreateSRem(LC, RC), Name);
844 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
846 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
847 MDNode *FPMathTag = nullptr) {
848 if (Constant *LC = dyn_cast<Constant>(LHS))
849 if (Constant *RC = dyn_cast<Constant>(RHS))
850 return Insert(Folder.CreateFRem(LC, RC), Name);
851 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
852 FPMathTag, FMF), Name);
855 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
856 bool HasNUW = false, bool HasNSW = false) {
857 if (Constant *LC = dyn_cast<Constant>(LHS))
858 if (Constant *RC = dyn_cast<Constant>(RHS))
859 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
860 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
863 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
864 bool HasNUW = false, bool HasNSW = false) {
865 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
868 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
869 bool HasNUW = false, bool HasNSW = false) {
870 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
874 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
875 bool isExact = false) {
876 if (Constant *LC = dyn_cast<Constant>(LHS))
877 if (Constant *RC = dyn_cast<Constant>(RHS))
878 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
880 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
881 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
883 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
884 bool isExact = false) {
885 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
887 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
888 bool isExact = false) {
889 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
892 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
893 bool isExact = false) {
894 if (Constant *LC = dyn_cast<Constant>(LHS))
895 if (Constant *RC = dyn_cast<Constant>(RHS))
896 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
898 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
899 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
901 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
902 bool isExact = false) {
903 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
905 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
906 bool isExact = false) {
907 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
910 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
911 if (Constant *RC = dyn_cast<Constant>(RHS)) {
912 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
913 return LHS; // LHS & -1 -> LHS
914 if (Constant *LC = dyn_cast<Constant>(LHS))
915 return Insert(Folder.CreateAnd(LC, RC), Name);
917 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
919 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
920 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
922 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
923 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
926 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
927 if (Constant *RC = dyn_cast<Constant>(RHS)) {
928 if (RC->isNullValue())
929 return LHS; // LHS | 0 -> LHS
930 if (Constant *LC = dyn_cast<Constant>(LHS))
931 return Insert(Folder.CreateOr(LC, RC), Name);
933 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
935 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
936 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
938 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
939 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
942 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
943 if (Constant *LC = dyn_cast<Constant>(LHS))
944 if (Constant *RC = dyn_cast<Constant>(RHS))
945 return Insert(Folder.CreateXor(LC, RC), Name);
946 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
948 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
949 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
951 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
952 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
955 Value *CreateBinOp(Instruction::BinaryOps Opc,
956 Value *LHS, Value *RHS, const Twine &Name = "",
957 MDNode *FPMathTag = nullptr) {
958 if (Constant *LC = dyn_cast<Constant>(LHS))
959 if (Constant *RC = dyn_cast<Constant>(RHS))
960 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
961 llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
962 if (isa<FPMathOperator>(BinOp))
963 BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
964 return Insert(BinOp, Name);
967 Value *CreateNeg(Value *V, const Twine &Name = "",
968 bool HasNUW = false, bool HasNSW = false) {
969 if (Constant *VC = dyn_cast<Constant>(V))
970 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
971 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
972 if (HasNUW) BO->setHasNoUnsignedWrap();
973 if (HasNSW) BO->setHasNoSignedWrap();
976 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
977 return CreateNeg(V, Name, false, true);
979 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
980 return CreateNeg(V, Name, true, false);
982 Value *CreateFNeg(Value *V, const Twine &Name = "",
983 MDNode *FPMathTag = nullptr) {
984 if (Constant *VC = dyn_cast<Constant>(V))
985 return Insert(Folder.CreateFNeg(VC), Name);
986 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
987 FPMathTag, FMF), Name);
989 Value *CreateNot(Value *V, const Twine &Name = "") {
990 if (Constant *VC = dyn_cast<Constant>(V))
991 return Insert(Folder.CreateNot(VC), Name);
992 return Insert(BinaryOperator::CreateNot(V), Name);
995 //===--------------------------------------------------------------------===//
996 // Instruction creation methods: Memory Instructions
997 //===--------------------------------------------------------------------===//
999 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1000 const Twine &Name = "") {
1001 return Insert(new AllocaInst(Ty, ArraySize), Name);
1003 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
1004 // converting the string to 'bool' for the isVolatile parameter.
1005 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1006 return Insert(new LoadInst(Ptr), Name);
1008 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1009 return Insert(new LoadInst(Ptr), Name);
1011 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1012 return Insert(new LoadInst(Ty, Ptr), Name);
1014 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1015 return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
1017 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1018 return Insert(new StoreInst(Val, Ptr, isVolatile));
1020 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
1021 // correctly, instead of converting the string to 'bool' for the isVolatile
1023 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
1024 LoadInst *LI = CreateLoad(Ptr, Name);
1025 LI->setAlignment(Align);
1028 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
1029 const Twine &Name = "") {
1030 LoadInst *LI = CreateLoad(Ptr, Name);
1031 LI->setAlignment(Align);
1034 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
1035 const Twine &Name = "") {
1036 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
1037 LI->setAlignment(Align);
1040 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
1041 bool isVolatile = false) {
1042 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
1043 SI->setAlignment(Align);
1046 FenceInst *CreateFence(AtomicOrdering Ordering,
1047 SynchronizationScope SynchScope = CrossThread,
1048 const Twine &Name = "") {
1049 return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
1052 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1053 AtomicOrdering SuccessOrdering,
1054 AtomicOrdering FailureOrdering,
1055 SynchronizationScope SynchScope = CrossThread) {
1056 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1057 FailureOrdering, SynchScope));
1059 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1060 AtomicOrdering Ordering,
1061 SynchronizationScope SynchScope = CrossThread) {
1062 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
1064 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1065 const Twine &Name = "") {
1066 return CreateGEP(nullptr, Ptr, IdxList, Name);
1068 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1069 const Twine &Name = "") {
1070 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1071 // Every index must be constant.
1073 for (i = 0, e = IdxList.size(); i != e; ++i)
1074 if (!isa<Constant>(IdxList[i]))
1077 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1079 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1081 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1082 const Twine &Name = "") {
1083 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1085 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1086 const Twine &Name = "") {
1087 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1088 // Every index must be constant.
1090 for (i = 0, e = IdxList.size(); i != e; ++i)
1091 if (!isa<Constant>(IdxList[i]))
1094 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1097 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1099 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1100 return CreateGEP(nullptr, Ptr, Idx, Name);
1102 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1103 if (Constant *PC = dyn_cast<Constant>(Ptr))
1104 if (Constant *IC = dyn_cast<Constant>(Idx))
1105 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1106 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1108 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1109 const Twine &Name = "") {
1110 if (Constant *PC = dyn_cast<Constant>(Ptr))
1111 if (Constant *IC = dyn_cast<Constant>(Idx))
1112 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1113 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1115 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1116 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1118 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1119 const Twine &Name = "") {
1120 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1122 if (Constant *PC = dyn_cast<Constant>(Ptr))
1123 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1125 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1127 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1128 const Twine &Name = "") {
1129 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1131 if (Constant *PC = dyn_cast<Constant>(Ptr))
1132 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1134 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1136 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1137 const Twine &Name = "") {
1139 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1140 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1143 if (Constant *PC = dyn_cast<Constant>(Ptr))
1144 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1146 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1148 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1149 unsigned Idx1, const Twine &Name = "") {
1151 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1152 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1155 if (Constant *PC = dyn_cast<Constant>(Ptr))
1156 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1158 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1160 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1161 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1163 if (Constant *PC = dyn_cast<Constant>(Ptr))
1164 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
1166 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
1168 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1169 const Twine &Name = "") {
1170 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1172 if (Constant *PC = dyn_cast<Constant>(Ptr))
1173 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
1175 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
1177 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1178 const Twine &Name = "") {
1180 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1181 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1184 if (Constant *PC = dyn_cast<Constant>(Ptr))
1185 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
1187 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
1189 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1190 const Twine &Name = "") {
1192 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1193 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1196 if (Constant *PC = dyn_cast<Constant>(Ptr))
1197 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
1200 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
1202 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1203 const Twine &Name = "") {
1204 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1207 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1208 /// instead of a pointer to array of i8.
1209 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1210 unsigned AddressSpace = 0) {
1211 GlobalVariable *gv = CreateGlobalString(Str, Name, AddressSpace);
1212 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1213 Value *Args[] = { zero, zero };
1214 return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
1217 //===--------------------------------------------------------------------===//
1218 // Instruction creation methods: Cast/Conversion Operators
1219 //===--------------------------------------------------------------------===//
1221 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1222 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1224 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1225 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1227 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1228 return CreateCast(Instruction::SExt, V, DestTy, Name);
1230 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1231 /// the value untouched if the type of V is already DestTy.
1232 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1233 const Twine &Name = "") {
1234 assert(V->getType()->isIntOrIntVectorTy() &&
1235 DestTy->isIntOrIntVectorTy() &&
1236 "Can only zero extend/truncate integers!");
1237 Type *VTy = V->getType();
1238 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1239 return CreateZExt(V, DestTy, Name);
1240 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1241 return CreateTrunc(V, DestTy, Name);
1244 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1245 /// the value untouched if the type of V is already DestTy.
1246 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1247 const Twine &Name = "") {
1248 assert(V->getType()->isIntOrIntVectorTy() &&
1249 DestTy->isIntOrIntVectorTy() &&
1250 "Can only sign extend/truncate integers!");
1251 Type *VTy = V->getType();
1252 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1253 return CreateSExt(V, DestTy, Name);
1254 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1255 return CreateTrunc(V, DestTy, Name);
1258 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1259 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1261 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1262 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1264 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1265 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1267 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1268 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1270 Value *CreateFPTrunc(Value *V, Type *DestTy,
1271 const Twine &Name = "") {
1272 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1274 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1275 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1277 Value *CreatePtrToInt(Value *V, Type *DestTy,
1278 const Twine &Name = "") {
1279 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1281 Value *CreateIntToPtr(Value *V, Type *DestTy,
1282 const Twine &Name = "") {
1283 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1285 Value *CreateBitCast(Value *V, Type *DestTy,
1286 const Twine &Name = "") {
1287 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1289 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1290 const Twine &Name = "") {
1291 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1293 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1294 const Twine &Name = "") {
1295 if (V->getType() == DestTy)
1297 if (Constant *VC = dyn_cast<Constant>(V))
1298 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1299 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1301 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1302 const Twine &Name = "") {
1303 if (V->getType() == DestTy)
1305 if (Constant *VC = dyn_cast<Constant>(V))
1306 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1307 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1309 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1310 const Twine &Name = "") {
1311 if (V->getType() == DestTy)
1313 if (Constant *VC = dyn_cast<Constant>(V))
1314 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1315 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1317 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1318 const Twine &Name = "") {
1319 if (V->getType() == DestTy)
1321 if (Constant *VC = dyn_cast<Constant>(V))
1322 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1323 return Insert(CastInst::Create(Op, V, DestTy), Name);
1325 Value *CreatePointerCast(Value *V, Type *DestTy,
1326 const Twine &Name = "") {
1327 if (V->getType() == DestTy)
1329 if (Constant *VC = dyn_cast<Constant>(V))
1330 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1331 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1334 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1335 const Twine &Name = "") {
1336 if (V->getType() == DestTy)
1339 if (Constant *VC = dyn_cast<Constant>(V)) {
1340 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1344 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1348 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1349 const Twine &Name = "") {
1350 if (V->getType() == DestTy)
1352 if (Constant *VC = dyn_cast<Constant>(V))
1353 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1354 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1357 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
1358 const Twine &Name = "") {
1359 if (V->getType() == DestTy)
1361 if (V->getType()->isPointerTy() && DestTy->isIntegerTy())
1362 return CreatePtrToInt(V, DestTy, Name);
1363 if (V->getType()->isIntegerTy() && DestTy->isPointerTy())
1364 return CreateIntToPtr(V, DestTy, Name);
1366 return CreateBitCast(V, DestTy, Name);
1369 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1370 // compile time error, instead of converting the string to bool for the
1371 // isSigned parameter.
1372 Value *CreateIntCast(Value *, Type *, const char *) = delete;
1374 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1375 if (V->getType() == DestTy)
1377 if (Constant *VC = dyn_cast<Constant>(V))
1378 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1379 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1382 //===--------------------------------------------------------------------===//
1383 // Instruction creation methods: Compare Instructions
1384 //===--------------------------------------------------------------------===//
1386 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1387 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1389 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1390 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1392 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1393 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1395 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1396 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1398 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1399 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1401 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1402 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1404 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1405 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1407 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1408 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1410 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1411 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1413 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1414 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1417 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1418 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1420 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1421 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1423 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1424 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1426 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1427 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1429 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1430 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1432 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1433 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1435 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1436 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1438 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1439 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1441 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1442 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1444 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1445 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1447 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1448 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1450 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1451 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1453 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1454 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1456 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1457 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1460 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1461 const Twine &Name = "") {
1462 if (Constant *LC = dyn_cast<Constant>(LHS))
1463 if (Constant *RC = dyn_cast<Constant>(RHS))
1464 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1465 return Insert(new ICmpInst(P, LHS, RHS), Name);
1467 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1468 const Twine &Name = "") {
1469 if (Constant *LC = dyn_cast<Constant>(LHS))
1470 if (Constant *RC = dyn_cast<Constant>(RHS))
1471 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1472 return Insert(new FCmpInst(P, LHS, RHS), Name);
1475 //===--------------------------------------------------------------------===//
1476 // Instruction creation methods: Other Instructions
1477 //===--------------------------------------------------------------------===//
1479 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1480 const Twine &Name = "") {
1481 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1484 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1485 const Twine &Name = "") {
1486 return Insert(CallInst::Create(Callee, Args), Name);
1489 CallInst *CreateCall(llvm::FunctionType *FTy, Value *Callee,
1490 ArrayRef<Value *> Args, const Twine &Name = "") {
1491 return Insert(CallInst::Create(FTy, Callee, Args), Name);
1494 CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args,
1495 const Twine &Name = "") {
1496 return CreateCall(Callee->getFunctionType(), Callee, Args, Name);
1499 Value *CreateSelect(Value *C, Value *True, Value *False,
1500 const Twine &Name = "") {
1501 if (Constant *CC = dyn_cast<Constant>(C))
1502 if (Constant *TC = dyn_cast<Constant>(True))
1503 if (Constant *FC = dyn_cast<Constant>(False))
1504 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1505 return Insert(SelectInst::Create(C, True, False), Name);
1508 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1509 return Insert(new VAArgInst(List, Ty), Name);
1512 Value *CreateExtractElement(Value *Vec, Value *Idx,
1513 const Twine &Name = "") {
1514 if (Constant *VC = dyn_cast<Constant>(Vec))
1515 if (Constant *IC = dyn_cast<Constant>(Idx))
1516 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1517 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1520 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
1521 const Twine &Name = "") {
1522 return CreateExtractElement(Vec, getInt64(Idx), Name);
1525 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1526 const Twine &Name = "") {
1527 if (Constant *VC = dyn_cast<Constant>(Vec))
1528 if (Constant *NC = dyn_cast<Constant>(NewElt))
1529 if (Constant *IC = dyn_cast<Constant>(Idx))
1530 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1531 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1534 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
1535 const Twine &Name = "") {
1536 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
1539 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1540 const Twine &Name = "") {
1541 if (Constant *V1C = dyn_cast<Constant>(V1))
1542 if (Constant *V2C = dyn_cast<Constant>(V2))
1543 if (Constant *MC = dyn_cast<Constant>(Mask))
1544 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1545 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1548 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> IntMask,
1549 const Twine &Name = "") {
1550 size_t MaskSize = IntMask.size();
1551 SmallVector<Constant*, 8> MaskVec(MaskSize);
1552 for (size_t i = 0; i != MaskSize; ++i)
1553 MaskVec[i] = getInt32(IntMask[i]);
1554 Value *Mask = ConstantVector::get(MaskVec);
1555 return CreateShuffleVector(V1, V2, Mask, Name);
1558 Value *CreateExtractValue(Value *Agg,
1559 ArrayRef<unsigned> Idxs,
1560 const Twine &Name = "") {
1561 if (Constant *AggC = dyn_cast<Constant>(Agg))
1562 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1563 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1566 Value *CreateInsertValue(Value *Agg, Value *Val,
1567 ArrayRef<unsigned> Idxs,
1568 const Twine &Name = "") {
1569 if (Constant *AggC = dyn_cast<Constant>(Agg))
1570 if (Constant *ValC = dyn_cast<Constant>(Val))
1571 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1572 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1575 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
1576 const Twine &Name = "") {
1577 return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
1580 //===--------------------------------------------------------------------===//
1581 // Utility creation methods
1582 //===--------------------------------------------------------------------===//
1584 /// \brief Return an i1 value testing if \p Arg is null.
1585 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1586 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1590 /// \brief Return an i1 value testing if \p Arg is not null.
1591 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1592 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1596 /// \brief Return the i64 difference between two pointer values, dividing out
1597 /// the size of the pointed-to objects.
1599 /// This is intended to implement C-style pointer subtraction. As such, the
1600 /// pointers must be appropriately aligned for their element types and
1601 /// pointing into the same object.
1602 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1603 assert(LHS->getType() == RHS->getType() &&
1604 "Pointer subtraction operand types must match!");
1605 PointerType *ArgType = cast<PointerType>(LHS->getType());
1606 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1607 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1608 Value *Difference = CreateSub(LHS_int, RHS_int);
1609 return CreateExactSDiv(Difference,
1610 ConstantExpr::getSizeOf(ArgType->getElementType()),
1614 /// \brief Return a vector value that contains \arg V broadcasted to \p
1615 /// NumElts elements.
1616 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1617 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1619 // First insert it into an undef vector so we can shuffle it.
1620 Type *I32Ty = getInt32Ty();
1621 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1622 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1623 Name + ".splatinsert");
1625 // Shuffle the value across the desired number of elements.
1626 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1627 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1630 /// \brief Return a value that has been extracted from a larger integer type.
1631 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
1632 IntegerType *ExtractedTy, uint64_t Offset,
1633 const Twine &Name) {
1634 IntegerType *IntTy = cast<IntegerType>(From->getType());
1635 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
1636 DL.getTypeStoreSize(IntTy) &&
1637 "Element extends past full value");
1638 uint64_t ShAmt = 8 * Offset;
1640 if (DL.isBigEndian())
1641 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
1642 DL.getTypeStoreSize(ExtractedTy) - Offset);
1644 V = CreateLShr(V, ShAmt, Name + ".shift");
1646 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
1647 "Cannot extract to a larger integer!");
1648 if (ExtractedTy != IntTy) {
1649 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
1654 /// \brief Create an assume intrinsic call that represents an alignment
1655 /// assumption on the provided pointer.
1657 /// An optional offset can be provided, and if it is provided, the offset
1658 /// must be subtracted from the provided pointer to get the pointer with the
1659 /// specified alignment.
1660 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
1662 Value *OffsetValue = nullptr) {
1663 assert(isa<PointerType>(PtrValue->getType()) &&
1664 "trying to create an alignment assumption on a non-pointer?");
1666 PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
1667 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
1668 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
1670 Value *Mask = ConstantInt::get(IntPtrTy,
1671 Alignment > 0 ? Alignment - 1 : 0);
1673 bool IsOffsetZero = false;
1674 if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
1675 IsOffsetZero = CI->isZero();
1677 if (!IsOffsetZero) {
1678 if (OffsetValue->getType() != IntPtrTy)
1679 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
1681 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
1685 Value *Zero = ConstantInt::get(IntPtrTy, 0);
1686 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
1687 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
1689 return CreateAssumption(InvCond);
1693 // Create wrappers for C Binding types (see CBindingWrapping.h).
1694 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)