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