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"
23 //===----------------------------------------------------------------------===//
24 // TerminatorInst Class
25 //===----------------------------------------------------------------------===//
27 TerminatorInst::TerminatorInst(Instruction::TermOps iType,
28 Use *Ops, unsigned NumOps, Instruction *IB)
29 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
32 TerminatorInst::TerminatorInst(Instruction::TermOps iType,
33 Use *Ops, unsigned NumOps, BasicBlock *IAE)
34 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
43 PHINode::PHINode(const PHINode &PN)
44 : Instruction(PN.getType(), Instruction::PHI,
45 new Use[PN.getNumOperands()], PN.getNumOperands()),
46 ReservedSpace(PN.getNumOperands()) {
47 Use *OL = OperandList;
48 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
49 OL[i].init(PN.getOperand(i), this);
50 OL[i+1].init(PN.getOperand(i+1), this);
55 delete [] OperandList;
58 // removeIncomingValue - Remove an incoming value. This is useful if a
59 // predecessor basic block is deleted.
60 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
61 unsigned NumOps = getNumOperands();
62 Use *OL = OperandList;
63 assert(Idx*2 < NumOps && "BB not in PHI node!");
64 Value *Removed = OL[Idx*2];
66 // Move everything after this operand down.
68 // FIXME: we could just swap with the end of the list, then erase. However,
69 // client might not expect this to happen. The code as it is thrashes the
70 // use/def lists, which is kinda lame.
71 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
76 // Nuke the last value.
78 OL[NumOps-2+1].set(0);
79 NumOperands = NumOps-2;
81 // If the PHI node is dead, because it has zero entries, nuke it now.
82 if (NumOps == 2 && DeletePHIIfEmpty) {
83 // If anyone is using this PHI, make them use a dummy value instead...
84 replaceAllUsesWith(UndefValue::get(getType()));
90 /// resizeOperands - resize operands - This adjusts the length of the operands
91 /// list according to the following behavior:
92 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
93 /// of operation. This grows the number of ops by 1.5 times.
94 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
95 /// 3. If NumOps == NumOperands, trim the reserved space.
97 void PHINode::resizeOperands(unsigned NumOps) {
99 NumOps = (getNumOperands())*3/2;
100 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
101 } else if (NumOps*2 > NumOperands) {
103 if (ReservedSpace >= NumOps) return;
104 } else if (NumOps == NumOperands) {
105 if (ReservedSpace == NumOps) return;
110 ReservedSpace = NumOps;
111 Use *NewOps = new Use[NumOps];
112 Use *OldOps = OperandList;
113 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
114 NewOps[i].init(OldOps[i], this);
118 OperandList = NewOps;
122 //===----------------------------------------------------------------------===//
123 // CallInst Implementation
124 //===----------------------------------------------------------------------===//
126 CallInst::~CallInst() {
127 delete [] OperandList;
130 void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
131 NumOperands = Params.size()+1;
132 Use *OL = OperandList = new Use[Params.size()+1];
133 OL[0].init(Func, this);
135 const FunctionType *FTy =
136 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
138 assert((Params.size() == FTy->getNumParams() ||
139 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
140 "Calling a function with bad signature");
141 for (unsigned i = 0, e = Params.size(); i != e; ++i)
142 OL[i+1].init(Params[i], this);
145 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
147 Use *OL = OperandList = new Use[3];
148 OL[0].init(Func, this);
149 OL[1].init(Actual1, this);
150 OL[2].init(Actual2, this);
152 const FunctionType *FTy =
153 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
155 assert((FTy->getNumParams() == 2 ||
156 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
157 "Calling a function with bad signature");
160 void CallInst::init(Value *Func, Value *Actual) {
162 Use *OL = OperandList = new Use[2];
163 OL[0].init(Func, this);
164 OL[1].init(Actual, this);
166 const FunctionType *FTy =
167 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
169 assert((FTy->getNumParams() == 1 ||
170 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
171 "Calling a function with bad signature");
174 void CallInst::init(Value *Func) {
176 Use *OL = OperandList = new Use[1];
177 OL[0].init(Func, this);
179 const FunctionType *MTy =
180 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
182 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
185 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
186 const std::string &Name, Instruction *InsertBefore)
187 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
188 ->getElementType())->getReturnType(),
189 Instruction::Call, 0, 0, Name, InsertBefore) {
193 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
194 const std::string &Name, BasicBlock *InsertAtEnd)
195 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
196 ->getElementType())->getReturnType(),
197 Instruction::Call, 0, 0, Name, InsertAtEnd) {
201 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
202 const std::string &Name, Instruction *InsertBefore)
203 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
204 ->getElementType())->getReturnType(),
205 Instruction::Call, 0, 0, Name, InsertBefore) {
206 init(Func, Actual1, Actual2);
209 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
210 const std::string &Name, BasicBlock *InsertAtEnd)
211 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
212 ->getElementType())->getReturnType(),
213 Instruction::Call, 0, 0, Name, InsertAtEnd) {
214 init(Func, Actual1, Actual2);
217 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
218 Instruction *InsertBefore)
219 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
220 ->getElementType())->getReturnType(),
221 Instruction::Call, 0, 0, Name, InsertBefore) {
225 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
226 BasicBlock *InsertAtEnd)
227 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
228 ->getElementType())->getReturnType(),
229 Instruction::Call, 0, 0, Name, InsertAtEnd) {
233 CallInst::CallInst(Value *Func, const std::string &Name,
234 Instruction *InsertBefore)
235 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
236 ->getElementType())->getReturnType(),
237 Instruction::Call, 0, 0, Name, InsertBefore) {
241 CallInst::CallInst(Value *Func, const std::string &Name,
242 BasicBlock *InsertAtEnd)
243 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
244 ->getElementType())->getReturnType(),
245 Instruction::Call, 0, 0, Name, InsertAtEnd) {
249 CallInst::CallInst(const CallInst &CI)
250 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
251 CI.getNumOperands()) {
252 Use *OL = OperandList;
253 Use *InOL = CI.OperandList;
254 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
255 OL[i].init(InOL[i], this);
259 //===----------------------------------------------------------------------===//
260 // InvokeInst Implementation
261 //===----------------------------------------------------------------------===//
263 InvokeInst::~InvokeInst() {
264 delete [] OperandList;
267 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
268 const std::vector<Value*> &Params) {
269 NumOperands = 3+Params.size();
270 Use *OL = OperandList = new Use[3+Params.size()];
271 OL[0].init(Fn, this);
272 OL[1].init(IfNormal, this);
273 OL[2].init(IfException, this);
274 const FunctionType *FTy =
275 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
277 assert((Params.size() == FTy->getNumParams()) ||
278 (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
279 "Calling a function with bad signature");
281 for (unsigned i = 0, e = Params.size(); i != e; i++)
282 OL[i+3].init(Params[i], this);
285 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
286 BasicBlock *IfException,
287 const std::vector<Value*> &Params,
288 const std::string &Name, Instruction *InsertBefore)
289 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
290 ->getElementType())->getReturnType(),
291 Instruction::Invoke, 0, 0, Name, InsertBefore) {
292 init(Fn, IfNormal, IfException, Params);
295 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
296 BasicBlock *IfException,
297 const std::vector<Value*> &Params,
298 const std::string &Name, BasicBlock *InsertAtEnd)
299 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
300 ->getElementType())->getReturnType(),
301 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
302 init(Fn, IfNormal, IfException, Params);
305 InvokeInst::InvokeInst(const InvokeInst &II)
306 : TerminatorInst(II.getType(), Instruction::Invoke,
307 new Use[II.getNumOperands()], II.getNumOperands()) {
308 Use *OL = OperandList, *InOL = II.OperandList;
309 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
310 OL[i].init(InOL[i], this);
313 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
314 return getSuccessor(idx);
316 unsigned InvokeInst::getNumSuccessorsV() const {
317 return getNumSuccessors();
319 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
320 return setSuccessor(idx, B);
324 //===----------------------------------------------------------------------===//
325 // ReturnInst Implementation
326 //===----------------------------------------------------------------------===//
328 void ReturnInst::init(Value *retVal) {
329 if (retVal && retVal->getType() != Type::VoidTy) {
330 assert(!isa<BasicBlock>(retVal) &&
331 "Cannot return basic block. Probably using the incorrect ctor");
333 RetVal.init(retVal, this);
337 unsigned ReturnInst::getNumSuccessorsV() const {
338 return getNumSuccessors();
341 // Out-of-line ReturnInst method, put here so the C++ compiler can choose to
342 // emit the vtable for the class in this translation unit.
343 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
344 assert(0 && "ReturnInst has no successors!");
347 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
348 assert(0 && "ReturnInst has no successors!");
354 //===----------------------------------------------------------------------===//
355 // UnwindInst Implementation
356 //===----------------------------------------------------------------------===//
358 unsigned UnwindInst::getNumSuccessorsV() const {
359 return getNumSuccessors();
362 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
363 assert(0 && "UnwindInst has no successors!");
366 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
367 assert(0 && "UnwindInst has no successors!");
372 //===----------------------------------------------------------------------===//
373 // UnreachableInst Implementation
374 //===----------------------------------------------------------------------===//
376 unsigned UnreachableInst::getNumSuccessorsV() const {
377 return getNumSuccessors();
380 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
381 assert(0 && "UnwindInst has no successors!");
384 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
385 assert(0 && "UnwindInst has no successors!");
390 //===----------------------------------------------------------------------===//
391 // BranchInst Implementation
392 //===----------------------------------------------------------------------===//
394 void BranchInst::AssertOK() {
396 assert(getCondition()->getType() == Type::BoolTy &&
397 "May only branch on boolean predicates!");
400 BranchInst::BranchInst(const BranchInst &BI) :
401 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
402 OperandList[0].init(BI.getOperand(0), this);
403 if (BI.getNumOperands() != 1) {
404 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
405 OperandList[1].init(BI.getOperand(1), this);
406 OperandList[2].init(BI.getOperand(2), this);
410 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
411 return getSuccessor(idx);
413 unsigned BranchInst::getNumSuccessorsV() const {
414 return getNumSuccessors();
416 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
417 setSuccessor(idx, B);
421 //===----------------------------------------------------------------------===//
422 // AllocationInst Implementation
423 //===----------------------------------------------------------------------===//
425 static Value *getAISize(Value *Amt) {
427 Amt = ConstantUInt::get(Type::UIntTy, 1);
429 assert(Amt->getType() == Type::UIntTy &&
430 "Malloc/Allocation array size != UIntTy!");
434 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
435 const std::string &Name,
436 Instruction *InsertBefore)
437 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
438 Name, InsertBefore) {
439 assert(Ty != Type::VoidTy && "Cannot allocate void!");
442 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
443 const std::string &Name,
444 BasicBlock *InsertAtEnd)
445 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
447 assert(Ty != Type::VoidTy && "Cannot allocate void!");
450 bool AllocationInst::isArrayAllocation() const {
451 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(getOperand(0)))
452 return CUI->getValue() != 1;
456 const Type *AllocationInst::getAllocatedType() const {
457 return getType()->getElementType();
460 AllocaInst::AllocaInst(const AllocaInst &AI)
461 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
462 Instruction::Alloca) {
465 MallocInst::MallocInst(const MallocInst &MI)
466 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
467 Instruction::Malloc) {
470 //===----------------------------------------------------------------------===//
471 // FreeInst Implementation
472 //===----------------------------------------------------------------------===//
474 void FreeInst::AssertOK() {
475 assert(isa<PointerType>(getOperand(0)->getType()) &&
476 "Can not free something of nonpointer type!");
479 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
480 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
484 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
485 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
490 //===----------------------------------------------------------------------===//
491 // LoadInst Implementation
492 //===----------------------------------------------------------------------===//
494 void LoadInst::AssertOK() {
495 assert(isa<PointerType>(getOperand(0)->getType()) &&
496 "Ptr must have pointer type.");
499 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
500 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
501 Load, Ptr, Name, InsertBef) {
506 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
507 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
508 Load, Ptr, Name, InsertAE) {
513 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
514 Instruction *InsertBef)
515 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
516 Load, Ptr, Name, InsertBef) {
517 setVolatile(isVolatile);
521 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
522 BasicBlock *InsertAE)
523 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
524 Load, Ptr, Name, InsertAE) {
525 setVolatile(isVolatile);
530 //===----------------------------------------------------------------------===//
531 // StoreInst Implementation
532 //===----------------------------------------------------------------------===//
534 void StoreInst::AssertOK() {
535 assert(isa<PointerType>(getOperand(1)->getType()) &&
536 "Ptr must have pointer type!");
537 assert(getOperand(0)->getType() ==
538 cast<PointerType>(getOperand(1)->getType())->getElementType()
539 && "Ptr must be a pointer to Val type!");
543 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
544 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
545 Ops[0].init(val, this);
546 Ops[1].init(addr, this);
551 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
552 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
553 Ops[0].init(val, this);
554 Ops[1].init(addr, this);
559 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
560 Instruction *InsertBefore)
561 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
562 Ops[0].init(val, this);
563 Ops[1].init(addr, this);
564 setVolatile(isVolatile);
568 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
569 BasicBlock *InsertAtEnd)
570 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
571 Ops[0].init(val, this);
572 Ops[1].init(addr, this);
573 setVolatile(isVolatile);
577 //===----------------------------------------------------------------------===//
578 // GetElementPtrInst Implementation
579 //===----------------------------------------------------------------------===//
581 // checkType - Simple wrapper function to give a better assertion failure
582 // message on bad indexes for a gep instruction.
584 static inline const Type *checkType(const Type *Ty) {
585 assert(Ty && "Invalid indices for type!");
589 void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) {
590 NumOperands = 1+Idx.size();
591 Use *OL = OperandList = new Use[NumOperands];
592 OL[0].init(Ptr, this);
594 for (unsigned i = 0, e = Idx.size(); i != e; ++i)
595 OL[i+1].init(Idx[i], this);
598 void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
600 Use *OL = OperandList = new Use[3];
601 OL[0].init(Ptr, this);
602 OL[1].init(Idx0, this);
603 OL[2].init(Idx1, this);
606 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
607 const std::string &Name, Instruction *InBe)
608 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
610 GetElementPtr, 0, 0, Name, InBe) {
614 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
615 const std::string &Name, BasicBlock *IAE)
616 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
618 GetElementPtr, 0, 0, Name, IAE) {
622 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
623 const std::string &Name, Instruction *InBe)
624 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
626 GetElementPtr, 0, 0, Name, InBe) {
627 init(Ptr, Idx0, Idx1);
630 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
631 const std::string &Name, BasicBlock *IAE)
632 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
634 GetElementPtr, 0, 0, Name, IAE) {
635 init(Ptr, Idx0, Idx1);
638 GetElementPtrInst::~GetElementPtrInst() {
639 delete[] OperandList;
642 // getIndexedType - Returns the type of the element that would be loaded with
643 // a load instruction with the specified parameters.
645 // A null type is returned if the indices are invalid for the specified
648 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
649 const std::vector<Value*> &Idx,
650 bool AllowCompositeLeaf) {
651 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
653 // Handle the special case of the empty set index set...
655 if (AllowCompositeLeaf ||
656 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
657 return cast<PointerType>(Ptr)->getElementType();
662 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
663 if (Idx.size() == CurIdx) {
664 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
665 return 0; // Can't load a whole structure or array!?!?
668 Value *Index = Idx[CurIdx++];
669 if (isa<PointerType>(CT) && CurIdx != 1)
670 return 0; // Can only index into pointer types at the first index!
671 if (!CT->indexValid(Index)) return 0;
672 Ptr = CT->getTypeAtIndex(Index);
674 // If the new type forwards to another type, then it is in the middle
675 // of being refined to another type (and hence, may have dropped all
676 // references to what it was using before). So, use the new forwarded
678 if (const Type * Ty = Ptr->getForwardedType()) {
682 return CurIdx == Idx.size() ? Ptr : 0;
685 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
686 Value *Idx0, Value *Idx1,
687 bool AllowCompositeLeaf) {
688 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
689 if (!PTy) return 0; // Type isn't a pointer type!
691 // Check the pointer index.
692 if (!PTy->indexValid(Idx0)) return 0;
694 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
695 if (!CT || !CT->indexValid(Idx1)) return 0;
697 const Type *ElTy = CT->getTypeAtIndex(Idx1);
698 if (AllowCompositeLeaf || ElTy->isFirstClassType())
703 //===----------------------------------------------------------------------===//
704 // BinaryOperator Class
705 //===----------------------------------------------------------------------===//
707 void BinaryOperator::init(BinaryOps iType)
709 Value *LHS = getOperand(0), *RHS = getOperand(1);
710 assert(LHS->getType() == RHS->getType() &&
711 "Binary operator operand types must match!");
717 assert(getType() == LHS->getType() &&
718 "Arithmetic operation should return same type as operands!");
719 assert((getType()->isInteger() ||
720 getType()->isFloatingPoint() ||
721 isa<PackedType>(getType()) ) &&
722 "Tried to create an arithmetic operation on a non-arithmetic type!");
726 assert(getType() == LHS->getType() &&
727 "Logical operation should return same type as operands!");
728 assert(getType()->isIntegral() &&
729 "Tried to create a logical operation on a non-integral type!");
731 case SetLT: case SetGT: case SetLE:
732 case SetGE: case SetEQ: case SetNE:
733 assert(getType() == Type::BoolTy && "Setcc must return bool!");
740 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
741 const std::string &Name,
742 Instruction *InsertBefore) {
743 assert(S1->getType() == S2->getType() &&
744 "Cannot create binary operator with two operands of differing type!");
746 // Binary comparison operators...
747 case SetLT: case SetGT: case SetLE:
748 case SetGE: case SetEQ: case SetNE:
749 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
752 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
756 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
757 const std::string &Name,
758 BasicBlock *InsertAtEnd) {
759 BinaryOperator *Res = create(Op, S1, S2, Name);
760 InsertAtEnd->getInstList().push_back(Res);
764 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
765 Instruction *InsertBefore) {
766 if (!Op->getType()->isFloatingPoint())
767 return new BinaryOperator(Instruction::Sub,
768 Constant::getNullValue(Op->getType()), Op,
769 Op->getType(), Name, InsertBefore);
771 return new BinaryOperator(Instruction::Sub,
772 ConstantFP::get(Op->getType(), -0.0), Op,
773 Op->getType(), Name, InsertBefore);
776 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
777 BasicBlock *InsertAtEnd) {
778 if (!Op->getType()->isFloatingPoint())
779 return new BinaryOperator(Instruction::Sub,
780 Constant::getNullValue(Op->getType()), Op,
781 Op->getType(), Name, InsertAtEnd);
783 return new BinaryOperator(Instruction::Sub,
784 ConstantFP::get(Op->getType(), -0.0), Op,
785 Op->getType(), Name, InsertAtEnd);
788 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
789 Instruction *InsertBefore) {
790 return new BinaryOperator(Instruction::Xor, Op,
791 ConstantIntegral::getAllOnesValue(Op->getType()),
792 Op->getType(), Name, InsertBefore);
795 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
796 BasicBlock *InsertAtEnd) {
797 return new BinaryOperator(Instruction::Xor, Op,
798 ConstantIntegral::getAllOnesValue(Op->getType()),
799 Op->getType(), Name, InsertAtEnd);
803 // isConstantAllOnes - Helper function for several functions below
804 static inline bool isConstantAllOnes(const Value *V) {
805 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
808 bool BinaryOperator::isNeg(const Value *V) {
809 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
810 if (Bop->getOpcode() == Instruction::Sub)
811 if (!V->getType()->isFloatingPoint())
812 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
814 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
818 bool BinaryOperator::isNot(const Value *V) {
819 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
820 return (Bop->getOpcode() == Instruction::Xor &&
821 (isConstantAllOnes(Bop->getOperand(1)) ||
822 isConstantAllOnes(Bop->getOperand(0))));
826 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
827 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
828 return Bop->getOperand(1);
831 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
832 return getNegArgument((BinaryOperator*)Bop);
835 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
836 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
837 Value *Op0 = Bop->getOperand(0);
838 Value *Op1 = Bop->getOperand(1);
839 if (isConstantAllOnes(Op0)) return Op1;
841 assert(isConstantAllOnes(Op1));
845 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
846 return getNotArgument((BinaryOperator*)Bop);
850 // swapOperands - Exchange the two operands to this instruction. This
851 // instruction is safe to use on any binary instruction and does not
852 // modify the semantics of the instruction. If the instruction is
853 // order dependent (SetLT f.e.) the opcode is changed.
855 bool BinaryOperator::swapOperands() {
857 ; // If the instruction is commutative, it is safe to swap the operands
858 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
859 /// FIXME: SetCC instructions shouldn't all have different opcodes.
860 setOpcode(SCI->getSwappedCondition());
862 return true; // Can't commute operands
864 std::swap(Ops[0], Ops[1]);
869 //===----------------------------------------------------------------------===//
871 //===----------------------------------------------------------------------===//
873 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
874 const std::string &Name, Instruction *InsertBefore)
875 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
877 // Make sure it's a valid type... getInverseCondition will assert out if not.
878 assert(getInverseCondition(Opcode));
881 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
882 const std::string &Name, BasicBlock *InsertAtEnd)
883 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
885 // Make sure it's a valid type... getInverseCondition will assert out if not.
886 assert(getInverseCondition(Opcode));
889 // getInverseCondition - Return the inverse of the current condition opcode.
890 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
892 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
895 assert(0 && "Unknown setcc opcode!");
896 case SetEQ: return SetNE;
897 case SetNE: return SetEQ;
898 case SetGT: return SetLE;
899 case SetLT: return SetGE;
900 case SetGE: return SetLT;
901 case SetLE: return SetGT;
905 // getSwappedCondition - Return the condition opcode that would be the result
906 // of exchanging the two operands of the setcc instruction without changing
907 // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
909 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
911 default: assert(0 && "Unknown setcc instruction!");
912 case SetEQ: case SetNE: return Opcode;
913 case SetGT: return SetLT;
914 case SetLT: return SetGT;
915 case SetGE: return SetLE;
916 case SetLE: return SetGE;
920 //===----------------------------------------------------------------------===//
921 // SwitchInst Implementation
922 //===----------------------------------------------------------------------===//
924 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
925 assert(Value && Default);
926 ReservedSpace = 2+NumCases*2;
928 OperandList = new Use[ReservedSpace];
930 OperandList[0].init(Value, this);
931 OperandList[1].init(Default, this);
934 SwitchInst::SwitchInst(const SwitchInst &SI)
935 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
936 SI.getNumOperands()) {
937 Use *OL = OperandList, *InOL = SI.OperandList;
938 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
939 OL[i].init(InOL[i], this);
940 OL[i+1].init(InOL[i+1], this);
944 SwitchInst::~SwitchInst() {
945 delete [] OperandList;
949 /// addCase - Add an entry to the switch instruction...
951 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
952 unsigned OpNo = NumOperands;
953 if (OpNo+2 > ReservedSpace)
954 resizeOperands(0); // Get more space!
955 // Initialize some new operands.
956 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
957 NumOperands = OpNo+2;
958 OperandList[OpNo].init(OnVal, this);
959 OperandList[OpNo+1].init(Dest, this);
962 /// removeCase - This method removes the specified successor from the switch
963 /// instruction. Note that this cannot be used to remove the default
964 /// destination (successor #0).
966 void SwitchInst::removeCase(unsigned idx) {
967 assert(idx != 0 && "Cannot remove the default case!");
968 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
970 unsigned NumOps = getNumOperands();
971 Use *OL = OperandList;
973 // Move everything after this operand down.
975 // FIXME: we could just swap with the end of the list, then erase. However,
976 // client might not expect this to happen. The code as it is thrashes the
977 // use/def lists, which is kinda lame.
978 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
983 // Nuke the last value.
985 OL[NumOps-2+1].set(0);
986 NumOperands = NumOps-2;
989 /// resizeOperands - resize operands - This adjusts the length of the operands
990 /// list according to the following behavior:
991 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
992 /// of operation. This grows the number of ops by 1.5 times.
993 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
994 /// 3. If NumOps == NumOperands, trim the reserved space.
996 void SwitchInst::resizeOperands(unsigned NumOps) {
998 NumOps = getNumOperands()/2*6;
999 } else if (NumOps*2 > NumOperands) {
1000 // No resize needed.
1001 if (ReservedSpace >= NumOps) return;
1002 } else if (NumOps == NumOperands) {
1003 if (ReservedSpace == NumOps) return;
1008 ReservedSpace = NumOps;
1009 Use *NewOps = new Use[NumOps];
1010 Use *OldOps = OperandList;
1011 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1012 NewOps[i].init(OldOps[i], this);
1016 OperandList = NewOps;
1020 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
1021 return getSuccessor(idx);
1023 unsigned SwitchInst::getNumSuccessorsV() const {
1024 return getNumSuccessors();
1026 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1027 setSuccessor(idx, B);
1031 // Define these methods here so vtables don't get emitted into every translation
1032 // unit that uses these classes.
1034 GetElementPtrInst *GetElementPtrInst::clone() const {
1035 return new GetElementPtrInst(*this);
1038 BinaryOperator *BinaryOperator::clone() const {
1039 return create(getOpcode(), Ops[0], Ops[1]);
1042 MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
1043 AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
1044 FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
1045 LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
1046 StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
1047 CastInst *CastInst::clone() const { return new CastInst(*this); }
1048 CallInst *CallInst::clone() const { return new CallInst(*this); }
1049 ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
1050 SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
1051 VANextInst *VANextInst::clone() const { return new VANextInst(*this); }
1052 VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
1053 PHINode *PHINode::clone() const { return new PHINode(*this); }
1054 ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
1055 BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
1056 SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
1057 InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
1058 UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
1059 UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}