1 //===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements all of the non-inline methods for the LLVM instruction
13 //===----------------------------------------------------------------------===//
15 #include "LLVMContextImpl.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Module.h"
21 #include "llvm/Operator.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/CallSite.h"
25 #include "llvm/Support/ConstantRange.h"
26 #include "llvm/Support/MathExtras.h"
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
33 User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
36 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
37 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
40 //===----------------------------------------------------------------------===//
41 // TerminatorInst Class
42 //===----------------------------------------------------------------------===//
44 // Out of line virtual method, so the vtable, etc has a home.
45 TerminatorInst::~TerminatorInst() {
48 //===----------------------------------------------------------------------===//
49 // UnaryInstruction Class
50 //===----------------------------------------------------------------------===//
52 // Out of line virtual method, so the vtable, etc has a home.
53 UnaryInstruction::~UnaryInstruction() {
56 //===----------------------------------------------------------------------===//
58 //===----------------------------------------------------------------------===//
60 /// areInvalidOperands - Return a string if the specified operands are invalid
61 /// for a select operation, otherwise return null.
62 const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
66 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
68 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
69 return "vector select condition element type must be i1";
70 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
72 return "selected values for vector select must be vectors";
73 if (ET->getNumElements() != VT->getNumElements())
74 return "vector select requires selected vectors to have "
75 "the same vector length as select condition";
76 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
77 return "select condition must be i1 or <n x i1>";
83 //===----------------------------------------------------------------------===//
85 //===----------------------------------------------------------------------===//
87 PHINode::PHINode(const PHINode &PN)
88 : Instruction(PN.getType(), Instruction::PHI,
89 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
90 ReservedSpace(PN.getNumOperands()) {
91 Use *OL = OperandList;
92 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
93 OL[i] = PN.getOperand(i);
94 OL[i+1] = PN.getOperand(i+1);
96 SubclassOptionalData = PN.SubclassOptionalData;
101 dropHungoffUses(OperandList);
104 // removeIncomingValue - Remove an incoming value. This is useful if a
105 // predecessor basic block is deleted.
106 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
107 unsigned NumOps = getNumOperands();
108 Use *OL = OperandList;
109 assert(Idx*2 < NumOps && "BB not in PHI node!");
110 Value *Removed = OL[Idx*2];
112 // Move everything after this operand down.
114 // FIXME: we could just swap with the end of the list, then erase. However,
115 // client might not expect this to happen. The code as it is thrashes the
116 // use/def lists, which is kinda lame.
117 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
122 // Nuke the last value.
124 OL[NumOps-2+1].set(0);
125 NumOperands = NumOps-2;
127 // If the PHI node is dead, because it has zero entries, nuke it now.
128 if (NumOps == 2 && DeletePHIIfEmpty) {
129 // If anyone is using this PHI, make them use a dummy value instead...
130 replaceAllUsesWith(UndefValue::get(getType()));
136 /// resizeOperands - resize operands - This adjusts the length of the operands
137 /// list according to the following behavior:
138 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
139 /// of operation. This grows the number of ops by 1.5 times.
140 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
141 /// 3. If NumOps == NumOperands, trim the reserved space.
143 void PHINode::resizeOperands(unsigned NumOps) {
144 unsigned e = getNumOperands();
147 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
148 } else if (NumOps*2 > NumOperands) {
150 if (ReservedSpace >= NumOps) return;
151 } else if (NumOps == NumOperands) {
152 if (ReservedSpace == NumOps) return;
157 ReservedSpace = NumOps;
158 Use *OldOps = OperandList;
159 Use *NewOps = allocHungoffUses(NumOps);
160 std::copy(OldOps, OldOps + e, NewOps);
161 OperandList = NewOps;
162 if (OldOps) Use::zap(OldOps, OldOps + e, true);
165 /// hasConstantValue - If the specified PHI node always merges together the same
166 /// value, return the value, otherwise return null.
168 /// If the PHI has undef operands, but all the rest of the operands are
169 /// some unique value, return that value if it can be proved that the
170 /// value dominates the PHI. If DT is null, use a conservative check,
171 /// otherwise use DT to test for dominance.
173 Value *PHINode::hasConstantValue(DominatorTree *DT) const {
174 // If the PHI node only has one incoming value, eliminate the PHI node.
175 if (getNumIncomingValues() == 1) {
176 if (getIncomingValue(0) != this) // not X = phi X
177 return getIncomingValue(0);
178 return UndefValue::get(getType()); // Self cycle is dead.
181 // Otherwise if all of the incoming values are the same for the PHI, replace
182 // the PHI node with the incoming value.
185 bool HasUndefInput = false;
186 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
187 if (isa<UndefValue>(getIncomingValue(i))) {
188 HasUndefInput = true;
189 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
190 if (InVal && getIncomingValue(i) != InVal)
191 return 0; // Not the same, bail out.
192 InVal = getIncomingValue(i);
195 // The only case that could cause InVal to be null is if we have a PHI node
196 // that only has entries for itself. In this case, there is no entry into the
197 // loop, so kill the PHI.
199 if (InVal == 0) InVal = UndefValue::get(getType());
201 // If we have a PHI node like phi(X, undef, X), where X is defined by some
202 // instruction, we cannot always return X as the result of the PHI node. Only
203 // do this if X is not an instruction (thus it must dominate the PHI block),
204 // or if the client is prepared to deal with this possibility.
205 if (!HasUndefInput || !isa<Instruction>(InVal))
208 Instruction *IV = cast<Instruction>(InVal);
210 // We have a DominatorTree. Do a precise test.
211 if (!DT->dominates(IV, this))
214 // If it is in the entry block, it obviously dominates everything.
215 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
217 return 0; // Cannot guarantee that InVal dominates this PHINode.
220 // All of the incoming values are the same, return the value now.
225 //===----------------------------------------------------------------------===//
226 // CallInst Implementation
227 //===----------------------------------------------------------------------===//
229 CallInst::~CallInst() {
232 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
233 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
236 const FunctionType *FTy =
237 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
238 FTy = FTy; // silence warning.
240 assert((NumParams == FTy->getNumParams() ||
241 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
242 "Calling a function with bad signature!");
243 for (unsigned i = 0; i != NumParams; ++i) {
244 assert((i >= FTy->getNumParams() ||
245 FTy->getParamType(i) == Params[i]->getType()) &&
246 "Calling a function with a bad signature!");
247 OperandList[i] = Params[i];
251 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
252 assert(NumOperands == 3 && "NumOperands not set up?");
257 const FunctionType *FTy =
258 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
259 FTy = FTy; // silence warning.
261 assert((FTy->getNumParams() == 2 ||
262 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
263 "Calling a function with bad signature");
264 assert((0 >= FTy->getNumParams() ||
265 FTy->getParamType(0) == Actual1->getType()) &&
266 "Calling a function with a bad signature!");
267 assert((1 >= FTy->getNumParams() ||
268 FTy->getParamType(1) == Actual2->getType()) &&
269 "Calling a function with a bad signature!");
272 void CallInst::init(Value *Func, Value *Actual) {
273 assert(NumOperands == 2 && "NumOperands not set up?");
277 const FunctionType *FTy =
278 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
279 FTy = FTy; // silence warning.
281 assert((FTy->getNumParams() == 1 ||
282 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
283 "Calling a function with bad signature");
284 assert((0 == FTy->getNumParams() ||
285 FTy->getParamType(0) == Actual->getType()) &&
286 "Calling a function with a bad signature!");
289 void CallInst::init(Value *Func) {
290 assert(NumOperands == 1 && "NumOperands not set up?");
293 const FunctionType *FTy =
294 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
295 FTy = FTy; // silence warning.
297 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
300 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
301 Instruction *InsertBefore)
302 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
303 ->getElementType())->getReturnType(),
305 OperandTraits<CallInst>::op_end(this) - 2,
311 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
312 BasicBlock *InsertAtEnd)
313 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
314 ->getElementType())->getReturnType(),
316 OperandTraits<CallInst>::op_end(this) - 2,
321 CallInst::CallInst(Value *Func, const Twine &Name,
322 Instruction *InsertBefore)
323 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
324 ->getElementType())->getReturnType(),
326 OperandTraits<CallInst>::op_end(this) - 1,
332 CallInst::CallInst(Value *Func, const Twine &Name,
333 BasicBlock *InsertAtEnd)
334 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
335 ->getElementType())->getReturnType(),
337 OperandTraits<CallInst>::op_end(this) - 1,
343 CallInst::CallInst(const CallInst &CI)
344 : Instruction(CI.getType(), Instruction::Call,
345 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
346 CI.getNumOperands()) {
347 setAttributes(CI.getAttributes());
348 setTailCall(CI.isTailCall());
349 setCallingConv(CI.getCallingConv());
351 Use *OL = OperandList;
352 Use *InOL = CI.OperandList;
353 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
355 SubclassOptionalData = CI.SubclassOptionalData;
358 void CallInst::addAttribute(unsigned i, Attributes attr) {
359 AttrListPtr PAL = getAttributes();
360 PAL = PAL.addAttr(i, attr);
364 void CallInst::removeAttribute(unsigned i, Attributes attr) {
365 AttrListPtr PAL = getAttributes();
366 PAL = PAL.removeAttr(i, attr);
370 bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
371 if (AttributeList.paramHasAttr(i, attr))
373 if (const Function *F = getCalledFunction())
374 return F->paramHasAttr(i, attr);
378 /// IsConstantOne - Return true only if val is constant int 1
379 static bool IsConstantOne(Value *val) {
380 assert(val && "IsConstantOne does not work with NULL val");
381 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
384 static Instruction *createMalloc(Instruction *InsertBefore,
385 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
386 const Type *AllocTy, Value *AllocSize,
387 Value *ArraySize, Function *MallocF,
389 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
390 "createMalloc needs either InsertBefore or InsertAtEnd");
392 // malloc(type) becomes:
393 // bitcast (i8* malloc(typeSize)) to type*
394 // malloc(type, arraySize) becomes:
395 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
397 ArraySize = ConstantInt::get(IntPtrTy, 1);
398 else if (ArraySize->getType() != IntPtrTy) {
400 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
403 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
407 if (!IsConstantOne(ArraySize)) {
408 if (IsConstantOne(AllocSize)) {
409 AllocSize = ArraySize; // Operand * 1 = Operand
410 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
411 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
413 // Malloc arg is constant product of type size and array size
414 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
416 // Multiply type size by the array size...
418 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
419 "mallocsize", InsertBefore);
421 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
422 "mallocsize", InsertAtEnd);
426 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
427 // Create the call to Malloc.
428 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
429 Module* M = BB->getParent()->getParent();
430 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
431 Value *MallocFunc = MallocF;
433 // prototype malloc as "void *malloc(size_t)"
434 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
435 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
436 CallInst *MCall = NULL;
437 Instruction *Result = NULL;
439 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
441 if (Result->getType() != AllocPtrType)
442 // Create a cast instruction to convert to the right type...
443 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
445 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
447 if (Result->getType() != AllocPtrType) {
448 InsertAtEnd->getInstList().push_back(MCall);
449 // Create a cast instruction to convert to the right type...
450 Result = new BitCastInst(MCall, AllocPtrType, Name);
453 MCall->setTailCall();
454 if (Function *F = dyn_cast<Function>(MallocFunc)) {
455 MCall->setCallingConv(F->getCallingConv());
456 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
458 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
463 /// CreateMalloc - Generate the IR for a call to malloc:
464 /// 1. Compute the malloc call's argument as the specified type's size,
465 /// possibly multiplied by the array size if the array size is not
467 /// 2. Call malloc with that argument.
468 /// 3. Bitcast the result of the malloc call to the specified type.
469 Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
470 const Type *IntPtrTy, const Type *AllocTy,
471 Value *AllocSize, Value *ArraySize,
474 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
475 ArraySize, MallocF, Name);
478 /// CreateMalloc - Generate the IR for a call to malloc:
479 /// 1. Compute the malloc call's argument as the specified type's size,
480 /// possibly multiplied by the array size if the array size is not
482 /// 2. Call malloc with that argument.
483 /// 3. Bitcast the result of the malloc call to the specified type.
484 /// Note: This function does not add the bitcast to the basic block, that is the
485 /// responsibility of the caller.
486 Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
487 const Type *IntPtrTy, const Type *AllocTy,
488 Value *AllocSize, Value *ArraySize,
489 Function *MallocF, const Twine &Name) {
490 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
491 ArraySize, MallocF, Name);
494 static Instruction* createFree(Value* Source, Instruction *InsertBefore,
495 BasicBlock *InsertAtEnd) {
496 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
497 "createFree needs either InsertBefore or InsertAtEnd");
498 assert(Source->getType()->isPointerTy() &&
499 "Can not free something of nonpointer type!");
501 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
502 Module* M = BB->getParent()->getParent();
504 const Type *VoidTy = Type::getVoidTy(M->getContext());
505 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
506 // prototype free as "void free(void*)"
507 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
508 CallInst* Result = NULL;
509 Value *PtrCast = Source;
511 if (Source->getType() != IntPtrTy)
512 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
513 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
515 if (Source->getType() != IntPtrTy)
516 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
517 Result = CallInst::Create(FreeFunc, PtrCast, "");
519 Result->setTailCall();
520 if (Function *F = dyn_cast<Function>(FreeFunc))
521 Result->setCallingConv(F->getCallingConv());
526 /// CreateFree - Generate the IR for a call to the builtin free function.
527 Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
528 return createFree(Source, InsertBefore, NULL);
531 /// CreateFree - Generate the IR for a call to the builtin free function.
532 /// Note: This function does not add the call to the basic block, that is the
533 /// responsibility of the caller.
534 Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
535 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
536 assert(FreeCall && "CreateFree did not create a CallInst");
540 //===----------------------------------------------------------------------===//
541 // InvokeInst Implementation
542 //===----------------------------------------------------------------------===//
544 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
545 Value* const *Args, unsigned NumArgs) {
546 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
549 Op<-1>() = IfException;
550 const FunctionType *FTy =
551 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
552 FTy = FTy; // silence warning.
554 assert(((NumArgs == FTy->getNumParams()) ||
555 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
556 "Invoking a function with bad signature");
558 Use *OL = OperandList;
559 for (unsigned i = 0, e = NumArgs; i != e; i++) {
560 assert((i >= FTy->getNumParams() ||
561 FTy->getParamType(i) == Args[i]->getType()) &&
562 "Invoking a function with a bad signature!");
568 InvokeInst::InvokeInst(const InvokeInst &II)
569 : TerminatorInst(II.getType(), Instruction::Invoke,
570 OperandTraits<InvokeInst>::op_end(this)
571 - II.getNumOperands(),
572 II.getNumOperands()) {
573 setAttributes(II.getAttributes());
574 setCallingConv(II.getCallingConv());
575 Use *OL = OperandList, *InOL = II.OperandList;
576 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
578 SubclassOptionalData = II.SubclassOptionalData;
581 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
582 return getSuccessor(idx);
584 unsigned InvokeInst::getNumSuccessorsV() const {
585 return getNumSuccessors();
587 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
588 return setSuccessor(idx, B);
591 bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
592 if (AttributeList.paramHasAttr(i, attr))
594 if (const Function *F = getCalledFunction())
595 return F->paramHasAttr(i, attr);
599 void InvokeInst::addAttribute(unsigned i, Attributes attr) {
600 AttrListPtr PAL = getAttributes();
601 PAL = PAL.addAttr(i, attr);
605 void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
606 AttrListPtr PAL = getAttributes();
607 PAL = PAL.removeAttr(i, attr);
612 //===----------------------------------------------------------------------===//
613 // ReturnInst Implementation
614 //===----------------------------------------------------------------------===//
616 ReturnInst::ReturnInst(const ReturnInst &RI)
617 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
618 OperandTraits<ReturnInst>::op_end(this) -
620 RI.getNumOperands()) {
621 if (RI.getNumOperands())
622 Op<0>() = RI.Op<0>();
623 SubclassOptionalData = RI.SubclassOptionalData;
626 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
627 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
628 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
633 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
634 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
635 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
640 ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
641 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
642 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
645 unsigned ReturnInst::getNumSuccessorsV() const {
646 return getNumSuccessors();
649 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
650 /// emit the vtable for the class in this translation unit.
651 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
652 llvm_unreachable("ReturnInst has no successors!");
655 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
656 llvm_unreachable("ReturnInst has no successors!");
660 ReturnInst::~ReturnInst() {
663 //===----------------------------------------------------------------------===//
664 // UnwindInst Implementation
665 //===----------------------------------------------------------------------===//
667 UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
668 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
669 0, 0, InsertBefore) {
671 UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
672 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
677 unsigned UnwindInst::getNumSuccessorsV() const {
678 return getNumSuccessors();
681 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
682 llvm_unreachable("UnwindInst has no successors!");
685 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
686 llvm_unreachable("UnwindInst has no successors!");
690 //===----------------------------------------------------------------------===//
691 // UnreachableInst Implementation
692 //===----------------------------------------------------------------------===//
694 UnreachableInst::UnreachableInst(LLVMContext &Context,
695 Instruction *InsertBefore)
696 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
697 0, 0, InsertBefore) {
699 UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
700 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
704 unsigned UnreachableInst::getNumSuccessorsV() const {
705 return getNumSuccessors();
708 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
709 llvm_unreachable("UnwindInst has no successors!");
712 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
713 llvm_unreachable("UnwindInst has no successors!");
717 //===----------------------------------------------------------------------===//
718 // BranchInst Implementation
719 //===----------------------------------------------------------------------===//
721 void BranchInst::AssertOK() {
723 assert(getCondition()->getType()->isIntegerTy(1) &&
724 "May only branch on boolean predicates!");
727 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
728 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
729 OperandTraits<BranchInst>::op_end(this) - 1,
731 assert(IfTrue != 0 && "Branch destination may not be null!");
734 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
735 Instruction *InsertBefore)
736 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
737 OperandTraits<BranchInst>::op_end(this) - 3,
747 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
748 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
749 OperandTraits<BranchInst>::op_end(this) - 1,
751 assert(IfTrue != 0 && "Branch destination may not be null!");
755 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
756 BasicBlock *InsertAtEnd)
757 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
758 OperandTraits<BranchInst>::op_end(this) - 3,
769 BranchInst::BranchInst(const BranchInst &BI) :
770 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
771 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
772 BI.getNumOperands()) {
773 Op<-1>() = BI.Op<-1>();
774 if (BI.getNumOperands() != 1) {
775 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
776 Op<-3>() = BI.Op<-3>();
777 Op<-2>() = BI.Op<-2>();
779 SubclassOptionalData = BI.SubclassOptionalData;
783 Use* Use::getPrefix() {
784 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
785 if (PotentialPrefix.getOpaqueValue())
788 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
791 BranchInst::~BranchInst() {
792 if (NumOperands == 1) {
793 if (Use *Prefix = OperandList->getPrefix()) {
796 // mark OperandList to have a special value for scrutiny
797 // by baseclass destructors and operator delete
798 OperandList = Prefix;
801 OperandList = op_begin();
807 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
808 return getSuccessor(idx);
810 unsigned BranchInst::getNumSuccessorsV() const {
811 return getNumSuccessors();
813 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
814 setSuccessor(idx, B);
818 //===----------------------------------------------------------------------===//
819 // AllocaInst Implementation
820 //===----------------------------------------------------------------------===//
822 static Value *getAISize(LLVMContext &Context, Value *Amt) {
824 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
826 assert(!isa<BasicBlock>(Amt) &&
827 "Passed basic block into allocation size parameter! Use other ctor");
828 assert(Amt->getType()->isIntegerTy() &&
829 "Allocation array size is not an integer!");
834 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
835 const Twine &Name, Instruction *InsertBefore)
836 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
837 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
839 assert(!Ty->isVoidTy() && "Cannot allocate void!");
843 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
844 const Twine &Name, BasicBlock *InsertAtEnd)
845 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
846 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
848 assert(!Ty->isVoidTy() && "Cannot allocate void!");
852 AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
853 Instruction *InsertBefore)
854 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
855 getAISize(Ty->getContext(), 0), InsertBefore) {
857 assert(!Ty->isVoidTy() && "Cannot allocate void!");
861 AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
862 BasicBlock *InsertAtEnd)
863 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
864 getAISize(Ty->getContext(), 0), InsertAtEnd) {
866 assert(!Ty->isVoidTy() && "Cannot allocate void!");
870 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
871 const Twine &Name, Instruction *InsertBefore)
872 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
873 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
875 assert(!Ty->isVoidTy() && "Cannot allocate void!");
879 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
880 const Twine &Name, BasicBlock *InsertAtEnd)
881 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
882 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
884 assert(!Ty->isVoidTy() && "Cannot allocate void!");
888 // Out of line virtual method, so the vtable, etc has a home.
889 AllocaInst::~AllocaInst() {
892 void AllocaInst::setAlignment(unsigned Align) {
893 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
894 assert(Align <= MaximumAlignment &&
895 "Alignment is greater than MaximumAlignment!");
896 setInstructionSubclassData(Log2_32(Align) + 1);
897 assert(getAlignment() == Align && "Alignment representation error!");
900 bool AllocaInst::isArrayAllocation() const {
901 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
902 return CI->getZExtValue() != 1;
906 const Type *AllocaInst::getAllocatedType() const {
907 return getType()->getElementType();
910 /// isStaticAlloca - Return true if this alloca is in the entry block of the
911 /// function and is a constant size. If so, the code generator will fold it
912 /// into the prolog/epilog code, so it is basically free.
913 bool AllocaInst::isStaticAlloca() const {
914 // Must be constant size.
915 if (!isa<ConstantInt>(getArraySize())) return false;
917 // Must be in the entry block.
918 const BasicBlock *Parent = getParent();
919 return Parent == &Parent->getParent()->front();
922 //===----------------------------------------------------------------------===//
923 // LoadInst Implementation
924 //===----------------------------------------------------------------------===//
926 void LoadInst::AssertOK() {
927 assert(getOperand(0)->getType()->isPointerTy() &&
928 "Ptr must have pointer type.");
931 LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
932 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
933 Load, Ptr, InsertBef) {
940 LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
941 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
942 Load, Ptr, InsertAE) {
949 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
950 Instruction *InsertBef)
951 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
952 Load, Ptr, InsertBef) {
953 setVolatile(isVolatile);
959 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
960 unsigned Align, Instruction *InsertBef)
961 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
962 Load, Ptr, InsertBef) {
963 setVolatile(isVolatile);
969 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
970 unsigned Align, BasicBlock *InsertAE)
971 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
972 Load, Ptr, InsertAE) {
973 setVolatile(isVolatile);
979 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
980 BasicBlock *InsertAE)
981 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
982 Load, Ptr, InsertAE) {
983 setVolatile(isVolatile);
991 LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
992 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
993 Load, Ptr, InsertBef) {
997 if (Name && Name[0]) setName(Name);
1000 LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1001 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1002 Load, Ptr, InsertAE) {
1006 if (Name && Name[0]) setName(Name);
1009 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1010 Instruction *InsertBef)
1011 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1012 Load, Ptr, InsertBef) {
1013 setVolatile(isVolatile);
1016 if (Name && Name[0]) setName(Name);
1019 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1020 BasicBlock *InsertAE)
1021 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1022 Load, Ptr, InsertAE) {
1023 setVolatile(isVolatile);
1026 if (Name && Name[0]) setName(Name);
1029 void LoadInst::setAlignment(unsigned Align) {
1030 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1031 assert(Align <= MaximumAlignment &&
1032 "Alignment is greater than MaximumAlignment!");
1033 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1034 ((Log2_32(Align)+1)<<1));
1035 assert(getAlignment() == Align && "Alignment representation error!");
1038 //===----------------------------------------------------------------------===//
1039 // StoreInst Implementation
1040 //===----------------------------------------------------------------------===//
1042 void StoreInst::AssertOK() {
1043 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
1044 assert(getOperand(1)->getType()->isPointerTy() &&
1045 "Ptr must have pointer type!");
1046 assert(getOperand(0)->getType() ==
1047 cast<PointerType>(getOperand(1)->getType())->getElementType()
1048 && "Ptr must be a pointer to Val type!");
1052 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
1053 : Instruction(Type::getVoidTy(val->getContext()), Store,
1054 OperandTraits<StoreInst>::op_begin(this),
1055 OperandTraits<StoreInst>::operands(this),
1064 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
1065 : Instruction(Type::getVoidTy(val->getContext()), Store,
1066 OperandTraits<StoreInst>::op_begin(this),
1067 OperandTraits<StoreInst>::operands(this),
1076 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1077 Instruction *InsertBefore)
1078 : Instruction(Type::getVoidTy(val->getContext()), Store,
1079 OperandTraits<StoreInst>::op_begin(this),
1080 OperandTraits<StoreInst>::operands(this),
1084 setVolatile(isVolatile);
1089 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1090 unsigned Align, Instruction *InsertBefore)
1091 : Instruction(Type::getVoidTy(val->getContext()), Store,
1092 OperandTraits<StoreInst>::op_begin(this),
1093 OperandTraits<StoreInst>::operands(this),
1097 setVolatile(isVolatile);
1098 setAlignment(Align);
1102 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1103 unsigned Align, BasicBlock *InsertAtEnd)
1104 : Instruction(Type::getVoidTy(val->getContext()), Store,
1105 OperandTraits<StoreInst>::op_begin(this),
1106 OperandTraits<StoreInst>::operands(this),
1110 setVolatile(isVolatile);
1111 setAlignment(Align);
1115 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1116 BasicBlock *InsertAtEnd)
1117 : Instruction(Type::getVoidTy(val->getContext()), Store,
1118 OperandTraits<StoreInst>::op_begin(this),
1119 OperandTraits<StoreInst>::operands(this),
1123 setVolatile(isVolatile);
1128 void StoreInst::setAlignment(unsigned Align) {
1129 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1130 assert(Align <= MaximumAlignment &&
1131 "Alignment is greater than MaximumAlignment!");
1132 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1133 ((Log2_32(Align)+1) << 1));
1134 assert(getAlignment() == Align && "Alignment representation error!");
1137 //===----------------------------------------------------------------------===//
1138 // GetElementPtrInst Implementation
1139 //===----------------------------------------------------------------------===//
1141 static unsigned retrieveAddrSpace(const Value *Val) {
1142 return cast<PointerType>(Val->getType())->getAddressSpace();
1145 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
1146 const Twine &Name) {
1147 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1148 Use *OL = OperandList;
1151 for (unsigned i = 0; i != NumIdx; ++i)
1157 void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
1158 assert(NumOperands == 2 && "NumOperands not initialized?");
1159 Use *OL = OperandList;
1166 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1167 : Instruction(GEPI.getType(), GetElementPtr,
1168 OperandTraits<GetElementPtrInst>::op_end(this)
1169 - GEPI.getNumOperands(),
1170 GEPI.getNumOperands()) {
1171 Use *OL = OperandList;
1172 Use *GEPIOL = GEPI.OperandList;
1173 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1175 SubclassOptionalData = GEPI.SubclassOptionalData;
1178 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1179 const Twine &Name, Instruction *InBe)
1180 : Instruction(PointerType::get(
1181 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
1183 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1185 init(Ptr, Idx, Name);
1188 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1189 const Twine &Name, BasicBlock *IAE)
1190 : Instruction(PointerType::get(
1191 checkType(getIndexedType(Ptr->getType(),Idx)),
1192 retrieveAddrSpace(Ptr)),
1194 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1196 init(Ptr, Idx, Name);
1199 /// getIndexedType - Returns the type of the element that would be accessed with
1200 /// a gep instruction with the specified parameters.
1202 /// The Idxs pointer should point to a continuous piece of memory containing the
1203 /// indices, either as Value* or uint64_t.
1205 /// A null type is returned if the indices are invalid for the specified
1208 template <typename IndexTy>
1209 static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1211 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1212 if (!PTy) return 0; // Type isn't a pointer type!
1213 const Type *Agg = PTy->getElementType();
1215 // Handle the special case of the empty set index set, which is always valid.
1219 // If there is at least one index, the top level type must be sized, otherwise
1220 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1221 // that contain opaque types) under the assumption that it will be resolved to
1222 // a sane type later.
1223 if (!Agg->isSized() && !Agg->isAbstract())
1226 unsigned CurIdx = 1;
1227 for (; CurIdx != NumIdx; ++CurIdx) {
1228 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1229 if (!CT || CT->isPointerTy()) return 0;
1230 IndexTy Index = Idxs[CurIdx];
1231 if (!CT->indexValid(Index)) return 0;
1232 Agg = CT->getTypeAtIndex(Index);
1234 // If the new type forwards to another type, then it is in the middle
1235 // of being refined to another type (and hence, may have dropped all
1236 // references to what it was using before). So, use the new forwarded
1238 if (const Type *Ty = Agg->getForwardedType())
1241 return CurIdx == NumIdx ? Agg : 0;
1244 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1247 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1250 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1251 uint64_t const *Idxs,
1253 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1256 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1257 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1258 if (!PTy) return 0; // Type isn't a pointer type!
1260 // Check the pointer index.
1261 if (!PTy->indexValid(Idx)) return 0;
1263 return PTy->getElementType();
1267 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1268 /// zeros. If so, the result pointer and the first operand have the same
1269 /// value, just potentially different types.
1270 bool GetElementPtrInst::hasAllZeroIndices() const {
1271 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1272 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1273 if (!CI->isZero()) return false;
1281 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1282 /// constant integers. If so, the result pointer and the first operand have
1283 /// a constant offset between them.
1284 bool GetElementPtrInst::hasAllConstantIndices() const {
1285 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1286 if (!isa<ConstantInt>(getOperand(i)))
1292 void GetElementPtrInst::setIsInBounds(bool B) {
1293 cast<GEPOperator>(this)->setIsInBounds(B);
1296 bool GetElementPtrInst::isInBounds() const {
1297 return cast<GEPOperator>(this)->isInBounds();
1300 //===----------------------------------------------------------------------===//
1301 // ExtractElementInst Implementation
1302 //===----------------------------------------------------------------------===//
1304 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1306 Instruction *InsertBef)
1307 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1309 OperandTraits<ExtractElementInst>::op_begin(this),
1311 assert(isValidOperands(Val, Index) &&
1312 "Invalid extractelement instruction operands!");
1318 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1320 BasicBlock *InsertAE)
1321 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1323 OperandTraits<ExtractElementInst>::op_begin(this),
1325 assert(isValidOperands(Val, Index) &&
1326 "Invalid extractelement instruction operands!");
1334 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1335 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
1341 //===----------------------------------------------------------------------===//
1342 // InsertElementInst Implementation
1343 //===----------------------------------------------------------------------===//
1345 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1347 Instruction *InsertBef)
1348 : Instruction(Vec->getType(), InsertElement,
1349 OperandTraits<InsertElementInst>::op_begin(this),
1351 assert(isValidOperands(Vec, Elt, Index) &&
1352 "Invalid insertelement instruction operands!");
1359 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1361 BasicBlock *InsertAE)
1362 : Instruction(Vec->getType(), InsertElement,
1363 OperandTraits<InsertElementInst>::op_begin(this),
1365 assert(isValidOperands(Vec, Elt, Index) &&
1366 "Invalid insertelement instruction operands!");
1374 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1375 const Value *Index) {
1376 if (!Vec->getType()->isVectorTy())
1377 return false; // First operand of insertelement must be vector type.
1379 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1380 return false;// Second operand of insertelement must be vector element type.
1382 if (!Index->getType()->isIntegerTy(32))
1383 return false; // Third operand of insertelement must be i32.
1388 //===----------------------------------------------------------------------===//
1389 // ShuffleVectorInst Implementation
1390 //===----------------------------------------------------------------------===//
1392 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1394 Instruction *InsertBefore)
1395 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1396 cast<VectorType>(Mask->getType())->getNumElements()),
1398 OperandTraits<ShuffleVectorInst>::op_begin(this),
1399 OperandTraits<ShuffleVectorInst>::operands(this),
1401 assert(isValidOperands(V1, V2, Mask) &&
1402 "Invalid shuffle vector instruction operands!");
1409 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1411 BasicBlock *InsertAtEnd)
1412 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1413 cast<VectorType>(Mask->getType())->getNumElements()),
1415 OperandTraits<ShuffleVectorInst>::op_begin(this),
1416 OperandTraits<ShuffleVectorInst>::operands(this),
1418 assert(isValidOperands(V1, V2, Mask) &&
1419 "Invalid shuffle vector instruction operands!");
1427 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1428 const Value *Mask) {
1429 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
1432 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1433 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
1436 // Check to see if Mask is valid.
1437 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1438 const VectorType *VTy = cast<VectorType>(V1->getType());
1439 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1440 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1441 if (CI->uge(VTy->getNumElements()*2))
1443 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1448 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1454 /// getMaskValue - Return the index from the shuffle mask for the specified
1455 /// output result. This is either -1 if the element is undef or a number less
1456 /// than 2*numelements.
1457 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1458 const Constant *Mask = cast<Constant>(getOperand(2));
1459 if (isa<UndefValue>(Mask)) return -1;
1460 if (isa<ConstantAggregateZero>(Mask)) return 0;
1461 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1462 assert(i < MaskCV->getNumOperands() && "Index out of range");
1464 if (isa<UndefValue>(MaskCV->getOperand(i)))
1466 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1469 //===----------------------------------------------------------------------===//
1470 // InsertValueInst Class
1471 //===----------------------------------------------------------------------===//
1473 void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1474 unsigned NumIdx, const Twine &Name) {
1475 assert(NumOperands == 2 && "NumOperands not initialized?");
1479 Indices.append(Idx, Idx + NumIdx);
1483 void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1484 const Twine &Name) {
1485 assert(NumOperands == 2 && "NumOperands not initialized?");
1489 Indices.push_back(Idx);
1493 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1494 : Instruction(IVI.getType(), InsertValue,
1495 OperandTraits<InsertValueInst>::op_begin(this), 2),
1496 Indices(IVI.Indices) {
1497 Op<0>() = IVI.getOperand(0);
1498 Op<1>() = IVI.getOperand(1);
1499 SubclassOptionalData = IVI.SubclassOptionalData;
1502 InsertValueInst::InsertValueInst(Value *Agg,
1506 Instruction *InsertBefore)
1507 : Instruction(Agg->getType(), InsertValue,
1508 OperandTraits<InsertValueInst>::op_begin(this),
1510 init(Agg, Val, Idx, Name);
1513 InsertValueInst::InsertValueInst(Value *Agg,
1517 BasicBlock *InsertAtEnd)
1518 : Instruction(Agg->getType(), InsertValue,
1519 OperandTraits<InsertValueInst>::op_begin(this),
1521 init(Agg, Val, Idx, Name);
1524 //===----------------------------------------------------------------------===//
1525 // ExtractValueInst Class
1526 //===----------------------------------------------------------------------===//
1528 void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
1529 const Twine &Name) {
1530 assert(NumOperands == 1 && "NumOperands not initialized?");
1532 Indices.append(Idx, Idx + NumIdx);
1536 void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
1537 assert(NumOperands == 1 && "NumOperands not initialized?");
1539 Indices.push_back(Idx);
1543 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1544 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
1545 Indices(EVI.Indices) {
1546 SubclassOptionalData = EVI.SubclassOptionalData;
1549 // getIndexedType - Returns the type of the element that would be extracted
1550 // with an extractvalue instruction with the specified parameters.
1552 // A null type is returned if the indices are invalid for the specified
1555 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1556 const unsigned *Idxs,
1558 unsigned CurIdx = 0;
1559 for (; CurIdx != NumIdx; ++CurIdx) {
1560 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1561 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
1562 unsigned Index = Idxs[CurIdx];
1563 if (!CT->indexValid(Index)) return 0;
1564 Agg = CT->getTypeAtIndex(Index);
1566 // If the new type forwards to another type, then it is in the middle
1567 // of being refined to another type (and hence, may have dropped all
1568 // references to what it was using before). So, use the new forwarded
1570 if (const Type *Ty = Agg->getForwardedType())
1573 return CurIdx == NumIdx ? Agg : 0;
1576 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1578 return getIndexedType(Agg, &Idx, 1);
1581 //===----------------------------------------------------------------------===//
1582 // BinaryOperator Class
1583 //===----------------------------------------------------------------------===//
1585 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1586 const Type *Ty, const Twine &Name,
1587 Instruction *InsertBefore)
1588 : Instruction(Ty, iType,
1589 OperandTraits<BinaryOperator>::op_begin(this),
1590 OperandTraits<BinaryOperator>::operands(this),
1598 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1599 const Type *Ty, const Twine &Name,
1600 BasicBlock *InsertAtEnd)
1601 : Instruction(Ty, iType,
1602 OperandTraits<BinaryOperator>::op_begin(this),
1603 OperandTraits<BinaryOperator>::operands(this),
1612 void BinaryOperator::init(BinaryOps iType) {
1613 Value *LHS = getOperand(0), *RHS = getOperand(1);
1614 LHS = LHS; RHS = RHS; // Silence warnings.
1615 assert(LHS->getType() == RHS->getType() &&
1616 "Binary operator operand types must match!");
1621 assert(getType() == LHS->getType() &&
1622 "Arithmetic operation should return same type as operands!");
1623 assert(getType()->isIntOrIntVectorTy() &&
1624 "Tried to create an integer operation on a non-integer type!");
1626 case FAdd: case FSub:
1628 assert(getType() == LHS->getType() &&
1629 "Arithmetic operation should return same type as operands!");
1630 assert(getType()->isFPOrFPVectorTy() &&
1631 "Tried to create a floating-point operation on a "
1632 "non-floating-point type!");
1636 assert(getType() == LHS->getType() &&
1637 "Arithmetic operation should return same type as operands!");
1638 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1639 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1640 "Incorrect operand type (not integer) for S/UDIV");
1643 assert(getType() == LHS->getType() &&
1644 "Arithmetic operation should return same type as operands!");
1645 assert(getType()->isFPOrFPVectorTy() &&
1646 "Incorrect operand type (not floating point) for FDIV");
1650 assert(getType() == LHS->getType() &&
1651 "Arithmetic operation should return same type as operands!");
1652 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1653 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1654 "Incorrect operand type (not integer) for S/UREM");
1657 assert(getType() == LHS->getType() &&
1658 "Arithmetic operation should return same type as operands!");
1659 assert(getType()->isFPOrFPVectorTy() &&
1660 "Incorrect operand type (not floating point) for FREM");
1665 assert(getType() == LHS->getType() &&
1666 "Shift operation should return same type as operands!");
1667 assert((getType()->isIntegerTy() ||
1668 (getType()->isVectorTy() &&
1669 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1670 "Tried to create a shift operation on a non-integral type!");
1674 assert(getType() == LHS->getType() &&
1675 "Logical operation should return same type as operands!");
1676 assert((getType()->isIntegerTy() ||
1677 (getType()->isVectorTy() &&
1678 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1679 "Tried to create a logical operation on a non-integral type!");
1687 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1689 Instruction *InsertBefore) {
1690 assert(S1->getType() == S2->getType() &&
1691 "Cannot create binary operator with two operands of differing type!");
1692 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1695 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1697 BasicBlock *InsertAtEnd) {
1698 BinaryOperator *Res = Create(Op, S1, S2, Name);
1699 InsertAtEnd->getInstList().push_back(Res);
1703 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1704 Instruction *InsertBefore) {
1705 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1706 return new BinaryOperator(Instruction::Sub,
1708 Op->getType(), Name, InsertBefore);
1711 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1712 BasicBlock *InsertAtEnd) {
1713 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1714 return new BinaryOperator(Instruction::Sub,
1716 Op->getType(), Name, InsertAtEnd);
1719 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1720 Instruction *InsertBefore) {
1721 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1722 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1725 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1726 BasicBlock *InsertAtEnd) {
1727 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1728 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1731 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1732 Instruction *InsertBefore) {
1733 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1734 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1737 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1738 BasicBlock *InsertAtEnd) {
1739 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1740 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1743 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1744 Instruction *InsertBefore) {
1745 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1746 return new BinaryOperator(Instruction::FSub,
1748 Op->getType(), Name, InsertBefore);
1751 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1752 BasicBlock *InsertAtEnd) {
1753 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1754 return new BinaryOperator(Instruction::FSub,
1756 Op->getType(), Name, InsertAtEnd);
1759 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1760 Instruction *InsertBefore) {
1762 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1763 C = Constant::getAllOnesValue(PTy->getElementType());
1764 C = ConstantVector::get(
1765 std::vector<Constant*>(PTy->getNumElements(), C));
1767 C = Constant::getAllOnesValue(Op->getType());
1770 return new BinaryOperator(Instruction::Xor, Op, C,
1771 Op->getType(), Name, InsertBefore);
1774 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1775 BasicBlock *InsertAtEnd) {
1777 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1778 // Create a vector of all ones values.
1779 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
1780 AllOnes = ConstantVector::get(
1781 std::vector<Constant*>(PTy->getNumElements(), Elt));
1783 AllOnes = Constant::getAllOnesValue(Op->getType());
1786 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1787 Op->getType(), Name, InsertAtEnd);
1791 // isConstantAllOnes - Helper function for several functions below
1792 static inline bool isConstantAllOnes(const Value *V) {
1793 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1794 return CI->isAllOnesValue();
1795 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1796 return CV->isAllOnesValue();
1800 bool BinaryOperator::isNeg(const Value *V) {
1801 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1802 if (Bop->getOpcode() == Instruction::Sub)
1803 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1804 return C->isNegativeZeroValue();
1808 bool BinaryOperator::isFNeg(const Value *V) {
1809 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1810 if (Bop->getOpcode() == Instruction::FSub)
1811 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1812 return C->isNegativeZeroValue();
1816 bool BinaryOperator::isNot(const Value *V) {
1817 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1818 return (Bop->getOpcode() == Instruction::Xor &&
1819 (isConstantAllOnes(Bop->getOperand(1)) ||
1820 isConstantAllOnes(Bop->getOperand(0))));
1824 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1825 return cast<BinaryOperator>(BinOp)->getOperand(1);
1828 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1829 return getNegArgument(const_cast<Value*>(BinOp));
1832 Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1833 return cast<BinaryOperator>(BinOp)->getOperand(1);
1836 const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1837 return getFNegArgument(const_cast<Value*>(BinOp));
1840 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1841 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1842 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1843 Value *Op0 = BO->getOperand(0);
1844 Value *Op1 = BO->getOperand(1);
1845 if (isConstantAllOnes(Op0)) return Op1;
1847 assert(isConstantAllOnes(Op1));
1851 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1852 return getNotArgument(const_cast<Value*>(BinOp));
1856 // swapOperands - Exchange the two operands to this instruction. This
1857 // instruction is safe to use on any binary instruction and does not
1858 // modify the semantics of the instruction. If the instruction is
1859 // order dependent (SetLT f.e.) the opcode is changed.
1861 bool BinaryOperator::swapOperands() {
1862 if (!isCommutative())
1863 return true; // Can't commute operands
1864 Op<0>().swap(Op<1>());
1868 void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1869 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1872 void BinaryOperator::setHasNoSignedWrap(bool b) {
1873 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1876 void BinaryOperator::setIsExact(bool b) {
1877 cast<SDivOperator>(this)->setIsExact(b);
1880 bool BinaryOperator::hasNoUnsignedWrap() const {
1881 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1884 bool BinaryOperator::hasNoSignedWrap() const {
1885 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1888 bool BinaryOperator::isExact() const {
1889 return cast<SDivOperator>(this)->isExact();
1892 //===----------------------------------------------------------------------===//
1894 //===----------------------------------------------------------------------===//
1896 // Just determine if this cast only deals with integral->integral conversion.
1897 bool CastInst::isIntegerCast() const {
1898 switch (getOpcode()) {
1899 default: return false;
1900 case Instruction::ZExt:
1901 case Instruction::SExt:
1902 case Instruction::Trunc:
1904 case Instruction::BitCast:
1905 return getOperand(0)->getType()->isIntegerTy() &&
1906 getType()->isIntegerTy();
1910 bool CastInst::isLosslessCast() const {
1911 // Only BitCast can be lossless, exit fast if we're not BitCast
1912 if (getOpcode() != Instruction::BitCast)
1915 // Identity cast is always lossless
1916 const Type* SrcTy = getOperand(0)->getType();
1917 const Type* DstTy = getType();
1921 // Pointer to pointer is always lossless.
1922 if (SrcTy->isPointerTy())
1923 return DstTy->isPointerTy();
1924 return false; // Other types have no identity values
1927 /// This function determines if the CastInst does not require any bits to be
1928 /// changed in order to effect the cast. Essentially, it identifies cases where
1929 /// no code gen is necessary for the cast, hence the name no-op cast. For
1930 /// example, the following are all no-op casts:
1931 /// # bitcast i32* %x to i8*
1932 /// # bitcast <2 x i32> %x to <4 x i16>
1933 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1934 /// @brief Determine if the described cast is a no-op.
1935 bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1938 const Type *IntPtrTy) {
1941 assert(!"Invalid CastOp");
1942 case Instruction::Trunc:
1943 case Instruction::ZExt:
1944 case Instruction::SExt:
1945 case Instruction::FPTrunc:
1946 case Instruction::FPExt:
1947 case Instruction::UIToFP:
1948 case Instruction::SIToFP:
1949 case Instruction::FPToUI:
1950 case Instruction::FPToSI:
1951 return false; // These always modify bits
1952 case Instruction::BitCast:
1953 return true; // BitCast never modifies bits.
1954 case Instruction::PtrToInt:
1955 return IntPtrTy->getScalarSizeInBits() ==
1956 DestTy->getScalarSizeInBits();
1957 case Instruction::IntToPtr:
1958 return IntPtrTy->getScalarSizeInBits() ==
1959 SrcTy->getScalarSizeInBits();
1963 /// @brief Determine if a cast is a no-op.
1964 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1965 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1968 /// This function determines if a pair of casts can be eliminated and what
1969 /// opcode should be used in the elimination. This assumes that there are two
1970 /// instructions like this:
1971 /// * %F = firstOpcode SrcTy %x to MidTy
1972 /// * %S = secondOpcode MidTy %F to DstTy
1973 /// The function returns a resultOpcode so these two casts can be replaced with:
1974 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1975 /// If no such cast is permited, the function returns 0.
1976 unsigned CastInst::isEliminableCastPair(
1977 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1978 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1980 // Define the 144 possibilities for these two cast instructions. The values
1981 // in this matrix determine what to do in a given situation and select the
1982 // case in the switch below. The rows correspond to firstOp, the columns
1983 // correspond to secondOp. In looking at the table below, keep in mind
1984 // the following cast properties:
1986 // Size Compare Source Destination
1987 // Operator Src ? Size Type Sign Type Sign
1988 // -------- ------------ ------------------- ---------------------
1989 // TRUNC > Integer Any Integral Any
1990 // ZEXT < Integral Unsigned Integer Any
1991 // SEXT < Integral Signed Integer Any
1992 // FPTOUI n/a FloatPt n/a Integral Unsigned
1993 // FPTOSI n/a FloatPt n/a Integral Signed
1994 // UITOFP n/a Integral Unsigned FloatPt n/a
1995 // SITOFP n/a Integral Signed FloatPt n/a
1996 // FPTRUNC > FloatPt n/a FloatPt n/a
1997 // FPEXT < FloatPt n/a FloatPt n/a
1998 // PTRTOINT n/a Pointer n/a Integral Unsigned
1999 // INTTOPTR n/a Integral Unsigned Pointer n/a
2000 // BITCAST = FirstClass n/a FirstClass n/a
2002 // NOTE: some transforms are safe, but we consider them to be non-profitable.
2003 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2004 // into "fptoui double to i64", but this loses information about the range
2005 // of the produced value (we no longer know the top-part is all zeros).
2006 // Further this conversion is often much more expensive for typical hardware,
2007 // and causes issues when building libgcc. We disallow fptosi+sext for the
2009 const unsigned numCastOps =
2010 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2011 static const uint8_t CastResults[numCastOps][numCastOps] = {
2012 // T F F U S F F P I B -+
2013 // R Z S P P I I T P 2 N T |
2014 // U E E 2 2 2 2 R E I T C +- secondOp
2015 // N X X U S F F N X N 2 V |
2016 // C T T I I P P C T T P T -+
2017 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2018 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2019 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
2020 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2021 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
2022 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2023 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2024 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2025 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2026 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2027 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2028 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2031 // If either of the casts are a bitcast from scalar to vector, disallow the
2033 if ((firstOp == Instruction::BitCast &&
2034 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2035 (secondOp == Instruction::BitCast &&
2036 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2037 return 0; // Disallowed
2039 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2040 [secondOp-Instruction::CastOpsBegin];
2043 // categorically disallowed
2046 // allowed, use first cast's opcode
2049 // allowed, use second cast's opcode
2052 // no-op cast in second op implies firstOp as long as the DestTy
2053 // is integer and we are not converting between a vector and a
2055 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
2059 // no-op cast in second op implies firstOp as long as the DestTy
2060 // is floating point.
2061 if (DstTy->isFloatingPointTy())
2065 // no-op cast in first op implies secondOp as long as the SrcTy
2067 if (SrcTy->isIntegerTy())
2071 // no-op cast in first op implies secondOp as long as the SrcTy
2072 // is a floating point.
2073 if (SrcTy->isFloatingPointTy())
2077 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
2080 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2081 unsigned MidSize = MidTy->getScalarSizeInBits();
2082 if (MidSize >= PtrSize)
2083 return Instruction::BitCast;
2087 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2088 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2089 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2090 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2091 unsigned DstSize = DstTy->getScalarSizeInBits();
2092 if (SrcSize == DstSize)
2093 return Instruction::BitCast;
2094 else if (SrcSize < DstSize)
2098 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2099 return Instruction::ZExt;
2101 // fpext followed by ftrunc is allowed if the bit size returned to is
2102 // the same as the original, in which case its just a bitcast
2104 return Instruction::BitCast;
2105 return 0; // If the types are not the same we can't eliminate it.
2107 // bitcast followed by ptrtoint is allowed as long as the bitcast
2108 // is a pointer to pointer cast.
2109 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
2113 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2114 if (MidTy->isPointerTy() && DstTy->isPointerTy())
2118 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2121 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2122 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2123 unsigned DstSize = DstTy->getScalarSizeInBits();
2124 if (SrcSize <= PtrSize && SrcSize == DstSize)
2125 return Instruction::BitCast;
2129 // cast combination can't happen (error in input). This is for all cases
2130 // where the MidTy is not the same for the two cast instructions.
2131 assert(!"Invalid Cast Combination");
2134 assert(!"Error in CastResults table!!!");
2140 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2141 const Twine &Name, Instruction *InsertBefore) {
2142 // Construct and return the appropriate CastInst subclass
2144 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2145 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2146 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2147 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2148 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2149 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2150 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2151 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2152 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2153 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2154 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2155 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2157 assert(!"Invalid opcode provided");
2162 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2163 const Twine &Name, BasicBlock *InsertAtEnd) {
2164 // Construct and return the appropriate CastInst subclass
2166 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2167 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2168 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2169 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2170 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2171 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2172 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2173 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2174 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2175 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2176 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2177 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2179 assert(!"Invalid opcode provided");
2184 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2186 Instruction *InsertBefore) {
2187 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2188 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2189 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
2192 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2194 BasicBlock *InsertAtEnd) {
2195 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2196 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2197 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
2200 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2202 Instruction *InsertBefore) {
2203 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2204 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2205 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
2208 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2210 BasicBlock *InsertAtEnd) {
2211 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2212 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2213 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
2216 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2218 Instruction *InsertBefore) {
2219 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2220 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2221 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
2224 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2226 BasicBlock *InsertAtEnd) {
2227 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2228 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2229 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
2232 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2234 BasicBlock *InsertAtEnd) {
2235 assert(S->getType()->isPointerTy() && "Invalid cast");
2236 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2239 if (Ty->isIntegerTy())
2240 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2241 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2244 /// @brief Create a BitCast or a PtrToInt cast instruction
2245 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2247 Instruction *InsertBefore) {
2248 assert(S->getType()->isPointerTy() && "Invalid cast");
2249 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2252 if (Ty->isIntegerTy())
2253 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2254 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2257 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2258 bool isSigned, const Twine &Name,
2259 Instruction *InsertBefore) {
2260 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2261 "Invalid integer cast");
2262 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2263 unsigned DstBits = Ty->getScalarSizeInBits();
2264 Instruction::CastOps opcode =
2265 (SrcBits == DstBits ? Instruction::BitCast :
2266 (SrcBits > DstBits ? Instruction::Trunc :
2267 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2268 return Create(opcode, C, Ty, Name, InsertBefore);
2271 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2272 bool isSigned, const Twine &Name,
2273 BasicBlock *InsertAtEnd) {
2274 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2276 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2277 unsigned DstBits = Ty->getScalarSizeInBits();
2278 Instruction::CastOps opcode =
2279 (SrcBits == DstBits ? Instruction::BitCast :
2280 (SrcBits > DstBits ? Instruction::Trunc :
2281 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2282 return Create(opcode, C, Ty, Name, InsertAtEnd);
2285 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2287 Instruction *InsertBefore) {
2288 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2290 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2291 unsigned DstBits = Ty->getScalarSizeInBits();
2292 Instruction::CastOps opcode =
2293 (SrcBits == DstBits ? Instruction::BitCast :
2294 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2295 return Create(opcode, C, Ty, Name, InsertBefore);
2298 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2300 BasicBlock *InsertAtEnd) {
2301 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2303 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2304 unsigned DstBits = Ty->getScalarSizeInBits();
2305 Instruction::CastOps opcode =
2306 (SrcBits == DstBits ? Instruction::BitCast :
2307 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2308 return Create(opcode, C, Ty, Name, InsertAtEnd);
2311 // Check whether it is valid to call getCastOpcode for these types.
2312 // This routine must be kept in sync with getCastOpcode.
2313 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2314 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2317 if (SrcTy == DestTy)
2320 // Get the bit sizes, we'll need these
2321 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2322 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2324 // Run through the possibilities ...
2325 if (DestTy->isIntegerTy()) { // Casting to integral
2326 if (SrcTy->isIntegerTy()) { // Casting from integral
2328 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2330 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2331 // Casting from vector
2332 return DestBits == PTy->getBitWidth();
2333 } else { // Casting from something else
2334 return SrcTy->isPointerTy();
2336 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2337 if (SrcTy->isIntegerTy()) { // Casting from integral
2339 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2341 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2342 // Casting from vector
2343 return DestBits == PTy->getBitWidth();
2344 } else { // Casting from something else
2347 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2348 // Casting to vector
2349 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2350 // Casting from vector
2351 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2352 } else { // Casting from something else
2353 return DestPTy->getBitWidth() == SrcBits;
2355 } else if (DestTy->isPointerTy()) { // Casting to pointer
2356 if (SrcTy->isPointerTy()) { // Casting from pointer
2358 } else if (SrcTy->isIntegerTy()) { // Casting from integral
2360 } else { // Casting from something else
2363 } else { // Casting to something else
2368 // Provide a way to get a "cast" where the cast opcode is inferred from the
2369 // types and size of the operand. This, basically, is a parallel of the
2370 // logic in the castIsValid function below. This axiom should hold:
2371 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2372 // should not assert in castIsValid. In other words, this produces a "correct"
2373 // casting opcode for the arguments passed to it.
2374 // This routine must be kept in sync with isCastable.
2375 Instruction::CastOps
2376 CastInst::getCastOpcode(
2377 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
2378 // Get the bit sizes, we'll need these
2379 const Type *SrcTy = Src->getType();
2380 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2381 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2383 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2384 "Only first class types are castable!");
2386 // Run through the possibilities ...
2387 if (DestTy->isIntegerTy()) { // Casting to integral
2388 if (SrcTy->isIntegerTy()) { // Casting from integral
2389 if (DestBits < SrcBits)
2390 return Trunc; // int -> smaller int
2391 else if (DestBits > SrcBits) { // its an extension
2393 return SExt; // signed -> SEXT
2395 return ZExt; // unsigned -> ZEXT
2397 return BitCast; // Same size, No-op cast
2399 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2401 return FPToSI; // FP -> sint
2403 return FPToUI; // FP -> uint
2404 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2405 assert(DestBits == PTy->getBitWidth() &&
2406 "Casting vector to integer of different width");
2408 return BitCast; // Same size, no-op cast
2410 assert(SrcTy->isPointerTy() &&
2411 "Casting from a value that is not first-class type");
2412 return PtrToInt; // ptr -> int
2414 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2415 if (SrcTy->isIntegerTy()) { // Casting from integral
2417 return SIToFP; // sint -> FP
2419 return UIToFP; // uint -> FP
2420 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2421 if (DestBits < SrcBits) {
2422 return FPTrunc; // FP -> smaller FP
2423 } else if (DestBits > SrcBits) {
2424 return FPExt; // FP -> larger FP
2426 return BitCast; // same size, no-op cast
2428 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2429 assert(DestBits == PTy->getBitWidth() &&
2430 "Casting vector to floating point of different width");
2432 return BitCast; // same size, no-op cast
2434 llvm_unreachable("Casting pointer or non-first class to float");
2436 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2437 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2438 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
2439 "Casting vector to vector of different widths");
2441 return BitCast; // vector -> vector
2442 } else if (DestPTy->getBitWidth() == SrcBits) {
2443 return BitCast; // float/int -> vector
2445 assert(!"Illegal cast to vector (wrong type or size)");
2447 } else if (DestTy->isPointerTy()) {
2448 if (SrcTy->isPointerTy()) {
2449 return BitCast; // ptr -> ptr
2450 } else if (SrcTy->isIntegerTy()) {
2451 return IntToPtr; // int -> ptr
2453 assert(!"Casting pointer to other than pointer or int");
2456 assert(!"Casting to type that is not first-class");
2459 // If we fall through to here we probably hit an assertion cast above
2460 // and assertions are not turned on. Anything we return is an error, so
2461 // BitCast is as good a choice as any.
2465 //===----------------------------------------------------------------------===//
2466 // CastInst SubClass Constructors
2467 //===----------------------------------------------------------------------===//
2469 /// Check that the construction parameters for a CastInst are correct. This
2470 /// could be broken out into the separate constructors but it is useful to have
2471 /// it in one place and to eliminate the redundant code for getting the sizes
2472 /// of the types involved.
2474 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
2476 // Check for type sanity on the arguments
2477 const Type *SrcTy = S->getType();
2478 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2479 SrcTy->isAggregateType() || DstTy->isAggregateType())
2482 // Get the size of the types in bits, we'll need this later
2483 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2484 unsigned DstBitSize = DstTy->getScalarSizeInBits();
2486 // Switch on the opcode provided
2488 default: return false; // This is an input error
2489 case Instruction::Trunc:
2490 return SrcTy->isIntOrIntVectorTy() &&
2491 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
2492 case Instruction::ZExt:
2493 return SrcTy->isIntOrIntVectorTy() &&
2494 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2495 case Instruction::SExt:
2496 return SrcTy->isIntOrIntVectorTy() &&
2497 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2498 case Instruction::FPTrunc:
2499 return SrcTy->isFPOrFPVectorTy() &&
2500 DstTy->isFPOrFPVectorTy() &&
2501 SrcBitSize > DstBitSize;
2502 case Instruction::FPExt:
2503 return SrcTy->isFPOrFPVectorTy() &&
2504 DstTy->isFPOrFPVectorTy() &&
2505 SrcBitSize < DstBitSize;
2506 case Instruction::UIToFP:
2507 case Instruction::SIToFP:
2508 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2509 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2510 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2511 DVTy->getElementType()->isFPOrFPVectorTy() &&
2512 SVTy->getNumElements() == DVTy->getNumElements();
2515 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
2516 case Instruction::FPToUI:
2517 case Instruction::FPToSI:
2518 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2519 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2520 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2521 DVTy->getElementType()->isIntOrIntVectorTy() &&
2522 SVTy->getNumElements() == DVTy->getNumElements();
2525 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
2526 case Instruction::PtrToInt:
2527 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
2528 case Instruction::IntToPtr:
2529 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
2530 case Instruction::BitCast:
2531 // BitCast implies a no-op cast of type only. No bits change.
2532 // However, you can't cast pointers to anything but pointers.
2533 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
2536 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2537 // these cases, the cast is okay if the source and destination bit widths
2539 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2543 TruncInst::TruncInst(
2544 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2545 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2546 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2549 TruncInst::TruncInst(
2550 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2551 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2552 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2556 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2557 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2558 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2562 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2563 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2564 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2567 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2568 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2569 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2573 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2574 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2575 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2578 FPTruncInst::FPTruncInst(
2579 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2580 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2584 FPTruncInst::FPTruncInst(
2585 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2586 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2590 FPExtInst::FPExtInst(
2591 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2592 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2596 FPExtInst::FPExtInst(
2597 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2598 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2602 UIToFPInst::UIToFPInst(
2603 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2604 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2608 UIToFPInst::UIToFPInst(
2609 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2610 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2614 SIToFPInst::SIToFPInst(
2615 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2616 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2617 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2620 SIToFPInst::SIToFPInst(
2621 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2622 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2623 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2626 FPToUIInst::FPToUIInst(
2627 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2628 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2629 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2632 FPToUIInst::FPToUIInst(
2633 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2634 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2635 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2638 FPToSIInst::FPToSIInst(
2639 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2640 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2641 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2644 FPToSIInst::FPToSIInst(
2645 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2646 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2647 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2650 PtrToIntInst::PtrToIntInst(
2651 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2652 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2653 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2656 PtrToIntInst::PtrToIntInst(
2657 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2658 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2659 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2662 IntToPtrInst::IntToPtrInst(
2663 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2664 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2665 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2668 IntToPtrInst::IntToPtrInst(
2669 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2670 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2671 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2674 BitCastInst::BitCastInst(
2675 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2676 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2677 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2680 BitCastInst::BitCastInst(
2681 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2682 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2683 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2686 //===----------------------------------------------------------------------===//
2688 //===----------------------------------------------------------------------===//
2690 void CmpInst::Anchor() const {}
2692 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2693 Value *LHS, Value *RHS, const Twine &Name,
2694 Instruction *InsertBefore)
2695 : Instruction(ty, op,
2696 OperandTraits<CmpInst>::op_begin(this),
2697 OperandTraits<CmpInst>::operands(this),
2701 setPredicate((Predicate)predicate);
2705 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2706 Value *LHS, Value *RHS, const Twine &Name,
2707 BasicBlock *InsertAtEnd)
2708 : Instruction(ty, op,
2709 OperandTraits<CmpInst>::op_begin(this),
2710 OperandTraits<CmpInst>::operands(this),
2714 setPredicate((Predicate)predicate);
2719 CmpInst::Create(OtherOps Op, unsigned short predicate,
2720 Value *S1, Value *S2,
2721 const Twine &Name, Instruction *InsertBefore) {
2722 if (Op == Instruction::ICmp) {
2724 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2727 return new ICmpInst(CmpInst::Predicate(predicate),
2732 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2735 return new FCmpInst(CmpInst::Predicate(predicate),
2740 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2741 const Twine &Name, BasicBlock *InsertAtEnd) {
2742 if (Op == Instruction::ICmp) {
2743 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2746 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2750 void CmpInst::swapOperands() {
2751 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2754 cast<FCmpInst>(this)->swapOperands();
2757 bool CmpInst::isCommutative() {
2758 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2759 return IC->isCommutative();
2760 return cast<FCmpInst>(this)->isCommutative();
2763 bool CmpInst::isEquality() {
2764 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2765 return IC->isEquality();
2766 return cast<FCmpInst>(this)->isEquality();
2770 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
2772 default: assert(!"Unknown cmp predicate!");
2773 case ICMP_EQ: return ICMP_NE;
2774 case ICMP_NE: return ICMP_EQ;
2775 case ICMP_UGT: return ICMP_ULE;
2776 case ICMP_ULT: return ICMP_UGE;
2777 case ICMP_UGE: return ICMP_ULT;
2778 case ICMP_ULE: return ICMP_UGT;
2779 case ICMP_SGT: return ICMP_SLE;
2780 case ICMP_SLT: return ICMP_SGE;
2781 case ICMP_SGE: return ICMP_SLT;
2782 case ICMP_SLE: return ICMP_SGT;
2784 case FCMP_OEQ: return FCMP_UNE;
2785 case FCMP_ONE: return FCMP_UEQ;
2786 case FCMP_OGT: return FCMP_ULE;
2787 case FCMP_OLT: return FCMP_UGE;
2788 case FCMP_OGE: return FCMP_ULT;
2789 case FCMP_OLE: return FCMP_UGT;
2790 case FCMP_UEQ: return FCMP_ONE;
2791 case FCMP_UNE: return FCMP_OEQ;
2792 case FCMP_UGT: return FCMP_OLE;
2793 case FCMP_ULT: return FCMP_OGE;
2794 case FCMP_UGE: return FCMP_OLT;
2795 case FCMP_ULE: return FCMP_OGT;
2796 case FCMP_ORD: return FCMP_UNO;
2797 case FCMP_UNO: return FCMP_ORD;
2798 case FCMP_TRUE: return FCMP_FALSE;
2799 case FCMP_FALSE: return FCMP_TRUE;
2803 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2805 default: assert(! "Unknown icmp predicate!");
2806 case ICMP_EQ: case ICMP_NE:
2807 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2809 case ICMP_UGT: return ICMP_SGT;
2810 case ICMP_ULT: return ICMP_SLT;
2811 case ICMP_UGE: return ICMP_SGE;
2812 case ICMP_ULE: return ICMP_SLE;
2816 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2818 default: assert(! "Unknown icmp predicate!");
2819 case ICMP_EQ: case ICMP_NE:
2820 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2822 case ICMP_SGT: return ICMP_UGT;
2823 case ICMP_SLT: return ICMP_ULT;
2824 case ICMP_SGE: return ICMP_UGE;
2825 case ICMP_SLE: return ICMP_ULE;
2829 /// Initialize a set of values that all satisfy the condition with C.
2832 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2835 uint32_t BitWidth = C.getBitWidth();
2837 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
2838 case ICmpInst::ICMP_EQ: Upper++; break;
2839 case ICmpInst::ICMP_NE: Lower++; break;
2840 case ICmpInst::ICMP_ULT:
2841 Lower = APInt::getMinValue(BitWidth);
2842 // Check for an empty-set condition.
2844 return ConstantRange(BitWidth, /*isFullSet=*/false);
2846 case ICmpInst::ICMP_SLT:
2847 Lower = APInt::getSignedMinValue(BitWidth);
2848 // Check for an empty-set condition.
2850 return ConstantRange(BitWidth, /*isFullSet=*/false);
2852 case ICmpInst::ICMP_UGT:
2853 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2854 // Check for an empty-set condition.
2856 return ConstantRange(BitWidth, /*isFullSet=*/false);
2858 case ICmpInst::ICMP_SGT:
2859 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2860 // Check for an empty-set condition.
2862 return ConstantRange(BitWidth, /*isFullSet=*/false);
2864 case ICmpInst::ICMP_ULE:
2865 Lower = APInt::getMinValue(BitWidth); Upper++;
2866 // Check for a full-set condition.
2868 return ConstantRange(BitWidth, /*isFullSet=*/true);
2870 case ICmpInst::ICMP_SLE:
2871 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2872 // Check for a full-set condition.
2874 return ConstantRange(BitWidth, /*isFullSet=*/true);
2876 case ICmpInst::ICMP_UGE:
2877 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2878 // Check for a full-set condition.
2880 return ConstantRange(BitWidth, /*isFullSet=*/true);
2882 case ICmpInst::ICMP_SGE:
2883 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2884 // Check for a full-set condition.
2886 return ConstantRange(BitWidth, /*isFullSet=*/true);
2889 return ConstantRange(Lower, Upper);
2892 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
2894 default: assert(!"Unknown cmp predicate!");
2895 case ICMP_EQ: case ICMP_NE:
2897 case ICMP_SGT: return ICMP_SLT;
2898 case ICMP_SLT: return ICMP_SGT;
2899 case ICMP_SGE: return ICMP_SLE;
2900 case ICMP_SLE: return ICMP_SGE;
2901 case ICMP_UGT: return ICMP_ULT;
2902 case ICMP_ULT: return ICMP_UGT;
2903 case ICMP_UGE: return ICMP_ULE;
2904 case ICMP_ULE: return ICMP_UGE;
2906 case FCMP_FALSE: case FCMP_TRUE:
2907 case FCMP_OEQ: case FCMP_ONE:
2908 case FCMP_UEQ: case FCMP_UNE:
2909 case FCMP_ORD: case FCMP_UNO:
2911 case FCMP_OGT: return FCMP_OLT;
2912 case FCMP_OLT: return FCMP_OGT;
2913 case FCMP_OGE: return FCMP_OLE;
2914 case FCMP_OLE: return FCMP_OGE;
2915 case FCMP_UGT: return FCMP_ULT;
2916 case FCMP_ULT: return FCMP_UGT;
2917 case FCMP_UGE: return FCMP_ULE;
2918 case FCMP_ULE: return FCMP_UGE;
2922 bool CmpInst::isUnsigned(unsigned short predicate) {
2923 switch (predicate) {
2924 default: return false;
2925 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2926 case ICmpInst::ICMP_UGE: return true;
2930 bool CmpInst::isSigned(unsigned short predicate) {
2931 switch (predicate) {
2932 default: return false;
2933 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2934 case ICmpInst::ICMP_SGE: return true;
2938 bool CmpInst::isOrdered(unsigned short predicate) {
2939 switch (predicate) {
2940 default: return false;
2941 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2942 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2943 case FCmpInst::FCMP_ORD: return true;
2947 bool CmpInst::isUnordered(unsigned short predicate) {
2948 switch (predicate) {
2949 default: return false;
2950 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2951 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2952 case FCmpInst::FCMP_UNO: return true;
2956 bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2958 default: return false;
2959 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2960 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2964 bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2966 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2967 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2968 default: return false;
2973 //===----------------------------------------------------------------------===//
2974 // SwitchInst Implementation
2975 //===----------------------------------------------------------------------===//
2977 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
2978 assert(Value && Default);
2979 ReservedSpace = 2+NumCases*2;
2981 OperandList = allocHungoffUses(ReservedSpace);
2983 OperandList[0] = Value;
2984 OperandList[1] = Default;
2987 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2988 /// switch on and a default destination. The number of additional cases can
2989 /// be specified here to make memory allocation more efficient. This
2990 /// constructor can also autoinsert before another instruction.
2991 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2992 Instruction *InsertBefore)
2993 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2994 0, 0, InsertBefore) {
2995 init(Value, Default, NumCases);
2998 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2999 /// switch on and a default destination. The number of additional cases can
3000 /// be specified here to make memory allocation more efficient. This
3001 /// constructor also autoinserts at the end of the specified BasicBlock.
3002 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3003 BasicBlock *InsertAtEnd)
3004 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3005 0, 0, InsertAtEnd) {
3006 init(Value, Default, NumCases);
3009 SwitchInst::SwitchInst(const SwitchInst &SI)
3010 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
3011 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
3012 Use *OL = OperandList, *InOL = SI.OperandList;
3013 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
3015 OL[i+1] = InOL[i+1];
3017 SubclassOptionalData = SI.SubclassOptionalData;
3020 SwitchInst::~SwitchInst() {
3021 dropHungoffUses(OperandList);
3025 /// addCase - Add an entry to the switch instruction...
3027 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
3028 unsigned OpNo = NumOperands;
3029 if (OpNo+2 > ReservedSpace)
3030 resizeOperands(0); // Get more space!
3031 // Initialize some new operands.
3032 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
3033 NumOperands = OpNo+2;
3034 OperandList[OpNo] = OnVal;
3035 OperandList[OpNo+1] = Dest;
3038 /// removeCase - This method removes the specified successor from the switch
3039 /// instruction. Note that this cannot be used to remove the default
3040 /// destination (successor #0).
3042 void SwitchInst::removeCase(unsigned idx) {
3043 assert(idx != 0 && "Cannot remove the default case!");
3044 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3046 unsigned NumOps = getNumOperands();
3047 Use *OL = OperandList;
3049 // Move everything after this operand down.
3051 // FIXME: we could just swap with the end of the list, then erase. However,
3052 // client might not expect this to happen. The code as it is thrashes the
3053 // use/def lists, which is kinda lame.
3054 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3056 OL[i-2+1] = OL[i+1];
3059 // Nuke the last value.
3060 OL[NumOps-2].set(0);
3061 OL[NumOps-2+1].set(0);
3062 NumOperands = NumOps-2;
3065 /// resizeOperands - resize operands - This adjusts the length of the operands
3066 /// list according to the following behavior:
3067 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3068 /// of operation. This grows the number of ops by 3 times.
3069 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3070 /// 3. If NumOps == NumOperands, trim the reserved space.
3072 void SwitchInst::resizeOperands(unsigned NumOps) {
3073 unsigned e = getNumOperands();
3076 } else if (NumOps*2 > NumOperands) {
3077 // No resize needed.
3078 if (ReservedSpace >= NumOps) return;
3079 } else if (NumOps == NumOperands) {
3080 if (ReservedSpace == NumOps) return;
3085 ReservedSpace = NumOps;
3086 Use *NewOps = allocHungoffUses(NumOps);
3087 Use *OldOps = OperandList;
3088 for (unsigned i = 0; i != e; ++i) {
3089 NewOps[i] = OldOps[i];
3091 OperandList = NewOps;
3092 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3096 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3097 return getSuccessor(idx);
3099 unsigned SwitchInst::getNumSuccessorsV() const {
3100 return getNumSuccessors();
3102 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3103 setSuccessor(idx, B);
3106 //===----------------------------------------------------------------------===//
3107 // SwitchInst Implementation
3108 //===----------------------------------------------------------------------===//
3110 void IndirectBrInst::init(Value *Address, unsigned NumDests) {
3111 assert(Address && Address->getType()->isPointerTy() &&
3112 "Address of indirectbr must be a pointer");
3113 ReservedSpace = 1+NumDests;
3115 OperandList = allocHungoffUses(ReservedSpace);
3117 OperandList[0] = Address;
3121 /// resizeOperands - resize operands - This adjusts the length of the operands
3122 /// list according to the following behavior:
3123 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3124 /// of operation. This grows the number of ops by 2 times.
3125 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3126 /// 3. If NumOps == NumOperands, trim the reserved space.
3128 void IndirectBrInst::resizeOperands(unsigned NumOps) {
3129 unsigned e = getNumOperands();
3132 } else if (NumOps*2 > NumOperands) {
3133 // No resize needed.
3134 if (ReservedSpace >= NumOps) return;
3135 } else if (NumOps == NumOperands) {
3136 if (ReservedSpace == NumOps) return;
3141 ReservedSpace = NumOps;
3142 Use *NewOps = allocHungoffUses(NumOps);
3143 Use *OldOps = OperandList;
3144 for (unsigned i = 0; i != e; ++i)
3145 NewOps[i] = OldOps[i];
3146 OperandList = NewOps;
3147 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3150 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3151 Instruction *InsertBefore)
3152 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3153 0, 0, InsertBefore) {
3154 init(Address, NumCases);
3157 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3158 BasicBlock *InsertAtEnd)
3159 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3160 0, 0, InsertAtEnd) {
3161 init(Address, NumCases);
3164 IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3165 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3166 allocHungoffUses(IBI.getNumOperands()),
3167 IBI.getNumOperands()) {
3168 Use *OL = OperandList, *InOL = IBI.OperandList;
3169 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3171 SubclassOptionalData = IBI.SubclassOptionalData;
3174 IndirectBrInst::~IndirectBrInst() {
3175 dropHungoffUses(OperandList);
3178 /// addDestination - Add a destination.
3180 void IndirectBrInst::addDestination(BasicBlock *DestBB) {
3181 unsigned OpNo = NumOperands;
3182 if (OpNo+1 > ReservedSpace)
3183 resizeOperands(0); // Get more space!
3184 // Initialize some new operands.
3185 assert(OpNo < ReservedSpace && "Growing didn't work!");
3186 NumOperands = OpNo+1;
3187 OperandList[OpNo] = DestBB;
3190 /// removeDestination - This method removes the specified successor from the
3191 /// indirectbr instruction.
3192 void IndirectBrInst::removeDestination(unsigned idx) {
3193 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3195 unsigned NumOps = getNumOperands();
3196 Use *OL = OperandList;
3198 // Replace this value with the last one.
3199 OL[idx+1] = OL[NumOps-1];
3201 // Nuke the last value.
3202 OL[NumOps-1].set(0);
3203 NumOperands = NumOps-1;
3206 BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
3207 return getSuccessor(idx);
3209 unsigned IndirectBrInst::getNumSuccessorsV() const {
3210 return getNumSuccessors();
3212 void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3213 setSuccessor(idx, B);
3216 //===----------------------------------------------------------------------===//
3217 // clone_impl() implementations
3218 //===----------------------------------------------------------------------===//
3220 // Define these methods here so vtables don't get emitted into every translation
3221 // unit that uses these classes.
3223 GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3224 return new (getNumOperands()) GetElementPtrInst(*this);
3227 BinaryOperator *BinaryOperator::clone_impl() const {
3228 return Create(getOpcode(), Op<0>(), Op<1>());
3231 FCmpInst* FCmpInst::clone_impl() const {
3232 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
3235 ICmpInst* ICmpInst::clone_impl() const {
3236 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
3239 ExtractValueInst *ExtractValueInst::clone_impl() const {
3240 return new ExtractValueInst(*this);
3243 InsertValueInst *InsertValueInst::clone_impl() const {
3244 return new InsertValueInst(*this);
3247 AllocaInst *AllocaInst::clone_impl() const {
3248 return new AllocaInst(getAllocatedType(),
3249 (Value*)getOperand(0),
3253 LoadInst *LoadInst::clone_impl() const {
3254 return new LoadInst(getOperand(0),
3255 Twine(), isVolatile(),
3259 StoreInst *StoreInst::clone_impl() const {
3260 return new StoreInst(getOperand(0), getOperand(1),
3261 isVolatile(), getAlignment());
3264 TruncInst *TruncInst::clone_impl() const {
3265 return new TruncInst(getOperand(0), getType());
3268 ZExtInst *ZExtInst::clone_impl() const {
3269 return new ZExtInst(getOperand(0), getType());
3272 SExtInst *SExtInst::clone_impl() const {
3273 return new SExtInst(getOperand(0), getType());
3276 FPTruncInst *FPTruncInst::clone_impl() const {
3277 return new FPTruncInst(getOperand(0), getType());
3280 FPExtInst *FPExtInst::clone_impl() const {
3281 return new FPExtInst(getOperand(0), getType());
3284 UIToFPInst *UIToFPInst::clone_impl() const {
3285 return new UIToFPInst(getOperand(0), getType());
3288 SIToFPInst *SIToFPInst::clone_impl() const {
3289 return new SIToFPInst(getOperand(0), getType());
3292 FPToUIInst *FPToUIInst::clone_impl() const {
3293 return new FPToUIInst(getOperand(0), getType());
3296 FPToSIInst *FPToSIInst::clone_impl() const {
3297 return new FPToSIInst(getOperand(0), getType());
3300 PtrToIntInst *PtrToIntInst::clone_impl() const {
3301 return new PtrToIntInst(getOperand(0), getType());
3304 IntToPtrInst *IntToPtrInst::clone_impl() const {
3305 return new IntToPtrInst(getOperand(0), getType());
3308 BitCastInst *BitCastInst::clone_impl() const {
3309 return new BitCastInst(getOperand(0), getType());
3312 CallInst *CallInst::clone_impl() const {
3313 return new(getNumOperands()) CallInst(*this);
3316 SelectInst *SelectInst::clone_impl() const {
3317 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
3320 VAArgInst *VAArgInst::clone_impl() const {
3321 return new VAArgInst(getOperand(0), getType());
3324 ExtractElementInst *ExtractElementInst::clone_impl() const {
3325 return ExtractElementInst::Create(getOperand(0), getOperand(1));
3328 InsertElementInst *InsertElementInst::clone_impl() const {
3329 return InsertElementInst::Create(getOperand(0),
3334 ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3335 return new ShuffleVectorInst(getOperand(0),
3340 PHINode *PHINode::clone_impl() const {
3341 return new PHINode(*this);
3344 ReturnInst *ReturnInst::clone_impl() const {
3345 return new(getNumOperands()) ReturnInst(*this);
3348 BranchInst *BranchInst::clone_impl() const {
3349 unsigned Ops(getNumOperands());
3350 return new(Ops, Ops == 1) BranchInst(*this);
3353 SwitchInst *SwitchInst::clone_impl() const {
3354 return new SwitchInst(*this);
3357 IndirectBrInst *IndirectBrInst::clone_impl() const {
3358 return new IndirectBrInst(*this);
3362 InvokeInst *InvokeInst::clone_impl() const {
3363 return new(getNumOperands()) InvokeInst(*this);
3366 UnwindInst *UnwindInst::clone_impl() const {
3367 LLVMContext &Context = getContext();
3368 return new UnwindInst(Context);
3371 UnreachableInst *UnreachableInst::clone_impl() const {
3372 LLVMContext &Context = getContext();
3373 return new UnreachableInst(Context);