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