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