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));
152 const Function *CallInst::getCalledFunction() const {
153 if (const Function *F = dyn_cast<Function>(Operands[0]))
157 Function *CallInst::getCalledFunction() {
158 if (Function *F = dyn_cast<Function>(Operands[0]))
164 //===----------------------------------------------------------------------===//
165 // InvokeInst Implementation
166 //===----------------------------------------------------------------------===//
168 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
169 const std::vector<Value*> &Params)
171 Operands.reserve(3+Params.size());
172 Operands.push_back(Use(Fn, this));
173 Operands.push_back(Use((Value*)IfNormal, this));
174 Operands.push_back(Use((Value*)IfException, this));
175 const FunctionType *MTy =
176 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
178 assert((Params.size() == MTy->getNumParams()) ||
179 (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
180 "Calling a function with bad signature");
182 for (unsigned i = 0; i < Params.size(); i++)
183 Operands.push_back(Use(Params[i], this));
186 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
187 BasicBlock *IfException,
188 const std::vector<Value*> &Params,
189 const std::string &Name, Instruction *InsertBefore)
190 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
191 ->getElementType())->getReturnType(),
192 Instruction::Invoke, Name, InsertBefore) {
193 init(Fn, IfNormal, IfException, Params);
196 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
197 BasicBlock *IfException,
198 const std::vector<Value*> &Params,
199 const std::string &Name, BasicBlock *InsertAtEnd)
200 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
201 ->getElementType())->getReturnType(),
202 Instruction::Invoke, Name, InsertAtEnd) {
203 init(Fn, IfNormal, IfException, Params);
206 InvokeInst::InvokeInst(const InvokeInst &CI)
207 : TerminatorInst(CI.getType(), Instruction::Invoke) {
208 Operands.reserve(CI.Operands.size());
209 for (unsigned i = 0; i < CI.Operands.size(); ++i)
210 Operands.push_back(Use(CI.Operands[i], this));
213 const Function *InvokeInst::getCalledFunction() const {
214 if (const Function *F = dyn_cast<Function>(Operands[0]))
218 Function *InvokeInst::getCalledFunction() {
219 if (Function *F = dyn_cast<Function>(Operands[0]))
224 // FIXME: Is this supposed to be here?
225 Function *CallSite::getCalledFunction() const {
226 Value *Callee = getCalledValue();
227 if (Function *F = dyn_cast<Function>(Callee))
232 //===----------------------------------------------------------------------===//
233 // ReturnInst Implementation
234 //===----------------------------------------------------------------------===//
236 // Out-of-line ReturnInst method, put here so the C++ compiler can choose to
237 // emit the vtable for the class in this translation unit.
238 void ReturnInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
239 assert(0 && "ReturnInst has no successors!");
242 //===----------------------------------------------------------------------===//
243 // UnwindInst Implementation
244 //===----------------------------------------------------------------------===//
246 // Likewise for UnwindInst
247 void UnwindInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
248 assert(0 && "UnwindInst has no successors!");
251 //===----------------------------------------------------------------------===//
252 // BranchInst Implementation
253 //===----------------------------------------------------------------------===//
255 void BranchInst::init(BasicBlock *IfTrue)
257 assert(IfTrue != 0 && "Branch destination may not be null!");
259 Operands.push_back(Use(IfTrue, this));
262 void BranchInst::init(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond)
264 assert(IfTrue && IfFalse && Cond &&
265 "Branch destinations and condition may not be null!");
266 assert(Cond && Cond->getType() == Type::BoolTy &&
267 "May only branch on boolean predicates!");
269 Operands.push_back(Use(IfTrue, this));
270 Operands.push_back(Use(IfFalse, this));
271 Operands.push_back(Use(Cond, this));
274 BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Instruction::Br) {
275 Operands.reserve(BI.Operands.size());
276 Operands.push_back(Use(BI.Operands[0], this));
277 if (BI.Operands.size() != 1) {
278 assert(BI.Operands.size() == 3 && "BR can have 1 or 3 operands!");
279 Operands.push_back(Use(BI.Operands[1], this));
280 Operands.push_back(Use(BI.Operands[2], this));
284 //===----------------------------------------------------------------------===//
285 // AllocationInst Implementation
286 //===----------------------------------------------------------------------===//
288 void AllocationInst::init(const Type *Ty, Value *ArraySize, unsigned iTy) {
289 assert(Ty != Type::VoidTy && "Cannot allocate void elements!");
290 // ArraySize defaults to 1.
291 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
294 assert(ArraySize->getType() == Type::UIntTy &&
295 "Malloc/Allocation array size != UIntTy!");
297 Operands.push_back(Use(ArraySize, this));
300 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
301 const std::string &Name,
302 Instruction *InsertBefore)
303 : Instruction(PointerType::get(Ty), iTy, Name, InsertBefore) {
304 init(Ty, ArraySize, iTy);
307 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
308 const std::string &Name,
309 BasicBlock *InsertAtEnd)
310 : Instruction(PointerType::get(Ty), iTy, Name, InsertAtEnd) {
311 init(Ty, ArraySize, iTy);
314 bool AllocationInst::isArrayAllocation() const {
315 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
318 const Type *AllocationInst::getAllocatedType() const {
319 return getType()->getElementType();
322 AllocaInst::AllocaInst(const AllocaInst &AI)
323 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
324 Instruction::Alloca) {
327 MallocInst::MallocInst(const MallocInst &MI)
328 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
329 Instruction::Malloc) {
332 //===----------------------------------------------------------------------===//
333 // FreeInst Implementation
334 //===----------------------------------------------------------------------===//
336 void FreeInst::init(Value *Ptr)
338 assert(Ptr && isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
340 Operands.push_back(Use(Ptr, this));
343 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
344 : Instruction(Type::VoidTy, Free, "", InsertBefore) {
348 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
349 : Instruction(Type::VoidTy, Free, "", InsertAtEnd) {
354 //===----------------------------------------------------------------------===//
355 // LoadInst Implementation
356 //===----------------------------------------------------------------------===//
358 void LoadInst::init(Value *Ptr) {
359 assert(Ptr && isa<PointerType>(Ptr->getType()) &&
360 "Ptr must have pointer type.");
362 Operands.push_back(Use(Ptr, this));
365 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
366 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
367 Load, Name, InsertBef), Volatile(false) {
371 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
372 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
373 Load, Name, InsertAE), Volatile(false) {
377 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
378 Instruction *InsertBef)
379 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
380 Load, Name, InsertBef), Volatile(isVolatile) {
384 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
385 BasicBlock *InsertAE)
386 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
387 Load, Name, InsertAE), Volatile(isVolatile) {
392 //===----------------------------------------------------------------------===//
393 // StoreInst Implementation
394 //===----------------------------------------------------------------------===//
396 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
397 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
401 StoreInst::StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd)
402 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(false) {
406 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
407 Instruction *InsertBefore)
408 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
412 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
413 BasicBlock *InsertAtEnd)
414 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(isVolatile) {
418 void StoreInst::init(Value *Val, Value *Ptr) {
419 assert(isa<PointerType>(Ptr->getType()) && "Ptr must have pointer type!");
420 assert(Val->getType() == cast<PointerType>(Ptr->getType())->getElementType()
421 && "Ptr must be a pointer to Val type!");
424 Operands.push_back(Use(Val, this));
425 Operands.push_back(Use(Ptr, this));
428 //===----------------------------------------------------------------------===//
429 // GetElementPtrInst Implementation
430 //===----------------------------------------------------------------------===//
432 // checkType - Simple wrapper function to give a better assertion failure
433 // message on bad indexes for a gep instruction.
435 static inline const Type *checkType(const Type *Ty) {
436 assert(Ty && "Invalid indices for type!");
440 void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx)
442 Operands.reserve(1+Idx.size());
443 Operands.push_back(Use(Ptr, this));
445 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
446 Operands.push_back(Use(Idx[i], this));
449 void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
451 Operands.push_back(Use(Ptr, this));
452 Operands.push_back(Use(Idx0, this));
453 Operands.push_back(Use(Idx1, this));
456 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
457 const std::string &Name, Instruction *InBe)
458 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
460 GetElementPtr, Name, InBe) {
464 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
465 const std::string &Name, BasicBlock *IAE)
466 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
468 GetElementPtr, Name, IAE) {
472 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
473 const std::string &Name, Instruction *InBe)
474 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
476 GetElementPtr, Name, InBe) {
477 init(Ptr, Idx0, Idx1);
480 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
481 const std::string &Name, BasicBlock *IAE)
482 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
484 GetElementPtr, Name, IAE) {
485 init(Ptr, Idx0, Idx1);
488 // getIndexedType - Returns the type of the element that would be loaded with
489 // a load instruction with the specified parameters.
491 // A null type is returned if the indices are invalid for the specified
494 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
495 const std::vector<Value*> &Idx,
496 bool AllowCompositeLeaf) {
497 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
499 // Handle the special case of the empty set index set...
501 if (AllowCompositeLeaf ||
502 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
503 return cast<PointerType>(Ptr)->getElementType();
508 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
509 if (Idx.size() == CurIdx) {
510 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
511 return 0; // Can't load a whole structure or array!?!?
514 Value *Index = Idx[CurIdx++];
515 if (isa<PointerType>(CT) && CurIdx != 1)
516 return 0; // Can only index into pointer types at the first index!
517 if (!CT->indexValid(Index)) return 0;
518 Ptr = CT->getTypeAtIndex(Index);
520 // If the new type forwards to another type, then it is in the middle
521 // of being refined to another type (and hence, may have dropped all
522 // references to what it was using before). So, use the new forwarded
524 if (const Type * Ty = Ptr->getForwardedType()) {
528 return CurIdx == Idx.size() ? Ptr : 0;
531 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
532 Value *Idx0, Value *Idx1,
533 bool AllowCompositeLeaf) {
534 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
535 if (!PTy) return 0; // Type isn't a pointer type!
537 // Check the pointer index.
538 if (!PTy->indexValid(Idx0)) return 0;
540 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
541 if (!CT || !CT->indexValid(Idx1)) return 0;
543 const Type *ElTy = CT->getTypeAtIndex(Idx1);
544 if (AllowCompositeLeaf || ElTy->isFirstClassType())
549 //===----------------------------------------------------------------------===//
550 // BinaryOperator Class
551 //===----------------------------------------------------------------------===//
553 void BinaryOperator::init(BinaryOps iType, Value *S1, Value *S2)
556 Operands.push_back(Use(S1, this));
557 Operands.push_back(Use(S2, this));
558 assert(S1 && S2 && S1->getType() == S2->getType());
565 assert(getType() == S1->getType() &&
566 "Arithmetic operation should return same type as operands!");
567 assert((getType()->isInteger() ||
568 getType()->isFloatingPoint() ||
569 isa<PackedType>(getType()) ) &&
570 "Tried to create an arithmetic operation on a non-arithmetic type!");
574 assert(getType() == S1->getType() &&
575 "Logical operation should return same type as operands!");
576 assert(getType()->isIntegral() &&
577 "Tried to create an logical operation on a non-integral type!");
579 case SetLT: case SetGT: case SetLE:
580 case SetGE: case SetEQ: case SetNE:
581 assert(getType() == Type::BoolTy && "Setcc must return bool!");
588 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
589 const std::string &Name,
590 Instruction *InsertBefore) {
591 assert(S1->getType() == S2->getType() &&
592 "Cannot create binary operator with two operands of differing type!");
594 // Binary comparison operators...
595 case SetLT: case SetGT: case SetLE:
596 case SetGE: case SetEQ: case SetNE:
597 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
600 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
604 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
605 const std::string &Name,
606 BasicBlock *InsertAtEnd) {
607 BinaryOperator *Res = create(Op, S1, S2, Name);
608 InsertAtEnd->getInstList().push_back(Res);
612 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
613 Instruction *InsertBefore) {
614 if (!Op->getType()->isFloatingPoint())
615 return new BinaryOperator(Instruction::Sub,
616 Constant::getNullValue(Op->getType()), Op,
617 Op->getType(), Name, InsertBefore);
619 return new BinaryOperator(Instruction::Sub,
620 ConstantFP::get(Op->getType(), -0.0), Op,
621 Op->getType(), Name, InsertBefore);
624 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
625 BasicBlock *InsertAtEnd) {
626 if (!Op->getType()->isFloatingPoint())
627 return new BinaryOperator(Instruction::Sub,
628 Constant::getNullValue(Op->getType()), Op,
629 Op->getType(), Name, InsertAtEnd);
631 return new BinaryOperator(Instruction::Sub,
632 ConstantFP::get(Op->getType(), -0.0), Op,
633 Op->getType(), Name, InsertAtEnd);
636 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
637 Instruction *InsertBefore) {
638 return new BinaryOperator(Instruction::Xor, Op,
639 ConstantIntegral::getAllOnesValue(Op->getType()),
640 Op->getType(), Name, InsertBefore);
643 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
644 BasicBlock *InsertAtEnd) {
645 return new BinaryOperator(Instruction::Xor, Op,
646 ConstantIntegral::getAllOnesValue(Op->getType()),
647 Op->getType(), Name, InsertAtEnd);
651 // isConstantAllOnes - Helper function for several functions below
652 static inline bool isConstantAllOnes(const Value *V) {
653 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
656 bool BinaryOperator::isNeg(const Value *V) {
657 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
658 if (Bop->getOpcode() == Instruction::Sub)
659 if (!V->getType()->isFloatingPoint())
660 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
662 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
666 bool BinaryOperator::isNot(const Value *V) {
667 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
668 return (Bop->getOpcode() == Instruction::Xor &&
669 (isConstantAllOnes(Bop->getOperand(1)) ||
670 isConstantAllOnes(Bop->getOperand(0))));
674 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
675 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
676 return Bop->getOperand(1);
679 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
680 return getNegArgument((BinaryOperator*)Bop);
683 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
684 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
685 Value *Op0 = Bop->getOperand(0);
686 Value *Op1 = Bop->getOperand(1);
687 if (isConstantAllOnes(Op0)) return Op1;
689 assert(isConstantAllOnes(Op1));
693 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
694 return getNotArgument((BinaryOperator*)Bop);
698 // swapOperands - Exchange the two operands to this instruction. This
699 // instruction is safe to use on any binary instruction and does not
700 // modify the semantics of the instruction. If the instruction is
701 // order dependent (SetLT f.e.) the opcode is changed.
703 bool BinaryOperator::swapOperands() {
705 ; // If the instruction is commutative, it is safe to swap the operands
706 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
707 /// FIXME: SetCC instructions shouldn't all have different opcodes.
708 setOpcode(SCI->getSwappedCondition());
710 return true; // Can't commute operands
712 std::swap(Operands[0], Operands[1]);
717 //===----------------------------------------------------------------------===//
719 //===----------------------------------------------------------------------===//
721 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
722 const std::string &Name, Instruction *InsertBefore)
723 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
725 // Make sure it's a valid type... getInverseCondition will assert out if not.
726 assert(getInverseCondition(Opcode));
729 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
730 const std::string &Name, BasicBlock *InsertAtEnd)
731 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
733 // Make sure it's a valid type... getInverseCondition will assert out if not.
734 assert(getInverseCondition(Opcode));
737 // getInverseCondition - Return the inverse of the current condition opcode.
738 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
740 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
743 assert(0 && "Unknown setcc opcode!");
744 case SetEQ: return SetNE;
745 case SetNE: return SetEQ;
746 case SetGT: return SetLE;
747 case SetLT: return SetGE;
748 case SetGE: return SetLT;
749 case SetLE: return SetGT;
753 // getSwappedCondition - Return the condition opcode that would be the result
754 // of exchanging the two operands of the setcc instruction without changing
755 // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
757 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
759 default: assert(0 && "Unknown setcc instruction!");
760 case SetEQ: case SetNE: return Opcode;
761 case SetGT: return SetLT;
762 case SetLT: return SetGT;
763 case SetGE: return SetLE;
764 case SetLE: return SetGE;
768 //===----------------------------------------------------------------------===//
769 // SwitchInst Implementation
770 //===----------------------------------------------------------------------===//
772 void SwitchInst::init(Value *Value, BasicBlock *Default)
774 assert(Value && Default);
775 Operands.push_back(Use(Value, this));
776 Operands.push_back(Use(Default, this));
779 SwitchInst::SwitchInst(const SwitchInst &SI)
780 : TerminatorInst(Instruction::Switch) {
781 Operands.reserve(SI.Operands.size());
783 for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
784 Operands.push_back(Use(SI.Operands[i], this));
785 Operands.push_back(Use(SI.Operands[i+1], this));
789 /// addCase - Add an entry to the switch instruction...
791 void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) {
792 Operands.push_back(Use((Value*)OnVal, this));
793 Operands.push_back(Use((Value*)Dest, this));
796 /// removeCase - This method removes the specified successor from the switch
797 /// instruction. Note that this cannot be used to remove the default
798 /// destination (successor #0).
800 void SwitchInst::removeCase(unsigned idx) {
801 assert(idx != 0 && "Cannot remove the default case!");
802 assert(idx*2 < Operands.size() && "Successor index out of range!!!");
803 Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2);