LLVM  14.0.0git
Instructions.cpp
Go to the documentation of this file.
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"
37 #include "llvm/Support/Casting.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 
49  "disable-i2p-p2i-opt", cl::init(false),
50  cl::desc("Disables inttoptr/ptrtoint roundtrip optimization"));
51 
52 //===----------------------------------------------------------------------===//
53 // AllocaInst Class
54 //===----------------------------------------------------------------------===//
55 
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()) {
106  std::copy(PN.op_begin(), PN.op_end(), op_begin());
107  std::copy(PN.block_begin(), PN.block_end(), block_begin());
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);
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...
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.
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.
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()) {
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;
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 
244  unsigned OpNo = getNumOperands();
245  growOperands(1);
246  assert(OpNo < ReservedSpace && "Growing didn't work!");
248  getOperandList()[OpNo] = Val;
249 }
250 
251 //===----------------------------------------------------------------------===//
252 // CallBase Implementation
253 //===----------------------------------------------------------------------===//
254 
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 
270  Instruction *InsertPt) {
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 
283 
285  assert(getOpcode() == Instruction::CallBr && "Unexpected opcode!");
286  return cast<CallBrInst>(this)->getNumIndirectDests() + 1;
287 }
288 
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.
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 
312  if (auto *F = getCalledFunction())
313  return F->getIntrinsicID();
315 }
316 
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 
329  unsigned Index;
330 
331  if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index))
333  if (const Function *F = getCalledFunction())
334  if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index))
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 
376  SmallVectorImpl<OperandBundleDef> &Defs) const {
377  for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
379 }
380 
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 
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.
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");
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 
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 
456  Instruction *InsertPt) {
457  if (CB->getOperandBundle(ID))
458  return CB;
459 
461  CB->getOperandBundlesAsDefs(Bundles);
462  Bundles.push_back(OB);
463  return Create(CB, Bundles, InsertPt);
464 }
465 
467  Instruction *InsertPt) {
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 
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.
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());
553 
554  std::copy(CI.op_begin(), CI.op_end(), op_begin());
558 }
559 
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.
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());
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(
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(
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,
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.
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 }
737  Type *IntPtrTy, Type *AllocTy,
738  Value *AllocSize, Value *ArraySize,
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.
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 }
762  Type *IntPtrTy, Type *AllocTy,
763  Value *AllocSize, Value *ArraySize,
765  Function *MallocF, const Twine &Name) {
766  return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
767  ArraySize, OpB, MallocF, Name);
768 }
769 
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.
806  return createFree(Source, None, InsertBefore, nullptr);
807 }
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.
818  Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
819  assert(FreeCall && "CreateFree did not create a CallInst");
820  return FreeCall;
821 }
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,
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.
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()) {
874  std::copy(II.op_begin(), II.op_end(), op_begin());
878 }
879 
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 
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,
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);
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()) {
957  std::copy(CBI.op_begin(), CBI.op_end(), op_begin());
961  NumIndirectDests = CBI.NumIndirectDests;
962 }
963 
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>();
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,
1041  CRI.getNumOperands(),
1042  CRI.getNumOperands()) {
1043  setSubclassData<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 
1173  unsigned OpNo = getNumOperands();
1174  growOperands(1);
1175  assert(OpNo < ReservedSpace && "Growing didn't work!");
1177  getOperandList()[OpNo] = Handler;
1178 }
1179 
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 
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 
1234  Instruction *InsertBefore)
1235  : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr,
1236  0, InsertBefore) {}
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>();
1305 }
1306 
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 
1322  if (!Amt)
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 
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 
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) {
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) {
1383  assert(!Ty->isVoidTy() && "Cannot allocate void!");
1384  setName(Name);
1385 }
1386 
1387 
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.
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  assert(!(isAtomic() && getAlignment() == 0) &&
1414  "Alignment required for atomic load");
1415 }
1416 
1418  assert(BB && "Insertion BB cannot be null when alignment not provided!");
1419  assert(BB->getParent() &&
1420  "BB must be in a Function when alignment not provided!");
1421  const DataLayout &DL = BB->getModule()->getDataLayout();
1422  return DL.getABITypeAlign(Ty);
1423 }
1424 
1426  assert(I && "Insertion position cannot be null when alignment not provided!");
1427  return computeLoadStoreDefaultAlign(Ty, I->getParent());
1428 }
1429 
1431  Instruction *InsertBef)
1432  : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {}
1433 
1435  BasicBlock *InsertAE)
1436  : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {}
1437 
1438 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1439  Instruction *InsertBef)
1440  : LoadInst(Ty, Ptr, Name, isVolatile,
1441  computeLoadStoreDefaultAlign(Ty, InsertBef), InsertBef) {}
1442 
1443 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1444  BasicBlock *InsertAE)
1445  : LoadInst(Ty, Ptr, Name, isVolatile,
1446  computeLoadStoreDefaultAlign(Ty, InsertAE), InsertAE) {}
1447 
1448 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1449  Align Align, Instruction *InsertBef)
1450  : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1451  SyncScope::System, InsertBef) {}
1452 
1453 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1454  Align Align, BasicBlock *InsertAE)
1455  : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1456  SyncScope::System, InsertAE) {}
1457 
1458 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1459  Align Align, AtomicOrdering Order, SyncScope::ID SSID,
1460  Instruction *InsertBef)
1461  : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1462  assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty));
1465  setAtomic(Order, SSID);
1466  AssertOK();
1467  setName(Name);
1468 }
1469 
1470 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
1471  Align Align, AtomicOrdering Order, SyncScope::ID SSID,
1472  BasicBlock *InsertAE)
1473  : UnaryInstruction(Ty, Load, Ptr, InsertAE) {
1474  assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty));
1477  setAtomic(Order, SSID);
1478  AssertOK();
1479  setName(Name);
1480 }
1481 
1482 //===----------------------------------------------------------------------===//
1483 // StoreInst Implementation
1484 //===----------------------------------------------------------------------===//
1485 
1486 void StoreInst::AssertOK() {
1487  assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
1488  assert(getOperand(1)->getType()->isPointerTy() &&
1489  "Ptr must have pointer type!");
1490  assert(cast<PointerType>(getOperand(1)->getType())
1491  ->isOpaqueOrPointeeTypeMatches(getOperand(0)->getType()) &&
1492  "Ptr must be a pointer to Val type!");
1493  assert(!(isAtomic() && getAlignment() == 0) &&
1494  "Alignment required for atomic store");
1495 }
1496 
1498  : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
1499 
1501  : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
1502 
1503 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1504  Instruction *InsertBefore)
1505  : StoreInst(val, addr, isVolatile,
1506  computeLoadStoreDefaultAlign(val->getType(), InsertBefore),
1507  InsertBefore) {}
1508 
1509 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1510  BasicBlock *InsertAtEnd)
1511  : StoreInst(val, addr, isVolatile,
1512  computeLoadStoreDefaultAlign(val->getType(), InsertAtEnd),
1513  InsertAtEnd) {}
1514 
1515 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align,
1516  Instruction *InsertBefore)
1517  : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1518  SyncScope::System, InsertBefore) {}
1519 
1520 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align,
1521  BasicBlock *InsertAtEnd)
1522  : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1523  SyncScope::System, InsertAtEnd) {}
1524 
1525 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align,
1526  AtomicOrdering Order, SyncScope::ID SSID,
1527  Instruction *InsertBefore)
1528  : Instruction(Type::getVoidTy(val->getContext()), Store,
1529  OperandTraits<StoreInst>::op_begin(this),
1530  OperandTraits<StoreInst>::operands(this), InsertBefore) {
1531  Op<0>() = val;
1532  Op<1>() = addr;
1535  setAtomic(Order, SSID);
1536  AssertOK();
1537 }
1538 
1539 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align,
1540  AtomicOrdering Order, SyncScope::ID SSID,
1541  BasicBlock *InsertAtEnd)
1542  : Instruction(Type::getVoidTy(val->getContext()), Store,
1543  OperandTraits<StoreInst>::op_begin(this),
1544  OperandTraits<StoreInst>::operands(this), InsertAtEnd) {
1545  Op<0>() = val;
1546  Op<1>() = addr;
1549  setAtomic(Order, SSID);
1550  AssertOK();
1551 }
1552 
1553 
1554 //===----------------------------------------------------------------------===//
1555 // AtomicCmpXchgInst Implementation
1556 //===----------------------------------------------------------------------===//
1557 
1558 void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1559  Align Alignment, AtomicOrdering SuccessOrdering,
1560  AtomicOrdering FailureOrdering,
1561  SyncScope::ID SSID) {
1562  Op<0>() = Ptr;
1563  Op<1>() = Cmp;
1564  Op<2>() = NewVal;
1565  setSuccessOrdering(SuccessOrdering);
1566  setFailureOrdering(FailureOrdering);
1567  setSyncScopeID(SSID);
1568  setAlignment(Alignment);
1569 
1570  assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1571  "All operands must be non-null!");
1572  assert(getOperand(0)->getType()->isPointerTy() &&
1573  "Ptr must have pointer type!");
1574  assert(cast<PointerType>(getOperand(0)->getType())
1575  ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) &&
1576  "Ptr must be a pointer to Cmp type!");
1577  assert(cast<PointerType>(getOperand(0)->getType())
1578  ->isOpaqueOrPointeeTypeMatches(getOperand(2)->getType()) &&
1579  "Ptr must be a pointer to NewVal type!");
1580  assert(getOperand(1)->getType() == getOperand(2)->getType() &&
1581  "Cmp type and NewVal type must be same!");
1582 }
1583 
1585  Align Alignment,
1586  AtomicOrdering SuccessOrdering,
1587  AtomicOrdering FailureOrdering,
1588  SyncScope::ID SSID,
1589  Instruction *InsertBefore)
1590  : Instruction(
1591  StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
1592  AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1593  OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
1594  Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID);
1595 }
1596 
1598  Align Alignment,
1599  AtomicOrdering SuccessOrdering,
1600  AtomicOrdering FailureOrdering,
1601  SyncScope::ID SSID,
1602  BasicBlock *InsertAtEnd)
1603  : Instruction(
1604  StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
1605  AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1606  OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
1607  Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID);
1608 }
1609 
1610 //===----------------------------------------------------------------------===//
1611 // AtomicRMWInst Implementation
1612 //===----------------------------------------------------------------------===//
1613 
1614 void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1615  Align Alignment, AtomicOrdering Ordering,
1616  SyncScope::ID SSID) {
1617  Op<0>() = Ptr;
1618  Op<1>() = Val;
1620  setOrdering(Ordering);
1621  setSyncScopeID(SSID);
1622  setAlignment(Alignment);
1623 
1624  assert(getOperand(0) && getOperand(1) &&
1625  "All operands must be non-null!");
1626  assert(getOperand(0)->getType()->isPointerTy() &&
1627  "Ptr must have pointer type!");
1628  assert(cast<PointerType>(getOperand(0)->getType())
1629  ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) &&
1630  "Ptr must be a pointer to Val type!");
1631  assert(Ordering != AtomicOrdering::NotAtomic &&
1632  "AtomicRMW instructions must be atomic!");
1633 }
1634 
1636  Align Alignment, AtomicOrdering Ordering,
1637  SyncScope::ID SSID, Instruction *InsertBefore)
1638  : Instruction(Val->getType(), AtomicRMW,
1639  OperandTraits<AtomicRMWInst>::op_begin(this),
1640  OperandTraits<AtomicRMWInst>::operands(this), InsertBefore) {
1641  Init(Operation, Ptr, Val, Alignment, Ordering, SSID);
1642 }
1643 
1645  Align Alignment, AtomicOrdering Ordering,
1646  SyncScope::ID SSID, BasicBlock *InsertAtEnd)
1647  : Instruction(Val->getType(), AtomicRMW,
1648  OperandTraits<AtomicRMWInst>::op_begin(this),
1649  OperandTraits<AtomicRMWInst>::operands(this), InsertAtEnd) {
1650  Init(Operation, Ptr, Val, Alignment, Ordering, SSID);
1651 }
1652 
1654  switch (Op) {
1655  case AtomicRMWInst::Xchg:
1656  return "xchg";
1657  case AtomicRMWInst::Add:
1658  return "add";
1659  case AtomicRMWInst::Sub:
1660  return "sub";
1661  case AtomicRMWInst::And:
1662  return "and";
1663  case AtomicRMWInst::Nand:
1664  return "nand";
1665  case AtomicRMWInst::Or:
1666  return "or";
1667  case AtomicRMWInst::Xor:
1668  return "xor";
1669  case AtomicRMWInst::Max:
1670  return "max";
1671  case AtomicRMWInst::Min:
1672  return "min";
1673  case AtomicRMWInst::UMax:
1674  return "umax";
1675  case AtomicRMWInst::UMin:
1676  return "umin";
1677  case AtomicRMWInst::FAdd:
1678  return "fadd";
1679  case AtomicRMWInst::FSub:
1680  return "fsub";
1682  return "<invalid operation>";
1683  }
1684 
1685  llvm_unreachable("invalid atomicrmw operation");
1686 }
1687 
1688 //===----------------------------------------------------------------------===//
1689 // FenceInst Implementation
1690 //===----------------------------------------------------------------------===//
1691 
1693  SyncScope::ID SSID,
1694  Instruction *InsertBefore)
1695  : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
1696  setOrdering(Ordering);
1697  setSyncScopeID(SSID);
1698 }
1699 
1701  SyncScope::ID SSID,
1702  BasicBlock *InsertAtEnd)
1703  : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
1704  setOrdering(Ordering);
1705  setSyncScopeID(SSID);
1706 }
1707 
1708 //===----------------------------------------------------------------------===//
1709 // GetElementPtrInst Implementation
1710 //===----------------------------------------------------------------------===//
1711 
1712 void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
1713  const Twine &Name) {
1714  assert(getNumOperands() == 1 + IdxList.size() &&
1715  "NumOperands not initialized?");
1716  Op<0>() = Ptr;
1717  llvm::copy(IdxList, op_begin() + 1);
1718  setName(Name);
1719 }
1720 
1721 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1722  : Instruction(GEPI.getType(), GetElementPtr,
1724  GEPI.getNumOperands(),
1725  GEPI.getNumOperands()),
1726  SourceElementType(GEPI.SourceElementType),
1727  ResultElementType(GEPI.ResultElementType) {
1728  std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
1730 }
1731 
1733  if (auto *Struct = dyn_cast<StructType>(Ty)) {
1734  if (!Struct->indexValid(Idx))
1735  return nullptr;
1736  return Struct->getTypeAtIndex(Idx);
1737  }
1738  if (!Idx->getType()->isIntOrIntVectorTy())
1739  return nullptr;
1740  if (auto *Array = dyn_cast<ArrayType>(Ty))
1741  return Array->getElementType();
1742  if (auto *Vector = dyn_cast<VectorType>(Ty))
1743  return Vector->getElementType();
1744  return nullptr;
1745 }
1746 
1748  if (auto *Struct = dyn_cast<StructType>(Ty)) {
1749  if (Idx >= Struct->getNumElements())
1750  return nullptr;
1751  return Struct->getElementType(Idx);
1752  }
1753  if (auto *Array = dyn_cast<ArrayType>(Ty))
1754  return Array->getElementType();
1755  if (auto *Vector = dyn_cast<VectorType>(Ty))
1756  return Vector->getElementType();
1757  return nullptr;
1758 }
1759 
1760 template <typename IndexTy>
1762  if (IdxList.empty())
1763  return Ty;
1764  for (IndexTy V : IdxList.slice(1)) {
1766  if (!Ty)
1767  return Ty;
1768  }
1769  return Ty;
1770 }
1771 
1773  return getIndexedTypeInternal(Ty, IdxList);
1774 }
1775 
1777  ArrayRef<Constant *> IdxList) {
1778  return getIndexedTypeInternal(Ty, IdxList);
1779 }
1780 
1782  return getIndexedTypeInternal(Ty, IdxList);
1783 }
1784 
1785 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1786 /// zeros. If so, the result pointer and the first operand have the same
1787 /// value, just potentially different types.
1789  for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1790  if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1791  if (!CI->isZero()) return false;
1792  } else {
1793  return false;
1794  }
1795  }
1796  return true;
1797 }
1798 
1799 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1800 /// constant integers. If so, the result pointer and the first operand have
1801 /// a constant offset between them.
1803  for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1804  if (!isa<ConstantInt>(getOperand(i)))
1805  return false;
1806  }
1807  return true;
1808 }
1809 
1811  cast<GEPOperator>(this)->setIsInBounds(B);
1812 }
1813 
1815  return cast<GEPOperator>(this)->isInBounds();
1816 }
1817 
1819  APInt &Offset) const {
1820  // Delegate to the generic GEPOperator implementation.
1821  return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
1822 }
1823 
1825  const DataLayout &DL, unsigned BitWidth,
1826  MapVector<Value *, APInt> &VariableOffsets,
1827  APInt &ConstantOffset) const {
1828  // Delegate to the generic GEPOperator implementation.
1829  return cast<GEPOperator>(this)->collectOffset(DL, BitWidth, VariableOffsets,
1830  ConstantOffset);
1831 }
1832 
1833 //===----------------------------------------------------------------------===//
1834 // ExtractElementInst Implementation
1835 //===----------------------------------------------------------------------===//
1836 
1837 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1838  const Twine &Name,
1839  Instruction *InsertBef)
1840  : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1841  ExtractElement,
1843  2, InsertBef) {
1844  assert(isValidOperands(Val, Index) &&
1845  "Invalid extractelement instruction operands!");
1846  Op<0>() = Val;
1847  Op<1>() = Index;
1848  setName(Name);
1849 }
1850 
1851 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1852  const Twine &Name,
1853  BasicBlock *InsertAE)
1854  : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1855  ExtractElement,
1857  2, InsertAE) {
1858  assert(isValidOperands(Val, Index) &&
1859  "Invalid extractelement instruction operands!");
1860 
1861  Op<0>() = Val;
1862  Op<1>() = Index;
1863  setName(Name);
1864 }
1865 
1867  if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
1868  return false;
1869  return true;
1870 }
1871 
1872 //===----------------------------------------------------------------------===//
1873 // InsertElementInst Implementation
1874 //===----------------------------------------------------------------------===//
1875 
1876 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1877  const Twine &Name,
1878  Instruction *InsertBef)
1879  : Instruction(Vec->getType(), InsertElement,
1880  OperandTraits<InsertElementInst>::op_begin(this),
1881  3, InsertBef) {
1882  assert(isValidOperands(Vec, Elt, Index) &&
1883  "Invalid insertelement instruction operands!");
1884  Op<0>() = Vec;
1885  Op<1>() = Elt;
1886  Op<2>() = Index;
1887  setName(Name);
1888 }
1889 
1890 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1891  const Twine &Name,
1892  BasicBlock *InsertAE)
1893  : Instruction(Vec->getType(), InsertElement,
1894  OperandTraits<InsertElementInst>::op_begin(this),
1895  3, InsertAE) {
1896  assert(isValidOperands(Vec, Elt, Index) &&
1897  "Invalid insertelement instruction operands!");
1898 
1899  Op<0>() = Vec;
1900  Op<1>() = Elt;
1901  Op<2>() = Index;
1902  setName(Name);
1903 }
1904 
1905 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1906  const Value *Index) {
1907  if (!Vec->getType()->isVectorTy())
1908  return false; // First operand of insertelement must be vector type.
1909 
1910  if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1911  return false;// Second operand of insertelement must be vector element type.
1912 
1913  if (!Index->getType()->isIntegerTy())
1914  return false; // Third operand of insertelement must be i32.
1915  return true;
1916 }
1917 
1918 //===----------------------------------------------------------------------===//
1919 // ShuffleVectorInst Implementation
1920 //===----------------------------------------------------------------------===//
1921 
1923  assert(V && "Cannot create placeholder of nullptr V");
1924  return PoisonValue::get(V->getType());
1925 }
1926 
1928  Instruction *InsertBefore)
1930  InsertBefore) {}
1931 
1933  BasicBlock *InsertAtEnd)
1935  InsertAtEnd) {}
1936 
1938  const Twine &Name,
1939  Instruction *InsertBefore)
1941  InsertBefore) {}
1942 
1944  const Twine &Name, BasicBlock *InsertAtEnd)
1946  InsertAtEnd) {}
1947 
1949  const Twine &Name,
1950  Instruction *InsertBefore)
1951  : Instruction(
1952  VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1953  cast<VectorType>(Mask->getType())->getElementCount()),
1954  ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this),
1955  OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) {
1956  assert(isValidOperands(V1, V2, Mask) &&
1957  "Invalid shuffle vector instruction operands!");
1958 
1959  Op<0>() = V1;
1960  Op<1>() = V2;
1961  SmallVector<int, 16> MaskArr;
1962  getShuffleMask(cast<Constant>(Mask), MaskArr);
1963  setShuffleMask(MaskArr);
1964  setName(Name);
1965 }
1966 
1968  const Twine &Name, BasicBlock *InsertAtEnd)
1969  : Instruction(
1970  VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1971  cast<VectorType>(Mask->getType())->getElementCount()),
1972  ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this),
1973  OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) {
1974  assert(isValidOperands(V1, V2, Mask) &&
1975  "Invalid shuffle vector instruction operands!");
1976 
1977  Op<0>() = V1;
1978  Op<1>() = V2;
1979  SmallVector<int, 16> MaskArr;
1980  getShuffleMask(cast<Constant>(Mask), MaskArr);
1981  setShuffleMask(MaskArr);
1982  setName(Name);
1983 }
1984 
1986  const Twine &Name,
1987  Instruction *InsertBefore)
1988  : Instruction(
1989  VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1990  Mask.size(), isa<ScalableVectorType>(V1->getType())),
1991  ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this),
1992  OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) {
1993  assert(isValidOperands(V1, V2, Mask) &&
1994  "Invalid shuffle vector instruction operands!");
1995  Op<0>() = V1;
1996  Op<1>() = V2;
1998  setName(Name);
1999 }
2000 
2002  const Twine &Name, BasicBlock *InsertAtEnd)
2003  : Instruction(
2004  VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
2005  Mask.size(), isa<ScalableVectorType>(V1->getType())),
2006  ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this),
2007  OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) {
2008  assert(isValidOperands(V1, V2, Mask) &&
2009  "Invalid shuffle vector instruction operands!");
2010 
2011  Op<0>() = V1;
2012  Op<1>() = V2;
2014  setName(Name);
2015 }
2016 
2018  int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2019  int NumMaskElts = ShuffleMask.size();
2020  SmallVector<int, 16> NewMask(NumMaskElts);
2021  for (int i = 0; i != NumMaskElts; ++i) {
2022  int MaskElt = getMaskValue(i);
2023  if (MaskElt == UndefMaskElem) {
2024  NewMask[i] = UndefMaskElem;
2025  continue;
2026  }
2027  assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask");
2028  MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts;
2029  NewMask[i] = MaskElt;
2030  }
2031  setShuffleMask(NewMask);
2032  Op<0>().swap(Op<1>());
2033 }
2034 
2036  ArrayRef<int> Mask) {
2037  // V1 and V2 must be vectors of the same type.
2038  if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
2039  return false;
2040 
2041  // Make sure the mask elements make sense.
2042  int V1Size =
2043  cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue();
2044  for (int Elem : Mask)
2045  if (Elem != UndefMaskElem && Elem >= V1Size * 2)
2046  return false;
2047 
2048  if (isa<ScalableVectorType>(V1->getType()))
2049  if ((Mask[0] != 0 && Mask[0] != UndefMaskElem) || !is_splat(Mask))
2050  return false;
2051 
2052  return true;
2053 }
2054 
2056  const Value *Mask) {
2057  // V1 and V2 must be vectors of the same type.
2058  if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
2059  return false;
2060 
2061  // Mask must be vector of i32, and must be the same kind of vector as the
2062  // input vectors
2063  auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
2064  if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32) ||
2065  isa<ScalableVectorType>(MaskTy) != isa<ScalableVectorType>(V1->getType()))
2066  return false;
2067 
2068  // Check to see if Mask is valid.
2069  if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
2070  return true;
2071 
2072  if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
2073  unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements();
2074  for (Value *Op : MV->operands()) {
2075  if (auto *CI = dyn_cast<ConstantInt>(Op)) {
2076  if (CI->uge(V1Size*2))
2077  return false;
2078  } else if (!isa<UndefValue>(Op)) {
2079  return false;
2080  }
2081  }
2082  return true;
2083  }
2084 
2085  if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
2086  unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements();
2087  for (unsigned i = 0, e = cast<FixedVectorType>(MaskTy)->getNumElements();
2088  i != e; ++i)
2089  if (CDS->getElementAsInteger(i) >= V1Size*2)
2090  return false;
2091  return true;
2092  }
2093 
2094  return false;
2095 }
2096 
2098  SmallVectorImpl<int> &Result) {
2099  ElementCount EC = cast<VectorType>(Mask->getType())->getElementCount();
2100 
2101  if (isa<ConstantAggregateZero>(Mask)) {
2102  Result.resize(EC.getKnownMinValue(), 0);
2103  return;
2104  }
2105 
2106  Result.reserve(EC.getKnownMinValue());
2107 
2108  if (EC.isScalable()) {
2109  assert((isa<ConstantAggregateZero>(Mask) || isa<UndefValue>(Mask)) &&
2110  "Scalable vector shuffle mask must be undef or zeroinitializer");
2111  int MaskVal = isa<UndefValue>(Mask) ? -1 : 0;
2112  for (unsigned I = 0; I < EC.getKnownMinValue(); ++I)
2113  Result.emplace_back(MaskVal);
2114  return;
2115  }
2116 
2117  unsigned NumElts = EC.getKnownMinValue();
2118 
2119  if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
2120  for (unsigned i = 0; i != NumElts; ++i)
2121  Result.push_back(CDS->getElementAsInteger(i));
2122  return;
2123  }
2124  for (unsigned i = 0; i != NumElts; ++i) {
2125  Constant *C = Mask->getAggregateElement(i);
2126  Result.push_back(isa<UndefValue>(C) ? -1 :
2127  cast<ConstantInt>(C)->getZExtValue());
2128  }
2129 }
2130 
2132  ShuffleMask.assign(Mask.begin(), Mask.end());
2133  ShuffleMaskForBitcode = convertShuffleMaskForBitcode(Mask, getType());
2134 }
2135 
2137  Type *ResultTy) {
2138  Type *Int32Ty = Type::getInt32Ty(ResultTy->getContext());
2139  if (isa<ScalableVectorType>(ResultTy)) {
2140  assert(is_splat(Mask) && "Unexpected shuffle");
2141  Type *VecTy = VectorType::get(Int32Ty, Mask.size(), true);
2142  if (Mask[0] == 0)
2143  return Constant::getNullValue(VecTy);
2144  return UndefValue::get(VecTy);
2145  }
2146  SmallVector<Constant *, 16> MaskConst;
2147  for (int Elem : Mask) {
2148  if (Elem == UndefMaskElem)
2149  MaskConst.push_back(UndefValue::get(Int32Ty));
2150  else
2151  MaskConst.push_back(ConstantInt::get(Int32Ty, Elem));
2152  }
2153  return ConstantVector::get(MaskConst);
2154 }
2155 
2156 static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
2157  assert(!Mask.empty() && "Shuffle mask must contain elements");
2158  bool UsesLHS = false;
2159  bool UsesRHS = false;
2160  for (int I : Mask) {
2161  if (I == -1)
2162  continue;
2163  assert(I >= 0 && I < (NumOpElts * 2) &&
2164  "Out-of-bounds shuffle mask element");
2165  UsesLHS |= (I < NumOpElts);
2166  UsesRHS |= (I >= NumOpElts);
2167  if (UsesLHS && UsesRHS)
2168  return false;
2169  }
2170  // Allow for degenerate case: completely undef mask means neither source is used.
2171  return UsesLHS || UsesRHS;
2172 }
2173 
2175  // We don't have vector operand size information, so assume operands are the
2176  // same size as the mask.
2177  return isSingleSourceMaskImpl(Mask, Mask.size());
2178 }
2179 
2180 static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
2181  if (!isSingleSourceMaskImpl(Mask, NumOpElts))
2182  return false;
2183  for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
2184  if (Mask[i] == -1)
2185  continue;
2186  if (Mask[i] != i && Mask[i] != (NumOpElts + i))
2187  return false;
2188  }
2189  return true;
2190 }
2191 
2193  // We don't have vector operand size information, so assume operands are the
2194  // same size as the mask.
2195  return isIdentityMaskImpl(Mask, Mask.size());
2196 }
2197 
2199  if (!isSingleSourceMask(Mask))
2200  return false;
2201  for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
2202  if (Mask[i] == -1)
2203  continue;
2204  if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
2205  return false;
2206  }
2207  return true;
2208 }
2209 
2211  if (!isSingleSourceMask(Mask))
2212  return false;
2213  for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
2214  if (Mask[i] == -1)
2215  continue;
2216  if (Mask[i] != 0 && Mask[i] != NumElts)
2217  return false;
2218  }
2219  return true;
2220 }
2221 
2223  // Select is differentiated from identity. It requires using both sources.
2224  if (isSingleSourceMask(Mask))
2225  return false;
2226  for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
2227  if (Mask[i] == -1)
2228  continue;
2229  if (Mask[i] != i && Mask[i] != (NumElts + i))
2230  return false;
2231  }
2232  return true;
2233 }
2234 
2236  // Example masks that will return true:
2237  // v1 = <a, b, c, d>
2238  // v2 = <e, f, g, h>
2239  // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
2240  // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
2241 
2242  // 1. The number of elements in the mask must be a power-of-2 and at least 2.
2243  int NumElts = Mask.size();
2244  if (NumElts < 2 || !isPowerOf2_32(NumElts))
2245  return false;
2246 
2247  // 2. The first element of the mask must be either a 0 or a 1.
2248  if (Mask[0] != 0 && Mask[0] != 1)
2249  return false;
2250 
2251  // 3. The difference between the first 2 elements must be equal to the
2252  // number of elements in the mask.
2253  if ((Mask[1] - Mask[0]) != NumElts)
2254  return false;
2255 
2256  // 4. The difference between consecutive even-numbered and odd-numbered
2257  // elements must be equal to 2.
2258  for (int i = 2; i < NumElts; ++i) {
2259  int MaskEltVal = Mask[i];
2260  if (MaskEltVal == -1)
2261  return false;
2262  int MaskEltPrevVal = Mask[i - 2];
2263  if (MaskEltVal - MaskEltPrevVal != 2)
2264  return false;
2265  }
2266  return true;
2267 }
2268 
2270  int NumSrcElts, int &Index) {
2271  // Must extract from a single source.
2272  if (!isSingleSourceMaskImpl(Mask, NumSrcElts))
2273  return false;
2274 
2275  // Must be smaller (else this is an Identity shuffle).
2276  if (NumSrcElts <= (int)Mask.size())
2277  return false;
2278 
2279  // Find start of extraction, accounting that we may start with an UNDEF.
2280  int SubIndex = -1;
2281  for (int i = 0, e = Mask.size(); i != e; ++i) {
2282  int M = Mask[i];
2283  if (M < 0)
2284  continue;
2285  int Offset = (M % NumSrcElts) - i;
2286  if (0 <= SubIndex && SubIndex != Offset)
2287  return false;
2288  SubIndex = Offset;
2289  }
2290 
2291  if (0 <= SubIndex && SubIndex + (int)Mask.size() <= NumSrcElts) {
2292  Index = SubIndex;
2293  return true;
2294  }
2295  return false;
2296 }
2297 
2299  int NumSrcElts, int &NumSubElts,
2300  int &Index) {
2301  int NumMaskElts = Mask.size();
2302 
2303  // Don't try to match if we're shuffling to a smaller size.
2304  if (NumMaskElts < NumSrcElts)
2305  return false;
2306 
2307  // TODO: We don't recognize self-insertion/widening.
2308  if (isSingleSourceMaskImpl(Mask, NumSrcElts))
2309  return false;
2310 
2311  // Determine which mask elements are attributed to which source.
2312  APInt UndefElts = APInt::getZero(NumMaskElts);
2313  APInt Src0Elts = APInt::getZero(NumMaskElts);
2314  APInt Src1Elts = APInt::getZero(NumMaskElts);
2315  bool Src0Identity = true;
2316  bool Src1Identity = true;
2317 
2318  for (int i = 0; i != NumMaskElts; ++i) {
2319  int M = Mask[i];
2320  if (M < 0) {
2321  UndefElts.setBit(i);
2322  continue;
2323  }
2324  if (M < NumSrcElts) {
2325  Src0Elts.setBit(i);
2326  Src0Identity &= (M == i);
2327  continue;
2328  }
2329  Src1Elts.setBit(i);
2330  Src1Identity &= (M == (i + NumSrcElts));
2331  continue;
2332  }
2333  assert((Src0Elts | Src1Elts | UndefElts).isAllOnes() &&
2334  "unknown shuffle elements");
2335  assert(!Src0Elts.isZero() && !Src1Elts.isZero() &&
2336  "2-source shuffle not found");
2337 
2338  // Determine lo/hi span ranges.
2339  // TODO: How should we handle undefs at the start of subvector insertions?
2340  int Src0Lo = Src0Elts.countTrailingZeros();
2341  int Src1Lo = Src1Elts.countTrailingZeros();
2342  int Src0Hi = NumMaskElts - Src0Elts.countLeadingZeros();
2343  int Src1Hi = NumMaskElts - Src1Elts.countLeadingZeros();
2344 
2345  // If src0 is in place, see if the src1 elements is inplace within its own
2346  // span.
2347  if (Src0Identity) {
2348  int NumSub1Elts = Src1Hi - Src1Lo;
2349  ArrayRef<int> Sub1Mask = Mask.slice(Src1Lo, NumSub1Elts);
2350  if (isIdentityMaskImpl(Sub1Mask, NumSrcElts)) {
2351  NumSubElts = NumSub1Elts;
2352  Index = Src1Lo;
2353  return true;
2354  }
2355  }
2356 
2357  // If src1 is in place, see if the src0 elements is inplace within its own
2358  // span.
2359  if (Src1Identity) {
2360  int NumSub0Elts = Src0Hi - Src0Lo;
2361  ArrayRef<int> Sub0Mask = Mask.slice(Src0Lo, NumSub0Elts);
2362  if (isIdentityMaskImpl(Sub0Mask, NumSrcElts)) {
2363  NumSubElts = NumSub0Elts;
2364  Index = Src0Lo;
2365  return true;
2366  }
2367  }
2368 
2369  return false;
2370 }
2371 
2373  if (isa<UndefValue>(Op<2>()))
2374  return false;
2375 
2376  // FIXME: Not currently possible to express a shuffle mask for a scalable
2377  // vector for this case.
2378  if (isa<ScalableVectorType>(getType()))
2379  return false;
2380 
2381  int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2382  int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements();
2383  if (NumMaskElts <= NumOpElts)
2384  return false;
2385 
2386  // The first part of the mask must choose elements from exactly 1 source op.
2388  if (!isIdentityMaskImpl(Mask, NumOpElts))
2389  return false;
2390 
2391  // All extending must be with undef elements.
2392  for (int i = NumOpElts; i < NumMaskElts; ++i)
2393  if (Mask[i] != -1)
2394  return false;
2395 
2396  return true;
2397 }
2398 
2400  if (isa<UndefValue>(Op<2>()))
2401  return false;
2402 
2403  // FIXME: Not currently possible to express a shuffle mask for a scalable
2404  // vector for this case.
2405  if (isa<ScalableVectorType>(getType()))
2406  return false;
2407 
2408  int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2409  int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements();
2410  if (NumMaskElts >= NumOpElts)
2411  return false;
2412 
2413  return isIdentityMaskImpl(getShuffleMask(), NumOpElts);
2414 }
2415 
2417  // Vector concatenation is differentiated from identity with padding.
2418  if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()) ||
2419  isa<UndefValue>(Op<2>()))
2420  return false;
2421 
2422  // FIXME: Not currently possible to express a shuffle mask for a scalable
2423  // vector for this case.
2424  if (isa<ScalableVectorType>(getType()))
2425  return false;
2426 
2427  int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2428  int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements();
2429  if (NumMaskElts != NumOpElts * 2)
2430  return false;
2431 
2432  // Use the mask length rather than the operands' vector lengths here. We
2433  // already know that the shuffle returns a vector twice as long as the inputs,
2434  // and neither of the inputs are undef vectors. If the mask picks consecutive
2435  // elements from both inputs, then this is a concatenation of the inputs.
2436  return isIdentityMaskImpl(getShuffleMask(), NumMaskElts);
2437 }
2438 
2439 //===----------------------------------------------------------------------===//
2440 // InsertValueInst Class
2441 //===----------------------------------------------------------------------===//
2442 
2443 void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2444  const Twine &Name) {
2445  assert(getNumOperands() == 2 && "NumOperands not initialized?");
2446 
2447  // There's no fundamental reason why we require at least one index
2448  // (other than weirdness with &*IdxBegin being invalid; see
2449  // getelementptr's init routine for example). But there's no
2450  // present need to support it.
2451  assert(!Idxs.empty() && "InsertValueInst must have at least one index");
2452 
2454  Val->getType() && "Inserted value must match indexed type!");
2455  Op<0>() = Agg;
2456  Op<1>() = Val;
2457 
2458  Indices.append(Idxs.begin(), Idxs.end());
2459  setName(Name);
2460 }
2461 
2462 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
2463  : Instruction(IVI.getType(), InsertValue,
2464  OperandTraits<InsertValueInst>::op_begin(this), 2),
2465  Indices(IVI.Indices) {
2466  Op<0>() = IVI.getOperand(0);
2467  Op<1>() = IVI.getOperand(1);
2469 }
2470 
2471 //===----------------------------------------------------------------------===//
2472 // ExtractValueInst Class
2473 //===----------------------------------------------------------------------===//
2474 
2475 void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
2476  assert(getNumOperands() == 1 && "NumOperands not initialized?");
2477 
2478  // There's no fundamental reason why we require at least one index.
2479  // But there's no present need to support it.
2480  assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
2481 
2482  Indices.append(Idxs.begin(), Idxs.end());
2483  setName(Name);
2484 }
2485 
2486 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
2487  : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
2488  Indices(EVI.Indices) {
2490 }
2491 
2492 // getIndexedType - Returns the type of the element that would be extracted
2493 // with an extractvalue instruction with the specified parameters.
2494 //
2495 // A null type is returned if the indices are invalid for the specified
2496 // pointer type.
2497 //
2499  ArrayRef<unsigned> Idxs) {
2500  for (unsigned Index : Idxs) {
2501  // We can't use CompositeType::indexValid(Index) here.
2502  // indexValid() always returns true for arrays because getelementptr allows
2503  // out-of-bounds indices. Since we don't allow those for extractvalue and
2504  // insertvalue we need to check array indexing manually.
2505  // Since the only other types we can index into are struct types it's just
2506  // as easy to check those manually as well.
2507  if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
2508  if (Index >= AT->getNumElements())
2509  return nullptr;
2510  Agg = AT->getElementType();
2511  } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
2512  if (Index >= ST->getNumElements())
2513  return nullptr;
2514  Agg = ST->getElementType(Index);
2515  } else {
2516  // Not a valid type to index into.
2517  return nullptr;
2518  }
2519  }
2520  return const_cast<Type*>(Agg);
2521 }
2522 
2523 //===----------------------------------------------------------------------===//
2524 // UnaryOperator Class
2525 //===----------------------------------------------------------------------===//
2526 
2528  Type *Ty, const Twine &Name,
2529  Instruction *InsertBefore)
2530  : UnaryInstruction(Ty, iType, S, InsertBefore) {
2531  Op<0>() = S;
2532  setName(Name);
2533  AssertOK();
2534 }
2535 
2537  Type *Ty, const Twine &Name,
2538  BasicBlock *InsertAtEnd)
2539  : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
2540  Op<0>() = S;
2541  setName(Name);
2542  AssertOK();
2543 }
2544 
2546  const Twine &Name,
2547  Instruction *InsertBefore) {
2548  return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore);
2549 }
2550 
2552  const Twine &Name,
2553  BasicBlock *InsertAtEnd) {
2554  UnaryOperator *Res = Create(Op, S, Name);
2555  InsertAtEnd->getInstList().push_back(Res);
2556  return Res;
2557 }
2558 
2559 void UnaryOperator::AssertOK() {
2560  Value *LHS = getOperand(0);
2561  (void)LHS; // Silence warnings.
2562 #ifndef NDEBUG
2563  switch (getOpcode()) {
2564  case FNeg:
2565  assert(getType() == LHS->getType() &&
2566  "Unary operation should return same type as operand!");
2567  assert(getType()->isFPOrFPVectorTy() &&
2568  "Tried to create a floating-point operation on a "
2569  "non-floating-point type!");
2570  break;
2571  default: llvm_unreachable("Invalid opcode provided");
2572  }
2573 #endif
2574 }
2575 
2576 //===----------------------------------------------------------------------===//
2577 // BinaryOperator Class
2578 //===----------------------------------------------------------------------===//
2579 
2581  Type *Ty, const Twine &Name,
2582  Instruction *InsertBefore)
2583  : Instruction(Ty, iType,
2584  OperandTraits<BinaryOperator>::op_begin(this),
2585  OperandTraits<BinaryOperator>::operands(this),
2586  InsertBefore) {
2587  Op<0>() = S1;
2588  Op<1>() = S2;
2589  setName(Name);
2590  AssertOK();
2591 }
2592 
2594  Type *Ty, const Twine &Name,
2595  BasicBlock *InsertAtEnd)
2596  : Instruction(Ty, iType,
2597  OperandTraits<BinaryOperator>::op_begin(this),
2598  OperandTraits<BinaryOperator>::operands(this),
2599  InsertAtEnd) {
2600  Op<0>() = S1;
2601  Op<1>() = S2;
2602  setName(Name);
2603  AssertOK();
2604 }
2605 
2606 void BinaryOperator::AssertOK() {
2607  Value *LHS = getOperand(0), *RHS = getOperand(1);
2608  (void)LHS; (void)RHS; // Silence warnings.
2609  assert(LHS->getType() == RHS->getType() &&
2610  "Binary operator operand types must match!");
2611 #ifndef NDEBUG
2612  switch (getOpcode()) {
2613  case Add: case Sub:
2614  case Mul:
2615  assert(getType() == LHS->getType() &&
2616  "Arithmetic operation should return same type as operands!");
2617  assert(getType()->isIntOrIntVectorTy() &&
2618  "Tried to create an integer operation on a non-integer type!");
2619  break;
2620  case FAdd: case FSub:
2621  case FMul:
2622  assert(getType() == LHS->getType() &&
2623  "Arithmetic operation should return same type as operands!");
2624  assert(getType()->isFPOrFPVectorTy() &&
2625  "Tried to create a floating-point operation on a "
2626  "non-floating-point type!");
2627  break;
2628  case UDiv:
2629  case SDiv:
2630  assert(getType() == LHS->getType() &&
2631  "Arithmetic operation should return same type as operands!");
2632  assert(getType()->isIntOrIntVectorTy() &&
2633  "Incorrect operand type (not integer) for S/UDIV");
2634  break;
2635  case FDiv:
2636  assert(getType() == LHS->getType() &&
2637  "Arithmetic operation should return same type as operands!");
2638  assert(getType()->isFPOrFPVectorTy() &&
2639  "Incorrect operand type (not floating point) for FDIV");
2640  break;
2641  case URem:
2642  case SRem:
2643  assert(getType() == LHS->getType() &&
2644  "Arithmetic operation should return same type as operands!");
2645  assert(getType()->isIntOrIntVectorTy() &&
2646  "Incorrect operand type (not integer) for S/UREM");
2647  break;
2648  case FRem:
2649  assert(getType() == LHS->getType() &&
2650  "Arithmetic operation should return same type as operands!");
2651  assert(getType()->isFPOrFPVectorTy() &&
2652  "Incorrect operand type (not floating point) for FREM");
2653  break;
2654  case Shl:
2655  case LShr:
2656  case AShr:
2657  assert(getType() == LHS->getType() &&
2658  "Shift operation should return same type as operands!");
2659  assert(getType()->isIntOrIntVectorTy() &&
2660  "Tried to create a shift operation on a non-integral type!");
2661  break;
2662  case And: case Or:
2663  case Xor:
2664  assert(getType() == LHS->getType() &&
2665  "Logical operation should return same type as operands!");
2666  assert(getType()->isIntOrIntVectorTy() &&
2667  "Tried to create a logical operation on a non-integral type!");
2668  break;
2669  default: llvm_unreachable("Invalid opcode provided");
2670  }
2671 #endif
2672 }
2673 
2675  const Twine &Name,
2676  Instruction *InsertBefore) {
2677  assert(S1->getType() == S2->getType() &&
2678  "Cannot create binary operator with two operands of differing type!");
2679  return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
2680 }
2681 
2683  const Twine &Name,
2684  BasicBlock *InsertAtEnd) {
2685  BinaryOperator *Res = Create(Op, S1, S2, Name);
2686  InsertAtEnd->getInstList().push_back(Res);
2687  return Res;
2688 }
2689 
2691  Instruction *InsertBefore) {
2693  return new BinaryOperator(Instruction::Sub,
2694  zero, Op,
2695  Op->getType(), Name, InsertBefore);
2696 }
2697 
2699  BasicBlock *InsertAtEnd) {
2701  return new BinaryOperator(Instruction::Sub,
2702  zero, Op,
2703  Op->getType(), Name, InsertAtEnd);
2704 }
2705 
2707  Instruction *InsertBefore) {
2709  return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2710 }
2711 
2713  BasicBlock *InsertAtEnd) {
2715  return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2716 }
2717 
2719  Instruction *InsertBefore) {
2721  return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2722 }
2723 
2725  BasicBlock *InsertAtEnd) {
2727  return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2728 }
2729 
2731  Instruction *InsertBefore) {
2733  return new BinaryOperator(Instruction::Xor, Op, C,
2734  Op->getType(), Name, InsertBefore);
2735 }
2736 
2738  BasicBlock *InsertAtEnd) {
2740  return new BinaryOperator(Instruction::Xor, Op, AllOnes,
2741  Op->getType(), Name, InsertAtEnd);
2742 }
2743 
2744 // Exchange the two operands to this instruction. This instruction is safe to
2745 // use on any binary instruction and does not modify the semantics of the
2746 // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2747 // is changed.
2749  if (!isCommutative())
2750  return true; // Can't commute operands
2751  Op<0>().swap(Op<1>());
2752  return false;
2753 }
2754 
2755 //===----------------------------------------------------------------------===//
2756 // FPMathOperator Class
2757 //===----------------------------------------------------------------------===//
2758 
2760  const MDNode *MD =
2761  cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2762  if (!MD)
2763  return 0.0;
2764  ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
2765  return Accuracy->getValueAPF().convertToFloat();
2766 }
2767 
2768 //===----------------------------------------------------------------------===//
2769 // CastInst Class
2770 //===----------------------------------------------------------------------===//
2771 
2772 // Just determine if this cast only deals with integral->integral conversion.
2774  switch (getOpcode()) {
2775  default: return false;
2776  case Instruction::ZExt:
2777  case Instruction::SExt:
2778  case Instruction::Trunc:
2779  return true;
2780  case Instruction::BitCast:
2781  return getOperand(0)->getType()->isIntegerTy() &&
2782  getType()->isIntegerTy();
2783  }
2784 }
2785 
2787  // Only BitCast can be lossless, exit fast if we're not BitCast
2788  if (getOpcode() != Instruction::BitCast)
2789  return false;
2790 
2791  // Identity cast is always lossless
2792  Type *SrcTy = getOperand(0)->getType();
2793  Type *DstTy = getType();
2794  if (SrcTy == DstTy)
2795  return true;
2796 
2797  // Pointer to pointer is always lossless.
2798  if (SrcTy->isPointerTy())
2799  return DstTy->isPointerTy();
2800  return false; // Other types have no identity values
2801 }
2802 
2803 /// This function determines if the CastInst does not require any bits to be
2804 /// changed in order to effect the cast. Essentially, it identifies cases where
2805 /// no code gen is necessary for the cast, hence the name no-op cast. For
2806 /// example, the following are all no-op casts:
2807 /// # bitcast i32* %x to i8*
2808 /// # bitcast <2 x i32> %x to <4 x i16>
2809 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
2810 /// Determine if the described cast is a no-op.
2812  Type *SrcTy,
2813  Type *DestTy,
2814  const DataLayout &DL) {
2815  assert(castIsValid(Opcode, SrcTy, DestTy) && "method precondition");
2816  switch (Opcode) {
2817  default: llvm_unreachable("Invalid CastOp");
2818  case Instruction::Trunc:
2819  case Instruction::ZExt:
2820  case Instruction::SExt:
2821  case Instruction::FPTrunc:
2822  case Instruction::FPExt:
2823  case Instruction::UIToFP:
2824  case Instruction::SIToFP:
2825  case Instruction::FPToUI:
2826  case Instruction::FPToSI:
2827  case Instruction::AddrSpaceCast:
2828  // TODO: Target informations may give a more accurate answer here.
2829  return false;
2830  case Instruction::BitCast:
2831  return true; // BitCast never modifies bits.
2832  case Instruction::PtrToInt:
2833  return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
2834  DestTy->getScalarSizeInBits();
2835  case Instruction::IntToPtr:
2836  return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
2837  SrcTy->getScalarSizeInBits();
2838  }
2839 }
2840 
2841 bool CastInst::isNoopCast(const DataLayout &DL) const {
2842  return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
2843 }
2844 
2845 /// This function determines if a pair of casts can be eliminated and what
2846 /// opcode should be used in the elimination. This assumes that there are two
2847 /// instructions like this:
2848 /// * %F = firstOpcode SrcTy %x to MidTy
2849 /// * %S = secondOpcode MidTy %F to DstTy
2850 /// The function returns a resultOpcode so these two casts can be replaced with:
2851 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
2852 /// If no such cast is permitted, the function returns 0.
2854  Instruction::CastOps firstOp, Instruction::CastOps secondOp,
2855  Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2856  Type *DstIntPtrTy) {
2857  // Define the 144 possibilities for these two cast instructions. The values
2858  // in this matrix determine what to do in a given situation and select the
2859  // case in the switch below. The rows correspond to firstOp, the columns
2860  // correspond to secondOp. In looking at the table below, keep in mind
2861  // the following cast properties:
2862  //
2863  // Size Compare Source Destination
2864  // Operator Src ? Size Type Sign Type Sign
2865  // -------- ------------ ------------------- ---------------------
2866  // TRUNC > Integer Any Integral Any
2867  // ZEXT < Integral Unsigned Integer Any
2868  // SEXT < Integral Signed Integer Any
2869  // FPTOUI n/a FloatPt n/a Integral Unsigned
2870  // FPTOSI n/a FloatPt n/a Integral Signed
2871  // UITOFP n/a Integral Unsigned FloatPt n/a
2872  // SITOFP n/a Integral Signed FloatPt n/a
2873  // FPTRUNC > FloatPt n/a FloatPt n/a
2874  // FPEXT < FloatPt n/a FloatPt n/a
2875  // PTRTOINT n/a Pointer n/a Integral Unsigned
2876  // INTTOPTR n/a Integral Unsigned Pointer n/a
2877  // BITCAST = FirstClass n/a FirstClass n/a
2878  // ADDRSPCST n/a Pointer n/a Pointer n/a
2879  //
2880  // NOTE: some transforms are safe, but we consider them to be non-profitable.
2881  // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2882  // into "fptoui double to i64", but this loses information about the range
2883  // of the produced value (we no longer know the top-part is all zeros).
2884  // Further this conversion is often much more expensive for typical hardware,
2885  // and causes issues when building libgcc. We disallow fptosi+sext for the
2886  // same reason.
2887  const unsigned numCastOps =
2888  Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2889  static const uint8_t CastResults[numCastOps][numCastOps] = {
2890  // T F F U S F F P I B A -+
2891  // R Z S P P I I T P 2 N T S |
2892  // U E E 2 2 2 2 R E I T C C +- secondOp
2893  // N X X U S F F N X N 2 V V |
2894  // C T T I I P P C T T P T T -+
2895  { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
2896  { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
2897  { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2898  { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2899  { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2900  { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2901  { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
2902  { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
2903  { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
2904  { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2905  { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2906  { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2907  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
2908  };
2909 
2910  // TODO: This logic could be encoded into the table above and handled in the
2911  // switch below.
2912  // If either of the casts are a bitcast from scalar to vector, disallow the
2913  // merging. However, any pair of bitcasts are allowed.
2914  bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2915  bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2916  bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
2917 
2918  // Check if any of the casts convert scalars <-> vectors.
2919  if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2920  (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2921  if (!AreBothBitcasts)
2922  return 0;
2923 
2924  int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2925  [secondOp-Instruction::CastOpsBegin];
2926  switch (ElimCase) {
2927  case 0:
2928  // Categorically disallowed.
2929  return 0;
2930  case 1:
2931  // Allowed, use first cast's opcode.
2932  return firstOp;
2933  case 2:
2934  // Allowed, use second cast's opcode.
2935  return secondOp;
2936  case 3:
2937  // No-op cast in second op implies firstOp as long as the DestTy
2938  // is integer and we are not converting between a vector and a
2939  // non-vector type.
2940  if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
2941  return firstOp;
2942  return 0;
2943  case 4:
2944  // No-op cast in second op implies firstOp as long as the DestTy
2945  // is floating point.
2946  if (DstTy->isFloatingPointTy())
2947  return firstOp;
2948  return 0;
2949  case 5:
2950  // No-op cast in first op implies secondOp as long as the SrcTy
2951  // is an integer.
2952  if (SrcTy->isIntegerTy())
2953  return secondOp;
2954  return 0;
2955  case 6:
2956  // No-op cast in first op implies secondOp as long as the SrcTy
2957  // is a floating point.
2958  if (SrcTy->isFloatingPointTy())
2959  return secondOp;
2960  return 0;
2961  case 7: {
2962  // Disable inttoptr/ptrtoint optimization if enabled.
2963  if (DisableI2pP2iOpt)
2964  return 0;
2965 
2966  // Cannot simplify if address spaces are different!
2967  if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2968  return 0;
2969 
2970  unsigned MidSize = MidTy->getScalarSizeInBits();
2971  // We can still fold this without knowing the actual sizes as long we
2972  // know that the intermediate pointer is the largest possible
2973  // pointer size.
2974  // FIXME: Is this always true?
2975  if (MidSize == 64)
2976  return Instruction::BitCast;
2977 
2978  // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
2979  if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
2980  return 0;
2981  unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
2982  if (MidSize >= PtrSize)
2983  return Instruction::BitCast;
2984  return 0;
2985  }
2986  case 8: {
2987  // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2988  // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2989  // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2990  unsigned SrcSize = SrcTy->getScalarSizeInBits();
2991  unsigned DstSize = DstTy->getScalarSizeInBits();
2992  if (SrcSize == DstSize)
2993  return Instruction::BitCast;
2994  else if (SrcSize < DstSize)
2995  return firstOp;
2996  return secondOp;
2997  }
2998  case 9:
2999  // zext, sext -> zext, because sext can't sign extend after zext
3000  return Instruction::ZExt;
3001  case 11: {
3002  // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
3003  if (!MidIntPtrTy)
3004  return 0;
3005  unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
3006  unsigned SrcSize = SrcTy->getScalarSizeInBits();
3007  unsigned DstSize = DstTy->getScalarSizeInBits();
3008  if (SrcSize <= PtrSize && SrcSize == DstSize)
3009  return Instruction::BitCast;
3010  return 0;
3011  }
3012  case 12:
3013  // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
3014  // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
3015  if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
3016  return Instruction::AddrSpaceCast;
3017  return Instruction::BitCast;
3018  case 13:
3019  // FIXME: this state can be merged with (1), but the following assert
3020  // is useful to check the correcteness of the sequence due to semantic
3021  // change of bitcast.
3022  assert(
3023  SrcTy->isPtrOrPtrVectorTy() &&
3024  MidTy->isPtrOrPtrVectorTy() &&
3025  DstTy->isPtrOrPtrVectorTy() &&
3026  SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
3027  MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
3028  "Illegal addrspacecast, bitcast sequence!");
3029  // Allowed, use first cast's opcode
3030  return firstOp;
3031  case 14: {
3032  // bitcast, addrspacecast -> addrspacecast if the element type of
3033  // bitcast's source is the same as that of addrspacecast's destination.
3034  PointerType *SrcPtrTy = cast<PointerType>(SrcTy->getScalarType());
3035  PointerType *DstPtrTy = cast<PointerType>(DstTy->getScalarType());
3036  if (SrcPtrTy->hasSameElementTypeAs(DstPtrTy))
3037  return Instruction::AddrSpaceCast;
3038  return 0;
3039  }
3040  case 15:
3041  // FIXME: this state can be merged with (1), but the following assert
3042  // is useful to check the correcteness of the sequence due to semantic
3043  // change of bitcast.
3044  assert(
3045  SrcTy->isIntOrIntVectorTy() &&
3046  MidTy->isPtrOrPtrVectorTy() &&
3047  DstTy->isPtrOrPtrVectorTy() &&
3048  MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
3049  "Illegal inttoptr, bitcast sequence!");
3050  // Allowed, use first cast's opcode
3051  return firstOp;
3052  case 16:
3053  // FIXME: this state can be merged with (2), but the following assert
3054  // is useful to check the correcteness of the sequence due to semantic
3055  // change of bitcast.
3056  assert(
3057  SrcTy->isPtrOrPtrVectorTy() &&
3058  MidTy->isPtrOrPtrVectorTy() &&
3059  DstTy->isIntOrIntVectorTy() &&
3060  SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
3061  "Illegal bitcast, ptrtoint sequence!");
3062  // Allowed, use second cast's opcode
3063  return secondOp;
3064  case 17:
3065  // (sitofp (zext x)) -> (uitofp x)
3066  return Instruction::UIToFP;
3067  case 99:
3068  // Cast combination can't happen (error in input). This is for all cases
3069  // where the MidTy is not the same for the two cast instructions.
3070  llvm_unreachable("Invalid Cast Combination");
3071  default:
3072  llvm_unreachable("Error in CastResults table!!!");
3073  }
3074 }
3075 
3077  const Twine &Name, Instruction *InsertBefore) {
3078  assert(castIsValid(op, S, Ty) && "Invalid cast!");
3079  // Construct and return the appropriate CastInst subclass
3080  switch (op) {
3081  case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
3082  case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
3083  case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
3084  case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
3085  case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
3086  case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
3087  case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
3088  case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
3089  case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
3090  case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
3091  case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
3092  case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
3093  case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
3094  default: llvm_unreachable("Invalid opcode provided");
3095  }
3096 }
3097 
3099  const Twine &Name, BasicBlock *InsertAtEnd) {
3100  assert(castIsValid(op, S, Ty) && "Invalid cast!");
3101  // Construct and return the appropriate CastInst subclass
3102  switch (op) {
3103  case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
3104  case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
3105  case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
3106  case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
3107  case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
3108  case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
3109  case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
3110  case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
3111  case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
3112  case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
3113  case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
3114  case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
3115  case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
3116  default: llvm_unreachable("Invalid opcode provided");
3117  }
3118 }
3119 
3121  const Twine &Name,
3122  Instruction *InsertBefore) {
3123  if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
3124  return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3125  return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
3126 }
3127 
3129  const Twine &Name,
3130  BasicBlock *InsertAtEnd) {
3131  if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
3132  return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
3133  return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
3134 }
3135 
3137  const Twine &Name,
3138  Instruction *InsertBefore) {
3139  if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
3140  return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3141  return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
3142 }
3143 
3145  const Twine &Name,
3146  BasicBlock *InsertAtEnd) {
3147  if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
3148  return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
3149  return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
3150 }
3151 
3153  const Twine &Name,
3154  Instruction *InsertBefore) {
3155  if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
3156  return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3157  return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
3158 }
3159 
3161  const Twine &Name,
3162  BasicBlock *InsertAtEnd) {
3163  if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
3164  return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
3165  return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
3166 }
3167 
3169  const Twine &Name,
3170  BasicBlock *InsertAtEnd) {
3171  assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
3172  assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
3173  "Invalid cast");
3174  assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
3175  assert((!Ty->isVectorTy() ||
3176  cast<VectorType>(Ty)->getElementCount() ==
3177  cast<VectorType>(S->getType())->getElementCount()) &&
3178  "Invalid cast");
3179 
3180  if (Ty->isIntOrIntVectorTy())
3181  return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
3182 
3183  return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
3184 }
3185 
3186 /// Create a BitCast or a PtrToInt cast instruction
3188  const Twine &Name,
3189  Instruction *InsertBefore) {
3190  assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
3191  assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
3192  "Invalid cast");
3193  assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
3194  assert((!Ty->isVectorTy() ||
3195  cast<VectorType>(Ty)->getElementCount() ==
3196  cast<VectorType>(S->getType())->getElementCount()) &&
3197  "Invalid cast");
3198 
3199  if (Ty->isIntOrIntVectorTy())
3200  return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
3201 
3202  return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
3203 }
3204 
3206  Value *S, Type *Ty,
3207  const Twine &Name,
3208  BasicBlock *InsertAtEnd) {
3209  assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
3210  assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
3211 
3212  if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
3213  return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
3214 
3215  return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
3216 }
3217 
3219  Value *S, Type *Ty,
3220  const Twine &Name,
3221  Instruction *InsertBefore) {
3222  assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
3223  assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
3224 
3225  if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
3226  return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
3227 
3228  return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3229 }
3230 
3232  const Twine &Name,
3233  Instruction *InsertBefore) {
3234  if (S->getType()->isPointerTy() && Ty->isIntegerTy())
3235  return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
3236  if (S->getType()->isIntegerTy() && Ty->isPointerTy())
3237  return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
3238 
3239  return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
3240 }
3241 
3243  bool isSigned, const Twine &Name,
3244  Instruction *InsertBefore) {
3245  assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
3246  "Invalid integer cast");
3247  unsigned SrcBits = C->getType()->getScalarSizeInBits();
3248  unsigned DstBits = Ty->getScalarSizeInBits();
3249  Instruction::CastOps opcode =
3250  (SrcBits == DstBits ? Instruction::BitCast :
3251  (SrcBits > DstBits ? Instruction::Trunc :
3252  (isSigned ? Instruction::SExt : Instruction::ZExt)));
3253  return Create(opcode, C, Ty, Name, InsertBefore);
3254 }
3255 
3257  bool isSigned, const Twine &Name,
3258  BasicBlock *InsertAtEnd) {
3259  assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
3260  "Invalid cast");
3261  unsigned SrcBits = C->getType()->getScalarSizeInBits();
3262  unsigned DstBits = Ty->getScalarSizeInBits();
3263  Instruction::CastOps opcode =
3264  (SrcBits == DstBits ? Instruction::BitCast :
3265  (SrcBits > DstBits ? Instruction::Trunc :
3266  (isSigned ? Instruction::SExt : Instruction::ZExt)));
3267  return Create(opcode, C, Ty, Name, InsertAtEnd);
3268 }
3269 
3271  const Twine &Name,
3272  Instruction *InsertBefore) {
3273  assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
3274  "Invalid cast");
3275  unsigned SrcBits = C->getType()->getScalarSizeInBits();
3276  unsigned DstBits = Ty->getScalarSizeInBits();
3277  Instruction::CastOps opcode =
3278  (SrcBits == DstBits ? Instruction::BitCast :
3279  (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
3280  return Create(opcode, C, Ty, Name, InsertBefore);
3281 }
3282 
3284  const Twine &Name,
3285  BasicBlock *InsertAtEnd) {
3286  assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
3287  "Invalid cast");
3288  unsigned SrcBits = C->getType()->getScalarSizeInBits();
3289  unsigned DstBits = Ty->getScalarSizeInBits();
3290  Instruction::CastOps opcode =
3291  (SrcBits == DstBits ? Instruction::BitCast :
3292  (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
3293  return Create(opcode, C, Ty, Name, InsertAtEnd);
3294 }
3295 
3296 bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
3297  if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
3298  return false;
3299 
3300  if (SrcTy == DestTy)
3301  return true;
3302 
3303  if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3304  if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
3305  if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) {
3306  // An element by element cast. Valid if casting the elements is valid.
3307  SrcTy = SrcVecTy->getElementType();
3308  DestTy = DestVecTy->getElementType();
3309  }
3310  }
3311  }
3312 
3313  if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
3314  if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
3315  return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
3316  }
3317  }
3318 
3319  TypeSize SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3320  TypeSize DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
3321 
3322  // Could still have vectors of pointers if the number of elements doesn't
3323  // match
3324  if (SrcBits.getKnownMinSize() == 0 || DestBits.getKnownMinSize() == 0)
3325  return false;
3326 
3327  if (SrcBits != DestBits)
3328  return false;
3329 
3330  if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
3331  return false;
3332 
3333  return true;
3334 }
3335 
3337  const DataLayout &DL) {
3338  // ptrtoint and inttoptr are not allowed on non-integral pointers
3339  if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
3340  if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
3341  return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
3342  !DL.isNonIntegralPointerType(PtrTy));
3343  if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
3344  if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
3345  return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
3346  !DL.isNonIntegralPointerType(PtrTy));
3347 
3348  return isBitCastable(SrcTy, DestTy);
3349 }
3350 
3351 // Provide a way to get a "cast" where the cast opcode is inferred from the
3352 // types and size of the operand. This, basically, is a parallel of the
3353 // logic in the castIsValid function below. This axiom should hold:
3354 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
3355 // should not assert in castIsValid. In other words, this produces a "correct"
3356 // casting opcode for the arguments passed to it.
3359  const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
3360  Type *SrcTy = Src->getType();
3361 
3362  assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
3363  "Only first class types are castable!");
3364 
3365  if (SrcTy == DestTy)
3366  return BitCast;
3367 
3368  // FIXME: Check address space sizes here
3369  if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3370  if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
3371  if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) {
3372  // An element by element cast. Find the appropriate opcode based on the
3373  // element types.
3374  SrcTy = SrcVecTy->getElementType();
3375  DestTy = DestVecTy->getElementType();
3376  }
3377 
3378  // Get the bit sizes, we'll need these
3379  unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3380  unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
3381 
3382  // Run through the possibilities ...
3383  if (DestTy->isIntegerTy()) { // Casting to integral
3384  if (SrcTy->isIntegerTy()) { // Casting from integral
3385  if (DestBits < SrcBits)
3386  return Trunc; // int -> smaller int
3387  else if (DestBits > SrcBits) { // its an extension
3388  if (SrcIsSigned)
3389  return SExt; // signed -> SEXT
3390  else
3391  return ZExt; // unsigned -> ZEXT
3392  } else {
3393  return BitCast; // Same size, No-op cast
3394  }
3395  } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
3396  if (DestIsSigned)
3397  return FPToSI; // FP -> sint
3398  else
3399  return FPToUI; // FP -> uint
3400  } else if (SrcTy->isVectorTy()) {
3401  assert(DestBits == SrcBits &&
3402  "Casting vector to integer of different width");
3403  return BitCast; // Same size, no-op cast
3404  } else {
3405  assert(SrcTy->isPointerTy() &&
3406  "Casting from a value that is not first-class type");
3407  return PtrToInt; // ptr -> int
3408  }
3409  } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3410  if (SrcTy->isIntegerTy()) { // Casting from integral
3411  if (SrcIsSigned)
3412  return SIToFP; // sint -> FP
3413  else
3414  return UIToFP; // uint -> FP
3415  } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
3416  if (DestBits < SrcBits) {
3417  return FPTrunc; // FP -> smaller FP
3418  } else if (DestBits > SrcBits) {
3419  return FPExt; // FP -> larger FP
3420  } else {
3421  return BitCast; // same size, no-op cast
3422  }
3423  } else if (SrcTy->isVectorTy()) {
3424  assert(DestBits == SrcBits &&
3425  "Casting vector to floating point of different width");
3426  return BitCast; // same size, no-op cast
3427  }
3428  llvm_unreachable("Casting pointer or non-first class to float");
3429  } else if (DestTy->isVectorTy()) {
3430  assert(DestBits == SrcBits &&
3431  "Illegal cast to vector (wrong type or size)");
3432  return BitCast;
3433  } else if (DestTy->isPointerTy()) {
3434  if (SrcTy->isPointerTy()) {
3435  if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3436  return AddrSpaceCast;
3437  return BitCast; // ptr -> ptr
3438  } else if (SrcTy->isIntegerTy()) {
3439  return IntToPtr; // int -> ptr
3440  }
3441  llvm_unreachable("Casting pointer to other than pointer or int");
3442  } else if (DestTy->isX86_MMXTy()) {
3443  if (SrcTy->isVectorTy()) {
3444  assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
3445  return BitCast; // 64-bit vector to MMX
3446  }
3447  llvm_unreachable("Illegal cast to X86_MMX");
3448  }
3449  llvm_unreachable("Casting to type that is not first-class");
3450 }
3451 
3452 //===----------------------------------------------------------------------===//
3453 // CastInst SubClass Constructors
3454 //===----------------------------------------------------------------------===//
3455 
3456 /// Check that the construction parameters for a CastInst are correct. This
3457 /// could be broken out into the separate constructors but it is useful to have
3458 /// it in one place and to eliminate the redundant code for getting the sizes
3459 /// of the types involved.
3460 bool
3462  if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3463  SrcTy->isAggregateType() || DstTy->isAggregateType())
3464  return false;
3465 
3466  // Get the size of the types in bits, and whether we are dealing
3467  // with vector types, we'll need this later.
3468  bool SrcIsVec = isa<VectorType>(SrcTy);
3469  bool DstIsVec = isa<VectorType>(DstTy);
3470  unsigned SrcScalarBitSize = SrcTy->getScalarSizeInBits();
3471  unsigned DstScalarBitSize = DstTy->getScalarSizeInBits();
3472 
3473  // If these are vector types, get the lengths of the vectors (using zero for
3474  // scalar types means that checking that vector lengths match also checks that
3475  // scalars are not being converted to vectors or vectors to scalars).
3476  ElementCount SrcEC = SrcIsVec ? cast<VectorType>(SrcTy)->getElementCount()
3478  ElementCount DstEC = DstIsVec ? cast<VectorType>(DstTy)->getElementCount()
3480 
3481  // Switch on the opcode provided
3482  switch (op) {
3483  default: return false; // This is an input error
3484  case Instruction::Trunc:
3485  return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3486  SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize;
3487  case Instruction::ZExt:
3488  return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3489  SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize;
3490  case Instruction::SExt:
3491  return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3492  SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize;
3493  case Instruction::FPTrunc:
3494  return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3495  SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize;
3496  case Instruction::FPExt:
3497  return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3498  SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize;
3499  case Instruction::UIToFP:
3500  case Instruction::SIToFP:
3501  return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3502  SrcEC == DstEC;
3503  case Instruction::FPToUI:
3504  case Instruction::FPToSI:
3505  return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3506  SrcEC == DstEC;
3507  case Instruction::PtrToInt:
3508  if (SrcEC != DstEC)
3509  return false;
3510  return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
3511  case Instruction::IntToPtr:
3512  if (SrcEC != DstEC)
3513  return false;
3514  return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
3515  case Instruction::BitCast: {
3516  PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3517  PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3518 
3519  // BitCast implies a no-op cast of type only. No bits change.
3520  // However, you can't cast pointers to anything but pointers.
3521  if (!SrcPtrTy != !DstPtrTy)
3522  return false;
3523 
3524  // For non-pointer cases, the cast is okay if the source and destination bit
3525  // widths are identical.
3526  if (!SrcPtrTy)
3527  return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3528 
3529  // If both are pointers then the address spaces must match.
3530  if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3531  return false;
3532 
3533  // A vector of pointers must have the same number of elements.
3534  if (SrcIsVec && DstIsVec)
3535  return SrcEC == DstEC;
3536  if (SrcIsVec)
3537  return SrcEC == ElementCount::getFixed(1);
3538  if (DstIsVec)
3539  return DstEC == ElementCount::getFixed(1);
3540 
3541  return true;
3542  }
3543  case Instruction::AddrSpaceCast: {
3544  PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3545  if (!SrcPtrTy)
3546  return false;
3547 
3548  PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3549  if (!DstPtrTy)
3550  return false;
3551 
3552  if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3553  return false;
3554 
3555  return SrcEC == DstEC;
3556  }
3557  }
3558 }
3559 
3561  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3562 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
3563  assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
3564 }
3565 
3567  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3568 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
3569  assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
3570 }
3571 
3573  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3574 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
3575  assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
3576 }
3577 
3579  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3580 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
3581  assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
3582 }
3584  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3585 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
3586  assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
3587 }
3588 
3590  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3591 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
3592  assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
3593 }
3594 
3596  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3597 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
3598  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
3599 }
3600 
3602  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3603 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
3604  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
3605 }
3606 
3608  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3609 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
3610  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
3611 }
3612 
3614  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3615 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
3616  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
3617 }
3618 
3620  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3621 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
3622  assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
3623 }
3624 
3626  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3627 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
3628  assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
3629 }
3630 
3632  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3633 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
3634  assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
3635 }
3636 
3638  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3639 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
3640  assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
3641 }
3642 
3644  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3645 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
3646  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
3647 }
3648 
3650  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3651 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
3652  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
3653 }
3654 
3656  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3657 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
3658  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
3659 }
3660 
3662  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3663 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
3664  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
3665 }
3666 
3668  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3669 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
3670  assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
3671 }
3672 
3674  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3675 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
3676  assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
3677 }
3678 
3680  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3681 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
3682  assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
3683 }
3684 
3686  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3687 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
3688  assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
3689 }
3690 
3692  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3693 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
3694  assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
3695 }
3696 
3698  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3699 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
3700  assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
3701 }
3702 
3704  Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3705 ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3706  assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3707 }
3708 
3710  Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3711 ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3712  assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3713 }
3714 
3715 //===----------------------------------------------------------------------===//
3716 // CmpInst Classes
3717 //===----------------------------------------------------------------------===//
3718 
3720  Value *RHS, const Twine &Name, Instruction *InsertBefore,
3721  Instruction *FlagsSource)
3722  : Instruction(ty, op,
3723  OperandTraits<CmpInst>::op_begin(this),
3724  OperandTraits<CmpInst>::operands(this),
3725  InsertBefore) {
3726  Op<0>() = LHS;
3727  Op<1>() = RHS;
3728  setPredicate((Predicate)predicate);
3729  setName(Name);
3730  if (FlagsSource)
3731  copyIRFlags(FlagsSource);
3732 }
3733 
3735  Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
3736  : Instruction(ty, op,
3737  OperandTraits<CmpInst>::op_begin(this),
3738  OperandTraits<CmpInst>::operands(this),
3739  InsertAtEnd) {
3740  Op<0>() = LHS;
3741  Op<1>() = RHS;
3742  setPredicate((Predicate)predicate);
3743  setName(Name);
3744 }
3745 
3746 CmpInst *
3748  const Twine &Name, Instruction *InsertBefore) {
3749  if (Op == Instruction::ICmp) {
3750  if (InsertBefore)
3751  return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3752  S1, S2, Name);
3753  else
3754  return new ICmpInst(CmpInst::Predicate(predicate),
3755  S1, S2, Name);
3756  }
3757 
3758  if (InsertBefore)
3759  return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3760  S1, S2, Name);
3761  else
3762  return new FCmpInst(CmpInst::Predicate(predicate),
3763  S1, S2, Name);
3764 }
3765 
3766 CmpInst *
3768  const Twine &Name, BasicBlock *InsertAtEnd) {
3769  if (Op == Instruction::ICmp) {
3770  return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3771  S1, S2, Name);
3772  }
3773  return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3774  S1, S2, Name);
3775 }
3776 
3778  if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3779  IC->swapOperands();
3780  else
3781  cast<FCmpInst>(this)->swapOperands();
3782 }
3783 
3785  if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
3786  return IC->isCommutative();
3787  return cast<FCmpInst>(this)->isCommutative();
3788 }
3789 
3792  return ICmpInst::isEquality(P);
3794  return FCmpInst::isEquality(P);
3795  llvm_unreachable("Unsupported predicate kind");
3796 }
3797 
3799  switch (pred) {
3800  default: llvm_unreachable("Unknown cmp predicate!");
3801  case ICMP_EQ: return ICMP_NE;
3802  case ICMP_NE: return ICMP_EQ;
3803  case ICMP_UGT: return ICMP_ULE;
3804  case ICMP_ULT: return ICMP_UGE;
3805  case ICMP_UGE: return ICMP_ULT;
3806  case ICMP_ULE: return ICMP_UGT;
3807  case ICMP_SGT: return ICMP_SLE;
3808  case ICMP_SLT: return ICMP_SGE;
3809  case ICMP_SGE: return ICMP_SLT;
3810  case ICMP_SLE: return ICMP_SGT;
3811 
3812  case FCMP_OEQ: return FCMP_UNE;
3813  case FCMP_ONE: return FCMP_UEQ;
3814  case FCMP_OGT: return FCMP_ULE;
3815  case FCMP_OLT: return FCMP_UGE;
3816  case FCMP_OGE: return FCMP_ULT;
3817  case FCMP_OLE: return FCMP_UGT;
3818  case FCMP_UEQ: return FCMP_ONE;
3819  case FCMP_UNE: return FCMP_OEQ;
3820  case FCMP_UGT: return FCMP_OLE;
3821  case FCMP_ULT: return FCMP_OGE;
3822  case FCMP_UGE: return FCMP_OLT;
3823  case FCMP_ULE: return FCMP_OGT;
3824  case FCMP_ORD: return FCMP_UNO;
3825  case FCMP_UNO: return FCMP_ORD;
3826  case FCMP_TRUE: return FCMP_FALSE;
3827  case FCMP_FALSE: return FCMP_TRUE;
3828  }
3829 }
3830 
3832  switch (Pred) {
3833  default: return "unknown";
3834  case FCmpInst::FCMP_FALSE: return "false";
3835  case FCmpInst::FCMP_OEQ: return "oeq";
3836  case FCmpInst::FCMP_OGT: return "ogt";
3837  case FCmpInst::FCMP_OGE: return "oge";
3838  case FCmpInst::FCMP_OLT: return "olt";
3839  case FCmpInst::FCMP_OLE: return "ole";
3840  case FCmpInst::FCMP_ONE: return "one";
3841  case FCmpInst::FCMP_ORD: return "ord";
3842  case FCmpInst::FCMP_UNO: return "uno";
3843  case FCmpInst::FCMP_UEQ: return "ueq";
3844  case FCmpInst::FCMP_UGT: return "ugt";
3845  case FCmpInst::FCMP_UGE: return "uge";
3846  case FCmpInst::FCMP_ULT: return "ult";
3847  case FCmpInst::FCMP_ULE: return "ule";
3848  case FCmpInst::FCMP_UNE: return "une";
3849  case FCmpInst::FCMP_TRUE: return "true";
3850  case ICmpInst::ICMP_EQ: return "eq";
3851  case ICmpInst::ICMP_NE: return "ne";
3852  case ICmpInst::ICMP_SGT: return "sgt";
3853  case ICmpInst::ICMP_SGE: return "sge";
3854  case ICmpInst::ICMP_SLT: return "slt";
3855  case ICmpInst::ICMP_SLE: return "sle";
3856  case ICmpInst::ICMP_UGT: return "ugt";
3857  case ICmpInst::ICMP_UGE: return "uge";
3858  case ICmpInst::ICMP_ULT: return "ult";
3859  case ICmpInst::ICMP_ULE: return "ule";
3860  }
3861 }
3862 
3864  switch (pred) {
3865  default: llvm_unreachable("Unknown icmp predicate!");
3866  case ICMP_EQ: case ICMP_NE:
3867  case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3868  return pred;
3869  case ICMP_UGT: return ICMP_SGT;
3870  case ICMP_ULT: return ICMP_SLT;
3871  case ICMP_UGE: return ICMP_SGE;
3872  case ICMP_ULE: return ICMP_SLE;
3873  }
3874 }
3875 
3877  switch (pred) {
3878  default: llvm_unreachable("Unknown icmp predicate!");
3879  case ICMP_EQ: case ICMP_NE:
3880  case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3881  return pred;
3882  case ICMP_SGT: return ICMP_UGT;
3883  case ICMP_SLT: return ICMP_ULT;
3884  case ICMP_SGE: return ICMP_UGE;
3885  case ICMP_SLE: return ICMP_ULE;
3886  }
3887 }
3888 
3890  switch (pred) {
3891  default: llvm_unreachable("Unknown cmp predicate!");
3892  case ICMP_EQ: case ICMP_NE:
3893  return pred;
3894  case ICMP_SGT: return ICMP_SLT;
3895  case ICMP_SLT: return ICMP_SGT;
3896  case ICMP_SGE: return ICMP_SLE;
3897  case ICMP_SLE: return ICMP_SGE;
3898  case ICMP_UGT: return ICMP_ULT;
3899  case ICMP_ULT: return ICMP_UGT;
3900  case ICMP_UGE: return ICMP_ULE;
3901  case ICMP_ULE: return ICMP_UGE;
3902 
3903  case FCMP_FALSE: case FCMP_TRUE:
3904  case FCMP_OEQ: case FCMP_ONE:
3905  case FCMP_UEQ: case FCMP_UNE:
3906  case FCMP_ORD: case FCMP_UNO:
3907  return pred;
3908  case FCMP_OGT: return FCMP_OLT;
3909  case FCMP_OLT: return FCMP_OGT;
3910  case FCMP_OGE: return FCMP_OLE;
3911  case FCMP_OLE: return FCMP_OGE;
3912  case FCMP_UGT: return FCMP_ULT;
3913  case FCMP_ULT: return FCMP_UGT;
3914  case FCMP_UGE: return FCMP_ULE;
3915  case FCMP_ULE: return FCMP_UGE;
3916  }
3917 }
3918 
3920  switch (pred) {
3921  case ICMP_SGE:
3922  case ICMP_SLE:
3923  case ICMP_UGE:
3924  case ICMP_ULE:
3925  case FCMP_OGE:
3926  case FCMP_OLE:
3927  case FCMP_UGE:
3928  case FCMP_ULE:
3929  return true;
3930  default:
3931  return false;
3932  }
3933 }
3934 
3936  switch (pred) {
3937  case ICMP_SGT:
3938  case ICMP_SLT:
3939  case ICMP_UGT:
3940  case ICMP_ULT:
3941  case FCMP_OGT:
3942  case FCMP_OLT:
3943  case FCMP_UGT:
3944  case FCMP_ULT:
3945  return true;
3946  default:
3947  return false;
3948  }
3949 }
3950 
3952  switch (pred) {
3953  case ICMP_SGE:
3954  return ICMP_SGT;
3955  case ICMP_SLE:
3956  return ICMP_SLT;
3957  case ICMP_UGE:
3958  return ICMP_UGT;
3959  case ICMP_ULE:
3960  return ICMP_ULT;
3961  case FCMP_OGE:
3962  return FCMP_OGT;
3963  case FCMP_OLE:
3964  return FCMP_OLT;
3965  case FCMP_UGE:
3966  return FCMP_UGT;
3967  case FCMP_ULE:
3968  return FCMP_ULT;
3969  default:
3970  return pred;
3971  }
3972 }
3973 
3975  switch (pred) {
3976  case ICMP_SGT:
3977  return ICMP_SGE;
3978  case ICMP_SLT:
3979  return ICMP_SLE;
3980  case ICMP_UGT:
3981  return ICMP_UGE;
3982  case ICMP_ULT:
3983  return ICMP_ULE;
3984  case FCMP_OGT:
3985  return FCMP_OGE;
3986  case FCMP_OLT:
3987  return FCMP_OLE;
3988  case FCMP_UGT:
3989  return FCMP_UGE;
3990  case FCMP_ULT:
3991  return FCMP_ULE;
3992  default:
3993  return pred;
3994  }
3995 }
3996 
3998  assert(CmpInst::isRelational(pred) && "Call only with relational predicate!");
3999 
4000  if (isStrictPredicate(pred))
4001  return getNonStrictPredicate(pred);
4003  return getStrictPredicate(pred);
4004 
4005  llvm_unreachable("Unknown predicate!");
4006 }
4007 
4009  assert(CmpInst::isUnsigned(pred) && "Call only with unsigned predicates!");
4010 
4011  switch (pred) {
4012  default:
4013  llvm_unreachable("Unknown predicate!");
4014  case CmpInst::ICMP_ULT:
4015  return CmpInst::ICMP_SLT;
4016  case CmpInst::ICMP_ULE:
4017  return CmpInst::ICMP_SLE;
4018  case CmpInst::ICMP_UGT:
4019  return CmpInst::ICMP_SGT;
4020  case CmpInst::ICMP_UGE:
4021  return CmpInst::ICMP_SGE;
4022  }
4023 }
4024 
4026  assert(CmpInst::isSigned(pred) && "Call only with signed predicates!");
4027 
4028  switch (pred) {
4029  default:
4030  llvm_unreachable("Unknown predicate!");
4031  case CmpInst::ICMP_SLT:
4032  return CmpInst::ICMP_ULT;
4033  case CmpInst::ICMP_SLE:
4034  return CmpInst::ICMP_ULE;
4035  case CmpInst::ICMP_SGT:
4036  return CmpInst::ICMP_UGT;
4037  case CmpInst::ICMP_SGE:
4038  return CmpInst::ICMP_UGE;
4039  }
4040 }
4041 
4043  switch (predicate) {
4044  default: return false;
4046  case ICmpInst::ICMP_UGE: return true;
4047  }
4048 }
4049 
4050 bool CmpInst::isSigned(Predicate predicate) {
4051  switch (predicate) {
4052  default: return false;
4054  case ICmpInst::ICMP_SGE: return true;
4055  }
4056 }
4057 
4060  "Call only with non-equality predicates!");
4061 
4062  if (isSigned(pred))
4063  return getUnsignedPredicate(pred);
4064  if (isUnsigned(pred))
4065  return getSignedPredicate(pred);
4066 
4067  llvm_unreachable("Unknown predicate!");
4068 }
4069 
4071  switch (predicate) {
4072  default: return false;
4075  case FCmpInst::FCMP_ORD: return true;
4076  }
4077 }
4078 
4080  switch (predicate) {
4081  default: return false;
4084  case FCmpInst::FCMP_UNO: return true;
4085  }
4086 }
4087 
4089  switch(predicate) {
4090  default: return false;
4091  case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
4092  case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
4093  }
4094 }
4095 
4097  switch(predicate) {
4098  case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
4099  case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
4100  default: return false;
4101  }
4102 }
4103 
4105  // If the predicates match, then we know the first condition implies the
4106  // second is true.
4107  if (Pred1 == Pred2)
4108  return true;
4109 
4110  switch (Pred1) {
4111  default:
4112  break;
4113  case ICMP_EQ:
4114  // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
4115  return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
4116  Pred2 == ICMP_SLE;
4117  case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
4118  return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
4119  case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
4120  return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
4121  case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
4122  return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
4123  case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
4124  return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
4125  }
4126  return false;
4127 }
4128 
4130  return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
4131 }
4132 
4133 //===----------------------------------------------------------------------===//
4134 // SwitchInst Implementation
4135 //===----------------------------------------------------------------------===//
4136 
4137 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
4138  assert(Value && Default && NumReserved);
4139  ReservedSpace = NumReserved;
4141  allocHungoffUses(ReservedSpace);
4142 
4143  Op<0>() = Value;
4144  Op<1>() = Default;
4145 }
4146 
4147 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
4148 /// switch on and a default destination. The number of additional cases can
4149 /// be specified here to make memory allocation more efficient. This
4150 /// constructor can also autoinsert before another instruction.
4151 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
4152  Instruction *InsertBefore)
4153  : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
4154  nullptr, 0, InsertBefore) {
4155  init(Value, Default, 2+NumCases*2);
4156 }
4157 
4158 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
4159 /// switch on and a default destination. The number of additional cases can
4160 /// be specified here to make memory allocation more efficient. This
4161 /// constructor also autoinserts at the end of the specified BasicBlock.
4162 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
4163  BasicBlock *InsertAtEnd)
4164  : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch,
4165  nullptr, 0, InsertAtEnd) {
4166  init(Value, Default, 2+NumCases*2);
4167 }
4168 
4169 SwitchInst::SwitchInst(const SwitchInst &SI)
4170  : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) {
4171  init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
4172  setNumHungOffUseOperands(SI.getNumOperands());
4173  Use *OL = getOperandList();
4174  const Use *InOL = SI.getOperandList();
4175  for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
4176  OL[i] = InOL[i];
4177  OL[i+1] = InOL[i+1];
4178  }
4179  SubclassOptionalData = SI.SubclassOptionalData;
4180 }
4181 
4182 /// addCase - Add an entry to the switch instruction...
4183 ///
4185  unsigned NewCaseIdx = getNumCases();
4186  unsigned OpNo = getNumOperands();
4187  if (OpNo+2 > ReservedSpace)
4188  growOperands(); // Get more space!
4189  // Initialize some new operands.
4190  assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
4191  setNumHungOffUseOperands(OpNo+2);
4192  CaseHandle Case(this, NewCaseIdx);
4193  Case.setValue(OnVal);
4194  Case.setSuccessor(Dest);
4195 }
4196 
4197 /// removeCase - This method removes the specified case and its successor
4198 /// from the switch instruction.
4200  unsigned idx = I->getCaseIndex();
4201 
4202  assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
4203 
4204  unsigned NumOps = getNumOperands();
4205  Use *OL = getOperandList();
4206 
4207  // Overwrite this case with the end of the list.
4208  if (2 + (idx + 1) * 2 != NumOps) {
4209  OL[2 + idx * 2] = OL[NumOps - 2];
4210  OL[2 + idx * 2 + 1] = OL[NumOps - 1];
4211  }
4212 
4213  // Nuke the last value.
4214  OL[NumOps-2].set(nullptr);
4215  OL[NumOps-2+1].set(nullptr);
4216  setNumHungOffUseOperands(NumOps-2);
4217 
4218  return CaseIt(this, idx);
4219 }
4220 
4221 /// growOperands - grow operands - This grows the operand list in response
4222 /// to a push_back style of operation. This grows the number of ops by 3 times.
4223 ///
4224 void SwitchInst::growOperands() {
4225  unsigned e = getNumOperands();
4226  unsigned NumOps = e*3;
4227 
4228  ReservedSpace = NumOps;
4229  growHungoffUses(ReservedSpace);
4230 }
4231 
4232 MDNode *
4234  if (MDNode *ProfileData = SI.getMetadata(LLVMContext::MD_prof))
4235  if (auto *MDName = dyn_cast<MDString>(ProfileData->getOperand(0)))
4236  if (MDName->getString() == "branch_weights")
4237  return ProfileData;
4238  return nullptr;
4239 }
4240 
4242  assert(Changed && "called only if metadata has changed");
4243 
4244  if (!Weights)
4245  return nullptr;
4246 
4247  assert(SI.getNumSuccessors() == Weights->size() &&
4248  "num of prof branch_weights must accord with num of successors");
4249 
4250  bool AllZeroes =
4251  all_of(Weights.getValue(), [](uint32_t W) { return W == 0; });
4252 
4253  if (AllZeroes || Weights.getValue().size() < 2)
4254  return nullptr;
4255 
4256  return MDBuilder(SI.getParent()->getContext()).createBranchWeights(*Weights);
4257 }
4258 
4260  MDNode *ProfileData = getProfBranchWeightsMD(SI);
4261  if (!ProfileData)
4262  return;
4263 
4264  if (ProfileData->getNumOperands() != SI.getNumSuccessors() + 1) {
4265  llvm_unreachable("number of prof branch_weights metadata operands does "
4266  "not correspond to number of succesors");
4267  }
4268 
4269  SmallVector<uint32_t, 8> Weights;
4270  for (unsigned CI = 1, CE = SI.getNumSuccessors(); CI <= CE; ++CI) {
4271  ConstantInt *C = mdconst::extract<ConstantInt>(ProfileData->getOperand(CI));
4272  uint32_t CW = C->getValue().getZExtValue();
4273  Weights.push_back(CW);
4274  }
4275  this->Weights = std::move(Weights);
4276 }
4277 
4280  if (Weights) {
4281  assert(SI.getNumSuccessors() == Weights->size() &&
4282  "num of prof branch_weights must accord with num of successors");
4283  Changed = true;
4284  // Copy the last case to the place of the removed one and shrink.
4285  // This is tightly coupled with the way SwitchInst::removeCase() removes
4286  // the cases in SwitchInst::removeCase(CaseIt).
4287  Weights.getValue()[I->getCaseIndex() + 1] = Weights.getValue().back();
4288  Weights.getValue().pop_back();
4289  }
4290  return SI.removeCase(I);
4291 }
4292 
4294  ConstantInt *OnVal, BasicBlock *Dest,
4296  SI.addCase(OnVal, Dest);
4297 
4298  if (!Weights && W && *W) {
4299  Changed = true;
4300  Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0);
4301  Weights.getValue()[SI.getNumSuccessors() - 1] = *W;
4302  } else if (Weights) {
4303  Changed = true;
4304  Weights.getValue().push_back(W ? *W : 0);
4305  }
4306  if (Weights)
4307  assert(SI.getNumSuccessors() == Weights->size() &&
4308  "num of prof branch_weights must accord with num of successors");
4309 }
4310 
4313  // Instruction is erased. Mark as unchanged to not touch it in the destructor.
4314  Changed = false;
4315  if (Weights)
4316  Weights->resize(0);
4317  return SI.eraseFromParent();
4318 }
4319 
4322  if (!Weights)
4323  return None;
4324  return Weights.getValue()[idx];
4325 }
4326 
4329  if (!W)
4330  return;
4331 
4332  if (!Weights && *W)
4333  Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0);
4334 
4335  if (Weights) {
4336  auto &OldW = Weights.getValue()[idx];
4337  if (*W != OldW) {
4338  Changed = true;
4339  OldW = *W;
4340  }
4341  }
4342 }
4343 
4346  unsigned idx) {
4347  if (MDNode *ProfileData = getProfBranchWeightsMD(SI))
4348  if (ProfileData->getNumOperands() == SI.getNumSuccessors() + 1)
4349  return mdconst::extract<ConstantInt>(ProfileData->getOperand(idx + 1))
4350  ->getValue()
4351  .getZExtValue();
4352 
4353  return None;
4354 }
4355 
4356 //===----------------------------------------------------------------------===//
4357 // IndirectBrInst Implementation
4358 //===----------------------------------------------------------------------===//
4359 
4360 void IndirectBrInst::init(Value *Address, unsigned NumDests) {
4361  assert(Address && Address->getType()->isPointerTy() &&
4362  "Address of indirectbr must be a pointer");
4363  ReservedSpace = 1+NumDests;
4365  allocHungoffUses(ReservedSpace);
4366 
4367  Op<0>() = Address;
4368 }
4369 
4370 
4371 /// growOperands - grow operands - This grows the operand list in response
4372 /// to a push_back style of operation. This grows the number of ops by 2 times.
4373 ///
4374 void IndirectBrInst::growOperands() {
4375  unsigned e = getNumOperands();
4376  unsigned NumOps = e*2;
4377 
4378  ReservedSpace = NumOps;
4379  growHungoffUses(ReservedSpace);
4380 }
4381 
4382 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
4383  Instruction *InsertBefore)
4384  : Instruction(Type::getVoidTy(Address->getContext()),
4385  Instruction::IndirectBr, nullptr, 0, InsertBefore) {
4386  init(Address, NumCases);
4387 }
4388 
4389 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
4390  BasicBlock *InsertAtEnd)
4391  : Instruction(Type::getVoidTy(Address->getContext()),
4392  Instruction::IndirectBr, nullptr, 0, InsertAtEnd) {
4393  init(Address, NumCases);
4394 }
4395 
4396 IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
4397  : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
4398  nullptr, IBI.getNumOperands()) {
4399  allocHungoffUses(IBI.getNumOperands());
4400  Use *OL = getOperandList();
4401  const Use *InOL = IBI.getOperandList();
4402  for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
4403  OL[i] = InOL[i];
4404  SubclassOptionalData = IBI.SubclassOptionalData;
4405 }
4406 
4407 /// addDestination - Add a destination.
4408 ///
4410  unsigned OpNo = getNumOperands();
4411  if (OpNo+1 > ReservedSpace)
4412  growOperands(); // Get more space!
4413  // Initialize some new operands.
4414  assert(OpNo < ReservedSpace && "Growing didn't work!");
4415  setNumHungOffUseOperands(OpNo+1);
4416  getOperandList()[OpNo] = DestBB;
4417 }
4418 
4419 /// removeDestination - This method removes the specified successor from the
4420 /// indirectbr instruction.
4422  assert(idx < getNumOperands()-1 && "Successor index out of range!");
4423 
4424  unsigned NumOps = getNumOperands();
4425  Use *OL = getOperandList();
4426 
4427  // Replace this value with the last one.
4428  OL[idx+1] = OL[NumOps-1];
4429 
4430  // Nuke the last value.
4431  OL[NumOps-1].set(nullptr);
4432  setNumHungOffUseOperands(NumOps-1);
4433 }
4434 
4435 //===----------------------------------------------------------------------===//
4436 // FreezeInst Implementation
4437 //===----------------------------------------------------------------------===//
4438 
4440