LLVM  9.0.0svn
IRBuilder.h
Go to the documentation of this file.
1 //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the IRBuilder class, which is used as a convenient way
10 // to create LLVM instructions with a consistent and simplified interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_IRBUILDER_H
15 #define LLVM_IR_IRBUILDER_H
16 
17 #include "llvm-c/Types.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Constant.h"
24 #include "llvm/IR/ConstantFolder.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/GlobalVariable.h"
31 #include "llvm/IR/InstrTypes.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/Intrinsics.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/Operator.h"
38 #include "llvm/IR/Type.h"
39 #include "llvm/IR/Value.h"
40 #include "llvm/IR/ValueHandle.h"
43 #include "llvm/Support/Casting.h"
44 #include <cassert>
45 #include <cstddef>
46 #include <cstdint>
47 #include <functional>
48 #include <utility>
49 
50 namespace llvm {
51 
52 class APInt;
53 class MDNode;
54 class Use;
55 
56 /// This provides the default implementation of the IRBuilder
57 /// 'InsertHelper' method that is called whenever an instruction is created by
58 /// IRBuilder and needs to be inserted.
59 ///
60 /// By default, this inserts the instruction at the insertion point.
62 protected:
64  BasicBlock *BB, BasicBlock::iterator InsertPt) const {
65  if (BB) BB->getInstList().insert(InsertPt, I);
66  I->setName(Name);
67  }
68 };
69 
70 /// Provides an 'InsertHelper' that calls a user-provided callback after
71 /// performing the default insertion.
73  std::function<void(Instruction *)> Callback;
74 
75 public:
77  : Callback(std::move(Callback)) {}
78 
79 protected:
81  BasicBlock *BB, BasicBlock::iterator InsertPt) const {
82  IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
83  Callback(I);
84  }
85 };
86 
87 /// Common base class shared among various IRBuilders.
89  DebugLoc CurDbgLocation;
90 
91 protected:
95 
98 
100 
101 public:
102  IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr,
103  ArrayRef<OperandBundleDef> OpBundles = None)
104  : Context(context), DefaultFPMathTag(FPMathTag),
105  DefaultOperandBundles(OpBundles) {
106  ClearInsertionPoint();
107  }
108 
109  //===--------------------------------------------------------------------===//
110  // Builder configuration methods
111  //===--------------------------------------------------------------------===//
112 
113  /// Clear the insertion point: created instructions will not be
114  /// inserted into a block.
116  BB = nullptr;
117  InsertPt = BasicBlock::iterator();
118  }
119 
120  BasicBlock *GetInsertBlock() const { return BB; }
121  BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
122  LLVMContext &getContext() const { return Context; }
123 
124  /// This specifies that created instructions should be appended to the
125  /// end of the specified block.
126  void SetInsertPoint(BasicBlock *TheBB) {
127  BB = TheBB;
128  InsertPt = BB->end();
129  }
130 
131  /// This specifies that created instructions should be inserted before
132  /// the specified instruction.
134  BB = I->getParent();
135  InsertPt = I->getIterator();
136  assert(InsertPt != BB->end() && "Can't read debug loc from end()");
137  SetCurrentDebugLocation(I->getDebugLoc());
138  }
139 
140  /// This specifies that created instructions should be inserted at the
141  /// specified point.
143  BB = TheBB;
144  InsertPt = IP;
145  if (IP != TheBB->end())
146  SetCurrentDebugLocation(IP->getDebugLoc());
147  }
148 
149  /// Set location information used by debugging information.
150  void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
151 
152  /// Get location information used by debugging information.
153  const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
154 
155  /// If this builder has a current debug location, set it on the
156  /// specified instruction.
158  if (CurDbgLocation)
159  I->setDebugLoc(CurDbgLocation);
160  }
161 
162  /// Get the return type of the current function that we're emitting
163  /// into.
164  Type *getCurrentFunctionReturnType() const;
165 
166  /// InsertPoint - A saved insertion point.
167  class InsertPoint {
168  BasicBlock *Block = nullptr;
169  BasicBlock::iterator Point;
170 
171  public:
172  /// Creates a new insertion point which doesn't point to anything.
173  InsertPoint() = default;
174 
175  /// Creates a new insertion point at the given location.
177  : Block(InsertBlock), Point(InsertPoint) {}
178 
179  /// Returns true if this insert point is set.
180  bool isSet() const { return (Block != nullptr); }
181 
182  BasicBlock *getBlock() const { return Block; }
183  BasicBlock::iterator getPoint() const { return Point; }
184  };
185 
186  /// Returns the current insert point.
187  InsertPoint saveIP() const {
188  return InsertPoint(GetInsertBlock(), GetInsertPoint());
189  }
190 
191  /// Returns the current insert point, clearing it in the process.
193  InsertPoint IP(GetInsertBlock(), GetInsertPoint());
194  ClearInsertionPoint();
195  return IP;
196  }
197 
198  /// Sets the current insert point to a previously-saved location.
200  if (IP.isSet())
201  SetInsertPoint(IP.getBlock(), IP.getPoint());
202  else
203  ClearInsertionPoint();
204  }
205 
206  /// Get the floating point math metadata being used.
207  MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
208 
209  /// Get the flags to be applied to created floating point ops
210  FastMathFlags getFastMathFlags() const { return FMF; }
211 
212  /// Clear the fast-math flags.
213  void clearFastMathFlags() { FMF.clear(); }
214 
215  /// Set the floating point math metadata to be used.
216  void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
217 
218  /// Set the fast-math flags to be used with generated fp-math operators
219  void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
220 
221  //===--------------------------------------------------------------------===//
222  // RAII helpers.
223  //===--------------------------------------------------------------------===//
224 
225  // RAII object that stores the current insertion point and restores it
226  // when the object is destroyed. This includes the debug location.
228  IRBuilderBase &Builder;
230  BasicBlock::iterator Point;
231  DebugLoc DbgLoc;
232 
233  public:
235  : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
236  DbgLoc(B.getCurrentDebugLocation()) {}
237 
238  InsertPointGuard(const InsertPointGuard &) = delete;
239  InsertPointGuard &operator=(const InsertPointGuard &) = delete;
240 
242  Builder.restoreIP(InsertPoint(Block, Point));
243  Builder.SetCurrentDebugLocation(DbgLoc);
244  }
245  };
246 
247  // RAII object that stores the current fast math settings and restores
248  // them when the object is destroyed.
250  IRBuilderBase &Builder;
251  FastMathFlags FMF;
252  MDNode *FPMathTag;
253 
254  public:
256  : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
257 
258  FastMathFlagGuard(const FastMathFlagGuard &) = delete;
259  FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
260 
262  Builder.FMF = FMF;
263  Builder.DefaultFPMathTag = FPMathTag;
264  }
265  };
266 
267  //===--------------------------------------------------------------------===//
268  // Miscellaneous creation methods.
269  //===--------------------------------------------------------------------===//
270 
271  /// Make a new global variable with initializer type i8*
272  ///
273  /// Make a new global variable with an initializer that has array of i8 type
274  /// filled in with the null terminated string value specified. The new global
275  /// variable will be marked mergable with any others of the same contents. If
276  /// Name is specified, it is the name of the global variable created.
277  GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
278  unsigned AddressSpace = 0);
279 
280  /// Get a constant value representing either true or false.
281  ConstantInt *getInt1(bool V) {
282  return ConstantInt::get(getInt1Ty(), V);
283  }
284 
285  /// Get the constant value for i1 true.
287  return ConstantInt::getTrue(Context);
288  }
289 
290  /// Get the constant value for i1 false.
292  return ConstantInt::getFalse(Context);
293  }
294 
295  /// Get a constant 8-bit value.
296  ConstantInt *getInt8(uint8_t C) {
297  return ConstantInt::get(getInt8Ty(), C);
298  }
299 
300  /// Get a constant 16-bit value.
301  ConstantInt *getInt16(uint16_t C) {
302  return ConstantInt::get(getInt16Ty(), C);
303  }
304 
305  /// Get a constant 32-bit value.
307  return ConstantInt::get(getInt32Ty(), C);
308  }
309 
310  /// Get a constant 64-bit value.
311  ConstantInt *getInt64(uint64_t C) {
312  return ConstantInt::get(getInt64Ty(), C);
313  }
314 
315  /// Get a constant N-bit value, zero extended or truncated from
316  /// a 64-bit value.
317  ConstantInt *getIntN(unsigned N, uint64_t C) {
318  return ConstantInt::get(getIntNTy(N), C);
319  }
320 
321  /// Get a constant integer value.
322  ConstantInt *getInt(const APInt &AI) {
323  return ConstantInt::get(Context, AI);
324  }
325 
326  //===--------------------------------------------------------------------===//
327  // Type creation methods
328  //===--------------------------------------------------------------------===//
329 
330  /// Fetch the type representing a single bit
332  return Type::getInt1Ty(Context);
333  }
334 
335  /// Fetch the type representing an 8-bit integer.
337  return Type::getInt8Ty(Context);
338  }
339 
340  /// Fetch the type representing a 16-bit integer.
342  return Type::getInt16Ty(Context);
343  }
344 
345  /// Fetch the type representing a 32-bit integer.
347  return Type::getInt32Ty(Context);
348  }
349 
350  /// Fetch the type representing a 64-bit integer.
352  return Type::getInt64Ty(Context);
353  }
354 
355  /// Fetch the type representing a 128-bit integer.
356  IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
357 
358  /// Fetch the type representing an N-bit integer.
359  IntegerType *getIntNTy(unsigned N) {
360  return Type::getIntNTy(Context, N);
361  }
362 
363  /// Fetch the type representing a 16-bit floating point value.
365  return Type::getHalfTy(Context);
366  }
367 
368  /// Fetch the type representing a 32-bit floating point value.
370  return Type::getFloatTy(Context);
371  }
372 
373  /// Fetch the type representing a 64-bit floating point value.
375  return Type::getDoubleTy(Context);
376  }
377 
378  /// Fetch the type representing void.
380  return Type::getVoidTy(Context);
381  }
382 
383  /// Fetch the type representing a pointer to an 8-bit integer value.
384  PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
385  return Type::getInt8PtrTy(Context, AddrSpace);
386  }
387 
388  /// Fetch the type representing a pointer to an integer value.
389  IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
390  return DL.getIntPtrType(Context, AddrSpace);
391  }
392 
393  //===--------------------------------------------------------------------===//
394  // Intrinsic creation methods
395  //===--------------------------------------------------------------------===//
396 
397  /// Create and insert a memset to the specified pointer and the
398  /// specified value.
399  ///
400  /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
401  /// specified, it will be added to the instruction. Likewise with alias.scope
402  /// and noalias tags.
403  CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
404  bool isVolatile = false, MDNode *TBAATag = nullptr,
405  MDNode *ScopeTag = nullptr,
406  MDNode *NoAliasTag = nullptr) {
407  return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
408  TBAATag, ScopeTag, NoAliasTag);
409  }
410 
411  CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
412  bool isVolatile = false, MDNode *TBAATag = nullptr,
413  MDNode *ScopeTag = nullptr,
414  MDNode *NoAliasTag = nullptr);
415 
416  /// Create and insert an element unordered-atomic memset of the region of
417  /// memory starting at the given pointer to the given value.
418  ///
419  /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
420  /// specified, it will be added to the instruction. Likewise with alias.scope
421  /// and noalias tags.
423  uint64_t Size, unsigned Align,
424  uint32_t ElementSize,
425  MDNode *TBAATag = nullptr,
426  MDNode *ScopeTag = nullptr,
427  MDNode *NoAliasTag = nullptr) {
428  return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), Align,
429  ElementSize, TBAATag, ScopeTag,
430  NoAliasTag);
431  }
432 
433  CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
434  Value *Size, unsigned Align,
435  uint32_t ElementSize,
436  MDNode *TBAATag = nullptr,
437  MDNode *ScopeTag = nullptr,
438  MDNode *NoAliasTag = nullptr);
439 
440  /// Create and insert a memcpy between the specified pointers.
441  ///
442  /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
443  /// specified, it will be added to the instruction. Likewise with alias.scope
444  /// and noalias tags.
445  CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
446  unsigned SrcAlign, uint64_t Size,
447  bool isVolatile = false, MDNode *TBAATag = nullptr,
448  MDNode *TBAAStructTag = nullptr,
449  MDNode *ScopeTag = nullptr,
450  MDNode *NoAliasTag = nullptr) {
451  return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
452  isVolatile, TBAATag, TBAAStructTag, ScopeTag,
453  NoAliasTag);
454  }
455 
456  CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
457  unsigned SrcAlign, Value *Size,
458  bool isVolatile = false, MDNode *TBAATag = nullptr,
459  MDNode *TBAAStructTag = nullptr,
460  MDNode *ScopeTag = nullptr,
461  MDNode *NoAliasTag = nullptr);
462 
463  /// Create and insert an element unordered-atomic memcpy between the
464  /// specified pointers.
465  ///
466  /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
467  ///
468  /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
469  /// specified, it will be added to the instruction. Likewise with alias.scope
470  /// and noalias tags.
472  Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
473  uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
474  MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
475  MDNode *NoAliasTag = nullptr) {
476  return CreateElementUnorderedAtomicMemCpy(
477  Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
478  TBAAStructTag, ScopeTag, NoAliasTag);
479  }
480 
481  CallInst *CreateElementUnorderedAtomicMemCpy(
482  Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
483  uint32_t ElementSize, MDNode *TBAATag = nullptr,
484  MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
485  MDNode *NoAliasTag = nullptr);
486 
487  /// Create and insert a memmove between the specified
488  /// pointers.
489  ///
490  /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
491  /// specified, it will be added to the instruction. Likewise with alias.scope
492  /// and noalias tags.
493  CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
494  uint64_t Size, bool isVolatile = false,
495  MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
496  MDNode *NoAliasTag = nullptr) {
497  return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), isVolatile,
498  TBAATag, ScopeTag, NoAliasTag);
499  }
500 
501  CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
502  Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr,
503  MDNode *ScopeTag = nullptr,
504  MDNode *NoAliasTag = nullptr);
505 
506  /// \brief Create and insert an element unordered-atomic memmove between the
507  /// specified pointers.
508  ///
509  /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
510  /// respectively.
511  ///
512  /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
513  /// specified, it will be added to the instruction. Likewise with alias.scope
514  /// and noalias tags.
516  Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
517  uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
518  MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
519  MDNode *NoAliasTag = nullptr) {
520  return CreateElementUnorderedAtomicMemMove(
521  Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
522  TBAAStructTag, ScopeTag, NoAliasTag);
523  }
524 
525  CallInst *CreateElementUnorderedAtomicMemMove(
526  Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
527  uint32_t ElementSize, MDNode *TBAATag = nullptr,
528  MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
529  MDNode *NoAliasTag = nullptr);
530 
531  /// Create a vector fadd reduction intrinsic of the source vector.
532  /// The first parameter is a scalar accumulator value for ordered reductions.
533  CallInst *CreateFAddReduce(Value *Acc, Value *Src);
534 
535  /// Create a vector fmul reduction intrinsic of the source vector.
536  /// The first parameter is a scalar accumulator value for ordered reductions.
537  CallInst *CreateFMulReduce(Value *Acc, Value *Src);
538 
539  /// Create a vector int add reduction intrinsic of the source vector.
540  CallInst *CreateAddReduce(Value *Src);
541 
542  /// Create a vector int mul reduction intrinsic of the source vector.
543  CallInst *CreateMulReduce(Value *Src);
544 
545  /// Create a vector int AND reduction intrinsic of the source vector.
546  CallInst *CreateAndReduce(Value *Src);
547 
548  /// Create a vector int OR reduction intrinsic of the source vector.
549  CallInst *CreateOrReduce(Value *Src);
550 
551  /// Create a vector int XOR reduction intrinsic of the source vector.
552  CallInst *CreateXorReduce(Value *Src);
553 
554  /// Create a vector integer max reduction intrinsic of the source
555  /// vector.
556  CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
557 
558  /// Create a vector integer min reduction intrinsic of the source
559  /// vector.
560  CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
561 
562  /// Create a vector float max reduction intrinsic of the source
563  /// vector.
564  CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
565 
566  /// Create a vector float min reduction intrinsic of the source
567  /// vector.
568  CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
569 
570  /// Create a lifetime.start intrinsic.
571  ///
572  /// If the pointer isn't i8* it will be converted.
573  CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
574 
575  /// Create a lifetime.end intrinsic.
576  ///
577  /// If the pointer isn't i8* it will be converted.
578  CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
579 
580  /// Create a call to invariant.start intrinsic.
581  ///
582  /// If the pointer isn't i8* it will be converted.
583  CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
584 
585  /// Create a call to Masked Load intrinsic
586  CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
587  Value *PassThru = nullptr, const Twine &Name = "");
588 
589  /// Create a call to Masked Store intrinsic
590  CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
591  Value *Mask);
592 
593  /// Create a call to Masked Gather intrinsic
594  CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align,
595  Value *Mask = nullptr,
596  Value *PassThru = nullptr,
597  const Twine& Name = "");
598 
599  /// Create a call to Masked Scatter intrinsic
600  CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align,
601  Value *Mask = nullptr);
602 
603  /// Create an assume intrinsic call that allows the optimizer to
604  /// assume that the provided condition will be true.
605  CallInst *CreateAssumption(Value *Cond);
606 
607  /// Create a call to the experimental.gc.statepoint intrinsic to
608  /// start a new statepoint sequence.
609  CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
610  Value *ActualCallee,
611  ArrayRef<Value *> CallArgs,
612  ArrayRef<Value *> DeoptArgs,
613  ArrayRef<Value *> GCArgs,
614  const Twine &Name = "");
615 
616  /// Create a call to the experimental.gc.statepoint intrinsic to
617  /// start a new statepoint sequence.
618  CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
619  Value *ActualCallee, uint32_t Flags,
620  ArrayRef<Use> CallArgs,
621  ArrayRef<Use> TransitionArgs,
622  ArrayRef<Use> DeoptArgs,
623  ArrayRef<Value *> GCArgs,
624  const Twine &Name = "");
625 
626  /// Conveninence function for the common case when CallArgs are filled
627  /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
628  /// .get()'ed to get the Value pointer.
629  CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
630  Value *ActualCallee, ArrayRef<Use> CallArgs,
631  ArrayRef<Value *> DeoptArgs,
632  ArrayRef<Value *> GCArgs,
633  const Twine &Name = "");
634 
635  /// Create an invoke to the experimental.gc.statepoint intrinsic to
636  /// start a new statepoint sequence.
637  InvokeInst *
638  CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
639  Value *ActualInvokee, BasicBlock *NormalDest,
640  BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
641  ArrayRef<Value *> DeoptArgs,
642  ArrayRef<Value *> GCArgs, const Twine &Name = "");
643 
644  /// Create an invoke to the experimental.gc.statepoint intrinsic to
645  /// start a new statepoint sequence.
646  InvokeInst *CreateGCStatepointInvoke(
647  uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
648  BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
649  ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
650  ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs,
651  const Twine &Name = "");
652 
653  // Convenience function for the common case when CallArgs are filled in using
654  // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
655  // get the Value *.
656  InvokeInst *
657  CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
658  Value *ActualInvokee, BasicBlock *NormalDest,
659  BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
660  ArrayRef<Value *> DeoptArgs,
661  ArrayRef<Value *> GCArgs, const Twine &Name = "");
662 
663  /// Create a call to the experimental.gc.result intrinsic to extract
664  /// the result from a call wrapped in a statepoint.
665  CallInst *CreateGCResult(Instruction *Statepoint,
666  Type *ResultType,
667  const Twine &Name = "");
668 
669  /// Create a call to the experimental.gc.relocate intrinsics to
670  /// project the relocated value of one pointer from the statepoint.
671  CallInst *CreateGCRelocate(Instruction *Statepoint,
672  int BaseOffset,
673  int DerivedOffset,
674  Type *ResultType,
675  const Twine &Name = "");
676 
677  /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
678  /// type.
679  CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
680  Instruction *FMFSource = nullptr,
681  const Twine &Name = "");
682 
683  /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
684  /// first type.
685  CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
686  Instruction *FMFSource = nullptr,
687  const Twine &Name = "");
688 
689  /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If
690  /// \p FMFSource is provided, copy fast-math-flags from that instruction to
691  /// the intrinsic.
692  CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
694  Instruction *FMFSource = nullptr,
695  const Twine &Name = "");
696 
697  /// Create call to the minnum intrinsic.
698  CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
699  return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
700  }
701 
702  /// Create call to the maxnum intrinsic.
703  CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
704  return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
705  }
706 
707  /// Create call to the minimum intrinsic.
708  CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
709  return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
710  }
711 
712  /// Create call to the maximum intrinsic.
713  CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
714  return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
715  }
716 
717 private:
718  /// Create a call to a masked intrinsic with given Id.
719  CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
720  ArrayRef<Type *> OverloadedTypes,
721  const Twine &Name = "");
722 
723  Value *getCastedInt8PtrValue(Value *Ptr);
724 };
725 
726 /// This provides a uniform API for creating instructions and inserting
727 /// them into a basic block: either at the end of a BasicBlock, or at a specific
728 /// iterator location in a block.
729 ///
730 /// Note that the builder does not expose the full generality of LLVM
731 /// instructions. For access to extra instruction properties, use the mutators
732 /// (e.g. setVolatile) on the instructions after they have been
733 /// created. Convenience state exists to specify fast-math flags and fp-math
734 /// tags.
735 ///
736 /// The first template argument specifies a class to use for creating constants.
737 /// This defaults to creating minimally folded constants. The second template
738 /// argument allows clients to specify custom insertion hooks that are called on
739 /// every newly created insertion.
740 template <typename T = ConstantFolder,
742 class IRBuilder : public IRBuilderBase, public Inserter {
743  T Folder;
744 
745 public:
746  IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
747  MDNode *FPMathTag = nullptr,
748  ArrayRef<OperandBundleDef> OpBundles = None)
749  : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)),
750  Folder(F) {}
751 
752  explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
753  ArrayRef<OperandBundleDef> OpBundles = None)
754  : IRBuilderBase(C, FPMathTag, OpBundles) {}
755 
756  explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr,
757  ArrayRef<OperandBundleDef> OpBundles = None)
758  : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
759  SetInsertPoint(TheBB);
760  }
761 
762  explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
763  ArrayRef<OperandBundleDef> OpBundles = None)
764  : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
765  SetInsertPoint(TheBB);
766  }
767 
768  explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
769  ArrayRef<OperandBundleDef> OpBundles = None)
770  : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles) {
771  SetInsertPoint(IP);
772  }
773 
775  MDNode *FPMathTag = nullptr,
776  ArrayRef<OperandBundleDef> OpBundles = None)
777  : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
778  SetInsertPoint(TheBB, IP);
779  }
780 
782  MDNode *FPMathTag = nullptr,
783  ArrayRef<OperandBundleDef> OpBundles = None)
784  : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
785  SetInsertPoint(TheBB, IP);
786  }
787 
788  /// Get the constant folder being used.
789  const T &getFolder() { return Folder; }
790 
791  /// Insert and return the specified instruction.
792  template<typename InstTy>
793  InstTy *Insert(InstTy *I, const Twine &Name = "") const {
794  this->InsertHelper(I, Name, BB, InsertPt);
795  this->SetInstDebugLocation(I);
796  return I;
797  }
798 
799  /// No-op overload to handle constants.
800  Constant *Insert(Constant *C, const Twine& = "") const {
801  return C;
802  }
803 
804  //===--------------------------------------------------------------------===//
805  // Instruction creation methods: Terminators
806  //===--------------------------------------------------------------------===//
807 
808 private:
809  /// Helper to add branch weight and unpredictable metadata onto an
810  /// instruction.
811  /// \returns The annotated instruction.
812  template <typename InstTy>
813  InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
814  if (Weights)
815  I->setMetadata(LLVMContext::MD_prof, Weights);
816  if (Unpredictable)
817  I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
818  return I;
819  }
820 
821 public:
822  /// Create a 'ret void' instruction.
824  return Insert(ReturnInst::Create(Context));
825  }
826 
827  /// Create a 'ret <val>' instruction.
829  return Insert(ReturnInst::Create(Context, V));
830  }
831 
832  /// Create a sequence of N insertvalue instructions,
833  /// with one Value from the retVals array each, that build a aggregate
834  /// return value one value at a time, and a ret instruction to return
835  /// the resulting aggregate value.
836  ///
837  /// This is a convenience function for code that uses aggregate return values
838  /// as a vehicle for having multiple return values.
839  ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
840  Value *V = UndefValue::get(getCurrentFunctionReturnType());
841  for (unsigned i = 0; i != N; ++i)
842  V = CreateInsertValue(V, retVals[i], i, "mrv");
843  return Insert(ReturnInst::Create(Context, V));
844  }
845 
846  /// Create an unconditional 'br label X' instruction.
848  return Insert(BranchInst::Create(Dest));
849  }
850 
851  /// Create a conditional 'br Cond, TrueDest, FalseDest'
852  /// instruction.
854  MDNode *BranchWeights = nullptr,
855  MDNode *Unpredictable = nullptr) {
856  return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
857  BranchWeights, Unpredictable));
858  }
859 
860  /// Create a conditional 'br Cond, TrueDest, FalseDest'
861  /// instruction. Copy branch meta data if available.
863  Instruction *MDSrc) {
864  BranchInst *Br = BranchInst::Create(True, False, Cond);
865  if (MDSrc) {
868  Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
869  }
870  return Insert(Br);
871  }
872 
873  /// Create a switch instruction with the specified value, default dest,
874  /// and with a hint for the number of cases that will be added (for efficient
875  /// allocation).
876  SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
877  MDNode *BranchWeights = nullptr,
878  MDNode *Unpredictable = nullptr) {
879  return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
880  BranchWeights, Unpredictable));
881  }
882 
883  /// Create an indirect branch instruction with the specified address
884  /// operand, with an optional hint for the number of destinations that will be
885  /// added (for efficient allocation).
886  IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
887  return Insert(IndirectBrInst::Create(Addr, NumDests));
888  }
889 
890  /// Create an invoke instruction.
892  BasicBlock *NormalDest, BasicBlock *UnwindDest,
894  ArrayRef<OperandBundleDef> OpBundles,
895  const Twine &Name = "") {
896  return Insert(
897  InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles),
898  Name);
899  }
901  BasicBlock *NormalDest, BasicBlock *UnwindDest,
903  const Twine &Name = "") {
904  return Insert(InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args),
905  Name);
906  }
907 
909  BasicBlock *UnwindDest, ArrayRef<Value *> Args,
910  ArrayRef<OperandBundleDef> OpBundles,
911  const Twine &Name = "") {
912  return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
913  NormalDest, UnwindDest, Args, OpBundles, Name);
914  }
915 
917  BasicBlock *UnwindDest,
919  const Twine &Name = "") {
920  return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
921  NormalDest, UnwindDest, Args, Name);
922  }
923 
924  // Deprecated [opaque pointer types]
925  InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
926  BasicBlock *UnwindDest, ArrayRef<Value *> Args,
927  ArrayRef<OperandBundleDef> OpBundles,
928  const Twine &Name = "") {
929  return CreateInvoke(
930  cast<FunctionType>(
931  cast<PointerType>(Callee->getType())->getElementType()),
932  Callee, NormalDest, UnwindDest, Args, OpBundles, Name);
933  }
934 
935  // Deprecated [opaque pointer types]
936  InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
937  BasicBlock *UnwindDest,
939  const Twine &Name = "") {
940  return CreateInvoke(
941  cast<FunctionType>(
942  cast<PointerType>(Callee->getType())->getElementType()),
943  Callee, NormalDest, UnwindDest, Args, Name);
944  }
945 
946  /// \brief Create a callbr instruction.
948  BasicBlock *DefaultDest,
949  ArrayRef<BasicBlock *> IndirectDests,
951  const Twine &Name = "") {
952  return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
953  Args), Name);
954  }
956  BasicBlock *DefaultDest,
957  ArrayRef<BasicBlock *> IndirectDests,
959  ArrayRef<OperandBundleDef> OpBundles,
960  const Twine &Name = "") {
961  return Insert(
962  CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
963  OpBundles), Name);
964  }
965 
967  ArrayRef<BasicBlock *> IndirectDests,
969  const Twine &Name = "") {
970  return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
971  DefaultDest, IndirectDests, Args, Name);
972  }
974  ArrayRef<BasicBlock *> IndirectDests,
976  ArrayRef<OperandBundleDef> OpBundles,
977  const Twine &Name = "") {
978  return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
979  DefaultDest, IndirectDests, Args, Name);
980  }
981 
983  return Insert(ResumeInst::Create(Exn));
984  }
985 
987  BasicBlock *UnwindBB = nullptr) {
988  return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
989  }
990 
992  unsigned NumHandlers,
993  const Twine &Name = "") {
994  return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
995  Name);
996  }
997 
999  const Twine &Name = "") {
1000  return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1001  }
1002 
1005  const Twine &Name = "") {
1006  return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1007  }
1008 
1010  return Insert(CatchReturnInst::Create(CatchPad, BB));
1011  }
1012 
1014  return Insert(new UnreachableInst(Context));
1015  }
1016 
1017  //===--------------------------------------------------------------------===//
1018  // Instruction creation methods: Binary Operators
1019  //===--------------------------------------------------------------------===//
1020 private:
1021  BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1022  Value *LHS, Value *RHS,
1023  const Twine &Name,
1024  bool HasNUW, bool HasNSW) {
1025  BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1026  if (HasNUW) BO->setHasNoUnsignedWrap();
1027  if (HasNSW) BO->setHasNoSignedWrap();
1028  return BO;
1029  }
1030 
1031  Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1032  FastMathFlags FMF) const {
1033  if (!FPMD)
1034  FPMD = DefaultFPMathTag;
1035  if (FPMD)
1037  I->setFastMathFlags(FMF);
1038  return I;
1039  }
1040 
1041  Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
1042  Value *R, const Twine &Name) const {
1043  auto *LC = dyn_cast<Constant>(L);
1044  auto *RC = dyn_cast<Constant>(R);
1045  return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
1046  }
1047 
1048 public:
1049  Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1050  bool HasNUW = false, bool HasNSW = false) {
1051  if (auto *LC = dyn_cast<Constant>(LHS))
1052  if (auto *RC = dyn_cast<Constant>(RHS))
1053  return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
1054  return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
1055  HasNUW, HasNSW);
1056  }
1057 
1058  Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1059  return CreateAdd(LHS, RHS, Name, false, true);
1060  }
1061 
1062  Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1063  return CreateAdd(LHS, RHS, Name, true, false);
1064  }
1065 
1066  Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1067  bool HasNUW = false, bool HasNSW = false) {
1068  if (auto *LC = dyn_cast<Constant>(LHS))
1069  if (auto *RC = dyn_cast<Constant>(RHS))
1070  return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
1071  return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
1072  HasNUW, HasNSW);
1073  }
1074 
1075  Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1076  return CreateSub(LHS, RHS, Name, false, true);
1077  }
1078 
1079  Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1080  return CreateSub(LHS, RHS, Name, true, false);
1081  }
1082 
1083  Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1084  bool HasNUW = false, bool HasNSW = false) {
1085  if (auto *LC = dyn_cast<Constant>(LHS))
1086  if (auto *RC = dyn_cast<Constant>(RHS))
1087  return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
1088  return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
1089  HasNUW, HasNSW);
1090  }
1091 
1092  Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1093  return CreateMul(LHS, RHS, Name, false, true);
1094  }
1095 
1096  Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1097  return CreateMul(LHS, RHS, Name, true, false);
1098  }
1099 
1100  Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1101  bool isExact = false) {
1102  if (auto *LC = dyn_cast<Constant>(LHS))
1103  if (auto *RC = dyn_cast<Constant>(RHS))
1104  return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
1105  if (!isExact)
1106  return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1107  return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1108  }
1109 
1110  Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1111  return CreateUDiv(LHS, RHS, Name, true);
1112  }
1113 
1114  Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1115  bool isExact = false) {
1116  if (auto *LC = dyn_cast<Constant>(LHS))
1117  if (auto *RC = dyn_cast<Constant>(RHS))
1118  return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
1119  if (!isExact)
1120  return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1121  return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1122  }
1123 
1124  Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1125  return CreateSDiv(LHS, RHS, Name, true);
1126  }
1127 
1128  Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1129  if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V;
1130  return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1131  }
1132 
1133  Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1134  if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V;
1135  return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1136  }
1137 
1138  Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1139  bool HasNUW = false, bool HasNSW = false) {
1140  if (auto *LC = dyn_cast<Constant>(LHS))
1141  if (auto *RC = dyn_cast<Constant>(RHS))
1142  return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
1143  return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1144  HasNUW, HasNSW);
1145  }
1146 
1147  Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1148  bool HasNUW = false, bool HasNSW = false) {
1149  return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1150  HasNUW, HasNSW);
1151  }
1152 
1153  Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1154  bool HasNUW = false, bool HasNSW = false) {
1155  return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1156  HasNUW, HasNSW);
1157  }
1158 
1159  Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1160  bool isExact = false) {
1161  if (auto *LC = dyn_cast<Constant>(LHS))
1162  if (auto *RC = dyn_cast<Constant>(RHS))
1163  return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
1164  if (!isExact)
1165  return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1166  return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1167  }
1168 
1169  Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1170  bool isExact = false) {
1171  return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1172  }
1173 
1174  Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1175  bool isExact = false) {
1176  return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1177  }
1178 
1179  Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1180  bool isExact = false) {
1181  if (auto *LC = dyn_cast<Constant>(LHS))
1182  if (auto *RC = dyn_cast<Constant>(RHS))
1183  return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
1184  if (!isExact)
1185  return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1186  return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1187  }
1188 
1189  Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1190  bool isExact = false) {
1191  return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1192  }
1193 
1194  Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1195  bool isExact = false) {
1196  return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1197  }
1198 
1199  Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1200  if (auto *RC = dyn_cast<Constant>(RHS)) {
1201  if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
1202  return LHS; // LHS & -1 -> LHS
1203  if (auto *LC = dyn_cast<Constant>(LHS))
1204  return Insert(Folder.CreateAnd(LC, RC), Name);
1205  }
1206  return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1207  }
1208 
1209  Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1210  return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1211  }
1212 
1213  Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1214  return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1215  }
1216 
1217  Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1218  if (auto *RC = dyn_cast<Constant>(RHS)) {
1219  if (RC->isNullValue())
1220  return LHS; // LHS | 0 -> LHS
1221  if (auto *LC = dyn_cast<Constant>(LHS))
1222  return Insert(Folder.CreateOr(LC, RC), Name);
1223  }
1224  return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1225  }
1226 
1227  Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1228  return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1229  }
1230 
1231  Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1232  return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1233  }
1234 
1235  Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1236  if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V;
1237  return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1238  }
1239 
1240  Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1241  return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1242  }
1243 
1244  Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1245  return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1246  }
1247 
1248  Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1249  MDNode *FPMD = nullptr) {
1250  if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1251  Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
1252  return Insert(I, Name);
1253  }
1254 
1255  /// Copy fast-math-flags from an instruction rather than using the builder's
1256  /// default FMF.
1258  const Twine &Name = "") {
1259  if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1260  Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr,
1261  FMFSource->getFastMathFlags());
1262  return Insert(I, Name);
1263  }
1264 
1265  Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1266  MDNode *FPMD = nullptr) {
1267  if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1268  Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
1269  return Insert(I, Name);
1270  }
1271 
1272  /// Copy fast-math-flags from an instruction rather than using the builder's
1273  /// default FMF.
1275  const Twine &Name = "") {
1276  if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1277  Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr,
1278  FMFSource->getFastMathFlags());
1279  return Insert(I, Name);
1280  }
1281 
1282  Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1283  MDNode *FPMD = nullptr) {
1284  if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1285  Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
1286  return Insert(I, Name);
1287  }
1288 
1289  /// Copy fast-math-flags from an instruction rather than using the builder's
1290  /// default FMF.
1292  const Twine &Name = "") {
1293  if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1294  Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr,
1295  FMFSource->getFastMathFlags());
1296  return Insert(I, Name);
1297  }
1298 
1299  Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1300  MDNode *FPMD = nullptr) {
1301  if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1302  Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
1303  return Insert(I, Name);
1304  }
1305 
1306  /// Copy fast-math-flags from an instruction rather than using the builder's
1307  /// default FMF.
1309  const Twine &Name = "") {
1310  if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1311  Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr,
1312  FMFSource->getFastMathFlags());
1313  return Insert(I, Name);
1314  }
1315 
1316  Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1317  MDNode *FPMD = nullptr) {
1318  if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1319  Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
1320  return Insert(I, Name);
1321  }
1322 
1323  /// Copy fast-math-flags from an instruction rather than using the builder's
1324  /// default FMF.
1326  const Twine &Name = "") {
1327  if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1328  Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr,
1329  FMFSource->getFastMathFlags());
1330  return Insert(I, Name);
1331  }
1332 
1334  Value *LHS, Value *RHS, const Twine &Name = "",
1335  MDNode *FPMathTag = nullptr) {
1336  if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V;
1337  Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1338  if (isa<FPMathOperator>(BinOp))
1339  BinOp = setFPAttrs(BinOp, FPMathTag, FMF);
1340  return Insert(BinOp, Name);
1341  }
1342 
1343  Value *CreateNeg(Value *V, const Twine &Name = "",
1344  bool HasNUW = false, bool HasNSW = false) {
1345  if (auto *VC = dyn_cast<Constant>(V))
1346  return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
1347  BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
1348  if (HasNUW) BO->setHasNoUnsignedWrap();
1349  if (HasNSW) BO->setHasNoSignedWrap();
1350  return BO;
1351  }
1352 
1353  Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1354  return CreateNeg(V, Name, false, true);
1355  }
1356 
1357  Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1358  return CreateNeg(V, Name, true, false);
1359  }
1360 
1361  Value *CreateFNeg(Value *V, const Twine &Name = "",
1362  MDNode *FPMathTag = nullptr) {
1363  if (auto *VC = dyn_cast<Constant>(V))
1364  return Insert(Folder.CreateFNeg(VC), Name);
1365  return Insert(setFPAttrs(BinaryOperator::CreateFNeg(V), FPMathTag, FMF),
1366  Name);
1367  }
1368 
1369  Value *CreateNot(Value *V, const Twine &Name = "") {
1370  if (auto *VC = dyn_cast<Constant>(V))
1371  return Insert(Folder.CreateNot(VC), Name);
1372  return Insert(BinaryOperator::CreateNot(V), Name);
1373  }
1374 
1375  //===--------------------------------------------------------------------===//
1376  // Instruction creation methods: Memory Instructions
1377  //===--------------------------------------------------------------------===//
1378 
1379  AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1380  Value *ArraySize = nullptr, const Twine &Name = "") {
1381  return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name);
1382  }
1383 
1384  AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1385  const Twine &Name = "") {
1386  const DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
1387  return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name);
1388  }
1389 
1390  /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1391  /// converting the string to 'bool' for the isVolatile parameter.
1392  LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1393  return Insert(new LoadInst(Ty, Ptr), Name);
1394  }
1395 
1396  LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1397  return Insert(new LoadInst(Ty, Ptr), Name);
1398  }
1399 
1401  const Twine &Name = "") {
1402  return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile), Name);
1403  }
1404 
1405  // Deprecated [opaque pointer types]
1406  LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1407  return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1408  }
1409 
1410  // Deprecated [opaque pointer types]
1411  LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1412  return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1413  }
1414 
1415  // Deprecated [opaque pointer types]
1416  LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1417  return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, isVolatile,
1418  Name);
1419  }
1420 
1421  StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1422  return Insert(new StoreInst(Val, Ptr, isVolatile));
1423  }
1424 
1425  /// Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
1426  /// correctly, instead of converting the string to 'bool' for the isVolatile
1427  /// parameter.
1429  const char *Name) {
1430  LoadInst *LI = CreateLoad(Ty, Ptr, Name);
1431  LI->setAlignment(Align);
1432  return LI;
1433  }
1435  const Twine &Name = "") {
1436  LoadInst *LI = CreateLoad(Ty, Ptr, Name);
1437  LI->setAlignment(Align);
1438  return LI;
1439  }
1441  bool isVolatile, const Twine &Name = "") {
1442  LoadInst *LI = CreateLoad(Ty, Ptr, isVolatile, Name);
1443  LI->setAlignment(Align);
1444  return LI;
1445  }
1446 
1447  // Deprecated [opaque pointer types]
1448  LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
1449  return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1450  Align, Name);
1451  }
1452  // Deprecated [opaque pointer types]
1454  const Twine &Name = "") {
1455  return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1456  Align, Name);
1457  }
1458  // Deprecated [opaque pointer types]
1460  const Twine &Name = "") {
1461  return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1462  Align, isVolatile, Name);
1463  }
1464 
1466  bool isVolatile = false) {
1467  StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
1468  SI->setAlignment(Align);
1469  return SI;
1470  }
1471 
1474  const Twine &Name = "") {
1475  return Insert(new FenceInst(Context, Ordering, SSID), Name);
1476  }
1477 
1480  AtomicOrdering SuccessOrdering,
1481  AtomicOrdering FailureOrdering,
1483  return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1484  FailureOrdering, SSID));
1485  }
1486 
1488  AtomicOrdering Ordering,
1490  return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SSID));
1491  }
1492 
1494  const Twine &Name = "") {
1495  return CreateGEP(nullptr, Ptr, IdxList, Name);
1496  }
1497 
1499  const Twine &Name = "") {
1500  if (auto *PC = dyn_cast<Constant>(Ptr)) {
1501  // Every index must be constant.
1502  size_t i, e;
1503  for (i = 0, e = IdxList.size(); i != e; ++i)
1504  if (!isa<Constant>(IdxList[i]))
1505  break;
1506  if (i == e)
1507  return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1508  }
1509  return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1510  }
1511 
1513  const Twine &Name = "") {
1514  return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1515  }
1516 
1518  const Twine &Name = "") {
1519  if (auto *PC = dyn_cast<Constant>(Ptr)) {
1520  // Every index must be constant.
1521  size_t i, e;
1522  for (i = 0, e = IdxList.size(); i != e; ++i)
1523  if (!isa<Constant>(IdxList[i]))
1524  break;
1525  if (i == e)
1526  return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1527  Name);
1528  }
1529  return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1530  }
1531 
1532  Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1533  return CreateGEP(nullptr, Ptr, Idx, Name);
1534  }
1535 
1536  Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1537  if (auto *PC = dyn_cast<Constant>(Ptr))
1538  if (auto *IC = dyn_cast<Constant>(Idx))
1539  return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1540  return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1541  }
1542 
1544  const Twine &Name = "") {
1545  if (auto *PC = dyn_cast<Constant>(Ptr))
1546  if (auto *IC = dyn_cast<Constant>(Idx))
1547  return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1548  return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1549  }
1550 
1551  Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1552  return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1553  }
1554 
1555  Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1556  const Twine &Name = "") {
1558 
1559  if (auto *PC = dyn_cast<Constant>(Ptr))
1560  return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1561 
1562  return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1563  }
1564 
1565  Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1566  const Twine &Name = "") {
1568 
1569  if (auto *PC = dyn_cast<Constant>(Ptr))
1570  return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1571 
1572  return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1573  }
1574 
1575  Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1576  const Twine &Name = "") {
1577  Value *Idxs[] = {
1580  };
1581 
1582  if (auto *PC = dyn_cast<Constant>(Ptr))
1583  return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1584 
1585  return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1586  }
1587 
1588  Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1589  unsigned Idx1, const Twine &Name = "") {
1590  Value *Idxs[] = {
1593  };
1594 
1595  if (auto *PC = dyn_cast<Constant>(Ptr))
1596  return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1597 
1598  return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1599  }
1600 
1601  Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1602  const Twine &Name = "") {
1604 
1605  if (auto *PC = dyn_cast<Constant>(Ptr))
1606  return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1607 
1608  return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1609  }
1610 
1611  Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1612  return CreateConstGEP1_64(nullptr, Ptr, Idx0, Name);
1613  }
1614 
1615  Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1616  const Twine &Name = "") {
1618 
1619  if (auto *PC = dyn_cast<Constant>(Ptr))
1620  return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1621 
1622  return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1623  }
1624 
1626  const Twine &Name = "") {
1627  return CreateConstInBoundsGEP1_64(nullptr, Ptr, Idx0, Name);
1628  }
1629 
1630  Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1631  const Twine &Name = "") {
1632  Value *Idxs[] = {
1635  };
1636 
1637  if (auto *PC = dyn_cast<Constant>(Ptr))
1638  return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1639 
1640  return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1641  }
1642 
1643  Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1644  const Twine &Name = "") {
1645  return CreateConstGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
1646  }
1647 
1648  Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1649  uint64_t Idx1, const Twine &Name = "") {
1650  Value *Idxs[] = {
1653  };
1654 
1655  if (auto *PC = dyn_cast<Constant>(Ptr))
1656  return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1657 
1658  return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1659  }
1660 
1661  Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1662  const Twine &Name = "") {
1663  return CreateConstInBoundsGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
1664  }
1665 
1666  Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1667  const Twine &Name = "") {
1668  return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1669  }
1670 
1671  Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1672  return CreateConstInBoundsGEP2_32(nullptr, Ptr, 0, Idx, Name);
1673  }
1674 
1675  /// Same as CreateGlobalString, but return a pointer with "i8*" type
1676  /// instead of a pointer to array of i8.
1678  unsigned AddressSpace = 0) {
1679  GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace);
1681  Constant *Indices[] = {Zero, Zero};
1683  Indices);
1684  }
1685 
1686  //===--------------------------------------------------------------------===//
1687  // Instruction creation methods: Cast/Conversion Operators
1688  //===--------------------------------------------------------------------===//
1689 
1690  Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1691  return CreateCast(Instruction::Trunc, V, DestTy, Name);
1692  }
1693 
1694  Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1695  return CreateCast(Instruction::ZExt, V, DestTy, Name);
1696  }
1697 
1698  Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1699  return CreateCast(Instruction::SExt, V, DestTy, Name);
1700  }
1701 
1702  /// Create a ZExt or Trunc from the integer value V to DestTy. Return
1703  /// the value untouched if the type of V is already DestTy.
1705  const Twine &Name = "") {
1706  assert(V->getType()->isIntOrIntVectorTy() &&
1707  DestTy->isIntOrIntVectorTy() &&
1708  "Can only zero extend/truncate integers!");
1709  Type *VTy = V->getType();
1710  if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1711  return CreateZExt(V, DestTy, Name);
1712  if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1713  return CreateTrunc(V, DestTy, Name);
1714  return V;
1715  }
1716 
1717  /// Create a SExt or Trunc from the integer value V to DestTy. Return
1718  /// the value untouched if the type of V is already DestTy.
1720  const Twine &Name = "") {
1721  assert(V->getType()->isIntOrIntVectorTy() &&
1722  DestTy->isIntOrIntVectorTy() &&
1723  "Can only sign extend/truncate integers!");
1724  Type *VTy = V->getType();
1725  if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1726  return CreateSExt(V, DestTy, Name);
1727  if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1728  return CreateTrunc(V, DestTy, Name);
1729  return V;
1730  }
1731 
1732  Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1733  return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1734  }
1735 
1736  Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1737  return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1738  }
1739 
1740  Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1741  return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1742  }
1743 
1744  Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1745  return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1746  }
1747 
1749  const Twine &Name = "") {
1750  return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1751  }
1752 
1753  Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1754  return CreateCast(Instruction::FPExt, V, DestTy, Name);
1755  }
1756 
1758  const Twine &Name = "") {
1759  return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1760  }
1761 
1763  const Twine &Name = "") {
1764  return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1765  }
1766 
1768  const Twine &Name = "") {
1769  return CreateCast(Instruction::BitCast, V, DestTy, Name);
1770  }
1771 
1773  const Twine &Name = "") {
1774  return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1775  }
1776 
1778  const Twine &Name = "") {
1779  if (V->getType() == DestTy)
1780  return V;
1781  if (auto *VC = dyn_cast<Constant>(V))
1782  return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1783  return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1784  }
1785 
1787  const Twine &Name = "") {
1788  if (V->getType() == DestTy)
1789  return V;
1790  if (auto *VC = dyn_cast<Constant>(V))
1791  return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1792  return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1793  }
1794 
1796  const Twine &Name = "") {
1797  if (V->getType() == DestTy)
1798  return V;
1799  if (auto *VC = dyn_cast<Constant>(V))
1800  return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1801  return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1802  }
1803 
1805  const Twine &Name = "") {
1806  if (V->getType() == DestTy)
1807  return V;
1808  if (auto *VC = dyn_cast<Constant>(V))
1809  return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1810  return Insert(CastInst::Create(Op, V, DestTy), Name);
1811  }
1812 
1814  const Twine &Name = "") {
1815  if (V->getType() == DestTy)
1816  return V;
1817  if (auto *VC = dyn_cast<Constant>(V))
1818  return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1819  return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1820  }
1821 
1823  const Twine &Name = "") {
1824  if (V->getType() == DestTy)
1825  return V;
1826 
1827  if (auto *VC = dyn_cast<Constant>(V)) {
1828  return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1829  Name);
1830  }
1831 
1832  return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1833  Name);
1834  }
1835 
1836  Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1837  const Twine &Name = "") {
1838  if (V->getType() == DestTy)
1839  return V;
1840  if (auto *VC = dyn_cast<Constant>(V))
1841  return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1842  return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1843  }
1844 
1846  const Twine &Name = "") {
1847  if (V->getType() == DestTy)
1848  return V;
1849  if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
1850  return CreatePtrToInt(V, DestTy, Name);
1851  if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
1852  return CreateIntToPtr(V, DestTy, Name);
1853 
1854  return CreateBitCast(V, DestTy, Name);
1855  }
1856 
1857  Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1858  if (V->getType() == DestTy)
1859  return V;
1860  if (auto *VC = dyn_cast<Constant>(V))
1861  return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1862  return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1863  }
1864 
1865  // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1866  // compile time error, instead of converting the string to bool for the
1867  // isSigned parameter.
1868  Value *CreateIntCast(Value *, Type *, const char *) = delete;
1869 
1870  //===--------------------------------------------------------------------===//
1871  // Instruction creation methods: Compare Instructions
1872  //===--------------------------------------------------------------------===//
1873 
1874  Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1875  return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1876  }
1877 
1878  Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1879  return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1880  }
1881 
1882  Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1883  return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1884  }
1885 
1886  Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1887  return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1888  }
1889 
1890  Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1891  return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1892  }
1893 
1894  Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1895  return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1896  }
1897 
1898  Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1899  return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1900  }
1901 
1902  Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1903  return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1904  }
1905 
1906  Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1907  return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1908  }
1909 
1910  Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1911  return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1912  }
1913 
1914  Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1915  MDNode *FPMathTag = nullptr) {
1916  return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
1917  }
1918 
1919  Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
1920  MDNode *FPMathTag = nullptr) {
1921  return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
1922  }
1923 
1924  Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
1925  MDNode *FPMathTag = nullptr) {
1926  return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
1927  }
1928 
1929  Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
1930  MDNode *FPMathTag = nullptr) {
1931  return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
1932  }
1933 
1934  Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
1935  MDNode *FPMathTag = nullptr) {
1936  return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
1937  }
1938 
1939  Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
1940  MDNode *FPMathTag = nullptr) {
1941  return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
1942  }
1943 
1944  Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
1945  MDNode *FPMathTag = nullptr) {
1946  return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
1947  }
1948 
1949  Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
1950  MDNode *FPMathTag = nullptr) {
1951  return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
1952  }
1953 
1954  Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1955  MDNode *FPMathTag = nullptr) {
1956  return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
1957  }
1958 
1959  Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
1960  MDNode *FPMathTag = nullptr) {
1961  return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
1962  }
1963 
1964  Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
1965  MDNode *FPMathTag = nullptr) {
1966  return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
1967  }
1968 
1969  Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
1970  MDNode *FPMathTag = nullptr) {
1971  return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
1972  }
1973 
1974  Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
1975  MDNode *FPMathTag = nullptr) {
1976  return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
1977  }
1978 
1979  Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
1980  MDNode *FPMathTag = nullptr) {
1981  return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
1982  }
1983 
1985  const Twine &Name = "") {
1986  if (auto *LC = dyn_cast<Constant>(LHS))
1987  if (auto *RC = dyn_cast<Constant>(RHS))
1988  return Insert(Folder.CreateICmp(P, LC, RC), Name);
1989  return Insert(new ICmpInst(P, LHS, RHS), Name);
1990  }
1991 
1993  const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1994  if (auto *LC = dyn_cast<Constant>(LHS))
1995  if (auto *RC = dyn_cast<Constant>(RHS))
1996  return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1997  return Insert(setFPAttrs(new FCmpInst(P, LHS, RHS), FPMathTag, FMF), Name);
1998  }
1999 
2000  //===--------------------------------------------------------------------===//
2001  // Instruction creation methods: Other Instructions
2002  //===--------------------------------------------------------------------===//
2003 
2004  PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2005  const Twine &Name = "") {
2006  return Insert(PHINode::Create(Ty, NumReservedValues), Name);
2007  }
2008 
2010  ArrayRef<Value *> Args = None, const Twine &Name = "",
2011  MDNode *FPMathTag = nullptr) {
2012  CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2013  if (isa<FPMathOperator>(CI))
2014  CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
2015  return Insert(CI, Name);
2016  }
2017 
2019  ArrayRef<OperandBundleDef> OpBundles,
2020  const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2021  CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
2022  if (isa<FPMathOperator>(CI))
2023  CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
2024  return Insert(CI, Name);
2025  }
2026 
2028  const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2029  return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
2030  FPMathTag);
2031  }
2032 
2034  ArrayRef<OperandBundleDef> OpBundles,
2035  const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2036  return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2037  OpBundles, Name, FPMathTag);
2038  }
2039 
2040  // Deprecated [opaque pointer types]
2042  const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2043  return CreateCall(
2044  cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee,
2045  Args, Name, FPMathTag);
2046  }
2047 
2048  // Deprecated [opaque pointer types]
2050  ArrayRef<OperandBundleDef> OpBundles,
2051  const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2052  return CreateCall(
2053  cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee,
2054  Args, OpBundles, Name, FPMathTag);
2055  }
2056 
2057  Value *CreateSelect(Value *C, Value *True, Value *False,
2058  const Twine &Name = "", Instruction *MDFrom = nullptr) {
2059  if (auto *CC = dyn_cast<Constant>(C))
2060  if (auto *TC = dyn_cast<Constant>(True))
2061  if (auto *FC = dyn_cast<Constant>(False))
2062  return Insert(Folder.CreateSelect(CC, TC, FC), Name);
2063 
2064  SelectInst *Sel = SelectInst::Create(C, True, False);
2065  if (MDFrom) {
2066  MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof);
2067  MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable);
2068  Sel = addBranchMetadata(Sel, Prof, Unpred);
2069  }
2070  return Insert(Sel, Name);
2071  }
2072 
2073  VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2074  return Insert(new VAArgInst(List, Ty), Name);
2075  }
2076 
2078  const Twine &Name = "") {
2079  if (auto *VC = dyn_cast<Constant>(Vec))
2080  if (auto *IC = dyn_cast<Constant>(Idx))
2081  return Insert(Folder.CreateExtractElement(VC, IC), Name);
2082  return Insert(ExtractElementInst::Create(Vec, Idx), Name);
2083  }
2084 
2085  Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2086  const Twine &Name = "") {
2087  return CreateExtractElement(Vec, getInt64(Idx), Name);
2088  }
2089 
2091  const Twine &Name = "") {
2092  if (auto *VC = dyn_cast<Constant>(Vec))
2093  if (auto *NC = dyn_cast<Constant>(NewElt))
2094  if (auto *IC = dyn_cast<Constant>(Idx))
2095  return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
2096  return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
2097  }
2098 
2099  Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2100  const Twine &Name = "") {
2101  return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
2102  }
2103 
2105  const Twine &Name = "") {
2106  if (auto *V1C = dyn_cast<Constant>(V1))
2107  if (auto *V2C = dyn_cast<Constant>(V2))
2108  if (auto *MC = dyn_cast<Constant>(Mask))
2109  return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
2110  return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
2111  }
2112 
2114  const Twine &Name = "") {
2116  return CreateShuffleVector(V1, V2, Mask, Name);
2117  }
2118 
2120  ArrayRef<unsigned> Idxs,
2121  const Twine &Name = "") {
2122  if (auto *AggC = dyn_cast<Constant>(Agg))
2123  return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
2124  return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
2125  }
2126 
2128  ArrayRef<unsigned> Idxs,
2129  const Twine &Name = "") {
2130  if (auto *AggC = dyn_cast<Constant>(Agg))
2131  if (auto *ValC = dyn_cast<Constant>(Val))
2132  return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
2133  return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
2134  }
2135 
2136  LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2137  const Twine &Name = "") {
2138  return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
2139  }
2140 
2141  //===--------------------------------------------------------------------===//
2142  // Utility creation methods
2143  //===--------------------------------------------------------------------===//
2144 
2145  /// Return an i1 value testing if \p Arg is null.
2146  Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2147  return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
2148  Name);
2149  }
2150 
2151  /// Return an i1 value testing if \p Arg is not null.
2152  Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2153  return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
2154  Name);
2155  }
2156 
2157  /// Return the i64 difference between two pointer values, dividing out
2158  /// the size of the pointed-to objects.
2159  ///
2160  /// This is intended to implement C-style pointer subtraction. As such, the
2161  /// pointers must be appropriately aligned for their element types and
2162  /// pointing into the same object.
2163  Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
2164  assert(LHS->getType() == RHS->getType() &&
2165  "Pointer subtraction operand types must match!");
2166  auto *ArgType = cast<PointerType>(LHS->getType());
2167  Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
2168  Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
2169  Value *Difference = CreateSub(LHS_int, RHS_int);
2170  return CreateExactSDiv(Difference,
2171  ConstantExpr::getSizeOf(ArgType->getElementType()),
2172  Name);
2173  }
2174 
2175  /// Create a launder.invariant.group intrinsic call. If Ptr type is
2176  /// different from pointer to i8, it's casted to pointer to i8 in the same
2177  /// address space before call and casted back to Ptr type after call.
2179  assert(isa<PointerType>(Ptr->getType()) &&
2180  "launder.invariant.group only applies to pointers.");
2181  // FIXME: we could potentially avoid casts to/from i8*.
2182  auto *PtrType = Ptr->getType();
2183  auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2184  if (PtrType != Int8PtrTy)
2185  Ptr = CreateBitCast(Ptr, Int8PtrTy);
2186  Module *M = BB->getParent()->getParent();
2187  Function *FnLaunderInvariantGroup = Intrinsic::getDeclaration(
2188  M, Intrinsic::launder_invariant_group, {Int8PtrTy});
2189 
2190  assert(FnLaunderInvariantGroup->getReturnType() == Int8PtrTy &&
2191  FnLaunderInvariantGroup->getFunctionType()->getParamType(0) ==
2192  Int8PtrTy &&
2193  "LaunderInvariantGroup should take and return the same type");
2194 
2195  CallInst *Fn = CreateCall(FnLaunderInvariantGroup, {Ptr});
2196 
2197  if (PtrType != Int8PtrTy)
2198  return CreateBitCast(Fn, PtrType);
2199  return Fn;
2200  }
2201 
2202  /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2203  /// different from pointer to i8, it's casted to pointer to i8 in the same
2204  /// address space before call and casted back to Ptr type after call.
2206  assert(isa<PointerType>(Ptr->getType()) &&
2207  "strip.invariant.group only applies to pointers.");
2208 
2209  // FIXME: we could potentially avoid casts to/from i8*.
2210  auto *PtrType = Ptr->getType();
2211  auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2212  if (PtrType != Int8PtrTy)
2213  Ptr = CreateBitCast(Ptr, Int8PtrTy);
2214  Module *M = BB->getParent()->getParent();
2215  Function *FnStripInvariantGroup = Intrinsic::getDeclaration(
2216  M, Intrinsic::strip_invariant_group, {Int8PtrTy});
2217 
2218  assert(FnStripInvariantGroup->getReturnType() == Int8PtrTy &&
2219  FnStripInvariantGroup->getFunctionType()->getParamType(0) ==
2220  Int8PtrTy &&
2221  "StripInvariantGroup should take and return the same type");
2222 
2223  CallInst *Fn = CreateCall(FnStripInvariantGroup, {Ptr});
2224 
2225  if (PtrType != Int8PtrTy)
2226  return CreateBitCast(Fn, PtrType);
2227  return Fn;
2228  }
2229 
2230  /// Return a vector value that contains \arg V broadcasted to \p
2231  /// NumElts elements.
2232  Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
2233  assert(NumElts > 0 && "Cannot splat to an empty vector!");
2234 
2235  // First insert it into an undef vector so we can shuffle it.
2236  Type *I32Ty = getInt32Ty();
2237  Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
2238  V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
2239  Name + ".splatinsert");
2240 
2241  // Shuffle the value across the desired number of elements.
2242  Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
2243  return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
2244  }
2245 
2246  /// Return a value that has been extracted from a larger integer type.
2248  IntegerType *ExtractedTy, uint64_t Offset,
2249  const Twine &Name) {
2250  auto *IntTy = cast<IntegerType>(From->getType());
2251  assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
2252  DL.getTypeStoreSize(IntTy) &&
2253  "Element extends past full value");
2254  uint64_t ShAmt = 8 * Offset;
2255  Value *V = From;
2256  if (DL.isBigEndian())
2257  ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
2258  DL.getTypeStoreSize(ExtractedTy) - Offset);
2259  if (ShAmt) {
2260  V = CreateLShr(V, ShAmt, Name + ".shift");
2261  }
2262  assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
2263  "Cannot extract to a larger integer!");
2264  if (ExtractedTy != IntTy) {
2265  V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
2266  }
2267  return V;
2268  }
2269 
2270 private:
2271  /// Helper function that creates an assume intrinsic call that
2272  /// represents an alignment assumption on the provided Ptr, Mask, Type
2273  /// and Offset. It may be sometimes useful to do some other logic
2274  /// based on this alignment check, thus it can be stored into 'TheCheck'.
2275  CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2276  Value *PtrValue, Value *Mask,
2277  Type *IntPtrTy, Value *OffsetValue,
2278  Value **TheCheck) {
2279  Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
2280 
2281  if (OffsetValue) {
2282  bool IsOffsetZero = false;
2283  if (const auto *CI = dyn_cast<ConstantInt>(OffsetValue))
2284  IsOffsetZero = CI->isZero();
2285 
2286  if (!IsOffsetZero) {
2287  if (OffsetValue->getType() != IntPtrTy)
2288  OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
2289  "offsetcast");
2290  PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
2291  }
2292  }
2293 
2294  Value *Zero = ConstantInt::get(IntPtrTy, 0);
2295  Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
2296  Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
2297  if (TheCheck)
2298  *TheCheck = InvCond;
2299 
2300  return CreateAssumption(InvCond);
2301  }
2302 
2303 public:
2304  /// Create an assume intrinsic call that represents an alignment
2305  /// assumption on the provided pointer.
2306  ///
2307  /// An optional offset can be provided, and if it is provided, the offset
2308  /// must be subtracted from the provided pointer to get the pointer with the
2309  /// specified alignment.
2310  ///
2311  /// It may be sometimes useful to do some other logic
2312  /// based on this alignment check, thus it can be stored into 'TheCheck'.
2314  unsigned Alignment,
2315  Value *OffsetValue = nullptr,
2316  Value **TheCheck = nullptr) {
2317  assert(isa<PointerType>(PtrValue->getType()) &&
2318  "trying to create an alignment assumption on a non-pointer?");
2319  assert(Alignment != 0 && "Invalid Alignment");
2320  auto *PtrTy = cast<PointerType>(PtrValue->getType());
2321  Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2322 
2323  Value *Mask = ConstantInt::get(IntPtrTy, Alignment - 1);
2324  return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2325  OffsetValue, TheCheck);
2326  }
2327 
2328  /// Create an assume intrinsic call that represents an alignment
2329  /// assumption on the provided pointer.
2330  ///
2331  /// An optional offset can be provided, and if it is provided, the offset
2332  /// must be subtracted from the provided pointer to get the pointer with the
2333  /// specified alignment.
2334  ///
2335  /// It may be sometimes useful to do some other logic
2336  /// based on this alignment check, thus it can be stored into 'TheCheck'.
2337  ///
2338  /// This overload handles the condition where the Alignment is dependent
2339  /// on an existing value rather than a static value.
2341  Value *Alignment,
2342  Value *OffsetValue = nullptr,
2343  Value **TheCheck = nullptr) {
2344  assert(isa<PointerType>(PtrValue->getType()) &&
2345  "trying to create an alignment assumption on a non-pointer?");
2346  auto *PtrTy = cast<PointerType>(PtrValue->getType());
2347  Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2348 
2349  if (Alignment->getType() != IntPtrTy)
2350  Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ false,
2351  "alignmentcast");
2352 
2353  Value *Mask = CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "mask");
2354 
2355  return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2356  OffsetValue, TheCheck);
2357  }
2358 };
2359 
2360 // Create wrappers for C Binding types (see CBindingWrapping.h).
2362 
2363 } // end namespace llvm
2364 
2365 #endif // LLVM_IR_IRBUILDER_H
IntegerType * getInt16Ty()
Fetch the type representing a 16-bit integer.
Definition: IRBuilder.h:341
Value * CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx, const Twine &Name="")
Definition: IRBuilder.h:2099
Value * CreateNSWNeg(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1353
Value * CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, const Twine &Name="")
Definition: IRBuilder.h:1565
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &Name="")
Definition: IRBuilder.h:1512
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:88
uint64_t CallInst * C
Return a value (possibly void), from a function.
Value * CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1189
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1543
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:584
Value * CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, const Twine &Name="")
Definition: IRBuilder.h:1601
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional &#39;br Cond, TrueDest, FalseDest&#39; instruction.
Definition: IRBuilder.h:853
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1984
Value * CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name="")
Definition: IRBuilder.h:1551
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
static Type * getDoubleTy(LLVMContext &C)
Definition: Type.cpp:164
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:172
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F, MDNode *FPMathTag=nullptr, ArrayRef< OperandBundleDef > OpBundles=None)
Definition: IRBuilder.h:774
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1333
Value * CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, const Twine &Name="")
Definition: IRBuilder.h:1555
FastMathFlags getFastMathFlags() const
Get the flags to be applied to created floating point ops.
Definition: IRBuilder.h:210
LLVMContext & Context
LLVMContext & getContext() const
Definition: IRBuilder.h:122
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1379
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1878
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return an i1 value testing if Arg is not null.
Definition: IRBuilder.h:2152
Atomic ordering constants.
Value * CreateAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1772
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:1704
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Value * CreateFPTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1748
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:134
CallBrInst * CreateCallBr(FunctionType *Ty, Value *Callee, BasicBlock *DefaultDest, ArrayRef< BasicBlock *> IndirectDests, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="")
Definition: IRBuilder.h:955
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1890
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve &#39;CreateLoad(Ty, Ptr, "...")&#39; correctly, instead of converting the string to &#39;bool...
Definition: IRBuilder.h:1392
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1235
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align, const char *Name)
Provided to resolve &#39;CreateAlignedLoad(Ptr, Align, "...")&#39; correctly, instead of converting the strin...
Definition: IRBuilder.h:1428
static ResumeInst * Create(Value *Exn, Instruction *InsertBefore=nullptr)
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2049
An instruction for ordering other memory operations.
Definition: Instructions.h:454
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Definition: Instructions.h:528
CallInst * CreateElementUnorderedAtomicMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert an element unordered-atomic memmove between the specified pointers.
Definition: IRBuilder.h:515
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:164
FastMathFlags FMF
Definition: IRBuilder.h:97
Value * CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1934
bool isSet() const
Returns true if this insert point is set.
Definition: IRBuilder.h:180
LoadInst * CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name="")
Definition: IRBuilder.h:1416
Value * CreateShl(Value *LHS, uint64_t RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1153
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1331
Value * CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1062
Value * CreateExtractInteger(const DataLayout &DL, Value *From, IntegerType *ExtractedTy, uint64_t Offset, const Twine &Name)
Return a value that has been extracted from a larger integer type.
Definition: IRBuilder.h:2247
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:899
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1906
This class represents a function call, abstracting a target machine&#39;s calling convention.
void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP)
This specifies that created instructions should be inserted at the specified point.
Definition: IRBuilder.h:142
Value * CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, const Twine &Name="")
Definition: IRBuilder.h:1575
unsigned less or equal
Definition: InstrTypes.h:672
unsigned less than
Definition: InstrTypes.h:671
IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag=nullptr, ArrayRef< OperandBundleDef > OpBundles=None)
Definition: IRBuilder.h:762
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:652
This instruction constructs a fixed permutation of two input vectors.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:662
Value * CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource, const Twine &Name="")
Copy fast-math-flags from an instruction rather than using the builder&#39;s default FMF.
Definition: IRBuilder.h:1325
struct LLVMOpaqueBuilder * LLVMBuilderRef
Represents an LLVM basic block builder.
Definition: Types.h:111
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1698
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:863
F(f)
Value * CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1964
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:403
An instruction for reading from memory.
Definition: Instructions.h:167
Value * CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1949
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:176
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:691
CallInst * CreateCall(FunctionCallee Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2027
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 maximum semantics.
Definition: APFloat.h:1261
Value * CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1894
Value * CreateShuffleVector(Value *V1, Value *V2, ArrayRef< uint32_t > IntMask, const Twine &Name="")
Definition: IRBuilder.h:2113
static IntegerType * getInt16Ty(LLVMContext &C)
Definition: Type.cpp:174
static CallBrInst * Create(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock *> IndirectDests, ArrayRef< Value *> Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
static LandingPadInst * Create(Type *RetTy, unsigned NumReservedClauses, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedClauses is a hint for the number of incoming clauses that this landingpad w...
Value * CreateLaunderInvariantGroup(Value *Ptr)
Create a launder.invariant.group intrinsic call.
Definition: IRBuilder.h:2178
Value * CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1058
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:264
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile=false)
Definition: IRBuilder.h:1465
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1369
LLVMContext & Context
Definition: IRBuilder.h:94
Value * CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1959
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, Instruction *InsertBefore=nullptr)
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:346
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:657
Value * CreateOr(Value *LHS, uint64_t RHS, const Twine &Name="")
Definition: IRBuilder.h:1231
AllocaInst * CreateAlloca(Type *Ty, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1384
unsigned getAllocaAddrSpace() const
Definition: DataLayout.h:257
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:656
InvokeInst * CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="")
Definition: IRBuilder.h:908
Definition: BitVector.h:937
Value * CreateFRem(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1316
Value * CreateFPExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1753
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
This class represents the LLVM &#39;select&#39; instruction.
Type * getPointerElementType() const
Definition: Type.h:375
Value * CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name="")
Return the i64 difference between two pointer values, dividing out the size of the pointed-to objects...
Definition: IRBuilder.h:2163
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
Value * CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1194
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Definition: IRBuilder.h:351
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:163
LoadInst * CreateLoad(Value *Ptr, const Twine &Name="")
Definition: IRBuilder.h:1411
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
CleanupPadInst * CreateCleanupPad(Value *ParentPad, ArrayRef< Value *> Args=None, const Twine &Name="")
Definition: IRBuilder.h:1003
Value * CreateSExtOrBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1786
ReturnInst * CreateRet(Value *V)
Create a &#39;ret <val>&#39; instruction.
Definition: IRBuilder.h:828
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:742
IntegerType * getIntPtrTy(const DataLayout &DL, unsigned AddrSpace=0)
Fetch the type representing a pointer to an integer value.
Definition: IRBuilder.h:389
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:653
Value * CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource, const Twine &Name="")
Copy fast-math-flags from an instruction rather than using the builder&#39;s default FMF.
Definition: IRBuilder.h:1257
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 minimum semantics.
Definition: APFloat.h:1248
const DebugLoc & getCurrentDebugLocation() const
Get location information used by debugging information.
Definition: IRBuilder.h:153
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1049
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:285
CallInst * CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memmove between the specified pointers.
Definition: IRBuilder.h:493
Value * CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1898
ConstantInt * getInt16(uint16_t C)
Get a constant 16-bit value.
Definition: IRBuilder.h:301
Type * getVoidTy()
Fetch the type representing void.
Definition: IRBuilder.h:379
BasicBlock * BB
Definition: IRBuilder.h:92
CallBrInst * CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, ArrayRef< BasicBlock *> IndirectDests, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="")
Definition: IRBuilder.h:973
AtomicOrdering
Atomic ordering for LLVM&#39;s memory model.
CatchReturnInst * CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB)
Definition: IRBuilder.h:1009
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1421
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1762
static Constant * getSizeOf(Type *Ty)
getSizeOf constant expr - computes the (alloc) size of a type (in address-units, not bits) in a targe...
Definition: Constants.cpp:1914
Class to represent function types.
Definition: DerivedTypes.h:102
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1767
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
Value * CreateSExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a SExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:1719
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1882
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:703
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:120
CallInst * CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, unsigned Alignment, Value *OffsetValue=nullptr, Value **TheCheck=nullptr)
Create an assume intrinsic call that represents an alignment assumption on the provided pointer...
Definition: IRBuilder.h:2313
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2018
void ClearInsertionPoint()
Clear the insertion point: created instructions will not be inserted into a block.
Definition: IRBuilder.h:115
void SetInstDebugLocation(Instruction *I) const
If this builder has a current debug location, set it on the specified instruction.
Definition: IRBuilder.h:157
This instruction compares its operands according to the predicate given to the constructor.
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.h:2232
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
ConstantFolder - Create constants with minimum, target independent, folding.
AtomicCmpXchgInst * CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, SyncScope::ID SSID=SyncScope::System)
Definition: IRBuilder.h:1479
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1066
Value * CreateSIToFP(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1744
An instruction for storing to memory.
Definition: Instructions.h:320
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:150
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:202
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1694
IRBuilder(LLVMContext &C, MDNode *FPMathTag=nullptr, ArrayRef< OperandBundleDef > OpBundles=None)
Definition: IRBuilder.h:752
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:65
Value * CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1914
Value * CreateZExtOrBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1777
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1018
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
Value * CreateXor(Value *LHS, const APInt &RHS, const Twine &Name="")
Definition: IRBuilder.h:1240
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:126
ConstantInt * getIntN(unsigned N, uint64_t C)
Get a constant N-bit value, zero extended or truncated from a 64-bit value.
Definition: IRBuilder.h:317
Class to represent pointers.
Definition: DerivedTypes.h:498
Type * getHalfTy()
Fetch the type representing a 16-bit floating point value.
Definition: IRBuilder.h:364
CallInst * CreateMinNum(Value *LHS, Value *RHS, const Twine &Name="")
Create call to the minnum intrinsic.
Definition: IRBuilder.h:698
Value * CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1954
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1217
IndirectBrInst * CreateIndirectBr(Value *Addr, unsigned NumDests=10)
Create an indirect branch instruction with the specified address operand, with an optional hint for t...
Definition: IRBuilder.h:886
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1992
InsertPoint saveAndClearIP()
Returns the current insert point, clearing it in the process.
Definition: IRBuilder.h:192
LoadInst * CreateLoad(Type *Ty, Value *Ptr, bool isVolatile, const Twine &Name="")
Definition: IRBuilder.h:1400
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Definition: IRBuilder.h:1406
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:749
Type * getDoubleTy()
Fetch the type representing a 64-bit floating point value.
Definition: IRBuilder.h:374
#define P(N)
CallBrInst * CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, ArrayRef< BasicBlock *> IndirectDests, ArrayRef< Value *> Args=None, const Twine &Name="")
Definition: IRBuilder.h:966
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:168
void restoreIP(InsertPoint IP)
Sets the current insert point to a previously-saved location.
Definition: IRBuilder.h:199
static IntegerType * getInt128Ty(LLVMContext &C)
Definition: Type.cpp:177
The landingpad instruction holds all of the information necessary to generate correct exception handl...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
IntegerType * getInt128Ty()
Fetch the type representing a 128-bit integer.
Definition: IRBuilder.h:356
Value * CreateFMul(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1282
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:321
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1179
InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
Creates a new insertion point at the given location.
Definition: IRBuilder.h:176
IRBuilder(Instruction *IP, MDNode *FPMathTag=nullptr, ArrayRef< OperandBundleDef > OpBundles=None)
Definition: IRBuilder.h:768
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Value * CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, const Twine &Name="")
Definition: IRBuilder.h:1615
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:286
UnreachableInst * CreateUnreachable()
Definition: IRBuilder.h:1013
Conditional or Unconditional Branch instruction.
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
MDNode * getDefaultFPMathTag() const
Get the floating point math metadata being used.
Definition: IRBuilder.h:207
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
This function has undefined behavior.
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition: IRBuilder.h:281
Value * CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1124
This is an important base class in LLVM.
Definition: Constant.h:41
InsertPoint saveIP() const
Returns the current insert point.
Definition: IRBuilder.h:187
Resume the propagation of an exception.
Value * CreateShl(Value *LHS, const APInt &RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1147
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1110
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.h:2057
Indirect Branch Instruction.
Value * CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1092
Value * CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1919
Value * CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, const Twine &Name="")
Definition: IRBuilder.h:1643
Value * CreateFPToUI(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1732
CallInst * CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name="")
Create call to the maxnum intrinsic.
Definition: IRBuilder.h:703
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const Twine &Name="")
Definition: IRBuilder.h:1396
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:160
CatchSwitchInst * CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB, unsigned NumHandlers, const Twine &Name="")
Definition: IRBuilder.h:991
Value * CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource, const Twine &Name="")
Copy fast-math-flags from an instruction rather than using the builder&#39;s default FMF.
Definition: IRBuilder.h:1274
Value * CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource, const Twine &Name="")
Copy fast-math-flags from an instruction rather than using the builder&#39;s default FMF.
Definition: IRBuilder.h:1308
VAArgInst * CreateVAArg(Value *List, Type *Ty, const Twine &Name="")
Definition: IRBuilder.h:2073
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1343
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:646
IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag=nullptr, ArrayRef< OperandBundleDef > OpBundles=None)
Definition: IRBuilder.h:756
CallInst * CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, Value *Alignment, Value *OffsetValue=nullptr, Value **TheCheck=nullptr)
Create an assume intrinsic call that represents an alignment assumption on the provided pointer...
Definition: IRBuilder.h:2340
Value * CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1902
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
A specialization of it&#39;s base class for read-write access to a gc.statepoint.
Definition: Statepoint.h:308
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:655
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast or an AddrSpaceCast cast instruction.
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align, const Twine &Name="")
Definition: IRBuilder.h:1434
void clearFastMathFlags()
Clear the fast-math flags.
Definition: IRBuilder.h:213
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1874
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align, bool isVolatile, const Twine &Name="")
Definition: IRBuilder.h:1440
self_iterator getIterator()
Definition: ilist_node.h:81
Class to represent integer types.
Definition: DerivedTypes.h:39
ConstantInt * getInt64(uint64_t C)
Get a constant 64-bit value.
Definition: IRBuilder.h:311
static CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:359
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2077
LoadInst * CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile, const Twine &Name="")
Definition: IRBuilder.h:1459
IRBuilderCallbackInserter(std::function< void(Instruction *)> Callback)
Definition: IRBuilder.h:76
void setAlignment(unsigned Align)
Constant * Insert(Constant *C, const Twine &="") const
No-op overload to handle constants.
Definition: IRBuilder.h:800
AtomicRMWInst * CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val, AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Definition: IRBuilder.h:1487
NUW NUW NUW NUW Exact static Exact BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1414
Value * CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1169
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
void SetInsertPoint(Instruction *I)
This specifies that created instructions should be inserted before the specified instruction.
Definition: IRBuilder.h:133
const T & getFolder()
Get the constant folder being used.
Definition: IRBuilder.h:789
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2119
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:219
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:384
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:661
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1225
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1083
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
Definition: APFloat.h:1237
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1690
static IndirectBrInst * Create(Value *Address, unsigned NumDests, Instruction *InsertBefore=nullptr)
Value * CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1174
Value * CreateSRem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1133
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
signed greater than
Definition: InstrTypes.h:673
Value * CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1979
Value * CreateUIToFP(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1740
Value * CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1924
Value * CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name="")
Definition: IRBuilder.h:1611
InvokeInst * CreateInvoke(FunctionType *Ty, Value *Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="")
Create an invoke instruction.
Definition: IRBuilder.h:891
This provides the default implementation of the IRBuilder &#39;InsertHelper&#39; method that is called whenev...
Definition: IRBuilder.h:61
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2004
Value * CreateGEP(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &Name="")
Definition: IRBuilder.h:1493
Value * CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1929
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:650
static BinaryOperator * CreateFNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
static Type * getHalfTy(LLVMContext &C)
Definition: Type.cpp:162
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:226
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:333
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
ResumeInst * CreateResume(Value *Exn)
Definition: IRBuilder.h:982
BlockVerifier::State From
InsertPoint - A saved insertion point.
Definition: IRBuilder.h:167
CallInst * CreateMinimum(Value *LHS, Value *RHS, const Twine &Name="")
Create call to the minimum intrinsic.
Definition: IRBuilder.h:708
static CleanupPadInst * Create(Value *ParentPad, ArrayRef< Value *> Args=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static CatchSwitchInst * Create(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumHandlers, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
iterator end()
Definition: BasicBlock.h:270
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, Instruction *InsertBefore=nullptr)
Value * CreateIsNull(Value *Arg, const Twine &Name="")
Return an i1 value testing if Arg is null.
Definition: IRBuilder.h:2146
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:129
InvokeInst * CreateInvoke(Value *Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="")
Definition: IRBuilder.h:925
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:1836
Value * CreateNUWNeg(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1357
Value * CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1886
CallInst * CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align, uint32_t ElementSize, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert an element unordered-atomic memset of the region of memory starting at the given po...
Definition: IRBuilder.h:422
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:660
Module.h This file contains the declarations for the Module class.
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2090
AddressSpace
Definition: NVPTXBaseInfo.h:21
signed less than
Definition: InstrTypes.h:675
IRBuilderBase(LLVMContext &context, MDNode *FPMathTag=nullptr, ArrayRef< OperandBundleDef > OpBundles=None)
Definition: IRBuilder.h:102
Value * CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1944
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:306
static CastInst * CreateFPCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create an FPExt, BitCast, or FPTrunc for fp -> fp casts.
Value * CreateExtractElement(Value *Vec, uint64_t Idx, const Twine &Name="")
Definition: IRBuilder.h:2085
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition: Type.cpp:179
Value * CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1939
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2104
CallInst * CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memcpy between the specified pointers.
Definition: IRBuilder.h:445
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:621
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
#define NC
Definition: regutils.h:42
CallBrInst * CreateCallBr(FunctionType *Ty, Value *Callee, BasicBlock *DefaultDest, ArrayRef< BasicBlock *> IndirectDests, ArrayRef< Value *> Args=None, const Twine &Name="")
Create a callbr instruction.
Definition: IRBuilder.h:947
Value * CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name="")
Definition: IRBuilder.h:1625
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1128
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:577
Value handle that asserts if the Value is deleted.
Definition: ValueHandle.h:237
Constant * CreateGlobalStringPtr(StringRef Str, const Twine &Name="", unsigned AddressSpace=0)
Same as CreateGlobalString, but return a pointer with "i8*" type instead of a pointer to array of i8...
Definition: IRBuilder.h:1677
ArrayRef< OperandBundleDef > DefaultOperandBundles
Definition: IRBuilder.h:99
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:163
Value * CreateFPCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1857
Value * CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name="")
Definition: IRBuilder.h:1213
signed less or equal
Definition: InstrTypes.h:676
Class for arbitrary precision integers.
Definition: APInt.h:69
BasicBlock::iterator getPoint() const
Definition: IRBuilder.h:183
CallInst * CreateMaximum(Value *LHS, Value *RHS, const Twine &Name="")
Create call to the maximum intrinsic.
Definition: IRBuilder.h:713
Value * CreateGEP(Value *Ptr, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1532
Value * CreateSDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1114
Value * CreateUDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1100
Value * CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1096
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Value * CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1822
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:89
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:336
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1138
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1813
Value * CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, const Twine &Name="")
Definition: IRBuilder.h:1661
static CatchPadInst * Create(Value *CatchSwitch, ArrayRef< Value *> Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:291
MDNode * DefaultFPMathTag
Definition: IRBuilder.h:96
LoadInst * CreateAlignedLoad(Value *Ptr, unsigned Align, const Twine &Name="")
Definition: IRBuilder.h:1453
IntegerType * getInt1Ty()
Fetch the type representing a single bit.
Definition: IRBuilder.h:331
Value * CreateBitOrPointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1845
SwitchInst * CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases=10, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a switch instruction with the specified value, default dest, and with a hint for the number of...
Definition: IRBuilder.h:876
Value * CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1795
Value * CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1079
ReturnInst * CreateRetVoid()
Create a &#39;ret void&#39; instruction.
Definition: IRBuilder.h:823
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass&#39;s ...
Value * CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1969
Value * CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1536
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &Name="")
Definition: IRBuilder.h:1498
Provides an &#39;InsertHelper&#39; that calls a user-provided callback after performing the default insertion...
Definition: IRBuilder.h:72
iterator insert(iterator where, pointer New)
Definition: ilist.h:226
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:324
FenceInst * CreateFence(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System, const Twine &Name="")
Definition: IRBuilder.h:1472
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:175
Value * CreateXor(Value *LHS, uint64_t RHS, const Twine &Name="")
Definition: IRBuilder.h:1244
static CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a Trunc or BitCast cast instruction.
CatchPadInst * CreateCatchPad(Value *ParentPad, ArrayRef< Value *> Args, const Twine &Name="")
Definition: IRBuilder.h:998
unsigned greater or equal
Definition: InstrTypes.h:670
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value *> Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
Value * CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1, const Twine &Name="")
Definition: IRBuilder.h:1630
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1180
CleanupReturnInst * CreateCleanupRet(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB=nullptr)
Definition: IRBuilder.h:986
const NodeList & List
Definition: RDFGraph.cpp:209
#define I(x, y, z)
Definition: MD5.cpp:58
Value * CreateFSub(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1265
#define N
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &Name="")
Definition: IRBuilder.h:1517
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:654
Value * CreateStripInvariantGroup(Value *Ptr)
Create a strip.invariant.group intrinsic call.
Definition: IRBuilder.h:2205
Value * CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, const Twine &Name="")
Definition: IRBuilder.h:1588
Value * CreateFPToSI(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1736
FunctionType * getFunctionType()
Definition: DerivedTypes.h:181
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:322
Type * getValueType() const
Definition: GlobalValue.h:275
uint32_t Size
Definition: Profile.cpp:46
void setDefaultFPMathTag(MDNode *FPMathTag)
Set the floating point math metadata to be used.
Definition: IRBuilder.h:216
static BinaryOperator * CreateNeg(Value *S1, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2009
Type * getFloatTy()
Fetch the type representing a 32-bit floating point value.
Definition: IRBuilder.h:369
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:658
Value * CreateFAdd(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1248
void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB, BasicBlock::iterator InsertPt) const
Definition: IRBuilder.h:80
LandingPadInst * CreateLandingPad(Type *Ty, unsigned NumClauses, const Twine &Name="")
Definition: IRBuilder.h:2136
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1199
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1757
Value * CreateFDiv(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1299
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
Definition: IRBuilder.h:793
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag...
Multiway switch.
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1804
static CastInst * CreateSExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a SExt or BitCast cast instruction.
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Definition: Instructions.h:933
CallInst * CreateCall(FunctionCallee Callee, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2033
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name="")
Definition: IRBuilder.h:1209
ReturnInst * CreateAggregateRet(Value *const *retVals, unsigned N)
Create a sequence of N insertvalue instructions, with one Value from the retVals array each...
Definition: IRBuilder.h:839
Value * CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource, const Twine &Name="")
Copy fast-math-flags from an instruction rather than using the builder&#39;s default FMF.
Definition: IRBuilder.h:1291
LoadInst * CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name)
Definition: IRBuilder.h:1448
static CatchReturnInst * Create(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore=nullptr)
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:649
LLVM Value Representation.
Definition: Value.h:72
void setAlignment(unsigned Align)
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:659
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:418
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, Instruction *MDSrc)
Create a conditional &#39;br Cond, TrueDest, FalseDest&#39; instruction.
Definition: IRBuilder.h:862
BranchInst * CreateBr(BasicBlock *Dest)
Create an unconditional &#39;br label X&#39; instruction.
Definition: IRBuilder.h:847
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:605
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:58
static CleanupReturnInst * Create(Value *CleanupPad, BasicBlock *UnwindBB=nullptr, Instruction *InsertBefore=nullptr)
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB, BasicBlock::iterator InsertPt) const
Definition: IRBuilder.h:63
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Definition: IRBuilder.h:219
BasicBlock::iterator GetInsertPoint() const
Definition: IRBuilder.h:121
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1159
Value * CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx, const Twine &Name="")
Definition: IRBuilder.h:1666
Invoke instruction.
ConstantInt * getInt8(uint8_t C)
Get a constant 8-bit value.
Definition: IRBuilder.h:296
print Print MemDeps of function
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:159
unsigned greater than
Definition: InstrTypes.h:669
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Value * CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1075
InvokeInst * CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value *> Args=None, const Twine &Name="")
Definition: IRBuilder.h:916
static bool isVolatile(Instruction *Inst)
CallInst * CreateElementUnorderedAtomicMemCpy(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert an element unordered-atomic memcpy between the specified pointers.
Definition: IRBuilder.h:471
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag=nullptr, ArrayRef< OperandBundleDef > OpBundles=None)
Definition: IRBuilder.h:781
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2127
bool isBigEndian() const
Definition: DataLayout.h:221
static Constant * get(LLVMContext &Context, ArrayRef< uint8_t > Elts)
get() constructors - Return a constant with vector type with an element count and element type matchi...
Definition: Constants.cpp:2582
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
IRBuilder(LLVMContext &C, const T &F, Inserter I=Inserter(), MDNode *FPMathTag=nullptr, ArrayRef< OperandBundleDef > OpBundles=None)
Definition: IRBuilder.h:746
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:322
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
BasicBlock * getBlock() const
Definition: IRBuilder.h:182
InvokeInst * CreateInvoke(Value *Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value *> Args=None, const Twine &Name="")
Definition: IRBuilder.h:936
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:651
InvokeInst * CreateInvoke(FunctionType *Ty, Value *Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value *> Args=None, const Twine &Name="")
Definition: IRBuilder.h:900
Value * CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1910
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:173
BasicBlock::iterator InsertPt
Definition: IRBuilder.h:93
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Value * CreateFNeg(Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1361
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
Definition: APFloat.h:1226
Value * CreateOr(Value *LHS, const APInt &RHS, const Twine &Name="")
Definition: IRBuilder.h:1227
signed greater or equal
Definition: InstrTypes.h:674
Value * CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1, const Twine &Name="")
Definition: IRBuilder.h:1648
const BasicBlock * getParent() const
Definition: Instruction.h:66
an instruction to allocate memory on the stack
Definition: Instructions.h:59
Value * CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1974
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2041
Value * CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name="")
Definition: IRBuilder.h:1671