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/None.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/Twine.h"
23#include "llvm/IR/BasicBlock.h"
24#include "llvm/IR/Constant.h"
25#include "llvm/IR/ConstantFolder.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/DebugLoc.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/FPEnv.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalVariable.h"
33#include "llvm/IR/InstrTypes.h"
34#include "llvm/IR/Instruction.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/Intrinsics.h"
37#include "llvm/IR/LLVMContext.h"
38#include "llvm/IR/Module.h"
39#include "llvm/IR/Operator.h"
40#include "llvm/IR/Type.h"
41#include "llvm/IR/Value.h"
42#include "llvm/IR/ValueHandle.h"
43#include "llvm/Support/AtomicOrdering.h"
44#include "llvm/Support/CBindingWrapping.h"
45#include "llvm/Support/Casting.h"
46#include <cassert>
47#include <cstdint>
48#include <functional>
49#include <utility>
50
51namespace llvm {
52
53class APInt;
54class Use;
55
56/// This provides the default implementation of the IRBuilder
57/// 'InsertHelper' method that is called whenever an instruction is created by
58/// IRBuilder and needs to be inserted.
59///
60/// By default, this inserts the instruction at the insertion point.
61class IRBuilderDefaultInserter {
62public:
63 virtual ~IRBuilderDefaultInserter();
64
65 virtual void InsertHelper(Instruction *I, const Twine &Name,
66 BasicBlock *BB,
67 BasicBlock::iterator InsertPt) const {
68 if (BB) BB->getInstList().insert(InsertPt, I);
69 I->setName(Name);
70 }
71};
72
73/// Provides an 'InsertHelper' that calls a user-provided callback after
74/// performing the default insertion.
75class 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 *BB,
86 BasicBlock::iterator InsertPt) const override {
87 IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
88 Callback(I);
89 }
90};
91
92/// Common base class shared among various IRBuilders.
93class IRBuilderBase {
94 /// Pairs of (metadata kind, MDNode *) that should be added to all newly
95 /// created instructions, like !dbg metadata.
96 SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy;
97
98 /// Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not
99 /// null. If \p MD is null, remove the entry with \p Kind.
100 void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) {
101 if (!MD) {
102 erase_if(MetadataToCopy, [Kind](const std::pair<unsigned, MDNode *> &KV) {
103 return KV.first == Kind;
104 });
105 return;
106 }
107
108 for (auto &KV : MetadataToCopy)
109 if (KV.first == Kind) {
110 KV.second = MD;
111 return;
112 }
113
114 MetadataToCopy.emplace_back(Kind, MD);
115 }
116
117protected:
118 BasicBlock *BB;
119 BasicBlock::iterator InsertPt;
120 LLVMContext &Context;
121 const IRBuilderFolder &Folder;
122 const IRBuilderDefaultInserter &Inserter;
123
124 MDNode *DefaultFPMathTag;
125 FastMathFlags FMF;
126
127 bool IsFPConstrained = false;
128 fp::ExceptionBehavior DefaultConstrainedExcept = fp::ebStrict;
129 RoundingMode DefaultConstrainedRounding = RoundingMode::Dynamic;
130
131 ArrayRef<OperandBundleDef> DefaultOperandBundles;
132
133public:
134 IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
135 const IRBuilderDefaultInserter &Inserter, MDNode *FPMathTag,
136 ArrayRef<OperandBundleDef> OpBundles)
137 : Context(context), Folder(Folder), Inserter(Inserter),
138 DefaultFPMathTag(FPMathTag), DefaultOperandBundles(OpBundles) {
139 ClearInsertionPoint();
140 }
141
142 /// Insert and return the specified instruction.
143 template<typename InstTy>
144 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
145 Inserter.InsertHelper(I, Name, BB, InsertPt);
146 AddMetadataToInst(I);
147 return I;
148 }
149
150 /// No-op overload to handle constants.
151 Constant *Insert(Constant *C, const Twine& = "") const {
152 return C;
153 }
154
155 Value *Insert(Value *V, const Twine &Name = "") const {
156 if (Instruction *I = dyn_cast<Instruction>(V))
157 return Insert(I, Name);
158 assert(isa<Constant>(V));
159 return V;
160 }
161
162 //===--------------------------------------------------------------------===//
163 // Builder configuration methods
164 //===--------------------------------------------------------------------===//
165
166 /// Clear the insertion point: created instructions will not be
167 /// inserted into a block.
168 void ClearInsertionPoint() {
169 BB = nullptr;
170 InsertPt = BasicBlock::iterator();
171 }
172
173 BasicBlock *GetInsertBlock() const { return BB; }
174 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
175 LLVMContext &getContext() const { return Context; }
176
177 /// This specifies that created instructions should be appended to the
178 /// end of the specified block.
179 void SetInsertPoint(BasicBlock *TheBB) {
180 BB = TheBB;
181 InsertPt = BB->end();
182 }
183
184 /// This specifies that created instructions should be inserted before
185 /// the specified instruction.
186 void SetInsertPoint(Instruction *I) {
187 BB = I->getParent();
188 InsertPt = I->getIterator();
189 assert(InsertPt != BB->end() && "Can't read debug loc from end()");
190 SetCurrentDebugLocation(I->getDebugLoc());
191 }
192
193 /// This specifies that created instructions should be inserted at the
194 /// specified point.
195 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
196 BB = TheBB;
197 InsertPt = IP;
198 if (IP != TheBB->end())
199 SetCurrentDebugLocation(IP->getDebugLoc());
200 }
201
202 /// Set location information used by debugging information.
203 void SetCurrentDebugLocation(DebugLoc L) {
204 AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode());
205 }
206
207 /// Collect metadata with IDs \p MetadataKinds from \p Src which should be
208 /// added to all created instructions. Entries present in MedataDataToCopy but
209 /// not on \p Src will be dropped from MetadataToCopy.
210 void CollectMetadataToCopy(Instruction *Src,
211 ArrayRef<unsigned> MetadataKinds) {
212 for (unsigned K : MetadataKinds)
213 AddOrRemoveMetadataToCopy(K, Src->getMetadata(K));
214 }
215
216 /// Get location information used by debugging information.
217 DebugLoc getCurrentDebugLocation() const;
218
219 /// If this builder has a current debug location, set it on the
220 /// specified instruction.
221 void SetInstDebugLocation(Instruction *I) const;
222
223 /// Add all entries in MetadataToCopy to \p I.
224 void AddMetadataToInst(Instruction *I) const {
225 for (const auto &KV : MetadataToCopy)
226 I->setMetadata(KV.first, KV.second);
227 }
228
229 /// Get the return type of the current function that we're emitting
230 /// into.
231 Type *getCurrentFunctionReturnType() const;
232
233 /// InsertPoint - A saved insertion point.
234 class InsertPoint {
235 BasicBlock *Block = nullptr;
236 BasicBlock::iterator Point;
237
238 public:
239 /// Creates a new insertion point which doesn't point to anything.
240 InsertPoint() = default;
241
242 /// Creates a new insertion point at the given location.
243 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
244 : Block(InsertBlock), Point(InsertPoint) {}
245
246 /// Returns true if this insert point is set.
247 bool isSet() const { return (Block != nullptr); }
248
249 BasicBlock *getBlock() const { return Block; }
250 BasicBlock::iterator getPoint() const { return Point; }
251 };
252
253 /// Returns the current insert point.
254 InsertPoint saveIP() const {
255 return InsertPoint(GetInsertBlock(), GetInsertPoint());
256 }
257
258 /// Returns the current insert point, clearing it in the process.
259 InsertPoint saveAndClearIP() {
260 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
261 ClearInsertionPoint();
262 return IP;
263 }
264
265 /// Sets the current insert point to a previously-saved location.
266 void restoreIP(InsertPoint IP) {
267 if (IP.isSet())
268 SetInsertPoint(IP.getBlock(), IP.getPoint());
269 else
270 ClearInsertionPoint();
271 }
272
273 /// Get the floating point math metadata being used.
274 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
275
276 /// Get the flags to be applied to created floating point ops
277 FastMathFlags getFastMathFlags() const { return FMF; }
278
279 FastMathFlags &getFastMathFlags() { return FMF; }
280
281 /// Clear the fast-math flags.
282 void clearFastMathFlags() { FMF.clear(); }
283
284 /// Set the floating point math metadata to be used.
285 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
286
287 /// Set the fast-math flags to be used with generated fp-math operators
288 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
289
290 /// Enable/Disable use of constrained floating point math. When
291 /// enabled the CreateF<op>() calls instead create constrained
292 /// floating point intrinsic calls. Fast math flags are unaffected
293 /// by this setting.
294 void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
295
296 /// Query for the use of constrained floating point math
297 bool getIsFPConstrained() { return IsFPConstrained; }
298
299 /// Set the exception handling to be used with constrained floating point
300 void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
301#ifndef NDEBUG
302 Optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(NewExcept);
303 assert(ExceptStr && "Garbage strict exception behavior!");
304#endif
305 DefaultConstrainedExcept = NewExcept;
306 }
307
308 /// Set the rounding mode handling to be used with constrained floating point
309 void setDefaultConstrainedRounding(RoundingMode NewRounding) {
310#ifndef NDEBUG
311 Optional<StringRef> RoundingStr = convertRoundingModeToStr(NewRounding);
312 assert(RoundingStr && "Garbage strict rounding mode!");
313#endif
314 DefaultConstrainedRounding = NewRounding;
315 }
316
317 /// Get the exception handling used with constrained floating point
318 fp::ExceptionBehavior getDefaultConstrainedExcept() {
319 return DefaultConstrainedExcept;
320 }
321
322 /// Get the rounding mode handling used with constrained floating point
323 RoundingMode getDefaultConstrainedRounding() {
324 return DefaultConstrainedRounding;
325 }
326
327 void setConstrainedFPFunctionAttr() {
328 assert(BB && "Must have a basic block to set any function attributes!");
329
330 Function *F = BB->getParent();
331 if (!F->hasFnAttribute(Attribute::StrictFP)) {
332 F->addFnAttr(Attribute::StrictFP);
333 }
334 }
335
336 void setConstrainedFPCallAttr(CallBase *I) {
337 I->addFnAttr(Attribute::StrictFP);
338 }
339
340 void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) {
341 DefaultOperandBundles = OpBundles;
342 }
343
344 //===--------------------------------------------------------------------===//
345 // RAII helpers.
346 //===--------------------------------------------------------------------===//
347
348 // RAII object that stores the current insertion point and restores it
349 // when the object is destroyed. This includes the debug location.
350 class InsertPointGuard {
351 IRBuilderBase &Builder;
352 AssertingVH<BasicBlock> Block;
353 BasicBlock::iterator Point;
354 DebugLoc DbgLoc;
355
356 public:
357 InsertPointGuard(IRBuilderBase &B)
358 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
359 DbgLoc(B.getCurrentDebugLocation()) {}
360
361 InsertPointGuard(const InsertPointGuard &) = delete;
362 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
363
364 ~InsertPointGuard() {
365 Builder.restoreIP(InsertPoint(Block, Point));
366 Builder.SetCurrentDebugLocation(DbgLoc);
367 }
368 };
369
370 // RAII object that stores the current fast math settings and restores
371 // them when the object is destroyed.
372 class FastMathFlagGuard {
373 IRBuilderBase &Builder;
374 FastMathFlags FMF;
375 MDNode *FPMathTag;
376 bool IsFPConstrained;
377 fp::ExceptionBehavior DefaultConstrainedExcept;
378 RoundingMode DefaultConstrainedRounding;
379
380 public:
381 FastMathFlagGuard(IRBuilderBase &B)
382 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag),
383 IsFPConstrained(B.IsFPConstrained),
384 DefaultConstrainedExcept(B.DefaultConstrainedExcept),
385 DefaultConstrainedRounding(B.DefaultConstrainedRounding) {}
386
387 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
388 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
389
390 ~FastMathFlagGuard() {
391 Builder.FMF = FMF;
392 Builder.DefaultFPMathTag = FPMathTag;
393 Builder.IsFPConstrained = IsFPConstrained;
394 Builder.DefaultConstrainedExcept = DefaultConstrainedExcept;
395 Builder.DefaultConstrainedRounding = DefaultConstrainedRounding;
396 }
397 };
398
399 // RAII object that stores the current default operand bundles and restores
400 // them when the object is destroyed.
401 class OperandBundlesGuard {
402 IRBuilderBase &Builder;
403 ArrayRef<OperandBundleDef> DefaultOperandBundles;
404
405 public:
406 OperandBundlesGuard(IRBuilderBase &B)
407 : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {}
408
409 OperandBundlesGuard(const OperandBundlesGuard &) = delete;
410 OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete;
411
412 ~OperandBundlesGuard() {
413 Builder.DefaultOperandBundles = DefaultOperandBundles;
414 }
415 };
416
417
418 //===--------------------------------------------------------------------===//
419 // Miscellaneous creation methods.
420 //===--------------------------------------------------------------------===//
421
422 /// Make a new global variable with initializer type i8*
423 ///
424 /// Make a new global variable with an initializer that has array of i8 type
425 /// filled in with the null terminated string value specified. The new global
426 /// variable will be marked mergable with any others of the same contents. If
427 /// Name is specified, it is the name of the global variable created.
428 ///
429 /// If no module is given via \p M, it is take from the insertion point basic
430 /// block.
431 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
432 unsigned AddressSpace = 0,
433 Module *M = nullptr);
434
435 /// Get a constant value representing either true or false.
436 ConstantInt *getInt1(bool V) {
437 return ConstantInt::get(getInt1Ty(), V);
438 }
439
440 /// Get the constant value for i1 true.
441 ConstantInt *getTrue() {
442 return ConstantInt::getTrue(Context);
443 }
444
445 /// Get the constant value for i1 false.
446 ConstantInt *getFalse() {
447 return ConstantInt::getFalse(Context);
448 }
449
450 /// Get a constant 8-bit value.
451 ConstantInt *getInt8(uint8_t C) {
452 return ConstantInt::get(getInt8Ty(), C);
453 }
454
455 /// Get a constant 16-bit value.
456 ConstantInt *getInt16(uint16_t C) {
457 return ConstantInt::get(getInt16Ty(), C);
458 }
459
460 /// Get a constant 32-bit value.
461 ConstantInt *getInt32(uint32_t C) {
462 return ConstantInt::get(getInt32Ty(), C);
463 }
464
465 /// Get a constant 64-bit value.
466 ConstantInt *getInt64(uint64_t C) {
467 return ConstantInt::get(getInt64Ty(), C);
468 }
469
470 /// Get a constant N-bit value, zero extended or truncated from
471 /// a 64-bit value.
472 ConstantInt *getIntN(unsigned N, uint64_t C) {
473 return ConstantInt::get(getIntNTy(N), C);
474 }
475
476 /// Get a constant integer value.
477 ConstantInt *getInt(const APInt &AI) {
478 return ConstantInt::get(Context, AI);
479 }
480
481 //===--------------------------------------------------------------------===//
482 // Type creation methods
483 //===--------------------------------------------------------------------===//
484
485 /// Fetch the type representing a single bit
486 IntegerType *getInt1Ty() {
487 return Type::getInt1Ty(Context);
488 }
489
490 /// Fetch the type representing an 8-bit integer.
491 IntegerType *getInt8Ty() {
492 return Type::getInt8Ty(Context);
493 }
494
495 /// Fetch the type representing a 16-bit integer.
496 IntegerType *getInt16Ty() {
497 return Type::getInt16Ty(Context);
498 }
499
500 /// Fetch the type representing a 32-bit integer.
501 IntegerType *getInt32Ty() {
502 return Type::getInt32Ty(Context);
503 }
504
505 /// Fetch the type representing a 64-bit integer.
506 IntegerType *getInt64Ty() {
507 return Type::getInt64Ty(Context);
508 }
509
510 /// Fetch the type representing a 128-bit integer.
511 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
512
513 /// Fetch the type representing an N-bit integer.
514 IntegerType *getIntNTy(unsigned N) {
515 return Type::getIntNTy(Context, N);
516 }
517
518 /// Fetch the type representing a 16-bit floating point value.
519 Type *getHalfTy() {
520 return Type::getHalfTy(Context);
521 }
522
523 /// Fetch the type representing a 16-bit brain floating point value.
524 Type *getBFloatTy() {
525 return Type::getBFloatTy(Context);
526 }
527
528 /// Fetch the type representing a 32-bit floating point value.
529 Type *getFloatTy() {
530 return Type::getFloatTy(Context);
531 }
532
533 /// Fetch the type representing a 64-bit floating point value.
534 Type *getDoubleTy() {
535 return Type::getDoubleTy(Context);
536 }
537
538 /// Fetch the type representing void.
539 Type *getVoidTy() {
540 return Type::getVoidTy(Context);
541 }
542
543 /// Fetch the type representing a pointer.
544 PointerType *getPtrTy(unsigned AddrSpace = 0) {
545 return PointerType::get(Context, AddrSpace);
546 }
547
548 /// Fetch the type representing a pointer to an 8-bit integer value.
549 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
550 return Type::getInt8PtrTy(Context, AddrSpace);
551 }
552
553 /// Fetch the type representing a pointer to an integer value.
554 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
555 return DL.getIntPtrType(Context, AddrSpace);
556 }
557
558 //===--------------------------------------------------------------------===//
559 // Intrinsic creation methods
560 //===--------------------------------------------------------------------===//
561
562 /// Create and insert a memset to the specified pointer and the
563 /// specified value.
564 ///
565 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
566 /// specified, it will be added to the instruction. Likewise with alias.scope
567 /// and noalias tags.
568 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
569 MaybeAlign Align, bool isVolatile = false,
570 MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
571 MDNode *NoAliasTag = nullptr) {
572 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
573 TBAATag, ScopeTag, NoAliasTag);
574 }
575
576 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
577 bool isVolatile = false, MDNode *TBAATag = nullptr,
578 MDNode *ScopeTag = nullptr,
579 MDNode *NoAliasTag = nullptr);
580
581 CallInst *CreateMemSetInline(Value *Dst, MaybeAlign DstAlign, Value *Val,
582 Value *Size, bool IsVolatile = false,
583 MDNode *TBAATag = nullptr,
584 MDNode *ScopeTag = nullptr,
585 MDNode *NoAliasTag = nullptr);
586
587 /// Create and insert an element unordered-atomic memset of the region of
588 /// memory starting at the given pointer to the given value.
589 ///
590 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
591 /// specified, it will be added to the instruction. Likewise with alias.scope
592 /// and noalias tags.
593 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
594 uint64_t Size, Align Alignment,
595 uint32_t ElementSize,
596 MDNode *TBAATag = nullptr,
597 MDNode *ScopeTag = nullptr,
598 MDNode *NoAliasTag = nullptr) {
599 return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
600 Align(Alignment), ElementSize,
601 TBAATag, ScopeTag, NoAliasTag);
602 }
603
604 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
605 Value *Size, Align Alignment,
606 uint32_t ElementSize,
607 MDNode *TBAATag = nullptr,
608 MDNode *ScopeTag = nullptr,
609 MDNode *NoAliasTag = nullptr);
610
611 /// Create and insert a memcpy between the specified pointers.
612 ///
613 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
614 /// specified, it will be added to the instruction. Likewise with alias.scope
615 /// and noalias tags.
616 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
617 MaybeAlign SrcAlign, uint64_t Size,
618 bool isVolatile = false, MDNode *TBAATag = nullptr,
619 MDNode *TBAAStructTag = nullptr,
620 MDNode *ScopeTag = nullptr,
621 MDNode *NoAliasTag = nullptr) {
622 return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
623 isVolatile, TBAATag, TBAAStructTag, ScopeTag,
624 NoAliasTag);
625 }
626
627 CallInst *CreateMemTransferInst(
628 Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
629 MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
630 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
631 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
632
633 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
634 MaybeAlign SrcAlign, Value *Size,
635 bool isVolatile = false, MDNode *TBAATag = nullptr,
636 MDNode *TBAAStructTag = nullptr,
637 MDNode *ScopeTag = nullptr,
638 MDNode *NoAliasTag = nullptr) {
639 return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src,
640 SrcAlign, Size, isVolatile, TBAATag,
641 TBAAStructTag, ScopeTag, NoAliasTag);
642 }
643
644 CallInst *
645 CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
646 MaybeAlign SrcAlign, Value *Size, bool IsVolatile = false,
647 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
648 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
649
650 /// Create and insert an element unordered-atomic memcpy between the
651 /// specified pointers.
652 ///
653 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
654 ///
655 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
656 /// specified, it will be added to the instruction. Likewise with alias.scope
657 /// and noalias tags.
658 CallInst *CreateElementUnorderedAtomicMemCpy(
659 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
660 uint32_t ElementSize, MDNode *TBAATag = nullptr,
661 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
662 MDNode *NoAliasTag = nullptr);
663
664 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
665 MaybeAlign SrcAlign, uint64_t Size,
666 bool isVolatile = false, MDNode *TBAATag = nullptr,
667 MDNode *ScopeTag = nullptr,
668 MDNode *NoAliasTag = nullptr) {
669 return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
670 isVolatile, TBAATag, ScopeTag, NoAliasTag);
671 }
672
673 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
674 MaybeAlign SrcAlign, Value *Size,
675 bool isVolatile = false, MDNode *TBAATag = nullptr,
676 MDNode *ScopeTag = nullptr,
677 MDNode *NoAliasTag = nullptr);
678
679 /// \brief Create and insert an element unordered-atomic memmove between the
680 /// specified pointers.
681 ///
682 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
683 /// respectively.
684 ///
685 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
686 /// specified, it will be added to the instruction. Likewise with alias.scope
687 /// and noalias tags.
688 CallInst *CreateElementUnorderedAtomicMemMove(
689 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
690 uint32_t ElementSize, MDNode *TBAATag = nullptr,
691 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
692 MDNode *NoAliasTag = nullptr);
693
694private:
695 CallInst *getReductionIntrinsic(Intrinsic::ID ID, Value *Src);
696
697public:
698 /// Create a sequential vector fadd reduction intrinsic of the source vector.
699 /// The first parameter is a scalar accumulator value. An unordered reduction
700 /// can be created by adding the reassoc fast-math flag to the resulting
701 /// sequential reduction.
702 CallInst *CreateFAddReduce(Value *Acc, Value *Src);
703
704 /// Create a sequential vector fmul reduction intrinsic of the source vector.
705 /// The first parameter is a scalar accumulator value. An unordered reduction
706 /// can be created by adding the reassoc fast-math flag to the resulting
707 /// sequential reduction.
708 CallInst *CreateFMulReduce(Value *Acc, Value *Src);
709
710 /// Create a vector int add reduction intrinsic of the source vector.
711 CallInst *CreateAddReduce(Value *Src);
712
713 /// Create a vector int mul reduction intrinsic of the source vector.
714 CallInst *CreateMulReduce(Value *Src);
715
716 /// Create a vector int AND reduction intrinsic of the source vector.
717 CallInst *CreateAndReduce(Value *Src);
718
719 /// Create a vector int OR reduction intrinsic of the source vector.
720 CallInst *CreateOrReduce(Value *Src);
721
722 /// Create a vector int XOR reduction intrinsic of the source vector.
723 CallInst *CreateXorReduce(Value *Src);
724
725 /// Create a vector integer max reduction intrinsic of the source
726 /// vector.
727 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
728
729 /// Create a vector integer min reduction intrinsic of the source
730 /// vector.
731 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
732
733 /// Create a vector float max reduction intrinsic of the source
734 /// vector.
735 CallInst *CreateFPMaxReduce(Value *Src);
736
737 /// Create a vector float min reduction intrinsic of the source
738 /// vector.
739 CallInst *CreateFPMinReduce(Value *Src);
740
741 /// Create a lifetime.start intrinsic.
742 ///
743 /// If the pointer isn't i8* it will be converted.
744 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
745
746 /// Create a lifetime.end intrinsic.
747 ///
748 /// If the pointer isn't i8* it will be converted.
749 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
750
751 /// Create a call to invariant.start intrinsic.
752 ///
753 /// If the pointer isn't i8* it will be converted.
754 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
755
756 /// Create a call to llvm.threadlocal.address intrinsic.
757 CallInst *CreateThreadLocalAddress(Value *Ptr);
758
759 /// Create a call to Masked Load intrinsic
760 CallInst *CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask,
761 Value *PassThru = nullptr, const Twine &Name = "");
762
763 /// Create a call to Masked Store intrinsic
764 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment,
765 Value *Mask);
766
767 /// Create a call to Masked Gather intrinsic
768 CallInst *CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment,
769 Value *Mask = nullptr, Value *PassThru = nullptr,
770 const Twine &Name = "");
771
772 /// Create a call to Masked Scatter intrinsic
773 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment,
774 Value *Mask = nullptr);
775
776 /// Create an assume intrinsic call that allows the optimizer to
777 /// assume that the provided condition will be true.
778 ///
779 /// The optional argument \p OpBundles specifies operand bundles that are
780 /// added to the call instruction.
781 CallInst *CreateAssumption(Value *Cond,
782 ArrayRef<OperandBundleDef> OpBundles = llvm::None);
783
784 /// Create a llvm.experimental.noalias.scope.decl intrinsic call.
785 Instruction *CreateNoAliasScopeDeclaration(Value *Scope);
786 Instruction *CreateNoAliasScopeDeclaration(MDNode *ScopeTag) {
787 return CreateNoAliasScopeDeclaration(
788 MetadataAsValue::get(Context, ScopeTag));
789 }
790
791 /// Create a call to the experimental.gc.statepoint intrinsic to
792 /// start a new statepoint sequence.
793 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
794 FunctionCallee ActualCallee,
795 ArrayRef<Value *> CallArgs,
796 Optional<ArrayRef<Value *>> DeoptArgs,
797 ArrayRef<Value *> GCArgs,
798 const Twine &Name = "");
799
800 /// Create a call to the experimental.gc.statepoint intrinsic to
801 /// start a new statepoint sequence.
802 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
803 FunctionCallee ActualCallee, uint32_t Flags,
804 ArrayRef<Value *> CallArgs,
805 Optional<ArrayRef<Use>> TransitionArgs,
806 Optional<ArrayRef<Use>> DeoptArgs,
807 ArrayRef<Value *> GCArgs,
808 const Twine &Name = "");
809
810 /// Conveninence function for the common case when CallArgs are filled
811 /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
812 /// .get()'ed to get the Value pointer.
813 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
814 FunctionCallee ActualCallee,
815 ArrayRef<Use> CallArgs,
816 Optional<ArrayRef<Value *>> DeoptArgs,
817 ArrayRef<Value *> GCArgs,
818 const Twine &Name = "");
819
820 /// Create an invoke to the experimental.gc.statepoint intrinsic to
821 /// start a new statepoint sequence.
822 InvokeInst *
823 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
824 FunctionCallee ActualInvokee, BasicBlock *NormalDest,
825 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
826 Optional<ArrayRef<Value *>> DeoptArgs,
827 ArrayRef<Value *> GCArgs, const Twine &Name = "");
828
829 /// Create an invoke to the experimental.gc.statepoint intrinsic to
830 /// start a new statepoint sequence.
831 InvokeInst *CreateGCStatepointInvoke(
832 uint64_t ID, uint32_t NumPatchBytes, FunctionCallee ActualInvokee,
833 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
834 ArrayRef<Value *> InvokeArgs, Optional<ArrayRef<Use>> TransitionArgs,
835 Optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs,
836 const Twine &Name = "");
837
838 // Convenience function for the common case when CallArgs are filled in using
839 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
840 // get the Value *.
841 InvokeInst *
842 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
843 FunctionCallee ActualInvokee, BasicBlock *NormalDest,
844 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
845 Optional<ArrayRef<Value *>> DeoptArgs,
846 ArrayRef<Value *> GCArgs, const Twine &Name = "");
847
848 /// Create a call to the experimental.gc.result intrinsic to extract
849 /// the result from a call wrapped in a statepoint.
850 CallInst *CreateGCResult(Instruction *Statepoint,
851 Type *ResultType,
852 const Twine &Name = "");
853
854 /// Create a call to the experimental.gc.relocate intrinsics to
855 /// project the relocated value of one pointer from the statepoint.
856 CallInst *CreateGCRelocate(Instruction *Statepoint,
857 int BaseOffset,
858 int DerivedOffset,
859 Type *ResultType,
860 const Twine &Name = "");
861
862 /// Create a call to the experimental.gc.pointer.base intrinsic to get the
863 /// base pointer for the specified derived pointer.
864 CallInst *CreateGCGetPointerBase(Value *DerivedPtr, const Twine &Name = "");
865
866 /// Create a call to the experimental.gc.get.pointer.offset intrinsic to get
867 /// the offset of the specified derived pointer from its base.
868 CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = "");
869
870 /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale
871 /// will be the same type as that of \p Scaling.
872 Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
873
874 /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...>
875 Value *CreateStepVector(Type *DstType, const Twine &Name = "");
876
877 /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
878 /// type.
879 CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
880 Instruction *FMFSource = nullptr,
881 const Twine &Name = "");
882
883 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
884 /// first type.
885 CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
886 Instruction *FMFSource = nullptr,
887 const Twine &Name = "");
888
889 /// Create a call to intrinsic \p ID with \p Args, mangled using \p Types. If
890 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
891 /// the intrinsic.
892 CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
893 ArrayRef<Value *> Args,
894 Instruction *FMFSource = nullptr,
895 const Twine &Name = "");
896
897 /// Create a call to intrinsic \p ID with \p RetTy and \p Args. If
898 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
899 /// the intrinsic.
900 CallInst *CreateIntrinsic(Type *RetTy, Intrinsic::ID ID,
901 ArrayRef<Value *> Args,
902 Instruction *FMFSource = nullptr,
903 const Twine &Name = "");
904
905 /// Create call to the minnum intrinsic.
906 CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
907 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
908 }
909
910 /// Create call to the maxnum intrinsic.
911 CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
912 return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
913 }
914
915 /// Create call to the minimum intrinsic.
916 CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
917 return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
918 }
919
920 /// Create call to the maximum intrinsic.
921 CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
922 return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
923 }
924
925 /// Create a call to the arithmetic_fence intrinsic.
926 CallInst *CreateArithmeticFence(Value *Val, Type *DstType,
927 const Twine &Name = "") {
928 return CreateIntrinsic(Intrinsic::arithmetic_fence, DstType, Val, nullptr,
929 Name);
930 }
931
932 /// Create a call to the vector.extract intrinsic.
933 CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, Value *Idx,
934 const Twine &Name = "") {
935 return CreateIntrinsic(Intrinsic::vector_extract,
936 {DstType, SrcVec->getType()}, {SrcVec, Idx}, nullptr,
937 Name);
938 }
939
940 /// Create a call to the vector.insert intrinsic.
941 CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec,
942 Value *Idx, const Twine &Name = "") {
943 return CreateIntrinsic(Intrinsic::vector_insert,
944 {DstType, SubVec->getType()}, {SrcVec, SubVec, Idx},
945 nullptr, Name);
946 }
947
948private:
949 /// Create a call to a masked intrinsic with given Id.
950 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
951 ArrayRef<Type *> OverloadedTypes,
952 const Twine &Name = "");
953
954 Value *getCastedInt8PtrValue(Value *Ptr);
955
956 //===--------------------------------------------------------------------===//
957 // Instruction creation methods: Terminators
958 //===--------------------------------------------------------------------===//
959
960private:
961 /// Helper to add branch weight and unpredictable metadata onto an
962 /// instruction.
963 /// \returns The annotated instruction.
964 template <typename InstTy>
965 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
966 if (Weights)
967 I->setMetadata(LLVMContext::MD_prof, Weights);
968 if (Unpredictable)
969 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
970 return I;
971 }
972
973public:
974 /// Create a 'ret void' instruction.
975 ReturnInst *CreateRetVoid() {
976 return Insert(ReturnInst::Create(Context));
977 }
978
979 /// Create a 'ret <val>' instruction.
980 ReturnInst *CreateRet(Value *V) {
981 return Insert(ReturnInst::Create(Context, V));
982 }
983
984 /// Create a sequence of N insertvalue instructions,
985 /// with one Value from the retVals array each, that build a aggregate
986 /// return value one value at a time, and a ret instruction to return
987 /// the resulting aggregate value.
988 ///
989 /// This is a convenience function for code that uses aggregate return values
990 /// as a vehicle for having multiple return values.
991 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
992 Value *V = PoisonValue::get(getCurrentFunctionReturnType());
993 for (unsigned i = 0; i != N; ++i)
994 V = CreateInsertValue(V, retVals[i], i, "mrv");
995 return Insert(ReturnInst::Create(Context, V));
996 }
997
998 /// Create an unconditional 'br label X' instruction.
999 BranchInst *CreateBr(BasicBlock *Dest) {
1000 return Insert(BranchInst::Create(Dest));
1001 }
1002
1003 /// Create a conditional 'br Cond, TrueDest, FalseDest'
1004 /// instruction.
1005 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1006 MDNode *BranchWeights = nullptr,
1007 MDNode *Unpredictable = nullptr) {
1008 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
1009 BranchWeights, Unpredictable));
1010 }
1011
1012 /// Create a conditional 'br Cond, TrueDest, FalseDest'
1013 /// instruction. Copy branch meta data if available.
1014 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1015 Instruction *MDSrc) {
1016 BranchInst *Br = BranchInst::Create(True, False, Cond);
1017 if (MDSrc) {
1018 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
1019 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
1020 Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
1021 }
1022 return Insert(Br);
1023 }
1024
1025 /// Create a switch instruction with the specified value, default dest,
1026 /// and with a hint for the number of cases that will be added (for efficient
1027 /// allocation).
1028 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
1029 MDNode *BranchWeights = nullptr,
1030 MDNode *Unpredictable = nullptr) {
1031 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
1032 BranchWeights, Unpredictable));
1033 }
1034
1035 /// Create an indirect branch instruction with the specified address
1036 /// operand, with an optional hint for the number of destinations that will be
1037 /// added (for efficient allocation).
1038 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
1039 return Insert(IndirectBrInst::Create(Addr, NumDests));
1040 }
1041
1042 /// Create an invoke instruction.
1043 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1044 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1045 ArrayRef<Value *> Args,
1046 ArrayRef<OperandBundleDef> OpBundles,
1047 const Twine &Name = "") {
1048 InvokeInst *II =
1049 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles);
1050 if (IsFPConstrained)
1051 setConstrainedFPCallAttr(II);
1052 return Insert(II, Name);
1053 }
1054 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1055 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1056 ArrayRef<Value *> Args = None,
1057 const Twine &Name = "") {
1058 InvokeInst *II =
1059 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args);
1060 if (IsFPConstrained)
1061 setConstrainedFPCallAttr(II);
1062 return Insert(II, Name);
1063 }
1064
1065 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1066 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
1067 ArrayRef<OperandBundleDef> OpBundles,
1068 const Twine &Name = "") {
1069 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1070 NormalDest, UnwindDest, Args, OpBundles, Name);
1071 }
1072
1073 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1074 BasicBlock *UnwindDest,
1075 ArrayRef<Value *> Args = None,
1076 const Twine &Name = "") {
1077 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1078 NormalDest, UnwindDest, Args, Name);
1079 }
1080
1081 /// \brief Create a callbr instruction.
1082 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1083 BasicBlock *DefaultDest,
1084 ArrayRef<BasicBlock *> IndirectDests,
1085 ArrayRef<Value *> Args = None,
1086 const Twine &Name = "") {
1087 return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
1088 Args), Name);
1089 }
1090 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1091 BasicBlock *DefaultDest,
1092 ArrayRef<BasicBlock *> IndirectDests,
1093 ArrayRef<Value *> Args,
1094 ArrayRef<OperandBundleDef> OpBundles,
1095 const Twine &Name = "") {
1096 return Insert(
1097 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
1098 OpBundles), Name);
1099 }
1100
1101 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1102 ArrayRef<BasicBlock *> IndirectDests,
1103 ArrayRef<Value *> Args = None,
1104 const Twine &Name = "") {
1105 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1106 DefaultDest, IndirectDests, Args, Name);
1107 }
1108 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1109 ArrayRef<BasicBlock *> IndirectDests,
1110 ArrayRef<Value *> Args,
1111 ArrayRef<OperandBundleDef> OpBundles,
1112 const Twine &Name = "") {
1113 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1114 DefaultDest, IndirectDests, Args, Name);
1115 }
1116
1117 ResumeInst *CreateResume(Value *Exn) {
1118 return Insert(ResumeInst::Create(Exn));
1119 }
1120
1121 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1122 BasicBlock *UnwindBB = nullptr) {
1123 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
1124 }
1125
1126 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1127 unsigned NumHandlers,
1128 const Twine &Name = "") {
1129 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
1130 Name);
1131 }
1132
1133 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1134 const Twine &Name = "") {
1135 return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1136 }
1137
1138 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1139 ArrayRef<Value *> Args = None,
1140 const Twine &Name = "") {
1141 return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1142 }
1143
1144 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1145 return Insert(CatchReturnInst::Create(CatchPad, BB));
1146 }
1147
1148 UnreachableInst *CreateUnreachable() {
1149 return Insert(new UnreachableInst(Context));
1150 }
1151
1152 //===--------------------------------------------------------------------===//
1153 // Instruction creation methods: Binary Operators
1154 //===--------------------------------------------------------------------===//
1155private:
1156 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1157 Value *LHS, Value *RHS,
1158 const Twine &Name,
1159 bool HasNUW, bool HasNSW) {
1160 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1161 if (HasNUW) BO->setHasNoUnsignedWrap();
1162 if (HasNSW) BO->setHasNoSignedWrap();
1163 return BO;
1164 }
1165
1166 Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1167 FastMathFlags FMF) const {
1168 if (!FPMD)
1169 FPMD = DefaultFPMathTag;
1170 if (FPMD)
1171 I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1172 I->setFastMathFlags(FMF);
1173 return I;
1174 }
1175
1176 Value *getConstrainedFPRounding(Optional<RoundingMode> Rounding) {
1177 RoundingMode UseRounding = DefaultConstrainedRounding;
1178
1179 if (Rounding)
1180 UseRounding = Rounding.value();
1181
1182 Optional<StringRef> RoundingStr = convertRoundingModeToStr(UseRounding);
1183 assert(RoundingStr && "Garbage strict rounding mode!");
1184 auto *RoundingMDS = MDString::get(Context, RoundingStr.value());
1185
1186 return MetadataAsValue::get(Context, RoundingMDS);
1187 }
1188
1189 Value *getConstrainedFPExcept(Optional<fp::ExceptionBehavior> Except) {
1190 fp::ExceptionBehavior UseExcept = DefaultConstrainedExcept;
1191
1192 if (Except)
1193 UseExcept = Except.value();
1194
1195 Optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(UseExcept);
1196 assert(ExceptStr && "Garbage strict exception behavior!");
1197 auto *ExceptMDS = MDString::get(Context, ExceptStr.value());
1198
1199 return MetadataAsValue::get(Context, ExceptMDS);
1200 }
1201
1202 Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) {
1203 assert(CmpInst::isFPPredicate(Predicate) &&
1204 Predicate != CmpInst::FCMP_FALSE &&
1205 Predicate != CmpInst::FCMP_TRUE &&
1206 "Invalid constrained FP comparison predicate!");
1207
1208 StringRef PredicateStr = CmpInst::getPredicateName(Predicate);
1209 auto *PredicateMDS = MDString::get(Context, PredicateStr);
1210
1211 return MetadataAsValue::get(Context, PredicateMDS);
1212 }
1213
1214public:
1215 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1216 bool HasNUW = false, bool HasNSW = false) {
1217 if (Value *V =
1218 Folder.FoldNoWrapBinOp(Instruction::Add, LHS, RHS, HasNUW, HasNSW))
1219 return V;
1220 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, HasNUW,
1221 HasNSW);
1222 }
1223
1224 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1225 return CreateAdd(LHS, RHS, Name, false, true);
1226 }
1227
1228 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1229 return CreateAdd(LHS, RHS, Name, true, false);
1230 }
1231
1232 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1233 bool HasNUW = false, bool HasNSW = false) {
1234 if (Value *V =
1235 Folder.FoldNoWrapBinOp(Instruction::Sub, LHS, RHS, HasNUW, HasNSW))
1236 return V;
1237 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, HasNUW,
1238 HasNSW);
1239 }
1240
1241 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1242 return CreateSub(LHS, RHS, Name, false, true);
1243 }
1244
1245 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1246 return CreateSub(LHS, RHS, Name, true, false);
1247 }
1248
1249 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1250 bool HasNUW = false, bool HasNSW = false) {
1251 if (Value *V =
1252 Folder.FoldNoWrapBinOp(Instruction::Mul, LHS, RHS, HasNUW, HasNSW))
1253 return V;
1254 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, HasNUW,
1255 HasNSW);
1256 }
1257
1258 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1259 return CreateMul(LHS, RHS, Name, false, true);
1260 }
1261
1262 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1263 return CreateMul(LHS, RHS, Name, true, false);
1264 }
1265
1266 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1267 bool isExact = false) {
1268 if (Value *V = Folder.FoldExactBinOp(Instruction::UDiv, LHS, RHS, isExact))
1269 return V;
1270 if (!isExact)
1271 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1272 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1273 }
1274
1275 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1276 return CreateUDiv(LHS, RHS, Name, true);
1277 }
1278
1279 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1280 bool isExact = false) {
1281 if (Value *V = Folder.FoldExactBinOp(Instruction::SDiv, LHS, RHS, isExact))
1282 return V;
1283 if (!isExact)
1284 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1285 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1286 }
1287
1288 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1289 return CreateSDiv(LHS, RHS, Name, true);
1290 }
1291
1292 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1293 if (Value *V = Folder.FoldBinOp(Instruction::URem, LHS, RHS))
1294 return V;
1295 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1296 }
1297
1298 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1299 if (Value *V = Folder.FoldBinOp(Instruction::SRem, LHS, RHS))
1300 return V;
1301 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1302 }
1303
1304 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1305 bool HasNUW = false, bool HasNSW = false) {
1306 if (Value *V =
1307 Folder.FoldNoWrapBinOp(Instruction::Shl, LHS, RHS, HasNUW, HasNSW))
1308 return V;
1309 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1310 HasNUW, HasNSW);
1311 }
1312
1313 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1314 bool HasNUW = false, bool HasNSW = false) {
1315 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1316 HasNUW, HasNSW);
1317 }
1318
1319 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1320 bool HasNUW = false, bool HasNSW = false) {
1321 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1322 HasNUW, HasNSW);
1323 }
1324
1325 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1326 bool isExact = false) {
1327 if (Value *V = Folder.FoldExactBinOp(Instruction::LShr, LHS, RHS, isExact))
1328 return V;
1329 if (!isExact)
1330 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1331 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1332 }
1333
1334 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1335 bool isExact = false) {
1336 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1337 }
1338
1339 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1340 bool isExact = false) {
1341 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1342 }
1343
1344 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1345 bool isExact = false) {
1346 if (Value *V = Folder.FoldExactBinOp(Instruction::AShr, LHS, RHS, isExact))
1347 return V;
1348 if (!isExact)
1349 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1350 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1351 }
1352
1353 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1354 bool isExact = false) {
1355 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1356 }
1357
1358 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1359 bool isExact = false) {
1360 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1361 }
1362
1363 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1364 if (auto *V = Folder.FoldBinOp(Instruction::And, LHS, RHS))
1365 return V;
1366 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1367 }
1368
1369 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1370 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1371 }
1372
1373 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1374 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1375 }
1376
1377 Value *CreateAnd(ArrayRef<Value*> Ops) {
1378 assert(!Ops.empty());
1379 Value *Accum = Ops[0];
1380 for (unsigned i = 1; i < Ops.size(); i++)
1381 Accum = CreateAnd(Accum, Ops[i]);
1382 return Accum;
1383 }
1384
1385 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1386 if (auto *V = Folder.FoldBinOp(Instruction::Or, LHS, RHS))
1387 return V;
1388 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1389 }
1390
1391 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1392 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1393 }
1394
1395 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1396 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1397 }
1398
1399 Value *CreateOr(ArrayRef<Value*> Ops) {
1400 assert(!Ops.empty());
1401 Value *Accum = Ops[0];
1402 for (unsigned i = 1; i < Ops.size(); i++)
1403 Accum = CreateOr(Accum, Ops[i]);
1404 return Accum;
1405 }
1406
1407 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1408 if (Value *V = Folder.FoldBinOp(Instruction::Xor, LHS, RHS))
1409 return V;
1410 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1411 }
1412
1413 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1414 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1415 }
1416
1417 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1418 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1419 }
1420
1421 Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1422 MDNode *FPMD = nullptr) {
1423 if (IsFPConstrained)
1424 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1425 L, R, nullptr, Name, FPMD);
1426
1427 if (Value *V = Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMF))
1428 return V;
1429 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
1430 return Insert(I, Name);
1431 }
1432
1433 /// Copy fast-math-flags from an instruction rather than using the builder's
1434 /// default FMF.
1435 Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
1436 const Twine &Name = "") {
1437 if (IsFPConstrained)
1438 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1439 L, R, FMFSource, Name);
1440
1441 FastMathFlags FMF = FMFSource->getFastMathFlags();
1442 if (Value *V = Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMF))
1443 return V;
1444 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr, FMF);
1445 return Insert(I, Name);
1446 }
1447
1448 Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1449 MDNode *FPMD = nullptr) {
1450 if (IsFPConstrained)
1451 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1452 L, R, nullptr, Name, FPMD);
1453
1454 if (Value *V = Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMF))
1455 return V;
1456 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
1457 return Insert(I, Name);
1458 }
1459
1460 /// Copy fast-math-flags from an instruction rather than using the builder's
1461 /// default FMF.
1462 Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
1463 const Twine &Name = "") {
1464 if (IsFPConstrained)
1465 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1466 L, R, FMFSource, Name);
1467
1468 FastMathFlags FMF = FMFSource->getFastMathFlags();
1469 if (Value *V = Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMF))
1470 return V;
1471 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr, FMF);
1472 return Insert(I, Name);
1473 }
1474
1475 Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1476 MDNode *FPMD = nullptr) {
1477 if (IsFPConstrained)
1478 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1479 L, R, nullptr, Name, FPMD);
1480
1481 if (Value *V = Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMF))
1482 return V;
1483 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
1484 return Insert(I, Name);
1485 }
1486
1487 /// Copy fast-math-flags from an instruction rather than using the builder's
1488 /// default FMF.
1489 Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
1490 const Twine &Name = "") {
1491 if (IsFPConstrained)
1492 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1493 L, R, FMFSource, Name);
1494
1495 FastMathFlags FMF = FMFSource->getFastMathFlags();
1496 if (Value *V = Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMF))
1497 return V;
1498 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr, FMF);
1499 return Insert(I, Name);
1500 }
1501
1502 Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1503 MDNode *FPMD = nullptr) {
1504 if (IsFPConstrained)
1505 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1506 L, R, nullptr, Name, FPMD);
1507
1508 if (Value *V = Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMF))
1509 return V;
1510 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
1511 return Insert(I, Name);
1512 }
1513
1514 /// Copy fast-math-flags from an instruction rather than using the builder's
1515 /// default FMF.
1516 Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
1517 const Twine &Name = "") {
1518 if (IsFPConstrained)
1519 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1520 L, R, FMFSource, Name);
1521
1522 if (Value *V = Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMF))
1523 return V;
1524 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr, FMF);
1525 return Insert(I, Name);
1526 }
1527
1528 Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1529 MDNode *FPMD = nullptr) {
1530 if (IsFPConstrained)
1531 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1532 L, R, nullptr, Name, FPMD);
1533
1534 if (Value *V = Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMF)) return V;
1535 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
1536 return Insert(I, Name);
1537 }
1538
1539 /// Copy fast-math-flags from an instruction rather than using the builder's
1540 /// default FMF.
1541 Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
1542 const Twine &Name = "") {
1543 if (IsFPConstrained)
1544 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1545 L, R, FMFSource, Name);
1546
1547 FastMathFlags FMF = FMFSource->getFastMathFlags();
1548 if (Value *V = Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMF)) return V;
1549 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr, FMF);
1550 return Insert(I, Name);
1551 }
1552
1553 Value *CreateBinOp(Instruction::BinaryOps Opc,
1554 Value *LHS, Value *RHS, const Twine &Name = "",
1555 MDNode *FPMathTag = nullptr) {
1556 if (Value *V = Folder.FoldBinOp(Opc, LHS, RHS)) return V;
1557 Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1558 if (isa<FPMathOperator>(BinOp))
1559 setFPAttrs(BinOp, FPMathTag, FMF);
1560 return Insert(BinOp, Name);
1561 }
1562
1563 Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1564 assert(Cond2->getType()->isIntOrIntVectorTy(1));
1565 return CreateSelect(Cond1, Cond2,
1566 ConstantInt::getNullValue(Cond2->getType()), Name);
1567 }
1568
1569 Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1570 assert(Cond2->getType()->isIntOrIntVectorTy(1));
1571 return CreateSelect(Cond1, ConstantInt::getAllOnesValue(Cond2->getType()),
1572 Cond2, Name);
1573 }
1574
1575 // NOTE: this is sequential, non-commutative, ordered reduction!
1576 Value *CreateLogicalOr(ArrayRef<Value *> Ops) {
1577 assert(!Ops.empty());
1578 Value *Accum = Ops[0];
1579 for (unsigned i = 1; i < Ops.size(); i++)
1580 Accum = CreateLogicalOr(Accum, Ops[i]);
1581 return Accum;
1582 }
1583
1584 CallInst *CreateConstrainedFPBinOp(
1585 Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
1586 const Twine &Name = "", MDNode *FPMathTag = nullptr,
1587 Optional<RoundingMode> Rounding = None,
1588 Optional<fp::ExceptionBehavior> Except = None);
1589
1590 Value *CreateNeg(Value *V, const Twine &Name = "", bool HasNUW = false,
1591 bool HasNSW = false) {
1592 return CreateSub(Constant::getNullValue(V->getType()), V, Name, HasNUW,
1593 HasNSW);
1594 }
1595
1596 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1597 return CreateNeg(V, Name, false, true);
1598 }
1599
1600 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1601 return CreateNeg(V, Name, true, false);
1602 }
1603
1604 Value *CreateFNeg(Value *V, const Twine &Name = "",
1605 MDNode *FPMathTag = nullptr) {
1606 if (Value *Res = Folder.FoldUnOpFMF(Instruction::FNeg, V, FMF))
1607 return Res;
1608 return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF),
1609 Name);
1610 }
1611
1612 /// Copy fast-math-flags from an instruction rather than using the builder's
1613 /// default FMF.
1614 Value *CreateFNegFMF(Value *V, Instruction *FMFSource,
1615 const Twine &Name = "") {
1616 FastMathFlags FMF = FMFSource->getFastMathFlags();
1617 if (Value *Res = Folder.FoldUnOpFMF(Instruction::FNeg, V, FMF))
1618 return Res;
1619 return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr, FMF),
1620 Name);
1621 }
1622
1623 Value *CreateNot(Value *V, const Twine &Name = "") {
1624 return CreateXor(V, Constant::getAllOnesValue(V->getType()), Name);
1625 }
1626
1627 Value *CreateUnOp(Instruction::UnaryOps Opc,
1628 Value *V, const Twine &Name = "",
1629 MDNode *FPMathTag = nullptr) {
1630 if (Value *Res = Folder.FoldUnOpFMF(Opc, V, FMF))
1631 return Res;
1632 Instruction *UnOp = UnaryOperator::Create(Opc, V);
1633 if (isa<FPMathOperator>(UnOp))
1634 setFPAttrs(UnOp, FPMathTag, FMF);
1635 return Insert(UnOp, Name);
1636 }
1637
1638 /// Create either a UnaryOperator or BinaryOperator depending on \p Opc.
1639 /// Correct number of operands must be passed accordingly.
1640 Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
1641 const Twine &Name = "", MDNode *FPMathTag = nullptr);
1642
1643 //===--------------------------------------------------------------------===//
1644 // Instruction creation methods: Memory Instructions
1645 //===--------------------------------------------------------------------===//
1646
1647 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1648 Value *ArraySize = nullptr, const Twine &Name = "") {
1649 const DataLayout &DL = BB->getModule()->getDataLayout();
1650 Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1651 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1652 }
1653
1654 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1655 const Twine &Name = "") {
1656 const DataLayout &DL = BB->getModule()->getDataLayout();
1657 Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1658 unsigned AddrSpace = DL.getAllocaAddrSpace();
1659 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1660 }
1661
1662 /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1663 /// converting the string to 'bool' for the isVolatile parameter.
1664 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1665 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1666 }
1667
1668 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1669 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1670 }
1671
1672 LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1673 const Twine &Name = "") {
1674 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name);
1675 }
1676
1677 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1678 return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile);
1679 }
1680
1681 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1682 const char *Name) {
1683 return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1684 }
1685
1686 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1687 const Twine &Name = "") {
1688 return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1689 }
1690
1691 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1692 bool isVolatile, const Twine &Name = "") {
1693 if (!Align) {
1694 const DataLayout &DL = BB->getModule()->getDataLayout();
1695 Align = DL.getABITypeAlign(Ty);
1696 }
1697 return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
1698 }
1699
1700 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
1701 bool isVolatile = false) {
1702 if (!Align) {
1703 const DataLayout &DL = BB->getModule()->getDataLayout();
1704 Align = DL.getABITypeAlign(Val->getType());
1705 }
1706 return Insert(new StoreInst(Val, Ptr, isVolatile, *Align));
1707 }
1708 FenceInst *CreateFence(AtomicOrdering Ordering,
1709 SyncScope::ID SSID = SyncScope::System,
1710 const Twine &Name = "") {
1711 return Insert(new FenceInst(Context, Ordering, SSID), Name);
1712 }
1713
1714 AtomicCmpXchgInst *
1715 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
1716 AtomicOrdering SuccessOrdering,
1717 AtomicOrdering FailureOrdering,
1718 SyncScope::ID SSID = SyncScope::System) {
1719 if (!Align) {
1720 const DataLayout &DL = BB->getModule()->getDataLayout();
1721 Align = llvm::Align(DL.getTypeStoreSize(New->getType()));
1722 }
1723
1724 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, *Align, SuccessOrdering,
1725 FailureOrdering, SSID));
1726 }
1727
1728 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr,
1729 Value *Val, MaybeAlign Align,
1730 AtomicOrdering Ordering,
1731 SyncScope::ID SSID = SyncScope::System) {
1732 if (!Align) {
1733 const DataLayout &DL = BB->getModule()->getDataLayout();
1734 Align = llvm::Align(DL.getTypeStoreSize(Val->getType()));
1735 }
1736
1737 return Insert(new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID));
1738 }
1739
1740 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1741 const Twine &Name = "", bool IsInBounds = false) {
1742 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, IsInBounds))
1743 return V;
1744 return Insert(IsInBounds
1745 ? GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList)
1746 : GetElementPtrInst::Create(Ty, Ptr, IdxList),
1747 Name);
1748 }
1749
1750 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1751 const Twine &Name = "") {
1752 return CreateGEP(Ty, Ptr, IdxList, Name, /* IsInBounds */ true);
1753 }
1754
1755 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1756 const Twine &Name = "") {
1757 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1758
1759 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/false))
1760 return V;
1761
1762 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1763 }
1764
1765 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1766 const Twine &Name = "") {
1767 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1768
1769 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/true))
1770 return V;
1771
1772 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1773 }
1774
1775 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1776 const Twine &Name = "") {
1777 Value *Idxs[] = {
1778 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1779 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1780 };
1781
1782 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/false))
1783 return V;
1784
1785 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1786 }
1787
1788 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1789 unsigned Idx1, const Twine &Name = "") {
1790 Value *Idxs[] = {
1791 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1792 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1793 };
1794
1795 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/true))
1796 return V;
1797
1798 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1799 }
1800
1801 Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1802 const Twine &Name = "") {
1803 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1804
1805 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/false))
1806 return V;
1807
1808 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1809 }
1810
1811 Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1812 const Twine &Name = "") {
1813 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1814
1815 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/true))
1816 return V;
1817
1818 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1819 }
1820
1821 Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1822 const Twine &Name = "") {
1823 Value *Idxs[] = {
1824 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1825 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1826 };
1827
1828 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/false))
1829 return V;
1830
1831 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1832 }
1833
1834 Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1835 uint64_t Idx1, const Twine &Name = "") {
1836 Value *Idxs[] = {
1837 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1838 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1839 };
1840
1841 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/true))
1842 return V;
1843
1844 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1845 }
1846
1847 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1848 const Twine &Name = "") {
1849 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1850 }
1851
1852 /// Same as CreateGlobalString, but return a pointer with "i8*" type
1853 /// instead of a pointer to array of i8.
1854 ///
1855 /// If no module is given via \p M, it is take from the insertion point basic
1856 /// block.
1857 Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1858 unsigned AddressSpace = 0,
1859 Module *M = nullptr) {
1860 GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M);
1861 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1862 Constant *Indices[] = {Zero, Zero};
1863 return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
1864 Indices);
1865 }
1866
1867 //===--------------------------------------------------------------------===//
1868 // Instruction creation methods: Cast/Conversion Operators
1869 //===--------------------------------------------------------------------===//
1870
1871 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1872 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1873 }
1874
1875 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1876 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1877 }
1878
1879 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1880 return CreateCast(Instruction::SExt, V, DestTy, Name);
1881 }
1882
1883 /// Create a ZExt or Trunc from the integer value V to DestTy. Return
1884 /// the value untouched if the type of V is already DestTy.
1885 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1886 const Twine &Name = "") {
1887 assert(V->getType()->isIntOrIntVectorTy() &&
1888 DestTy->isIntOrIntVectorTy() &&
1889 "Can only zero extend/truncate integers!");
1890 Type *VTy = V->getType();
1891 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1892 return CreateZExt(V, DestTy, Name);
1893 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1894 return CreateTrunc(V, DestTy, Name);
1895 return V;
1896 }
1897
1898 /// Create a SExt or Trunc from the integer value V to DestTy. Return
1899 /// the value untouched if the type of V is already DestTy.
1900 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1901 const Twine &Name = "") {
1902 assert(V->getType()->isIntOrIntVectorTy() &&
1903 DestTy->isIntOrIntVectorTy() &&
1904 "Can only sign extend/truncate integers!");
1905 Type *VTy = V->getType();
1906 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1907 return CreateSExt(V, DestTy, Name);
1908 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1909 return CreateTrunc(V, DestTy, Name);
1910 return V;
1911 }
1912
1913 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
1914 if (IsFPConstrained)
1915 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui,
1916 V, DestTy, nullptr, Name);
1917 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1918 }
1919
1920 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
1921 if (IsFPConstrained)
1922 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi,
1923 V, DestTy, nullptr, Name);
1924 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1925 }
1926
1927 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1928 if (IsFPConstrained)
1929 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp,
1930 V, DestTy, nullptr, Name);
1931 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1932 }
1933
1934 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1935 if (IsFPConstrained)
1936 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp,
1937 V, DestTy, nullptr, Name);
1938 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1939 }
1940
1941 Value *CreateFPTrunc(Value *V, Type *DestTy,
1942 const Twine &Name = "") {
1943 if (IsFPConstrained)
1944 return CreateConstrainedFPCast(
1945 Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr,
1946 Name);
1947 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1948 }
1949
1950 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1951 if (IsFPConstrained)
1952 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext,
1953 V, DestTy, nullptr, Name);
1954 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1955 }
1956
1957 Value *CreatePtrToInt(Value *V, Type *DestTy,
1958 const Twine &Name = "") {
1959 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1960 }
1961
1962 Value *CreateIntToPtr(Value *V, Type *DestTy,
1963 const Twine &Name = "") {
1964 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1965 }
1966
1967 Value *CreateBitCast(Value *V, Type *DestTy,
1968 const Twine &Name = "") {
1969 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1970 }
1971
1972 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1973 const Twine &Name = "") {
1974 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1975 }
1976
1977 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1978 const Twine &Name = "") {
1979 if (V->getType() == DestTy)
1980 return V;
1981 if (auto *VC = dyn_cast<Constant>(V))
1982 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1983 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1984 }
1985
1986 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1987 const Twine &Name = "") {
1988 if (V->getType() == DestTy)
1989 return V;
1990 if (auto *VC = dyn_cast<Constant>(V))
1991 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1992 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1993 }
1994
1995 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1996 const Twine &Name = "") {
1997 if (V->getType() == DestTy)
1998 return V;
1999 if (auto *VC = dyn_cast<Constant>(V))
2000 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
2001 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
2002 }
2003
2004 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
2005 const Twine &Name = "") {
2006 if (V->getType() == DestTy)
2007 return V;
2008 if (auto *VC = dyn_cast<Constant>(V))
2009 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
2010 return Insert(CastInst::Create(Op, V, DestTy), Name);
2011 }
2012
2013 Value *CreatePointerCast(Value *V, Type *DestTy,
2014 const Twine &Name = "") {
2015 if (V->getType() == DestTy)
2016 return V;
2017 if (auto *VC = dyn_cast<Constant>(V))
2018 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
2019 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
2020 }
2021
2022 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
2023 const Twine &Name = "") {
2024 if (V->getType() == DestTy)
2025 return V;
2026
2027 if (auto *VC = dyn_cast<Constant>(V)) {
2028 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
2029 Name);
2030 }
2031
2032 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
2033 Name);
2034 }
2035
2036 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
2037 const Twine &Name = "") {
2038 if (V->getType() == DestTy)
2039 return V;
2040 if (auto *VC = dyn_cast<Constant>(V))
2041 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
2042 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
2043 }
2044
2045 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
2046 const Twine &Name = "") {
2047 if (V->getType() == DestTy)
2048 return V;
2049 if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
2050 return CreatePtrToInt(V, DestTy, Name);
2051 if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
2052 return CreateIntToPtr(V, DestTy, Name);
2053
2054 return CreateBitCast(V, DestTy, Name);
2055 }
2056
2057 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
2058 if (V->getType() == DestTy)
2059 return V;
2060 if (auto *VC = dyn_cast<Constant>(V))
2061 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
2062 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
2063 }
2064
2065 CallInst *CreateConstrainedFPCast(
2066 Intrinsic::ID ID, Value *V, Type *DestTy,
2067 Instruction *FMFSource = nullptr, const Twine &Name = "",
2068 MDNode *FPMathTag = nullptr,
2069 Optional<RoundingMode> Rounding = None,
2070 Optional<fp::ExceptionBehavior> Except = None);
2071
2072 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
2073 // compile time error, instead of converting the string to bool for the
2074 // isSigned parameter.
2075 Value *CreateIntCast(Value *, Type *, const char *) = delete;
2076
2077 //===--------------------------------------------------------------------===//
2078 // Instruction creation methods: Compare Instructions
2079 //===--------------------------------------------------------------------===//
2080
2081 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
2082 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
2083 }
2084
2085 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
2086 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
2087 }
2088
2089 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2090 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
2091 }
2092
2093 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2094 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
2095 }
2096
2097 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
2098 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
2099 }
2100
2101 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
2102 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
2103 }
2104
2105 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2106 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
2107 }
2108
2109 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2110 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS,