LCOV - code coverage report
Current view: top level - lib/IR - Instructions.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1276 1541 82.8 %
Date: 2018-07-13 00:08:38 Functions: 264 312 84.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Instructions.cpp - Implement the LLVM instructions -----------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements all of the non-inline methods for the LLVM instruction
      11             : // classes.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/IR/Instructions.h"
      16             : #include "LLVMContextImpl.h"
      17             : #include "llvm/ADT/None.h"
      18             : #include "llvm/ADT/SmallVector.h"
      19             : #include "llvm/ADT/Twine.h"
      20             : #include "llvm/IR/Attributes.h"
      21             : #include "llvm/IR/BasicBlock.h"
      22             : #include "llvm/IR/CallSite.h"
      23             : #include "llvm/IR/Constant.h"
      24             : #include "llvm/IR/Constants.h"
      25             : #include "llvm/IR/DataLayout.h"
      26             : #include "llvm/IR/DerivedTypes.h"
      27             : #include "llvm/IR/Function.h"
      28             : #include "llvm/IR/InstrTypes.h"
      29             : #include "llvm/IR/Instruction.h"
      30             : #include "llvm/IR/LLVMContext.h"
      31             : #include "llvm/IR/Metadata.h"
      32             : #include "llvm/IR/Module.h"
      33             : #include "llvm/IR/Operator.h"
      34             : #include "llvm/IR/Type.h"
      35             : #include "llvm/IR/Value.h"
      36             : #include "llvm/Support/AtomicOrdering.h"
      37             : #include "llvm/Support/Casting.h"
      38             : #include "llvm/Support/ErrorHandling.h"
      39             : #include "llvm/Support/MathExtras.h"
      40             : #include <algorithm>
      41             : #include <cassert>
      42             : #include <cstdint>
      43             : #include <vector>
      44             : 
      45             : using namespace llvm;
      46             : 
      47             : //===----------------------------------------------------------------------===//
      48             : //                            AllocaInst Class
      49             : //===----------------------------------------------------------------------===//
      50             : 
      51             : Optional<uint64_t>
      52           5 : AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
      53           5 :   uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType());
      54           5 :   if (isArrayAllocation()) {
      55             :     auto C = dyn_cast<ConstantInt>(getArraySize());
      56             :     if (!C)
      57             :       return None;
      58           0 :     Size *= C->getZExtValue();
      59             :   }
      60             :   return Size;
      61             : }
      62             : 
      63             : //===----------------------------------------------------------------------===//
      64             : //                            CallSite Class
      65             : //===----------------------------------------------------------------------===//
      66             : 
      67           0 : User::op_iterator CallSite::getCallee() const {
      68             :   Instruction *II(getInstruction());
      69             :   return isCall()
      70           0 :     ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
      71           0 :     : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
      72             : }
      73             : 
      74             : //===----------------------------------------------------------------------===//
      75             : //                            TerminatorInst Class
      76             : //===----------------------------------------------------------------------===//
      77             : 
      78    61319429 : unsigned TerminatorInst::getNumSuccessors() const {
      79    61319429 :   switch (getOpcode()) {
      80             : #define HANDLE_TERM_INST(N, OPC, CLASS)                                        \
      81             :   case Instruction::OPC:                                                       \
      82             :     return static_cast<const CLASS *>(this)->getNumSuccessors();
      83             : #include "llvm/IR/Instruction.def"
      84             :   default:
      85             :     break;
      86             :   }
      87           0 :   llvm_unreachable("not a terminator");
      88             : }
      89             : 
      90    46578402 : BasicBlock *TerminatorInst::getSuccessor(unsigned idx) const {
      91    46578402 :   switch (getOpcode()) {
      92             : #define HANDLE_TERM_INST(N, OPC, CLASS)                                        \
      93             :   case Instruction::OPC:                                                       \
      94             :     return static_cast<const CLASS *>(this)->getSuccessor(idx);
      95             : #include "llvm/IR/Instruction.def"
      96             :   default:
      97             :     break;
      98             :   }
      99           0 :   llvm_unreachable("not a terminator");
     100             : }
     101             : 
     102        8229 : void TerminatorInst::setSuccessor(unsigned idx, BasicBlock *B) {
     103        8229 :   switch (getOpcode()) {
     104             : #define HANDLE_TERM_INST(N, OPC, CLASS)                                        \
     105             :   case Instruction::OPC:                                                       \
     106             :     return static_cast<CLASS *>(this)->setSuccessor(idx, B);
     107             : #include "llvm/IR/Instruction.def"
     108             :   default:
     109             :     break;
     110             :   }
     111           0 :   llvm_unreachable("not a terminator");
     112             : }
     113             : 
     114             : //===----------------------------------------------------------------------===//
     115             : //                              SelectInst Class
     116             : //===----------------------------------------------------------------------===//
     117             : 
     118             : /// areInvalidOperands - Return a string if the specified operands are invalid
     119             : /// for a select operation, otherwise return null.
     120      123791 : const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
     121      123791 :   if (Op1->getType() != Op2->getType())
     122             :     return "both values to select must have same type";
     123             : 
     124      123791 :   if (Op1->getType()->isTokenTy())
     125             :     return "select values cannot have token type";
     126             : 
     127      123790 :   if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
     128             :     // Vector select.
     129       77193 :     if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
     130             :       return "vector select condition element type must be i1";
     131       77193 :     VectorType *ET = dyn_cast<VectorType>(Op1->getType());
     132             :     if (!ET)
     133             :       return "selected values for vector select must be vectors";
     134       77193 :     if (ET->getNumElements() != VT->getNumElements())
     135             :       return "vector select requires selected vectors to have "
     136             :                    "the same vector length as select condition";
     137       46597 :   } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
     138             :     return "select condition must be i1 or <n x i1>";
     139             :   }
     140             :   return nullptr;
     141             : }
     142             : 
     143             : //===----------------------------------------------------------------------===//
     144             : //                               PHINode Class
     145             : //===----------------------------------------------------------------------===//
     146             : 
     147       23251 : PHINode::PHINode(const PHINode &PN)
     148             :     : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
     149       69753 :       ReservedSpace(PN.getNumOperands()) {
     150             :   allocHungoffUses(PN.getNumOperands());
     151             :   std::copy(PN.op_begin(), PN.op_end(), op_begin());
     152             :   std::copy(PN.block_begin(), PN.block_end(), block_begin());
     153       23251 :   SubclassOptionalData = PN.SubclassOptionalData;
     154       23251 : }
     155             : 
     156             : // removeIncomingValue - Remove an incoming value.  This is useful if a
     157             : // predecessor basic block is deleted.
     158       42760 : Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
     159             :   Value *Removed = getIncomingValue(Idx);
     160             : 
     161             :   // Move everything after this operand down.
     162             :   //
     163             :   // FIXME: we could just swap with the end of the list, then erase.  However,
     164             :   // clients might not expect this to happen.  The code as it is thrashes the
     165             :   // use/def lists, which is kinda lame.
     166       42760 :   std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
     167       85520 :   std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
     168             : 
     169             :   // Nuke the last value.
     170             :   Op<-1>().set(nullptr);
     171       42760 :   setNumHungOffUseOperands(getNumOperands() - 1);
     172             : 
     173             :   // If the PHI node is dead, because it has zero entries, nuke it now.
     174       42760 :   if (getNumOperands() == 0 && DeletePHIIfEmpty) {
     175             :     // If anyone is using this PHI, make them use a dummy value instead...
     176          32 :     replaceAllUsesWith(UndefValue::get(getType()));
     177          32 :     eraseFromParent();
     178             :   }
     179       42760 :   return Removed;
     180             : }
     181             : 
     182             : /// growOperands - grow operands - This grows the operand list in response
     183             : /// to a push_back style of operation.  This grows the number of ops by 1.5
     184             : /// times.
     185             : ///
     186        4371 : void PHINode::growOperands() {
     187             :   unsigned e = getNumOperands();
     188        4371 :   unsigned NumOps = e + e / 2;
     189        4371 :   if (NumOps < 2) NumOps = 2;      // 2 op PHI nodes are VERY common.
     190             : 
     191        4371 :   ReservedSpace = NumOps;
     192        4371 :   growHungoffUses(ReservedSpace, /* IsPhi */ true);
     193        4371 : }
     194             : 
     195             : /// hasConstantValue - If the specified PHI node always merges together the same
     196             : /// value, return the value, otherwise return null.
     197     1161257 : Value *PHINode::hasConstantValue() const {
     198             :   // Exploit the fact that phi nodes always have at least one entry.
     199             :   Value *ConstantValue = getIncomingValue(0);
     200     1187033 :   for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
     201     1172482 :     if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
     202     1159607 :       if (ConstantValue != this)
     203             :         return nullptr; // Incoming values not all the same.
     204             :        // The case where the first value is this PHI.
     205             :       ConstantValue = getIncomingValue(i);
     206             :     }
     207        1663 :   if (ConstantValue == this)
     208           3 :     return UndefValue::get(getType());
     209             :   return ConstantValue;
     210             : }
     211             : 
     212             : /// hasConstantOrUndefValue - Whether the specified PHI node always merges
     213             : /// together the same value, assuming that undefs result in the same value as
     214             : /// non-undefs.
     215             : /// Unlike \ref hasConstantValue, this does not return a value because the
     216             : /// unique non-undef incoming value need not dominate the PHI node.
     217        3658 : bool PHINode::hasConstantOrUndefValue() const {
     218             :   Value *ConstantValue = nullptr;
     219       15510 :   for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
     220             :     Value *Incoming = getIncomingValue(i);
     221       14714 :     if (Incoming != this && !isa<UndefValue>(Incoming)) {
     222        5140 :       if (ConstantValue && ConstantValue != Incoming)
     223             :         return false;
     224             :       ConstantValue = Incoming;
     225             :     }
     226             :   }
     227             :   return true;
     228             : }
     229             : 
     230             : //===----------------------------------------------------------------------===//
     231             : //                       LandingPadInst Implementation
     232             : //===----------------------------------------------------------------------===//
     233             : 
     234       30198 : LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
     235       30198 :                                const Twine &NameStr, Instruction *InsertBefore)
     236       30198 :     : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
     237       30198 :   init(NumReservedValues, NameStr);
     238       30198 : }
     239             : 
     240           4 : LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
     241           4 :                                const Twine &NameStr, BasicBlock *InsertAtEnd)
     242           4 :     : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
     243           4 :   init(NumReservedValues, NameStr);
     244           4 : }
     245             : 
     246        7879 : LandingPadInst::LandingPadInst(const LandingPadInst &LP)
     247             :     : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
     248             :                   LP.getNumOperands()),
     249       23637 :       ReservedSpace(LP.getNumOperands()) {
     250        7879 :   allocHungoffUses(LP.getNumOperands());
     251             :   Use *OL = getOperandList();
     252        7879 :   const Use *InOL = LP.getOperandList();
     253       14265 :   for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
     254        6386 :     OL[I] = InOL[I];
     255             : 
     256             :   setCleanup(LP.isCleanup());
     257        7879 : }
     258             : 
     259       30198 : LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
     260             :                                        const Twine &NameStr,
     261             :                                        Instruction *InsertBefore) {
     262       30198 :   return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
     263             : }
     264             : 
     265           4 : LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
     266             :                                        const Twine &NameStr,
     267             :                                        BasicBlock *InsertAtEnd) {
     268           4 :   return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
     269             : }
     270             : 
     271       30202 : void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
     272       30202 :   ReservedSpace = NumReservedValues;
     273             :   setNumHungOffUseOperands(0);
     274       30202 :   allocHungoffUses(ReservedSpace);
     275       30202 :   setName(NameStr);
     276             :   setCleanup(false);
     277       30202 : }
     278             : 
     279             : /// growOperands - grow operands - This grows the operand list in response to a
     280             : /// push_back style of operation. This grows the number of ops by 2 times.
     281        6407 : void LandingPadInst::growOperands(unsigned Size) {
     282        6407 :   unsigned e = getNumOperands();
     283        6791 :   if (ReservedSpace >= e + Size) return;
     284       12046 :   ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
     285        6023 :   growHungoffUses(ReservedSpace);
     286             : }
     287             : 
     288        6219 : void LandingPadInst::addClause(Constant *Val) {
     289             :   unsigned OpNo = getNumOperands();
     290        6219 :   growOperands(1);
     291             :   assert(OpNo < ReservedSpace && "Growing didn't work!");
     292        6219 :   setNumHungOffUseOperands(getNumOperands() + 1);
     293       12438 :   getOperandList()[OpNo] = Val;
     294        6219 : }
     295             : 
     296             : //===----------------------------------------------------------------------===//
     297             : //                        CallInst Implementation
     298             : //===----------------------------------------------------------------------===//
     299             : 
     300      795734 : void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
     301             :                     ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
     302      795734 :   this->FTy = FTy;
     303             :   assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
     304             :          "NumOperands not set up?");
     305             :   Op<-1>() = Func;
     306             : 
     307             : #ifndef NDEBUG
     308             :   assert((Args.size() == FTy->getNumParams() ||
     309             :           (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
     310             :          "Calling a function with bad signature!");
     311             : 
     312             :   for (unsigned i = 0; i != Args.size(); ++i)
     313             :     assert((i >= FTy->getNumParams() || 
     314             :             FTy->getParamType(i) == Args[i]->getType()) &&
     315             :            "Calling a function with a bad signature!");
     316             : #endif
     317             : 
     318      795734 :   std::copy(Args.begin(), Args.end(), op_begin());
     319             : 
     320      795734 :   auto It = populateBundleOperandInfos(Bundles, Args.size());
     321             :   (void)It;
     322             :   assert(It + 1 == op_end() && "Should add up!");
     323             : 
     324      795734 :   setName(NameStr);
     325      795734 : }
     326             : 
     327         903 : void CallInst::init(Value *Func, const Twine &NameStr) {
     328         903 :   FTy =
     329         903 :       cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
     330             :   assert(getNumOperands() == 1 && "NumOperands not set up?");
     331             :   Op<-1>() = Func;
     332             : 
     333             :   assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
     334             : 
     335         903 :   setName(NameStr);
     336         903 : }
     337             : 
     338         847 : CallInst::CallInst(Value *Func, const Twine &Name, Instruction *InsertBefore)
     339             :     : CallBase<CallInst>(
     340             :           cast<FunctionType>(
     341             :               cast<PointerType>(Func->getType())->getElementType())
     342             :               ->getReturnType(),
     343             :           Instruction::Call,
     344             :           OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1,
     345         847 :           InsertBefore) {
     346         847 :   init(Func, Name);
     347         847 : }
     348             : 
     349          56 : CallInst::CallInst(Value *Func, const Twine &Name, BasicBlock *InsertAtEnd)
     350             :     : CallBase<CallInst>(
     351             :           cast<FunctionType>(
     352             :               cast<PointerType>(Func->getType())->getElementType())
     353             :               ->getReturnType(),
     354             :           Instruction::Call,
     355          56 :           OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1, InsertAtEnd) {
     356          56 :   init(Func, Name);
     357          56 : }
     358             : 
     359      173615 : CallInst::CallInst(const CallInst &CI)
     360      173615 :     : CallBase<CallInst>(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
     361      173615 :                          OperandTraits<CallBase<CallInst>>::op_end(this) -
     362      173615 :                              CI.getNumOperands(),
     363      173615 :                          CI.getNumOperands()) {
     364             :   setTailCallKind(CI.getTailCallKind());
     365             :   setCallingConv(CI.getCallingConv());
     366             : 
     367      173615 :   std::copy(CI.op_begin(), CI.op_end(), op_begin());
     368             :   std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
     369             :             bundle_op_info_begin());
     370      173615 :   SubclassOptionalData = CI.SubclassOptionalData;
     371      173615 : }
     372             : 
     373          31 : CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
     374             :                            Instruction *InsertPt) {
     375          31 :   std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
     376             : 
     377          62 :   auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
     378             :                                  InsertPt);
     379             :   NewCI->setTailCallKind(CI->getTailCallKind());
     380             :   NewCI->setCallingConv(CI->getCallingConv());
     381          31 :   NewCI->SubclassOptionalData = CI->SubclassOptionalData;
     382             :   NewCI->setAttributes(CI->getAttributes());
     383          31 :   NewCI->setDebugLoc(CI->getDebugLoc());
     384          31 :   return NewCI;
     385             : }
     386             : 
     387             : 
     388             : 
     389             : 
     390             : 
     391             : 
     392             : 
     393             : 
     394             : 
     395             : 
     396             : /// IsConstantOne - Return true only if val is constant int 1
     397          49 : static bool IsConstantOne(Value *val) {
     398             :   assert(val && "IsConstantOne does not work with nullptr val");
     399             :   const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
     400          39 :   return CVal && CVal->isOne();
     401             : }
     402             : 
     403          27 : static Instruction *createMalloc(Instruction *InsertBefore,
     404             :                                  BasicBlock *InsertAtEnd, Type *IntPtrTy,
     405             :                                  Type *AllocTy, Value *AllocSize,
     406             :                                  Value *ArraySize,
     407             :                                  ArrayRef<OperandBundleDef> OpB,
     408             :                                  Function *MallocF, const Twine &Name) {
     409             :   assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
     410             :          "createMalloc needs either InsertBefore or InsertAtEnd");
     411             : 
     412             :   // malloc(type) becomes: 
     413             :   //       bitcast (i8* malloc(typeSize)) to type*
     414             :   // malloc(type, arraySize) becomes:
     415             :   //       bitcast (i8* malloc(typeSize*arraySize)) to type*
     416          27 :   if (!ArraySize)
     417           5 :     ArraySize = ConstantInt::get(IntPtrTy, 1);
     418          22 :   else if (ArraySize->getType() != IntPtrTy) {
     419           4 :     if (InsertBefore)
     420           4 :       ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
     421             :                                               "", InsertBefore);
     422             :     else
     423           0 :       ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
     424             :                                               "", InsertAtEnd);
     425             :   }
     426             : 
     427          27 :   if (!IsConstantOne(ArraySize)) {
     428          22 :     if (IsConstantOne(AllocSize)) {
     429           0 :       AllocSize = ArraySize;         // Operand * 1 = Operand
     430             :     } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
     431             :       Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
     432          12 :                                                      false /*ZExt*/);
     433             :       // Malloc arg is constant product of type size and array size
     434          12 :       AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
     435             :     } else {
     436             :       // Multiply type size by the array size...
     437          10 :       if (InsertBefore)
     438          10 :         AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
     439             :                                               "mallocsize", InsertBefore);
     440             :       else
     441           0 :         AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
     442             :                                               "mallocsize", InsertAtEnd);
     443             :     }
     444             :   }
     445             : 
     446             :   assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
     447             :   // Create the call to Malloc.
     448          27 :   BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
     449          27 :   Module *M = BB->getParent()->getParent();
     450          27 :   Type *BPTy = Type::getInt8PtrTy(BB->getContext());
     451             :   Value *MallocFunc = MallocF;
     452          27 :   if (!MallocFunc)
     453             :     // prototype malloc as "void *malloc(size_t)"
     454             :     MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
     455             :   PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
     456             :   CallInst *MCall = nullptr;
     457             :   Instruction *Result = nullptr;
     458          27 :   if (InsertBefore) {
     459          27 :     MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
     460             :                              InsertBefore);
     461             :     Result = MCall;
     462          27 :     if (Result->getType() != AllocPtrType)
     463             :       // Create a cast instruction to convert to the right type...
     464          27 :       Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
     465             :   } else {
     466           0 :     MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
     467             :     Result = MCall;
     468           0 :     if (Result->getType() != AllocPtrType) {
     469           0 :       InsertAtEnd->getInstList().push_back(MCall);
     470             :       // Create a cast instruction to convert to the right type...
     471           0 :       Result = new BitCastInst(MCall, AllocPtrType, Name);
     472             :     }
     473             :   }
     474             :   MCall->setTailCall();
     475             :   if (Function *F = dyn_cast<Function>(MallocFunc)) {
     476             :     MCall->setCallingConv(F->getCallingConv());
     477          23 :     if (!F->returnDoesNotAlias())
     478             :       F->setReturnDoesNotAlias();
     479             :   }
     480             :   assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
     481             : 
     482          27 :   return Result;
     483             : }
     484             : 
     485             : /// CreateMalloc - Generate the IR for a call to malloc:
     486             : /// 1. Compute the malloc call's argument as the specified type's size,
     487             : ///    possibly multiplied by the array size if the array size is not
     488             : ///    constant 1.
     489             : /// 2. Call malloc with that argument.
     490             : /// 3. Bitcast the result of the malloc call to the specified type.
     491          11 : Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
     492             :                                     Type *IntPtrTy, Type *AllocTy,
     493             :                                     Value *AllocSize, Value *ArraySize,
     494             :                                     Function *MallocF,
     495             :                                     const Twine &Name) {
     496          11 :   return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
     497          22 :                       ArraySize, None, MallocF, Name);
     498             : }
     499          16 : Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
     500             :                                     Type *IntPtrTy, Type *AllocTy,
     501             :                                     Value *AllocSize, Value *ArraySize,
     502             :                                     ArrayRef<OperandBundleDef> OpB,
     503             :                                     Function *MallocF,
     504             :                                     const Twine &Name) {
     505             :   return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
     506          16 :                       ArraySize, OpB, MallocF, Name);
     507             : }
     508             : 
     509             : /// CreateMalloc - Generate the IR for a call to malloc:
     510             : /// 1. Compute the malloc call's argument as the specified type's size,
     511             : ///    possibly multiplied by the array size if the array size is not
     512             : ///    constant 1.
     513             : /// 2. Call malloc with that argument.
     514             : /// 3. Bitcast the result of the malloc call to the specified type.
     515             : /// Note: This function does not add the bitcast to the basic block, that is the
     516             : /// responsibility of the caller.
     517           0 : Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
     518             :                                     Type *IntPtrTy, Type *AllocTy,
     519             :                                     Value *AllocSize, Value *ArraySize, 
     520             :                                     Function *MallocF, const Twine &Name) {
     521           0 :   return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
     522           0 :                       ArraySize, None, MallocF, Name);
     523             : }
     524           0 : Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
     525             :                                     Type *IntPtrTy, Type *AllocTy,
     526             :                                     Value *AllocSize, Value *ArraySize,
     527             :                                     ArrayRef<OperandBundleDef> OpB,
     528             :                                     Function *MallocF, const Twine &Name) {
     529             :   return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
     530           0 :                       ArraySize, OpB, MallocF, Name);
     531             : }
     532             : 
     533          22 : static Instruction *createFree(Value *Source,
     534             :                                ArrayRef<OperandBundleDef> Bundles,
     535             :                                Instruction *InsertBefore,
     536             :                                BasicBlock *InsertAtEnd) {
     537             :   assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
     538             :          "createFree needs either InsertBefore or InsertAtEnd");
     539             :   assert(Source->getType()->isPointerTy() &&
     540             :          "Can not free something of nonpointer type!");
     541             : 
     542          22 :   BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
     543          22 :   Module *M = BB->getParent()->getParent();
     544             : 
     545          22 :   Type *VoidTy = Type::getVoidTy(M->getContext());
     546          22 :   Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
     547             :   // prototype free as "void free(void*)"
     548             :   Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
     549             :   CallInst *Result = nullptr;
     550          22 :   Value *PtrCast = Source;
     551          22 :   if (InsertBefore) {
     552          22 :     if (Source->getType() != IntPtrTy)
     553          22 :       PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
     554          22 :     Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
     555             :   } else {
     556           0 :     if (Source->getType() != IntPtrTy)
     557           0 :       PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
     558           0 :     Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
     559             :   }
     560             :   Result->setTailCall();
     561             :   if (Function *F = dyn_cast<Function>(FreeFunc))
     562             :     Result->setCallingConv(F->getCallingConv());
     563             : 
     564          22 :   return Result;
     565             : }
     566             : 
     567             : /// CreateFree - Generate the IR for a call to the builtin free function.
     568           8 : Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
     569           8 :   return createFree(Source, None, InsertBefore, nullptr);
     570             : }
     571          14 : Instruction *CallInst::CreateFree(Value *Source,
     572             :                                   ArrayRef<OperandBundleDef> Bundles,
     573             :                                   Instruction *InsertBefore) {
     574          14 :   return createFree(Source, Bundles, InsertBefore, nullptr);
     575             : }
     576             : 
     577             : /// CreateFree - Generate the IR for a call to the builtin free function.
     578             : /// Note: This function does not add the call to the basic block, that is the
     579             : /// responsibility of the caller.
     580           0 : Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
     581           0 :   Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
     582             :   assert(FreeCall && "CreateFree did not create a CallInst");
     583           0 :   return FreeCall;
     584             : }
     585           0 : Instruction *CallInst::CreateFree(Value *Source,
     586             :                                   ArrayRef<OperandBundleDef> Bundles,
     587             :                                   BasicBlock *InsertAtEnd) {
     588           0 :   Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
     589             :   assert(FreeCall && "CreateFree did not create a CallInst");
     590           0 :   return FreeCall;
     591             : }
     592             : 
     593             : //===----------------------------------------------------------------------===//
     594             : //                        InvokeInst Implementation
     595             : //===----------------------------------------------------------------------===//
     596             : 
     597       58712 : void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
     598             :                       BasicBlock *IfException, ArrayRef<Value *> Args,
     599             :                       ArrayRef<OperandBundleDef> Bundles,
     600             :                       const Twine &NameStr) {
     601       58712 :   this->FTy = FTy;
     602             : 
     603             :   assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
     604             :          "NumOperands not set up?");
     605             :   Op<-3>() = Fn;
     606             :   Op<-2>() = IfNormal;
     607             :   Op<-1>() = IfException;
     608             : 
     609             : #ifndef NDEBUG
     610             :   assert(((Args.size() == FTy->getNumParams()) ||
     611             :           (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
     612             :          "Invoking a function with bad signature");
     613             : 
     614             :   for (unsigned i = 0, e = Args.size(); i != e; i++)
     615             :     assert((i >= FTy->getNumParams() || 
     616             :             FTy->getParamType(i) == Args[i]->getType()) &&
     617             :            "Invoking a function with a bad signature!");
     618             : #endif
     619             : 
     620       58712 :   std::copy(Args.begin(), Args.end(), op_begin());
     621             : 
     622       58712 :   auto It = populateBundleOperandInfos(Bundles, Args.size());
     623             :   (void)It;
     624             :   assert(It + 3 == op_end() && "Should add up!");
     625             : 
     626       58712 :   setName(NameStr);
     627       58712 : }
     628             : 
     629        8522 : InvokeInst::InvokeInst(const InvokeInst &II)
     630        8522 :     : CallBase<InvokeInst>(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
     631        8522 :                            OperandTraits<CallBase<InvokeInst>>::op_end(this) -
     632        8522 :                                II.getNumOperands(),
     633        8522 :                            II.getNumOperands()) {
     634             :   setCallingConv(II.getCallingConv());
     635        8522 :   std::copy(II.op_begin(), II.op_end(), op_begin());
     636             :   std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
     637             :             bundle_op_info_begin());
     638        8522 :   SubclassOptionalData = II.SubclassOptionalData;
     639        8522 : }
     640             : 
     641           7 : InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
     642             :                                Instruction *InsertPt) {
     643           7 :   std::vector<Value *> Args(II->arg_begin(), II->arg_end());
     644             : 
     645           7 :   auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
     646             :                                    II->getUnwindDest(), Args, OpB,
     647          14 :                                    II->getName(), InsertPt);
     648             :   NewII->setCallingConv(II->getCallingConv());
     649           7 :   NewII->SubclassOptionalData = II->SubclassOptionalData;
     650             :   NewII->setAttributes(II->getAttributes());
     651           7 :   NewII->setDebugLoc(II->getDebugLoc());
     652           7 :   return NewII;
     653             : }
     654             : 
     655             : 
     656         262 : LandingPadInst *InvokeInst::getLandingPadInst() const {
     657         262 :   return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
     658             : }
     659             : 
     660             : //===----------------------------------------------------------------------===//
     661             : //                        ReturnInst Implementation
     662             : //===----------------------------------------------------------------------===//
     663             : 
     664       96323 : ReturnInst::ReturnInst(const ReturnInst &RI)
     665             :   : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
     666       96323 :                    OperandTraits<ReturnInst>::op_end(this) -
     667       96323 :                      RI.getNumOperands(),
     668       96323 :                    RI.getNumOperands()) {
     669       96323 :   if (RI.getNumOperands())
     670       57559 :     Op<0>() = RI.Op<0>();
     671       96323 :   SubclassOptionalData = RI.SubclassOptionalData;
     672       96323 : }
     673             : 
     674      428093 : ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
     675             :   : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
     676      428093 :                    OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
     677      856186 :                    InsertBefore) {
     678      428093 :   if (retVal)
     679             :     Op<0>() = retVal;
     680      428093 : }
     681             : 
     682         282 : ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
     683             :   : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
     684         282 :                    OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
     685         564 :                    InsertAtEnd) {
     686         282 :   if (retVal)
     687             :     Op<0>() = retVal;
     688         282 : }
     689             : 
     690         467 : ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
     691             :   : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
     692         467 :                    OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
     693         467 : }
     694             : 
     695             : //===----------------------------------------------------------------------===//
     696             : //                        ResumeInst Implementation
     697             : //===----------------------------------------------------------------------===//
     698             : 
     699         524 : ResumeInst::ResumeInst(const ResumeInst &RI)
     700             :   : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
     701         524 :                    OperandTraits<ResumeInst>::op_begin(this), 1) {
     702         524 :   Op<0>() = RI.Op<0>();
     703         524 : }
     704             : 
     705        5055 : ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
     706             :   : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
     707        5055 :                    OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
     708             :   Op<0>() = Exn;
     709        5055 : }
     710             : 
     711           4 : ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
     712             :   : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
     713           4 :                    OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
     714             :   Op<0>() = Exn;
     715           4 : }
     716             : 
     717             : //===----------------------------------------------------------------------===//
     718             : //                        CleanupReturnInst Implementation
     719             : //===----------------------------------------------------------------------===//
     720             : 
     721          60 : CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
     722             :     : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
     723          60 :                      OperandTraits<CleanupReturnInst>::op_end(this) -
     724          60 :                          CRI.getNumOperands(),
     725          60 :                      CRI.getNumOperands()) {
     726          60 :   setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
     727          60 :   Op<0>() = CRI.Op<0>();
     728          60 :   if (CRI.hasUnwindDest())
     729          35 :     Op<1>() = CRI.Op<1>();
     730          60 : }
     731             : 
     732         386 : void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
     733         386 :   if (UnwindBB)
     734         133 :     setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
     735             : 
     736             :   Op<0>() = CleanupPad;
     737         386 :   if (UnwindBB)
     738             :     Op<1>() = UnwindBB;
     739         386 : }
     740             : 
     741         379 : CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
     742         379 :                                      unsigned Values, Instruction *InsertBefore)
     743             :     : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
     744             :                      Instruction::CleanupRet,
     745         379 :                      OperandTraits<CleanupReturnInst>::op_end(this) - Values,
     746         379 :                      Values, InsertBefore) {
     747         379 :   init(CleanupPad, UnwindBB);
     748         379 : }
     749             : 
     750           7 : CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
     751           7 :                                      unsigned Values, BasicBlock *InsertAtEnd)
     752             :     : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
     753             :                      Instruction::CleanupRet,
     754           7 :                      OperandTraits<CleanupReturnInst>::op_end(this) - Values,
     755           7 :                      Values, InsertAtEnd) {
     756           7 :   init(CleanupPad, UnwindBB);
     757           7 : }
     758             : 
     759             : //===----------------------------------------------------------------------===//
     760             : //                        CatchReturnInst Implementation
     761             : //===----------------------------------------------------------------------===//
     762         317 : void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
     763             :   Op<0>() = CatchPad;
     764             :   Op<1>() = BB;
     765         317 : }
     766             : 
     767          10 : CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
     768             :     : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
     769          10 :                      OperandTraits<CatchReturnInst>::op_begin(this), 2) {
     770          10 :   Op<0>() = CRI.Op<0>();
     771          10 :   Op<1>() = CRI.Op<1>();
     772          10 : }
     773             : 
     774         317 : CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
     775         317 :                                  Instruction *InsertBefore)
     776             :     : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
     777             :                      OperandTraits<CatchReturnInst>::op_begin(this), 2,
     778         317 :                      InsertBefore) {
     779         317 :   init(CatchPad, BB);
     780         317 : }
     781             : 
     782           0 : CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
     783           0 :                                  BasicBlock *InsertAtEnd)
     784             :     : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
     785             :                      OperandTraits<CatchReturnInst>::op_begin(this), 2,
     786           0 :                      InsertAtEnd) {
     787           0 :   init(CatchPad, BB);
     788           0 : }
     789             : 
     790             : //===----------------------------------------------------------------------===//
     791             : //                       CatchSwitchInst Implementation
     792             : //===----------------------------------------------------------------------===//
     793             : 
     794         430 : CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
     795             :                                  unsigned NumReservedValues,
     796             :                                  const Twine &NameStr,
     797         430 :                                  Instruction *InsertBefore)
     798             :     : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
     799         430 :                      InsertBefore) {
     800         430 :   if (UnwindDest)
     801         124 :     ++NumReservedValues;
     802         430 :   init(ParentPad, UnwindDest, NumReservedValues + 1);
     803         430 :   setName(NameStr);
     804         430 : }
     805             : 
     806           0 : CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
     807             :                                  unsigned NumReservedValues,
     808           0 :                                  const Twine &NameStr, BasicBlock *InsertAtEnd)
     809             :     : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
     810           0 :                      InsertAtEnd) {
     811           0 :   if (UnwindDest)
     812           0 :     ++NumReservedValues;
     813           0 :   init(ParentPad, UnwindDest, NumReservedValues + 1);
     814           0 :   setName(NameStr);
     815           0 : }
     816             : 
     817          26 : CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
     818             :     : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
     819          26 :                      CSI.getNumOperands()) {
     820          26 :   init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
     821          26 :   setNumHungOffUseOperands(ReservedSpace);
     822          26 :   Use *OL = getOperandList();
     823             :   const Use *InOL = CSI.getOperandList();
     824          88 :   for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
     825          31 :     OL[I] = InOL[I];
     826          26 : }
     827             : 
     828         456 : void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
     829             :                            unsigned NumReservedValues) {
     830             :   assert(ParentPad && NumReservedValues);
     831             : 
     832         456 :   ReservedSpace = NumReservedValues;
     833         456 :   setNumHungOffUseOperands(UnwindDest ? 2 : 1);
     834         456 :   allocHungoffUses(ReservedSpace);
     835             : 
     836             :   Op<0>() = ParentPad;
     837         456 :   if (UnwindDest) {
     838         129 :     setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
     839             :     setUnwindDest(UnwindDest);
     840             :   }
     841         456 : }
     842             : 
     843             : /// growOperands - grow operands - This grows the operand list in response to a
     844             : /// push_back style of operation. This grows the number of ops by 2 times.
     845         460 : void CatchSwitchInst::growOperands(unsigned Size) {
     846             :   unsigned NumOperands = getNumOperands();
     847             :   assert(NumOperands >= 1);
     848         460 :   if (ReservedSpace >= NumOperands + Size)
     849             :     return;
     850           0 :   ReservedSpace = (NumOperands + Size / 2) * 2;
     851           0 :   growHungoffUses(ReservedSpace);
     852             : }
     853             : 
     854         460 : void CatchSwitchInst::addHandler(BasicBlock *Handler) {
     855             :   unsigned OpNo = getNumOperands();
     856         460 :   growOperands(1);
     857             :   assert(OpNo < ReservedSpace && "Growing didn't work!");
     858         460 :   setNumHungOffUseOperands(getNumOperands() + 1);
     859         920 :   getOperandList()[OpNo] = Handler;
     860         460 : }
     861             : 
     862           8 : void CatchSwitchInst::removeHandler(handler_iterator HI) {
     863             :   // Move all subsequent handlers up one.
     864           8 :   Use *EndDst = op_end() - 1;
     865          10 :   for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
     866           2 :     *CurDst = *(CurDst + 1);
     867             :   // Null out the last handler use.
     868             :   *EndDst = nullptr;
     869             : 
     870           8 :   setNumHungOffUseOperands(getNumOperands() - 1);
     871           8 : }
     872             : 
     873             : //===----------------------------------------------------------------------===//
     874             : //                        FuncletPadInst Implementation
     875             : //===----------------------------------------------------------------------===//
     876         948 : void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
     877             :                           const Twine &NameStr) {
     878             :   assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
     879             :   std::copy(Args.begin(), Args.end(), op_begin());
     880             :   setParentPad(ParentPad);
     881         948 :   setName(NameStr);
     882         948 : }
     883             : 
     884         120 : FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
     885             :     : Instruction(FPI.getType(), FPI.getOpcode(),
     886         120 :                   OperandTraits<FuncletPadInst>::op_end(this) -
     887         120 :                       FPI.getNumOperands(),
     888         360 :                   FPI.getNumOperands()) {
     889             :   std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
     890             :   setParentPad(FPI.getParentPad());
     891         120 : }
     892             : 
     893         941 : FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
     894             :                                ArrayRef<Value *> Args, unsigned Values,
     895         941 :                                const Twine &NameStr, Instruction *InsertBefore)
     896             :     : Instruction(ParentPad->getType(), Op,
     897         941 :                   OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
     898        1882 :                   InsertBefore) {
     899         941 :   init(ParentPad, Args, NameStr);
     900         941 : }
     901             : 
     902           7 : FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
     903             :                                ArrayRef<Value *> Args, unsigned Values,
     904           7 :                                const Twine &NameStr, BasicBlock *InsertAtEnd)
     905             :     : Instruction(ParentPad->getType(), Op,
     906           7 :                   OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
     907          14 :                   InsertAtEnd) {
     908           7 :   init(ParentPad, Args, NameStr);
     909           7 : }
     910             : 
     911             : //===----------------------------------------------------------------------===//
     912             : //                      UnreachableInst Implementation
     913             : //===----------------------------------------------------------------------===//
     914             : 
     915       26259 : UnreachableInst::UnreachableInst(LLVMContext &Context, 
     916       26259 :                                  Instruction *InsertBefore)
     917             :   : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
     918       26259 :                    nullptr, 0, InsertBefore) {
     919       26259 : }
     920       28695 : UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
     921             :   : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
     922       28695 :                    nullptr, 0, InsertAtEnd) {
     923       28695 : }
     924             : 
     925             : //===----------------------------------------------------------------------===//
     926             : //                        BranchInst Implementation
     927             : //===----------------------------------------------------------------------===//
     928             : 
     929           0 : void BranchInst::AssertOK() {
     930             :   if (isConditional())
     931             :     assert(getCondition()->getType()->isIntegerTy(1) &&
     932             :            "May only branch on boolean predicates!");
     933           0 : }
     934             : 
     935      367243 : BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
     936             :   : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
     937             :                    OperandTraits<BranchInst>::op_end(this) - 1,
     938      367243 :                    1, InsertBefore) {
     939             :   assert(IfTrue && "Branch destination may not be null!");
     940             :   Op<-1>() = IfTrue;
     941      367243 : }
     942             : 
     943      115658 : BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
     944      115658 :                        Instruction *InsertBefore)
     945             :   : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
     946             :                    OperandTraits<BranchInst>::op_end(this) - 3,
     947      115658 :                    3, InsertBefore) {
     948             :   Op<-1>() = IfTrue;
     949             :   Op<-2>() = IfFalse;
     950             :   Op<-3>() = Cond;
     951             : #ifndef NDEBUG
     952             :   AssertOK();
     953             : #endif
     954      115658 : }
     955             : 
     956       99947 : BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
     957             :   : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
     958             :                    OperandTraits<BranchInst>::op_end(this) - 1,
     959       99947 :                    1, InsertAtEnd) {
     960             :   assert(IfTrue && "Branch destination may not be null!");
     961             :   Op<-1>() = IfTrue;
     962       99947 : }
     963             : 
     964        1044 : BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
     965        1044 :            BasicBlock *InsertAtEnd)
     966             :   : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
     967             :                    OperandTraits<BranchInst>::op_end(this) - 3,
     968        1044 :                    3, InsertAtEnd) {
     969             :   Op<-1>() = IfTrue;
     970             :   Op<-2>() = IfFalse;
     971             :   Op<-3>() = Cond;
     972             : #ifndef NDEBUG
     973             :   AssertOK();
     974             : #endif
     975        1044 : }
     976             : 
     977       65888 : BranchInst::BranchInst(const BranchInst &BI) :
     978             :   TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
     979       65888 :                  OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
     980       65888 :                  BI.getNumOperands()) {
     981       65888 :   Op<-1>() = BI.Op<-1>();
     982       65888 :   if (BI.getNumOperands() != 1) {
     983             :     assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
     984       37454 :     Op<-3>() = BI.Op<-3>();
     985       37454 :     Op<-2>() = BI.Op<-2>();
     986             :   }
     987       65888 :   SubclassOptionalData = BI.SubclassOptionalData;
     988       65888 : }
     989             : 
     990        3452 : void BranchInst::swapSuccessors() {
     991             :   assert(isConditional() &&
     992             :          "Cannot swap successors of an unconditional branch");
     993        3452 :   Op<-1>().swap(Op<-2>());
     994             : 
     995             :   // Update profile metadata if present and it matches our structural
     996             :   // expectations.
     997        3452 :   swapProfMetadata();
     998        3452 : }
     999             : 
    1000             : //===----------------------------------------------------------------------===//
    1001             : //                        AllocaInst Implementation
    1002             : //===----------------------------------------------------------------------===//
    1003             : 
    1004      730882 : static Value *getAISize(LLVMContext &Context, Value *Amt) {
    1005      730882 :   if (!Amt)
    1006      611072 :     Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
    1007             :   else {
    1008             :     assert(!isa<BasicBlock>(Amt) &&
    1009             :            "Passed basic block into allocation size parameter! Use other ctor");
    1010             :     assert(Amt->getType()->isIntegerTy() &&
    1011             :            "Allocation array size is not an integer!");
    1012             :   }
    1013      730882 :   return Amt;
    1014             : }
    1015             : 
    1016         981 : AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
    1017         981 :                        Instruction *InsertBefore)
    1018         981 :   : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
    1019             : 
    1020           0 : AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
    1021           0 :                        BasicBlock *InsertAtEnd)
    1022           0 :   : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
    1023             : 
    1024      553339 : AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
    1025      553339 :                        const Twine &Name, Instruction *InsertBefore)
    1026      553339 :   : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
    1027             : 
    1028           0 : AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
    1029           0 :                        const Twine &Name, BasicBlock *InsertAtEnd)
    1030           0 :   : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
    1031             : 
    1032      730882 : AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
    1033             :                        unsigned Align, const Twine &Name,
    1034      730882 :                        Instruction *InsertBefore)
    1035      730882 :   : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
    1036             :                      getAISize(Ty->getContext(), ArraySize), InsertBefore),
    1037     1461764 :     AllocatedType(Ty) {
    1038      730882 :   setAlignment(Align);
    1039             :   assert(!Ty->isVoidTy() && "Cannot allocate void!");
    1040      730882 :   setName(Name);
    1041      730882 : }
    1042             : 
    1043           0 : AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
    1044             :                        unsigned Align, const Twine &Name,
    1045           0 :                        BasicBlock *InsertAtEnd)
    1046           0 :   : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
    1047             :                      getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
    1048           0 :       AllocatedType(Ty) {
    1049           0 :   setAlignment(Align);
    1050             :   assert(!Ty->isVoidTy() && "Cannot allocate void!");
    1051           0 :   setName(Name);
    1052           0 : }
    1053             : 
    1054     1270971 : void AllocaInst::setAlignment(unsigned Align) {
    1055             :   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
    1056             :   assert(Align <= MaximumAlignment &&
    1057             :          "Alignment is greater than MaximumAlignment!");
    1058     2541942 :   setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
    1059     1270971 :                              (Log2_32(Align) + 1));
    1060             :   assert(getAlignment() == Align && "Alignment representation error!");
    1061     1270971 : }
    1062             : 
    1063     2668799 : bool AllocaInst::isArrayAllocation() const {
    1064             :   if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
    1065     2667248 :     return !CI->isOne();
    1066             :   return true;
    1067             : }
    1068             : 
    1069             : /// isStaticAlloca - Return true if this alloca is in the entry block of the
    1070             : /// function and is a constant size.  If so, the code generator will fold it
    1071             : /// into the prolog/epilog code, so it is basically free.
    1072     2849680 : bool AllocaInst::isStaticAlloca() const {
    1073             :   // Must be constant size.
    1074     2849680 :   if (!isa<ConstantInt>(getArraySize())) return false;
    1075             :   
    1076             :   // Must be in the entry block.
    1077     2848859 :   const BasicBlock *Parent = getParent();
    1078     8546423 :   return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
    1079             : }
    1080             : 
    1081             : //===----------------------------------------------------------------------===//
    1082             : //                           LoadInst Implementation
    1083             : //===----------------------------------------------------------------------===//
    1084             : 
    1085     1607472 : void LoadInst::AssertOK() {
    1086             :   assert(getOperand(0)->getType()->isPointerTy() &&
    1087             :          "Ptr must have pointer type.");
    1088             :   assert(!(isAtomic() && getAlignment() == 0) &&
    1089             :          "Alignment required for atomic load");
    1090     1607472 : }
    1091             : 
    1092        1410 : LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
    1093        1410 :     : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
    1094             : 
    1095           0 : LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
    1096           0 :     : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
    1097             : 
    1098        1669 : LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
    1099        1669 :                    Instruction *InsertBef)
    1100        1669 :     : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
    1101             : 
    1102           0 : LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
    1103           0 :                    BasicBlock *InsertAE)
    1104           0 :     : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
    1105             : 
    1106       35703 : LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
    1107       35703 :                    unsigned Align, Instruction *InsertBef)
    1108             :     : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
    1109       35703 :                SyncScope::System, InsertBef) {}
    1110             : 
    1111           0 : LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
    1112           0 :                    unsigned Align, BasicBlock *InsertAE)
    1113             :     : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
    1114           0 :                SyncScope::System, InsertAE) {}
    1115             : 
    1116      646468 : LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
    1117             :                    unsigned Align, AtomicOrdering Order,
    1118      646468 :                    SyncScope::ID SSID, Instruction *InsertBef)
    1119      646468 :     : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
    1120             :   assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
    1121             :   setVolatile(isVolatile);
    1122      646468 :   setAlignment(Align);
    1123             :   setAtomic(Order, SSID);
    1124      646468 :   AssertOK();
    1125      646468 :   setName(Name);
    1126      646468 : }
    1127             : 
    1128           0 : LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, 
    1129             :                    unsigned Align, AtomicOrdering Order,
    1130             :                    SyncScope::ID SSID,
    1131           0 :                    BasicBlock *InsertAE)
    1132             :   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
    1133           0 :                      Load, Ptr, InsertAE) {
    1134             :   setVolatile(isVolatile);
    1135           0 :   setAlignment(Align);
    1136             :   setAtomic(Order, SSID);
    1137           0 :   AssertOK();
    1138           0 :   setName(Name);
    1139           0 : }
    1140             : 
    1141         835 : LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
    1142             :   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
    1143         835 :                      Load, Ptr, InsertBef) {
    1144             :   setVolatile(false);
    1145         835 :   setAlignment(0);
    1146             :   setAtomic(AtomicOrdering::NotAtomic);
    1147         835 :   AssertOK();
    1148         855 :   if (Name && Name[0]) setName(Name);
    1149         835 : }
    1150             : 
    1151          23 : LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
    1152             :   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
    1153          23 :                      Load, Ptr, InsertAE) {
    1154             :   setVolatile(false);
    1155          23 :   setAlignment(0);
    1156             :   setAtomic(AtomicOrdering::NotAtomic);
    1157          23 :   AssertOK();
    1158          46 :   if (Name && Name[0]) setName(Name);
    1159          23 : }
    1160             : 
    1161      960118 : LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
    1162      960118 :                    Instruction *InsertBef)
    1163      960118 :     : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
    1164             :   assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
    1165             :   setVolatile(isVolatile);
    1166      960118 :   setAlignment(0);
    1167             :   setAtomic(AtomicOrdering::NotAtomic);
    1168      960118 :   AssertOK();
    1169      960118 :   if (Name && Name[0]) setName(Name);
    1170      960118 : }
    1171             : 
    1172          28 : LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
    1173          28 :                    BasicBlock *InsertAE)
    1174             :   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
    1175          28 :                      Load, Ptr, InsertAE) {
    1176             :   setVolatile(isVolatile);
    1177          28 :   setAlignment(0);
    1178             :   setAtomic(AtomicOrdering::NotAtomic);
    1179          28 :   AssertOK();
    1180          36 :   if (Name && Name[0]) setName(Name);
    1181          28 : }
    1182             : 
    1183     2631026 : void LoadInst::setAlignment(unsigned Align) {
    1184             :   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
    1185             :   assert(Align <= MaximumAlignment &&
    1186             :          "Alignment is greater than MaximumAlignment!");
    1187     5262052 :   setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
    1188     2631026 :                              ((Log2_32(Align)+1)<<1));
    1189             :   assert(getAlignment() == Align && "Alignment representation error!");
    1190     2631026 : }
    1191             : 
    1192             : //===----------------------------------------------------------------------===//
    1193             : //                           StoreInst Implementation
    1194             : //===----------------------------------------------------------------------===//
    1195             : 
    1196     1781327 : void StoreInst::AssertOK() {
    1197             :   assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
    1198             :   assert(getOperand(1)->getType()->isPointerTy() &&
    1199             :          "Ptr must have pointer type!");
    1200             :   assert(getOperand(0)->getType() ==
    1201             :                  cast<PointerType>(getOperand(1)->getType())->getElementType()
    1202             :          && "Ptr must be a pointer to Val type!");
    1203             :   assert(!(isAtomic() && getAlignment() == 0) &&
    1204             :          "Alignment required for atomic store");
    1205     1781327 : }
    1206             : 
    1207        9298 : StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
    1208        9298 :     : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
    1209             : 
    1210          17 : StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
    1211          17 :     : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
    1212             : 
    1213     1195592 : StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
    1214     1195592 :                      Instruction *InsertBefore)
    1215     1195592 :     : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
    1216             : 
    1217          17 : StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
    1218          17 :                      BasicBlock *InsertAtEnd)
    1219          17 :     : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
    1220             : 
    1221     1234626 : StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
    1222     1234626 :                      Instruction *InsertBefore)
    1223             :     : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
    1224     1234626 :                 SyncScope::System, InsertBefore) {}
    1225             : 
    1226          17 : StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
    1227          17 :                      BasicBlock *InsertAtEnd)
    1228             :     : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
    1229          17 :                 SyncScope::System, InsertAtEnd) {}
    1230             : 
    1231     1781310 : StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
    1232             :                      unsigned Align, AtomicOrdering Order,
    1233             :                      SyncScope::ID SSID,
    1234     1781310 :                      Instruction *InsertBefore)
    1235             :   : Instruction(Type::getVoidTy(val->getContext()), Store,
    1236             :                 OperandTraits<StoreInst>::op_begin(this),
    1237             :                 OperandTraits<StoreInst>::operands(this),
    1238     3562620 :                 InsertBefore) {
    1239             :   Op<0>() = val;
    1240             :   Op<1>() = addr;
    1241             :   setVolatile(isVolatile);
    1242     1781310 :   setAlignment(Align);
    1243             :   setAtomic(Order, SSID);
    1244     1781310 :   AssertOK();
    1245     1781310 : }
    1246             : 
    1247          17 : StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
    1248             :                      unsigned Align, AtomicOrdering Order,
    1249             :                      SyncScope::ID SSID,
    1250          17 :                      BasicBlock *InsertAtEnd)
    1251             :   : Instruction(Type::getVoidTy(val->getContext()), Store,
    1252             :                 OperandTraits<StoreInst>::op_begin(this),
    1253             :                 OperandTraits<StoreInst>::operands(this),
    1254          34 :                 InsertAtEnd) {
    1255             :   Op<0>() = val;
    1256             :   Op<1>() = addr;
    1257             :   setVolatile(isVolatile);
    1258          17 :   setAlignment(Align);
    1259             :   setAtomic(Order, SSID);
    1260          17 :   AssertOK();
    1261          17 : }
    1262             : 
    1263     3040815 : void StoreInst::setAlignment(unsigned Align) {
    1264             :   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
    1265             :   assert(Align <= MaximumAlignment &&
    1266             :          "Alignment is greater than MaximumAlignment!");
    1267     6081630 :   setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
    1268     3040815 :                              ((Log2_32(Align)+1) << 1));
    1269             :   assert(getAlignment() == Align && "Alignment representation error!");
    1270     3040815 : }
    1271             : 
    1272             : //===----------------------------------------------------------------------===//
    1273             : //                       AtomicCmpXchgInst Implementation
    1274             : //===----------------------------------------------------------------------===//
    1275             : 
    1276        2296 : void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
    1277             :                              AtomicOrdering SuccessOrdering,
    1278             :                              AtomicOrdering FailureOrdering,
    1279             :                              SyncScope::ID SSID) {
    1280             :   Op<0>() = Ptr;
    1281             :   Op<1>() = Cmp;
    1282             :   Op<2>() = NewVal;
    1283             :   setSuccessOrdering(SuccessOrdering);
    1284             :   setFailureOrdering(FailureOrdering);
    1285             :   setSyncScopeID(SSID);
    1286             : 
    1287             :   assert(getOperand(0) && getOperand(1) && getOperand(2) &&
    1288             :          "All operands must be non-null!");
    1289             :   assert(getOperand(0)->getType()->isPointerTy() &&
    1290             :          "Ptr must have pointer type!");
    1291             :   assert(getOperand(1)->getType() ==
    1292             :                  cast<PointerType>(getOperand(0)->getType())->getElementType()
    1293             :          && "Ptr must be a pointer to Cmp type!");
    1294             :   assert(getOperand(2)->getType() ==
    1295             :                  cast<PointerType>(getOperand(0)->getType())->getElementType()
    1296             :          && "Ptr must be a pointer to NewVal type!");
    1297             :   assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
    1298             :          "AtomicCmpXchg instructions must be atomic!");
    1299             :   assert(FailureOrdering != AtomicOrdering::NotAtomic &&
    1300             :          "AtomicCmpXchg instructions must be atomic!");
    1301             :   assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
    1302             :          "AtomicCmpXchg failure argument shall be no stronger than the success "
    1303             :          "argument");
    1304             :   assert(FailureOrdering != AtomicOrdering::Release &&
    1305             :          FailureOrdering != AtomicOrdering::AcquireRelease &&
    1306             :          "AtomicCmpXchg failure ordering cannot include release semantics");
    1307        2296 : }
    1308             : 
    1309        2295 : AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
    1310             :                                      AtomicOrdering SuccessOrdering,
    1311             :                                      AtomicOrdering FailureOrdering,
    1312             :                                      SyncScope::ID SSID,
    1313        2295 :                                      Instruction *InsertBefore)
    1314             :     : Instruction(
    1315        2295 :           StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
    1316             :           AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
    1317        4590 :           OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
    1318        2295 :   Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
    1319        2295 : }
    1320             : 
    1321           1 : AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
    1322             :                                      AtomicOrdering SuccessOrdering,
    1323             :                                      AtomicOrdering FailureOrdering,
    1324             :                                      SyncScope::ID SSID,
    1325           1 :                                      BasicBlock *InsertAtEnd)
    1326             :     : Instruction(
    1327           1 :           StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
    1328             :           AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
    1329           2 :           OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
    1330           1 :   Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
    1331           1 : }
    1332             : 
    1333             : //===----------------------------------------------------------------------===//
    1334             : //                       AtomicRMWInst Implementation
    1335             : //===----------------------------------------------------------------------===//
    1336             : 
    1337        7603 : void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
    1338             :                          AtomicOrdering Ordering,
    1339             :                          SyncScope::ID SSID) {
    1340             :   Op<0>() = Ptr;
    1341             :   Op<1>() = Val;
    1342             :   setOperation(Operation);
    1343             :   setOrdering(Ordering);
    1344             :   setSyncScopeID(SSID);
    1345             : 
    1346             :   assert(getOperand(0) && getOperand(1) &&
    1347             :          "All operands must be non-null!");
    1348             :   assert(getOperand(0)->getType()->isPointerTy() &&
    1349             :          "Ptr must have pointer type!");
    1350             :   assert(getOperand(1)->getType() ==
    1351             :          cast<PointerType>(getOperand(0)->getType())->getElementType()
    1352             :          && "Ptr must be a pointer to Val type!");
    1353             :   assert(Ordering != AtomicOrdering::NotAtomic &&
    1354             :          "AtomicRMW instructions must be atomic!");
    1355        7603 : }
    1356             : 
    1357        7602 : AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
    1358             :                              AtomicOrdering Ordering,
    1359             :                              SyncScope::ID SSID,
    1360        7602 :                              Instruction *InsertBefore)
    1361             :   : Instruction(Val->getType(), AtomicRMW,
    1362             :                 OperandTraits<AtomicRMWInst>::op_begin(this),
    1363             :                 OperandTraits<AtomicRMWInst>::operands(this),
    1364       15204 :                 InsertBefore) {
    1365        7602 :   Init(Operation, Ptr, Val, Ordering, SSID);
    1366        7602 : }
    1367             : 
    1368           1 : AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
    1369             :                              AtomicOrdering Ordering,
    1370             :                              SyncScope::ID SSID,
    1371           1 :                              BasicBlock *InsertAtEnd)
    1372             :   : Instruction(Val->getType(), AtomicRMW,
    1373             :                 OperandTraits<AtomicRMWInst>::op_begin(this),
    1374             :                 OperandTraits<AtomicRMWInst>::operands(this),
    1375           2 :                 InsertAtEnd) {
    1376           1 :   Init(Operation, Ptr, Val, Ordering, SSID);
    1377           1 : }
    1378             : 
    1379             : //===----------------------------------------------------------------------===//
    1380             : //                       FenceInst Implementation
    1381             : //===----------------------------------------------------------------------===//
    1382             : 
    1383         987 : FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, 
    1384             :                      SyncScope::ID SSID,
    1385         987 :                      Instruction *InsertBefore)
    1386         987 :   : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
    1387             :   setOrdering(Ordering);
    1388             :   setSyncScopeID(SSID);
    1389         987 : }
    1390             : 
    1391           0 : FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, 
    1392             :                      SyncScope::ID SSID,
    1393           0 :                      BasicBlock *InsertAtEnd)
    1394           0 :   : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
    1395             :   setOrdering(Ordering);
    1396             :   setSyncScopeID(SSID);
    1397           0 : }
    1398             : 
    1399             : //===----------------------------------------------------------------------===//
    1400             : //                       GetElementPtrInst Implementation
    1401             : //===----------------------------------------------------------------------===//
    1402             : 
    1403      444477 : void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
    1404             :                              const Twine &Name) {
    1405             :   assert(getNumOperands() == 1 + IdxList.size() &&
    1406             :          "NumOperands not initialized?");
    1407             :   Op<0>() = Ptr;
    1408      444477 :   std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
    1409      444477 :   setName(Name);
    1410      444477 : }
    1411             : 
    1412      113302 : GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
    1413             :     : Instruction(GEPI.getType(), GetElementPtr,
    1414      113302 :                   OperandTraits<GetElementPtrInst>::op_end(this) -
    1415      113302 :                       GEPI.getNumOperands(),
    1416             :                   GEPI.getNumOperands()),
    1417      113302 :       SourceElementType(GEPI.SourceElementType),
    1418      339906 :       ResultElementType(GEPI.ResultElementType) {
    1419             :   std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
    1420      113302 :   SubclassOptionalData = GEPI.SubclassOptionalData;
    1421      113302 : }
    1422             : 
    1423             : /// getIndexedType - Returns the type of the element that would be accessed with
    1424             : /// a gep instruction with the specified parameters.
    1425             : ///
    1426             : /// The Idxs pointer should point to a continuous piece of memory containing the
    1427             : /// indices, either as Value* or uint64_t.
    1428             : ///
    1429             : /// A null type is returned if the indices are invalid for the specified
    1430             : /// pointer type.
    1431             : ///
    1432             : template <typename IndexTy>
    1433    36775990 : static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
    1434             :   // Handle the special case of the empty set index set, which is always valid.
    1435    36775990 :   if (IdxList.empty())
    1436             :     return Agg;
    1437             : 
    1438             :   // If there is at least one index, the top level type must be sized, otherwise
    1439             :   // it cannot be 'stepped over'.
    1440    36775637 :   if (!Agg->isSized())
    1441             :     return nullptr;
    1442             : 
    1443             :   unsigned CurIdx = 1;
    1444    88673511 :   for (; CurIdx != IdxList.size(); ++CurIdx) {
    1445             :     CompositeType *CT = dyn_cast<CompositeType>(Agg);
    1446    25948939 :     if (!CT || CT->isPointerTy()) return nullptr;
    1447    51897878 :     IndexTy Index = IdxList[CurIdx];
    1448    25948939 :     if (!CT->indexValid(Index)) return nullptr;
    1449    25948937 :     Agg = CT->getTypeAtIndex(Index);
    1450             :   }
    1451             :   return CurIdx == IdxList.size() ? Agg : nullptr;
    1452             : }
    1453             : 
    1454    25993563 : Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
    1455    25993563 :   return getIndexedTypeInternal(Ty, IdxList);
    1456             : }
    1457             : 
    1458    10782387 : Type *GetElementPtrInst::getIndexedType(Type *Ty,
    1459             :                                         ArrayRef<Constant *> IdxList) {
    1460    10782387 :   return getIndexedTypeInternal(Ty, IdxList);
    1461             : }
    1462             : 
    1463          40 : Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
    1464          40 :   return getIndexedTypeInternal(Ty, IdxList);
    1465             : }
    1466             : 
    1467             : /// hasAllZeroIndices - Return true if all of the indices of this GEP are
    1468             : /// zeros.  If so, the result pointer and the first operand have the same
    1469             : /// value, just potentially different types.
    1470      533137 : bool GetElementPtrInst::hasAllZeroIndices() const {
    1471     1636283 :   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
    1472             :     if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
    1473      890096 :       if (!CI->isZero()) return false;
    1474             :     } else {
    1475             :       return false;
    1476             :     }
    1477             :   }
    1478             :   return true;
    1479             : }
    1480             : 
    1481             : /// hasAllConstantIndices - Return true if all of the indices of this GEP are
    1482             : /// constant integers.  If so, the result pointer and the first operand have
    1483             : /// a constant offset between them.
    1484       17446 : bool GetElementPtrInst::hasAllConstantIndices() const {
    1485       47638 :   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
    1486       23569 :     if (!isa<ConstantInt>(getOperand(i)))
    1487             :       return false;
    1488             :   }
    1489             :   return true;
    1490             : }
    1491             : 
    1492      404235 : void GetElementPtrInst::setIsInBounds(bool B) {
    1493             :   cast<GEPOperator>(this)->setIsInBounds(B);
    1494      404235 : }
    1495             : 
    1496     1912625 : bool GetElementPtrInst::isInBounds() const {
    1497     1912625 :   return cast<GEPOperator>(this)->isInBounds();
    1498             : }
    1499             : 
    1500       40561 : bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
    1501             :                                                  APInt &Offset) const {
    1502             :   // Delegate to the generic GEPOperator implementation.
    1503       40561 :   return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
    1504             : }
    1505             : 
    1506             : //===----------------------------------------------------------------------===//
    1507             : //                           ExtractElementInst Implementation
    1508             : //===----------------------------------------------------------------------===//
    1509             : 
    1510       66298 : ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
    1511             :                                        const Twine &Name,
    1512       66298 :                                        Instruction *InsertBef)
    1513       66298 :   : Instruction(cast<VectorType>(Val->getType())->getElementType(),
    1514             :                 ExtractElement,
    1515             :                 OperandTraits<ExtractElementInst>::op_begin(this),
    1516      132596 :                 2, InsertBef) {
    1517             :   assert(isValidOperands(Val, Index) &&
    1518             :          "Invalid extractelement instruction operands!");
    1519             :   Op<0>() = Val;
    1520             :   Op<1>() = Index;
    1521       66298 :   setName(Name);
    1522       66298 : }
    1523             : 
    1524           0 : ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
    1525             :                                        const Twine &Name,
    1526           0 :                                        BasicBlock *InsertAE)
    1527           0 :   : Instruction(cast<VectorType>(Val->getType())->getElementType(),
    1528             :                 ExtractElement,
    1529             :                 OperandTraits<ExtractElementInst>::op_begin(this),
    1530           0 :                 2, InsertAE) {
    1531             :   assert(isValidOperands(Val, Index) &&
    1532             :          "Invalid extractelement instruction operands!");
    1533             : 
    1534             :   Op<0>() = Val;
    1535             :   Op<1>() = Index;
    1536           0 :   setName(Name);
    1537           0 : }
    1538             : 
    1539      130317 : bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
    1540      390951 :   if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
    1541             :     return false;
    1542             :   return true;
    1543             : }
    1544             : 
    1545             : //===----------------------------------------------------------------------===//
    1546             : //                           InsertElementInst Implementation
    1547             : //===----------------------------------------------------------------------===//
    1548             : 
    1549       56368 : InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
    1550             :                                      const Twine &Name,
    1551       56368 :                                      Instruction *InsertBef)
    1552             :   : Instruction(Vec->getType(), InsertElement,
    1553             :                 OperandTraits<InsertElementInst>::op_begin(this),
    1554      112736 :                 3, InsertBef) {
    1555             :   assert(isValidOperands(Vec, Elt, Index) &&
    1556             :          "Invalid insertelement instruction operands!");
    1557             :   Op<0>() = Vec;
    1558             :   Op<1>() = Elt;
    1559             :   Op<2>() = Index;
    1560       56368 :   setName(Name);
    1561       56368 : }
    1562             : 
    1563           0 : InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
    1564             :                                      const Twine &Name,
    1565           0 :                                      BasicBlock *InsertAE)
    1566             :   : Instruction(Vec->getType(), InsertElement,
    1567             :                 OperandTraits<InsertElementInst>::op_begin(this),
    1568           0 :                 3, InsertAE) {
    1569             :   assert(isValidOperands(Vec, Elt, Index) &&
    1570             :          "Invalid insertelement instruction operands!");
    1571             : 
    1572             :   Op<0>() = Vec;
    1573             :   Op<1>() = Elt;
    1574             :   Op<2>() = Index;
    1575           0 :   setName(Name);
    1576           0 : }
    1577             : 
    1578      138777 : bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, 
    1579             :                                         const Value *Index) {
    1580      277554 :   if (!Vec->getType()->isVectorTy())
    1581             :     return false;   // First operand of insertelement must be vector type.
    1582             :   
    1583      138777 :   if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
    1584             :     return false;// Second operand of insertelement must be vector element type.
    1585             :     
    1586      277554 :   if (!Index->getType()->isIntegerTy())
    1587             :     return false;  // Third operand of insertelement must be i32.
    1588      138777 :   return true;
    1589             : }
    1590             : 
    1591             : //===----------------------------------------------------------------------===//
    1592             : //                      ShuffleVectorInst Implementation
    1593             : //===----------------------------------------------------------------------===//
    1594             : 
    1595       61718 : ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
    1596             :                                      const Twine &Name,
    1597       61718 :                                      Instruction *InsertBefore)
    1598       61718 : : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
    1599       61718 :                 cast<VectorType>(Mask->getType())->getNumElements()),
    1600             :               ShuffleVector,
    1601             :               OperandTraits<ShuffleVectorInst>::op_begin(this),
    1602             :               OperandTraits<ShuffleVectorInst>::operands(this),
    1603      123436 :               InsertBefore) {
    1604             :   assert(isValidOperands(V1, V2, Mask) &&
    1605             :          "Invalid shuffle vector instruction operands!");
    1606             :   Op<0>() = V1;
    1607             :   Op<1>() = V2;
    1608             :   Op<2>() = Mask;
    1609       61718 :   setName(Name);
    1610       61718 : }
    1611             : 
    1612           0 : ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
    1613             :                                      const Twine &Name,
    1614           0 :                                      BasicBlock *InsertAtEnd)
    1615           0 : : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
    1616           0 :                 cast<VectorType>(Mask->getType())->getNumElements()),
    1617             :               ShuffleVector,
    1618             :               OperandTraits<ShuffleVectorInst>::op_begin(this),
    1619             :               OperandTraits<ShuffleVectorInst>::operands(this),
    1620           0 :               InsertAtEnd) {
    1621             :   assert(isValidOperands(V1, V2, Mask) &&
    1622             :          "Invalid shuffle vector instruction operands!");
    1623             : 
    1624             :   Op<0>() = V1;
    1625             :   Op<1>() = V2;
    1626             :   Op<2>() = Mask;
    1627           0 :   setName(Name);
    1628           0 : }
    1629             : 
    1630      182185 : bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
    1631             :                                         const Value *Mask) {
    1632             :   // V1 and V2 must be vectors of the same type.
    1633      364370 :   if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
    1634             :     return false;
    1635             :   
    1636             :   // Mask must be vector of i32.
    1637      182185 :   auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
    1638      182185 :   if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
    1639             :     return false;
    1640             : 
    1641             :   // Check to see if Mask is valid.
    1642      182185 :   if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
    1643             :     return true;
    1644             : 
    1645             :   if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
    1646       29241 :     unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
    1647     1593802 :     for (Value *Op : MV->operands()) {
    1648             :       if (auto *CI = dyn_cast<ConstantInt>(Op)) {
    1649      569662 :         if (CI->uge(V1Size*2))
    1650             :           return false;
    1651      482829 :       } else if (!isa<UndefValue>(Op)) {
    1652             :         return false;
    1653             :       }
    1654             :     }
    1655             :     return true;
    1656             :   }
    1657             :   
    1658             :   if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
    1659      131931 :     unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
    1660     1838644 :     for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
    1661     1706713 :       if (CDS->getElementAsInteger(i) >= V1Size*2)
    1662             :         return false;
    1663             :     return true;
    1664             :   }
    1665             :   
    1666             :   // The bitcode reader can create a place holder for a forward reference
    1667             :   // used as the shuffle mask. When this occurs, the shuffle mask will
    1668             :   // fall into this case and fail. To avoid this error, do this bit of
    1669             :   // ugliness to allow such a mask pass.
    1670             :   if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
    1671           0 :     if (CE->getOpcode() == Instruction::UserOp1)
    1672             :       return true;
    1673             : 
    1674             :   return false;
    1675             : }
    1676             : 
    1677      116175 : int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) {
    1678             :   assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
    1679             :   if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
    1680       58655 :     return CDS->getElementAsInteger(i);
    1681       57520 :   Constant *C = Mask->getAggregateElement(i);
    1682       57520 :   if (isa<UndefValue>(C))
    1683             :     return -1;
    1684       46013 :   return cast<ConstantInt>(C)->getZExtValue();
    1685             : }
    1686             : 
    1687      103301 : void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
    1688             :                                        SmallVectorImpl<int> &Result) {
    1689      103301 :   unsigned NumElts = Mask->getType()->getVectorNumElements();
    1690             :   
    1691             :   if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
    1692     1599623 :     for (unsigned i = 0; i != NumElts; ++i)
    1693      765382 :       Result.push_back(CDS->getElementAsInteger(i));
    1694             :     return;
    1695             :   }    
    1696     1124178 :   for (unsigned i = 0; i != NumElts; ++i) {
    1697      544868 :     Constant *C = Mask->getAggregateElement(i);
    1698     1356693 :     Result.push_back(isa<UndefValue>(C) ? -1 :
    1699             :                      cast<ConstantInt>(C)->getZExtValue());
    1700             :   }
    1701             : }
    1702             : 
    1703       14365 : bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
    1704             :   assert(!Mask.empty() && "Shuffle mask must contain elements");
    1705             :   bool UsesLHS = false;
    1706             :   bool UsesRHS = false;
    1707      128748 :   for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
    1708      238740 :     if (Mask[i] == -1)
    1709        7744 :       continue;
    1710             :     assert(Mask[i] >= 0 && Mask[i] < (NumElts * 2) &&
    1711             :            "Out-of-bounds shuffle mask element");
    1712      111626 :     UsesLHS |= (Mask[i] < NumElts);
    1713      111626 :     UsesRHS |= (Mask[i] >= NumElts);
    1714      111626 :     if (UsesLHS && UsesRHS)
    1715             :       return false;
    1716             :   }
    1717             :   assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source");
    1718             :   return true;
    1719             : }
    1720             : 
    1721        2488 : bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
    1722        2488 :   if (!isSingleSourceMask(Mask))
    1723             :     return false;
    1724        3287 :   for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
    1725        6382 :     if (Mask[i] == -1)
    1726         330 :       continue;
    1727        2861 :     if (Mask[i] != i && Mask[i] != (NumElts + i))
    1728             :       return false;
    1729             :   }
    1730             :   return true;
    1731             : }
    1732             : 
    1733        2395 : bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
    1734        2395 :   if (!isSingleSourceMask(Mask))
    1735             :     return false;
    1736        6719 :   for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
    1737       12818 :     if (Mask[i] == -1)
    1738          24 :       continue;
    1739        6385 :     if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
    1740             :       return false;
    1741             :   }
    1742             :   return true;
    1743             : }
    1744             : 
    1745        1665 : bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
    1746        1665 :   if (!isSingleSourceMask(Mask))
    1747             :     return false;
    1748       14299 :   for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
    1749       26936 :     if (Mask[i] == -1)
    1750           6 :       continue;
    1751       13462 :     if (Mask[i] != 0 && Mask[i] != NumElts)
    1752             :       return false;
    1753             :   }
    1754             :   return true;
    1755             : }
    1756             : 
    1757        6980 : bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
    1758             :   // Select is differentiated from identity. It requires using both sources.
    1759        6980 :   if (isSingleSourceMask(Mask))
    1760             :     return false;
    1761       12346 :   for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
    1762       21974 :     if (Mask[i] == -1)
    1763         146 :       continue;
    1764       10841 :     if (Mask[i] != i && Mask[i] != (NumElts + i))
    1765             :       return false;
    1766             :   }
    1767             :   return true;
    1768             : }
    1769             : 
    1770        1687 : bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
    1771             :   // Example masks that will return true:
    1772             :   // v1 = <a, b, c, d>
    1773             :   // v2 = <e, f, g, h>
    1774             :   // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
    1775             :   // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
    1776             : 
    1777             :   // 1. The number of elements in the mask must be a power-of-2 and at least 2.
    1778        1687 :   int NumElts = Mask.size();
    1779        1687 :   if (NumElts < 2 || !isPowerOf2_32(NumElts))
    1780             :     return false;
    1781             : 
    1782             :   // 2. The first element of the mask must be either a 0 or a 1.
    1783        1682 :   if (Mask[0] != 0 && Mask[0] != 1)
    1784             :     return false;
    1785             : 
    1786             :   // 3. The difference between the first 2 elements must be equal to the
    1787             :   // number of elements in the mask.
    1788        1064 :   if ((Mask[1] - Mask[0]) != NumElts)
    1789             :     return false;
    1790             : 
    1791             :   // 4. The difference between consecutive even-numbered and odd-numbered
    1792             :   // elements must be equal to 2.
    1793         161 :   for (int i = 2; i < NumElts; ++i) {
    1794         136 :     int MaskEltVal = Mask[i];
    1795          68 :     if (MaskEltVal == -1)
    1796             :       return false;
    1797         136 :     int MaskEltPrevVal = Mask[i - 2];
    1798          68 :     if (MaskEltVal - MaskEltPrevVal != 2)
    1799             :       return false;
    1800             :   }
    1801             :   return true;
    1802             : }
    1803             : 
    1804             : 
    1805             : //===----------------------------------------------------------------------===//
    1806             : //                             InsertValueInst Class
    1807             : //===----------------------------------------------------------------------===//
    1808             : 
    1809       12943 : void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, 
    1810             :                            const Twine &Name) {
    1811             :   assert(getNumOperands() == 2 && "NumOperands not initialized?");
    1812             : 
    1813             :   // There's no fundamental reason why we require at least one index
    1814             :   // (other than weirdness with &*IdxBegin being invalid; see
    1815             :   // getelementptr's init routine for example). But there's no
    1816             :   // present need to support it.
    1817             :   assert(!Idxs.empty() && "InsertValueInst must have at least one index");
    1818             : 
    1819             :   assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
    1820             :          Val->getType() && "Inserted value must match indexed type!");
    1821             :   Op<0>() = Agg;
    1822             :   Op<1>() = Val;
    1823             : 
    1824       25886 :   Indices.append(Idxs.begin(), Idxs.end());
    1825       12943 :   setName(Name);
    1826       12943 : }
    1827             : 
    1828         850 : InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
    1829             :   : Instruction(IVI.getType(), InsertValue,
    1830             :                 OperandTraits<InsertValueInst>::op_begin(this), 2),
    1831        1700 :     Indices(IVI.Indices) {
    1832             :   Op<0>() = IVI.getOperand(0);
    1833             :   Op<1>() = IVI.getOperand(1);
    1834         850 :   SubclassOptionalData = IVI.SubclassOptionalData;
    1835         850 : }
    1836             : 
    1837             : //===----------------------------------------------------------------------===//
    1838             : //                             ExtractValueInst Class
    1839             : //===----------------------------------------------------------------------===//
    1840             : 
    1841       74195 : void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
    1842             :   assert(getNumOperands() == 1 && "NumOperands not initialized?");
    1843             : 
    1844             :   // There's no fundamental reason why we require at least one index.
    1845             :   // But there's no present need to support it.
    1846             :   assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
    1847             : 
    1848      148390 :   Indices.append(Idxs.begin(), Idxs.end());
    1849       74195 :   setName(Name);
    1850       74195 : }
    1851             : 
    1852        9524 : ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
    1853             :   : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
    1854       19048 :     Indices(EVI.Indices) {
    1855        9524 :   SubclassOptionalData = EVI.SubclassOptionalData;
    1856        9524 : }
    1857             : 
    1858             : // getIndexedType - Returns the type of the element that would be extracted
    1859             : // with an extractvalue instruction with the specified parameters.
    1860             : //
    1861             : // A null type is returned if the indices are invalid for the specified
    1862             : // pointer type.
    1863             : //
    1864      281842 : Type *ExtractValueInst::getIndexedType(Type *Agg,
    1865             :                                        ArrayRef<unsigned> Idxs) {
    1866      848392 :   for (unsigned Index : Idxs) {
    1867             :     // We can't use CompositeType::indexValid(Index) here.
    1868             :     // indexValid() always returns true for arrays because getelementptr allows
    1869             :     // out-of-bounds indices. Since we don't allow those for extractvalue and
    1870             :     // insertvalue we need to check array indexing manually.
    1871             :     // Since the only other types we can index into are struct types it's just
    1872             :     // as easy to check those manually as well.
    1873             :     if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
    1874      166044 :       if (Index >= AT->getNumElements())
    1875             :         return nullptr;
    1876             :     } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
    1877      117233 :       if (Index >= ST->getNumElements())
    1878             :         return nullptr;
    1879             :     } else {
    1880             :       // Not a valid type to index into.
    1881             :       return nullptr;
    1882             :     }
    1883             : 
    1884      283275 :     Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
    1885             :   }
    1886             :   return const_cast<Type*>(Agg);
    1887             : }
    1888             : 
    1889             : //===----------------------------------------------------------------------===//
    1890             : //                             BinaryOperator Class
    1891             : //===----------------------------------------------------------------------===//
    1892             : 
    1893      878636 : BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
    1894             :                                Type *Ty, const Twine &Name,
    1895      878636 :                                Instruction *InsertBefore)
    1896             :   : Instruction(Ty, iType,
    1897             :                 OperandTraits<BinaryOperator>::op_begin(this),
    1898             :                 OperandTraits<BinaryOperator>::operands(this),
    1899     1757272 :                 InsertBefore) {
    1900             :   Op<0>() = S1;
    1901             :   Op<1>() = S2;
    1902      878636 :   setName(Name);
    1903      878636 :   AssertOK();
    1904      878636 : }
    1905             : 
    1906           0 : BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 
    1907             :                                Type *Ty, const Twine &Name,
    1908           0 :                                BasicBlock *InsertAtEnd)
    1909             :   : Instruction(Ty, iType,
    1910             :                 OperandTraits<BinaryOperator>::op_begin(this),
    1911             :                 OperandTraits<BinaryOperator>::operands(this),
    1912           0 :                 InsertAtEnd) {
    1913             :   Op<0>() = S1;
    1914             :   Op<1>() = S2;
    1915           0 :   setName(Name);
    1916           0 :   AssertOK();
    1917           0 : }
    1918             : 
    1919      878636 : void BinaryOperator::AssertOK() {
    1920             :   Value *LHS = getOperand(0), *RHS = getOperand(1);
    1921             :   (void)LHS; (void)RHS; // Silence warnings.
    1922             :   assert(LHS->getType() == RHS->getType() &&
    1923             :          "Binary operator operand types must match!");
    1924             : #ifndef NDEBUG
    1925             :   switch (getOpcode()) {
    1926             :   case Add: case Sub:
    1927             :   case Mul:
    1928             :     assert(getType() == LHS->getType() &&
    1929             :            "Arithmetic operation should return same type as operands!");
    1930             :     assert(getType()->isIntOrIntVectorTy() &&
    1931             :            "Tried to create an integer operation on a non-integer type!");
    1932             :     break;
    1933             :   case FAdd: case FSub:
    1934             :   case FMul:
    1935             :     assert(getType() == LHS->getType() &&
    1936             :            "Arithmetic operation should return same type as operands!");
    1937             :     assert(getType()->isFPOrFPVectorTy() &&
    1938             :            "Tried to create a floating-point operation on a "
    1939             :            "non-floating-point type!");
    1940             :     break;
    1941             :   case UDiv: 
    1942             :   case SDiv: 
    1943             :     assert(getType() == LHS->getType() &&
    1944             :            "Arithmetic operation should return same type as operands!");
    1945             :     assert(getType()->isIntOrIntVectorTy() &&
    1946             :            "Incorrect operand type (not integer) for S/UDIV");
    1947             :     break;
    1948             :   case FDiv:
    1949             :     assert(getType() == LHS->getType() &&
    1950             :            "Arithmetic operation should return same type as operands!");
    1951             :     assert(getType()->isFPOrFPVectorTy() &&
    1952             :            "Incorrect operand type (not floating point) for FDIV");
    1953             :     break;
    1954             :   case URem: 
    1955             :   case SRem: 
    1956             :     assert(getType() == LHS->getType() &&
    1957             :            "Arithmetic operation should return same type as operands!");
    1958             :     assert(getType()->isIntOrIntVectorTy() &&
    1959             :            "Incorrect operand type (not integer) for S/UREM");
    1960             :     break;
    1961             :   case FRem:
    1962             :     assert(getType() == LHS->getType() &&
    1963             :            "Arithmetic operation should return same type as operands!");
    1964             :     assert(getType()->isFPOrFPVectorTy() &&
    1965             :            "Incorrect operand type (not floating point) for FREM");
    1966             :     break;
    1967             :   case Shl:
    1968             :   case LShr:
    1969             :   case AShr:
    1970             :     assert(getType() == LHS->getType() &&
    1971             :            "Shift operation should return same type as operands!");
    1972             :     assert(getType()->isIntOrIntVectorTy() &&
    1973             :            "Tried to create a shift operation on a non-integral type!");
    1974             :     break;
    1975             :   case And: case Or:
    1976             :   case Xor:
    1977             :     assert(getType() == LHS->getType() &&
    1978             :            "Logical operation should return same type as operands!");
    1979             :     assert(getType()->isIntOrIntVectorTy() &&
    1980             :            "Tried to create a logical operation on a non-integral type!");
    1981             :     break;
    1982             :   default: llvm_unreachable("Invalid opcode provided");
    1983             :   }
    1984             : #endif
    1985      878636 : }
    1986             : 
    1987      867314 : BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
    1988             :                                        const Twine &Name,
    1989             :                                        Instruction *InsertBefore) {
    1990             :   assert(S1->getType() == S2->getType() &&
    1991             :          "Cannot create binary operator with two operands of differing type!");
    1992     1734628 :   return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
    1993             : }
    1994             : 
    1995         376 : BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
    1996             :                                        const Twine &Name,
    1997             :                                        BasicBlock *InsertAtEnd) {
    1998         376 :   BinaryOperator *Res = Create(Op, S1, S2, Name);
    1999         376 :   InsertAtEnd->getInstList().push_back(Res);
    2000         376 :   return Res;
    2001             : }
    2002             : 
    2003         968 : BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
    2004             :                                           Instruction *InsertBefore) {
    2005         968 :   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
    2006             :   return new BinaryOperator(Instruction::Sub,
    2007             :                             zero, Op,
    2008        1936 :                             Op->getType(), Name, InsertBefore);
    2009             : }
    2010             : 
    2011           0 : BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
    2012             :                                           BasicBlock *InsertAtEnd) {
    2013           0 :   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
    2014             :   return new BinaryOperator(Instruction::Sub,
    2015             :                             zero, Op,
    2016           0 :                             Op->getType(), Name, InsertAtEnd);
    2017             : }
    2018             : 
    2019           0 : BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
    2020             :                                              Instruction *InsertBefore) {
    2021           0 :   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
    2022           0 :   return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
    2023             : }
    2024             : 
    2025           0 : BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
    2026             :                                              BasicBlock *InsertAtEnd) {
    2027           0 :   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
    2028           0 :   return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
    2029             : }
    2030             : 
    2031           0 : BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
    2032             :                                              Instruction *InsertBefore) {
    2033           0 :   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
    2034           0 :   return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
    2035             : }
    2036             : 
    2037           0 : BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
    2038             :                                              BasicBlock *InsertAtEnd) {
    2039           0 :   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
    2040           0 :   return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
    2041             : }
    2042             : 
    2043         930 : BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
    2044             :                                            Instruction *InsertBefore) {
    2045         930 :   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
    2046             :   return new BinaryOperator(Instruction::FSub, zero, Op,
    2047        1860 :                             Op->getType(), Name, InsertBefore);
    2048             : }
    2049             : 
    2050           0 : BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
    2051             :                                            BasicBlock *InsertAtEnd) {
    2052           0 :   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
    2053             :   return new BinaryOperator(Instruction::FSub, zero, Op,
    2054           0 :                             Op->getType(), Name, InsertAtEnd);
    2055             : }
    2056             : 
    2057        9424 : BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
    2058             :                                           Instruction *InsertBefore) {
    2059        9424 :   Constant *C = Constant::getAllOnesValue(Op->getType());
    2060             :   return new BinaryOperator(Instruction::Xor, Op, C,
    2061       18848 :                             Op->getType(), Name, InsertBefore);
    2062             : }
    2063             : 
    2064           0 : BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
    2065             :                                           BasicBlock *InsertAtEnd) {
    2066           0 :   Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
    2067             :   return new BinaryOperator(Instruction::Xor, Op, AllOnes,
    2068           0 :                             Op->getType(), Name, InsertAtEnd);
    2069             : }
    2070             : 
    2071             : // isConstantAllOnes - Helper function for several functions below
    2072             : static inline bool isConstantAllOnes(const Value *V) {
    2073             :   if (const Constant *C = dyn_cast<Constant>(V))
    2074        8122 :     return C->isAllOnesValue();
    2075             :   return false;
    2076             : }
    2077             : 
    2078     4814330 : bool BinaryOperator::isNeg(const Value *V) {
    2079             :   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
    2080      187965 :     if (Bop->getOpcode() == Instruction::Sub)
    2081             :       if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
    2082        4469 :         return C->isNegativeZeroValue();
    2083             :   return false;
    2084             : }
    2085             : 
    2086     4767654 : bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
    2087             :   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
    2088      175297 :     if (Bop->getOpcode() == Instruction::FSub)
    2089             :       if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
    2090         424 :         if (!IgnoreZeroSign)
    2091         369 :           IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
    2092         424 :         return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
    2093             :       }
    2094             :   return false;
    2095             : }
    2096             : 
    2097     5108078 : bool BinaryOperator::isNot(const Value *V) {
    2098             :   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
    2099      187130 :     return (Bop->getOpcode() == Instruction::Xor &&
    2100           2 :             (isConstantAllOnes(Bop->getOperand(1)) ||
    2101             :              isConstantAllOnes(Bop->getOperand(0))));
    2102             :   return false;
    2103             : }
    2104             : 
    2105         366 : Value *BinaryOperator::getNegArgument(Value *BinOp) {
    2106         366 :   return cast<BinaryOperator>(BinOp)->getOperand(1);
    2107             : }
    2108             : 
    2109           0 : const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
    2110           0 :   return getNegArgument(const_cast<Value*>(BinOp));
    2111             : }
    2112             : 
    2113          97 : Value *BinaryOperator::getFNegArgument(Value *BinOp) {
    2114          97 :   return cast<BinaryOperator>(BinOp)->getOperand(1);
    2115             : }
    2116             : 
    2117          42 : const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
    2118          42 :   return getFNegArgument(const_cast<Value*>(BinOp));
    2119             : }
    2120             : 
    2121        1801 : Value *BinaryOperator::getNotArgument(Value *BinOp) {
    2122             :   assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
    2123             :   BinaryOperator *BO = cast<BinaryOperator>(BinOp);
    2124             :   Value *Op0 = BO->getOperand(0);
    2125             :   Value *Op1 = BO->getOperand(1);
    2126           3 :   if (isConstantAllOnes(Op0)) return Op1;
    2127             : 
    2128             :   assert(isConstantAllOnes(Op1));
    2129             :   return Op0;
    2130             : }
    2131             : 
    2132          22 : const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
    2133          22 :   return getNotArgument(const_cast<Value*>(BinOp));
    2134             : }
    2135             : 
    2136             : // Exchange the two operands to this instruction. This instruction is safe to
    2137             : // use on any binary instruction and does not modify the semantics of the
    2138             : // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
    2139             : // is changed.
    2140       81833 : bool BinaryOperator::swapOperands() {
    2141             :   if (!isCommutative())
    2142             :     return true; // Can't commute operands
    2143       81833 :   Op<0>().swap(Op<1>());
    2144       81833 :   return false;
    2145             : }
    2146             : 
    2147             : //===----------------------------------------------------------------------===//
    2148             : //                             FPMathOperator Class
    2149             : //===----------------------------------------------------------------------===//
    2150             : 
    2151          70 : float FPMathOperator::getFPAccuracy() const {
    2152             :   const MDNode *MD =
    2153             :       cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
    2154          70 :   if (!MD)
    2155             :     return 0.0;
    2156             :   ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
    2157          70 :   return Accuracy->getValueAPF().convertToFloat();
    2158             : }
    2159             : 
    2160             : //===----------------------------------------------------------------------===//
    2161             : //                                CastInst Class
    2162             : //===----------------------------------------------------------------------===//
    2163             : 
    2164             : // Just determine if this cast only deals with integral->integral conversion.
    2165          15 : bool CastInst::isIntegerCast() const {
    2166          15 :   switch (getOpcode()) {
    2167             :     default: return false;
    2168          14 :     case Instruction::ZExt:
    2169             :     case Instruction::SExt:
    2170             :     case Instruction::Trunc:
    2171          14 :       return true;
    2172           1 :     case Instruction::BitCast:
    2173           2 :       return getOperand(0)->getType()->isIntegerTy() &&
    2174           0 :         getType()->isIntegerTy();
    2175             :   }
    2176             : }
    2177             : 
    2178         277 : bool CastInst::isLosslessCast() const {
    2179             :   // Only BitCast can be lossless, exit fast if we're not BitCast
    2180         277 :   if (getOpcode() != Instruction::BitCast)
    2181             :     return false;
    2182             : 
    2183             :   // Identity cast is always lossless
    2184          10 :   Type *SrcTy = getOperand(0)->getType();
    2185          10 :   Type *DstTy = getType();
    2186          10 :   if (SrcTy == DstTy)
    2187             :     return true;
    2188             :   
    2189             :   // Pointer to pointer is always lossless.
    2190          10 :   if (SrcTy->isPointerTy())
    2191          10 :     return DstTy->isPointerTy();
    2192             :   return false;  // Other types have no identity values
    2193             : }
    2194             : 
    2195             : /// This function determines if the CastInst does not require any bits to be
    2196             : /// changed in order to effect the cast. Essentially, it identifies cases where
    2197             : /// no code gen is necessary for the cast, hence the name no-op cast.  For 
    2198             : /// example, the following are all no-op casts:
    2199             : /// # bitcast i32* %x to i8*
    2200             : /// # bitcast <2 x i32> %x to <4 x i16> 
    2201             : /// # ptrtoint i32* %x to i32     ; on 32-bit plaforms only
    2202             : /// Determine if the described cast is a no-op.
    2203       18838 : bool CastInst::isNoopCast(Instruction::CastOps Opcode,
    2204             :                           Type *SrcTy,
    2205             :                           Type *DestTy,
    2206             :                           const DataLayout &DL) {
    2207       18838 :   switch (Opcode) {
    2208           0 :     default: llvm_unreachable("Invalid CastOp");
    2209             :     case Instruction::Trunc:
    2210             :     case Instruction::ZExt:
    2211             :     case Instruction::SExt: 
    2212             :     case Instruction::FPTrunc:
    2213             :     case Instruction::FPExt:
    2214             :     case Instruction::UIToFP:
    2215             :     case Instruction::SIToFP:
    2216             :     case Instruction::FPToUI:
    2217             :     case Instruction::FPToSI:
    2218             :     case Instruction::AddrSpaceCast:
    2219             :       // TODO: Target informations may give a more accurate answer here.
    2220             :       return false;
    2221        5073 :     case Instruction::BitCast:
    2222        5073 :       return true;  // BitCast never modifies bits.
    2223        2297 :     case Instruction::PtrToInt:
    2224        2297 :       return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
    2225        2297 :              DestTy->getScalarSizeInBits();
    2226         782 :     case Instruction::IntToPtr:
    2227         782 :       return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
    2228         782 :              SrcTy->getScalarSizeInBits();
    2229             :   }
    2230             : }
    2231             : 
    2232       18831 : bool CastInst::isNoopCast(const DataLayout &DL) const {
    2233       56493 :   return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
    2234             : }
    2235             : 
    2236             : /// This function determines if a pair of casts can be eliminated and what
    2237             : /// opcode should be used in the elimination. This assumes that there are two
    2238             : /// instructions like this:
    2239             : /// *  %F = firstOpcode SrcTy %x to MidTy
    2240             : /// *  %S = secondOpcode MidTy %F to DstTy
    2241             : /// The function returns a resultOpcode so these two casts can be replaced with:
    2242             : /// *  %Replacement = resultOpcode %SrcTy %x to DstTy
    2243             : /// If no such cast is permitted, the function returns 0.
    2244      553959 : unsigned CastInst::isEliminableCastPair(
    2245             :   Instruction::CastOps firstOp, Instruction::CastOps secondOp,
    2246             :   Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
    2247             :   Type *DstIntPtrTy) {
    2248             :   // Define the 144 possibilities for these two cast instructions. The values
    2249             :   // in this matrix determine what to do in a given situation and select the
    2250             :   // case in the switch below.  The rows correspond to firstOp, the columns 
    2251             :   // correspond to secondOp.  In looking at the table below, keep in mind
    2252             :   // the following cast properties:
    2253             :   //
    2254             :   //          Size Compare       Source               Destination
    2255             :   // Operator  Src ? Size   Type       Sign         Type       Sign
    2256             :   // -------- ------------ -------------------   ---------------------
    2257             :   // TRUNC         >       Integer      Any        Integral     Any
    2258             :   // ZEXT          <       Integral   Unsigned     Integer      Any
    2259             :   // SEXT          <       Integral    Signed      Integer      Any
    2260             :   // FPTOUI       n/a      FloatPt      n/a        Integral   Unsigned
    2261             :   // FPTOSI       n/a      FloatPt      n/a        Integral    Signed
    2262             :   // UITOFP       n/a      Integral   Unsigned     FloatPt      n/a
    2263             :   // SITOFP       n/a      Integral    Signed      FloatPt      n/a
    2264             :   // FPTRUNC       >       FloatPt      n/a        FloatPt      n/a
    2265             :   // FPEXT         <       FloatPt      n/a        FloatPt      n/a
    2266             :   // PTRTOINT     n/a      Pointer      n/a        Integral   Unsigned
    2267             :   // INTTOPTR     n/a      Integral   Unsigned     Pointer      n/a
    2268             :   // BITCAST       =       FirstClass   n/a       FirstClass    n/a
    2269             :   // ADDRSPCST    n/a      Pointer      n/a        Pointer      n/a
    2270             :   //
    2271             :   // NOTE: some transforms are safe, but we consider them to be non-profitable.
    2272             :   // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
    2273             :   // into "fptoui double to i64", but this loses information about the range
    2274             :   // of the produced value (we no longer know the top-part is all zeros).
    2275             :   // Further this conversion is often much more expensive for typical hardware,
    2276             :   // and causes issues when building libgcc.  We disallow fptosi+sext for the
    2277             :   // same reason.
    2278             :   const unsigned numCastOps =
    2279             :     Instruction::CastOpsEnd - Instruction::CastOpsBegin;
    2280             :   static const uint8_t CastResults[numCastOps][numCastOps] = {
    2281             :     // T        F  F  U  S  F  F  P  I  B  A  -+
    2282             :     // R  Z  S  P  P  I  I  T  P  2  N  T  S   |
    2283             :     // U  E  E  2  2  2  2  R  E  I  T  C  C   +- secondOp
    2284             :     // N  X  X  U  S  F  F  N  X  N  2  V  V   |
    2285             :     // C  T  T  I  I  P  P  C  T  T  P  T  T  -+
    2286             :     {  1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc         -+
    2287             :     {  8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt           |
    2288             :     {  8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt           |
    2289             :     {  0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI         |
    2290             :     {  0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI         |
    2291             :     { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP         +- firstOp
    2292             :     { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP         |
    2293             :     { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc        |
    2294             :     { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt          |
    2295             :     {  1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt       |
    2296             :     { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr       |
    2297             :     {  5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast        |
    2298             :     {  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
    2299             :   };
    2300             : 
    2301             :   // TODO: This logic could be encoded into the table above and handled in the
    2302             :   // switch below.
    2303             :   // If either of the casts are a bitcast from scalar to vector, disallow the
    2304             :   // merging. However, any pair of bitcasts are allowed.
    2305      553959 :   bool IsFirstBitcast  = (firstOp == Instruction::BitCast);
    2306      553959 :   bool IsSecondBitcast = (secondOp == Instruction::BitCast);
    2307      553959 :   bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
    2308             : 
    2309             :   // Check if any of the casts convert scalars <-> vectors.
    2310     1096792 :   if ((IsFirstBitcast  && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
    2311      541337 :       (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
    2312         137 :     if (!AreBothBitcasts)
    2313             :       return 0;
    2314             : 
    2315      553874 :   int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
    2316      553874 :                             [secondOp-Instruction::CastOpsBegin];
    2317      553874 :   switch (ElimCase) {
    2318             :     case 0: 
    2319             :       // Categorically disallowed.
    2320             :       return 0;
    2321      540950 :     case 1: 
    2322             :       // Allowed, use first cast's opcode.
    2323      540950 :       return firstOp;
    2324          85 :     case 2: 
    2325             :       // Allowed, use second cast's opcode.
    2326          85 :       return secondOp;
    2327         148 :     case 3: 
    2328             :       // No-op cast in second op implies firstOp as long as the DestTy
    2329             :       // is integer and we are not converting between a vector and a
    2330             :       // non-vector type.
    2331         229 :       if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
    2332             :         return firstOp;
    2333             :       return 0;
    2334          98 :     case 4:
    2335             :       // No-op cast in second op implies firstOp as long as the DestTy
    2336             :       // is floating point.
    2337             :       if (DstTy->isFloatingPointTy())
    2338             :         return firstOp;
    2339             :       return 0;
    2340         221 :     case 5: 
    2341             :       // No-op cast in first op implies secondOp as long as the SrcTy
    2342             :       // is an integer.
    2343         221 :       if (SrcTy->isIntegerTy())
    2344             :         return secondOp;
    2345         221 :       return 0;
    2346           0 :     case 6:
    2347             :       // No-op cast in first op implies secondOp as long as the SrcTy
    2348             :       // is a floating point.
    2349             :       if (SrcTy->isFloatingPointTy())
    2350             :         return secondOp;
    2351             :       return 0;
    2352        6559 :     case 7: {
    2353             :       // Cannot simplify if address spaces are different!
    2354        6559 :       if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
    2355             :         return 0;
    2356             : 
    2357        6554 :       unsigned MidSize = MidTy->getScalarSizeInBits();
    2358             :       // We can still fold this without knowing the actual sizes as long we
    2359             :       // know that the intermediate pointer is the largest possible
    2360             :       // pointer size.
    2361             :       // FIXME: Is this always true?
    2362        6554 :       if (MidSize == 64)
    2363             :         return Instruction::BitCast;
    2364             : 
    2365             :       // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
    2366        1234 :       if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
    2367             :         return 0;
    2368        1217 :       unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
    2369        1217 :       if (MidSize >= PtrSize)
    2370             :         return Instruction::BitCast;
    2371           0 :       return 0;
    2372             :     }
    2373         919 :     case 8: {
    2374             :       // ext, trunc -> bitcast,    if the SrcTy and DstTy are same size
    2375             :       // ext, trunc -> ext,        if sizeof(SrcTy) < sizeof(DstTy)
    2376             :       // ext, trunc -> trunc,      if sizeof(SrcTy) > sizeof(DstTy)
    2377         919 :       unsigned SrcSize = SrcTy->getScalarSizeInBits();
    2378         919 :       unsigned DstSize = DstTy->getScalarSizeInBits();
    2379         919 :       if (SrcSize == DstSize)
    2380             :         return Instruction::BitCast;
    2381         126 :       else if (SrcSize < DstSize)
    2382             :         return firstOp;
    2383          73 :       return secondOp;
    2384             :     }
    2385          31 :     case 9:
    2386             :       // zext, sext -> zext, because sext can't sign extend after zext
    2387          31 :       return Instruction::ZExt;
    2388         365 :     case 11: {
    2389             :       // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
    2390         365 :       if (!MidIntPtrTy)
    2391             :         return 0;
    2392         365 :       unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
    2393         365 :       unsigned SrcSize = SrcTy->getScalarSizeInBits();
    2394         365 :       unsigned DstSize = DstTy->getScalarSizeInBits();
    2395         365 :       if (SrcSize <= PtrSize && SrcSize == DstSize)
    2396             :         return Instruction::BitCast;
    2397          47 :       return 0;
    2398             :     }
    2399          78 :     case 12:
    2400             :       // addrspacecast, addrspacecast -> bitcast,       if SrcAS == DstAS
    2401             :       // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
    2402          78 :       if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
    2403             :         return Instruction::AddrSpaceCast;
    2404          22 :       return Instruction::BitCast;
    2405          92 :     case 13:
    2406             :       // FIXME: this state can be merged with (1), but the following assert
    2407             :       // is useful to check the correcteness of the sequence due to semantic
    2408             :       // change of bitcast.
    2409             :       assert(
    2410             :         SrcTy->isPtrOrPtrVectorTy() &&
    2411             :         MidTy->isPtrOrPtrVectorTy() &&
    2412             :         DstTy->isPtrOrPtrVectorTy() &&
    2413             :         SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
    2414             :         MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
    2415             :         "Illegal addrspacecast, bitcast sequence!");
    2416             :       // Allowed, use first cast's opcode
    2417          92 :       return firstOp;
    2418         405 :     case 14:
    2419             :       // bitcast, addrspacecast -> addrspacecast if the element type of
    2420             :       // bitcast's source is the same as that of addrspacecast's destination.
    2421         810 :       if (SrcTy->getScalarType()->getPointerElementType() ==
    2422         405 :           DstTy->getScalarType()->getPointerElementType())
    2423             :         return Instruction::AddrSpaceCast;
    2424         405 :       return 0;
    2425         147 :     case 15:
    2426             :       // FIXME: this state can be merged with (1), but the following assert
    2427             :       // is useful to check the correcteness of the sequence due to semantic
    2428             :       // change of bitcast.
    2429             :       assert(
    2430             :         SrcTy->isIntOrIntVectorTy() &&
    2431             :         MidTy->isPtrOrPtrVectorTy() &&
    2432             :         DstTy->isPtrOrPtrVectorTy() &&
    2433             :         MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
    2434             :         "Illegal inttoptr, bitcast sequence!");
    2435             :       // Allowed, use first cast's opcode
    2436         147 :       return firstOp;
    2437        1295 :     case 16:
    2438             :       // FIXME: this state can be merged with (2), but the following assert
    2439             :       // is useful to check the correcteness of the sequence due to semantic
    2440             :       // change of bitcast.
    2441             :       assert(
    2442             :         SrcTy->isPtrOrPtrVectorTy() &&
    2443             :         MidTy->isPtrOrPtrVectorTy() &&
    2444             :         DstTy->isIntOrIntVectorTy() &&
    2445             :         SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
    2446             :         "Illegal bitcast, ptrtoint sequence!");
    2447             :       // Allowed, use second cast's opcode
    2448        1295 :       return secondOp;
    2449           2 :     case 17:
    2450             :       // (sitofp (zext x)) -> (uitofp x)
    2451           2 :       return Instruction::UIToFP;
    2452           0 :     case 99: 
    2453             :       // Cast combination can't happen (error in input). This is for all cases
    2454             :       // where the MidTy is not the same for the two cast instructions.
    2455           0 :       llvm_unreachable("Invalid Cast Combination");
    2456           0 :     default:
    2457           0 :       llvm_unreachable("Error in CastResults table!!!");
    2458             :   }
    2459             : }
    2460             : 
    2461      676722 : CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, 
    2462             :   const Twine &Name, Instruction *InsertBefore) {
    2463             :   assert(castIsValid(op, S, Ty) && "Invalid cast!");
    2464             :   // Construct and return the appropriate CastInst subclass
    2465      676722 :   switch (op) {
    2466       32940 :   case Trunc:         return new TruncInst         (S, Ty, Name, InsertBefore);
    2467       39531 :   case ZExt:          return new ZExtInst          (S, Ty, Name, InsertBefore);
    2468       36713 :   case SExt:          return new SExtInst          (S, Ty, Name, InsertBefore);
    2469        2698 :   case FPTrunc:       return new FPTruncInst       (S, Ty, Name, InsertBefore);
    2470        6175 :   case FPExt:         return new FPExtInst         (S, Ty, Name, InsertBefore);
    2471        4016 :   case UIToFP:        return new UIToFPInst        (S, Ty, Name, InsertBefore);
    2472        7414 :   case SIToFP:        return new SIToFPInst        (S, Ty, Name, InsertBefore);
    2473        2645 :   case FPToUI:        return new FPToUIInst        (S, Ty, Name, InsertBefore);
    2474        4166 :   case FPToSI:        return new FPToSIInst        (S, Ty, Name, InsertBefore);
    2475       12786 :   case PtrToInt:      return new PtrToIntInst      (S, Ty, Name, InsertBefore);
    2476       22798 :   case IntToPtr:      return new IntToPtrInst      (S, Ty, Name, InsertBefore);
    2477      503794 :   case BitCast:       return new BitCastInst       (S, Ty, Name, InsertBefore);
    2478        1046 :   case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
    2479           0 :   default: llvm_unreachable("Invalid opcode provided");
    2480             :   }
    2481             : }
    2482             : 
    2483           2 : CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
    2484             :   const Twine &Name, BasicBlock *InsertAtEnd) {
    2485             :   assert(castIsValid(op, S, Ty) && "Invalid cast!");
    2486             :   // Construct and return the appropriate CastInst subclass
    2487           2 :   switch (op) {
    2488           1 :   case Trunc:         return new TruncInst         (S, Ty, Name, InsertAtEnd);
    2489           0 :   case ZExt:          return new ZExtInst          (S, Ty, Name, InsertAtEnd);
    2490           0 :   case SExt:          return new SExtInst          (S, Ty, Name, InsertAtEnd);
    2491           0 :   case FPTrunc:       return new FPTruncInst       (S, Ty, Name, InsertAtEnd);
    2492           0 :   case FPExt:         return new FPExtInst         (S, Ty, Name, InsertAtEnd);
    2493           0 :   case UIToFP:        return new UIToFPInst        (S, Ty, Name, InsertAtEnd);
    2494           0 :   case SIToFP:        return new SIToFPInst        (S, Ty, Name, InsertAtEnd);
    2495           0 :   case FPToUI:        return new FPToUIInst        (S, Ty, Name, InsertAtEnd);
    2496           0 :   case FPToSI:        return new FPToSIInst        (S, Ty, Name, InsertAtEnd);
    2497           1 :   case PtrToInt:      return new PtrToIntInst      (S, Ty, Name, InsertAtEnd);
    2498           0 :   case IntToPtr:      return new IntToPtrInst      (S, Ty, Name, InsertAtEnd);
    2499           0 :   case BitCast:       return new BitCastInst       (S, Ty, Name, InsertAtEnd);
    2500           0 :   case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
    2501           0 :   default: llvm_unreachable("Invalid opcode provided");
    2502             :   }
    2503             : }
    2504             : 
    2505         570 : CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, 
    2506             :                                         const Twine &Name,
    2507             :                                         Instruction *InsertBefore) {
    2508         570 :   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
    2509           0 :     return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
    2510         570 :   return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
    2511             : }
    2512             : 
    2513           0 : CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, 
    2514             :                                         const Twine &Name,
    2515             :                                         BasicBlock *InsertAtEnd) {
    2516           0 :   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
    2517           0 :     return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
    2518           0 :   return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
    2519             : }
    2520             : 
    2521         189 : CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, 
    2522             :                                         const Twine &Name,
    2523             :                                         Instruction *InsertBefore) {
    2524         189 :   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
    2525           0 :     return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
    2526         189 :   return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
    2527             : }
    2528             : 
    2529           0 : CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, 
    2530             :                                         const Twine &Name,
    2531             :                                         BasicBlock *InsertAtEnd) {
    2532           0 :   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
    2533           0 :     return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
    2534           0 :   return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
    2535             : }
    2536             : 
    2537         737 : CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
    2538             :                                          const Twine &Name,
    2539             :                                          Instruction *InsertBefore) {
    2540         737 :   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
    2541         402 :     return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
    2542         335 :   return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
    2543             : }
    2544             : 
    2545           1 : CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
    2546             :                                          const Twine &Name, 
    2547             :                                          BasicBlock *InsertAtEnd) {
    2548           1 :   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
    2549           0 :     return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
    2550           1 :   return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
    2551             : }
    2552             : 
    2553           1 : CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
    2554             :                                       const Twine &Name,
    2555             :                                       BasicBlock *InsertAtEnd) {
    2556             :   assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
    2557             :   assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
    2558             :          "Invalid cast");
    2559             :   assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
    2560             :   assert((!Ty->isVectorTy() ||
    2561             :           Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
    2562             :          "Invalid cast");
    2563             : 
    2564           1 :   if (Ty->isIntOrIntVectorTy())
    2565           1 :     return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
    2566             : 
    2567           0 :   return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
    2568             : }
    2569             : 
    2570             : /// Create a BitCast or a PtrToInt cast instruction
    2571       33420 : CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
    2572             :                                       const Twine &Name,
    2573             :                                       Instruction *InsertBefore) {
    2574             :   assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
    2575             :   assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
    2576             :          "Invalid cast");
    2577             :   assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
    2578             :   assert((!Ty->isVectorTy() ||
    2579             :           Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
    2580             :          "Invalid cast");
    2581             : 
    2582       33420 :   if (Ty->isIntOrIntVectorTy())
    2583        1071 :     return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
    2584             : 
    2585       32349 :   return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
    2586             : }
    2587             : 
    2588           0 : CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
    2589             :   Value *S, Type *Ty,
    2590             :   const Twine &Name,
    2591             :   BasicBlock *InsertAtEnd) {
    2592             :   assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
    2593             :   assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
    2594             : 
    2595           0 :   if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
    2596           0 :     return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
    2597             : 
    2598           0 :   return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
    2599             : }
    2600             : 
    2601       53427 : CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
    2602             :   Value *S, Type *Ty,
    2603             :   const Twine &Name,
    2604             :   Instruction *InsertBefore) {
    2605             :   assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
    2606             :   assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
    2607             : 
    2608      106854 :   if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
    2609         438 :     return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
    2610             : 
    2611       52989 :   return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
    2612             : }
    2613             : 
    2614         117 : CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
    2615             :                                            const Twine &Name,
    2616             :                                            Instruction *InsertBefore) {
    2617         303 :   if (S->getType()->isPointerTy() && Ty->isIntegerTy())
    2618          58 :     return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
    2619          99 :   if (S->getType()->isIntegerTy() && Ty->isPointerTy())
    2620          37 :     return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
    2621             : 
    2622          22 :   return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
    2623             : }
    2624             : 
    2625       43397 : CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
    2626             :                                       bool isSigned, const Twine &Name,
    2627             :                                       Instruction *InsertBefore) {
    2628             :   assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
    2629             :          "Invalid integer cast");
    2630       43397 :   unsigned SrcBits = C->getType()->getScalarSizeInBits();
    2631       43397 :   unsigned DstBits = Ty->getScalarSizeInBits();
    2632             :   Instruction::CastOps opcode =
    2633       43397 :     (SrcBits == DstBits ? Instruction::BitCast :
    2634       43397 :      (SrcBits > DstBits ? Instruction::Trunc :
    2635       29300 :       (isSigned ? Instruction::SExt : Instruction::ZExt)));
    2636       43397 :   return Create(opcode, C, Ty, Name, InsertBefore);
    2637             : }
    2638             : 
    2639           0 : CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, 
    2640             :                                       bool isSigned, const Twine &Name,
    2641             :                                       BasicBlock *InsertAtEnd) {
    2642             :   assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
    2643             :          "Invalid cast");
    2644           0 :   unsigned SrcBits = C->getType()->getScalarSizeInBits();
    2645           0 :   unsigned DstBits = Ty->getScalarSizeInBits();
    2646             :   Instruction::CastOps opcode =
    2647           0 :     (SrcBits == DstBits ? Instruction::BitCast :
    2648           0 :      (SrcBits > DstBits ? Instruction::Trunc :
    2649           0 :       (isSigned ? Instruction::SExt : Instruction::ZExt)));
    2650           0 :   return Create(opcode, C, Ty, Name, InsertAtEnd);
    2651             : }
    2652             : 
    2653           9 : CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, 
    2654             :                                  const Twine &Name, 
    2655             :                                  Instruction *InsertBefore) {
    2656             :   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
    2657             :          "Invalid cast");
    2658           9 :   unsigned SrcBits = C->getType()->getScalarSizeInBits();
    2659           9 :   unsigned DstBits = Ty->getScalarSizeInBits();
    2660             :   Instruction::CastOps opcode =
    2661           9 :     (SrcBits == DstBits ? Instruction::BitCast :
    2662           7 :      (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
    2663           9 :   return Create(opcode, C, Ty, Name, InsertBefore);
    2664             : }
    2665             : 
    2666           0 : CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, 
    2667             :                                  const Twine &Name, 
    2668             :                                  BasicBlock *InsertAtEnd) {
    2669             :   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
    2670             :          "Invalid cast");
    2671           0 :   unsigned SrcBits = C->getType()->getScalarSizeInBits();
    2672           0 :   unsigned DstBits = Ty->getScalarSizeInBits();
    2673             :   Instruction::CastOps opcode =
    2674           0 :     (SrcBits == DstBits ? Instruction::BitCast :
    2675           0 :      (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
    2676           0 :   return Create(opcode, C, Ty, Name, InsertAtEnd);
    2677             : }
    2678             : 
    2679             : // Check whether it is valid to call getCastOpcode for these types.
    2680             : // This routine must be kept in sync with getCastOpcode.
    2681           6 : bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
    2682             :   if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
    2683             :     return false;
    2684             : 
    2685           6 :   if (SrcTy == DestTy)
    2686             :     return true;
    2687             : 
    2688           4 :   if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
    2689           3 :     if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
    2690           3 :       if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
    2691             :         // An element by element cast.  Valid if casting the elements is valid.
    2692           3 :         SrcTy = SrcVecTy->getElementType();
    2693           3 :         DestTy = DestVecTy->getElementType();
    2694             :       }
    2695             : 
    2696             :   // Get the bit sizes, we'll need these
    2697           6 :   unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();   // 0 for ptr
    2698           6 :   unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
    2699             : 
    2700             :   // Run through the possibilities ...
    2701           6 :   if (DestTy->isIntegerTy()) {               // Casting to integral
    2702           2 :     if (SrcTy->isIntegerTy())                // Casting from integral
    2703             :         return true;
    2704             :     if (SrcTy->isFloatingPointTy())   // Casting from floating pt
    2705             :       return true;
    2706           0 :     if (SrcTy->isVectorTy())          // Casting from vector
    2707           0 :       return DestBits == SrcBits;
    2708             :                                       // Casting from something else
    2709           0 :     return SrcTy->isPointerTy();
    2710             :   } 
    2711             :   if (DestTy->isFloatingPointTy()) {  // Casting to floating pt
    2712           0 :     if (SrcTy->isIntegerTy())                // Casting from integral
    2713             :       return true;
    2714             :     if (SrcTy->isFloatingPointTy())   // Casting from floating pt
    2715             :       return true;
    2716           0 :     if (SrcTy->isVectorTy())          // Casting from vector
    2717           0 :       return DestBits == SrcBits;
    2718             :                                     // Casting from something else
    2719             :     return false;
    2720             :   }
    2721           4 :   if (DestTy->isVectorTy())         // Casting to vector
    2722           1 :     return DestBits == SrcBits;
    2723           3 :   if (DestTy->isPointerTy()) {        // Casting to pointer
    2724           1 :     if (SrcTy->isPointerTy())                // Casting from pointer
    2725             :       return true;
    2726           0 :     return SrcTy->isIntegerTy();             // Casting from integral
    2727             :   } 
    2728           2 :   if (DestTy->isX86_MMXTy()) {
    2729           2 :     if (SrcTy->isVectorTy())
    2730           1 :       return DestBits == SrcBits;       // 64-bit vector to MMX
    2731             :     return false;
    2732             :   }                                    // Casting to something else
    2733             :   return false;
    2734             : }
    2735             : 
    2736        5082 : bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
    2737             :   if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
    2738             :     return false;
    2739             : 
    2740        5008 :   if (SrcTy == DestTy)
    2741             :     return true;
    2742             : 
    2743         113 :   if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
    2744          69 :     if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
    2745          69 :       if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
    2746             :         // An element by element cast. Valid if casting the elements is valid.
    2747          20 :         SrcTy = SrcVecTy->getElementType();
    2748          20 :         DestTy = DestVecTy->getElementType();
    2749             :       }
    2750             :     }
    2751             :   }
    2752             : 
    2753             :   if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
    2754             :     if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
    2755        1738 :       return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
    2756             :     }
    2757             :   }
    2758             : 
    2759         328 :   unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();   // 0 for ptr
    2760         328 :   unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
    2761             : 
    2762             :   // Could still have vectors of pointers if the number of elements doesn't
    2763             :   // match
    2764         328 :   if (SrcBits == 0 || DestBits == 0)
    2765             :     return false;
    2766             : 
    2767         304 :   if (SrcBits != DestBits)
    2768             :     return false;
    2769             : 
    2770         368 :   if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
    2771             :     return false;
    2772             : 
    2773             :   return true;
    2774             : }
    2775             : 
    2776        5470 : bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
    2777             :                                           const DataLayout &DL) {
    2778             :   // ptrtoint and inttoptr are not allowed on non-integral pointers
    2779             :   if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
    2780             :     if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
    2781         426 :       return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
    2782         210 :               !DL.isNonIntegralPointerType(PtrTy));
    2783             :   if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
    2784             :     if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
    2785         676 :       return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
    2786         334 :               !DL.isNonIntegralPointerType(PtrTy));
    2787             : 
    2788        4912 :   return isBitCastable(SrcTy, DestTy);
    2789             : }
    2790             : 
    2791             : // Provide a way to get a "cast" where the cast opcode is inferred from the
    2792             : // types and size of the operand. This, basically, is a parallel of the
    2793             : // logic in the castIsValid function below.  This axiom should hold:
    2794             : //   castIsValid( getCastOpcode(Val, Ty), Val, Ty)
    2795             : // should not assert in castIsValid. In other words, this produces a "correct"
    2796             : // casting opcode for the arguments passed to it.
    2797             : // This routine must be kept in sync with isCastable.
    2798             : Instruction::CastOps
    2799      450708 : CastInst::getCastOpcode(
    2800             :   const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
    2801      450708 :   Type *SrcTy = Src->getType();
    2802             : 
    2803             :   assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
    2804             :          "Only first class types are castable!");
    2805             : 
    2806      450708 :   if (SrcTy == DestTy)
    2807             :     return BitCast;
    2808             : 
    2809             :   // FIXME: Check address space sizes here
    2810             :   if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
    2811             :     if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
    2812          13 :       if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
    2813             :         // An element by element cast.  Find the appropriate opcode based on the
    2814             :         // element types.
    2815          13 :         SrcTy = SrcVecTy->getElementType();
    2816          13 :         DestTy = DestVecTy->getElementType();
    2817             :       }
    2818             : 
    2819             :   // Get the bit sizes, we'll need these
    2820      387080 :   unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();   // 0 for ptr
    2821      387080 :   unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
    2822             : 
    2823             :   // Run through the possibilities ...
    2824      387080 :   if (DestTy->isIntegerTy()) {                      // Casting to integral
    2825      378963 :     if (SrcTy->isIntegerTy()) {                     // Casting from integral
    2826      378407 :       if (DestBits < SrcBits)
    2827             :         return Trunc;                               // int -> smaller int
    2828       15397 :       else if (DestBits > SrcBits) {                // its an extension
    2829       15397 :         if (SrcIsSigned)
    2830             :           return SExt;                              // signed -> SEXT
    2831             :         else
    2832          21 :           return ZExt;                              // unsigned -> ZEXT
    2833             :       } else {
    2834             :         return BitCast;                             // Same size, No-op cast
    2835             :       }
    2836             :     } else if (SrcTy->isFloatingPointTy()) {        // Casting from floating pt
    2837           0 :       if (DestIsSigned) 
    2838             :         return FPToSI;                              // FP -> sint
    2839             :       else
    2840           0 :         return FPToUI;                              // FP -> uint 
    2841         556 :     } else if (SrcTy->isVectorTy()) {
    2842             :       assert(DestBits == SrcBits &&
    2843             :              "Casting vector to integer of different width");
    2844             :       return BitCast;                             // Same size, no-op cast
    2845             :     } else {
    2846             :       assert(SrcTy->isPointerTy() &&
    2847             :              "Casting from a value that is not first-class type");
    2848         556 :       return PtrToInt;                              // ptr -> int
    2849             :     }
    2850             :   } else if (DestTy->isFloatingPointTy()) {         // Casting to floating pt
    2851          26 :     if (SrcTy->isIntegerTy()) {                     // Casting from integral
    2852          26 :       if (SrcIsSigned)
    2853             :         return SIToFP;                              // sint -> FP
    2854             :       else
    2855           0 :         return UIToFP;                              // uint -> FP
    2856             :     } else if (SrcTy->isFloatingPointTy()) {        // Casting from floating pt
    2857           0 :       if (DestBits < SrcBits) {
    2858             :         return FPTrunc;                             // FP -> smaller FP
    2859           0 :       } else if (DestBits > SrcBits) {
    2860             :         return FPExt;                               // FP -> larger FP
    2861             :       } else  {
    2862           0 :         return BitCast;                             // same size, no-op cast
    2863             :       }
    2864           0 :     } else if (SrcTy->isVectorTy()) {
    2865             :       assert(DestBits == SrcBits &&
    2866             :              "Casting vector to floating point of different width");
    2867             :       return BitCast;                             // same size, no-op cast
    2868             :     }
    2869           0 :     llvm_unreachable("Casting pointer or non-first class to float");
    2870        8091 :   } else if (DestTy->isVectorTy()) {
    2871             :     assert(DestBits == SrcBits &&
    2872             :            "Illegal cast to vector (wrong type or size)");
    2873             :     return BitCast;
    2874        8091 :   } else if (DestTy->isPointerTy()) {
    2875        8091 :     if (SrcTy->isPointerTy()) {
    2876        7743 :       if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
    2877             :         return AddrSpaceCast;
    2878        7742 :       return BitCast;                               // ptr -> ptr
    2879         348 :     } else if (SrcTy->isIntegerTy()) {
    2880             :       return IntToPtr;                              // int -> ptr
    2881             :     }
    2882           0 :     llvm_unreachable("Casting pointer to other than pointer or int");
    2883           0 :   } else if (DestTy->isX86_MMXTy()) {
    2884           0 :     if (SrcTy->isVectorTy()) {
    2885             :       assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
    2886             :       return BitCast;                               // 64-bit vector to MMX
    2887             :     }
    2888           0 :     llvm_unreachable("Illegal cast to X86_MMX");
    2889             :   }
    2890           0 :   llvm_unreachable("Casting to type that is not first-class");
    2891             : }
    2892             : 
    2893             : //===----------------------------------------------------------------------===//
    2894             : //                    CastInst SubClass Constructors
    2895             : //===----------------------------------------------------------------------===//
    2896             : 
    2897             : /// Check that the construction parameters for a CastInst are correct. This
    2898             : /// could be broken out into the separate constructors but it is useful to have
    2899             : /// it in one place and to eliminate the redundant code for getting the sizes
    2900             : /// of the types involved.
    2901             : bool 
    2902      552959 : CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
    2903             :   // Check for type sanity on the arguments
    2904      552959 :   Type *SrcTy = S->getType();
    2905             : 
    2906             :   if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
    2907             :       SrcTy->isAggregateType() || DstTy->isAggregateType())
    2908             :     return false;
    2909             : 
    2910             :   // Get the size of the types in bits, we'll need this later
    2911      552886 :   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
    2912      552886 :   unsigned DstBitSize = DstTy->getScalarSizeInBits();
    2913             : 
    2914             :   // If these are vector types, get the lengths of the vectors (using zero for
    2915             :   // scalar types means that checking that vector lengths match also checks that
    2916             :   // scalars are not being converted to vectors or vectors to scalars).
    2917             :   unsigned SrcLength = SrcTy->isVectorTy() ?
    2918      552886 :     cast<VectorType>(SrcTy)->getNumElements() : 0;
    2919             :   unsigned DstLength = DstTy->isVectorTy() ?
    2920      552886 :     cast<VectorType>(DstTy)->getNumElements() : 0;
    2921             : 
    2922             :   // Switch on the opcode provided
    2923      552886 :   switch (op) {
    2924             :   default: return false; // This is an input error
    2925       11993 :   case Instruction::Trunc:
    2926       11993 :     return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
    2927       23983 :       SrcLength == DstLength && SrcBitSize > DstBitSize;
    2928       19871 :   case Instruction::ZExt:
    2929       19871 :     return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
    2930       39742 :       SrcLength == DstLength && SrcBitSize < DstBitSize;
    2931       17097 :   case Instruction::SExt: 
    2932       17097 :     return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
    2933       34194 :       SrcLength == DstLength && SrcBitSize < DstBitSize;
    2934        1553 :   case Instruction::FPTrunc:
    2935             :     return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
    2936        1553 :       SrcLength == DstLength && SrcBitSize > DstBitSize;
    2937        2780 :   case Instruction::FPExt:
    2938             :     return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
    2939        2780 :       SrcLength == DstLength && SrcBitSize < DstBitSize;
    2940        8311 :   case Instruction::UIToFP:
    2941             :   case Instruction::SIToFP:
    2942       16622 :     return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
    2943             :       SrcLength == DstLength;
    2944        5362 :   case Instruction::FPToUI:
    2945             :   case Instruction::FPToSI:
    2946        5362 :     return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
    2947             :       SrcLength == DstLength;
    2948        2844 :   case Instruction::PtrToInt:
    2949        2844 :     if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
    2950             :       return false;
    2951             :     if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
    2952          28 :       if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
    2953             :         return false;
    2954        5686 :     return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
    2955        2359 :   case Instruction::IntToPtr:
    2956        2359 :     if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
    2957             :       return false;
    2958             :     if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
    2959          17 :       if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
    2960             :         return false;
    2961        4718 :     return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
    2962      479981 :   case Instruction::BitCast: {
    2963             :     PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
    2964             :     PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
    2965             : 
    2966             :     // BitCast implies a no-op cast of type only. No bits change.
    2967             :     // However, you can't cast pointers to anything but pointers.
    2968      479981 :     if (!SrcPtrTy != !DstPtrTy)
    2969             :       return false;
    2970             : 
    2971             :     // For non-pointer cases, the cast is okay if the source and destination bit
    2972             :     // widths are identical.
    2973      479978 :     if (!SrcPtrTy)
    2974      213829 :       return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
    2975             : 
    2976             :     // If both are pointers then the address spaces must match.
    2977      266149 :     if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
    2978             :       return false;
    2979             : 
    2980             :     // A vector of pointers must have the same number of elements.
    2981             :     if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
    2982             :       if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
    2983          70 :         return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
    2984             : 
    2985             :       return false;
    2986             :     }
    2987             : 
    2988             :     return true;
    2989             :   }
    2990         735 :   case Instruction::AddrSpaceCast: {
    2991             :     PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
    2992             :     if (!SrcPtrTy)
    2993             :       return false;
    2994             : 
    2995             :     PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
    2996             :     if (!DstPtrTy)
    2997             :       return false;
    2998             : 
    2999         735 :     if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
    3000             :       return false;
    3001             : 
    3002             :     if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
    3003             :       if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
    3004          27 :         return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
    3005             : 
    3006             :       return false;
    3007             :     }
    3008             : 
    3009             :     return true;
    3010             :   }
    3011             :   }
    3012             : }
    3013             : 
    3014       39101 : TruncInst::TruncInst(
    3015             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3016             : ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
    3017             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
    3018       39101 : }
    3019             : 
    3020           1 : TruncInst::TruncInst(
    3021             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3022             : ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { 
    3023             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
    3024           1 : }
    3025             : 
    3026       47107 : ZExtInst::ZExtInst(
    3027             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3028             : )  : CastInst(Ty, ZExt, S, Name, InsertBefore) { 
    3029             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
    3030       47107 : }
    3031             : 
    3032           0 : ZExtInst::ZExtInst(
    3033             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3034             : )  : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { 
    3035             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
    3036           0 : }
    3037       38858 : SExtInst::SExtInst(
    3038             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3039             : ) : CastInst(Ty, SExt, S, Name, InsertBefore) { 
    3040             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
    3041       38858 : }
    3042             : 
    3043           0 : SExtInst::SExtInst(
    3044             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3045             : )  : CastInst(Ty, SExt, S, Name, InsertAtEnd) { 
    3046             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
    3047           0 : }
    3048             : 
    3049        2727 : FPTruncInst::FPTruncInst(
    3050             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3051             : ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { 
    3052             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
    3053        2727 : }
    3054             : 
    3055           0 : FPTruncInst::FPTruncInst(
    3056             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3057             : ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { 
    3058             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
    3059           0 : }
    3060             : 
    3061        6378 : FPExtInst::FPExtInst(
    3062             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3063             : ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { 
    3064             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
    3065        6378 : }
    3066             : 
    3067           0 : FPExtInst::FPExtInst(
    3068             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3069             : ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { 
    3070             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
    3071           0 : }
    3072             : 
    3073        4112 : UIToFPInst::UIToFPInst(
    3074             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3075             : ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { 
    3076             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
    3077        4112 : }
    3078             : 
    3079           0 : UIToFPInst::UIToFPInst(
    3080             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3081             : ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { 
    3082             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
    3083           0 : }
    3084             : 
    3085        7671 : SIToFPInst::SIToFPInst(
    3086             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3087             : ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { 
    3088             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
    3089        7671 : }
    3090             : 
    3091           0 : SIToFPInst::SIToFPInst(
    3092             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3093             : ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { 
    3094             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
    3095           0 : }
    3096             : 
    3097        2679 : FPToUIInst::FPToUIInst(
    3098             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3099             : ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { 
    3100             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
    3101        2679 : }
    3102             : 
    3103           0 : FPToUIInst::FPToUIInst(
    3104             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3105             : ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { 
    3106             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
    3107           0 : }
    3108             : 
    3109        4213 : FPToSIInst::FPToSIInst(
    3110             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3111             : ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { 
    3112             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
    3113        4213 : }
    3114             : 
    3115           0 : FPToSIInst::FPToSIInst(
    3116             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3117             : ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { 
    3118             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
    3119           0 : }
    3120             : 
    3121       15481 : PtrToIntInst::PtrToIntInst(
    3122             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3123             : ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { 
    3124             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
    3125       15481 : }
    3126             : 
    3127           1 : PtrToIntInst::PtrToIntInst(
    3128             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3129             : ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { 
    3130             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
    3131           1 : }
    3132             : 
    3133       24014 : IntToPtrInst::IntToPtrInst(
    3134             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3135             : ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { 
    3136             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
    3137       24014 : }
    3138             : 
    3139          20 : IntToPtrInst::IntToPtrInst(
    3140             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3141             : ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { 
    3142             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
    3143          20 : }
    3144             : 
    3145      635523 : BitCastInst::BitCastInst(
    3146             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3147             : ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { 
    3148             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
    3149      635523 : }
    3150             : 
    3151      169185 : BitCastInst::BitCastInst(
    3152             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3153             : ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { 
    3154             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
    3155      169185 : }
    3156             : 
    3157        1629 : AddrSpaceCastInst::AddrSpaceCastInst(
    3158             :   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
    3159             : ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
    3160             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
    3161        1629 : }
    3162             : 
    3163           0 : AddrSpaceCastInst::AddrSpaceCastInst(
    3164             :   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
    3165             : ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
    3166             :   assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
    3167           0 : }
    3168             : 
    3169             : //===----------------------------------------------------------------------===//
    3170             : //                               CmpInst Classes
    3171             : //===----------------------------------------------------------------------===//
    3172             : 
    3173      220155 : CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
    3174      220155 :                  Value *RHS, const Twine &Name, Instruction *InsertBefore)
    3175             :   : Instruction(ty, op,
    3176             :                 OperandTraits<CmpInst>::op_begin(this),
    3177             :                 OperandTraits<CmpInst>::operands(this),
    3178      440310 :                 InsertBefore) {
    3179             :     Op<0>() = LHS;
    3180             :     Op<1>() = RHS;
    3181             :   setPredicate((Predicate)predicate);
    3182      220155 :   setName(Name);
    3183      220155 : }
    3184             : 
    3185          87 : CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
    3186          87 :                  Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
    3187             :   : Instruction(ty, op,
    3188             :                 OperandTraits<CmpInst>::op_begin(this),
    3189             :                 OperandTraits<CmpInst>::operands(this),
    3190         174 :                 InsertAtEnd) {
    3191             :   Op<0>() = LHS;
    3192             :   Op<1>() = RHS;
    3193             :   setPredicate((Predicate)predicate);
    3194          87 :   setName(Name);
    3195          87 : }
    3196             : 
    3197             : CmpInst *
    3198        1651 : CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
    3199             :                 const Twine &Name, Instruction *InsertBefore) {
    3200        1651 :   if (Op == Instruction::ICmp) {
    3201        1571 :     if (InsertBefore)
    3202             :       return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
    3203        1563 :                           S1, S2, Name);
    3204             :     else
    3205             :       return new ICmpInst(CmpInst::Predicate(predicate),
    3206           8 :                           S1, S2, Name);
    3207             :   }
    3208             :   
    3209          80 :   if (InsertBefore)
    3210             :     return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
    3211          79 :                         S1, S2, Name);
    3212             :   else
    3213             :     return new FCmpInst(CmpInst::Predicate(predicate),
    3214           1 :                         S1, S2, Name);
    3215             : }
    3216             : 
    3217             : CmpInst *
    3218          20 : CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
    3219             :                 const Twine &Name, BasicBlock *InsertAtEnd) {
    3220          20 :   if (Op == Instruction::ICmp) {
    3221             :     return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
    3222          20 :                         S1, S2, Name);
    3223             :   }
    3224             :   return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
    3225           0 :                       S1, S2, Name);
    3226             : }
    3227             : 
    3228           0 : void CmpInst::swapOperands() {
    3229             :   if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
    3230           0 :     IC->swapOperands();
    3231             :   else
    3232           0 :     cast<FCmpInst>(this)->swapOperands();
    3233           0 : }
    3234             : 
    3235           0 : bool CmpInst::isCommutative() const {
    3236             :   if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
    3237           0 :     return IC->isCommutative();
    3238           0 :   return cast<FCmpInst>(this)->isCommutative();
    3239             : }
    3240             : 
    3241     1910577 : bool CmpInst::isEquality() const {
    3242             :   if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
    3243     1898112 :     return IC->isEquality();
    3244             :   return cast<FCmpInst>(this)->isEquality();
    3245             : }
    3246             : 
    3247      491616 : CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
    3248      491616 :   switch (pred) {
    3249           0 :     default: llvm_unreachable("Unknown cmp predicate!");
    3250             :     case ICMP_EQ: return ICMP_NE;
    3251      110233 :     case ICMP_NE: return ICMP_EQ;
    3252       42967 :     case ICMP_UGT: return ICMP_ULE;
    3253       51999 :     case ICMP_ULT: return ICMP_UGE;
    3254       18887 :     case ICMP_UGE: return ICMP_ULT;
    3255       38471 :     case ICMP_ULE: return ICMP_UGT;
    3256       30874 :     case ICMP_SGT: return ICMP_SLE;
    3257       32274 :     case ICMP_SLT: return ICMP_SGE;
    3258       20386 :     case ICMP_SGE: return ICMP_SLT;
    3259       21194 :     case ICMP_SLE: return ICMP_SGT;
    3260             : 
    3261          40 :     case FCMP_OEQ: return FCMP_UNE;
    3262           1 :     case FCMP_ONE: return FCMP_UEQ;
    3263          44 :     case FCMP_OGT: return FCMP_ULE;
    3264         103 :     case FCMP_OLT: return FCMP_UGE;
    3265          38 :     case FCMP_OGE: return FCMP_ULT;
    3266          23 :     case FCMP_OLE: return FCMP_UGT;
    3267           1 :     case FCMP_UEQ: return FCMP_ONE;
    3268          12 :     case FCMP_UNE: return FCMP_OEQ;
    3269          25 :     case FCMP_UGT: return FCMP_OLE;
    3270          50 :     case FCMP_ULT: return FCMP_OGE;
    3271           9 :     case FCMP_UGE: return FCMP_OLT;
    3272          12 :     case FCMP_ULE: return FCMP_OGT;
    3273           0 :     case FCMP_ORD: return FCMP_UNO;
    3274          10 :     case FCMP_UNO: return FCMP_ORD;
    3275           0 :     case FCMP_TRUE: return FCMP_FALSE;
    3276           0 :     case FCMP_FALSE: return FCMP_TRUE;
    3277             :   }
    3278             : }
    3279             : 
    3280       65691 : StringRef CmpInst::getPredicateName(Predicate Pred) {
    3281       65691 :   switch (Pred) {
    3282           0 :   default:                   return "unknown";
    3283          56 :   case FCmpInst::FCMP_FALSE: return "false";
    3284         485 :   case FCmpInst::FCMP_OEQ:   return "oeq";
    3285         960 :   case FCmpInst::FCMP_OGT:   return "ogt";
    3286         321 :   case FCmpInst::FCMP_OGE:   return "oge";
    3287         584 :   case FCmpInst::FCMP_OLT:   return "olt";
    3288         275 :   case FCmpInst::FCMP_OLE:   return "ole";
    3289          82 :   case FCmpInst::FCMP_ONE:   return "one";
    3290         108 :   case FCmpInst::FCMP_ORD:   return "ord";
    3291         241 :   case FCmpInst::FCMP_UNO:   return "uno";
    3292          79 :   case FCmpInst::FCMP_UEQ:   return "ueq";
    3293          93 :   case FCmpInst::FCMP_UGT:   return "ugt";
    3294         100 :   case FCmpInst::FCMP_UGE:   return "uge";
    3295          86 :   case FCmpInst::FCMP_ULT:   return "ult";
    3296          76 :   case FCmpInst::FCMP_ULE:   return "ule";
    3297         329 :   case FCmpInst::FCMP_UNE:   return "une";
    3298          54 :   case FCmpInst::FCMP_TRUE:  return "true";
    3299       16780 :   case ICmpInst::ICMP_EQ:    return "eq";
    3300       14979 :   case ICmpInst::ICMP_NE:    return "ne";
    3301        7574 :   case ICmpInst::ICMP_SGT:   return "sgt";
    3302         893 :   case ICmpInst::ICMP_SGE:   return "sge";
    3303        7900 :   case ICmpInst::ICMP_SLT:   return "slt";
    3304        7272 :   case ICmpInst::ICMP_SLE:   return "sle";
    3305        1401 :   case ICmpInst::ICMP_UGT:   return "ugt";
    3306         625 :   case ICmpInst::ICMP_UGE:   return "uge";
    3307        3227 :   case ICmpInst::ICMP_ULT:   return "ult";
    3308        1111 :   case ICmpInst::ICMP_ULE:   return "ule";
    3309             :   }
    3310             : }
    3311             : 
    3312        5252 : ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
    3313        5252 :   switch (pred) {
    3314           0 :     default: llvm_unreachable("Unknown icmp predicate!");
    3315             :     case ICMP_EQ: case ICMP_NE: 
    3316             :     case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: 
    3317             :        return pred;
    3318        2274 :     case ICMP_UGT: return ICMP_SGT;
    3319        2912 :     case ICMP_ULT: return ICMP_SLT;
    3320          13 :     case ICMP_UGE: return ICMP_SGE;
    3321           4 :     case ICMP_ULE: return ICMP_SLE;
    3322             :   }
    3323             : }
    3324             : 
    3325       10865 : ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
    3326       10865 :   switch (pred) {
    3327           0 :     default: llvm_unreachable("Unknown icmp predicate!");
    3328             :     case ICMP_EQ: case ICMP_NE: 
    3329             :     case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: 
    3330             :        return pred;
    3331         106 :     case ICMP_SGT: return ICMP_UGT;
    3332         384 :     case ICMP_SLT: return ICMP_ULT;
    3333          50 :     case ICMP_SGE: return ICMP_UGE;
    3334          22 :     case ICMP_SLE: return ICMP_ULE;
    3335             :   }
    3336             : }
    3337             : 
    3338          75 : CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
    3339          75 :   switch (pred) {
    3340           0 :     default: llvm_unreachable("Unknown or unsupported cmp predicate!");
    3341             :     case ICMP_SGT: return ICMP_SGE;
    3342          20 :     case ICMP_SLT: return ICMP_SLE;
    3343           2 :     case ICMP_SGE: return ICMP_SGT;
    3344           8 :     case ICMP_SLE: return ICMP_SLT;
    3345           4 :     case ICMP_UGT: return ICMP_UGE;
    3346          33 :     case ICMP_ULT: return ICMP_ULE;
    3347           4 :     case ICMP_UGE: return ICMP_UGT;
    3348           2 :     case ICMP_ULE: return ICMP_ULT;
    3349             : 
    3350           0 :     case FCMP_OGT: return FCMP_OGE;
    3351           0 :     case FCMP_OLT: return FCMP_OLE;
    3352           0 :     case FCMP_OGE: return FCMP_OGT;
    3353           0 :     case FCMP_OLE: return FCMP_OLT;
    3354           0 :     case FCMP_UGT: return FCMP_UGE;
    3355           0 :     case FCMP_ULT: return FCMP_ULE;
    3356           0 :     case FCMP_UGE: return FCMP_UGT;
    3357           0 :     case FCMP_ULE: return FCMP_ULT;
    3358             :   }
    3359             : }
    3360             : 
    3361      987555 : CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
    3362      987555 :   switch (pred) {
    3363           0 :     default: llvm_unreachable("Unknown cmp predicate!");
    3364             :     case ICMP_EQ: case ICMP_NE:
    3365             :       return pred;
    3366       39708 :     case ICMP_SGT: return ICMP_SLT;
    3367       30852 :     case ICMP_SLT: return ICMP_SGT;
    3368        6094 :     case ICMP_SGE: return ICMP_SLE;
    3369        6993 :     case ICMP_SLE: return ICMP_SGE;
    3370       89340 :     case ICMP_UGT: return ICMP_ULT;
    3371      365090 :     case ICMP_ULT: return ICMP_UGT;
    3372       26296 :     case ICMP_UGE: return ICMP_ULE;
    3373       16391 :     case ICMP_ULE: return ICMP_UGE;
    3374             :   
    3375             :     case FCMP_FALSE: case FCMP_TRUE:
    3376             :     case FCMP_OEQ: case FCMP_ONE:
    3377             :     case FCMP_UEQ: case FCMP_UNE:
    3378             :     case FCMP_ORD: case FCMP_UNO:
    3379             :       return pred;
    3380         700 :     case FCMP_OGT: return FCMP_OLT;
    3381        1087 :     case FCMP_OLT: return FCMP_OGT;
    3382         717 :     case FCMP_OGE: return FCMP_OLE;
    3383         221 :     case FCMP_OLE: return FCMP_OGE;
    3384         326 :     case FCMP_UGT: return FCMP_ULT;
    3385         210 :     case FCMP_ULT: return FCMP_UGT;
    3386         692 :     case FCMP_UGE: return FCMP_ULE;
    3387         178 :     case FCMP_ULE: return FCMP_UGE;
    3388             :   }
    3389             : }
    3390             : 
    3391       19779 : CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
    3392       19779 :   switch (pred) {
    3393             :   case ICMP_SGT: return ICMP_SGE;
    3394        4368 :   case ICMP_SLT: return ICMP_SLE;
    3395        2990 :   case ICMP_UGT: return ICMP_UGE;
    3396        3238 :   case ICMP_ULT: return ICMP_ULE;
    3397           0 :   case FCMP_OGT: return FCMP_OGE;
    3398           0 :   case FCMP_OLT: return FCMP_OLE;
    3399           0 :   case FCMP_UGT: return FCMP_UGE;
    3400           0 :   case FCMP_ULT: return FCMP_ULE;
    3401        6295 :   default: return pred;
    3402             :   }
    3403             : }
    3404             : 
    3405       20866 : CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
    3406             :   assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
    3407             : 
    3408       20866 :   switch (pred) {
    3409           0 :   default:
    3410           0 :     llvm_unreachable("Unknown predicate!");
    3411             :   case CmpInst::ICMP_ULT:
    3412             :     return CmpInst::ICMP_SLT;
    3413         245 :   case CmpInst::ICMP_ULE:
    3414         245 :     return CmpInst::ICMP_SLE;
    3415        2427 :   case CmpInst::ICMP_UGT:
    3416        2427 :     return CmpInst::ICMP_SGT;
    3417         962 :   case CmpInst::ICMP_UGE:
    3418         962 :     return CmpInst::ICMP_SGE;
    3419             :   }
    3420             : }
    3421             : 
    3422     1171339 : bool CmpInst::isUnsigned(Predicate predicate) {
    3423     1171339 :   switch (predicate) {
    3424             :     default: return false;
    3425      291627 :     case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: 
    3426      291627 :     case ICmpInst::ICMP_UGE: return true;
    3427             :   }
    3428             : }
    3429             : 
    3430     1171524 : bool CmpInst::isSigned(Predicate predicate) {
    3431     1171524 :   switch (predicate) {
    3432             :     default: return false;
    3433      155125 :     case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: 
    3434      155125 :     case ICmpInst::ICMP_SGE: return true;
    3435             :   }
    3436             : }
    3437             : 
    3438        6708 : bool CmpInst::isOrdered(Predicate predicate) {
    3439        6708 :   switch (predicate) {
    3440             :     default: return false;
    3441        5052 :     case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: 
    3442             :     case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: 
    3443        5052 :     case FCmpInst::FCMP_ORD: return true;
    3444             :   }
    3445             : }
    3446             :       
    3447         407 : bool CmpInst::isUnordered(Predicate predicate) {
    3448         407 :   switch (predicate) {
    3449             :     default: return false;
    3450          66 :     case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: 
    3451             :     case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: 
    3452          66 :     case FCmpInst::FCMP_UNO: return true;
    3453             :   }
    3454             : }
    3455             : 
    3456       66044 : bool CmpInst::isTrueWhenEqual(Predicate predicate) {
    3457             :   switch(predicate) {
    3458             :     default: return false;
    3459       20009 :     case ICMP_EQ:   case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
    3460       20009 :     case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
    3461             :   }
    3462             : }
    3463             : 
    3464         236 : bool CmpInst::isFalseWhenEqual(Predicate predicate) {
    3465             :   switch(predicate) {
    3466             :   case ICMP_NE:    case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
    3467             :   case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
    3468         152 :   default: return false;
    3469             :   }
    3470             : }
    3471             : 
    3472        2903 : bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
    3473             :   // If the predicates match, then we know the first condition implies the
    3474             :   // second is true.
    3475        2903 :   if (Pred1 == Pred2)
    3476             :     return true;
    3477             : 
    3478        2726 :   switch (Pred1) {
    3479             :   default:
    3480             :     break;
    3481          92 :   case ICMP_EQ:
    3482             :     // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
    3483         164 :     return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
    3484          72 :            Pred2 == ICMP_SLE;
    3485          53 :   case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
    3486          53 :     return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
    3487          48 :   case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
    3488          48 :     return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
    3489          76 :   case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
    3490          76 :     return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
    3491          65 :   case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
    3492          65 :     return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
    3493             :   }
    3494             :   return false;
    3495             : }
    3496             : 
    3497        1174 : bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
    3498        1174 :   return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
    3499             : }
    3500             : 
    3501             : //===----------------------------------------------------------------------===//
    3502             : //                        SwitchInst Implementation
    3503             : //===----------------------------------------------------------------------===//
    3504             : 
    3505        5265 : void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
    3506             :   assert(Value && Default && NumReserved);
    3507        5265 :   ReservedSpace = NumReserved;
    3508             :   setNumHungOffUseOperands(2);
    3509        5265 :   allocHungoffUses(ReservedSpace);
    3510             : 
    3511             :   Op<0>() = Value;
    3512             :   Op<1>() = Default;
    3513        5265 : }
    3514             : 
    3515             : /// SwitchInst ctor - Create a new switch instruction, specifying a value to
    3516             : /// switch on and a default destination.  The number of additional cases can
    3517             : /// be specified here to make memory allocation more efficient.  This
    3518             : /// constructor can also autoinsert before another instruction.
    3519        4302 : SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
    3520        4302 :                        Instruction *InsertBefore)
    3521             :   : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
    3522        4302 :                    nullptr, 0, InsertBefore) {
    3523        4302 :   init(Value, Default, 2+NumCases*2);
    3524        4302 : }
    3525             : 
    3526             : /// SwitchInst ctor - Create a new switch instruction, specifying a value to
    3527             : /// switch on and a default destination.  The number of additional cases can
    3528             : /// be specified here to make memory allocation more efficient.  This
    3529             : /// constructor also autoinserts at the end of the specified BasicBlock.
    3530         128 : SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
    3531         128 :                        BasicBlock *InsertAtEnd)
    3532             :   : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
    3533         128 :                    nullptr, 0, InsertAtEnd) {
    3534         128 :   init(Value, Default, 2+NumCases*2);
    3535         128 : }
    3536             : 
    3537         835 : SwitchInst::SwitchInst(const SwitchInst &SI)
    3538         835 :   : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
    3539         835 :   init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
    3540             :   setNumHungOffUseOperands(SI.getNumOperands());
    3541         835 :   Use *OL = getOperandList();
    3542             :   const Use *InOL = SI.getOperandList();
    3543        4881 :   for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
    3544        2023 :     OL[i] = InOL[i];
    3545        2023 :     OL[i+1] = InOL[i+1];
    3546             :   }
    3547         835 :   SubclassOptionalData = SI.SubclassOptionalData;
    3548         835 : }
    3549             : 
    3550             : 
    3551             : /// addCase - Add an entry to the switch instruction...
    3552             : ///
    3553       16549 : void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
    3554             :   unsigned NewCaseIdx = getNumCases();
    3555             :   unsigned OpNo = getNumOperands();
    3556       16549 :   if (OpNo+2 > ReservedSpace)
    3557         127 :     growOperands();  // Get more space!
    3558             :   // Initialize some new operands.
    3559             :   assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
    3560             :   setNumHungOffUseOperands(OpNo+2);
    3561             :   CaseHandle Case(this, NewCaseIdx);
    3562             :   Case.setValue(OnVal);
    3563             :   Case.setSuccessor(Dest);
    3564       16549 : }
    3565             : 
    3566             : /// removeCase - This method removes the specified case and its successor
    3567             : /// from the switch instruction.
    3568         754 : SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
    3569         754 :   unsigned idx = I->getCaseIndex();
    3570             : 
    3571             :   assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
    3572             : 
    3573             :   unsigned NumOps = getNumOperands();
    3574         754 :   Use *OL = getOperandList();
    3575             : 
    3576             :   // Overwrite this case with the end of the list.
    3577         754 :   if (2 + (idx + 1) * 2 != NumOps) {
    3578         470 :     OL[2 + idx * 2] = OL[NumOps - 2];
    3579         470 :     OL[2 + idx * 2 + 1] = OL[NumOps - 1];
    3580             :   }
    3581             : 
    3582             :   // Nuke the last value.
    3583         754 :   OL[NumOps-2].set(nullptr);
    3584         754 :   OL[NumOps-2+1].set(nullptr);
    3585             :   setNumHungOffUseOperands(NumOps-2);
    3586             : 
    3587         754 :   return CaseIt(this, idx);
    3588             : }
    3589             : 
    3590             : /// growOperands - grow operands - This grows the operand list in response
    3591             : /// to a push_back style of operation.  This grows the number of ops by 3 times.
    3592             : ///
    3593         127 : void SwitchInst::growOperands() {
    3594             :   unsigned e = getNumOperands();
    3595         127 :   unsigned NumOps = e*3;
    3596             : 
    3597         127 :   ReservedSpace = NumOps;
    3598         127 :   growHungoffUses(ReservedSpace);
    3599         127 : }
    3600             : 
    3601             : //===----------------------------------------------------------------------===//
    3602             : //                        IndirectBrInst Implementation
    3603             : //===----------------------------------------------------------------------===//
    3604             : 
    3605         387 : void IndirectBrInst::init(Value *Address, unsigned NumDests) {
    3606             :   assert(Address && Address->getType()->isPointerTy() &&
    3607             :          "Address of indirectbr must be a pointer");
    3608         387 :   ReservedSpace = 1+NumDests;
    3609             :   setNumHungOffUseOperands(1);
    3610         387 :   allocHungoffUses(ReservedSpace);
    3611             : 
    3612             :   Op<0>() = Address;
    3613         387 : }
    3614             : 
    3615             : 
    3616             : /// growOperands - grow operands - This grows the operand list in response
    3617             : /// to a push_back style of operation.  This grows the number of ops by 2 times.
    3618             : ///
    3619           0 : void IndirectBrInst::growOperands() {
    3620             :   unsigned e = getNumOperands();
    3621           0 :   unsigned NumOps = e*2;
    3622             :   
    3623           0 :   ReservedSpace = NumOps;
    3624           0 :   growHungoffUses(ReservedSpace);
    3625           0 : }
    3626             : 
    3627         387 : IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
    3628         387 :                                Instruction *InsertBefore)
    3629             : : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
    3630         387 :                  nullptr, 0, InsertBefore) {
    3631         387 :   init(Address, NumCases);
    3632         387 : }
    3633             : 
    3634           0 : IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
    3635           0 :                                BasicBlock *InsertAtEnd)
    3636             : : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
    3637           0 :                  nullptr, 0, InsertAtEnd) {
    3638           0 :   init(Address, NumCases);
    3639           0 : }
    3640             : 
    3641           0 : IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
    3642             :     : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
    3643           0 :                      nullptr, IBI.getNumOperands()) {
    3644           0 :   allocHungoffUses(IBI.getNumOperands());
    3645             :   Use *OL = getOperandList();
    3646           0 :   const Use *InOL = IBI.getOperandList();
    3647           0 :   for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
    3648           0 :     OL[i] = InOL[i];
    3649           0 :   SubclassOptionalData = IBI.SubclassOptionalData;
    3650           0 : }
    3651             : 
    3652             : /// addDestination - Add a destination.
    3653             : ///
    3654        1034 : void IndirectBrInst::addDestination(BasicBlock *DestBB) {
    3655             :   unsigned OpNo = getNumOperands();
    3656        1034 :   if (OpNo+1 > ReservedSpace)
    3657           0 :     growOperands();  // Get more space!
    3658             :   // Initialize some new operands.
    3659             :   assert(OpNo < ReservedSpace && "Growing didn't work!");
    3660             :   setNumHungOffUseOperands(OpNo+1);
    3661        2068 :   getOperandList()[OpNo] = DestBB;
    3662        1034 : }
    3663             : 
    3664             : /// removeDestination - This method removes the specified successor from the
    3665             : /// indirectbr instruction.
    3666          16 : void IndirectBrInst::removeDestination(unsigned idx) {
    3667             :   assert(idx < getNumOperands()-1 && "Successor index out of range!");
    3668             :   
    3669             :   unsigned NumOps = getNumOperands();
    3670          16 :   Use *OL = getOperandList();
    3671             : 
    3672             :   // Replace this value with the last one.
    3673          16 :   OL[idx+1] = OL[NumOps-1];
    3674             :   
    3675             :   // Nuke the last value.
    3676             :   OL[NumOps-1].set(nullptr);
    3677             :   setNumHungOffUseOperands(NumOps-1);
    3678          16 : }
    3679             : 
    3680             : //===----------------------------------------------------------------------===//
    3681             : //                           cloneImpl() implementations
    3682             : //===----------------------------------------------------------------------===//
    3683             : 
    3684             : // Define these methods here so vtables don't get emitted into every translation
    3685             : // unit that uses these classes.
    3686             : 
    3687      113302 : GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
    3688      113302 :   return new (getNumOperands()) GetElementPtrInst(*this);
    3689             : }
    3690             : 
    3691      344583 : BinaryOperator *BinaryOperator::cloneImpl() const {
    3692      689166 :   return Create(getOpcode(), Op<0>(), Op<1>());
    3693             : }
    3694             : 
    3695         270 : FCmpInst *FCmpInst::cloneImpl() const {
    3696         540 :   return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
    3697             : }
    3698             : 
    3699       55398 : ICmpInst *ICmpInst::cloneImpl() const {
    3700      110796 :   return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
    3701             : }
    3702             : 
    3703        9524 : ExtractValueInst *ExtractValueInst::cloneImpl() const {
    3704        9524 :   return new ExtractValueInst(*this);
    3705             : }
    3706             : 
    3707         850 : InsertValueInst *InsertValueInst::cloneImpl() const {
    3708         850 :   return new InsertValueInst(*this);
    3709             : }
    3710             : 
    3711       87117 : AllocaInst *AllocaInst::cloneImpl() const {
    3712       87117 :   AllocaInst *Result = new AllocaInst(getAllocatedType(),
    3713             :                                       getType()->getAddressSpace(),
    3714       87117 :                                       (Value *)getOperand(0), getAlignment());
    3715             :   Result->setUsedWithInAlloca(isUsedWithInAlloca());
    3716             :   Result->setSwiftError(isSwiftError());
    3717       87117 :   return Result;
    3718             : }
    3719             : 
    3720      447443 : LoadInst *LoadInst::cloneImpl() const {
    3721      894886 :   return new LoadInst(getOperand(0), Twine(), isVolatile(),
    3722      894886 :                       getAlignment(), getOrdering(), getSyncScopeID());
    3723             : }
    3724             : 
    3725      421750 : StoreInst *StoreInst::cloneImpl() const {
    3726      421750 :   return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
    3727      843500 :                        getAlignment(), getOrdering(), getSyncScopeID());
    3728             :   
    3729             : }
    3730             : 
    3731          50 : AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
    3732             :   AtomicCmpXchgInst *Result =
    3733             :     new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
    3734             :                           getSuccessOrdering(), getFailureOrdering(),
    3735         100 :                           getSyncScopeID());
    3736             :   Result->setVolatile(isVolatile());
    3737             :   Result->setWeak(isWeak());
    3738          50 :   return Result;
    3739             : }
    3740             : 
    3741         120 : AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
    3742             :   AtomicRMWInst *Result =
    3743             :     new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
    3744         240 :                       getOrdering(), getSyncScopeID());
    3745             :   Result->setVolatile(isVolatile());
    3746         120 :   return Result;
    3747             : }
    3748             : 
    3749          10 : FenceInst *FenceInst::cloneImpl() const {
    3750          30 :   return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
    3751             : }
    3752             : 
    3753        5658 : TruncInst *TruncInst::cloneImpl() const {
    3754       11316 :   return new TruncInst(getOperand(0), getType());
    3755             : }
    3756             : 
    3757        7050 : ZExtInst *ZExtInst::cloneImpl() const {
    3758       14100 :   return new ZExtInst(getOperand(0), getType());
    3759             : }
    3760             : 
    3761        2058 : SExtInst *SExtInst::cloneImpl() const {
    3762        4116 :   return new SExtInst(getOperand(0), getType());
    3763             : }
    3764             : 
    3765          29 : FPTruncInst *FPTruncInst::cloneImpl() const {
    3766          58 :   return new FPTruncInst(getOperand(0), getType());
    3767             : }
    3768             : 
    3769          56 : FPExtInst *FPExtInst::cloneImpl() const {
    3770         112 :   return new FPExtInst(getOperand(0), getType());
    3771             : }
    3772             : 
    3773          96 : UIToFPInst *UIToFPInst::cloneImpl() const {
    3774         192 :   return new UIToFPInst(getOperand(0), getType());
    3775             : }
    3776             : 
    3777         249 : SIToFPInst *SIToFPInst::cloneImpl() const {
    3778         498 :   return new SIToFPInst(getOperand(0), getType());
    3779             : }
    3780             : 
    3781          34 : FPToUIInst *FPToUIInst::cloneImpl() const {
    3782          68 :   return new FPToUIInst(getOperand(0), getType());
    3783             : }
    3784             : 
    3785          47 : FPToSIInst *FPToSIInst::cloneImpl() const {
    3786          94 :   return new FPToSIInst(getOperand(0), getType());
    3787             : }
    3788             : 
    3789        2695 : PtrToIntInst *PtrToIntInst::cloneImpl() const {
    3790        5390 :   return new PtrToIntInst(getOperand(0), getType());
    3791             : }
    3792             : 
    3793        1193 : IntToPtrInst *IntToPtrInst::cloneImpl() const {
    3794        2386 :   return new IntToPtrInst(getOperand(0), getType());
    3795             : }
    3796             : 
    3797       75149 : BitCastInst *BitCastInst::cloneImpl() const {
    3798      150298 :   return new BitCastInst(getOperand(0), getType());
    3799             : }
    3800             : 
    3801          16 : AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
    3802          32 :   return new AddrSpaceCastInst(getOperand(0), getType());
    3803             : }
    3804             : 
    3805      173615 : CallInst *CallInst::cloneImpl() const {
    3806      347230 :   if (hasOperandBundles()) {
    3807          64 :     unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
    3808          64 :     return new(getNumOperands(), DescriptorBytes) CallInst(*this);
    3809             :   }
    3810      173551 :   return  new(getNumOperands()) CallInst(*this);
    3811             : }
    3812             : 
    3813       24015 : SelectInst *SelectInst::cloneImpl() const {
    3814       24015 :   return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
    3815             : }
    3816             : 
    3817           6 : VAArgInst *VAArgInst::cloneImpl() const {
    3818          12 :   return new VAArgInst(getOperand(0), getType());
    3819             : }
    3820             : 
    3821         936 : ExtractElementInst *ExtractElementInst::cloneImpl() const {
    3822         936 :   return ExtractElementInst::Create(getOperand(0), getOperand(1));
    3823             : }
    3824             : 
    3825        8833 : InsertElementInst *InsertElementInst::cloneImpl() const {
    3826        8833 :   return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
    3827             : }
    3828             : 
    3829        3061 : ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
    3830        3061 :   return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
    3831             : }
    3832             : 
    3833       23251 : PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
    3834             : 
    3835        7879 : LandingPadInst *LandingPadInst::cloneImpl() const {
    3836        7879 :   return new LandingPadInst(*this);
    3837             : }
    3838             : 
    3839       96323 : ReturnInst *ReturnInst::cloneImpl() const {
    3840       96323 :   return new(getNumOperands()) ReturnInst(*this);
    3841             : }
    3842             : 
    3843       65888 : BranchInst *BranchInst::cloneImpl() const {
    3844       65888 :   return new(getNumOperands()) BranchInst(*this);
    3845             : }
    3846             : 
    3847        1670 : SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
    3848             : 
    3849           0 : IndirectBrInst *IndirectBrInst::cloneImpl() const {
    3850           0 :   return new IndirectBrInst(*this);
    3851             : }
    3852             : 
    3853        8522 : InvokeInst *InvokeInst::cloneImpl() const {
    3854       17044 :   if (hasOperandBundles()) {
    3855          46 :     unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
    3856          46 :     return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
    3857             :   }
    3858        8476 :   return new(getNumOperands()) InvokeInst(*this);
    3859             : }
    3860             : 
    3861         524 : ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
    3862             : 
    3863          60 : CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
    3864          60 :   return new (getNumOperands()) CleanupReturnInst(*this);
    3865             : }
    3866             : 
    3867          10 : CatchReturnInst *CatchReturnInst::cloneImpl() const {
    3868          10 :   return new (getNumOperands()) CatchReturnInst(*this);
    3869             : }
    3870             : 
    3871          26 : CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
    3872          26 :   return new CatchSwitchInst(*this);
    3873             : }
    3874             : 
    3875         120 : FuncletPadInst *FuncletPadInst::cloneImpl() const {
    3876         120 :   return new (getNumOperands()) FuncletPadInst(*this);
    3877             : }
    3878             : 
    3879        8182 : UnreachableInst *UnreachableInst::cloneImpl() const {
    3880        8182 :   LLVMContext &Context = getContext();
    3881        8182 :   return new UnreachableInst(Context);
    3882             : }

Generated by: LCOV version 1.13