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 (!isa<Constant>(Mask) || MaskTy == 0 ||
1434 !MaskTy->getElementType()->isIntegerTy(32))
1439 /// getMaskValue - Return the index from the shuffle mask for the specified
1440 /// output result. This is either -1 if the element is undef or a number less
1441 /// than 2*numelements.
1442 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1443 const Constant *Mask = cast<Constant>(getOperand(2));
1444 if (isa<UndefValue>(Mask)) return -1;
1445 if (isa<ConstantAggregateZero>(Mask)) return 0;
1446 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1447 assert(i < MaskCV->getNumOperands() && "Index out of range");
1449 if (isa<UndefValue>(MaskCV->getOperand(i)))
1451 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1454 //===----------------------------------------------------------------------===//
1455 // InsertValueInst Class
1456 //===----------------------------------------------------------------------===//
1458 void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1459 unsigned NumIdx, const Twine &Name) {
1460 assert(NumOperands == 2 && "NumOperands not initialized?");
1464 Indices.append(Idx, Idx + NumIdx);
1468 void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1469 const Twine &Name) {
1470 assert(NumOperands == 2 && "NumOperands not initialized?");
1474 Indices.push_back(Idx);
1478 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1479 : Instruction(IVI.getType(), InsertValue,
1480 OperandTraits<InsertValueInst>::op_begin(this), 2),
1481 Indices(IVI.Indices) {
1482 Op<0>() = IVI.getOperand(0);
1483 Op<1>() = IVI.getOperand(1);
1484 SubclassOptionalData = IVI.SubclassOptionalData;
1487 InsertValueInst::InsertValueInst(Value *Agg,
1491 Instruction *InsertBefore)
1492 : Instruction(Agg->getType(), InsertValue,
1493 OperandTraits<InsertValueInst>::op_begin(this),
1495 init(Agg, Val, Idx, Name);
1498 InsertValueInst::InsertValueInst(Value *Agg,
1502 BasicBlock *InsertAtEnd)
1503 : Instruction(Agg->getType(), InsertValue,
1504 OperandTraits<InsertValueInst>::op_begin(this),
1506 init(Agg, Val, Idx, Name);
1509 //===----------------------------------------------------------------------===//
1510 // ExtractValueInst Class
1511 //===----------------------------------------------------------------------===//
1513 void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
1514 const Twine &Name) {
1515 assert(NumOperands == 1 && "NumOperands not initialized?");
1517 Indices.append(Idx, Idx + NumIdx);
1521 void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
1522 assert(NumOperands == 1 && "NumOperands not initialized?");
1524 Indices.push_back(Idx);
1528 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1529 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
1530 Indices(EVI.Indices) {
1531 SubclassOptionalData = EVI.SubclassOptionalData;
1534 // getIndexedType - Returns the type of the element that would be extracted
1535 // with an extractvalue instruction with the specified parameters.
1537 // A null type is returned if the indices are invalid for the specified
1540 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1541 const unsigned *Idxs,
1543 unsigned CurIdx = 0;
1544 for (; CurIdx != NumIdx; ++CurIdx) {
1545 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1546 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
1547 unsigned Index = Idxs[CurIdx];
1548 if (!CT->indexValid(Index)) return 0;
1549 Agg = CT->getTypeAtIndex(Index);
1551 // If the new type forwards to another type, then it is in the middle
1552 // of being refined to another type (and hence, may have dropped all
1553 // references to what it was using before). So, use the new forwarded
1555 if (const Type *Ty = Agg->getForwardedType())
1558 return CurIdx == NumIdx ? Agg : 0;
1561 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1563 return getIndexedType(Agg, &Idx, 1);
1566 //===----------------------------------------------------------------------===//
1567 // BinaryOperator Class
1568 //===----------------------------------------------------------------------===//
1570 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1571 const Type *Ty, const Twine &Name,
1572 Instruction *InsertBefore)
1573 : Instruction(Ty, iType,
1574 OperandTraits<BinaryOperator>::op_begin(this),
1575 OperandTraits<BinaryOperator>::operands(this),
1583 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1584 const Type *Ty, const Twine &Name,
1585 BasicBlock *InsertAtEnd)
1586 : Instruction(Ty, iType,
1587 OperandTraits<BinaryOperator>::op_begin(this),
1588 OperandTraits<BinaryOperator>::operands(this),
1597 void BinaryOperator::init(BinaryOps iType) {
1598 Value *LHS = getOperand(0), *RHS = getOperand(1);
1599 LHS = LHS; RHS = RHS; // Silence warnings.
1600 assert(LHS->getType() == RHS->getType() &&
1601 "Binary operator operand types must match!");
1606 assert(getType() == LHS->getType() &&
1607 "Arithmetic operation should return same type as operands!");
1608 assert(getType()->isIntOrIntVectorTy() &&
1609 "Tried to create an integer operation on a non-integer type!");
1611 case FAdd: case FSub:
1613 assert(getType() == LHS->getType() &&
1614 "Arithmetic operation should return same type as operands!");
1615 assert(getType()->isFPOrFPVectorTy() &&
1616 "Tried to create a floating-point operation on a "
1617 "non-floating-point type!");
1621 assert(getType() == LHS->getType() &&
1622 "Arithmetic operation should return same type as operands!");
1623 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1624 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1625 "Incorrect operand type (not integer) for S/UDIV");
1628 assert(getType() == LHS->getType() &&
1629 "Arithmetic operation should return same type as operands!");
1630 assert(getType()->isFPOrFPVectorTy() &&
1631 "Incorrect operand type (not floating point) for FDIV");
1635 assert(getType() == LHS->getType() &&
1636 "Arithmetic operation should return same type as operands!");
1637 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1638 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1639 "Incorrect operand type (not integer) for S/UREM");
1642 assert(getType() == LHS->getType() &&
1643 "Arithmetic operation should return same type as operands!");
1644 assert(getType()->isFPOrFPVectorTy() &&
1645 "Incorrect operand type (not floating point) for FREM");
1650 assert(getType() == LHS->getType() &&
1651 "Shift operation should return same type as operands!");
1652 assert((getType()->isIntegerTy() ||
1653 (getType()->isVectorTy() &&
1654 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1655 "Tried to create a shift operation on a non-integral type!");
1659 assert(getType() == LHS->getType() &&
1660 "Logical operation should return same type as operands!");
1661 assert((getType()->isIntegerTy() ||
1662 (getType()->isVectorTy() &&
1663 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1664 "Tried to create a logical operation on a non-integral type!");
1672 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1674 Instruction *InsertBefore) {
1675 assert(S1->getType() == S2->getType() &&
1676 "Cannot create binary operator with two operands of differing type!");
1677 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1680 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1682 BasicBlock *InsertAtEnd) {
1683 BinaryOperator *Res = Create(Op, S1, S2, Name);
1684 InsertAtEnd->getInstList().push_back(Res);
1688 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1689 Instruction *InsertBefore) {
1690 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1691 return new BinaryOperator(Instruction::Sub,
1693 Op->getType(), Name, InsertBefore);
1696 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1697 BasicBlock *InsertAtEnd) {
1698 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1699 return new BinaryOperator(Instruction::Sub,
1701 Op->getType(), Name, InsertAtEnd);
1704 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1705 Instruction *InsertBefore) {
1706 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1707 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1710 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1711 BasicBlock *InsertAtEnd) {
1712 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1713 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1716 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1717 Instruction *InsertBefore) {
1718 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1719 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1722 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1723 BasicBlock *InsertAtEnd) {
1724 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1725 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1728 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1729 Instruction *InsertBefore) {
1730 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1731 return new BinaryOperator(Instruction::FSub,
1733 Op->getType(), Name, InsertBefore);
1736 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1737 BasicBlock *InsertAtEnd) {
1738 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1739 return new BinaryOperator(Instruction::FSub,
1741 Op->getType(), Name, InsertAtEnd);
1744 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1745 Instruction *InsertBefore) {
1747 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1748 C = Constant::getAllOnesValue(PTy->getElementType());
1749 C = ConstantVector::get(
1750 std::vector<Constant*>(PTy->getNumElements(), C));
1752 C = Constant::getAllOnesValue(Op->getType());
1755 return new BinaryOperator(Instruction::Xor, Op, C,
1756 Op->getType(), Name, InsertBefore);
1759 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1760 BasicBlock *InsertAtEnd) {
1762 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1763 // Create a vector of all ones values.
1764 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
1765 AllOnes = ConstantVector::get(
1766 std::vector<Constant*>(PTy->getNumElements(), Elt));
1768 AllOnes = Constant::getAllOnesValue(Op->getType());
1771 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1772 Op->getType(), Name, InsertAtEnd);
1776 // isConstantAllOnes - Helper function for several functions below
1777 static inline bool isConstantAllOnes(const Value *V) {
1778 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1779 return CI->isAllOnesValue();
1780 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1781 return CV->isAllOnesValue();
1785 bool BinaryOperator::isNeg(const Value *V) {
1786 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1787 if (Bop->getOpcode() == Instruction::Sub)
1788 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1789 return C->isNegativeZeroValue();
1793 bool BinaryOperator::isFNeg(const Value *V) {
1794 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1795 if (Bop->getOpcode() == Instruction::FSub)
1796 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1797 return C->isNegativeZeroValue();
1801 bool BinaryOperator::isNot(const Value *V) {
1802 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1803 return (Bop->getOpcode() == Instruction::Xor &&
1804 (isConstantAllOnes(Bop->getOperand(1)) ||
1805 isConstantAllOnes(Bop->getOperand(0))));
1809 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1810 return cast<BinaryOperator>(BinOp)->getOperand(1);
1813 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1814 return getNegArgument(const_cast<Value*>(BinOp));
1817 Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1818 return cast<BinaryOperator>(BinOp)->getOperand(1);
1821 const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1822 return getFNegArgument(const_cast<Value*>(BinOp));
1825 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1826 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1827 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1828 Value *Op0 = BO->getOperand(0);
1829 Value *Op1 = BO->getOperand(1);
1830 if (isConstantAllOnes(Op0)) return Op1;
1832 assert(isConstantAllOnes(Op1));
1836 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1837 return getNotArgument(const_cast<Value*>(BinOp));
1841 // swapOperands - Exchange the two operands to this instruction. This
1842 // instruction is safe to use on any binary instruction and does not
1843 // modify the semantics of the instruction. If the instruction is
1844 // order dependent (SetLT f.e.) the opcode is changed.
1846 bool BinaryOperator::swapOperands() {
1847 if (!isCommutative())
1848 return true; // Can't commute operands
1849 Op<0>().swap(Op<1>());
1853 void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1854 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1857 void BinaryOperator::setHasNoSignedWrap(bool b) {
1858 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1861 void BinaryOperator::setIsExact(bool b) {
1862 cast<SDivOperator>(this)->setIsExact(b);
1865 bool BinaryOperator::hasNoUnsignedWrap() const {
1866 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1869 bool BinaryOperator::hasNoSignedWrap() const {
1870 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1873 bool BinaryOperator::isExact() const {
1874 return cast<SDivOperator>(this)->isExact();
1877 //===----------------------------------------------------------------------===//
1879 //===----------------------------------------------------------------------===//
1881 // Just determine if this cast only deals with integral->integral conversion.
1882 bool CastInst::isIntegerCast() const {
1883 switch (getOpcode()) {
1884 default: return false;
1885 case Instruction::ZExt:
1886 case Instruction::SExt:
1887 case Instruction::Trunc:
1889 case Instruction::BitCast:
1890 return getOperand(0)->getType()->isIntegerTy() &&
1891 getType()->isIntegerTy();
1895 bool CastInst::isLosslessCast() const {
1896 // Only BitCast can be lossless, exit fast if we're not BitCast
1897 if (getOpcode() != Instruction::BitCast)
1900 // Identity cast is always lossless
1901 const Type* SrcTy = getOperand(0)->getType();
1902 const Type* DstTy = getType();
1906 // Pointer to pointer is always lossless.
1907 if (SrcTy->isPointerTy())
1908 return DstTy->isPointerTy();
1909 return false; // Other types have no identity values
1912 /// This function determines if the CastInst does not require any bits to be
1913 /// changed in order to effect the cast. Essentially, it identifies cases where
1914 /// no code gen is necessary for the cast, hence the name no-op cast. For
1915 /// example, the following are all no-op casts:
1916 /// # bitcast i32* %x to i8*
1917 /// # bitcast <2 x i32> %x to <4 x i16>
1918 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1919 /// @brief Determine if the described cast is a no-op.
1920 bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1923 const Type *IntPtrTy) {
1926 assert(!"Invalid CastOp");
1927 case Instruction::Trunc:
1928 case Instruction::ZExt:
1929 case Instruction::SExt:
1930 case Instruction::FPTrunc:
1931 case Instruction::FPExt:
1932 case Instruction::UIToFP:
1933 case Instruction::SIToFP:
1934 case Instruction::FPToUI:
1935 case Instruction::FPToSI:
1936 return false; // These always modify bits
1937 case Instruction::BitCast:
1938 return true; // BitCast never modifies bits.
1939 case Instruction::PtrToInt:
1940 return IntPtrTy->getScalarSizeInBits() ==
1941 DestTy->getScalarSizeInBits();
1942 case Instruction::IntToPtr:
1943 return IntPtrTy->getScalarSizeInBits() ==
1944 SrcTy->getScalarSizeInBits();
1948 /// @brief Determine if a cast is a no-op.
1949 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1950 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1953 /// This function determines if a pair of casts can be eliminated and what
1954 /// opcode should be used in the elimination. This assumes that there are two
1955 /// instructions like this:
1956 /// * %F = firstOpcode SrcTy %x to MidTy
1957 /// * %S = secondOpcode MidTy %F to DstTy
1958 /// The function returns a resultOpcode so these two casts can be replaced with:
1959 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1960 /// If no such cast is permited, the function returns 0.
1961 unsigned CastInst::isEliminableCastPair(
1962 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1963 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1965 // Define the 144 possibilities for these two cast instructions. The values
1966 // in this matrix determine what to do in a given situation and select the
1967 // case in the switch below. The rows correspond to firstOp, the columns
1968 // correspond to secondOp. In looking at the table below, keep in mind
1969 // the following cast properties:
1971 // Size Compare Source Destination
1972 // Operator Src ? Size Type Sign Type Sign
1973 // -------- ------------ ------------------- ---------------------
1974 // TRUNC > Integer Any Integral Any
1975 // ZEXT < Integral Unsigned Integer Any
1976 // SEXT < Integral Signed Integer Any
1977 // FPTOUI n/a FloatPt n/a Integral Unsigned
1978 // FPTOSI n/a FloatPt n/a Integral Signed
1979 // UITOFP n/a Integral Unsigned FloatPt n/a
1980 // SITOFP n/a Integral Signed FloatPt n/a
1981 // FPTRUNC > FloatPt n/a FloatPt n/a
1982 // FPEXT < FloatPt n/a FloatPt n/a
1983 // PTRTOINT n/a Pointer n/a Integral Unsigned
1984 // INTTOPTR n/a Integral Unsigned Pointer n/a
1985 // BITCAST = FirstClass n/a FirstClass n/a
1987 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1988 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1989 // into "fptoui double to i64", but this loses information about the range
1990 // of the produced value (we no longer know the top-part is all zeros).
1991 // Further this conversion is often much more expensive for typical hardware,
1992 // and causes issues when building libgcc. We disallow fptosi+sext for the
1994 const unsigned numCastOps =
1995 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1996 static const uint8_t CastResults[numCastOps][numCastOps] = {
1997 // T F F U S F F P I B -+
1998 // R Z S P P I I T P 2 N T |
1999 // U E E 2 2 2 2 R E I T C +- secondOp
2000 // N X X U S F F N X N 2 V |
2001 // C T T I I P P C T T P T -+
2002 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2003 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2004 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
2005 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2006 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
2007 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2008 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2009 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2010 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2011 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2012 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2013 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2016 // If either of the casts are a bitcast from scalar to vector, disallow the
2018 if ((firstOp == Instruction::BitCast &&
2019 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2020 (secondOp == Instruction::BitCast &&
2021 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2022 return 0; // Disallowed
2024 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2025 [secondOp-Instruction::CastOpsBegin];
2028 // categorically disallowed
2031 // allowed, use first cast's opcode
2034 // allowed, use second cast's opcode
2037 // no-op cast in second op implies firstOp as long as the DestTy
2038 // is integer and we are not converting between a vector and a
2040 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
2044 // no-op cast in second op implies firstOp as long as the DestTy
2045 // is floating point.
2046 if (DstTy->isFloatingPointTy())
2050 // no-op cast in first op implies secondOp as long as the SrcTy
2052 if (SrcTy->isIntegerTy())
2056 // no-op cast in first op implies secondOp as long as the SrcTy
2057 // is a floating point.
2058 if (SrcTy->isFloatingPointTy())
2062 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
2065 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2066 unsigned MidSize = MidTy->getScalarSizeInBits();
2067 if (MidSize >= PtrSize)
2068 return Instruction::BitCast;
2072 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2073 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2074 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2075 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2076 unsigned DstSize = DstTy->getScalarSizeInBits();
2077 if (SrcSize == DstSize)
2078 return Instruction::BitCast;
2079 else if (SrcSize < DstSize)
2083 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2084 return Instruction::ZExt;
2086 // fpext followed by ftrunc is allowed if the bit size returned to is
2087 // the same as the original, in which case its just a bitcast
2089 return Instruction::BitCast;
2090 return 0; // If the types are not the same we can't eliminate it.
2092 // bitcast followed by ptrtoint is allowed as long as the bitcast
2093 // is a pointer to pointer cast.
2094 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
2098 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2099 if (MidTy->isPointerTy() && DstTy->isPointerTy())
2103 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2106 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2107 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2108 unsigned DstSize = DstTy->getScalarSizeInBits();
2109 if (SrcSize <= PtrSize && SrcSize == DstSize)
2110 return Instruction::BitCast;
2114 // cast combination can't happen (error in input). This is for all cases
2115 // where the MidTy is not the same for the two cast instructions.
2116 assert(!"Invalid Cast Combination");
2119 assert(!"Error in CastResults table!!!");
2125 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2126 const Twine &Name, Instruction *InsertBefore) {
2127 // Construct and return the appropriate CastInst subclass
2129 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2130 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2131 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2132 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2133 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2134 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2135 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2136 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2137 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2138 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2139 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2140 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2142 assert(!"Invalid opcode provided");
2147 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2148 const Twine &Name, BasicBlock *InsertAtEnd) {
2149 // Construct and return the appropriate CastInst subclass
2151 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2152 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2153 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2154 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2155 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2156 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2157 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2158 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2159 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2160 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2161 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2162 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2164 assert(!"Invalid opcode provided");
2169 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2171 Instruction *InsertBefore) {
2172 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2173 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2174 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
2177 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2179 BasicBlock *InsertAtEnd) {
2180 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2181 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2182 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
2185 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2187 Instruction *InsertBefore) {
2188 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2189 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2190 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
2193 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2195 BasicBlock *InsertAtEnd) {
2196 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2197 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2198 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
2201 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2203 Instruction *InsertBefore) {
2204 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2205 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2206 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
2209 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2211 BasicBlock *InsertAtEnd) {
2212 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2213 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2214 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
2217 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2219 BasicBlock *InsertAtEnd) {
2220 assert(S->getType()->isPointerTy() && "Invalid cast");
2221 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2224 if (Ty->isIntegerTy())
2225 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2226 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2229 /// @brief Create a BitCast or a PtrToInt cast instruction
2230 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2232 Instruction *InsertBefore) {
2233 assert(S->getType()->isPointerTy() && "Invalid cast");
2234 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2237 if (Ty->isIntegerTy())
2238 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2239 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2242 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2243 bool isSigned, const Twine &Name,
2244 Instruction *InsertBefore) {
2245 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2246 "Invalid integer cast");
2247 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2248 unsigned DstBits = Ty->getScalarSizeInBits();
2249 Instruction::CastOps opcode =
2250 (SrcBits == DstBits ? Instruction::BitCast :
2251 (SrcBits > DstBits ? Instruction::Trunc :
2252 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2253 return Create(opcode, C, Ty, Name, InsertBefore);
2256 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2257 bool isSigned, const Twine &Name,
2258 BasicBlock *InsertAtEnd) {
2259 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2261 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2262 unsigned DstBits = Ty->getScalarSizeInBits();
2263 Instruction::CastOps opcode =
2264 (SrcBits == DstBits ? Instruction::BitCast :
2265 (SrcBits > DstBits ? Instruction::Trunc :
2266 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2267 return Create(opcode, C, Ty, Name, InsertAtEnd);
2270 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2272 Instruction *InsertBefore) {
2273 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2275 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2276 unsigned DstBits = Ty->getScalarSizeInBits();
2277 Instruction::CastOps opcode =
2278 (SrcBits == DstBits ? Instruction::BitCast :
2279 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2280 return Create(opcode, C, Ty, Name, InsertBefore);
2283 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2285 BasicBlock *InsertAtEnd) {
2286 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2288 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2289 unsigned DstBits = Ty->getScalarSizeInBits();
2290 Instruction::CastOps opcode =
2291 (SrcBits == DstBits ? Instruction::BitCast :
2292 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2293 return Create(opcode, C, Ty, Name, InsertAtEnd);
2296 // Check whether it is valid to call getCastOpcode for these types.
2297 // This routine must be kept in sync with getCastOpcode.
2298 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2299 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2302 if (SrcTy == DestTy)
2305 // Get the bit sizes, we'll need these
2306 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2307 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2309 // Run through the possibilities ...
2310 if (DestTy->isIntegerTy()) { // Casting to integral
2311 if (SrcTy->isIntegerTy()) { // Casting from integral
2313 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2315 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2316 // Casting from vector
2317 return DestBits == PTy->getBitWidth();
2318 } else { // Casting from something else
2319 return SrcTy->isPointerTy();
2321 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2322 if (SrcTy->isIntegerTy()) { // Casting from integral
2324 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2326 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2327 // Casting from vector
2328 return DestBits == PTy->getBitWidth();
2329 } else { // Casting from something else
2332 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2333 // Casting to vector
2334 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2335 // Casting from vector
2336 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2337 } else { // Casting from something else
2338 return DestPTy->getBitWidth() == SrcBits;
2340 } else if (DestTy->isPointerTy()) { // Casting to pointer
2341 if (SrcTy->isPointerTy()) { // Casting from pointer
2343 } else if (SrcTy->isIntegerTy()) { // Casting from integral
2345 } else { // Casting from something else
2348 } else { // Casting to something else
2353 // Provide a way to get a "cast" where the cast opcode is inferred from the
2354 // types and size of the operand. This, basically, is a parallel of the
2355 // logic in the castIsValid function below. This axiom should hold:
2356 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2357 // should not assert in castIsValid. In other words, this produces a "correct"
2358 // casting opcode for the arguments passed to it.
2359 // This routine must be kept in sync with isCastable.
2360 Instruction::CastOps
2361 CastInst::getCastOpcode(
2362 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
2363 // Get the bit sizes, we'll need these
2364 const Type *SrcTy = Src->getType();
2365 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2366 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2368 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2369 "Only first class types are castable!");
2371 // Run through the possibilities ...
2372 if (DestTy->isIntegerTy()) { // Casting to integral
2373 if (SrcTy->isIntegerTy()) { // Casting from integral
2374 if (DestBits < SrcBits)
2375 return Trunc; // int -> smaller int
2376 else if (DestBits > SrcBits) { // its an extension
2378 return SExt; // signed -> SEXT
2380 return ZExt; // unsigned -> ZEXT
2382 return BitCast; // Same size, No-op cast
2384 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2386 return FPToSI; // FP -> sint
2388 return FPToUI; // FP -> uint
2389 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2390 assert(DestBits == PTy->getBitWidth() &&
2391 "Casting vector to integer of different width");
2393 return BitCast; // Same size, no-op cast
2395 assert(SrcTy->isPointerTy() &&
2396 "Casting from a value that is not first-class type");
2397 return PtrToInt; // ptr -> int
2399 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2400 if (SrcTy->isIntegerTy()) { // Casting from integral
2402 return SIToFP; // sint -> FP
2404 return UIToFP; // uint -> FP
2405 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2406 if (DestBits < SrcBits) {
2407 return FPTrunc; // FP -> smaller FP
2408 } else if (DestBits > SrcBits) {
2409 return FPExt; // FP -> larger FP
2411 return BitCast; // same size, no-op cast
2413 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2414 assert(DestBits == PTy->getBitWidth() &&
2415 "Casting vector to floating point of different width");
2417 return BitCast; // same size, no-op cast
2419 llvm_unreachable("Casting pointer or non-first class to float");
2421 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2422 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2423 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
2424 "Casting vector to vector of different widths");
2426 return BitCast; // vector -> vector
2427 } else if (DestPTy->getBitWidth() == SrcBits) {
2428 return BitCast; // float/int -> vector
2430 assert(!"Illegal cast to vector (wrong type or size)");
2432 } else if (DestTy->isPointerTy()) {
2433 if (SrcTy->isPointerTy()) {
2434 return BitCast; // ptr -> ptr
2435 } else if (SrcTy->isIntegerTy()) {
2436 return IntToPtr; // int -> ptr
2438 assert(!"Casting pointer to other than pointer or int");
2441 assert(!"Casting to type that is not first-class");
2444 // If we fall through to here we probably hit an assertion cast above
2445 // and assertions are not turned on. Anything we return is an error, so
2446 // BitCast is as good a choice as any.
2450 //===----------------------------------------------------------------------===//
2451 // CastInst SubClass Constructors
2452 //===----------------------------------------------------------------------===//
2454 /// Check that the construction parameters for a CastInst are correct. This
2455 /// could be broken out into the separate constructors but it is useful to have
2456 /// it in one place and to eliminate the redundant code for getting the sizes
2457 /// of the types involved.
2459 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
2461 // Check for type sanity on the arguments
2462 const Type *SrcTy = S->getType();
2463 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2464 SrcTy->isAggregateType() || DstTy->isAggregateType())
2467 // Get the size of the types in bits, we'll need this later
2468 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2469 unsigned DstBitSize = DstTy->getScalarSizeInBits();
2471 // Switch on the opcode provided
2473 default: return false; // This is an input error
2474 case Instruction::Trunc:
2475 return SrcTy->isIntOrIntVectorTy() &&
2476 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
2477 case Instruction::ZExt:
2478 return SrcTy->isIntOrIntVectorTy() &&
2479 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2480 case Instruction::SExt:
2481 return SrcTy->isIntOrIntVectorTy() &&
2482 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2483 case Instruction::FPTrunc:
2484 return SrcTy->isFPOrFPVectorTy() &&
2485 DstTy->isFPOrFPVectorTy() &&
2486 SrcBitSize > DstBitSize;
2487 case Instruction::FPExt:
2488 return SrcTy->isFPOrFPVectorTy() &&
2489 DstTy->isFPOrFPVectorTy() &&
2490 SrcBitSize < DstBitSize;
2491 case Instruction::UIToFP:
2492 case Instruction::SIToFP:
2493 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2494 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2495 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2496 DVTy->getElementType()->isFPOrFPVectorTy() &&
2497 SVTy->getNumElements() == DVTy->getNumElements();
2500 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
2501 case Instruction::FPToUI:
2502 case Instruction::FPToSI:
2503 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2504 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2505 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2506 DVTy->getElementType()->isIntOrIntVectorTy() &&
2507 SVTy->getNumElements() == DVTy->getNumElements();
2510 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
2511 case Instruction::PtrToInt:
2512 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
2513 case Instruction::IntToPtr:
2514 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
2515 case Instruction::BitCast:
2516 // BitCast implies a no-op cast of type only. No bits change.
2517 // However, you can't cast pointers to anything but pointers.
2518 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
2521 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2522 // these cases, the cast is okay if the source and destination bit widths
2524 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2528 TruncInst::TruncInst(
2529 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2530 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2531 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2534 TruncInst::TruncInst(
2535 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2536 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2537 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2541 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2542 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2543 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2547 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2548 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2549 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2552 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2553 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2554 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2558 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2559 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2560 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2563 FPTruncInst::FPTruncInst(
2564 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2565 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2566 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2569 FPTruncInst::FPTruncInst(
2570 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2571 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2572 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2575 FPExtInst::FPExtInst(
2576 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2577 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2578 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2581 FPExtInst::FPExtInst(
2582 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2583 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2584 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2587 UIToFPInst::UIToFPInst(
2588 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2589 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2590 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2593 UIToFPInst::UIToFPInst(
2594 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2595 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2596 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2599 SIToFPInst::SIToFPInst(
2600 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2601 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2602 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2605 SIToFPInst::SIToFPInst(
2606 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2607 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2608 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2611 FPToUIInst::FPToUIInst(
2612 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2613 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2614 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2617 FPToUIInst::FPToUIInst(
2618 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2619 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2620 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2623 FPToSIInst::FPToSIInst(
2624 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2625 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2626 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2629 FPToSIInst::FPToSIInst(
2630 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2631 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2632 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2635 PtrToIntInst::PtrToIntInst(
2636 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2637 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2638 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2641 PtrToIntInst::PtrToIntInst(
2642 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2643 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2644 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2647 IntToPtrInst::IntToPtrInst(
2648 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2649 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2650 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2653 IntToPtrInst::IntToPtrInst(
2654 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2655 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2656 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2659 BitCastInst::BitCastInst(
2660 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2661 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2662 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2665 BitCastInst::BitCastInst(
2666 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2667 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2668 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2671 //===----------------------------------------------------------------------===//
2673 //===----------------------------------------------------------------------===//
2675 void CmpInst::Anchor() const {}
2677 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2678 Value *LHS, Value *RHS, const Twine &Name,
2679 Instruction *InsertBefore)
2680 : Instruction(ty, op,
2681 OperandTraits<CmpInst>::op_begin(this),
2682 OperandTraits<CmpInst>::operands(this),
2686 setPredicate((Predicate)predicate);
2690 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2691 Value *LHS, Value *RHS, const Twine &Name,
2692 BasicBlock *InsertAtEnd)
2693 : Instruction(ty, op,
2694 OperandTraits<CmpInst>::op_begin(this),
2695 OperandTraits<CmpInst>::operands(this),
2699 setPredicate((Predicate)predicate);
2704 CmpInst::Create(OtherOps Op, unsigned short predicate,
2705 Value *S1, Value *S2,
2706 const Twine &Name, Instruction *InsertBefore) {
2707 if (Op == Instruction::ICmp) {
2709 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2712 return new ICmpInst(CmpInst::Predicate(predicate),
2717 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2720 return new FCmpInst(CmpInst::Predicate(predicate),
2725 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2726 const Twine &Name, BasicBlock *InsertAtEnd) {
2727 if (Op == Instruction::ICmp) {
2728 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2731 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2735 void CmpInst::swapOperands() {
2736 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2739 cast<FCmpInst>(this)->swapOperands();
2742 bool CmpInst::isCommutative() {
2743 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2744 return IC->isCommutative();
2745 return cast<FCmpInst>(this)->isCommutative();
2748 bool CmpInst::isEquality() {
2749 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2750 return IC->isEquality();
2751 return cast<FCmpInst>(this)->isEquality();
2755 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
2757 default: assert(!"Unknown cmp predicate!");
2758 case ICMP_EQ: return ICMP_NE;
2759 case ICMP_NE: return ICMP_EQ;
2760 case ICMP_UGT: return ICMP_ULE;
2761 case ICMP_ULT: return ICMP_UGE;
2762 case ICMP_UGE: return ICMP_ULT;
2763 case ICMP_ULE: return ICMP_UGT;
2764 case ICMP_SGT: return ICMP_SLE;
2765 case ICMP_SLT: return ICMP_SGE;
2766 case ICMP_SGE: return ICMP_SLT;
2767 case ICMP_SLE: return ICMP_SGT;
2769 case FCMP_OEQ: return FCMP_UNE;
2770 case FCMP_ONE: return FCMP_UEQ;
2771 case FCMP_OGT: return FCMP_ULE;
2772 case FCMP_OLT: return FCMP_UGE;
2773 case FCMP_OGE: return FCMP_ULT;
2774 case FCMP_OLE: return FCMP_UGT;
2775 case FCMP_UEQ: return FCMP_ONE;
2776 case FCMP_UNE: return FCMP_OEQ;
2777 case FCMP_UGT: return FCMP_OLE;
2778 case FCMP_ULT: return FCMP_OGE;
2779 case FCMP_UGE: return FCMP_OLT;
2780 case FCMP_ULE: return FCMP_OGT;
2781 case FCMP_ORD: return FCMP_UNO;
2782 case FCMP_UNO: return FCMP_ORD;
2783 case FCMP_TRUE: return FCMP_FALSE;
2784 case FCMP_FALSE: return FCMP_TRUE;
2788 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2790 default: assert(! "Unknown icmp predicate!");
2791 case ICMP_EQ: case ICMP_NE:
2792 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2794 case ICMP_UGT: return ICMP_SGT;
2795 case ICMP_ULT: return ICMP_SLT;
2796 case ICMP_UGE: return ICMP_SGE;
2797 case ICMP_ULE: return ICMP_SLE;
2801 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2803 default: assert(! "Unknown icmp predicate!");
2804 case ICMP_EQ: case ICMP_NE:
2805 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2807 case ICMP_SGT: return ICMP_UGT;
2808 case ICMP_SLT: return ICMP_ULT;
2809 case ICMP_SGE: return ICMP_UGE;
2810 case ICMP_SLE: return ICMP_ULE;
2814 /// Initialize a set of values that all satisfy the condition with C.
2817 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2820 uint32_t BitWidth = C.getBitWidth();
2822 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
2823 case ICmpInst::ICMP_EQ: Upper++; break;
2824 case ICmpInst::ICMP_NE: Lower++; break;
2825 case ICmpInst::ICMP_ULT:
2826 Lower = APInt::getMinValue(BitWidth);
2827 // Check for an empty-set condition.
2829 return ConstantRange(BitWidth, /*isFullSet=*/false);
2831 case ICmpInst::ICMP_SLT:
2832 Lower = APInt::getSignedMinValue(BitWidth);
2833 // Check for an empty-set condition.
2835 return ConstantRange(BitWidth, /*isFullSet=*/false);
2837 case ICmpInst::ICMP_UGT:
2838 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2839 // Check for an empty-set condition.
2841 return ConstantRange(BitWidth, /*isFullSet=*/false);
2843 case ICmpInst::ICMP_SGT:
2844 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2845 // Check for an empty-set condition.
2847 return ConstantRange(BitWidth, /*isFullSet=*/false);
2849 case ICmpInst::ICMP_ULE:
2850 Lower = APInt::getMinValue(BitWidth); Upper++;
2851 // Check for a full-set condition.
2853 return ConstantRange(BitWidth, /*isFullSet=*/true);
2855 case ICmpInst::ICMP_SLE:
2856 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2857 // Check for a full-set condition.
2859 return ConstantRange(BitWidth, /*isFullSet=*/true);
2861 case ICmpInst::ICMP_UGE:
2862 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2863 // Check for a full-set condition.
2865 return ConstantRange(BitWidth, /*isFullSet=*/true);
2867 case ICmpInst::ICMP_SGE:
2868 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2869 // Check for a full-set condition.
2871 return ConstantRange(BitWidth, /*isFullSet=*/true);
2874 return ConstantRange(Lower, Upper);
2877 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
2879 default: assert(!"Unknown cmp predicate!");
2880 case ICMP_EQ: case ICMP_NE:
2882 case ICMP_SGT: return ICMP_SLT;
2883 case ICMP_SLT: return ICMP_SGT;
2884 case ICMP_SGE: return ICMP_SLE;
2885 case ICMP_SLE: return ICMP_SGE;
2886 case ICMP_UGT: return ICMP_ULT;
2887 case ICMP_ULT: return ICMP_UGT;
2888 case ICMP_UGE: return ICMP_ULE;
2889 case ICMP_ULE: return ICMP_UGE;
2891 case FCMP_FALSE: case FCMP_TRUE:
2892 case FCMP_OEQ: case FCMP_ONE:
2893 case FCMP_UEQ: case FCMP_UNE:
2894 case FCMP_ORD: case FCMP_UNO:
2896 case FCMP_OGT: return FCMP_OLT;
2897 case FCMP_OLT: return FCMP_OGT;
2898 case FCMP_OGE: return FCMP_OLE;
2899 case FCMP_OLE: return FCMP_OGE;
2900 case FCMP_UGT: return FCMP_ULT;
2901 case FCMP_ULT: return FCMP_UGT;
2902 case FCMP_UGE: return FCMP_ULE;
2903 case FCMP_ULE: return FCMP_UGE;
2907 bool CmpInst::isUnsigned(unsigned short predicate) {
2908 switch (predicate) {
2909 default: return false;
2910 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2911 case ICmpInst::ICMP_UGE: return true;
2915 bool CmpInst::isSigned(unsigned short predicate) {
2916 switch (predicate) {
2917 default: return false;
2918 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2919 case ICmpInst::ICMP_SGE: return true;
2923 bool CmpInst::isOrdered(unsigned short predicate) {
2924 switch (predicate) {
2925 default: return false;
2926 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2927 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2928 case FCmpInst::FCMP_ORD: return true;
2932 bool CmpInst::isUnordered(unsigned short predicate) {
2933 switch (predicate) {
2934 default: return false;
2935 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2936 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2937 case FCmpInst::FCMP_UNO: return true;
2941 bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2943 default: return false;
2944 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2945 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2949 bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2951 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2952 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2953 default: return false;
2958 //===----------------------------------------------------------------------===//
2959 // SwitchInst Implementation
2960 //===----------------------------------------------------------------------===//
2962 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
2963 assert(Value && Default);
2964 ReservedSpace = 2+NumCases*2;
2966 OperandList = allocHungoffUses(ReservedSpace);
2968 OperandList[0] = Value;
2969 OperandList[1] = Default;
2972 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2973 /// switch on and a default destination. The number of additional cases can
2974 /// be specified here to make memory allocation more efficient. This
2975 /// constructor can also autoinsert before another instruction.
2976 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2977 Instruction *InsertBefore)
2978 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2979 0, 0, InsertBefore) {
2980 init(Value, Default, NumCases);
2983 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2984 /// switch on and a default destination. The number of additional cases can
2985 /// be specified here to make memory allocation more efficient. This
2986 /// constructor also autoinserts at the end of the specified BasicBlock.
2987 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2988 BasicBlock *InsertAtEnd)
2989 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2990 0, 0, InsertAtEnd) {
2991 init(Value, Default, NumCases);
2994 SwitchInst::SwitchInst(const SwitchInst &SI)
2995 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
2996 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
2997 Use *OL = OperandList, *InOL = SI.OperandList;
2998 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
3000 OL[i+1] = InOL[i+1];
3002 SubclassOptionalData = SI.SubclassOptionalData;
3005 SwitchInst::~SwitchInst() {
3006 dropHungoffUses(OperandList);
3010 /// addCase - Add an entry to the switch instruction...
3012 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
3013 unsigned OpNo = NumOperands;
3014 if (OpNo+2 > ReservedSpace)
3015 resizeOperands(0); // Get more space!
3016 // Initialize some new operands.
3017 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
3018 NumOperands = OpNo+2;
3019 OperandList[OpNo] = OnVal;
3020 OperandList[OpNo+1] = Dest;
3023 /// removeCase - This method removes the specified successor from the switch
3024 /// instruction. Note that this cannot be used to remove the default
3025 /// destination (successor #0).
3027 void SwitchInst::removeCase(unsigned idx) {
3028 assert(idx != 0 && "Cannot remove the default case!");
3029 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3031 unsigned NumOps = getNumOperands();
3032 Use *OL = OperandList;
3034 // Move everything after this operand down.
3036 // FIXME: we could just swap with the end of the list, then erase. However,
3037 // client might not expect this to happen. The code as it is thrashes the
3038 // use/def lists, which is kinda lame.
3039 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3041 OL[i-2+1] = OL[i+1];
3044 // Nuke the last value.
3045 OL[NumOps-2].set(0);
3046 OL[NumOps-2+1].set(0);
3047 NumOperands = NumOps-2;
3050 /// resizeOperands - resize operands - This adjusts the length of the operands
3051 /// list according to the following behavior:
3052 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3053 /// of operation. This grows the number of ops by 3 times.
3054 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3055 /// 3. If NumOps == NumOperands, trim the reserved space.
3057 void SwitchInst::resizeOperands(unsigned NumOps) {
3058 unsigned e = getNumOperands();
3061 } else if (NumOps*2 > NumOperands) {
3062 // No resize needed.
3063 if (ReservedSpace >= NumOps) return;
3064 } else if (NumOps == NumOperands) {
3065 if (ReservedSpace == NumOps) return;
3070 ReservedSpace = NumOps;
3071 Use *NewOps = allocHungoffUses(NumOps);
3072 Use *OldOps = OperandList;
3073 for (unsigned i = 0; i != e; ++i) {
3074 NewOps[i] = OldOps[i];
3076 OperandList = NewOps;
3077 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3081 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3082 return getSuccessor(idx);
3084 unsigned SwitchInst::getNumSuccessorsV() const {
3085 return getNumSuccessors();
3087 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3088 setSuccessor(idx, B);
3091 //===----------------------------------------------------------------------===//
3092 // SwitchInst Implementation
3093 //===----------------------------------------------------------------------===//
3095 void IndirectBrInst::init(Value *Address, unsigned NumDests) {
3096 assert(Address && Address->getType()->isPointerTy() &&
3097 "Address of indirectbr must be a pointer");
3098 ReservedSpace = 1+NumDests;
3100 OperandList = allocHungoffUses(ReservedSpace);
3102 OperandList[0] = Address;
3106 /// resizeOperands - resize operands - This adjusts the length of the operands
3107 /// list according to the following behavior:
3108 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3109 /// of operation. This grows the number of ops by 2 times.
3110 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3111 /// 3. If NumOps == NumOperands, trim the reserved space.
3113 void IndirectBrInst::resizeOperands(unsigned NumOps) {
3114 unsigned e = getNumOperands();
3117 } else if (NumOps*2 > NumOperands) {
3118 // No resize needed.
3119 if (ReservedSpace >= NumOps) return;
3120 } else if (NumOps == NumOperands) {
3121 if (ReservedSpace == NumOps) return;
3126 ReservedSpace = NumOps;
3127 Use *NewOps = allocHungoffUses(NumOps);
3128 Use *OldOps = OperandList;
3129 for (unsigned i = 0; i != e; ++i)
3130 NewOps[i] = OldOps[i];
3131 OperandList = NewOps;
3132 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3135 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3136 Instruction *InsertBefore)
3137 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3138 0, 0, InsertBefore) {
3139 init(Address, NumCases);
3142 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3143 BasicBlock *InsertAtEnd)
3144 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3145 0, 0, InsertAtEnd) {
3146 init(Address, NumCases);
3149 IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3150 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3151 allocHungoffUses(IBI.getNumOperands()),
3152 IBI.getNumOperands()) {
3153 Use *OL = OperandList, *InOL = IBI.OperandList;
3154 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3156 SubclassOptionalData = IBI.SubclassOptionalData;
3159 IndirectBrInst::~IndirectBrInst() {
3160 dropHungoffUses(OperandList);
3163 /// addDestination - Add a destination.
3165 void IndirectBrInst::addDestination(BasicBlock *DestBB) {
3166 unsigned OpNo = NumOperands;
3167 if (OpNo+1 > ReservedSpace)
3168 resizeOperands(0); // Get more space!
3169 // Initialize some new operands.
3170 assert(OpNo < ReservedSpace && "Growing didn't work!");
3171 NumOperands = OpNo+1;
3172 OperandList[OpNo] = DestBB;
3175 /// removeDestination - This method removes the specified successor from the
3176 /// indirectbr instruction.
3177 void IndirectBrInst::removeDestination(unsigned idx) {
3178 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3180 unsigned NumOps = getNumOperands();
3181 Use *OL = OperandList;
3183 // Replace this value with the last one.
3184 OL[idx+1] = OL[NumOps-1];
3186 // Nuke the last value.
3187 OL[NumOps-1].set(0);
3188 NumOperands = NumOps-1;
3191 BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
3192 return getSuccessor(idx);
3194 unsigned IndirectBrInst::getNumSuccessorsV() const {
3195 return getNumSuccessors();
3197 void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3198 setSuccessor(idx, B);
3201 //===----------------------------------------------------------------------===//
3202 // clone_impl() implementations
3203 //===----------------------------------------------------------------------===//
3205 // Define these methods here so vtables don't get emitted into every translation
3206 // unit that uses these classes.
3208 GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3209 return new (getNumOperands()) GetElementPtrInst(*this);
3212 BinaryOperator *BinaryOperator::clone_impl() const {
3213 return Create(getOpcode(), Op<0>(), Op<1>());
3216 FCmpInst* FCmpInst::clone_impl() const {
3217 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
3220 ICmpInst* ICmpInst::clone_impl() const {
3221 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
3224 ExtractValueInst *ExtractValueInst::clone_impl() const {
3225 return new ExtractValueInst(*this);
3228 InsertValueInst *InsertValueInst::clone_impl() const {
3229 return new InsertValueInst(*this);
3232 AllocaInst *AllocaInst::clone_impl() const {
3233 return new AllocaInst(getAllocatedType(),
3234 (Value*)getOperand(0),
3238 LoadInst *LoadInst::clone_impl() const {
3239 return new LoadInst(getOperand(0),
3240 Twine(), isVolatile(),
3244 StoreInst *StoreInst::clone_impl() const {
3245 return new StoreInst(getOperand(0), getOperand(1),
3246 isVolatile(), getAlignment());
3249 TruncInst *TruncInst::clone_impl() const {
3250 return new TruncInst(getOperand(0), getType());
3253 ZExtInst *ZExtInst::clone_impl() const {
3254 return new ZExtInst(getOperand(0), getType());
3257 SExtInst *SExtInst::clone_impl() const {
3258 return new SExtInst(getOperand(0), getType());
3261 FPTruncInst *FPTruncInst::clone_impl() const {
3262 return new FPTruncInst(getOperand(0), getType());
3265 FPExtInst *FPExtInst::clone_impl() const {
3266 return new FPExtInst(getOperand(0), getType());
3269 UIToFPInst *UIToFPInst::clone_impl() const {
3270 return new UIToFPInst(getOperand(0), getType());
3273 SIToFPInst *SIToFPInst::clone_impl() const {
3274 return new SIToFPInst(getOperand(0), getType());
3277 FPToUIInst *FPToUIInst::clone_impl() const {
3278 return new FPToUIInst(getOperand(0), getType());
3281 FPToSIInst *FPToSIInst::clone_impl() const {
3282 return new FPToSIInst(getOperand(0), getType());
3285 PtrToIntInst *PtrToIntInst::clone_impl() const {
3286 return new PtrToIntInst(getOperand(0), getType());
3289 IntToPtrInst *IntToPtrInst::clone_impl() const {
3290 return new IntToPtrInst(getOperand(0), getType());
3293 BitCastInst *BitCastInst::clone_impl() const {
3294 return new BitCastInst(getOperand(0), getType());
3297 CallInst *CallInst::clone_impl() const {
3298 return new(getNumOperands()) CallInst(*this);
3301 SelectInst *SelectInst::clone_impl() const {
3302 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
3305 VAArgInst *VAArgInst::clone_impl() const {
3306 return new VAArgInst(getOperand(0), getType());
3309 ExtractElementInst *ExtractElementInst::clone_impl() const {
3310 return ExtractElementInst::Create(getOperand(0), getOperand(1));
3313 InsertElementInst *InsertElementInst::clone_impl() const {
3314 return InsertElementInst::Create(getOperand(0),
3319 ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3320 return new ShuffleVectorInst(getOperand(0),
3325 PHINode *PHINode::clone_impl() const {
3326 return new PHINode(*this);
3329 ReturnInst *ReturnInst::clone_impl() const {
3330 return new(getNumOperands()) ReturnInst(*this);
3333 BranchInst *BranchInst::clone_impl() const {
3334 unsigned Ops(getNumOperands());
3335 return new(Ops, Ops == 1) BranchInst(*this);
3338 SwitchInst *SwitchInst::clone_impl() const {
3339 return new SwitchInst(*this);
3342 IndirectBrInst *IndirectBrInst::clone_impl() const {
3343 return new IndirectBrInst(*this);
3347 InvokeInst *InvokeInst::clone_impl() const {
3348 return new(getNumOperands()) InvokeInst(*this);
3351 UnwindInst *UnwindInst::clone_impl() const {
3352 LLVMContext &Context = getContext();
3353 return new UnwindInst(Context);
3356 UnreachableInst *UnreachableInst::clone_impl() const {
3357 LLVMContext &Context = getContext();
3358 return new UnreachableInst(Context);