LCOV - code coverage report
Current view: top level - /usr/lib/llvm-19/include/llvm/IR - InstrTypes.h (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 0.0 % 17 0
Test Date: 2026-07-03 19:57:34 Functions: 0.0 % 7 0
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 0.0 % 6 0

             Branch data     Line data    Source code
       1                 :             : //===- llvm/InstrTypes.h - Important Instruction subclasses -----*- 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 various meta classes of instructions that exist in the VM
      10                 :             : // representation.  Specific concrete subclasses of these may be found in the
      11                 :             : // i*.h files...
      12                 :             : //
      13                 :             : //===----------------------------------------------------------------------===//
      14                 :             : 
      15                 :             : #ifndef LLVM_IR_INSTRTYPES_H
      16                 :             : #define LLVM_IR_INSTRTYPES_H
      17                 :             : 
      18                 :             : #include "llvm/ADT/ArrayRef.h"
      19                 :             : #include "llvm/ADT/STLExtras.h"
      20                 :             : #include "llvm/ADT/Sequence.h"
      21                 :             : #include "llvm/ADT/StringMap.h"
      22                 :             : #include "llvm/ADT/Twine.h"
      23                 :             : #include "llvm/ADT/iterator_range.h"
      24                 :             : #include "llvm/IR/Attributes.h"
      25                 :             : #include "llvm/IR/CallingConv.h"
      26                 :             : #include "llvm/IR/DerivedTypes.h"
      27                 :             : #include "llvm/IR/FMF.h"
      28                 :             : #include "llvm/IR/Function.h"
      29                 :             : #include "llvm/IR/Instruction.h"
      30                 :             : #include "llvm/IR/LLVMContext.h"
      31                 :             : #include "llvm/IR/OperandTraits.h"
      32                 :             : #include "llvm/IR/User.h"
      33                 :             : #include <algorithm>
      34                 :             : #include <cassert>
      35                 :             : #include <cstddef>
      36                 :             : #include <cstdint>
      37                 :             : #include <iterator>
      38                 :             : #include <optional>
      39                 :             : #include <string>
      40                 :             : #include <vector>
      41                 :             : 
      42                 :             : namespace llvm {
      43                 :             : 
      44                 :             : class StringRef;
      45                 :             : class Type;
      46                 :             : class Value;
      47                 :             : class ConstantRange;
      48                 :             : 
      49                 :             : namespace Intrinsic {
      50                 :             : typedef unsigned ID;
      51                 :             : }
      52                 :             : 
      53                 :             : //===----------------------------------------------------------------------===//
      54                 :             : //                          UnaryInstruction Class
      55                 :             : //===----------------------------------------------------------------------===//
      56                 :             : 
      57                 :             : class UnaryInstruction : public Instruction {
      58                 :             : protected:
      59                 :             :   UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock::iterator IB)
      60                 :             :       : Instruction(Ty, iType, &Op<0>(), 1, IB) {
      61                 :             :     Op<0>() = V;
      62                 :             :   }
      63                 :             :   UnaryInstruction(Type *Ty, unsigned iType, Value *V,
      64                 :             :                    Instruction *IB = nullptr)
      65                 :             :     : Instruction(Ty, iType, &Op<0>(), 1, IB) {
      66                 :             :     Op<0>() = V;
      67                 :             :   }
      68                 :             :   UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
      69                 :             :     : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
      70                 :             :     Op<0>() = V;
      71                 :             :   }
      72                 :             : 
      73                 :             : public:
      74                 :             :   // allocate space for exactly one operand
      75                 :             :   void *operator new(size_t S) { return User::operator new(S, 1); }
      76                 :             :   void operator delete(void *Ptr) { User::operator delete(Ptr); }
      77                 :             : 
      78                 :             :   /// Transparently provide more efficient getOperand methods.
      79                 :             :   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
      80                 :             : 
      81                 :             :   // Methods for support type inquiry through isa, cast, and dyn_cast:
      82                 :             :   static bool classof(const Instruction *I) {
      83                 :             :     return I->isUnaryOp() ||
      84                 :             :            I->getOpcode() == Instruction::Alloca ||
      85                 :             :            I->getOpcode() == Instruction::Load ||
      86                 :             :            I->getOpcode() == Instruction::VAArg ||
      87                 :             :            I->getOpcode() == Instruction::ExtractValue ||
      88                 :             :            (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
      89                 :             :   }
      90                 :             :   static bool classof(const Value *V) {
      91                 :             :     return isa<Instruction>(V) && classof(cast<Instruction>(V));
      92                 :             :   }
      93                 :             : };
      94                 :             : 
      95                 :             : template <>
      96                 :             : struct OperandTraits<UnaryInstruction> :
      97                 :             :   public FixedNumOperandTraits<UnaryInstruction, 1> {
      98                 :             : };
      99                 :             : 
     100                 :             : DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
     101                 :             : 
     102                 :             : //===----------------------------------------------------------------------===//
     103                 :             : //                                UnaryOperator Class
     104                 :             : //===----------------------------------------------------------------------===//
     105                 :             : 
     106                 :             : class UnaryOperator : public UnaryInstruction {
     107                 :             :   void AssertOK();
     108                 :             : 
     109                 :             : protected:
     110                 :             :   UnaryOperator(UnaryOps iType, Value *S, Type *Ty, const Twine &Name,
     111                 :             :                 InsertPosition InsertBefore);
     112                 :             : 
     113                 :             :   // Note: Instruction needs to be a friend here to call cloneImpl.
     114                 :             :   friend class Instruction;
     115                 :             : 
     116                 :             :   UnaryOperator *cloneImpl() const;
     117                 :             : 
     118                 :             : public:
     119                 :             :   /// Construct a unary instruction, given the opcode and an operand.
     120                 :             :   /// Optionally (if InstBefore is specified) insert the instruction
     121                 :             :   /// into a BasicBlock right before the specified instruction.  The specified
     122                 :             :   /// Instruction is allowed to be a dereferenced end iterator.
     123                 :             :   ///
     124                 :             :   static UnaryOperator *Create(UnaryOps Op, Value *S,
     125                 :             :                                const Twine &Name = Twine(),
     126                 :             :                                InsertPosition InsertBefore = nullptr);
     127                 :             : 
     128                 :             :   /// These methods just forward to Create, and are useful when you
     129                 :             :   /// statically know what type of instruction you're going to create.  These
     130                 :             :   /// helpers just save some typing.
     131                 :             : #define HANDLE_UNARY_INST(N, OPC, CLASS) \
     132                 :             :   static UnaryOperator *Create##OPC(Value *V, const Twine &Name = "") {\
     133                 :             :     return Create(Instruction::OPC, V, Name);\
     134                 :             :   }
     135                 :             : #include "llvm/IR/Instruction.def"
     136                 :             : #define HANDLE_UNARY_INST(N, OPC, CLASS) \
     137                 :             :   static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \
     138                 :             :                                     BasicBlock *BB) {\
     139                 :             :     return Create(Instruction::OPC, V, Name, BB);\
     140                 :             :   }
     141                 :             : #include "llvm/IR/Instruction.def"
     142                 :             : #define HANDLE_UNARY_INST(N, OPC, CLASS) \
     143                 :             :   static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \
     144                 :             :                                     Instruction *I) {\
     145                 :             :     return Create(Instruction::OPC, V, Name, I);\
     146                 :             :   }
     147                 :             : #include "llvm/IR/Instruction.def"
     148                 :             : #define HANDLE_UNARY_INST(N, OPC, CLASS) \
     149                 :             :   static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \
     150                 :             :                                     BasicBlock::iterator It) {\
     151                 :             :     return Create(Instruction::OPC, V, Name, It);\
     152                 :             :   }
     153                 :             : #include "llvm/IR/Instruction.def"
     154                 :             : 
     155                 :             :   static UnaryOperator *
     156                 :             :   CreateWithCopiedFlags(UnaryOps Opc, Value *V, Instruction *CopyO,
     157                 :             :                         const Twine &Name = "",
     158                 :             :                         InsertPosition InsertBefore = nullptr) {
     159                 :             :     UnaryOperator *UO = Create(Opc, V, Name, InsertBefore);
     160                 :             :     UO->copyIRFlags(CopyO);
     161                 :             :     return UO;
     162                 :             :   }
     163                 :             : 
     164                 :             :   static UnaryOperator *CreateFNegFMF(Value *Op, Instruction *FMFSource,
     165                 :             :                                       const Twine &Name = "",
     166                 :             :                                       InsertPosition InsertBefore = nullptr) {
     167                 :             :     return CreateWithCopiedFlags(Instruction::FNeg, Op, FMFSource, Name,
     168                 :             :                                  InsertBefore);
     169                 :             :   }
     170                 :             : 
     171                 :             :   UnaryOps getOpcode() const {
     172                 :             :     return static_cast<UnaryOps>(Instruction::getOpcode());
     173                 :             :   }
     174                 :             : 
     175                 :             :   // Methods for support type inquiry through isa, cast, and dyn_cast:
     176                 :             :   static bool classof(const Instruction *I) {
     177                 :             :     return I->isUnaryOp();
     178                 :             :   }
     179                 :             :   static bool classof(const Value *V) {
     180                 :             :     return isa<Instruction>(V) && classof(cast<Instruction>(V));
     181                 :             :   }
     182                 :             : };
     183                 :             : 
     184                 :             : //===----------------------------------------------------------------------===//
     185                 :             : //                           BinaryOperator Class
     186                 :             : //===----------------------------------------------------------------------===//
     187                 :             : 
     188                 :             : class BinaryOperator : public Instruction {
     189                 :             :   void AssertOK();
     190                 :             : 
     191                 :             : protected:
     192                 :             :   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
     193                 :             :                  const Twine &Name, InsertPosition InsertBefore);
     194                 :             : 
     195                 :             :   // Note: Instruction needs to be a friend here to call cloneImpl.
     196                 :             :   friend class Instruction;
     197                 :             : 
     198                 :             :   BinaryOperator *cloneImpl() const;
     199                 :             : 
     200                 :             : public:
     201                 :             :   // allocate space for exactly two operands
     202                 :             :   void *operator new(size_t S) { return User::operator new(S, 2); }
     203                 :             :   void operator delete(void *Ptr) { User::operator delete(Ptr); }
     204                 :             : 
     205                 :             :   /// Transparently provide more efficient getOperand methods.
     206                 :             :   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
     207                 :             : 
     208                 :             :   /// Construct a binary instruction, given the opcode and the two
     209                 :             :   /// operands.  Optionally (if InstBefore is specified) insert the instruction
     210                 :             :   /// into a BasicBlock right before the specified instruction.  The specified
     211                 :             :   /// Instruction is allowed to be a dereferenced end iterator.
     212                 :             :   ///
     213                 :             :   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
     214                 :             :                                 const Twine &Name = Twine(),
     215                 :             :                                 InsertPosition InsertBefore = nullptr);
     216                 :             : 
     217                 :             :   /// These methods just forward to Create, and are useful when you
     218                 :             :   /// statically know what type of instruction you're going to create.  These
     219                 :             :   /// helpers just save some typing.
     220                 :             : #define HANDLE_BINARY_INST(N, OPC, CLASS) \
     221                 :             :   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
     222                 :             :                                      const Twine &Name = "") {\
     223                 :             :     return Create(Instruction::OPC, V1, V2, Name);\
     224                 :             :   }
     225                 :             : #include "llvm/IR/Instruction.def"
     226                 :             : #define HANDLE_BINARY_INST(N, OPC, CLASS) \
     227                 :             :   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
     228                 :             :                                      const Twine &Name, BasicBlock *BB) {\
     229                 :             :     return Create(Instruction::OPC, V1, V2, Name, BB);\
     230                 :             :   }
     231                 :             : #include "llvm/IR/Instruction.def"
     232                 :             : #define HANDLE_BINARY_INST(N, OPC, CLASS) \
     233                 :             :   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
     234                 :             :                                      const Twine &Name, Instruction *I) {\
     235                 :             :     return Create(Instruction::OPC, V1, V2, Name, I);\
     236                 :             :   }
     237                 :             : #include "llvm/IR/Instruction.def"
     238                 :             : #define HANDLE_BINARY_INST(N, OPC, CLASS) \
     239                 :             :   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
     240                 :             :                                      const Twine &Name, BasicBlock::iterator It) {\
     241                 :             :     return Create(Instruction::OPC, V1, V2, Name, It);\
     242                 :             :   }
     243                 :             : #include "llvm/IR/Instruction.def"
     244                 :             : 
     245                 :             :   static BinaryOperator *
     246                 :             :   CreateWithCopiedFlags(BinaryOps Opc, Value *V1, Value *V2, Value *CopyO,
     247                 :             :                         const Twine &Name = "",
     248                 :             :                         InsertPosition InsertBefore = nullptr) {
     249                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore);
     250                 :             :     BO->copyIRFlags(CopyO);
     251                 :             :     return BO;
     252                 :             :   }
     253                 :             : 
     254                 :             :   static BinaryOperator *CreateWithFMF(BinaryOps Opc, Value *V1, Value *V2,
     255                 :             :                                        FastMathFlags FMF,
     256                 :             :                                        const Twine &Name = "",
     257                 :             :                                        InsertPosition InsertBefore = nullptr) {
     258                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore);
     259                 :             :     BO->setFastMathFlags(FMF);
     260                 :             :     return BO;
     261                 :             :   }
     262                 :             : 
     263                 :             :   static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2, FastMathFlags FMF,
     264                 :             :                                        const Twine &Name = "") {
     265                 :             :     return CreateWithFMF(Instruction::FAdd, V1, V2, FMF, Name);
     266                 :             :   }
     267                 :             :   static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2, FastMathFlags FMF,
     268                 :             :                                        const Twine &Name = "") {
     269                 :             :     return CreateWithFMF(Instruction::FSub, V1, V2, FMF, Name);
     270                 :             :   }
     271                 :             :   static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2, FastMathFlags FMF,
     272                 :             :                                        const Twine &Name = "") {
     273                 :             :     return CreateWithFMF(Instruction::FMul, V1, V2, FMF, Name);
     274                 :             :   }
     275                 :             :   static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2, FastMathFlags FMF,
     276                 :             :                                        const Twine &Name = "") {
     277                 :             :     return CreateWithFMF(Instruction::FDiv, V1, V2, FMF, Name);
     278                 :             :   }
     279                 :             : 
     280                 :             :   static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2,
     281                 :             :                                        Instruction *FMFSource,
     282                 :             :                                        const Twine &Name = "") {
     283                 :             :     return CreateWithCopiedFlags(Instruction::FAdd, V1, V2, FMFSource, Name);
     284                 :             :   }
     285                 :             :   static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2,
     286                 :             :                                        Instruction *FMFSource,
     287                 :             :                                        const Twine &Name = "") {
     288                 :             :     return CreateWithCopiedFlags(Instruction::FSub, V1, V2, FMFSource, Name);
     289                 :             :   }
     290                 :             :   static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2,
     291                 :             :                                        Instruction *FMFSource,
     292                 :             :                                        const Twine &Name = "") {
     293                 :             :     return CreateWithCopiedFlags(Instruction::FMul, V1, V2, FMFSource, Name);
     294                 :             :   }
     295                 :             :   static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2,
     296                 :             :                                        Instruction *FMFSource,
     297                 :             :                                        const Twine &Name = "") {
     298                 :             :     return CreateWithCopiedFlags(Instruction::FDiv, V1, V2, FMFSource, Name);
     299                 :             :   }
     300                 :             :   static BinaryOperator *CreateFRemFMF(Value *V1, Value *V2,
     301                 :             :                                        Instruction *FMFSource,
     302                 :             :                                        const Twine &Name = "") {
     303                 :             :     return CreateWithCopiedFlags(Instruction::FRem, V1, V2, FMFSource, Name);
     304                 :             :   }
     305                 :             : 
     306                 :             :   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
     307                 :             :                                    const Twine &Name = "") {
     308                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name);
     309                 :             :     BO->setHasNoSignedWrap(true);
     310                 :             :     return BO;
     311                 :             :   }
     312                 :             :   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
     313                 :             :                                    const Twine &Name, BasicBlock *BB) {
     314                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
     315                 :             :     BO->setHasNoSignedWrap(true);
     316                 :             :     return BO;
     317                 :             :   }
     318                 :             :   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
     319                 :             :                                    const Twine &Name, Instruction *I) {
     320                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
     321                 :             :     BO->setHasNoSignedWrap(true);
     322                 :             :     return BO;
     323                 :             :   }
     324                 :             :   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
     325                 :             :                                    const Twine &Name, BasicBlock::iterator It) {
     326                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, It);
     327                 :             :     BO->setHasNoSignedWrap(true);
     328                 :             :     return BO;
     329                 :             :   }
     330                 :             : 
     331                 :             :   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
     332                 :             :                                    const Twine &Name = "") {
     333                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name);
     334                 :             :     BO->setHasNoUnsignedWrap(true);
     335                 :             :     return BO;
     336                 :             :   }
     337                 :             :   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
     338                 :             :                                    const Twine &Name, BasicBlock *BB) {
     339                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
     340                 :             :     BO->setHasNoUnsignedWrap(true);
     341                 :             :     return BO;
     342                 :             :   }
     343                 :             :   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
     344                 :             :                                    const Twine &Name, Instruction *I) {
     345                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
     346                 :             :     BO->setHasNoUnsignedWrap(true);
     347                 :             :     return BO;
     348                 :             :   }
     349                 :             :   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
     350                 :             :                                    const Twine &Name, BasicBlock::iterator It) {
     351                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, It);
     352                 :             :     BO->setHasNoUnsignedWrap(true);
     353                 :             :     return BO;
     354                 :             :   }
     355                 :             : 
     356                 :             :   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
     357                 :             :                                      const Twine &Name = "") {
     358                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name);
     359                 :             :     BO->setIsExact(true);
     360                 :             :     return BO;
     361                 :             :   }
     362                 :             :   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
     363                 :             :                                      const Twine &Name, BasicBlock *BB) {
     364                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
     365                 :             :     BO->setIsExact(true);
     366                 :             :     return BO;
     367                 :             :   }
     368                 :             :   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
     369                 :             :                                      const Twine &Name, Instruction *I) {
     370                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
     371                 :             :     BO->setIsExact(true);
     372                 :             :     return BO;
     373                 :             :   }
     374                 :             :   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
     375                 :             :                                      const Twine &Name,
     376                 :             :                                      BasicBlock::iterator It) {
     377                 :             :     BinaryOperator *BO = Create(Opc, V1, V2, Name, It);
     378                 :             :     BO->setIsExact(true);
     379                 :             :     return BO;
     380                 :             :   }
     381                 :             : 
     382                 :             :   static inline BinaryOperator *
     383                 :             :   CreateDisjoint(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name = "");
     384                 :             :   static inline BinaryOperator *CreateDisjoint(BinaryOps Opc, Value *V1,
     385                 :             :                                                Value *V2, const Twine &Name,
     386                 :             :                                                BasicBlock *BB);
     387                 :             :   static inline BinaryOperator *CreateDisjoint(BinaryOps Opc, Value *V1,
     388                 :             :                                                Value *V2, const Twine &Name,
     389                 :             :                                                Instruction *I);
     390                 :             :   static inline BinaryOperator *CreateDisjoint(BinaryOps Opc, Value *V1,
     391                 :             :                                                Value *V2, const Twine &Name,
     392                 :             :                                                BasicBlock::iterator It);
     393                 :             : 
     394                 :             : #define DEFINE_HELPERS(OPC, NUWNSWEXACT)                                       \
     395                 :             :   static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2,        \
     396                 :             :                                                   const Twine &Name = "") {    \
     397                 :             :     return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name);                \
     398                 :             :   }                                                                            \
     399                 :             :   static BinaryOperator *Create##NUWNSWEXACT##OPC(                             \
     400                 :             :       Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) {               \
     401                 :             :     return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB);            \
     402                 :             :   }                                                                            \
     403                 :             :   static BinaryOperator *Create##NUWNSWEXACT##OPC(                             \
     404                 :             :       Value *V1, Value *V2, const Twine &Name, Instruction *I) {               \
     405                 :             :     return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I);             \
     406                 :             :   }                                                                            \
     407                 :             :   static BinaryOperator *Create##NUWNSWEXACT##OPC(                             \
     408                 :             :       Value *V1, Value *V2, const Twine &Name, BasicBlock::iterator It) {      \
     409                 :             :     return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, It);            \
     410                 :             :   }
     411                 :             : 
     412                 :             :   DEFINE_HELPERS(Add, NSW) // CreateNSWAdd
     413                 :             :   DEFINE_HELPERS(Add, NUW) // CreateNUWAdd
     414                 :             :   DEFINE_HELPERS(Sub, NSW) // CreateNSWSub
     415                 :             :   DEFINE_HELPERS(Sub, NUW) // CreateNUWSub
     416                 :             :   DEFINE_HELPERS(Mul, NSW) // CreateNSWMul
     417                 :             :   DEFINE_HELPERS(Mul, NUW) // CreateNUWMul
     418                 :             :   DEFINE_HELPERS(Shl, NSW) // CreateNSWShl
     419                 :             :   DEFINE_HELPERS(Shl, NUW) // CreateNUWShl
     420                 :             : 
     421                 :             :   DEFINE_HELPERS(SDiv, Exact)  // CreateExactSDiv
     422                 :             :   DEFINE_HELPERS(UDiv, Exact)  // CreateExactUDiv
     423                 :             :   DEFINE_HELPERS(AShr, Exact)  // CreateExactAShr
     424                 :             :   DEFINE_HELPERS(LShr, Exact)  // CreateExactLShr
     425                 :             : 
     426                 :             :   DEFINE_HELPERS(Or, Disjoint) // CreateDisjointOr
     427                 :             : 
     428                 :             : #undef DEFINE_HELPERS
     429                 :             : 
     430                 :             :   /// Helper functions to construct and inspect unary operations (NEG and NOT)
     431                 :             :   /// via binary operators SUB and XOR:
     432                 :             :   ///
     433                 :             :   /// Create the NEG and NOT instructions out of SUB and XOR instructions.
     434                 :             :   ///
     435                 :             :   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
     436                 :             :                                    InsertPosition InsertBefore = nullptr);
     437                 :             :   static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
     438                 :             :                                       InsertPosition InsertBefore = nullptr);
     439                 :             :   static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
     440                 :             :                                    InsertPosition InsertBefore = nullptr);
     441                 :             : 
     442                 :             :   BinaryOps getOpcode() const {
     443                 :             :     return static_cast<BinaryOps>(Instruction::getOpcode());
     444                 :             :   }
     445                 :             : 
     446                 :             :   /// Exchange the two operands to this instruction.
     447                 :             :   /// This instruction is safe to use on any binary instruction and
     448                 :             :   /// does not modify the semantics of the instruction.  If the instruction
     449                 :             :   /// cannot be reversed (ie, it's a Div), then return true.
     450                 :             :   ///
     451                 :             :   bool swapOperands();
     452                 :             : 
     453                 :             :   // Methods for support type inquiry through isa, cast, and dyn_cast:
     454                 :             :   static bool classof(const Instruction *I) {
     455                 :             :     return I->isBinaryOp();
     456                 :             :   }
     457                 :             :   static bool classof(const Value *V) {
     458                 :             :     return isa<Instruction>(V) && classof(cast<Instruction>(V));
     459                 :             :   }
     460                 :             : };
     461                 :             : 
     462                 :             : template <>
     463                 :             : struct OperandTraits<BinaryOperator> :
     464                 :             :   public FixedNumOperandTraits<BinaryOperator, 2> {
     465                 :             : };
     466                 :             : 
     467                 :             : DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
     468                 :             : 
     469                 :             : /// An or instruction, which can be marked as "disjoint", indicating that the
     470                 :             : /// inputs don't have a 1 in the same bit position. Meaning this instruction
     471                 :             : /// can also be treated as an add.
     472                 :             : class PossiblyDisjointInst : public BinaryOperator {
     473                 :             : public:
     474                 :             :   enum { IsDisjoint = (1 << 0) };
     475                 :             : 
     476                 :             :   void setIsDisjoint(bool B) {
     477                 :             :     SubclassOptionalData =
     478                 :             :         (SubclassOptionalData & ~IsDisjoint) | (B * IsDisjoint);
     479                 :             :   }
     480                 :             : 
     481                 :             :   bool isDisjoint() const { return SubclassOptionalData & IsDisjoint; }
     482                 :             : 
     483                 :             :   static bool classof(const Instruction *I) {
     484                 :             :     return I->getOpcode() == Instruction::Or;
     485                 :             :   }
     486                 :             : 
     487                 :             :   static bool classof(const Value *V) {
     488                 :             :     return isa<Instruction>(V) && classof(cast<Instruction>(V));
     489                 :             :   }
     490                 :             : };
     491                 :             : 
     492                 :             : BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
     493                 :             :                                                Value *V2, const Twine &Name) {
     494                 :             :   BinaryOperator *BO = Create(Opc, V1, V2, Name);
     495                 :             :   cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
     496                 :             :   return BO;
     497                 :             : }
     498                 :             : BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
     499                 :             :                                                Value *V2, const Twine &Name,
     500                 :             :                                                BasicBlock *BB) {
     501                 :             :   BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
     502                 :             :   cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
     503                 :             :   return BO;
     504                 :             : }
     505                 :             : BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
     506                 :             :                                                Value *V2, const Twine &Name,
     507                 :             :                                                Instruction *I) {
     508                 :             :   BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
     509                 :             :   cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
     510                 :             :   return BO;
     511                 :             : }
     512                 :             : BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
     513                 :             :                                                Value *V2, const Twine &Name,
     514                 :             :                                                BasicBlock::iterator It) {
     515                 :             :   BinaryOperator *BO = Create(Opc, V1, V2, Name, It);
     516                 :             :   cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
     517                 :             :   return BO;
     518                 :             : }
     519                 :             : 
     520                 :             : //===----------------------------------------------------------------------===//
     521                 :             : //                               CastInst Class
     522                 :             : //===----------------------------------------------------------------------===//
     523                 :             : 
     524                 :             : /// This is the base class for all instructions that perform data
     525                 :             : /// casts. It is simply provided so that instruction category testing
     526                 :             : /// can be performed with code like:
     527                 :             : ///
     528                 :             : /// if (isa<CastInst>(Instr)) { ... }
     529                 :             : /// Base class of casting instructions.
     530                 :             : class CastInst : public UnaryInstruction {
     531                 :             : protected:
     532                 :             :   /// Constructor with insert-before-instruction semantics for subclasses
     533                 :             :   CastInst(Type *Ty, unsigned iType, Value *S, const Twine &NameStr = "",
     534                 :             :            InsertPosition InsertBefore = nullptr)
     535                 :             :       : UnaryInstruction(Ty, iType, S, InsertBefore) {
     536                 :             :     setName(NameStr);
     537                 :             :   }
     538                 :             : 
     539                 :             : public:
     540                 :             :   /// Provides a way to construct any of the CastInst subclasses using an
     541                 :             :   /// opcode instead of the subclass's constructor. The opcode must be in the
     542                 :             :   /// CastOps category (Instruction::isCast(opcode) returns true). This
     543                 :             :   /// constructor has insert-before-instruction semantics to automatically
     544                 :             :   /// insert the new CastInst before InsertBefore (if it is non-null).
     545                 :             :   /// Construct any of the CastInst subclasses
     546                 :             :   static CastInst *Create(
     547                 :             :       Instruction::CastOps,   ///< The opcode of the cast instruction
     548                 :             :       Value *S,               ///< The value to be casted (operand 0)
     549                 :             :       Type *Ty,               ///< The type to which cast should be made
     550                 :             :       const Twine &Name = "", ///< Name for the instruction
     551                 :             :       InsertPosition InsertBefore = nullptr ///< Place to insert the instruction
     552                 :             :   );
     553                 :             : 
     554                 :             :   /// Create a ZExt or BitCast cast instruction
     555                 :             :   static CastInst *CreateZExtOrBitCast(
     556                 :             :       Value *S,               ///< The value to be casted (operand 0)
     557                 :             :       Type *Ty,               ///< The type to which cast should be made
     558                 :             :       const Twine &Name = "", ///< Name for the instruction
     559                 :             :       InsertPosition InsertBefore = nullptr ///< Place to insert the instruction
     560                 :             :   );
     561                 :             : 
     562                 :             :   /// Create a SExt or BitCast cast instruction
     563                 :             :   static CastInst *CreateSExtOrBitCast(
     564                 :             :       Value *S,               ///< The value to be casted (operand 0)
     565                 :             :       Type *Ty,               ///< The type to which cast should be made
     566                 :             :       const Twine &Name = "", ///< Name for the instruction
     567                 :             :       InsertPosition InsertBefore = nullptr ///< Place to insert the instruction
     568                 :             :   );
     569                 :             : 
     570                 :             :   /// Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
     571                 :             :   static CastInst *CreatePointerCast(
     572                 :             :       Value *S,               ///< The pointer value to be casted (operand 0)
     573                 :             :       Type *Ty,               ///< The type to which cast should be made
     574                 :             :       const Twine &Name = "", ///< Name for the instruction
     575                 :             :       InsertPosition InsertBefore = nullptr ///< Place to insert the instruction
     576                 :             :   );
     577                 :             : 
     578                 :             :   /// Create a BitCast or an AddrSpaceCast cast instruction.
     579                 :             :   static CastInst *CreatePointerBitCastOrAddrSpaceCast(
     580                 :             :       Value *S,               ///< The pointer value to be casted (operand 0)
     581                 :             :       Type *Ty,               ///< The type to which cast should be made
     582                 :             :       const Twine &Name = "", ///< Name for the instruction
     583                 :             :       InsertPosition InsertBefore = nullptr ///< Place to insert the instruction
     584                 :             :   );
     585                 :             : 
     586                 :             :   /// Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
     587                 :             :   ///
     588                 :             :   /// If the value is a pointer type and the destination an integer type,
     589                 :             :   /// creates a PtrToInt cast. If the value is an integer type and the
     590                 :             :   /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates
     591                 :             :   /// a bitcast.
     592                 :             :   static CastInst *CreateBitOrPointerCast(
     593                 :             :       Value *S,               ///< The pointer value to be casted (operand 0)
     594                 :             :       Type *Ty,               ///< The type to which cast should be made
     595                 :             :       const Twine &Name = "", ///< Name for the instruction
     596                 :             :       InsertPosition InsertBefore = nullptr ///< Place to insert the instruction
     597                 :             :   );
     598                 :             : 
     599                 :             :   /// Create a ZExt, BitCast, or Trunc for int -> int casts.
     600                 :             :   static CastInst *CreateIntegerCast(
     601                 :             :       Value *S,               ///< The pointer value to be casted (operand 0)
     602                 :             :       Type *Ty,               ///< The type to which cast should be made
     603                 :             :       bool isSigned,          ///< Whether to regard S as signed or not
     604                 :             :       const Twine &Name = "", ///< Name for the instruction
     605                 :             :       InsertPosition InsertBefore = nullptr ///< Place to insert the instruction
     606                 :             :   );
     607                 :             : 
     608                 :             :   /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
     609                 :             :   static CastInst *CreateFPCast(
     610                 :             :       Value *S,               ///< The floating point value to be casted
     611                 :             :       Type *Ty,               ///< The floating point type to cast to
     612                 :             :       const Twine &Name = "", ///< Name for the instruction
     613                 :             :       InsertPosition InsertBefore = nullptr ///< Place to insert the instruction
     614                 :             :   );
     615                 :             : 
     616                 :             :   /// Create a Trunc or BitCast cast instruction
     617                 :             :   static CastInst *CreateTruncOrBitCast(
     618                 :             :       Value *S,               ///< The value to be casted (operand 0)
     619                 :             :       Type *Ty,               ///< The type to which cast should be made
     620                 :             :       const Twine &Name = "", ///< Name for the instruction
     621                 :             :       InsertPosition InsertBefore = nullptr ///< Place to insert the instruction
     622                 :             :   );
     623                 :             : 
     624                 :             :   /// Check whether a bitcast between these types is valid
     625                 :             :   static bool isBitCastable(
     626                 :             :     Type *SrcTy, ///< The Type from which the value should be cast.
     627                 :             :     Type *DestTy ///< The Type to which the value should be cast.
     628                 :             :   );
     629                 :             : 
     630                 :             :   /// Check whether a bitcast, inttoptr, or ptrtoint cast between these
     631                 :             :   /// types is valid and a no-op.
     632                 :             :   ///
     633                 :             :   /// This ensures that any pointer<->integer cast has enough bits in the
     634                 :             :   /// integer and any other cast is a bitcast.
     635                 :             :   static bool isBitOrNoopPointerCastable(
     636                 :             :       Type *SrcTy,  ///< The Type from which the value should be cast.
     637                 :             :       Type *DestTy, ///< The Type to which the value should be cast.
     638                 :             :       const DataLayout &DL);
     639                 :             : 
     640                 :             :   /// Returns the opcode necessary to cast Val into Ty using usual casting
     641                 :             :   /// rules.
     642                 :             :   /// Infer the opcode for cast operand and type
     643                 :             :   static Instruction::CastOps getCastOpcode(
     644                 :             :     const Value *Val, ///< The value to cast
     645                 :             :     bool SrcIsSigned, ///< Whether to treat the source as signed
     646                 :             :     Type *Ty,   ///< The Type to which the value should be casted
     647                 :             :     bool DstIsSigned  ///< Whether to treate the dest. as signed
     648                 :             :   );
     649                 :             : 
     650                 :             :   /// There are several places where we need to know if a cast instruction
     651                 :             :   /// only deals with integer source and destination types. To simplify that
     652                 :             :   /// logic, this method is provided.
     653                 :             :   /// @returns true iff the cast has only integral typed operand and dest type.
     654                 :             :   /// Determine if this is an integer-only cast.
     655                 :             :   bool isIntegerCast() const;
     656                 :             : 
     657                 :             :   /// A no-op cast is one that can be effected without changing any bits.
     658                 :             :   /// It implies that the source and destination types are the same size. The
     659                 :             :   /// DataLayout argument is to determine the pointer size when examining casts
     660                 :             :   /// involving Integer and Pointer types. They are no-op casts if the integer
     661                 :             :   /// is the same size as the pointer. However, pointer size varies with
     662                 :             :   /// platform.  Note that a precondition of this method is that the cast is
     663                 :             :   /// legal - i.e. the instruction formed with these operands would verify.
     664                 :             :   static bool isNoopCast(
     665                 :             :     Instruction::CastOps Opcode, ///< Opcode of cast
     666                 :             :     Type *SrcTy,         ///< SrcTy of cast
     667                 :             :     Type *DstTy,         ///< DstTy of cast
     668                 :             :     const DataLayout &DL ///< DataLayout to get the Int Ptr type from.
     669                 :             :   );
     670                 :             : 
     671                 :             :   /// Determine if this cast is a no-op cast.
     672                 :             :   ///
     673                 :             :   /// \param DL is the DataLayout to determine pointer size.
     674                 :             :   bool isNoopCast(const DataLayout &DL) const;
     675                 :             : 
     676                 :             :   /// Determine how a pair of casts can be eliminated, if they can be at all.
     677                 :             :   /// This is a helper function for both CastInst and ConstantExpr.
     678                 :             :   /// @returns 0 if the CastInst pair can't be eliminated, otherwise
     679                 :             :   /// returns Instruction::CastOps value for a cast that can replace
     680                 :             :   /// the pair, casting SrcTy to DstTy.
     681                 :             :   /// Determine if a cast pair is eliminable
     682                 :             :   static unsigned isEliminableCastPair(
     683                 :             :     Instruction::CastOps firstOpcode,  ///< Opcode of first cast
     684                 :             :     Instruction::CastOps secondOpcode, ///< Opcode of second cast
     685                 :             :     Type *SrcTy, ///< SrcTy of 1st cast
     686                 :             :     Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
     687                 :             :     Type *DstTy, ///< DstTy of 2nd cast
     688                 :             :     Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
     689                 :             :     Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
     690                 :             :     Type *DstIntPtrTy  ///< Integer type corresponding to Ptr DstTy, or null
     691                 :             :   );
     692                 :             : 
     693                 :             :   /// Return the opcode of this CastInst
     694                 :             :   Instruction::CastOps getOpcode() const {
     695                 :             :     return Instruction::CastOps(Instruction::getOpcode());
     696                 :             :   }
     697                 :             : 
     698                 :             :   /// Return the source type, as a convenience
     699                 :             :   Type* getSrcTy() const { return getOperand(0)->getType(); }
     700                 :             :   /// Return the destination type, as a convenience
     701                 :             :   Type* getDestTy() const { return getType(); }
     702                 :             : 
     703                 :             :   /// This method can be used to determine if a cast from SrcTy to DstTy using
     704                 :             :   /// Opcode op is valid or not.
     705                 :             :   /// @returns true iff the proposed cast is valid.
     706                 :             :   /// Determine if a cast is valid without creating one.
     707                 :             :   static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy);
     708                 :             :   static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
     709                 :             :     return castIsValid(op, S->getType(), DstTy);
     710                 :             :   }
     711                 :             : 
     712                 :             :   /// Methods for support type inquiry through isa, cast, and dyn_cast:
     713                 :             :   static bool classof(const Instruction *I) {
     714                 :             :     return I->isCast();
     715                 :             :   }
     716                 :             :   static bool classof(const Value *V) {
     717                 :             :     return isa<Instruction>(V) && classof(cast<Instruction>(V));
     718                 :             :   }
     719                 :             : };
     720                 :             : 
     721                 :             : /// Instruction that can have a nneg flag (zext/uitofp).
     722                 :             : class PossiblyNonNegInst : public CastInst {
     723                 :             : public:
     724                 :             :   enum { NonNeg = (1 << 0) };
     725                 :             : 
     726                 :             :   static bool classof(const Instruction *I) {
     727                 :             :     switch (I->getOpcode()) {
     728                 :             :     case Instruction::ZExt:
     729                 :             :     case Instruction::UIToFP:
     730                 :             :       return true;
     731                 :             :     default:
     732                 :             :       return false;
     733                 :             :     }
     734                 :             :   }
     735                 :             : 
     736                 :             :   static bool classof(const Value *V) {
     737                 :             :     return isa<Instruction>(V) && classof(cast<Instruction>(V));
     738                 :             :   }
     739                 :             : };
     740                 :             : 
     741                 :             : //===----------------------------------------------------------------------===//
     742                 :             : //                               CmpInst Class
     743                 :             : //===----------------------------------------------------------------------===//
     744                 :             : 
     745                 :             : /// This class is the base class for the comparison instructions.
     746                 :             : /// Abstract base class of comparison instructions.
     747                 :             : class CmpInst : public Instruction {
     748                 :             : public:
     749                 :             :   /// This enumeration lists the possible predicates for CmpInst subclasses.
     750                 :             :   /// Values in the range 0-31 are reserved for FCmpInst, while values in the
     751                 :             :   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
     752                 :             :   /// predicate values are not overlapping between the classes.
     753                 :             :   ///
     754                 :             :   /// Some passes (e.g. InstCombine) depend on the bit-wise characteristics of
     755                 :             :   /// FCMP_* values. Changing the bit patterns requires a potential change to
     756                 :             :   /// those passes.
     757                 :             :   enum Predicate : unsigned {
     758                 :             :     // Opcode            U L G E    Intuitive operation
     759                 :             :     FCMP_FALSE = 0, ///< 0 0 0 0    Always false (always folded)
     760                 :             :     FCMP_OEQ = 1,   ///< 0 0 0 1    True if ordered and equal
     761                 :             :     FCMP_OGT = 2,   ///< 0 0 1 0    True if ordered and greater than
     762                 :             :     FCMP_OGE = 3,   ///< 0 0 1 1    True if ordered and greater than or equal
     763                 :             :     FCMP_OLT = 4,   ///< 0 1 0 0    True if ordered and less than
     764                 :             :     FCMP_OLE = 5,   ///< 0 1 0 1    True if ordered and less than or equal
     765                 :             :     FCMP_ONE = 6,   ///< 0 1 1 0    True if ordered and operands are unequal
     766                 :             :     FCMP_ORD = 7,   ///< 0 1 1 1    True if ordered (no nans)
     767                 :             :     FCMP_UNO = 8,   ///< 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
     768                 :             :     FCMP_UEQ = 9,   ///< 1 0 0 1    True if unordered or equal
     769                 :             :     FCMP_UGT = 10,  ///< 1 0 1 0    True if unordered or greater than
     770                 :             :     FCMP_UGE = 11,  ///< 1 0 1 1    True if unordered, greater than, or equal
     771                 :             :     FCMP_ULT = 12,  ///< 1 1 0 0    True if unordered or less than
     772                 :             :     FCMP_ULE = 13,  ///< 1 1 0 1    True if unordered, less than, or equal
     773                 :             :     FCMP_UNE = 14,  ///< 1 1 1 0    True if unordered or not equal
     774                 :             :     FCMP_TRUE = 15, ///< 1 1 1 1    Always true (always folded)
     775                 :             :     FIRST_FCMP_PREDICATE = FCMP_FALSE,
     776                 :             :     LAST_FCMP_PREDICATE = FCMP_TRUE,
     777                 :             :     BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
     778                 :             :     ICMP_EQ = 32,  ///< equal
     779                 :             :     ICMP_NE = 33,  ///< not equal
     780                 :             :     ICMP_UGT = 34, ///< unsigned greater than
     781                 :             :     ICMP_UGE = 35, ///< unsigned greater or equal
     782                 :             :     ICMP_ULT = 36, ///< unsigned less than
     783                 :             :     ICMP_ULE = 37, ///< unsigned less or equal
     784                 :             :     ICMP_SGT = 38, ///< signed greater than
     785                 :             :     ICMP_SGE = 39, ///< signed greater or equal
     786                 :             :     ICMP_SLT = 40, ///< signed less than
     787                 :             :     ICMP_SLE = 41, ///< signed less or equal
     788                 :             :     FIRST_ICMP_PREDICATE = ICMP_EQ,
     789                 :             :     LAST_ICMP_PREDICATE = ICMP_SLE,
     790                 :             :     BAD_ICMP_PREDICATE = ICMP_SLE + 1
     791                 :             :   };
     792                 :             :   using PredicateField =
     793                 :             :       Bitfield::Element<Predicate, 0, 6, LAST_ICMP_PREDICATE>;
     794                 :             : 
     795                 :             :   /// Returns the sequence of all FCmp predicates.
     796                 :             :   static auto FCmpPredicates() {
     797                 :             :     return enum_seq_inclusive(Predicate::FIRST_FCMP_PREDICATE,
     798                 :             :                               Predicate::LAST_FCMP_PREDICATE,
     799                 :             :                               force_iteration_on_noniterable_enum);
     800                 :             :   }
     801                 :             : 
     802                 :             :   /// Returns the sequence of all ICmp predicates.
     803                 :             :   static auto ICmpPredicates() {
     804                 :             :     return enum_seq_inclusive(Predicate::FIRST_ICMP_PREDICATE,
     805                 :             :                               Predicate::LAST_ICMP_PREDICATE,
     806                 :             :                               force_iteration_on_noniterable_enum);
     807                 :             :   }
     808                 :             : 
     809                 :             : protected:
     810                 :             :   CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, Value *LHS,
     811                 :             :           Value *RHS, const Twine &Name = "",
     812                 :             :           InsertPosition InsertBefore = nullptr,
     813                 :             :           Instruction *FlagsSource = nullptr);
     814                 :             : 
     815                 :             : public:
     816                 :             :   // allocate space for exactly two operands
     817                 :             :   void *operator new(size_t S) { return User::operator new(S, 2); }
     818                 :             :   void operator delete(void *Ptr) { User::operator delete(Ptr); }
     819                 :             : 
     820                 :             :   /// Construct a compare instruction, given the opcode, the predicate and
     821                 :             :   /// the two operands.  Optionally (if InstBefore is specified) insert the
     822                 :             :   /// instruction into a BasicBlock right before the specified instruction.
     823                 :             :   /// The specified Instruction is allowed to be a dereferenced end iterator.
     824                 :             :   /// Create a CmpInst
     825                 :             :   static CmpInst *Create(OtherOps Op, Predicate Pred, Value *S1, Value *S2,
     826                 :             :                          const Twine &Name = "",
     827                 :             :                          InsertPosition InsertBefore = nullptr);
     828                 :             : 
     829                 :             :   /// Construct a compare instruction, given the opcode, the predicate,
     830                 :             :   /// the two operands and the instruction to copy the flags from. Optionally
     831                 :             :   /// (if InstBefore is specified) insert the instruction into a BasicBlock
     832                 :             :   /// right before the specified instruction. The specified Instruction is
     833                 :             :   /// allowed to be a dereferenced end iterator.
     834                 :             :   /// Create a CmpInst
     835                 :             :   static CmpInst *CreateWithCopiedFlags(OtherOps Op, Predicate Pred, Value *S1,
     836                 :             :                                         Value *S2,
     837                 :             :                                         const Instruction *FlagsSource,
     838                 :             :                                         const Twine &Name = "",
     839                 :             :                                         InsertPosition InsertBefore = nullptr);
     840                 :             : 
     841                 :             :   /// Get the opcode casted to the right type
     842                 :             :   OtherOps getOpcode() const {
     843                 :             :     return static_cast<OtherOps>(Instruction::getOpcode());
     844                 :             :   }
     845                 :             : 
     846                 :             :   /// Return the predicate for this instruction.
     847                 :             :   Predicate getPredicate() const { return getSubclassData<PredicateField>(); }
     848                 :             : 
     849                 :             :   /// Set the predicate for this instruction to the specified value.
     850                 :             :   void setPredicate(Predicate P) { setSubclassData<PredicateField>(P); }
     851                 :             : 
     852                 :             :   static bool isFPPredicate(Predicate P) {
     853                 :             :     static_assert(FIRST_FCMP_PREDICATE == 0,
     854                 :             :                   "FIRST_FCMP_PREDICATE is required to be 0");
     855                 :             :     return P <= LAST_FCMP_PREDICATE;
     856                 :             :   }
     857                 :             : 
     858                 :             :   static bool isIntPredicate(Predicate P) {
     859                 :             :     return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
     860                 :             :   }
     861                 :             : 
     862                 :             :   static StringRef getPredicateName(Predicate P);
     863                 :             : 
     864                 :             :   bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
     865                 :             :   bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
     866                 :             : 
     867                 :             :   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
     868                 :             :   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
     869                 :             :   /// @returns the inverse predicate for the instruction's current predicate.
     870                 :             :   /// Return the inverse of the instruction's predicate.
     871                 :             :   Predicate getInversePredicate() const {
     872                 :             :     return getInversePredicate(getPredicate());
     873                 :             :   }
     874                 :             : 
     875                 :             :   /// Returns the ordered variant of a floating point compare.
     876                 :             :   ///
     877                 :             :   /// For example, UEQ -> OEQ, ULT -> OLT, OEQ -> OEQ
     878                 :             :   static Predicate getOrderedPredicate(Predicate Pred) {
     879                 :             :     return static_cast<Predicate>(Pred & FCMP_ORD);
     880                 :             :   }
     881                 :             : 
     882                 :             :   Predicate getOrderedPredicate() const {
     883                 :             :     return getOrderedPredicate(getPredicate());
     884                 :             :   }
     885                 :             : 
     886                 :             :   /// Returns the unordered variant of a floating point compare.
     887                 :             :   ///
     888                 :             :   /// For example, OEQ -> UEQ, OLT -> ULT, OEQ -> UEQ
     889                 :             :   static Predicate getUnorderedPredicate(Predicate Pred) {
     890                 :             :     return static_cast<Predicate>(Pred | FCMP_UNO);
     891                 :             :   }
     892                 :             : 
     893                 :             :   Predicate getUnorderedPredicate() const {
     894                 :             :     return getUnorderedPredicate(getPredicate());
     895                 :             :   }
     896                 :             : 
     897                 :             :   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
     898                 :             :   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
     899                 :             :   /// @returns the inverse predicate for predicate provided in \p pred.
     900                 :             :   /// Return the inverse of a given predicate
     901                 :             :   static Predicate getInversePredicate(Predicate pred);
     902                 :             : 
     903                 :             :   /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
     904                 :             :   ///              OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
     905                 :             :   /// @returns the predicate that would be the result of exchanging the two
     906                 :             :   /// operands of the CmpInst instruction without changing the result
     907                 :             :   /// produced.
     908                 :             :   /// Return the predicate as if the operands were swapped
     909                 :             :   Predicate getSwappedPredicate() const {
     910                 :             :     return getSwappedPredicate(getPredicate());
     911                 :             :   }
     912                 :             : 
     913                 :             :   /// This is a static version that you can use without an instruction
     914                 :             :   /// available.
     915                 :             :   /// Return the predicate as if the operands were swapped.
     916                 :             :   static Predicate getSwappedPredicate(Predicate pred);
     917                 :             : 
     918                 :             :   /// This is a static version that you can use without an instruction
     919                 :             :   /// available.
     920                 :             :   /// @returns true if the comparison predicate is strict, false otherwise.
     921                 :             :   static bool isStrictPredicate(Predicate predicate);
     922                 :             : 
     923                 :             :   /// @returns true if the comparison predicate is strict, false otherwise.
     924                 :             :   /// Determine if this instruction is using an strict comparison predicate.
     925                 :             :   bool isStrictPredicate() const { return isStrictPredicate(getPredicate()); }
     926                 :             : 
     927                 :             :   /// This is a static version that you can use without an instruction
     928                 :             :   /// available.
     929                 :             :   /// @returns true if the comparison predicate is non-strict, false otherwise.
     930                 :             :   static bool isNonStrictPredicate(Predicate predicate);
     931                 :             : 
     932                 :             :   /// @returns true if the comparison predicate is non-strict, false otherwise.
     933                 :             :   /// Determine if this instruction is using an non-strict comparison predicate.
     934                 :             :   bool isNonStrictPredicate() const {
     935                 :             :     return isNonStrictPredicate(getPredicate());
     936                 :             :   }
     937                 :             : 
     938                 :             :   /// For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT.
     939                 :             :   /// Returns the strict version of non-strict comparisons.
     940                 :             :   Predicate getStrictPredicate() const {
     941                 :             :     return getStrictPredicate(getPredicate());
     942                 :             :   }
     943                 :             : 
     944                 :             :   /// This is a static version that you can use without an instruction
     945                 :             :   /// available.
     946                 :             :   /// @returns the strict version of comparison provided in \p pred.
     947                 :             :   /// If \p pred is not a strict comparison predicate, returns \p pred.
     948                 :             :   /// Returns the strict version of non-strict comparisons.
     949                 :             :   static Predicate getStrictPredicate(Predicate pred);
     950                 :             : 
     951                 :             :   /// For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
     952                 :             :   /// Returns the non-strict version of strict comparisons.
     953                 :             :   Predicate getNonStrictPredicate() const {
     954                 :             :     return getNonStrictPredicate(getPredicate());
     955                 :             :   }
     956                 :             : 
     957                 :             :   /// This is a static version that you can use without an instruction
     958                 :             :   /// available.
     959                 :             :   /// @returns the non-strict version of comparison provided in \p pred.
     960                 :             :   /// If \p pred is not a strict comparison predicate, returns \p pred.
     961                 :             :   /// Returns the non-strict version of strict comparisons.
     962                 :             :   static Predicate getNonStrictPredicate(Predicate pred);
     963                 :             : 
     964                 :             :   /// This is a static version that you can use without an instruction
     965                 :             :   /// available.
     966                 :             :   /// Return the flipped strictness of predicate
     967                 :             :   static Predicate getFlippedStrictnessPredicate(Predicate pred);
     968                 :             : 
     969                 :             :   /// For predicate of kind "is X or equal to 0" returns the predicate "is X".
     970                 :             :   /// For predicate of kind "is X" returns the predicate "is X or equal to 0".
     971                 :             :   /// does not support other kind of predicates.
     972                 :             :   /// @returns the predicate that does not contains is equal to zero if
     973                 :             :   /// it had and vice versa.
     974                 :             :   /// Return the flipped strictness of predicate
     975                 :             :   Predicate getFlippedStrictnessPredicate() const {
     976                 :             :     return getFlippedStrictnessPredicate(getPredicate());
     977                 :             :   }
     978                 :             : 
     979                 :             :   /// Provide more efficient getOperand methods.
     980                 :             :   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
     981                 :             : 
     982                 :             :   /// This is just a convenience that dispatches to the subclasses.
     983                 :             :   /// Swap the operands and adjust predicate accordingly to retain
     984                 :             :   /// the same comparison.
     985                 :             :   void swapOperands();
     986                 :             : 
     987                 :             :   /// This is just a convenience that dispatches to the subclasses.
     988                 :             :   /// Determine if this CmpInst is commutative.
     989                 :             :   bool isCommutative() const;
     990                 :             : 
     991                 :             :   /// Determine if this is an equals/not equals predicate.
     992                 :             :   /// This is a static version that you can use without an instruction
     993                 :             :   /// available.
     994                 :             :   static bool isEquality(Predicate pred);
     995                 :             : 
     996                 :             :   /// Determine if this is an equals/not equals predicate.
     997                 :             :   bool isEquality() const { return isEquality(getPredicate()); }
     998                 :             : 
     999                 :             :   /// Return true if the predicate is relational (not EQ or NE).
    1000                 :             :   static bool isRelational(Predicate P) { return !isEquality(P); }
    1001                 :             : 
    1002                 :             :   /// Return true if the predicate is relational (not EQ or NE).
    1003                 :             :   bool isRelational() const { return !isEquality(); }
    1004                 :             : 
    1005                 :             :   /// @returns true if the comparison is signed, false otherwise.
    1006                 :             :   /// Determine if this instruction is using a signed comparison.
    1007                 :             :   bool isSigned() const {
    1008                 :             :     return isSigned(getPredicate());
    1009                 :             :   }
    1010                 :             : 
    1011                 :             :   /// @returns true if the comparison is unsigned, false otherwise.
    1012                 :             :   /// Determine if this instruction is using an unsigned comparison.
    1013                 :             :   bool isUnsigned() const {
    1014                 :             :     return isUnsigned(getPredicate());
    1015                 :             :   }
    1016                 :             : 
    1017                 :             :   /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
    1018                 :             :   /// @returns the signed version of the unsigned predicate pred.
    1019                 :             :   /// return the signed version of a predicate
    1020                 :             :   static Predicate getSignedPredicate(Predicate pred);
    1021                 :             : 
    1022                 :             :   /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
    1023                 :             :   /// @returns the signed version of the predicate for this instruction (which
    1024                 :             :   /// has to be an unsigned predicate).
    1025                 :             :   /// return the signed version of a predicate
    1026                 :             :   Predicate getSignedPredicate() {
    1027                 :             :     return getSignedPredicate(getPredicate());
    1028                 :             :   }
    1029                 :             : 
    1030                 :             :   /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
    1031                 :             :   /// @returns the unsigned version of the signed predicate pred.
    1032                 :             :   static Predicate getUnsignedPredicate(Predicate pred);
    1033                 :             : 
    1034                 :             :   /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
    1035                 :             :   /// @returns the unsigned version of the predicate for this instruction (which
    1036                 :             :   /// has to be an signed predicate).
    1037                 :             :   /// return the unsigned version of a predicate
    1038                 :             :   Predicate getUnsignedPredicate() {
    1039                 :             :     return getUnsignedPredicate(getPredicate());
    1040                 :             :   }
    1041                 :             : 
    1042                 :             :   /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
    1043                 :             :   /// @returns the unsigned version of the signed predicate pred or
    1044                 :             :   ///          the signed version of the signed predicate pred.
    1045                 :             :   static Predicate getFlippedSignednessPredicate(Predicate pred);
    1046                 :             : 
    1047                 :             :   /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
    1048                 :             :   /// @returns the unsigned version of the signed predicate pred or
    1049                 :             :   ///          the signed version of the signed predicate pred.
    1050                 :             :   Predicate getFlippedSignednessPredicate() {
    1051                 :             :     return getFlippedSignednessPredicate(getPredicate());
    1052                 :             :   }
    1053                 :             : 
    1054                 :             :   /// This is just a convenience.
    1055                 :             :   /// Determine if this is true when both operands are the same.
    1056                 :             :   bool isTrueWhenEqual() const {
    1057                 :             :     return isTrueWhenEqual(getPredicate());
    1058                 :             :   }
    1059                 :             : 
    1060                 :             :   /// This is just a convenience.
    1061                 :             :   /// Determine if this is false when both operands are the same.
    1062                 :             :   bool isFalseWhenEqual() const {
    1063                 :             :     return isFalseWhenEqual(getPredicate());
    1064                 :             :   }
    1065                 :             : 
    1066                 :             :   /// @returns true if the predicate is unsigned, false otherwise.
    1067                 :             :   /// Determine if the predicate is an unsigned operation.
    1068                 :             :   static bool isUnsigned(Predicate predicate);
    1069                 :             : 
    1070                 :             :   /// @returns true if the predicate is signed, false otherwise.
    1071                 :             :   /// Determine if the predicate is an signed operation.
    1072                 :             :   static bool isSigned(Predicate predicate);
    1073                 :             : 
    1074                 :             :   /// Determine if the predicate is an ordered operation.
    1075                 :             :   static bool isOrdered(Predicate predicate);
    1076                 :             : 
    1077                 :             :   /// Determine if the predicate is an unordered operation.
    1078                 :             :   static bool isUnordered(Predicate predicate);
    1079                 :             : 
    1080                 :             :   /// Determine if the predicate is true when comparing a value with itself.
    1081                 :             :   static bool isTrueWhenEqual(Predicate predicate);
    1082                 :             : 
    1083                 :             :   /// Determine if the predicate is false when comparing a value with itself.
    1084                 :             :   static bool isFalseWhenEqual(Predicate predicate);
    1085                 :             : 
    1086                 :             :   /// Determine if Pred1 implies Pred2 is true when two compares have matching
    1087                 :             :   /// operands.
    1088                 :             :   static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2);
    1089                 :             : 
    1090                 :             :   /// Determine if Pred1 implies Pred2 is false when two compares have matching
    1091                 :             :   /// operands.
    1092                 :             :   static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2);
    1093                 :             : 
    1094                 :             :   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    1095                 :             :   static bool classof(const Instruction *I) {
    1096                 :             :     return I->getOpcode() == Instruction::ICmp ||
    1097                 :             :            I->getOpcode() == Instruction::FCmp;
    1098                 :             :   }
    1099                 :             :   static bool classof(const Value *V) {
    1100                 :             :     return isa<Instruction>(V) && classof(cast<Instruction>(V));
    1101                 :             :   }
    1102                 :             : 
    1103                 :             :   /// Create a result type for fcmp/icmp
    1104                 :             :   static Type* makeCmpResultType(Type* opnd_type) {
    1105                 :             :     if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
    1106                 :             :       return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
    1107                 :             :                              vt->getElementCount());
    1108                 :             :     }
    1109                 :             :     return Type::getInt1Ty(opnd_type->getContext());
    1110                 :             :   }
    1111                 :             : 
    1112                 :             : private:
    1113                 :             :   // Shadow Value::setValueSubclassData with a private forwarding method so that
    1114                 :             :   // subclasses cannot accidentally use it.
    1115                 :             :   void setValueSubclassData(unsigned short D) {
    1116                 :             :     Value::setValueSubclassData(D);
    1117                 :             :   }
    1118                 :             : };
    1119                 :             : 
    1120                 :             : // FIXME: these are redundant if CmpInst < BinaryOperator
    1121                 :             : template <>
    1122                 :             : struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
    1123                 :             : };
    1124                 :             : 
    1125                 :             : DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
    1126                 :             : 
    1127                 :             : raw_ostream &operator<<(raw_ostream &OS, CmpInst::Predicate Pred);
    1128                 :             : 
    1129                 :             : /// A lightweight accessor for an operand bundle meant to be passed
    1130                 :             : /// around by value.
    1131                 :             : struct OperandBundleUse {
    1132                 :             :   ArrayRef<Use> Inputs;
    1133                 :             : 
    1134                 :             :   OperandBundleUse() = default;
    1135                 :             :   explicit OperandBundleUse(StringMapEntry<uint32_t> *Tag, ArrayRef<Use> Inputs)
    1136                 :             :       : Inputs(Inputs), Tag(Tag) {}
    1137                 :             : 
    1138                 :             :   /// Return true if the operand at index \p Idx in this operand bundle
    1139                 :             :   /// has the attribute A.
    1140                 :             :   bool operandHasAttr(unsigned Idx, Attribute::AttrKind A) const {
    1141                 :             :     if (isDeoptOperandBundle())
    1142                 :             :       if (A == Attribute::ReadOnly || A == Attribute::NoCapture)
    1143                 :             :         return Inputs[Idx]->getType()->isPointerTy();
    1144                 :             : 
    1145                 :             :     // Conservative answer:  no operands have any attributes.
    1146                 :             :     return false;
    1147                 :             :   }
    1148                 :             : 
    1149                 :             :   /// Return the tag of this operand bundle as a string.
    1150                 :             :   StringRef getTagName() const {
    1151                 :             :     return Tag->getKey();
    1152                 :             :   }
    1153                 :             : 
    1154                 :             :   /// Return the tag of this operand bundle as an integer.
    1155                 :             :   ///
    1156                 :             :   /// Operand bundle tags are interned by LLVMContextImpl::getOrInsertBundleTag,
    1157                 :             :   /// and this function returns the unique integer getOrInsertBundleTag
    1158                 :             :   /// associated the tag of this operand bundle to.
    1159                 :             :   uint32_t getTagID() const {
    1160                 :             :     return Tag->getValue();
    1161                 :             :   }
    1162                 :             : 
    1163                 :             :   /// Return true if this is a "deopt" operand bundle.
    1164                 :             :   bool isDeoptOperandBundle() const {
    1165                 :             :     return getTagID() == LLVMContext::OB_deopt;
    1166                 :             :   }
    1167                 :             : 
    1168                 :             :   /// Return true if this is a "funclet" operand bundle.
    1169                 :             :   bool isFuncletOperandBundle() const {
    1170                 :             :     return getTagID() == LLVMContext::OB_funclet;
    1171                 :             :   }
    1172                 :             : 
    1173                 :             :   /// Return true if this is a "cfguardtarget" operand bundle.
    1174                 :             :   bool isCFGuardTargetOperandBundle() const {
    1175                 :             :     return getTagID() == LLVMContext::OB_cfguardtarget;
    1176                 :             :   }
    1177                 :             : 
    1178                 :             : private:
    1179                 :             :   /// Pointer to an entry in LLVMContextImpl::getOrInsertBundleTag.
    1180                 :             :   StringMapEntry<uint32_t> *Tag;
    1181                 :             : };
    1182                 :             : 
    1183                 :             : /// A container for an operand bundle being viewed as a set of values
    1184                 :             : /// rather than a set of uses.
    1185                 :             : ///
    1186                 :             : /// Unlike OperandBundleUse, OperandBundleDefT owns the memory it carries, and
    1187                 :             : /// so it is possible to create and pass around "self-contained" instances of
    1188                 :             : /// OperandBundleDef and ConstOperandBundleDef.
    1189                 :             : template <typename InputTy> class OperandBundleDefT {
    1190                 :             :   std::string Tag;
    1191                 :             :   std::vector<InputTy> Inputs;
    1192                 :             : 
    1193                 :             : public:
    1194                 :             :   explicit OperandBundleDefT(std::string Tag, std::vector<InputTy> Inputs)
    1195                 :             :       : Tag(std::move(Tag)), Inputs(std::move(Inputs)) {}
    1196                 :             :   explicit OperandBundleDefT(std::string Tag, ArrayRef<InputTy> Inputs)
    1197                 :             :       : Tag(std::move(Tag)), Inputs(Inputs) {}
    1198                 :             : 
    1199                 :             :   explicit OperandBundleDefT(const OperandBundleUse &OBU) {
    1200                 :             :     Tag = std::string(OBU.getTagName());
    1201                 :             :     llvm::append_range(Inputs, OBU.Inputs);
    1202                 :             :   }
    1203                 :             : 
    1204                 :             :   ArrayRef<InputTy> inputs() const { return Inputs; }
    1205                 :             : 
    1206                 :             :   using input_iterator = typename std::vector<InputTy>::const_iterator;
    1207                 :             : 
    1208                 :           0 :   size_t input_size() const { return Inputs.size(); }
    1209                 :             :   input_iterator input_begin() const { return Inputs.begin(); }
    1210                 :             :   input_iterator input_end() const { return Inputs.end(); }
    1211                 :             : 
    1212                 :             :   StringRef getTag() const { return Tag; }
    1213                 :             : };
    1214                 :             : 
    1215                 :             : using OperandBundleDef = OperandBundleDefT<Value *>;
    1216                 :             : using ConstOperandBundleDef = OperandBundleDefT<const Value *>;
    1217                 :             : 
    1218                 :             : //===----------------------------------------------------------------------===//
    1219                 :             : //                               CallBase Class
    1220                 :             : //===----------------------------------------------------------------------===//
    1221                 :             : 
    1222                 :             : /// Base class for all callable instructions (InvokeInst and CallInst)
    1223                 :             : /// Holds everything related to calling a function.
    1224                 :             : ///
    1225                 :             : /// All call-like instructions are required to use a common operand layout:
    1226                 :             : /// - Zero or more arguments to the call,
    1227                 :             : /// - Zero or more operand bundles with zero or more operand inputs each
    1228                 :             : ///   bundle,
    1229                 :             : /// - Zero or more subclass controlled operands
    1230                 :             : /// - The called function.
    1231                 :             : ///
    1232                 :             : /// This allows this base class to easily access the called function and the
    1233                 :             : /// start of the arguments without knowing how many other operands a particular
    1234                 :             : /// subclass requires. Note that accessing the end of the argument list isn't
    1235                 :             : /// as cheap as most other operations on the base class.
    1236                 :             : class CallBase : public Instruction {
    1237                 :             : protected:
    1238                 :             :   // The first two bits are reserved by CallInst for fast retrieval,
    1239                 :             :   using CallInstReservedField = Bitfield::Element<unsigned, 0, 2>;
    1240                 :             :   using CallingConvField =
    1241                 :             :       Bitfield::Element<CallingConv::ID, CallInstReservedField::NextBit, 10,
    1242                 :             :                         CallingConv::MaxID>;
    1243                 :             :   static_assert(
    1244                 :             :       Bitfield::areContiguous<CallInstReservedField, CallingConvField>(),
    1245                 :             :       "Bitfields must be contiguous");
    1246                 :             : 
    1247                 :             :   /// The last operand is the called operand.
    1248                 :             :   static constexpr int CalledOperandOpEndIdx = -1;
    1249                 :             : 
    1250                 :             :   AttributeList Attrs; ///< parameter attributes for callable
    1251                 :             :   FunctionType *FTy;
    1252                 :             : 
    1253                 :             :   template <class... ArgsTy>
    1254                 :             :   CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args)
    1255                 :             :       : Instruction(std::forward<ArgsTy>(Args)...), Attrs(A), FTy(FT) {}
    1256                 :             : 
    1257                 :             :   using Instruction::Instruction;
    1258                 :             : 
    1259                 :             :   bool hasDescriptor() const { return Value::HasDescriptor; }
    1260                 :             : 
    1261                 :             :   unsigned getNumSubclassExtraOperands() const {
    1262                 :             :     switch (getOpcode()) {
    1263                 :             :     case Instruction::Call:
    1264                 :             :       return 0;
    1265                 :             :     case Instruction::Invoke:
    1266                 :             :       return 2;
    1267                 :             :     case Instruction::CallBr:
    1268                 :             :       return getNumSubclassExtraOperandsDynamic();
    1269                 :             :     }
    1270                 :             :     llvm_unreachable("Invalid opcode!");
    1271                 :             :   }
    1272                 :             : 
    1273                 :             :   /// Get the number of extra operands for instructions that don't have a fixed
    1274                 :             :   /// number of extra operands.
    1275                 :             :   unsigned getNumSubclassExtraOperandsDynamic() const;
    1276                 :             : 
    1277                 :             : public:
    1278                 :             :   using Instruction::getContext;
    1279                 :             : 
    1280                 :             :   /// Create a clone of \p CB with a different set of operand bundles and
    1281                 :             :   /// insert it before \p InsertPt.
    1282                 :             :   ///
    1283                 :             :   /// The returned call instruction is identical \p CB in every way except that
    1284                 :             :   /// the operand bundles for the new instruction are set to the operand bundles
    1285                 :             :   /// in \p Bundles.
    1286                 :             :   static CallBase *Create(CallBase *CB, ArrayRef<OperandBundleDef> Bundles,
    1287                 :             :                           InsertPosition InsertPt = nullptr);
    1288                 :             : 
    1289                 :             :   /// Create a clone of \p CB with the operand bundle with the tag matching
    1290                 :             :   /// \p Bundle's tag replaced with Bundle, and insert it before \p InsertPt.
    1291                 :             :   ///
    1292                 :             :   /// The returned call instruction is identical \p CI in every way except that
    1293                 :             :   /// the specified operand bundle has been replaced.
    1294                 :             :   static CallBase *Create(CallBase *CB, OperandBundleDef Bundle,
    1295                 :             :                           InsertPosition InsertPt = nullptr);
    1296                 :             : 
    1297                 :             :   /// Create a clone of \p CB with operand bundle \p OB added.
    1298                 :             :   static CallBase *addOperandBundle(CallBase *CB, uint32_t ID,
    1299                 :             :                                     OperandBundleDef OB,
    1300                 :             :                                     InsertPosition InsertPt = nullptr);
    1301                 :             : 
    1302                 :             :   /// Create a clone of \p CB with operand bundle \p ID removed.
    1303                 :             :   static CallBase *removeOperandBundle(CallBase *CB, uint32_t ID,
    1304                 :             :                                        InsertPosition InsertPt = nullptr);
    1305                 :             : 
    1306                 :             :   /// Return the convergence control token for this call, if it exists.
    1307                 :             :   Value *getConvergenceControlToken() const {
    1308                 :             :     if (auto Bundle = getOperandBundle(llvm::LLVMContext::OB_convergencectrl)) {
    1309                 :             :       return Bundle->Inputs[0].get();
    1310                 :             :     }
    1311                 :             :     return nullptr;
    1312                 :             :   }
    1313                 :             : 
    1314                 :             :   static bool classof(const Instruction *I) {
    1315                 :             :     return I->getOpcode() == Instruction::Call ||
    1316                 :             :            I->getOpcode() == Instruction::Invoke ||
    1317                 :             :            I->getOpcode() == Instruction::CallBr;
    1318                 :             :   }
    1319                 :             :   static bool classof(const Value *V) {
    1320                 :             :     return isa<Instruction>(V) && classof(cast<Instruction>(V));
    1321                 :             :   }
    1322                 :             : 
    1323                 :           0 :   FunctionType *getFunctionType() const { return FTy; }
    1324                 :             : 
    1325                 :             :   void mutateFunctionType(FunctionType *FTy) {
    1326                 :             :     Value::mutateType(FTy->getReturnType());
    1327                 :             :     this->FTy = FTy;
    1328                 :             :   }
    1329                 :             : 
    1330                 :             :   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
    1331                 :             : 
    1332                 :             :   /// data_operands_begin/data_operands_end - Return iterators iterating over
    1333                 :             :   /// the call / invoke argument list and bundle operands.  For invokes, this is
    1334                 :             :   /// the set of instruction operands except the invoke target and the two
    1335                 :             :   /// successor blocks; and for calls this is the set of instruction operands
    1336                 :             :   /// except the call target.
    1337                 :             :   User::op_iterator data_operands_begin() { return op_begin(); }
    1338                 :             :   User::const_op_iterator data_operands_begin() const {
    1339                 :             :     return const_cast<CallBase *>(this)->data_operands_begin();
    1340                 :             :   }
    1341                 :             :   User::op_iterator data_operands_end() {
    1342                 :             :     // Walk from the end of the operands over the called operand and any
    1343                 :             :     // subclass operands.
    1344                 :             :     return op_end() - getNumSubclassExtraOperands() - 1;
    1345                 :             :   }
    1346                 :             :   User::const_op_iterator data_operands_end() const {
    1347                 :             :     return const_cast<CallBase *>(this)->data_operands_end();
    1348                 :             :   }
    1349                 :             :   iterator_range<User::op_iterator> data_ops() {
    1350                 :             :     return make_range(data_operands_begin(), data_operands_end());
    1351                 :             :   }
    1352                 :             :   iterator_range<User::const_op_iterator> data_ops() const {
    1353                 :             :     return make_range(data_operands_begin(), data_operands_end());
    1354                 :             :   }
    1355                 :             :   bool data_operands_empty() const {
    1356                 :             :     return data_operands_end() == data_operands_begin();
    1357                 :             :   }
    1358                 :             :   unsigned data_operands_size() const {
    1359                 :             :     return std::distance(data_operands_begin(), data_operands_end());
    1360                 :             :   }
    1361                 :             : 
    1362                 :             :   bool isDataOperand(const Use *U) const {
    1363                 :             :     assert(this == U->getUser() &&
    1364                 :             :            "Only valid to query with a use of this instruction!");
    1365                 :             :     return data_operands_begin() <= U && U < data_operands_end();
    1366                 :             :   }
    1367                 :             :   bool isDataOperand(Value::const_user_iterator UI) const {
    1368                 :             :     return isDataOperand(&UI.getUse());
    1369                 :             :   }
    1370                 :             : 
    1371                 :             :   /// Given a value use iterator, return the data operand corresponding to it.
    1372                 :             :   /// Iterator must actually correspond to a data operand.
    1373                 :             :   unsigned getDataOperandNo(Value::const_user_iterator UI) const {
    1374                 :             :     return getDataOperandNo(&UI.getUse());
    1375                 :             :   }
    1376                 :             : 
    1377                 :             :   /// Given a use for a data operand, get the data operand number that
    1378                 :             :   /// corresponds to it.
    1379                 :             :   unsigned getDataOperandNo(const Use *U) const {
    1380                 :             :     assert(isDataOperand(U) && "Data operand # out of range!");
    1381                 :             :     return U - data_operands_begin();
    1382                 :             :   }
    1383                 :             : 
    1384                 :             :   /// Return the iterator pointing to the beginning of the argument list.
    1385                 :             :   User::op_iterator arg_begin() { return op_begin(); }
    1386                 :             :   User::const_op_iterator arg_begin() const {
    1387                 :             :     return const_cast<CallBase *>(this)->arg_begin();
    1388                 :             :   }
    1389                 :             : 
    1390                 :             :   /// Return the iterator pointing to the end of the argument list.
    1391                 :             :   User::op_iterator arg_end() {
    1392                 :             :     // From the end of the data operands, walk backwards past the bundle
    1393                 :             :     // operands.
    1394                 :             :     return data_operands_end() - getNumTotalBundleOperands();
    1395                 :             :   }
    1396                 :             :   User::const_op_iterator arg_end() const {
    1397                 :             :     return const_cast<CallBase *>(this)->arg_end();
    1398                 :             :   }
    1399                 :             : 
    1400                 :             :   /// Iteration adapter for range-for loops.
    1401                 :             :   iterator_range<User::op_iterator> args() {
    1402                 :             :     return make_range(arg_begin(), arg_end());
    1403                 :             :   }
    1404                 :             :   iterator_range<User::const_op_iterator> args() const {
    1405                 :             :     return make_range(arg_begin(), arg_end());
    1406                 :             :   }
    1407                 :             :   bool arg_empty() const { return arg_end() == arg_begin(); }
    1408                 :             :   unsigned arg_size() const { return arg_end() - arg_begin(); }
    1409                 :             : 
    1410                 :             :   Value *getArgOperand(unsigned i) const {
    1411                 :             :     assert(i < arg_size() && "Out of bounds!");
    1412                 :             :     return getOperand(i);
    1413                 :             :   }
    1414                 :             : 
    1415                 :             :   void setArgOperand(unsigned i, Value *v) {
    1416                 :             :     assert(i < arg_size() && "Out of bounds!");
    1417                 :             :     setOperand(i, v);
    1418                 :             :   }
    1419                 :             : 
    1420                 :             :   /// Wrappers for getting the \c Use of a call argument.
    1421                 :             :   const Use &getArgOperandUse(unsigned i) const {
    1422                 :             :     assert(i < arg_size() && "Out of bounds!");
    1423                 :             :     return User::getOperandUse(i);
    1424                 :             :   }
    1425                 :             :   Use &getArgOperandUse(unsigned i) {
    1426                 :             :     assert(i < arg_size() && "Out of bounds!");
    1427                 :             :     return User::getOperandUse(i);
    1428                 :             :   }
    1429                 :             : 
    1430                 :             :   bool isArgOperand(const Use *U) const {
    1431                 :             :     assert(this == U->getUser() &&
    1432                 :             :            "Only valid to query with a use of this instruction!");
    1433                 :             :     return arg_begin() <= U && U < arg_end();
    1434                 :             :   }
    1435                 :             :   bool isArgOperand(Value::const_user_iterator UI) const {
    1436                 :             :     return isArgOperand(&UI.getUse());
    1437                 :             :   }
    1438                 :             : 
    1439                 :             :   /// Given a use for a arg operand, get the arg operand number that
    1440                 :             :   /// corresponds to it.
    1441                 :             :   unsigned getArgOperandNo(const Use *U) const {
    1442                 :             :     assert(isArgOperand(U) && "Arg operand # out of range!");
    1443                 :             :     return U - arg_begin();
    1444                 :             :   }
    1445                 :             : 
    1446                 :             :   /// Given a value use iterator, return the arg operand number corresponding to
    1447                 :             :   /// it. Iterator must actually correspond to a data operand.
    1448                 :             :   unsigned getArgOperandNo(Value::const_user_iterator UI) const {
    1449                 :             :     return getArgOperandNo(&UI.getUse());
    1450                 :             :   }
    1451                 :             : 
    1452                 :             :   /// Returns true if this CallSite passes the given Value* as an argument to
    1453                 :             :   /// the called function.
    1454                 :             :   bool hasArgument(const Value *V) const {
    1455                 :             :     return llvm::is_contained(args(), V);
    1456                 :             :   }
    1457                 :             : 
    1458                 :           0 :   Value *getCalledOperand() const { return Op<CalledOperandOpEndIdx>(); }
    1459                 :             : 
    1460                 :             :   const Use &getCalledOperandUse() const { return Op<CalledOperandOpEndIdx>(); }
    1461                 :             :   Use &getCalledOperandUse() { return Op<CalledOperandOpEndIdx>(); }
    1462                 :             : 
    1463                 :             :   /// Returns the function called, or null if this is an indirect function
    1464                 :             :   /// invocation or the function signature does not match the call signature.
    1465                 :           0 :   Function *getCalledFunction() const {
    1466         [ #  # ]:           0 :     if (auto *F = dyn_cast_or_null<Function>(getCalledOperand()))
    1467         [ #  # ]:           0 :       if (F->getValueType() == getFunctionType())
    1468                 :           0 :         return F;
    1469                 :           0 :     return nullptr;
    1470                 :             :   }
    1471                 :             : 
    1472                 :             :   /// Return true if the callsite is an indirect call.
    1473                 :             :   bool isIndirectCall() const;
    1474                 :             : 
    1475                 :             :   /// Determine whether the passed iterator points to the callee operand's Use.
    1476                 :             :   bool isCallee(Value::const_user_iterator UI) const {
    1477                 :             :     return isCallee(&UI.getUse());
    1478                 :             :   }
    1479                 :             : 
    1480                 :             :   /// Determine whether this Use is the callee operand's Use.
    1481                 :             :   bool isCallee(const Use *U) const { return &getCalledOperandUse() == U; }
    1482                 :             : 
    1483                 :             :   /// Helper to get the caller (the parent function).
    1484                 :             :   Function *getCaller();
    1485                 :             :   const Function *getCaller() const {
    1486                 :             :     return const_cast<CallBase *>(this)->getCaller();
    1487                 :             :   }
    1488                 :             : 
    1489                 :             :   /// Tests if this call site must be tail call optimized. Only a CallInst can
    1490                 :             :   /// be tail call optimized.
    1491                 :             :   bool isMustTailCall() const;
    1492                 :             : 
    1493                 :             :   /// Tests if this call site is marked as a tail call.
    1494                 :             :   bool isTailCall() const;
    1495                 :             : 
    1496                 :             :   /// Returns the intrinsic ID of the intrinsic called or
    1497                 :             :   /// Intrinsic::not_intrinsic if the called function is not an intrinsic, or if
    1498                 :             :   /// this is an indirect call.
    1499                 :             :   Intrinsic::ID getIntrinsicID() const;
    1500                 :             : 
    1501                 :             :   void setCalledOperand(Value *V) { Op<CalledOperandOpEndIdx>() = V; }
    1502                 :             : 
    1503                 :             :   /// Sets the function called, including updating the function type.
    1504                 :             :   void setCalledFunction(Function *Fn) {
    1505                 :             :     setCalledFunction(Fn->getFunctionType(), Fn);
    1506                 :             :   }
    1507                 :             : 
    1508                 :             :   /// Sets the function called, including updating the function type.
    1509                 :             :   void setCalledFunction(FunctionCallee Fn) {
    1510                 :             :     setCalledFunction(Fn.getFunctionType(), Fn.getCallee());
    1511                 :             :   }
    1512                 :             : 
    1513                 :             :   /// Sets the function called, including updating to the specified function
    1514                 :             :   /// type.
    1515                 :             :   void setCalledFunction(FunctionType *FTy, Value *Fn) {
    1516                 :             :     this->FTy = FTy;
    1517                 :             :     // This function doesn't mutate the return type, only the function
    1518                 :             :     // type. Seems broken, but I'm just gonna stick an assert in for now.
    1519                 :             :     assert(getType() == FTy->getReturnType());
    1520                 :             :     setCalledOperand(Fn);
    1521                 :             :   }
    1522                 :             : 
    1523                 :             :   CallingConv::ID getCallingConv() const {
    1524                 :             :     return getSubclassData<CallingConvField>();
    1525                 :             :   }
    1526                 :             : 
    1527                 :             :   void setCallingConv(CallingConv::ID CC) {
    1528                 :             :     setSubclassData<CallingConvField>(CC);
    1529                 :             :   }
    1530                 :             : 
    1531                 :             :   /// Check if this call is an inline asm statement.
    1532                 :             :   bool isInlineAsm() const { return isa<InlineAsm>(getCalledOperand()); }
    1533                 :             : 
    1534                 :             :   /// \name Attribute API
    1535                 :             :   ///
    1536                 :             :   /// These methods access and modify attributes on this call (including
    1537                 :             :   /// looking through to the attributes on the called function when necessary).
    1538                 :             :   ///@{
    1539                 :             : 
    1540                 :             :   /// Return the parameter attributes for this call.
    1541                 :             :   ///
    1542                 :             :   AttributeList getAttributes() const { return Attrs; }
    1543                 :             : 
    1544                 :             :   /// Set the parameter attributes for this call.
    1545                 :             :   ///
    1546                 :             :   void setAttributes(AttributeList A) { Attrs = A; }
    1547                 :             : 
    1548                 :             :   /// Determine whether this call has the given attribute. If it does not
    1549                 :             :   /// then determine if the called function has the attribute, but only if
    1550                 :             :   /// the attribute is allowed for the call.
    1551                 :             :   bool hasFnAttr(Attribute::AttrKind Kind) const {
    1552                 :             :     assert(Kind != Attribute::NoBuiltin &&
    1553                 :             :            "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin");
    1554                 :             :     return hasFnAttrImpl(Kind);
    1555                 :             :   }
    1556                 :             : 
    1557                 :             :   /// Determine whether this call has the given attribute. If it does not
    1558                 :             :   /// then determine if the called function has the attribute, but only if
    1559                 :             :   /// the attribute is allowed for the call.
    1560                 :             :   bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); }
    1561                 :             : 
    1562                 :             :   // TODO: remove non-AtIndex versions of these methods.
    1563                 :             :   /// adds the attribute to the list of attributes.
    1564                 :             :   void addAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) {
    1565                 :             :     Attrs = Attrs.addAttributeAtIndex(getContext(), i, Kind);
    1566                 :             :   }
    1567                 :             : 
    1568                 :             :   /// adds the attribute to the list of attributes.
    1569                 :             :   void addAttributeAtIndex(unsigned i, Attribute Attr) {
    1570                 :             :     Attrs = Attrs.addAttributeAtIndex(getContext(), i, Attr);
    1571                 :             :   }
    1572                 :             : 
    1573                 :             :   /// Adds the attribute to the function.
    1574                 :           0 :   void addFnAttr(Attribute::AttrKind Kind) {
    1575                 :           0 :     Attrs = Attrs.addFnAttribute(getContext(), Kind);
    1576                 :           0 :   }
    1577                 :             : 
    1578                 :             :   /// Adds the attribute to the function.
    1579                 :             :   void addFnAttr(Attribute Attr) {
    1580                 :             :     Attrs = Attrs.addFnAttribute(getContext(), Attr);
    1581                 :             :   }
    1582                 :             : 
    1583                 :             :   /// Adds the attribute to the return value.
    1584                 :             :   void addRetAttr(Attribute::AttrKind Kind) {
    1585                 :             :     Attrs = Attrs.addRetAttribute(getContext(), Kind);
    1586                 :             :   }
    1587                 :             : 
    1588                 :             :   /// Adds the attribute to the return value.
    1589                 :             :   void addRetAttr(Attribute Attr) {
    1590                 :             :     Attrs = Attrs.addRetAttribute(getContext(), Attr);
    1591                 :             :   }
    1592                 :             : 
    1593                 :             :   /// Adds the attribute to the indicated argument
    1594                 :             :   void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
    1595                 :             :     assert(ArgNo < arg_size() && "Out of bounds");
    1596                 :             :     Attrs = Attrs.addParamAttribute(getContext(), ArgNo, Kind);
    1597                 :             :   }
    1598                 :             : 
    1599                 :             :   /// Adds the attribute to the indicated argument
    1600                 :             :   void addParamAttr(unsigned ArgNo, Attribute Attr) {
    1601                 :             :     assert(ArgNo < arg_size() && "Out of bounds");
    1602                 :             :     Attrs = Attrs.addParamAttribute(getContext(), ArgNo, Attr);
    1603                 :             :   }
    1604                 :             : 
    1605                 :             :   /// removes the attribute from the list of attributes.
    1606                 :             :   void removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) {
    1607                 :             :     Attrs = Attrs.removeAttributeAtIndex(getContext(), i, Kind);
    1608                 :             :   }
    1609                 :             : 
    1610                 :             :   /// removes the attribute from the list of attributes.
    1611                 :             :   void removeAttributeAtIndex(unsigned i, StringRef Kind) {
    1612                 :             :     Attrs = Attrs.removeAttributeAtIndex(getContext(), i, Kind);
    1613                 :             :   }
    1614                 :             : 
    1615                 :             :   /// Removes the attributes from the function
    1616                 :             :   void removeFnAttrs(const AttributeMask &AttrsToRemove) {
    1617                 :             :     Attrs = Attrs.removeFnAttributes(getContext(), AttrsToRemove);
    1618                 :             :   }
    1619                 :             : 
    1620                 :             :   /// Removes the attribute from the function
    1621                 :             :   void removeFnAttr(Attribute::AttrKind Kind) {
    1622                 :             :     Attrs = Attrs.removeFnAttribute(getContext(), Kind);
    1623                 :             :   }
    1624                 :             : 
    1625                 :             :   /// Removes the attribute from the function
    1626                 :             :   void removeFnAttr(StringRef Kind) {
    1627                 :             :     Attrs = Attrs.removeFnAttribute(getContext(), Kind);
    1628                 :             :   }
    1629                 :             : 
    1630                 :             :   /// Removes the attribute from the return value
    1631                 :             :   void removeRetAttr(Attribute::AttrKind Kind) {
    1632                 :             :     Attrs = Attrs.removeRetAttribute(getContext(), Kind);
    1633                 :             :   }
    1634                 :             : 
    1635                 :             :   /// Removes the attributes from the return value
    1636                 :             :   void removeRetAttrs(const AttributeMask &AttrsToRemove) {
    1637                 :             :     Attrs = Attrs.removeRetAttributes(getContext(), AttrsToRemove);
    1638                 :             :   }
    1639                 :             : 
    1640                 :             :   /// Removes the attribute from the given argument
    1641                 :             :   void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
    1642                 :             :     assert(ArgNo < arg_size() && "Out of bounds");
    1643                 :             :     Attrs = Attrs.removeParamAttribute(getContext(), ArgNo, Kind);
    1644                 :             :   }
    1645                 :             : 
    1646                 :             :   /// Removes the attribute from the given argument
    1647                 :             :   void removeParamAttr(unsigned ArgNo, StringRef Kind) {
    1648                 :             :     assert(ArgNo < arg_size() && "Out of bounds");
    1649                 :             :     Attrs = Attrs.removeParamAttribute(getContext(), ArgNo, Kind);
    1650                 :             :   }
    1651                 :             : 
    1652                 :             :   /// Removes the attributes from the given argument
    1653                 :             :   void removeParamAttrs(unsigned ArgNo, const AttributeMask &AttrsToRemove) {
    1654                 :             :     Attrs = Attrs.removeParamAttributes(getContext(), ArgNo, AttrsToRemove);
    1655                 :             :   }
    1656                 :             : 
    1657                 :             :   /// adds the dereferenceable attribute to the list of attributes.
    1658                 :             :   void addDereferenceableParamAttr(unsigned i, uint64_t Bytes) {
    1659                 :             :     Attrs = Attrs.addDereferenceableParamAttr(getContext(), i, Bytes);
    1660                 :             :   }
    1661                 :             : 
    1662                 :             :   /// adds the dereferenceable attribute to the list of attributes.
    1663                 :             :   void addDereferenceableRetAttr(uint64_t Bytes) {
    1664                 :             :     Attrs = Attrs.addDereferenceableRetAttr(getContext(), Bytes);
    1665                 :             :   }
    1666                 :             : 
    1667                 :             :   /// adds the range attribute to the list of attributes.
    1668                 :             :   void addRangeRetAttr(const ConstantRange &CR) {
    1669                 :             :     Attrs = Attrs.addRangeRetAttr(getContext(), CR);
    1670                 :             :   }
    1671                 :             : 
    1672                 :             :   /// Determine whether the return value has the given attribute.
    1673                 :             :   bool hasRetAttr(Attribute::AttrKind Kind) const {
    1674                 :             :     return hasRetAttrImpl(Kind);
    1675                 :             :   }
    1676                 :             :   /// Determine whether the return value has the given attribute.
    1677                 :             :   bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); }
    1678                 :             : 
    1679                 :             :   /// Return the attribute for the given attribute kind for the return value.
    1680                 :             :   Attribute getRetAttr(Attribute::AttrKind Kind) const {
    1681                 :             :     Attribute RetAttr = Attrs.getRetAttr(Kind);
    1682                 :             :     if (RetAttr.isValid())
    1683                 :             :       return RetAttr;
    1684                 :             : 
    1685                 :             :     // Look at the callee, if available.
    1686                 :             :     if (const Function *F = getCalledFunction())
    1687                 :             :       return F->getRetAttribute(Kind);
    1688                 :             :     return Attribute();
    1689                 :             :   }
    1690                 :             : 
    1691                 :             :   /// Determine whether the argument or parameter has the given attribute.
    1692                 :             :   bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const;
    1693                 :             : 
    1694                 :             :   /// Get the attribute of a given kind at a position.
    1695                 :             :   Attribute getAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) const {
    1696                 :             :     return getAttributes().getAttributeAtIndex(i, Kind);
    1697                 :             :   }
    1698                 :             : 
    1699                 :             :   /// Get the attribute of a given kind at a position.
    1700                 :             :   Attribute getAttributeAtIndex(unsigned i, StringRef Kind) const {
    1701                 :             :     return getAttributes().getAttributeAtIndex(i, Kind);
    1702                 :             :   }
    1703                 :             : 
    1704                 :             :   /// Get the attribute of a given kind for the function.
    1705                 :             :   Attribute getFnAttr(StringRef Kind) const {
    1706                 :             :     Attribute Attr = getAttributes().getFnAttr(Kind);
    1707                 :             :     if (Attr.isValid())
    1708                 :             :       return Attr;
    1709                 :             :     return getFnAttrOnCalledFunction(Kind);
    1710                 :             :   }
    1711                 :             : 
    1712                 :             :   /// Get the attribute of a given kind for the function.
    1713                 :             :   Attribute getFnAttr(Attribute::AttrKind Kind) const {
    1714                 :             :     Attribute A = getAttributes().getFnAttr(Kind);
    1715                 :             :     if (A.isValid())
    1716                 :             :       return A;
    1717                 :             :     return getFnAttrOnCalledFunction(Kind);
    1718                 :             :   }
    1719                 :             : 
    1720                 :             :   /// Get the attribute of a given kind from a given arg
    1721                 :             :   Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
    1722                 :             :     assert(ArgNo < arg_size() && "Out of bounds");
    1723                 :             :     Attribute A = getAttributes().getParamAttr(ArgNo, Kind);
    1724                 :             :     if (A.isValid())
    1725                 :             :       return A;
    1726                 :             :     return getParamAttrOnCalledFunction(ArgNo, Kind);
    1727                 :             :   }
    1728                 :             : 
    1729                 :             :   /// Get the attribute of a given kind from a given arg
    1730                 :             :   Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
    1731                 :             :     assert(ArgNo < arg_size() && "Out of bounds");
    1732                 :             :     Attribute A = getAttributes().getParamAttr(ArgNo, Kind);
    1733                 :             :     if (A.isValid())
    1734                 :             :       return A;
    1735                 :             :     return getParamAttrOnCalledFunction(ArgNo, Kind);
    1736                 :             :   }
    1737                 :             : 
    1738                 :             :   /// Return true if the data operand at index \p i has the attribute \p
    1739                 :             :   /// A.
    1740                 :             :   ///
    1741                 :             :   /// Data operands include call arguments and values used in operand bundles,
    1742                 :             :   /// but does not include the callee operand.
    1743                 :             :   ///
    1744                 :             :   /// The index \p i is interpreted as
    1745                 :             :   ///
    1746                 :             :   ///  \p i in [0, arg_size)  -> argument number (\p i)
    1747                 :             :   ///  \p i in [arg_size, data_operand_size) -> bundle operand at index
    1748                 :             :   ///     (\p i) in the operand list.
    1749                 :             :   bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
    1750                 :             :     // Note that we have to add one because `i` isn't zero-indexed.
    1751                 :             :     assert(i < arg_size() + getNumTotalBundleOperands() &&
    1752                 :             :            "Data operand index out of bounds!");
    1753                 :             : 
    1754                 :             :     // The attribute A can either be directly specified, if the operand in
    1755                 :             :     // question is a call argument; or be indirectly implied by the kind of its
    1756                 :             :     // containing operand bundle, if the operand is a bundle operand.
    1757                 :             : 
    1758                 :             :     if (i < arg_size())
    1759                 :             :       return paramHasAttr(i, Kind);
    1760                 :             : 
    1761                 :             :     assert(hasOperandBundles() && i >= getBundleOperandsStartIndex() &&
    1762                 :             :            "Must be either a call argument or an operand bundle!");
    1763                 :             :     return bundleOperandHasAttr(i, Kind);
    1764                 :             :   }
    1765                 :             : 
    1766                 :             :   /// Determine whether this data operand is not captured.
    1767                 :             :   // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to
    1768                 :             :   // better indicate that this may return a conservative answer.
    1769                 :             :   bool doesNotCapture(unsigned OpNo) const {
    1770                 :             :     return dataOperandHasImpliedAttr(OpNo, Attribute::NoCapture);
    1771                 :             :   }
    1772                 :             : 
    1773                 :             :   /// Determine whether this argument is passed by value.
    1774                 :             :   bool isByValArgument(unsigned ArgNo) const {
    1775                 :             :     return paramHasAttr(ArgNo, Attribute::ByVal);
    1776                 :             :   }
    1777                 :             : 
    1778                 :             :   /// Determine whether this argument is passed in an alloca.
    1779                 :             :   bool isInAllocaArgument(unsigned ArgNo) const {
    1780                 :             :     return paramHasAttr(ArgNo, Attribute::InAlloca);
    1781                 :             :   }
    1782                 :             : 
    1783                 :             :   /// Determine whether this argument is passed by value, in an alloca, or is
    1784                 :             :   /// preallocated.
    1785                 :             :   bool isPassPointeeByValueArgument(unsigned ArgNo) const {
    1786                 :             :     return paramHasAttr(ArgNo, Attribute::ByVal) ||
    1787                 :             :            paramHasAttr(ArgNo, Attribute::InAlloca) ||
    1788                 :             :            paramHasAttr(ArgNo, Attribute::Preallocated);
    1789                 :             :   }
    1790                 :             : 
    1791                 :             :   /// Determine whether passing undef to this argument is undefined behavior.
    1792                 :             :   /// If passing undef to this argument is UB, passing poison is UB as well
    1793                 :             :   /// because poison is more undefined than undef.
    1794                 :             :   bool isPassingUndefUB(unsigned ArgNo) const {
    1795                 :             :     return paramHasAttr(ArgNo, Attribute::NoUndef) ||
    1796                 :             :            // dereferenceable implies noundef.
    1797                 :             :            paramHasAttr(ArgNo, Attribute::Dereferenceable) ||
    1798                 :             :            // dereferenceable implies noundef, and null is a well-defined value.
    1799                 :             :            paramHasAttr(ArgNo, Attribute::DereferenceableOrNull);
    1800                 :             :   }
    1801                 :             : 
    1802                 :             :   /// Determine if there are is an inalloca argument. Only the last argument can
    1803                 :             :   /// have the inalloca attribute.
    1804                 :             :   bool hasInAllocaArgument() const {
    1805                 :             :     return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca);
    1806                 :             :   }
    1807                 :             : 
    1808                 :             :   // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to
    1809                 :             :   // better indicate that this may return a conservative answer.
    1810                 :             :   bool doesNotAccessMemory(unsigned OpNo) const {
    1811                 :             :     return dataOperandHasImpliedAttr(OpNo, Attribute::ReadNone);
    1812                 :             :   }
    1813                 :             : 
    1814                 :             :   // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to
    1815                 :             :   // better indicate that this may return a conservative answer.
    1816                 :             :   bool onlyReadsMemory(unsigned OpNo) const {
    1817                 :             :     return dataOperandHasImpliedAttr(OpNo, Attribute::ReadOnly) ||
    1818                 :             :            dataOperandHasImpliedAttr(OpNo, Attribute::ReadNone);
    1819                 :             :   }
    1820                 :             : 
    1821                 :             :   // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to
    1822                 :             :   // better indicate that this may return a conservative answer.
    1823                 :             :   bool onlyWritesMemory(unsigned OpNo) const {
    1824                 :             :     return dataOperandHasImpliedAttr(OpNo, Attribute::WriteOnly) ||
    1825                 :             :            dataOperandHasImpliedAttr(OpNo, Attribute::ReadNone);
    1826                 :             :   }
    1827                 :             : 
    1828                 :             :   /// Extract the alignment of the return value.
    1829                 :             :   MaybeAlign getRetAlign() const {
    1830                 :             :     if (auto Align = Attrs.getRetAlignment())
    1831                 :             :       return Align;
    1832                 :             :     if (const Function *F = getCalledFunction())
    1833                 :             :       return F->getAttributes().getRetAlignment();
    1834                 :             :     return std::nullopt;
    1835                 :             :   }
    1836                 :             : 
    1837                 :             :   /// Extract the alignment for a call or parameter (0=unknown).
    1838                 :             :   MaybeAlign getParamAlign(unsigned ArgNo) const {
    1839                 :             :     return Attrs.getParamAlignment(ArgNo);
    1840                 :             :   }
    1841                 :             : 
    1842                 :             :   MaybeAlign getParamStackAlign(unsigned ArgNo) const {
    1843                 :             :     return Attrs.getParamStackAlignment(ArgNo);
    1844                 :             :   }
    1845                 :             : 
    1846                 :             :   /// Extract the byref type for a call or parameter.
    1847                 :             :   Type *getParamByRefType(unsigned ArgNo) const {
    1848                 :             :     if (auto *Ty = Attrs.getParamByRefType(ArgNo))
    1849                 :             :       return Ty;
    1850                 :             :     if (const Function *F = getCalledFunction())
    1851                 :             :       return F->getAttributes().getParamByRefType(ArgNo);
    1852                 :             :     return nullptr;
    1853                 :             :   }
    1854                 :             : 
    1855                 :             :   /// Extract the byval type for a call or parameter.
    1856                 :             :   Type *getParamByValType(unsigned ArgNo) const {
    1857                 :             :     if (auto *Ty = Attrs.getParamByValType(ArgNo))
    1858                 :             :       return Ty;
    1859                 :             :     if (const Function *F = getCalledFunction())
    1860                 :             :       return F->getAttributes().getParamByValType(ArgNo);
    1861                 :             :     return nullptr;
    1862                 :             :   }
    1863                 :             : 
    1864                 :             :   /// Extract the preallocated type for a call or parameter.
    1865                 :             :   Type *getParamPreallocatedType(unsigned ArgNo) const {
    1866                 :             :     if (auto *Ty = Attrs.getParamPreallocatedType(ArgNo))
    1867                 :             :       return Ty;
    1868                 :             :     if (const Function *F = getCalledFunction())
    1869                 :             :       return F->getAttributes().getParamPreallocatedType(ArgNo);
    1870                 :             :     return nullptr;
    1871                 :             :   }
    1872                 :             : 
    1873                 :             :   /// Extract the inalloca type for a call or parameter.
    1874                 :             :   Type *getParamInAllocaType(unsigned ArgNo) const {
    1875                 :             :     if (auto *Ty = Attrs.getParamInAllocaType(ArgNo))
    1876                 :             :       return Ty;
    1877                 :             :     if (const Function *F = getCalledFunction())
    1878                 :             :       return F->getAttributes().getParamInAllocaType(ArgNo);
    1879                 :             :     return nullptr;
    1880                 :             :   }
    1881                 :             : 
    1882                 :             :   /// Extract the sret type for a call or parameter.
    1883                 :             :   Type *getParamStructRetType(unsigned ArgNo) const {
    1884                 :             :     if (auto *Ty = Attrs.getParamStructRetType(ArgNo))
    1885                 :             :       return Ty;
    1886                 :             :     if (const Function *F = getCalledFunction())
    1887                 :             :       return F->getAttributes().getParamStructRetType(ArgNo);
    1888                 :             :     return nullptr;
    1889                 :             :   }
    1890                 :             : 
    1891                 :             :   /// Extract the elementtype type for a parameter.
    1892                 :             :   /// Note that elementtype() can only be applied to call arguments, not
    1893                 :             :   /// function declaration parameters.
    1894                 :             :   Type *getParamElementType(unsigned ArgNo) const {
    1895                 :             :     return Attrs.getParamElementType(ArgNo);
    1896                 :             :   }
    1897                 :             : 
    1898                 :             :   /// Extract the number of dereferenceable bytes for a call or
    1899                 :             :   /// parameter (0=unknown).
    1900                 :             :   uint64_t getRetDereferenceableBytes() const {
    1901                 :             :     uint64_t Bytes = Attrs.getRetDereferenceableBytes();
    1902                 :             :     if (const Function *F = getCalledFunction())
    1903                 :             :       Bytes = std::max(Bytes, F->getAttributes().getRetDereferenceableBytes());
    1904                 :             :     return Bytes;
    1905                 :             :   }
    1906                 :             : 
    1907                 :             :   /// Extract the number of dereferenceable bytes for a call or
    1908                 :             :   /// parameter (0=unknown).
    1909                 :             :   uint64_t getParamDereferenceableBytes(unsigned i) const {
    1910                 :             :     return Attrs.getParamDereferenceableBytes(i);
    1911                 :             :   }
    1912                 :             : 
    1913                 :             :   /// Extract the number of dereferenceable_or_null bytes for a call
    1914                 :             :   /// (0=unknown).
    1915                 :             :   uint64_t getRetDereferenceableOrNullBytes() const {
    1916                 :             :     uint64_t Bytes = Attrs.getRetDereferenceableOrNullBytes();
    1917                 :             :     if (const Function *F = getCalledFunction()) {
    1918                 :             :       Bytes = std::max(Bytes,
    1919                 :             :                        F->getAttributes().getRetDereferenceableOrNullBytes());
    1920                 :             :     }
    1921                 :             : 
    1922                 :             :     return Bytes;
    1923                 :             :   }
    1924                 :             : 
    1925                 :             :   /// Extract the number of dereferenceable_or_null bytes for a
    1926                 :             :   /// parameter (0=unknown).
    1927                 :             :   uint64_t getParamDereferenceableOrNullBytes(unsigned i) const {
    1928                 :             :     return Attrs.getParamDereferenceableOrNullBytes(i);
    1929                 :             :   }
    1930                 :             : 
    1931                 :             :   /// Extract a test mask for disallowed floating-point value classes for the
    1932                 :             :   /// return value.
    1933                 :             :   FPClassTest getRetNoFPClass() const;
    1934                 :             : 
    1935                 :             :   /// Extract a test mask for disallowed floating-point value classes for the
    1936                 :             :   /// parameter.
    1937                 :             :   FPClassTest getParamNoFPClass(unsigned i) const;
    1938                 :             : 
    1939                 :             :   /// If this return value has a range attribute, return the value range of the
    1940                 :             :   /// argument. Otherwise, std::nullopt is returned.
    1941                 :             :   std::optional<ConstantRange> getRange() const;
    1942                 :             : 
    1943                 :             :   /// Return true if the return value is known to be not null.
    1944                 :             :   /// This may be because it has the nonnull attribute, or because at least
    1945                 :             :   /// one byte is dereferenceable and the pointer is in addrspace(0).
    1946                 :             :   bool isReturnNonNull() const;
    1947                 :             : 
    1948                 :             :   /// Determine if the return value is marked with NoAlias attribute.
    1949                 :             :   bool returnDoesNotAlias() const {
    1950                 :             :     return Attrs.hasRetAttr(Attribute::NoAlias);
    1951                 :             :   }
    1952                 :             : 
    1953                 :             :   /// If one of the arguments has the 'returned' attribute, returns its
    1954                 :             :   /// operand value. Otherwise, return nullptr.
    1955                 :             :   Value *getReturnedArgOperand() const {
    1956                 :             :     return getArgOperandWithAttribute(Attribute::Returned);
    1957                 :             :   }
    1958                 :             : 
    1959                 :             :   /// If one of the arguments has the specified attribute, returns its
    1960                 :             :   /// operand value. Otherwise, return nullptr.
    1961                 :             :   Value *getArgOperandWithAttribute(Attribute::AttrKind Kind) const;
    1962                 :             : 
    1963                 :             :   /// Return true if the call should not be treated as a call to a
    1964                 :             :   /// builtin.
    1965                 :             :   bool isNoBuiltin() const {
    1966                 :             :     return hasFnAttrImpl(Attribute::NoBuiltin) &&
    1967                 :             :            !hasFnAttrImpl(Attribute::Builtin);
    1968                 :             :   }
    1969                 :             : 
    1970                 :             :   /// Determine if the call requires strict floating point semantics.
    1971                 :             :   bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); }
    1972                 :             : 
    1973                 :             :   /// Return true if the call should not be inlined.
    1974                 :             :   bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
    1975                 :             :   void setIsNoInline() { addFnAttr(Attribute::NoInline); }
    1976                 :             : 
    1977                 :             :   MemoryEffects getMemoryEffects() const;
    1978                 :             :   void setMemoryEffects(MemoryEffects ME);
    1979                 :             : 
    1980                 :             :   /// Determine if the call does not access memory.
    1981                 :             :   bool doesNotAccessMemory() const;
    1982                 :             :   void setDoesNotAccessMemory();
    1983                 :             : 
    1984                 :             :   /// Determine if the call does not access or only reads memory.
    1985                 :             :   bool onlyReadsMemory() const;
    1986                 :             :   void setOnlyReadsMemory();
    1987                 :             : 
    1988                 :             :   /// Determine if the call does not access or only writes memory.
    1989                 :             :   bool onlyWritesMemory() const;
    1990                 :             :   void setOnlyWritesMemory();
    1991                 :             : 
    1992                 :             :   /// Determine if the call can access memmory only using pointers based
    1993                 :             :   /// on its arguments.
    1994                 :             :   bool onlyAccessesArgMemory() const;
    1995                 :             :   void setOnlyAccessesArgMemory();
    1996                 :             : 
    1997                 :             :   /// Determine if the function may only access memory that is
    1998                 :             :   /// inaccessible from the IR.
    1999                 :             :   bool onlyAccessesInaccessibleMemory() const;
    2000                 :             :   void setOnlyAccessesInaccessibleMemory();
    2001                 :             : 
    2002                 :             :   /// Determine if the function may only access memory that is
    2003                 :             :   /// either inaccessible from the IR or pointed to by its arguments.
    2004                 :             :   bool onlyAccessesInaccessibleMemOrArgMem() const;
    2005                 :             :   void setOnlyAccessesInaccessibleMemOrArgMem();
    2006                 :             : 
    2007                 :             :   /// Determine if the call cannot return.
    2008                 :             :   bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
    2009                 :             :   void setDoesNotReturn() { addFnAttr(Attribute::NoReturn); }
    2010                 :             : 
    2011                 :             :   /// Determine if the call should not perform indirect branch tracking.
    2012                 :             :   bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
    2013                 :             : 
    2014                 :             :   /// Determine if the call cannot unwind.
    2015                 :             :   bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
    2016                 :             :   void setDoesNotThrow() { addFnAttr(Attribute::NoUnwind); }
    2017                 :             : 
    2018                 :             :   /// Determine if the invoke cannot be duplicated.
    2019                 :             :   bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); }
    2020                 :             :   void setCannotDuplicate() { addFnAttr(Attribute::NoDuplicate); }
    2021                 :             : 
    2022                 :             :   /// Determine if the call cannot be tail merged.
    2023                 :             :   bool cannotMerge() const { return hasFnAttr(Attribute::NoMerge); }
    2024                 :             :   void setCannotMerge() { addFnAttr(Attribute::NoMerge); }
    2025                 :             : 
    2026                 :             :   /// Determine if the invoke is convergent
    2027                 :             :   bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
    2028                 :             :   void setConvergent() { addFnAttr(Attribute::Convergent); }
    2029                 :             :   void setNotConvergent() { removeFnAttr(Attribute::Convergent); }
    2030                 :             : 
    2031                 :             :   /// Determine if the call returns a structure through first
    2032                 :             :   /// pointer argument.
    2033                 :             :   bool hasStructRetAttr() const {
    2034                 :             :     if (arg_empty())
    2035                 :             :       return false;
    2036                 :             : 
    2037                 :             :     // Be friendly and also check the callee.
    2038                 :             :     return paramHasAttr(0, Attribute::StructRet);
    2039                 :             :   }
    2040                 :             : 
    2041                 :             :   /// Determine if any call argument is an aggregate passed by value.
    2042                 :             :   bool hasByValArgument() const {
    2043                 :             :     return Attrs.hasAttrSomewhere(Attribute::ByVal);
    2044                 :             :   }
    2045                 :             : 
    2046                 :             :   ///@}
    2047                 :             :   // End of attribute API.
    2048                 :             : 
    2049                 :             :   /// \name Operand Bundle API
    2050                 :             :   ///
    2051                 :             :   /// This group of methods provides the API to access and manipulate operand
    2052                 :             :   /// bundles on this call.
    2053                 :             :   /// @{
    2054                 :             : 
    2055                 :             :   /// Return the number of operand bundles associated with this User.
    2056                 :             :   unsigned getNumOperandBundles() const {
    2057                 :             :     return std::distance(bundle_op_info_begin(), bundle_op_info_end());
    2058                 :             :   }
    2059                 :             : 
    2060                 :             :   /// Return true if this User has any operand bundles.
    2061                 :             :   bool hasOperandBundles() const { return getNumOperandBundles() != 0; }
    2062                 :             : 
    2063                 :             :   /// Return the index of the first bundle operand in the Use array.
    2064                 :             :   unsigned getBundleOperandsStartIndex() const {
    2065                 :             :     assert(hasOperandBundles() && "Don't call otherwise!");
    2066                 :             :     return bundle_op_info_begin()->Begin;
    2067                 :             :   }
    2068                 :             : 
    2069                 :             :   /// Return the index of the last bundle operand in the Use array.
    2070                 :             :   unsigned getBundleOperandsEndIndex() const {
    2071                 :             :     assert(hasOperandBundles() && "Don't call otherwise!");
    2072                 :             :     return bundle_op_info_end()[-1].End;
    2073                 :             :   }
    2074                 :             : 
    2075                 :             :   /// Return true if the operand at index \p Idx is a bundle operand.
    2076                 :             :   bool isBundleOperand(unsigned Idx) const {
    2077                 :             :     return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() &&
    2078                 :             :            Idx < getBundleOperandsEndIndex();
    2079                 :             :   }
    2080                 :             : 
    2081                 :             :   /// Return true if the operand at index \p Idx is a bundle operand that has
    2082                 :             :   /// tag ID \p ID.
    2083                 :             :   bool isOperandBundleOfType(uint32_t ID, unsigned Idx) const {
    2084                 :             :     return isBundleOperand(Idx) &&
    2085                 :             :            getOperandBundleForOperand(Idx).getTagID() == ID;
    2086                 :             :   }
    2087                 :             : 
    2088                 :             :   /// Returns true if the use is a bundle operand.
    2089                 :             :   bool isBundleOperand(const Use *U) const {
    2090                 :             :     assert(this == U->getUser() &&
    2091                 :             :            "Only valid to query with a use of this instruction!");
    2092                 :             :     return hasOperandBundles() && isBundleOperand(U - op_begin());
    2093                 :             :   }
    2094                 :             :   bool isBundleOperand(Value::const_user_iterator UI) const {
    2095                 :             :     return isBundleOperand(&UI.getUse());
    2096                 :             :   }
    2097                 :             : 
    2098                 :             :   /// Return the total number operands (not operand bundles) used by
    2099                 :             :   /// every operand bundle in this OperandBundleUser.
    2100                 :             :   unsigned getNumTotalBundleOperands() const {
    2101                 :             :     if (!hasOperandBundles())
    2102                 :             :       return 0;
    2103                 :             : 
    2104                 :             :     unsigned Begin = getBundleOperandsStartIndex();
    2105                 :             :     unsigned End = getBundleOperandsEndIndex();
    2106                 :             : 
    2107                 :             :     assert(Begin <= End && "Should be!");
    2108                 :             :     return End - Begin;
    2109                 :             :   }
    2110                 :             : 
    2111                 :             :   /// Return the operand bundle at a specific index.
    2112                 :             :   OperandBundleUse getOperandBundleAt(unsigned Index) const {
    2113                 :             :     assert(Index < getNumOperandBundles() && "Index out of bounds!");
    2114                 :             :     return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index));
    2115                 :             :   }
    2116                 :             : 
    2117                 :             :   /// Return the number of operand bundles with the tag Name attached to
    2118                 :             :   /// this instruction.
    2119                 :             :   unsigned countOperandBundlesOfType(StringRef Name) const {
    2120                 :             :     unsigned Count = 0;
    2121                 :             :     for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
    2122                 :             :       if (getOperandBundleAt(i).getTagName() == Name)
    2123                 :             :         Count++;
    2124                 :             : 
    2125                 :             :     return Count;
    2126                 :             :   }
    2127                 :             : 
    2128                 :             :   /// Return the number of operand bundles with the tag ID attached to
    2129                 :             :   /// this instruction.
    2130                 :             :   unsigned countOperandBundlesOfType(uint32_t ID) const {
    2131                 :             :     unsigned Count = 0;
    2132                 :             :     for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
    2133                 :             :       if (getOperandBundleAt(i).getTagID() == ID)
    2134                 :             :         Count++;
    2135                 :             : 
    2136                 :             :     return Count;
    2137                 :             :   }
    2138                 :             : 
    2139                 :             :   /// Return an operand bundle by name, if present.
    2140                 :             :   ///
    2141                 :             :   /// It is an error to call this for operand bundle types that may have
    2142                 :             :   /// multiple instances of them on the same instruction.
    2143                 :             :   std::optional<OperandBundleUse> getOperandBundle(StringRef Name) const {
    2144                 :             :     assert(countOperandBundlesOfType(Name) < 2 && "Precondition violated!");
    2145                 :             : 
    2146                 :             :     for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
    2147                 :             :       OperandBundleUse U = getOperandBundleAt(i);
    2148                 :             :       if (U.getTagName() == Name)
    2149                 :             :         return U;
    2150                 :             :     }
    2151                 :             : 
    2152                 :             :     return std::nullopt;
    2153                 :             :   }
    2154                 :             : 
    2155                 :             :   /// Return an operand bundle by tag ID, if present.
    2156                 :             :   ///
    2157                 :             :   /// It is an error to call this for operand bundle types that may have
    2158                 :             :   /// multiple instances of them on the same instruction.
    2159                 :             :   std::optional<OperandBundleUse> getOperandBundle(uint32_t ID) const {
    2160                 :             :     assert(countOperandBundlesOfType(ID) < 2 && "Precondition violated!");
    2161                 :             : 
    2162                 :             :     for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
    2163                 :             :       OperandBundleUse U = getOperandBundleAt(i);
    2164                 :             :       if (U.getTagID() == ID)
    2165                 :             :         return U;
    2166                 :             :     }
    2167                 :             : 
    2168                 :             :     return std::nullopt;
    2169                 :             :   }
    2170                 :             : 
    2171                 :             :   /// Return the list of operand bundles attached to this instruction as
    2172                 :             :   /// a vector of OperandBundleDefs.
    2173                 :             :   ///
    2174                 :             :   /// This function copies the OperandBundeUse instances associated with this
    2175                 :             :   /// OperandBundleUser to a vector of OperandBundleDefs.  Note:
    2176                 :             :   /// OperandBundeUses and OperandBundleDefs are non-trivially *different*
    2177                 :             :   /// representations of operand bundles (see documentation above).
    2178                 :             :   void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const;
    2179                 :             : 
    2180                 :             :   /// Return the operand bundle for the operand at index OpIdx.
    2181                 :             :   ///
    2182                 :             :   /// It is an error to call this with an OpIdx that does not correspond to an
    2183                 :             :   /// bundle operand.
    2184                 :             :   OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const {
    2185                 :             :     return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx));
    2186                 :             :   }
    2187                 :             : 
    2188                 :             :   /// Return true if this operand bundle user has operand bundles that
    2189                 :             :   /// may read from the heap.
    2190                 :             :   bool hasReadingOperandBundles() const;
    2191                 :             : 
    2192                 :             :   /// Return true if this operand bundle user has operand bundles that
    2193                 :             :   /// may write to the heap.
    2194                 :             :   bool hasClobberingOperandBundles() const;
    2195                 :             : 
    2196                 :             :   /// Return true if the bundle operand at index \p OpIdx has the
    2197                 :             :   /// attribute \p A.
    2198                 :             :   bool bundleOperandHasAttr(unsigned OpIdx,  Attribute::AttrKind A) const {
    2199                 :             :     auto &BOI = getBundleOpInfoForOperand(OpIdx);
    2200                 :             :     auto OBU = operandBundleFromBundleOpInfo(BOI);
    2201                 :             :     return OBU.operandHasAttr(OpIdx - BOI.Begin, A);
    2202                 :             :   }
    2203                 :             : 
    2204                 :             :   /// Return true if \p Other has the same sequence of operand bundle
    2205                 :             :   /// tags with the same number of operands on each one of them as this
    2206                 :             :   /// OperandBundleUser.
    2207                 :             :   bool hasIdenticalOperandBundleSchema(const CallBase &Other) const {
    2208                 :             :     if (getNumOperandBundles() != Other.getNumOperandBundles())
    2209                 :             :       return false;
    2210                 :             : 
    2211                 :             :     return std::equal(bundle_op_info_begin(), bundle_op_info_end(),
    2212                 :             :                       Other.bundle_op_info_begin());
    2213                 :             :   }
    2214                 :             : 
    2215                 :             :   /// Return true if this operand bundle user contains operand bundles
    2216                 :             :   /// with tags other than those specified in \p IDs.
    2217                 :             :   bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const {
    2218                 :             :     for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
    2219                 :             :       uint32_t ID = getOperandBundleAt(i).getTagID();
    2220                 :             :       if (!is_contained(IDs, ID))
    2221                 :             :         return true;
    2222                 :             :     }
    2223                 :             :     return false;
    2224                 :             :   }
    2225                 :             : 
    2226                 :             :   /// Used to keep track of an operand bundle.  See the main comment on
    2227                 :             :   /// OperandBundleUser above.
    2228                 :             :   struct BundleOpInfo {
    2229                 :             :     /// The operand bundle tag, interned by
    2230                 :             :     /// LLVMContextImpl::getOrInsertBundleTag.
    2231                 :             :     StringMapEntry<uint32_t> *Tag;
    2232                 :             : 
    2233                 :             :     /// The index in the Use& vector where operands for this operand
    2234                 :             :     /// bundle starts.
    2235                 :             :     uint32_t Begin;
    2236                 :             : 
    2237                 :             :     /// The index in the Use& vector where operands for this operand
    2238                 :             :     /// bundle ends.
    2239                 :             :     uint32_t End;
    2240                 :             : 
    2241                 :             :     bool operator==(const BundleOpInfo &Other) const {
    2242                 :             :       return Tag == Other.Tag && Begin == Other.Begin && End == Other.End;
    2243                 :             :     }
    2244                 :             :   };
    2245                 :             : 
    2246                 :             :   /// Simple helper function to map a BundleOpInfo to an
    2247                 :             :   /// OperandBundleUse.
    2248                 :             :   OperandBundleUse
    2249                 :             :   operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const {
    2250                 :             :     const auto *begin = op_begin();
    2251                 :             :     ArrayRef<Use> Inputs(begin + BOI.Begin, begin + BOI.End);
    2252                 :             :     return OperandBundleUse(BOI.Tag, Inputs);
    2253                 :             :   }
    2254                 :             : 
    2255                 :             :   using bundle_op_iterator = BundleOpInfo *;
    2256                 :             :   using const_bundle_op_iterator = const BundleOpInfo *;
    2257                 :             : 
    2258                 :             :   /// Return the start of the list of BundleOpInfo instances associated
    2259                 :             :   /// with this OperandBundleUser.
    2260                 :             :   ///
    2261                 :             :   /// OperandBundleUser uses the descriptor area co-allocated with the host User
    2262                 :             :   /// to store some meta information about which operands are "normal" operands,
    2263                 :             :   /// and which ones belong to some operand bundle.
    2264                 :             :   ///
    2265                 :             :   /// The layout of an operand bundle user is
    2266                 :             :   ///
    2267                 :             :   ///          +-----------uint32_t End-------------------------------------+
    2268                 :             :   ///          |                                                            |
    2269                 :             :   ///          |  +--------uint32_t Begin--------------------+              |
    2270                 :             :   ///          |  |                                          |              |
    2271                 :             :   ///          ^  ^                                          v              v
    2272                 :             :   ///  |------|------|----|----|----|----|----|---------|----|---------|----|-----
    2273                 :             :   ///  | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un
    2274                 :             :   ///  |------|------|----|----|----|----|----|---------|----|---------|----|-----
    2275                 :             :   ///   v  v                                  ^              ^
    2276                 :             :   ///   |  |                                  |              |
    2277                 :             :   ///   |  +--------uint32_t Begin------------+              |
    2278                 :             :   ///   |                                                    |
    2279                 :             :   ///   +-----------uint32_t End-----------------------------+
    2280                 :             :   ///
    2281                 :             :   ///
    2282                 :             :   /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use
    2283                 :             :   /// list. These descriptions are installed and managed by this class, and
    2284                 :             :   /// they're all instances of OperandBundleUser<T>::BundleOpInfo.
    2285                 :             :   ///
    2286                 :             :   /// DU is an additional descriptor installed by User's 'operator new' to keep
    2287                 :             :   /// track of the 'BOI0 ... BOIN' co-allocation.  OperandBundleUser does not
    2288                 :             :   /// access or modify DU in any way, it's an implementation detail private to
    2289                 :             :   /// User.
    2290                 :             :   ///
    2291                 :             :   /// The regular Use& vector for the User starts at U0.  The operand bundle
    2292                 :             :   /// uses are part of the Use& vector, just like normal uses.  In the diagram
    2293                 :             :   /// above, the operand bundle uses start at BOI0_U0.  Each instance of
    2294                 :             :   /// BundleOpInfo has information about a contiguous set of uses constituting
    2295                 :             :   /// an operand bundle, and the total set of operand bundle uses themselves
    2296                 :             :   /// form a contiguous set of uses (i.e. there are no gaps between uses
    2297                 :             :   /// corresponding to individual operand bundles).
    2298                 :             :   ///
    2299                 :             :   /// This class does not know the location of the set of operand bundle uses
    2300                 :             :   /// within the use list -- that is decided by the User using this class via
    2301                 :             :   /// the BeginIdx argument in populateBundleOperandInfos.
    2302                 :             :   ///
    2303                 :             :   /// Currently operand bundle users with hung-off operands are not supported.
    2304                 :             :   bundle_op_iterator bundle_op_info_begin() {
    2305                 :             :     if (!hasDescriptor())
    2306                 :             :       return nullptr;
    2307                 :             : 
    2308                 :             :     uint8_t *BytesBegin = getDescriptor().begin();
    2309                 :             :     return reinterpret_cast<bundle_op_iterator>(BytesBegin);
    2310                 :             :   }
    2311                 :             : 
    2312                 :             :   /// Return the start of the list of BundleOpInfo instances associated
    2313                 :             :   /// with this OperandBundleUser.
    2314                 :             :   const_bundle_op_iterator bundle_op_info_begin() const {
    2315                 :             :     auto *NonConstThis = const_cast<CallBase *>(this);
    2316                 :             :     return NonConstThis->bundle_op_info_begin();
    2317                 :             :   }
    2318                 :             : 
    2319                 :             :   /// Return the end of the list of BundleOpInfo instances associated
    2320                 :             :   /// with this OperandBundleUser.
    2321                 :             :   bundle_op_iterator bundle_op_info_end() {
    2322                 :             :     if (!hasDescriptor())
    2323                 :             :       return nullptr;
    2324                 :             : 
    2325                 :             :     uint8_t *BytesEnd = getDescriptor().end();
    2326                 :             :     return reinterpret_cast<bundle_op_iterator>(BytesEnd);
    2327                 :             :   }
    2328                 :             : 
    2329                 :             :   /// Return the end of the list of BundleOpInfo instances associated
    2330                 :             :   /// with this OperandBundleUser.
    2331                 :             :   const_bundle_op_iterator bundle_op_info_end() const {
    2332                 :             :     auto *NonConstThis = const_cast<CallBase *>(this);
    2333                 :             :     return NonConstThis->bundle_op_info_end();
    2334                 :             :   }
    2335                 :             : 
    2336                 :             :   /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end).
    2337                 :             :   iterator_range<bundle_op_iterator> bundle_op_infos() {
    2338                 :             :     return make_range(bundle_op_info_begin(), bundle_op_info_end());
    2339                 :             :   }
    2340                 :             : 
    2341                 :             :   /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end).
    2342                 :             :   iterator_range<const_bundle_op_iterator> bundle_op_infos() const {
    2343                 :             :     return make_range(bundle_op_info_begin(), bundle_op_info_end());
    2344                 :             :   }
    2345                 :             : 
    2346                 :             :   /// Populate the BundleOpInfo instances and the Use& vector from \p
    2347                 :             :   /// Bundles.  Return the op_iterator pointing to the Use& one past the last
    2348                 :             :   /// last bundle operand use.
    2349                 :             :   ///
    2350                 :             :   /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo
    2351                 :             :   /// instance allocated in this User's descriptor.
    2352                 :             :   op_iterator populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles,
    2353                 :             :                                          const unsigned BeginIndex);
    2354                 :             : 
    2355                 :             :   /// Return true if the call has deopt state bundle.
    2356                 :             :   bool hasDeoptState() const {
    2357                 :             :     return getOperandBundle(LLVMContext::OB_deopt).has_value();
    2358                 :             :   }
    2359                 :             : 
    2360                 :             : public:
    2361                 :             :   /// Return the BundleOpInfo for the operand at index OpIdx.
    2362                 :             :   ///
    2363                 :             :   /// It is an error to call this with an OpIdx that does not correspond to an
    2364                 :             :   /// bundle operand.
    2365                 :             :   BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx);
    2366                 :             :   const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const {
    2367                 :             :     return const_cast<CallBase *>(this)->getBundleOpInfoForOperand(OpIdx);
    2368                 :             :   }
    2369                 :             : 
    2370                 :             : protected:
    2371                 :             :   /// Return the total number of values used in \p Bundles.
    2372                 :           0 :   static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) {
    2373                 :           0 :     unsigned Total = 0;
    2374         [ #  # ]:           0 :     for (const auto &B : Bundles)
    2375                 :           0 :       Total += B.input_size();
    2376                 :           0 :     return Total;
    2377                 :             :   }
    2378                 :             : 
    2379                 :             :   /// @}
    2380                 :             :   // End of operand bundle API.
    2381                 :             : 
    2382                 :             : private:
    2383                 :             :   bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const;
    2384                 :             :   bool hasFnAttrOnCalledFunction(StringRef Kind) const;
    2385                 :             : 
    2386                 :             :   template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const {
    2387                 :             :     if (Attrs.hasFnAttr(Kind))
    2388                 :             :       return true;
    2389                 :             : 
    2390                 :             :     return hasFnAttrOnCalledFunction(Kind);
    2391                 :             :   }
    2392                 :             :   template <typename AK> Attribute getFnAttrOnCalledFunction(AK Kind) const;
    2393                 :             :   template <typename AK>
    2394                 :             :   Attribute getParamAttrOnCalledFunction(unsigned ArgNo, AK Kind) const;
    2395                 :             : 
    2396                 :             :   /// Determine whether the return value has the given attribute. Supports
    2397                 :             :   /// Attribute::AttrKind and StringRef as \p AttrKind types.
    2398                 :             :   template <typename AttrKind> bool hasRetAttrImpl(AttrKind Kind) const {
    2399                 :             :     if (Attrs.hasRetAttr(Kind))
    2400                 :             :       return true;
    2401                 :             : 
    2402                 :             :     // Look at the callee, if available.
    2403                 :             :     if (const Function *F = getCalledFunction())
    2404                 :             :       return F->getAttributes().hasRetAttr(Kind);
    2405                 :             :     return false;
    2406                 :             :   }
    2407                 :             : };
    2408                 :             : 
    2409                 :             : template <>
    2410                 :             : struct OperandTraits<CallBase> : public VariadicOperandTraits<CallBase, 1> {};
    2411                 :             : 
    2412                 :           0 : DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallBase, Value)
    2413                 :             : 
    2414                 :             : //===----------------------------------------------------------------------===//
    2415                 :             : //                           FuncletPadInst Class
    2416                 :             : //===----------------------------------------------------------------------===//
    2417                 :             : class FuncletPadInst : public Instruction {
    2418                 :             : private:
    2419                 :             :   FuncletPadInst(const FuncletPadInst &CPI);
    2420                 :             : 
    2421                 :             :   explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
    2422                 :             :                           ArrayRef<Value *> Args, unsigned Values,
    2423                 :             :                           const Twine &NameStr, InsertPosition InsertBefore);
    2424                 :             : 
    2425                 :             :   void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr);
    2426                 :             : 
    2427                 :             : protected:
    2428                 :             :   // Note: Instruction needs to be a friend here to call cloneImpl.
    2429                 :             :   friend class Instruction;
    2430                 :             :   friend class CatchPadInst;
    2431                 :             :   friend class CleanupPadInst;
    2432                 :             : 
    2433                 :             :   FuncletPadInst *cloneImpl() const;
    2434                 :             : 
    2435                 :             : public:
    2436                 :             :   /// Provide fast operand accessors
    2437                 :             :   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
    2438                 :             : 
    2439                 :             :   /// arg_size - Return the number of funcletpad arguments.
    2440                 :             :   ///
    2441                 :             :   unsigned arg_size() const { return getNumOperands() - 1; }
    2442                 :             : 
    2443                 :             :   /// Convenience accessors
    2444                 :             : 
    2445                 :             :   /// Return the outer EH-pad this funclet is nested within.
    2446                 :             :   ///
    2447                 :             :   /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst
    2448                 :             :   /// is a CatchPadInst.
    2449                 :             :   Value *getParentPad() const { return Op<-1>(); }
    2450                 :             :   void setParentPad(Value *ParentPad) {
    2451                 :             :     assert(ParentPad);
    2452                 :             :     Op<-1>() = ParentPad;
    2453                 :             :   }
    2454                 :             : 
    2455                 :             :   /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument.
    2456                 :             :   ///
    2457                 :             :   Value *getArgOperand(unsigned i) const { return getOperand(i); }
    2458                 :             :   void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
    2459                 :             : 
    2460                 :             :   /// arg_operands - iteration adapter for range-for loops.
    2461                 :             :   op_range arg_operands() { return op_range(op_begin(), op_end() - 1); }
    2462                 :             : 
    2463                 :             :   /// arg_operands - iteration adapter for range-for loops.
    2464                 :             :   const_op_range arg_operands() const {
    2465                 :             :     return const_op_range(op_begin(), op_end() - 1);
    2466                 :             :   }
    2467                 :             : 
    2468                 :             :   // Methods for support type inquiry through isa, cast, and dyn_cast:
    2469                 :             :   static bool classof(const Instruction *I) { return I->isFuncletPad(); }
    2470                 :             :   static bool classof(const Value *V) {
    2471                 :             :     return isa<Instruction>(V) && classof(cast<Instruction>(V));
    2472                 :             :   }
    2473                 :             : };
    2474                 :             : 
    2475                 :             : template <>
    2476                 :             : struct OperandTraits<FuncletPadInst>
    2477                 :             :     : public VariadicOperandTraits<FuncletPadInst, /*MINARITY=*/1> {};
    2478                 :             : 
    2479                 :             : DEFINE_TRANSPARENT_OPERAND_ACCESSORS(FuncletPadInst, Value)
    2480                 :             : 
    2481                 :             : } // end namespace llvm
    2482                 :             : 
    2483                 :             : #endif // LLVM_IR_INSTRTYPES_H
        

Generated by: LCOV version 2.0-1