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"
30 //===----------------------------------------------------------------------===//
32 //===----------------------------------------------------------------------===//
34 #define CALLSITE_DELEGATE_GETTER(METHOD) \
35 Instruction *II(getInstruction()); \
37 ? cast<CallInst>(II)->METHOD \
38 : cast<InvokeInst>(II)->METHOD
40 #define CALLSITE_DELEGATE_SETTER(METHOD) \
41 Instruction *II(getInstruction()); \
43 cast<CallInst>(II)->METHOD; \
45 cast<InvokeInst>(II)->METHOD
47 CallSite::CallSite(Instruction *C) {
48 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
50 I.setInt(isa<CallInst>(C));
52 CallingConv::ID CallSite::getCallingConv() const {
53 CALLSITE_DELEGATE_GETTER(getCallingConv());
55 void CallSite::setCallingConv(CallingConv::ID CC) {
56 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
58 const AttrListPtr &CallSite::getAttributes() const {
59 CALLSITE_DELEGATE_GETTER(getAttributes());
61 void CallSite::setAttributes(const AttrListPtr &PAL) {
62 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
64 bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
65 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
67 uint16_t CallSite::getParamAlignment(uint16_t i) const {
68 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
70 bool CallSite::doesNotAccessMemory() const {
71 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
73 void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
74 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
76 bool CallSite::onlyReadsMemory() const {
77 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
79 void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
80 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
82 bool CallSite::doesNotReturn() const {
83 CALLSITE_DELEGATE_GETTER(doesNotReturn());
85 void CallSite::setDoesNotReturn(bool doesNotReturn) {
86 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
88 bool CallSite::doesNotThrow() const {
89 CALLSITE_DELEGATE_GETTER(doesNotThrow());
91 void CallSite::setDoesNotThrow(bool doesNotThrow) {
92 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
95 bool CallSite::hasArgument(const Value *Arg) const {
96 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
102 #undef CALLSITE_DELEGATE_GETTER
103 #undef CALLSITE_DELEGATE_SETTER
105 //===----------------------------------------------------------------------===//
106 // TerminatorInst Class
107 //===----------------------------------------------------------------------===//
109 // Out of line virtual method, so the vtable, etc has a home.
110 TerminatorInst::~TerminatorInst() {
113 //===----------------------------------------------------------------------===//
114 // UnaryInstruction Class
115 //===----------------------------------------------------------------------===//
117 // Out of line virtual method, so the vtable, etc has a home.
118 UnaryInstruction::~UnaryInstruction() {
121 //===----------------------------------------------------------------------===//
123 //===----------------------------------------------------------------------===//
125 /// areInvalidOperands - Return a string if the specified operands are invalid
126 /// for a select operation, otherwise return null.
127 const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
128 if (Op1->getType() != Op2->getType())
129 return "both values to select must have same type";
131 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
133 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
134 return "vector select condition element type must be i1";
135 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
137 return "selected values for vector select must be vectors";
138 if (ET->getNumElements() != VT->getNumElements())
139 return "vector select requires selected vectors to have "
140 "the same vector length as select condition";
141 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
142 return "select condition must be i1 or <n x i1>";
148 //===----------------------------------------------------------------------===//
150 //===----------------------------------------------------------------------===//
152 PHINode::PHINode(const PHINode &PN)
153 : Instruction(PN.getType(), Instruction::PHI,
154 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
155 ReservedSpace(PN.getNumOperands()) {
156 Use *OL = OperandList;
157 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
158 OL[i] = PN.getOperand(i);
159 OL[i+1] = PN.getOperand(i+1);
161 SubclassOptionalData = PN.SubclassOptionalData;
164 PHINode::~PHINode() {
166 dropHungoffUses(OperandList);
169 // removeIncomingValue - Remove an incoming value. This is useful if a
170 // predecessor basic block is deleted.
171 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
172 unsigned NumOps = getNumOperands();
173 Use *OL = OperandList;
174 assert(Idx*2 < NumOps && "BB not in PHI node!");
175 Value *Removed = OL[Idx*2];
177 // Move everything after this operand down.
179 // FIXME: we could just swap with the end of the list, then erase. However,
180 // client might not expect this to happen. The code as it is thrashes the
181 // use/def lists, which is kinda lame.
182 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
187 // Nuke the last value.
189 OL[NumOps-2+1].set(0);
190 NumOperands = NumOps-2;
192 // If the PHI node is dead, because it has zero entries, nuke it now.
193 if (NumOps == 2 && DeletePHIIfEmpty) {
194 // If anyone is using this PHI, make them use a dummy value instead...
195 replaceAllUsesWith(UndefValue::get(getType()));
201 /// resizeOperands - resize operands - This adjusts the length of the operands
202 /// list according to the following behavior:
203 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
204 /// of operation. This grows the number of ops by 1.5 times.
205 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
206 /// 3. If NumOps == NumOperands, trim the reserved space.
208 void PHINode::resizeOperands(unsigned NumOps) {
209 unsigned e = getNumOperands();
212 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
213 } else if (NumOps*2 > NumOperands) {
215 if (ReservedSpace >= NumOps) return;
216 } else if (NumOps == NumOperands) {
217 if (ReservedSpace == NumOps) return;
222 ReservedSpace = NumOps;
223 Use *OldOps = OperandList;
224 Use *NewOps = allocHungoffUses(NumOps);
225 std::copy(OldOps, OldOps + e, NewOps);
226 OperandList = NewOps;
227 if (OldOps) Use::zap(OldOps, OldOps + e, true);
230 /// hasConstantValue - If the specified PHI node always merges together the same
231 /// value, return the value, otherwise return null.
233 /// If the PHI has undef operands, but all the rest of the operands are
234 /// some unique value, return that value if it can be proved that the
235 /// value dominates the PHI. If DT is null, use a conservative check,
236 /// otherwise use DT to test for dominance.
238 Value *PHINode::hasConstantValue(DominatorTree *DT) const {
239 // If the PHI node only has one incoming value, eliminate the PHI node.
240 if (getNumIncomingValues() == 1) {
241 if (getIncomingValue(0) != this) // not X = phi X
242 return getIncomingValue(0);
243 return UndefValue::get(getType()); // Self cycle is dead.
246 // Otherwise if all of the incoming values are the same for the PHI, replace
247 // the PHI node with the incoming value.
250 bool HasUndefInput = false;
251 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
252 if (isa<UndefValue>(getIncomingValue(i))) {
253 HasUndefInput = true;
254 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
255 if (InVal && getIncomingValue(i) != InVal)
256 return 0; // Not the same, bail out.
257 InVal = getIncomingValue(i);
260 // The only case that could cause InVal to be null is if we have a PHI node
261 // that only has entries for itself. In this case, there is no entry into the
262 // loop, so kill the PHI.
264 if (InVal == 0) InVal = UndefValue::get(getType());
266 // If we have a PHI node like phi(X, undef, X), where X is defined by some
267 // instruction, we cannot always return X as the result of the PHI node. Only
268 // do this if X is not an instruction (thus it must dominate the PHI block),
269 // or if the client is prepared to deal with this possibility.
270 if (!HasUndefInput || !isa<Instruction>(InVal))
273 Instruction *IV = cast<Instruction>(InVal);
275 // We have a DominatorTree. Do a precise test.
276 if (!DT->dominates(IV, this))
279 // If it is in the entry block, it obviously dominates everything.
280 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
282 return 0; // Cannot guarantee that InVal dominates this PHINode.
285 // All of the incoming values are the same, return the value now.
290 //===----------------------------------------------------------------------===//
291 // CallInst Implementation
292 //===----------------------------------------------------------------------===//
294 CallInst::~CallInst() {
297 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
298 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
299 Use *OL = OperandList;
302 const FunctionType *FTy =
303 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
304 FTy = FTy; // silence warning.
306 assert((NumParams == FTy->getNumParams() ||
307 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
308 "Calling a function with bad signature!");
309 for (unsigned i = 0; i != NumParams; ++i) {
310 assert((i >= FTy->getNumParams() ||
311 FTy->getParamType(i) == Params[i]->getType()) &&
312 "Calling a function with a bad signature!");
317 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
318 assert(NumOperands == 3 && "NumOperands not set up?");
319 Use *OL = OperandList;
324 const FunctionType *FTy =
325 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
326 FTy = FTy; // silence warning.
328 assert((FTy->getNumParams() == 2 ||
329 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
330 "Calling a function with bad signature");
331 assert((0 >= FTy->getNumParams() ||
332 FTy->getParamType(0) == Actual1->getType()) &&
333 "Calling a function with a bad signature!");
334 assert((1 >= FTy->getNumParams() ||
335 FTy->getParamType(1) == Actual2->getType()) &&
336 "Calling a function with a bad signature!");
339 void CallInst::init(Value *Func, Value *Actual) {
340 assert(NumOperands == 2 && "NumOperands not set up?");
341 Use *OL = OperandList;
345 const FunctionType *FTy =
346 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
347 FTy = FTy; // silence warning.
349 assert((FTy->getNumParams() == 1 ||
350 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
351 "Calling a function with bad signature");
352 assert((0 == FTy->getNumParams() ||
353 FTy->getParamType(0) == Actual->getType()) &&
354 "Calling a function with a bad signature!");
357 void CallInst::init(Value *Func) {
358 assert(NumOperands == 1 && "NumOperands not set up?");
359 Use *OL = OperandList;
362 const FunctionType *FTy =
363 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
364 FTy = FTy; // silence warning.
366 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
369 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
370 Instruction *InsertBefore)
371 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
372 ->getElementType())->getReturnType(),
374 OperandTraits<CallInst>::op_end(this) - 2,
380 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
381 BasicBlock *InsertAtEnd)
382 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
383 ->getElementType())->getReturnType(),
385 OperandTraits<CallInst>::op_end(this) - 2,
390 CallInst::CallInst(Value *Func, const Twine &Name,
391 Instruction *InsertBefore)
392 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
393 ->getElementType())->getReturnType(),
395 OperandTraits<CallInst>::op_end(this) - 1,
401 CallInst::CallInst(Value *Func, const Twine &Name,
402 BasicBlock *InsertAtEnd)
403 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
404 ->getElementType())->getReturnType(),
406 OperandTraits<CallInst>::op_end(this) - 1,
412 CallInst::CallInst(const CallInst &CI)
413 : Instruction(CI.getType(), Instruction::Call,
414 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
415 CI.getNumOperands()) {
416 setAttributes(CI.getAttributes());
417 SubclassData = CI.SubclassData;
418 Use *OL = OperandList;
419 Use *InOL = CI.OperandList;
420 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
422 SubclassOptionalData = CI.SubclassOptionalData;
425 void CallInst::addAttribute(unsigned i, Attributes attr) {
426 AttrListPtr PAL = getAttributes();
427 PAL = PAL.addAttr(i, attr);
431 void CallInst::removeAttribute(unsigned i, Attributes attr) {
432 AttrListPtr PAL = getAttributes();
433 PAL = PAL.removeAttr(i, attr);
437 bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
438 if (AttributeList.paramHasAttr(i, attr))
440 if (const Function *F = getCalledFunction())
441 return F->paramHasAttr(i, attr);
445 /// IsConstantOne - Return true only if val is constant int 1
446 static bool IsConstantOne(Value *val) {
447 assert(val && "IsConstantOne does not work with NULL val");
448 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
451 static Value *checkArraySize(Value *Amt, const Type *IntPtrTy) {
453 Amt = ConstantInt::get(IntPtrTy, 1);
455 assert(!isa<BasicBlock>(Amt) &&
456 "Passed basic block into malloc size parameter! Use other ctor");
457 assert(Amt->getType() == IntPtrTy &&
458 "Malloc array size is not an intptr!");
463 static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd,
464 const Type *IntPtrTy, const Type *AllocTy,
465 Value *ArraySize, const Twine &NameStr) {
466 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
467 "createMalloc needs either InsertBefore or InsertAtEnd");
469 // malloc(type) becomes:
470 // bitcast (i8* malloc(typeSize)) to type*
471 // malloc(type, arraySize) becomes:
472 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
473 Value *AllocSize = ConstantExpr::getSizeOf(AllocTy);
474 AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize),
476 ArraySize = checkArraySize(ArraySize, IntPtrTy);
478 if (!IsConstantOne(ArraySize)) {
479 if (IsConstantOne(AllocSize)) {
480 AllocSize = ArraySize; // Operand * 1 = Operand
481 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
482 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
484 // Malloc arg is constant product of type size and array size
485 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
487 // Multiply type size by the array size...
489 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
490 "mallocsize", InsertBefore);
492 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
493 "mallocsize", InsertAtEnd);
497 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
498 // Create the call to Malloc.
499 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
500 Module* M = BB->getParent()->getParent();
501 const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(BB->getContext()));
502 // prototype malloc as "void *malloc(size_t)"
503 Constant *MallocF = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
504 if (!cast<Function>(MallocF)->doesNotAlias(0))
505 cast<Function>(MallocF)->setDoesNotAlias(0);
506 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
507 CallInst *MCall = NULL;
510 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore);
511 // Create a cast instruction to convert to the right type...
512 MCast = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
514 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertAtEnd);
515 // Create a cast instruction to convert to the right type...
516 MCast = new BitCastInst(MCall, AllocPtrType, NameStr);
518 MCall->setTailCall();
519 assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
520 "Malloc has void return type");
525 /// CreateMalloc - Generate the IR for a call to malloc:
526 /// 1. Compute the malloc call's argument as the specified type's size,
527 /// possibly multiplied by the array size if the array size is not
529 /// 2. Call malloc with that argument.
530 /// 3. Bitcast the result of the malloc call to the specified type.
531 Value *CallInst::CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
532 const Type *AllocTy, Value *ArraySize,
534 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, ArraySize, Name);
537 /// CreateMalloc - Generate the IR for a call to malloc:
538 /// 1. Compute the malloc call's argument as the specified type's size,
539 /// possibly multiplied by the array size if the array size is not
541 /// 2. Call malloc with that argument.
542 /// 3. Bitcast the result of the malloc call to the specified type.
543 /// Note: This function does not add the bitcast to the basic block, that is the
544 /// responsibility of the caller.
545 Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
546 const Type *AllocTy, Value *ArraySize,
548 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, ArraySize, Name);
551 //===----------------------------------------------------------------------===//
552 // InvokeInst Implementation
553 //===----------------------------------------------------------------------===//
555 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
556 Value* const *Args, unsigned NumArgs) {
557 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
558 Use *OL = OperandList;
562 const FunctionType *FTy =
563 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
564 FTy = FTy; // silence warning.
566 assert(((NumArgs == FTy->getNumParams()) ||
567 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
568 "Calling a function with bad signature");
570 for (unsigned i = 0, e = NumArgs; i != e; i++) {
571 assert((i >= FTy->getNumParams() ||
572 FTy->getParamType(i) == Args[i]->getType()) &&
573 "Invoking a function with a bad signature!");
579 InvokeInst::InvokeInst(const InvokeInst &II)
580 : TerminatorInst(II.getType(), Instruction::Invoke,
581 OperandTraits<InvokeInst>::op_end(this)
582 - II.getNumOperands(),
583 II.getNumOperands()) {
584 setAttributes(II.getAttributes());
585 SubclassData = II.SubclassData;
586 Use *OL = OperandList, *InOL = II.OperandList;
587 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
589 SubclassOptionalData = II.SubclassOptionalData;
592 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
593 return getSuccessor(idx);
595 unsigned InvokeInst::getNumSuccessorsV() const {
596 return getNumSuccessors();
598 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
599 return setSuccessor(idx, B);
602 bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
603 if (AttributeList.paramHasAttr(i, attr))
605 if (const Function *F = getCalledFunction())
606 return F->paramHasAttr(i, attr);
610 void InvokeInst::addAttribute(unsigned i, Attributes attr) {
611 AttrListPtr PAL = getAttributes();
612 PAL = PAL.addAttr(i, attr);
616 void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
617 AttrListPtr PAL = getAttributes();
618 PAL = PAL.removeAttr(i, attr);
623 //===----------------------------------------------------------------------===//
624 // ReturnInst Implementation
625 //===----------------------------------------------------------------------===//
627 ReturnInst::ReturnInst(const ReturnInst &RI)
628 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
629 OperandTraits<ReturnInst>::op_end(this) -
631 RI.getNumOperands()) {
632 if (RI.getNumOperands())
633 Op<0>() = RI.Op<0>();
634 SubclassOptionalData = RI.SubclassOptionalData;
637 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
638 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
639 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
644 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
645 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
646 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
651 ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
652 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
653 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
656 unsigned ReturnInst::getNumSuccessorsV() const {
657 return getNumSuccessors();
660 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
661 /// emit the vtable for the class in this translation unit.
662 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
663 llvm_unreachable("ReturnInst has no successors!");
666 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
667 llvm_unreachable("ReturnInst has no successors!");
671 ReturnInst::~ReturnInst() {
674 //===----------------------------------------------------------------------===//
675 // UnwindInst Implementation
676 //===----------------------------------------------------------------------===//
678 UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
679 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
680 0, 0, InsertBefore) {
682 UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
683 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
688 unsigned UnwindInst::getNumSuccessorsV() const {
689 return getNumSuccessors();
692 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
693 llvm_unreachable("UnwindInst has no successors!");
696 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
697 llvm_unreachable("UnwindInst has no successors!");
701 //===----------------------------------------------------------------------===//
702 // UnreachableInst Implementation
703 //===----------------------------------------------------------------------===//
705 UnreachableInst::UnreachableInst(LLVMContext &Context,
706 Instruction *InsertBefore)
707 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
708 0, 0, InsertBefore) {
710 UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
711 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
715 unsigned UnreachableInst::getNumSuccessorsV() const {
716 return getNumSuccessors();
719 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
720 llvm_unreachable("UnwindInst has no successors!");
723 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
724 llvm_unreachable("UnwindInst has no successors!");
728 //===----------------------------------------------------------------------===//
729 // BranchInst Implementation
730 //===----------------------------------------------------------------------===//
732 void BranchInst::AssertOK() {
734 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
735 "May only branch on boolean predicates!");
738 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
739 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
740 OperandTraits<BranchInst>::op_end(this) - 1,
742 assert(IfTrue != 0 && "Branch destination may not be null!");
745 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
746 Instruction *InsertBefore)
747 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
748 OperandTraits<BranchInst>::op_end(this) - 3,
758 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
759 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
760 OperandTraits<BranchInst>::op_end(this) - 1,
762 assert(IfTrue != 0 && "Branch destination may not be null!");
766 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
767 BasicBlock *InsertAtEnd)
768 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
769 OperandTraits<BranchInst>::op_end(this) - 3,
780 BranchInst::BranchInst(const BranchInst &BI) :
781 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
782 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
783 BI.getNumOperands()) {
784 Op<-1>() = BI.Op<-1>();
785 if (BI.getNumOperands() != 1) {
786 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
787 Op<-3>() = BI.Op<-3>();
788 Op<-2>() = BI.Op<-2>();
790 SubclassOptionalData = BI.SubclassOptionalData;
794 Use* Use::getPrefix() {
795 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
796 if (PotentialPrefix.getOpaqueValue())
799 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
802 BranchInst::~BranchInst() {
803 if (NumOperands == 1) {
804 if (Use *Prefix = OperandList->getPrefix()) {
807 // mark OperandList to have a special value for scrutiny
808 // by baseclass destructors and operator delete
809 OperandList = Prefix;
812 OperandList = op_begin();
818 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
819 return getSuccessor(idx);
821 unsigned BranchInst::getNumSuccessorsV() const {
822 return getNumSuccessors();
824 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
825 setSuccessor(idx, B);
829 //===----------------------------------------------------------------------===//
830 // AllocationInst Implementation
831 //===----------------------------------------------------------------------===//
833 static Value *getAISize(LLVMContext &Context, Value *Amt) {
835 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
837 assert(!isa<BasicBlock>(Amt) &&
838 "Passed basic block into allocation size parameter! Use other ctor");
839 assert(Amt->getType() == Type::getInt32Ty(Context) &&
840 "Malloc/Allocation array size is not a 32-bit integer!");
845 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
846 unsigned Align, const Twine &Name,
847 Instruction *InsertBefore)
848 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
849 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
851 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
855 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
856 unsigned Align, const Twine &Name,
857 BasicBlock *InsertAtEnd)
858 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
859 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
861 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
865 // Out of line virtual method, so the vtable, etc has a home.
866 AllocationInst::~AllocationInst() {
869 void AllocationInst::setAlignment(unsigned Align) {
870 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
871 SubclassData = Log2_32(Align) + 1;
872 assert(getAlignment() == Align && "Alignment representation error!");
875 bool AllocationInst::isArrayAllocation() const {
876 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
877 return CI->getZExtValue() != 1;
881 const Type *AllocationInst::getAllocatedType() const {
882 return getType()->getElementType();
885 /// isStaticAlloca - Return true if this alloca is in the entry block of the
886 /// function and is a constant size. If so, the code generator will fold it
887 /// into the prolog/epilog code, so it is basically free.
888 bool AllocaInst::isStaticAlloca() const {
889 // Must be constant size.
890 if (!isa<ConstantInt>(getArraySize())) return false;
892 // Must be in the entry block.
893 const BasicBlock *Parent = getParent();
894 return Parent == &Parent->getParent()->front();
897 //===----------------------------------------------------------------------===//
898 // FreeInst Implementation
899 //===----------------------------------------------------------------------===//
901 void FreeInst::AssertOK() {
902 assert(isa<PointerType>(getOperand(0)->getType()) &&
903 "Can not free something of nonpointer type!");
906 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
907 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
908 Free, Ptr, InsertBefore) {
912 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
913 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
914 Free, Ptr, InsertAtEnd) {
919 //===----------------------------------------------------------------------===//
920 // LoadInst Implementation
921 //===----------------------------------------------------------------------===//
923 void LoadInst::AssertOK() {
924 assert(isa<PointerType>(getOperand(0)->getType()) &&
925 "Ptr must have pointer type.");
928 LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
929 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
930 Load, Ptr, InsertBef) {
937 LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
938 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
939 Load, Ptr, InsertAE) {
946 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
947 Instruction *InsertBef)
948 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
949 Load, Ptr, InsertBef) {
950 setVolatile(isVolatile);
956 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
957 unsigned Align, Instruction *InsertBef)
958 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
959 Load, Ptr, InsertBef) {
960 setVolatile(isVolatile);
966 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
967 unsigned Align, BasicBlock *InsertAE)
968 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
969 Load, Ptr, InsertAE) {
970 setVolatile(isVolatile);
976 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
977 BasicBlock *InsertAE)
978 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
979 Load, Ptr, InsertAE) {
980 setVolatile(isVolatile);
988 LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
989 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
990 Load, Ptr, InsertBef) {
994 if (Name && Name[0]) setName(Name);
997 LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
998 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
999 Load, Ptr, InsertAE) {
1003 if (Name && Name[0]) setName(Name);
1006 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1007 Instruction *InsertBef)
1008 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1009 Load, Ptr, InsertBef) {
1010 setVolatile(isVolatile);
1013 if (Name && Name[0]) setName(Name);
1016 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1017 BasicBlock *InsertAE)
1018 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1019 Load, Ptr, InsertAE) {
1020 setVolatile(isVolatile);
1023 if (Name && Name[0]) setName(Name);
1026 void LoadInst::setAlignment(unsigned Align) {
1027 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1028 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1031 //===----------------------------------------------------------------------===//
1032 // StoreInst Implementation
1033 //===----------------------------------------------------------------------===//
1035 void StoreInst::AssertOK() {
1036 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
1037 assert(isa<PointerType>(getOperand(1)->getType()) &&
1038 "Ptr must have pointer type!");
1039 assert(getOperand(0)->getType() ==
1040 cast<PointerType>(getOperand(1)->getType())->getElementType()
1041 && "Ptr must be a pointer to Val type!");
1045 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
1046 : Instruction(Type::getVoidTy(val->getContext()), Store,
1047 OperandTraits<StoreInst>::op_begin(this),
1048 OperandTraits<StoreInst>::operands(this),
1057 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
1058 : Instruction(Type::getVoidTy(val->getContext()), Store,
1059 OperandTraits<StoreInst>::op_begin(this),
1060 OperandTraits<StoreInst>::operands(this),
1069 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1070 Instruction *InsertBefore)
1071 : Instruction(Type::getVoidTy(val->getContext()), Store,
1072 OperandTraits<StoreInst>::op_begin(this),
1073 OperandTraits<StoreInst>::operands(this),
1077 setVolatile(isVolatile);
1082 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1083 unsigned Align, Instruction *InsertBefore)
1084 : Instruction(Type::getVoidTy(val->getContext()), Store,
1085 OperandTraits<StoreInst>::op_begin(this),
1086 OperandTraits<StoreInst>::operands(this),
1090 setVolatile(isVolatile);
1091 setAlignment(Align);
1095 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1096 unsigned Align, BasicBlock *InsertAtEnd)
1097 : Instruction(Type::getVoidTy(val->getContext()), Store,
1098 OperandTraits<StoreInst>::op_begin(this),
1099 OperandTraits<StoreInst>::operands(this),
1103 setVolatile(isVolatile);
1104 setAlignment(Align);
1108 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1109 BasicBlock *InsertAtEnd)
1110 : Instruction(Type::getVoidTy(val->getContext()), Store,
1111 OperandTraits<StoreInst>::op_begin(this),
1112 OperandTraits<StoreInst>::operands(this),
1116 setVolatile(isVolatile);
1121 void StoreInst::setAlignment(unsigned Align) {
1122 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1123 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1126 //===----------------------------------------------------------------------===//
1127 // GetElementPtrInst Implementation
1128 //===----------------------------------------------------------------------===//
1130 static unsigned retrieveAddrSpace(const Value *Val) {
1131 return cast<PointerType>(Val->getType())->getAddressSpace();
1134 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
1135 const Twine &Name) {
1136 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1137 Use *OL = OperandList;
1140 for (unsigned i = 0; i != NumIdx; ++i)
1146 void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
1147 assert(NumOperands == 2 && "NumOperands not initialized?");
1148 Use *OL = OperandList;
1155 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1156 : Instruction(GEPI.getType(), GetElementPtr,
1157 OperandTraits<GetElementPtrInst>::op_end(this)
1158 - GEPI.getNumOperands(),
1159 GEPI.getNumOperands()) {
1160 Use *OL = OperandList;
1161 Use *GEPIOL = GEPI.OperandList;
1162 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1164 SubclassOptionalData = GEPI.SubclassOptionalData;
1167 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1168 const Twine &Name, Instruction *InBe)
1169 : Instruction(PointerType::get(
1170 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
1172 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1174 init(Ptr, Idx, Name);
1177 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1178 const Twine &Name, BasicBlock *IAE)
1179 : Instruction(PointerType::get(
1180 checkType(getIndexedType(Ptr->getType(),Idx)),
1181 retrieveAddrSpace(Ptr)),
1183 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1185 init(Ptr, Idx, Name);
1188 /// getIndexedType - Returns the type of the element that would be accessed with
1189 /// a gep instruction with the specified parameters.
1191 /// The Idxs pointer should point to a continuous piece of memory containing the
1192 /// indices, either as Value* or uint64_t.
1194 /// A null type is returned if the indices are invalid for the specified
1197 template <typename IndexTy>
1198 static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1200 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1201 if (!PTy) return 0; // Type isn't a pointer type!
1202 const Type *Agg = PTy->getElementType();
1204 // Handle the special case of the empty set index set, which is always valid.
1208 // If there is at least one index, the top level type must be sized, otherwise
1209 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1210 // that contain opaque types) under the assumption that it will be resolved to
1211 // a sane type later.
1212 if (!Agg->isSized() && !Agg->isAbstract())
1215 unsigned CurIdx = 1;
1216 for (; CurIdx != NumIdx; ++CurIdx) {
1217 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1218 if (!CT || isa<PointerType>(CT)) return 0;
1219 IndexTy Index = Idxs[CurIdx];
1220 if (!CT->indexValid(Index)) return 0;
1221 Agg = CT->getTypeAtIndex(Index);
1223 // If the new type forwards to another type, then it is in the middle
1224 // of being refined to another type (and hence, may have dropped all
1225 // references to what it was using before). So, use the new forwarded
1227 if (const Type *Ty = Agg->getForwardedType())
1230 return CurIdx == NumIdx ? Agg : 0;
1233 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1236 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1239 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1240 uint64_t const *Idxs,
1242 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1245 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1246 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1247 if (!PTy) return 0; // Type isn't a pointer type!
1249 // Check the pointer index.
1250 if (!PTy->indexValid(Idx)) return 0;
1252 return PTy->getElementType();
1256 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1257 /// zeros. If so, the result pointer and the first operand have the same
1258 /// value, just potentially different types.
1259 bool GetElementPtrInst::hasAllZeroIndices() const {
1260 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1261 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1262 if (!CI->isZero()) return false;
1270 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1271 /// constant integers. If so, the result pointer and the first operand have
1272 /// a constant offset between them.
1273 bool GetElementPtrInst::hasAllConstantIndices() const {
1274 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1275 if (!isa<ConstantInt>(getOperand(i)))
1281 void GetElementPtrInst::setIsInBounds(bool B) {
1282 cast<GEPOperator>(this)->setIsInBounds(B);
1285 bool GetElementPtrInst::isInBounds() const {
1286 return cast<GEPOperator>(this)->isInBounds();
1289 //===----------------------------------------------------------------------===//
1290 // ExtractElementInst Implementation
1291 //===----------------------------------------------------------------------===//
1293 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1295 Instruction *InsertBef)
1296 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1298 OperandTraits<ExtractElementInst>::op_begin(this),
1300 assert(isValidOperands(Val, Index) &&
1301 "Invalid extractelement instruction operands!");
1307 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1309 BasicBlock *InsertAE)
1310 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1312 OperandTraits<ExtractElementInst>::op_begin(this),
1314 assert(isValidOperands(Val, Index) &&
1315 "Invalid extractelement instruction operands!");
1323 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1324 if (!isa<VectorType>(Val->getType()) ||
1325 Index->getType() != Type::getInt32Ty(Val->getContext()))
1331 //===----------------------------------------------------------------------===//
1332 // InsertElementInst Implementation
1333 //===----------------------------------------------------------------------===//
1335 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1337 Instruction *InsertBef)
1338 : Instruction(Vec->getType(), InsertElement,
1339 OperandTraits<InsertElementInst>::op_begin(this),
1341 assert(isValidOperands(Vec, Elt, Index) &&
1342 "Invalid insertelement instruction operands!");
1349 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1351 BasicBlock *InsertAE)
1352 : Instruction(Vec->getType(), InsertElement,
1353 OperandTraits<InsertElementInst>::op_begin(this),
1355 assert(isValidOperands(Vec, Elt, Index) &&
1356 "Invalid insertelement instruction operands!");
1364 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1365 const Value *Index) {
1366 if (!isa<VectorType>(Vec->getType()))
1367 return false; // First operand of insertelement must be vector type.
1369 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1370 return false;// Second operand of insertelement must be vector element type.
1372 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
1373 return false; // Third operand of insertelement must be i32.
1378 //===----------------------------------------------------------------------===//
1379 // ShuffleVectorInst Implementation
1380 //===----------------------------------------------------------------------===//
1382 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1384 Instruction *InsertBefore)
1385 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1386 cast<VectorType>(Mask->getType())->getNumElements()),
1388 OperandTraits<ShuffleVectorInst>::op_begin(this),
1389 OperandTraits<ShuffleVectorInst>::operands(this),
1391 assert(isValidOperands(V1, V2, Mask) &&
1392 "Invalid shuffle vector instruction operands!");
1399 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1401 BasicBlock *InsertAtEnd)
1402 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1403 cast<VectorType>(Mask->getType())->getNumElements()),
1405 OperandTraits<ShuffleVectorInst>::op_begin(this),
1406 OperandTraits<ShuffleVectorInst>::operands(this),
1408 assert(isValidOperands(V1, V2, Mask) &&
1409 "Invalid shuffle vector instruction operands!");
1417 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1418 const Value *Mask) {
1419 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
1422 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1423 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1424 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
1429 /// getMaskValue - Return the index from the shuffle mask for the specified
1430 /// output result. This is either -1 if the element is undef or a number less
1431 /// than 2*numelements.
1432 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1433 const Constant *Mask = cast<Constant>(getOperand(2));
1434 if (isa<UndefValue>(Mask)) return -1;
1435 if (isa<ConstantAggregateZero>(Mask)) return 0;
1436 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1437 assert(i < MaskCV->getNumOperands() && "Index out of range");
1439 if (isa<UndefValue>(MaskCV->getOperand(i)))
1441 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1444 //===----------------------------------------------------------------------===//
1445 // InsertValueInst Class
1446 //===----------------------------------------------------------------------===//
1448 void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1449 unsigned NumIdx, const Twine &Name) {
1450 assert(NumOperands == 2 && "NumOperands not initialized?");
1454 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
1458 void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1459 const Twine &Name) {
1460 assert(NumOperands == 2 && "NumOperands not initialized?");
1464 Indices.push_back(Idx);
1468 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1469 : Instruction(IVI.getType(), InsertValue,
1470 OperandTraits<InsertValueInst>::op_begin(this), 2),
1471 Indices(IVI.Indices) {
1472 Op<0>() = IVI.getOperand(0);
1473 Op<1>() = IVI.getOperand(1);
1474 SubclassOptionalData = IVI.SubclassOptionalData;
1477 InsertValueInst::InsertValueInst(Value *Agg,
1481 Instruction *InsertBefore)
1482 : Instruction(Agg->getType(), InsertValue,
1483 OperandTraits<InsertValueInst>::op_begin(this),
1485 init(Agg, Val, Idx, Name);
1488 InsertValueInst::InsertValueInst(Value *Agg,
1492 BasicBlock *InsertAtEnd)
1493 : Instruction(Agg->getType(), InsertValue,
1494 OperandTraits<InsertValueInst>::op_begin(this),
1496 init(Agg, Val, Idx, Name);
1499 //===----------------------------------------------------------------------===//
1500 // ExtractValueInst Class
1501 //===----------------------------------------------------------------------===//
1503 void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
1504 const Twine &Name) {
1505 assert(NumOperands == 1 && "NumOperands not initialized?");
1507 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
1511 void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
1512 assert(NumOperands == 1 && "NumOperands not initialized?");
1514 Indices.push_back(Idx);
1518 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1519 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
1520 Indices(EVI.Indices) {
1521 SubclassOptionalData = EVI.SubclassOptionalData;
1524 // getIndexedType - Returns the type of the element that would be extracted
1525 // with an extractvalue instruction with the specified parameters.
1527 // A null type is returned if the indices are invalid for the specified
1530 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1531 const unsigned *Idxs,
1533 unsigned CurIdx = 0;
1534 for (; CurIdx != NumIdx; ++CurIdx) {
1535 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1536 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1537 unsigned Index = Idxs[CurIdx];
1538 if (!CT->indexValid(Index)) return 0;
1539 Agg = CT->getTypeAtIndex(Index);
1541 // If the new type forwards to another type, then it is in the middle
1542 // of being refined to another type (and hence, may have dropped all
1543 // references to what it was using before). So, use the new forwarded
1545 if (const Type *Ty = Agg->getForwardedType())
1548 return CurIdx == NumIdx ? Agg : 0;
1551 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1553 return getIndexedType(Agg, &Idx, 1);
1556 //===----------------------------------------------------------------------===//
1557 // BinaryOperator Class
1558 //===----------------------------------------------------------------------===//
1560 /// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1561 /// type is floating-point, to help provide compatibility with an older API.
1563 static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1565 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1566 if (Ty->isFPOrFPVector()) {
1567 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1568 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1569 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1574 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1575 const Type *Ty, const Twine &Name,
1576 Instruction *InsertBefore)
1577 : Instruction(Ty, AdjustIType(iType, Ty),
1578 OperandTraits<BinaryOperator>::op_begin(this),
1579 OperandTraits<BinaryOperator>::operands(this),
1583 init(AdjustIType(iType, Ty));
1587 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1588 const Type *Ty, const Twine &Name,
1589 BasicBlock *InsertAtEnd)
1590 : Instruction(Ty, AdjustIType(iType, Ty),
1591 OperandTraits<BinaryOperator>::op_begin(this),
1592 OperandTraits<BinaryOperator>::operands(this),
1596 init(AdjustIType(iType, Ty));
1601 void BinaryOperator::init(BinaryOps iType) {
1602 Value *LHS = getOperand(0), *RHS = getOperand(1);
1603 LHS = LHS; RHS = RHS; // Silence warnings.
1604 assert(LHS->getType() == RHS->getType() &&
1605 "Binary operator operand types must match!");
1610 assert(getType() == LHS->getType() &&
1611 "Arithmetic operation should return same type as operands!");
1612 assert(getType()->isIntOrIntVector() &&
1613 "Tried to create an integer operation on a non-integer type!");
1615 case FAdd: case FSub:
1617 assert(getType() == LHS->getType() &&
1618 "Arithmetic operation should return same type as operands!");
1619 assert(getType()->isFPOrFPVector() &&
1620 "Tried to create a floating-point operation on a "
1621 "non-floating-point type!");
1625 assert(getType() == LHS->getType() &&
1626 "Arithmetic operation should return same type as operands!");
1627 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1628 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1629 "Incorrect operand type (not integer) for S/UDIV");
1632 assert(getType() == LHS->getType() &&
1633 "Arithmetic operation should return same type as operands!");
1634 assert(getType()->isFPOrFPVector() &&
1635 "Incorrect operand type (not floating point) for FDIV");
1639 assert(getType() == LHS->getType() &&
1640 "Arithmetic operation should return same type as operands!");
1641 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1642 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1643 "Incorrect operand type (not integer) for S/UREM");
1646 assert(getType() == LHS->getType() &&
1647 "Arithmetic operation should return same type as operands!");
1648 assert(getType()->isFPOrFPVector() &&
1649 "Incorrect operand type (not floating point) for FREM");
1654 assert(getType() == LHS->getType() &&
1655 "Shift operation should return same type as operands!");
1656 assert((getType()->isInteger() ||
1657 (isa<VectorType>(getType()) &&
1658 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1659 "Tried to create a shift operation on a non-integral type!");
1663 assert(getType() == LHS->getType() &&
1664 "Logical operation should return same type as operands!");
1665 assert((getType()->isInteger() ||
1666 (isa<VectorType>(getType()) &&
1667 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1668 "Tried to create a logical operation on a non-integral type!");
1676 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1678 Instruction *InsertBefore) {
1679 assert(S1->getType() == S2->getType() &&
1680 "Cannot create binary operator with two operands of differing type!");
1681 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1684 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1686 BasicBlock *InsertAtEnd) {
1687 BinaryOperator *Res = Create(Op, S1, S2, Name);
1688 InsertAtEnd->getInstList().push_back(Res);
1692 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1693 Instruction *InsertBefore) {
1694 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1695 return new BinaryOperator(Instruction::Sub,
1697 Op->getType(), Name, InsertBefore);
1700 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1701 BasicBlock *InsertAtEnd) {
1702 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1703 return new BinaryOperator(Instruction::Sub,
1705 Op->getType(), Name, InsertAtEnd);
1708 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1709 Instruction *InsertBefore) {
1710 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1711 return new BinaryOperator(Instruction::FSub,
1713 Op->getType(), Name, InsertBefore);
1716 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1717 BasicBlock *InsertAtEnd) {
1718 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1719 return new BinaryOperator(Instruction::FSub,
1721 Op->getType(), Name, InsertAtEnd);
1724 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1725 Instruction *InsertBefore) {
1727 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1728 C = Constant::getAllOnesValue(PTy->getElementType());
1729 C = ConstantVector::get(
1730 std::vector<Constant*>(PTy->getNumElements(), C));
1732 C = Constant::getAllOnesValue(Op->getType());
1735 return new BinaryOperator(Instruction::Xor, Op, C,
1736 Op->getType(), Name, InsertBefore);
1739 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1740 BasicBlock *InsertAtEnd) {
1742 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1743 // Create a vector of all ones values.
1744 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
1745 AllOnes = ConstantVector::get(
1746 std::vector<Constant*>(PTy->getNumElements(), Elt));
1748 AllOnes = Constant::getAllOnesValue(Op->getType());
1751 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1752 Op->getType(), Name, InsertAtEnd);
1756 // isConstantAllOnes - Helper function for several functions below
1757 static inline bool isConstantAllOnes(const Value *V) {
1758 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1759 return CI->isAllOnesValue();
1760 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1761 return CV->isAllOnesValue();
1765 bool BinaryOperator::isNeg(const Value *V) {
1766 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1767 if (Bop->getOpcode() == Instruction::Sub)
1768 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1769 return C->isNegativeZeroValue();
1773 bool BinaryOperator::isFNeg(const Value *V) {
1774 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1775 if (Bop->getOpcode() == Instruction::FSub)
1776 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1777 return C->isNegativeZeroValue();
1781 bool BinaryOperator::isNot(const Value *V) {
1782 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1783 return (Bop->getOpcode() == Instruction::Xor &&
1784 (isConstantAllOnes(Bop->getOperand(1)) ||
1785 isConstantAllOnes(Bop->getOperand(0))));
1789 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1790 return cast<BinaryOperator>(BinOp)->getOperand(1);
1793 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1794 return getNegArgument(const_cast<Value*>(BinOp));
1797 Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1798 return cast<BinaryOperator>(BinOp)->getOperand(1);
1801 const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1802 return getFNegArgument(const_cast<Value*>(BinOp));
1805 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1806 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1807 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1808 Value *Op0 = BO->getOperand(0);
1809 Value *Op1 = BO->getOperand(1);
1810 if (isConstantAllOnes(Op0)) return Op1;
1812 assert(isConstantAllOnes(Op1));
1816 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1817 return getNotArgument(const_cast<Value*>(BinOp));
1821 // swapOperands - Exchange the two operands to this instruction. This
1822 // instruction is safe to use on any binary instruction and does not
1823 // modify the semantics of the instruction. If the instruction is
1824 // order dependent (SetLT f.e.) the opcode is changed.
1826 bool BinaryOperator::swapOperands() {
1827 if (!isCommutative())
1828 return true; // Can't commute operands
1829 Op<0>().swap(Op<1>());
1833 void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1834 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1837 void BinaryOperator::setHasNoSignedWrap(bool b) {
1838 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1841 void BinaryOperator::setIsExact(bool b) {
1842 cast<SDivOperator>(this)->setIsExact(b);
1845 bool BinaryOperator::hasNoUnsignedWrap() const {
1846 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1849 bool BinaryOperator::hasNoSignedWrap() const {
1850 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1853 bool BinaryOperator::isExact() const {
1854 return cast<SDivOperator>(this)->isExact();
1857 //===----------------------------------------------------------------------===//
1859 //===----------------------------------------------------------------------===//
1861 // Just determine if this cast only deals with integral->integral conversion.
1862 bool CastInst::isIntegerCast() const {
1863 switch (getOpcode()) {
1864 default: return false;
1865 case Instruction::ZExt:
1866 case Instruction::SExt:
1867 case Instruction::Trunc:
1869 case Instruction::BitCast:
1870 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
1874 bool CastInst::isLosslessCast() const {
1875 // Only BitCast can be lossless, exit fast if we're not BitCast
1876 if (getOpcode() != Instruction::BitCast)
1879 // Identity cast is always lossless
1880 const Type* SrcTy = getOperand(0)->getType();
1881 const Type* DstTy = getType();
1885 // Pointer to pointer is always lossless.
1886 if (isa<PointerType>(SrcTy))
1887 return isa<PointerType>(DstTy);
1888 return false; // Other types have no identity values
1891 /// This function determines if the CastInst does not require any bits to be
1892 /// changed in order to effect the cast. Essentially, it identifies cases where
1893 /// no code gen is necessary for the cast, hence the name no-op cast. For
1894 /// example, the following are all no-op casts:
1895 /// # bitcast i32* %x to i8*
1896 /// # bitcast <2 x i32> %x to <4 x i16>
1897 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1898 /// @brief Determine if a cast is a no-op.
1899 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1900 switch (getOpcode()) {
1902 assert(!"Invalid CastOp");
1903 case Instruction::Trunc:
1904 case Instruction::ZExt:
1905 case Instruction::SExt:
1906 case Instruction::FPTrunc:
1907 case Instruction::FPExt:
1908 case Instruction::UIToFP:
1909 case Instruction::SIToFP:
1910 case Instruction::FPToUI:
1911 case Instruction::FPToSI:
1912 return false; // These always modify bits
1913 case Instruction::BitCast:
1914 return true; // BitCast never modifies bits.
1915 case Instruction::PtrToInt:
1916 return IntPtrTy->getScalarSizeInBits() ==
1917 getType()->getScalarSizeInBits();
1918 case Instruction::IntToPtr:
1919 return IntPtrTy->getScalarSizeInBits() ==
1920 getOperand(0)->getType()->getScalarSizeInBits();
1924 /// This function determines if a pair of casts can be eliminated and what
1925 /// opcode should be used in the elimination. This assumes that there are two
1926 /// instructions like this:
1927 /// * %F = firstOpcode SrcTy %x to MidTy
1928 /// * %S = secondOpcode MidTy %F to DstTy
1929 /// The function returns a resultOpcode so these two casts can be replaced with:
1930 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1931 /// If no such cast is permited, the function returns 0.
1932 unsigned CastInst::isEliminableCastPair(
1933 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1934 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1936 // Define the 144 possibilities for these two cast instructions. The values
1937 // in this matrix determine what to do in a given situation and select the
1938 // case in the switch below. The rows correspond to firstOp, the columns
1939 // correspond to secondOp. In looking at the table below, keep in mind
1940 // the following cast properties:
1942 // Size Compare Source Destination
1943 // Operator Src ? Size Type Sign Type Sign
1944 // -------- ------------ ------------------- ---------------------
1945 // TRUNC > Integer Any Integral Any
1946 // ZEXT < Integral Unsigned Integer Any
1947 // SEXT < Integral Signed Integer Any
1948 // FPTOUI n/a FloatPt n/a Integral Unsigned
1949 // FPTOSI n/a FloatPt n/a Integral Signed
1950 // UITOFP n/a Integral Unsigned FloatPt n/a
1951 // SITOFP n/a Integral Signed FloatPt n/a
1952 // FPTRUNC > FloatPt n/a FloatPt n/a
1953 // FPEXT < FloatPt n/a FloatPt n/a
1954 // PTRTOINT n/a Pointer n/a Integral Unsigned
1955 // INTTOPTR n/a Integral Unsigned Pointer n/a
1956 // BITCONVERT = FirstClass n/a FirstClass n/a
1958 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1959 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1960 // into "fptoui double to i64", but this loses information about the range
1961 // of the produced value (we no longer know the top-part is all zeros).
1962 // Further this conversion is often much more expensive for typical hardware,
1963 // and causes issues when building libgcc. We disallow fptosi+sext for the
1965 const unsigned numCastOps =
1966 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1967 static const uint8_t CastResults[numCastOps][numCastOps] = {
1968 // T F F U S F F P I B -+
1969 // R Z S P P I I T P 2 N T |
1970 // U E E 2 2 2 2 R E I T C +- secondOp
1971 // N X X U S F F N X N 2 V |
1972 // C T T I I P P C T T P T -+
1973 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1974 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1975 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
1976 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1977 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
1978 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1979 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1980 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1981 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1982 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1983 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1984 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1987 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1988 [secondOp-Instruction::CastOpsBegin];
1991 // categorically disallowed
1994 // allowed, use first cast's opcode
1997 // allowed, use second cast's opcode
2000 // no-op cast in second op implies firstOp as long as the DestTy
2002 if (DstTy->isInteger())
2006 // no-op cast in second op implies firstOp as long as the DestTy
2007 // is floating point
2008 if (DstTy->isFloatingPoint())
2012 // no-op cast in first op implies secondOp as long as the SrcTy
2014 if (SrcTy->isInteger())
2018 // no-op cast in first op implies secondOp as long as the SrcTy
2019 // is a floating point
2020 if (SrcTy->isFloatingPoint())
2024 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
2027 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2028 unsigned MidSize = MidTy->getScalarSizeInBits();
2029 if (MidSize >= PtrSize)
2030 return Instruction::BitCast;
2034 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2035 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2036 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2037 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2038 unsigned DstSize = DstTy->getScalarSizeInBits();
2039 if (SrcSize == DstSize)
2040 return Instruction::BitCast;
2041 else if (SrcSize < DstSize)
2045 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2046 return Instruction::ZExt;
2048 // fpext followed by ftrunc is allowed if the bit size returned to is
2049 // the same as the original, in which case its just a bitcast
2051 return Instruction::BitCast;
2052 return 0; // If the types are not the same we can't eliminate it.
2054 // bitcast followed by ptrtoint is allowed as long as the bitcast
2055 // is a pointer to pointer cast.
2056 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2060 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2061 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2065 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2068 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2069 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2070 unsigned DstSize = DstTy->getScalarSizeInBits();
2071 if (SrcSize <= PtrSize && SrcSize == DstSize)
2072 return Instruction::BitCast;
2076 // cast combination can't happen (error in input). This is for all cases
2077 // where the MidTy is not the same for the two cast instructions.
2078 assert(!"Invalid Cast Combination");
2081 assert(!"Error in CastResults table!!!");
2087 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2088 const Twine &Name, Instruction *InsertBefore) {
2089 // Construct and return the appropriate CastInst subclass
2091 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2092 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2093 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2094 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2095 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2096 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2097 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2098 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2099 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2100 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2101 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2102 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2104 assert(!"Invalid opcode provided");
2109 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2110 const Twine &Name, BasicBlock *InsertAtEnd) {
2111 // Construct and return the appropriate CastInst subclass
2113 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2114 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2115 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2116 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2117 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2118 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2119 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2120 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2121 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2122 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2123 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2124 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2126 assert(!"Invalid opcode provided");
2131 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2133 Instruction *InsertBefore) {
2134 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2135 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2136 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
2139 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2141 BasicBlock *InsertAtEnd) {
2142 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2143 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2144 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
2147 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2149 Instruction *InsertBefore) {
2150 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2151 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2152 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
2155 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2157 BasicBlock *InsertAtEnd) {
2158 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2159 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2160 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
2163 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2165 Instruction *InsertBefore) {
2166 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2167 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2168 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
2171 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2173 BasicBlock *InsertAtEnd) {
2174 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2175 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2176 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
2179 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2181 BasicBlock *InsertAtEnd) {
2182 assert(isa<PointerType>(S->getType()) && "Invalid cast");
2183 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
2186 if (Ty->isInteger())
2187 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2188 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2191 /// @brief Create a BitCast or a PtrToInt cast instruction
2192 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2194 Instruction *InsertBefore) {
2195 assert(isa<PointerType>(S->getType()) && "Invalid cast");
2196 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
2199 if (Ty->isInteger())
2200 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2201 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2204 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2205 bool isSigned, const Twine &Name,
2206 Instruction *InsertBefore) {
2207 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
2208 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2209 unsigned DstBits = Ty->getScalarSizeInBits();
2210 Instruction::CastOps opcode =
2211 (SrcBits == DstBits ? Instruction::BitCast :
2212 (SrcBits > DstBits ? Instruction::Trunc :
2213 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2214 return Create(opcode, C, Ty, Name, InsertBefore);
2217 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2218 bool isSigned, const Twine &Name,
2219 BasicBlock *InsertAtEnd) {
2220 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2222 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2223 unsigned DstBits = Ty->getScalarSizeInBits();
2224 Instruction::CastOps opcode =
2225 (SrcBits == DstBits ? Instruction::BitCast :
2226 (SrcBits > DstBits ? Instruction::Trunc :
2227 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2228 return Create(opcode, C, Ty, Name, InsertAtEnd);
2231 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2233 Instruction *InsertBefore) {
2234 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2236 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2237 unsigned DstBits = Ty->getScalarSizeInBits();
2238 Instruction::CastOps opcode =
2239 (SrcBits == DstBits ? Instruction::BitCast :
2240 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2241 return Create(opcode, C, Ty, Name, InsertBefore);
2244 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2246 BasicBlock *InsertAtEnd) {
2247 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2249 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2250 unsigned DstBits = Ty->getScalarSizeInBits();
2251 Instruction::CastOps opcode =
2252 (SrcBits == DstBits ? Instruction::BitCast :
2253 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2254 return Create(opcode, C, Ty, Name, InsertAtEnd);
2257 // Check whether it is valid to call getCastOpcode for these types.
2258 // This routine must be kept in sync with getCastOpcode.
2259 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2260 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2263 if (SrcTy == DestTy)
2266 // Get the bit sizes, we'll need these
2267 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2268 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2270 // Run through the possibilities ...
2271 if (DestTy->isInteger()) { // Casting to integral
2272 if (SrcTy->isInteger()) { // Casting from integral
2274 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2276 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2277 // Casting from vector
2278 return DestBits == PTy->getBitWidth();
2279 } else { // Casting from something else
2280 return isa<PointerType>(SrcTy);
2282 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2283 if (SrcTy->isInteger()) { // Casting from integral
2285 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2287 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2288 // Casting from vector
2289 return DestBits == PTy->getBitWidth();
2290 } else { // Casting from something else
2293 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2294 // Casting to vector
2295 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2296 // Casting from vector
2297 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2298 } else { // Casting from something else
2299 return DestPTy->getBitWidth() == SrcBits;
2301 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2302 if (isa<PointerType>(SrcTy)) { // Casting from pointer
2304 } else if (SrcTy->isInteger()) { // Casting from integral
2306 } else { // Casting from something else
2309 } else { // Casting to something else
2314 // Provide a way to get a "cast" where the cast opcode is inferred from the
2315 // types and size of the operand. This, basically, is a parallel of the
2316 // logic in the castIsValid function below. This axiom should hold:
2317 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2318 // should not assert in castIsValid. In other words, this produces a "correct"
2319 // casting opcode for the arguments passed to it.
2320 // This routine must be kept in sync with isCastable.
2321 Instruction::CastOps
2322 CastInst::getCastOpcode(
2323 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
2324 // Get the bit sizes, we'll need these
2325 const Type *SrcTy = Src->getType();
2326 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2327 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2329 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2330 "Only first class types are castable!");
2332 // Run through the possibilities ...
2333 if (DestTy->isInteger()) { // Casting to integral
2334 if (SrcTy->isInteger()) { // Casting from integral
2335 if (DestBits < SrcBits)
2336 return Trunc; // int -> smaller int
2337 else if (DestBits > SrcBits) { // its an extension
2339 return SExt; // signed -> SEXT
2341 return ZExt; // unsigned -> ZEXT
2343 return BitCast; // Same size, No-op cast
2345 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2347 return FPToSI; // FP -> sint
2349 return FPToUI; // FP -> uint
2350 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2351 assert(DestBits == PTy->getBitWidth() &&
2352 "Casting vector to integer of different width");
2354 return BitCast; // Same size, no-op cast
2356 assert(isa<PointerType>(SrcTy) &&
2357 "Casting from a value that is not first-class type");
2358 return PtrToInt; // ptr -> int
2360 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2361 if (SrcTy->isInteger()) { // Casting from integral
2363 return SIToFP; // sint -> FP
2365 return UIToFP; // uint -> FP
2366 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2367 if (DestBits < SrcBits) {
2368 return FPTrunc; // FP -> smaller FP
2369 } else if (DestBits > SrcBits) {
2370 return FPExt; // FP -> larger FP
2372 return BitCast; // same size, no-op cast
2374 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2375 assert(DestBits == PTy->getBitWidth() &&
2376 "Casting vector to floating point of different width");
2378 return BitCast; // same size, no-op cast
2380 llvm_unreachable("Casting pointer or non-first class to float");
2382 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2383 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2384 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
2385 "Casting vector to vector of different widths");
2387 return BitCast; // vector -> vector
2388 } else if (DestPTy->getBitWidth() == SrcBits) {
2389 return BitCast; // float/int -> vector
2391 assert(!"Illegal cast to vector (wrong type or size)");
2393 } else if (isa<PointerType>(DestTy)) {
2394 if (isa<PointerType>(SrcTy)) {
2395 return BitCast; // ptr -> ptr
2396 } else if (SrcTy->isInteger()) {
2397 return IntToPtr; // int -> ptr
2399 assert(!"Casting pointer to other than pointer or int");
2402 assert(!"Casting to type that is not first-class");
2405 // If we fall through to here we probably hit an assertion cast above
2406 // and assertions are not turned on. Anything we return is an error, so
2407 // BitCast is as good a choice as any.
2411 //===----------------------------------------------------------------------===//
2412 // CastInst SubClass Constructors
2413 //===----------------------------------------------------------------------===//
2415 /// Check that the construction parameters for a CastInst are correct. This
2416 /// could be broken out into the separate constructors but it is useful to have
2417 /// it in one place and to eliminate the redundant code for getting the sizes
2418 /// of the types involved.
2420 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
2422 // Check for type sanity on the arguments
2423 const Type *SrcTy = S->getType();
2424 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2427 // Get the size of the types in bits, we'll need this later
2428 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2429 unsigned DstBitSize = DstTy->getScalarSizeInBits();
2431 // Switch on the opcode provided
2433 default: return false; // This is an input error
2434 case Instruction::Trunc:
2435 return SrcTy->isIntOrIntVector() &&
2436 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
2437 case Instruction::ZExt:
2438 return SrcTy->isIntOrIntVector() &&
2439 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
2440 case Instruction::SExt:
2441 return SrcTy->isIntOrIntVector() &&
2442 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
2443 case Instruction::FPTrunc:
2444 return SrcTy->isFPOrFPVector() &&
2445 DstTy->isFPOrFPVector() &&
2446 SrcBitSize > DstBitSize;
2447 case Instruction::FPExt:
2448 return SrcTy->isFPOrFPVector() &&
2449 DstTy->isFPOrFPVector() &&
2450 SrcBitSize < DstBitSize;
2451 case Instruction::UIToFP:
2452 case Instruction::SIToFP:
2453 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2454 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2455 return SVTy->getElementType()->isIntOrIntVector() &&
2456 DVTy->getElementType()->isFPOrFPVector() &&
2457 SVTy->getNumElements() == DVTy->getNumElements();
2460 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
2461 case Instruction::FPToUI:
2462 case Instruction::FPToSI:
2463 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2464 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2465 return SVTy->getElementType()->isFPOrFPVector() &&
2466 DVTy->getElementType()->isIntOrIntVector() &&
2467 SVTy->getNumElements() == DVTy->getNumElements();
2470 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
2471 case Instruction::PtrToInt:
2472 return isa<PointerType>(SrcTy) && DstTy->isInteger();
2473 case Instruction::IntToPtr:
2474 return SrcTy->isInteger() && isa<PointerType>(DstTy);
2475 case Instruction::BitCast:
2476 // BitCast implies a no-op cast of type only. No bits change.
2477 // However, you can't cast pointers to anything but pointers.
2478 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2481 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2482 // these cases, the cast is okay if the source and destination bit widths
2484 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2488 TruncInst::TruncInst(
2489 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2490 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2491 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2494 TruncInst::TruncInst(
2495 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2496 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2497 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2501 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2502 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2503 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2507 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2508 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2509 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2512 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2513 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2514 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2518 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2519 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2520 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2523 FPTruncInst::FPTruncInst(
2524 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2525 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2526 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2529 FPTruncInst::FPTruncInst(
2530 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2531 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2532 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2535 FPExtInst::FPExtInst(
2536 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2537 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2538 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2541 FPExtInst::FPExtInst(
2542 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2543 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2544 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2547 UIToFPInst::UIToFPInst(
2548 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2549 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2550 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2553 UIToFPInst::UIToFPInst(
2554 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2555 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2556 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2559 SIToFPInst::SIToFPInst(
2560 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2561 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2562 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2565 SIToFPInst::SIToFPInst(
2566 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2567 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2568 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2571 FPToUIInst::FPToUIInst(
2572 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2573 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2574 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2577 FPToUIInst::FPToUIInst(
2578 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2579 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2580 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2583 FPToSIInst::FPToSIInst(
2584 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2585 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2586 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2589 FPToSIInst::FPToSIInst(
2590 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2591 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2592 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2595 PtrToIntInst::PtrToIntInst(
2596 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2597 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2598 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2601 PtrToIntInst::PtrToIntInst(
2602 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2603 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2604 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2607 IntToPtrInst::IntToPtrInst(
2608 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2609 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2610 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2613 IntToPtrInst::IntToPtrInst(
2614 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2615 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2616 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2619 BitCastInst::BitCastInst(
2620 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2621 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2622 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2625 BitCastInst::BitCastInst(
2626 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2627 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2628 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2631 //===----------------------------------------------------------------------===//
2633 //===----------------------------------------------------------------------===//
2635 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2636 Value *LHS, Value *RHS, const Twine &Name,
2637 Instruction *InsertBefore)
2638 : Instruction(ty, op,
2639 OperandTraits<CmpInst>::op_begin(this),
2640 OperandTraits<CmpInst>::operands(this),
2644 SubclassData = predicate;
2648 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2649 Value *LHS, Value *RHS, const Twine &Name,
2650 BasicBlock *InsertAtEnd)
2651 : Instruction(ty, op,
2652 OperandTraits<CmpInst>::op_begin(this),
2653 OperandTraits<CmpInst>::operands(this),
2657 SubclassData = predicate;
2662 CmpInst::Create(OtherOps Op, unsigned short predicate,
2663 Value *S1, Value *S2,
2664 const Twine &Name, Instruction *InsertBefore) {
2665 if (Op == Instruction::ICmp) {
2667 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2670 return new ICmpInst(CmpInst::Predicate(predicate),
2675 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2678 return new FCmpInst(CmpInst::Predicate(predicate),
2683 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2684 const Twine &Name, BasicBlock *InsertAtEnd) {
2685 if (Op == Instruction::ICmp) {
2686 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2689 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2693 void CmpInst::swapOperands() {
2694 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2697 cast<FCmpInst>(this)->swapOperands();
2700 bool CmpInst::isCommutative() {
2701 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2702 return IC->isCommutative();
2703 return cast<FCmpInst>(this)->isCommutative();
2706 bool CmpInst::isEquality() {
2707 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2708 return IC->isEquality();
2709 return cast<FCmpInst>(this)->isEquality();
2713 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
2715 default: assert(!"Unknown cmp predicate!");
2716 case ICMP_EQ: return ICMP_NE;
2717 case ICMP_NE: return ICMP_EQ;
2718 case ICMP_UGT: return ICMP_ULE;
2719 case ICMP_ULT: return ICMP_UGE;
2720 case ICMP_UGE: return ICMP_ULT;
2721 case ICMP_ULE: return ICMP_UGT;
2722 case ICMP_SGT: return ICMP_SLE;
2723 case ICMP_SLT: return ICMP_SGE;
2724 case ICMP_SGE: return ICMP_SLT;
2725 case ICMP_SLE: return ICMP_SGT;
2727 case FCMP_OEQ: return FCMP_UNE;
2728 case FCMP_ONE: return FCMP_UEQ;
2729 case FCMP_OGT: return FCMP_ULE;
2730 case FCMP_OLT: return FCMP_UGE;
2731 case FCMP_OGE: return FCMP_ULT;
2732 case FCMP_OLE: return FCMP_UGT;
2733 case FCMP_UEQ: return FCMP_ONE;
2734 case FCMP_UNE: return FCMP_OEQ;
2735 case FCMP_UGT: return FCMP_OLE;
2736 case FCMP_ULT: return FCMP_OGE;
2737 case FCMP_UGE: return FCMP_OLT;
2738 case FCMP_ULE: return FCMP_OGT;
2739 case FCMP_ORD: return FCMP_UNO;
2740 case FCMP_UNO: return FCMP_ORD;
2741 case FCMP_TRUE: return FCMP_FALSE;
2742 case FCMP_FALSE: return FCMP_TRUE;
2746 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2748 default: assert(! "Unknown icmp predicate!");
2749 case ICMP_EQ: case ICMP_NE:
2750 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2752 case ICMP_UGT: return ICMP_SGT;
2753 case ICMP_ULT: return ICMP_SLT;
2754 case ICMP_UGE: return ICMP_SGE;
2755 case ICMP_ULE: return ICMP_SLE;
2759 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2761 default: assert(! "Unknown icmp predicate!");
2762 case ICMP_EQ: case ICMP_NE:
2763 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2765 case ICMP_SGT: return ICMP_UGT;
2766 case ICMP_SLT: return ICMP_ULT;
2767 case ICMP_SGE: return ICMP_UGE;
2768 case ICMP_SLE: return ICMP_ULE;
2772 bool ICmpInst::isSignedPredicate(Predicate pred) {
2774 default: assert(! "Unknown icmp predicate!");
2775 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2777 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2778 case ICMP_UGE: case ICMP_ULE:
2783 /// Initialize a set of values that all satisfy the condition with C.
2786 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2789 uint32_t BitWidth = C.getBitWidth();
2791 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
2792 case ICmpInst::ICMP_EQ: Upper++; break;
2793 case ICmpInst::ICMP_NE: Lower++; break;
2794 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2795 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2796 case ICmpInst::ICMP_UGT:
2797 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2799 case ICmpInst::ICMP_SGT:
2800 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2802 case ICmpInst::ICMP_ULE:
2803 Lower = APInt::getMinValue(BitWidth); Upper++;
2805 case ICmpInst::ICMP_SLE:
2806 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2808 case ICmpInst::ICMP_UGE:
2809 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2811 case ICmpInst::ICMP_SGE:
2812 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2815 return ConstantRange(Lower, Upper);
2818 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
2820 default: assert(!"Unknown cmp predicate!");
2821 case ICMP_EQ: case ICMP_NE:
2823 case ICMP_SGT: return ICMP_SLT;
2824 case ICMP_SLT: return ICMP_SGT;
2825 case ICMP_SGE: return ICMP_SLE;
2826 case ICMP_SLE: return ICMP_SGE;
2827 case ICMP_UGT: return ICMP_ULT;
2828 case ICMP_ULT: return ICMP_UGT;
2829 case ICMP_UGE: return ICMP_ULE;
2830 case ICMP_ULE: return ICMP_UGE;
2832 case FCMP_FALSE: case FCMP_TRUE:
2833 case FCMP_OEQ: case FCMP_ONE:
2834 case FCMP_UEQ: case FCMP_UNE:
2835 case FCMP_ORD: case FCMP_UNO:
2837 case FCMP_OGT: return FCMP_OLT;
2838 case FCMP_OLT: return FCMP_OGT;
2839 case FCMP_OGE: return FCMP_OLE;
2840 case FCMP_OLE: return FCMP_OGE;
2841 case FCMP_UGT: return FCMP_ULT;
2842 case FCMP_ULT: return FCMP_UGT;
2843 case FCMP_UGE: return FCMP_ULE;
2844 case FCMP_ULE: return FCMP_UGE;
2848 bool CmpInst::isUnsigned(unsigned short predicate) {
2849 switch (predicate) {
2850 default: return false;
2851 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2852 case ICmpInst::ICMP_UGE: return true;
2856 bool CmpInst::isSigned(unsigned short predicate){
2857 switch (predicate) {
2858 default: return false;
2859 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2860 case ICmpInst::ICMP_SGE: return true;
2864 bool CmpInst::isOrdered(unsigned short predicate) {
2865 switch (predicate) {
2866 default: return false;
2867 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2868 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2869 case FCmpInst::FCMP_ORD: return true;
2873 bool CmpInst::isUnordered(unsigned short predicate) {
2874 switch (predicate) {
2875 default: return false;
2876 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2877 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2878 case FCmpInst::FCMP_UNO: return true;
2882 //===----------------------------------------------------------------------===//
2883 // SwitchInst Implementation
2884 //===----------------------------------------------------------------------===//
2886 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
2887 assert(Value && Default);
2888 ReservedSpace = 2+NumCases*2;
2890 OperandList = allocHungoffUses(ReservedSpace);
2892 OperandList[0] = Value;
2893 OperandList[1] = Default;
2896 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2897 /// switch on and a default destination. The number of additional cases can
2898 /// be specified here to make memory allocation more efficient. This
2899 /// constructor can also autoinsert before another instruction.
2900 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2901 Instruction *InsertBefore)
2902 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2903 0, 0, InsertBefore) {
2904 init(Value, Default, NumCases);
2907 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2908 /// switch on and a default destination. The number of additional cases can
2909 /// be specified here to make memory allocation more efficient. This
2910 /// constructor also autoinserts at the end of the specified BasicBlock.
2911 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2912 BasicBlock *InsertAtEnd)
2913 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2914 0, 0, InsertAtEnd) {
2915 init(Value, Default, NumCases);
2918 SwitchInst::SwitchInst(const SwitchInst &SI)
2919 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
2920 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
2921 Use *OL = OperandList, *InOL = SI.OperandList;
2922 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2924 OL[i+1] = InOL[i+1];
2926 SubclassOptionalData = SI.SubclassOptionalData;
2929 SwitchInst::~SwitchInst() {
2930 dropHungoffUses(OperandList);
2934 /// addCase - Add an entry to the switch instruction...
2936 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
2937 unsigned OpNo = NumOperands;
2938 if (OpNo+2 > ReservedSpace)
2939 resizeOperands(0); // Get more space!
2940 // Initialize some new operands.
2941 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
2942 NumOperands = OpNo+2;
2943 OperandList[OpNo] = OnVal;
2944 OperandList[OpNo+1] = Dest;
2947 /// removeCase - This method removes the specified successor from the switch
2948 /// instruction. Note that this cannot be used to remove the default
2949 /// destination (successor #0).
2951 void SwitchInst::removeCase(unsigned idx) {
2952 assert(idx != 0 && "Cannot remove the default case!");
2953 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2955 unsigned NumOps = getNumOperands();
2956 Use *OL = OperandList;
2958 // Move everything after this operand down.
2960 // FIXME: we could just swap with the end of the list, then erase. However,
2961 // client might not expect this to happen. The code as it is thrashes the
2962 // use/def lists, which is kinda lame.
2963 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2965 OL[i-2+1] = OL[i+1];
2968 // Nuke the last value.
2969 OL[NumOps-2].set(0);
2970 OL[NumOps-2+1].set(0);
2971 NumOperands = NumOps-2;
2974 /// resizeOperands - resize operands - This adjusts the length of the operands
2975 /// list according to the following behavior:
2976 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
2977 /// of operation. This grows the number of ops by 3 times.
2978 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2979 /// 3. If NumOps == NumOperands, trim the reserved space.
2981 void SwitchInst::resizeOperands(unsigned NumOps) {
2982 unsigned e = getNumOperands();
2985 } else if (NumOps*2 > NumOperands) {
2986 // No resize needed.
2987 if (ReservedSpace >= NumOps) return;
2988 } else if (NumOps == NumOperands) {
2989 if (ReservedSpace == NumOps) return;
2994 ReservedSpace = NumOps;
2995 Use *NewOps = allocHungoffUses(NumOps);
2996 Use *OldOps = OperandList;
2997 for (unsigned i = 0; i != e; ++i) {
2998 NewOps[i] = OldOps[i];
3000 OperandList = NewOps;
3001 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3005 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3006 return getSuccessor(idx);
3008 unsigned SwitchInst::getNumSuccessorsV() const {
3009 return getNumSuccessors();
3011 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3012 setSuccessor(idx, B);
3015 // Define these methods here so vtables don't get emitted into every translation
3016 // unit that uses these classes.
3018 GetElementPtrInst *GetElementPtrInst::clone() const {
3019 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
3020 New->SubclassOptionalData = SubclassOptionalData;
3021 if (hasMetadata()) {
3022 LLVMContext &Context = getContext();
3023 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3028 BinaryOperator *BinaryOperator::clone() const {
3029 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
3030 New->SubclassOptionalData = SubclassOptionalData;
3031 if (hasMetadata()) {
3032 LLVMContext &Context = getContext();
3033 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3038 FCmpInst* FCmpInst::clone() const {
3039 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
3040 New->SubclassOptionalData = SubclassOptionalData;
3041 if (hasMetadata()) {
3042 LLVMContext &Context = getContext();
3043 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3047 ICmpInst* ICmpInst::clone() const {
3048 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
3049 New->SubclassOptionalData = SubclassOptionalData;
3050 if (hasMetadata()) {
3051 LLVMContext &Context = getContext();
3052 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3057 ExtractValueInst *ExtractValueInst::clone() const {
3058 ExtractValueInst *New = new ExtractValueInst(*this);
3059 New->SubclassOptionalData = SubclassOptionalData;
3060 if (hasMetadata()) {
3061 LLVMContext &Context = getContext();
3062 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3066 InsertValueInst *InsertValueInst::clone() const {
3067 InsertValueInst *New = new InsertValueInst(*this);
3068 New->SubclassOptionalData = SubclassOptionalData;
3069 if (hasMetadata()) {
3070 LLVMContext &Context = getContext();
3071 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3076 MallocInst *MallocInst::clone() const {
3077 MallocInst *New = new MallocInst(getAllocatedType(),
3078 (Value*)getOperand(0),
3080 New->SubclassOptionalData = SubclassOptionalData;
3081 if (hasMetadata()) {
3082 LLVMContext &Context = getContext();
3083 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3088 AllocaInst *AllocaInst::clone() const {
3089 AllocaInst *New = new AllocaInst(getAllocatedType(),
3090 (Value*)getOperand(0),
3092 New->SubclassOptionalData = SubclassOptionalData;
3093 if (hasMetadata()) {
3094 LLVMContext &Context = getContext();
3095 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3100 FreeInst *FreeInst::clone() const {
3101 FreeInst *New = new FreeInst(getOperand(0));
3102 New->SubclassOptionalData = SubclassOptionalData;
3103 if (hasMetadata()) {
3104 LLVMContext &Context = getContext();
3105 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3110 LoadInst *LoadInst::clone() const {
3111 LoadInst *New = new LoadInst(getOperand(0),
3112 Twine(), isVolatile(),
3114 New->SubclassOptionalData = SubclassOptionalData;
3115 if (hasMetadata()) {
3116 LLVMContext &Context = getContext();
3117 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3122 StoreInst *StoreInst::clone() const {
3123 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
3124 isVolatile(), getAlignment());
3125 New->SubclassOptionalData = SubclassOptionalData;
3126 if (hasMetadata()) {
3127 LLVMContext &Context = getContext();
3128 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3133 TruncInst *TruncInst::clone() const {
3134 TruncInst *New = new TruncInst(getOperand(0), getType());
3135 New->SubclassOptionalData = SubclassOptionalData;
3136 if (hasMetadata()) {
3137 LLVMContext &Context = getContext();
3138 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3143 ZExtInst *ZExtInst::clone() const {
3144 ZExtInst *New = new ZExtInst(getOperand(0), getType());
3145 New->SubclassOptionalData = SubclassOptionalData;
3146 if (hasMetadata()) {
3147 LLVMContext &Context = getContext();
3148 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3153 SExtInst *SExtInst::clone() const {
3154 SExtInst *New = new SExtInst(getOperand(0), getType());
3155 New->SubclassOptionalData = SubclassOptionalData;
3156 if (hasMetadata()) {
3157 LLVMContext &Context = getContext();
3158 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3163 FPTruncInst *FPTruncInst::clone() const {
3164 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
3165 New->SubclassOptionalData = SubclassOptionalData;
3166 if (hasMetadata()) {
3167 LLVMContext &Context = getContext();
3168 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3173 FPExtInst *FPExtInst::clone() const {
3174 FPExtInst *New = new FPExtInst(getOperand(0), getType());
3175 New->SubclassOptionalData = SubclassOptionalData;
3176 if (hasMetadata()) {
3177 LLVMContext &Context = getContext();
3178 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3183 UIToFPInst *UIToFPInst::clone() const {
3184 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
3185 New->SubclassOptionalData = SubclassOptionalData;
3186 if (hasMetadata()) {
3187 LLVMContext &Context = getContext();
3188 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3193 SIToFPInst *SIToFPInst::clone() const {
3194 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
3195 New->SubclassOptionalData = SubclassOptionalData;
3196 if (hasMetadata()) {
3197 LLVMContext &Context = getContext();
3198 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3203 FPToUIInst *FPToUIInst::clone() const {
3204 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
3205 New->SubclassOptionalData = SubclassOptionalData;
3206 if (hasMetadata()) {
3207 LLVMContext &Context = getContext();
3208 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3213 FPToSIInst *FPToSIInst::clone() const {
3214 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
3215 New->SubclassOptionalData = SubclassOptionalData;
3216 if (hasMetadata()) {
3217 LLVMContext &Context = getContext();
3218 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3223 PtrToIntInst *PtrToIntInst::clone() const {
3224 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
3225 New->SubclassOptionalData = SubclassOptionalData;
3226 if (hasMetadata()) {
3227 LLVMContext &Context = getContext();
3228 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3233 IntToPtrInst *IntToPtrInst::clone() const {
3234 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
3235 New->SubclassOptionalData = SubclassOptionalData;
3236 if (hasMetadata()) {
3237 LLVMContext &Context = getContext();
3238 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3243 BitCastInst *BitCastInst::clone() const {
3244 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3245 New->SubclassOptionalData = SubclassOptionalData;
3246 if (hasMetadata()) {
3247 LLVMContext &Context = getContext();
3248 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3253 CallInst *CallInst::clone() const {
3254 CallInst *New = new(getNumOperands()) CallInst(*this);
3255 New->SubclassOptionalData = SubclassOptionalData;
3256 if (hasMetadata()) {
3257 LLVMContext &Context = getContext();
3258 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3263 SelectInst *SelectInst::clone() const {
3264 SelectInst *New = SelectInst::Create(getOperand(0),
3267 New->SubclassOptionalData = SubclassOptionalData;
3268 if (hasMetadata()) {
3269 LLVMContext &Context = getContext();
3270 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3275 VAArgInst *VAArgInst::clone() const {
3276 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3277 New->SubclassOptionalData = SubclassOptionalData;
3278 if (hasMetadata()) {
3279 LLVMContext &Context = getContext();
3280 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3285 ExtractElementInst *ExtractElementInst::clone() const {
3286 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3288 New->SubclassOptionalData = SubclassOptionalData;
3289 if (hasMetadata()) {
3290 LLVMContext &Context = getContext();
3291 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3296 InsertElementInst *InsertElementInst::clone() const {
3297 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3300 New->SubclassOptionalData = SubclassOptionalData;
3301 if (hasMetadata()) {
3302 LLVMContext &Context = getContext();
3303 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3308 ShuffleVectorInst *ShuffleVectorInst::clone() const {
3309 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3312 New->SubclassOptionalData = SubclassOptionalData;
3313 if (hasMetadata()) {
3314 LLVMContext &Context = getContext();
3315 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3320 PHINode *PHINode::clone() const {
3321 PHINode *New = new PHINode(*this);
3322 New->SubclassOptionalData = SubclassOptionalData;
3323 if (hasMetadata()) {
3324 LLVMContext &Context = getContext();
3325 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3330 ReturnInst *ReturnInst::clone() const {
3331 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3332 New->SubclassOptionalData = SubclassOptionalData;
3333 if (hasMetadata()) {
3334 LLVMContext &Context = getContext();
3335 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3340 BranchInst *BranchInst::clone() const {
3341 unsigned Ops(getNumOperands());
3342 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3343 New->SubclassOptionalData = SubclassOptionalData;
3344 if (hasMetadata()) {
3345 LLVMContext &Context = getContext();
3346 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3351 SwitchInst *SwitchInst::clone() const {
3352 SwitchInst *New = new SwitchInst(*this);
3353 New->SubclassOptionalData = SubclassOptionalData;
3354 if (hasMetadata()) {
3355 LLVMContext &Context = getContext();
3356 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3361 InvokeInst *InvokeInst::clone() const {
3362 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3363 New->SubclassOptionalData = SubclassOptionalData;
3364 if (hasMetadata()) {
3365 LLVMContext &Context = getContext();
3366 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3371 UnwindInst *UnwindInst::clone() const {
3372 LLVMContext &Context = getContext();
3373 UnwindInst *New = new UnwindInst(Context);
3374 New->SubclassOptionalData = SubclassOptionalData;
3376 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3380 UnreachableInst *UnreachableInst::clone() const {
3381 LLVMContext &Context = getContext();
3382 UnreachableInst *New = new UnreachableInst(Context);
3383 New->SubclassOptionalData = SubclassOptionalData;
3385 Context.pImpl->TheMetadata.ValueIsCloned(this, New);