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/Support/ErrorHandling.h"
23 #include "llvm/Support/CallSite.h"
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Support/MathExtras.h"
28 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
35 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
36 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
39 //===----------------------------------------------------------------------===//
40 // TerminatorInst Class
41 //===----------------------------------------------------------------------===//
43 // Out of line virtual method, so the vtable, etc has a home.
44 TerminatorInst::~TerminatorInst() {
47 //===----------------------------------------------------------------------===//
48 // UnaryInstruction Class
49 //===----------------------------------------------------------------------===//
51 // Out of line virtual method, so the vtable, etc has a home.
52 UnaryInstruction::~UnaryInstruction() {
55 //===----------------------------------------------------------------------===//
57 //===----------------------------------------------------------------------===//
59 /// areInvalidOperands - Return a string if the specified operands are invalid
60 /// for a select operation, otherwise return null.
61 const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
65 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
67 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
68 return "vector select condition element type must be i1";
69 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
75 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
76 return "select condition must be i1 or <n x i1>";
82 //===----------------------------------------------------------------------===//
84 //===----------------------------------------------------------------------===//
86 PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
88 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
89 ReservedSpace(PN.getNumOperands()) {
90 Use *OL = OperandList;
91 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
92 OL[i] = PN.getOperand(i);
93 OL[i+1] = PN.getOperand(i+1);
95 SubclassOptionalData = PN.SubclassOptionalData;
100 dropHungoffUses(OperandList);
103 // removeIncomingValue - Remove an incoming value. This is useful if a
104 // predecessor basic block is deleted.
105 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
106 unsigned NumOps = getNumOperands();
107 Use *OL = OperandList;
108 assert(Idx*2 < NumOps && "BB not in PHI node!");
109 Value *Removed = OL[Idx*2];
111 // Move everything after this operand down.
113 // FIXME: we could just swap with the end of the list, then erase. However,
114 // client might not expect this to happen. The code as it is thrashes the
115 // use/def lists, which is kinda lame.
116 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
121 // Nuke the last value.
123 OL[NumOps-2+1].set(0);
124 NumOperands = NumOps-2;
126 // If the PHI node is dead, because it has zero entries, nuke it now.
127 if (NumOps == 2 && DeletePHIIfEmpty) {
128 // If anyone is using this PHI, make them use a dummy value instead...
129 replaceAllUsesWith(UndefValue::get(getType()));
135 /// resizeOperands - resize operands - This adjusts the length of the operands
136 /// list according to the following behavior:
137 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
138 /// of operation. This grows the number of ops by 1.5 times.
139 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
140 /// 3. If NumOps == NumOperands, trim the reserved space.
142 void PHINode::resizeOperands(unsigned NumOps) {
143 unsigned e = getNumOperands();
146 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
147 } else if (NumOps*2 > NumOperands) {
149 if (ReservedSpace >= NumOps) return;
150 } else if (NumOps == NumOperands) {
151 if (ReservedSpace == NumOps) return;
156 ReservedSpace = NumOps;
157 Use *OldOps = OperandList;
158 Use *NewOps = allocHungoffUses(NumOps);
159 std::copy(OldOps, OldOps + e, NewOps);
160 OperandList = NewOps;
161 if (OldOps) Use::zap(OldOps, OldOps + e, true);
164 /// hasConstantValue - If the specified PHI node always merges together the same
165 /// value, return the value, otherwise return null.
166 Value *PHINode::hasConstantValue() const {
167 // Exploit the fact that phi nodes always have at least one entry.
168 Value *ConstantValue = getIncomingValue(0);
169 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
170 if (getIncomingValue(i) != ConstantValue)
171 return 0; // Incoming values not all the same.
172 return ConstantValue;
176 //===----------------------------------------------------------------------===//
177 // CallInst Implementation
178 //===----------------------------------------------------------------------===//
180 CallInst::~CallInst() {
183 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
184 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
187 const FunctionType *FTy =
188 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
189 (void)FTy; // silence warning.
191 assert((NumParams == FTy->getNumParams() ||
192 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
193 "Calling a function with bad signature!");
194 for (unsigned i = 0; i != NumParams; ++i) {
195 assert((i >= FTy->getNumParams() ||
196 FTy->getParamType(i) == Params[i]->getType()) &&
197 "Calling a function with a bad signature!");
198 OperandList[i] = Params[i];
202 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
203 assert(NumOperands == 3 && "NumOperands not set up?");
208 const FunctionType *FTy =
209 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
210 (void)FTy; // silence warning.
212 assert((FTy->getNumParams() == 2 ||
213 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
214 "Calling a function with bad signature");
215 assert((0 >= FTy->getNumParams() ||
216 FTy->getParamType(0) == Actual1->getType()) &&
217 "Calling a function with a bad signature!");
218 assert((1 >= FTy->getNumParams() ||
219 FTy->getParamType(1) == Actual2->getType()) &&
220 "Calling a function with a bad signature!");
223 void CallInst::init(Value *Func, Value *Actual) {
224 assert(NumOperands == 2 && "NumOperands not set up?");
228 const FunctionType *FTy =
229 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
230 (void)FTy; // silence warning.
232 assert((FTy->getNumParams() == 1 ||
233 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
234 "Calling a function with bad signature");
235 assert((0 == FTy->getNumParams() ||
236 FTy->getParamType(0) == Actual->getType()) &&
237 "Calling a function with a bad signature!");
240 void CallInst::init(Value *Func) {
241 assert(NumOperands == 1 && "NumOperands not set up?");
244 const FunctionType *FTy =
245 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
246 (void)FTy; // silence warning.
248 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
251 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
252 Instruction *InsertBefore)
253 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
254 ->getElementType())->getReturnType(),
256 OperandTraits<CallInst>::op_end(this) - 2,
262 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
263 BasicBlock *InsertAtEnd)
264 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
265 ->getElementType())->getReturnType(),
267 OperandTraits<CallInst>::op_end(this) - 2,
272 CallInst::CallInst(Value *Func, const Twine &Name,
273 Instruction *InsertBefore)
274 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
275 ->getElementType())->getReturnType(),
277 OperandTraits<CallInst>::op_end(this) - 1,
283 CallInst::CallInst(Value *Func, const Twine &Name,
284 BasicBlock *InsertAtEnd)
285 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
286 ->getElementType())->getReturnType(),
288 OperandTraits<CallInst>::op_end(this) - 1,
294 CallInst::CallInst(const CallInst &CI)
295 : Instruction(CI.getType(), Instruction::Call,
296 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
297 CI.getNumOperands()) {
298 setAttributes(CI.getAttributes());
299 setTailCall(CI.isTailCall());
300 setCallingConv(CI.getCallingConv());
302 Use *OL = OperandList;
303 Use *InOL = CI.OperandList;
304 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
306 SubclassOptionalData = CI.SubclassOptionalData;
309 void CallInst::addAttribute(unsigned i, Attributes attr) {
310 AttrListPtr PAL = getAttributes();
311 PAL = PAL.addAttr(i, attr);
315 void CallInst::removeAttribute(unsigned i, Attributes attr) {
316 AttrListPtr PAL = getAttributes();
317 PAL = PAL.removeAttr(i, attr);
321 bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
322 if (AttributeList.paramHasAttr(i, attr))
324 if (const Function *F = getCalledFunction())
325 return F->paramHasAttr(i, attr);
329 /// IsConstantOne - Return true only if val is constant int 1
330 static bool IsConstantOne(Value *val) {
331 assert(val && "IsConstantOne does not work with NULL val");
332 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
335 static Instruction *createMalloc(Instruction *InsertBefore,
336 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
337 const Type *AllocTy, Value *AllocSize,
338 Value *ArraySize, Function *MallocF,
340 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
341 "createMalloc needs either InsertBefore or InsertAtEnd");
343 // malloc(type) becomes:
344 // bitcast (i8* malloc(typeSize)) to type*
345 // malloc(type, arraySize) becomes:
346 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
348 ArraySize = ConstantInt::get(IntPtrTy, 1);
349 else if (ArraySize->getType() != IntPtrTy) {
351 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
354 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
358 if (!IsConstantOne(ArraySize)) {
359 if (IsConstantOne(AllocSize)) {
360 AllocSize = ArraySize; // Operand * 1 = Operand
361 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
362 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
364 // Malloc arg is constant product of type size and array size
365 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
367 // Multiply type size by the array size...
369 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
370 "mallocsize", InsertBefore);
372 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
373 "mallocsize", InsertAtEnd);
377 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
378 // Create the call to Malloc.
379 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
380 Module* M = BB->getParent()->getParent();
381 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
382 Value *MallocFunc = MallocF;
384 // prototype malloc as "void *malloc(size_t)"
385 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
386 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
387 CallInst *MCall = NULL;
388 Instruction *Result = NULL;
390 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
392 if (Result->getType() != AllocPtrType)
393 // Create a cast instruction to convert to the right type...
394 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
396 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
398 if (Result->getType() != AllocPtrType) {
399 InsertAtEnd->getInstList().push_back(MCall);
400 // Create a cast instruction to convert to the right type...
401 Result = new BitCastInst(MCall, AllocPtrType, Name);
404 MCall->setTailCall();
405 if (Function *F = dyn_cast<Function>(MallocFunc)) {
406 MCall->setCallingConv(F->getCallingConv());
407 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
409 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
414 /// CreateMalloc - Generate the IR for a call to malloc:
415 /// 1. Compute the malloc call's argument as the specified type's size,
416 /// possibly multiplied by the array size if the array size is not
418 /// 2. Call malloc with that argument.
419 /// 3. Bitcast the result of the malloc call to the specified type.
420 Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
421 const Type *IntPtrTy, const Type *AllocTy,
422 Value *AllocSize, Value *ArraySize,
425 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
426 ArraySize, MallocF, Name);
429 /// CreateMalloc - Generate the IR for a call to malloc:
430 /// 1. Compute the malloc call's argument as the specified type's size,
431 /// possibly multiplied by the array size if the array size is not
433 /// 2. Call malloc with that argument.
434 /// 3. Bitcast the result of the malloc call to the specified type.
435 /// Note: This function does not add the bitcast to the basic block, that is the
436 /// responsibility of the caller.
437 Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
438 const Type *IntPtrTy, const Type *AllocTy,
439 Value *AllocSize, Value *ArraySize,
440 Function *MallocF, const Twine &Name) {
441 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
442 ArraySize, MallocF, Name);
445 static Instruction* createFree(Value* Source, Instruction *InsertBefore,
446 BasicBlock *InsertAtEnd) {
447 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
448 "createFree needs either InsertBefore or InsertAtEnd");
449 assert(Source->getType()->isPointerTy() &&
450 "Can not free something of nonpointer type!");
452 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
453 Module* M = BB->getParent()->getParent();
455 const Type *VoidTy = Type::getVoidTy(M->getContext());
456 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
457 // prototype free as "void free(void*)"
458 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
459 CallInst* Result = NULL;
460 Value *PtrCast = Source;
462 if (Source->getType() != IntPtrTy)
463 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
464 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
466 if (Source->getType() != IntPtrTy)
467 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
468 Result = CallInst::Create(FreeFunc, PtrCast, "");
470 Result->setTailCall();
471 if (Function *F = dyn_cast<Function>(FreeFunc))
472 Result->setCallingConv(F->getCallingConv());
477 /// CreateFree - Generate the IR for a call to the builtin free function.
478 Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
479 return createFree(Source, InsertBefore, NULL);
482 /// CreateFree - Generate the IR for a call to the builtin free function.
483 /// Note: This function does not add the call to the basic block, that is the
484 /// responsibility of the caller.
485 Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
486 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
487 assert(FreeCall && "CreateFree did not create a CallInst");
491 //===----------------------------------------------------------------------===//
492 // InvokeInst Implementation
493 //===----------------------------------------------------------------------===//
495 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
496 Value* const *Args, unsigned NumArgs) {
497 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
500 Op<-1>() = IfException;
501 const FunctionType *FTy =
502 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
503 (void)FTy; // silence warning.
505 assert(((NumArgs == FTy->getNumParams()) ||
506 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
507 "Invoking a function with bad signature");
509 Use *OL = OperandList;
510 for (unsigned i = 0, e = NumArgs; i != e; i++) {
511 assert((i >= FTy->getNumParams() ||
512 FTy->getParamType(i) == Args[i]->getType()) &&
513 "Invoking a function with a bad signature!");
519 InvokeInst::InvokeInst(const InvokeInst &II)
520 : TerminatorInst(II.getType(), Instruction::Invoke,
521 OperandTraits<InvokeInst>::op_end(this)
522 - II.getNumOperands(),
523 II.getNumOperands()) {
524 setAttributes(II.getAttributes());
525 setCallingConv(II.getCallingConv());
526 Use *OL = OperandList, *InOL = II.OperandList;
527 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
529 SubclassOptionalData = II.SubclassOptionalData;
532 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
533 return getSuccessor(idx);
535 unsigned InvokeInst::getNumSuccessorsV() const {
536 return getNumSuccessors();
538 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
539 return setSuccessor(idx, B);
542 bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
543 if (AttributeList.paramHasAttr(i, attr))
545 if (const Function *F = getCalledFunction())
546 return F->paramHasAttr(i, attr);
550 void InvokeInst::addAttribute(unsigned i, Attributes attr) {
551 AttrListPtr PAL = getAttributes();
552 PAL = PAL.addAttr(i, attr);
556 void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
557 AttrListPtr PAL = getAttributes();
558 PAL = PAL.removeAttr(i, attr);
563 //===----------------------------------------------------------------------===//
564 // ReturnInst Implementation
565 //===----------------------------------------------------------------------===//
567 ReturnInst::ReturnInst(const ReturnInst &RI)
568 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
569 OperandTraits<ReturnInst>::op_end(this) -
571 RI.getNumOperands()) {
572 if (RI.getNumOperands())
573 Op<0>() = RI.Op<0>();
574 SubclassOptionalData = RI.SubclassOptionalData;
577 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
578 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
579 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
584 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
585 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
586 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
591 ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
592 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
593 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
596 unsigned ReturnInst::getNumSuccessorsV() const {
597 return getNumSuccessors();
600 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
601 /// emit the vtable for the class in this translation unit.
602 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
603 llvm_unreachable("ReturnInst has no successors!");
606 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
607 llvm_unreachable("ReturnInst has no successors!");
611 ReturnInst::~ReturnInst() {
614 //===----------------------------------------------------------------------===//
615 // UnwindInst Implementation
616 //===----------------------------------------------------------------------===//
618 UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
619 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
620 0, 0, InsertBefore) {
622 UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
623 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
628 unsigned UnwindInst::getNumSuccessorsV() const {
629 return getNumSuccessors();
632 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
633 llvm_unreachable("UnwindInst has no successors!");
636 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
637 llvm_unreachable("UnwindInst has no successors!");
641 //===----------------------------------------------------------------------===//
642 // UnreachableInst Implementation
643 //===----------------------------------------------------------------------===//
645 UnreachableInst::UnreachableInst(LLVMContext &Context,
646 Instruction *InsertBefore)
647 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
648 0, 0, InsertBefore) {
650 UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
651 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
655 unsigned UnreachableInst::getNumSuccessorsV() const {
656 return getNumSuccessors();
659 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
660 llvm_unreachable("UnwindInst has no successors!");
663 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
664 llvm_unreachable("UnwindInst has no successors!");
668 //===----------------------------------------------------------------------===//
669 // BranchInst Implementation
670 //===----------------------------------------------------------------------===//
672 void BranchInst::AssertOK() {
674 assert(getCondition()->getType()->isIntegerTy(1) &&
675 "May only branch on boolean predicates!");
678 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
679 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
680 OperandTraits<BranchInst>::op_end(this) - 1,
682 assert(IfTrue != 0 && "Branch destination may not be null!");
685 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
686 Instruction *InsertBefore)
687 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
688 OperandTraits<BranchInst>::op_end(this) - 3,
698 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
699 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
700 OperandTraits<BranchInst>::op_end(this) - 1,
702 assert(IfTrue != 0 && "Branch destination may not be null!");
706 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
707 BasicBlock *InsertAtEnd)
708 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
709 OperandTraits<BranchInst>::op_end(this) - 3,
720 BranchInst::BranchInst(const BranchInst &BI) :
721 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
722 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
723 BI.getNumOperands()) {
724 Op<-1>() = BI.Op<-1>();
725 if (BI.getNumOperands() != 1) {
726 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
727 Op<-3>() = BI.Op<-3>();
728 Op<-2>() = BI.Op<-2>();
730 SubclassOptionalData = BI.SubclassOptionalData;
733 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
734 return getSuccessor(idx);
736 unsigned BranchInst::getNumSuccessorsV() const {
737 return getNumSuccessors();
739 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
740 setSuccessor(idx, B);
744 //===----------------------------------------------------------------------===//
745 // AllocaInst Implementation
746 //===----------------------------------------------------------------------===//
748 static Value *getAISize(LLVMContext &Context, Value *Amt) {
750 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
752 assert(!isa<BasicBlock>(Amt) &&
753 "Passed basic block into allocation size parameter! Use other ctor");
754 assert(Amt->getType()->isIntegerTy() &&
755 "Allocation array size is not an integer!");
760 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
761 const Twine &Name, Instruction *InsertBefore)
762 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
763 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
765 assert(!Ty->isVoidTy() && "Cannot allocate void!");
769 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
770 const Twine &Name, BasicBlock *InsertAtEnd)
771 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
772 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
774 assert(!Ty->isVoidTy() && "Cannot allocate void!");
778 AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
779 Instruction *InsertBefore)
780 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
781 getAISize(Ty->getContext(), 0), InsertBefore) {
783 assert(!Ty->isVoidTy() && "Cannot allocate void!");
787 AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
788 BasicBlock *InsertAtEnd)
789 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
790 getAISize(Ty->getContext(), 0), InsertAtEnd) {
792 assert(!Ty->isVoidTy() && "Cannot allocate void!");
796 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
797 const Twine &Name, Instruction *InsertBefore)
798 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
799 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
801 assert(!Ty->isVoidTy() && "Cannot allocate void!");
805 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
806 const Twine &Name, BasicBlock *InsertAtEnd)
807 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
808 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
810 assert(!Ty->isVoidTy() && "Cannot allocate void!");
814 // Out of line virtual method, so the vtable, etc has a home.
815 AllocaInst::~AllocaInst() {
818 void AllocaInst::setAlignment(unsigned Align) {
819 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
820 assert(Align <= MaximumAlignment &&
821 "Alignment is greater than MaximumAlignment!");
822 setInstructionSubclassData(Log2_32(Align) + 1);
823 assert(getAlignment() == Align && "Alignment representation error!");
826 bool AllocaInst::isArrayAllocation() const {
827 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
832 const Type *AllocaInst::getAllocatedType() const {
833 return getType()->getElementType();
836 /// isStaticAlloca - Return true if this alloca is in the entry block of the
837 /// function and is a constant size. If so, the code generator will fold it
838 /// into the prolog/epilog code, so it is basically free.
839 bool AllocaInst::isStaticAlloca() const {
840 // Must be constant size.
841 if (!isa<ConstantInt>(getArraySize())) return false;
843 // Must be in the entry block.
844 const BasicBlock *Parent = getParent();
845 return Parent == &Parent->getParent()->front();
848 //===----------------------------------------------------------------------===//
849 // LoadInst Implementation
850 //===----------------------------------------------------------------------===//
852 void LoadInst::AssertOK() {
853 assert(getOperand(0)->getType()->isPointerTy() &&
854 "Ptr must have pointer type.");
857 LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
858 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
859 Load, Ptr, InsertBef) {
866 LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
867 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
868 Load, Ptr, InsertAE) {
875 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
876 Instruction *InsertBef)
877 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
878 Load, Ptr, InsertBef) {
879 setVolatile(isVolatile);
885 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
886 unsigned Align, Instruction *InsertBef)
887 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
888 Load, Ptr, InsertBef) {
889 setVolatile(isVolatile);
895 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
896 unsigned Align, BasicBlock *InsertAE)
897 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
898 Load, Ptr, InsertAE) {
899 setVolatile(isVolatile);
905 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
906 BasicBlock *InsertAE)
907 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
908 Load, Ptr, InsertAE) {
909 setVolatile(isVolatile);
917 LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
918 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
919 Load, Ptr, InsertBef) {
923 if (Name && Name[0]) setName(Name);
926 LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
927 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
928 Load, Ptr, InsertAE) {
932 if (Name && Name[0]) setName(Name);
935 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
936 Instruction *InsertBef)
937 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
938 Load, Ptr, InsertBef) {
939 setVolatile(isVolatile);
942 if (Name && Name[0]) setName(Name);
945 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
946 BasicBlock *InsertAE)
947 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
948 Load, Ptr, InsertAE) {
949 setVolatile(isVolatile);
952 if (Name && Name[0]) setName(Name);
955 void LoadInst::setAlignment(unsigned Align) {
956 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
957 assert(Align <= MaximumAlignment &&
958 "Alignment is greater than MaximumAlignment!");
959 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
960 ((Log2_32(Align)+1)<<1));
961 assert(getAlignment() == Align && "Alignment representation error!");
964 //===----------------------------------------------------------------------===//
965 // StoreInst Implementation
966 //===----------------------------------------------------------------------===//
968 void StoreInst::AssertOK() {
969 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
970 assert(getOperand(1)->getType()->isPointerTy() &&
971 "Ptr must have pointer type!");
972 assert(getOperand(0)->getType() ==
973 cast<PointerType>(getOperand(1)->getType())->getElementType()
974 && "Ptr must be a pointer to Val type!");
978 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
979 : Instruction(Type::getVoidTy(val->getContext()), Store,
980 OperandTraits<StoreInst>::op_begin(this),
981 OperandTraits<StoreInst>::operands(this),
990 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
991 : Instruction(Type::getVoidTy(val->getContext()), Store,
992 OperandTraits<StoreInst>::op_begin(this),
993 OperandTraits<StoreInst>::operands(this),
1002 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1003 Instruction *InsertBefore)
1004 : Instruction(Type::getVoidTy(val->getContext()), Store,
1005 OperandTraits<StoreInst>::op_begin(this),
1006 OperandTraits<StoreInst>::operands(this),
1010 setVolatile(isVolatile);
1015 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1016 unsigned Align, Instruction *InsertBefore)
1017 : Instruction(Type::getVoidTy(val->getContext()), Store,
1018 OperandTraits<StoreInst>::op_begin(this),
1019 OperandTraits<StoreInst>::operands(this),
1023 setVolatile(isVolatile);
1024 setAlignment(Align);
1028 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1029 unsigned Align, BasicBlock *InsertAtEnd)
1030 : Instruction(Type::getVoidTy(val->getContext()), Store,
1031 OperandTraits<StoreInst>::op_begin(this),
1032 OperandTraits<StoreInst>::operands(this),
1036 setVolatile(isVolatile);
1037 setAlignment(Align);
1041 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1042 BasicBlock *InsertAtEnd)
1043 : Instruction(Type::getVoidTy(val->getContext()), Store,
1044 OperandTraits<StoreInst>::op_begin(this),
1045 OperandTraits<StoreInst>::operands(this),
1049 setVolatile(isVolatile);
1054 void StoreInst::setAlignment(unsigned Align) {
1055 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1056 assert(Align <= MaximumAlignment &&
1057 "Alignment is greater than MaximumAlignment!");
1058 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1059 ((Log2_32(Align)+1) << 1));
1060 assert(getAlignment() == Align && "Alignment representation error!");
1063 //===----------------------------------------------------------------------===//
1064 // GetElementPtrInst Implementation
1065 //===----------------------------------------------------------------------===//
1067 static unsigned retrieveAddrSpace(const Value *Val) {
1068 return cast<PointerType>(Val->getType())->getAddressSpace();
1071 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
1072 const Twine &Name) {
1073 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1074 Use *OL = OperandList;
1077 for (unsigned i = 0; i != NumIdx; ++i)
1083 void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
1084 assert(NumOperands == 2 && "NumOperands not initialized?");
1085 Use *OL = OperandList;
1092 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1093 : Instruction(GEPI.getType(), GetElementPtr,
1094 OperandTraits<GetElementPtrInst>::op_end(this)
1095 - GEPI.getNumOperands(),
1096 GEPI.getNumOperands()) {
1097 Use *OL = OperandList;
1098 Use *GEPIOL = GEPI.OperandList;
1099 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1101 SubclassOptionalData = GEPI.SubclassOptionalData;
1104 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1105 const Twine &Name, Instruction *InBe)
1106 : Instruction(PointerType::get(
1107 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
1109 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1111 init(Ptr, Idx, Name);
1114 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1115 const Twine &Name, BasicBlock *IAE)
1116 : Instruction(PointerType::get(
1117 checkType(getIndexedType(Ptr->getType(),Idx)),
1118 retrieveAddrSpace(Ptr)),
1120 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1122 init(Ptr, Idx, Name);
1125 /// getIndexedType - Returns the type of the element that would be accessed with
1126 /// a gep instruction with the specified parameters.
1128 /// The Idxs pointer should point to a continuous piece of memory containing the
1129 /// indices, either as Value* or uint64_t.
1131 /// A null type is returned if the indices are invalid for the specified
1134 template <typename IndexTy>
1135 static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1137 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1138 if (!PTy) return 0; // Type isn't a pointer type!
1139 const Type *Agg = PTy->getElementType();
1141 // Handle the special case of the empty set index set, which is always valid.
1145 // If there is at least one index, the top level type must be sized, otherwise
1146 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1147 // that contain opaque types) under the assumption that it will be resolved to
1148 // a sane type later.
1149 if (!Agg->isSized() && !Agg->isAbstract())
1152 unsigned CurIdx = 1;
1153 for (; CurIdx != NumIdx; ++CurIdx) {
1154 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1155 if (!CT || CT->isPointerTy()) return 0;
1156 IndexTy Index = Idxs[CurIdx];
1157 if (!CT->indexValid(Index)) return 0;
1158 Agg = CT->getTypeAtIndex(Index);
1160 // If the new type forwards to another type, then it is in the middle
1161 // of being refined to another type (and hence, may have dropped all
1162 // references to what it was using before). So, use the new forwarded
1164 if (const Type *Ty = Agg->getForwardedType())
1167 return CurIdx == NumIdx ? Agg : 0;
1170 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1173 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1176 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1177 uint64_t const *Idxs,
1179 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1182 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1183 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1184 if (!PTy) return 0; // Type isn't a pointer type!
1186 // Check the pointer index.
1187 if (!PTy->indexValid(Idx)) return 0;
1189 return PTy->getElementType();
1193 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1194 /// zeros. If so, the result pointer and the first operand have the same
1195 /// value, just potentially different types.
1196 bool GetElementPtrInst::hasAllZeroIndices() const {
1197 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1198 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1199 if (!CI->isZero()) return false;
1207 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1208 /// constant integers. If so, the result pointer and the first operand have
1209 /// a constant offset between them.
1210 bool GetElementPtrInst::hasAllConstantIndices() const {
1211 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1212 if (!isa<ConstantInt>(getOperand(i)))
1218 void GetElementPtrInst::setIsInBounds(bool B) {
1219 cast<GEPOperator>(this)->setIsInBounds(B);
1222 bool GetElementPtrInst::isInBounds() const {
1223 return cast<GEPOperator>(this)->isInBounds();
1226 //===----------------------------------------------------------------------===//
1227 // ExtractElementInst Implementation
1228 //===----------------------------------------------------------------------===//
1230 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1232 Instruction *InsertBef)
1233 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1235 OperandTraits<ExtractElementInst>::op_begin(this),
1237 assert(isValidOperands(Val, Index) &&
1238 "Invalid extractelement instruction operands!");
1244 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1246 BasicBlock *InsertAE)
1247 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1249 OperandTraits<ExtractElementInst>::op_begin(this),
1251 assert(isValidOperands(Val, Index) &&
1252 "Invalid extractelement instruction operands!");
1260 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1261 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
1267 //===----------------------------------------------------------------------===//
1268 // InsertElementInst Implementation
1269 //===----------------------------------------------------------------------===//
1271 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1273 Instruction *InsertBef)
1274 : Instruction(Vec->getType(), InsertElement,
1275 OperandTraits<InsertElementInst>::op_begin(this),
1277 assert(isValidOperands(Vec, Elt, Index) &&
1278 "Invalid insertelement instruction operands!");
1285 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1287 BasicBlock *InsertAE)
1288 : Instruction(Vec->getType(), InsertElement,
1289 OperandTraits<InsertElementInst>::op_begin(this),
1291 assert(isValidOperands(Vec, Elt, Index) &&
1292 "Invalid insertelement instruction operands!");
1300 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1301 const Value *Index) {
1302 if (!Vec->getType()->isVectorTy())
1303 return false; // First operand of insertelement must be vector type.
1305 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1306 return false;// Second operand of insertelement must be vector element type.
1308 if (!Index->getType()->isIntegerTy(32))
1309 return false; // Third operand of insertelement must be i32.
1314 //===----------------------------------------------------------------------===//
1315 // ShuffleVectorInst Implementation
1316 //===----------------------------------------------------------------------===//
1318 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1320 Instruction *InsertBefore)
1321 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1322 cast<VectorType>(Mask->getType())->getNumElements()),
1324 OperandTraits<ShuffleVectorInst>::op_begin(this),
1325 OperandTraits<ShuffleVectorInst>::operands(this),
1327 assert(isValidOperands(V1, V2, Mask) &&
1328 "Invalid shuffle vector instruction operands!");
1335 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1337 BasicBlock *InsertAtEnd)
1338 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1339 cast<VectorType>(Mask->getType())->getNumElements()),
1341 OperandTraits<ShuffleVectorInst>::op_begin(this),
1342 OperandTraits<ShuffleVectorInst>::operands(this),
1344 assert(isValidOperands(V1, V2, Mask) &&
1345 "Invalid shuffle vector instruction operands!");
1353 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1354 const Value *Mask) {
1355 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
1358 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1359 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
1362 // Check to see if Mask is valid.
1363 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1364 const VectorType *VTy = cast<VectorType>(V1->getType());
1365 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1366 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1367 if (CI->uge(VTy->getNumElements()*2))
1369 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1374 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1380 /// getMaskValue - Return the index from the shuffle mask for the specified
1381 /// output result. This is either -1 if the element is undef or a number less
1382 /// than 2*numelements.
1383 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1384 const Constant *Mask = cast<Constant>(getOperand(2));
1385 if (isa<UndefValue>(Mask)) return -1;
1386 if (isa<ConstantAggregateZero>(Mask)) return 0;
1387 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1388 assert(i < MaskCV->getNumOperands() && "Index out of range");
1390 if (isa<UndefValue>(MaskCV->getOperand(i)))
1392 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1395 //===----------------------------------------------------------------------===//
1396 // InsertValueInst Class
1397 //===----------------------------------------------------------------------===//
1399 void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1400 unsigned NumIdx, const Twine &Name) {
1401 assert(NumOperands == 2 && "NumOperands not initialized?");
1402 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx, Idx + NumIdx) ==
1403 Val->getType() && "Inserted value must match indexed type!");
1407 Indices.append(Idx, Idx + NumIdx);
1411 void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1412 const Twine &Name) {
1413 assert(NumOperands == 2 && "NumOperands not initialized?");
1414 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx) == Val->getType()
1415 && "Inserted value must match indexed type!");
1419 Indices.push_back(Idx);
1423 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1424 : Instruction(IVI.getType(), InsertValue,
1425 OperandTraits<InsertValueInst>::op_begin(this), 2),
1426 Indices(IVI.Indices) {
1427 Op<0>() = IVI.getOperand(0);
1428 Op<1>() = IVI.getOperand(1);
1429 SubclassOptionalData = IVI.SubclassOptionalData;
1432 InsertValueInst::InsertValueInst(Value *Agg,
1436 Instruction *InsertBefore)
1437 : Instruction(Agg->getType(), InsertValue,
1438 OperandTraits<InsertValueInst>::op_begin(this),
1440 init(Agg, Val, Idx, Name);
1443 InsertValueInst::InsertValueInst(Value *Agg,
1447 BasicBlock *InsertAtEnd)
1448 : Instruction(Agg->getType(), InsertValue,
1449 OperandTraits<InsertValueInst>::op_begin(this),
1451 init(Agg, Val, Idx, Name);
1454 //===----------------------------------------------------------------------===//
1455 // ExtractValueInst Class
1456 //===----------------------------------------------------------------------===//
1458 void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
1459 const Twine &Name) {
1460 assert(NumOperands == 1 && "NumOperands not initialized?");
1462 Indices.append(Idx, Idx + NumIdx);
1466 void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
1467 assert(NumOperands == 1 && "NumOperands not initialized?");
1469 Indices.push_back(Idx);
1473 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1474 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
1475 Indices(EVI.Indices) {
1476 SubclassOptionalData = EVI.SubclassOptionalData;
1479 // getIndexedType - Returns the type of the element that would be extracted
1480 // with an extractvalue instruction with the specified parameters.
1482 // A null type is returned if the indices are invalid for the specified
1485 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1486 const unsigned *Idxs,
1488 for (unsigned CurIdx = 0; CurIdx != NumIdx; ++CurIdx) {
1489 unsigned Index = Idxs[CurIdx];
1490 // We can't use CompositeType::indexValid(Index) here.
1491 // indexValid() always returns true for arrays because getelementptr allows
1492 // out-of-bounds indices. Since we don't allow those for extractvalue and
1493 // insertvalue we need to check array indexing manually.
1494 // Since the only other types we can index into are struct types it's just
1495 // as easy to check those manually as well.
1496 if (const ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
1497 if (Index >= AT->getNumElements())
1499 } else if (const StructType *ST = dyn_cast<StructType>(Agg)) {
1500 if (Index >= ST->getNumElements())
1503 // Not a valid type to index into.
1507 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
1509 // If the new type forwards to another type, then it is in the middle
1510 // of being refined to another type (and hence, may have dropped all
1511 // references to what it was using before). So, use the new forwarded
1513 if (const Type *Ty = Agg->getForwardedType())
1519 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1521 return getIndexedType(Agg, &Idx, 1);
1524 //===----------------------------------------------------------------------===//
1525 // BinaryOperator Class
1526 //===----------------------------------------------------------------------===//
1528 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1529 const Type *Ty, const Twine &Name,
1530 Instruction *InsertBefore)
1531 : Instruction(Ty, iType,
1532 OperandTraits<BinaryOperator>::op_begin(this),
1533 OperandTraits<BinaryOperator>::operands(this),
1541 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1542 const Type *Ty, const Twine &Name,
1543 BasicBlock *InsertAtEnd)
1544 : Instruction(Ty, iType,
1545 OperandTraits<BinaryOperator>::op_begin(this),
1546 OperandTraits<BinaryOperator>::operands(this),
1555 void BinaryOperator::init(BinaryOps iType) {
1556 Value *LHS = getOperand(0), *RHS = getOperand(1);
1557 (void)LHS; (void)RHS; // Silence warnings.
1558 assert(LHS->getType() == RHS->getType() &&
1559 "Binary operator operand types must match!");
1564 assert(getType() == LHS->getType() &&
1565 "Arithmetic operation should return same type as operands!");
1566 assert(getType()->isIntOrIntVectorTy() &&
1567 "Tried to create an integer operation on a non-integer type!");
1569 case FAdd: case FSub:
1571 assert(getType() == LHS->getType() &&
1572 "Arithmetic operation should return same type as operands!");
1573 assert(getType()->isFPOrFPVectorTy() &&
1574 "Tried to create a floating-point operation on a "
1575 "non-floating-point type!");
1579 assert(getType() == LHS->getType() &&
1580 "Arithmetic operation should return same type as operands!");
1581 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1582 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1583 "Incorrect operand type (not integer) for S/UDIV");
1586 assert(getType() == LHS->getType() &&
1587 "Arithmetic operation should return same type as operands!");
1588 assert(getType()->isFPOrFPVectorTy() &&
1589 "Incorrect operand type (not floating point) for FDIV");
1593 assert(getType() == LHS->getType() &&
1594 "Arithmetic operation should return same type as operands!");
1595 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1596 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1597 "Incorrect operand type (not integer) for S/UREM");
1600 assert(getType() == LHS->getType() &&
1601 "Arithmetic operation should return same type as operands!");
1602 assert(getType()->isFPOrFPVectorTy() &&
1603 "Incorrect operand type (not floating point) for FREM");
1608 assert(getType() == LHS->getType() &&
1609 "Shift operation should return same type as operands!");
1610 assert((getType()->isIntegerTy() ||
1611 (getType()->isVectorTy() &&
1612 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1613 "Tried to create a shift operation on a non-integral type!");
1617 assert(getType() == LHS->getType() &&
1618 "Logical operation should return same type as operands!");
1619 assert((getType()->isIntegerTy() ||
1620 (getType()->isVectorTy() &&
1621 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1622 "Tried to create a logical operation on a non-integral type!");
1630 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1632 Instruction *InsertBefore) {
1633 assert(S1->getType() == S2->getType() &&
1634 "Cannot create binary operator with two operands of differing type!");
1635 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1638 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1640 BasicBlock *InsertAtEnd) {
1641 BinaryOperator *Res = Create(Op, S1, S2, Name);
1642 InsertAtEnd->getInstList().push_back(Res);
1646 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1647 Instruction *InsertBefore) {
1648 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1649 return new BinaryOperator(Instruction::Sub,
1651 Op->getType(), Name, InsertBefore);
1654 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1655 BasicBlock *InsertAtEnd) {
1656 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1657 return new BinaryOperator(Instruction::Sub,
1659 Op->getType(), Name, InsertAtEnd);
1662 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1663 Instruction *InsertBefore) {
1664 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1665 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1668 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1669 BasicBlock *InsertAtEnd) {
1670 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1671 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1674 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1675 Instruction *InsertBefore) {
1676 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1677 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1680 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1681 BasicBlock *InsertAtEnd) {
1682 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1683 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1686 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1687 Instruction *InsertBefore) {
1688 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1689 return new BinaryOperator(Instruction::FSub,
1691 Op->getType(), Name, InsertBefore);
1694 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1695 BasicBlock *InsertAtEnd) {
1696 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1697 return new BinaryOperator(Instruction::FSub,
1699 Op->getType(), Name, InsertAtEnd);
1702 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1703 Instruction *InsertBefore) {
1705 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1706 C = Constant::getAllOnesValue(PTy->getElementType());
1707 C = ConstantVector::get(
1708 std::vector<Constant*>(PTy->getNumElements(), C));
1710 C = Constant::getAllOnesValue(Op->getType());
1713 return new BinaryOperator(Instruction::Xor, Op, C,
1714 Op->getType(), Name, InsertBefore);
1717 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1718 BasicBlock *InsertAtEnd) {
1720 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1721 // Create a vector of all ones values.
1722 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
1723 AllOnes = ConstantVector::get(
1724 std::vector<Constant*>(PTy->getNumElements(), Elt));
1726 AllOnes = Constant::getAllOnesValue(Op->getType());
1729 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1730 Op->getType(), Name, InsertAtEnd);
1734 // isConstantAllOnes - Helper function for several functions below
1735 static inline bool isConstantAllOnes(const Value *V) {
1736 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1737 return CI->isAllOnesValue();
1738 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1739 return CV->isAllOnesValue();
1743 bool BinaryOperator::isNeg(const Value *V) {
1744 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1745 if (Bop->getOpcode() == Instruction::Sub)
1746 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1747 return C->isNegativeZeroValue();
1751 bool BinaryOperator::isFNeg(const Value *V) {
1752 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1753 if (Bop->getOpcode() == Instruction::FSub)
1754 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1755 return C->isNegativeZeroValue();
1759 bool BinaryOperator::isNot(const Value *V) {
1760 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1761 return (Bop->getOpcode() == Instruction::Xor &&
1762 (isConstantAllOnes(Bop->getOperand(1)) ||
1763 isConstantAllOnes(Bop->getOperand(0))));
1767 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1768 return cast<BinaryOperator>(BinOp)->getOperand(1);
1771 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1772 return getNegArgument(const_cast<Value*>(BinOp));
1775 Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1776 return cast<BinaryOperator>(BinOp)->getOperand(1);
1779 const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1780 return getFNegArgument(const_cast<Value*>(BinOp));
1783 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1784 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1785 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1786 Value *Op0 = BO->getOperand(0);
1787 Value *Op1 = BO->getOperand(1);
1788 if (isConstantAllOnes(Op0)) return Op1;
1790 assert(isConstantAllOnes(Op1));
1794 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1795 return getNotArgument(const_cast<Value*>(BinOp));
1799 // swapOperands - Exchange the two operands to this instruction. This
1800 // instruction is safe to use on any binary instruction and does not
1801 // modify the semantics of the instruction. If the instruction is
1802 // order dependent (SetLT f.e.) the opcode is changed.
1804 bool BinaryOperator::swapOperands() {
1805 if (!isCommutative())
1806 return true; // Can't commute operands
1807 Op<0>().swap(Op<1>());
1811 void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1812 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1815 void BinaryOperator::setHasNoSignedWrap(bool b) {
1816 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1819 void BinaryOperator::setIsExact(bool b) {
1820 cast<SDivOperator>(this)->setIsExact(b);
1823 bool BinaryOperator::hasNoUnsignedWrap() const {
1824 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1827 bool BinaryOperator::hasNoSignedWrap() const {
1828 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1831 bool BinaryOperator::isExact() const {
1832 return cast<SDivOperator>(this)->isExact();
1835 //===----------------------------------------------------------------------===//
1837 //===----------------------------------------------------------------------===//
1839 // Just determine if this cast only deals with integral->integral conversion.
1840 bool CastInst::isIntegerCast() const {
1841 switch (getOpcode()) {
1842 default: return false;
1843 case Instruction::ZExt:
1844 case Instruction::SExt:
1845 case Instruction::Trunc:
1847 case Instruction::BitCast:
1848 return getOperand(0)->getType()->isIntegerTy() &&
1849 getType()->isIntegerTy();
1853 bool CastInst::isLosslessCast() const {
1854 // Only BitCast can be lossless, exit fast if we're not BitCast
1855 if (getOpcode() != Instruction::BitCast)
1858 // Identity cast is always lossless
1859 const Type* SrcTy = getOperand(0)->getType();
1860 const Type* DstTy = getType();
1864 // Pointer to pointer is always lossless.
1865 if (SrcTy->isPointerTy())
1866 return DstTy->isPointerTy();
1867 return false; // Other types have no identity values
1870 /// This function determines if the CastInst does not require any bits to be
1871 /// changed in order to effect the cast. Essentially, it identifies cases where
1872 /// no code gen is necessary for the cast, hence the name no-op cast. For
1873 /// example, the following are all no-op casts:
1874 /// # bitcast i32* %x to i8*
1875 /// # bitcast <2 x i32> %x to <4 x i16>
1876 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1877 /// @brief Determine if the described cast is a no-op.
1878 bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1881 const Type *IntPtrTy) {
1884 assert(!"Invalid CastOp");
1885 case Instruction::Trunc:
1886 case Instruction::ZExt:
1887 case Instruction::SExt:
1888 case Instruction::FPTrunc:
1889 case Instruction::FPExt:
1890 case Instruction::UIToFP:
1891 case Instruction::SIToFP:
1892 case Instruction::FPToUI:
1893 case Instruction::FPToSI:
1894 return false; // These always modify bits
1895 case Instruction::BitCast:
1896 return true; // BitCast never modifies bits.
1897 case Instruction::PtrToInt:
1898 return IntPtrTy->getScalarSizeInBits() ==
1899 DestTy->getScalarSizeInBits();
1900 case Instruction::IntToPtr:
1901 return IntPtrTy->getScalarSizeInBits() ==
1902 SrcTy->getScalarSizeInBits();
1906 /// @brief Determine if a cast is a no-op.
1907 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1908 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1911 /// This function determines if a pair of casts can be eliminated and what
1912 /// opcode should be used in the elimination. This assumes that there are two
1913 /// instructions like this:
1914 /// * %F = firstOpcode SrcTy %x to MidTy
1915 /// * %S = secondOpcode MidTy %F to DstTy
1916 /// The function returns a resultOpcode so these two casts can be replaced with:
1917 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1918 /// If no such cast is permited, the function returns 0.
1919 unsigned CastInst::isEliminableCastPair(
1920 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1921 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1923 // Define the 144 possibilities for these two cast instructions. The values
1924 // in this matrix determine what to do in a given situation and select the
1925 // case in the switch below. The rows correspond to firstOp, the columns
1926 // correspond to secondOp. In looking at the table below, keep in mind
1927 // the following cast properties:
1929 // Size Compare Source Destination
1930 // Operator Src ? Size Type Sign Type Sign
1931 // -------- ------------ ------------------- ---------------------
1932 // TRUNC > Integer Any Integral Any
1933 // ZEXT < Integral Unsigned Integer Any
1934 // SEXT < Integral Signed Integer Any
1935 // FPTOUI n/a FloatPt n/a Integral Unsigned
1936 // FPTOSI n/a FloatPt n/a Integral Signed
1937 // UITOFP n/a Integral Unsigned FloatPt n/a
1938 // SITOFP n/a Integral Signed FloatPt n/a
1939 // FPTRUNC > FloatPt n/a FloatPt n/a
1940 // FPEXT < FloatPt n/a FloatPt n/a
1941 // PTRTOINT n/a Pointer n/a Integral Unsigned
1942 // INTTOPTR n/a Integral Unsigned Pointer n/a
1943 // BITCAST = FirstClass n/a FirstClass n/a
1945 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1946 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1947 // into "fptoui double to i64", but this loses information about the range
1948 // of the produced value (we no longer know the top-part is all zeros).
1949 // Further this conversion is often much more expensive for typical hardware,
1950 // and causes issues when building libgcc. We disallow fptosi+sext for the
1952 const unsigned numCastOps =
1953 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1954 static const uint8_t CastResults[numCastOps][numCastOps] = {
1955 // T F F U S F F P I B -+
1956 // R Z S P P I I T P 2 N T |
1957 // U E E 2 2 2 2 R E I T C +- secondOp
1958 // N X X U S F F N X N 2 V |
1959 // C T T I I P P C T T P T -+
1960 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1961 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1962 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
1963 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1964 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
1965 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1966 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1967 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1968 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1969 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1970 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1971 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1974 // If either of the casts are a bitcast from scalar to vector, disallow the
1976 if ((firstOp == Instruction::BitCast &&
1977 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
1978 (secondOp == Instruction::BitCast &&
1979 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
1980 return 0; // Disallowed
1982 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1983 [secondOp-Instruction::CastOpsBegin];
1986 // categorically disallowed
1989 // allowed, use first cast's opcode
1992 // allowed, use second cast's opcode
1995 // no-op cast in second op implies firstOp as long as the DestTy
1996 // is integer and we are not converting between a vector and a
1998 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
2002 // no-op cast in second op implies firstOp as long as the DestTy
2003 // is floating point.
2004 if (DstTy->isFloatingPointTy())
2008 // no-op cast in first op implies secondOp as long as the SrcTy
2010 if (SrcTy->isIntegerTy())
2014 // no-op cast in first op implies secondOp as long as the SrcTy
2015 // is a floating point.
2016 if (SrcTy->isFloatingPointTy())
2020 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
2023 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2024 unsigned MidSize = MidTy->getScalarSizeInBits();
2025 if (MidSize >= PtrSize)
2026 return Instruction::BitCast;
2030 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2031 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2032 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2033 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2034 unsigned DstSize = DstTy->getScalarSizeInBits();
2035 if (SrcSize == DstSize)
2036 return Instruction::BitCast;
2037 else if (SrcSize < DstSize)
2041 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2042 return Instruction::ZExt;
2044 // fpext followed by ftrunc is allowed if the bit size returned to is
2045 // the same as the original, in which case its just a bitcast
2047 return Instruction::BitCast;
2048 return 0; // If the types are not the same we can't eliminate it.
2050 // bitcast followed by ptrtoint is allowed as long as the bitcast
2051 // is a pointer to pointer cast.
2052 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
2056 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2057 if (MidTy->isPointerTy() && DstTy->isPointerTy())
2061 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2064 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2065 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2066 unsigned DstSize = DstTy->getScalarSizeInBits();
2067 if (SrcSize <= PtrSize && SrcSize == DstSize)
2068 return Instruction::BitCast;
2072 // cast combination can't happen (error in input). This is for all cases
2073 // where the MidTy is not the same for the two cast instructions.
2074 assert(!"Invalid Cast Combination");
2077 assert(!"Error in CastResults table!!!");
2083 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2084 const Twine &Name, Instruction *InsertBefore) {
2085 // Construct and return the appropriate CastInst subclass
2087 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2088 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2089 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2090 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2091 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2092 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2093 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2094 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2095 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2096 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2097 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2098 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2100 assert(!"Invalid opcode provided");
2105 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2106 const Twine &Name, BasicBlock *InsertAtEnd) {
2107 // Construct and return the appropriate CastInst subclass
2109 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2110 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2111 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2112 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2113 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2114 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2115 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2116 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2117 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2118 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2119 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2120 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2122 assert(!"Invalid opcode provided");
2127 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2129 Instruction *InsertBefore) {
2130 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2131 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2132 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
2135 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2137 BasicBlock *InsertAtEnd) {
2138 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2139 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2140 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
2143 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2145 Instruction *InsertBefore) {
2146 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2147 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2148 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
2151 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2153 BasicBlock *InsertAtEnd) {
2154 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2155 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2156 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
2159 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2161 Instruction *InsertBefore) {
2162 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2163 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2164 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
2167 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2169 BasicBlock *InsertAtEnd) {
2170 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2171 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2172 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
2175 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2177 BasicBlock *InsertAtEnd) {
2178 assert(S->getType()->isPointerTy() && "Invalid cast");
2179 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2182 if (Ty->isIntegerTy())
2183 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2184 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2187 /// @brief Create a BitCast or a PtrToInt cast instruction
2188 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2190 Instruction *InsertBefore) {
2191 assert(S->getType()->isPointerTy() && "Invalid cast");
2192 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2195 if (Ty->isIntegerTy())
2196 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2197 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2200 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2201 bool isSigned, const Twine &Name,
2202 Instruction *InsertBefore) {
2203 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2204 "Invalid integer cast");
2205 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2206 unsigned DstBits = Ty->getScalarSizeInBits();
2207 Instruction::CastOps opcode =
2208 (SrcBits == DstBits ? Instruction::BitCast :
2209 (SrcBits > DstBits ? Instruction::Trunc :
2210 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2211 return Create(opcode, C, Ty, Name, InsertBefore);
2214 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2215 bool isSigned, const Twine &Name,
2216 BasicBlock *InsertAtEnd) {
2217 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2219 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2220 unsigned DstBits = Ty->getScalarSizeInBits();
2221 Instruction::CastOps opcode =
2222 (SrcBits == DstBits ? Instruction::BitCast :
2223 (SrcBits > DstBits ? Instruction::Trunc :
2224 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2225 return Create(opcode, C, Ty, Name, InsertAtEnd);
2228 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2230 Instruction *InsertBefore) {
2231 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2233 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2234 unsigned DstBits = Ty->getScalarSizeInBits();
2235 Instruction::CastOps opcode =
2236 (SrcBits == DstBits ? Instruction::BitCast :
2237 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2238 return Create(opcode, C, Ty, Name, InsertBefore);
2241 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2243 BasicBlock *InsertAtEnd) {
2244 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2246 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2247 unsigned DstBits = Ty->getScalarSizeInBits();
2248 Instruction::CastOps opcode =
2249 (SrcBits == DstBits ? Instruction::BitCast :
2250 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2251 return Create(opcode, C, Ty, Name, InsertAtEnd);
2254 // Check whether it is valid to call getCastOpcode for these types.
2255 // This routine must be kept in sync with getCastOpcode.
2256 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2257 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2260 if (SrcTy == DestTy)
2263 // Get the bit sizes, we'll need these
2264 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2265 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2267 // Run through the possibilities ...
2268 if (DestTy->isIntegerTy()) { // Casting to integral
2269 if (SrcTy->isIntegerTy()) { // Casting from integral
2271 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2273 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2274 // Casting from vector
2275 return DestBits == PTy->getBitWidth();
2276 } else { // Casting from something else
2277 return SrcTy->isPointerTy();
2279 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2280 if (SrcTy->isIntegerTy()) { // Casting from integral
2282 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2284 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2285 // Casting from vector
2286 return DestBits == PTy->getBitWidth();
2287 } else { // Casting from something else
2290 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2291 // Casting to vector
2292 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2293 // Casting from vector
2294 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2295 } else { // Casting from something else
2296 return DestPTy->getBitWidth() == SrcBits;
2298 } else if (DestTy->isPointerTy()) { // Casting to pointer
2299 if (SrcTy->isPointerTy()) { // Casting from pointer
2301 } else if (SrcTy->isIntegerTy()) { // Casting from integral
2303 } else { // Casting from something else
2306 } else if (DestTy->isX86_MMXTy()) {
2307 return SrcBits == 64;
2308 } else { // Casting to something else
2313 // Provide a way to get a "cast" where the cast opcode is inferred from the
2314 // types and size of the operand. This, basically, is a parallel of the
2315 // logic in the castIsValid function below. This axiom should hold:
2316 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2317 // should not assert in castIsValid. In other words, this produces a "correct"
2318 // casting opcode for the arguments passed to it.
2319 // This routine must be kept in sync with isCastable.
2320 Instruction::CastOps
2321 CastInst::getCastOpcode(
2322 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
2323 // Get the bit sizes, we'll need these
2324 const Type *SrcTy = Src->getType();
2325 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2326 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2328 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2329 "Only first class types are castable!");
2331 // Run through the possibilities ...
2332 if (DestTy->isIntegerTy()) { // Casting to integral
2333 if (SrcTy->isIntegerTy()) { // Casting from integral
2334 if (DestBits < SrcBits)
2335 return Trunc; // int -> smaller int
2336 else if (DestBits > SrcBits) { // its an extension
2338 return SExt; // signed -> SEXT
2340 return ZExt; // unsigned -> ZEXT
2342 return BitCast; // Same size, No-op cast
2344 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2346 return FPToSI; // FP -> sint
2348 return FPToUI; // FP -> uint
2349 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2350 assert(DestBits == PTy->getBitWidth() &&
2351 "Casting vector to integer of different width");
2353 return BitCast; // Same size, no-op cast
2355 assert(SrcTy->isPointerTy() &&
2356 "Casting from a value that is not first-class type");
2357 return PtrToInt; // ptr -> int
2359 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2360 if (SrcTy->isIntegerTy()) { // Casting from integral
2362 return SIToFP; // sint -> FP
2364 return UIToFP; // uint -> FP
2365 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2366 if (DestBits < SrcBits) {
2367 return FPTrunc; // FP -> smaller FP
2368 } else if (DestBits > SrcBits) {
2369 return FPExt; // FP -> larger FP
2371 return BitCast; // same size, no-op cast
2373 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2374 assert(DestBits == PTy->getBitWidth() &&
2375 "Casting vector to floating point of different width");
2377 return BitCast; // same size, no-op cast
2379 llvm_unreachable("Casting pointer or non-first class to float");
2381 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2382 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2383 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
2384 "Casting vector to vector of different widths");
2386 return BitCast; // vector -> vector
2387 } else if (DestPTy->getBitWidth() == SrcBits) {
2388 return BitCast; // float/int -> vector
2389 } else if (SrcTy->isX86_MMXTy()) {
2390 assert(DestPTy->getBitWidth()==64 &&
2391 "Casting X86_MMX to vector of wrong width");
2392 return BitCast; // MMX to 64-bit vector
2394 assert(!"Illegal cast to vector (wrong type or size)");
2396 } else if (DestTy->isPointerTy()) {
2397 if (SrcTy->isPointerTy()) {
2398 return BitCast; // ptr -> ptr
2399 } else if (SrcTy->isIntegerTy()) {
2400 return IntToPtr; // int -> ptr
2402 assert(!"Casting pointer to other than pointer or int");
2404 } else if (DestTy->isX86_MMXTy()) {
2405 if (isa<VectorType>(SrcTy)) {
2406 assert(cast<VectorType>(SrcTy)->getBitWidth() == 64 &&
2407 "Casting vector of wrong width to X86_MMX");
2408 return BitCast; // 64-bit vector to MMX
2410 assert(!"Illegal cast to X86_MMX");
2413 assert(!"Casting to type that is not first-class");
2416 // If we fall through to here we probably hit an assertion cast above
2417 // and assertions are not turned on. Anything we return is an error, so
2418 // BitCast is as good a choice as any.
2422 //===----------------------------------------------------------------------===//
2423 // CastInst SubClass Constructors
2424 //===----------------------------------------------------------------------===//
2426 /// Check that the construction parameters for a CastInst are correct. This
2427 /// could be broken out into the separate constructors but it is useful to have
2428 /// it in one place and to eliminate the redundant code for getting the sizes
2429 /// of the types involved.
2431 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
2433 // Check for type sanity on the arguments
2434 const Type *SrcTy = S->getType();
2435 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2436 SrcTy->isAggregateType() || DstTy->isAggregateType())
2439 // Get the size of the types in bits, we'll need this later
2440 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2441 unsigned DstBitSize = DstTy->getScalarSizeInBits();
2443 // Switch on the opcode provided
2445 default: return false; // This is an input error
2446 case Instruction::Trunc:
2447 return SrcTy->isIntOrIntVectorTy() &&
2448 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
2449 case Instruction::ZExt:
2450 return SrcTy->isIntOrIntVectorTy() &&
2451 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2452 case Instruction::SExt:
2453 return SrcTy->isIntOrIntVectorTy() &&
2454 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2455 case Instruction::FPTrunc:
2456 return SrcTy->isFPOrFPVectorTy() &&
2457 DstTy->isFPOrFPVectorTy() &&
2458 SrcBitSize > DstBitSize;
2459 case Instruction::FPExt:
2460 return SrcTy->isFPOrFPVectorTy() &&
2461 DstTy->isFPOrFPVectorTy() &&
2462 SrcBitSize < DstBitSize;
2463 case Instruction::UIToFP:
2464 case Instruction::SIToFP:
2465 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2466 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2467 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2468 DVTy->getElementType()->isFPOrFPVectorTy() &&
2469 SVTy->getNumElements() == DVTy->getNumElements();
2472 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
2473 case Instruction::FPToUI:
2474 case Instruction::FPToSI:
2475 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2476 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2477 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2478 DVTy->getElementType()->isIntOrIntVectorTy() &&
2479 SVTy->getNumElements() == DVTy->getNumElements();
2482 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
2483 case Instruction::PtrToInt:
2484 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
2485 case Instruction::IntToPtr:
2486 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
2487 case Instruction::BitCast:
2488 // BitCast implies a no-op cast of type only. No bits change.
2489 // However, you can't cast pointers to anything but pointers.
2490 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
2493 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2494 // these cases, the cast is okay if the source and destination bit widths
2496 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2500 TruncInst::TruncInst(
2501 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2502 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2503 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2506 TruncInst::TruncInst(
2507 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2508 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2509 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2513 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2514 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2515 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2519 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2520 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2521 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2524 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2525 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2526 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2530 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2531 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2532 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2535 FPTruncInst::FPTruncInst(
2536 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2537 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2538 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2541 FPTruncInst::FPTruncInst(
2542 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2543 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2544 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2547 FPExtInst::FPExtInst(
2548 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2549 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2550 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2553 FPExtInst::FPExtInst(
2554 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2555 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2556 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2559 UIToFPInst::UIToFPInst(
2560 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2561 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2562 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2565 UIToFPInst::UIToFPInst(
2566 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2567 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2568 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2571 SIToFPInst::SIToFPInst(
2572 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2573 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2574 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2577 SIToFPInst::SIToFPInst(
2578 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2579 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2580 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2583 FPToUIInst::FPToUIInst(
2584 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2585 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2586 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2589 FPToUIInst::FPToUIInst(
2590 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2591 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2592 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2595 FPToSIInst::FPToSIInst(
2596 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2597 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2598 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2601 FPToSIInst::FPToSIInst(
2602 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2603 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2604 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2607 PtrToIntInst::PtrToIntInst(
2608 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2609 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2610 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2613 PtrToIntInst::PtrToIntInst(
2614 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2615 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2616 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2619 IntToPtrInst::IntToPtrInst(
2620 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2621 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2622 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2625 IntToPtrInst::IntToPtrInst(
2626 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2627 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2628 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2631 BitCastInst::BitCastInst(
2632 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2633 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2634 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2637 BitCastInst::BitCastInst(
2638 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2639 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2640 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2643 //===----------------------------------------------------------------------===//
2645 //===----------------------------------------------------------------------===//
2647 void CmpInst::Anchor() const {}
2649 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2650 Value *LHS, Value *RHS, const Twine &Name,
2651 Instruction *InsertBefore)
2652 : Instruction(ty, op,
2653 OperandTraits<CmpInst>::op_begin(this),
2654 OperandTraits<CmpInst>::operands(this),
2658 setPredicate((Predicate)predicate);
2662 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2663 Value *LHS, Value *RHS, const Twine &Name,
2664 BasicBlock *InsertAtEnd)
2665 : Instruction(ty, op,
2666 OperandTraits<CmpInst>::op_begin(this),
2667 OperandTraits<CmpInst>::operands(this),
2671 setPredicate((Predicate)predicate);
2676 CmpInst::Create(OtherOps Op, unsigned short predicate,
2677 Value *S1, Value *S2,
2678 const Twine &Name, Instruction *InsertBefore) {
2679 if (Op == Instruction::ICmp) {
2681 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2684 return new ICmpInst(CmpInst::Predicate(predicate),
2689 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2692 return new FCmpInst(CmpInst::Predicate(predicate),
2697 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2698 const Twine &Name, BasicBlock *InsertAtEnd) {
2699 if (Op == Instruction::ICmp) {
2700 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2703 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2707 void CmpInst::swapOperands() {
2708 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2711 cast<FCmpInst>(this)->swapOperands();
2714 bool CmpInst::isCommutative() const {
2715 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
2716 return IC->isCommutative();
2717 return cast<FCmpInst>(this)->isCommutative();
2720 bool CmpInst::isEquality() const {
2721 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
2722 return IC->isEquality();
2723 return cast<FCmpInst>(this)->isEquality();
2727 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
2729 default: assert(!"Unknown cmp predicate!");
2730 case ICMP_EQ: return ICMP_NE;
2731 case ICMP_NE: return ICMP_EQ;
2732 case ICMP_UGT: return ICMP_ULE;
2733 case ICMP_ULT: return ICMP_UGE;
2734 case ICMP_UGE: return ICMP_ULT;
2735 case ICMP_ULE: return ICMP_UGT;
2736 case ICMP_SGT: return ICMP_SLE;
2737 case ICMP_SLT: return ICMP_SGE;
2738 case ICMP_SGE: return ICMP_SLT;
2739 case ICMP_SLE: return ICMP_SGT;
2741 case FCMP_OEQ: return FCMP_UNE;
2742 case FCMP_ONE: return FCMP_UEQ;
2743 case FCMP_OGT: return FCMP_ULE;
2744 case FCMP_OLT: return FCMP_UGE;
2745 case FCMP_OGE: return FCMP_ULT;
2746 case FCMP_OLE: return FCMP_UGT;
2747 case FCMP_UEQ: return FCMP_ONE;
2748 case FCMP_UNE: return FCMP_OEQ;
2749 case FCMP_UGT: return FCMP_OLE;
2750 case FCMP_ULT: return FCMP_OGE;
2751 case FCMP_UGE: return FCMP_OLT;
2752 case FCMP_ULE: return FCMP_OGT;
2753 case FCMP_ORD: return FCMP_UNO;
2754 case FCMP_UNO: return FCMP_ORD;
2755 case FCMP_TRUE: return FCMP_FALSE;
2756 case FCMP_FALSE: return FCMP_TRUE;
2760 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2762 default: assert(! "Unknown icmp predicate!");
2763 case ICMP_EQ: case ICMP_NE:
2764 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2766 case ICMP_UGT: return ICMP_SGT;
2767 case ICMP_ULT: return ICMP_SLT;
2768 case ICMP_UGE: return ICMP_SGE;
2769 case ICMP_ULE: return ICMP_SLE;
2773 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2775 default: assert(! "Unknown icmp predicate!");
2776 case ICMP_EQ: case ICMP_NE:
2777 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2779 case ICMP_SGT: return ICMP_UGT;
2780 case ICMP_SLT: return ICMP_ULT;
2781 case ICMP_SGE: return ICMP_UGE;
2782 case ICMP_SLE: return ICMP_ULE;
2786 /// Initialize a set of values that all satisfy the condition with C.
2789 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2792 uint32_t BitWidth = C.getBitWidth();
2794 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
2795 case ICmpInst::ICMP_EQ: Upper++; break;
2796 case ICmpInst::ICMP_NE: Lower++; break;
2797 case ICmpInst::ICMP_ULT:
2798 Lower = APInt::getMinValue(BitWidth);
2799 // Check for an empty-set condition.
2801 return ConstantRange(BitWidth, /*isFullSet=*/false);
2803 case ICmpInst::ICMP_SLT:
2804 Lower = APInt::getSignedMinValue(BitWidth);
2805 // Check for an empty-set condition.
2807 return ConstantRange(BitWidth, /*isFullSet=*/false);
2809 case ICmpInst::ICMP_UGT:
2810 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2811 // Check for an empty-set condition.
2813 return ConstantRange(BitWidth, /*isFullSet=*/false);
2815 case ICmpInst::ICMP_SGT:
2816 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2817 // Check for an empty-set condition.
2819 return ConstantRange(BitWidth, /*isFullSet=*/false);
2821 case ICmpInst::ICMP_ULE:
2822 Lower = APInt::getMinValue(BitWidth); Upper++;
2823 // Check for a full-set condition.
2825 return ConstantRange(BitWidth, /*isFullSet=*/true);
2827 case ICmpInst::ICMP_SLE:
2828 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2829 // Check for a full-set condition.
2831 return ConstantRange(BitWidth, /*isFullSet=*/true);
2833 case ICmpInst::ICMP_UGE:
2834 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2835 // Check for a full-set condition.
2837 return ConstantRange(BitWidth, /*isFullSet=*/true);
2839 case ICmpInst::ICMP_SGE:
2840 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2841 // Check for a full-set condition.
2843 return ConstantRange(BitWidth, /*isFullSet=*/true);
2846 return ConstantRange(Lower, Upper);
2849 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
2851 default: assert(!"Unknown cmp predicate!");
2852 case ICMP_EQ: case ICMP_NE:
2854 case ICMP_SGT: return ICMP_SLT;
2855 case ICMP_SLT: return ICMP_SGT;
2856 case ICMP_SGE: return ICMP_SLE;
2857 case ICMP_SLE: return ICMP_SGE;
2858 case ICMP_UGT: return ICMP_ULT;
2859 case ICMP_ULT: return ICMP_UGT;
2860 case ICMP_UGE: return ICMP_ULE;
2861 case ICMP_ULE: return ICMP_UGE;
2863 case FCMP_FALSE: case FCMP_TRUE:
2864 case FCMP_OEQ: case FCMP_ONE:
2865 case FCMP_UEQ: case FCMP_UNE:
2866 case FCMP_ORD: case FCMP_UNO:
2868 case FCMP_OGT: return FCMP_OLT;
2869 case FCMP_OLT: return FCMP_OGT;
2870 case FCMP_OGE: return FCMP_OLE;
2871 case FCMP_OLE: return FCMP_OGE;
2872 case FCMP_UGT: return FCMP_ULT;
2873 case FCMP_ULT: return FCMP_UGT;
2874 case FCMP_UGE: return FCMP_ULE;
2875 case FCMP_ULE: return FCMP_UGE;
2879 bool CmpInst::isUnsigned(unsigned short predicate) {
2880 switch (predicate) {
2881 default: return false;
2882 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2883 case ICmpInst::ICMP_UGE: return true;
2887 bool CmpInst::isSigned(unsigned short predicate) {
2888 switch (predicate) {
2889 default: return false;
2890 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2891 case ICmpInst::ICMP_SGE: return true;
2895 bool CmpInst::isOrdered(unsigned short predicate) {
2896 switch (predicate) {
2897 default: return false;
2898 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2899 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2900 case FCmpInst::FCMP_ORD: return true;
2904 bool CmpInst::isUnordered(unsigned short predicate) {
2905 switch (predicate) {
2906 default: return false;
2907 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2908 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2909 case FCmpInst::FCMP_UNO: return true;
2913 bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2915 default: return false;
2916 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2917 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2921 bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2923 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2924 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2925 default: return false;
2930 //===----------------------------------------------------------------------===//
2931 // SwitchInst Implementation
2932 //===----------------------------------------------------------------------===//
2934 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
2935 assert(Value && Default && NumReserved);
2936 ReservedSpace = NumReserved;
2938 OperandList = allocHungoffUses(ReservedSpace);
2940 OperandList[0] = Value;
2941 OperandList[1] = Default;
2944 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2945 /// switch on and a default destination. The number of additional cases can
2946 /// be specified here to make memory allocation more efficient. This
2947 /// constructor can also autoinsert before another instruction.
2948 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2949 Instruction *InsertBefore)
2950 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2951 0, 0, InsertBefore) {
2952 init(Value, Default, 2+NumCases*2);
2955 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2956 /// switch on and a default destination. The number of additional cases can
2957 /// be specified here to make memory allocation more efficient. This
2958 /// constructor also autoinserts at the end of the specified BasicBlock.
2959 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2960 BasicBlock *InsertAtEnd)
2961 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2962 0, 0, InsertAtEnd) {
2963 init(Value, Default, 2+NumCases*2);
2966 SwitchInst::SwitchInst(const SwitchInst &SI)
2967 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
2968 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
2969 NumOperands = SI.getNumOperands();
2970 Use *OL = OperandList, *InOL = SI.OperandList;
2971 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
2973 OL[i+1] = InOL[i+1];
2975 SubclassOptionalData = SI.SubclassOptionalData;
2978 SwitchInst::~SwitchInst() {
2979 dropHungoffUses(OperandList);
2983 /// addCase - Add an entry to the switch instruction...
2985 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
2986 unsigned OpNo = NumOperands;
2987 if (OpNo+2 > ReservedSpace)
2988 resizeOperands(0); // Get more space!
2989 // Initialize some new operands.
2990 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
2991 NumOperands = OpNo+2;
2992 OperandList[OpNo] = OnVal;
2993 OperandList[OpNo+1] = Dest;
2996 /// removeCase - This method removes the specified successor from the switch
2997 /// instruction. Note that this cannot be used to remove the default
2998 /// destination (successor #0).
3000 void SwitchInst::removeCase(unsigned idx) {
3001 assert(idx != 0 && "Cannot remove the default case!");
3002 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3004 unsigned NumOps = getNumOperands();
3005 Use *OL = OperandList;
3007 // Move everything after this operand down.
3009 // FIXME: we could just swap with the end of the list, then erase. However,
3010 // client might not expect this to happen. The code as it is thrashes the
3011 // use/def lists, which is kinda lame.
3012 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3014 OL[i-2+1] = OL[i+1];
3017 // Nuke the last value.
3018 OL[NumOps-2].set(0);
3019 OL[NumOps-2+1].set(0);
3020 NumOperands = NumOps-2;
3023 /// resizeOperands - resize operands - This adjusts the length of the operands
3024 /// list according to the following behavior:
3025 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3026 /// of operation. This grows the number of ops by 3 times.
3027 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3028 /// 3. If NumOps == NumOperands, trim the reserved space.
3030 void SwitchInst::resizeOperands(unsigned NumOps) {
3031 unsigned e = getNumOperands();
3034 } else if (NumOps*2 > NumOperands) {
3035 // No resize needed.
3036 if (ReservedSpace >= NumOps) return;
3037 } else if (NumOps == NumOperands) {
3038 if (ReservedSpace == NumOps) return;
3043 ReservedSpace = NumOps;
3044 Use *NewOps = allocHungoffUses(NumOps);
3045 Use *OldOps = OperandList;
3046 for (unsigned i = 0; i != e; ++i) {
3047 NewOps[i] = OldOps[i];
3049 OperandList = NewOps;
3050 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3054 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3055 return getSuccessor(idx);
3057 unsigned SwitchInst::getNumSuccessorsV() const {
3058 return getNumSuccessors();
3060 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3061 setSuccessor(idx, B);
3064 //===----------------------------------------------------------------------===//
3065 // SwitchInst Implementation
3066 //===----------------------------------------------------------------------===//
3068 void IndirectBrInst::init(Value *Address, unsigned NumDests) {
3069 assert(Address && Address->getType()->isPointerTy() &&
3070 "Address of indirectbr must be a pointer");
3071 ReservedSpace = 1+NumDests;
3073 OperandList = allocHungoffUses(ReservedSpace);
3075 OperandList[0] = Address;
3079 /// resizeOperands - resize operands - This adjusts the length of the operands
3080 /// list according to the following behavior:
3081 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3082 /// of operation. This grows the number of ops by 2 times.
3083 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3084 /// 3. If NumOps == NumOperands, trim the reserved space.
3086 void IndirectBrInst::resizeOperands(unsigned NumOps) {
3087 unsigned e = getNumOperands();
3090 } else if (NumOps*2 > NumOperands) {
3091 // No resize needed.
3092 if (ReservedSpace >= NumOps) return;
3093 } else if (NumOps == NumOperands) {
3094 if (ReservedSpace == NumOps) return;
3099 ReservedSpace = NumOps;
3100 Use *NewOps = allocHungoffUses(NumOps);
3101 Use *OldOps = OperandList;
3102 for (unsigned i = 0; i != e; ++i)
3103 NewOps[i] = OldOps[i];
3104 OperandList = NewOps;
3105 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3108 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3109 Instruction *InsertBefore)
3110 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3111 0, 0, InsertBefore) {
3112 init(Address, NumCases);
3115 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3116 BasicBlock *InsertAtEnd)
3117 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3118 0, 0, InsertAtEnd) {
3119 init(Address, NumCases);
3122 IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3123 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3124 allocHungoffUses(IBI.getNumOperands()),
3125 IBI.getNumOperands()) {
3126 Use *OL = OperandList, *InOL = IBI.OperandList;
3127 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3129 SubclassOptionalData = IBI.SubclassOptionalData;
3132 IndirectBrInst::~IndirectBrInst() {
3133 dropHungoffUses(OperandList);
3136 /// addDestination - Add a destination.
3138 void IndirectBrInst::addDestination(BasicBlock *DestBB) {
3139 unsigned OpNo = NumOperands;
3140 if (OpNo+1 > ReservedSpace)
3141 resizeOperands(0); // Get more space!
3142 // Initialize some new operands.
3143 assert(OpNo < ReservedSpace && "Growing didn't work!");
3144 NumOperands = OpNo+1;
3145 OperandList[OpNo] = DestBB;
3148 /// removeDestination - This method removes the specified successor from the
3149 /// indirectbr instruction.
3150 void IndirectBrInst::removeDestination(unsigned idx) {
3151 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3153 unsigned NumOps = getNumOperands();
3154 Use *OL = OperandList;
3156 // Replace this value with the last one.
3157 OL[idx+1] = OL[NumOps-1];
3159 // Nuke the last value.
3160 OL[NumOps-1].set(0);
3161 NumOperands = NumOps-1;
3164 BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
3165 return getSuccessor(idx);
3167 unsigned IndirectBrInst::getNumSuccessorsV() const {
3168 return getNumSuccessors();
3170 void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3171 setSuccessor(idx, B);
3174 //===----------------------------------------------------------------------===//
3175 // clone_impl() implementations
3176 //===----------------------------------------------------------------------===//
3178 // Define these methods here so vtables don't get emitted into every translation
3179 // unit that uses these classes.
3181 GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3182 return new (getNumOperands()) GetElementPtrInst(*this);
3185 BinaryOperator *BinaryOperator::clone_impl() const {
3186 return Create(getOpcode(), Op<0>(), Op<1>());
3189 FCmpInst* FCmpInst::clone_impl() const {
3190 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
3193 ICmpInst* ICmpInst::clone_impl() const {
3194 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
3197 ExtractValueInst *ExtractValueInst::clone_impl() const {
3198 return new ExtractValueInst(*this);
3201 InsertValueInst *InsertValueInst::clone_impl() const {
3202 return new InsertValueInst(*this);
3205 AllocaInst *AllocaInst::clone_impl() const {
3206 return new AllocaInst(getAllocatedType(),
3207 (Value*)getOperand(0),
3211 LoadInst *LoadInst::clone_impl() const {
3212 return new LoadInst(getOperand(0),
3213 Twine(), isVolatile(),
3217 StoreInst *StoreInst::clone_impl() const {
3218 return new StoreInst(getOperand(0), getOperand(1),
3219 isVolatile(), getAlignment());
3222 TruncInst *TruncInst::clone_impl() const {
3223 return new TruncInst(getOperand(0), getType());
3226 ZExtInst *ZExtInst::clone_impl() const {
3227 return new ZExtInst(getOperand(0), getType());
3230 SExtInst *SExtInst::clone_impl() const {
3231 return new SExtInst(getOperand(0), getType());
3234 FPTruncInst *FPTruncInst::clone_impl() const {
3235 return new FPTruncInst(getOperand(0), getType());
3238 FPExtInst *FPExtInst::clone_impl() const {
3239 return new FPExtInst(getOperand(0), getType());
3242 UIToFPInst *UIToFPInst::clone_impl() const {
3243 return new UIToFPInst(getOperand(0), getType());
3246 SIToFPInst *SIToFPInst::clone_impl() const {
3247 return new SIToFPInst(getOperand(0), getType());
3250 FPToUIInst *FPToUIInst::clone_impl() const {
3251 return new FPToUIInst(getOperand(0), getType());
3254 FPToSIInst *FPToSIInst::clone_impl() const {
3255 return new FPToSIInst(getOperand(0), getType());
3258 PtrToIntInst *PtrToIntInst::clone_impl() const {
3259 return new PtrToIntInst(getOperand(0), getType());
3262 IntToPtrInst *IntToPtrInst::clone_impl() const {
3263 return new IntToPtrInst(getOperand(0), getType());
3266 BitCastInst *BitCastInst::clone_impl() const {
3267 return new BitCastInst(getOperand(0), getType());
3270 CallInst *CallInst::clone_impl() const {
3271 return new(getNumOperands()) CallInst(*this);
3274 SelectInst *SelectInst::clone_impl() const {
3275 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
3278 VAArgInst *VAArgInst::clone_impl() const {
3279 return new VAArgInst(getOperand(0), getType());
3282 ExtractElementInst *ExtractElementInst::clone_impl() const {
3283 return ExtractElementInst::Create(getOperand(0), getOperand(1));
3286 InsertElementInst *InsertElementInst::clone_impl() const {
3287 return InsertElementInst::Create(getOperand(0),
3292 ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3293 return new ShuffleVectorInst(getOperand(0),
3298 PHINode *PHINode::clone_impl() const {
3299 return new PHINode(*this);
3302 ReturnInst *ReturnInst::clone_impl() const {
3303 return new(getNumOperands()) ReturnInst(*this);
3306 BranchInst *BranchInst::clone_impl() const {
3307 return new(getNumOperands()) BranchInst(*this);
3310 SwitchInst *SwitchInst::clone_impl() const {
3311 return new SwitchInst(*this);
3314 IndirectBrInst *IndirectBrInst::clone_impl() const {
3315 return new IndirectBrInst(*this);
3319 InvokeInst *InvokeInst::clone_impl() const {
3320 return new(getNumOperands()) InvokeInst(*this);
3323 UnwindInst *UnwindInst::clone_impl() const {
3324 LLVMContext &Context = getContext();
3325 return new UnwindInst(Context);
3328 UnreachableInst *UnreachableInst::clone_impl() const {
3329 LLVMContext &Context = getContext();
3330 return new UnreachableInst(Context);