Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / VMCore / Instructions.cpp
blobc6730e14c5c098750ebdb113503337606f4ffe12
1 //===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements all of the non-inline methods for the LLVM instruction
11 // classes.
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 using namespace llvm;
23 unsigned CallSite::getCallingConv() const {
24 if (CallInst *CI = dyn_cast<CallInst>(I))
25 return CI->getCallingConv();
26 else
27 return cast<InvokeInst>(I)->getCallingConv();
29 void CallSite::setCallingConv(unsigned CC) {
30 if (CallInst *CI = dyn_cast<CallInst>(I))
31 CI->setCallingConv(CC);
32 else
33 cast<InvokeInst>(I)->setCallingConv(CC);
39 //===----------------------------------------------------------------------===//
40 // TerminatorInst Class
41 //===----------------------------------------------------------------------===//
43 TerminatorInst::TerminatorInst(Instruction::TermOps iType,
44 Use *Ops, unsigned NumOps, Instruction *IB)
45 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
48 TerminatorInst::TerminatorInst(Instruction::TermOps iType,
49 Use *Ops, unsigned NumOps, BasicBlock *IAE)
50 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
53 // Out of line virtual method, so the vtable, etc has a home.
54 TerminatorInst::~TerminatorInst() {
57 // Out of line virtual method, so the vtable, etc has a home.
58 UnaryInstruction::~UnaryInstruction() {
62 //===----------------------------------------------------------------------===//
63 // PHINode Class
64 //===----------------------------------------------------------------------===//
66 PHINode::PHINode(const PHINode &PN)
67 : Instruction(PN.getType(), Instruction::PHI,
68 new Use[PN.getNumOperands()], PN.getNumOperands()),
69 ReservedSpace(PN.getNumOperands()) {
70 Use *OL = OperandList;
71 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
72 OL[i].init(PN.getOperand(i), this);
73 OL[i+1].init(PN.getOperand(i+1), this);
77 PHINode::~PHINode() {
78 delete [] OperandList;
81 // removeIncomingValue - Remove an incoming value. This is useful if a
82 // predecessor basic block is deleted.
83 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
84 unsigned NumOps = getNumOperands();
85 Use *OL = OperandList;
86 assert(Idx*2 < NumOps && "BB not in PHI node!");
87 Value *Removed = OL[Idx*2];
89 // Move everything after this operand down.
91 // FIXME: we could just swap with the end of the list, then erase. However,
92 // client might not expect this to happen. The code as it is thrashes the
93 // use/def lists, which is kinda lame.
94 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
95 OL[i-2] = OL[i];
96 OL[i-2+1] = OL[i+1];
99 // Nuke the last value.
100 OL[NumOps-2].set(0);
101 OL[NumOps-2+1].set(0);
102 NumOperands = NumOps-2;
104 // If the PHI node is dead, because it has zero entries, nuke it now.
105 if (NumOps == 2 && DeletePHIIfEmpty) {
106 // If anyone is using this PHI, make them use a dummy value instead...
107 replaceAllUsesWith(UndefValue::get(getType()));
108 eraseFromParent();
110 return Removed;
113 /// resizeOperands - resize operands - This adjusts the length of the operands
114 /// list according to the following behavior:
115 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
116 /// of operation. This grows the number of ops by 1.5 times.
117 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
118 /// 3. If NumOps == NumOperands, trim the reserved space.
120 void PHINode::resizeOperands(unsigned NumOps) {
121 if (NumOps == 0) {
122 NumOps = (getNumOperands())*3/2;
123 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
124 } else if (NumOps*2 > NumOperands) {
125 // No resize needed.
126 if (ReservedSpace >= NumOps) return;
127 } else if (NumOps == NumOperands) {
128 if (ReservedSpace == NumOps) return;
129 } else {
130 return;
133 ReservedSpace = NumOps;
134 Use *NewOps = new Use[NumOps];
135 Use *OldOps = OperandList;
136 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
137 NewOps[i].init(OldOps[i], this);
138 OldOps[i].set(0);
140 delete [] OldOps;
141 OperandList = NewOps;
144 /// hasConstantValue - If the specified PHI node always merges together the same
145 /// value, return the value, otherwise return null.
147 Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
148 // If the PHI node only has one incoming value, eliminate the PHI node...
149 if (getNumIncomingValues() == 1)
150 if (getIncomingValue(0) != this) // not X = phi X
151 return getIncomingValue(0);
152 else
153 return UndefValue::get(getType()); // Self cycle is dead.
155 // Otherwise if all of the incoming values are the same for the PHI, replace
156 // the PHI node with the incoming value.
158 Value *InVal = 0;
159 bool HasUndefInput = false;
160 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
161 if (isa<UndefValue>(getIncomingValue(i)))
162 HasUndefInput = true;
163 else if (getIncomingValue(i) != this) // Not the PHI node itself...
164 if (InVal && getIncomingValue(i) != InVal)
165 return 0; // Not the same, bail out.
166 else
167 InVal = getIncomingValue(i);
169 // The only case that could cause InVal to be null is if we have a PHI node
170 // that only has entries for itself. In this case, there is no entry into the
171 // loop, so kill the PHI.
173 if (InVal == 0) InVal = UndefValue::get(getType());
175 // If we have a PHI node like phi(X, undef, X), where X is defined by some
176 // instruction, we cannot always return X as the result of the PHI node. Only
177 // do this if X is not an instruction (thus it must dominate the PHI block),
178 // or if the client is prepared to deal with this possibility.
179 if (HasUndefInput && !AllowNonDominatingInstruction)
180 if (Instruction *IV = dyn_cast<Instruction>(InVal))
181 // If it's in the entry block, it dominates everything.
182 if (IV->getParent() != &IV->getParent()->getParent()->front() ||
183 isa<InvokeInst>(IV))
184 return 0; // Cannot guarantee that InVal dominates this PHINode.
186 // All of the incoming values are the same, return the value now.
187 return InVal;
191 //===----------------------------------------------------------------------===//
192 // CallInst Implementation
193 //===----------------------------------------------------------------------===//
195 CallInst::~CallInst() {
196 delete [] OperandList;
199 void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
200 NumOperands = Params.size()+1;
201 Use *OL = OperandList = new Use[Params.size()+1];
202 OL[0].init(Func, this);
204 const FunctionType *FTy =
205 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
207 assert((Params.size() == FTy->getNumParams() ||
208 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
209 "Calling a function with bad signature!");
210 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
211 assert((i >= FTy->getNumParams() ||
212 FTy->getParamType(i) == Params[i]->getType()) &&
213 "Calling a function with a bad signature!");
214 OL[i+1].init(Params[i], this);
218 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
219 NumOperands = 3;
220 Use *OL = OperandList = new Use[3];
221 OL[0].init(Func, this);
222 OL[1].init(Actual1, this);
223 OL[2].init(Actual2, this);
225 const FunctionType *FTy =
226 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
228 assert((FTy->getNumParams() == 2 ||
229 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
230 "Calling a function with bad signature");
231 assert((0 >= FTy->getNumParams() ||
232 FTy->getParamType(0) == Actual1->getType()) &&
233 "Calling a function with a bad signature!");
234 assert((1 >= FTy->getNumParams() ||
235 FTy->getParamType(1) == Actual2->getType()) &&
236 "Calling a function with a bad signature!");
239 void CallInst::init(Value *Func, Value *Actual) {
240 NumOperands = 2;
241 Use *OL = OperandList = new Use[2];
242 OL[0].init(Func, this);
243 OL[1].init(Actual, this);
245 const FunctionType *FTy =
246 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
248 assert((FTy->getNumParams() == 1 ||
249 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
250 "Calling a function with bad signature");
251 assert((0 == FTy->getNumParams() ||
252 FTy->getParamType(0) == Actual->getType()) &&
253 "Calling a function with a bad signature!");
256 void CallInst::init(Value *Func) {
257 NumOperands = 1;
258 Use *OL = OperandList = new Use[1];
259 OL[0].init(Func, this);
261 const FunctionType *MTy =
262 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
264 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
267 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
268 const std::string &Name, Instruction *InsertBefore)
269 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
270 ->getElementType())->getReturnType(),
271 Instruction::Call, 0, 0, Name, InsertBefore) {
272 init(Func, Params);
275 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
276 const std::string &Name, BasicBlock *InsertAtEnd)
277 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
278 ->getElementType())->getReturnType(),
279 Instruction::Call, 0, 0, Name, InsertAtEnd) {
280 init(Func, Params);
283 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
284 const std::string &Name, Instruction *InsertBefore)
285 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
286 ->getElementType())->getReturnType(),
287 Instruction::Call, 0, 0, Name, InsertBefore) {
288 init(Func, Actual1, Actual2);
291 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
292 const std::string &Name, BasicBlock *InsertAtEnd)
293 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
294 ->getElementType())->getReturnType(),
295 Instruction::Call, 0, 0, Name, InsertAtEnd) {
296 init(Func, Actual1, Actual2);
299 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
300 Instruction *InsertBefore)
301 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
302 ->getElementType())->getReturnType(),
303 Instruction::Call, 0, 0, Name, InsertBefore) {
304 init(Func, Actual);
307 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
308 BasicBlock *InsertAtEnd)
309 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
310 ->getElementType())->getReturnType(),
311 Instruction::Call, 0, 0, Name, InsertAtEnd) {
312 init(Func, Actual);
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, Name, InsertBefore) {
320 init(Func);
323 CallInst::CallInst(Value *Func, const std::string &Name,
324 BasicBlock *InsertAtEnd)
325 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
326 ->getElementType())->getReturnType(),
327 Instruction::Call, 0, 0, Name, InsertAtEnd) {
328 init(Func);
331 CallInst::CallInst(const CallInst &CI)
332 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
333 CI.getNumOperands()) {
334 SubclassData = CI.SubclassData;
335 Use *OL = OperandList;
336 Use *InOL = CI.OperandList;
337 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
338 OL[i].init(InOL[i], this);
342 //===----------------------------------------------------------------------===//
343 // InvokeInst Implementation
344 //===----------------------------------------------------------------------===//
346 InvokeInst::~InvokeInst() {
347 delete [] OperandList;
350 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
351 const std::vector<Value*> &Params) {
352 NumOperands = 3+Params.size();
353 Use *OL = OperandList = new Use[3+Params.size()];
354 OL[0].init(Fn, this);
355 OL[1].init(IfNormal, this);
356 OL[2].init(IfException, this);
357 const FunctionType *FTy =
358 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
360 assert((Params.size() == FTy->getNumParams()) ||
361 (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
362 "Calling a function with bad signature");
364 for (unsigned i = 0, e = Params.size(); i != e; i++) {
365 assert((i >= FTy->getNumParams() ||
366 FTy->getParamType(i) == Params[i]->getType()) &&
367 "Invoking a function with a bad signature!");
369 OL[i+3].init(Params[i], this);
373 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
374 BasicBlock *IfException,
375 const std::vector<Value*> &Params,
376 const std::string &Name, Instruction *InsertBefore)
377 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
378 ->getElementType())->getReturnType(),
379 Instruction::Invoke, 0, 0, Name, InsertBefore) {
380 init(Fn, IfNormal, IfException, Params);
383 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
384 BasicBlock *IfException,
385 const std::vector<Value*> &Params,
386 const std::string &Name, BasicBlock *InsertAtEnd)
387 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
388 ->getElementType())->getReturnType(),
389 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
390 init(Fn, IfNormal, IfException, Params);
393 InvokeInst::InvokeInst(const InvokeInst &II)
394 : TerminatorInst(II.getType(), Instruction::Invoke,
395 new Use[II.getNumOperands()], II.getNumOperands()) {
396 SubclassData = II.SubclassData;
397 Use *OL = OperandList, *InOL = II.OperandList;
398 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
399 OL[i].init(InOL[i], this);
402 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
403 return getSuccessor(idx);
405 unsigned InvokeInst::getNumSuccessorsV() const {
406 return getNumSuccessors();
408 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
409 return setSuccessor(idx, B);
413 //===----------------------------------------------------------------------===//
414 // ReturnInst Implementation
415 //===----------------------------------------------------------------------===//
417 void ReturnInst::init(Value *retVal) {
418 if (retVal && retVal->getType() != Type::VoidTy) {
419 assert(!isa<BasicBlock>(retVal) &&
420 "Cannot return basic block. Probably using the incorrect ctor");
421 NumOperands = 1;
422 RetVal.init(retVal, this);
426 unsigned ReturnInst::getNumSuccessorsV() const {
427 return getNumSuccessors();
430 // Out-of-line ReturnInst method, put here so the C++ compiler can choose to
431 // emit the vtable for the class in this translation unit.
432 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
433 assert(0 && "ReturnInst has no successors!");
436 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
437 assert(0 && "ReturnInst has no successors!");
438 abort();
439 return 0;
443 //===----------------------------------------------------------------------===//
444 // UnwindInst Implementation
445 //===----------------------------------------------------------------------===//
447 unsigned UnwindInst::getNumSuccessorsV() const {
448 return getNumSuccessors();
451 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
452 assert(0 && "UnwindInst has no successors!");
455 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
456 assert(0 && "UnwindInst has no successors!");
457 abort();
458 return 0;
461 //===----------------------------------------------------------------------===//
462 // UnreachableInst Implementation
463 //===----------------------------------------------------------------------===//
465 unsigned UnreachableInst::getNumSuccessorsV() const {
466 return getNumSuccessors();
469 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
470 assert(0 && "UnwindInst has no successors!");
473 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
474 assert(0 && "UnwindInst has no successors!");
475 abort();
476 return 0;
479 //===----------------------------------------------------------------------===//
480 // BranchInst Implementation
481 //===----------------------------------------------------------------------===//
483 void BranchInst::AssertOK() {
484 if (isConditional())
485 assert(getCondition()->getType() == Type::BoolTy &&
486 "May only branch on boolean predicates!");
489 BranchInst::BranchInst(const BranchInst &BI) :
490 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
491 OperandList[0].init(BI.getOperand(0), this);
492 if (BI.getNumOperands() != 1) {
493 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
494 OperandList[1].init(BI.getOperand(1), this);
495 OperandList[2].init(BI.getOperand(2), this);
499 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
500 return getSuccessor(idx);
502 unsigned BranchInst::getNumSuccessorsV() const {
503 return getNumSuccessors();
505 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
506 setSuccessor(idx, B);
510 //===----------------------------------------------------------------------===//
511 // AllocationInst Implementation
512 //===----------------------------------------------------------------------===//
514 static Value *getAISize(Value *Amt) {
515 if (!Amt)
516 Amt = ConstantUInt::get(Type::UIntTy, 1);
517 else {
518 assert(!isa<BasicBlock>(Amt) &&
519 "Passed basic block into allocation size parameter! Ue other ctor");
520 assert(Amt->getType() == Type::UIntTy &&
521 "Malloc/Allocation array size != UIntTy!");
523 return Amt;
526 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
527 unsigned Align, const std::string &Name,
528 Instruction *InsertBefore)
529 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
530 Name, InsertBefore), Alignment(Align) {
531 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
532 assert(Ty != Type::VoidTy && "Cannot allocate void!");
535 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
536 unsigned Align, const std::string &Name,
537 BasicBlock *InsertAtEnd)
538 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
539 Name, InsertAtEnd), Alignment(Align) {
540 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
541 assert(Ty != Type::VoidTy && "Cannot allocate void!");
544 // Out of line virtual method, so the vtable, etc has a home.
545 AllocationInst::~AllocationInst() {
548 bool AllocationInst::isArrayAllocation() const {
549 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(getOperand(0)))
550 return CUI->getValue() != 1;
551 return true;
554 const Type *AllocationInst::getAllocatedType() const {
555 return getType()->getElementType();
558 AllocaInst::AllocaInst(const AllocaInst &AI)
559 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
560 Instruction::Alloca, AI.getAlignment()) {
563 MallocInst::MallocInst(const MallocInst &MI)
564 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
565 Instruction::Malloc, MI.getAlignment()) {
568 //===----------------------------------------------------------------------===//
569 // FreeInst Implementation
570 //===----------------------------------------------------------------------===//
572 void FreeInst::AssertOK() {
573 assert(isa<PointerType>(getOperand(0)->getType()) &&
574 "Can not free something of nonpointer type!");
577 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
578 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
579 AssertOK();
582 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
583 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
584 AssertOK();
588 //===----------------------------------------------------------------------===//
589 // LoadInst Implementation
590 //===----------------------------------------------------------------------===//
592 void LoadInst::AssertOK() {
593 assert(isa<PointerType>(getOperand(0)->getType()) &&
594 "Ptr must have pointer type.");
597 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
598 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
599 Load, Ptr, Name, InsertBef) {
600 setVolatile(false);
601 AssertOK();
604 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
605 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
606 Load, Ptr, Name, InsertAE) {
607 setVolatile(false);
608 AssertOK();
611 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
612 Instruction *InsertBef)
613 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
614 Load, Ptr, Name, InsertBef) {
615 setVolatile(isVolatile);
616 AssertOK();
619 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
620 BasicBlock *InsertAE)
621 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
622 Load, Ptr, Name, InsertAE) {
623 setVolatile(isVolatile);
624 AssertOK();
628 //===----------------------------------------------------------------------===//
629 // StoreInst Implementation
630 //===----------------------------------------------------------------------===//
632 void StoreInst::AssertOK() {
633 assert(isa<PointerType>(getOperand(1)->getType()) &&
634 "Ptr must have pointer type!");
635 assert(getOperand(0)->getType() ==
636 cast<PointerType>(getOperand(1)->getType())->getElementType()
637 && "Ptr must be a pointer to Val type!");
641 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
642 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
643 Ops[0].init(val, this);
644 Ops[1].init(addr, this);
645 setVolatile(false);
646 AssertOK();
649 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
650 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
651 Ops[0].init(val, this);
652 Ops[1].init(addr, this);
653 setVolatile(false);
654 AssertOK();
657 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
658 Instruction *InsertBefore)
659 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
660 Ops[0].init(val, this);
661 Ops[1].init(addr, this);
662 setVolatile(isVolatile);
663 AssertOK();
666 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
667 BasicBlock *InsertAtEnd)
668 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
669 Ops[0].init(val, this);
670 Ops[1].init(addr, this);
671 setVolatile(isVolatile);
672 AssertOK();
675 //===----------------------------------------------------------------------===//
676 // GetElementPtrInst Implementation
677 //===----------------------------------------------------------------------===//
679 // checkType - Simple wrapper function to give a better assertion failure
680 // message on bad indexes for a gep instruction.
682 static inline const Type *checkType(const Type *Ty) {
683 assert(Ty && "Invalid GetElementPtrInst indices for type!");
684 return Ty;
687 void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) {
688 NumOperands = 1+Idx.size();
689 Use *OL = OperandList = new Use[NumOperands];
690 OL[0].init(Ptr, this);
692 for (unsigned i = 0, e = Idx.size(); i != e; ++i)
693 OL[i+1].init(Idx[i], this);
696 void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
697 NumOperands = 3;
698 Use *OL = OperandList = new Use[3];
699 OL[0].init(Ptr, this);
700 OL[1].init(Idx0, this);
701 OL[2].init(Idx1, this);
704 void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
705 NumOperands = 2;
706 Use *OL = OperandList = new Use[2];
707 OL[0].init(Ptr, this);
708 OL[1].init(Idx, this);
711 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
712 const std::string &Name, Instruction *InBe)
713 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
714 Idx, true))),
715 GetElementPtr, 0, 0, Name, InBe) {
716 init(Ptr, Idx);
719 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
720 const std::string &Name, BasicBlock *IAE)
721 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
722 Idx, true))),
723 GetElementPtr, 0, 0, Name, IAE) {
724 init(Ptr, Idx);
727 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
728 const std::string &Name, Instruction *InBe)
729 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
730 GetElementPtr, 0, 0, Name, InBe) {
731 init(Ptr, Idx);
734 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
735 const std::string &Name, BasicBlock *IAE)
736 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
737 GetElementPtr, 0, 0, Name, IAE) {
738 init(Ptr, Idx);
741 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
742 const std::string &Name, Instruction *InBe)
743 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
744 Idx0, Idx1, true))),
745 GetElementPtr, 0, 0, Name, InBe) {
746 init(Ptr, Idx0, Idx1);
749 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
750 const std::string &Name, BasicBlock *IAE)
751 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
752 Idx0, Idx1, true))),
753 GetElementPtr, 0, 0, Name, IAE) {
754 init(Ptr, Idx0, Idx1);
757 GetElementPtrInst::~GetElementPtrInst() {
758 delete[] OperandList;
761 // getIndexedType - Returns the type of the element that would be loaded with
762 // a load instruction with the specified parameters.
764 // A null type is returned if the indices are invalid for the specified
765 // pointer type.
767 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
768 const std::vector<Value*> &Idx,
769 bool AllowCompositeLeaf) {
770 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
772 // Handle the special case of the empty set index set...
773 if (Idx.empty())
774 if (AllowCompositeLeaf ||
775 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
776 return cast<PointerType>(Ptr)->getElementType();
777 else
778 return 0;
780 unsigned CurIdx = 0;
781 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
782 if (Idx.size() == CurIdx) {
783 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
784 return 0; // Can't load a whole structure or array!?!?
787 Value *Index = Idx[CurIdx++];
788 if (isa<PointerType>(CT) && CurIdx != 1)
789 return 0; // Can only index into pointer types at the first index!
790 if (!CT->indexValid(Index)) return 0;
791 Ptr = CT->getTypeAtIndex(Index);
793 // If the new type forwards to another type, then it is in the middle
794 // of being refined to another type (and hence, may have dropped all
795 // references to what it was using before). So, use the new forwarded
796 // type.
797 if (const Type * Ty = Ptr->getForwardedType()) {
798 Ptr = Ty;
801 return CurIdx == Idx.size() ? Ptr : 0;
804 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
805 Value *Idx0, Value *Idx1,
806 bool AllowCompositeLeaf) {
807 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
808 if (!PTy) return 0; // Type isn't a pointer type!
810 // Check the pointer index.
811 if (!PTy->indexValid(Idx0)) return 0;
813 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
814 if (!CT || !CT->indexValid(Idx1)) return 0;
816 const Type *ElTy = CT->getTypeAtIndex(Idx1);
817 if (AllowCompositeLeaf || ElTy->isFirstClassType())
818 return ElTy;
819 return 0;
822 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
823 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
824 if (!PTy) return 0; // Type isn't a pointer type!
826 // Check the pointer index.
827 if (!PTy->indexValid(Idx)) return 0;
829 return PTy->getElementType();
832 //===----------------------------------------------------------------------===//
833 // ExtractElementInst Implementation
834 //===----------------------------------------------------------------------===//
836 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
837 const std::string &Name,
838 Instruction *InsertBef)
839 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
840 ExtractElement, Ops, 2, Name, InsertBef) {
841 assert(isValidOperands(Val, Index) &&
842 "Invalid extractelement instruction operands!");
843 Ops[0].init(Val, this);
844 Ops[1].init(Index, this);
847 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
848 const std::string &Name,
849 BasicBlock *InsertAE)
850 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
851 ExtractElement, Ops, 2, Name, InsertAE) {
852 assert(isValidOperands(Val, Index) &&
853 "Invalid extractelement instruction operands!");
855 Ops[0].init(Val, this);
856 Ops[1].init(Index, this);
859 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
860 if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::UIntTy)
861 return false;
862 return true;
866 //===----------------------------------------------------------------------===//
867 // InsertElementInst Implementation
868 //===----------------------------------------------------------------------===//
870 InsertElementInst::InsertElementInst(const InsertElementInst &IE)
871 : Instruction(IE.getType(), InsertElement, Ops, 3) {
872 Ops[0].init(IE.Ops[0], this);
873 Ops[1].init(IE.Ops[1], this);
874 Ops[2].init(IE.Ops[2], this);
876 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
877 const std::string &Name,
878 Instruction *InsertBef)
879 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
880 assert(isValidOperands(Vec, Elt, Index) &&
881 "Invalid insertelement instruction operands!");
882 Ops[0].init(Vec, this);
883 Ops[1].init(Elt, this);
884 Ops[2].init(Index, this);
887 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
888 const std::string &Name,
889 BasicBlock *InsertAE)
890 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
891 assert(isValidOperands(Vec, Elt, Index) &&
892 "Invalid insertelement instruction operands!");
894 Ops[0].init(Vec, this);
895 Ops[1].init(Elt, this);
896 Ops[2].init(Index, this);
899 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
900 const Value *Index) {
901 if (!isa<PackedType>(Vec->getType()))
902 return false; // First operand of insertelement must be packed type.
904 if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType())
905 return false;// Second operand of insertelement must be packed element type.
907 if (Index->getType() != Type::UIntTy)
908 return false; // Third operand of insertelement must be uint.
909 return true;
913 //===----------------------------------------------------------------------===//
914 // ShuffleVectorInst Implementation
915 //===----------------------------------------------------------------------===//
917 ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
918 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
919 Ops[0].init(SV.Ops[0], this);
920 Ops[1].init(SV.Ops[1], this);
921 Ops[2].init(SV.Ops[2], this);
924 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
925 const std::string &Name,
926 Instruction *InsertBefore)
927 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) {
928 assert(isValidOperands(V1, V2, Mask) &&
929 "Invalid shuffle vector instruction operands!");
930 Ops[0].init(V1, this);
931 Ops[1].init(V2, this);
932 Ops[2].init(Mask, this);
935 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
936 const std::string &Name,
937 BasicBlock *InsertAtEnd)
938 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) {
939 assert(isValidOperands(V1, V2, Mask) &&
940 "Invalid shuffle vector instruction operands!");
942 Ops[0].init(V1, this);
943 Ops[1].init(V2, this);
944 Ops[2].init(Mask, this);
947 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
948 const Value *Mask) {
949 if (!isa<PackedType>(V1->getType())) return false;
950 if (V1->getType() != V2->getType()) return false;
951 if (!isa<PackedType>(Mask->getType()) ||
952 cast<PackedType>(Mask->getType())->getElementType() != Type::UIntTy ||
953 cast<PackedType>(Mask->getType())->getNumElements() !=
954 cast<PackedType>(V1->getType())->getNumElements())
955 return false;
956 return true;
960 //===----------------------------------------------------------------------===//
961 // BinaryOperator Class
962 //===----------------------------------------------------------------------===//
964 void BinaryOperator::init(BinaryOps iType)
966 Value *LHS = getOperand(0), *RHS = getOperand(1);
967 assert(LHS->getType() == RHS->getType() &&
968 "Binary operator operand types must match!");
969 #ifndef NDEBUG
970 switch (iType) {
971 case Add: case Sub:
972 case Mul: case Div:
973 case Rem:
974 assert(getType() == LHS->getType() &&
975 "Arithmetic operation should return same type as operands!");
976 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
977 isa<PackedType>(getType())) &&
978 "Tried to create an arithmetic operation on a non-arithmetic type!");
979 break;
980 case And: case Or:
981 case Xor:
982 assert(getType() == LHS->getType() &&
983 "Logical operation should return same type as operands!");
984 assert((getType()->isIntegral() ||
985 (isa<PackedType>(getType()) &&
986 cast<PackedType>(getType())->getElementType()->isIntegral())) &&
987 "Tried to create a logical operation on a non-integral type!");
988 break;
989 case SetLT: case SetGT: case SetLE:
990 case SetGE: case SetEQ: case SetNE:
991 assert(getType() == Type::BoolTy && "Setcc must return bool!");
992 default:
993 break;
995 #endif
998 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
999 const std::string &Name,
1000 Instruction *InsertBefore) {
1001 assert(S1->getType() == S2->getType() &&
1002 "Cannot create binary operator with two operands of differing type!");
1003 switch (Op) {
1004 // Binary comparison operators...
1005 case SetLT: case SetGT: case SetLE:
1006 case SetGE: case SetEQ: case SetNE:
1007 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
1009 default:
1010 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1014 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
1015 const std::string &Name,
1016 BasicBlock *InsertAtEnd) {
1017 BinaryOperator *Res = create(Op, S1, S2, Name);
1018 InsertAtEnd->getInstList().push_back(Res);
1019 return Res;
1022 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1023 Instruction *InsertBefore) {
1024 if (!Op->getType()->isFloatingPoint())
1025 return new BinaryOperator(Instruction::Sub,
1026 Constant::getNullValue(Op->getType()), Op,
1027 Op->getType(), Name, InsertBefore);
1028 else
1029 return new BinaryOperator(Instruction::Sub,
1030 ConstantFP::get(Op->getType(), -0.0), Op,
1031 Op->getType(), Name, InsertBefore);
1034 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1035 BasicBlock *InsertAtEnd) {
1036 if (!Op->getType()->isFloatingPoint())
1037 return new BinaryOperator(Instruction::Sub,
1038 Constant::getNullValue(Op->getType()), Op,
1039 Op->getType(), Name, InsertAtEnd);
1040 else
1041 return new BinaryOperator(Instruction::Sub,
1042 ConstantFP::get(Op->getType(), -0.0), Op,
1043 Op->getType(), Name, InsertAtEnd);
1046 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1047 Instruction *InsertBefore) {
1048 Constant *C;
1049 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
1050 C = ConstantIntegral::getAllOnesValue(PTy->getElementType());
1051 C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C));
1052 } else {
1053 C = ConstantIntegral::getAllOnesValue(Op->getType());
1056 return new BinaryOperator(Instruction::Xor, Op, C,
1057 Op->getType(), Name, InsertBefore);
1060 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1061 BasicBlock *InsertAtEnd) {
1062 Constant *AllOnes;
1063 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
1064 // Create a vector of all ones values.
1065 Constant *Elt = ConstantIntegral::getAllOnesValue(PTy->getElementType());
1066 AllOnes =
1067 ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1068 } else {
1069 AllOnes = ConstantIntegral::getAllOnesValue(Op->getType());
1072 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1073 Op->getType(), Name, InsertAtEnd);
1077 // isConstantAllOnes - Helper function for several functions below
1078 static inline bool isConstantAllOnes(const Value *V) {
1079 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
1082 bool BinaryOperator::isNeg(const Value *V) {
1083 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1084 if (Bop->getOpcode() == Instruction::Sub)
1085 if (!V->getType()->isFloatingPoint())
1086 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
1087 else
1088 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
1089 return false;
1092 bool BinaryOperator::isNot(const Value *V) {
1093 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1094 return (Bop->getOpcode() == Instruction::Xor &&
1095 (isConstantAllOnes(Bop->getOperand(1)) ||
1096 isConstantAllOnes(Bop->getOperand(0))));
1097 return false;
1100 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1101 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1102 return cast<BinaryOperator>(BinOp)->getOperand(1);
1105 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1106 return getNegArgument(const_cast<Value*>(BinOp));
1109 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1110 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1111 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1112 Value *Op0 = BO->getOperand(0);
1113 Value *Op1 = BO->getOperand(1);
1114 if (isConstantAllOnes(Op0)) return Op1;
1116 assert(isConstantAllOnes(Op1));
1117 return Op0;
1120 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1121 return getNotArgument(const_cast<Value*>(BinOp));
1125 // swapOperands - Exchange the two operands to this instruction. This
1126 // instruction is safe to use on any binary instruction and does not
1127 // modify the semantics of the instruction. If the instruction is
1128 // order dependent (SetLT f.e.) the opcode is changed.
1130 bool BinaryOperator::swapOperands() {
1131 if (isCommutative())
1132 ; // If the instruction is commutative, it is safe to swap the operands
1133 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
1134 /// FIXME: SetCC instructions shouldn't all have different opcodes.
1135 setOpcode(SCI->getSwappedCondition());
1136 else
1137 return true; // Can't commute operands
1139 std::swap(Ops[0], Ops[1]);
1140 return false;
1144 //===----------------------------------------------------------------------===//
1145 // SetCondInst Class
1146 //===----------------------------------------------------------------------===//
1148 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
1149 const std::string &Name, Instruction *InsertBefore)
1150 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
1152 // Make sure it's a valid type... getInverseCondition will assert out if not.
1153 assert(getInverseCondition(Opcode));
1156 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
1157 const std::string &Name, BasicBlock *InsertAtEnd)
1158 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
1160 // Make sure it's a valid type... getInverseCondition will assert out if not.
1161 assert(getInverseCondition(Opcode));
1164 // getInverseCondition - Return the inverse of the current condition opcode.
1165 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
1167 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
1168 switch (Opcode) {
1169 default:
1170 assert(0 && "Unknown setcc opcode!");
1171 case SetEQ: return SetNE;
1172 case SetNE: return SetEQ;
1173 case SetGT: return SetLE;
1174 case SetLT: return SetGE;
1175 case SetGE: return SetLT;
1176 case SetLE: return SetGT;
1180 // getSwappedCondition - Return the condition opcode that would be the result
1181 // of exchanging the two operands of the setcc instruction without changing
1182 // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
1184 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
1185 switch (Opcode) {
1186 default: assert(0 && "Unknown setcc instruction!");
1187 case SetEQ: case SetNE: return Opcode;
1188 case SetGT: return SetLT;
1189 case SetLT: return SetGT;
1190 case SetGE: return SetLE;
1191 case SetLE: return SetGE;
1195 //===----------------------------------------------------------------------===//
1196 // SwitchInst Implementation
1197 //===----------------------------------------------------------------------===//
1199 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
1200 assert(Value && Default);
1201 ReservedSpace = 2+NumCases*2;
1202 NumOperands = 2;
1203 OperandList = new Use[ReservedSpace];
1205 OperandList[0].init(Value, this);
1206 OperandList[1].init(Default, this);
1209 SwitchInst::SwitchInst(const SwitchInst &SI)
1210 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
1211 SI.getNumOperands()) {
1212 Use *OL = OperandList, *InOL = SI.OperandList;
1213 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
1214 OL[i].init(InOL[i], this);
1215 OL[i+1].init(InOL[i+1], this);
1219 SwitchInst::~SwitchInst() {
1220 delete [] OperandList;
1224 /// addCase - Add an entry to the switch instruction...
1226 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
1227 unsigned OpNo = NumOperands;
1228 if (OpNo+2 > ReservedSpace)
1229 resizeOperands(0); // Get more space!
1230 // Initialize some new operands.
1231 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
1232 NumOperands = OpNo+2;
1233 OperandList[OpNo].init(OnVal, this);
1234 OperandList[OpNo+1].init(Dest, this);
1237 /// removeCase - This method removes the specified successor from the switch
1238 /// instruction. Note that this cannot be used to remove the default
1239 /// destination (successor #0).
1241 void SwitchInst::removeCase(unsigned idx) {
1242 assert(idx != 0 && "Cannot remove the default case!");
1243 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
1245 unsigned NumOps = getNumOperands();
1246 Use *OL = OperandList;
1248 // Move everything after this operand down.
1250 // FIXME: we could just swap with the end of the list, then erase. However,
1251 // client might not expect this to happen. The code as it is thrashes the
1252 // use/def lists, which is kinda lame.
1253 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
1254 OL[i-2] = OL[i];
1255 OL[i-2+1] = OL[i+1];
1258 // Nuke the last value.
1259 OL[NumOps-2].set(0);
1260 OL[NumOps-2+1].set(0);
1261 NumOperands = NumOps-2;
1264 /// resizeOperands - resize operands - This adjusts the length of the operands
1265 /// list according to the following behavior:
1266 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
1267 /// of operation. This grows the number of ops by 1.5 times.
1268 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
1269 /// 3. If NumOps == NumOperands, trim the reserved space.
1271 void SwitchInst::resizeOperands(unsigned NumOps) {
1272 if (NumOps == 0) {
1273 NumOps = getNumOperands()/2*6;
1274 } else if (NumOps*2 > NumOperands) {
1275 // No resize needed.
1276 if (ReservedSpace >= NumOps) return;
1277 } else if (NumOps == NumOperands) {
1278 if (ReservedSpace == NumOps) return;
1279 } else {
1280 return;
1283 ReservedSpace = NumOps;
1284 Use *NewOps = new Use[NumOps];
1285 Use *OldOps = OperandList;
1286 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1287 NewOps[i].init(OldOps[i], this);
1288 OldOps[i].set(0);
1290 delete [] OldOps;
1291 OperandList = NewOps;
1295 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
1296 return getSuccessor(idx);
1298 unsigned SwitchInst::getNumSuccessorsV() const {
1299 return getNumSuccessors();
1301 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1302 setSuccessor(idx, B);
1306 // Define these methods here so vtables don't get emitted into every translation
1307 // unit that uses these classes.
1309 GetElementPtrInst *GetElementPtrInst::clone() const {
1310 return new GetElementPtrInst(*this);
1313 BinaryOperator *BinaryOperator::clone() const {
1314 return create(getOpcode(), Ops[0], Ops[1]);
1317 MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
1318 AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
1319 FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
1320 LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
1321 StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
1322 CastInst *CastInst::clone() const { return new CastInst(*this); }
1323 CallInst *CallInst::clone() const { return new CallInst(*this); }
1324 ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
1325 SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
1326 VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
1327 ExtractElementInst *ExtractElementInst::clone() const {
1328 return new ExtractElementInst(*this);
1330 InsertElementInst *InsertElementInst::clone() const {
1331 return new InsertElementInst(*this);
1333 ShuffleVectorInst *ShuffleVectorInst::clone() const {
1334 return new ShuffleVectorInst(*this);
1336 PHINode *PHINode::clone() const { return new PHINode(*this); }
1337 ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
1338 BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
1339 SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
1340 InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
1341 UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
1342 UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}