1 //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the IRBuilder class, which is used as a convenient way
10 // to create LLVM instructions with a consistent and simplified interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_IRBUILDER_H
15 #define LLVM_IR_IRBUILDER_H
17 #include "llvm-c/Types.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Constant.h"
24 #include "llvm/IR/ConstantFolder.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/GlobalVariable.h"
31 #include "llvm/IR/InstrTypes.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/Intrinsics.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/Operator.h"
38 #include "llvm/IR/Type.h"
39 #include "llvm/IR/Value.h"
40 #include "llvm/IR/ValueHandle.h"
41 #include "llvm/Support/AtomicOrdering.h"
42 #include "llvm/Support/CBindingWrapping.h"
43 #include "llvm/Support/Casting.h"
56 /// This provides the default implementation of the IRBuilder
57 /// 'InsertHelper' method that is called whenever an instruction is created by
58 /// IRBuilder and needs to be inserted.
60 /// By default, this inserts the instruction at the insertion point.
61 class IRBuilderDefaultInserter
{
63 void InsertHelper(Instruction
*I
, const Twine
&Name
,
64 BasicBlock
*BB
, BasicBlock::iterator InsertPt
) const {
65 if (BB
) BB
->getInstList().insert(InsertPt
, I
);
70 /// Provides an 'InsertHelper' that calls a user-provided callback after
71 /// performing the default insertion.
72 class IRBuilderCallbackInserter
: IRBuilderDefaultInserter
{
73 std::function
<void(Instruction
*)> Callback
;
76 IRBuilderCallbackInserter(std::function
<void(Instruction
*)> Callback
)
77 : Callback(std::move(Callback
)) {}
80 void InsertHelper(Instruction
*I
, const Twine
&Name
,
81 BasicBlock
*BB
, BasicBlock::iterator InsertPt
) const {
82 IRBuilderDefaultInserter::InsertHelper(I
, Name
, BB
, InsertPt
);
87 /// Common base class shared among various IRBuilders.
89 DebugLoc CurDbgLocation
;
93 BasicBlock::iterator InsertPt
;
96 MDNode
*DefaultFPMathTag
;
99 ArrayRef
<OperandBundleDef
> DefaultOperandBundles
;
102 IRBuilderBase(LLVMContext
&context
, MDNode
*FPMathTag
= nullptr,
103 ArrayRef
<OperandBundleDef
> OpBundles
= None
)
104 : Context(context
), DefaultFPMathTag(FPMathTag
),
105 DefaultOperandBundles(OpBundles
) {
106 ClearInsertionPoint();
109 //===--------------------------------------------------------------------===//
110 // Builder configuration methods
111 //===--------------------------------------------------------------------===//
113 /// Clear the insertion point: created instructions will not be
114 /// inserted into a block.
115 void ClearInsertionPoint() {
117 InsertPt
= BasicBlock::iterator();
120 BasicBlock
*GetInsertBlock() const { return BB
; }
121 BasicBlock::iterator
GetInsertPoint() const { return InsertPt
; }
122 LLVMContext
&getContext() const { return Context
; }
124 /// This specifies that created instructions should be appended to the
125 /// end of the specified block.
126 void SetInsertPoint(BasicBlock
*TheBB
) {
128 InsertPt
= BB
->end();
131 /// This specifies that created instructions should be inserted before
132 /// the specified instruction.
133 void SetInsertPoint(Instruction
*I
) {
135 InsertPt
= I
->getIterator();
136 assert(InsertPt
!= BB
->end() && "Can't read debug loc from end()");
137 SetCurrentDebugLocation(I
->getDebugLoc());
140 /// This specifies that created instructions should be inserted at the
142 void SetInsertPoint(BasicBlock
*TheBB
, BasicBlock::iterator IP
) {
145 if (IP
!= TheBB
->end())
146 SetCurrentDebugLocation(IP
->getDebugLoc());
149 /// Set location information used by debugging information.
150 void SetCurrentDebugLocation(DebugLoc L
) { CurDbgLocation
= std::move(L
); }
152 /// Get location information used by debugging information.
153 const DebugLoc
&getCurrentDebugLocation() const { return CurDbgLocation
; }
155 /// If this builder has a current debug location, set it on the
156 /// specified instruction.
157 void SetInstDebugLocation(Instruction
*I
) const {
159 I
->setDebugLoc(CurDbgLocation
);
162 /// Get the return type of the current function that we're emitting
164 Type
*getCurrentFunctionReturnType() const;
166 /// InsertPoint - A saved insertion point.
168 BasicBlock
*Block
= nullptr;
169 BasicBlock::iterator Point
;
172 /// Creates a new insertion point which doesn't point to anything.
173 InsertPoint() = default;
175 /// Creates a new insertion point at the given location.
176 InsertPoint(BasicBlock
*InsertBlock
, BasicBlock::iterator InsertPoint
)
177 : Block(InsertBlock
), Point(InsertPoint
) {}
179 /// Returns true if this insert point is set.
180 bool isSet() const { return (Block
!= nullptr); }
182 BasicBlock
*getBlock() const { return Block
; }
183 BasicBlock::iterator
getPoint() const { return Point
; }
186 /// Returns the current insert point.
187 InsertPoint
saveIP() const {
188 return InsertPoint(GetInsertBlock(), GetInsertPoint());
191 /// Returns the current insert point, clearing it in the process.
192 InsertPoint
saveAndClearIP() {
193 InsertPoint
IP(GetInsertBlock(), GetInsertPoint());
194 ClearInsertionPoint();
198 /// Sets the current insert point to a previously-saved location.
199 void restoreIP(InsertPoint IP
) {
201 SetInsertPoint(IP
.getBlock(), IP
.getPoint());
203 ClearInsertionPoint();
206 /// Get the floating point math metadata being used.
207 MDNode
*getDefaultFPMathTag() const { return DefaultFPMathTag
; }
209 /// Get the flags to be applied to created floating point ops
210 FastMathFlags
getFastMathFlags() const { return FMF
; }
212 /// Clear the fast-math flags.
213 void clearFastMathFlags() { FMF
.clear(); }
215 /// Set the floating point math metadata to be used.
216 void setDefaultFPMathTag(MDNode
*FPMathTag
) { DefaultFPMathTag
= FPMathTag
; }
218 /// Set the fast-math flags to be used with generated fp-math operators
219 void setFastMathFlags(FastMathFlags NewFMF
) { FMF
= NewFMF
; }
221 //===--------------------------------------------------------------------===//
223 //===--------------------------------------------------------------------===//
225 // RAII object that stores the current insertion point and restores it
226 // when the object is destroyed. This includes the debug location.
227 class InsertPointGuard
{
228 IRBuilderBase
&Builder
;
229 AssertingVH
<BasicBlock
> Block
;
230 BasicBlock::iterator Point
;
234 InsertPointGuard(IRBuilderBase
&B
)
235 : Builder(B
), Block(B
.GetInsertBlock()), Point(B
.GetInsertPoint()),
236 DbgLoc(B
.getCurrentDebugLocation()) {}
238 InsertPointGuard(const InsertPointGuard
&) = delete;
239 InsertPointGuard
&operator=(const InsertPointGuard
&) = delete;
241 ~InsertPointGuard() {
242 Builder
.restoreIP(InsertPoint(Block
, Point
));
243 Builder
.SetCurrentDebugLocation(DbgLoc
);
247 // RAII object that stores the current fast math settings and restores
248 // them when the object is destroyed.
249 class FastMathFlagGuard
{
250 IRBuilderBase
&Builder
;
255 FastMathFlagGuard(IRBuilderBase
&B
)
256 : Builder(B
), FMF(B
.FMF
), FPMathTag(B
.DefaultFPMathTag
) {}
258 FastMathFlagGuard(const FastMathFlagGuard
&) = delete;
259 FastMathFlagGuard
&operator=(const FastMathFlagGuard
&) = delete;
261 ~FastMathFlagGuard() {
263 Builder
.DefaultFPMathTag
= FPMathTag
;
267 //===--------------------------------------------------------------------===//
268 // Miscellaneous creation methods.
269 //===--------------------------------------------------------------------===//
271 /// Make a new global variable with initializer type i8*
273 /// Make a new global variable with an initializer that has array of i8 type
274 /// filled in with the null terminated string value specified. The new global
275 /// variable will be marked mergable with any others of the same contents. If
276 /// Name is specified, it is the name of the global variable created.
277 GlobalVariable
*CreateGlobalString(StringRef Str
, const Twine
&Name
= "",
278 unsigned AddressSpace
= 0);
280 /// Get a constant value representing either true or false.
281 ConstantInt
*getInt1(bool V
) {
282 return ConstantInt::get(getInt1Ty(), V
);
285 /// Get the constant value for i1 true.
286 ConstantInt
*getTrue() {
287 return ConstantInt::getTrue(Context
);
290 /// Get the constant value for i1 false.
291 ConstantInt
*getFalse() {
292 return ConstantInt::getFalse(Context
);
295 /// Get a constant 8-bit value.
296 ConstantInt
*getInt8(uint8_t C
) {
297 return ConstantInt::get(getInt8Ty(), C
);
300 /// Get a constant 16-bit value.
301 ConstantInt
*getInt16(uint16_t C
) {
302 return ConstantInt::get(getInt16Ty(), C
);
305 /// Get a constant 32-bit value.
306 ConstantInt
*getInt32(uint32_t C
) {
307 return ConstantInt::get(getInt32Ty(), C
);
310 /// Get a constant 64-bit value.
311 ConstantInt
*getInt64(uint64_t C
) {
312 return ConstantInt::get(getInt64Ty(), C
);
315 /// Get a constant N-bit value, zero extended or truncated from
317 ConstantInt
*getIntN(unsigned N
, uint64_t C
) {
318 return ConstantInt::get(getIntNTy(N
), C
);
321 /// Get a constant integer value.
322 ConstantInt
*getInt(const APInt
&AI
) {
323 return ConstantInt::get(Context
, AI
);
326 //===--------------------------------------------------------------------===//
327 // Type creation methods
328 //===--------------------------------------------------------------------===//
330 /// Fetch the type representing a single bit
331 IntegerType
*getInt1Ty() {
332 return Type::getInt1Ty(Context
);
335 /// Fetch the type representing an 8-bit integer.
336 IntegerType
*getInt8Ty() {
337 return Type::getInt8Ty(Context
);
340 /// Fetch the type representing a 16-bit integer.
341 IntegerType
*getInt16Ty() {
342 return Type::getInt16Ty(Context
);
345 /// Fetch the type representing a 32-bit integer.
346 IntegerType
*getInt32Ty() {
347 return Type::getInt32Ty(Context
);
350 /// Fetch the type representing a 64-bit integer.
351 IntegerType
*getInt64Ty() {
352 return Type::getInt64Ty(Context
);
355 /// Fetch the type representing a 128-bit integer.
356 IntegerType
*getInt128Ty() { return Type::getInt128Ty(Context
); }
358 /// Fetch the type representing an N-bit integer.
359 IntegerType
*getIntNTy(unsigned N
) {
360 return Type::getIntNTy(Context
, N
);
363 /// Fetch the type representing a 16-bit floating point value.
365 return Type::getHalfTy(Context
);
368 /// Fetch the type representing a 32-bit floating point value.
370 return Type::getFloatTy(Context
);
373 /// Fetch the type representing a 64-bit floating point value.
374 Type
*getDoubleTy() {
375 return Type::getDoubleTy(Context
);
378 /// Fetch the type representing void.
380 return Type::getVoidTy(Context
);
383 /// Fetch the type representing a pointer to an 8-bit integer value.
384 PointerType
*getInt8PtrTy(unsigned AddrSpace
= 0) {
385 return Type::getInt8PtrTy(Context
, AddrSpace
);
388 /// Fetch the type representing a pointer to an integer value.
389 IntegerType
*getIntPtrTy(const DataLayout
&DL
, unsigned AddrSpace
= 0) {
390 return DL
.getIntPtrType(Context
, AddrSpace
);
393 //===--------------------------------------------------------------------===//
394 // Intrinsic creation methods
395 //===--------------------------------------------------------------------===//
397 /// Create and insert a memset to the specified pointer and the
400 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
401 /// specified, it will be added to the instruction. Likewise with alias.scope
402 /// and noalias tags.
403 CallInst
*CreateMemSet(Value
*Ptr
, Value
*Val
, uint64_t Size
, unsigned Align
,
404 bool isVolatile
= false, MDNode
*TBAATag
= nullptr,
405 MDNode
*ScopeTag
= nullptr,
406 MDNode
*NoAliasTag
= nullptr) {
407 return CreateMemSet(Ptr
, Val
, getInt64(Size
), Align
, isVolatile
,
408 TBAATag
, ScopeTag
, NoAliasTag
);
411 CallInst
*CreateMemSet(Value
*Ptr
, Value
*Val
, Value
*Size
, unsigned Align
,
412 bool isVolatile
= false, MDNode
*TBAATag
= nullptr,
413 MDNode
*ScopeTag
= nullptr,
414 MDNode
*NoAliasTag
= nullptr);
416 /// Create and insert an element unordered-atomic memset of the region of
417 /// memory starting at the given pointer to the given value.
419 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
420 /// specified, it will be added to the instruction. Likewise with alias.scope
421 /// and noalias tags.
422 CallInst
*CreateElementUnorderedAtomicMemSet(Value
*Ptr
, Value
*Val
,
423 uint64_t Size
, unsigned Align
,
424 uint32_t ElementSize
,
425 MDNode
*TBAATag
= nullptr,
426 MDNode
*ScopeTag
= nullptr,
427 MDNode
*NoAliasTag
= nullptr) {
428 return CreateElementUnorderedAtomicMemSet(Ptr
, Val
, getInt64(Size
), Align
,
429 ElementSize
, TBAATag
, ScopeTag
,
433 CallInst
*CreateElementUnorderedAtomicMemSet(Value
*Ptr
, Value
*Val
,
434 Value
*Size
, unsigned Align
,
435 uint32_t ElementSize
,
436 MDNode
*TBAATag
= nullptr,
437 MDNode
*ScopeTag
= nullptr,
438 MDNode
*NoAliasTag
= nullptr);
440 /// Create and insert a memcpy between the specified pointers.
442 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
443 /// specified, it will be added to the instruction. Likewise with alias.scope
444 /// and noalias tags.
445 CallInst
*CreateMemCpy(Value
*Dst
, unsigned DstAlign
, Value
*Src
,
446 unsigned SrcAlign
, uint64_t Size
,
447 bool isVolatile
= false, MDNode
*TBAATag
= nullptr,
448 MDNode
*TBAAStructTag
= nullptr,
449 MDNode
*ScopeTag
= nullptr,
450 MDNode
*NoAliasTag
= nullptr) {
451 return CreateMemCpy(Dst
, DstAlign
, Src
, SrcAlign
, getInt64(Size
),
452 isVolatile
, TBAATag
, TBAAStructTag
, ScopeTag
,
456 CallInst
*CreateMemCpy(Value
*Dst
, unsigned DstAlign
, Value
*Src
,
457 unsigned SrcAlign
, Value
*Size
,
458 bool isVolatile
= false, MDNode
*TBAATag
= nullptr,
459 MDNode
*TBAAStructTag
= nullptr,
460 MDNode
*ScopeTag
= nullptr,
461 MDNode
*NoAliasTag
= nullptr);
463 /// Create and insert an element unordered-atomic memcpy between the
464 /// specified pointers.
466 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
468 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
469 /// specified, it will be added to the instruction. Likewise with alias.scope
470 /// and noalias tags.
471 CallInst
*CreateElementUnorderedAtomicMemCpy(
472 Value
*Dst
, unsigned DstAlign
, Value
*Src
, unsigned SrcAlign
,
473 uint64_t Size
, uint32_t ElementSize
, MDNode
*TBAATag
= nullptr,
474 MDNode
*TBAAStructTag
= nullptr, MDNode
*ScopeTag
= nullptr,
475 MDNode
*NoAliasTag
= nullptr) {
476 return CreateElementUnorderedAtomicMemCpy(
477 Dst
, DstAlign
, Src
, SrcAlign
, getInt64(Size
), ElementSize
, TBAATag
,
478 TBAAStructTag
, ScopeTag
, NoAliasTag
);
481 CallInst
*CreateElementUnorderedAtomicMemCpy(
482 Value
*Dst
, unsigned DstAlign
, Value
*Src
, unsigned SrcAlign
, Value
*Size
,
483 uint32_t ElementSize
, MDNode
*TBAATag
= nullptr,
484 MDNode
*TBAAStructTag
= nullptr, MDNode
*ScopeTag
= nullptr,
485 MDNode
*NoAliasTag
= nullptr);
487 /// Create and insert a memmove between the specified
490 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
491 /// specified, it will be added to the instruction. Likewise with alias.scope
492 /// and noalias tags.
493 CallInst
*CreateMemMove(Value
*Dst
, unsigned DstAlign
, Value
*Src
, unsigned SrcAlign
,
494 uint64_t Size
, bool isVolatile
= false,
495 MDNode
*TBAATag
= nullptr, MDNode
*ScopeTag
= nullptr,
496 MDNode
*NoAliasTag
= nullptr) {
497 return CreateMemMove(Dst
, DstAlign
, Src
, SrcAlign
, getInt64(Size
), isVolatile
,
498 TBAATag
, ScopeTag
, NoAliasTag
);
501 CallInst
*CreateMemMove(Value
*Dst
, unsigned DstAlign
, Value
*Src
, unsigned SrcAlign
,
502 Value
*Size
, bool isVolatile
= false, MDNode
*TBAATag
= nullptr,
503 MDNode
*ScopeTag
= nullptr,
504 MDNode
*NoAliasTag
= nullptr);
506 /// \brief Create and insert an element unordered-atomic memmove between the
507 /// specified pointers.
509 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
512 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
513 /// specified, it will be added to the instruction. Likewise with alias.scope
514 /// and noalias tags.
515 CallInst
*CreateElementUnorderedAtomicMemMove(
516 Value
*Dst
, unsigned DstAlign
, Value
*Src
, unsigned SrcAlign
,
517 uint64_t Size
, uint32_t ElementSize
, MDNode
*TBAATag
= nullptr,
518 MDNode
*TBAAStructTag
= nullptr, MDNode
*ScopeTag
= nullptr,
519 MDNode
*NoAliasTag
= nullptr) {
520 return CreateElementUnorderedAtomicMemMove(
521 Dst
, DstAlign
, Src
, SrcAlign
, getInt64(Size
), ElementSize
, TBAATag
,
522 TBAAStructTag
, ScopeTag
, NoAliasTag
);
525 CallInst
*CreateElementUnorderedAtomicMemMove(
526 Value
*Dst
, unsigned DstAlign
, Value
*Src
, unsigned SrcAlign
, Value
*Size
,
527 uint32_t ElementSize
, MDNode
*TBAATag
= nullptr,
528 MDNode
*TBAAStructTag
= nullptr, MDNode
*ScopeTag
= nullptr,
529 MDNode
*NoAliasTag
= nullptr);
531 /// Create a vector fadd reduction intrinsic of the source vector.
532 /// The first parameter is a scalar accumulator value for ordered reductions.
533 CallInst
*CreateFAddReduce(Value
*Acc
, Value
*Src
);
535 /// Create a vector fmul reduction intrinsic of the source vector.
536 /// The first parameter is a scalar accumulator value for ordered reductions.
537 CallInst
*CreateFMulReduce(Value
*Acc
, Value
*Src
);
539 /// Create a vector int add reduction intrinsic of the source vector.
540 CallInst
*CreateAddReduce(Value
*Src
);
542 /// Create a vector int mul reduction intrinsic of the source vector.
543 CallInst
*CreateMulReduce(Value
*Src
);
545 /// Create a vector int AND reduction intrinsic of the source vector.
546 CallInst
*CreateAndReduce(Value
*Src
);
548 /// Create a vector int OR reduction intrinsic of the source vector.
549 CallInst
*CreateOrReduce(Value
*Src
);
551 /// Create a vector int XOR reduction intrinsic of the source vector.
552 CallInst
*CreateXorReduce(Value
*Src
);
554 /// Create a vector integer max reduction intrinsic of the source
556 CallInst
*CreateIntMaxReduce(Value
*Src
, bool IsSigned
= false);
558 /// Create a vector integer min reduction intrinsic of the source
560 CallInst
*CreateIntMinReduce(Value
*Src
, bool IsSigned
= false);
562 /// Create a vector float max reduction intrinsic of the source
564 CallInst
*CreateFPMaxReduce(Value
*Src
, bool NoNaN
= false);
566 /// Create a vector float min reduction intrinsic of the source
568 CallInst
*CreateFPMinReduce(Value
*Src
, bool NoNaN
= false);
570 /// Create a lifetime.start intrinsic.
572 /// If the pointer isn't i8* it will be converted.
573 CallInst
*CreateLifetimeStart(Value
*Ptr
, ConstantInt
*Size
= nullptr);
575 /// Create a lifetime.end intrinsic.
577 /// If the pointer isn't i8* it will be converted.
578 CallInst
*CreateLifetimeEnd(Value
*Ptr
, ConstantInt
*Size
= nullptr);
580 /// Create a call to invariant.start intrinsic.
582 /// If the pointer isn't i8* it will be converted.
583 CallInst
*CreateInvariantStart(Value
*Ptr
, ConstantInt
*Size
= nullptr);
585 /// Create a call to Masked Load intrinsic
586 CallInst
*CreateMaskedLoad(Value
*Ptr
, unsigned Align
, Value
*Mask
,
587 Value
*PassThru
= nullptr, const Twine
&Name
= "");
589 /// Create a call to Masked Store intrinsic
590 CallInst
*CreateMaskedStore(Value
*Val
, Value
*Ptr
, unsigned Align
,
593 /// Create a call to Masked Gather intrinsic
594 CallInst
*CreateMaskedGather(Value
*Ptrs
, unsigned Align
,
595 Value
*Mask
= nullptr,
596 Value
*PassThru
= nullptr,
597 const Twine
& Name
= "");
599 /// Create a call to Masked Scatter intrinsic
600 CallInst
*CreateMaskedScatter(Value
*Val
, Value
*Ptrs
, unsigned Align
,
601 Value
*Mask
= nullptr);
603 /// Create an assume intrinsic call that allows the optimizer to
604 /// assume that the provided condition will be true.
605 CallInst
*CreateAssumption(Value
*Cond
);
607 /// Create a call to the experimental.gc.statepoint intrinsic to
608 /// start a new statepoint sequence.
609 CallInst
*CreateGCStatepointCall(uint64_t ID
, uint32_t NumPatchBytes
,
611 ArrayRef
<Value
*> CallArgs
,
612 ArrayRef
<Value
*> DeoptArgs
,
613 ArrayRef
<Value
*> GCArgs
,
614 const Twine
&Name
= "");
616 /// Create a call to the experimental.gc.statepoint intrinsic to
617 /// start a new statepoint sequence.
618 CallInst
*CreateGCStatepointCall(uint64_t ID
, uint32_t NumPatchBytes
,
619 Value
*ActualCallee
, uint32_t Flags
,
620 ArrayRef
<Use
> CallArgs
,
621 ArrayRef
<Use
> TransitionArgs
,
622 ArrayRef
<Use
> DeoptArgs
,
623 ArrayRef
<Value
*> GCArgs
,
624 const Twine
&Name
= "");
626 /// Conveninence function for the common case when CallArgs are filled
627 /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
628 /// .get()'ed to get the Value pointer.
629 CallInst
*CreateGCStatepointCall(uint64_t ID
, uint32_t NumPatchBytes
,
630 Value
*ActualCallee
, ArrayRef
<Use
> CallArgs
,
631 ArrayRef
<Value
*> DeoptArgs
,
632 ArrayRef
<Value
*> GCArgs
,
633 const Twine
&Name
= "");
635 /// Create an invoke to the experimental.gc.statepoint intrinsic to
636 /// start a new statepoint sequence.
638 CreateGCStatepointInvoke(uint64_t ID
, uint32_t NumPatchBytes
,
639 Value
*ActualInvokee
, BasicBlock
*NormalDest
,
640 BasicBlock
*UnwindDest
, ArrayRef
<Value
*> InvokeArgs
,
641 ArrayRef
<Value
*> DeoptArgs
,
642 ArrayRef
<Value
*> GCArgs
, const Twine
&Name
= "");
644 /// Create an invoke to the experimental.gc.statepoint intrinsic to
645 /// start a new statepoint sequence.
646 InvokeInst
*CreateGCStatepointInvoke(
647 uint64_t ID
, uint32_t NumPatchBytes
, Value
*ActualInvokee
,
648 BasicBlock
*NormalDest
, BasicBlock
*UnwindDest
, uint32_t Flags
,
649 ArrayRef
<Use
> InvokeArgs
, ArrayRef
<Use
> TransitionArgs
,
650 ArrayRef
<Use
> DeoptArgs
, ArrayRef
<Value
*> GCArgs
,
651 const Twine
&Name
= "");
653 // Convenience function for the common case when CallArgs are filled in using
654 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
657 CreateGCStatepointInvoke(uint64_t ID
, uint32_t NumPatchBytes
,
658 Value
*ActualInvokee
, BasicBlock
*NormalDest
,
659 BasicBlock
*UnwindDest
, ArrayRef
<Use
> InvokeArgs
,
660 ArrayRef
<Value
*> DeoptArgs
,
661 ArrayRef
<Value
*> GCArgs
, const Twine
&Name
= "");
663 /// Create a call to the experimental.gc.result intrinsic to extract
664 /// the result from a call wrapped in a statepoint.
665 CallInst
*CreateGCResult(Instruction
*Statepoint
,
667 const Twine
&Name
= "");
669 /// Create a call to the experimental.gc.relocate intrinsics to
670 /// project the relocated value of one pointer from the statepoint.
671 CallInst
*CreateGCRelocate(Instruction
*Statepoint
,
675 const Twine
&Name
= "");
677 /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
679 CallInst
*CreateUnaryIntrinsic(Intrinsic::ID ID
, Value
*V
,
680 Instruction
*FMFSource
= nullptr,
681 const Twine
&Name
= "");
683 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
685 CallInst
*CreateBinaryIntrinsic(Intrinsic::ID ID
, Value
*LHS
, Value
*RHS
,
686 Instruction
*FMFSource
= nullptr,
687 const Twine
&Name
= "");
689 /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If
690 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
692 CallInst
*CreateIntrinsic(Intrinsic::ID ID
, ArrayRef
<Type
*> Types
,
693 ArrayRef
<Value
*> Args
,
694 Instruction
*FMFSource
= nullptr,
695 const Twine
&Name
= "");
697 /// Create call to the minnum intrinsic.
698 CallInst
*CreateMinNum(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
699 return CreateBinaryIntrinsic(Intrinsic::minnum
, LHS
, RHS
, nullptr, Name
);
702 /// Create call to the maxnum intrinsic.
703 CallInst
*CreateMaxNum(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
704 return CreateBinaryIntrinsic(Intrinsic::maxnum
, LHS
, RHS
, nullptr, Name
);
707 /// Create call to the minimum intrinsic.
708 CallInst
*CreateMinimum(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
709 return CreateBinaryIntrinsic(Intrinsic::minimum
, LHS
, RHS
, nullptr, Name
);
712 /// Create call to the maximum intrinsic.
713 CallInst
*CreateMaximum(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
714 return CreateBinaryIntrinsic(Intrinsic::maximum
, LHS
, RHS
, nullptr, Name
);
718 /// Create a call to a masked intrinsic with given Id.
719 CallInst
*CreateMaskedIntrinsic(Intrinsic::ID Id
, ArrayRef
<Value
*> Ops
,
720 ArrayRef
<Type
*> OverloadedTypes
,
721 const Twine
&Name
= "");
723 Value
*getCastedInt8PtrValue(Value
*Ptr
);
726 /// This provides a uniform API for creating instructions and inserting
727 /// them into a basic block: either at the end of a BasicBlock, or at a specific
728 /// iterator location in a block.
730 /// Note that the builder does not expose the full generality of LLVM
731 /// instructions. For access to extra instruction properties, use the mutators
732 /// (e.g. setVolatile) on the instructions after they have been
733 /// created. Convenience state exists to specify fast-math flags and fp-math
736 /// The first template argument specifies a class to use for creating constants.
737 /// This defaults to creating minimally folded constants. The second template
738 /// argument allows clients to specify custom insertion hooks that are called on
739 /// every newly created insertion.
740 template <typename T
= ConstantFolder
,
741 typename Inserter
= IRBuilderDefaultInserter
>
742 class IRBuilder
: public IRBuilderBase
, public Inserter
{
746 IRBuilder(LLVMContext
&C
, const T
&F
, Inserter I
= Inserter(),
747 MDNode
*FPMathTag
= nullptr,
748 ArrayRef
<OperandBundleDef
> OpBundles
= None
)
749 : IRBuilderBase(C
, FPMathTag
, OpBundles
), Inserter(std::move(I
)),
752 explicit IRBuilder(LLVMContext
&C
, MDNode
*FPMathTag
= nullptr,
753 ArrayRef
<OperandBundleDef
> OpBundles
= None
)
754 : IRBuilderBase(C
, FPMathTag
, OpBundles
) {}
756 explicit IRBuilder(BasicBlock
*TheBB
, const T
&F
, MDNode
*FPMathTag
= nullptr,
757 ArrayRef
<OperandBundleDef
> OpBundles
= None
)
758 : IRBuilderBase(TheBB
->getContext(), FPMathTag
, OpBundles
), Folder(F
) {
759 SetInsertPoint(TheBB
);
762 explicit IRBuilder(BasicBlock
*TheBB
, MDNode
*FPMathTag
= nullptr,
763 ArrayRef
<OperandBundleDef
> OpBundles
= None
)
764 : IRBuilderBase(TheBB
->getContext(), FPMathTag
, OpBundles
) {
765 SetInsertPoint(TheBB
);
768 explicit IRBuilder(Instruction
*IP
, MDNode
*FPMathTag
= nullptr,
769 ArrayRef
<OperandBundleDef
> OpBundles
= None
)
770 : IRBuilderBase(IP
->getContext(), FPMathTag
, OpBundles
) {
774 IRBuilder(BasicBlock
*TheBB
, BasicBlock::iterator IP
, const T
&F
,
775 MDNode
*FPMathTag
= nullptr,
776 ArrayRef
<OperandBundleDef
> OpBundles
= None
)
777 : IRBuilderBase(TheBB
->getContext(), FPMathTag
, OpBundles
), Folder(F
) {
778 SetInsertPoint(TheBB
, IP
);
781 IRBuilder(BasicBlock
*TheBB
, BasicBlock::iterator IP
,
782 MDNode
*FPMathTag
= nullptr,
783 ArrayRef
<OperandBundleDef
> OpBundles
= None
)
784 : IRBuilderBase(TheBB
->getContext(), FPMathTag
, OpBundles
) {
785 SetInsertPoint(TheBB
, IP
);
788 /// Get the constant folder being used.
789 const T
&getFolder() { return Folder
; }
791 /// Insert and return the specified instruction.
792 template<typename InstTy
>
793 InstTy
*Insert(InstTy
*I
, const Twine
&Name
= "") const {
794 this->InsertHelper(I
, Name
, BB
, InsertPt
);
795 this->SetInstDebugLocation(I
);
799 /// No-op overload to handle constants.
800 Constant
*Insert(Constant
*C
, const Twine
& = "") const {
804 //===--------------------------------------------------------------------===//
805 // Instruction creation methods: Terminators
806 //===--------------------------------------------------------------------===//
809 /// Helper to add branch weight and unpredictable metadata onto an
811 /// \returns The annotated instruction.
812 template <typename InstTy
>
813 InstTy
*addBranchMetadata(InstTy
*I
, MDNode
*Weights
, MDNode
*Unpredictable
) {
815 I
->setMetadata(LLVMContext::MD_prof
, Weights
);
817 I
->setMetadata(LLVMContext::MD_unpredictable
, Unpredictable
);
822 /// Create a 'ret void' instruction.
823 ReturnInst
*CreateRetVoid() {
824 return Insert(ReturnInst::Create(Context
));
827 /// Create a 'ret <val>' instruction.
828 ReturnInst
*CreateRet(Value
*V
) {
829 return Insert(ReturnInst::Create(Context
, V
));
832 /// Create a sequence of N insertvalue instructions,
833 /// with one Value from the retVals array each, that build a aggregate
834 /// return value one value at a time, and a ret instruction to return
835 /// the resulting aggregate value.
837 /// This is a convenience function for code that uses aggregate return values
838 /// as a vehicle for having multiple return values.
839 ReturnInst
*CreateAggregateRet(Value
*const *retVals
, unsigned N
) {
840 Value
*V
= UndefValue::get(getCurrentFunctionReturnType());
841 for (unsigned i
= 0; i
!= N
; ++i
)
842 V
= CreateInsertValue(V
, retVals
[i
], i
, "mrv");
843 return Insert(ReturnInst::Create(Context
, V
));
846 /// Create an unconditional 'br label X' instruction.
847 BranchInst
*CreateBr(BasicBlock
*Dest
) {
848 return Insert(BranchInst::Create(Dest
));
851 /// Create a conditional 'br Cond, TrueDest, FalseDest'
853 BranchInst
*CreateCondBr(Value
*Cond
, BasicBlock
*True
, BasicBlock
*False
,
854 MDNode
*BranchWeights
= nullptr,
855 MDNode
*Unpredictable
= nullptr) {
856 return Insert(addBranchMetadata(BranchInst::Create(True
, False
, Cond
),
857 BranchWeights
, Unpredictable
));
860 /// Create a conditional 'br Cond, TrueDest, FalseDest'
861 /// instruction. Copy branch meta data if available.
862 BranchInst
*CreateCondBr(Value
*Cond
, BasicBlock
*True
, BasicBlock
*False
,
863 Instruction
*MDSrc
) {
864 BranchInst
*Br
= BranchInst::Create(True
, False
, Cond
);
866 unsigned WL
[4] = {LLVMContext::MD_prof
, LLVMContext::MD_unpredictable
,
867 LLVMContext::MD_make_implicit
, LLVMContext::MD_dbg
};
868 Br
->copyMetadata(*MDSrc
, makeArrayRef(&WL
[0], 4));
873 /// Create a switch instruction with the specified value, default dest,
874 /// and with a hint for the number of cases that will be added (for efficient
876 SwitchInst
*CreateSwitch(Value
*V
, BasicBlock
*Dest
, unsigned NumCases
= 10,
877 MDNode
*BranchWeights
= nullptr,
878 MDNode
*Unpredictable
= nullptr) {
879 return Insert(addBranchMetadata(SwitchInst::Create(V
, Dest
, NumCases
),
880 BranchWeights
, Unpredictable
));
883 /// Create an indirect branch instruction with the specified address
884 /// operand, with an optional hint for the number of destinations that will be
885 /// added (for efficient allocation).
886 IndirectBrInst
*CreateIndirectBr(Value
*Addr
, unsigned NumDests
= 10) {
887 return Insert(IndirectBrInst::Create(Addr
, NumDests
));
890 /// Create an invoke instruction.
891 InvokeInst
*CreateInvoke(FunctionType
*Ty
, Value
*Callee
,
892 BasicBlock
*NormalDest
, BasicBlock
*UnwindDest
,
893 ArrayRef
<Value
*> Args
,
894 ArrayRef
<OperandBundleDef
> OpBundles
,
895 const Twine
&Name
= "") {
897 InvokeInst::Create(Ty
, Callee
, NormalDest
, UnwindDest
, Args
, OpBundles
),
900 InvokeInst
*CreateInvoke(FunctionType
*Ty
, Value
*Callee
,
901 BasicBlock
*NormalDest
, BasicBlock
*UnwindDest
,
902 ArrayRef
<Value
*> Args
= None
,
903 const Twine
&Name
= "") {
904 return Insert(InvokeInst::Create(Ty
, Callee
, NormalDest
, UnwindDest
, Args
),
908 InvokeInst
*CreateInvoke(FunctionCallee Callee
, BasicBlock
*NormalDest
,
909 BasicBlock
*UnwindDest
, ArrayRef
<Value
*> Args
,
910 ArrayRef
<OperandBundleDef
> OpBundles
,
911 const Twine
&Name
= "") {
912 return CreateInvoke(Callee
.getFunctionType(), Callee
.getCallee(),
913 NormalDest
, UnwindDest
, Args
, OpBundles
, Name
);
916 InvokeInst
*CreateInvoke(FunctionCallee Callee
, BasicBlock
*NormalDest
,
917 BasicBlock
*UnwindDest
,
918 ArrayRef
<Value
*> Args
= None
,
919 const Twine
&Name
= "") {
920 return CreateInvoke(Callee
.getFunctionType(), Callee
.getCallee(),
921 NormalDest
, UnwindDest
, Args
, Name
);
924 // Deprecated [opaque pointer types]
925 InvokeInst
*CreateInvoke(Value
*Callee
, BasicBlock
*NormalDest
,
926 BasicBlock
*UnwindDest
, ArrayRef
<Value
*> Args
,
927 ArrayRef
<OperandBundleDef
> OpBundles
,
928 const Twine
&Name
= "") {
931 cast
<PointerType
>(Callee
->getType())->getElementType()),
932 Callee
, NormalDest
, UnwindDest
, Args
, OpBundles
, Name
);
935 // Deprecated [opaque pointer types]
936 InvokeInst
*CreateInvoke(Value
*Callee
, BasicBlock
*NormalDest
,
937 BasicBlock
*UnwindDest
,
938 ArrayRef
<Value
*> Args
= None
,
939 const Twine
&Name
= "") {
942 cast
<PointerType
>(Callee
->getType())->getElementType()),
943 Callee
, NormalDest
, UnwindDest
, Args
, Name
);
946 /// \brief Create a callbr instruction.
947 CallBrInst
*CreateCallBr(FunctionType
*Ty
, Value
*Callee
,
948 BasicBlock
*DefaultDest
,
949 ArrayRef
<BasicBlock
*> IndirectDests
,
950 ArrayRef
<Value
*> Args
= None
,
951 const Twine
&Name
= "") {
952 return Insert(CallBrInst::Create(Ty
, Callee
, DefaultDest
, IndirectDests
,
955 CallBrInst
*CreateCallBr(FunctionType
*Ty
, Value
*Callee
,
956 BasicBlock
*DefaultDest
,
957 ArrayRef
<BasicBlock
*> IndirectDests
,
958 ArrayRef
<Value
*> Args
,
959 ArrayRef
<OperandBundleDef
> OpBundles
,
960 const Twine
&Name
= "") {
962 CallBrInst::Create(Ty
, Callee
, DefaultDest
, IndirectDests
, Args
,
966 CallBrInst
*CreateCallBr(FunctionCallee Callee
, BasicBlock
*DefaultDest
,
967 ArrayRef
<BasicBlock
*> IndirectDests
,
968 ArrayRef
<Value
*> Args
= None
,
969 const Twine
&Name
= "") {
970 return CreateCallBr(Callee
.getFunctionType(), Callee
.getCallee(),
971 DefaultDest
, IndirectDests
, Args
, Name
);
973 CallBrInst
*CreateCallBr(FunctionCallee Callee
, BasicBlock
*DefaultDest
,
974 ArrayRef
<BasicBlock
*> IndirectDests
,
975 ArrayRef
<Value
*> Args
,
976 ArrayRef
<OperandBundleDef
> OpBundles
,
977 const Twine
&Name
= "") {
978 return CreateCallBr(Callee
.getFunctionType(), Callee
.getCallee(),
979 DefaultDest
, IndirectDests
, Args
, Name
);
982 ResumeInst
*CreateResume(Value
*Exn
) {
983 return Insert(ResumeInst::Create(Exn
));
986 CleanupReturnInst
*CreateCleanupRet(CleanupPadInst
*CleanupPad
,
987 BasicBlock
*UnwindBB
= nullptr) {
988 return Insert(CleanupReturnInst::Create(CleanupPad
, UnwindBB
));
991 CatchSwitchInst
*CreateCatchSwitch(Value
*ParentPad
, BasicBlock
*UnwindBB
,
992 unsigned NumHandlers
,
993 const Twine
&Name
= "") {
994 return Insert(CatchSwitchInst::Create(ParentPad
, UnwindBB
, NumHandlers
),
998 CatchPadInst
*CreateCatchPad(Value
*ParentPad
, ArrayRef
<Value
*> Args
,
999 const Twine
&Name
= "") {
1000 return Insert(CatchPadInst::Create(ParentPad
, Args
), Name
);
1003 CleanupPadInst
*CreateCleanupPad(Value
*ParentPad
,
1004 ArrayRef
<Value
*> Args
= None
,
1005 const Twine
&Name
= "") {
1006 return Insert(CleanupPadInst::Create(ParentPad
, Args
), Name
);
1009 CatchReturnInst
*CreateCatchRet(CatchPadInst
*CatchPad
, BasicBlock
*BB
) {
1010 return Insert(CatchReturnInst::Create(CatchPad
, BB
));
1013 UnreachableInst
*CreateUnreachable() {
1014 return Insert(new UnreachableInst(Context
));
1017 //===--------------------------------------------------------------------===//
1018 // Instruction creation methods: Binary Operators
1019 //===--------------------------------------------------------------------===//
1021 BinaryOperator
*CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc
,
1022 Value
*LHS
, Value
*RHS
,
1024 bool HasNUW
, bool HasNSW
) {
1025 BinaryOperator
*BO
= Insert(BinaryOperator::Create(Opc
, LHS
, RHS
), Name
);
1026 if (HasNUW
) BO
->setHasNoUnsignedWrap();
1027 if (HasNSW
) BO
->setHasNoSignedWrap();
1031 Instruction
*setFPAttrs(Instruction
*I
, MDNode
*FPMD
,
1032 FastMathFlags FMF
) const {
1034 FPMD
= DefaultFPMathTag
;
1036 I
->setMetadata(LLVMContext::MD_fpmath
, FPMD
);
1037 I
->setFastMathFlags(FMF
);
1041 Value
*foldConstant(Instruction::BinaryOps Opc
, Value
*L
,
1042 Value
*R
, const Twine
&Name
) const {
1043 auto *LC
= dyn_cast
<Constant
>(L
);
1044 auto *RC
= dyn_cast
<Constant
>(R
);
1045 return (LC
&& RC
) ? Insert(Folder
.CreateBinOp(Opc
, LC
, RC
), Name
) : nullptr;
1049 Value
*CreateAdd(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1050 bool HasNUW
= false, bool HasNSW
= false) {
1051 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1052 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1053 return Insert(Folder
.CreateAdd(LC
, RC
, HasNUW
, HasNSW
), Name
);
1054 return CreateInsertNUWNSWBinOp(Instruction::Add
, LHS
, RHS
, Name
,
1058 Value
*CreateNSWAdd(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1059 return CreateAdd(LHS
, RHS
, Name
, false, true);
1062 Value
*CreateNUWAdd(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1063 return CreateAdd(LHS
, RHS
, Name
, true, false);
1066 Value
*CreateSub(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1067 bool HasNUW
= false, bool HasNSW
= false) {
1068 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1069 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1070 return Insert(Folder
.CreateSub(LC
, RC
, HasNUW
, HasNSW
), Name
);
1071 return CreateInsertNUWNSWBinOp(Instruction::Sub
, LHS
, RHS
, Name
,
1075 Value
*CreateNSWSub(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1076 return CreateSub(LHS
, RHS
, Name
, false, true);
1079 Value
*CreateNUWSub(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1080 return CreateSub(LHS
, RHS
, Name
, true, false);
1083 Value
*CreateMul(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1084 bool HasNUW
= false, bool HasNSW
= false) {
1085 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1086 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1087 return Insert(Folder
.CreateMul(LC
, RC
, HasNUW
, HasNSW
), Name
);
1088 return CreateInsertNUWNSWBinOp(Instruction::Mul
, LHS
, RHS
, Name
,
1092 Value
*CreateNSWMul(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1093 return CreateMul(LHS
, RHS
, Name
, false, true);
1096 Value
*CreateNUWMul(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1097 return CreateMul(LHS
, RHS
, Name
, true, false);
1100 Value
*CreateUDiv(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1101 bool isExact
= false) {
1102 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1103 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1104 return Insert(Folder
.CreateUDiv(LC
, RC
, isExact
), Name
);
1106 return Insert(BinaryOperator::CreateUDiv(LHS
, RHS
), Name
);
1107 return Insert(BinaryOperator::CreateExactUDiv(LHS
, RHS
), Name
);
1110 Value
*CreateExactUDiv(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1111 return CreateUDiv(LHS
, RHS
, Name
, true);
1114 Value
*CreateSDiv(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1115 bool isExact
= false) {
1116 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1117 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1118 return Insert(Folder
.CreateSDiv(LC
, RC
, isExact
), Name
);
1120 return Insert(BinaryOperator::CreateSDiv(LHS
, RHS
), Name
);
1121 return Insert(BinaryOperator::CreateExactSDiv(LHS
, RHS
), Name
);
1124 Value
*CreateExactSDiv(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1125 return CreateSDiv(LHS
, RHS
, Name
, true);
1128 Value
*CreateURem(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1129 if (Value
*V
= foldConstant(Instruction::URem
, LHS
, RHS
, Name
)) return V
;
1130 return Insert(BinaryOperator::CreateURem(LHS
, RHS
), Name
);
1133 Value
*CreateSRem(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1134 if (Value
*V
= foldConstant(Instruction::SRem
, LHS
, RHS
, Name
)) return V
;
1135 return Insert(BinaryOperator::CreateSRem(LHS
, RHS
), Name
);
1138 Value
*CreateShl(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1139 bool HasNUW
= false, bool HasNSW
= false) {
1140 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1141 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1142 return Insert(Folder
.CreateShl(LC
, RC
, HasNUW
, HasNSW
), Name
);
1143 return CreateInsertNUWNSWBinOp(Instruction::Shl
, LHS
, RHS
, Name
,
1147 Value
*CreateShl(Value
*LHS
, const APInt
&RHS
, const Twine
&Name
= "",
1148 bool HasNUW
= false, bool HasNSW
= false) {
1149 return CreateShl(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
,
1153 Value
*CreateShl(Value
*LHS
, uint64_t RHS
, const Twine
&Name
= "",
1154 bool HasNUW
= false, bool HasNSW
= false) {
1155 return CreateShl(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
,
1159 Value
*CreateLShr(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1160 bool isExact
= false) {
1161 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1162 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1163 return Insert(Folder
.CreateLShr(LC
, RC
, isExact
), Name
);
1165 return Insert(BinaryOperator::CreateLShr(LHS
, RHS
), Name
);
1166 return Insert(BinaryOperator::CreateExactLShr(LHS
, RHS
), Name
);
1169 Value
*CreateLShr(Value
*LHS
, const APInt
&RHS
, const Twine
&Name
= "",
1170 bool isExact
= false) {
1171 return CreateLShr(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
,isExact
);
1174 Value
*CreateLShr(Value
*LHS
, uint64_t RHS
, const Twine
&Name
= "",
1175 bool isExact
= false) {
1176 return CreateLShr(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
,isExact
);
1179 Value
*CreateAShr(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1180 bool isExact
= false) {
1181 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1182 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1183 return Insert(Folder
.CreateAShr(LC
, RC
, isExact
), Name
);
1185 return Insert(BinaryOperator::CreateAShr(LHS
, RHS
), Name
);
1186 return Insert(BinaryOperator::CreateExactAShr(LHS
, RHS
), Name
);
1189 Value
*CreateAShr(Value
*LHS
, const APInt
&RHS
, const Twine
&Name
= "",
1190 bool isExact
= false) {
1191 return CreateAShr(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
,isExact
);
1194 Value
*CreateAShr(Value
*LHS
, uint64_t RHS
, const Twine
&Name
= "",
1195 bool isExact
= false) {
1196 return CreateAShr(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
,isExact
);
1199 Value
*CreateAnd(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1200 if (auto *RC
= dyn_cast
<Constant
>(RHS
)) {
1201 if (isa
<ConstantInt
>(RC
) && cast
<ConstantInt
>(RC
)->isMinusOne())
1202 return LHS
; // LHS & -1 -> LHS
1203 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1204 return Insert(Folder
.CreateAnd(LC
, RC
), Name
);
1206 return Insert(BinaryOperator::CreateAnd(LHS
, RHS
), Name
);
1209 Value
*CreateAnd(Value
*LHS
, const APInt
&RHS
, const Twine
&Name
= "") {
1210 return CreateAnd(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
);
1213 Value
*CreateAnd(Value
*LHS
, uint64_t RHS
, const Twine
&Name
= "") {
1214 return CreateAnd(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
);
1217 Value
*CreateOr(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1218 if (auto *RC
= dyn_cast
<Constant
>(RHS
)) {
1219 if (RC
->isNullValue())
1220 return LHS
; // LHS | 0 -> LHS
1221 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1222 return Insert(Folder
.CreateOr(LC
, RC
), Name
);
1224 return Insert(BinaryOperator::CreateOr(LHS
, RHS
), Name
);
1227 Value
*CreateOr(Value
*LHS
, const APInt
&RHS
, const Twine
&Name
= "") {
1228 return CreateOr(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
);
1231 Value
*CreateOr(Value
*LHS
, uint64_t RHS
, const Twine
&Name
= "") {
1232 return CreateOr(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
);
1235 Value
*CreateXor(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1236 if (Value
*V
= foldConstant(Instruction::Xor
, LHS
, RHS
, Name
)) return V
;
1237 return Insert(BinaryOperator::CreateXor(LHS
, RHS
), Name
);
1240 Value
*CreateXor(Value
*LHS
, const APInt
&RHS
, const Twine
&Name
= "") {
1241 return CreateXor(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
);
1244 Value
*CreateXor(Value
*LHS
, uint64_t RHS
, const Twine
&Name
= "") {
1245 return CreateXor(LHS
, ConstantInt::get(LHS
->getType(), RHS
), Name
);
1248 Value
*CreateFAdd(Value
*L
, Value
*R
, const Twine
&Name
= "",
1249 MDNode
*FPMD
= nullptr) {
1250 if (Value
*V
= foldConstant(Instruction::FAdd
, L
, R
, Name
)) return V
;
1251 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFAdd(L
, R
), FPMD
, FMF
);
1252 return Insert(I
, Name
);
1255 /// Copy fast-math-flags from an instruction rather than using the builder's
1257 Value
*CreateFAddFMF(Value
*L
, Value
*R
, Instruction
*FMFSource
,
1258 const Twine
&Name
= "") {
1259 if (Value
*V
= foldConstant(Instruction::FAdd
, L
, R
, Name
)) return V
;
1260 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFAdd(L
, R
), nullptr,
1261 FMFSource
->getFastMathFlags());
1262 return Insert(I
, Name
);
1265 Value
*CreateFSub(Value
*L
, Value
*R
, const Twine
&Name
= "",
1266 MDNode
*FPMD
= nullptr) {
1267 if (Value
*V
= foldConstant(Instruction::FSub
, L
, R
, Name
)) return V
;
1268 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFSub(L
, R
), FPMD
, FMF
);
1269 return Insert(I
, Name
);
1272 /// Copy fast-math-flags from an instruction rather than using the builder's
1274 Value
*CreateFSubFMF(Value
*L
, Value
*R
, Instruction
*FMFSource
,
1275 const Twine
&Name
= "") {
1276 if (Value
*V
= foldConstant(Instruction::FSub
, L
, R
, Name
)) return V
;
1277 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFSub(L
, R
), nullptr,
1278 FMFSource
->getFastMathFlags());
1279 return Insert(I
, Name
);
1282 Value
*CreateFMul(Value
*L
, Value
*R
, const Twine
&Name
= "",
1283 MDNode
*FPMD
= nullptr) {
1284 if (Value
*V
= foldConstant(Instruction::FMul
, L
, R
, Name
)) return V
;
1285 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFMul(L
, R
), FPMD
, FMF
);
1286 return Insert(I
, Name
);
1289 /// Copy fast-math-flags from an instruction rather than using the builder's
1291 Value
*CreateFMulFMF(Value
*L
, Value
*R
, Instruction
*FMFSource
,
1292 const Twine
&Name
= "") {
1293 if (Value
*V
= foldConstant(Instruction::FMul
, L
, R
, Name
)) return V
;
1294 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFMul(L
, R
), nullptr,
1295 FMFSource
->getFastMathFlags());
1296 return Insert(I
, Name
);
1299 Value
*CreateFDiv(Value
*L
, Value
*R
, const Twine
&Name
= "",
1300 MDNode
*FPMD
= nullptr) {
1301 if (Value
*V
= foldConstant(Instruction::FDiv
, L
, R
, Name
)) return V
;
1302 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFDiv(L
, R
), FPMD
, FMF
);
1303 return Insert(I
, Name
);
1306 /// Copy fast-math-flags from an instruction rather than using the builder's
1308 Value
*CreateFDivFMF(Value
*L
, Value
*R
, Instruction
*FMFSource
,
1309 const Twine
&Name
= "") {
1310 if (Value
*V
= foldConstant(Instruction::FDiv
, L
, R
, Name
)) return V
;
1311 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFDiv(L
, R
), nullptr,
1312 FMFSource
->getFastMathFlags());
1313 return Insert(I
, Name
);
1316 Value
*CreateFRem(Value
*L
, Value
*R
, const Twine
&Name
= "",
1317 MDNode
*FPMD
= nullptr) {
1318 if (Value
*V
= foldConstant(Instruction::FRem
, L
, R
, Name
)) return V
;
1319 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFRem(L
, R
), FPMD
, FMF
);
1320 return Insert(I
, Name
);
1323 /// Copy fast-math-flags from an instruction rather than using the builder's
1325 Value
*CreateFRemFMF(Value
*L
, Value
*R
, Instruction
*FMFSource
,
1326 const Twine
&Name
= "") {
1327 if (Value
*V
= foldConstant(Instruction::FRem
, L
, R
, Name
)) return V
;
1328 Instruction
*I
= setFPAttrs(BinaryOperator::CreateFRem(L
, R
), nullptr,
1329 FMFSource
->getFastMathFlags());
1330 return Insert(I
, Name
);
1333 Value
*CreateBinOp(Instruction::BinaryOps Opc
,
1334 Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1335 MDNode
*FPMathTag
= nullptr) {
1336 if (Value
*V
= foldConstant(Opc
, LHS
, RHS
, Name
)) return V
;
1337 Instruction
*BinOp
= BinaryOperator::Create(Opc
, LHS
, RHS
);
1338 if (isa
<FPMathOperator
>(BinOp
))
1339 BinOp
= setFPAttrs(BinOp
, FPMathTag
, FMF
);
1340 return Insert(BinOp
, Name
);
1343 Value
*CreateNeg(Value
*V
, const Twine
&Name
= "",
1344 bool HasNUW
= false, bool HasNSW
= false) {
1345 if (auto *VC
= dyn_cast
<Constant
>(V
))
1346 return Insert(Folder
.CreateNeg(VC
, HasNUW
, HasNSW
), Name
);
1347 BinaryOperator
*BO
= Insert(BinaryOperator::CreateNeg(V
), Name
);
1348 if (HasNUW
) BO
->setHasNoUnsignedWrap();
1349 if (HasNSW
) BO
->setHasNoSignedWrap();
1353 Value
*CreateNSWNeg(Value
*V
, const Twine
&Name
= "") {
1354 return CreateNeg(V
, Name
, false, true);
1357 Value
*CreateNUWNeg(Value
*V
, const Twine
&Name
= "") {
1358 return CreateNeg(V
, Name
, true, false);
1361 Value
*CreateFNeg(Value
*V
, const Twine
&Name
= "",
1362 MDNode
*FPMathTag
= nullptr) {
1363 if (auto *VC
= dyn_cast
<Constant
>(V
))
1364 return Insert(Folder
.CreateFNeg(VC
), Name
);
1365 return Insert(setFPAttrs(BinaryOperator::CreateFNeg(V
), FPMathTag
, FMF
),
1369 Value
*CreateNot(Value
*V
, const Twine
&Name
= "") {
1370 if (auto *VC
= dyn_cast
<Constant
>(V
))
1371 return Insert(Folder
.CreateNot(VC
), Name
);
1372 return Insert(BinaryOperator::CreateNot(V
), Name
);
1375 //===--------------------------------------------------------------------===//
1376 // Instruction creation methods: Memory Instructions
1377 //===--------------------------------------------------------------------===//
1379 AllocaInst
*CreateAlloca(Type
*Ty
, unsigned AddrSpace
,
1380 Value
*ArraySize
= nullptr, const Twine
&Name
= "") {
1381 return Insert(new AllocaInst(Ty
, AddrSpace
, ArraySize
), Name
);
1384 AllocaInst
*CreateAlloca(Type
*Ty
, Value
*ArraySize
= nullptr,
1385 const Twine
&Name
= "") {
1386 const DataLayout
&DL
= BB
->getParent()->getParent()->getDataLayout();
1387 return Insert(new AllocaInst(Ty
, DL
.getAllocaAddrSpace(), ArraySize
), Name
);
1390 /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1391 /// converting the string to 'bool' for the isVolatile parameter.
1392 LoadInst
*CreateLoad(Type
*Ty
, Value
*Ptr
, const char *Name
) {
1393 return Insert(new LoadInst(Ty
, Ptr
), Name
);
1396 LoadInst
*CreateLoad(Type
*Ty
, Value
*Ptr
, const Twine
&Name
= "") {
1397 return Insert(new LoadInst(Ty
, Ptr
), Name
);
1400 LoadInst
*CreateLoad(Type
*Ty
, Value
*Ptr
, bool isVolatile
,
1401 const Twine
&Name
= "") {
1402 return Insert(new LoadInst(Ty
, Ptr
, Twine(), isVolatile
), Name
);
1405 // Deprecated [opaque pointer types]
1406 LoadInst
*CreateLoad(Value
*Ptr
, const char *Name
) {
1407 return CreateLoad(Ptr
->getType()->getPointerElementType(), Ptr
, Name
);
1410 // Deprecated [opaque pointer types]
1411 LoadInst
*CreateLoad(Value
*Ptr
, const Twine
&Name
= "") {
1412 return CreateLoad(Ptr
->getType()->getPointerElementType(), Ptr
, Name
);
1415 // Deprecated [opaque pointer types]
1416 LoadInst
*CreateLoad(Value
*Ptr
, bool isVolatile
, const Twine
&Name
= "") {
1417 return CreateLoad(Ptr
->getType()->getPointerElementType(), Ptr
, isVolatile
,
1421 StoreInst
*CreateStore(Value
*Val
, Value
*Ptr
, bool isVolatile
= false) {
1422 return Insert(new StoreInst(Val
, Ptr
, isVolatile
));
1425 /// Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
1426 /// correctly, instead of converting the string to 'bool' for the isVolatile
1428 LoadInst
*CreateAlignedLoad(Type
*Ty
, Value
*Ptr
, unsigned Align
,
1430 LoadInst
*LI
= CreateLoad(Ty
, Ptr
, Name
);
1431 LI
->setAlignment(Align
);
1434 LoadInst
*CreateAlignedLoad(Type
*Ty
, Value
*Ptr
, unsigned Align
,
1435 const Twine
&Name
= "") {
1436 LoadInst
*LI
= CreateLoad(Ty
, Ptr
, Name
);
1437 LI
->setAlignment(Align
);
1440 LoadInst
*CreateAlignedLoad(Type
*Ty
, Value
*Ptr
, unsigned Align
,
1441 bool isVolatile
, const Twine
&Name
= "") {
1442 LoadInst
*LI
= CreateLoad(Ty
, Ptr
, isVolatile
, Name
);
1443 LI
->setAlignment(Align
);
1447 // Deprecated [opaque pointer types]
1448 LoadInst
*CreateAlignedLoad(Value
*Ptr
, unsigned Align
, const char *Name
) {
1449 return CreateAlignedLoad(Ptr
->getType()->getPointerElementType(), Ptr
,
1452 // Deprecated [opaque pointer types]
1453 LoadInst
*CreateAlignedLoad(Value
*Ptr
, unsigned Align
,
1454 const Twine
&Name
= "") {
1455 return CreateAlignedLoad(Ptr
->getType()->getPointerElementType(), Ptr
,
1458 // Deprecated [opaque pointer types]
1459 LoadInst
*CreateAlignedLoad(Value
*Ptr
, unsigned Align
, bool isVolatile
,
1460 const Twine
&Name
= "") {
1461 return CreateAlignedLoad(Ptr
->getType()->getPointerElementType(), Ptr
,
1462 Align
, isVolatile
, Name
);
1465 StoreInst
*CreateAlignedStore(Value
*Val
, Value
*Ptr
, unsigned Align
,
1466 bool isVolatile
= false) {
1467 StoreInst
*SI
= CreateStore(Val
, Ptr
, isVolatile
);
1468 SI
->setAlignment(Align
);
1472 FenceInst
*CreateFence(AtomicOrdering Ordering
,
1473 SyncScope::ID SSID
= SyncScope::System
,
1474 const Twine
&Name
= "") {
1475 return Insert(new FenceInst(Context
, Ordering
, SSID
), Name
);
1479 CreateAtomicCmpXchg(Value
*Ptr
, Value
*Cmp
, Value
*New
,
1480 AtomicOrdering SuccessOrdering
,
1481 AtomicOrdering FailureOrdering
,
1482 SyncScope::ID SSID
= SyncScope::System
) {
1483 return Insert(new AtomicCmpXchgInst(Ptr
, Cmp
, New
, SuccessOrdering
,
1484 FailureOrdering
, SSID
));
1487 AtomicRMWInst
*CreateAtomicRMW(AtomicRMWInst::BinOp Op
, Value
*Ptr
, Value
*Val
,
1488 AtomicOrdering Ordering
,
1489 SyncScope::ID SSID
= SyncScope::System
) {
1490 return Insert(new AtomicRMWInst(Op
, Ptr
, Val
, Ordering
, SSID
));
1493 Value
*CreateGEP(Value
*Ptr
, ArrayRef
<Value
*> IdxList
,
1494 const Twine
&Name
= "") {
1495 return CreateGEP(nullptr, Ptr
, IdxList
, Name
);
1498 Value
*CreateGEP(Type
*Ty
, Value
*Ptr
, ArrayRef
<Value
*> IdxList
,
1499 const Twine
&Name
= "") {
1500 if (auto *PC
= dyn_cast
<Constant
>(Ptr
)) {
1501 // Every index must be constant.
1503 for (i
= 0, e
= IdxList
.size(); i
!= e
; ++i
)
1504 if (!isa
<Constant
>(IdxList
[i
]))
1507 return Insert(Folder
.CreateGetElementPtr(Ty
, PC
, IdxList
), Name
);
1509 return Insert(GetElementPtrInst::Create(Ty
, Ptr
, IdxList
), Name
);
1512 Value
*CreateInBoundsGEP(Value
*Ptr
, ArrayRef
<Value
*> IdxList
,
1513 const Twine
&Name
= "") {
1514 return CreateInBoundsGEP(nullptr, Ptr
, IdxList
, Name
);
1517 Value
*CreateInBoundsGEP(Type
*Ty
, Value
*Ptr
, ArrayRef
<Value
*> IdxList
,
1518 const Twine
&Name
= "") {
1519 if (auto *PC
= dyn_cast
<Constant
>(Ptr
)) {
1520 // Every index must be constant.
1522 for (i
= 0, e
= IdxList
.size(); i
!= e
; ++i
)
1523 if (!isa
<Constant
>(IdxList
[i
]))
1526 return Insert(Folder
.CreateInBoundsGetElementPtr(Ty
, PC
, IdxList
),
1529 return Insert(GetElementPtrInst::CreateInBounds(Ty
, Ptr
, IdxList
), Name
);
1532 Value
*CreateGEP(Value
*Ptr
, Value
*Idx
, const Twine
&Name
= "") {
1533 return CreateGEP(nullptr, Ptr
, Idx
, Name
);
1536 Value
*CreateGEP(Type
*Ty
, Value
*Ptr
, Value
*Idx
, const Twine
&Name
= "") {
1537 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1538 if (auto *IC
= dyn_cast
<Constant
>(Idx
))
1539 return Insert(Folder
.CreateGetElementPtr(Ty
, PC
, IC
), Name
);
1540 return Insert(GetElementPtrInst::Create(Ty
, Ptr
, Idx
), Name
);
1543 Value
*CreateInBoundsGEP(Type
*Ty
, Value
*Ptr
, Value
*Idx
,
1544 const Twine
&Name
= "") {
1545 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1546 if (auto *IC
= dyn_cast
<Constant
>(Idx
))
1547 return Insert(Folder
.CreateInBoundsGetElementPtr(Ty
, PC
, IC
), Name
);
1548 return Insert(GetElementPtrInst::CreateInBounds(Ty
, Ptr
, Idx
), Name
);
1551 Value
*CreateConstGEP1_32(Value
*Ptr
, unsigned Idx0
, const Twine
&Name
= "") {
1552 return CreateConstGEP1_32(nullptr, Ptr
, Idx0
, Name
);
1555 Value
*CreateConstGEP1_32(Type
*Ty
, Value
*Ptr
, unsigned Idx0
,
1556 const Twine
&Name
= "") {
1557 Value
*Idx
= ConstantInt::get(Type::getInt32Ty(Context
), Idx0
);
1559 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1560 return Insert(Folder
.CreateGetElementPtr(Ty
, PC
, Idx
), Name
);
1562 return Insert(GetElementPtrInst::Create(Ty
, Ptr
, Idx
), Name
);
1565 Value
*CreateConstInBoundsGEP1_32(Type
*Ty
, Value
*Ptr
, unsigned Idx0
,
1566 const Twine
&Name
= "") {
1567 Value
*Idx
= ConstantInt::get(Type::getInt32Ty(Context
), Idx0
);
1569 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1570 return Insert(Folder
.CreateInBoundsGetElementPtr(Ty
, PC
, Idx
), Name
);
1572 return Insert(GetElementPtrInst::CreateInBounds(Ty
, Ptr
, Idx
), Name
);
1575 Value
*CreateConstGEP2_32(Type
*Ty
, Value
*Ptr
, unsigned Idx0
, unsigned Idx1
,
1576 const Twine
&Name
= "") {
1578 ConstantInt::get(Type::getInt32Ty(Context
), Idx0
),
1579 ConstantInt::get(Type::getInt32Ty(Context
), Idx1
)
1582 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1583 return Insert(Folder
.CreateGetElementPtr(Ty
, PC
, Idxs
), Name
);
1585 return Insert(GetElementPtrInst::Create(Ty
, Ptr
, Idxs
), Name
);
1588 Value
*CreateConstInBoundsGEP2_32(Type
*Ty
, Value
*Ptr
, unsigned Idx0
,
1589 unsigned Idx1
, const Twine
&Name
= "") {
1591 ConstantInt::get(Type::getInt32Ty(Context
), Idx0
),
1592 ConstantInt::get(Type::getInt32Ty(Context
), Idx1
)
1595 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1596 return Insert(Folder
.CreateInBoundsGetElementPtr(Ty
, PC
, Idxs
), Name
);
1598 return Insert(GetElementPtrInst::CreateInBounds(Ty
, Ptr
, Idxs
), Name
);
1601 Value
*CreateConstGEP1_64(Type
*Ty
, Value
*Ptr
, uint64_t Idx0
,
1602 const Twine
&Name
= "") {
1603 Value
*Idx
= ConstantInt::get(Type::getInt64Ty(Context
), Idx0
);
1605 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1606 return Insert(Folder
.CreateGetElementPtr(Ty
, PC
, Idx
), Name
);
1608 return Insert(GetElementPtrInst::Create(Ty
, Ptr
, Idx
), Name
);
1611 Value
*CreateConstGEP1_64(Value
*Ptr
, uint64_t Idx0
, const Twine
&Name
= "") {
1612 return CreateConstGEP1_64(nullptr, Ptr
, Idx0
, Name
);
1615 Value
*CreateConstInBoundsGEP1_64(Type
*Ty
, Value
*Ptr
, uint64_t Idx0
,
1616 const Twine
&Name
= "") {
1617 Value
*Idx
= ConstantInt::get(Type::getInt64Ty(Context
), Idx0
);
1619 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1620 return Insert(Folder
.CreateInBoundsGetElementPtr(Ty
, PC
, Idx
), Name
);
1622 return Insert(GetElementPtrInst::CreateInBounds(Ty
, Ptr
, Idx
), Name
);
1625 Value
*CreateConstInBoundsGEP1_64(Value
*Ptr
, uint64_t Idx0
,
1626 const Twine
&Name
= "") {
1627 return CreateConstInBoundsGEP1_64(nullptr, Ptr
, Idx0
, Name
);
1630 Value
*CreateConstGEP2_64(Type
*Ty
, Value
*Ptr
, uint64_t Idx0
, uint64_t Idx1
,
1631 const Twine
&Name
= "") {
1633 ConstantInt::get(Type::getInt64Ty(Context
), Idx0
),
1634 ConstantInt::get(Type::getInt64Ty(Context
), Idx1
)
1637 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1638 return Insert(Folder
.CreateGetElementPtr(Ty
, PC
, Idxs
), Name
);
1640 return Insert(GetElementPtrInst::Create(Ty
, Ptr
, Idxs
), Name
);
1643 Value
*CreateConstGEP2_64(Value
*Ptr
, uint64_t Idx0
, uint64_t Idx1
,
1644 const Twine
&Name
= "") {
1645 return CreateConstGEP2_64(nullptr, Ptr
, Idx0
, Idx1
, Name
);
1648 Value
*CreateConstInBoundsGEP2_64(Type
*Ty
, Value
*Ptr
, uint64_t Idx0
,
1649 uint64_t Idx1
, const Twine
&Name
= "") {
1651 ConstantInt::get(Type::getInt64Ty(Context
), Idx0
),
1652 ConstantInt::get(Type::getInt64Ty(Context
), Idx1
)
1655 if (auto *PC
= dyn_cast
<Constant
>(Ptr
))
1656 return Insert(Folder
.CreateInBoundsGetElementPtr(Ty
, PC
, Idxs
), Name
);
1658 return Insert(GetElementPtrInst::CreateInBounds(Ty
, Ptr
, Idxs
), Name
);
1661 Value
*CreateConstInBoundsGEP2_64(Value
*Ptr
, uint64_t Idx0
, uint64_t Idx1
,
1662 const Twine
&Name
= "") {
1663 return CreateConstInBoundsGEP2_64(nullptr, Ptr
, Idx0
, Idx1
, Name
);
1666 Value
*CreateStructGEP(Type
*Ty
, Value
*Ptr
, unsigned Idx
,
1667 const Twine
&Name
= "") {
1668 return CreateConstInBoundsGEP2_32(Ty
, Ptr
, 0, Idx
, Name
);
1671 Value
*CreateStructGEP(Value
*Ptr
, unsigned Idx
, const Twine
&Name
= "") {
1672 return CreateConstInBoundsGEP2_32(nullptr, Ptr
, 0, Idx
, Name
);
1675 /// Same as CreateGlobalString, but return a pointer with "i8*" type
1676 /// instead of a pointer to array of i8.
1677 Constant
*CreateGlobalStringPtr(StringRef Str
, const Twine
&Name
= "",
1678 unsigned AddressSpace
= 0) {
1679 GlobalVariable
*GV
= CreateGlobalString(Str
, Name
, AddressSpace
);
1680 Constant
*Zero
= ConstantInt::get(Type::getInt32Ty(Context
), 0);
1681 Constant
*Indices
[] = {Zero
, Zero
};
1682 return ConstantExpr::getInBoundsGetElementPtr(GV
->getValueType(), GV
,
1686 //===--------------------------------------------------------------------===//
1687 // Instruction creation methods: Cast/Conversion Operators
1688 //===--------------------------------------------------------------------===//
1690 Value
*CreateTrunc(Value
*V
, Type
*DestTy
, const Twine
&Name
= "") {
1691 return CreateCast(Instruction::Trunc
, V
, DestTy
, Name
);
1694 Value
*CreateZExt(Value
*V
, Type
*DestTy
, const Twine
&Name
= "") {
1695 return CreateCast(Instruction::ZExt
, V
, DestTy
, Name
);
1698 Value
*CreateSExt(Value
*V
, Type
*DestTy
, const Twine
&Name
= "") {
1699 return CreateCast(Instruction::SExt
, V
, DestTy
, Name
);
1702 /// Create a ZExt or Trunc from the integer value V to DestTy. Return
1703 /// the value untouched if the type of V is already DestTy.
1704 Value
*CreateZExtOrTrunc(Value
*V
, Type
*DestTy
,
1705 const Twine
&Name
= "") {
1706 assert(V
->getType()->isIntOrIntVectorTy() &&
1707 DestTy
->isIntOrIntVectorTy() &&
1708 "Can only zero extend/truncate integers!");
1709 Type
*VTy
= V
->getType();
1710 if (VTy
->getScalarSizeInBits() < DestTy
->getScalarSizeInBits())
1711 return CreateZExt(V
, DestTy
, Name
);
1712 if (VTy
->getScalarSizeInBits() > DestTy
->getScalarSizeInBits())
1713 return CreateTrunc(V
, DestTy
, Name
);
1717 /// Create a SExt or Trunc from the integer value V to DestTy. Return
1718 /// the value untouched if the type of V is already DestTy.
1719 Value
*CreateSExtOrTrunc(Value
*V
, Type
*DestTy
,
1720 const Twine
&Name
= "") {
1721 assert(V
->getType()->isIntOrIntVectorTy() &&
1722 DestTy
->isIntOrIntVectorTy() &&
1723 "Can only sign extend/truncate integers!");
1724 Type
*VTy
= V
->getType();
1725 if (VTy
->getScalarSizeInBits() < DestTy
->getScalarSizeInBits())
1726 return CreateSExt(V
, DestTy
, Name
);
1727 if (VTy
->getScalarSizeInBits() > DestTy
->getScalarSizeInBits())
1728 return CreateTrunc(V
, DestTy
, Name
);
1732 Value
*CreateFPToUI(Value
*V
, Type
*DestTy
, const Twine
&Name
= ""){
1733 return CreateCast(Instruction::FPToUI
, V
, DestTy
, Name
);
1736 Value
*CreateFPToSI(Value
*V
, Type
*DestTy
, const Twine
&Name
= ""){
1737 return CreateCast(Instruction::FPToSI
, V
, DestTy
, Name
);
1740 Value
*CreateUIToFP(Value
*V
, Type
*DestTy
, const Twine
&Name
= ""){
1741 return CreateCast(Instruction::UIToFP
, V
, DestTy
, Name
);
1744 Value
*CreateSIToFP(Value
*V
, Type
*DestTy
, const Twine
&Name
= ""){
1745 return CreateCast(Instruction::SIToFP
, V
, DestTy
, Name
);
1748 Value
*CreateFPTrunc(Value
*V
, Type
*DestTy
,
1749 const Twine
&Name
= "") {
1750 return CreateCast(Instruction::FPTrunc
, V
, DestTy
, Name
);
1753 Value
*CreateFPExt(Value
*V
, Type
*DestTy
, const Twine
&Name
= "") {
1754 return CreateCast(Instruction::FPExt
, V
, DestTy
, Name
);
1757 Value
*CreatePtrToInt(Value
*V
, Type
*DestTy
,
1758 const Twine
&Name
= "") {
1759 return CreateCast(Instruction::PtrToInt
, V
, DestTy
, Name
);
1762 Value
*CreateIntToPtr(Value
*V
, Type
*DestTy
,
1763 const Twine
&Name
= "") {
1764 return CreateCast(Instruction::IntToPtr
, V
, DestTy
, Name
);
1767 Value
*CreateBitCast(Value
*V
, Type
*DestTy
,
1768 const Twine
&Name
= "") {
1769 return CreateCast(Instruction::BitCast
, V
, DestTy
, Name
);
1772 Value
*CreateAddrSpaceCast(Value
*V
, Type
*DestTy
,
1773 const Twine
&Name
= "") {
1774 return CreateCast(Instruction::AddrSpaceCast
, V
, DestTy
, Name
);
1777 Value
*CreateZExtOrBitCast(Value
*V
, Type
*DestTy
,
1778 const Twine
&Name
= "") {
1779 if (V
->getType() == DestTy
)
1781 if (auto *VC
= dyn_cast
<Constant
>(V
))
1782 return Insert(Folder
.CreateZExtOrBitCast(VC
, DestTy
), Name
);
1783 return Insert(CastInst::CreateZExtOrBitCast(V
, DestTy
), Name
);
1786 Value
*CreateSExtOrBitCast(Value
*V
, Type
*DestTy
,
1787 const Twine
&Name
= "") {
1788 if (V
->getType() == DestTy
)
1790 if (auto *VC
= dyn_cast
<Constant
>(V
))
1791 return Insert(Folder
.CreateSExtOrBitCast(VC
, DestTy
), Name
);
1792 return Insert(CastInst::CreateSExtOrBitCast(V
, DestTy
), Name
);
1795 Value
*CreateTruncOrBitCast(Value
*V
, Type
*DestTy
,
1796 const Twine
&Name
= "") {
1797 if (V
->getType() == DestTy
)
1799 if (auto *VC
= dyn_cast
<Constant
>(V
))
1800 return Insert(Folder
.CreateTruncOrBitCast(VC
, DestTy
), Name
);
1801 return Insert(CastInst::CreateTruncOrBitCast(V
, DestTy
), Name
);
1804 Value
*CreateCast(Instruction::CastOps Op
, Value
*V
, Type
*DestTy
,
1805 const Twine
&Name
= "") {
1806 if (V
->getType() == DestTy
)
1808 if (auto *VC
= dyn_cast
<Constant
>(V
))
1809 return Insert(Folder
.CreateCast(Op
, VC
, DestTy
), Name
);
1810 return Insert(CastInst::Create(Op
, V
, DestTy
), Name
);
1813 Value
*CreatePointerCast(Value
*V
, Type
*DestTy
,
1814 const Twine
&Name
= "") {
1815 if (V
->getType() == DestTy
)
1817 if (auto *VC
= dyn_cast
<Constant
>(V
))
1818 return Insert(Folder
.CreatePointerCast(VC
, DestTy
), Name
);
1819 return Insert(CastInst::CreatePointerCast(V
, DestTy
), Name
);
1822 Value
*CreatePointerBitCastOrAddrSpaceCast(Value
*V
, Type
*DestTy
,
1823 const Twine
&Name
= "") {
1824 if (V
->getType() == DestTy
)
1827 if (auto *VC
= dyn_cast
<Constant
>(V
)) {
1828 return Insert(Folder
.CreatePointerBitCastOrAddrSpaceCast(VC
, DestTy
),
1832 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V
, DestTy
),
1836 Value
*CreateIntCast(Value
*V
, Type
*DestTy
, bool isSigned
,
1837 const Twine
&Name
= "") {
1838 if (V
->getType() == DestTy
)
1840 if (auto *VC
= dyn_cast
<Constant
>(V
))
1841 return Insert(Folder
.CreateIntCast(VC
, DestTy
, isSigned
), Name
);
1842 return Insert(CastInst::CreateIntegerCast(V
, DestTy
, isSigned
), Name
);
1845 Value
*CreateBitOrPointerCast(Value
*V
, Type
*DestTy
,
1846 const Twine
&Name
= "") {
1847 if (V
->getType() == DestTy
)
1849 if (V
->getType()->isPtrOrPtrVectorTy() && DestTy
->isIntOrIntVectorTy())
1850 return CreatePtrToInt(V
, DestTy
, Name
);
1851 if (V
->getType()->isIntOrIntVectorTy() && DestTy
->isPtrOrPtrVectorTy())
1852 return CreateIntToPtr(V
, DestTy
, Name
);
1854 return CreateBitCast(V
, DestTy
, Name
);
1857 Value
*CreateFPCast(Value
*V
, Type
*DestTy
, const Twine
&Name
= "") {
1858 if (V
->getType() == DestTy
)
1860 if (auto *VC
= dyn_cast
<Constant
>(V
))
1861 return Insert(Folder
.CreateFPCast(VC
, DestTy
), Name
);
1862 return Insert(CastInst::CreateFPCast(V
, DestTy
), Name
);
1865 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1866 // compile time error, instead of converting the string to bool for the
1867 // isSigned parameter.
1868 Value
*CreateIntCast(Value
*, Type
*, const char *) = delete;
1870 //===--------------------------------------------------------------------===//
1871 // Instruction creation methods: Compare Instructions
1872 //===--------------------------------------------------------------------===//
1874 Value
*CreateICmpEQ(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1875 return CreateICmp(ICmpInst::ICMP_EQ
, LHS
, RHS
, Name
);
1878 Value
*CreateICmpNE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1879 return CreateICmp(ICmpInst::ICMP_NE
, LHS
, RHS
, Name
);
1882 Value
*CreateICmpUGT(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1883 return CreateICmp(ICmpInst::ICMP_UGT
, LHS
, RHS
, Name
);
1886 Value
*CreateICmpUGE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1887 return CreateICmp(ICmpInst::ICMP_UGE
, LHS
, RHS
, Name
);
1890 Value
*CreateICmpULT(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1891 return CreateICmp(ICmpInst::ICMP_ULT
, LHS
, RHS
, Name
);
1894 Value
*CreateICmpULE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1895 return CreateICmp(ICmpInst::ICMP_ULE
, LHS
, RHS
, Name
);
1898 Value
*CreateICmpSGT(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1899 return CreateICmp(ICmpInst::ICMP_SGT
, LHS
, RHS
, Name
);
1902 Value
*CreateICmpSGE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1903 return CreateICmp(ICmpInst::ICMP_SGE
, LHS
, RHS
, Name
);
1906 Value
*CreateICmpSLT(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1907 return CreateICmp(ICmpInst::ICMP_SLT
, LHS
, RHS
, Name
);
1910 Value
*CreateICmpSLE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
1911 return CreateICmp(ICmpInst::ICMP_SLE
, LHS
, RHS
, Name
);
1914 Value
*CreateFCmpOEQ(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1915 MDNode
*FPMathTag
= nullptr) {
1916 return CreateFCmp(FCmpInst::FCMP_OEQ
, LHS
, RHS
, Name
, FPMathTag
);
1919 Value
*CreateFCmpOGT(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1920 MDNode
*FPMathTag
= nullptr) {
1921 return CreateFCmp(FCmpInst::FCMP_OGT
, LHS
, RHS
, Name
, FPMathTag
);
1924 Value
*CreateFCmpOGE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1925 MDNode
*FPMathTag
= nullptr) {
1926 return CreateFCmp(FCmpInst::FCMP_OGE
, LHS
, RHS
, Name
, FPMathTag
);
1929 Value
*CreateFCmpOLT(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1930 MDNode
*FPMathTag
= nullptr) {
1931 return CreateFCmp(FCmpInst::FCMP_OLT
, LHS
, RHS
, Name
, FPMathTag
);
1934 Value
*CreateFCmpOLE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1935 MDNode
*FPMathTag
= nullptr) {
1936 return CreateFCmp(FCmpInst::FCMP_OLE
, LHS
, RHS
, Name
, FPMathTag
);
1939 Value
*CreateFCmpONE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1940 MDNode
*FPMathTag
= nullptr) {
1941 return CreateFCmp(FCmpInst::FCMP_ONE
, LHS
, RHS
, Name
, FPMathTag
);
1944 Value
*CreateFCmpORD(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1945 MDNode
*FPMathTag
= nullptr) {
1946 return CreateFCmp(FCmpInst::FCMP_ORD
, LHS
, RHS
, Name
, FPMathTag
);
1949 Value
*CreateFCmpUNO(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1950 MDNode
*FPMathTag
= nullptr) {
1951 return CreateFCmp(FCmpInst::FCMP_UNO
, LHS
, RHS
, Name
, FPMathTag
);
1954 Value
*CreateFCmpUEQ(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1955 MDNode
*FPMathTag
= nullptr) {
1956 return CreateFCmp(FCmpInst::FCMP_UEQ
, LHS
, RHS
, Name
, FPMathTag
);
1959 Value
*CreateFCmpUGT(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1960 MDNode
*FPMathTag
= nullptr) {
1961 return CreateFCmp(FCmpInst::FCMP_UGT
, LHS
, RHS
, Name
, FPMathTag
);
1964 Value
*CreateFCmpUGE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1965 MDNode
*FPMathTag
= nullptr) {
1966 return CreateFCmp(FCmpInst::FCMP_UGE
, LHS
, RHS
, Name
, FPMathTag
);
1969 Value
*CreateFCmpULT(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1970 MDNode
*FPMathTag
= nullptr) {
1971 return CreateFCmp(FCmpInst::FCMP_ULT
, LHS
, RHS
, Name
, FPMathTag
);
1974 Value
*CreateFCmpULE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1975 MDNode
*FPMathTag
= nullptr) {
1976 return CreateFCmp(FCmpInst::FCMP_ULE
, LHS
, RHS
, Name
, FPMathTag
);
1979 Value
*CreateFCmpUNE(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "",
1980 MDNode
*FPMathTag
= nullptr) {
1981 return CreateFCmp(FCmpInst::FCMP_UNE
, LHS
, RHS
, Name
, FPMathTag
);
1984 Value
*CreateICmp(CmpInst::Predicate P
, Value
*LHS
, Value
*RHS
,
1985 const Twine
&Name
= "") {
1986 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1987 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1988 return Insert(Folder
.CreateICmp(P
, LC
, RC
), Name
);
1989 return Insert(new ICmpInst(P
, LHS
, RHS
), Name
);
1992 Value
*CreateFCmp(CmpInst::Predicate P
, Value
*LHS
, Value
*RHS
,
1993 const Twine
&Name
= "", MDNode
*FPMathTag
= nullptr) {
1994 if (auto *LC
= dyn_cast
<Constant
>(LHS
))
1995 if (auto *RC
= dyn_cast
<Constant
>(RHS
))
1996 return Insert(Folder
.CreateFCmp(P
, LC
, RC
), Name
);
1997 return Insert(setFPAttrs(new FCmpInst(P
, LHS
, RHS
), FPMathTag
, FMF
), Name
);
2000 //===--------------------------------------------------------------------===//
2001 // Instruction creation methods: Other Instructions
2002 //===--------------------------------------------------------------------===//
2004 PHINode
*CreatePHI(Type
*Ty
, unsigned NumReservedValues
,
2005 const Twine
&Name
= "") {
2006 return Insert(PHINode::Create(Ty
, NumReservedValues
), Name
);
2009 CallInst
*CreateCall(FunctionType
*FTy
, Value
*Callee
,
2010 ArrayRef
<Value
*> Args
= None
, const Twine
&Name
= "",
2011 MDNode
*FPMathTag
= nullptr) {
2012 CallInst
*CI
= CallInst::Create(FTy
, Callee
, Args
, DefaultOperandBundles
);
2013 if (isa
<FPMathOperator
>(CI
))
2014 CI
= cast
<CallInst
>(setFPAttrs(CI
, FPMathTag
, FMF
));
2015 return Insert(CI
, Name
);
2018 CallInst
*CreateCall(FunctionType
*FTy
, Value
*Callee
, ArrayRef
<Value
*> Args
,
2019 ArrayRef
<OperandBundleDef
> OpBundles
,
2020 const Twine
&Name
= "", MDNode
*FPMathTag
= nullptr) {
2021 CallInst
*CI
= CallInst::Create(FTy
, Callee
, Args
, OpBundles
);
2022 if (isa
<FPMathOperator
>(CI
))
2023 CI
= cast
<CallInst
>(setFPAttrs(CI
, FPMathTag
, FMF
));
2024 return Insert(CI
, Name
);
2027 CallInst
*CreateCall(FunctionCallee Callee
, ArrayRef
<Value
*> Args
= None
,
2028 const Twine
&Name
= "", MDNode
*FPMathTag
= nullptr) {
2029 return CreateCall(Callee
.getFunctionType(), Callee
.getCallee(), Args
, Name
,
2033 CallInst
*CreateCall(FunctionCallee Callee
, ArrayRef
<Value
*> Args
,
2034 ArrayRef
<OperandBundleDef
> OpBundles
,
2035 const Twine
&Name
= "", MDNode
*FPMathTag
= nullptr) {
2036 return CreateCall(Callee
.getFunctionType(), Callee
.getCallee(), Args
,
2037 OpBundles
, Name
, FPMathTag
);
2040 // Deprecated [opaque pointer types]
2041 CallInst
*CreateCall(Value
*Callee
, ArrayRef
<Value
*> Args
= None
,
2042 const Twine
&Name
= "", MDNode
*FPMathTag
= nullptr) {
2044 cast
<FunctionType
>(Callee
->getType()->getPointerElementType()), Callee
,
2045 Args
, Name
, FPMathTag
);
2048 // Deprecated [opaque pointer types]
2049 CallInst
*CreateCall(Value
*Callee
, ArrayRef
<Value
*> Args
,
2050 ArrayRef
<OperandBundleDef
> OpBundles
,
2051 const Twine
&Name
= "", MDNode
*FPMathTag
= nullptr) {
2053 cast
<FunctionType
>(Callee
->getType()->getPointerElementType()), Callee
,
2054 Args
, OpBundles
, Name
, FPMathTag
);
2057 Value
*CreateSelect(Value
*C
, Value
*True
, Value
*False
,
2058 const Twine
&Name
= "", Instruction
*MDFrom
= nullptr) {
2059 if (auto *CC
= dyn_cast
<Constant
>(C
))
2060 if (auto *TC
= dyn_cast
<Constant
>(True
))
2061 if (auto *FC
= dyn_cast
<Constant
>(False
))
2062 return Insert(Folder
.CreateSelect(CC
, TC
, FC
), Name
);
2064 SelectInst
*Sel
= SelectInst::Create(C
, True
, False
);
2066 MDNode
*Prof
= MDFrom
->getMetadata(LLVMContext::MD_prof
);
2067 MDNode
*Unpred
= MDFrom
->getMetadata(LLVMContext::MD_unpredictable
);
2068 Sel
= addBranchMetadata(Sel
, Prof
, Unpred
);
2070 return Insert(Sel
, Name
);
2073 VAArgInst
*CreateVAArg(Value
*List
, Type
*Ty
, const Twine
&Name
= "") {
2074 return Insert(new VAArgInst(List
, Ty
), Name
);
2077 Value
*CreateExtractElement(Value
*Vec
, Value
*Idx
,
2078 const Twine
&Name
= "") {
2079 if (auto *VC
= dyn_cast
<Constant
>(Vec
))
2080 if (auto *IC
= dyn_cast
<Constant
>(Idx
))
2081 return Insert(Folder
.CreateExtractElement(VC
, IC
), Name
);
2082 return Insert(ExtractElementInst::Create(Vec
, Idx
), Name
);
2085 Value
*CreateExtractElement(Value
*Vec
, uint64_t Idx
,
2086 const Twine
&Name
= "") {
2087 return CreateExtractElement(Vec
, getInt64(Idx
), Name
);
2090 Value
*CreateInsertElement(Value
*Vec
, Value
*NewElt
, Value
*Idx
,
2091 const Twine
&Name
= "") {
2092 if (auto *VC
= dyn_cast
<Constant
>(Vec
))
2093 if (auto *NC
= dyn_cast
<Constant
>(NewElt
))
2094 if (auto *IC
= dyn_cast
<Constant
>(Idx
))
2095 return Insert(Folder
.CreateInsertElement(VC
, NC
, IC
), Name
);
2096 return Insert(InsertElementInst::Create(Vec
, NewElt
, Idx
), Name
);
2099 Value
*CreateInsertElement(Value
*Vec
, Value
*NewElt
, uint64_t Idx
,
2100 const Twine
&Name
= "") {
2101 return CreateInsertElement(Vec
, NewElt
, getInt64(Idx
), Name
);
2104 Value
*CreateShuffleVector(Value
*V1
, Value
*V2
, Value
*Mask
,
2105 const Twine
&Name
= "") {
2106 if (auto *V1C
= dyn_cast
<Constant
>(V1
))
2107 if (auto *V2C
= dyn_cast
<Constant
>(V2
))
2108 if (auto *MC
= dyn_cast
<Constant
>(Mask
))
2109 return Insert(Folder
.CreateShuffleVector(V1C
, V2C
, MC
), Name
);
2110 return Insert(new ShuffleVectorInst(V1
, V2
, Mask
), Name
);
2113 Value
*CreateShuffleVector(Value
*V1
, Value
*V2
, ArrayRef
<uint32_t> IntMask
,
2114 const Twine
&Name
= "") {
2115 Value
*Mask
= ConstantDataVector::get(Context
, IntMask
);
2116 return CreateShuffleVector(V1
, V2
, Mask
, Name
);
2119 Value
*CreateExtractValue(Value
*Agg
,
2120 ArrayRef
<unsigned> Idxs
,
2121 const Twine
&Name
= "") {
2122 if (auto *AggC
= dyn_cast
<Constant
>(Agg
))
2123 return Insert(Folder
.CreateExtractValue(AggC
, Idxs
), Name
);
2124 return Insert(ExtractValueInst::Create(Agg
, Idxs
), Name
);
2127 Value
*CreateInsertValue(Value
*Agg
, Value
*Val
,
2128 ArrayRef
<unsigned> Idxs
,
2129 const Twine
&Name
= "") {
2130 if (auto *AggC
= dyn_cast
<Constant
>(Agg
))
2131 if (auto *ValC
= dyn_cast
<Constant
>(Val
))
2132 return Insert(Folder
.CreateInsertValue(AggC
, ValC
, Idxs
), Name
);
2133 return Insert(InsertValueInst::Create(Agg
, Val
, Idxs
), Name
);
2136 LandingPadInst
*CreateLandingPad(Type
*Ty
, unsigned NumClauses
,
2137 const Twine
&Name
= "") {
2138 return Insert(LandingPadInst::Create(Ty
, NumClauses
), Name
);
2141 //===--------------------------------------------------------------------===//
2142 // Utility creation methods
2143 //===--------------------------------------------------------------------===//
2145 /// Return an i1 value testing if \p Arg is null.
2146 Value
*CreateIsNull(Value
*Arg
, const Twine
&Name
= "") {
2147 return CreateICmpEQ(Arg
, Constant::getNullValue(Arg
->getType()),
2151 /// Return an i1 value testing if \p Arg is not null.
2152 Value
*CreateIsNotNull(Value
*Arg
, const Twine
&Name
= "") {
2153 return CreateICmpNE(Arg
, Constant::getNullValue(Arg
->getType()),
2157 /// Return the i64 difference between two pointer values, dividing out
2158 /// the size of the pointed-to objects.
2160 /// This is intended to implement C-style pointer subtraction. As such, the
2161 /// pointers must be appropriately aligned for their element types and
2162 /// pointing into the same object.
2163 Value
*CreatePtrDiff(Value
*LHS
, Value
*RHS
, const Twine
&Name
= "") {
2164 assert(LHS
->getType() == RHS
->getType() &&
2165 "Pointer subtraction operand types must match!");
2166 auto *ArgType
= cast
<PointerType
>(LHS
->getType());
2167 Value
*LHS_int
= CreatePtrToInt(LHS
, Type::getInt64Ty(Context
));
2168 Value
*RHS_int
= CreatePtrToInt(RHS
, Type::getInt64Ty(Context
));
2169 Value
*Difference
= CreateSub(LHS_int
, RHS_int
);
2170 return CreateExactSDiv(Difference
,
2171 ConstantExpr::getSizeOf(ArgType
->getElementType()),
2175 /// Create a launder.invariant.group intrinsic call. If Ptr type is
2176 /// different from pointer to i8, it's casted to pointer to i8 in the same
2177 /// address space before call and casted back to Ptr type after call.
2178 Value
*CreateLaunderInvariantGroup(Value
*Ptr
) {
2179 assert(isa
<PointerType
>(Ptr
->getType()) &&
2180 "launder.invariant.group only applies to pointers.");
2181 // FIXME: we could potentially avoid casts to/from i8*.
2182 auto *PtrType
= Ptr
->getType();
2183 auto *Int8PtrTy
= getInt8PtrTy(PtrType
->getPointerAddressSpace());
2184 if (PtrType
!= Int8PtrTy
)
2185 Ptr
= CreateBitCast(Ptr
, Int8PtrTy
);
2186 Module
*M
= BB
->getParent()->getParent();
2187 Function
*FnLaunderInvariantGroup
= Intrinsic::getDeclaration(
2188 M
, Intrinsic::launder_invariant_group
, {Int8PtrTy
});
2190 assert(FnLaunderInvariantGroup
->getReturnType() == Int8PtrTy
&&
2191 FnLaunderInvariantGroup
->getFunctionType()->getParamType(0) ==
2193 "LaunderInvariantGroup should take and return the same type");
2195 CallInst
*Fn
= CreateCall(FnLaunderInvariantGroup
, {Ptr
});
2197 if (PtrType
!= Int8PtrTy
)
2198 return CreateBitCast(Fn
, PtrType
);
2202 /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2203 /// different from pointer to i8, it's casted to pointer to i8 in the same
2204 /// address space before call and casted back to Ptr type after call.
2205 Value
*CreateStripInvariantGroup(Value
*Ptr
) {
2206 assert(isa
<PointerType
>(Ptr
->getType()) &&
2207 "strip.invariant.group only applies to pointers.");
2209 // FIXME: we could potentially avoid casts to/from i8*.
2210 auto *PtrType
= Ptr
->getType();
2211 auto *Int8PtrTy
= getInt8PtrTy(PtrType
->getPointerAddressSpace());
2212 if (PtrType
!= Int8PtrTy
)
2213 Ptr
= CreateBitCast(Ptr
, Int8PtrTy
);
2214 Module
*M
= BB
->getParent()->getParent();
2215 Function
*FnStripInvariantGroup
= Intrinsic::getDeclaration(
2216 M
, Intrinsic::strip_invariant_group
, {Int8PtrTy
});
2218 assert(FnStripInvariantGroup
->getReturnType() == Int8PtrTy
&&
2219 FnStripInvariantGroup
->getFunctionType()->getParamType(0) ==
2221 "StripInvariantGroup should take and return the same type");
2223 CallInst
*Fn
= CreateCall(FnStripInvariantGroup
, {Ptr
});
2225 if (PtrType
!= Int8PtrTy
)
2226 return CreateBitCast(Fn
, PtrType
);
2230 /// Return a vector value that contains \arg V broadcasted to \p
2231 /// NumElts elements.
2232 Value
*CreateVectorSplat(unsigned NumElts
, Value
*V
, const Twine
&Name
= "") {
2233 assert(NumElts
> 0 && "Cannot splat to an empty vector!");
2235 // First insert it into an undef vector so we can shuffle it.
2236 Type
*I32Ty
= getInt32Ty();
2237 Value
*Undef
= UndefValue::get(VectorType::get(V
->getType(), NumElts
));
2238 V
= CreateInsertElement(Undef
, V
, ConstantInt::get(I32Ty
, 0),
2239 Name
+ ".splatinsert");
2241 // Shuffle the value across the desired number of elements.
2242 Value
*Zeros
= ConstantAggregateZero::get(VectorType::get(I32Ty
, NumElts
));
2243 return CreateShuffleVector(V
, Undef
, Zeros
, Name
+ ".splat");
2246 /// Return a value that has been extracted from a larger integer type.
2247 Value
*CreateExtractInteger(const DataLayout
&DL
, Value
*From
,
2248 IntegerType
*ExtractedTy
, uint64_t Offset
,
2249 const Twine
&Name
) {
2250 auto *IntTy
= cast
<IntegerType
>(From
->getType());
2251 assert(DL
.getTypeStoreSize(ExtractedTy
) + Offset
<=
2252 DL
.getTypeStoreSize(IntTy
) &&
2253 "Element extends past full value");
2254 uint64_t ShAmt
= 8 * Offset
;
2256 if (DL
.isBigEndian())
2257 ShAmt
= 8 * (DL
.getTypeStoreSize(IntTy
) -
2258 DL
.getTypeStoreSize(ExtractedTy
) - Offset
);
2260 V
= CreateLShr(V
, ShAmt
, Name
+ ".shift");
2262 assert(ExtractedTy
->getBitWidth() <= IntTy
->getBitWidth() &&
2263 "Cannot extract to a larger integer!");
2264 if (ExtractedTy
!= IntTy
) {
2265 V
= CreateTrunc(V
, ExtractedTy
, Name
+ ".trunc");
2271 /// Helper function that creates an assume intrinsic call that
2272 /// represents an alignment assumption on the provided Ptr, Mask, Type
2273 /// and Offset. It may be sometimes useful to do some other logic
2274 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2275 CallInst
*CreateAlignmentAssumptionHelper(const DataLayout
&DL
,
2276 Value
*PtrValue
, Value
*Mask
,
2277 Type
*IntPtrTy
, Value
*OffsetValue
,
2279 Value
*PtrIntValue
= CreatePtrToInt(PtrValue
, IntPtrTy
, "ptrint");
2282 bool IsOffsetZero
= false;
2283 if (const auto *CI
= dyn_cast
<ConstantInt
>(OffsetValue
))
2284 IsOffsetZero
= CI
->isZero();
2286 if (!IsOffsetZero
) {
2287 if (OffsetValue
->getType() != IntPtrTy
)
2288 OffsetValue
= CreateIntCast(OffsetValue
, IntPtrTy
, /*isSigned*/ true,
2290 PtrIntValue
= CreateSub(PtrIntValue
, OffsetValue
, "offsetptr");
2294 Value
*Zero
= ConstantInt::get(IntPtrTy
, 0);
2295 Value
*MaskedPtr
= CreateAnd(PtrIntValue
, Mask
, "maskedptr");
2296 Value
*InvCond
= CreateICmpEQ(MaskedPtr
, Zero
, "maskcond");
2298 *TheCheck
= InvCond
;
2300 return CreateAssumption(InvCond
);
2304 /// Create an assume intrinsic call that represents an alignment
2305 /// assumption on the provided pointer.
2307 /// An optional offset can be provided, and if it is provided, the offset
2308 /// must be subtracted from the provided pointer to get the pointer with the
2309 /// specified alignment.
2311 /// It may be sometimes useful to do some other logic
2312 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2313 CallInst
*CreateAlignmentAssumption(const DataLayout
&DL
, Value
*PtrValue
,
2315 Value
*OffsetValue
= nullptr,
2316 Value
**TheCheck
= nullptr) {
2317 assert(isa
<PointerType
>(PtrValue
->getType()) &&
2318 "trying to create an alignment assumption on a non-pointer?");
2319 assert(Alignment
!= 0 && "Invalid Alignment");
2320 auto *PtrTy
= cast
<PointerType
>(PtrValue
->getType());
2321 Type
*IntPtrTy
= getIntPtrTy(DL
, PtrTy
->getAddressSpace());
2323 Value
*Mask
= ConstantInt::get(IntPtrTy
, Alignment
- 1);
2324 return CreateAlignmentAssumptionHelper(DL
, PtrValue
, Mask
, IntPtrTy
,
2325 OffsetValue
, TheCheck
);
2328 /// Create an assume intrinsic call that represents an alignment
2329 /// assumption on the provided pointer.
2331 /// An optional offset can be provided, and if it is provided, the offset
2332 /// must be subtracted from the provided pointer to get the pointer with the
2333 /// specified alignment.
2335 /// It may be sometimes useful to do some other logic
2336 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2338 /// This overload handles the condition where the Alignment is dependent
2339 /// on an existing value rather than a static value.
2340 CallInst
*CreateAlignmentAssumption(const DataLayout
&DL
, Value
*PtrValue
,
2342 Value
*OffsetValue
= nullptr,
2343 Value
**TheCheck
= nullptr) {
2344 assert(isa
<PointerType
>(PtrValue
->getType()) &&
2345 "trying to create an alignment assumption on a non-pointer?");
2346 auto *PtrTy
= cast
<PointerType
>(PtrValue
->getType());
2347 Type
*IntPtrTy
= getIntPtrTy(DL
, PtrTy
->getAddressSpace());
2349 if (Alignment
->getType() != IntPtrTy
)
2350 Alignment
= CreateIntCast(Alignment
, IntPtrTy
, /*isSigned*/ false,
2353 Value
*Mask
= CreateSub(Alignment
, ConstantInt::get(IntPtrTy
, 1), "mask");
2355 return CreateAlignmentAssumptionHelper(DL
, PtrValue
, Mask
, IntPtrTy
,
2356 OffsetValue
, TheCheck
);
2360 // Create wrappers for C Binding types (see CBindingWrapping.h).
2361 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder
<>, LLVMBuilderRef
)
2363 } // end namespace llvm
2365 #endif // LLVM_IR_IRBUILDER_H