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