1 //===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements all of the non-inline methods for the LLVM instruction
13 //===----------------------------------------------------------------------===//
15 #include "LLVMContextImpl.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Module.h"
21 #include "llvm/Operator.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/CallSite.h"
25 #include "llvm/Support/ConstantRange.h"
26 #include "llvm/Support/MathExtras.h"
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
33 User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
36 ? (CallInst::ArgOffset
37 ? cast</*FIXME: CallInst*/User>(II)->op_begin()
38 : cast</*FIXME: CallInst*/User>(II)->op_end() - 1)
39 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Function
42 //===----------------------------------------------------------------------===//
43 // TerminatorInst Class
44 //===----------------------------------------------------------------------===//
46 // Out of line virtual method, so the vtable, etc has a home.
47 TerminatorInst::~TerminatorInst() {
50 //===----------------------------------------------------------------------===//
51 // UnaryInstruction Class
52 //===----------------------------------------------------------------------===//
54 // Out of line virtual method, so the vtable, etc has a home.
55 UnaryInstruction::~UnaryInstruction() {
58 //===----------------------------------------------------------------------===//
60 //===----------------------------------------------------------------------===//
62 /// areInvalidOperands - Return a string if the specified operands are invalid
63 /// for a select operation, otherwise return null.
64 const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
65 if (Op1->getType() != Op2->getType())
66 return "both values to select must have same type";
68 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
70 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
71 return "vector select condition element type must be i1";
72 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
74 return "selected values for vector select must be vectors";
75 if (ET->getNumElements() != VT->getNumElements())
76 return "vector select requires selected vectors to have "
77 "the same vector length as select condition";
78 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
79 return "select condition must be i1 or <n x i1>";
85 //===----------------------------------------------------------------------===//
87 //===----------------------------------------------------------------------===//
89 PHINode::PHINode(const PHINode &PN)
90 : Instruction(PN.getType(), Instruction::PHI,
91 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
92 ReservedSpace(PN.getNumOperands()) {
93 Use *OL = OperandList;
94 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
95 OL[i] = PN.getOperand(i);
96 OL[i+1] = PN.getOperand(i+1);
98 SubclassOptionalData = PN.SubclassOptionalData;
101 PHINode::~PHINode() {
103 dropHungoffUses(OperandList);
106 // removeIncomingValue - Remove an incoming value. This is useful if a
107 // predecessor basic block is deleted.
108 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
109 unsigned NumOps = getNumOperands();
110 Use *OL = OperandList;
111 assert(Idx*2 < NumOps && "BB not in PHI node!");
112 Value *Removed = OL[Idx*2];
114 // Move everything after this operand down.
116 // FIXME: we could just swap with the end of the list, then erase. However,
117 // client might not expect this to happen. The code as it is thrashes the
118 // use/def lists, which is kinda lame.
119 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
124 // Nuke the last value.
126 OL[NumOps-2+1].set(0);
127 NumOperands = NumOps-2;
129 // If the PHI node is dead, because it has zero entries, nuke it now.
130 if (NumOps == 2 && DeletePHIIfEmpty) {
131 // If anyone is using this PHI, make them use a dummy value instead...
132 replaceAllUsesWith(UndefValue::get(getType()));
138 /// resizeOperands - resize operands - This adjusts the length of the operands
139 /// list according to the following behavior:
140 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
141 /// of operation. This grows the number of ops by 1.5 times.
142 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
143 /// 3. If NumOps == NumOperands, trim the reserved space.
145 void PHINode::resizeOperands(unsigned NumOps) {
146 unsigned e = getNumOperands();
149 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
150 } else if (NumOps*2 > NumOperands) {
152 if (ReservedSpace >= NumOps) return;
153 } else if (NumOps == NumOperands) {
154 if (ReservedSpace == NumOps) return;
159 ReservedSpace = NumOps;
160 Use *OldOps = OperandList;
161 Use *NewOps = allocHungoffUses(NumOps);
162 std::copy(OldOps, OldOps + e, NewOps);
163 OperandList = NewOps;
164 if (OldOps) Use::zap(OldOps, OldOps + e, true);
167 /// hasConstantValue - If the specified PHI node always merges together the same
168 /// value, return the value, otherwise return null.
170 /// If the PHI has undef operands, but all the rest of the operands are
171 /// some unique value, return that value if it can be proved that the
172 /// value dominates the PHI. If DT is null, use a conservative check,
173 /// otherwise use DT to test for dominance.
175 Value *PHINode::hasConstantValue(DominatorTree *DT) const {
176 // If the PHI node only has one incoming value, eliminate the PHI node.
177 if (getNumIncomingValues() == 1) {
178 if (getIncomingValue(0) != this) // not X = phi X
179 return getIncomingValue(0);
180 return UndefValue::get(getType()); // Self cycle is dead.
183 // Otherwise if all of the incoming values are the same for the PHI, replace
184 // the PHI node with the incoming value.
187 bool HasUndefInput = false;
188 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
189 if (isa<UndefValue>(getIncomingValue(i))) {
190 HasUndefInput = true;
191 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
192 if (InVal && getIncomingValue(i) != InVal)
193 return 0; // Not the same, bail out.
194 InVal = getIncomingValue(i);
197 // The only case that could cause InVal to be null is if we have a PHI node
198 // that only has entries for itself. In this case, there is no entry into the
199 // loop, so kill the PHI.
201 if (InVal == 0) InVal = UndefValue::get(getType());
203 // If we have a PHI node like phi(X, undef, X), where X is defined by some
204 // instruction, we cannot always return X as the result of the PHI node. Only
205 // do this if X is not an instruction (thus it must dominate the PHI block),
206 // or if the client is prepared to deal with this possibility.
207 if (!HasUndefInput || !isa<Instruction>(InVal))
210 Instruction *IV = cast<Instruction>(InVal);
212 // We have a DominatorTree. Do a precise test.
213 if (!DT->dominates(IV, this))
216 // If it is in the entry block, it obviously dominates everything.
217 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
219 return 0; // Cannot guarantee that InVal dominates this PHINode.
222 // All of the incoming values are the same, return the value now.
227 //===----------------------------------------------------------------------===//
228 // CallInst Implementation
229 //===----------------------------------------------------------------------===//
231 CallInst::~CallInst() {
234 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
235 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
236 Op<ArgOffset -1>() = Func;
238 const FunctionType *FTy =
239 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
240 FTy = FTy; // silence warning.
242 assert((NumParams == FTy->getNumParams() ||
243 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
244 "Calling a function with bad signature!");
245 for (unsigned i = 0; i != NumParams; ++i) {
246 assert((i >= FTy->getNumParams() ||
247 FTy->getParamType(i) == Params[i]->getType()) &&
248 "Calling a function with a bad signature!");
249 OperandList[i + ArgOffset] = Params[i];
253 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
254 assert(NumOperands == 3 && "NumOperands not set up?");
255 Op<ArgOffset -1>() = Func;
256 Op<ArgOffset + 0>() = Actual1;
257 Op<ArgOffset + 1>() = Actual2;
259 const FunctionType *FTy =
260 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
261 FTy = FTy; // silence warning.
263 assert((FTy->getNumParams() == 2 ||
264 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
265 "Calling a function with bad signature");
266 assert((0 >= FTy->getNumParams() ||
267 FTy->getParamType(0) == Actual1->getType()) &&
268 "Calling a function with a bad signature!");
269 assert((1 >= FTy->getNumParams() ||
270 FTy->getParamType(1) == Actual2->getType()) &&
271 "Calling a function with a bad signature!");
274 void CallInst::init(Value *Func, Value *Actual) {
275 assert(NumOperands == 2 && "NumOperands not set up?");
276 Op<ArgOffset -1>() = Func;
277 Op<ArgOffset + 0>() = Actual;
279 const FunctionType *FTy =
280 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
281 FTy = FTy; // silence warning.
283 assert((FTy->getNumParams() == 1 ||
284 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
285 "Calling a function with bad signature");
286 assert((0 == FTy->getNumParams() ||
287 FTy->getParamType(0) == Actual->getType()) &&
288 "Calling a function with a bad signature!");
291 void CallInst::init(Value *Func) {
292 assert(NumOperands == 1 && "NumOperands not set up?");
293 Op<ArgOffset -1>() = Func;
295 const FunctionType *FTy =
296 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
297 FTy = FTy; // silence warning.
299 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
302 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
303 Instruction *InsertBefore)
304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
307 OperandTraits<CallInst>::op_end(this) - 2,
313 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
314 BasicBlock *InsertAtEnd)
315 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
316 ->getElementType())->getReturnType(),
318 OperandTraits<CallInst>::op_end(this) - 2,
323 CallInst::CallInst(Value *Func, const Twine &Name,
324 Instruction *InsertBefore)
325 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
326 ->getElementType())->getReturnType(),
328 OperandTraits<CallInst>::op_end(this) - 1,
334 CallInst::CallInst(Value *Func, const Twine &Name,
335 BasicBlock *InsertAtEnd)
336 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
337 ->getElementType())->getReturnType(),
339 OperandTraits<CallInst>::op_end(this) - 1,
345 CallInst::CallInst(const CallInst &CI)
346 : Instruction(CI.getType(), Instruction::Call,
347 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
348 CI.getNumOperands()) {
349 setAttributes(CI.getAttributes());
350 setTailCall(CI.isTailCall());
351 setCallingConv(CI.getCallingConv());
353 Use *OL = OperandList;
354 Use *InOL = CI.OperandList;
355 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
357 SubclassOptionalData = CI.SubclassOptionalData;
360 void CallInst::addAttribute(unsigned i, Attributes attr) {
361 AttrListPtr PAL = getAttributes();
362 PAL = PAL.addAttr(i, attr);
366 void CallInst::removeAttribute(unsigned i, Attributes attr) {
367 AttrListPtr PAL = getAttributes();
368 PAL = PAL.removeAttr(i, attr);
372 bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
373 if (AttributeList.paramHasAttr(i, attr))
375 if (const Function *F = getCalledFunction())
376 return F->paramHasAttr(i, attr);
380 /// IsConstantOne - Return true only if val is constant int 1
381 static bool IsConstantOne(Value *val) {
382 assert(val && "IsConstantOne does not work with NULL val");
383 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
386 static Instruction *createMalloc(Instruction *InsertBefore,
387 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
388 const Type *AllocTy, Value *AllocSize,
389 Value *ArraySize, Function *MallocF,
391 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
392 "createMalloc needs either InsertBefore or InsertAtEnd");
394 // malloc(type) becomes:
395 // bitcast (i8* malloc(typeSize)) to type*
396 // malloc(type, arraySize) becomes:
397 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
399 ArraySize = ConstantInt::get(IntPtrTy, 1);
400 else if (ArraySize->getType() != IntPtrTy) {
402 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
405 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
409 if (!IsConstantOne(ArraySize)) {
410 if (IsConstantOne(AllocSize)) {
411 AllocSize = ArraySize; // Operand * 1 = Operand
412 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
413 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
415 // Malloc arg is constant product of type size and array size
416 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
418 // Multiply type size by the array size...
420 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
421 "mallocsize", InsertBefore);
423 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
424 "mallocsize", InsertAtEnd);
428 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
429 // Create the call to Malloc.
430 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
431 Module* M = BB->getParent()->getParent();
432 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
433 Value *MallocFunc = MallocF;
435 // prototype malloc as "void *malloc(size_t)"
436 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
437 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
438 CallInst *MCall = NULL;
439 Instruction *Result = NULL;
441 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
443 if (Result->getType() != AllocPtrType)
444 // Create a cast instruction to convert to the right type...
445 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
447 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
449 if (Result->getType() != AllocPtrType) {
450 InsertAtEnd->getInstList().push_back(MCall);
451 // Create a cast instruction to convert to the right type...
452 Result = new BitCastInst(MCall, AllocPtrType, Name);
455 MCall->setTailCall();
456 if (Function *F = dyn_cast<Function>(MallocFunc)) {
457 MCall->setCallingConv(F->getCallingConv());
458 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
460 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
465 /// CreateMalloc - Generate the IR for a call to malloc:
466 /// 1. Compute the malloc call's argument as the specified type's size,
467 /// possibly multiplied by the array size if the array size is not
469 /// 2. Call malloc with that argument.
470 /// 3. Bitcast the result of the malloc call to the specified type.
471 Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
472 const Type *IntPtrTy, const Type *AllocTy,
473 Value *AllocSize, Value *ArraySize,
475 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
476 ArraySize, NULL, Name);
479 /// CreateMalloc - Generate the IR for a call to malloc:
480 /// 1. Compute the malloc call's argument as the specified type's size,
481 /// possibly multiplied by the array size if the array size is not
483 /// 2. Call malloc with that argument.
484 /// 3. Bitcast the result of the malloc call to the specified type.
485 /// Note: This function does not add the bitcast to the basic block, that is the
486 /// responsibility of the caller.
487 Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
488 const Type *IntPtrTy, const Type *AllocTy,
489 Value *AllocSize, Value *ArraySize,
490 Function *MallocF, const Twine &Name) {
491 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
492 ArraySize, MallocF, Name);
495 static Instruction* createFree(Value* Source, Instruction *InsertBefore,
496 BasicBlock *InsertAtEnd) {
497 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
498 "createFree needs either InsertBefore or InsertAtEnd");
499 assert(Source->getType()->isPointerTy() &&
500 "Can not free something of nonpointer type!");
502 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
503 Module* M = BB->getParent()->getParent();
505 const Type *VoidTy = Type::getVoidTy(M->getContext());
506 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
507 // prototype free as "void free(void*)"
508 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
509 CallInst* Result = NULL;
510 Value *PtrCast = Source;
512 if (Source->getType() != IntPtrTy)
513 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
514 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
516 if (Source->getType() != IntPtrTy)
517 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
518 Result = CallInst::Create(FreeFunc, PtrCast, "");
520 Result->setTailCall();
521 if (Function *F = dyn_cast<Function>(FreeFunc))
522 Result->setCallingConv(F->getCallingConv());
527 /// CreateFree - Generate the IR for a call to the builtin free function.
528 void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
529 createFree(Source, InsertBefore, NULL);
532 /// CreateFree - Generate the IR for a call to the builtin free function.
533 /// Note: This function does not add the call to the basic block, that is the
534 /// responsibility of the caller.
535 Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
536 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
537 assert(FreeCall && "CreateFree did not create a CallInst");
541 //===----------------------------------------------------------------------===//
542 // InvokeInst Implementation
543 //===----------------------------------------------------------------------===//
545 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
546 Value* const *Args, unsigned NumArgs) {
547 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
550 Op<-1>() = IfException;
551 const FunctionType *FTy =
552 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
553 FTy = FTy; // silence warning.
555 assert(((NumArgs == FTy->getNumParams()) ||
556 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
557 "Invoking a function with bad signature");
559 Use *OL = OperandList;
560 for (unsigned i = 0, e = NumArgs; i != e; i++) {
561 assert((i >= FTy->getNumParams() ||
562 FTy->getParamType(i) == Args[i]->getType()) &&
563 "Invoking a function with a bad signature!");
569 InvokeInst::InvokeInst(const InvokeInst &II)
570 : TerminatorInst(II.getType(), Instruction::Invoke,
571 OperandTraits<InvokeInst>::op_end(this)
572 - II.getNumOperands(),
573 II.getNumOperands()) {
574 setAttributes(II.getAttributes());
575 setCallingConv(II.getCallingConv());
576 Use *OL = OperandList, *InOL = II.OperandList;
577 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
579 SubclassOptionalData = II.SubclassOptionalData;
582 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
583 return getSuccessor(idx);
585 unsigned InvokeInst::getNumSuccessorsV() const {
586 return getNumSuccessors();
588 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
589 return setSuccessor(idx, B);
592 bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
593 if (AttributeList.paramHasAttr(i, attr))
595 if (const Function *F = getCalledFunction())
596 return F->paramHasAttr(i, attr);
600 void InvokeInst::addAttribute(unsigned i, Attributes attr) {
601 AttrListPtr PAL = getAttributes();
602 PAL = PAL.addAttr(i, attr);
606 void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
607 AttrListPtr PAL = getAttributes();
608 PAL = PAL.removeAttr(i, attr);
613 //===----------------------------------------------------------------------===//
614 // ReturnInst Implementation
615 //===----------------------------------------------------------------------===//
617 ReturnInst::ReturnInst(const ReturnInst &RI)
618 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
619 OperandTraits<ReturnInst>::op_end(this) -
621 RI.getNumOperands()) {
622 if (RI.getNumOperands())
623 Op<0>() = RI.Op<0>();
624 SubclassOptionalData = RI.SubclassOptionalData;
627 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
628 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
629 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
634 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
635 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
636 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
641 ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
642 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
643 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
646 unsigned ReturnInst::getNumSuccessorsV() const {
647 return getNumSuccessors();
650 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
651 /// emit the vtable for the class in this translation unit.
652 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
653 llvm_unreachable("ReturnInst has no successors!");
656 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
657 llvm_unreachable("ReturnInst has no successors!");
661 ReturnInst::~ReturnInst() {
664 //===----------------------------------------------------------------------===//
665 // UnwindInst Implementation
666 //===----------------------------------------------------------------------===//
668 UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
669 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
670 0, 0, InsertBefore) {
672 UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
673 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
678 unsigned UnwindInst::getNumSuccessorsV() const {
679 return getNumSuccessors();
682 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
683 llvm_unreachable("UnwindInst has no successors!");
686 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
687 llvm_unreachable("UnwindInst has no successors!");
691 //===----------------------------------------------------------------------===//
692 // UnreachableInst Implementation
693 //===----------------------------------------------------------------------===//
695 UnreachableInst::UnreachableInst(LLVMContext &Context,
696 Instruction *InsertBefore)
697 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
698 0, 0, InsertBefore) {
700 UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
701 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
705 unsigned UnreachableInst::getNumSuccessorsV() const {
706 return getNumSuccessors();
709 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
710 llvm_unreachable("UnwindInst has no successors!");
713 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
714 llvm_unreachable("UnwindInst has no successors!");
718 //===----------------------------------------------------------------------===//
719 // BranchInst Implementation
720 //===----------------------------------------------------------------------===//
722 void BranchInst::AssertOK() {
724 assert(getCondition()->getType()->isIntegerTy(1) &&
725 "May only branch on boolean predicates!");
728 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
729 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
730 OperandTraits<BranchInst>::op_end(this) - 1,
732 assert(IfTrue != 0 && "Branch destination may not be null!");
735 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
736 Instruction *InsertBefore)
737 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
738 OperandTraits<BranchInst>::op_end(this) - 3,
748 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
749 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
750 OperandTraits<BranchInst>::op_end(this) - 1,
752 assert(IfTrue != 0 && "Branch destination may not be null!");
756 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
757 BasicBlock *InsertAtEnd)
758 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
759 OperandTraits<BranchInst>::op_end(this) - 3,
770 BranchInst::BranchInst(const BranchInst &BI) :
771 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
772 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
773 BI.getNumOperands()) {
774 Op<-1>() = BI.Op<-1>();
775 if (BI.getNumOperands() != 1) {
776 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
777 Op<-3>() = BI.Op<-3>();
778 Op<-2>() = BI.Op<-2>();
780 SubclassOptionalData = BI.SubclassOptionalData;
784 Use* Use::getPrefix() {
785 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
786 if (PotentialPrefix.getOpaqueValue())
789 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
792 BranchInst::~BranchInst() {
793 if (NumOperands == 1) {
794 if (Use *Prefix = OperandList->getPrefix()) {
797 // mark OperandList to have a special value for scrutiny
798 // by baseclass destructors and operator delete
799 OperandList = Prefix;
802 OperandList = op_begin();
808 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
809 return getSuccessor(idx);
811 unsigned BranchInst::getNumSuccessorsV() const {
812 return getNumSuccessors();
814 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
815 setSuccessor(idx, B);
819 //===----------------------------------------------------------------------===//
820 // AllocaInst Implementation
821 //===----------------------------------------------------------------------===//
823 static Value *getAISize(LLVMContext &Context, Value *Amt) {
825 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
827 assert(!isa<BasicBlock>(Amt) &&
828 "Passed basic block into allocation size parameter! Use other ctor");
829 assert(Amt->getType()->isIntegerTy() &&
830 "Allocation array size is not an integer!");
835 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
836 const Twine &Name, Instruction *InsertBefore)
837 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
838 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
840 assert(!Ty->isVoidTy() && "Cannot allocate void!");
844 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
845 const Twine &Name, BasicBlock *InsertAtEnd)
846 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
847 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
849 assert(!Ty->isVoidTy() && "Cannot allocate void!");
853 AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
854 Instruction *InsertBefore)
855 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
856 getAISize(Ty->getContext(), 0), InsertBefore) {
858 assert(!Ty->isVoidTy() && "Cannot allocate void!");
862 AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
863 BasicBlock *InsertAtEnd)
864 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
865 getAISize(Ty->getContext(), 0), InsertAtEnd) {
867 assert(!Ty->isVoidTy() && "Cannot allocate void!");
871 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
872 const Twine &Name, Instruction *InsertBefore)
873 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
874 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
876 assert(!Ty->isVoidTy() && "Cannot allocate void!");
880 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
881 const Twine &Name, BasicBlock *InsertAtEnd)
882 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
883 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
885 assert(!Ty->isVoidTy() && "Cannot allocate void!");
889 // Out of line virtual method, so the vtable, etc has a home.
890 AllocaInst::~AllocaInst() {
893 void AllocaInst::setAlignment(unsigned Align) {
894 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
895 setInstructionSubclassData(Log2_32(Align) + 1);
896 assert(getAlignment() == Align && "Alignment representation error!");
899 bool AllocaInst::isArrayAllocation() const {
900 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
901 return CI->getZExtValue() != 1;
905 const Type *AllocaInst::getAllocatedType() const {
906 return getType()->getElementType();
909 /// isStaticAlloca - Return true if this alloca is in the entry block of the
910 /// function and is a constant size. If so, the code generator will fold it
911 /// into the prolog/epilog code, so it is basically free.
912 bool AllocaInst::isStaticAlloca() const {
913 // Must be constant size.
914 if (!isa<ConstantInt>(getArraySize())) return false;
916 // Must be in the entry block.
917 const BasicBlock *Parent = getParent();
918 return Parent == &Parent->getParent()->front();
921 //===----------------------------------------------------------------------===//
922 // LoadInst Implementation
923 //===----------------------------------------------------------------------===//
925 void LoadInst::AssertOK() {
926 assert(getOperand(0)->getType()->isPointerTy() &&
927 "Ptr must have pointer type.");
930 LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
931 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
932 Load, Ptr, InsertBef) {
939 LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
940 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
941 Load, Ptr, InsertAE) {
948 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
949 Instruction *InsertBef)
950 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
951 Load, Ptr, InsertBef) {
952 setVolatile(isVolatile);
958 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
959 unsigned Align, Instruction *InsertBef)
960 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
961 Load, Ptr, InsertBef) {
962 setVolatile(isVolatile);
968 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
969 unsigned Align, BasicBlock *InsertAE)
970 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
971 Load, Ptr, InsertAE) {
972 setVolatile(isVolatile);
978 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
979 BasicBlock *InsertAE)
980 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
981 Load, Ptr, InsertAE) {
982 setVolatile(isVolatile);
990 LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
991 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
992 Load, Ptr, InsertBef) {
996 if (Name && Name[0]) setName(Name);
999 LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1000 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1001 Load, Ptr, InsertAE) {
1005 if (Name && Name[0]) setName(Name);
1008 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1009 Instruction *InsertBef)
1010 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1011 Load, Ptr, InsertBef) {
1012 setVolatile(isVolatile);
1015 if (Name && Name[0]) setName(Name);
1018 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1019 BasicBlock *InsertAE)
1020 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1021 Load, Ptr, InsertAE) {
1022 setVolatile(isVolatile);
1025 if (Name && Name[0]) setName(Name);
1028 void LoadInst::setAlignment(unsigned Align) {
1029 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1030 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1031 ((Log2_32(Align)+1)<<1));
1034 //===----------------------------------------------------------------------===//
1035 // StoreInst Implementation
1036 //===----------------------------------------------------------------------===//
1038 void StoreInst::AssertOK() {
1039 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
1040 assert(getOperand(1)->getType()->isPointerTy() &&
1041 "Ptr must have pointer type!");
1042 assert(getOperand(0)->getType() ==
1043 cast<PointerType>(getOperand(1)->getType())->getElementType()
1044 && "Ptr must be a pointer to Val type!");
1048 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
1049 : Instruction(Type::getVoidTy(val->getContext()), Store,
1050 OperandTraits<StoreInst>::op_begin(this),
1051 OperandTraits<StoreInst>::operands(this),
1060 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
1061 : Instruction(Type::getVoidTy(val->getContext()), Store,
1062 OperandTraits<StoreInst>::op_begin(this),
1063 OperandTraits<StoreInst>::operands(this),
1072 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1073 Instruction *InsertBefore)
1074 : Instruction(Type::getVoidTy(val->getContext()), Store,
1075 OperandTraits<StoreInst>::op_begin(this),
1076 OperandTraits<StoreInst>::operands(this),
1080 setVolatile(isVolatile);
1085 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1086 unsigned Align, Instruction *InsertBefore)
1087 : Instruction(Type::getVoidTy(val->getContext()), Store,
1088 OperandTraits<StoreInst>::op_begin(this),
1089 OperandTraits<StoreInst>::operands(this),
1093 setVolatile(isVolatile);
1094 setAlignment(Align);
1098 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1099 unsigned Align, BasicBlock *InsertAtEnd)
1100 : Instruction(Type::getVoidTy(val->getContext()), Store,
1101 OperandTraits<StoreInst>::op_begin(this),
1102 OperandTraits<StoreInst>::operands(this),
1106 setVolatile(isVolatile);
1107 setAlignment(Align);
1111 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1112 BasicBlock *InsertAtEnd)
1113 : Instruction(Type::getVoidTy(val->getContext()), Store,
1114 OperandTraits<StoreInst>::op_begin(this),
1115 OperandTraits<StoreInst>::operands(this),
1119 setVolatile(isVolatile);
1124 void StoreInst::setAlignment(unsigned Align) {
1125 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1126 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1127 ((Log2_32(Align)+1) << 1));
1130 //===----------------------------------------------------------------------===//
1131 // GetElementPtrInst Implementation
1132 //===----------------------------------------------------------------------===//
1134 static unsigned retrieveAddrSpace(const Value *Val) {
1135 return cast<PointerType>(Val->getType())->getAddressSpace();
1138 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
1139 const Twine &Name) {
1140 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1141 Use *OL = OperandList;
1144 for (unsigned i = 0; i != NumIdx; ++i)
1150 void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
1151 assert(NumOperands == 2 && "NumOperands not initialized?");
1152 Use *OL = OperandList;
1159 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1160 : Instruction(GEPI.getType(), GetElementPtr,
1161 OperandTraits<GetElementPtrInst>::op_end(this)
1162 - GEPI.getNumOperands(),
1163 GEPI.getNumOperands()) {
1164 Use *OL = OperandList;
1165 Use *GEPIOL = GEPI.OperandList;
1166 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1168 SubclassOptionalData = GEPI.SubclassOptionalData;
1171 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1172 const Twine &Name, Instruction *InBe)
1173 : Instruction(PointerType::get(
1174 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
1176 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1178 init(Ptr, Idx, Name);
1181 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1182 const Twine &Name, BasicBlock *IAE)
1183 : Instruction(PointerType::get(
1184 checkType(getIndexedType(Ptr->getType(),Idx)),
1185 retrieveAddrSpace(Ptr)),
1187 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1189 init(Ptr, Idx, Name);
1192 /// getIndexedType - Returns the type of the element that would be accessed with
1193 /// a gep instruction with the specified parameters.
1195 /// The Idxs pointer should point to a continuous piece of memory containing the
1196 /// indices, either as Value* or uint64_t.
1198 /// A null type is returned if the indices are invalid for the specified
1201 template <typename IndexTy>
1202 static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1204 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1205 if (!PTy) return 0; // Type isn't a pointer type!
1206 const Type *Agg = PTy->getElementType();
1208 // Handle the special case of the empty set index set, which is always valid.
1212 // If there is at least one index, the top level type must be sized, otherwise
1213 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1214 // that contain opaque types) under the assumption that it will be resolved to
1215 // a sane type later.
1216 if (!Agg->isSized() && !Agg->isAbstract())
1219 unsigned CurIdx = 1;
1220 for (; CurIdx != NumIdx; ++CurIdx) {
1221 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1222 if (!CT || CT->isPointerTy()) return 0;
1223 IndexTy Index = Idxs[CurIdx];
1224 if (!CT->indexValid(Index)) return 0;
1225 Agg = CT->getTypeAtIndex(Index);
1227 // If the new type forwards to another type, then it is in the middle
1228 // of being refined to another type (and hence, may have dropped all
1229 // references to what it was using before). So, use the new forwarded
1231 if (const Type *Ty = Agg->getForwardedType())
1234 return CurIdx == NumIdx ? Agg : 0;
1237 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1240 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1243 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1244 uint64_t const *Idxs,
1246 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1249 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1250 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1251 if (!PTy) return 0; // Type isn't a pointer type!
1253 // Check the pointer index.
1254 if (!PTy->indexValid(Idx)) return 0;
1256 return PTy->getElementType();
1260 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1261 /// zeros. If so, the result pointer and the first operand have the same
1262 /// value, just potentially different types.
1263 bool GetElementPtrInst::hasAllZeroIndices() const {
1264 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1265 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1266 if (!CI->isZero()) return false;
1274 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1275 /// constant integers. If so, the result pointer and the first operand have
1276 /// a constant offset between them.
1277 bool GetElementPtrInst::hasAllConstantIndices() const {
1278 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1279 if (!isa<ConstantInt>(getOperand(i)))
1285 void GetElementPtrInst::setIsInBounds(bool B) {
1286 cast<GEPOperator>(this)->setIsInBounds(B);
1289 bool GetElementPtrInst::isInBounds() const {
1290 return cast<GEPOperator>(this)->isInBounds();
1293 //===----------------------------------------------------------------------===//
1294 // ExtractElementInst Implementation
1295 //===----------------------------------------------------------------------===//
1297 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1299 Instruction *InsertBef)
1300 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1302 OperandTraits<ExtractElementInst>::op_begin(this),
1304 assert(isValidOperands(Val, Index) &&
1305 "Invalid extractelement instruction operands!");
1311 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1313 BasicBlock *InsertAE)
1314 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1316 OperandTraits<ExtractElementInst>::op_begin(this),
1318 assert(isValidOperands(Val, Index) &&
1319 "Invalid extractelement instruction operands!");
1327 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1328 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
1334 //===----------------------------------------------------------------------===//
1335 // InsertElementInst Implementation
1336 //===----------------------------------------------------------------------===//
1338 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1340 Instruction *InsertBef)
1341 : Instruction(Vec->getType(), InsertElement,
1342 OperandTraits<InsertElementInst>::op_begin(this),
1344 assert(isValidOperands(Vec, Elt, Index) &&
1345 "Invalid insertelement instruction operands!");
1352 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1354 BasicBlock *InsertAE)
1355 : Instruction(Vec->getType(), InsertElement,
1356 OperandTraits<InsertElementInst>::op_begin(this),
1358 assert(isValidOperands(Vec, Elt, Index) &&
1359 "Invalid insertelement instruction operands!");
1367 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1368 const Value *Index) {
1369 if (!Vec->getType()->isVectorTy())
1370 return false; // First operand of insertelement must be vector type.
1372 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1373 return false;// Second operand of insertelement must be vector element type.
1375 if (!Index->getType()->isIntegerTy(32))
1376 return false; // Third operand of insertelement must be i32.
1381 //===----------------------------------------------------------------------===//
1382 // ShuffleVectorInst Implementation
1383 //===----------------------------------------------------------------------===//
1385 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1387 Instruction *InsertBefore)
1388 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1389 cast<VectorType>(Mask->getType())->getNumElements()),
1391 OperandTraits<ShuffleVectorInst>::op_begin(this),
1392 OperandTraits<ShuffleVectorInst>::operands(this),
1394 assert(isValidOperands(V1, V2, Mask) &&
1395 "Invalid shuffle vector instruction operands!");
1402 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1404 BasicBlock *InsertAtEnd)
1405 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1406 cast<VectorType>(Mask->getType())->getNumElements()),
1408 OperandTraits<ShuffleVectorInst>::op_begin(this),
1409 OperandTraits<ShuffleVectorInst>::operands(this),
1411 assert(isValidOperands(V1, V2, Mask) &&
1412 "Invalid shuffle vector instruction operands!");
1420 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1421 const Value *Mask) {
1422 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
1425 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1426 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1427 !MaskTy->getElementType()->isIntegerTy(32))
1432 /// getMaskValue - Return the index from the shuffle mask for the specified
1433 /// output result. This is either -1 if the element is undef or a number less
1434 /// than 2*numelements.
1435 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1436 const Constant *Mask = cast<Constant>(getOperand(2));
1437 if (isa<UndefValue>(Mask)) return -1;
1438 if (isa<ConstantAggregateZero>(Mask)) return 0;
1439 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1440 assert(i < MaskCV->getNumOperands() && "Index out of range");
1442 if (isa<UndefValue>(MaskCV->getOperand(i)))
1444 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1447 //===----------------------------------------------------------------------===//
1448 // InsertValueInst Class
1449 //===----------------------------------------------------------------------===//
1451 void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1452 unsigned NumIdx, const Twine &Name) {
1453 assert(NumOperands == 2 && "NumOperands not initialized?");
1457 Indices.append(Idx, Idx + NumIdx);
1461 void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1462 const Twine &Name) {
1463 assert(NumOperands == 2 && "NumOperands not initialized?");
1467 Indices.push_back(Idx);
1471 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1472 : Instruction(IVI.getType(), InsertValue,
1473 OperandTraits<InsertValueInst>::op_begin(this), 2),
1474 Indices(IVI.Indices) {
1475 Op<0>() = IVI.getOperand(0);
1476 Op<1>() = IVI.getOperand(1);
1477 SubclassOptionalData = IVI.SubclassOptionalData;
1480 InsertValueInst::InsertValueInst(Value *Agg,
1484 Instruction *InsertBefore)
1485 : Instruction(Agg->getType(), InsertValue,
1486 OperandTraits<InsertValueInst>::op_begin(this),
1488 init(Agg, Val, Idx, Name);
1491 InsertValueInst::InsertValueInst(Value *Agg,
1495 BasicBlock *InsertAtEnd)
1496 : Instruction(Agg->getType(), InsertValue,
1497 OperandTraits<InsertValueInst>::op_begin(this),
1499 init(Agg, Val, Idx, Name);
1502 //===----------------------------------------------------------------------===//
1503 // ExtractValueInst Class
1504 //===----------------------------------------------------------------------===//
1506 void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
1507 const Twine &Name) {
1508 assert(NumOperands == 1 && "NumOperands not initialized?");
1510 Indices.append(Idx, Idx + NumIdx);
1514 void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
1515 assert(NumOperands == 1 && "NumOperands not initialized?");
1517 Indices.push_back(Idx);
1521 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1522 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
1523 Indices(EVI.Indices) {
1524 SubclassOptionalData = EVI.SubclassOptionalData;
1527 // getIndexedType - Returns the type of the element that would be extracted
1528 // with an extractvalue instruction with the specified parameters.
1530 // A null type is returned if the indices are invalid for the specified
1533 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1534 const unsigned *Idxs,
1536 unsigned CurIdx = 0;
1537 for (; CurIdx != NumIdx; ++CurIdx) {
1538 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1539 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
1540 unsigned Index = Idxs[CurIdx];
1541 if (!CT->indexValid(Index)) return 0;
1542 Agg = CT->getTypeAtIndex(Index);
1544 // If the new type forwards to another type, then it is in the middle
1545 // of being refined to another type (and hence, may have dropped all
1546 // references to what it was using before). So, use the new forwarded
1548 if (const Type *Ty = Agg->getForwardedType())
1551 return CurIdx == NumIdx ? Agg : 0;
1554 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1556 return getIndexedType(Agg, &Idx, 1);
1559 //===----------------------------------------------------------------------===//
1560 // BinaryOperator Class
1561 //===----------------------------------------------------------------------===//
1563 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1564 const Type *Ty, const Twine &Name,
1565 Instruction *InsertBefore)
1566 : Instruction(Ty, iType,
1567 OperandTraits<BinaryOperator>::op_begin(this),
1568 OperandTraits<BinaryOperator>::operands(this),
1576 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1577 const Type *Ty, const Twine &Name,
1578 BasicBlock *InsertAtEnd)
1579 : Instruction(Ty, iType,
1580 OperandTraits<BinaryOperator>::op_begin(this),
1581 OperandTraits<BinaryOperator>::operands(this),
1590 void BinaryOperator::init(BinaryOps iType) {
1591 Value *LHS = getOperand(0), *RHS = getOperand(1);
1592 LHS = LHS; RHS = RHS; // Silence warnings.
1593 assert(LHS->getType() == RHS->getType() &&
1594 "Binary operator operand types must match!");
1599 assert(getType() == LHS->getType() &&
1600 "Arithmetic operation should return same type as operands!");
1601 assert(getType()->isIntOrIntVectorTy() &&
1602 "Tried to create an integer operation on a non-integer type!");
1604 case FAdd: case FSub:
1606 assert(getType() == LHS->getType() &&
1607 "Arithmetic operation should return same type as operands!");
1608 assert(getType()->isFPOrFPVectorTy() &&
1609 "Tried to create a floating-point operation on a "
1610 "non-floating-point type!");
1614 assert(getType() == LHS->getType() &&
1615 "Arithmetic operation should return same type as operands!");
1616 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1617 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1618 "Incorrect operand type (not integer) for S/UDIV");
1621 assert(getType() == LHS->getType() &&
1622 "Arithmetic operation should return same type as operands!");
1623 assert(getType()->isFPOrFPVectorTy() &&
1624 "Incorrect operand type (not floating point) for FDIV");
1628 assert(getType() == LHS->getType() &&
1629 "Arithmetic operation should return same type as operands!");
1630 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1631 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1632 "Incorrect operand type (not integer) for S/UREM");
1635 assert(getType() == LHS->getType() &&
1636 "Arithmetic operation should return same type as operands!");
1637 assert(getType()->isFPOrFPVectorTy() &&
1638 "Incorrect operand type (not floating point) for FREM");
1643 assert(getType() == LHS->getType() &&
1644 "Shift operation should return same type as operands!");
1645 assert((getType()->isIntegerTy() ||
1646 (getType()->isVectorTy() &&
1647 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1648 "Tried to create a shift operation on a non-integral type!");
1652 assert(getType() == LHS->getType() &&
1653 "Logical operation should return same type as operands!");
1654 assert((getType()->isIntegerTy() ||
1655 (getType()->isVectorTy() &&
1656 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1657 "Tried to create a logical operation on a non-integral type!");
1665 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1667 Instruction *InsertBefore) {
1668 assert(S1->getType() == S2->getType() &&
1669 "Cannot create binary operator with two operands of differing type!");
1670 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1673 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1675 BasicBlock *InsertAtEnd) {
1676 BinaryOperator *Res = Create(Op, S1, S2, Name);
1677 InsertAtEnd->getInstList().push_back(Res);
1681 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1682 Instruction *InsertBefore) {
1683 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1684 return new BinaryOperator(Instruction::Sub,
1686 Op->getType(), Name, InsertBefore);
1689 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1690 BasicBlock *InsertAtEnd) {
1691 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1692 return new BinaryOperator(Instruction::Sub,
1694 Op->getType(), Name, InsertAtEnd);
1697 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1698 Instruction *InsertBefore) {
1699 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1700 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1703 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1704 BasicBlock *InsertAtEnd) {
1705 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1706 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1709 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1710 Instruction *InsertBefore) {
1711 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1712 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1715 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1716 BasicBlock *InsertAtEnd) {
1717 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1718 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1721 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1722 Instruction *InsertBefore) {
1723 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1724 return new BinaryOperator(Instruction::FSub,
1726 Op->getType(), Name, InsertBefore);
1729 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1730 BasicBlock *InsertAtEnd) {
1731 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1732 return new BinaryOperator(Instruction::FSub,
1734 Op->getType(), Name, InsertAtEnd);
1737 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1738 Instruction *InsertBefore) {
1740 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1741 C = Constant::getAllOnesValue(PTy->getElementType());
1742 C = ConstantVector::get(
1743 std::vector<Constant*>(PTy->getNumElements(), C));
1745 C = Constant::getAllOnesValue(Op->getType());
1748 return new BinaryOperator(Instruction::Xor, Op, C,
1749 Op->getType(), Name, InsertBefore);
1752 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1753 BasicBlock *InsertAtEnd) {
1755 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1756 // Create a vector of all ones values.
1757 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
1758 AllOnes = ConstantVector::get(
1759 std::vector<Constant*>(PTy->getNumElements(), Elt));
1761 AllOnes = Constant::getAllOnesValue(Op->getType());
1764 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1765 Op->getType(), Name, InsertAtEnd);
1769 // isConstantAllOnes - Helper function for several functions below
1770 static inline bool isConstantAllOnes(const Value *V) {
1771 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1772 return CI->isAllOnesValue();
1773 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1774 return CV->isAllOnesValue();
1778 bool BinaryOperator::isNeg(const Value *V) {
1779 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1780 if (Bop->getOpcode() == Instruction::Sub)
1781 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1782 return C->isNegativeZeroValue();
1786 bool BinaryOperator::isFNeg(const Value *V) {
1787 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1788 if (Bop->getOpcode() == Instruction::FSub)
1789 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1790 return C->isNegativeZeroValue();
1794 bool BinaryOperator::isNot(const Value *V) {
1795 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1796 return (Bop->getOpcode() == Instruction::Xor &&
1797 (isConstantAllOnes(Bop->getOperand(1)) ||
1798 isConstantAllOnes(Bop->getOperand(0))));
1802 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1803 return cast<BinaryOperator>(BinOp)->getOperand(1);
1806 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1807 return getNegArgument(const_cast<Value*>(BinOp));
1810 Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1811 return cast<BinaryOperator>(BinOp)->getOperand(1);
1814 const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1815 return getFNegArgument(const_cast<Value*>(BinOp));
1818 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1819 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1820 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1821 Value *Op0 = BO->getOperand(0);
1822 Value *Op1 = BO->getOperand(1);
1823 if (isConstantAllOnes(Op0)) return Op1;
1825 assert(isConstantAllOnes(Op1));
1829 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1830 return getNotArgument(const_cast<Value*>(BinOp));
1834 // swapOperands - Exchange the two operands to this instruction. This
1835 // instruction is safe to use on any binary instruction and does not
1836 // modify the semantics of the instruction. If the instruction is
1837 // order dependent (SetLT f.e.) the opcode is changed.
1839 bool BinaryOperator::swapOperands() {
1840 if (!isCommutative())
1841 return true; // Can't commute operands
1842 Op<0>().swap(Op<1>());
1846 void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1847 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1850 void BinaryOperator::setHasNoSignedWrap(bool b) {
1851 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1854 void BinaryOperator::setIsExact(bool b) {
1855 cast<SDivOperator>(this)->setIsExact(b);
1858 bool BinaryOperator::hasNoUnsignedWrap() const {
1859 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1862 bool BinaryOperator::hasNoSignedWrap() const {
1863 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1866 bool BinaryOperator::isExact() const {
1867 return cast<SDivOperator>(this)->isExact();
1870 //===----------------------------------------------------------------------===//
1872 //===----------------------------------------------------------------------===//
1874 // Just determine if this cast only deals with integral->integral conversion.
1875 bool CastInst::isIntegerCast() const {
1876 switch (getOpcode()) {
1877 default: return false;
1878 case Instruction::ZExt:
1879 case Instruction::SExt:
1880 case Instruction::Trunc:
1882 case Instruction::BitCast:
1883 return getOperand(0)->getType()->isIntegerTy() &&
1884 getType()->isIntegerTy();
1888 bool CastInst::isLosslessCast() const {
1889 // Only BitCast can be lossless, exit fast if we're not BitCast
1890 if (getOpcode() != Instruction::BitCast)
1893 // Identity cast is always lossless
1894 const Type* SrcTy = getOperand(0)->getType();
1895 const Type* DstTy = getType();
1899 // Pointer to pointer is always lossless.
1900 if (SrcTy->isPointerTy())
1901 return DstTy->isPointerTy();
1902 return false; // Other types have no identity values
1905 /// This function determines if the CastInst does not require any bits to be
1906 /// changed in order to effect the cast. Essentially, it identifies cases where
1907 /// no code gen is necessary for the cast, hence the name no-op cast. For
1908 /// example, the following are all no-op casts:
1909 /// # bitcast i32* %x to i8*
1910 /// # bitcast <2 x i32> %x to <4 x i16>
1911 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1912 /// @brief Determine if the described cast is a no-op.
1913 bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1916 const Type *IntPtrTy) {
1919 assert(!"Invalid CastOp");
1920 case Instruction::Trunc:
1921 case Instruction::ZExt:
1922 case Instruction::SExt:
1923 case Instruction::FPTrunc:
1924 case Instruction::FPExt:
1925 case Instruction::UIToFP:
1926 case Instruction::SIToFP:
1927 case Instruction::FPToUI:
1928 case Instruction::FPToSI:
1929 return false; // These always modify bits
1930 case Instruction::BitCast:
1931 return true; // BitCast never modifies bits.
1932 case Instruction::PtrToInt:
1933 return IntPtrTy->getScalarSizeInBits() ==
1934 DestTy->getScalarSizeInBits();
1935 case Instruction::IntToPtr:
1936 return IntPtrTy->getScalarSizeInBits() ==
1937 SrcTy->getScalarSizeInBits();
1941 /// @brief Determine if a cast is a no-op.
1942 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1943 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1946 /// This function determines if a pair of casts can be eliminated and what
1947 /// opcode should be used in the elimination. This assumes that there are two
1948 /// instructions like this:
1949 /// * %F = firstOpcode SrcTy %x to MidTy
1950 /// * %S = secondOpcode MidTy %F to DstTy
1951 /// The function returns a resultOpcode so these two casts can be replaced with:
1952 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1953 /// If no such cast is permited, the function returns 0.
1954 unsigned CastInst::isEliminableCastPair(
1955 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1956 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1958 // Define the 144 possibilities for these two cast instructions. The values
1959 // in this matrix determine what to do in a given situation and select the
1960 // case in the switch below. The rows correspond to firstOp, the columns
1961 // correspond to secondOp. In looking at the table below, keep in mind
1962 // the following cast properties:
1964 // Size Compare Source Destination
1965 // Operator Src ? Size Type Sign Type Sign
1966 // -------- ------------ ------------------- ---------------------
1967 // TRUNC > Integer Any Integral Any
1968 // ZEXT < Integral Unsigned Integer Any
1969 // SEXT < Integral Signed Integer Any
1970 // FPTOUI n/a FloatPt n/a Integral Unsigned
1971 // FPTOSI n/a FloatPt n/a Integral Signed
1972 // UITOFP n/a Integral Unsigned FloatPt n/a
1973 // SITOFP n/a Integral Signed FloatPt n/a
1974 // FPTRUNC > FloatPt n/a FloatPt n/a
1975 // FPEXT < FloatPt n/a FloatPt n/a
1976 // PTRTOINT n/a Pointer n/a Integral Unsigned
1977 // INTTOPTR n/a Integral Unsigned Pointer n/a
1978 // BITCAST = FirstClass n/a FirstClass n/a
1980 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1981 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1982 // into "fptoui double to i64", but this loses information about the range
1983 // of the produced value (we no longer know the top-part is all zeros).
1984 // Further this conversion is often much more expensive for typical hardware,
1985 // and causes issues when building libgcc. We disallow fptosi+sext for the
1987 const unsigned numCastOps =
1988 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1989 static const uint8_t CastResults[numCastOps][numCastOps] = {
1990 // T F F U S F F P I B -+
1991 // R Z S P P I I T P 2 N T |
1992 // U E E 2 2 2 2 R E I T C +- secondOp
1993 // N X X U S F F N X N 2 V |
1994 // C T T I I P P C T T P T -+
1995 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1996 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1997 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
1998 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1999 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
2000 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2001 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2002 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2003 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2004 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2005 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2006 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2009 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2010 [secondOp-Instruction::CastOpsBegin];
2013 // categorically disallowed
2016 // allowed, use first cast's opcode
2019 // allowed, use second cast's opcode
2022 // no-op cast in second op implies firstOp as long as the DestTy
2023 // is integer and we are not converting between a vector and a
2025 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
2029 // no-op cast in second op implies firstOp as long as the DestTy
2030 // is floating point.
2031 if (DstTy->isFloatingPointTy())
2035 // no-op cast in first op implies secondOp as long as the SrcTy
2037 if (SrcTy->isIntegerTy())
2041 // no-op cast in first op implies secondOp as long as the SrcTy
2042 // is a floating point.
2043 if (SrcTy->isFloatingPointTy())
2047 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
2050 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2051 unsigned MidSize = MidTy->getScalarSizeInBits();
2052 if (MidSize >= PtrSize)
2053 return Instruction::BitCast;
2057 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2058 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2059 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2060 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2061 unsigned DstSize = DstTy->getScalarSizeInBits();
2062 if (SrcSize == DstSize)
2063 return Instruction::BitCast;
2064 else if (SrcSize < DstSize)
2068 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2069 return Instruction::ZExt;
2071 // fpext followed by ftrunc is allowed if the bit size returned to is
2072 // the same as the original, in which case its just a bitcast
2074 return Instruction::BitCast;
2075 return 0; // If the types are not the same we can't eliminate it.
2077 // bitcast followed by ptrtoint is allowed as long as the bitcast
2078 // is a pointer to pointer cast.
2079 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
2083 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2084 if (MidTy->isPointerTy() && DstTy->isPointerTy())
2088 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2091 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2092 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2093 unsigned DstSize = DstTy->getScalarSizeInBits();
2094 if (SrcSize <= PtrSize && SrcSize == DstSize)
2095 return Instruction::BitCast;
2099 // cast combination can't happen (error in input). This is for all cases
2100 // where the MidTy is not the same for the two cast instructions.
2101 assert(!"Invalid Cast Combination");
2104 assert(!"Error in CastResults table!!!");
2110 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2111 const Twine &Name, Instruction *InsertBefore) {
2112 // Construct and return the appropriate CastInst subclass
2114 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2115 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2116 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2117 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2118 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2119 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2120 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2121 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2122 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2123 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2124 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2125 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2127 assert(!"Invalid opcode provided");
2132 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2133 const Twine &Name, BasicBlock *InsertAtEnd) {
2134 // Construct and return the appropriate CastInst subclass
2136 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2137 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2138 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2139 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2140 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2141 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2142 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2143 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2144 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2145 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2146 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2147 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2149 assert(!"Invalid opcode provided");
2154 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2156 Instruction *InsertBefore) {
2157 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2158 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2159 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
2162 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2164 BasicBlock *InsertAtEnd) {
2165 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2166 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2167 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
2170 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2172 Instruction *InsertBefore) {
2173 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2174 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2175 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
2178 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2180 BasicBlock *InsertAtEnd) {
2181 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2182 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2183 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
2186 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2188 Instruction *InsertBefore) {
2189 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2190 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2191 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
2194 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2196 BasicBlock *InsertAtEnd) {
2197 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2198 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2199 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
2202 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2204 BasicBlock *InsertAtEnd) {
2205 assert(S->getType()->isPointerTy() && "Invalid cast");
2206 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2209 if (Ty->isIntegerTy())
2210 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2211 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2214 /// @brief Create a BitCast or a PtrToInt cast instruction
2215 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2217 Instruction *InsertBefore) {
2218 assert(S->getType()->isPointerTy() && "Invalid cast");
2219 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2222 if (Ty->isIntegerTy())
2223 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2224 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2227 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2228 bool isSigned, const Twine &Name,
2229 Instruction *InsertBefore) {
2230 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2231 "Invalid integer cast");
2232 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2233 unsigned DstBits = Ty->getScalarSizeInBits();
2234 Instruction::CastOps opcode =
2235 (SrcBits == DstBits ? Instruction::BitCast :
2236 (SrcBits > DstBits ? Instruction::Trunc :
2237 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2238 return Create(opcode, C, Ty, Name, InsertBefore);
2241 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2242 bool isSigned, const Twine &Name,
2243 BasicBlock *InsertAtEnd) {
2244 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2246 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2247 unsigned DstBits = Ty->getScalarSizeInBits();
2248 Instruction::CastOps opcode =
2249 (SrcBits == DstBits ? Instruction::BitCast :
2250 (SrcBits > DstBits ? Instruction::Trunc :
2251 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2252 return Create(opcode, C, Ty, Name, InsertAtEnd);
2255 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2257 Instruction *InsertBefore) {
2258 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2260 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2261 unsigned DstBits = Ty->getScalarSizeInBits();
2262 Instruction::CastOps opcode =
2263 (SrcBits == DstBits ? Instruction::BitCast :
2264 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2265 return Create(opcode, C, Ty, Name, InsertBefore);
2268 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2270 BasicBlock *InsertAtEnd) {
2271 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2273 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2274 unsigned DstBits = Ty->getScalarSizeInBits();
2275 Instruction::CastOps opcode =
2276 (SrcBits == DstBits ? Instruction::BitCast :
2277 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2278 return Create(opcode, C, Ty, Name, InsertAtEnd);
2281 // Check whether it is valid to call getCastOpcode for these types.
2282 // This routine must be kept in sync with getCastOpcode.
2283 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2284 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2287 if (SrcTy == DestTy)
2290 // Get the bit sizes, we'll need these
2291 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2292 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2294 // Run through the possibilities ...
2295 if (DestTy->isIntegerTy()) { // Casting to integral
2296 if (SrcTy->isIntegerTy()) { // Casting from integral
2298 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2300 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2301 // Casting from vector
2302 return DestBits == PTy->getBitWidth();
2303 } else { // Casting from something else
2304 return SrcTy->isPointerTy();
2306 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2307 if (SrcTy->isIntegerTy()) { // Casting from integral
2309 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2311 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2312 // Casting from vector
2313 return DestBits == PTy->getBitWidth();
2314 } else { // Casting from something else
2317 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2318 // Casting to vector
2319 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2320 // Casting from vector
2321 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2322 } else { // Casting from something else
2323 return DestPTy->getBitWidth() == SrcBits;
2325 } else if (DestTy->isPointerTy()) { // Casting to pointer
2326 if (SrcTy->isPointerTy()) { // Casting from pointer
2328 } else if (SrcTy->isIntegerTy()) { // Casting from integral
2330 } else { // Casting from something else
2333 } else { // Casting to something else
2338 // Provide a way to get a "cast" where the cast opcode is inferred from the
2339 // types and size of the operand. This, basically, is a parallel of the
2340 // logic in the castIsValid function below. This axiom should hold:
2341 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2342 // should not assert in castIsValid. In other words, this produces a "correct"
2343 // casting opcode for the arguments passed to it.
2344 // This routine must be kept in sync with isCastable.
2345 Instruction::CastOps
2346 CastInst::getCastOpcode(
2347 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
2348 // Get the bit sizes, we'll need these
2349 const Type *SrcTy = Src->getType();
2350 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2351 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2353 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2354 "Only first class types are castable!");
2356 // Run through the possibilities ...
2357 if (DestTy->isIntegerTy()) { // Casting to integral
2358 if (SrcTy->isIntegerTy()) { // Casting from integral
2359 if (DestBits < SrcBits)
2360 return Trunc; // int -> smaller int
2361 else if (DestBits > SrcBits) { // its an extension
2363 return SExt; // signed -> SEXT
2365 return ZExt; // unsigned -> ZEXT
2367 return BitCast; // Same size, No-op cast
2369 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2371 return FPToSI; // FP -> sint
2373 return FPToUI; // FP -> uint
2374 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2375 assert(DestBits == PTy->getBitWidth() &&
2376 "Casting vector to integer of different width");
2378 return BitCast; // Same size, no-op cast
2380 assert(SrcTy->isPointerTy() &&
2381 "Casting from a value that is not first-class type");
2382 return PtrToInt; // ptr -> int
2384 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2385 if (SrcTy->isIntegerTy()) { // Casting from integral
2387 return SIToFP; // sint -> FP
2389 return UIToFP; // uint -> FP
2390 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2391 if (DestBits < SrcBits) {
2392 return FPTrunc; // FP -> smaller FP
2393 } else if (DestBits > SrcBits) {
2394 return FPExt; // FP -> larger FP
2396 return BitCast; // same size, no-op cast
2398 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2399 assert(DestBits == PTy->getBitWidth() &&
2400 "Casting vector to floating point of different width");
2402 return BitCast; // same size, no-op cast
2404 llvm_unreachable("Casting pointer or non-first class to float");
2406 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2407 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2408 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
2409 "Casting vector to vector of different widths");
2411 return BitCast; // vector -> vector
2412 } else if (DestPTy->getBitWidth() == SrcBits) {
2413 return BitCast; // float/int -> vector
2415 assert(!"Illegal cast to vector (wrong type or size)");
2417 } else if (DestTy->isPointerTy()) {
2418 if (SrcTy->isPointerTy()) {
2419 return BitCast; // ptr -> ptr
2420 } else if (SrcTy->isIntegerTy()) {
2421 return IntToPtr; // int -> ptr
2423 assert(!"Casting pointer to other than pointer or int");
2426 assert(!"Casting to type that is not first-class");
2429 // If we fall through to here we probably hit an assertion cast above
2430 // and assertions are not turned on. Anything we return is an error, so
2431 // BitCast is as good a choice as any.
2435 //===----------------------------------------------------------------------===//
2436 // CastInst SubClass Constructors
2437 //===----------------------------------------------------------------------===//
2439 /// Check that the construction parameters for a CastInst are correct. This
2440 /// could be broken out into the separate constructors but it is useful to have
2441 /// it in one place and to eliminate the redundant code for getting the sizes
2442 /// of the types involved.
2444 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
2446 // Check for type sanity on the arguments
2447 const Type *SrcTy = S->getType();
2448 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2449 SrcTy->isAggregateType() || DstTy->isAggregateType())
2452 // Get the size of the types in bits, we'll need this later
2453 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2454 unsigned DstBitSize = DstTy->getScalarSizeInBits();
2456 // Switch on the opcode provided
2458 default: return false; // This is an input error
2459 case Instruction::Trunc:
2460 return SrcTy->isIntOrIntVectorTy() &&
2461 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
2462 case Instruction::ZExt:
2463 return SrcTy->isIntOrIntVectorTy() &&
2464 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2465 case Instruction::SExt:
2466 return SrcTy->isIntOrIntVectorTy() &&
2467 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2468 case Instruction::FPTrunc:
2469 return SrcTy->isFPOrFPVectorTy() &&
2470 DstTy->isFPOrFPVectorTy() &&
2471 SrcBitSize > DstBitSize;
2472 case Instruction::FPExt:
2473 return SrcTy->isFPOrFPVectorTy() &&
2474 DstTy->isFPOrFPVectorTy() &&
2475 SrcBitSize < DstBitSize;
2476 case Instruction::UIToFP:
2477 case Instruction::SIToFP:
2478 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2479 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2480 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2481 DVTy->getElementType()->isFPOrFPVectorTy() &&
2482 SVTy->getNumElements() == DVTy->getNumElements();
2485 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
2486 case Instruction::FPToUI:
2487 case Instruction::FPToSI:
2488 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2489 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2490 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2491 DVTy->getElementType()->isIntOrIntVectorTy() &&
2492 SVTy->getNumElements() == DVTy->getNumElements();
2495 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
2496 case Instruction::PtrToInt:
2497 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
2498 case Instruction::IntToPtr:
2499 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
2500 case Instruction::BitCast:
2501 // BitCast implies a no-op cast of type only. No bits change.
2502 // However, you can't cast pointers to anything but pointers.
2503 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
2506 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2507 // these cases, the cast is okay if the source and destination bit widths
2509 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2513 TruncInst::TruncInst(
2514 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2515 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2516 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2519 TruncInst::TruncInst(
2520 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2521 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2522 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2526 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2527 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2528 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2532 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2533 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2534 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2537 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2538 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2539 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2543 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2544 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2545 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2548 FPTruncInst::FPTruncInst(
2549 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2550 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2551 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2554 FPTruncInst::FPTruncInst(
2555 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2556 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2557 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2560 FPExtInst::FPExtInst(
2561 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2562 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2563 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2566 FPExtInst::FPExtInst(
2567 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2568 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2569 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2572 UIToFPInst::UIToFPInst(
2573 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2574 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2575 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2578 UIToFPInst::UIToFPInst(
2579 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2580 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2584 SIToFPInst::SIToFPInst(
2585 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2586 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2590 SIToFPInst::SIToFPInst(
2591 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2592 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2596 FPToUIInst::FPToUIInst(
2597 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2598 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2602 FPToUIInst::FPToUIInst(
2603 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2604 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2608 FPToSIInst::FPToSIInst(
2609 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2610 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2614 FPToSIInst::FPToSIInst(
2615 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2616 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2617 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2620 PtrToIntInst::PtrToIntInst(
2621 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2622 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2623 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2626 PtrToIntInst::PtrToIntInst(
2627 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2628 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2629 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2632 IntToPtrInst::IntToPtrInst(
2633 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2634 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2635 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2638 IntToPtrInst::IntToPtrInst(
2639 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2640 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2641 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2644 BitCastInst::BitCastInst(
2645 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2646 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2647 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2650 BitCastInst::BitCastInst(
2651 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2652 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2653 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2656 //===----------------------------------------------------------------------===//
2658 //===----------------------------------------------------------------------===//
2660 void CmpInst::Anchor() const {}
2662 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2663 Value *LHS, Value *RHS, const Twine &Name,
2664 Instruction *InsertBefore)
2665 : Instruction(ty, op,
2666 OperandTraits<CmpInst>::op_begin(this),
2667 OperandTraits<CmpInst>::operands(this),
2671 setPredicate((Predicate)predicate);
2675 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2676 Value *LHS, Value *RHS, const Twine &Name,
2677 BasicBlock *InsertAtEnd)
2678 : Instruction(ty, op,
2679 OperandTraits<CmpInst>::op_begin(this),
2680 OperandTraits<CmpInst>::operands(this),
2684 setPredicate((Predicate)predicate);
2689 CmpInst::Create(OtherOps Op, unsigned short predicate,
2690 Value *S1, Value *S2,
2691 const Twine &Name, Instruction *InsertBefore) {
2692 if (Op == Instruction::ICmp) {
2694 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2697 return new ICmpInst(CmpInst::Predicate(predicate),
2702 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2705 return new FCmpInst(CmpInst::Predicate(predicate),
2710 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2711 const Twine &Name, BasicBlock *InsertAtEnd) {
2712 if (Op == Instruction::ICmp) {
2713 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2716 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2720 void CmpInst::swapOperands() {
2721 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2724 cast<FCmpInst>(this)->swapOperands();
2727 bool CmpInst::isCommutative() {
2728 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2729 return IC->isCommutative();
2730 return cast<FCmpInst>(this)->isCommutative();
2733 bool CmpInst::isEquality() {
2734 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2735 return IC->isEquality();
2736 return cast<FCmpInst>(this)->isEquality();
2740 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
2742 default: assert(!"Unknown cmp predicate!");
2743 case ICMP_EQ: return ICMP_NE;
2744 case ICMP_NE: return ICMP_EQ;
2745 case ICMP_UGT: return ICMP_ULE;
2746 case ICMP_ULT: return ICMP_UGE;
2747 case ICMP_UGE: return ICMP_ULT;
2748 case ICMP_ULE: return ICMP_UGT;
2749 case ICMP_SGT: return ICMP_SLE;
2750 case ICMP_SLT: return ICMP_SGE;
2751 case ICMP_SGE: return ICMP_SLT;
2752 case ICMP_SLE: return ICMP_SGT;
2754 case FCMP_OEQ: return FCMP_UNE;
2755 case FCMP_ONE: return FCMP_UEQ;
2756 case FCMP_OGT: return FCMP_ULE;
2757 case FCMP_OLT: return FCMP_UGE;
2758 case FCMP_OGE: return FCMP_ULT;
2759 case FCMP_OLE: return FCMP_UGT;
2760 case FCMP_UEQ: return FCMP_ONE;
2761 case FCMP_UNE: return FCMP_OEQ;
2762 case FCMP_UGT: return FCMP_OLE;
2763 case FCMP_ULT: return FCMP_OGE;
2764 case FCMP_UGE: return FCMP_OLT;
2765 case FCMP_ULE: return FCMP_OGT;
2766 case FCMP_ORD: return FCMP_UNO;
2767 case FCMP_UNO: return FCMP_ORD;
2768 case FCMP_TRUE: return FCMP_FALSE;
2769 case FCMP_FALSE: return FCMP_TRUE;
2773 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2775 default: assert(! "Unknown icmp predicate!");
2776 case ICMP_EQ: case ICMP_NE:
2777 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2779 case ICMP_UGT: return ICMP_SGT;
2780 case ICMP_ULT: return ICMP_SLT;
2781 case ICMP_UGE: return ICMP_SGE;
2782 case ICMP_ULE: return ICMP_SLE;
2786 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2788 default: assert(! "Unknown icmp predicate!");
2789 case ICMP_EQ: case ICMP_NE:
2790 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2792 case ICMP_SGT: return ICMP_UGT;
2793 case ICMP_SLT: return ICMP_ULT;
2794 case ICMP_SGE: return ICMP_UGE;
2795 case ICMP_SLE: return ICMP_ULE;
2799 /// Initialize a set of values that all satisfy the condition with C.
2802 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2805 uint32_t BitWidth = C.getBitWidth();
2807 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
2808 case ICmpInst::ICMP_EQ: Upper++; break;
2809 case ICmpInst::ICMP_NE: Lower++; break;
2810 case ICmpInst::ICMP_ULT:
2811 Lower = APInt::getMinValue(BitWidth);
2812 // Check for an empty-set condition.
2814 return ConstantRange(BitWidth, /*isFullSet=*/false);
2816 case ICmpInst::ICMP_SLT:
2817 Lower = APInt::getSignedMinValue(BitWidth);
2818 // Check for an empty-set condition.
2820 return ConstantRange(BitWidth, /*isFullSet=*/false);
2822 case ICmpInst::ICMP_UGT:
2823 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2824 // Check for an empty-set condition.
2826 return ConstantRange(BitWidth, /*isFullSet=*/false);
2828 case ICmpInst::ICMP_SGT:
2829 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2830 // Check for an empty-set condition.
2832 return ConstantRange(BitWidth, /*isFullSet=*/false);
2834 case ICmpInst::ICMP_ULE:
2835 Lower = APInt::getMinValue(BitWidth); Upper++;
2836 // Check for a full-set condition.
2838 return ConstantRange(BitWidth, /*isFullSet=*/true);
2840 case ICmpInst::ICMP_SLE:
2841 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2842 // Check for a full-set condition.
2844 return ConstantRange(BitWidth, /*isFullSet=*/true);
2846 case ICmpInst::ICMP_UGE:
2847 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2848 // Check for a full-set condition.
2850 return ConstantRange(BitWidth, /*isFullSet=*/true);
2852 case ICmpInst::ICMP_SGE:
2853 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2854 // Check for a full-set condition.
2856 return ConstantRange(BitWidth, /*isFullSet=*/true);
2859 return ConstantRange(Lower, Upper);
2862 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
2864 default: assert(!"Unknown cmp predicate!");
2865 case ICMP_EQ: case ICMP_NE:
2867 case ICMP_SGT: return ICMP_SLT;
2868 case ICMP_SLT: return ICMP_SGT;
2869 case ICMP_SGE: return ICMP_SLE;
2870 case ICMP_SLE: return ICMP_SGE;
2871 case ICMP_UGT: return ICMP_ULT;
2872 case ICMP_ULT: return ICMP_UGT;
2873 case ICMP_UGE: return ICMP_ULE;
2874 case ICMP_ULE: return ICMP_UGE;
2876 case FCMP_FALSE: case FCMP_TRUE:
2877 case FCMP_OEQ: case FCMP_ONE:
2878 case FCMP_UEQ: case FCMP_UNE:
2879 case FCMP_ORD: case FCMP_UNO:
2881 case FCMP_OGT: return FCMP_OLT;
2882 case FCMP_OLT: return FCMP_OGT;
2883 case FCMP_OGE: return FCMP_OLE;
2884 case FCMP_OLE: return FCMP_OGE;
2885 case FCMP_UGT: return FCMP_ULT;
2886 case FCMP_ULT: return FCMP_UGT;
2887 case FCMP_UGE: return FCMP_ULE;
2888 case FCMP_ULE: return FCMP_UGE;
2892 bool CmpInst::isUnsigned(unsigned short predicate) {
2893 switch (predicate) {
2894 default: return false;
2895 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2896 case ICmpInst::ICMP_UGE: return true;
2900 bool CmpInst::isSigned(unsigned short predicate) {
2901 switch (predicate) {
2902 default: return false;
2903 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2904 case ICmpInst::ICMP_SGE: return true;
2908 bool CmpInst::isOrdered(unsigned short predicate) {
2909 switch (predicate) {
2910 default: return false;
2911 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2912 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2913 case FCmpInst::FCMP_ORD: return true;
2917 bool CmpInst::isUnordered(unsigned short predicate) {
2918 switch (predicate) {
2919 default: return false;
2920 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2921 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2922 case FCmpInst::FCMP_UNO: return true;
2926 bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2928 default: return false;
2929 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2930 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2934 bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2936 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2937 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2938 default: return false;
2943 //===----------------------------------------------------------------------===//
2944 // SwitchInst Implementation
2945 //===----------------------------------------------------------------------===//
2947 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
2948 assert(Value && Default);
2949 ReservedSpace = 2+NumCases*2;
2951 OperandList = allocHungoffUses(ReservedSpace);
2953 OperandList[0] = Value;
2954 OperandList[1] = Default;
2957 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2958 /// switch on and a default destination. The number of additional cases can
2959 /// be specified here to make memory allocation more efficient. This
2960 /// constructor can also autoinsert before another instruction.
2961 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2962 Instruction *InsertBefore)
2963 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2964 0, 0, InsertBefore) {
2965 init(Value, Default, NumCases);
2968 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2969 /// switch on and a default destination. The number of additional cases can
2970 /// be specified here to make memory allocation more efficient. This
2971 /// constructor also autoinserts at the end of the specified BasicBlock.
2972 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2973 BasicBlock *InsertAtEnd)
2974 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2975 0, 0, InsertAtEnd) {
2976 init(Value, Default, NumCases);
2979 SwitchInst::SwitchInst(const SwitchInst &SI)
2980 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
2981 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
2982 Use *OL = OperandList, *InOL = SI.OperandList;
2983 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2985 OL[i+1] = InOL[i+1];
2987 SubclassOptionalData = SI.SubclassOptionalData;
2990 SwitchInst::~SwitchInst() {
2991 dropHungoffUses(OperandList);
2995 /// addCase - Add an entry to the switch instruction...
2997 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
2998 unsigned OpNo = NumOperands;
2999 if (OpNo+2 > ReservedSpace)
3000 resizeOperands(0); // Get more space!
3001 // Initialize some new operands.
3002 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
3003 NumOperands = OpNo+2;
3004 OperandList[OpNo] = OnVal;
3005 OperandList[OpNo+1] = Dest;
3008 /// removeCase - This method removes the specified successor from the switch
3009 /// instruction. Note that this cannot be used to remove the default
3010 /// destination (successor #0).
3012 void SwitchInst::removeCase(unsigned idx) {
3013 assert(idx != 0 && "Cannot remove the default case!");
3014 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3016 unsigned NumOps = getNumOperands();
3017 Use *OL = OperandList;
3019 // Move everything after this operand down.
3021 // FIXME: we could just swap with the end of the list, then erase. However,
3022 // client might not expect this to happen. The code as it is thrashes the
3023 // use/def lists, which is kinda lame.
3024 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3026 OL[i-2+1] = OL[i+1];
3029 // Nuke the last value.
3030 OL[NumOps-2].set(0);
3031 OL[NumOps-2+1].set(0);
3032 NumOperands = NumOps-2;
3035 /// resizeOperands - resize operands - This adjusts the length of the operands
3036 /// list according to the following behavior:
3037 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3038 /// of operation. This grows the number of ops by 3 times.
3039 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3040 /// 3. If NumOps == NumOperands, trim the reserved space.
3042 void SwitchInst::resizeOperands(unsigned NumOps) {
3043 unsigned e = getNumOperands();
3046 } else if (NumOps*2 > NumOperands) {
3047 // No resize needed.
3048 if (ReservedSpace >= NumOps) return;
3049 } else if (NumOps == NumOperands) {
3050 if (ReservedSpace == NumOps) return;
3055 ReservedSpace = NumOps;
3056 Use *NewOps = allocHungoffUses(NumOps);
3057 Use *OldOps = OperandList;
3058 for (unsigned i = 0; i != e; ++i) {
3059 NewOps[i] = OldOps[i];
3061 OperandList = NewOps;
3062 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3066 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3067 return getSuccessor(idx);
3069 unsigned SwitchInst::getNumSuccessorsV() const {
3070 return getNumSuccessors();
3072 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3073 setSuccessor(idx, B);
3076 //===----------------------------------------------------------------------===//
3077 // SwitchInst Implementation
3078 //===----------------------------------------------------------------------===//
3080 void IndirectBrInst::init(Value *Address, unsigned NumDests) {
3081 assert(Address && Address->getType()->isPointerTy() &&
3082 "Address of indirectbr must be a pointer");
3083 ReservedSpace = 1+NumDests;
3085 OperandList = allocHungoffUses(ReservedSpace);
3087 OperandList[0] = Address;
3091 /// resizeOperands - resize operands - This adjusts the length of the operands
3092 /// list according to the following behavior:
3093 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3094 /// of operation. This grows the number of ops by 2 times.
3095 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3096 /// 3. If NumOps == NumOperands, trim the reserved space.
3098 void IndirectBrInst::resizeOperands(unsigned NumOps) {
3099 unsigned e = getNumOperands();
3102 } else if (NumOps*2 > NumOperands) {
3103 // No resize needed.
3104 if (ReservedSpace >= NumOps) return;
3105 } else if (NumOps == NumOperands) {
3106 if (ReservedSpace == NumOps) return;
3111 ReservedSpace = NumOps;
3112 Use *NewOps = allocHungoffUses(NumOps);
3113 Use *OldOps = OperandList;
3114 for (unsigned i = 0; i != e; ++i)
3115 NewOps[i] = OldOps[i];
3116 OperandList = NewOps;
3117 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3120 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3121 Instruction *InsertBefore)
3122 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3123 0, 0, InsertBefore) {
3124 init(Address, NumCases);
3127 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3128 BasicBlock *InsertAtEnd)
3129 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3130 0, 0, InsertAtEnd) {
3131 init(Address, NumCases);
3134 IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3135 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3136 allocHungoffUses(IBI.getNumOperands()),
3137 IBI.getNumOperands()) {
3138 Use *OL = OperandList, *InOL = IBI.OperandList;
3139 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3141 SubclassOptionalData = IBI.SubclassOptionalData;
3144 IndirectBrInst::~IndirectBrInst() {
3145 dropHungoffUses(OperandList);
3148 /// addDestination - Add a destination.
3150 void IndirectBrInst::addDestination(BasicBlock *DestBB) {
3151 unsigned OpNo = NumOperands;
3152 if (OpNo+1 > ReservedSpace)
3153 resizeOperands(0); // Get more space!
3154 // Initialize some new operands.
3155 assert(OpNo < ReservedSpace && "Growing didn't work!");
3156 NumOperands = OpNo+1;
3157 OperandList[OpNo] = DestBB;
3160 /// removeDestination - This method removes the specified successor from the
3161 /// indirectbr instruction.
3162 void IndirectBrInst::removeDestination(unsigned idx) {
3163 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3165 unsigned NumOps = getNumOperands();
3166 Use *OL = OperandList;
3168 // Replace this value with the last one.
3169 OL[idx+1] = OL[NumOps-1];
3171 // Nuke the last value.
3172 OL[NumOps-1].set(0);
3173 NumOperands = NumOps-1;
3176 BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
3177 return getSuccessor(idx);
3179 unsigned IndirectBrInst::getNumSuccessorsV() const {
3180 return getNumSuccessors();
3182 void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3183 setSuccessor(idx, B);
3186 //===----------------------------------------------------------------------===//
3187 // clone_impl() implementations
3188 //===----------------------------------------------------------------------===//
3190 // Define these methods here so vtables don't get emitted into every translation
3191 // unit that uses these classes.
3193 GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3194 return new (getNumOperands()) GetElementPtrInst(*this);
3197 BinaryOperator *BinaryOperator::clone_impl() const {
3198 return Create(getOpcode(), Op<0>(), Op<1>());
3201 FCmpInst* FCmpInst::clone_impl() const {
3202 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
3205 ICmpInst* ICmpInst::clone_impl() const {
3206 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
3209 ExtractValueInst *ExtractValueInst::clone_impl() const {
3210 return new ExtractValueInst(*this);
3213 InsertValueInst *InsertValueInst::clone_impl() const {
3214 return new InsertValueInst(*this);
3217 AllocaInst *AllocaInst::clone_impl() const {
3218 return new AllocaInst(getAllocatedType(),
3219 (Value*)getOperand(0),
3223 LoadInst *LoadInst::clone_impl() const {
3224 return new LoadInst(getOperand(0),
3225 Twine(), isVolatile(),
3229 StoreInst *StoreInst::clone_impl() const {
3230 return new StoreInst(getOperand(0), getOperand(1),
3231 isVolatile(), getAlignment());
3234 TruncInst *TruncInst::clone_impl() const {
3235 return new TruncInst(getOperand(0), getType());
3238 ZExtInst *ZExtInst::clone_impl() const {
3239 return new ZExtInst(getOperand(0), getType());
3242 SExtInst *SExtInst::clone_impl() const {
3243 return new SExtInst(getOperand(0), getType());
3246 FPTruncInst *FPTruncInst::clone_impl() const {
3247 return new FPTruncInst(getOperand(0), getType());
3250 FPExtInst *FPExtInst::clone_impl() const {
3251 return new FPExtInst(getOperand(0), getType());
3254 UIToFPInst *UIToFPInst::clone_impl() const {
3255 return new UIToFPInst(getOperand(0), getType());
3258 SIToFPInst *SIToFPInst::clone_impl() const {
3259 return new SIToFPInst(getOperand(0), getType());
3262 FPToUIInst *FPToUIInst::clone_impl() const {
3263 return new FPToUIInst(getOperand(0), getType());
3266 FPToSIInst *FPToSIInst::clone_impl() const {
3267 return new FPToSIInst(getOperand(0), getType());
3270 PtrToIntInst *PtrToIntInst::clone_impl() const {
3271 return new PtrToIntInst(getOperand(0), getType());
3274 IntToPtrInst *IntToPtrInst::clone_impl() const {
3275 return new IntToPtrInst(getOperand(0), getType());
3278 BitCastInst *BitCastInst::clone_impl() const {
3279 return new BitCastInst(getOperand(0), getType());
3282 CallInst *CallInst::clone_impl() const {
3283 return new(getNumOperands()) CallInst(*this);
3286 SelectInst *SelectInst::clone_impl() const {
3287 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
3290 VAArgInst *VAArgInst::clone_impl() const {
3291 return new VAArgInst(getOperand(0), getType());
3294 ExtractElementInst *ExtractElementInst::clone_impl() const {
3295 return ExtractElementInst::Create(getOperand(0), getOperand(1));
3298 InsertElementInst *InsertElementInst::clone_impl() const {
3299 return InsertElementInst::Create(getOperand(0),
3304 ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3305 return new ShuffleVectorInst(getOperand(0),
3310 PHINode *PHINode::clone_impl() const {
3311 return new PHINode(*this);
3314 ReturnInst *ReturnInst::clone_impl() const {
3315 return new(getNumOperands()) ReturnInst(*this);
3318 BranchInst *BranchInst::clone_impl() const {
3319 unsigned Ops(getNumOperands());
3320 return new(Ops, Ops == 1) BranchInst(*this);
3323 SwitchInst *SwitchInst::clone_impl() const {
3324 return new SwitchInst(*this);
3327 IndirectBrInst *IndirectBrInst::clone_impl() const {
3328 return new IndirectBrInst(*this);
3332 InvokeInst *InvokeInst::clone_impl() const {
3333 return new(getNumOperands()) InvokeInst(*this);
3336 UnwindInst *UnwindInst::clone_impl() const {
3337 LLVMContext &Context = getContext();
3338 return new UnwindInst(Context);
3341 UnreachableInst *UnreachableInst::clone_impl() const {
3342 LLVMContext &Context = getContext();
3343 return new UnreachableInst(Context);