1//===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_IRBUILDER_H
15#define LLVM_IR_IRBUILDER_H
16
17#include "llvm-c/Types.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.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/FPEnv.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/GlobalVariable.h"
32#include "llvm/IR/InstrTypes.h"
33#include "llvm/IR/Instruction.h"
34#include "llvm/IR/Instructions.h"
35#include "llvm/IR/Intrinsics.h"
36#include "llvm/IR/LLVMContext.h"
37#include "llvm/IR/Module.h"
38#include "llvm/IR/Operator.h"
39#include "llvm/IR/Type.h"
40#include "llvm/IR/Value.h"
41#include "llvm/IR/ValueHandle.h"
42#include "llvm/Support/AtomicOrdering.h"
43#include "llvm/Support/CBindingWrapping.h"
44#include "llvm/Support/Casting.h"
45#include <cassert>
46#include <cstdint>
47#include <functional>
48#include <optional>
49#include <utility>
50
51namespace llvm {
52
53class APInt;
54class Use;
55
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.
59///
60/// By default, this inserts the instruction at the insertion point.
61class IRBuilderDefaultInserter {
62public:
63 virtual ~IRBuilderDefaultInserter();
64
65 virtual void InsertHelper(Instruction *I, const Twine &Name,
66 BasicBlock *BB,
67 BasicBlock::iterator InsertPt) const {
68 if (BB)
69 I->insertInto(ParentBB: BB, It: InsertPt);
70 I->setName(Name);
71 }
72};
73
74/// Provides an 'InsertHelper' that calls a user-provided callback after
75/// performing the default insertion.
76class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
77 std::function<void(Instruction *)> Callback;
78
79public:
80 ~IRBuilderCallbackInserter() override;
81
82 IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
83 : Callback(std::move(Callback)) {}
84
85 void InsertHelper(Instruction *I, const Twine &Name,
86 BasicBlock *BB,
87 BasicBlock::iterator InsertPt) const override {
88 IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
89 Callback(I);
90 }
91};
92
93/// Common base class shared among various IRBuilders.
94class IRBuilderBase {
95 /// Pairs of (metadata kind, MDNode *) that should be added to all newly
96 /// created instructions, like !dbg metadata.
97 SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy;
98
99 /// Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not
100 /// null. If \p MD is null, remove the entry with \p Kind.
101 void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) {
102 if (!MD) {
103 erase_if(C&: MetadataToCopy, P: [Kind](const std::pair<unsigned, MDNode *> &KV) {
104 return KV.first == Kind;
105 });
106 return;
107 }
108
109 for (auto &KV : MetadataToCopy)
110 if (KV.first == Kind) {
111 KV.second = MD;
112 return;
113 }
114
115 MetadataToCopy.emplace_back(Args&: Kind, Args&: MD);
116 }
117
118protected:
119 BasicBlock *BB;
120 BasicBlock::iterator InsertPt;
121 LLVMContext &Context;
122 const IRBuilderFolder &Folder;
123 const IRBuilderDefaultInserter &Inserter;
124
125 MDNode *DefaultFPMathTag;
126 FastMathFlags FMF;
127
128 bool IsFPConstrained = false;
129 fp::ExceptionBehavior DefaultConstrainedExcept = fp::ebStrict;
130 RoundingMode DefaultConstrainedRounding = RoundingMode::Dynamic;
131
132 ArrayRef<OperandBundleDef> DefaultOperandBundles;
133
134public:
135 IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
136 const IRBuilderDefaultInserter &Inserter, MDNode *FPMathTag,
137 ArrayRef<OperandBundleDef> OpBundles)
138 : Context(context), Folder(Folder), Inserter(Inserter),
139 DefaultFPMathTag(FPMathTag), DefaultOperandBundles(OpBundles) {
140 ClearInsertionPoint();
141 }
142
143 /// Insert and return the specified instruction.
144 template<typename InstTy>
145 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
146 Inserter.InsertHelper(I, Name, BB, InsertPt);
147 AddMetadataToInst(I);
148 return I;
149 }
150
151 /// No-op overload to handle constants.
152 Constant *Insert(Constant *C, const Twine& = "") const {
153 return C;
154 }
155
156 Value *Insert(Value *V, const Twine &Name = "") const {
157 if (Instruction *I = dyn_cast<Instruction>(Val: V))
158 return Insert(I, Name);
159 assert(isa<Constant>(V));
160 return V;
161 }
162
163 //===--------------------------------------------------------------------===//
164 // Builder configuration methods
165 //===--------------------------------------------------------------------===//
166
167 /// Clear the insertion point: created instructions will not be
168 /// inserted into a block.
169 void ClearInsertionPoint() {
170 BB = nullptr;
171 InsertPt = BasicBlock::iterator();
172 }
173
174 BasicBlock *GetInsertBlock() const { return BB; }
175 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
176 LLVMContext &getContext() const { return Context; }
177
178 /// This specifies that created instructions should be appended to the
179 /// end of the specified block.
180 void SetInsertPoint(BasicBlock *TheBB) {
181 BB = TheBB;
182 InsertPt = BB->end();
183 }
184
185 /// This specifies that created instructions should be inserted before
186 /// the specified instruction.
187 void SetInsertPoint(Instruction *I) {
188 BB = I->getParent();
189 InsertPt = I->getIterator();
190 assert(InsertPt != BB->end() && "Can't read debug loc from end()");
191 SetCurrentDebugLocation(I->getStableDebugLoc());
192 }
193
194 /// This specifies that created instructions should be inserted at the
195 /// specified point.
196 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
197 BB = TheBB;
198 InsertPt = IP;
199 if (IP != TheBB->end())
200 SetCurrentDebugLocation(IP->getStableDebugLoc());
201 }
202
203 /// This specifies that created instructions should be inserted at
204 /// the specified point, but also requires that \p IP is dereferencable.
205 void SetInsertPoint(BasicBlock::iterator IP) {
206 BB = IP->getParent();
207 InsertPt = IP;
208 SetCurrentDebugLocation(IP->getStableDebugLoc());
209 }
210
211 /// This specifies that created instructions should inserted at the beginning
212 /// end of the specified function, but after already existing static alloca
213 /// instructions that are at the start.
214 void SetInsertPointPastAllocas(Function *F) {
215 BB = &F->getEntryBlock();
216 InsertPt = BB->getFirstNonPHIOrDbgOrAlloca();
217 }
218
219 /// Set location information used by debugging information.
220 void SetCurrentDebugLocation(DebugLoc L) {
221 AddOrRemoveMetadataToCopy(Kind: LLVMContext::MD_dbg, MD: L.getAsMDNode());
222 }
223
224 /// Set nosanitize metadata.
225 void SetNoSanitizeMetadata() {
226 AddOrRemoveMetadataToCopy(Kind: llvm::LLVMContext::MD_nosanitize,
227 MD: llvm::MDNode::get(Context&: getContext(), MDs: std::nullopt));
228 }
229
230 /// Collect metadata with IDs \p MetadataKinds from \p Src which should be
231 /// added to all created instructions. Entries present in MedataDataToCopy but
232 /// not on \p Src will be dropped from MetadataToCopy.
233 void CollectMetadataToCopy(Instruction *Src,
234 ArrayRef<unsigned> MetadataKinds) {
235 for (unsigned K : MetadataKinds)
236 AddOrRemoveMetadataToCopy(Kind: K, MD: Src->getMetadata(KindID: K));
237 }
238
239 /// Get location information used by debugging information.
240 DebugLoc getCurrentDebugLocation() const;
241
242 /// If this builder has a current debug location, set it on the
243 /// specified instruction.
244 void SetInstDebugLocation(Instruction *I) const;
245
246 /// Add all entries in MetadataToCopy to \p I.
247 void AddMetadataToInst(Instruction *I) const {
248 for (const auto &KV : MetadataToCopy)
249 I->setMetadata(KindID: KV.first, Node: KV.second);
250 }
251
252 /// Get the return type of the current function that we're emitting
253 /// into.
254 Type *getCurrentFunctionReturnType() const;
255
256 /// InsertPoint - A saved insertion point.
257 class InsertPoint {
258 BasicBlock *Block = nullptr;
259 BasicBlock::iterator Point;
260
261 public:
262 /// Creates a new insertion point which doesn't point to anything.
263 InsertPoint() = default;
264
265 /// Creates a new insertion point at the given location.
266 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
267 : Block(InsertBlock), Point(InsertPoint) {}
268
269 /// Returns true if this insert point is set.
270 bool isSet() const { return (Block != nullptr); }
271
272 BasicBlock *getBlock() const { return Block; }
273 BasicBlock::iterator getPoint() const { return Point; }
274 };
275
276 /// Returns the current insert point.
277 InsertPoint saveIP() const {
278 return InsertPoint(GetInsertBlock(), GetInsertPoint());
279 }
280
281 /// Returns the current insert point, clearing it in the process.
282 InsertPoint saveAndClearIP() {
283 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
284 ClearInsertionPoint();
285 return IP;
286 }
287
288 /// Sets the current insert point to a previously-saved location.
289 void restoreIP(InsertPoint IP) {
290 if (IP.isSet())
291 SetInsertPoint(TheBB: IP.getBlock(), IP: IP.getPoint());
292 else
293 ClearInsertionPoint();
294 }
295
296 /// Get the floating point math metadata being used.
297 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
298
299 /// Get the flags to be applied to created floating point ops
300 FastMathFlags getFastMathFlags() const { return FMF; }
301
302 FastMathFlags &getFastMathFlags() { return FMF; }
303
304 /// Clear the fast-math flags.
305 void clearFastMathFlags() { FMF.clear(); }
306
307 /// Set the floating point math metadata to be used.
308 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
309
310 /// Set the fast-math flags to be used with generated fp-math operators
311 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
312
313 /// Enable/Disable use of constrained floating point math. When
314 /// enabled the CreateF<op>() calls instead create constrained
315 /// floating point intrinsic calls. Fast math flags are unaffected
316 /// by this setting.
317 void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
318
319 /// Query for the use of constrained floating point math
320 bool getIsFPConstrained() { return IsFPConstrained; }
321
322 /// Set the exception handling to be used with constrained floating point
323 void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
324#ifndef NDEBUG
325 std::optional<StringRef> ExceptStr =
326 convertExceptionBehaviorToStr(NewExcept);
327 assert(ExceptStr && "Garbage strict exception behavior!");
328#endif
329 DefaultConstrainedExcept = NewExcept;
330 }
331
332 /// Set the rounding mode handling to be used with constrained floating point
333 void setDefaultConstrainedRounding(RoundingMode NewRounding) {
334#ifndef NDEBUG
335 std::optional<StringRef> RoundingStr =
336 convertRoundingModeToStr(NewRounding);
337 assert(RoundingStr && "Garbage strict rounding mode!");
338#endif
339 DefaultConstrainedRounding = NewRounding;
340 }
341
342 /// Get the exception handling used with constrained floating point
343 fp::ExceptionBehavior getDefaultConstrainedExcept() {
344 return DefaultConstrainedExcept;
345 }
346
347 /// Get the rounding mode handling used with constrained floating point
348 RoundingMode getDefaultConstrainedRounding() {
349 return DefaultConstrainedRounding;
350 }
351
352 void setConstrainedFPFunctionAttr() {
353 assert(BB && "Must have a basic block to set any function attributes!");
354
355 Function *F = BB->getParent();
356 if (!F->hasFnAttribute(Attribute::StrictFP)) {
357 F->addFnAttr(Attribute::StrictFP);
358 }
359 }
360
361 void setConstrainedFPCallAttr(CallBase *I) {
362 I->addFnAttr(Attribute::StrictFP);
363 }
364
365 void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) {
366 DefaultOperandBundles = OpBundles;
367 }
368
369 //===--------------------------------------------------------------------===//
370 // RAII helpers.
371 //===--------------------------------------------------------------------===//
372
373 // RAII object that stores the current insertion point and restores it
374 // when the object is destroyed. This includes the debug location.
375 class InsertPointGuard {
376 IRBuilderBase &Builder;
377 AssertingVH<BasicBlock> Block;
378 BasicBlock::iterator Point;
379 DebugLoc DbgLoc;
380
381 public:
382 InsertPointGuard(IRBuilderBase &B)
383 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
384 DbgLoc(B.getCurrentDebugLocation()) {}
385
386 InsertPointGuard(const InsertPointGuard &) = delete;
387 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
388
389 ~InsertPointGuard() {
390 Builder.restoreIP(IP: InsertPoint(Block, Point));
391 Builder.SetCurrentDebugLocation(DbgLoc);
392 }
393 };
394
395 // RAII object that stores the current fast math settings and restores
396 // them when the object is destroyed.
397 class FastMathFlagGuard {
398 IRBuilderBase &Builder;
399 FastMathFlags FMF;
400 MDNode *FPMathTag;
401 bool IsFPConstrained;
402 fp::ExceptionBehavior DefaultConstrainedExcept;
403 RoundingMode DefaultConstrainedRounding;
404
405 public:
406 FastMathFlagGuard(IRBuilderBase &B)
407 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag),
408 IsFPConstrained(B.IsFPConstrained),
409 DefaultConstrainedExcept(B.DefaultConstrainedExcept),
410 DefaultConstrainedRounding(B.DefaultConstrainedRounding) {}
411
412 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
413 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
414
415 ~FastMathFlagGuard() {
416 Builder.FMF = FMF;
417 Builder.DefaultFPMathTag = FPMathTag;
418 Builder.IsFPConstrained = IsFPConstrained;
419 Builder.DefaultConstrainedExcept = DefaultConstrainedExcept;
420 Builder.DefaultConstrainedRounding = DefaultConstrainedRounding;
421 }
422 };
423
424 // RAII object that stores the current default operand bundles and restores
425 // them when the object is destroyed.
426 class OperandBundlesGuard {
427 IRBuilderBase &Builder;
428 ArrayRef<OperandBundleDef> DefaultOperandBundles;
429
430 public:
431 OperandBundlesGuard(IRBuilderBase &B)
432 : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {}
433
434 OperandBundlesGuard(const OperandBundlesGuard &) = delete;
435 OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete;
436
437 ~OperandBundlesGuard() {
438 Builder.DefaultOperandBundles = DefaultOperandBundles;
439 }
440 };
441
442
443 //===--------------------------------------------------------------------===//
444 // Miscellaneous creation methods.
445 //===--------------------------------------------------------------------===//
446
447 /// Make a new global variable with initializer type i8*
448 ///
449 /// Make a new global variable with an initializer that has array of i8 type
450 /// filled in with the null terminated string value specified. The new global
451 /// variable will be marked mergable with any others of the same contents. If
452 /// Name is specified, it is the name of the global variable created.
453 ///
454 /// If no module is given via \p M, it is take from the insertion point basic
455 /// block.
456 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
457 unsigned AddressSpace = 0,
458 Module *M = nullptr);
459
460 /// Get a constant value representing either true or false.
461 ConstantInt *getInt1(bool V) {
462 return ConstantInt::get(Ty: getInt1Ty(), V);
463 }
464
465 /// Get the constant value for i1 true.
466 ConstantInt *getTrue() {
467 return ConstantInt::getTrue(Context);
468 }
469
470 /// Get the constant value for i1 false.
471 ConstantInt *getFalse() {
472 return ConstantInt::getFalse(Context);
473 }
474
475 /// Get a constant 8-bit value.
476 ConstantInt *getInt8(uint8_t C) {
477 return ConstantInt::get(Ty: getInt8Ty(), V: C);
478 }
479
480 /// Get a constant 16-bit value.
481 ConstantInt *getInt16(uint16_t C) {
482 return ConstantInt::get(Ty: getInt16Ty(), V: C);
483 }
484
485 /// Get a constant 32-bit value.
486 ConstantInt *getInt32(uint32_t C) {
487 return ConstantInt::get(Ty: getInt32Ty(), V: C);
488 }
489
490 /// Get a constant 64-bit value.
491 ConstantInt *getInt64(uint64_t C) {
492 return ConstantInt::get(Ty: getInt64Ty(), V: C);
493 }
494
495 /// Get a constant N-bit value, zero extended or truncated from
496 /// a 64-bit value.
497 ConstantInt *getIntN(unsigned N, uint64_t C) {
498 return ConstantInt::get(Ty: getIntNTy(N), V: C);
499 }
500
501 /// Get a constant integer value.
502 ConstantInt *getInt(const APInt &AI) {
503 return ConstantInt::get(Context, V: AI);
504 }
505
506 //===--------------------------------------------------------------------===//
507 // Type creation methods
508 //===--------------------------------------------------------------------===//
509
510 /// Fetch the type representing a single bit
511 IntegerType *getInt1Ty() {
512 return Type::getInt1Ty(C&: Context);
513 }
514
515 /// Fetch the type representing an 8-bit integer.
516 IntegerType *getInt8Ty() {
517 return Type::getInt8Ty(C&: Context);
518 }
519
520 /// Fetch the type representing a 16-bit integer.
521 IntegerType *getInt16Ty() {
522 return Type::getInt16Ty(C&: Context);
523 }
524
525 /// Fetch the type representing a 32-bit integer.
526 IntegerType *getInt32Ty() {
527 return Type::getInt32Ty(C&: Context);
528 }
529
530 /// Fetch the type representing a 64-bit integer.
531 IntegerType *getInt64Ty() {
532 return Type::getInt64Ty(C&: Context);
533 }
534
535 /// Fetch the type representing a 128-bit integer.
536 IntegerType *getInt128Ty() { return Type::getInt128Ty(C&: Context); }
537
538 /// Fetch the type representing an N-bit integer.
539 IntegerType *getIntNTy(unsigned N) {
540 return Type::getIntNTy(C&: Context, N);
541 }
542
543 /// Fetch the type representing a 16-bit floating point value.
544 Type *getHalfTy() {
545 return Type::getHalfTy(C&: Context);
546 }
547
548 /// Fetch the type representing a 16-bit brain floating point value.
549 Type *getBFloatTy() {
550 return Type::getBFloatTy(C&: Context);
551 }
552
553 /// Fetch the type representing a 32-bit floating point value.
554 Type *getFloatTy() {
555 return Type::getFloatTy(C&: Context);
556 }
557
558 /// Fetch the type representing a 64-bit floating point value.
559 Type *getDoubleTy() {
560 return Type::getDoubleTy(C&: Context);
561 }
562
563 /// Fetch the type representing void.
564 Type *getVoidTy() {
565 return Type::getVoidTy(C&: Context);
566 }
567
568 /// Fetch the type representing a pointer.
569 PointerType *getPtrTy(unsigned AddrSpace = 0) {
570 return PointerType::get(C&: Context, AddressSpace: AddrSpace);
571 }
572
573 /// Fetch the type of an integer with size at least as big as that of a
574 /// pointer in the given address space.
575 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
576 return DL.getIntPtrType(C&: Context, AddressSpace: AddrSpace);
577 }
578
579 /// Fetch the type of an integer that should be used to index GEP operations
580 /// within AddressSpace.
581 IntegerType *getIndexTy(const DataLayout &DL, unsigned AddrSpace) {
582 return DL.getIndexType(C&: Context, AddressSpace: AddrSpace);
583 }
584
585 //===--------------------------------------------------------------------===//
586 // Intrinsic creation methods
587 //===--------------------------------------------------------------------===//
588
589 /// Create and insert a memset to the specified pointer and the
590 /// specified value.
591 ///
592 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
593 /// specified, it will be added to the instruction. Likewise with alias.scope
594 /// and noalias tags.
595 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
596 MaybeAlign Align, bool isVolatile = false,
597 MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
598 MDNode *NoAliasTag = nullptr) {
599 return CreateMemSet(Ptr, Val, Size: getInt64(C: Size), Align, isVolatile,
600 TBAATag, ScopeTag, NoAliasTag);
601 }
602
603 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
604 bool isVolatile = false, MDNode *TBAATag = nullptr,
605 MDNode *ScopeTag = nullptr,
606 MDNode *NoAliasTag = nullptr);
607
608 CallInst *CreateMemSetInline(Value *Dst, MaybeAlign DstAlign, Value *Val,
609 Value *Size, bool IsVolatile = false,
610 MDNode *TBAATag = nullptr,
611 MDNode *ScopeTag = nullptr,
612 MDNode *NoAliasTag = nullptr);
613
614 /// Create and insert an element unordered-atomic memset of the region of
615 /// memory starting at the given pointer to the given value.
616 ///
617 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
618 /// specified, it will be added to the instruction. Likewise with alias.scope
619 /// and noalias tags.
620 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
621 uint64_t Size, Align Alignment,
622 uint32_t ElementSize,
623 MDNode *TBAATag = nullptr,
624 MDNode *ScopeTag = nullptr,
625 MDNode *NoAliasTag = nullptr) {
626 return CreateElementUnorderedAtomicMemSet(Ptr, Val, Size: getInt64(C: Size),
627 Alignment: Align(Alignment), ElementSize,
628 TBAATag, ScopeTag, NoAliasTag);
629 }
630
631 CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
632 Value *ArraySize, ArrayRef<OperandBundleDef> OpB,
633 Function *MallocF = nullptr, const Twine &Name = "");
634
635 /// CreateMalloc - Generate the IR for a call to malloc:
636 /// 1. Compute the malloc call's argument as the specified type's size,
637 /// possibly multiplied by the array size if the array size is not
638 /// constant 1.
639 /// 2. Call malloc with that argument.
640 CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
641 Value *ArraySize, Function *MallocF = nullptr,
642 const Twine &Name = "");
643 /// Generate the IR for a call to the builtin free function.
644 CallInst *CreateFree(Value *Source,
645 ArrayRef<OperandBundleDef> Bundles = std::nullopt);
646
647 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
648 Value *Size, Align Alignment,
649 uint32_t ElementSize,
650 MDNode *TBAATag = nullptr,
651 MDNode *ScopeTag = nullptr,
652 MDNode *NoAliasTag = nullptr);
653
654 /// Create and insert a memcpy between the specified pointers.
655 ///
656 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
657 /// specified, it will be added to the instruction. Likewise with alias.scope
658 /// and noalias tags.
659 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
660 MaybeAlign SrcAlign, uint64_t Size,
661 bool isVolatile = false, MDNode *TBAATag = nullptr,
662 MDNode *TBAAStructTag = nullptr,
663 MDNode *ScopeTag = nullptr,
664 MDNode *NoAliasTag = nullptr) {
665 return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, Size: getInt64(C: Size),
666 isVolatile, TBAATag, TBAAStructTag, ScopeTag,
667 NoAliasTag);
668 }
669
670 CallInst *CreateMemTransferInst(
671 Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
672 MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
673 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
674 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
675
676 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
677 MaybeAlign SrcAlign, Value *Size,
678 bool isVolatile = false, MDNode *TBAATag = nullptr,
679 MDNode *TBAAStructTag = nullptr,
680 MDNode *ScopeTag = nullptr,
681 MDNode *NoAliasTag = nullptr) {
682 return CreateMemTransferInst(Intrinsic::IntrID: memcpy, Dst, DstAlign, Src,
683 SrcAlign, Size, isVolatile, TBAATag,
684 TBAAStructTag, ScopeTag, NoAliasTag);
685 }
686
687 CallInst *
688 CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
689 MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
690 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
691 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) {
692 return CreateMemTransferInst(Intrinsic::IntrID: memcpy_inline, Dst, DstAlign, Src,
693 SrcAlign, Size, isVolatile, TBAATag,
694 TBAAStructTag, ScopeTag, NoAliasTag);
695 }
696
697 /// Create and insert an element unordered-atomic memcpy between the
698 /// specified pointers.
699 ///
700 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
701 ///
702 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
703 /// specified, it will be added to the instruction. Likewise with alias.scope
704 /// and noalias tags.
705 CallInst *CreateElementUnorderedAtomicMemCpy(
706 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
707 uint32_t ElementSize, MDNode *TBAATag = nullptr,
708 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
709 MDNode *NoAliasTag = nullptr);
710
711 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
712 MaybeAlign SrcAlign, uint64_t Size,
713 bool isVolatile = false, MDNode *TBAATag = nullptr,
714 MDNode *ScopeTag = nullptr,
715 MDNode *NoAliasTag = nullptr) {
716 return CreateMemMove(Dst, DstAlign, Src, SrcAlign, Size: getInt64(C: Size),
717 isVolatile, TBAATag, ScopeTag, NoAliasTag);
718 }
719
720 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
721 MaybeAlign SrcAlign, Value *Size,
722 bool isVolatile = false, MDNode *TBAATag = nullptr,
723 MDNode *ScopeTag = nullptr,
724 MDNode *NoAliasTag = nullptr) {
725 return CreateMemTransferInst(Intrinsic::IntrID: memmove, Dst, DstAlign, Src,
726 SrcAlign, Size, isVolatile, TBAATag,
727 /*TBAAStructTag=*/TBAAStructTag: nullptr, ScopeTag,
728 NoAliasTag);
729 }
730
731 /// \brief Create and insert an element unordered-atomic memmove between the
732 /// specified pointers.
733 ///
734 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
735 /// respectively.
736 ///
737 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
738 /// specified, it will be added to the instruction. Likewise with alias.scope
739 /// and noalias tags.
740 CallInst *CreateElementUnorderedAtomicMemMove(
741 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
742 uint32_t ElementSize, MDNode *TBAATag = nullptr,
743 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
744 MDNode *NoAliasTag = nullptr);
745
746private:
747 CallInst *getReductionIntrinsic(Intrinsic::ID ID, Value *Src);
748
749public:
750 /// Create a sequential vector fadd reduction intrinsic of the source vector.
751 /// The first parameter is a scalar accumulator value. An unordered reduction
752 /// can be created by adding the reassoc fast-math flag to the resulting
753 /// sequential reduction.
754 CallInst *CreateFAddReduce(Value *Acc, Value *Src);
755
756 /// Create a sequential vector fmul reduction intrinsic of the source vector.
757 /// The first parameter is a scalar accumulator value. An unordered reduction
758 /// can be created by adding the reassoc fast-math flag to the resulting
759 /// sequential reduction.
760 CallInst *CreateFMulReduce(Value *Acc, Value *Src);
761
762 /// Create a vector int add reduction intrinsic of the source vector.
763 CallInst *CreateAddReduce(Value *Src);
764
765 /// Create a vector int mul reduction intrinsic of the source vector.
766 CallInst *CreateMulReduce(Value *Src);
767
768 /// Create a vector int AND reduction intrinsic of the source vector.
769 CallInst *CreateAndReduce(Value *Src);
770
771 /// Create a vector int OR reduction intrinsic of the source vector.
772 CallInst *CreateOrReduce(Value *Src);
773
774 /// Create a vector int XOR reduction intrinsic of the source vector.
775 CallInst *CreateXorReduce(Value *Src);
776
777 /// Create a vector integer max reduction intrinsic of the source
778 /// vector.
779 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
780
781 /// Create a vector integer min reduction intrinsic of the source
782 /// vector.
783 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
784
785 /// Create a vector float max reduction intrinsic of the source
786 /// vector.
787 CallInst *CreateFPMaxReduce(Value *Src);
788
789 /// Create a vector float min reduction intrinsic of the source
790 /// vector.
791 CallInst *CreateFPMinReduce(Value *Src);
792
793 /// Create a vector float maximum reduction intrinsic of the source
794 /// vector. This variant follows the NaN and signed zero semantic of
795 /// llvm.maximum intrinsic.
796 CallInst *CreateFPMaximumReduce(Value *Src);
797
798 /// Create a vector float minimum reduction intrinsic of the source
799 /// vector. This variant follows the NaN and signed zero semantic of
800 /// llvm.minimum intrinsic.
801 CallInst *CreateFPMinimumReduce(Value *Src);
802
803 /// Create a lifetime.start intrinsic.
804 ///
805 /// If the pointer isn't i8* it will be converted.
806 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
807
808 /// Create a lifetime.end intrinsic.
809 ///
810 /// If the pointer isn't i8* it will be converted.
811 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
812
813 /// Create a call to invariant.start intrinsic.
814 ///
815 /// If the pointer isn't i8* it will be converted.
816 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
817
818 /// Create a call to llvm.threadlocal.address intrinsic.
819 CallInst *CreateThreadLocalAddress(Value *Ptr);
820
821 /// Create a call to Masked Load intrinsic
822 CallInst *CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask,
823 Value *PassThru = nullptr, const Twine &Name = "");
824
825 /// Create a call to Masked Store intrinsic
826 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment,
827 Value *Mask);
828
829 /// Create a call to Masked Gather intrinsic
830 CallInst *CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment,
831 Value *Mask = nullptr, Value *PassThru = nullptr,
832 const Twine &Name = "");
833
834 /// Create a call to Masked Scatter intrinsic
835 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment,
836 Value *Mask = nullptr);
837
838 /// Create a call to Masked Expand Load intrinsic
839 CallInst *CreateMaskedExpandLoad(Type *Ty, Value *Ptr, Value *Mask = nullptr,
840 Value *PassThru = nullptr,
841 const Twine &Name = "");
842
843 /// Create a call to Masked Compress Store intrinsic
844 CallInst *CreateMaskedCompressStore(Value *Val, Value *Ptr,
845 Value *Mask = nullptr);
846
847 /// Return an all true boolean vector (mask) with \p NumElts lanes.
848 Value *getAllOnesMask(ElementCount NumElts) {
849 VectorType *VTy = VectorType::get(ElementType: Type::getInt1Ty(C&: Context), EC: NumElts);
850 return Constant::getAllOnesValue(Ty: VTy);
851 }
852
853 /// Create an assume intrinsic call that allows the optimizer to
854 /// assume that the provided condition will be true.
855 ///
856 /// The optional argument \p OpBundles specifies operand bundles that are
857 /// added to the call instruction.
858 CallInst *
859 CreateAssumption(Value *Cond,
860 ArrayRef<OperandBundleDef> OpBundles = std::nullopt);
861
862 /// Create a llvm.experimental.noalias.scope.decl intrinsic call.
863 Instruction *CreateNoAliasScopeDeclaration(Value *Scope);
864 Instruction *CreateNoAliasScopeDeclaration(MDNode *ScopeTag) {
865 return CreateNoAliasScopeDeclaration(
866 Scope: MetadataAsValue::get(Context, MD: ScopeTag));
867 }
868
869 /// Create a call to the experimental.gc.statepoint intrinsic to
870 /// start a new statepoint sequence.
871 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
872 FunctionCallee ActualCallee,
873 ArrayRef<Value *> CallArgs,
874 std::optional<ArrayRef<Value *>> DeoptArgs,
875 ArrayRef<Value *> GCArgs,
876 const Twine &Name = "");
877
878 /// Create a call to the experimental.gc.statepoint intrinsic to
879 /// start a new statepoint sequence.
880 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
881 FunctionCallee ActualCallee, uint32_t Flags,
882 ArrayRef<Value *> CallArgs,
883 std::optional<ArrayRef<Use>> TransitionArgs,
884 std::optional<ArrayRef<Use>> DeoptArgs,
885 ArrayRef<Value *> GCArgs,
886 const Twine &Name = "");
887
888 /// Conveninence function for the common case when CallArgs are filled
889 /// in using ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
890 /// .get()'ed to get the Value pointer.
891 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
892 FunctionCallee ActualCallee,
893 ArrayRef<Use> CallArgs,
894 std::optional<ArrayRef<Value *>> DeoptArgs,
895 ArrayRef<Value *> GCArgs,
896 const Twine &Name = "");
897
898 /// Create an invoke to the experimental.gc.statepoint intrinsic to
899 /// start a new statepoint sequence.
900 InvokeInst *
901 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
902 FunctionCallee ActualInvokee, BasicBlock *NormalDest,
903 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
904 std::optional<ArrayRef<Value *>> DeoptArgs,
905 ArrayRef<Value *> GCArgs, const Twine &Name = "");
906
907 /// Create an invoke to the experimental.gc.statepoint intrinsic to
908 /// start a new statepoint sequence.
909 InvokeInst *CreateGCStatepointInvoke(
910 uint64_t ID, uint32_t NumPatchBytes, FunctionCallee ActualInvokee,
911 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
912 ArrayRef<Value *> InvokeArgs, std::optional<ArrayRef<Use>> TransitionArgs,
913 std::optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs,
914 const Twine &Name = "");
915
916 // Convenience function for the common case when CallArgs are filled in using
917 // ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
918 // get the Value *.
919 InvokeInst *
920 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
921 FunctionCallee ActualInvokee, BasicBlock *NormalDest,
922 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
923 std::optional<ArrayRef<Value *>> DeoptArgs,
924 ArrayRef<Value *> GCArgs, const Twine &Name = "");
925
926 /// Create a call to the experimental.gc.result intrinsic to extract
927 /// the result from a call wrapped in a statepoint.
928 CallInst *CreateGCResult(Instruction *Statepoint,
929 Type *ResultType,
930 const Twine &Name = "");
931
932 /// Create a call to the experimental.gc.relocate intrinsics to
933 /// project the relocated value of one pointer from the statepoint.
934 CallInst *CreateGCRelocate(Instruction *Statepoint,
935 int BaseOffset,
936 int DerivedOffset,
937 Type *ResultType,
938 const Twine &Name = "");
939
940 /// Create a call to the experimental.gc.pointer.base intrinsic to get the
941 /// base pointer for the specified derived pointer.
942 CallInst *CreateGCGetPointerBase(Value *DerivedPtr, const Twine &Name = "");
943
944 /// Create a call to the experimental.gc.get.pointer.offset intrinsic to get
945 /// the offset of the specified derived pointer from its base.
946 CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = "");
947
948 /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale
949 /// will be the same type as that of \p Scaling.
950 Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
951
952 /// Create an expression which evaluates to the number of elements in \p EC
953 /// at runtime.
954 Value *CreateElementCount(Type *DstType, ElementCount EC);
955
956 /// Create an expression which evaluates to the number of units in \p Size
957 /// at runtime. This works for both units of bits and bytes.
958 Value *CreateTypeSize(Type *DstType, TypeSize Size);
959
960 /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...>
961 Value *CreateStepVector(Type *DstType, const Twine &Name = "");
962
963 /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
964 /// type.
965 CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
966 Instruction *FMFSource = nullptr,
967 const Twine &Name = "");
968
969 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
970 /// first type.
971 Value *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
972 Instruction *FMFSource = nullptr,
973 const Twine &Name = "");
974
975 /// Create a call to intrinsic \p ID with \p Args, mangled using \p Types. If
976 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
977 /// the intrinsic.
978 CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
979 ArrayRef<Value *> Args,
980 Instruction *FMFSource = nullptr,
981 const Twine &Name = "");
982
983 /// Create a call to intrinsic \p ID with \p RetTy and \p Args. If
984 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
985 /// the intrinsic.
986 CallInst *CreateIntrinsic(Type *RetTy, Intrinsic::ID ID,
987 ArrayRef<Value *> Args,
988 Instruction *FMFSource = nullptr,
989 const Twine &Name = "");
990
991 /// Create call to the minnum intrinsic.
992 Value *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
993 if (IsFPConstrained) {
994 return CreateConstrainedFPUnroundedBinOp(
995 Intrinsic::ID: experimental_constrained_minnum, L: LHS, R: RHS, FMFSource: nullptr, Name);
996 }
997
998 return CreateBinaryIntrinsic(Intrinsic::ID: minnum, LHS, RHS, FMFSource: nullptr, Name);
999 }
1000
1001 /// Create call to the maxnum intrinsic.
1002 Value *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1003 if (IsFPConstrained) {
1004 return CreateConstrainedFPUnroundedBinOp(
1005 Intrinsic::ID: experimental_constrained_maxnum, L: LHS, R: RHS, FMFSource: nullptr, Name);
1006 }
1007
1008 return CreateBinaryIntrinsic(Intrinsic::ID: maxnum, LHS, RHS, FMFSource: nullptr, Name);
1009 }
1010
1011 /// Create call to the minimum intrinsic.
1012 Value *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
1013 return CreateBinaryIntrinsic(Intrinsic::ID: minimum, LHS, RHS, FMFSource: nullptr, Name);
1014 }
1015
1016 /// Create call to the maximum intrinsic.
1017 Value *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
1018 return CreateBinaryIntrinsic(Intrinsic::ID: maximum, LHS, RHS, FMFSource: nullptr, Name);
1019 }
1020
1021 /// Create call to the copysign intrinsic.
1022 Value *CreateCopySign(Value *LHS, Value *RHS,
1023 Instruction *FMFSource = nullptr,
1024 const Twine &Name = "") {
1025 return CreateBinaryIntrinsic(Intrinsic::ID: copysign, LHS, RHS, FMFSource,
1026 Name);
1027 }
1028
1029 /// Create a call to the arithmetic_fence intrinsic.
1030 CallInst *CreateArithmeticFence(Value *Val, Type *DstType,
1031 const Twine &Name = "") {
1032 return CreateIntrinsic(Intrinsic::arithmetic_fence, DstType, Val, nullptr,
1033 Name);
1034 }
1035
1036 /// Create a call to the vector.extract intrinsic.
1037 CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, Value *Idx,
1038 const Twine &Name = "") {
1039 return CreateIntrinsic(Intrinsic::vector_extract,
1040 {DstType, SrcVec->getType()}, {SrcVec, Idx}, nullptr,
1041 Name);
1042 }
1043
1044 /// Create a call to the vector.insert intrinsic.
1045 CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec,
1046 Value *Idx, const Twine &Name = "") {
1047 return CreateIntrinsic(Intrinsic::vector_insert,
1048 {DstType, SubVec->getType()}, {SrcVec, SubVec, Idx},
1049 nullptr, Name);
1050 }
1051
1052 /// Create a call to llvm.stacksave
1053 CallInst *CreateStackSave(const Twine &Name = "") {
1054 const DataLayout &DL = BB->getModule()->getDataLayout();
1055 return CreateIntrinsic(Intrinsic::stacksave, {DL.getAllocaPtrType(Ctx&: Context)},
1056 {}, nullptr, Name);
1057 }
1058
1059 /// Create a call to llvm.stackrestore
1060 CallInst *CreateStackRestore(Value *Ptr, const Twine &Name = "") {
1061 return CreateIntrinsic(Intrinsic::stackrestore, {Ptr->getType()}, {Ptr},
1062 nullptr, Name);
1063 }
1064
1065private:
1066 /// Create a call to a masked intrinsic with given Id.
1067 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
1068 ArrayRef<Type *> OverloadedTypes,
1069 const Twine &Name = "");
1070
1071 //===--------------------------------------------------------------------===//
1072 // Instruction creation methods: Terminators
1073 //===--------------------------------------------------------------------===//
1074
1075private:
1076 /// Helper to add branch weight and unpredictable metadata onto an
1077 /// instruction.
1078 /// \returns The annotated instruction.
1079 template <typename InstTy>
1080 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
1081 if (Weights)
1082 I->setMetadata(LLVMContext::MD_prof, Weights);
1083 if (Unpredictable)
1084 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
1085 return I;
1086 }
1087
1088public:
1089 /// Create a 'ret void' instruction.
1090 ReturnInst *CreateRetVoid() {
1091 return Insert(I: ReturnInst::Create(C&: Context));
1092 }
1093
1094 /// Create a 'ret <val>' instruction.
1095 ReturnInst *CreateRet(Value *V) {
1096 return Insert(I: ReturnInst::Create(C&: Context, retVal: V));
1097 }
1098
1099 /// Create a sequence of N insertvalue instructions,
1100 /// with one Value from the retVals array each, that build a aggregate
1101 /// return value one value at a time, and a ret instruction to return
1102 /// the resulting aggregate value.
1103 ///
1104 /// This is a convenience function for code that uses aggregate return values
1105 /// as a vehicle for having multiple return values.
1106 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
1107 Value *V = PoisonValue::get(T: getCurrentFunctionReturnType());
1108 for (unsigned i = 0; i != N; ++i)
1109 V = CreateInsertValue(Agg: V, Val: retVals[i], Idxs: i, Name: "mrv");
1110 return Insert(I: ReturnInst::Create(C&: Context, retVal: V));
1111 }
1112
1113 /// Create an unconditional 'br label X' instruction.
1114 BranchInst *CreateBr(BasicBlock *Dest) {
1115 return Insert(I: BranchInst::Create(IfTrue: Dest));
1116 }
1117
1118 /// Create a conditional 'br Cond, TrueDest, FalseDest'
1119 /// instruction.
1120 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1121 MDNode *BranchWeights = nullptr,
1122 MDNode *Unpredictable = nullptr) {
1123 return Insert(I: addBranchMetadata(I: BranchInst::Create(IfTrue: True, IfFalse: False, Cond),
1124 Weights: BranchWeights, Unpredictable));
1125 }
1126
1127 /// Create a conditional 'br Cond, TrueDest, FalseDest'
1128 /// instruction. Copy branch meta data if available.
1129 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1130 Instruction *MDSrc) {
1131 BranchInst *Br = BranchInst::Create(IfTrue: True, IfFalse: False, Cond);
1132 if (MDSrc) {
1133 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
1134 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
1135 Br->copyMetadata(SrcInst: *MDSrc, WL);
1136 }
1137 return Insert(I: Br);
1138 }
1139
1140 /// Create a switch instruction with the specified value, default dest,
1141 /// and with a hint for the number of cases that will be added (for efficient
1142 /// allocation).
1143 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
1144 MDNode *BranchWeights = nullptr,
1145 MDNode *Unpredictable = nullptr) {
1146 return Insert(I: addBranchMetadata(I: SwitchInst::Create(Value: V, Default: Dest, NumCases),
1147 Weights: BranchWeights, Unpredictable));
1148 }
1149
1150 /// Create an indirect branch instruction with the specified address
1151 /// operand, with an optional hint for the number of destinations that will be
1152 /// added (for efficient allocation).
1153 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
1154 return Insert(I: IndirectBrInst::Create(Address: Addr, NumDests));
1155 }
1156
1157 /// Create an invoke instruction.
1158 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1159 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1160 ArrayRef<Value *> Args,
1161 ArrayRef<OperandBundleDef> OpBundles,
1162 const Twine &Name = "") {
1163 InvokeInst *II =
1164 InvokeInst::Create(Ty, Func: Callee, IfNormal: NormalDest, IfException: UnwindDest, Args, Bundles: OpBundles);
1165 if (IsFPConstrained)
1166 setConstrainedFPCallAttr(II);
1167 return Insert(I: II, Name);
1168 }
1169 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1170 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1171 ArrayRef<Value *> Args = std::nullopt,
1172 const Twine &Name = "") {
1173 InvokeInst *II =
1174 InvokeInst::Create(Ty, Func: Callee, IfNormal: NormalDest, IfException: UnwindDest, Args);
1175 if (IsFPConstrained)
1176 setConstrainedFPCallAttr(II);
1177 return Insert(I: II, Name);
1178 }
1179
1180 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1181 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
1182 ArrayRef<OperandBundleDef> OpBundles,
1183 const Twine &Name = "") {
1184 return CreateInvoke(Ty: Callee.getFunctionType(), Callee: Callee.getCallee(),
1185 NormalDest, UnwindDest, Args, OpBundles, Name);
1186 }
1187
1188 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1189 BasicBlock *UnwindDest,
1190 ArrayRef<Value *> Args = std::nullopt,
1191 const Twine &Name = "") {
1192 return CreateInvoke(Ty: Callee.getFunctionType(), Callee: Callee.getCallee(),
1193 NormalDest, UnwindDest, Args, Name);
1194 }
1195
1196 /// \brief Create a callbr instruction.
1197 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1198 BasicBlock *DefaultDest,
1199 ArrayRef<BasicBlock *> IndirectDests,
1200 ArrayRef<Value *> Args = std::nullopt,
1201 const Twine &Name = "") {
1202 return Insert(I: CallBrInst::Create(Ty, Func: Callee, DefaultDest, IndirectDests,
1203 Args), Name);
1204 }
1205 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1206 BasicBlock *DefaultDest,
1207 ArrayRef<BasicBlock *> IndirectDests,
1208 ArrayRef<Value *> Args,
1209 ArrayRef<OperandBundleDef> OpBundles,
1210 const Twine &Name = "") {
1211 return Insert(
1212 I: CallBrInst::Create(Ty, Func: Callee, DefaultDest, IndirectDests, Args,
1213 Bundles: OpBundles), Name);
1214 }
1215
1216 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1217 ArrayRef<BasicBlock *> IndirectDests,
1218 ArrayRef<Value *> Args = std::nullopt,
1219 const Twine &Name = "") {
1220 return CreateCallBr(Ty: Callee.getFunctionType(), Callee: Callee.getCallee(),
1221 DefaultDest, IndirectDests, Args, Name);
1222 }
1223 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1224 ArrayRef<BasicBlock *> IndirectDests,
1225 ArrayRef<Value *> Args,
1226 ArrayRef<OperandBundleDef> OpBundles,
1227 const Twine &Name = "") {
1228 return CreateCallBr(Ty: Callee.getFunctionType(), Callee: Callee.getCallee(),
1229 DefaultDest, IndirectDests, Args, Name);
1230 }
1231
1232 ResumeInst *CreateResume(Value *Exn) {
1233 return Insert(I: ResumeInst::Create(Exn));
1234 }
1235
1236 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1237 BasicBlock *UnwindBB = nullptr) {
1238 return Insert(I: CleanupReturnInst::Create(CleanupPad, UnwindBB));
1239 }
1240
1241 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1242 unsigned NumHandlers,
1243 const Twine &Name = "") {
1244 return Insert(I: CatchSwitchInst::Create(ParentPad, UnwindDest: UnwindBB, NumHandlers),
1245 Name);
1246 }
1247
1248 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1249 const Twine &Name = "") {
1250 return Insert(I: CatchPadInst::Create(CatchSwitch: ParentPad, Args), Name);
1251 }
1252
1253 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1254 ArrayRef<Value *> Args = std::nullopt,
1255 const Twine &Name = "") {
1256 return Insert(I: CleanupPadInst::Create(ParentPad, Args), Name);
1257 }
1258
1259 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1260 return Insert(I: CatchReturnInst::Create(CatchPad, BB));
1261 }
1262
1263 UnreachableInst *CreateUnreachable() {
1264 return Insert(I: new UnreachableInst(Context));
1265 }
1266
1267 //===--------------------------------------------------------------------===//
1268 // Instruction creation methods: Binary Operators
1269 //===--------------------------------------------------------------------===//
1270private:
1271 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1272 Value *LHS, Value *RHS,
1273 const Twine &Name,
1274 bool HasNUW, bool HasNSW) {
1275 BinaryOperator *BO = Insert(I: BinaryOperator::Create(Op: Opc, S1: LHS, S2: RHS), Name);
1276 if (HasNUW) BO->setHasNoUnsignedWrap();
1277 if (HasNSW) BO->setHasNoSignedWrap();
1278 return BO;
1279 }
1280
1281 Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1282 FastMathFlags FMF) const {
1283 if (!FPMD)
1284 FPMD = DefaultFPMathTag;
1285 if (FPMD)
1286 I->setMetadata(KindID: LLVMContext::MD_fpmath, Node: FPMD);
1287 I->setFastMathFlags(FMF);
1288 return I;
1289 }
1290
1291 Value *getConstrainedFPRounding(std::optional<RoundingMode> Rounding) {
1292 RoundingMode UseRounding = DefaultConstrainedRounding;
1293
1294 if (Rounding)
1295 UseRounding = *Rounding;
1296
1297 std::optional<StringRef> RoundingStr =
1298 convertRoundingModeToStr(UseRounding);
1299 assert(RoundingStr && "Garbage strict rounding mode!");
1300 auto *RoundingMDS = MDString::get(Context, Str: *RoundingStr);
1301
1302 return MetadataAsValue::get(Context, MD: RoundingMDS);
1303 }
1304
1305 Value *getConstrainedFPExcept(std::optional<fp::ExceptionBehavior> Except) {
1306 std::optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(
1307 Except.value_or(u&: DefaultConstrainedExcept));
1308 assert(ExceptStr && "Garbage strict exception behavior!");
1309 auto *ExceptMDS = MDString::get(Context, Str: *ExceptStr);
1310
1311 return MetadataAsValue::get(Context, MD: ExceptMDS);
1312 }
1313
1314 Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) {
1315 assert(CmpInst::isFPPredicate(Predicate) &&
1316 Predicate != CmpInst::FCMP_FALSE &&
1317 Predicate != CmpInst::FCMP_TRUE &&
1318 "Invalid constrained FP comparison predicate!");
1319
1320 StringRef PredicateStr = CmpInst::getPredicateName(P: Predicate);
1321 auto *PredicateMDS = MDString::get(Context, Str: PredicateStr);
1322
1323 return MetadataAsValue::get(Context, MD: PredicateMDS);
1324 }
1325
1326public:
1327 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1328 bool HasNUW = false, bool HasNSW = false) {
1329 if (Value *V =
1330 Folder.FoldNoWrapBinOp(Opc: Instruction::Add, LHS, RHS, HasNUW, HasNSW))
1331 return V;
1332 return CreateInsertNUWNSWBinOp(Opc: Instruction::Add, LHS, RHS, Name, HasNUW,
1333 HasNSW);
1334 }
1335
1336 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1337 return CreateAdd(LHS, RHS, Name, HasNUW: false, HasNSW: true);
1338 }
1339
1340 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1341 return CreateAdd(LHS, RHS, Name, HasNUW: true, HasNSW: false);
1342 }
1343
1344 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1345 bool HasNUW = false, bool HasNSW = false) {
1346 if (Value *V =
1347 Folder.FoldNoWrapBinOp(Opc: Instruction::Sub, LHS, RHS, HasNUW, HasNSW))
1348 return V;
1349 return CreateInsertNUWNSWBinOp(Opc: Instruction::Sub, LHS, RHS, Name, HasNUW,
1350 HasNSW);
1351 }
1352
1353 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1354 return CreateSub(LHS, RHS, Name, HasNUW: false, HasNSW: true);
1355 }
1356
1357 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1358 return CreateSub(LHS, RHS, Name, HasNUW: true, HasNSW: false);
1359 }
1360
1361 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1362 bool HasNUW = false, bool HasNSW = false) {
1363 if (Value *V =
1364 Folder.FoldNoWrapBinOp(Opc: Instruction::Mul, LHS, RHS, HasNUW, HasNSW))
1365 return V;
1366 return CreateInsertNUWNSWBinOp(Opc: Instruction::Mul, LHS, RHS, Name, HasNUW,
1367 HasNSW);
1368 }
1369
1370 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1371 return CreateMul(LHS, RHS, Name, HasNUW: false, HasNSW: true);
1372 }
1373
1374 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1375 return CreateMul(LHS, RHS, Name, HasNUW: true, HasNSW: false);
1376 }
1377
1378 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1379 bool isExact = false) {
1380 if (Value *V = Folder.FoldExactBinOp(Opc: Instruction::UDiv, LHS, RHS, IsExact: isExact))
1381 return V;
1382 if (!isExact)
1383 return Insert(I: BinaryOperator::CreateUDiv(V1: LHS, V2: RHS), Name);
1384 return Insert(I: BinaryOperator::CreateExactUDiv(V1: LHS, V2: RHS), Name);
1385 }
1386
1387 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1388 return CreateUDiv(LHS, RHS, Name, isExact: true);
1389 }
1390
1391 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1392 bool isExact = false) {
1393 if (Value *V = Folder.FoldExactBinOp(Opc: Instruction::SDiv, LHS, RHS, IsExact: isExact))
1394 return V;
1395 if (!isExact)
1396 return Insert(I: BinaryOperator::CreateSDiv(V1: LHS, V2: RHS), Name);
1397 return Insert(I: BinaryOperator::CreateExactSDiv(V1: LHS, V2: RHS), Name);
1398 }
1399
1400 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1401 return CreateSDiv(LHS, RHS, Name, isExact: true);
1402 }
1403
1404 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1405 if (Value *V = Folder.FoldBinOp(Opc: Instruction::URem, LHS, RHS))
1406 return V;
1407 return Insert(I: BinaryOperator::CreateURem(V1: LHS, V2: RHS), Name);
1408 }
1409
1410 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1411 if (Value *V = Folder.FoldBinOp(Opc: Instruction::SRem, LHS, RHS))
1412 return V;
1413 return Insert(I: BinaryOperator::CreateSRem(V1: LHS, V2: RHS), Name);
1414 }
1415
1416 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1417 bool HasNUW = false, bool HasNSW = false) {
1418 if (Value *V =
1419 Folder.FoldNoWrapBinOp(Opc: Instruction::Shl, LHS, RHS, HasNUW, HasNSW))
1420 return V;
1421 return CreateInsertNUWNSWBinOp(Opc: Instruction::Shl, LHS, RHS, Name,
1422 HasNUW, HasNSW);
1423 }
1424
1425 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1426 bool HasNUW = false, bool HasNSW = false) {
1427 return CreateShl(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name,
1428 HasNUW, HasNSW);
1429 }
1430
1431 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1432 bool HasNUW = false, bool HasNSW = false) {
1433 return CreateShl(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name,
1434 HasNUW, HasNSW);
1435 }
1436
1437 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1438 bool isExact = false) {
1439 if (Value *V = Folder.FoldExactBinOp(Opc: Instruction::LShr, LHS, RHS, IsExact: isExact))
1440 return V;
1441 if (!isExact)
1442 return Insert(I: BinaryOperator::CreateLShr(V1: LHS, V2: RHS), Name);
1443 return Insert(I: BinaryOperator::CreateExactLShr(V1: LHS, V2: RHS), Name);
1444 }
1445
1446 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1447 bool isExact = false) {
1448 return CreateLShr(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name,isExact);
1449 }
1450
1451 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1452 bool isExact = false) {
1453 return CreateLShr(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name,isExact);
1454 }
1455
1456 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1457 bool isExact = false) {
1458 if (Value *V = Folder.FoldExactBinOp(Opc: Instruction::AShr, LHS, RHS, IsExact: isExact))
1459 return V;
1460 if (!isExact)
1461 return Insert(I: BinaryOperator::CreateAShr(V1: LHS, V2: RHS), Name);
1462 return Insert(I: BinaryOperator::CreateExactAShr(V1: LHS, V2: RHS), Name);
1463 }
1464
1465 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1466 bool isExact = false) {
1467 return CreateAShr(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name,isExact);
1468 }
1469
1470 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1471 bool isExact = false) {
1472 return CreateAShr(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name,isExact);
1473 }
1474
1475 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1476 if (auto *V = Folder.FoldBinOp(Opc: Instruction::And, LHS, RHS))
1477 return V;
1478 return Insert(I: BinaryOperator::CreateAnd(V1: LHS, V2: RHS), Name);
1479 }
1480
1481 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1482 return CreateAnd(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name);
1483 }
1484
1485 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1486 return CreateAnd(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name);
1487 }
1488
1489 Value *CreateAnd(ArrayRef<Value*> Ops) {
1490 assert(!Ops.empty());
1491 Value *Accum = Ops[0];
1492 for (unsigned i = 1; i < Ops.size(); i++)
1493 Accum = CreateAnd(LHS: Accum, RHS: Ops[i]);
1494 return Accum;
1495 }
1496
1497 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1498 if (auto *V = Folder.FoldBinOp(Opc: Instruction::Or, LHS, RHS))
1499 return V;
1500 return Insert(I: BinaryOperator::CreateOr(V1: LHS, V2: RHS), Name);
1501 }
1502
1503 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1504 return CreateOr(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name);
1505 }
1506
1507 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1508 return CreateOr(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name);
1509 }
1510
1511 Value *CreateOr(ArrayRef<Value*> Ops) {
1512 assert(!Ops.empty());
1513 Value *Accum = Ops[0];
1514 for (unsigned i = 1; i < Ops.size(); i++)
1515 Accum = CreateOr(LHS: Accum, RHS: Ops[i]);
1516 return Accum;
1517 }
1518
1519 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1520 if (Value *V = Folder.FoldBinOp(Opc: Instruction::Xor, LHS, RHS))
1521 return V;
1522 return Insert(I: BinaryOperator::CreateXor(V1: LHS, V2: RHS), Name);
1523 }
1524
1525 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1526 return CreateXor(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name);
1527 }
1528
1529 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1530 return CreateXor(LHS, RHS: ConstantInt::get(Ty: LHS->getType(), V: RHS), Name);
1531 }
1532
1533 Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1534 MDNode *FPMD = nullptr) {
1535 if (IsFPConstrained)
1536 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_fadd,
1537 L, R, FMFSource: nullptr, Name, FPMathTag: FPMD);
1538
1539 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FAdd, LHS: L, RHS: R, FMF))
1540 return V;
1541 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFAdd(V1: L, V2: R), FPMD, FMF);
1542 return Insert(I, Name);
1543 }
1544
1545 /// Copy fast-math-flags from an instruction rather than using the builder's
1546 /// default FMF.
1547 Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
1548 const Twine &Name = "") {
1549 if (IsFPConstrained)
1550 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_fadd,
1551 L, R, FMFSource, Name);
1552
1553 FastMathFlags FMF = FMFSource->getFastMathFlags();
1554 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FAdd, LHS: L, RHS: R, FMF))
1555 return V;
1556 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFAdd(V1: L, V2: R), FPMD: nullptr, FMF);
1557 return Insert(I, Name);
1558 }
1559
1560 Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1561 MDNode *FPMD = nullptr) {
1562 if (IsFPConstrained)
1563 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_fsub,
1564 L, R, FMFSource: nullptr, Name, FPMathTag: FPMD);
1565
1566 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FSub, LHS: L, RHS: R, FMF))
1567 return V;
1568 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFSub(V1: L, V2: R), FPMD, FMF);
1569 return Insert(I, Name);
1570 }
1571
1572 /// Copy fast-math-flags from an instruction rather than using the builder's
1573 /// default FMF.
1574 Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
1575 const Twine &Name = "") {
1576 if (IsFPConstrained)
1577 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_fsub,
1578 L, R, FMFSource, Name);
1579
1580 FastMathFlags FMF = FMFSource->getFastMathFlags();
1581 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FSub, LHS: L, RHS: R, FMF))
1582 return V;
1583 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFSub(V1: L, V2: R), FPMD: nullptr, FMF);
1584 return Insert(I, Name);
1585 }
1586
1587 Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1588 MDNode *FPMD = nullptr) {
1589 if (IsFPConstrained)
1590 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_fmul,
1591 L, R, FMFSource: nullptr, Name, FPMathTag: FPMD);
1592
1593 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FMul, LHS: L, RHS: R, FMF))
1594 return V;
1595 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFMul(V1: L, V2: R), FPMD, FMF);
1596 return Insert(I, Name);
1597 }
1598
1599 /// Copy fast-math-flags from an instruction rather than using the builder's
1600 /// default FMF.
1601 Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
1602 const Twine &Name = "") {
1603 if (IsFPConstrained)
1604 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_fmul,
1605 L, R, FMFSource, Name);
1606
1607 FastMathFlags FMF = FMFSource->getFastMathFlags();
1608 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FMul, LHS: L, RHS: R, FMF))
1609 return V;
1610 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFMul(V1: L, V2: R), FPMD: nullptr, FMF);
1611 return Insert(I, Name);
1612 }
1613
1614 Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1615 MDNode *FPMD = nullptr) {
1616 if (IsFPConstrained)
1617 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_fdiv,
1618 L, R, FMFSource: nullptr, Name, FPMathTag: FPMD);
1619
1620 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FDiv, LHS: L, RHS: R, FMF))
1621 return V;
1622 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFDiv(V1: L, V2: R), FPMD, FMF);
1623 return Insert(I, Name);
1624 }
1625
1626 /// Copy fast-math-flags from an instruction rather than using the builder's
1627 /// default FMF.
1628 Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
1629 const Twine &Name = "") {
1630 if (IsFPConstrained)
1631 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_fdiv,
1632 L, R, FMFSource, Name);
1633
1634 FastMathFlags FMF = FMFSource->getFastMathFlags();
1635 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FDiv, LHS: L, RHS: R, FMF))
1636 return V;
1637 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFDiv(V1: L, V2: R), FPMD: nullptr, FMF);
1638 return Insert(I, Name);
1639 }
1640
1641 Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1642 MDNode *FPMD = nullptr) {
1643 if (IsFPConstrained)
1644 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_frem,
1645 L, R, FMFSource: nullptr, Name, FPMathTag: FPMD);
1646
1647 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FRem, LHS: L, RHS: R, FMF)) return V;
1648 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFRem(V1: L, V2: R), FPMD, FMF);
1649 return Insert(I, Name);
1650 }
1651
1652 /// Copy fast-math-flags from an instruction rather than using the builder's
1653 /// default FMF.
1654 Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
1655 const Twine &Name = "") {
1656 if (IsFPConstrained)
1657 return CreateConstrainedFPBinOp(Intrinsic::ID: experimental_constrained_frem,
1658 L, R, FMFSource, Name);
1659
1660 FastMathFlags FMF = FMFSource->getFastMathFlags();
1661 if (Value *V = Folder.FoldBinOpFMF(Opc: Instruction::FRem, LHS: L, RHS: R, FMF)) return V;
1662 Instruction *I = setFPAttrs(I: BinaryOperator::CreateFRem(V1: L, V2: R), FPMD: nullptr, FMF);
1663 return Insert(I, Name);
1664 }
1665
1666 Value *CreateBinOp(Instruction::BinaryOps Opc,
1667 Value *LHS, Value *RHS, const Twine &Name = "",
1668 MDNode *FPMathTag = nullptr) {
1669 if (Value *V = Folder.FoldBinOp(Opc, LHS, RHS)) return V;
1670 Instruction *BinOp = BinaryOperator::Create(Op: Opc, S1: LHS, S2: RHS);
1671 if (isa<FPMathOperator>(Val: BinOp))
1672 setFPAttrs(I: BinOp, FPMD: FPMathTag, FMF);
1673 return Insert(I: BinOp, Name);
1674 }
1675
1676 Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1677 assert(Cond2->getType()->isIntOrIntVectorTy(1));
1678 return CreateSelect(C: Cond1, True: Cond2,
1679 False: ConstantInt::getNullValue(Ty: Cond2->getType()), Name);
1680 }
1681
1682 Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1683 assert(Cond2->getType()->isIntOrIntVectorTy(1));
1684 return CreateSelect(C: Cond1, True: ConstantInt::getAllOnesValue(Ty: Cond2->getType()),
1685 False: Cond2, Name);
1686 }
1687
1688 Value *CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2,
1689 const Twine &Name = "") {
1690 switch (Opc) {
1691 case Instruction::And:
1692 return CreateLogicalAnd(Cond1, Cond2, Name);
1693 case Instruction::Or:
1694 return CreateLogicalOr(Cond1, Cond2, Name);
1695 default:
1696 break;
1697 }
1698 llvm_unreachable("Not a logical operation.");
1699 }
1700
1701 // NOTE: this is sequential, non-commutative, ordered reduction!
1702 Value *CreateLogicalOr(ArrayRef<Value *> Ops) {
1703 assert(!Ops.empty());
1704 Value *Accum = Ops[0];
1705 for (unsigned i = 1; i < Ops.size(); i++)
1706 Accum = CreateLogicalOr(Cond1: Accum, Cond2: Ops[i]);
1707 return Accum;
1708 }
1709
1710 CallInst *CreateConstrainedFPBinOp(
1711 Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
1712 const Twine &Name = "", MDNode *FPMathTag = nullptr,
1713 std::optional<RoundingMode> Rounding = std::nullopt,
1714 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1715
1716 CallInst *CreateConstrainedFPUnroundedBinOp(
1717 Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
1718 const Twine &Name = "", MDNode *FPMathTag = nullptr,
1719 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1720
1721 Value *CreateNeg(Value *V, const Twine &Name = "", bool HasNSW = false) {
1722 return CreateSub(LHS: Constant::getNullValue(Ty: V->getType()), RHS: V, Name,
1723 /*HasNUW=*/HasNUW: 0, HasNSW);
1724 }
1725
1726 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1727 return CreateNeg(V, Name, /*HasNSW=*/HasNSW: true);
1728 }
1729
1730 Value *CreateFNeg(Value *V, const Twine &Name = "",
1731 MDNode *FPMathTag = nullptr) {
1732 if (Value *Res = Folder.FoldUnOpFMF(Opc: Instruction::FNeg, V, FMF))
1733 return Res;
1734 return Insert(I: setFPAttrs(I: UnaryOperator::CreateFNeg(V), FPMD: FPMathTag, FMF),
1735 Name);
1736 }
1737
1738 /// Copy fast-math-flags from an instruction rather than using the builder's
1739 /// default FMF.
1740 Value *CreateFNegFMF(Value *V, Instruction *FMFSource,
1741 const Twine &Name = "") {
1742 FastMathFlags FMF = FMFSource->getFastMathFlags();
1743 if (Value *Res = Folder.FoldUnOpFMF(Opc: Instruction::FNeg, V, FMF))
1744 return Res;
1745 return Insert(I: setFPAttrs(I: UnaryOperator::CreateFNeg(V), FPMD: nullptr, FMF),
1746 Name);
1747 }
1748
1749 Value *CreateNot(Value *V, const Twine &Name = "") {
1750 return CreateXor(LHS: V, RHS: Constant::getAllOnesValue(Ty: V->getType()), Name);
1751 }
1752
1753 Value *CreateUnOp(Instruction::UnaryOps Opc,
1754 Value *V, const Twine &Name = "",
1755 MDNode *FPMathTag = nullptr) {
1756 if (Value *Res = Folder.FoldUnOpFMF(Opc, V, FMF))
1757 return Res;
1758 Instruction *UnOp = UnaryOperator::Create(Op: Opc, S: V);
1759 if (isa<FPMathOperator>(Val: UnOp))
1760 setFPAttrs(I: UnOp, FPMD: FPMathTag, FMF);
1761 return Insert(I: UnOp, Name);
1762 }
1763
1764 /// Create either a UnaryOperator or BinaryOperator depending on \p Opc.
1765 /// Correct number of operands must be passed accordingly.
1766 Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
1767 const Twine &Name = "", MDNode *FPMathTag = nullptr);
1768
1769 //===--------------------------------------------------------------------===//
1770 // Instruction creation methods: Memory Instructions
1771 //===--------------------------------------------------------------------===//
1772
1773 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1774 Value *ArraySize = nullptr, const Twine &Name = "") {
1775 const DataLayout &DL = BB->getModule()->getDataLayout();
1776 Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1777 return Insert(I: new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1778 }
1779
1780 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1781 const Twine &Name = "") {
1782 const DataLayout &DL = BB->getModule()->getDataLayout();
1783 Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1784 unsigned AddrSpace = DL.getAllocaAddrSpace();
1785 return Insert(I: new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1786 }
1787
1788 /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1789 /// converting the string to 'bool' for the isVolatile parameter.
1790 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1791 return CreateAlignedLoad(Ty, Ptr, Align: MaybeAlign(), Name);
1792 }
1793
1794 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1795 return CreateAlignedLoad(Ty, Ptr, Align: MaybeAlign(), Name);
1796 }
1797
1798 LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1799 const Twine &Name = "") {
1800 return CreateAlignedLoad(Ty, Ptr, Align: MaybeAlign(), isVolatile, Name);
1801 }
1802
1803 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1804 return CreateAlignedStore(Val, Ptr, Align: MaybeAlign(), isVolatile);
1805 }
1806
1807 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1808 const char *Name) {
1809 return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/isVolatile: false, Name);
1810 }
1811
1812 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1813 const Twine &Name = "") {
1814 return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/isVolatile: false, Name);
1815 }
1816
1817 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1818 bool isVolatile, const Twine &Name = "") {
1819 if (!Align) {
1820 const DataLayout &DL = BB->getModule()->getDataLayout();
1821 Align = DL.getABITypeAlign(Ty);
1822 }
1823 return Insert(I: new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
1824 }
1825
1826 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
1827 bool isVolatile = false) {
1828 if (!Align) {
1829 const DataLayout &DL = BB->getModule()->getDataLayout();
1830 Align = DL.getABITypeAlign(Ty: Val->getType());
1831 }
1832 return Insert(I: new StoreInst(Val, Ptr, isVolatile, *Align));
1833 }
1834 FenceInst *CreateFence(AtomicOrdering Ordering,
1835 SyncScope::ID SSID = SyncScope::System,
1836 const Twine &Name = "") {
1837 return Insert(I: new FenceInst(Context, Ordering, SSID), Name);
1838 }
1839
1840 AtomicCmpXchgInst *
1841 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
1842 AtomicOrdering SuccessOrdering,
1843 AtomicOrdering FailureOrdering,
1844 SyncScope::ID SSID = SyncScope::System) {
1845 if (!Align) {
1846 const DataLayout &DL = BB->getModule()->getDataLayout();
1847 Align = llvm::Align(DL.getTypeStoreSize(Ty: New->getType()));
1848 }
1849
1850 return Insert(I: new AtomicCmpXchgInst(Ptr, Cmp, New, *Align, SuccessOrdering,
1851 FailureOrdering, SSID));
1852 }
1853
1854 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr,
1855 Value *Val, MaybeAlign Align,
1856 AtomicOrdering Ordering,
1857 SyncScope::ID SSID = SyncScope::System) {
1858 if (!Align) {
1859 const DataLayout &DL = BB->getModule()->getDataLayout();
1860 Align = llvm::Align(DL.getTypeStoreSize(Ty: Val->getType()));
1861 }
1862
1863 return Insert(I: new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID));
1864 }
1865
1866 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1867 const Twine &Name = "", bool IsInBounds = false) {
1868 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, IsInBounds))
1869 return V;
1870 return Insert(I: IsInBounds
1871 ? GetElementPtrInst::CreateInBounds(PointeeType: Ty, Ptr, IdxList)
1872 : GetElementPtrInst::Create(PointeeType: Ty, Ptr, IdxList),
1873 Name);
1874 }
1875
1876 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1877 const Twine &Name = "") {
1878 return CreateGEP(Ty, Ptr, IdxList, Name, /* IsInBounds */ IsInBounds: true);
1879 }
1880
1881 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1882 const Twine &Name = "") {
1883 Value *Idx = ConstantInt::get(Ty: Type::getInt32Ty(C&: Context), V: Idx0);
1884
1885 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList: Idx, /*IsInBounds=*/IsInBounds: false))
1886 return V;
1887
1888 return Insert(I: GetElementPtrInst::Create(PointeeType: Ty, Ptr, IdxList: Idx), Name);
1889 }
1890
1891 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1892 const Twine &Name = "") {
1893 Value *Idx = ConstantInt::get(Ty: Type::getInt32Ty(C&: Context), V: Idx0);
1894
1895 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList: Idx, /*IsInBounds=*/IsInBounds: true))
1896 return V;
1897
1898 return Insert(I: GetElementPtrInst::CreateInBounds(PointeeType: Ty, Ptr, IdxList: Idx), Name);
1899 }
1900
1901 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1902 const Twine &Name = "") {
1903 Value *Idxs[] = {
1904 ConstantInt::get(Ty: Type::getInt32Ty(C&: Context), V: Idx0),
1905 ConstantInt::get(Ty: Type::getInt32Ty(C&: Context), V: Idx1)
1906 };
1907
1908 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList: Idxs, /*IsInBounds=*/IsInBounds: false))
1909 return V;
1910
1911 return Insert(I: GetElementPtrInst::Create(PointeeType: Ty, Ptr, IdxList: Idxs), Name);
1912 }
1913
1914 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1915 unsigned Idx1, const Twine &Name = "") {
1916 Value *Idxs[] = {
1917 ConstantInt::get(Ty: Type::getInt32Ty(C&: Context), V: Idx0),
1918 ConstantInt::get(Ty: Type::getInt32Ty(C&: Context), V: Idx1)
1919 };
1920
1921 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList: Idxs, /*IsInBounds=*/IsInBounds: true))
1922 return V;
1923
1924 return Insert(I: GetElementPtrInst::CreateInBounds(PointeeType: Ty, Ptr, IdxList: Idxs), Name);
1925 }
1926
1927 Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1928 const Twine &Name = "") {
1929 Value *Idx = ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Idx0);
1930
1931 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList: Idx, /*IsInBounds=*/IsInBounds: false))
1932 return V;
1933
1934 return Insert(I: GetElementPtrInst::Create(PointeeType: Ty, Ptr, IdxList: Idx), Name);
1935 }
1936
1937 Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1938 const Twine &Name = "") {
1939 Value *Idx = ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Idx0);
1940
1941 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList: Idx, /*IsInBounds=*/IsInBounds: true))
1942 return V;
1943
1944 return Insert(I: GetElementPtrInst::CreateInBounds(PointeeType: Ty, Ptr, IdxList: Idx), Name);
1945 }
1946
1947 Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1948 const Twine &Name = "") {
1949 Value *Idxs[] = {
1950 ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Idx0),
1951 ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Idx1)
1952 };
1953
1954 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList: Idxs, /*IsInBounds=*/IsInBounds: false))
1955 return V;
1956
1957 return Insert(I: GetElementPtrInst::Create(PointeeType: Ty, Ptr, IdxList: Idxs), Name);
1958 }
1959
1960 Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1961 uint64_t Idx1, const Twine &Name = "") {
1962 Value *Idxs[] = {
1963 ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Idx0),
1964 ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Idx1)
1965 };
1966
1967 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList: Idxs, /*IsInBounds=*/IsInBounds: true))
1968 return V;
1969
1970 return Insert(I: GetElementPtrInst::CreateInBounds(PointeeType: Ty, Ptr, IdxList: Idxs), Name);
1971 }
1972
1973 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1974 const Twine &Name = "") {
1975 return CreateConstInBoundsGEP2_32(Ty, Ptr, Idx0: 0, Idx1: Idx, Name);
1976 }
1977
1978 Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
1979 bool IsInBounds = false) {
1980 return CreateGEP(Ty: getInt8Ty(), Ptr, IdxList: Offset, Name, IsInBounds);
1981 }
1982
1983 Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
1984 const Twine &Name = "") {
1985 return CreateGEP(Ty: getInt8Ty(), Ptr, IdxList: Offset, Name, /*IsInBounds*/ IsInBounds: true);
1986 }
1987
1988 /// Same as CreateGlobalString, but return a pointer with "i8*" type
1989 /// instead of a pointer to array of i8.
1990 ///
1991 /// If no module is given via \p M, it is take from the insertion point basic
1992 /// block.
1993 Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1994 unsigned AddressSpace = 0,
1995 Module *M = nullptr) {
1996 GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M);
1997 Constant *Zero = ConstantInt::get(Ty: Type::getInt32Ty(C&: Context), V: 0);
1998 Constant *Indices[] = {Zero, Zero};
1999 return ConstantExpr::getInBoundsGetElementPtr(Ty: GV->getValueType(), C: GV,
2000 IdxList: Indices);
2001 }
2002
2003 //===--------------------------------------------------------------------===//
2004 // Instruction creation methods: Cast/Conversion Operators
2005 //===--------------------------------------------------------------------===//
2006
2007 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "",
2008 bool IsNUW = false, bool IsNSW = false) {
2009 if (V->getType() == DestTy)
2010 return V;
2011 if (Value *Folded = Folder.FoldCast(Op: Instruction::Trunc, V, DestTy))
2012 return Folded;
2013 Instruction *I = CastInst::Create(Instruction::Trunc, S: V, Ty: DestTy);
2014 if (IsNUW)
2015 I->setHasNoUnsignedWrap();
2016 if (IsNSW)
2017 I->setHasNoSignedWrap();
2018 return Insert(I, Name);
2019 }
2020
2021 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "",
2022 bool IsNonNeg = false) {
2023 if (V->getType() == DestTy)
2024 return V;
2025 if (Value *Folded = Folder.FoldCast(Op: Instruction::ZExt, V, DestTy))
2026 return Folded;
2027 Instruction *I = Insert(I: new ZExtInst(V, DestTy), Name);
2028 if (IsNonNeg)
2029 I->setNonNeg();
2030 return I;
2031 }
2032
2033 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
2034 return CreateCast(Op: Instruction::SExt, V, DestTy, Name);
2035 }
2036
2037 /// Create a ZExt or Trunc from the integer value V to DestTy. Return
2038 /// the value untouched if the type of V is already DestTy.
2039 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
2040 const Twine &Name = "") {
2041 assert(V->getType()->isIntOrIntVectorTy() &&
2042 DestTy->isIntOrIntVectorTy() &&
2043 "Can only zero extend/truncate integers!");
2044 Type *VTy = V->getType();
2045 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2046 return CreateZExt(V, DestTy, Name);
2047 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2048 return CreateTrunc(V, DestTy, Name);
2049 return V;
2050 }
2051
2052 /// Create a SExt or Trunc from the integer value V to DestTy. Return
2053 /// the value untouched if the type of V is already DestTy.
2054 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
2055 const Twine &Name = "") {
2056 assert(V->getType()->isIntOrIntVectorTy() &&
2057 DestTy->isIntOrIntVectorTy() &&
2058 "Can only sign extend/truncate integers!");
2059 Type *VTy = V->getType();
2060 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2061 return CreateSExt(V, DestTy, Name);
2062 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2063 return CreateTrunc(V, DestTy, Name);
2064 return V;
2065 }
2066
2067 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
2068 if (IsFPConstrained)
2069 return CreateConstrainedFPCast(Intrinsic::ID: experimental_constrained_fptoui,
2070 V, DestTy, FMFSource: nullptr, Name);
2071 return CreateCast(Op: Instruction::FPToUI, V, DestTy, Name);
2072 }
2073
2074 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
2075 if (IsFPConstrained)
2076 return CreateConstrainedFPCast(Intrinsic::ID: experimental_constrained_fptosi,
2077 V, DestTy, FMFSource: nullptr, Name);
2078 return CreateCast(Op: Instruction::FPToSI, V, DestTy, Name);
2079 }
2080
2081 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = "",
2082 bool IsNonNeg = false) {
2083 if (IsFPConstrained)
2084 return CreateConstrainedFPCast(Intrinsic::ID: experimental_constrained_uitofp,
2085 V, DestTy, FMFSource: nullptr, Name);
2086 if (Value *Folded = Folder.FoldCast(Op: Instruction::UIToFP, V, DestTy))
2087 return Folded;
2088 Instruction *I = Insert(I: new UIToFPInst(V, DestTy), Name);
2089 if (IsNonNeg)
2090 I->setNonNeg();
2091 return I;
2092 }
2093
2094 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
2095 if (IsFPConstrained)
2096 return CreateConstrainedFPCast(Intrinsic::ID: experimental_constrained_sitofp,
2097 V, DestTy, FMFSource: nullptr, Name);
2098 return CreateCast(Op: Instruction::SIToFP, V, DestTy, Name);
2099 }
2100
2101 Value *CreateFPTrunc(Value *V, Type *DestTy,
2102 const Twine &Name = "") {
2103 if (IsFPConstrained)
2104 return CreateConstrainedFPCast(
2105 Intrinsic::ID: experimental_constrained_fptrunc, V, DestTy, FMFSource: nullptr,
2106 Name);
2107 return CreateCast(Op: Instruction::FPTrunc, V, DestTy, Name);
2108 }
2109
2110 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
2111 if (IsFPConstrained)
2112 return CreateConstrainedFPCast(Intrinsic::ID: experimental_constrained_fpext,
2113 V, DestTy, FMFSource: nullptr, Name);
2114 return CreateCast(Op: Instruction::FPExt, V, DestTy, Name);
2115 }
2116
2117 Value *CreatePtrToInt(Value *V, Type *DestTy,
2118 const Twine &Name = "") {
2119 return CreateCast(Op: Instruction::PtrToInt, V, DestTy, Name);
2120 }
2121
2122 Value *CreateIntToPtr(Value *V, Type *DestTy,
2123 const Twine &Name = "") {
2124 return CreateCast(Op: Instruction::IntToPtr, V, DestTy, Name);
2125 }
2126
2127 Value *CreateBitCast(Value *V, Type *DestTy,
2128 const Twine &Name = "") {
2129 return CreateCast(Op: Instruction::BitCast, V, DestTy, Name);
2130 }
2131
2132 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
2133 const Twine &Name = "") {
2134 return CreateCast(Op: Instruction::AddrSpaceCast, V, DestTy, Name);
2135 }
2136
2137 Value *CreateZExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2138 Instruction::CastOps CastOp =
2139 V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2140 ? Instruction::BitCast
2141 : Instruction::ZExt;
2142 return CreateCast(Op: CastOp, V, DestTy, Name);
2143 }
2144
2145 Value *CreateSExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2146 Instruction::CastOps CastOp =
2147 V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2148 ? Instruction::BitCast
2149 : Instruction::SExt;
2150 return CreateCast(Op: CastOp, V, DestTy, Name);
2151 }
2152
2153 Value *CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2154 Instruction::CastOps CastOp =
2155 V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2156 ? Instruction::BitCast
2157 : Instruction::Trunc;
2158 return CreateCast(Op: CastOp, V, DestTy, Name);
2159 }
2160
2161 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
2162 const Twine &Name = "") {
2163 if (V->getType() == DestTy)
2164 return V;
2165 if (Value *Folded = Folder.FoldCast(Op, V, DestTy))
2166 return Folded;
2167 return Insert(I: CastInst::Create(Op, S: V, Ty: DestTy), Name);
2168 }
2169
2170 Value *CreatePointerCast(Value *V, Type *DestTy,
2171 const Twine &Name = "") {
2172 if (V->getType() == DestTy)
2173 return V;
2174 if (auto *VC = dyn_cast<Constant>(Val: V))
2175 return Insert(V: Folder.CreatePointerCast(C: VC, DestTy), Name);
2176 return Insert(I: CastInst::CreatePointerCast(S: V, Ty: DestTy), Name);
2177 }
2178
2179 // With opaque pointers enabled, this can be substituted with
2180 // CreateAddrSpaceCast.
2181 // TODO: Replace uses of this method and remove the method itself.
2182 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
2183 const Twine &Name = "") {
2184 if (V->getType() == DestTy)
2185 return V;
2186
2187 if (auto *VC = dyn_cast<Constant>(Val: V)) {
2188 return Insert(V: Folder.CreatePointerBitCastOrAddrSpaceCast(C: VC, DestTy),
2189 Name);
2190 }
2191
2192 return Insert(I: CastInst::CreatePointerBitCastOrAddrSpaceCast(S: V, Ty: DestTy),
2193 Name);
2194 }
2195
2196 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
2197 const Twine &Name = "") {
2198 Instruction::CastOps CastOp =
2199 V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2200 ? Instruction::Trunc
2201 : (isSigned ? Instruction::SExt : Instruction::ZExt);
2202 return CreateCast(Op: CastOp, V, DestTy, Name);
2203 }
2204
2205 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
2206 const Twine &Name = "") {
2207 if (V->getType() == DestTy)
2208 return V;
2209 if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
2210 return CreatePtrToInt(V, DestTy, Name);
2211 if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
2212 return CreateIntToPtr(V, DestTy, Name);
2213
2214 return CreateBitCast(V, DestTy, Name);
2215 }
2216
2217 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
2218 Instruction::CastOps CastOp =
2219 V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2220 ? Instruction::FPTrunc
2221 : Instruction::FPExt;
2222 return CreateCast(Op: CastOp, V, DestTy, Name);
2223 }
2224
2225 CallInst *CreateConstrainedFPCast(
2226 Intrinsic::ID ID, Value *V, Type *DestTy,
2227 Instruction *FMFSource = nullptr, const Twine &Name = "",
2228 MDNode *FPMathTag = nullptr,
2229 std::optional<RoundingMode> Rounding = std::nullopt,
2230 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2231
2232 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
2233 // compile time error, instead of converting the string to bool for the
2234 // isSigned parameter.
2235 Value *CreateIntCast(Value *, Type *, const char *) = delete;
2236
2237 //===--------------------------------------------------------------------===//
2238 // Instruction creation methods: Compare Instructions
2239 //===--------------------------------------------------------------------===//
2240
2241 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
2242 return CreateICmp(P: ICmpInst::ICMP_EQ, LHS, RHS, Name);
2243 }
2244
2245 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
2246 return CreateICmp(P: ICmpInst::ICMP_NE, LHS, RHS, Name);
2247 }
2248
2249 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2250 return CreateICmp(P: ICmpInst::ICMP_UGT, LHS, RHS, Name);
2251 }
2252
2253 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2254 return CreateICmp(P: ICmpInst::ICMP_UGE, LHS, RHS, Name);
2255 }
2256
2257 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
2258 return CreateICmp(P: ICmpInst::ICMP_ULT, LHS, RHS, Name);
2259 }
2260
2261 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
2262 return CreateICmp(P: ICmpInst::ICMP_ULE, LHS, RHS, Name);
2263 }
2264
2265 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2266 return CreateICmp(P: ICmpInst::ICMP_SGT, LHS, RHS, Name);
2267 }
2268
2269 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2270 return CreateICmp(P: ICmpInst::ICMP_SGE, LHS, RHS, Name);
2271 }
2272
2273 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
2274 return CreateICmp(P: ICmpInst::ICMP_SLT, LHS, RHS, Name);
2275 }
2276
2277 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
2278 return CreateICmp(P: ICmpInst::ICMP_SLE, LHS, RHS, Name);
2279 }
2280
2281 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2282 MDNode *FPMathTag = nullptr) {
2283 return CreateFCmp(P: FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
2284 }
2285
2286 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
2287 MDNode *FPMathTag = nullptr) {
2288 return CreateFCmp(P: FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
2289 }
2290
2291 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
2292 MDNode *FPMathTag = nullptr) {
2293 return CreateFCmp(P: FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
2294 }
2295
2296 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
2297 MDNode *FPMathTag = nullptr) {
2298 return CreateFCmp(P: FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
2299 }
2300
2301 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
2302 MDNode *FPMathTag = nullptr) {
2303 return CreateFCmp(P: FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
2304 }
2305
2306 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
2307 MDNode *FPMathTag = nullptr) {
2308 return CreateFCmp(P: FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
2309 }
2310
2311 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
2312 MDNode *FPMathTag = nullptr) {
2313 return CreateFCmp(P: FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
2314 }
2315
2316 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
2317 MDNode *FPMathTag = nullptr) {
2318 return CreateFCmp(P: FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
2319 }
2320
2321 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2322 MDNode *FPMathTag = nullptr) {
2323 return CreateFCmp(P: FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
2324 }
2325
2326 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
2327 MDNode *FPMathTag = nullptr) {
2328 return CreateFCmp(P: FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
2329 }
2330
2331 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
2332 MDNode *FPMathTag = nullptr) {
2333 return CreateFCmp(P: FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
2334 }
2335
2336 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
2337 MDNode *FPMathTag = nullptr) {
2338 return CreateFCmp(P: FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
2339 }
2340
2341 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
2342 MDNode *FPMathTag = nullptr) {
2343 return CreateFCmp(P: FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
2344 }
2345
2346 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
2347 MDNode *FPMathTag = nullptr) {
2348 return CreateFCmp(P: FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
2349 }
2350
2351 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2352 const Twine &Name = "") {
2353 if (auto *V = Folder.FoldICmp(P, LHS, RHS))
2354 return V;
2355 return Insert(I: new ICmpInst(P, LHS, RHS), Name);
2356 }
2357
2358 // Create a quiet floating-point comparison (i.e. one that raises an FP
2359 // exception only in the case where an input is a signaling NaN).
2360 // Note that this differs from CreateFCmpS only if IsFPConstrained is true.
2361 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2362 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2363 return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, IsSignaling: false);
2364 }
2365
2366 Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
2367 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2368 return CmpInst::isFPPredicate(P: Pred)
2369 ? CreateFCmp(P: Pred, LHS, RHS, Name, FPMathTag)
2370 : CreateICmp(P: Pred, LHS, RHS, Name);
2371 }
2372
2373 // Create a signaling floating-point comparison (i.e. one that raises an FP
2374 // exception whenever an input is any NaN, signaling or quiet).
2375 // Note that this differs from CreateFCmp only if IsFPConstrained is true.
2376 Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS,
2377 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2378 return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, IsSignaling: true);
2379 }
2380
2381private:
2382 // Helper routine to create either a signaling or a quiet FP comparison.
2383 Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS,
2384 const Twine &Name, MDNode *FPMathTag,
2385 bool IsSignaling);
2386
2387public:
2388 CallInst *CreateConstrainedFPCmp(
2389 Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R,
2390 const Twine &Name = "",
2391 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2392
2393 //===--------------------------------------------------------------------===//
2394 // Instruction creation methods: Other Instructions
2395 //===--------------------------------------------------------------------===//
2396
2397 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2398 const Twine &Name = "") {
2399 PHINode *Phi = PHINode::Create(Ty, NumReservedValues);
2400 if (isa<FPMathOperator>(Val: Phi))
2401 setFPAttrs(I: Phi, FPMD: nullptr /* MDNode* */, FMF);
2402 return Insert(I: Phi, Name);
2403 }
2404
2405private:
2406 CallInst *createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
2407 const Twine &Name = "",
2408 Instruction *FMFSource = nullptr,
2409 ArrayRef<OperandBundleDef> OpBundles = {});
2410
2411public:
2412 CallInst *CreateCall(FunctionType *FTy, Value *Callee,
2413 ArrayRef<Value *> Args = std::nullopt,
2414 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2415 CallInst *CI = CallInst::Create(Ty: FTy, Func: Callee, Args, Bundles: DefaultOperandBundles);
2416 if (IsFPConstrained)
2417 setConstrainedFPCallAttr(CI);
2418 if (isa<FPMathOperator>(Val: CI))
2419 setFPAttrs(I: CI, FPMD: FPMathTag, FMF);
2420 return Insert(I: CI, Name);
2421 }
2422
2423 CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
2424 ArrayRef<OperandBundleDef> OpBundles,
2425 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2426 CallInst *CI = CallInst::Create(Ty: FTy, Func: Callee, Args, Bundles: OpBundles);
2427 if (IsFPConstrained)
2428 setConstrainedFPCallAttr(CI);
2429 if (isa<FPMathOperator>(Val: CI))
2430 setFPAttrs(I: CI, FPMD: FPMathTag, FMF);
2431 return Insert(I: CI, Name);
2432 }
2433
2434 CallInst *CreateCall(FunctionCallee Callee,
2435 ArrayRef<Value *> Args = std::nullopt,
2436 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2437 return CreateCall(FTy: Callee.getFunctionType(), Callee: Callee.getCallee(), Args, Name,
2438 FPMathTag);
2439 }
2440
2441 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
2442 ArrayRef<OperandBundleDef> OpBundles,
2443 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2444 return CreateCall(FTy: Callee.getFunctionType(), Callee: Callee.getCallee(), Args,
2445 OpBundles, Name, FPMathTag);
2446 }
2447
2448 CallInst *CreateConstrainedFPCall(
2449 Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "",
2450 std::optional<RoundingMode> Rounding = std::nullopt,
2451 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2452
2453 Value *CreateSelect(Value *C, Value *True, Value *False,
2454 const Twine &Name = "", Instruction *MDFrom = nullptr);
2455
2456 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2457 return Insert(I: new VAArgInst(List, Ty), Name);
2458 }
2459
2460 Value *CreateExtractElement(Value *Vec, Value *Idx,
2461 const Twine &Name = "") {
2462 if (Value *V = Folder.FoldExtractElement(Vec, Idx))
2463 return V;
2464 return Insert(I: ExtractElementInst::Create(Vec, Idx), Name);
2465 }
2466
2467 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2468 const Twine &Name = "") {
2469 return CreateExtractElement(Vec, Idx: getInt64(C: Idx), Name);
2470 }
2471
2472 Value *CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx,
2473 const Twine &Name = "") {
2474 return CreateInsertElement(Vec: PoisonValue::get(T: VecTy), NewElt, Idx, Name);
2475 }
2476
2477 Value *CreateInsertElement(Type *VecTy, Value *NewElt, uint64_t Idx,
2478 const Twine &Name = "") {
2479 return CreateInsertElement(Vec: PoisonValue::get(T: VecTy), NewElt, Idx, Name);
2480 }
2481
2482 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
2483 const Twine &Name = "") {
2484 if (Value *V = Folder.FoldInsertElement(Vec, NewElt, Idx))
2485 return V;
2486 return Insert(I: InsertElementInst::Create(Vec, NewElt, Idx), Name);
2487 }
2488
2489 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2490 const Twine &Name = "") {
2491 return CreateInsertElement(Vec, NewElt, Idx: getInt64(C: Idx), Name);
2492 }
2493
2494 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
2495 const Twine &Name = "") {
2496 SmallVector<int, 16> IntMask;
2497 ShuffleVectorInst::getShuffleMask(Mask: cast<Constant>(Val: Mask), Result&: IntMask);
2498 return CreateShuffleVector(V1, V2, Mask: IntMask, Name);
2499 }
2500
2501 /// See class ShuffleVectorInst for a description of the mask representation.
2502 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask,
2503 const Twine &Name = "") {
2504 if (Value *V = Folder.FoldShuffleVector(V1, V2, Mask))
2505 return V;
2506 return Insert(I: new ShuffleVectorInst(V1, V2, Mask), Name);
2507 }
2508
2509 /// Create a unary shuffle. The second vector operand of the IR instruction
2510 /// is poison.
2511 Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask,
2512 const Twine &Name = "") {
2513 return CreateShuffleVector(V1: V, V2: PoisonValue::get(T: V->getType()), Mask, Name);
2514 }
2515
2516 Value *CreateExtractValue(Value *Agg, ArrayRef<unsigned> Idxs,
2517 const Twine &Name = "") {
2518 if (auto *V = Folder.FoldExtractValue(Agg, IdxList: Idxs))
2519 return V;
2520 return Insert(I: ExtractValueInst::Create(Agg, Idxs), Name);
2521 }
2522
2523 Value *CreateInsertValue(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2524 const Twine &Name = "") {
2525 if (auto *V = Folder.FoldInsertValue(Agg, Val, IdxList: Idxs))
2526 return V;
2527 return Insert(I: InsertValueInst::Create(Agg, Val, Idxs), Name);
2528 }
2529
2530 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2531 const Twine &Name = "") {
2532 return Insert(I: LandingPadInst::Create(RetTy: Ty, NumReservedClauses: NumClauses), Name);
2533 }
2534
2535 Value *CreateFreeze(Value *V, const Twine &Name = "") {
2536 return Insert(I: new FreezeInst(V), Name);
2537 }
2538
2539 //===--------------------------------------------------------------------===//
2540 // Utility creation methods
2541 //===--------------------------------------------------------------------===//
2542
2543 /// Return a boolean value testing if \p Arg == 0.
2544 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2545 return CreateICmpEQ(LHS: Arg, RHS: Constant::getNullValue(Ty: Arg->getType()), Name);
2546 }
2547
2548 /// Return a boolean value testing if \p Arg != 0.
2549 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2550 return CreateICmpNE(LHS: Arg, RHS: Constant::getNullValue(Ty: Arg->getType()), Name);
2551 }
2552
2553 /// Return a boolean value testing if \p Arg < 0.
2554 Value *CreateIsNeg(Value *Arg, const Twine &Name = "") {
2555 return CreateICmpSLT(LHS: Arg, RHS: ConstantInt::getNullValue(Ty: Arg->getType()), Name);
2556 }
2557
2558 /// Return a boolean value testing if \p Arg > -1.
2559 Value *CreateIsNotNeg(Value *Arg, const Twine &Name = "") {
2560 return CreateICmpSGT(LHS: Arg, RHS: ConstantInt::getAllOnesValue(Ty: Arg->getType()),
2561 Name);
2562 }
2563
2564 /// Return the i64 difference between two pointer values, dividing out
2565 /// the size of the pointed-to objects.
2566 ///
2567 /// This is intended to implement C-style pointer subtraction. As such, the
2568 /// pointers must be appropriately aligned for their element types and
2569 /// pointing into the same object.
2570 Value *CreatePtrDiff(Type *ElemTy, Value *LHS, Value *RHS,
2571 const Twine &Name = "");
2572
2573 /// Create a launder.invariant.group intrinsic call. If Ptr type is
2574 /// different from pointer to i8, it's casted to pointer to i8 in the same
2575 /// address space before call and casted back to Ptr type after call.
2576 Value *CreateLaunderInvariantGroup(Value *Ptr);
2577
2578 /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2579 /// different from pointer to i8, it's casted to pointer to i8 in the same
2580 /// address space before call and casted back to Ptr type after call.
2581 Value *CreateStripInvariantGroup(Value *Ptr);
2582
2583 /// Return a vector value that contains the vector V reversed
2584 Value *CreateVectorReverse(Value *V, const Twine &Name = "");
2585
2586 /// Return a vector splice intrinsic if using scalable vectors, otherwise
2587 /// return a shufflevector. If the immediate is positive, a vector is
2588 /// extracted from concat(V1, V2), starting at Imm. If the immediate
2589 /// is negative, we extract -Imm elements from V1 and the remaining
2590 /// elements from V2. Imm is a signed integer in the range
2591 /// -VL <= Imm < VL (where VL is the runtime vector length of the
2592 /// source/result vector)
2593 Value *CreateVectorSplice(Value *V1, Value *V2, int64_t Imm,
2594 const Twine &Name = "");
2595
2596 /// Return a vector value that contains \arg V broadcasted to \p
2597 /// NumElts elements.
2598 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "");
2599
2600 /// Return a vector value that contains \arg V broadcasted to \p
2601 /// EC elements.
2602 Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = "");
2603
2604 Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base,
2605 unsigned Dimension, unsigned LastIndex,
2606 MDNode *DbgInfo);
2607
2608 Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex,
2609 MDNode *DbgInfo);
2610
2611 Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base,
2612 unsigned Index, unsigned FieldIndex,
2613 MDNode *DbgInfo);
2614
2615 Value *createIsFPClass(Value *FPNum, unsigned Test);
2616
2617private:
2618 /// Helper function that creates an assume intrinsic call that
2619 /// represents an alignment assumption on the provided pointer \p PtrValue
2620 /// with offset \p OffsetValue and alignment value \p AlignValue.
2621 CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2622 Value *PtrValue, Value *AlignValue,
2623 Value *OffsetValue);
2624
2625public:
2626 /// Create an assume intrinsic call that represents an alignment
2627 /// assumption on the provided pointer.
2628 ///
2629 /// An optional offset can be provided, and if it is provided, the offset
2630 /// must be subtracted from the provided pointer to get the pointer with the
2631 /// specified alignment.
2632 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2633 unsigned Alignment,
2634 Value *OffsetValue = nullptr);
2635
2636 /// Create an assume intrinsic call that represents an alignment
2637 /// assumption on the provided pointer.
2638 ///
2639 /// An optional offset can be provided, and if it is provided, the offset
2640 /// must be subtracted from the provided pointer to get the pointer with the
2641 /// specified alignment.
2642 ///
2643 /// This overload handles the condition where the Alignment is dependent
2644 /// on an existing value rather than a static value.
2645 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2646 Value *Alignment,
2647 Value *OffsetValue = nullptr);
2648};
2649
2650/// This provides a uniform API for creating instructions and inserting
2651/// them into a basic block: either at the end of a BasicBlock, or at a specific
2652/// iterator location in a block.
2653///
2654/// Note that the builder does not expose the full generality of LLVM
2655/// instructions. For access to extra instruction properties, use the mutators
2656/// (e.g. setVolatile) on the instructions after they have been
2657/// created. Convenience state exists to specify fast-math flags and fp-math
2658/// tags.
2659///
2660/// The first template argument specifies a class to use for creating constants.
2661/// This defaults to creating minimally folded constants. The second template
2662/// argument allows clients to specify custom insertion hooks that are called on
2663/// every newly created insertion.
2664template <typename FolderTy = ConstantFolder,
2665 typename InserterTy = IRBuilderDefaultInserter>
2666class IRBuilder : public IRBuilderBase {
2667private:
2668 FolderTy Folder;
2669 InserterTy Inserter;
2670
2671public:
2672 IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(),
2673 MDNode *FPMathTag = nullptr,
2674 ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2675 : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles),
2676 Folder(Folder), Inserter(Inserter) {}
2677
2678 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
2679 ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2680 : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {}
2681
2682 explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder,
2683 MDNode *FPMathTag = nullptr,
2684 ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2685 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2686 FPMathTag, OpBundles),
2687 Folder(Folder) {
2688 SetInsertPoint(TheBB);
2689 }
2690
2691 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
2692 ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2693 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2694 FPMathTag, OpBundles) {
2695 SetInsertPoint(TheBB);
2696 }
2697
2698 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
2699 ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2700 : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, FPMathTag,
2701 OpBundles) {
2702 SetInsertPoint(IP);
2703 }
2704
2705 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder,
2706 MDNode *FPMathTag = nullptr,
2707 ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2708 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2709 FPMathTag, OpBundles),
2710 Folder(Folder) {
2711 SetInsertPoint(TheBB, IP);
2712 }
2713
2714 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
2715 MDNode *FPMathTag = nullptr,
2716 ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2717 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2718 FPMathTag, OpBundles) {
2719 SetInsertPoint(TheBB, IP);
2720 }
2721
2722 /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard
2723 /// or FastMathFlagGuard instead.
2724 IRBuilder(const IRBuilder &) = delete;
2725
2726 InserterTy &getInserter() { return Inserter; }
2727 const InserterTy &getInserter() const { return Inserter; }
2728};
2729
2730template <typename FolderTy, typename InserterTy>
2731IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *,
2732 ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy, InserterTy>;
2733IRBuilder(LLVMContext &, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2734template <typename FolderTy>
2735IRBuilder(BasicBlock *, FolderTy, MDNode *, ArrayRef<OperandBundleDef>)
2736 -> IRBuilder<FolderTy>;
2737IRBuilder(BasicBlock *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2738IRBuilder(Instruction *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2739template <typename FolderTy>
2740IRBuilder(BasicBlock *, BasicBlock::iterator, FolderTy, MDNode *,
2741 ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy>;
2742IRBuilder(BasicBlock *, BasicBlock::iterator, MDNode *,
2743 ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2744
2745
2746// Create wrappers for C Binding types (see CBindingWrapping.h).
2747DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
2748
2749} // end namespace llvm
2750
2751#endif // LLVM_IR_IRBUILDER_H
2752

source code of llvm/include/llvm/IR/IRBuilder.h