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