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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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