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