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