1 //===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements all of the non-inline methods for the LLVM instruction
13 //===----------------------------------------------------------------------===//
15 #include "LLVMContextImpl.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Module.h"
21 #include "llvm/Operator.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/CallSite.h"
25 #include "llvm/Support/ConstantRange.h"
26 #include "llvm/Support/MathExtras.h"
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
33 User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
36 ? cast<CallInst>(II)->op_begin()
37 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Function
40 //===----------------------------------------------------------------------===//
41 // TerminatorInst Class
42 //===----------------------------------------------------------------------===//
44 // Out of line virtual method, so the vtable, etc has a home.
45 TerminatorInst::~TerminatorInst() {
48 //===----------------------------------------------------------------------===//
49 // UnaryInstruction Class
50 //===----------------------------------------------------------------------===//
52 // Out of line virtual method, so the vtable, etc has a home.
53 UnaryInstruction::~UnaryInstruction() {
56 //===----------------------------------------------------------------------===//
58 //===----------------------------------------------------------------------===//
60 /// areInvalidOperands - Return a string if the specified operands are invalid
61 /// for a select operation, otherwise return null.
62 const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
66 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
68 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
69 return "vector select condition element type must be i1";
70 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
72 return "selected values for vector select must be vectors";
73 if (ET->getNumElements() != VT->getNumElements())
74 return "vector select requires selected vectors to have "
75 "the same vector length as select condition";
76 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
77 return "select condition must be i1 or <n x i1>";
83 //===----------------------------------------------------------------------===//
85 //===----------------------------------------------------------------------===//
87 PHINode::PHINode(const PHINode &PN)
88 : Instruction(PN.getType(), Instruction::PHI,
89 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
90 ReservedSpace(PN.getNumOperands()) {
91 Use *OL = OperandList;
92 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
93 OL[i] = PN.getOperand(i);
94 OL[i+1] = PN.getOperand(i+1);
96 SubclassOptionalData = PN.SubclassOptionalData;
101 dropHungoffUses(OperandList);
104 // removeIncomingValue - Remove an incoming value. This is useful if a
105 // predecessor basic block is deleted.
106 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
107 unsigned NumOps = getNumOperands();
108 Use *OL = OperandList;
109 assert(Idx*2 < NumOps && "BB not in PHI node!");
110 Value *Removed = OL[Idx*2];
112 // Move everything after this operand down.
114 // FIXME: we could just swap with the end of the list, then erase. However,
115 // client might not expect this to happen. The code as it is thrashes the
116 // use/def lists, which is kinda lame.
117 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
122 // Nuke the last value.
124 OL[NumOps-2+1].set(0);
125 NumOperands = NumOps-2;
127 // If the PHI node is dead, because it has zero entries, nuke it now.
128 if (NumOps == 2 && DeletePHIIfEmpty) {
129 // If anyone is using this PHI, make them use a dummy value instead...
130 replaceAllUsesWith(UndefValue::get(getType()));
136 /// resizeOperands - resize operands - This adjusts the length of the operands
137 /// list according to the following behavior:
138 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
139 /// of operation. This grows the number of ops by 1.5 times.
140 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
141 /// 3. If NumOps == NumOperands, trim the reserved space.
143 void PHINode::resizeOperands(unsigned NumOps) {
144 unsigned e = getNumOperands();
147 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
148 } else if (NumOps*2 > NumOperands) {
150 if (ReservedSpace >= NumOps) return;
151 } else if (NumOps == NumOperands) {
152 if (ReservedSpace == NumOps) return;
157 ReservedSpace = NumOps;
158 Use *OldOps = OperandList;
159 Use *NewOps = allocHungoffUses(NumOps);
160 std::copy(OldOps, OldOps + e, NewOps);
161 OperandList = NewOps;
162 if (OldOps) Use::zap(OldOps, OldOps + e, true);
165 /// hasConstantValue - If the specified PHI node always merges together the same
166 /// value, return the value, otherwise return null.
168 /// If the PHI has undef operands, but all the rest of the operands are
169 /// some unique value, return that value if it can be proved that the
170 /// value dominates the PHI. If DT is null, use a conservative check,
171 /// otherwise use DT to test for dominance.
173 Value *PHINode::hasConstantValue(DominatorTree *DT) const {
174 // If the PHI node only has one incoming value, eliminate the PHI node.
175 if (getNumIncomingValues() == 1) {
176 if (getIncomingValue(0) != this) // not X = phi X
177 return getIncomingValue(0);
178 return UndefValue::get(getType()); // Self cycle is dead.
181 // Otherwise if all of the incoming values are the same for the PHI, replace
182 // the PHI node with the incoming value.
185 bool HasUndefInput = false;
186 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
187 if (isa<UndefValue>(getIncomingValue(i))) {
188 HasUndefInput = true;
189 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
190 if (InVal && getIncomingValue(i) != InVal)
191 return 0; // Not the same, bail out.
192 InVal = getIncomingValue(i);
195 // The only case that could cause InVal to be null is if we have a PHI node
196 // that only has entries for itself. In this case, there is no entry into the
197 // loop, so kill the PHI.
199 if (InVal == 0) InVal = UndefValue::get(getType());
201 // If we have a PHI node like phi(X, undef, X), where X is defined by some
202 // instruction, we cannot always return X as the result of the PHI node. Only
203 // do this if X is not an instruction (thus it must dominate the PHI block),
204 // or if the client is prepared to deal with this possibility.
205 if (!HasUndefInput || !isa<Instruction>(InVal))
208 Instruction *IV = cast<Instruction>(InVal);
210 // We have a DominatorTree. Do a precise test.
211 if (!DT->dominates(IV, this))
214 // If it is in the entry block, it obviously dominates everything.
215 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
217 return 0; // Cannot guarantee that InVal dominates this PHINode.
220 // All of the incoming values are the same, return the value now.
225 //===----------------------------------------------------------------------===//
226 // CallInst Implementation
227 //===----------------------------------------------------------------------===//
229 CallInst::~CallInst() {
232 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
233 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
234 Use *OL = OperandList;
237 const FunctionType *FTy =
238 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
239 FTy = FTy; // silence warning.
241 assert((NumParams == FTy->getNumParams() ||
242 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
243 "Calling a function with bad signature!");
244 for (unsigned i = 0; i != NumParams; ++i) {
245 assert((i >= FTy->getNumParams() ||
246 FTy->getParamType(i) == Params[i]->getType()) &&
247 "Calling a function with a bad signature!");
252 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
253 assert(NumOperands == 3 && "NumOperands not set up?");
254 Use *OL = OperandList;
259 const FunctionType *FTy =
260 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
261 FTy = FTy; // silence warning.
263 assert((FTy->getNumParams() == 2 ||
264 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
265 "Calling a function with bad signature");
266 assert((0 >= FTy->getNumParams() ||
267 FTy->getParamType(0) == Actual1->getType()) &&
268 "Calling a function with a bad signature!");
269 assert((1 >= FTy->getNumParams() ||
270 FTy->getParamType(1) == Actual2->getType()) &&
271 "Calling a function with a bad signature!");
274 void CallInst::init(Value *Func, Value *Actual) {
275 assert(NumOperands == 2 && "NumOperands not set up?");
276 Use *OL = OperandList;
280 const FunctionType *FTy =
281 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
282 FTy = FTy; // silence warning.
284 assert((FTy->getNumParams() == 1 ||
285 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
286 "Calling a function with bad signature");
287 assert((0 == FTy->getNumParams() ||
288 FTy->getParamType(0) == Actual->getType()) &&
289 "Calling a function with a bad signature!");
292 void CallInst::init(Value *Func) {
293 assert(NumOperands == 1 && "NumOperands not set up?");
294 Use *OL = OperandList;
297 const FunctionType *FTy =
298 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
299 FTy = FTy; // silence warning.
301 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
304 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
305 Instruction *InsertBefore)
306 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
307 ->getElementType())->getReturnType(),
309 OperandTraits<CallInst>::op_end(this) - 2,
315 CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
316 BasicBlock *InsertAtEnd)
317 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
318 ->getElementType())->getReturnType(),
320 OperandTraits<CallInst>::op_end(this) - 2,
325 CallInst::CallInst(Value *Func, const Twine &Name,
326 Instruction *InsertBefore)
327 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
328 ->getElementType())->getReturnType(),
330 OperandTraits<CallInst>::op_end(this) - 1,
336 CallInst::CallInst(Value *Func, const Twine &Name,
337 BasicBlock *InsertAtEnd)
338 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
339 ->getElementType())->getReturnType(),
341 OperandTraits<CallInst>::op_end(this) - 1,
347 CallInst::CallInst(const CallInst &CI)
348 : Instruction(CI.getType(), Instruction::Call,
349 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
350 CI.getNumOperands()) {
351 setAttributes(CI.getAttributes());
352 setTailCall(CI.isTailCall());
353 setCallingConv(CI.getCallingConv());
355 Use *OL = OperandList;
356 Use *InOL = CI.OperandList;
357 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
359 SubclassOptionalData = CI.SubclassOptionalData;
362 void CallInst::addAttribute(unsigned i, Attributes attr) {
363 AttrListPtr PAL = getAttributes();
364 PAL = PAL.addAttr(i, attr);
368 void CallInst::removeAttribute(unsigned i, Attributes attr) {
369 AttrListPtr PAL = getAttributes();
370 PAL = PAL.removeAttr(i, attr);
374 bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
375 if (AttributeList.paramHasAttr(i, attr))
377 if (const Function *F = getCalledFunction())
378 return F->paramHasAttr(i, attr);
382 /// IsConstantOne - Return true only if val is constant int 1
383 static bool IsConstantOne(Value *val) {
384 assert(val && "IsConstantOne does not work with NULL val");
385 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
388 static Instruction *createMalloc(Instruction *InsertBefore,
389 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
390 const Type *AllocTy, Value *AllocSize,
391 Value *ArraySize, Function *MallocF,
393 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
394 "createMalloc needs either InsertBefore or InsertAtEnd");
396 // malloc(type) becomes:
397 // bitcast (i8* malloc(typeSize)) to type*
398 // malloc(type, arraySize) becomes:
399 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
401 ArraySize = ConstantInt::get(IntPtrTy, 1);
402 else if (ArraySize->getType() != IntPtrTy) {
404 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
407 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
411 if (!IsConstantOne(ArraySize)) {
412 if (IsConstantOne(AllocSize)) {
413 AllocSize = ArraySize; // Operand * 1 = Operand
414 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
415 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
417 // Malloc arg is constant product of type size and array size
418 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
420 // Multiply type size by the array size...
422 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
423 "mallocsize", InsertBefore);
425 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
426 "mallocsize", InsertAtEnd);
430 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
431 // Create the call to Malloc.
432 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
433 Module* M = BB->getParent()->getParent();
434 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
435 Value *MallocFunc = MallocF;
437 // prototype malloc as "void *malloc(size_t)"
438 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
439 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
440 CallInst *MCall = NULL;
441 Instruction *Result = NULL;
443 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
445 if (Result->getType() != AllocPtrType)
446 // Create a cast instruction to convert to the right type...
447 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
449 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
451 if (Result->getType() != AllocPtrType) {
452 InsertAtEnd->getInstList().push_back(MCall);
453 // Create a cast instruction to convert to the right type...
454 Result = new BitCastInst(MCall, AllocPtrType, Name);
457 MCall->setTailCall();
458 if (Function *F = dyn_cast<Function>(MallocFunc)) {
459 MCall->setCallingConv(F->getCallingConv());
460 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
462 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
467 /// CreateMalloc - Generate the IR for a call to malloc:
468 /// 1. Compute the malloc call's argument as the specified type's size,
469 /// possibly multiplied by the array size if the array size is not
471 /// 2. Call malloc with that argument.
472 /// 3. Bitcast the result of the malloc call to the specified type.
473 Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
474 const Type *IntPtrTy, const Type *AllocTy,
475 Value *AllocSize, Value *ArraySize,
477 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
478 ArraySize, NULL, Name);
481 /// CreateMalloc - Generate the IR for a call to malloc:
482 /// 1. Compute the malloc call's argument as the specified type's size,
483 /// possibly multiplied by the array size if the array size is not
485 /// 2. Call malloc with that argument.
486 /// 3. Bitcast the result of the malloc call to the specified type.
487 /// Note: This function does not add the bitcast to the basic block, that is the
488 /// responsibility of the caller.
489 Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
490 const Type *IntPtrTy, const Type *AllocTy,
491 Value *AllocSize, Value *ArraySize,
492 Function *MallocF, const Twine &Name) {
493 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
494 ArraySize, MallocF, Name);
497 static Instruction* createFree(Value* Source, Instruction *InsertBefore,
498 BasicBlock *InsertAtEnd) {
499 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
500 "createFree needs either InsertBefore or InsertAtEnd");
501 assert(Source->getType()->isPointerTy() &&
502 "Can not free something of nonpointer type!");
504 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
505 Module* M = BB->getParent()->getParent();
507 const Type *VoidTy = Type::getVoidTy(M->getContext());
508 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
509 // prototype free as "void free(void*)"
510 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
511 CallInst* Result = NULL;
512 Value *PtrCast = Source;
514 if (Source->getType() != IntPtrTy)
515 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
516 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
518 if (Source->getType() != IntPtrTy)
519 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
520 Result = CallInst::Create(FreeFunc, PtrCast, "");
522 Result->setTailCall();
523 if (Function *F = dyn_cast<Function>(FreeFunc))
524 Result->setCallingConv(F->getCallingConv());
529 /// CreateFree - Generate the IR for a call to the builtin free function.
530 void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
531 createFree(Source, InsertBefore, NULL);
534 /// CreateFree - Generate the IR for a call to the builtin free function.
535 /// Note: This function does not add the call to the basic block, that is the
536 /// responsibility of the caller.
537 Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
538 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
539 assert(FreeCall && "CreateFree did not create a CallInst");
543 //===----------------------------------------------------------------------===//
544 // InvokeInst Implementation
545 //===----------------------------------------------------------------------===//
547 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
548 Value* const *Args, unsigned NumArgs) {
549 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
552 Op<-1>() = IfException;
553 const FunctionType *FTy =
554 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
555 FTy = FTy; // silence warning.
557 assert(((NumArgs == FTy->getNumParams()) ||
558 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
559 "Invoking a function with bad signature");
561 Use *OL = OperandList;
562 for (unsigned i = 0, e = NumArgs; i != e; i++) {
563 assert((i >= FTy->getNumParams() ||
564 FTy->getParamType(i) == Args[i]->getType()) &&
565 "Invoking a function with a bad signature!");
571 InvokeInst::InvokeInst(const InvokeInst &II)
572 : TerminatorInst(II.getType(), Instruction::Invoke,
573 OperandTraits<InvokeInst>::op_end(this)
574 - II.getNumOperands(),
575 II.getNumOperands()) {
576 setAttributes(II.getAttributes());
577 setCallingConv(II.getCallingConv());
578 Use *OL = OperandList, *InOL = II.OperandList;
579 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
581 SubclassOptionalData = II.SubclassOptionalData;
584 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
585 return getSuccessor(idx);
587 unsigned InvokeInst::getNumSuccessorsV() const {
588 return getNumSuccessors();
590 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
591 return setSuccessor(idx, B);
594 bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
595 if (AttributeList.paramHasAttr(i, attr))
597 if (const Function *F = getCalledFunction())
598 return F->paramHasAttr(i, attr);
602 void InvokeInst::addAttribute(unsigned i, Attributes attr) {
603 AttrListPtr PAL = getAttributes();
604 PAL = PAL.addAttr(i, attr);
608 void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
609 AttrListPtr PAL = getAttributes();
610 PAL = PAL.removeAttr(i, attr);
615 //===----------------------------------------------------------------------===//
616 // ReturnInst Implementation
617 //===----------------------------------------------------------------------===//
619 ReturnInst::ReturnInst(const ReturnInst &RI)
620 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
621 OperandTraits<ReturnInst>::op_end(this) -
623 RI.getNumOperands()) {
624 if (RI.getNumOperands())
625 Op<0>() = RI.Op<0>();
626 SubclassOptionalData = RI.SubclassOptionalData;
629 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
630 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
631 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
636 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
637 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
638 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
643 ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
644 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
645 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
648 unsigned ReturnInst::getNumSuccessorsV() const {
649 return getNumSuccessors();
652 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
653 /// emit the vtable for the class in this translation unit.
654 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
655 llvm_unreachable("ReturnInst has no successors!");
658 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
659 llvm_unreachable("ReturnInst has no successors!");
663 ReturnInst::~ReturnInst() {
666 //===----------------------------------------------------------------------===//
667 // UnwindInst Implementation
668 //===----------------------------------------------------------------------===//
670 UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
671 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
672 0, 0, InsertBefore) {
674 UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
675 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
680 unsigned UnwindInst::getNumSuccessorsV() const {
681 return getNumSuccessors();
684 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
685 llvm_unreachable("UnwindInst has no successors!");
688 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
689 llvm_unreachable("UnwindInst has no successors!");
693 //===----------------------------------------------------------------------===//
694 // UnreachableInst Implementation
695 //===----------------------------------------------------------------------===//
697 UnreachableInst::UnreachableInst(LLVMContext &Context,
698 Instruction *InsertBefore)
699 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
700 0, 0, InsertBefore) {
702 UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
703 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
707 unsigned UnreachableInst::getNumSuccessorsV() const {
708 return getNumSuccessors();
711 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
712 llvm_unreachable("UnwindInst has no successors!");
715 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
716 llvm_unreachable("UnwindInst has no successors!");
720 //===----------------------------------------------------------------------===//
721 // BranchInst Implementation
722 //===----------------------------------------------------------------------===//
724 void BranchInst::AssertOK() {
726 assert(getCondition()->getType()->isIntegerTy(1) &&
727 "May only branch on boolean predicates!");
730 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
731 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
732 OperandTraits<BranchInst>::op_end(this) - 1,
734 assert(IfTrue != 0 && "Branch destination may not be null!");
737 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
738 Instruction *InsertBefore)
739 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
740 OperandTraits<BranchInst>::op_end(this) - 3,
750 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
751 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
752 OperandTraits<BranchInst>::op_end(this) - 1,
754 assert(IfTrue != 0 && "Branch destination may not be null!");
758 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
759 BasicBlock *InsertAtEnd)
760 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
761 OperandTraits<BranchInst>::op_end(this) - 3,
772 BranchInst::BranchInst(const BranchInst &BI) :
773 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
774 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
775 BI.getNumOperands()) {
776 Op<-1>() = BI.Op<-1>();
777 if (BI.getNumOperands() != 1) {
778 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
779 Op<-3>() = BI.Op<-3>();
780 Op<-2>() = BI.Op<-2>();
782 SubclassOptionalData = BI.SubclassOptionalData;
786 Use* Use::getPrefix() {
787 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
788 if (PotentialPrefix.getOpaqueValue())
791 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
794 BranchInst::~BranchInst() {
795 if (NumOperands == 1) {
796 if (Use *Prefix = OperandList->getPrefix()) {
799 // mark OperandList to have a special value for scrutiny
800 // by baseclass destructors and operator delete
801 OperandList = Prefix;
804 OperandList = op_begin();
810 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
811 return getSuccessor(idx);
813 unsigned BranchInst::getNumSuccessorsV() const {
814 return getNumSuccessors();
816 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
817 setSuccessor(idx, B);
821 //===----------------------------------------------------------------------===//
822 // AllocaInst Implementation
823 //===----------------------------------------------------------------------===//
825 static Value *getAISize(LLVMContext &Context, Value *Amt) {
827 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
829 assert(!isa<BasicBlock>(Amt) &&
830 "Passed basic block into allocation size parameter! Use other ctor");
831 assert(Amt->getType()->isIntegerTy() &&
832 "Allocation array size is not an integer!");
837 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
838 const Twine &Name, Instruction *InsertBefore)
839 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
840 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
842 assert(!Ty->isVoidTy() && "Cannot allocate void!");
846 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
847 const Twine &Name, BasicBlock *InsertAtEnd)
848 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
849 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
851 assert(!Ty->isVoidTy() && "Cannot allocate void!");
855 AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
856 Instruction *InsertBefore)
857 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
858 getAISize(Ty->getContext(), 0), InsertBefore) {
860 assert(!Ty->isVoidTy() && "Cannot allocate void!");
864 AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
865 BasicBlock *InsertAtEnd)
866 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
867 getAISize(Ty->getContext(), 0), InsertAtEnd) {
869 assert(!Ty->isVoidTy() && "Cannot allocate void!");
873 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
874 const Twine &Name, Instruction *InsertBefore)
875 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
876 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
878 assert(!Ty->isVoidTy() && "Cannot allocate void!");
882 AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
883 const Twine &Name, BasicBlock *InsertAtEnd)
884 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
885 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
887 assert(!Ty->isVoidTy() && "Cannot allocate void!");
891 // Out of line virtual method, so the vtable, etc has a home.
892 AllocaInst::~AllocaInst() {
895 void AllocaInst::setAlignment(unsigned Align) {
896 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
897 setInstructionSubclassData(Log2_32(Align) + 1);
898 assert(getAlignment() == Align && "Alignment representation error!");
901 bool AllocaInst::isArrayAllocation() const {
902 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
903 return CI->getZExtValue() != 1;
907 const Type *AllocaInst::getAllocatedType() const {
908 return getType()->getElementType();
911 /// isStaticAlloca - Return true if this alloca is in the entry block of the
912 /// function and is a constant size. If so, the code generator will fold it
913 /// into the prolog/epilog code, so it is basically free.
914 bool AllocaInst::isStaticAlloca() const {
915 // Must be constant size.
916 if (!isa<ConstantInt>(getArraySize())) return false;
918 // Must be in the entry block.
919 const BasicBlock *Parent = getParent();
920 return Parent == &Parent->getParent()->front();
923 //===----------------------------------------------------------------------===//
924 // LoadInst Implementation
925 //===----------------------------------------------------------------------===//
927 void LoadInst::AssertOK() {
928 assert(getOperand(0)->getType()->isPointerTy() &&
929 "Ptr must have pointer type.");
932 LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
933 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
934 Load, Ptr, InsertBef) {
941 LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
942 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
943 Load, Ptr, InsertAE) {
950 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
951 Instruction *InsertBef)
952 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
953 Load, Ptr, InsertBef) {
954 setVolatile(isVolatile);
960 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
961 unsigned Align, Instruction *InsertBef)
962 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
963 Load, Ptr, InsertBef) {
964 setVolatile(isVolatile);
970 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
971 unsigned Align, BasicBlock *InsertAE)
972 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
973 Load, Ptr, InsertAE) {
974 setVolatile(isVolatile);
980 LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
981 BasicBlock *InsertAE)
982 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
983 Load, Ptr, InsertAE) {
984 setVolatile(isVolatile);
992 LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
993 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
994 Load, Ptr, InsertBef) {
998 if (Name && Name[0]) setName(Name);
1001 LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1002 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1003 Load, Ptr, InsertAE) {
1007 if (Name && Name[0]) setName(Name);
1010 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1011 Instruction *InsertBef)
1012 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1013 Load, Ptr, InsertBef) {
1014 setVolatile(isVolatile);
1017 if (Name && Name[0]) setName(Name);
1020 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1021 BasicBlock *InsertAE)
1022 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1023 Load, Ptr, InsertAE) {
1024 setVolatile(isVolatile);
1027 if (Name && Name[0]) setName(Name);
1030 void LoadInst::setAlignment(unsigned Align) {
1031 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1032 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1033 ((Log2_32(Align)+1)<<1));
1036 //===----------------------------------------------------------------------===//
1037 // StoreInst Implementation
1038 //===----------------------------------------------------------------------===//
1040 void StoreInst::AssertOK() {
1041 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
1042 assert(getOperand(1)->getType()->isPointerTy() &&
1043 "Ptr must have pointer type!");
1044 assert(getOperand(0)->getType() ==
1045 cast<PointerType>(getOperand(1)->getType())->getElementType()
1046 && "Ptr must be a pointer to Val type!");
1050 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
1051 : Instruction(Type::getVoidTy(val->getContext()), Store,
1052 OperandTraits<StoreInst>::op_begin(this),
1053 OperandTraits<StoreInst>::operands(this),
1062 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
1063 : Instruction(Type::getVoidTy(val->getContext()), Store,
1064 OperandTraits<StoreInst>::op_begin(this),
1065 OperandTraits<StoreInst>::operands(this),
1074 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1075 Instruction *InsertBefore)
1076 : Instruction(Type::getVoidTy(val->getContext()), Store,
1077 OperandTraits<StoreInst>::op_begin(this),
1078 OperandTraits<StoreInst>::operands(this),
1082 setVolatile(isVolatile);
1087 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1088 unsigned Align, Instruction *InsertBefore)
1089 : Instruction(Type::getVoidTy(val->getContext()), Store,
1090 OperandTraits<StoreInst>::op_begin(this),
1091 OperandTraits<StoreInst>::operands(this),
1095 setVolatile(isVolatile);
1096 setAlignment(Align);
1100 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1101 unsigned Align, BasicBlock *InsertAtEnd)
1102 : Instruction(Type::getVoidTy(val->getContext()), Store,
1103 OperandTraits<StoreInst>::op_begin(this),
1104 OperandTraits<StoreInst>::operands(this),
1108 setVolatile(isVolatile);
1109 setAlignment(Align);
1113 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1114 BasicBlock *InsertAtEnd)
1115 : Instruction(Type::getVoidTy(val->getContext()), Store,
1116 OperandTraits<StoreInst>::op_begin(this),
1117 OperandTraits<StoreInst>::operands(this),
1121 setVolatile(isVolatile);
1126 void StoreInst::setAlignment(unsigned Align) {
1127 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1128 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1129 ((Log2_32(Align)+1) << 1));
1132 //===----------------------------------------------------------------------===//
1133 // GetElementPtrInst Implementation
1134 //===----------------------------------------------------------------------===//
1136 static unsigned retrieveAddrSpace(const Value *Val) {
1137 return cast<PointerType>(Val->getType())->getAddressSpace();
1140 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
1141 const Twine &Name) {
1142 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1143 Use *OL = OperandList;
1146 for (unsigned i = 0; i != NumIdx; ++i)
1152 void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
1153 assert(NumOperands == 2 && "NumOperands not initialized?");
1154 Use *OL = OperandList;
1161 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1162 : Instruction(GEPI.getType(), GetElementPtr,
1163 OperandTraits<GetElementPtrInst>::op_end(this)
1164 - GEPI.getNumOperands(),
1165 GEPI.getNumOperands()) {
1166 Use *OL = OperandList;
1167 Use *GEPIOL = GEPI.OperandList;
1168 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1170 SubclassOptionalData = GEPI.SubclassOptionalData;
1173 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1174 const Twine &Name, Instruction *InBe)
1175 : Instruction(PointerType::get(
1176 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
1178 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1180 init(Ptr, Idx, Name);
1183 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1184 const Twine &Name, BasicBlock *IAE)
1185 : Instruction(PointerType::get(
1186 checkType(getIndexedType(Ptr->getType(),Idx)),
1187 retrieveAddrSpace(Ptr)),
1189 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1191 init(Ptr, Idx, Name);
1194 /// getIndexedType - Returns the type of the element that would be accessed with
1195 /// a gep instruction with the specified parameters.
1197 /// The Idxs pointer should point to a continuous piece of memory containing the
1198 /// indices, either as Value* or uint64_t.
1200 /// A null type is returned if the indices are invalid for the specified
1203 template <typename IndexTy>
1204 static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1206 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1207 if (!PTy) return 0; // Type isn't a pointer type!
1208 const Type *Agg = PTy->getElementType();
1210 // Handle the special case of the empty set index set, which is always valid.
1214 // If there is at least one index, the top level type must be sized, otherwise
1215 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1216 // that contain opaque types) under the assumption that it will be resolved to
1217 // a sane type later.
1218 if (!Agg->isSized() && !Agg->isAbstract())
1221 unsigned CurIdx = 1;
1222 for (; CurIdx != NumIdx; ++CurIdx) {
1223 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1224 if (!CT || CT->isPointerTy()) return 0;
1225 IndexTy Index = Idxs[CurIdx];
1226 if (!CT->indexValid(Index)) return 0;
1227 Agg = CT->getTypeAtIndex(Index);
1229 // If the new type forwards to another type, then it is in the middle
1230 // of being refined to another type (and hence, may have dropped all
1231 // references to what it was using before). So, use the new forwarded
1233 if (const Type *Ty = Agg->getForwardedType())
1236 return CurIdx == NumIdx ? Agg : 0;
1239 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1242 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1245 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1246 uint64_t const *Idxs,
1248 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1251 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1252 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1253 if (!PTy) return 0; // Type isn't a pointer type!
1255 // Check the pointer index.
1256 if (!PTy->indexValid(Idx)) return 0;
1258 return PTy->getElementType();
1262 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1263 /// zeros. If so, the result pointer and the first operand have the same
1264 /// value, just potentially different types.
1265 bool GetElementPtrInst::hasAllZeroIndices() const {
1266 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1267 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1268 if (!CI->isZero()) return false;
1276 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1277 /// constant integers. If so, the result pointer and the first operand have
1278 /// a constant offset between them.
1279 bool GetElementPtrInst::hasAllConstantIndices() const {
1280 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1281 if (!isa<ConstantInt>(getOperand(i)))
1287 void GetElementPtrInst::setIsInBounds(bool B) {
1288 cast<GEPOperator>(this)->setIsInBounds(B);
1291 bool GetElementPtrInst::isInBounds() const {
1292 return cast<GEPOperator>(this)->isInBounds();
1295 //===----------------------------------------------------------------------===//
1296 // ExtractElementInst Implementation
1297 //===----------------------------------------------------------------------===//
1299 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1301 Instruction *InsertBef)
1302 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1304 OperandTraits<ExtractElementInst>::op_begin(this),
1306 assert(isValidOperands(Val, Index) &&
1307 "Invalid extractelement instruction operands!");
1313 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1315 BasicBlock *InsertAE)
1316 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1318 OperandTraits<ExtractElementInst>::op_begin(this),
1320 assert(isValidOperands(Val, Index) &&
1321 "Invalid extractelement instruction operands!");
1329 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1330 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
1336 //===----------------------------------------------------------------------===//
1337 // InsertElementInst Implementation
1338 //===----------------------------------------------------------------------===//
1340 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1342 Instruction *InsertBef)
1343 : Instruction(Vec->getType(), InsertElement,
1344 OperandTraits<InsertElementInst>::op_begin(this),
1346 assert(isValidOperands(Vec, Elt, Index) &&
1347 "Invalid insertelement instruction operands!");
1354 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1356 BasicBlock *InsertAE)
1357 : Instruction(Vec->getType(), InsertElement,
1358 OperandTraits<InsertElementInst>::op_begin(this),
1360 assert(isValidOperands(Vec, Elt, Index) &&
1361 "Invalid insertelement instruction operands!");
1369 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1370 const Value *Index) {
1371 if (!Vec->getType()->isVectorTy())
1372 return false; // First operand of insertelement must be vector type.
1374 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1375 return false;// Second operand of insertelement must be vector element type.
1377 if (!Index->getType()->isIntegerTy(32))
1378 return false; // Third operand of insertelement must be i32.
1383 //===----------------------------------------------------------------------===//
1384 // ShuffleVectorInst Implementation
1385 //===----------------------------------------------------------------------===//
1387 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1389 Instruction *InsertBefore)
1390 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1391 cast<VectorType>(Mask->getType())->getNumElements()),
1393 OperandTraits<ShuffleVectorInst>::op_begin(this),
1394 OperandTraits<ShuffleVectorInst>::operands(this),
1396 assert(isValidOperands(V1, V2, Mask) &&
1397 "Invalid shuffle vector instruction operands!");
1404 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1406 BasicBlock *InsertAtEnd)
1407 : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1408 cast<VectorType>(Mask->getType())->getNumElements()),
1410 OperandTraits<ShuffleVectorInst>::op_begin(this),
1411 OperandTraits<ShuffleVectorInst>::operands(this),
1413 assert(isValidOperands(V1, V2, Mask) &&
1414 "Invalid shuffle vector instruction operands!");
1422 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1423 const Value *Mask) {
1424 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
1427 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1428 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1429 !MaskTy->getElementType()->isIntegerTy(32))
1434 /// getMaskValue - Return the index from the shuffle mask for the specified
1435 /// output result. This is either -1 if the element is undef or a number less
1436 /// than 2*numelements.
1437 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1438 const Constant *Mask = cast<Constant>(getOperand(2));
1439 if (isa<UndefValue>(Mask)) return -1;
1440 if (isa<ConstantAggregateZero>(Mask)) return 0;
1441 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1442 assert(i < MaskCV->getNumOperands() && "Index out of range");
1444 if (isa<UndefValue>(MaskCV->getOperand(i)))
1446 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1449 //===----------------------------------------------------------------------===//
1450 // InsertValueInst Class
1451 //===----------------------------------------------------------------------===//
1453 void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1454 unsigned NumIdx, const Twine &Name) {
1455 assert(NumOperands == 2 && "NumOperands not initialized?");
1459 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
1463 void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1464 const Twine &Name) {
1465 assert(NumOperands == 2 && "NumOperands not initialized?");
1469 Indices.push_back(Idx);
1473 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1474 : Instruction(IVI.getType(), InsertValue,
1475 OperandTraits<InsertValueInst>::op_begin(this), 2),
1476 Indices(IVI.Indices) {
1477 Op<0>() = IVI.getOperand(0);
1478 Op<1>() = IVI.getOperand(1);
1479 SubclassOptionalData = IVI.SubclassOptionalData;
1482 InsertValueInst::InsertValueInst(Value *Agg,
1486 Instruction *InsertBefore)
1487 : Instruction(Agg->getType(), InsertValue,
1488 OperandTraits<InsertValueInst>::op_begin(this),
1490 init(Agg, Val, Idx, Name);
1493 InsertValueInst::InsertValueInst(Value *Agg,
1497 BasicBlock *InsertAtEnd)
1498 : Instruction(Agg->getType(), InsertValue,
1499 OperandTraits<InsertValueInst>::op_begin(this),
1501 init(Agg, Val, Idx, Name);
1504 //===----------------------------------------------------------------------===//
1505 // ExtractValueInst Class
1506 //===----------------------------------------------------------------------===//
1508 void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
1509 const Twine &Name) {
1510 assert(NumOperands == 1 && "NumOperands not initialized?");
1512 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
1516 void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
1517 assert(NumOperands == 1 && "NumOperands not initialized?");
1519 Indices.push_back(Idx);
1523 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1524 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
1525 Indices(EVI.Indices) {
1526 SubclassOptionalData = EVI.SubclassOptionalData;
1529 // getIndexedType - Returns the type of the element that would be extracted
1530 // with an extractvalue instruction with the specified parameters.
1532 // A null type is returned if the indices are invalid for the specified
1535 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1536 const unsigned *Idxs,
1538 unsigned CurIdx = 0;
1539 for (; CurIdx != NumIdx; ++CurIdx) {
1540 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1541 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
1542 unsigned Index = Idxs[CurIdx];
1543 if (!CT->indexValid(Index)) return 0;
1544 Agg = CT->getTypeAtIndex(Index);
1546 // If the new type forwards to another type, then it is in the middle
1547 // of being refined to another type (and hence, may have dropped all
1548 // references to what it was using before). So, use the new forwarded
1550 if (const Type *Ty = Agg->getForwardedType())
1553 return CurIdx == NumIdx ? Agg : 0;
1556 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1558 return getIndexedType(Agg, &Idx, 1);
1561 //===----------------------------------------------------------------------===//
1562 // BinaryOperator Class
1563 //===----------------------------------------------------------------------===//
1565 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1566 const Type *Ty, const Twine &Name,
1567 Instruction *InsertBefore)
1568 : Instruction(Ty, iType,
1569 OperandTraits<BinaryOperator>::op_begin(this),
1570 OperandTraits<BinaryOperator>::operands(this),
1578 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1579 const Type *Ty, const Twine &Name,
1580 BasicBlock *InsertAtEnd)
1581 : Instruction(Ty, iType,
1582 OperandTraits<BinaryOperator>::op_begin(this),
1583 OperandTraits<BinaryOperator>::operands(this),
1592 void BinaryOperator::init(BinaryOps iType) {
1593 Value *LHS = getOperand(0), *RHS = getOperand(1);
1594 LHS = LHS; RHS = RHS; // Silence warnings.
1595 assert(LHS->getType() == RHS->getType() &&
1596 "Binary operator operand types must match!");
1601 assert(getType() == LHS->getType() &&
1602 "Arithmetic operation should return same type as operands!");
1603 assert(getType()->isIntOrIntVectorTy() &&
1604 "Tried to create an integer operation on a non-integer type!");
1606 case FAdd: case FSub:
1608 assert(getType() == LHS->getType() &&
1609 "Arithmetic operation should return same type as operands!");
1610 assert(getType()->isFPOrFPVectorTy() &&
1611 "Tried to create a floating-point operation on a "
1612 "non-floating-point type!");
1616 assert(getType() == LHS->getType() &&
1617 "Arithmetic operation should return same type as operands!");
1618 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1619 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1620 "Incorrect operand type (not integer) for S/UDIV");
1623 assert(getType() == LHS->getType() &&
1624 "Arithmetic operation should return same type as operands!");
1625 assert(getType()->isFPOrFPVectorTy() &&
1626 "Incorrect operand type (not floating point) for FDIV");
1630 assert(getType() == LHS->getType() &&
1631 "Arithmetic operation should return same type as operands!");
1632 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1633 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1634 "Incorrect operand type (not integer) for S/UREM");
1637 assert(getType() == LHS->getType() &&
1638 "Arithmetic operation should return same type as operands!");
1639 assert(getType()->isFPOrFPVectorTy() &&
1640 "Incorrect operand type (not floating point) for FREM");
1645 assert(getType() == LHS->getType() &&
1646 "Shift operation should return same type as operands!");
1647 assert((getType()->isIntegerTy() ||
1648 (getType()->isVectorTy() &&
1649 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1650 "Tried to create a shift operation on a non-integral type!");
1654 assert(getType() == LHS->getType() &&
1655 "Logical operation should return same type as operands!");
1656 assert((getType()->isIntegerTy() ||
1657 (getType()->isVectorTy() &&
1658 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1659 "Tried to create a logical operation on a non-integral type!");
1667 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1669 Instruction *InsertBefore) {
1670 assert(S1->getType() == S2->getType() &&
1671 "Cannot create binary operator with two operands of differing type!");
1672 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1675 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1677 BasicBlock *InsertAtEnd) {
1678 BinaryOperator *Res = Create(Op, S1, S2, Name);
1679 InsertAtEnd->getInstList().push_back(Res);
1683 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1684 Instruction *InsertBefore) {
1685 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1686 return new BinaryOperator(Instruction::Sub,
1688 Op->getType(), Name, InsertBefore);
1691 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1692 BasicBlock *InsertAtEnd) {
1693 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1694 return new BinaryOperator(Instruction::Sub,
1696 Op->getType(), Name, InsertAtEnd);
1699 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1700 Instruction *InsertBefore) {
1701 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1702 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1705 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1706 BasicBlock *InsertAtEnd) {
1707 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1708 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1711 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1712 Instruction *InsertBefore) {
1713 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1714 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1717 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1718 BasicBlock *InsertAtEnd) {
1719 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1720 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1723 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1724 Instruction *InsertBefore) {
1725 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1726 return new BinaryOperator(Instruction::FSub,
1728 Op->getType(), Name, InsertBefore);
1731 BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1732 BasicBlock *InsertAtEnd) {
1733 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1734 return new BinaryOperator(Instruction::FSub,
1736 Op->getType(), Name, InsertAtEnd);
1739 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1740 Instruction *InsertBefore) {
1742 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1743 C = Constant::getAllOnesValue(PTy->getElementType());
1744 C = ConstantVector::get(
1745 std::vector<Constant*>(PTy->getNumElements(), C));
1747 C = Constant::getAllOnesValue(Op->getType());
1750 return new BinaryOperator(Instruction::Xor, Op, C,
1751 Op->getType(), Name, InsertBefore);
1754 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1755 BasicBlock *InsertAtEnd) {
1757 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1758 // Create a vector of all ones values.
1759 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
1760 AllOnes = ConstantVector::get(
1761 std::vector<Constant*>(PTy->getNumElements(), Elt));
1763 AllOnes = Constant::getAllOnesValue(Op->getType());
1766 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1767 Op->getType(), Name, InsertAtEnd);
1771 // isConstantAllOnes - Helper function for several functions below
1772 static inline bool isConstantAllOnes(const Value *V) {
1773 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1774 return CI->isAllOnesValue();
1775 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1776 return CV->isAllOnesValue();
1780 bool BinaryOperator::isNeg(const Value *V) {
1781 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1782 if (Bop->getOpcode() == Instruction::Sub)
1783 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1784 return C->isNegativeZeroValue();
1788 bool BinaryOperator::isFNeg(const Value *V) {
1789 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1790 if (Bop->getOpcode() == Instruction::FSub)
1791 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1792 return C->isNegativeZeroValue();
1796 bool BinaryOperator::isNot(const Value *V) {
1797 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1798 return (Bop->getOpcode() == Instruction::Xor &&
1799 (isConstantAllOnes(Bop->getOperand(1)) ||
1800 isConstantAllOnes(Bop->getOperand(0))));
1804 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1805 return cast<BinaryOperator>(BinOp)->getOperand(1);
1808 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1809 return getNegArgument(const_cast<Value*>(BinOp));
1812 Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1813 return cast<BinaryOperator>(BinOp)->getOperand(1);
1816 const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1817 return getFNegArgument(const_cast<Value*>(BinOp));
1820 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1821 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1822 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1823 Value *Op0 = BO->getOperand(0);
1824 Value *Op1 = BO->getOperand(1);
1825 if (isConstantAllOnes(Op0)) return Op1;
1827 assert(isConstantAllOnes(Op1));
1831 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1832 return getNotArgument(const_cast<Value*>(BinOp));
1836 // swapOperands - Exchange the two operands to this instruction. This
1837 // instruction is safe to use on any binary instruction and does not
1838 // modify the semantics of the instruction. If the instruction is
1839 // order dependent (SetLT f.e.) the opcode is changed.
1841 bool BinaryOperator::swapOperands() {
1842 if (!isCommutative())
1843 return true; // Can't commute operands
1844 Op<0>().swap(Op<1>());
1848 void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1849 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1852 void BinaryOperator::setHasNoSignedWrap(bool b) {
1853 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1856 void BinaryOperator::setIsExact(bool b) {
1857 cast<SDivOperator>(this)->setIsExact(b);
1860 bool BinaryOperator::hasNoUnsignedWrap() const {
1861 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1864 bool BinaryOperator::hasNoSignedWrap() const {
1865 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1868 bool BinaryOperator::isExact() const {
1869 return cast<SDivOperator>(this)->isExact();
1872 //===----------------------------------------------------------------------===//
1874 //===----------------------------------------------------------------------===//
1876 // Just determine if this cast only deals with integral->integral conversion.
1877 bool CastInst::isIntegerCast() const {
1878 switch (getOpcode()) {
1879 default: return false;
1880 case Instruction::ZExt:
1881 case Instruction::SExt:
1882 case Instruction::Trunc:
1884 case Instruction::BitCast:
1885 return getOperand(0)->getType()->isIntegerTy() &&
1886 getType()->isIntegerTy();
1890 bool CastInst::isLosslessCast() const {
1891 // Only BitCast can be lossless, exit fast if we're not BitCast
1892 if (getOpcode() != Instruction::BitCast)
1895 // Identity cast is always lossless
1896 const Type* SrcTy = getOperand(0)->getType();
1897 const Type* DstTy = getType();
1901 // Pointer to pointer is always lossless.
1902 if (SrcTy->isPointerTy())
1903 return DstTy->isPointerTy();
1904 return false; // Other types have no identity values
1907 /// This function determines if the CastInst does not require any bits to be
1908 /// changed in order to effect the cast. Essentially, it identifies cases where
1909 /// no code gen is necessary for the cast, hence the name no-op cast. For
1910 /// example, the following are all no-op casts:
1911 /// # bitcast i32* %x to i8*
1912 /// # bitcast <2 x i32> %x to <4 x i16>
1913 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1914 /// @brief Determine if the described cast is a no-op.
1915 bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1918 const Type *IntPtrTy) {
1921 assert(!"Invalid CastOp");
1922 case Instruction::Trunc:
1923 case Instruction::ZExt:
1924 case Instruction::SExt:
1925 case Instruction::FPTrunc:
1926 case Instruction::FPExt:
1927 case Instruction::UIToFP:
1928 case Instruction::SIToFP:
1929 case Instruction::FPToUI:
1930 case Instruction::FPToSI:
1931 return false; // These always modify bits
1932 case Instruction::BitCast:
1933 return true; // BitCast never modifies bits.
1934 case Instruction::PtrToInt:
1935 return IntPtrTy->getScalarSizeInBits() ==
1936 DestTy->getScalarSizeInBits();
1937 case Instruction::IntToPtr:
1938 return IntPtrTy->getScalarSizeInBits() ==
1939 SrcTy->getScalarSizeInBits();
1943 /// @brief Determine if a cast is a no-op.
1944 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1945 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1948 /// This function determines if a pair of casts can be eliminated and what
1949 /// opcode should be used in the elimination. This assumes that there are two
1950 /// instructions like this:
1951 /// * %F = firstOpcode SrcTy %x to MidTy
1952 /// * %S = secondOpcode MidTy %F to DstTy
1953 /// The function returns a resultOpcode so these two casts can be replaced with:
1954 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1955 /// If no such cast is permited, the function returns 0.
1956 unsigned CastInst::isEliminableCastPair(
1957 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1958 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1960 // Define the 144 possibilities for these two cast instructions. The values
1961 // in this matrix determine what to do in a given situation and select the
1962 // case in the switch below. The rows correspond to firstOp, the columns
1963 // correspond to secondOp. In looking at the table below, keep in mind
1964 // the following cast properties:
1966 // Size Compare Source Destination
1967 // Operator Src ? Size Type Sign Type Sign
1968 // -------- ------------ ------------------- ---------------------
1969 // TRUNC > Integer Any Integral Any
1970 // ZEXT < Integral Unsigned Integer Any
1971 // SEXT < Integral Signed Integer Any
1972 // FPTOUI n/a FloatPt n/a Integral Unsigned
1973 // FPTOSI n/a FloatPt n/a Integral Signed
1974 // UITOFP n/a Integral Unsigned FloatPt n/a
1975 // SITOFP n/a Integral Signed FloatPt n/a
1976 // FPTRUNC > FloatPt n/a FloatPt n/a
1977 // FPEXT < FloatPt n/a FloatPt n/a
1978 // PTRTOINT n/a Pointer n/a Integral Unsigned
1979 // INTTOPTR n/a Integral Unsigned Pointer n/a
1980 // BITCAST = FirstClass n/a FirstClass n/a
1982 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1983 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1984 // into "fptoui double to i64", but this loses information about the range
1985 // of the produced value (we no longer know the top-part is all zeros).
1986 // Further this conversion is often much more expensive for typical hardware,
1987 // and causes issues when building libgcc. We disallow fptosi+sext for the
1989 const unsigned numCastOps =
1990 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1991 static const uint8_t CastResults[numCastOps][numCastOps] = {
1992 // T F F U S F F P I B -+
1993 // R Z S P P I I T P 2 N T |
1994 // U E E 2 2 2 2 R E I T C +- secondOp
1995 // N X X U S F F N X N 2 V |
1996 // C T T I I P P C T T P T -+
1997 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1998 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1999 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
2000 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2001 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
2002 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2003 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2004 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2005 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2006 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2007 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2008 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2011 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2012 [secondOp-Instruction::CastOpsBegin];
2015 // categorically disallowed
2018 // allowed, use first cast's opcode
2021 // allowed, use second cast's opcode
2024 // no-op cast in second op implies firstOp as long as the DestTy
2025 // is integer and we are not converting between a vector and a
2027 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
2031 // no-op cast in second op implies firstOp as long as the DestTy
2032 // is floating point.
2033 if (DstTy->isFloatingPointTy())
2037 // no-op cast in first op implies secondOp as long as the SrcTy
2039 if (SrcTy->isIntegerTy())
2043 // no-op cast in first op implies secondOp as long as the SrcTy
2044 // is a floating point.
2045 if (SrcTy->isFloatingPointTy())
2049 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
2052 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2053 unsigned MidSize = MidTy->getScalarSizeInBits();
2054 if (MidSize >= PtrSize)
2055 return Instruction::BitCast;
2059 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2060 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2061 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2062 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2063 unsigned DstSize = DstTy->getScalarSizeInBits();
2064 if (SrcSize == DstSize)
2065 return Instruction::BitCast;
2066 else if (SrcSize < DstSize)
2070 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2071 return Instruction::ZExt;
2073 // fpext followed by ftrunc is allowed if the bit size returned to is
2074 // the same as the original, in which case its just a bitcast
2076 return Instruction::BitCast;
2077 return 0; // If the types are not the same we can't eliminate it.
2079 // bitcast followed by ptrtoint is allowed as long as the bitcast
2080 // is a pointer to pointer cast.
2081 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
2085 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2086 if (MidTy->isPointerTy() && DstTy->isPointerTy())
2090 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2093 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2094 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2095 unsigned DstSize = DstTy->getScalarSizeInBits();
2096 if (SrcSize <= PtrSize && SrcSize == DstSize)
2097 return Instruction::BitCast;
2101 // cast combination can't happen (error in input). This is for all cases
2102 // where the MidTy is not the same for the two cast instructions.
2103 assert(!"Invalid Cast Combination");
2106 assert(!"Error in CastResults table!!!");
2112 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2113 const Twine &Name, Instruction *InsertBefore) {
2114 // Construct and return the appropriate CastInst subclass
2116 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2117 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2118 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2119 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2120 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2121 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2122 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2123 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2124 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2125 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2126 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2127 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2129 assert(!"Invalid opcode provided");
2134 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
2135 const Twine &Name, BasicBlock *InsertAtEnd) {
2136 // Construct and return the appropriate CastInst subclass
2138 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2139 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2140 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2141 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2142 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2143 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2144 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2145 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2146 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2147 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2148 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2149 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2151 assert(!"Invalid opcode provided");
2156 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2158 Instruction *InsertBefore) {
2159 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2160 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2161 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
2164 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
2166 BasicBlock *InsertAtEnd) {
2167 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2168 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2169 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
2172 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2174 Instruction *InsertBefore) {
2175 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2176 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2177 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
2180 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
2182 BasicBlock *InsertAtEnd) {
2183 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2184 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2185 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
2188 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2190 Instruction *InsertBefore) {
2191 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2192 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2193 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
2196 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
2198 BasicBlock *InsertAtEnd) {
2199 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2200 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2201 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
2204 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2206 BasicBlock *InsertAtEnd) {
2207 assert(S->getType()->isPointerTy() && "Invalid cast");
2208 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2211 if (Ty->isIntegerTy())
2212 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2213 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2216 /// @brief Create a BitCast or a PtrToInt cast instruction
2217 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2219 Instruction *InsertBefore) {
2220 assert(S->getType()->isPointerTy() && "Invalid cast");
2221 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2224 if (Ty->isIntegerTy())
2225 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2226 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2229 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2230 bool isSigned, const Twine &Name,
2231 Instruction *InsertBefore) {
2232 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2233 "Invalid integer cast");
2234 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2235 unsigned DstBits = Ty->getScalarSizeInBits();
2236 Instruction::CastOps opcode =
2237 (SrcBits == DstBits ? Instruction::BitCast :
2238 (SrcBits > DstBits ? Instruction::Trunc :
2239 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2240 return Create(opcode, C, Ty, Name, InsertBefore);
2243 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2244 bool isSigned, const Twine &Name,
2245 BasicBlock *InsertAtEnd) {
2246 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2248 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2249 unsigned DstBits = Ty->getScalarSizeInBits();
2250 Instruction::CastOps opcode =
2251 (SrcBits == DstBits ? Instruction::BitCast :
2252 (SrcBits > DstBits ? Instruction::Trunc :
2253 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2254 return Create(opcode, C, Ty, Name, InsertAtEnd);
2257 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2259 Instruction *InsertBefore) {
2260 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2262 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2263 unsigned DstBits = Ty->getScalarSizeInBits();
2264 Instruction::CastOps opcode =
2265 (SrcBits == DstBits ? Instruction::BitCast :
2266 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2267 return Create(opcode, C, Ty, Name, InsertBefore);
2270 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2272 BasicBlock *InsertAtEnd) {
2273 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2275 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2276 unsigned DstBits = Ty->getScalarSizeInBits();
2277 Instruction::CastOps opcode =
2278 (SrcBits == DstBits ? Instruction::BitCast :
2279 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2280 return Create(opcode, C, Ty, Name, InsertAtEnd);
2283 // Check whether it is valid to call getCastOpcode for these types.
2284 // This routine must be kept in sync with getCastOpcode.
2285 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2286 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2289 if (SrcTy == DestTy)
2292 // Get the bit sizes, we'll need these
2293 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2294 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2296 // Run through the possibilities ...
2297 if (DestTy->isIntegerTy()) { // Casting to integral
2298 if (SrcTy->isIntegerTy()) { // Casting from integral
2300 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2302 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2303 // Casting from vector
2304 return DestBits == PTy->getBitWidth();
2305 } else { // Casting from something else
2306 return SrcTy->isPointerTy();
2308 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2309 if (SrcTy->isIntegerTy()) { // Casting from integral
2311 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2313 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2314 // Casting from vector
2315 return DestBits == PTy->getBitWidth();
2316 } else { // Casting from something else
2319 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2320 // Casting to vector
2321 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2322 // Casting from vector
2323 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2324 } else { // Casting from something else
2325 return DestPTy->getBitWidth() == SrcBits;
2327 } else if (DestTy->isPointerTy()) { // Casting to pointer
2328 if (SrcTy->isPointerTy()) { // Casting from pointer
2330 } else if (SrcTy->isIntegerTy()) { // Casting from integral
2332 } else { // Casting from something else
2335 } else { // Casting to something else
2340 // Provide a way to get a "cast" where the cast opcode is inferred from the
2341 // types and size of the operand. This, basically, is a parallel of the
2342 // logic in the castIsValid function below. This axiom should hold:
2343 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2344 // should not assert in castIsValid. In other words, this produces a "correct"
2345 // casting opcode for the arguments passed to it.
2346 // This routine must be kept in sync with isCastable.
2347 Instruction::CastOps
2348 CastInst::getCastOpcode(
2349 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
2350 // Get the bit sizes, we'll need these
2351 const Type *SrcTy = Src->getType();
2352 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2353 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
2355 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2356 "Only first class types are castable!");
2358 // Run through the possibilities ...
2359 if (DestTy->isIntegerTy()) { // Casting to integral
2360 if (SrcTy->isIntegerTy()) { // Casting from integral
2361 if (DestBits < SrcBits)
2362 return Trunc; // int -> smaller int
2363 else if (DestBits > SrcBits) { // its an extension
2365 return SExt; // signed -> SEXT
2367 return ZExt; // unsigned -> ZEXT
2369 return BitCast; // Same size, No-op cast
2371 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2373 return FPToSI; // FP -> sint
2375 return FPToUI; // FP -> uint
2376 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2377 assert(DestBits == PTy->getBitWidth() &&
2378 "Casting vector to integer of different width");
2380 return BitCast; // Same size, no-op cast
2382 assert(SrcTy->isPointerTy() &&
2383 "Casting from a value that is not first-class type");
2384 return PtrToInt; // ptr -> int
2386 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2387 if (SrcTy->isIntegerTy()) { // Casting from integral
2389 return SIToFP; // sint -> FP
2391 return UIToFP; // uint -> FP
2392 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2393 if (DestBits < SrcBits) {
2394 return FPTrunc; // FP -> smaller FP
2395 } else if (DestBits > SrcBits) {
2396 return FPExt; // FP -> larger FP
2398 return BitCast; // same size, no-op cast
2400 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2401 assert(DestBits == PTy->getBitWidth() &&
2402 "Casting vector to floating point of different width");
2404 return BitCast; // same size, no-op cast
2406 llvm_unreachable("Casting pointer or non-first class to float");
2408 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2409 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2410 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
2411 "Casting vector to vector of different widths");
2413 return BitCast; // vector -> vector
2414 } else if (DestPTy->getBitWidth() == SrcBits) {
2415 return BitCast; // float/int -> vector
2417 assert(!"Illegal cast to vector (wrong type or size)");
2419 } else if (DestTy->isPointerTy()) {
2420 if (SrcTy->isPointerTy()) {
2421 return BitCast; // ptr -> ptr
2422 } else if (SrcTy->isIntegerTy()) {
2423 return IntToPtr; // int -> ptr
2425 assert(!"Casting pointer to other than pointer or int");
2428 assert(!"Casting to type that is not first-class");
2431 // If we fall through to here we probably hit an assertion cast above
2432 // and assertions are not turned on. Anything we return is an error, so
2433 // BitCast is as good a choice as any.
2437 //===----------------------------------------------------------------------===//
2438 // CastInst SubClass Constructors
2439 //===----------------------------------------------------------------------===//
2441 /// Check that the construction parameters for a CastInst are correct. This
2442 /// could be broken out into the separate constructors but it is useful to have
2443 /// it in one place and to eliminate the redundant code for getting the sizes
2444 /// of the types involved.
2446 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
2448 // Check for type sanity on the arguments
2449 const Type *SrcTy = S->getType();
2450 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2451 SrcTy->isAggregateType() || DstTy->isAggregateType())
2454 // Get the size of the types in bits, we'll need this later
2455 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2456 unsigned DstBitSize = DstTy->getScalarSizeInBits();
2458 // Switch on the opcode provided
2460 default: return false; // This is an input error
2461 case Instruction::Trunc:
2462 return SrcTy->isIntOrIntVectorTy() &&
2463 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
2464 case Instruction::ZExt:
2465 return SrcTy->isIntOrIntVectorTy() &&
2466 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2467 case Instruction::SExt:
2468 return SrcTy->isIntOrIntVectorTy() &&
2469 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
2470 case Instruction::FPTrunc:
2471 return SrcTy->isFPOrFPVectorTy() &&
2472 DstTy->isFPOrFPVectorTy() &&
2473 SrcBitSize > DstBitSize;
2474 case Instruction::FPExt:
2475 return SrcTy->isFPOrFPVectorTy() &&
2476 DstTy->isFPOrFPVectorTy() &&
2477 SrcBitSize < DstBitSize;
2478 case Instruction::UIToFP:
2479 case Instruction::SIToFP:
2480 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2481 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2482 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2483 DVTy->getElementType()->isFPOrFPVectorTy() &&
2484 SVTy->getNumElements() == DVTy->getNumElements();
2487 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
2488 case Instruction::FPToUI:
2489 case Instruction::FPToSI:
2490 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2491 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2492 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2493 DVTy->getElementType()->isIntOrIntVectorTy() &&
2494 SVTy->getNumElements() == DVTy->getNumElements();
2497 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
2498 case Instruction::PtrToInt:
2499 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
2500 case Instruction::IntToPtr:
2501 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
2502 case Instruction::BitCast:
2503 // BitCast implies a no-op cast of type only. No bits change.
2504 // However, you can't cast pointers to anything but pointers.
2505 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
2508 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2509 // these cases, the cast is okay if the source and destination bit widths
2511 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2515 TruncInst::TruncInst(
2516 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2517 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2518 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2521 TruncInst::TruncInst(
2522 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2523 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2524 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2528 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2529 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2530 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2534 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2535 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2536 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2539 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2540 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2541 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2545 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2546 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2547 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2550 FPTruncInst::FPTruncInst(
2551 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2552 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2553 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2556 FPTruncInst::FPTruncInst(
2557 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2558 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2559 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2562 FPExtInst::FPExtInst(
2563 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2564 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2565 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2568 FPExtInst::FPExtInst(
2569 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2570 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2571 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2574 UIToFPInst::UIToFPInst(
2575 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2576 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2577 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2580 UIToFPInst::UIToFPInst(
2581 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2582 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2583 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2586 SIToFPInst::SIToFPInst(
2587 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2588 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2589 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2592 SIToFPInst::SIToFPInst(
2593 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2594 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2595 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2598 FPToUIInst::FPToUIInst(
2599 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2600 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2601 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2604 FPToUIInst::FPToUIInst(
2605 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2606 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2607 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2610 FPToSIInst::FPToSIInst(
2611 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2612 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2613 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2616 FPToSIInst::FPToSIInst(
2617 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2618 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2619 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2622 PtrToIntInst::PtrToIntInst(
2623 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2624 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2625 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2628 PtrToIntInst::PtrToIntInst(
2629 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2630 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2631 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2634 IntToPtrInst::IntToPtrInst(
2635 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2636 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2637 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2640 IntToPtrInst::IntToPtrInst(
2641 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2642 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2643 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2646 BitCastInst::BitCastInst(
2647 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
2648 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2649 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2652 BitCastInst::BitCastInst(
2653 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2654 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2655 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2658 //===----------------------------------------------------------------------===//
2660 //===----------------------------------------------------------------------===//
2662 void CmpInst::Anchor() const {}
2664 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2665 Value *LHS, Value *RHS, const Twine &Name,
2666 Instruction *InsertBefore)
2667 : Instruction(ty, op,
2668 OperandTraits<CmpInst>::op_begin(this),
2669 OperandTraits<CmpInst>::operands(this),
2673 setPredicate((Predicate)predicate);
2677 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2678 Value *LHS, Value *RHS, const Twine &Name,
2679 BasicBlock *InsertAtEnd)
2680 : Instruction(ty, op,
2681 OperandTraits<CmpInst>::op_begin(this),
2682 OperandTraits<CmpInst>::operands(this),
2686 setPredicate((Predicate)predicate);
2691 CmpInst::Create(OtherOps Op, unsigned short predicate,
2692 Value *S1, Value *S2,
2693 const Twine &Name, Instruction *InsertBefore) {
2694 if (Op == Instruction::ICmp) {
2696 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2699 return new ICmpInst(CmpInst::Predicate(predicate),
2704 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2707 return new FCmpInst(CmpInst::Predicate(predicate),
2712 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2713 const Twine &Name, BasicBlock *InsertAtEnd) {
2714 if (Op == Instruction::ICmp) {
2715 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2718 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2722 void CmpInst::swapOperands() {
2723 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2726 cast<FCmpInst>(this)->swapOperands();
2729 bool CmpInst::isCommutative() {
2730 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2731 return IC->isCommutative();
2732 return cast<FCmpInst>(this)->isCommutative();
2735 bool CmpInst::isEquality() {
2736 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2737 return IC->isEquality();
2738 return cast<FCmpInst>(this)->isEquality();
2742 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
2744 default: assert(!"Unknown cmp predicate!");
2745 case ICMP_EQ: return ICMP_NE;
2746 case ICMP_NE: return ICMP_EQ;
2747 case ICMP_UGT: return ICMP_ULE;
2748 case ICMP_ULT: return ICMP_UGE;
2749 case ICMP_UGE: return ICMP_ULT;
2750 case ICMP_ULE: return ICMP_UGT;
2751 case ICMP_SGT: return ICMP_SLE;
2752 case ICMP_SLT: return ICMP_SGE;
2753 case ICMP_SGE: return ICMP_SLT;
2754 case ICMP_SLE: return ICMP_SGT;
2756 case FCMP_OEQ: return FCMP_UNE;
2757 case FCMP_ONE: return FCMP_UEQ;
2758 case FCMP_OGT: return FCMP_ULE;
2759 case FCMP_OLT: return FCMP_UGE;
2760 case FCMP_OGE: return FCMP_ULT;
2761 case FCMP_OLE: return FCMP_UGT;
2762 case FCMP_UEQ: return FCMP_ONE;
2763 case FCMP_UNE: return FCMP_OEQ;
2764 case FCMP_UGT: return FCMP_OLE;
2765 case FCMP_ULT: return FCMP_OGE;
2766 case FCMP_UGE: return FCMP_OLT;
2767 case FCMP_ULE: return FCMP_OGT;
2768 case FCMP_ORD: return FCMP_UNO;
2769 case FCMP_UNO: return FCMP_ORD;
2770 case FCMP_TRUE: return FCMP_FALSE;
2771 case FCMP_FALSE: return FCMP_TRUE;
2775 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2777 default: assert(! "Unknown icmp predicate!");
2778 case ICMP_EQ: case ICMP_NE:
2779 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2781 case ICMP_UGT: return ICMP_SGT;
2782 case ICMP_ULT: return ICMP_SLT;
2783 case ICMP_UGE: return ICMP_SGE;
2784 case ICMP_ULE: return ICMP_SLE;
2788 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2790 default: assert(! "Unknown icmp predicate!");
2791 case ICMP_EQ: case ICMP_NE:
2792 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2794 case ICMP_SGT: return ICMP_UGT;
2795 case ICMP_SLT: return ICMP_ULT;
2796 case ICMP_SGE: return ICMP_UGE;
2797 case ICMP_SLE: return ICMP_ULE;
2801 /// Initialize a set of values that all satisfy the condition with C.
2804 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2807 uint32_t BitWidth = C.getBitWidth();
2809 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
2810 case ICmpInst::ICMP_EQ: Upper++; break;
2811 case ICmpInst::ICMP_NE: Lower++; break;
2812 case ICmpInst::ICMP_ULT:
2813 Lower = APInt::getMinValue(BitWidth);
2814 // Check for an empty-set condition.
2816 return ConstantRange(BitWidth, /*isFullSet=*/false);
2818 case ICmpInst::ICMP_SLT:
2819 Lower = APInt::getSignedMinValue(BitWidth);
2820 // Check for an empty-set condition.
2822 return ConstantRange(BitWidth, /*isFullSet=*/false);
2824 case ICmpInst::ICMP_UGT:
2825 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2826 // Check for an empty-set condition.
2828 return ConstantRange(BitWidth, /*isFullSet=*/false);
2830 case ICmpInst::ICMP_SGT:
2831 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2832 // Check for an empty-set condition.
2834 return ConstantRange(BitWidth, /*isFullSet=*/false);
2836 case ICmpInst::ICMP_ULE:
2837 Lower = APInt::getMinValue(BitWidth); Upper++;
2838 // Check for a full-set condition.
2840 return ConstantRange(BitWidth, /*isFullSet=*/true);
2842 case ICmpInst::ICMP_SLE:
2843 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2844 // Check for a full-set condition.
2846 return ConstantRange(BitWidth, /*isFullSet=*/true);
2848 case ICmpInst::ICMP_UGE:
2849 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2850 // Check for a full-set condition.
2852 return ConstantRange(BitWidth, /*isFullSet=*/true);
2854 case ICmpInst::ICMP_SGE:
2855 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2856 // Check for a full-set condition.
2858 return ConstantRange(BitWidth, /*isFullSet=*/true);
2861 return ConstantRange(Lower, Upper);
2864 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
2866 default: assert(!"Unknown cmp predicate!");
2867 case ICMP_EQ: case ICMP_NE:
2869 case ICMP_SGT: return ICMP_SLT;
2870 case ICMP_SLT: return ICMP_SGT;
2871 case ICMP_SGE: return ICMP_SLE;
2872 case ICMP_SLE: return ICMP_SGE;
2873 case ICMP_UGT: return ICMP_ULT;
2874 case ICMP_ULT: return ICMP_UGT;
2875 case ICMP_UGE: return ICMP_ULE;
2876 case ICMP_ULE: return ICMP_UGE;
2878 case FCMP_FALSE: case FCMP_TRUE:
2879 case FCMP_OEQ: case FCMP_ONE:
2880 case FCMP_UEQ: case FCMP_UNE:
2881 case FCMP_ORD: case FCMP_UNO:
2883 case FCMP_OGT: return FCMP_OLT;
2884 case FCMP_OLT: return FCMP_OGT;
2885 case FCMP_OGE: return FCMP_OLE;
2886 case FCMP_OLE: return FCMP_OGE;
2887 case FCMP_UGT: return FCMP_ULT;
2888 case FCMP_ULT: return FCMP_UGT;
2889 case FCMP_UGE: return FCMP_ULE;
2890 case FCMP_ULE: return FCMP_UGE;
2894 bool CmpInst::isUnsigned(unsigned short predicate) {
2895 switch (predicate) {
2896 default: return false;
2897 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2898 case ICmpInst::ICMP_UGE: return true;
2902 bool CmpInst::isSigned(unsigned short predicate) {
2903 switch (predicate) {
2904 default: return false;
2905 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2906 case ICmpInst::ICMP_SGE: return true;
2910 bool CmpInst::isOrdered(unsigned short predicate) {
2911 switch (predicate) {
2912 default: return false;
2913 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2914 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2915 case FCmpInst::FCMP_ORD: return true;
2919 bool CmpInst::isUnordered(unsigned short predicate) {
2920 switch (predicate) {
2921 default: return false;
2922 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2923 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2924 case FCmpInst::FCMP_UNO: return true;
2928 bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2930 default: return false;
2931 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2932 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2936 bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2938 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2939 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2940 default: return false;
2945 //===----------------------------------------------------------------------===//
2946 // SwitchInst Implementation
2947 //===----------------------------------------------------------------------===//
2949 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
2950 assert(Value && Default);
2951 ReservedSpace = 2+NumCases*2;
2953 OperandList = allocHungoffUses(ReservedSpace);
2955 OperandList[0] = Value;
2956 OperandList[1] = Default;
2959 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2960 /// switch on and a default destination. The number of additional cases can
2961 /// be specified here to make memory allocation more efficient. This
2962 /// constructor can also autoinsert before another instruction.
2963 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2964 Instruction *InsertBefore)
2965 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2966 0, 0, InsertBefore) {
2967 init(Value, Default, NumCases);
2970 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2971 /// switch on and a default destination. The number of additional cases can
2972 /// be specified here to make memory allocation more efficient. This
2973 /// constructor also autoinserts at the end of the specified BasicBlock.
2974 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2975 BasicBlock *InsertAtEnd)
2976 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2977 0, 0, InsertAtEnd) {
2978 init(Value, Default, NumCases);
2981 SwitchInst::SwitchInst(const SwitchInst &SI)
2982 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
2983 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
2984 Use *OL = OperandList, *InOL = SI.OperandList;
2985 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2987 OL[i+1] = InOL[i+1];
2989 SubclassOptionalData = SI.SubclassOptionalData;
2992 SwitchInst::~SwitchInst() {
2993 dropHungoffUses(OperandList);
2997 /// addCase - Add an entry to the switch instruction...
2999 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
3000 unsigned OpNo = NumOperands;
3001 if (OpNo+2 > ReservedSpace)
3002 resizeOperands(0); // Get more space!
3003 // Initialize some new operands.
3004 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
3005 NumOperands = OpNo+2;
3006 OperandList[OpNo] = OnVal;
3007 OperandList[OpNo+1] = Dest;
3010 /// removeCase - This method removes the specified successor from the switch
3011 /// instruction. Note that this cannot be used to remove the default
3012 /// destination (successor #0).
3014 void SwitchInst::removeCase(unsigned idx) {
3015 assert(idx != 0 && "Cannot remove the default case!");
3016 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3018 unsigned NumOps = getNumOperands();
3019 Use *OL = OperandList;
3021 // Move everything after this operand down.
3023 // FIXME: we could just swap with the end of the list, then erase. However,
3024 // client might not expect this to happen. The code as it is thrashes the
3025 // use/def lists, which is kinda lame.
3026 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3028 OL[i-2+1] = OL[i+1];
3031 // Nuke the last value.
3032 OL[NumOps-2].set(0);
3033 OL[NumOps-2+1].set(0);
3034 NumOperands = NumOps-2;
3037 /// resizeOperands - resize operands - This adjusts the length of the operands
3038 /// list according to the following behavior:
3039 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3040 /// of operation. This grows the number of ops by 3 times.
3041 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3042 /// 3. If NumOps == NumOperands, trim the reserved space.
3044 void SwitchInst::resizeOperands(unsigned NumOps) {
3045 unsigned e = getNumOperands();
3048 } else if (NumOps*2 > NumOperands) {
3049 // No resize needed.
3050 if (ReservedSpace >= NumOps) return;
3051 } else if (NumOps == NumOperands) {
3052 if (ReservedSpace == NumOps) return;
3057 ReservedSpace = NumOps;
3058 Use *NewOps = allocHungoffUses(NumOps);
3059 Use *OldOps = OperandList;
3060 for (unsigned i = 0; i != e; ++i) {
3061 NewOps[i] = OldOps[i];
3063 OperandList = NewOps;
3064 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3068 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3069 return getSuccessor(idx);
3071 unsigned SwitchInst::getNumSuccessorsV() const {
3072 return getNumSuccessors();
3074 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3075 setSuccessor(idx, B);
3078 //===----------------------------------------------------------------------===//
3079 // SwitchInst Implementation
3080 //===----------------------------------------------------------------------===//
3082 void IndirectBrInst::init(Value *Address, unsigned NumDests) {
3083 assert(Address && Address->getType()->isPointerTy() &&
3084 "Address of indirectbr must be a pointer");
3085 ReservedSpace = 1+NumDests;
3087 OperandList = allocHungoffUses(ReservedSpace);
3089 OperandList[0] = Address;
3093 /// resizeOperands - resize operands - This adjusts the length of the operands
3094 /// list according to the following behavior:
3095 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
3096 /// of operation. This grows the number of ops by 2 times.
3097 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3098 /// 3. If NumOps == NumOperands, trim the reserved space.
3100 void IndirectBrInst::resizeOperands(unsigned NumOps) {
3101 unsigned e = getNumOperands();
3104 } else if (NumOps*2 > NumOperands) {
3105 // No resize needed.
3106 if (ReservedSpace >= NumOps) return;
3107 } else if (NumOps == NumOperands) {
3108 if (ReservedSpace == NumOps) return;
3113 ReservedSpace = NumOps;
3114 Use *NewOps = allocHungoffUses(NumOps);
3115 Use *OldOps = OperandList;
3116 for (unsigned i = 0; i != e; ++i)
3117 NewOps[i] = OldOps[i];
3118 OperandList = NewOps;
3119 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3122 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3123 Instruction *InsertBefore)
3124 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3125 0, 0, InsertBefore) {
3126 init(Address, NumCases);
3129 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3130 BasicBlock *InsertAtEnd)
3131 : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3132 0, 0, InsertAtEnd) {
3133 init(Address, NumCases);
3136 IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3137 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3138 allocHungoffUses(IBI.getNumOperands()),
3139 IBI.getNumOperands()) {
3140 Use *OL = OperandList, *InOL = IBI.OperandList;
3141 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3143 SubclassOptionalData = IBI.SubclassOptionalData;
3146 IndirectBrInst::~IndirectBrInst() {
3147 dropHungoffUses(OperandList);
3150 /// addDestination - Add a destination.
3152 void IndirectBrInst::addDestination(BasicBlock *DestBB) {
3153 unsigned OpNo = NumOperands;
3154 if (OpNo+1 > ReservedSpace)
3155 resizeOperands(0); // Get more space!
3156 // Initialize some new operands.
3157 assert(OpNo < ReservedSpace && "Growing didn't work!");
3158 NumOperands = OpNo+1;
3159 OperandList[OpNo] = DestBB;
3162 /// removeDestination - This method removes the specified successor from the
3163 /// indirectbr instruction.
3164 void IndirectBrInst::removeDestination(unsigned idx) {
3165 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3167 unsigned NumOps = getNumOperands();
3168 Use *OL = OperandList;
3170 // Replace this value with the last one.
3171 OL[idx+1] = OL[NumOps-1];
3173 // Nuke the last value.
3174 OL[NumOps-1].set(0);
3175 NumOperands = NumOps-1;
3178 BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
3179 return getSuccessor(idx);
3181 unsigned IndirectBrInst::getNumSuccessorsV() const {
3182 return getNumSuccessors();
3184 void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3185 setSuccessor(idx, B);
3188 //===----------------------------------------------------------------------===//
3189 // clone_impl() implementations
3190 //===----------------------------------------------------------------------===//
3192 // Define these methods here so vtables don't get emitted into every translation
3193 // unit that uses these classes.
3195 GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3196 return new (getNumOperands()) GetElementPtrInst(*this);
3199 BinaryOperator *BinaryOperator::clone_impl() const {
3200 return Create(getOpcode(), Op<0>(), Op<1>());
3203 FCmpInst* FCmpInst::clone_impl() const {
3204 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
3207 ICmpInst* ICmpInst::clone_impl() const {
3208 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
3211 ExtractValueInst *ExtractValueInst::clone_impl() const {
3212 return new ExtractValueInst(*this);
3215 InsertValueInst *InsertValueInst::clone_impl() const {
3216 return new InsertValueInst(*this);
3219 AllocaInst *AllocaInst::clone_impl() const {
3220 return new AllocaInst(getAllocatedType(),
3221 (Value*)getOperand(0),
3225 LoadInst *LoadInst::clone_impl() const {
3226 return new LoadInst(getOperand(0),
3227 Twine(), isVolatile(),
3231 StoreInst *StoreInst::clone_impl() const {
3232 return new StoreInst(getOperand(0), getOperand(1),
3233 isVolatile(), getAlignment());
3236 TruncInst *TruncInst::clone_impl() const {
3237 return new TruncInst(getOperand(0), getType());
3240 ZExtInst *ZExtInst::clone_impl() const {
3241 return new ZExtInst(getOperand(0), getType());
3244 SExtInst *SExtInst::clone_impl() const {
3245 return new SExtInst(getOperand(0), getType());
3248 FPTruncInst *FPTruncInst::clone_impl() const {
3249 return new FPTruncInst(getOperand(0), getType());
3252 FPExtInst *FPExtInst::clone_impl() const {
3253 return new FPExtInst(getOperand(0), getType());
3256 UIToFPInst *UIToFPInst::clone_impl() const {
3257 return new UIToFPInst(getOperand(0), getType());
3260 SIToFPInst *SIToFPInst::clone_impl() const {
3261 return new SIToFPInst(getOperand(0), getType());
3264 FPToUIInst *FPToUIInst::clone_impl() const {
3265 return new FPToUIInst(getOperand(0), getType());
3268 FPToSIInst *FPToSIInst::clone_impl() const {
3269 return new FPToSIInst(getOperand(0), getType());
3272 PtrToIntInst *PtrToIntInst::clone_impl() const {
3273 return new PtrToIntInst(getOperand(0), getType());
3276 IntToPtrInst *IntToPtrInst::clone_impl() const {
3277 return new IntToPtrInst(getOperand(0), getType());
3280 BitCastInst *BitCastInst::clone_impl() const {
3281 return new BitCastInst(getOperand(0), getType());
3284 CallInst *CallInst::clone_impl() const {
3285 return new(getNumOperands()) CallInst(*this);
3288 SelectInst *SelectInst::clone_impl() const {
3289 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
3292 VAArgInst *VAArgInst::clone_impl() const {
3293 return new VAArgInst(getOperand(0), getType());
3296 ExtractElementInst *ExtractElementInst::clone_impl() const {
3297 return ExtractElementInst::Create(getOperand(0), getOperand(1));
3300 InsertElementInst *InsertElementInst::clone_impl() const {
3301 return InsertElementInst::Create(getOperand(0),
3306 ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3307 return new ShuffleVectorInst(getOperand(0),
3312 PHINode *PHINode::clone_impl() const {
3313 return new PHINode(*this);
3316 ReturnInst *ReturnInst::clone_impl() const {
3317 return new(getNumOperands()) ReturnInst(*this);
3320 BranchInst *BranchInst::clone_impl() const {
3321 unsigned Ops(getNumOperands());
3322 return new(Ops, Ops == 1) BranchInst(*this);
3325 SwitchInst *SwitchInst::clone_impl() const {
3326 return new SwitchInst(*this);
3329 IndirectBrInst *IndirectBrInst::clone_impl() const {
3330 return new IndirectBrInst(*this);
3334 InvokeInst *InvokeInst::clone_impl() const {
3335 return new(getNumOperands()) InvokeInst(*this);
3338 UnwindInst *UnwindInst::clone_impl() const {
3339 LLVMContext &Context = getContext();
3340 return new UnwindInst(Context);
3343 UnreachableInst *UnreachableInst::clone_impl() const {
3344 LLVMContext &Context = getContext();
3345 return new UnreachableInst(Context);