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 the LLVM instructions...
12 //===----------------------------------------------------------------------===//
14 #include "llvm/BasicBlock.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Support/CallSite.h"
22 //===----------------------------------------------------------------------===//
23 // CallInst Implementation
24 //===----------------------------------------------------------------------===//
26 void CallInst::init(Value *Func, const std::vector<Value*> &Params)
28 Operands.reserve(1+Params.size());
29 Operands.push_back(Use(Func, this));
31 const FunctionType *FTy =
32 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
34 assert((Params.size() == FTy->getNumParams() ||
35 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
36 "Calling a function with bad signature");
37 for (unsigned i = 0; i != Params.size(); i++)
38 Operands.push_back(Use(Params[i], this));
41 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2)
44 Operands.push_back(Use(Func, this));
46 const FunctionType *MTy =
47 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
49 assert((MTy->getNumParams() == 2 ||
50 (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
51 "Calling a function with bad signature");
52 Operands.push_back(Use(Actual1, this));
53 Operands.push_back(Use(Actual2, this));
56 void CallInst::init(Value *Func, Value *Actual)
59 Operands.push_back(Use(Func, this));
61 const FunctionType *MTy =
62 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
64 assert((MTy->getNumParams() == 1 ||
65 (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
66 "Calling a function with bad signature");
67 Operands.push_back(Use(Actual, this));
70 void CallInst::init(Value *Func)
73 Operands.push_back(Use(Func, this));
75 const FunctionType *MTy =
76 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
78 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
81 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
82 const std::string &Name, Instruction *InsertBefore)
83 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
84 ->getElementType())->getReturnType(),
85 Instruction::Call, Name, InsertBefore) {
89 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
90 const std::string &Name, BasicBlock *InsertAtEnd)
91 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
92 ->getElementType())->getReturnType(),
93 Instruction::Call, Name, InsertAtEnd) {
97 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
98 const std::string &Name, Instruction *InsertBefore)
99 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
100 ->getElementType())->getReturnType(),
101 Instruction::Call, Name, InsertBefore) {
102 init(Func, Actual1, Actual2);
105 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
106 const std::string &Name, BasicBlock *InsertAtEnd)
107 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
108 ->getElementType())->getReturnType(),
109 Instruction::Call, Name, InsertAtEnd) {
110 init(Func, Actual1, Actual2);
113 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
114 Instruction *InsertBefore)
115 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
116 ->getElementType())->getReturnType(),
117 Instruction::Call, Name, InsertBefore) {
121 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
122 BasicBlock *InsertAtEnd)
123 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
124 ->getElementType())->getReturnType(),
125 Instruction::Call, Name, InsertAtEnd) {
129 CallInst::CallInst(Value *Func, const std::string &Name,
130 Instruction *InsertBefore)
131 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
132 ->getElementType())->getReturnType(),
133 Instruction::Call, Name, InsertBefore) {
137 CallInst::CallInst(Value *Func, const std::string &Name,
138 BasicBlock *InsertAtEnd)
139 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
140 ->getElementType())->getReturnType(),
141 Instruction::Call, Name, InsertAtEnd) {
145 CallInst::CallInst(const CallInst &CI)
146 : Instruction(CI.getType(), Instruction::Call) {
147 Operands.reserve(CI.Operands.size());
148 for (unsigned i = 0; i < CI.Operands.size(); ++i)
149 Operands.push_back(Use(CI.Operands[i], this));
153 //===----------------------------------------------------------------------===//
154 // InvokeInst Implementation
155 //===----------------------------------------------------------------------===//
157 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
158 const std::vector<Value*> &Params)
160 Operands.reserve(3+Params.size());
161 Operands.push_back(Use(Fn, this));
162 Operands.push_back(Use((Value*)IfNormal, this));
163 Operands.push_back(Use((Value*)IfException, this));
164 const FunctionType *MTy =
165 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
167 assert((Params.size() == MTy->getNumParams()) ||
168 (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
169 "Calling a function with bad signature");
171 for (unsigned i = 0; i < Params.size(); i++)
172 Operands.push_back(Use(Params[i], this));
175 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
176 BasicBlock *IfException,
177 const std::vector<Value*> &Params,
178 const std::string &Name, Instruction *InsertBefore)
179 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
180 ->getElementType())->getReturnType(),
181 Instruction::Invoke, Name, InsertBefore) {
182 init(Fn, IfNormal, IfException, Params);
185 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
186 BasicBlock *IfException,
187 const std::vector<Value*> &Params,
188 const std::string &Name, BasicBlock *InsertAtEnd)
189 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
190 ->getElementType())->getReturnType(),
191 Instruction::Invoke, Name, InsertAtEnd) {
192 init(Fn, IfNormal, IfException, Params);
195 InvokeInst::InvokeInst(const InvokeInst &CI)
196 : TerminatorInst(CI.getType(), Instruction::Invoke) {
197 Operands.reserve(CI.Operands.size());
198 for (unsigned i = 0; i < CI.Operands.size(); ++i)
199 Operands.push_back(Use(CI.Operands[i], this));
202 //===----------------------------------------------------------------------===//
203 // ReturnInst Implementation
204 //===----------------------------------------------------------------------===//
206 void ReturnInst::init(Value* RetVal) {
207 if (RetVal && RetVal->getType() != Type::VoidTy) {
208 assert(!isa<BasicBlock>(RetVal) &&
209 "Cannot return basic block. Probably using the incorrect ctor");
211 Operands.push_back(Use(RetVal, this));
215 // Out-of-line ReturnInst method, put here so the C++ compiler can choose to
216 // emit the vtable for the class in this translation unit.
217 void ReturnInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
218 assert(0 && "ReturnInst has no successors!");
221 //===----------------------------------------------------------------------===//
222 // UnwindInst Implementation
223 //===----------------------------------------------------------------------===//
225 // Likewise for UnwindInst
226 void UnwindInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
227 assert(0 && "UnwindInst has no successors!");
230 //===----------------------------------------------------------------------===//
231 // UnreachableInst Implementation
232 //===----------------------------------------------------------------------===//
234 void UnreachableInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
235 assert(0 && "UnreachableInst has no successors!");
238 //===----------------------------------------------------------------------===//
239 // BranchInst Implementation
240 //===----------------------------------------------------------------------===//
242 void BranchInst::init(BasicBlock *IfTrue)
244 assert(IfTrue != 0 && "Branch destination may not be null!");
246 Operands.push_back(Use(IfTrue, this));
249 void BranchInst::init(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond)
251 assert(IfTrue && IfFalse && Cond &&
252 "Branch destinations and condition may not be null!");
253 assert(Cond && Cond->getType() == Type::BoolTy &&
254 "May only branch on boolean predicates!");
256 Operands.push_back(Use(IfTrue, this));
257 Operands.push_back(Use(IfFalse, this));
258 Operands.push_back(Use(Cond, this));
261 BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Instruction::Br) {
262 Operands.reserve(BI.Operands.size());
263 Operands.push_back(Use(BI.Operands[0], this));
264 if (BI.Operands.size() != 1) {
265 assert(BI.Operands.size() == 3 && "BR can have 1 or 3 operands!");
266 Operands.push_back(Use(BI.Operands[1], this));
267 Operands.push_back(Use(BI.Operands[2], this));
271 //===----------------------------------------------------------------------===//
272 // AllocationInst Implementation
273 //===----------------------------------------------------------------------===//
275 void AllocationInst::init(const Type *Ty, Value *ArraySize, unsigned iTy) {
276 assert(Ty != Type::VoidTy && "Cannot allocate void elements!");
277 // ArraySize defaults to 1.
278 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
281 assert(ArraySize->getType() == Type::UIntTy &&
282 "Malloc/Allocation array size != UIntTy!");
284 Operands.push_back(Use(ArraySize, this));
287 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
288 const std::string &Name,
289 Instruction *InsertBefore)
290 : Instruction(PointerType::get(Ty), iTy, Name, InsertBefore) {
291 init(Ty, ArraySize, iTy);
294 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
295 const std::string &Name,
296 BasicBlock *InsertAtEnd)
297 : Instruction(PointerType::get(Ty), iTy, Name, InsertAtEnd) {
298 init(Ty, ArraySize, iTy);
301 bool AllocationInst::isArrayAllocation() const {
302 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
305 const Type *AllocationInst::getAllocatedType() const {
306 return getType()->getElementType();
309 AllocaInst::AllocaInst(const AllocaInst &AI)
310 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
311 Instruction::Alloca) {
314 MallocInst::MallocInst(const MallocInst &MI)
315 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
316 Instruction::Malloc) {
319 //===----------------------------------------------------------------------===//
320 // FreeInst Implementation
321 //===----------------------------------------------------------------------===//
323 void FreeInst::init(Value *Ptr)
325 assert(Ptr && isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
327 Operands.push_back(Use(Ptr, this));
330 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
331 : Instruction(Type::VoidTy, Free, "", InsertBefore) {
335 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
336 : Instruction(Type::VoidTy, Free, "", InsertAtEnd) {
341 //===----------------------------------------------------------------------===//
342 // LoadInst Implementation
343 //===----------------------------------------------------------------------===//
345 void LoadInst::init(Value *Ptr) {
346 assert(Ptr && isa<PointerType>(Ptr->getType()) &&
347 "Ptr must have pointer type.");
349 Operands.push_back(Use(Ptr, this));
352 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
353 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
354 Load, Name, InsertBef), Volatile(false) {
358 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
359 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
360 Load, Name, InsertAE), Volatile(false) {
364 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
365 Instruction *InsertBef)
366 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
367 Load, Name, InsertBef), Volatile(isVolatile) {
371 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
372 BasicBlock *InsertAE)
373 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
374 Load, Name, InsertAE), Volatile(isVolatile) {
379 //===----------------------------------------------------------------------===//
380 // StoreInst Implementation
381 //===----------------------------------------------------------------------===//
383 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
384 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
388 StoreInst::StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd)
389 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(false) {
393 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
394 Instruction *InsertBefore)
395 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
399 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
400 BasicBlock *InsertAtEnd)
401 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(isVolatile) {
405 void StoreInst::init(Value *Val, Value *Ptr) {
406 assert(isa<PointerType>(Ptr->getType()) && "Ptr must have pointer type!");
407 assert(Val->getType() == cast<PointerType>(Ptr->getType())->getElementType()
408 && "Ptr must be a pointer to Val type!");
411 Operands.push_back(Use(Val, this));
412 Operands.push_back(Use(Ptr, this));
415 //===----------------------------------------------------------------------===//
416 // GetElementPtrInst Implementation
417 //===----------------------------------------------------------------------===//
419 // checkType - Simple wrapper function to give a better assertion failure
420 // message on bad indexes for a gep instruction.
422 static inline const Type *checkType(const Type *Ty) {
423 assert(Ty && "Invalid indices for type!");
427 void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx)
429 Operands.reserve(1+Idx.size());
430 Operands.push_back(Use(Ptr, this));
432 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
433 Operands.push_back(Use(Idx[i], this));
436 void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
438 Operands.push_back(Use(Ptr, this));
439 Operands.push_back(Use(Idx0, this));
440 Operands.push_back(Use(Idx1, this));
443 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
444 const std::string &Name, Instruction *InBe)
445 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
447 GetElementPtr, Name, InBe) {
451 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
452 const std::string &Name, BasicBlock *IAE)
453 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
455 GetElementPtr, Name, IAE) {
459 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
460 const std::string &Name, Instruction *InBe)
461 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
463 GetElementPtr, Name, InBe) {
464 init(Ptr, Idx0, Idx1);
467 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
468 const std::string &Name, BasicBlock *IAE)
469 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
471 GetElementPtr, Name, IAE) {
472 init(Ptr, Idx0, Idx1);
475 // getIndexedType - Returns the type of the element that would be loaded with
476 // a load instruction with the specified parameters.
478 // A null type is returned if the indices are invalid for the specified
481 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
482 const std::vector<Value*> &Idx,
483 bool AllowCompositeLeaf) {
484 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
486 // Handle the special case of the empty set index set...
488 if (AllowCompositeLeaf ||
489 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
490 return cast<PointerType>(Ptr)->getElementType();
495 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
496 if (Idx.size() == CurIdx) {
497 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
498 return 0; // Can't load a whole structure or array!?!?
501 Value *Index = Idx[CurIdx++];
502 if (isa<PointerType>(CT) && CurIdx != 1)
503 return 0; // Can only index into pointer types at the first index!
504 if (!CT->indexValid(Index)) return 0;
505 Ptr = CT->getTypeAtIndex(Index);
507 // If the new type forwards to another type, then it is in the middle
508 // of being refined to another type (and hence, may have dropped all
509 // references to what it was using before). So, use the new forwarded
511 if (const Type * Ty = Ptr->getForwardedType()) {
515 return CurIdx == Idx.size() ? Ptr : 0;
518 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
519 Value *Idx0, Value *Idx1,
520 bool AllowCompositeLeaf) {
521 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
522 if (!PTy) return 0; // Type isn't a pointer type!
524 // Check the pointer index.
525 if (!PTy->indexValid(Idx0)) return 0;
527 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
528 if (!CT || !CT->indexValid(Idx1)) return 0;
530 const Type *ElTy = CT->getTypeAtIndex(Idx1);
531 if (AllowCompositeLeaf || ElTy->isFirstClassType())
536 //===----------------------------------------------------------------------===//
537 // BinaryOperator Class
538 //===----------------------------------------------------------------------===//
540 void BinaryOperator::init(BinaryOps iType, Value *S1, Value *S2)
543 Operands.push_back(Use(S1, this));
544 Operands.push_back(Use(S2, this));
545 assert(S1 && S2 && S1->getType() == S2->getType());
552 assert(getType() == S1->getType() &&
553 "Arithmetic operation should return same type as operands!");
554 assert((getType()->isInteger() ||
555 getType()->isFloatingPoint() ||
556 isa<PackedType>(getType()) ) &&
557 "Tried to create an arithmetic operation on a non-arithmetic type!");
561 assert(getType() == S1->getType() &&
562 "Logical operation should return same type as operands!");
563 assert(getType()->isIntegral() &&
564 "Tried to create a logical operation on a non-integral type!");
566 case SetLT: case SetGT: case SetLE:
567 case SetGE: case SetEQ: case SetNE:
568 assert(getType() == Type::BoolTy && "Setcc must return bool!");
575 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
576 const std::string &Name,
577 Instruction *InsertBefore) {
578 assert(S1->getType() == S2->getType() &&
579 "Cannot create binary operator with two operands of differing type!");
581 // Binary comparison operators...
582 case SetLT: case SetGT: case SetLE:
583 case SetGE: case SetEQ: case SetNE:
584 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
587 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
591 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
592 const std::string &Name,
593 BasicBlock *InsertAtEnd) {
594 BinaryOperator *Res = create(Op, S1, S2, Name);
595 InsertAtEnd->getInstList().push_back(Res);
599 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
600 Instruction *InsertBefore) {
601 if (!Op->getType()->isFloatingPoint())
602 return new BinaryOperator(Instruction::Sub,
603 Constant::getNullValue(Op->getType()), Op,
604 Op->getType(), Name, InsertBefore);
606 return new BinaryOperator(Instruction::Sub,
607 ConstantFP::get(Op->getType(), -0.0), Op,
608 Op->getType(), Name, InsertBefore);
611 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
612 BasicBlock *InsertAtEnd) {
613 if (!Op->getType()->isFloatingPoint())
614 return new BinaryOperator(Instruction::Sub,
615 Constant::getNullValue(Op->getType()), Op,
616 Op->getType(), Name, InsertAtEnd);
618 return new BinaryOperator(Instruction::Sub,
619 ConstantFP::get(Op->getType(), -0.0), Op,
620 Op->getType(), Name, InsertAtEnd);
623 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
624 Instruction *InsertBefore) {
625 return new BinaryOperator(Instruction::Xor, Op,
626 ConstantIntegral::getAllOnesValue(Op->getType()),
627 Op->getType(), Name, InsertBefore);
630 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
631 BasicBlock *InsertAtEnd) {
632 return new BinaryOperator(Instruction::Xor, Op,
633 ConstantIntegral::getAllOnesValue(Op->getType()),
634 Op->getType(), Name, InsertAtEnd);
638 // isConstantAllOnes - Helper function for several functions below
639 static inline bool isConstantAllOnes(const Value *V) {
640 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
643 bool BinaryOperator::isNeg(const Value *V) {
644 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
645 if (Bop->getOpcode() == Instruction::Sub)
646 if (!V->getType()->isFloatingPoint())
647 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
649 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
653 bool BinaryOperator::isNot(const Value *V) {
654 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
655 return (Bop->getOpcode() == Instruction::Xor &&
656 (isConstantAllOnes(Bop->getOperand(1)) ||
657 isConstantAllOnes(Bop->getOperand(0))));
661 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
662 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
663 return Bop->getOperand(1);
666 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
667 return getNegArgument((BinaryOperator*)Bop);
670 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
671 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
672 Value *Op0 = Bop->getOperand(0);
673 Value *Op1 = Bop->getOperand(1);
674 if (isConstantAllOnes(Op0)) return Op1;
676 assert(isConstantAllOnes(Op1));
680 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
681 return getNotArgument((BinaryOperator*)Bop);
685 // swapOperands - Exchange the two operands to this instruction. This
686 // instruction is safe to use on any binary instruction and does not
687 // modify the semantics of the instruction. If the instruction is
688 // order dependent (SetLT f.e.) the opcode is changed.
690 bool BinaryOperator::swapOperands() {
692 ; // If the instruction is commutative, it is safe to swap the operands
693 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
694 /// FIXME: SetCC instructions shouldn't all have different opcodes.
695 setOpcode(SCI->getSwappedCondition());
697 return true; // Can't commute operands
699 std::swap(Operands[0], Operands[1]);
704 //===----------------------------------------------------------------------===//
706 //===----------------------------------------------------------------------===//
708 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
709 const std::string &Name, Instruction *InsertBefore)
710 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
712 // Make sure it's a valid type... getInverseCondition will assert out if not.
713 assert(getInverseCondition(Opcode));
716 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
717 const std::string &Name, BasicBlock *InsertAtEnd)
718 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
720 // Make sure it's a valid type... getInverseCondition will assert out if not.
721 assert(getInverseCondition(Opcode));
724 // getInverseCondition - Return the inverse of the current condition opcode.
725 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
727 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
730 assert(0 && "Unknown setcc opcode!");
731 case SetEQ: return SetNE;
732 case SetNE: return SetEQ;
733 case SetGT: return SetLE;
734 case SetLT: return SetGE;
735 case SetGE: return SetLT;
736 case SetLE: return SetGT;
740 // getSwappedCondition - Return the condition opcode that would be the result
741 // of exchanging the two operands of the setcc instruction without changing
742 // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
744 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
746 default: assert(0 && "Unknown setcc instruction!");
747 case SetEQ: case SetNE: return Opcode;
748 case SetGT: return SetLT;
749 case SetLT: return SetGT;
750 case SetGE: return SetLE;
751 case SetLE: return SetGE;
755 //===----------------------------------------------------------------------===//
756 // SwitchInst Implementation
757 //===----------------------------------------------------------------------===//
759 void SwitchInst::init(Value *Value, BasicBlock *Default)
761 assert(Value && Default);
762 Operands.push_back(Use(Value, this));
763 Operands.push_back(Use(Default, this));
766 SwitchInst::SwitchInst(const SwitchInst &SI)
767 : TerminatorInst(Instruction::Switch) {
768 Operands.reserve(SI.Operands.size());
770 for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
771 Operands.push_back(Use(SI.Operands[i], this));
772 Operands.push_back(Use(SI.Operands[i+1], this));
776 /// addCase - Add an entry to the switch instruction...
778 void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) {
779 Operands.push_back(Use((Value*)OnVal, this));
780 Operands.push_back(Use((Value*)Dest, this));
783 /// removeCase - This method removes the specified successor from the switch
784 /// instruction. Note that this cannot be used to remove the default
785 /// destination (successor #0).
787 void SwitchInst::removeCase(unsigned idx) {
788 assert(idx != 0 && "Cannot remove the default case!");
789 assert(idx*2 < Operands.size() && "Successor index out of range!!!");
790 Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2);
794 // Define these methods here so vtables don't get emitted into every translation
795 // unit that uses these classes.
797 GetElementPtrInst *GetElementPtrInst::clone() const {
798 return new GetElementPtrInst(*this);
801 BinaryOperator *BinaryOperator::clone() const {
802 return create(getOpcode(), Operands[0], Operands[1]);
805 MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
806 AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
807 FreeInst *FreeInst::clone() const { return new FreeInst(Operands[0]); }
808 LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
809 StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
810 CastInst *CastInst::clone() const { return new CastInst(*this); }
811 CallInst *CallInst::clone() const { return new CallInst(*this); }
812 ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
813 SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
814 VANextInst *VANextInst::clone() const { return new VANextInst(*this); }
815 VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
816 PHINode *PHINode::clone() const { return new PHINode(*this); }
817 ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
818 BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
819 SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
820 InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
821 UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
822 UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}