1 //===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Support/CallSite.h"
20 #include "llvm/Support/ConstantRange.h"
21 #include "llvm/Support/MathExtras.h"
24 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
28 CallSite::CallSite(Instruction *C) {
29 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
32 unsigned CallSite::getCallingConv() const {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 return CI->getCallingConv();
36 return cast<InvokeInst>(I)->getCallingConv();
38 void CallSite::setCallingConv(unsigned CC) {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 CI->setCallingConv(CC);
42 cast<InvokeInst>(I)->setCallingConv(CC);
44 const PAListPtr &CallSite::getParamAttrs() const {
45 if (CallInst *CI = dyn_cast<CallInst>(I))
46 return CI->getParamAttrs();
48 return cast<InvokeInst>(I)->getParamAttrs();
50 void CallSite::setParamAttrs(const PAListPtr &PAL) {
51 if (CallInst *CI = dyn_cast<CallInst>(I))
52 CI->setParamAttrs(PAL);
54 cast<InvokeInst>(I)->setParamAttrs(PAL);
56 bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
57 if (CallInst *CI = dyn_cast<CallInst>(I))
58 return CI->paramHasAttr(i, attr);
60 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
62 uint16_t CallSite::getParamAlignment(uint16_t i) const {
63 if (CallInst *CI = dyn_cast<CallInst>(I))
64 return CI->getParamAlignment(i);
66 return cast<InvokeInst>(I)->getParamAlignment(i);
69 bool CallSite::doesNotAccessMemory() const {
70 if (CallInst *CI = dyn_cast<CallInst>(I))
71 return CI->doesNotAccessMemory();
73 return cast<InvokeInst>(I)->doesNotAccessMemory();
75 bool CallSite::onlyReadsMemory() const {
76 if (CallInst *CI = dyn_cast<CallInst>(I))
77 return CI->onlyReadsMemory();
79 return cast<InvokeInst>(I)->onlyReadsMemory();
81 bool CallSite::doesNotThrow() const {
82 if (CallInst *CI = dyn_cast<CallInst>(I))
83 return CI->doesNotThrow();
85 return cast<InvokeInst>(I)->doesNotThrow();
87 void CallSite::setDoesNotThrow(bool doesNotThrow) {
88 if (CallInst *CI = dyn_cast<CallInst>(I))
89 CI->setDoesNotThrow(doesNotThrow);
91 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
94 //===----------------------------------------------------------------------===//
95 // TerminatorInst Class
96 //===----------------------------------------------------------------------===//
98 // Out of line virtual method, so the vtable, etc has a home.
99 TerminatorInst::~TerminatorInst() {
102 //===----------------------------------------------------------------------===//
103 // UnaryInstruction Class
104 //===----------------------------------------------------------------------===//
106 // Out of line virtual method, so the vtable, etc has a home.
107 UnaryInstruction::~UnaryInstruction() {
110 //===----------------------------------------------------------------------===//
112 //===----------------------------------------------------------------------===//
114 PHINode::PHINode(const PHINode &PN)
115 : Instruction(PN.getType(), Instruction::PHI,
116 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
117 ReservedSpace(PN.getNumOperands()) {
118 Use *OL = OperandList;
119 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
120 OL[i] = PN.getOperand(i);
121 OL[i+1] = PN.getOperand(i+1);
125 PHINode::~PHINode() {
126 dropHungoffUses(OperandList);
129 // removeIncomingValue - Remove an incoming value. This is useful if a
130 // predecessor basic block is deleted.
131 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
132 unsigned NumOps = getNumOperands();
133 Use *OL = OperandList;
134 assert(Idx*2 < NumOps && "BB not in PHI node!");
135 Value *Removed = OL[Idx*2];
137 // Move everything after this operand down.
139 // FIXME: we could just swap with the end of the list, then erase. However,
140 // client might not expect this to happen. The code as it is thrashes the
141 // use/def lists, which is kinda lame.
142 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
147 // Nuke the last value.
149 OL[NumOps-2+1].set(0);
150 NumOperands = NumOps-2;
152 // If the PHI node is dead, because it has zero entries, nuke it now.
153 if (NumOps == 2 && DeletePHIIfEmpty) {
154 // If anyone is using this PHI, make them use a dummy value instead...
155 replaceAllUsesWith(UndefValue::get(getType()));
161 /// resizeOperands - resize operands - This adjusts the length of the operands
162 /// list according to the following behavior:
163 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
164 /// of operation. This grows the number of ops by 1.5 times.
165 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
166 /// 3. If NumOps == NumOperands, trim the reserved space.
168 void PHINode::resizeOperands(unsigned NumOps) {
169 unsigned e = getNumOperands();
172 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
173 } else if (NumOps*2 > NumOperands) {
175 if (ReservedSpace >= NumOps) return;
176 } else if (NumOps == NumOperands) {
177 if (ReservedSpace == NumOps) return;
182 ReservedSpace = NumOps;
183 Use *OldOps = OperandList;
184 Use *NewOps = allocHungoffUses(NumOps);
185 for (unsigned i = 0; i != e; ++i) {
186 NewOps[i] = OldOps[i];
188 OperandList = NewOps;
189 if (OldOps) Use::zap(OldOps, OldOps + e, true);
192 /// hasConstantValue - If the specified PHI node always merges together the same
193 /// value, return the value, otherwise return null.
195 Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
196 // If the PHI node only has one incoming value, eliminate the PHI node...
197 if (getNumIncomingValues() == 1) {
198 if (getIncomingValue(0) != this) // not X = phi X
199 return getIncomingValue(0);
201 return UndefValue::get(getType()); // Self cycle is dead.
204 // Otherwise if all of the incoming values are the same for the PHI, replace
205 // the PHI node with the incoming value.
208 bool HasUndefInput = false;
209 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
210 if (isa<UndefValue>(getIncomingValue(i))) {
211 HasUndefInput = true;
212 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
213 if (InVal && getIncomingValue(i) != InVal)
214 return 0; // Not the same, bail out.
216 InVal = getIncomingValue(i);
219 // The only case that could cause InVal to be null is if we have a PHI node
220 // that only has entries for itself. In this case, there is no entry into the
221 // loop, so kill the PHI.
223 if (InVal == 0) InVal = UndefValue::get(getType());
225 // If we have a PHI node like phi(X, undef, X), where X is defined by some
226 // instruction, we cannot always return X as the result of the PHI node. Only
227 // do this if X is not an instruction (thus it must dominate the PHI block),
228 // or if the client is prepared to deal with this possibility.
229 if (HasUndefInput && !AllowNonDominatingInstruction)
230 if (Instruction *IV = dyn_cast<Instruction>(InVal))
231 // If it's in the entry block, it dominates everything.
232 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
234 return 0; // Cannot guarantee that InVal dominates this PHINode.
236 // All of the incoming values are the same, return the value now.
241 //===----------------------------------------------------------------------===//
242 // CallInst Implementation
243 //===----------------------------------------------------------------------===//
245 CallInst::~CallInst() {
248 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
249 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
250 Use *OL = OperandList;
253 const FunctionType *FTy =
254 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
255 FTy = FTy; // silence warning.
257 assert((NumParams == FTy->getNumParams() ||
258 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
259 "Calling a function with bad signature!");
260 for (unsigned i = 0; i != NumParams; ++i) {
261 assert((i >= FTy->getNumParams() ||
262 FTy->getParamType(i) == Params[i]->getType()) &&
263 "Calling a function with a bad signature!");
268 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
269 assert(NumOperands == 3 && "NumOperands not set up?");
270 Use *OL = OperandList;
275 const FunctionType *FTy =
276 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
277 FTy = FTy; // silence warning.
279 assert((FTy->getNumParams() == 2 ||
280 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
281 "Calling a function with bad signature");
282 assert((0 >= FTy->getNumParams() ||
283 FTy->getParamType(0) == Actual1->getType()) &&
284 "Calling a function with a bad signature!");
285 assert((1 >= FTy->getNumParams() ||
286 FTy->getParamType(1) == Actual2->getType()) &&
287 "Calling a function with a bad signature!");
290 void CallInst::init(Value *Func, Value *Actual) {
291 assert(NumOperands == 2 && "NumOperands not set up?");
292 Use *OL = OperandList;
296 const FunctionType *FTy =
297 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
298 FTy = FTy; // silence warning.
300 assert((FTy->getNumParams() == 1 ||
301 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
302 "Calling a function with bad signature");
303 assert((0 == FTy->getNumParams() ||
304 FTy->getParamType(0) == Actual->getType()) &&
305 "Calling a function with a bad signature!");
308 void CallInst::init(Value *Func) {
309 assert(NumOperands == 1 && "NumOperands not set up?");
310 Use *OL = OperandList;
313 const FunctionType *FTy =
314 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
315 FTy = FTy; // silence warning.
317 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
320 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
321 Instruction *InsertBefore)
322 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
323 ->getElementType())->getReturnType(),
325 OperandTraits<CallInst>::op_end(this) - 2,
331 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
332 BasicBlock *InsertAtEnd)
333 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
334 ->getElementType())->getReturnType(),
336 OperandTraits<CallInst>::op_end(this) - 2,
341 CallInst::CallInst(Value *Func, const std::string &Name,
342 Instruction *InsertBefore)
343 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
344 ->getElementType())->getReturnType(),
346 OperandTraits<CallInst>::op_end(this) - 1,
352 CallInst::CallInst(Value *Func, const std::string &Name,
353 BasicBlock *InsertAtEnd)
354 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
355 ->getElementType())->getReturnType(),
357 OperandTraits<CallInst>::op_end(this) - 1,
363 CallInst::CallInst(const CallInst &CI)
364 : Instruction(CI.getType(), Instruction::Call,
365 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
366 CI.getNumOperands()) {
367 setParamAttrs(CI.getParamAttrs());
368 SubclassData = CI.SubclassData;
369 Use *OL = OperandList;
370 Use *InOL = CI.OperandList;
371 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
375 void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
376 PAListPtr PAL = getParamAttrs();
377 PAL = PAL.addAttr(i, attr);
381 bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
382 if (ParamAttrs.paramHasAttr(i, attr))
384 if (const Function *F = getCalledFunction())
385 return F->paramHasAttr(i, attr);
389 void CallInst::setDoesNotThrow(bool doesNotThrow) {
390 PAListPtr PAL = getParamAttrs();
392 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
394 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
399 //===----------------------------------------------------------------------===//
400 // InvokeInst Implementation
401 //===----------------------------------------------------------------------===//
403 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
404 Value* const *Args, unsigned NumArgs) {
405 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
406 Use *OL = OperandList;
410 const FunctionType *FTy =
411 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
412 FTy = FTy; // silence warning.
414 assert(((NumArgs == FTy->getNumParams()) ||
415 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
416 "Calling a function with bad signature");
418 for (unsigned i = 0, e = NumArgs; i != e; i++) {
419 assert((i >= FTy->getNumParams() ||
420 FTy->getParamType(i) == Args[i]->getType()) &&
421 "Invoking a function with a bad signature!");
427 InvokeInst::InvokeInst(const InvokeInst &II)
428 : TerminatorInst(II.getType(), Instruction::Invoke,
429 OperandTraits<InvokeInst>::op_end(this)
430 - II.getNumOperands(),
431 II.getNumOperands()) {
432 setParamAttrs(II.getParamAttrs());
433 SubclassData = II.SubclassData;
434 Use *OL = OperandList, *InOL = II.OperandList;
435 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
439 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
440 return getSuccessor(idx);
442 unsigned InvokeInst::getNumSuccessorsV() const {
443 return getNumSuccessors();
445 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
446 return setSuccessor(idx, B);
449 bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
450 if (ParamAttrs.paramHasAttr(i, attr))
452 if (const Function *F = getCalledFunction())
453 return F->paramHasAttr(i, attr);
457 void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
458 PAListPtr PAL = getParamAttrs();
459 PAL = PAL.addAttr(i, attr);
463 void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
464 PAListPtr PAL = getParamAttrs();
466 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
468 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
473 //===----------------------------------------------------------------------===//
474 // ReturnInst Implementation
475 //===----------------------------------------------------------------------===//
477 ReturnInst::ReturnInst(const ReturnInst &RI)
478 : TerminatorInst(Type::VoidTy, Instruction::Ret,
479 OperandTraits<ReturnInst>::op_end(this)
480 - RI.getNumOperands(),
481 RI.getNumOperands()) {
482 unsigned N = RI.getNumOperands();
484 Op<0>() = RI.Op<0>();
486 Use *OL = OperandList;
487 for (unsigned i = 0; i < N; ++i)
488 OL[i] = RI.getOperand(i);
492 ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
493 : TerminatorInst(Type::VoidTy, Instruction::Ret,
494 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
495 retVal != 0, InsertBefore) {
499 ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
500 : TerminatorInst(Type::VoidTy, Instruction::Ret,
501 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
502 retVal != 0, InsertAtEnd) {
506 ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
507 : TerminatorInst(Type::VoidTy, Instruction::Ret,
508 OperandTraits<ReturnInst>::op_end(this),
512 ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
513 Instruction *InsertBefore)
514 : TerminatorInst(Type::VoidTy, Instruction::Ret,
515 OperandTraits<ReturnInst>::op_end(this) - N,
520 ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
521 BasicBlock *InsertAtEnd)
522 : TerminatorInst(Type::VoidTy, Instruction::Ret,
523 OperandTraits<ReturnInst>::op_end(this) - N,
529 void ReturnInst::init(Value * const* retVals, unsigned N) {
530 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
533 if (NumOperands == 1) {
535 if (V->getType() == Type::VoidTy)
541 Use *OL = OperandList;
542 for (unsigned i = 0; i < NumOperands; ++i) {
543 Value *V = *retVals++;
544 assert(!isa<BasicBlock>(V) &&
545 "Cannot return basic block. Probably using the incorrect ctor");
550 unsigned ReturnInst::getNumSuccessorsV() const {
551 return getNumSuccessors();
554 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
555 /// emit the vtable for the class in this translation unit.
556 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
557 assert(0 && "ReturnInst has no successors!");
560 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
561 assert(0 && "ReturnInst has no successors!");
566 ReturnInst::~ReturnInst() {
569 //===----------------------------------------------------------------------===//
570 // UnwindInst Implementation
571 //===----------------------------------------------------------------------===//
573 UnwindInst::UnwindInst(Instruction *InsertBefore)
574 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
576 UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
577 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
581 unsigned UnwindInst::getNumSuccessorsV() const {
582 return getNumSuccessors();
585 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
586 assert(0 && "UnwindInst has no successors!");
589 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
590 assert(0 && "UnwindInst has no successors!");
595 //===----------------------------------------------------------------------===//
596 // UnreachableInst Implementation
597 //===----------------------------------------------------------------------===//
599 UnreachableInst::UnreachableInst(Instruction *InsertBefore)
600 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
602 UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
603 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
606 unsigned UnreachableInst::getNumSuccessorsV() const {
607 return getNumSuccessors();
610 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
611 assert(0 && "UnwindInst has no successors!");
614 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
615 assert(0 && "UnwindInst has no successors!");
620 //===----------------------------------------------------------------------===//
621 // BranchInst Implementation
622 //===----------------------------------------------------------------------===//
624 void BranchInst::AssertOK() {
626 assert(getCondition()->getType() == Type::Int1Ty &&
627 "May only branch on boolean predicates!");
630 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
631 : TerminatorInst(Type::VoidTy, Instruction::Br,
632 OperandTraits<BranchInst>::op_end(this) - 1,
634 assert(IfTrue != 0 && "Branch destination may not be null!");
637 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
638 Instruction *InsertBefore)
639 : TerminatorInst(Type::VoidTy, Instruction::Br,
640 OperandTraits<BranchInst>::op_end(this) - 3,
650 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
651 : TerminatorInst(Type::VoidTy, Instruction::Br,
652 OperandTraits<BranchInst>::op_end(this) - 1,
654 assert(IfTrue != 0 && "Branch destination may not be null!");
658 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
659 BasicBlock *InsertAtEnd)
660 : TerminatorInst(Type::VoidTy, Instruction::Br,
661 OperandTraits<BranchInst>::op_end(this) - 3,
672 BranchInst::BranchInst(const BranchInst &BI) :
673 TerminatorInst(Type::VoidTy, Instruction::Br,
674 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
675 BI.getNumOperands()) {
676 OperandList[0] = BI.getOperand(0);
677 if (BI.getNumOperands() != 1) {
678 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
679 OperandList[1] = BI.getOperand(1);
680 OperandList[2] = BI.getOperand(2);
684 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
685 return getSuccessor(idx);
687 unsigned BranchInst::getNumSuccessorsV() const {
688 return getNumSuccessors();
690 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
691 setSuccessor(idx, B);
695 //===----------------------------------------------------------------------===//
696 // AllocationInst Implementation
697 //===----------------------------------------------------------------------===//
699 static Value *getAISize(Value *Amt) {
701 Amt = ConstantInt::get(Type::Int32Ty, 1);
703 assert(!isa<BasicBlock>(Amt) &&
704 "Passed basic block into allocation size parameter! Use other ctor");
705 assert(Amt->getType() == Type::Int32Ty &&
706 "Malloc/Allocation array size is not a 32-bit integer!");
711 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
712 unsigned Align, const std::string &Name,
713 Instruction *InsertBefore)
714 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
717 assert(Ty != Type::VoidTy && "Cannot allocate void!");
721 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
722 unsigned Align, const std::string &Name,
723 BasicBlock *InsertAtEnd)
724 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
727 assert(Ty != Type::VoidTy && "Cannot allocate void!");
731 // Out of line virtual method, so the vtable, etc has a home.
732 AllocationInst::~AllocationInst() {
735 void AllocationInst::setAlignment(unsigned Align) {
736 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
737 SubclassData = Log2_32(Align) + 1;
738 assert(getAlignment() == Align && "Alignment representation error!");
741 bool AllocationInst::isArrayAllocation() const {
742 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
743 return CI->getZExtValue() != 1;
747 const Type *AllocationInst::getAllocatedType() const {
748 return getType()->getElementType();
751 AllocaInst::AllocaInst(const AllocaInst &AI)
752 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
753 Instruction::Alloca, AI.getAlignment()) {
756 MallocInst::MallocInst(const MallocInst &MI)
757 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
758 Instruction::Malloc, MI.getAlignment()) {
761 //===----------------------------------------------------------------------===//
762 // FreeInst Implementation
763 //===----------------------------------------------------------------------===//
765 void FreeInst::AssertOK() {
766 assert(isa<PointerType>(getOperand(0)->getType()) &&
767 "Can not free something of nonpointer type!");
770 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
771 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
775 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
776 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
781 //===----------------------------------------------------------------------===//
782 // LoadInst Implementation
783 //===----------------------------------------------------------------------===//
785 void LoadInst::AssertOK() {
786 assert(isa<PointerType>(getOperand(0)->getType()) &&
787 "Ptr must have pointer type.");
790 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
791 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
792 Load, Ptr, InsertBef) {
799 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
800 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
801 Load, Ptr, InsertAE) {
808 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
809 Instruction *InsertBef)
810 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
811 Load, Ptr, InsertBef) {
812 setVolatile(isVolatile);
818 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
819 unsigned Align, Instruction *InsertBef)
820 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
821 Load, Ptr, InsertBef) {
822 setVolatile(isVolatile);
828 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
829 unsigned Align, BasicBlock *InsertAE)
830 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
831 Load, Ptr, InsertAE) {
832 setVolatile(isVolatile);
838 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
839 BasicBlock *InsertAE)
840 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
841 Load, Ptr, InsertAE) {
842 setVolatile(isVolatile);
850 LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
851 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
852 Load, Ptr, InsertBef) {
856 if (Name && Name[0]) setName(Name);
859 LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
860 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
861 Load, Ptr, InsertAE) {
865 if (Name && Name[0]) setName(Name);
868 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
869 Instruction *InsertBef)
870 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
871 Load, Ptr, InsertBef) {
872 setVolatile(isVolatile);
875 if (Name && Name[0]) setName(Name);
878 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
879 BasicBlock *InsertAE)
880 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
881 Load, Ptr, InsertAE) {
882 setVolatile(isVolatile);
885 if (Name && Name[0]) setName(Name);
888 void LoadInst::setAlignment(unsigned Align) {
889 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
890 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
893 //===----------------------------------------------------------------------===//
894 // StoreInst Implementation
895 //===----------------------------------------------------------------------===//
897 void StoreInst::AssertOK() {
898 assert(isa<PointerType>(getOperand(1)->getType()) &&
899 "Ptr must have pointer type!");
900 assert(getOperand(0)->getType() ==
901 cast<PointerType>(getOperand(1)->getType())->getElementType()
902 && "Ptr must be a pointer to Val type!");
906 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
907 : Instruction(Type::VoidTy, Store,
908 OperandTraits<StoreInst>::op_begin(this),
909 OperandTraits<StoreInst>::operands(this),
918 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
919 : Instruction(Type::VoidTy, Store,
920 OperandTraits<StoreInst>::op_begin(this),
921 OperandTraits<StoreInst>::operands(this),
930 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
931 Instruction *InsertBefore)
932 : Instruction(Type::VoidTy, Store,
933 OperandTraits<StoreInst>::op_begin(this),
934 OperandTraits<StoreInst>::operands(this),
938 setVolatile(isVolatile);
943 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
944 unsigned Align, Instruction *InsertBefore)
945 : Instruction(Type::VoidTy, Store,
946 OperandTraits<StoreInst>::op_begin(this),
947 OperandTraits<StoreInst>::operands(this),
951 setVolatile(isVolatile);
956 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
957 unsigned Align, BasicBlock *InsertAtEnd)
958 : Instruction(Type::VoidTy, Store,
959 OperandTraits<StoreInst>::op_begin(this),
960 OperandTraits<StoreInst>::operands(this),
964 setVolatile(isVolatile);
969 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
970 BasicBlock *InsertAtEnd)
971 : Instruction(Type::VoidTy, Store,
972 OperandTraits<StoreInst>::op_begin(this),
973 OperandTraits<StoreInst>::operands(this),
977 setVolatile(isVolatile);
982 void StoreInst::setAlignment(unsigned Align) {
983 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
984 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
987 //===----------------------------------------------------------------------===//
988 // GetElementPtrInst Implementation
989 //===----------------------------------------------------------------------===//
991 static unsigned retrieveAddrSpace(const Value *Val) {
992 return cast<PointerType>(Val->getType())->getAddressSpace();
995 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
996 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
997 Use *OL = OperandList;
1000 for (unsigned i = 0; i != NumIdx; ++i)
1004 void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
1005 assert(NumOperands == 2 && "NumOperands not initialized?");
1006 Use *OL = OperandList;
1011 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1012 : Instruction(GEPI.getType(), GetElementPtr,
1013 OperandTraits<GetElementPtrInst>::op_end(this)
1014 - GEPI.getNumOperands(),
1015 GEPI.getNumOperands()) {
1016 Use *OL = OperandList;
1017 Use *GEPIOL = GEPI.OperandList;
1018 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1022 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1023 const std::string &Name, Instruction *InBe)
1024 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
1025 retrieveAddrSpace(Ptr)),
1027 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1033 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1034 const std::string &Name, BasicBlock *IAE)
1035 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
1036 retrieveAddrSpace(Ptr)),
1038 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1044 // getIndexedType - Returns the type of the element that would be loaded with
1045 // a load instruction with the specified parameters.
1047 // A null type is returned if the indices are invalid for the specified
1050 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1053 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1054 if (!PTy) return 0; // Type isn't a pointer type!
1055 const Type *Agg = PTy->getElementType();
1057 // Handle the special case of the empty set index set...
1061 return ExtractValueInst::getIndexedType(Agg, Idxs+1, Idxs+NumIdx);
1064 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1065 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1066 if (!PTy) return 0; // Type isn't a pointer type!
1068 // Check the pointer index.
1069 if (!PTy->indexValid(Idx)) return 0;
1071 return PTy->getElementType();
1075 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1076 /// zeros. If so, the result pointer and the first operand have the same
1077 /// value, just potentially different types.
1078 bool GetElementPtrInst::hasAllZeroIndices() const {
1079 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1080 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1081 if (!CI->isZero()) return false;
1089 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1090 /// constant integers. If so, the result pointer and the first operand have
1091 /// a constant offset between them.
1092 bool GetElementPtrInst::hasAllConstantIndices() const {
1093 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1094 if (!isa<ConstantInt>(getOperand(i)))
1101 //===----------------------------------------------------------------------===//
1102 // ExtractElementInst Implementation
1103 //===----------------------------------------------------------------------===//
1105 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1106 const std::string &Name,
1107 Instruction *InsertBef)
1108 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1110 OperandTraits<ExtractElementInst>::op_begin(this),
1112 assert(isValidOperands(Val, Index) &&
1113 "Invalid extractelement instruction operands!");
1119 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1120 const std::string &Name,
1121 Instruction *InsertBef)
1122 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1124 OperandTraits<ExtractElementInst>::op_begin(this),
1126 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1127 assert(isValidOperands(Val, Index) &&
1128 "Invalid extractelement instruction operands!");
1135 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1136 const std::string &Name,
1137 BasicBlock *InsertAE)
1138 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1140 OperandTraits<ExtractElementInst>::op_begin(this),
1142 assert(isValidOperands(Val, Index) &&
1143 "Invalid extractelement instruction operands!");
1150 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1151 const std::string &Name,
1152 BasicBlock *InsertAE)
1153 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1155 OperandTraits<ExtractElementInst>::op_begin(this),
1157 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1158 assert(isValidOperands(Val, Index) &&
1159 "Invalid extractelement instruction operands!");
1167 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1168 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
1174 //===----------------------------------------------------------------------===//
1175 // InsertElementInst Implementation
1176 //===----------------------------------------------------------------------===//
1178 InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1179 : Instruction(IE.getType(), InsertElement,
1180 OperandTraits<InsertElementInst>::op_begin(this), 3) {
1181 Op<0>() = IE.Op<0>();
1182 Op<1>() = IE.Op<1>();
1183 Op<2>() = IE.Op<2>();
1185 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1186 const std::string &Name,
1187 Instruction *InsertBef)
1188 : Instruction(Vec->getType(), InsertElement,
1189 OperandTraits<InsertElementInst>::op_begin(this),
1191 assert(isValidOperands(Vec, Elt, Index) &&
1192 "Invalid insertelement instruction operands!");
1199 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1200 const std::string &Name,
1201 Instruction *InsertBef)
1202 : Instruction(Vec->getType(), InsertElement,
1203 OperandTraits<InsertElementInst>::op_begin(this),
1205 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1206 assert(isValidOperands(Vec, Elt, Index) &&
1207 "Invalid insertelement instruction operands!");
1215 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1216 const std::string &Name,
1217 BasicBlock *InsertAE)
1218 : Instruction(Vec->getType(), InsertElement,
1219 OperandTraits<InsertElementInst>::op_begin(this),
1221 assert(isValidOperands(Vec, Elt, Index) &&
1222 "Invalid insertelement instruction operands!");
1230 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1231 const std::string &Name,
1232 BasicBlock *InsertAE)
1233 : Instruction(Vec->getType(), InsertElement,
1234 OperandTraits<InsertElementInst>::op_begin(this),
1236 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1237 assert(isValidOperands(Vec, Elt, Index) &&
1238 "Invalid insertelement instruction operands!");
1246 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1247 const Value *Index) {
1248 if (!isa<VectorType>(Vec->getType()))
1249 return false; // First operand of insertelement must be vector type.
1251 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1252 return false;// Second operand of insertelement must be vector element type.
1254 if (Index->getType() != Type::Int32Ty)
1255 return false; // Third operand of insertelement must be uint.
1260 //===----------------------------------------------------------------------===//
1261 // ShuffleVectorInst Implementation
1262 //===----------------------------------------------------------------------===//
1264 ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1265 : Instruction(SV.getType(), ShuffleVector,
1266 OperandTraits<ShuffleVectorInst>::op_begin(this),
1267 OperandTraits<ShuffleVectorInst>::operands(this)) {
1268 Op<0>() = SV.Op<0>();
1269 Op<1>() = SV.Op<1>();
1270 Op<2>() = SV.Op<2>();
1273 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1274 const std::string &Name,
1275 Instruction *InsertBefore)
1276 : Instruction(V1->getType(), ShuffleVector,
1277 OperandTraits<ShuffleVectorInst>::op_begin(this),
1278 OperandTraits<ShuffleVectorInst>::operands(this),
1280 assert(isValidOperands(V1, V2, Mask) &&
1281 "Invalid shuffle vector instruction operands!");
1288 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1289 const std::string &Name,
1290 BasicBlock *InsertAtEnd)
1291 : Instruction(V1->getType(), ShuffleVector,
1292 OperandTraits<ShuffleVectorInst>::op_begin(this),
1293 OperandTraits<ShuffleVectorInst>::operands(this),
1295 assert(isValidOperands(V1, V2, Mask) &&
1296 "Invalid shuffle vector instruction operands!");
1304 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1305 const Value *Mask) {
1306 if (!isa<VectorType>(V1->getType()) ||
1307 V1->getType() != V2->getType())
1310 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1311 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1312 MaskTy->getElementType() != Type::Int32Ty ||
1313 MaskTy->getNumElements() !=
1314 cast<VectorType>(V1->getType())->getNumElements())
1319 /// getMaskValue - Return the index from the shuffle mask for the specified
1320 /// output result. This is either -1 if the element is undef or a number less
1321 /// than 2*numelements.
1322 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1323 const Constant *Mask = cast<Constant>(getOperand(2));
1324 if (isa<UndefValue>(Mask)) return -1;
1325 if (isa<ConstantAggregateZero>(Mask)) return 0;
1326 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1327 assert(i < MaskCV->getNumOperands() && "Index out of range");
1329 if (isa<UndefValue>(MaskCV->getOperand(i)))
1331 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1334 //===----------------------------------------------------------------------===//
1335 // InsertValueInst Class
1336 //===----------------------------------------------------------------------===//
1338 void InsertValueInst::init(Value *Agg, Value *Val, Value* const *Idx, unsigned NumIdx) {
1339 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1340 Use *OL = OperandList;
1344 for (unsigned i = 0; i != NumIdx; ++i)
1348 void InsertValueInst::init(Value *Agg, Value *Val, Value *Idx) {
1349 assert(NumOperands == 3 && "NumOperands not initialized?");
1350 Use *OL = OperandList;
1356 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1357 : Instruction(IVI.getType(), InsertValue,
1358 OperandTraits<InsertValueInst>::op_end(this)
1359 - IVI.getNumOperands(),
1360 IVI.getNumOperands()) {
1361 Use *OL = OperandList;
1362 Use *IVIOL = IVI.OperandList;
1363 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1367 //===----------------------------------------------------------------------===//
1368 // ExtractValueInst Class
1369 //===----------------------------------------------------------------------===//
1371 void ExtractValueInst::init(Value *Agg, Value* const *Idx, unsigned NumIdx) {
1372 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1373 Use *OL = OperandList;
1376 for (unsigned i = 0; i != NumIdx; ++i)
1380 void ExtractValueInst::init(Value *Agg, Value *Idx) {
1381 assert(NumOperands == 2 && "NumOperands not initialized?");
1382 Use *OL = OperandList;
1387 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1388 : Instruction(reinterpret_cast<const Type*>(EVI.getType()), ExtractValue,
1389 OperandTraits<ExtractValueInst>::op_end(this)
1390 - EVI.getNumOperands(),
1391 EVI.getNumOperands()) {
1392 Use *OL = OperandList;
1393 Use *EVIOL = EVI.OperandList;
1394 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1398 // getIndexedType - Returns the type of the element that would be extracted
1399 // with an extractvalue instruction with the specified parameters.
1401 // A null type is returned if the indices are invalid for the specified
1404 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1407 unsigned CurIdx = 0;
1408 for (; CurIdx != NumIdx; ++CurIdx) {
1409 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1410 if (!CT || isa<PointerType>(CT)) return 0;
1411 Value *Index = Idxs[CurIdx];
1412 if (!CT->indexValid(Index)) return 0;
1413 Agg = CT->getTypeAtIndex(Index);
1415 // If the new type forwards to another type, then it is in the middle
1416 // of being refined to another type (and hence, may have dropped all
1417 // references to what it was using before). So, use the new forwarded
1419 if (const Type *Ty = Agg->getForwardedType())
1422 return CurIdx == NumIdx ? Agg : 0;
1425 //===----------------------------------------------------------------------===//
1426 // BinaryOperator Class
1427 //===----------------------------------------------------------------------===//
1429 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1430 const Type *Ty, const std::string &Name,
1431 Instruction *InsertBefore)
1432 : Instruction(Ty, iType,
1433 OperandTraits<BinaryOperator>::op_begin(this),
1434 OperandTraits<BinaryOperator>::operands(this),
1442 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1443 const Type *Ty, const std::string &Name,
1444 BasicBlock *InsertAtEnd)
1445 : Instruction(Ty, iType,
1446 OperandTraits<BinaryOperator>::op_begin(this),
1447 OperandTraits<BinaryOperator>::operands(this),
1456 void BinaryOperator::init(BinaryOps iType) {
1457 Value *LHS = getOperand(0), *RHS = getOperand(1);
1458 LHS = LHS; RHS = RHS; // Silence warnings.
1459 assert(LHS->getType() == RHS->getType() &&
1460 "Binary operator operand types must match!");
1465 assert(getType() == LHS->getType() &&
1466 "Arithmetic operation should return same type as operands!");
1467 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
1468 isa<VectorType>(getType())) &&
1469 "Tried to create an arithmetic operation on a non-arithmetic type!");
1473 assert(getType() == LHS->getType() &&
1474 "Arithmetic operation should return same type as operands!");
1475 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1476 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1477 "Incorrect operand type (not integer) for S/UDIV");
1480 assert(getType() == LHS->getType() &&
1481 "Arithmetic operation should return same type as operands!");
1482 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1483 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
1484 && "Incorrect operand type (not floating point) for FDIV");
1488 assert(getType() == LHS->getType() &&
1489 "Arithmetic operation should return same type as operands!");
1490 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1491 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1492 "Incorrect operand type (not integer) for S/UREM");
1495 assert(getType() == LHS->getType() &&
1496 "Arithmetic operation should return same type as operands!");
1497 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1498 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
1499 && "Incorrect operand type (not floating point) for FREM");
1504 assert(getType() == LHS->getType() &&
1505 "Shift operation should return same type as operands!");
1506 assert(getType()->isInteger() &&
1507 "Shift operation requires integer operands");
1511 assert(getType() == LHS->getType() &&
1512 "Logical operation should return same type as operands!");
1513 assert((getType()->isInteger() ||
1514 (isa<VectorType>(getType()) &&
1515 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1516 "Tried to create a logical operation on a non-integral type!");
1524 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1525 const std::string &Name,
1526 Instruction *InsertBefore) {
1527 assert(S1->getType() == S2->getType() &&
1528 "Cannot create binary operator with two operands of differing type!");
1529 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1532 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1533 const std::string &Name,
1534 BasicBlock *InsertAtEnd) {
1535 BinaryOperator *Res = Create(Op, S1, S2, Name);
1536 InsertAtEnd->getInstList().push_back(Res);
1540 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
1541 Instruction *InsertBefore) {
1542 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1543 return new BinaryOperator(Instruction::Sub,
1545 Op->getType(), Name, InsertBefore);
1548 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
1549 BasicBlock *InsertAtEnd) {
1550 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1551 return new BinaryOperator(Instruction::Sub,
1553 Op->getType(), Name, InsertAtEnd);
1556 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
1557 Instruction *InsertBefore) {
1559 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1560 C = ConstantInt::getAllOnesValue(PTy->getElementType());
1561 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
1563 C = ConstantInt::getAllOnesValue(Op->getType());
1566 return new BinaryOperator(Instruction::Xor, Op, C,
1567 Op->getType(), Name, InsertBefore);
1570 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
1571 BasicBlock *InsertAtEnd) {
1573 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1574 // Create a vector of all ones values.
1575 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
1577 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1579 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
1582 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1583 Op->getType(), Name, InsertAtEnd);
1587 // isConstantAllOnes - Helper function for several functions below
1588 static inline bool isConstantAllOnes(const Value *V) {
1589 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1590 return CI->isAllOnesValue();
1591 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1592 return CV->isAllOnesValue();
1596 bool BinaryOperator::isNeg(const Value *V) {
1597 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1598 if (Bop->getOpcode() == Instruction::Sub)
1599 return Bop->getOperand(0) ==
1600 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
1604 bool BinaryOperator::isNot(const Value *V) {
1605 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1606 return (Bop->getOpcode() == Instruction::Xor &&
1607 (isConstantAllOnes(Bop->getOperand(1)) ||
1608 isConstantAllOnes(Bop->getOperand(0))));
1612 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1613 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1614 return cast<BinaryOperator>(BinOp)->getOperand(1);
1617 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1618 return getNegArgument(const_cast<Value*>(BinOp));
1621 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1622 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1623 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1624 Value *Op0 = BO->getOperand(0);
1625 Value *Op1 = BO->getOperand(1);
1626 if (isConstantAllOnes(Op0)) return Op1;
1628 assert(isConstantAllOnes(Op1));
1632 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1633 return getNotArgument(const_cast<Value*>(BinOp));
1637 // swapOperands - Exchange the two operands to this instruction. This
1638 // instruction is safe to use on any binary instruction and does not
1639 // modify the semantics of the instruction. If the instruction is
1640 // order dependent (SetLT f.e.) the opcode is changed.
1642 bool BinaryOperator::swapOperands() {
1643 if (!isCommutative())
1644 return true; // Can't commute operands
1645 Op<0>().swap(Op<1>());
1649 //===----------------------------------------------------------------------===//
1651 //===----------------------------------------------------------------------===//
1653 // Just determine if this cast only deals with integral->integral conversion.
1654 bool CastInst::isIntegerCast() const {
1655 switch (getOpcode()) {
1656 default: return false;
1657 case Instruction::ZExt:
1658 case Instruction::SExt:
1659 case Instruction::Trunc:
1661 case Instruction::BitCast:
1662 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
1666 bool CastInst::isLosslessCast() const {
1667 // Only BitCast can be lossless, exit fast if we're not BitCast
1668 if (getOpcode() != Instruction::BitCast)
1671 // Identity cast is always lossless
1672 const Type* SrcTy = getOperand(0)->getType();
1673 const Type* DstTy = getType();
1677 // Pointer to pointer is always lossless.
1678 if (isa<PointerType>(SrcTy))
1679 return isa<PointerType>(DstTy);
1680 return false; // Other types have no identity values
1683 /// This function determines if the CastInst does not require any bits to be
1684 /// changed in order to effect the cast. Essentially, it identifies cases where
1685 /// no code gen is necessary for the cast, hence the name no-op cast. For
1686 /// example, the following are all no-op casts:
1687 /// # bitcast i32* %x to i8*
1688 /// # bitcast <2 x i32> %x to <4 x i16>
1689 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1690 /// @brief Determine if a cast is a no-op.
1691 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1692 switch (getOpcode()) {
1694 assert(!"Invalid CastOp");
1695 case Instruction::Trunc:
1696 case Instruction::ZExt:
1697 case Instruction::SExt:
1698 case Instruction::FPTrunc:
1699 case Instruction::FPExt:
1700 case Instruction::UIToFP:
1701 case Instruction::SIToFP:
1702 case Instruction::FPToUI:
1703 case Instruction::FPToSI:
1704 return false; // These always modify bits
1705 case Instruction::BitCast:
1706 return true; // BitCast never modifies bits.
1707 case Instruction::PtrToInt:
1708 return IntPtrTy->getPrimitiveSizeInBits() ==
1709 getType()->getPrimitiveSizeInBits();
1710 case Instruction::IntToPtr:
1711 return IntPtrTy->getPrimitiveSizeInBits() ==
1712 getOperand(0)->getType()->getPrimitiveSizeInBits();
1716 /// This function determines if a pair of casts can be eliminated and what
1717 /// opcode should be used in the elimination. This assumes that there are two
1718 /// instructions like this:
1719 /// * %F = firstOpcode SrcTy %x to MidTy
1720 /// * %S = secondOpcode MidTy %F to DstTy
1721 /// The function returns a resultOpcode so these two casts can be replaced with:
1722 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1723 /// If no such cast is permited, the function returns 0.
1724 unsigned CastInst::isEliminableCastPair(
1725 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1726 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1728 // Define the 144 possibilities for these two cast instructions. The values
1729 // in this matrix determine what to do in a given situation and select the
1730 // case in the switch below. The rows correspond to firstOp, the columns
1731 // correspond to secondOp. In looking at the table below, keep in mind
1732 // the following cast properties:
1734 // Size Compare Source Destination
1735 // Operator Src ? Size Type Sign Type Sign
1736 // -------- ------------ ------------------- ---------------------
1737 // TRUNC > Integer Any Integral Any
1738 // ZEXT < Integral Unsigned Integer Any
1739 // SEXT < Integral Signed Integer Any
1740 // FPTOUI n/a FloatPt n/a Integral Unsigned
1741 // FPTOSI n/a FloatPt n/a Integral Signed
1742 // UITOFP n/a Integral Unsigned FloatPt n/a
1743 // SITOFP n/a Integral Signed FloatPt n/a
1744 // FPTRUNC > FloatPt n/a FloatPt n/a
1745 // FPEXT < FloatPt n/a FloatPt n/a
1746 // PTRTOINT n/a Pointer n/a Integral Unsigned
1747 // INTTOPTR n/a Integral Unsigned Pointer n/a
1748 // BITCONVERT = FirstClass n/a FirstClass n/a
1750 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1751 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1752 // into "fptoui double to ulong", but this loses information about the range
1753 // of the produced value (we no longer know the top-part is all zeros).
1754 // Further this conversion is often much more expensive for typical hardware,
1755 // and causes issues when building libgcc. We disallow fptosi+sext for the
1757 const unsigned numCastOps =
1758 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1759 static const uint8_t CastResults[numCastOps][numCastOps] = {
1760 // T F F U S F F P I B -+
1761 // R Z S P P I I T P 2 N T |
1762 // U E E 2 2 2 2 R E I T C +- secondOp
1763 // N X X U S F F N X N 2 V |
1764 // C T T I I P P C T T P T -+
1765 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1766 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1767 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
1768 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1769 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
1770 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1771 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1772 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1773 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1774 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1775 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1776 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1779 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1780 [secondOp-Instruction::CastOpsBegin];
1783 // categorically disallowed
1786 // allowed, use first cast's opcode
1789 // allowed, use second cast's opcode
1792 // no-op cast in second op implies firstOp as long as the DestTy
1794 if (DstTy->isInteger())
1798 // no-op cast in second op implies firstOp as long as the DestTy
1799 // is floating point
1800 if (DstTy->isFloatingPoint())
1804 // no-op cast in first op implies secondOp as long as the SrcTy
1806 if (SrcTy->isInteger())
1810 // no-op cast in first op implies secondOp as long as the SrcTy
1811 // is a floating point
1812 if (SrcTy->isFloatingPoint())
1816 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1817 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1818 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1819 if (MidSize >= PtrSize)
1820 return Instruction::BitCast;
1824 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1825 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1826 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1827 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1828 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1829 if (SrcSize == DstSize)
1830 return Instruction::BitCast;
1831 else if (SrcSize < DstSize)
1835 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1836 return Instruction::ZExt;
1838 // fpext followed by ftrunc is allowed if the bit size returned to is
1839 // the same as the original, in which case its just a bitcast
1841 return Instruction::BitCast;
1842 return 0; // If the types are not the same we can't eliminate it.
1844 // bitcast followed by ptrtoint is allowed as long as the bitcast
1845 // is a pointer to pointer cast.
1846 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1850 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1851 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1855 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1856 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1857 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1858 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1859 if (SrcSize <= PtrSize && SrcSize == DstSize)
1860 return Instruction::BitCast;
1864 // cast combination can't happen (error in input). This is for all cases
1865 // where the MidTy is not the same for the two cast instructions.
1866 assert(!"Invalid Cast Combination");
1869 assert(!"Error in CastResults table!!!");
1875 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
1876 const std::string &Name, Instruction *InsertBefore) {
1877 // Construct and return the appropriate CastInst subclass
1879 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1880 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1881 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1882 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1883 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1884 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1885 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1886 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1887 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1888 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1889 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1890 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1892 assert(!"Invalid opcode provided");
1897 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
1898 const std::string &Name, BasicBlock *InsertAtEnd) {
1899 // Construct and return the appropriate CastInst subclass
1901 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1902 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1903 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1904 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1905 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1906 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1907 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1908 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1909 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1910 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1911 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1912 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1914 assert(!"Invalid opcode provided");
1919 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
1920 const std::string &Name,
1921 Instruction *InsertBefore) {
1922 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1923 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1924 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1927 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
1928 const std::string &Name,
1929 BasicBlock *InsertAtEnd) {
1930 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1931 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1932 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1935 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
1936 const std::string &Name,
1937 Instruction *InsertBefore) {
1938 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1939 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1940 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
1943 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
1944 const std::string &Name,
1945 BasicBlock *InsertAtEnd) {
1946 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1947 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1948 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1951 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
1952 const std::string &Name,
1953 Instruction *InsertBefore) {
1954 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1955 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1956 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1959 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
1960 const std::string &Name,
1961 BasicBlock *InsertAtEnd) {
1962 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1963 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1964 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1967 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
1968 const std::string &Name,
1969 BasicBlock *InsertAtEnd) {
1970 assert(isa<PointerType>(S->getType()) && "Invalid cast");
1971 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
1974 if (Ty->isInteger())
1975 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1976 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1979 /// @brief Create a BitCast or a PtrToInt cast instruction
1980 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
1981 const std::string &Name,
1982 Instruction *InsertBefore) {
1983 assert(isa<PointerType>(S->getType()) && "Invalid cast");
1984 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
1987 if (Ty->isInteger())
1988 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1989 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1992 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
1993 bool isSigned, const std::string &Name,
1994 Instruction *InsertBefore) {
1995 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
1996 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1997 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1998 Instruction::CastOps opcode =
1999 (SrcBits == DstBits ? Instruction::BitCast :
2000 (SrcBits > DstBits ? Instruction::Trunc :
2001 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2002 return Create(opcode, C, Ty, Name, InsertBefore);
2005 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2006 bool isSigned, const std::string &Name,
2007 BasicBlock *InsertAtEnd) {
2008 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
2009 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2010 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2011 Instruction::CastOps opcode =
2012 (SrcBits == DstBits ? Instruction::BitCast :
2013 (SrcBits > DstBits ? Instruction::Trunc :
2014 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2015 return Create(opcode, C, Ty, Name, InsertAtEnd);
2018 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2019 const std::string &Name,
2020 Instruction *InsertBefore) {
2021 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2023 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2024 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2025 Instruction::CastOps opcode =
2026 (SrcBits == DstBits ? Instruction::BitCast :
2027 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2028 return Create(opcode, C, Ty, Name, InsertBefore);
2031 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2032 const std::string &Name,
2033 BasicBlock *InsertAtEnd) {
2034 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2036 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2037 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2038 Instruction::CastOps opcode =
2039 (SrcBits == DstBits ? Instruction::BitCast :
2040 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2041 return Create(opcode, C, Ty, Name, InsertAtEnd);
2044 // Check whether it is valid to call getCastOpcode for these types.
2045 // This routine must be kept in sync with getCastOpcode.
2046 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2047 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2050 if (SrcTy == DestTy)
2053 // Get the bit sizes, we'll need these
2054 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2055 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2057 // Run through the possibilities ...
2058 if (DestTy->isInteger()) { // Casting to integral
2059 if (SrcTy->isInteger()) { // Casting from integral
2061 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2063 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2064 // Casting from vector
2065 return DestBits == PTy->getBitWidth();
2066 } else { // Casting from something else
2067 return isa<PointerType>(SrcTy);
2069 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2070 if (SrcTy->isInteger()) { // Casting from integral
2072 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2074 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2075 // Casting from vector
2076 return DestBits == PTy->getBitWidth();
2077 } else { // Casting from something else
2080 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2081 // Casting to vector
2082 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2083 // Casting from vector
2084 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2085 } else { // Casting from something else
2086 return DestPTy->getBitWidth() == SrcBits;
2088 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2089 if (isa<PointerType>(SrcTy)) { // Casting from pointer
2091 } else if (SrcTy->isInteger()) { // Casting from integral
2093 } else { // Casting from something else
2096 } else { // Casting to something else
2101 // Provide a way to get a "cast" where the cast opcode is inferred from the
2102 // types and size of the operand. This, basically, is a parallel of the
2103 // logic in the castIsValid function below. This axiom should hold:
2104 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2105 // should not assert in castIsValid. In other words, this produces a "correct"
2106 // casting opcode for the arguments passed to it.
2107 // This routine must be kept in sync with isCastable.
2108 Instruction::CastOps
2109 CastInst::getCastOpcode(
2110 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
2111 // Get the bit sizes, we'll need these
2112 const Type *SrcTy = Src->getType();
2113 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2114 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2116 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2117 "Only first class types are castable!");
2119 // Run through the possibilities ...
2120 if (DestTy->isInteger()) { // Casting to integral
2121 if (SrcTy->isInteger()) { // Casting from integral
2122 if (DestBits < SrcBits)
2123 return Trunc; // int -> smaller int
2124 else if (DestBits > SrcBits) { // its an extension
2126 return SExt; // signed -> SEXT
2128 return ZExt; // unsigned -> ZEXT
2130 return BitCast; // Same size, No-op cast
2132 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2134 return FPToSI; // FP -> sint
2136 return FPToUI; // FP -> uint
2137 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2138 assert(DestBits == PTy->getBitWidth() &&
2139 "Casting vector to integer of different width");
2140 return BitCast; // Same size, no-op cast
2142 assert(isa<PointerType>(SrcTy) &&
2143 "Casting from a value that is not first-class type");
2144 return PtrToInt; // ptr -> int
2146 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2147 if (SrcTy->isInteger()) { // Casting from integral
2149 return SIToFP; // sint -> FP
2151 return UIToFP; // uint -> FP
2152 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2153 if (DestBits < SrcBits) {
2154 return FPTrunc; // FP -> smaller FP
2155 } else if (DestBits > SrcBits) {
2156 return FPExt; // FP -> larger FP
2158 return BitCast; // same size, no-op cast
2160 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2161 assert(DestBits == PTy->getBitWidth() &&
2162 "Casting vector to floating point of different width");
2163 return BitCast; // same size, no-op cast
2165 assert(0 && "Casting pointer or non-first class to float");
2167 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2168 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2169 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
2170 "Casting vector to vector of different widths");
2171 return BitCast; // vector -> vector
2172 } else if (DestPTy->getBitWidth() == SrcBits) {
2173 return BitCast; // float/int -> vector
2175 assert(!"Illegal cast to vector (wrong type or size)");
2177 } else if (isa<PointerType>(DestTy)) {
2178 if (isa<PointerType>(SrcTy)) {
2179 return BitCast; // ptr -> ptr
2180 } else if (SrcTy->isInteger()) {
2181 return IntToPtr; // int -> ptr
2183 assert(!"Casting pointer to other than pointer or int");
2186 assert(!"Casting to type that is not first-class");
2189 // If we fall through to here we probably hit an assertion cast above
2190 // and assertions are not turned on. Anything we return is an error, so
2191 // BitCast is as good a choice as any.
2195 //===----------------------------------------------------------------------===//
2196 // CastInst SubClass Constructors
2197 //===----------------------------------------------------------------------===//
2199 /// Check that the construction parameters for a CastInst are correct. This
2200 /// could be broken out into the separate constructors but it is useful to have
2201 /// it in one place and to eliminate the redundant code for getting the sizes
2202 /// of the types involved.
2204 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
2206 // Check for type sanity on the arguments
2207 const Type *SrcTy = S->getType();
2208 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2211 // Get the size of the types in bits, we'll need this later
2212 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2213 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2215 // Switch on the opcode provided
2217 default: return false; // This is an input error
2218 case Instruction::Trunc:
2219 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
2220 case Instruction::ZExt:
2221 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
2222 case Instruction::SExt:
2223 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
2224 case Instruction::FPTrunc:
2225 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2226 SrcBitSize > DstBitSize;
2227 case Instruction::FPExt:
2228 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2229 SrcBitSize < DstBitSize;
2230 case Instruction::UIToFP:
2231 case Instruction::SIToFP:
2232 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2233 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2234 return SVTy->getElementType()->isInteger() &&
2235 DVTy->getElementType()->isFloatingPoint() &&
2236 SVTy->getNumElements() == DVTy->getNumElements();
2239 return SrcTy->isInteger() && DstTy->isFloatingPoint();
2240 case Instruction::FPToUI:
2241 case Instruction::FPToSI:
2242 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2243 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2244 return SVTy->getElementType()->isFloatingPoint() &&
2245 DVTy->getElementType()->isInteger() &&
2246 SVTy->getNumElements() == DVTy->getNumElements();
2249 return SrcTy->isFloatingPoint() && DstTy->isInteger();
2250 case Instruction::PtrToInt:
2251 return isa<PointerType>(SrcTy) && DstTy->isInteger();
2252 case Instruction::IntToPtr:
2253 return SrcTy->isInteger() && isa<PointerType>(DstTy);
2254 case Instruction::BitCast:
2255 // BitCast implies a no-op cast of type only. No bits change.
2256 // However, you can't cast pointers to anything but pointers.
2257 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2260 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2261 // these cases, the cast is okay if the source and destination bit widths
2263 return SrcBitSize == DstBitSize;
2267 TruncInst::TruncInst(
2268 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2269 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2270 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2273 TruncInst::TruncInst(
2274 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2275 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2276 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2280 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2281 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2282 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2286 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2287 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2288 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2291 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2292 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2293 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2297 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2298 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2299 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2302 FPTruncInst::FPTruncInst(
2303 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2304 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2305 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2308 FPTruncInst::FPTruncInst(
2309 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2310 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2311 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2314 FPExtInst::FPExtInst(
2315 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2316 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2317 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2320 FPExtInst::FPExtInst(
2321 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2322 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2323 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2326 UIToFPInst::UIToFPInst(
2327 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2328 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2329 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2332 UIToFPInst::UIToFPInst(
2333 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2334 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2335 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2338 SIToFPInst::SIToFPInst(
2339 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2340 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2341 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2344 SIToFPInst::SIToFPInst(
2345 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2346 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2347 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2350 FPToUIInst::FPToUIInst(
2351 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2352 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2353 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2356 FPToUIInst::FPToUIInst(
2357 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2358 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2359 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2362 FPToSIInst::FPToSIInst(
2363 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2364 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2365 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2368 FPToSIInst::FPToSIInst(
2369 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2370 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2371 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2374 PtrToIntInst::PtrToIntInst(
2375 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2376 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2377 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2380 PtrToIntInst::PtrToIntInst(
2381 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2382 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2383 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2386 IntToPtrInst::IntToPtrInst(
2387 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2388 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2389 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2392 IntToPtrInst::IntToPtrInst(
2393 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2394 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2395 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2398 BitCastInst::BitCastInst(
2399 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2400 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2401 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2404 BitCastInst::BitCastInst(
2405 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2406 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2407 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2410 //===----------------------------------------------------------------------===//
2412 //===----------------------------------------------------------------------===//
2414 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2415 Value *LHS, Value *RHS, const std::string &Name,
2416 Instruction *InsertBefore)
2417 : Instruction(ty, op,
2418 OperandTraits<CmpInst>::op_begin(this),
2419 OperandTraits<CmpInst>::operands(this),
2423 SubclassData = predicate;
2427 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2428 Value *LHS, Value *RHS, const std::string &Name,
2429 BasicBlock *InsertAtEnd)
2430 : Instruction(ty, op,
2431 OperandTraits<CmpInst>::op_begin(this),
2432 OperandTraits<CmpInst>::operands(this),
2436 SubclassData = predicate;
2441 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2442 const std::string &Name, Instruction *InsertBefore) {
2443 if (Op == Instruction::ICmp) {
2444 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2447 if (Op == Instruction::FCmp) {
2448 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2451 if (Op == Instruction::VICmp) {
2452 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2455 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2460 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2461 const std::string &Name, BasicBlock *InsertAtEnd) {
2462 if (Op == Instruction::ICmp) {
2463 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2466 if (Op == Instruction::FCmp) {
2467 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2470 if (Op == Instruction::VICmp) {
2471 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2474 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2478 void CmpInst::swapOperands() {
2479 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2482 cast<FCmpInst>(this)->swapOperands();
2485 bool CmpInst::isCommutative() {
2486 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2487 return IC->isCommutative();
2488 return cast<FCmpInst>(this)->isCommutative();
2491 bool CmpInst::isEquality() {
2492 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2493 return IC->isEquality();
2494 return cast<FCmpInst>(this)->isEquality();
2498 ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2501 assert(!"Unknown icmp predicate!");
2502 case ICMP_EQ: return ICMP_NE;
2503 case ICMP_NE: return ICMP_EQ;
2504 case ICMP_UGT: return ICMP_ULE;
2505 case ICMP_ULT: return ICMP_UGE;
2506 case ICMP_UGE: return ICMP_ULT;
2507 case ICMP_ULE: return ICMP_UGT;
2508 case ICMP_SGT: return ICMP_SLE;
2509 case ICMP_SLT: return ICMP_SGE;
2510 case ICMP_SGE: return ICMP_SLT;
2511 case ICMP_SLE: return ICMP_SGT;
2515 ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2517 default: assert(! "Unknown icmp predicate!");
2518 case ICMP_EQ: case ICMP_NE:
2520 case ICMP_SGT: return ICMP_SLT;
2521 case ICMP_SLT: return ICMP_SGT;
2522 case ICMP_SGE: return ICMP_SLE;
2523 case ICMP_SLE: return ICMP_SGE;
2524 case ICMP_UGT: return ICMP_ULT;
2525 case ICMP_ULT: return ICMP_UGT;
2526 case ICMP_UGE: return ICMP_ULE;
2527 case ICMP_ULE: return ICMP_UGE;
2531 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2533 default: assert(! "Unknown icmp predicate!");
2534 case ICMP_EQ: case ICMP_NE:
2535 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2537 case ICMP_UGT: return ICMP_SGT;
2538 case ICMP_ULT: return ICMP_SLT;
2539 case ICMP_UGE: return ICMP_SGE;
2540 case ICMP_ULE: return ICMP_SLE;
2544 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2546 default: assert(! "Unknown icmp predicate!");
2547 case ICMP_EQ: case ICMP_NE:
2548 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2550 case ICMP_SGT: return ICMP_UGT;
2551 case ICMP_SLT: return ICMP_ULT;
2552 case ICMP_SGE: return ICMP_UGE;
2553 case ICMP_SLE: return ICMP_ULE;
2557 bool ICmpInst::isSignedPredicate(Predicate pred) {
2559 default: assert(! "Unknown icmp predicate!");
2560 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2562 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2563 case ICMP_UGE: case ICMP_ULE:
2568 /// Initialize a set of values that all satisfy the condition with C.
2571 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2574 uint32_t BitWidth = C.getBitWidth();
2576 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2577 case ICmpInst::ICMP_EQ: Upper++; break;
2578 case ICmpInst::ICMP_NE: Lower++; break;
2579 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2580 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2581 case ICmpInst::ICMP_UGT:
2582 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2584 case ICmpInst::ICMP_SGT:
2585 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2587 case ICmpInst::ICMP_ULE:
2588 Lower = APInt::getMinValue(BitWidth); Upper++;
2590 case ICmpInst::ICMP_SLE:
2591 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2593 case ICmpInst::ICMP_UGE:
2594 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2596 case ICmpInst::ICMP_SGE:
2597 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2600 return ConstantRange(Lower, Upper);
2603 FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2606 assert(!"Unknown icmp predicate!");
2607 case FCMP_OEQ: return FCMP_UNE;
2608 case FCMP_ONE: return FCMP_UEQ;
2609 case FCMP_OGT: return FCMP_ULE;
2610 case FCMP_OLT: return FCMP_UGE;
2611 case FCMP_OGE: return FCMP_ULT;
2612 case FCMP_OLE: return FCMP_UGT;
2613 case FCMP_UEQ: return FCMP_ONE;
2614 case FCMP_UNE: return FCMP_OEQ;
2615 case FCMP_UGT: return FCMP_OLE;
2616 case FCMP_ULT: return FCMP_OGE;
2617 case FCMP_UGE: return FCMP_OLT;
2618 case FCMP_ULE: return FCMP_OGT;
2619 case FCMP_ORD: return FCMP_UNO;
2620 case FCMP_UNO: return FCMP_ORD;
2621 case FCMP_TRUE: return FCMP_FALSE;
2622 case FCMP_FALSE: return FCMP_TRUE;
2626 FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2628 default: assert(!"Unknown fcmp predicate!");
2629 case FCMP_FALSE: case FCMP_TRUE:
2630 case FCMP_OEQ: case FCMP_ONE:
2631 case FCMP_UEQ: case FCMP_UNE:
2632 case FCMP_ORD: case FCMP_UNO:
2634 case FCMP_OGT: return FCMP_OLT;
2635 case FCMP_OLT: return FCMP_OGT;
2636 case FCMP_OGE: return FCMP_OLE;
2637 case FCMP_OLE: return FCMP_OGE;
2638 case FCMP_UGT: return FCMP_ULT;
2639 case FCMP_ULT: return FCMP_UGT;
2640 case FCMP_UGE: return FCMP_ULE;
2641 case FCMP_ULE: return FCMP_UGE;
2645 bool CmpInst::isUnsigned(unsigned short predicate) {
2646 switch (predicate) {
2647 default: return false;
2648 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2649 case ICmpInst::ICMP_UGE: return true;
2653 bool CmpInst::isSigned(unsigned short predicate){
2654 switch (predicate) {
2655 default: return false;
2656 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2657 case ICmpInst::ICMP_SGE: return true;
2661 bool CmpInst::isOrdered(unsigned short predicate) {
2662 switch (predicate) {
2663 default: return false;
2664 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2665 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2666 case FCmpInst::FCMP_ORD: return true;
2670 bool CmpInst::isUnordered(unsigned short predicate) {
2671 switch (predicate) {
2672 default: return false;
2673 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2674 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2675 case FCmpInst::FCMP_UNO: return true;
2679 //===----------------------------------------------------------------------===//
2680 // SwitchInst Implementation
2681 //===----------------------------------------------------------------------===//
2683 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
2684 assert(Value && Default);
2685 ReservedSpace = 2+NumCases*2;
2687 OperandList = allocHungoffUses(ReservedSpace);
2689 OperandList[0] = Value;
2690 OperandList[1] = Default;
2693 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2694 /// switch on and a default destination. The number of additional cases can
2695 /// be specified here to make memory allocation more efficient. This
2696 /// constructor can also autoinsert before another instruction.
2697 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2698 Instruction *InsertBefore)
2699 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2700 init(Value, Default, NumCases);
2703 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2704 /// switch on and a default destination. The number of additional cases can
2705 /// be specified here to make memory allocation more efficient. This
2706 /// constructor also autoinserts at the end of the specified BasicBlock.
2707 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2708 BasicBlock *InsertAtEnd)
2709 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2710 init(Value, Default, NumCases);
2713 SwitchInst::SwitchInst(const SwitchInst &SI)
2714 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2715 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
2716 Use *OL = OperandList, *InOL = SI.OperandList;
2717 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2719 OL[i+1] = InOL[i+1];
2723 SwitchInst::~SwitchInst() {
2724 dropHungoffUses(OperandList);
2728 /// addCase - Add an entry to the switch instruction...
2730 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
2731 unsigned OpNo = NumOperands;
2732 if (OpNo+2 > ReservedSpace)
2733 resizeOperands(0); // Get more space!
2734 // Initialize some new operands.
2735 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
2736 NumOperands = OpNo+2;
2737 OperandList[OpNo] = OnVal;
2738 OperandList[OpNo+1] = Dest;
2741 /// removeCase - This method removes the specified successor from the switch
2742 /// instruction. Note that this cannot be used to remove the default
2743 /// destination (successor #0).
2745 void SwitchInst::removeCase(unsigned idx) {
2746 assert(idx != 0 && "Cannot remove the default case!");
2747 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2749 unsigned NumOps = getNumOperands();
2750 Use *OL = OperandList;
2752 // Move everything after this operand down.
2754 // FIXME: we could just swap with the end of the list, then erase. However,
2755 // client might not expect this to happen. The code as it is thrashes the
2756 // use/def lists, which is kinda lame.
2757 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2759 OL[i-2+1] = OL[i+1];
2762 // Nuke the last value.
2763 OL[NumOps-2].set(0);
2764 OL[NumOps-2+1].set(0);
2765 NumOperands = NumOps-2;
2768 /// resizeOperands - resize operands - This adjusts the length of the operands
2769 /// list according to the following behavior:
2770 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
2771 /// of operation. This grows the number of ops by 3 times.
2772 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2773 /// 3. If NumOps == NumOperands, trim the reserved space.
2775 void SwitchInst::resizeOperands(unsigned NumOps) {
2776 unsigned e = getNumOperands();
2779 } else if (NumOps*2 > NumOperands) {
2780 // No resize needed.
2781 if (ReservedSpace >= NumOps) return;
2782 } else if (NumOps == NumOperands) {
2783 if (ReservedSpace == NumOps) return;
2788 ReservedSpace = NumOps;
2789 Use *NewOps = allocHungoffUses(NumOps);
2790 Use *OldOps = OperandList;
2791 for (unsigned i = 0; i != e; ++i) {
2792 NewOps[i] = OldOps[i];
2794 OperandList = NewOps;
2795 if (OldOps) Use::zap(OldOps, OldOps + e, true);
2799 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2800 return getSuccessor(idx);
2802 unsigned SwitchInst::getNumSuccessorsV() const {
2803 return getNumSuccessors();
2805 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2806 setSuccessor(idx, B);
2809 //===----------------------------------------------------------------------===//
2810 // GetResultInst Implementation
2811 //===----------------------------------------------------------------------===//
2813 GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
2814 const std::string &Name,
2815 Instruction *InsertBef)
2816 : UnaryInstruction(cast<StructType>(Aggregate->getType())
2817 ->getElementType(Index),
2818 GetResult, Aggregate, InsertBef),
2820 assert(isValidOperands(Aggregate, Index)
2821 && "Invalid GetResultInst operands!");
2825 bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2829 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2830 unsigned NumElements = STy->getNumElements();
2831 if (Index >= NumElements || NumElements == 0)
2834 // getresult aggregate value's element types are restricted to
2835 // avoid nested aggregates.
2836 for (unsigned i = 0; i < NumElements; ++i)
2837 if (!STy->getElementType(i)->isFirstClassType())
2840 // Otherwise, Aggregate is valid.
2846 // Define these methods here so vtables don't get emitted into every translation
2847 // unit that uses these classes.
2849 GetElementPtrInst *GetElementPtrInst::clone() const {
2850 return new(getNumOperands()) GetElementPtrInst(*this);
2853 BinaryOperator *BinaryOperator::clone() const {
2854 return Create(getOpcode(), Op<0>(), Op<1>());
2857 FCmpInst* FCmpInst::clone() const {
2858 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
2860 ICmpInst* ICmpInst::clone() const {
2861 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
2864 VFCmpInst* VFCmpInst::clone() const {
2865 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2867 VICmpInst* VICmpInst::clone() const {
2868 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2871 ExtractValueInst *ExtractValueInst::clone() const {
2872 return new(getNumOperands()) ExtractValueInst(*this);
2874 InsertValueInst *InsertValueInst::clone() const {
2875 return new(getNumOperands()) InsertValueInst(*this);
2879 MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2880 AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2881 FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2882 LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2883 StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2884 CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2885 CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2886 CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2887 CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2888 CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2889 CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2890 CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2891 CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2892 CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2893 CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2894 CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2895 CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2896 CallInst *CallInst::clone() const {
2897 return new(getNumOperands()) CallInst(*this);
2899 SelectInst *SelectInst::clone() const {
2900 return new(getNumOperands()) SelectInst(*this);
2902 VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2904 ExtractElementInst *ExtractElementInst::clone() const {
2905 return new ExtractElementInst(*this);
2907 InsertElementInst *InsertElementInst::clone() const {
2908 return InsertElementInst::Create(*this);
2910 ShuffleVectorInst *ShuffleVectorInst::clone() const {
2911 return new ShuffleVectorInst(*this);
2913 PHINode *PHINode::clone() const { return new PHINode(*this); }
2914 ReturnInst *ReturnInst::clone() const {
2915 return new(getNumOperands()) ReturnInst(*this);
2917 BranchInst *BranchInst::clone() const {
2918 return new(getNumOperands()) BranchInst(*this);
2920 SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2921 InvokeInst *InvokeInst::clone() const {
2922 return new(getNumOperands()) InvokeInst(*this);
2924 UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
2925 UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
2926 GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }