1 //===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements all of the non-inline methods for the LLVM instruction
13 //===----------------------------------------------------------------------===//
15 #include "llvm/BasicBlock.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Support/CallSite.h"
21 #include "llvm/Support/ConstantRange.h"
24 unsigned CallSite::getCallingConv() const {
25 if (CallInst *CI = dyn_cast<CallInst>(I))
26 return CI->getCallingConv();
28 return cast<InvokeInst>(I)->getCallingConv();
30 void CallSite::setCallingConv(unsigned CC) {
31 if (CallInst *CI = dyn_cast<CallInst>(I))
32 CI->setCallingConv(CC);
34 cast<InvokeInst>(I)->setCallingConv(CC);
40 //===----------------------------------------------------------------------===//
41 // TerminatorInst Class
42 //===----------------------------------------------------------------------===//
44 // Out of line virtual method, so the vtable, etc has a home.
45 TerminatorInst::~TerminatorInst() {
48 // Out of line virtual method, so the vtable, etc has a home.
49 UnaryInstruction::~UnaryInstruction() {
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 PHINode::PHINode(const PHINode &PN)
58 : Instruction(PN.getType(), Instruction::PHI,
59 new Use[PN.getNumOperands()], PN.getNumOperands()),
60 ReservedSpace(PN.getNumOperands()) {
61 Use *OL = OperandList;
62 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
63 OL[i].init(PN.getOperand(i), this);
64 OL[i+1].init(PN.getOperand(i+1), this);
69 delete [] OperandList;
72 // removeIncomingValue - Remove an incoming value. This is useful if a
73 // predecessor basic block is deleted.
74 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
75 unsigned NumOps = getNumOperands();
76 Use *OL = OperandList;
77 assert(Idx*2 < NumOps && "BB not in PHI node!");
78 Value *Removed = OL[Idx*2];
80 // Move everything after this operand down.
82 // FIXME: we could just swap with the end of the list, then erase. However,
83 // client might not expect this to happen. The code as it is thrashes the
84 // use/def lists, which is kinda lame.
85 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
90 // Nuke the last value.
92 OL[NumOps-2+1].set(0);
93 NumOperands = NumOps-2;
95 // If the PHI node is dead, because it has zero entries, nuke it now.
96 if (NumOps == 2 && DeletePHIIfEmpty) {
97 // If anyone is using this PHI, make them use a dummy value instead...
98 replaceAllUsesWith(UndefValue::get(getType()));
104 /// resizeOperands - resize operands - This adjusts the length of the operands
105 /// list according to the following behavior:
106 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
107 /// of operation. This grows the number of ops by 1.5 times.
108 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
109 /// 3. If NumOps == NumOperands, trim the reserved space.
111 void PHINode::resizeOperands(unsigned NumOps) {
113 NumOps = (getNumOperands())*3/2;
114 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
115 } else if (NumOps*2 > NumOperands) {
117 if (ReservedSpace >= NumOps) return;
118 } else if (NumOps == NumOperands) {
119 if (ReservedSpace == NumOps) return;
124 ReservedSpace = NumOps;
125 Use *NewOps = new Use[NumOps];
126 Use *OldOps = OperandList;
127 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
128 NewOps[i].init(OldOps[i], this);
132 OperandList = NewOps;
135 /// hasConstantValue - If the specified PHI node always merges together the same
136 /// value, return the value, otherwise return null.
138 Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
139 // If the PHI node only has one incoming value, eliminate the PHI node...
140 if (getNumIncomingValues() == 1)
141 if (getIncomingValue(0) != this) // not X = phi X
142 return getIncomingValue(0);
144 return UndefValue::get(getType()); // Self cycle is dead.
146 // Otherwise if all of the incoming values are the same for the PHI, replace
147 // the PHI node with the incoming value.
150 bool HasUndefInput = false;
151 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
152 if (isa<UndefValue>(getIncomingValue(i)))
153 HasUndefInput = true;
154 else if (getIncomingValue(i) != this) // Not the PHI node itself...
155 if (InVal && getIncomingValue(i) != InVal)
156 return 0; // Not the same, bail out.
158 InVal = getIncomingValue(i);
160 // The only case that could cause InVal to be null is if we have a PHI node
161 // that only has entries for itself. In this case, there is no entry into the
162 // loop, so kill the PHI.
164 if (InVal == 0) InVal = UndefValue::get(getType());
166 // If we have a PHI node like phi(X, undef, X), where X is defined by some
167 // instruction, we cannot always return X as the result of the PHI node. Only
168 // do this if X is not an instruction (thus it must dominate the PHI block),
169 // or if the client is prepared to deal with this possibility.
170 if (HasUndefInput && !AllowNonDominatingInstruction)
171 if (Instruction *IV = dyn_cast<Instruction>(InVal))
172 // If it's in the entry block, it dominates everything.
173 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
175 return 0; // Cannot guarantee that InVal dominates this PHINode.
177 // All of the incoming values are the same, return the value now.
182 //===----------------------------------------------------------------------===//
183 // CallInst Implementation
184 //===----------------------------------------------------------------------===//
186 CallInst::~CallInst() {
187 delete [] OperandList;
190 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
191 NumOperands = NumParams+1;
192 Use *OL = OperandList = new Use[NumParams+1];
193 OL[0].init(Func, this);
195 const FunctionType *FTy =
196 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
197 FTy = FTy; // silence warning.
199 assert((NumParams == FTy->getNumParams() ||
200 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
201 "Calling a function with bad signature!");
202 for (unsigned i = 0; i != NumParams; ++i) {
203 assert((i >= FTy->getNumParams() ||
204 FTy->getParamType(i) == Params[i]->getType()) &&
205 "Calling a function with a bad signature!");
206 OL[i+1].init(Params[i], this);
210 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
212 Use *OL = OperandList = new Use[3];
213 OL[0].init(Func, this);
214 OL[1].init(Actual1, this);
215 OL[2].init(Actual2, this);
217 const FunctionType *FTy =
218 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
219 FTy = FTy; // silence warning.
221 assert((FTy->getNumParams() == 2 ||
222 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
223 "Calling a function with bad signature");
224 assert((0 >= FTy->getNumParams() ||
225 FTy->getParamType(0) == Actual1->getType()) &&
226 "Calling a function with a bad signature!");
227 assert((1 >= FTy->getNumParams() ||
228 FTy->getParamType(1) == Actual2->getType()) &&
229 "Calling a function with a bad signature!");
232 void CallInst::init(Value *Func, Value *Actual) {
234 Use *OL = OperandList = new Use[2];
235 OL[0].init(Func, this);
236 OL[1].init(Actual, this);
238 const FunctionType *FTy =
239 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
240 FTy = FTy; // silence warning.
242 assert((FTy->getNumParams() == 1 ||
243 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
244 "Calling a function with bad signature");
245 assert((0 == FTy->getNumParams() ||
246 FTy->getParamType(0) == Actual->getType()) &&
247 "Calling a function with a bad signature!");
250 void CallInst::init(Value *Func) {
252 Use *OL = OperandList = new Use[1];
253 OL[0].init(Func, this);
255 const FunctionType *FTy =
256 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
257 FTy = FTy; // silence warning.
259 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
262 CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
263 const std::string &Name, BasicBlock *InsertAtEnd)
264 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
265 ->getElementType())->getReturnType(),
266 Instruction::Call, 0, 0, InsertAtEnd) {
267 init(Func, Args, NumArgs);
270 CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
271 const std::string &Name, Instruction *InsertBefore)
272 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
273 ->getElementType())->getReturnType(),
274 Instruction::Call, 0, 0, InsertBefore) {
275 init(Func, Args, NumArgs);
279 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
280 const std::string &Name, Instruction *InsertBefore)
281 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
282 ->getElementType())->getReturnType(),
283 Instruction::Call, 0, 0, InsertBefore) {
284 init(Func, Actual1, Actual2);
288 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
289 const std::string &Name, BasicBlock *InsertAtEnd)
290 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
291 ->getElementType())->getReturnType(),
292 Instruction::Call, 0, 0, InsertAtEnd) {
293 init(Func, Actual1, Actual2);
297 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
298 Instruction *InsertBefore)
299 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
300 ->getElementType())->getReturnType(),
301 Instruction::Call, 0, 0, InsertBefore) {
306 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
307 BasicBlock *InsertAtEnd)
308 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
309 ->getElementType())->getReturnType(),
310 Instruction::Call, 0, 0, InsertAtEnd) {
315 CallInst::CallInst(Value *Func, const std::string &Name,
316 Instruction *InsertBefore)
317 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
318 ->getElementType())->getReturnType(),
319 Instruction::Call, 0, 0, InsertBefore) {
324 CallInst::CallInst(Value *Func, const std::string &Name,
325 BasicBlock *InsertAtEnd)
326 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
327 ->getElementType())->getReturnType(),
328 Instruction::Call, 0, 0, InsertAtEnd) {
333 CallInst::CallInst(const CallInst &CI)
334 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
335 CI.getNumOperands()) {
336 SubclassData = CI.SubclassData;
337 Use *OL = OperandList;
338 Use *InOL = CI.OperandList;
339 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
340 OL[i].init(InOL[i], this);
344 //===----------------------------------------------------------------------===//
345 // InvokeInst Implementation
346 //===----------------------------------------------------------------------===//
348 InvokeInst::~InvokeInst() {
349 delete [] OperandList;
352 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
353 Value* const *Args, unsigned NumArgs) {
354 NumOperands = 3+NumArgs;
355 Use *OL = OperandList = new Use[3+NumArgs];
356 OL[0].init(Fn, this);
357 OL[1].init(IfNormal, this);
358 OL[2].init(IfException, this);
359 const FunctionType *FTy =
360 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
361 FTy = FTy; // silence warning.
363 assert((NumArgs == FTy->getNumParams()) ||
364 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
365 "Calling a function with bad signature");
367 for (unsigned i = 0, e = NumArgs; i != e; i++) {
368 assert((i >= FTy->getNumParams() ||
369 FTy->getParamType(i) == Args[i]->getType()) &&
370 "Invoking a function with a bad signature!");
372 OL[i+3].init(Args[i], this);
376 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
377 BasicBlock *IfException,
378 Value* const *Args, unsigned NumArgs,
379 const std::string &Name, Instruction *InsertBefore)
380 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
381 ->getElementType())->getReturnType(),
382 Instruction::Invoke, 0, 0, InsertBefore) {
383 init(Fn, IfNormal, IfException, Args, NumArgs);
387 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
388 BasicBlock *IfException,
389 Value* const *Args, unsigned NumArgs,
390 const std::string &Name, BasicBlock *InsertAtEnd)
391 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
392 ->getElementType())->getReturnType(),
393 Instruction::Invoke, 0, 0, InsertAtEnd) {
394 init(Fn, IfNormal, IfException, Args, NumArgs);
398 InvokeInst::InvokeInst(const InvokeInst &II)
399 : TerminatorInst(II.getType(), Instruction::Invoke,
400 new Use[II.getNumOperands()], II.getNumOperands()) {
401 SubclassData = II.SubclassData;
402 Use *OL = OperandList, *InOL = II.OperandList;
403 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
404 OL[i].init(InOL[i], this);
407 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
408 return getSuccessor(idx);
410 unsigned InvokeInst::getNumSuccessorsV() const {
411 return getNumSuccessors();
413 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
414 return setSuccessor(idx, B);
418 //===----------------------------------------------------------------------===//
419 // ReturnInst Implementation
420 //===----------------------------------------------------------------------===//
422 ReturnInst::ReturnInst(const ReturnInst &RI)
423 : TerminatorInst(Type::VoidTy, Instruction::Ret,
424 &RetVal, RI.getNumOperands()) {
425 if (RI.getNumOperands())
426 RetVal.init(RI.RetVal, this);
429 ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
430 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
433 ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
434 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
437 ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
438 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
443 void ReturnInst::init(Value *retVal) {
444 if (retVal && retVal->getType() != Type::VoidTy) {
445 assert(!isa<BasicBlock>(retVal) &&
446 "Cannot return basic block. Probably using the incorrect ctor");
448 RetVal.init(retVal, this);
452 unsigned ReturnInst::getNumSuccessorsV() const {
453 return getNumSuccessors();
456 // Out-of-line ReturnInst method, put here so the C++ compiler can choose to
457 // emit the vtable for the class in this translation unit.
458 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
459 assert(0 && "ReturnInst has no successors!");
462 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
463 assert(0 && "ReturnInst has no successors!");
469 //===----------------------------------------------------------------------===//
470 // UnwindInst Implementation
471 //===----------------------------------------------------------------------===//
473 UnwindInst::UnwindInst(Instruction *InsertBefore)
474 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
476 UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
477 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
481 unsigned UnwindInst::getNumSuccessorsV() const {
482 return getNumSuccessors();
485 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
486 assert(0 && "UnwindInst has no successors!");
489 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
490 assert(0 && "UnwindInst has no successors!");
495 //===----------------------------------------------------------------------===//
496 // UnreachableInst Implementation
497 //===----------------------------------------------------------------------===//
499 UnreachableInst::UnreachableInst(Instruction *InsertBefore)
500 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
502 UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
503 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
506 unsigned UnreachableInst::getNumSuccessorsV() const {
507 return getNumSuccessors();
510 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
511 assert(0 && "UnwindInst has no successors!");
514 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
515 assert(0 && "UnwindInst has no successors!");
520 //===----------------------------------------------------------------------===//
521 // BranchInst Implementation
522 //===----------------------------------------------------------------------===//
524 void BranchInst::AssertOK() {
526 assert(getCondition()->getType() == Type::Int1Ty &&
527 "May only branch on boolean predicates!");
530 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
531 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
532 assert(IfTrue != 0 && "Branch destination may not be null!");
533 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
535 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
536 Instruction *InsertBefore)
537 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
538 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
539 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
540 Ops[2].init(Cond, this);
546 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
547 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
548 assert(IfTrue != 0 && "Branch destination may not be null!");
549 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
552 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
553 BasicBlock *InsertAtEnd)
554 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
555 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
556 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
557 Ops[2].init(Cond, this);
564 BranchInst::BranchInst(const BranchInst &BI) :
565 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
566 OperandList[0].init(BI.getOperand(0), this);
567 if (BI.getNumOperands() != 1) {
568 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
569 OperandList[1].init(BI.getOperand(1), this);
570 OperandList[2].init(BI.getOperand(2), this);
574 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
575 return getSuccessor(idx);
577 unsigned BranchInst::getNumSuccessorsV() const {
578 return getNumSuccessors();
580 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
581 setSuccessor(idx, B);
585 //===----------------------------------------------------------------------===//
586 // AllocationInst Implementation
587 //===----------------------------------------------------------------------===//
589 static Value *getAISize(Value *Amt) {
591 Amt = ConstantInt::get(Type::Int32Ty, 1);
593 assert(!isa<BasicBlock>(Amt) &&
594 "Passed basic block into allocation size parameter! Ue other ctor");
595 assert(Amt->getType() == Type::Int32Ty &&
596 "Malloc/Allocation array size is not a 32-bit integer!");
601 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
602 unsigned Align, const std::string &Name,
603 Instruction *InsertBefore)
604 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
605 InsertBefore), Alignment(Align) {
606 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
607 assert(Ty != Type::VoidTy && "Cannot allocate void!");
611 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
612 unsigned Align, const std::string &Name,
613 BasicBlock *InsertAtEnd)
614 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
615 InsertAtEnd), Alignment(Align) {
616 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
617 assert(Ty != Type::VoidTy && "Cannot allocate void!");
621 // Out of line virtual method, so the vtable, etc has a home.
622 AllocationInst::~AllocationInst() {
625 bool AllocationInst::isArrayAllocation() const {
626 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
627 return CI->getZExtValue() != 1;
631 const Type *AllocationInst::getAllocatedType() const {
632 return getType()->getElementType();
635 AllocaInst::AllocaInst(const AllocaInst &AI)
636 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
637 Instruction::Alloca, AI.getAlignment()) {
640 MallocInst::MallocInst(const MallocInst &MI)
641 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
642 Instruction::Malloc, MI.getAlignment()) {
645 //===----------------------------------------------------------------------===//
646 // FreeInst Implementation
647 //===----------------------------------------------------------------------===//
649 void FreeInst::AssertOK() {
650 assert(isa<PointerType>(getOperand(0)->getType()) &&
651 "Can not free something of nonpointer type!");
654 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
655 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
659 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
660 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
665 //===----------------------------------------------------------------------===//
666 // LoadInst Implementation
667 //===----------------------------------------------------------------------===//
669 void LoadInst::AssertOK() {
670 assert(isa<PointerType>(getOperand(0)->getType()) &&
671 "Ptr must have pointer type.");
674 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
675 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
676 Load, Ptr, InsertBef) {
682 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
683 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
684 Load, Ptr, InsertAE) {
690 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
691 Instruction *InsertBef)
692 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
693 Load, Ptr, InsertBef) {
694 setVolatile(isVolatile);
699 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
700 BasicBlock *InsertAE)
701 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
702 Load, Ptr, InsertAE) {
703 setVolatile(isVolatile);
710 LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
711 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
712 Load, Ptr, InsertBef) {
715 if (Name && Name[0]) setName(Name);
718 LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
719 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
720 Load, Ptr, InsertAE) {
723 if (Name && Name[0]) setName(Name);
726 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
727 Instruction *InsertBef)
728 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
729 Load, Ptr, InsertBef) {
730 setVolatile(isVolatile);
732 if (Name && Name[0]) setName(Name);
735 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
736 BasicBlock *InsertAE)
737 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
738 Load, Ptr, InsertAE) {
739 setVolatile(isVolatile);
741 if (Name && Name[0]) setName(Name);
745 //===----------------------------------------------------------------------===//
746 // StoreInst Implementation
747 //===----------------------------------------------------------------------===//
749 void StoreInst::AssertOK() {
750 assert(isa<PointerType>(getOperand(1)->getType()) &&
751 "Ptr must have pointer type!");
752 assert(getOperand(0)->getType() ==
753 cast<PointerType>(getOperand(1)->getType())->getElementType()
754 && "Ptr must be a pointer to Val type!");
758 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
759 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
760 Ops[0].init(val, this);
761 Ops[1].init(addr, this);
766 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
767 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
768 Ops[0].init(val, this);
769 Ops[1].init(addr, this);
774 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
775 Instruction *InsertBefore)
776 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
777 Ops[0].init(val, this);
778 Ops[1].init(addr, this);
779 setVolatile(isVolatile);
783 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
784 BasicBlock *InsertAtEnd)
785 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
786 Ops[0].init(val, this);
787 Ops[1].init(addr, this);
788 setVolatile(isVolatile);
792 //===----------------------------------------------------------------------===//
793 // GetElementPtrInst Implementation
794 //===----------------------------------------------------------------------===//
796 // checkType - Simple wrapper function to give a better assertion failure
797 // message on bad indexes for a gep instruction.
799 static inline const Type *checkType(const Type *Ty) {
800 assert(Ty && "Invalid GetElementPtrInst indices for type!");
804 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
805 NumOperands = 1+NumIdx;
806 Use *OL = OperandList = new Use[NumOperands];
807 OL[0].init(Ptr, this);
809 for (unsigned i = 0; i != NumIdx; ++i)
810 OL[i+1].init(Idx[i], this);
813 void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
815 Use *OL = OperandList = new Use[3];
816 OL[0].init(Ptr, this);
817 OL[1].init(Idx0, this);
818 OL[2].init(Idx1, this);
821 void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
823 Use *OL = OperandList = new Use[2];
824 OL[0].init(Ptr, this);
825 OL[1].init(Idx, this);
829 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
831 const std::string &Name, Instruction *InBe)
832 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
833 Idx, NumIdx, true))),
834 GetElementPtr, 0, 0, InBe) {
835 init(Ptr, Idx, NumIdx);
839 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
841 const std::string &Name, BasicBlock *IAE)
842 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
843 Idx, NumIdx, true))),
844 GetElementPtr, 0, 0, IAE) {
845 init(Ptr, Idx, NumIdx);
849 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
850 const std::string &Name, Instruction *InBe)
851 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
852 GetElementPtr, 0, 0, InBe) {
857 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
858 const std::string &Name, BasicBlock *IAE)
859 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
860 GetElementPtr, 0, 0, IAE) {
865 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
866 const std::string &Name, Instruction *InBe)
867 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
869 GetElementPtr, 0, 0, InBe) {
870 init(Ptr, Idx0, Idx1);
874 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
875 const std::string &Name, BasicBlock *IAE)
876 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
878 GetElementPtr, 0, 0, IAE) {
879 init(Ptr, Idx0, Idx1);
883 GetElementPtrInst::~GetElementPtrInst() {
884 delete[] OperandList;
887 // getIndexedType - Returns the type of the element that would be loaded with
888 // a load instruction with the specified parameters.
890 // A null type is returned if the indices are invalid for the specified
893 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
896 bool AllowCompositeLeaf) {
897 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
899 // Handle the special case of the empty set index set...
901 if (AllowCompositeLeaf ||
902 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
903 return cast<PointerType>(Ptr)->getElementType();
908 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
909 if (NumIdx == CurIdx) {
910 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
911 return 0; // Can't load a whole structure or array!?!?
914 Value *Index = Idxs[CurIdx++];
915 if (isa<PointerType>(CT) && CurIdx != 1)
916 return 0; // Can only index into pointer types at the first index!
917 if (!CT->indexValid(Index)) return 0;
918 Ptr = CT->getTypeAtIndex(Index);
920 // If the new type forwards to another type, then it is in the middle
921 // of being refined to another type (and hence, may have dropped all
922 // references to what it was using before). So, use the new forwarded
924 if (const Type * Ty = Ptr->getForwardedType()) {
928 return CurIdx == NumIdx ? Ptr : 0;
931 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
932 Value *Idx0, Value *Idx1,
933 bool AllowCompositeLeaf) {
934 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
935 if (!PTy) return 0; // Type isn't a pointer type!
937 // Check the pointer index.
938 if (!PTy->indexValid(Idx0)) return 0;
940 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
941 if (!CT || !CT->indexValid(Idx1)) return 0;
943 const Type *ElTy = CT->getTypeAtIndex(Idx1);
944 if (AllowCompositeLeaf || ElTy->isFirstClassType())
949 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
950 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
951 if (!PTy) return 0; // Type isn't a pointer type!
953 // Check the pointer index.
954 if (!PTy->indexValid(Idx)) return 0;
956 return PTy->getElementType();
959 //===----------------------------------------------------------------------===//
960 // ExtractElementInst Implementation
961 //===----------------------------------------------------------------------===//
963 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
964 const std::string &Name,
965 Instruction *InsertBef)
966 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
967 ExtractElement, Ops, 2, InsertBef) {
968 assert(isValidOperands(Val, Index) &&
969 "Invalid extractelement instruction operands!");
970 Ops[0].init(Val, this);
971 Ops[1].init(Index, this);
975 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
976 const std::string &Name,
977 Instruction *InsertBef)
978 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
979 ExtractElement, Ops, 2, InsertBef) {
980 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
981 assert(isValidOperands(Val, Index) &&
982 "Invalid extractelement instruction operands!");
983 Ops[0].init(Val, this);
984 Ops[1].init(Index, this);
989 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
990 const std::string &Name,
991 BasicBlock *InsertAE)
992 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
993 ExtractElement, Ops, 2, InsertAE) {
994 assert(isValidOperands(Val, Index) &&
995 "Invalid extractelement instruction operands!");
997 Ops[0].init(Val, this);
998 Ops[1].init(Index, this);
1002 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1003 const std::string &Name,
1004 BasicBlock *InsertAE)
1005 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1006 ExtractElement, Ops, 2, InsertAE) {
1007 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1008 assert(isValidOperands(Val, Index) &&
1009 "Invalid extractelement instruction operands!");
1011 Ops[0].init(Val, this);
1012 Ops[1].init(Index, this);
1017 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1018 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
1024 //===----------------------------------------------------------------------===//
1025 // InsertElementInst Implementation
1026 //===----------------------------------------------------------------------===//
1028 InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1029 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1030 Ops[0].init(IE.Ops[0], this);
1031 Ops[1].init(IE.Ops[1], this);
1032 Ops[2].init(IE.Ops[2], this);
1034 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1035 const std::string &Name,
1036 Instruction *InsertBef)
1037 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
1038 assert(isValidOperands(Vec, Elt, Index) &&
1039 "Invalid insertelement instruction operands!");
1040 Ops[0].init(Vec, this);
1041 Ops[1].init(Elt, this);
1042 Ops[2].init(Index, this);
1046 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1047 const std::string &Name,
1048 Instruction *InsertBef)
1049 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
1050 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1051 assert(isValidOperands(Vec, Elt, Index) &&
1052 "Invalid insertelement instruction operands!");
1053 Ops[0].init(Vec, this);
1054 Ops[1].init(Elt, this);
1055 Ops[2].init(Index, this);
1060 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1061 const std::string &Name,
1062 BasicBlock *InsertAE)
1063 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
1064 assert(isValidOperands(Vec, Elt, Index) &&
1065 "Invalid insertelement instruction operands!");
1067 Ops[0].init(Vec, this);
1068 Ops[1].init(Elt, this);
1069 Ops[2].init(Index, this);
1073 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1074 const std::string &Name,
1075 BasicBlock *InsertAE)
1076 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
1077 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1078 assert(isValidOperands(Vec, Elt, Index) &&
1079 "Invalid insertelement instruction operands!");
1081 Ops[0].init(Vec, this);
1082 Ops[1].init(Elt, this);
1083 Ops[2].init(Index, this);
1087 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1088 const Value *Index) {
1089 if (!isa<VectorType>(Vec->getType()))
1090 return false; // First operand of insertelement must be vector type.
1092 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1093 return false;// Second operand of insertelement must be packed element type.
1095 if (Index->getType() != Type::Int32Ty)
1096 return false; // Third operand of insertelement must be uint.
1101 //===----------------------------------------------------------------------===//
1102 // ShuffleVectorInst Implementation
1103 //===----------------------------------------------------------------------===//
1105 ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1106 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1107 Ops[0].init(SV.Ops[0], this);
1108 Ops[1].init(SV.Ops[1], this);
1109 Ops[2].init(SV.Ops[2], this);
1112 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1113 const std::string &Name,
1114 Instruction *InsertBefore)
1115 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
1116 assert(isValidOperands(V1, V2, Mask) &&
1117 "Invalid shuffle vector instruction operands!");
1118 Ops[0].init(V1, this);
1119 Ops[1].init(V2, this);
1120 Ops[2].init(Mask, this);
1124 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1125 const std::string &Name,
1126 BasicBlock *InsertAtEnd)
1127 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
1128 assert(isValidOperands(V1, V2, Mask) &&
1129 "Invalid shuffle vector instruction operands!");
1131 Ops[0].init(V1, this);
1132 Ops[1].init(V2, this);
1133 Ops[2].init(Mask, this);
1137 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1138 const Value *Mask) {
1139 if (!isa<VectorType>(V1->getType())) return false;
1140 if (V1->getType() != V2->getType()) return false;
1141 if (!isa<VectorType>(Mask->getType()) ||
1142 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1143 cast<VectorType>(Mask->getType())->getNumElements() !=
1144 cast<VectorType>(V1->getType())->getNumElements())
1150 //===----------------------------------------------------------------------===//
1151 // BinaryOperator Class
1152 //===----------------------------------------------------------------------===//
1154 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1155 const Type *Ty, const std::string &Name,
1156 Instruction *InsertBefore)
1157 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1158 Ops[0].init(S1, this);
1159 Ops[1].init(S2, this);
1164 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1165 const Type *Ty, const std::string &Name,
1166 BasicBlock *InsertAtEnd)
1167 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1168 Ops[0].init(S1, this);
1169 Ops[1].init(S2, this);
1175 void BinaryOperator::init(BinaryOps iType) {
1176 Value *LHS = getOperand(0), *RHS = getOperand(1);
1177 LHS = LHS; RHS = RHS; // Silence warnings.
1178 assert(LHS->getType() == RHS->getType() &&
1179 "Binary operator operand types must match!");
1184 assert(getType() == LHS->getType() &&
1185 "Arithmetic operation should return same type as operands!");
1186 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
1187 isa<VectorType>(getType())) &&
1188 "Tried to create an arithmetic operation on a non-arithmetic type!");
1192 assert(getType() == LHS->getType() &&
1193 "Arithmetic operation should return same type as operands!");
1194 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1195 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1196 "Incorrect operand type (not integer) for S/UDIV");
1199 assert(getType() == LHS->getType() &&
1200 "Arithmetic operation should return same type as operands!");
1201 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1202 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
1203 && "Incorrect operand type (not floating point) for FDIV");
1207 assert(getType() == LHS->getType() &&
1208 "Arithmetic operation should return same type as operands!");
1209 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1210 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1211 "Incorrect operand type (not integer) for S/UREM");
1214 assert(getType() == LHS->getType() &&
1215 "Arithmetic operation should return same type as operands!");
1216 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1217 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
1218 && "Incorrect operand type (not floating point) for FREM");
1223 assert(getType() == LHS->getType() &&
1224 "Shift operation should return same type as operands!");
1225 assert(getType()->isInteger() &&
1226 "Shift operation requires integer operands");
1230 assert(getType() == LHS->getType() &&
1231 "Logical operation should return same type as operands!");
1232 assert((getType()->isInteger() ||
1233 (isa<VectorType>(getType()) &&
1234 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1235 "Tried to create a logical operation on a non-integral type!");
1243 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
1244 const std::string &Name,
1245 Instruction *InsertBefore) {
1246 assert(S1->getType() == S2->getType() &&
1247 "Cannot create binary operator with two operands of differing type!");
1248 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1251 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
1252 const std::string &Name,
1253 BasicBlock *InsertAtEnd) {
1254 BinaryOperator *Res = create(Op, S1, S2, Name);
1255 InsertAtEnd->getInstList().push_back(Res);
1259 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1260 Instruction *InsertBefore) {
1261 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1262 return new BinaryOperator(Instruction::Sub,
1264 Op->getType(), Name, InsertBefore);
1267 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1268 BasicBlock *InsertAtEnd) {
1269 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1270 return new BinaryOperator(Instruction::Sub,
1272 Op->getType(), Name, InsertAtEnd);
1275 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1276 Instruction *InsertBefore) {
1278 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1279 C = ConstantInt::getAllOnesValue(PTy->getElementType());
1280 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
1282 C = ConstantInt::getAllOnesValue(Op->getType());
1285 return new BinaryOperator(Instruction::Xor, Op, C,
1286 Op->getType(), Name, InsertBefore);
1289 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1290 BasicBlock *InsertAtEnd) {
1292 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1293 // Create a vector of all ones values.
1294 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
1296 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1298 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
1301 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1302 Op->getType(), Name, InsertAtEnd);
1306 // isConstantAllOnes - Helper function for several functions below
1307 static inline bool isConstantAllOnes(const Value *V) {
1308 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
1311 bool BinaryOperator::isNeg(const Value *V) {
1312 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1313 if (Bop->getOpcode() == Instruction::Sub)
1314 return Bop->getOperand(0) ==
1315 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
1319 bool BinaryOperator::isNot(const Value *V) {
1320 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1321 return (Bop->getOpcode() == Instruction::Xor &&
1322 (isConstantAllOnes(Bop->getOperand(1)) ||
1323 isConstantAllOnes(Bop->getOperand(0))));
1327 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1328 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1329 return cast<BinaryOperator>(BinOp)->getOperand(1);
1332 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1333 return getNegArgument(const_cast<Value*>(BinOp));
1336 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1337 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1338 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1339 Value *Op0 = BO->getOperand(0);
1340 Value *Op1 = BO->getOperand(1);
1341 if (isConstantAllOnes(Op0)) return Op1;
1343 assert(isConstantAllOnes(Op1));
1347 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1348 return getNotArgument(const_cast<Value*>(BinOp));
1352 // swapOperands - Exchange the two operands to this instruction. This
1353 // instruction is safe to use on any binary instruction and does not
1354 // modify the semantics of the instruction. If the instruction is
1355 // order dependent (SetLT f.e.) the opcode is changed.
1357 bool BinaryOperator::swapOperands() {
1358 if (!isCommutative())
1359 return true; // Can't commute operands
1360 std::swap(Ops[0], Ops[1]);
1364 //===----------------------------------------------------------------------===//
1366 //===----------------------------------------------------------------------===//
1368 // Just determine if this cast only deals with integral->integral conversion.
1369 bool CastInst::isIntegerCast() const {
1370 switch (getOpcode()) {
1371 default: return false;
1372 case Instruction::ZExt:
1373 case Instruction::SExt:
1374 case Instruction::Trunc:
1376 case Instruction::BitCast:
1377 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
1381 bool CastInst::isLosslessCast() const {
1382 // Only BitCast can be lossless, exit fast if we're not BitCast
1383 if (getOpcode() != Instruction::BitCast)
1386 // Identity cast is always lossless
1387 const Type* SrcTy = getOperand(0)->getType();
1388 const Type* DstTy = getType();
1392 // Pointer to pointer is always lossless.
1393 if (isa<PointerType>(SrcTy))
1394 return isa<PointerType>(DstTy);
1395 return false; // Other types have no identity values
1398 /// This function determines if the CastInst does not require any bits to be
1399 /// changed in order to effect the cast. Essentially, it identifies cases where
1400 /// no code gen is necessary for the cast, hence the name no-op cast. For
1401 /// example, the following are all no-op casts:
1402 /// # bitcast uint %X, int
1403 /// # bitcast uint* %x, sbyte*
1404 /// # bitcast packed< 2 x int > %x, packed< 4 x short>
1405 /// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1406 /// @brief Determine if a cast is a no-op.
1407 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1408 switch (getOpcode()) {
1410 assert(!"Invalid CastOp");
1411 case Instruction::Trunc:
1412 case Instruction::ZExt:
1413 case Instruction::SExt:
1414 case Instruction::FPTrunc:
1415 case Instruction::FPExt:
1416 case Instruction::UIToFP:
1417 case Instruction::SIToFP:
1418 case Instruction::FPToUI:
1419 case Instruction::FPToSI:
1420 return false; // These always modify bits
1421 case Instruction::BitCast:
1422 return true; // BitCast never modifies bits.
1423 case Instruction::PtrToInt:
1424 return IntPtrTy->getPrimitiveSizeInBits() ==
1425 getType()->getPrimitiveSizeInBits();
1426 case Instruction::IntToPtr:
1427 return IntPtrTy->getPrimitiveSizeInBits() ==
1428 getOperand(0)->getType()->getPrimitiveSizeInBits();
1432 /// This function determines if a pair of casts can be eliminated and what
1433 /// opcode should be used in the elimination. This assumes that there are two
1434 /// instructions like this:
1435 /// * %F = firstOpcode SrcTy %x to MidTy
1436 /// * %S = secondOpcode MidTy %F to DstTy
1437 /// The function returns a resultOpcode so these two casts can be replaced with:
1438 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1439 /// If no such cast is permited, the function returns 0.
1440 unsigned CastInst::isEliminableCastPair(
1441 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1442 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1444 // Define the 144 possibilities for these two cast instructions. The values
1445 // in this matrix determine what to do in a given situation and select the
1446 // case in the switch below. The rows correspond to firstOp, the columns
1447 // correspond to secondOp. In looking at the table below, keep in mind
1448 // the following cast properties:
1450 // Size Compare Source Destination
1451 // Operator Src ? Size Type Sign Type Sign
1452 // -------- ------------ ------------------- ---------------------
1453 // TRUNC > Integer Any Integral Any
1454 // ZEXT < Integral Unsigned Integer Any
1455 // SEXT < Integral Signed Integer Any
1456 // FPTOUI n/a FloatPt n/a Integral Unsigned
1457 // FPTOSI n/a FloatPt n/a Integral Signed
1458 // UITOFP n/a Integral Unsigned FloatPt n/a
1459 // SITOFP n/a Integral Signed FloatPt n/a
1460 // FPTRUNC > FloatPt n/a FloatPt n/a
1461 // FPEXT < FloatPt n/a FloatPt n/a
1462 // PTRTOINT n/a Pointer n/a Integral Unsigned
1463 // INTTOPTR n/a Integral Unsigned Pointer n/a
1464 // BITCONVERT = FirstClass n/a FirstClass n/a
1466 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1467 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1468 // into "fptoui double to ulong", but this loses information about the range
1469 // of the produced value (we no longer know the top-part is all zeros).
1470 // Further this conversion is often much more expensive for typical hardware,
1471 // and causes issues when building libgcc. We disallow fptosi+sext for the
1473 const unsigned numCastOps =
1474 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1475 static const uint8_t CastResults[numCastOps][numCastOps] = {
1476 // T F F U S F F P I B -+
1477 // R Z S P P I I T P 2 N T |
1478 // U E E 2 2 2 2 R E I T C +- secondOp
1479 // N X X U S F F N X N 2 V |
1480 // C T T I I P P C T T P T -+
1481 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1482 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1483 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
1484 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1485 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
1486 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1487 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1488 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1489 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1490 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1491 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1492 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1495 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1496 [secondOp-Instruction::CastOpsBegin];
1499 // categorically disallowed
1502 // allowed, use first cast's opcode
1505 // allowed, use second cast's opcode
1508 // no-op cast in second op implies firstOp as long as the DestTy
1510 if (DstTy->isInteger())
1514 // no-op cast in second op implies firstOp as long as the DestTy
1515 // is floating point
1516 if (DstTy->isFloatingPoint())
1520 // no-op cast in first op implies secondOp as long as the SrcTy
1522 if (SrcTy->isInteger())
1526 // no-op cast in first op implies secondOp as long as the SrcTy
1527 // is a floating point
1528 if (SrcTy->isFloatingPoint())
1532 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1533 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1534 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1535 if (MidSize >= PtrSize)
1536 return Instruction::BitCast;
1540 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1541 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1542 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1543 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1544 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1545 if (SrcSize == DstSize)
1546 return Instruction::BitCast;
1547 else if (SrcSize < DstSize)
1551 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1552 return Instruction::ZExt;
1554 // fpext followed by ftrunc is allowed if the bit size returned to is
1555 // the same as the original, in which case its just a bitcast
1557 return Instruction::BitCast;
1558 return 0; // If the types are not the same we can't eliminate it.
1560 // bitcast followed by ptrtoint is allowed as long as the bitcast
1561 // is a pointer to pointer cast.
1562 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1566 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1567 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1571 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1572 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1573 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1574 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1575 if (SrcSize <= PtrSize && SrcSize == DstSize)
1576 return Instruction::BitCast;
1580 // cast combination can't happen (error in input). This is for all cases
1581 // where the MidTy is not the same for the two cast instructions.
1582 assert(!"Invalid Cast Combination");
1585 assert(!"Error in CastResults table!!!");
1591 CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1592 const std::string &Name, Instruction *InsertBefore) {
1593 // Construct and return the appropriate CastInst subclass
1595 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1596 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1597 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1598 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1599 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1600 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1601 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1602 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1603 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1604 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1605 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1606 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1608 assert(!"Invalid opcode provided");
1613 CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1614 const std::string &Name, BasicBlock *InsertAtEnd) {
1615 // Construct and return the appropriate CastInst subclass
1617 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1618 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1619 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1620 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1621 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1622 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1623 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1624 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1625 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1626 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1627 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1628 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1630 assert(!"Invalid opcode provided");
1635 CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1636 const std::string &Name,
1637 Instruction *InsertBefore) {
1638 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1639 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1640 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1643 CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1644 const std::string &Name,
1645 BasicBlock *InsertAtEnd) {
1646 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1647 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1648 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1651 CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1652 const std::string &Name,
1653 Instruction *InsertBefore) {
1654 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1655 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1656 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1659 CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1660 const std::string &Name,
1661 BasicBlock *InsertAtEnd) {
1662 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1663 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1664 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1667 CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1668 const std::string &Name,
1669 Instruction *InsertBefore) {
1670 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1671 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1672 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1675 CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1676 const std::string &Name,
1677 BasicBlock *InsertAtEnd) {
1678 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1679 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1680 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1683 CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1684 const std::string &Name,
1685 BasicBlock *InsertAtEnd) {
1686 assert(isa<PointerType>(S->getType()) && "Invalid cast");
1687 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
1690 if (Ty->isInteger())
1691 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1692 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1695 /// @brief Create a BitCast or a PtrToInt cast instruction
1696 CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1697 const std::string &Name,
1698 Instruction *InsertBefore) {
1699 assert(isa<PointerType>(S->getType()) && "Invalid cast");
1700 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
1703 if (Ty->isInteger())
1704 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1705 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1708 CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1709 bool isSigned, const std::string &Name,
1710 Instruction *InsertBefore) {
1711 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
1712 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1713 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1714 Instruction::CastOps opcode =
1715 (SrcBits == DstBits ? Instruction::BitCast :
1716 (SrcBits > DstBits ? Instruction::Trunc :
1717 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1718 return create(opcode, C, Ty, Name, InsertBefore);
1721 CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1722 bool isSigned, const std::string &Name,
1723 BasicBlock *InsertAtEnd) {
1724 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
1725 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1726 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1727 Instruction::CastOps opcode =
1728 (SrcBits == DstBits ? Instruction::BitCast :
1729 (SrcBits > DstBits ? Instruction::Trunc :
1730 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1731 return create(opcode, C, Ty, Name, InsertAtEnd);
1734 CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1735 const std::string &Name,
1736 Instruction *InsertBefore) {
1737 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1739 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1740 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1741 Instruction::CastOps opcode =
1742 (SrcBits == DstBits ? Instruction::BitCast :
1743 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1744 return create(opcode, C, Ty, Name, InsertBefore);
1747 CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1748 const std::string &Name,
1749 BasicBlock *InsertAtEnd) {
1750 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1752 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1753 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1754 Instruction::CastOps opcode =
1755 (SrcBits == DstBits ? Instruction::BitCast :
1756 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1757 return create(opcode, C, Ty, Name, InsertAtEnd);
1760 // Provide a way to get a "cast" where the cast opcode is inferred from the
1761 // types and size of the operand. This, basically, is a parallel of the
1762 // logic in the castIsValid function below. This axiom should hold:
1763 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1764 // should not assert in castIsValid. In other words, this produces a "correct"
1765 // casting opcode for the arguments passed to it.
1766 Instruction::CastOps
1767 CastInst::getCastOpcode(
1768 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
1769 // Get the bit sizes, we'll need these
1770 const Type *SrcTy = Src->getType();
1771 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1772 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1774 // Run through the possibilities ...
1775 if (DestTy->isInteger()) { // Casting to integral
1776 if (SrcTy->isInteger()) { // Casting from integral
1777 if (DestBits < SrcBits)
1778 return Trunc; // int -> smaller int
1779 else if (DestBits > SrcBits) { // its an extension
1781 return SExt; // signed -> SEXT
1783 return ZExt; // unsigned -> ZEXT
1785 return BitCast; // Same size, No-op cast
1787 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1789 return FPToSI; // FP -> sint
1791 return FPToUI; // FP -> uint
1792 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1793 assert(DestBits == PTy->getBitWidth() &&
1794 "Casting packed to integer of different width");
1795 return BitCast; // Same size, no-op cast
1797 assert(isa<PointerType>(SrcTy) &&
1798 "Casting from a value that is not first-class type");
1799 return PtrToInt; // ptr -> int
1801 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1802 if (SrcTy->isInteger()) { // Casting from integral
1804 return SIToFP; // sint -> FP
1806 return UIToFP; // uint -> FP
1807 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1808 if (DestBits < SrcBits) {
1809 return FPTrunc; // FP -> smaller FP
1810 } else if (DestBits > SrcBits) {
1811 return FPExt; // FP -> larger FP
1813 return BitCast; // same size, no-op cast
1815 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1816 assert(DestBits == PTy->getBitWidth() &&
1817 "Casting packed to floating point of different width");
1818 return BitCast; // same size, no-op cast
1820 assert(0 && "Casting pointer or non-first class to float");
1822 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1823 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
1824 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1825 "Casting packed to packed of different widths");
1826 return BitCast; // packed -> packed
1827 } else if (DestPTy->getBitWidth() == SrcBits) {
1828 return BitCast; // float/int -> packed
1830 assert(!"Illegal cast to packed (wrong type or size)");
1832 } else if (isa<PointerType>(DestTy)) {
1833 if (isa<PointerType>(SrcTy)) {
1834 return BitCast; // ptr -> ptr
1835 } else if (SrcTy->isInteger()) {
1836 return IntToPtr; // int -> ptr
1838 assert(!"Casting pointer to other than pointer or int");
1841 assert(!"Casting to type that is not first-class");
1844 // If we fall through to here we probably hit an assertion cast above
1845 // and assertions are not turned on. Anything we return is an error, so
1846 // BitCast is as good a choice as any.
1850 //===----------------------------------------------------------------------===//
1851 // CastInst SubClass Constructors
1852 //===----------------------------------------------------------------------===//
1854 /// Check that the construction parameters for a CastInst are correct. This
1855 /// could be broken out into the separate constructors but it is useful to have
1856 /// it in one place and to eliminate the redundant code for getting the sizes
1857 /// of the types involved.
1859 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
1861 // Check for type sanity on the arguments
1862 const Type *SrcTy = S->getType();
1863 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1866 // Get the size of the types in bits, we'll need this later
1867 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1868 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1870 // Switch on the opcode provided
1872 default: return false; // This is an input error
1873 case Instruction::Trunc:
1874 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
1875 case Instruction::ZExt:
1876 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
1877 case Instruction::SExt:
1878 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
1879 case Instruction::FPTrunc:
1880 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1881 SrcBitSize > DstBitSize;
1882 case Instruction::FPExt:
1883 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1884 SrcBitSize < DstBitSize;
1885 case Instruction::UIToFP:
1886 return SrcTy->isInteger() && DstTy->isFloatingPoint();
1887 case Instruction::SIToFP:
1888 return SrcTy->isInteger() && DstTy->isFloatingPoint();
1889 case Instruction::FPToUI:
1890 return SrcTy->isFloatingPoint() && DstTy->isInteger();
1891 case Instruction::FPToSI:
1892 return SrcTy->isFloatingPoint() && DstTy->isInteger();
1893 case Instruction::PtrToInt:
1894 return isa<PointerType>(SrcTy) && DstTy->isInteger();
1895 case Instruction::IntToPtr:
1896 return SrcTy->isInteger() && isa<PointerType>(DstTy);
1897 case Instruction::BitCast:
1898 // BitCast implies a no-op cast of type only. No bits change.
1899 // However, you can't cast pointers to anything but pointers.
1900 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1903 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1904 // these cases, the cast is okay if the source and destination bit widths
1906 return SrcBitSize == DstBitSize;
1910 TruncInst::TruncInst(
1911 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1912 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
1913 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
1916 TruncInst::TruncInst(
1917 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1918 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
1919 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
1923 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1924 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
1925 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
1929 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1930 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
1931 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
1934 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1935 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
1936 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
1940 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1941 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
1942 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
1945 FPTruncInst::FPTruncInst(
1946 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1947 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
1948 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
1951 FPTruncInst::FPTruncInst(
1952 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1953 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
1954 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
1957 FPExtInst::FPExtInst(
1958 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1959 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
1960 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
1963 FPExtInst::FPExtInst(
1964 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1965 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
1966 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
1969 UIToFPInst::UIToFPInst(
1970 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1971 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
1972 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
1975 UIToFPInst::UIToFPInst(
1976 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1977 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
1978 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
1981 SIToFPInst::SIToFPInst(
1982 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1983 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
1984 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
1987 SIToFPInst::SIToFPInst(
1988 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1989 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
1990 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
1993 FPToUIInst::FPToUIInst(
1994 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1995 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
1996 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
1999 FPToUIInst::FPToUIInst(
2000 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2001 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2002 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2005 FPToSIInst::FPToSIInst(
2006 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2007 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2008 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2011 FPToSIInst::FPToSIInst(
2012 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2013 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2014 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2017 PtrToIntInst::PtrToIntInst(
2018 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2019 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2020 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2023 PtrToIntInst::PtrToIntInst(
2024 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2025 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2026 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2029 IntToPtrInst::IntToPtrInst(
2030 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2031 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2032 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2035 IntToPtrInst::IntToPtrInst(
2036 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2037 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2038 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2041 BitCastInst::BitCastInst(
2042 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2043 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2044 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2047 BitCastInst::BitCastInst(
2048 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2049 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2050 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2053 //===----------------------------------------------------------------------===//
2055 //===----------------------------------------------------------------------===//
2057 CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2058 const std::string &Name, Instruction *InsertBefore)
2059 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
2060 Ops[0].init(LHS, this);
2061 Ops[1].init(RHS, this);
2062 SubclassData = predicate;
2063 if (op == Instruction::ICmp) {
2064 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2065 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2066 "Invalid ICmp predicate value");
2067 const Type* Op0Ty = getOperand(0)->getType();
2068 const Type* Op1Ty = getOperand(1)->getType();
2069 assert(Op0Ty == Op1Ty &&
2070 "Both operands to ICmp instruction are not of the same type!");
2071 // Check that the operands are the right type
2072 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
2073 "Invalid operand types for ICmp instruction");
2076 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2077 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2078 "Invalid FCmp predicate value");
2079 const Type* Op0Ty = getOperand(0)->getType();
2080 const Type* Op1Ty = getOperand(1)->getType();
2081 assert(Op0Ty == Op1Ty &&
2082 "Both operands to FCmp instruction are not of the same type!");
2083 // Check that the operands are the right type
2084 assert(Op0Ty->isFloatingPoint() &&
2085 "Invalid operand types for FCmp instruction");
2089 CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2090 const std::string &Name, BasicBlock *InsertAtEnd)
2091 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
2092 Ops[0].init(LHS, this);
2093 Ops[1].init(RHS, this);
2094 SubclassData = predicate;
2095 if (op == Instruction::ICmp) {
2096 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2097 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2098 "Invalid ICmp predicate value");
2100 const Type* Op0Ty = getOperand(0)->getType();
2101 const Type* Op1Ty = getOperand(1)->getType();
2102 assert(Op0Ty == Op1Ty &&
2103 "Both operands to ICmp instruction are not of the same type!");
2104 // Check that the operands are the right type
2105 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
2106 "Invalid operand types for ICmp instruction");
2109 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2110 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2111 "Invalid FCmp predicate value");
2112 const Type* Op0Ty = getOperand(0)->getType();
2113 const Type* Op1Ty = getOperand(1)->getType();
2114 assert(Op0Ty == Op1Ty &&
2115 "Both operands to FCmp instruction are not of the same type!");
2116 // Check that the operands are the right type
2117 assert(Op0Ty->isFloatingPoint() &&
2118 "Invalid operand types for FCmp instruction");
2123 CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2124 const std::string &Name, Instruction *InsertBefore) {
2125 if (Op == Instruction::ICmp) {
2126 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2129 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2134 CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2135 const std::string &Name, BasicBlock *InsertAtEnd) {
2136 if (Op == Instruction::ICmp) {
2137 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2140 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2144 void CmpInst::swapOperands() {
2145 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2148 cast<FCmpInst>(this)->swapOperands();
2151 bool CmpInst::isCommutative() {
2152 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2153 return IC->isCommutative();
2154 return cast<FCmpInst>(this)->isCommutative();
2157 bool CmpInst::isEquality() {
2158 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2159 return IC->isEquality();
2160 return cast<FCmpInst>(this)->isEquality();
2164 ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2167 assert(!"Unknown icmp predicate!");
2168 case ICMP_EQ: return ICMP_NE;
2169 case ICMP_NE: return ICMP_EQ;
2170 case ICMP_UGT: return ICMP_ULE;
2171 case ICMP_ULT: return ICMP_UGE;
2172 case ICMP_UGE: return ICMP_ULT;
2173 case ICMP_ULE: return ICMP_UGT;
2174 case ICMP_SGT: return ICMP_SLE;
2175 case ICMP_SLT: return ICMP_SGE;
2176 case ICMP_SGE: return ICMP_SLT;
2177 case ICMP_SLE: return ICMP_SGT;
2181 ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2183 default: assert(! "Unknown icmp predicate!");
2184 case ICMP_EQ: case ICMP_NE:
2186 case ICMP_SGT: return ICMP_SLT;
2187 case ICMP_SLT: return ICMP_SGT;
2188 case ICMP_SGE: return ICMP_SLE;
2189 case ICMP_SLE: return ICMP_SGE;
2190 case ICMP_UGT: return ICMP_ULT;
2191 case ICMP_ULT: return ICMP_UGT;
2192 case ICMP_UGE: return ICMP_ULE;
2193 case ICMP_ULE: return ICMP_UGE;
2197 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2199 default: assert(! "Unknown icmp predicate!");
2200 case ICMP_EQ: case ICMP_NE:
2201 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2203 case ICMP_UGT: return ICMP_SGT;
2204 case ICMP_ULT: return ICMP_SLT;
2205 case ICMP_UGE: return ICMP_SGE;
2206 case ICMP_ULE: return ICMP_SLE;
2210 bool ICmpInst::isSignedPredicate(Predicate pred) {
2212 default: assert(! "Unknown icmp predicate!");
2213 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2215 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2216 case ICMP_UGE: case ICMP_ULE:
2221 /// Initialize a set of values that all satisfy the condition with C.
2224 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2227 uint32_t BitWidth = C.getBitWidth();
2229 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2230 case ICmpInst::ICMP_EQ: Upper++; break;
2231 case ICmpInst::ICMP_NE: Lower++; break;
2232 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2233 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2234 case ICmpInst::ICMP_UGT:
2235 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2237 case ICmpInst::ICMP_SGT:
2238 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2240 case ICmpInst::ICMP_ULE:
2241 Lower = APInt::getMinValue(BitWidth); Upper++;
2243 case ICmpInst::ICMP_SLE:
2244 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2246 case ICmpInst::ICMP_UGE:
2247 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2249 case ICmpInst::ICMP_SGE:
2250 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2253 return ConstantRange(Lower, Upper);
2256 FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2259 assert(!"Unknown icmp predicate!");
2260 case FCMP_OEQ: return FCMP_UNE;
2261 case FCMP_ONE: return FCMP_UEQ;
2262 case FCMP_OGT: return FCMP_ULE;
2263 case FCMP_OLT: return FCMP_UGE;
2264 case FCMP_OGE: return FCMP_ULT;
2265 case FCMP_OLE: return FCMP_UGT;
2266 case FCMP_UEQ: return FCMP_ONE;
2267 case FCMP_UNE: return FCMP_OEQ;
2268 case FCMP_UGT: return FCMP_OLE;
2269 case FCMP_ULT: return FCMP_OGE;
2270 case FCMP_UGE: return FCMP_OLT;
2271 case FCMP_ULE: return FCMP_OGT;
2272 case FCMP_ORD: return FCMP_UNO;
2273 case FCMP_UNO: return FCMP_ORD;
2274 case FCMP_TRUE: return FCMP_FALSE;
2275 case FCMP_FALSE: return FCMP_TRUE;
2279 FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2281 default: assert(!"Unknown fcmp predicate!");
2282 case FCMP_FALSE: case FCMP_TRUE:
2283 case FCMP_OEQ: case FCMP_ONE:
2284 case FCMP_UEQ: case FCMP_UNE:
2285 case FCMP_ORD: case FCMP_UNO:
2287 case FCMP_OGT: return FCMP_OLT;
2288 case FCMP_OLT: return FCMP_OGT;
2289 case FCMP_OGE: return FCMP_OLE;
2290 case FCMP_OLE: return FCMP_OGE;
2291 case FCMP_UGT: return FCMP_ULT;
2292 case FCMP_ULT: return FCMP_UGT;
2293 case FCMP_UGE: return FCMP_ULE;
2294 case FCMP_ULE: return FCMP_UGE;
2298 bool CmpInst::isUnsigned(unsigned short predicate) {
2299 switch (predicate) {
2300 default: return false;
2301 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2302 case ICmpInst::ICMP_UGE: return true;
2306 bool CmpInst::isSigned(unsigned short predicate){
2307 switch (predicate) {
2308 default: return false;
2309 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2310 case ICmpInst::ICMP_SGE: return true;
2314 bool CmpInst::isOrdered(unsigned short predicate) {
2315 switch (predicate) {
2316 default: return false;
2317 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2318 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2319 case FCmpInst::FCMP_ORD: return true;
2323 bool CmpInst::isUnordered(unsigned short predicate) {
2324 switch (predicate) {
2325 default: return false;
2326 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2327 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2328 case FCmpInst::FCMP_UNO: return true;
2332 //===----------------------------------------------------------------------===//
2333 // SwitchInst Implementation
2334 //===----------------------------------------------------------------------===//
2336 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
2337 assert(Value && Default);
2338 ReservedSpace = 2+NumCases*2;
2340 OperandList = new Use[ReservedSpace];
2342 OperandList[0].init(Value, this);
2343 OperandList[1].init(Default, this);
2346 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2347 /// switch on and a default destination. The number of additional cases can
2348 /// be specified here to make memory allocation more efficient. This
2349 /// constructor can also autoinsert before another instruction.
2350 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2351 Instruction *InsertBefore)
2352 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2353 init(Value, Default, NumCases);
2356 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2357 /// switch on and a default destination. The number of additional cases can
2358 /// be specified here to make memory allocation more efficient. This
2359 /// constructor also autoinserts at the end of the specified BasicBlock.
2360 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2361 BasicBlock *InsertAtEnd)
2362 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2363 init(Value, Default, NumCases);
2366 SwitchInst::SwitchInst(const SwitchInst &SI)
2367 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2368 new Use[SI.getNumOperands()], SI.getNumOperands()) {
2369 Use *OL = OperandList, *InOL = SI.OperandList;
2370 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2371 OL[i].init(InOL[i], this);
2372 OL[i+1].init(InOL[i+1], this);
2376 SwitchInst::~SwitchInst() {
2377 delete [] OperandList;
2381 /// addCase - Add an entry to the switch instruction...
2383 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
2384 unsigned OpNo = NumOperands;
2385 if (OpNo+2 > ReservedSpace)
2386 resizeOperands(0); // Get more space!
2387 // Initialize some new operands.
2388 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
2389 NumOperands = OpNo+2;
2390 OperandList[OpNo].init(OnVal, this);
2391 OperandList[OpNo+1].init(Dest, this);
2394 /// removeCase - This method removes the specified successor from the switch
2395 /// instruction. Note that this cannot be used to remove the default
2396 /// destination (successor #0).
2398 void SwitchInst::removeCase(unsigned idx) {
2399 assert(idx != 0 && "Cannot remove the default case!");
2400 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2402 unsigned NumOps = getNumOperands();
2403 Use *OL = OperandList;
2405 // Move everything after this operand down.
2407 // FIXME: we could just swap with the end of the list, then erase. However,
2408 // client might not expect this to happen. The code as it is thrashes the
2409 // use/def lists, which is kinda lame.
2410 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2412 OL[i-2+1] = OL[i+1];
2415 // Nuke the last value.
2416 OL[NumOps-2].set(0);
2417 OL[NumOps-2+1].set(0);
2418 NumOperands = NumOps-2;
2421 /// resizeOperands - resize operands - This adjusts the length of the operands
2422 /// list according to the following behavior:
2423 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
2424 /// of operation. This grows the number of ops by 1.5 times.
2425 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2426 /// 3. If NumOps == NumOperands, trim the reserved space.
2428 void SwitchInst::resizeOperands(unsigned NumOps) {
2430 NumOps = getNumOperands()/2*6;
2431 } else if (NumOps*2 > NumOperands) {
2432 // No resize needed.
2433 if (ReservedSpace >= NumOps) return;
2434 } else if (NumOps == NumOperands) {
2435 if (ReservedSpace == NumOps) return;
2440 ReservedSpace = NumOps;
2441 Use *NewOps = new Use[NumOps];
2442 Use *OldOps = OperandList;
2443 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2444 NewOps[i].init(OldOps[i], this);
2448 OperandList = NewOps;
2452 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2453 return getSuccessor(idx);
2455 unsigned SwitchInst::getNumSuccessorsV() const {
2456 return getNumSuccessors();
2458 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2459 setSuccessor(idx, B);
2463 // Define these methods here so vtables don't get emitted into every translation
2464 // unit that uses these classes.
2466 GetElementPtrInst *GetElementPtrInst::clone() const {
2467 return new GetElementPtrInst(*this);
2470 BinaryOperator *BinaryOperator::clone() const {
2471 return create(getOpcode(), Ops[0], Ops[1]);
2474 CmpInst* CmpInst::clone() const {
2475 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
2478 MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2479 AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2480 FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2481 LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2482 StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2483 CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2484 CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2485 CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2486 CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2487 CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2488 CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2489 CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2490 CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2491 CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2492 CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2493 CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2494 CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2495 CallInst *CallInst::clone() const { return new CallInst(*this); }
2496 SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2497 VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2499 ExtractElementInst *ExtractElementInst::clone() const {
2500 return new ExtractElementInst(*this);
2502 InsertElementInst *InsertElementInst::clone() const {
2503 return new InsertElementInst(*this);
2505 ShuffleVectorInst *ShuffleVectorInst::clone() const {
2506 return new ShuffleVectorInst(*this);
2508 PHINode *PHINode::clone() const { return new PHINode(*this); }
2509 ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2510 BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2511 SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2512 InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2513 UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
2514 UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}