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 AttrListPtr &CallSite::getAttributes() const {
45 if (CallInst *CI = dyn_cast<CallInst>(I))
46 return CI->getAttributes();
48 return cast<InvokeInst>(I)->getAttributes();
50 void CallSite::setAttributes(const AttrListPtr &PAL) {
51 if (CallInst *CI = dyn_cast<CallInst>(I))
52 CI->setAttributes(PAL);
54 cast<InvokeInst>(I)->setAttributes(PAL);
56 bool CallSite::paramHasAttr(uint16_t i, Attributes 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 void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
76 if (CallInst *CI = dyn_cast<CallInst>(I))
77 CI->setDoesNotAccessMemory(doesNotAccessMemory);
79 cast<InvokeInst>(I)->setDoesNotAccessMemory(doesNotAccessMemory);
81 bool CallSite::onlyReadsMemory() const {
82 if (CallInst *CI = dyn_cast<CallInst>(I))
83 return CI->onlyReadsMemory();
85 return cast<InvokeInst>(I)->onlyReadsMemory();
87 void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
88 if (CallInst *CI = dyn_cast<CallInst>(I))
89 CI->setOnlyReadsMemory(onlyReadsMemory);
91 cast<InvokeInst>(I)->setOnlyReadsMemory(onlyReadsMemory);
93 bool CallSite::doesNotReturn() const {
94 if (CallInst *CI = dyn_cast<CallInst>(I))
95 return CI->doesNotReturn();
97 return cast<InvokeInst>(I)->doesNotReturn();
99 void CallSite::setDoesNotReturn(bool doesNotReturn) {
100 if (CallInst *CI = dyn_cast<CallInst>(I))
101 CI->setDoesNotReturn(doesNotReturn);
103 cast<InvokeInst>(I)->setDoesNotReturn(doesNotReturn);
105 bool CallSite::doesNotThrow() const {
106 if (CallInst *CI = dyn_cast<CallInst>(I))
107 return CI->doesNotThrow();
109 return cast<InvokeInst>(I)->doesNotThrow();
111 void CallSite::setDoesNotThrow(bool doesNotThrow) {
112 if (CallInst *CI = dyn_cast<CallInst>(I))
113 CI->setDoesNotThrow(doesNotThrow);
115 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
118 bool CallSite::hasArgument(const Value *Arg) const {
119 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
120 if (AI->get() == Arg)
125 //===----------------------------------------------------------------------===//
126 // TerminatorInst Class
127 //===----------------------------------------------------------------------===//
129 // Out of line virtual method, so the vtable, etc has a home.
130 TerminatorInst::~TerminatorInst() {
133 //===----------------------------------------------------------------------===//
134 // UnaryInstruction Class
135 //===----------------------------------------------------------------------===//
137 // Out of line virtual method, so the vtable, etc has a home.
138 UnaryInstruction::~UnaryInstruction() {
141 //===----------------------------------------------------------------------===//
143 //===----------------------------------------------------------------------===//
145 PHINode::PHINode(const PHINode &PN)
146 : Instruction(PN.getType(), Instruction::PHI,
147 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
148 ReservedSpace(PN.getNumOperands()) {
149 Use *OL = OperandList;
150 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
151 OL[i] = PN.getOperand(i);
152 OL[i+1] = PN.getOperand(i+1);
156 PHINode::~PHINode() {
158 dropHungoffUses(OperandList);
161 // removeIncomingValue - Remove an incoming value. This is useful if a
162 // predecessor basic block is deleted.
163 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
164 unsigned NumOps = getNumOperands();
165 Use *OL = OperandList;
166 assert(Idx*2 < NumOps && "BB not in PHI node!");
167 Value *Removed = OL[Idx*2];
169 // Move everything after this operand down.
171 // FIXME: we could just swap with the end of the list, then erase. However,
172 // client might not expect this to happen. The code as it is thrashes the
173 // use/def lists, which is kinda lame.
174 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
179 // Nuke the last value.
181 OL[NumOps-2+1].set(0);
182 NumOperands = NumOps-2;
184 // If the PHI node is dead, because it has zero entries, nuke it now.
185 if (NumOps == 2 && DeletePHIIfEmpty) {
186 // If anyone is using this PHI, make them use a dummy value instead...
187 replaceAllUsesWith(UndefValue::get(getType()));
193 /// resizeOperands - resize operands - This adjusts the length of the operands
194 /// list according to the following behavior:
195 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
196 /// of operation. This grows the number of ops by 1.5 times.
197 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
198 /// 3. If NumOps == NumOperands, trim the reserved space.
200 void PHINode::resizeOperands(unsigned NumOps) {
201 unsigned e = getNumOperands();
204 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
205 } else if (NumOps*2 > NumOperands) {
207 if (ReservedSpace >= NumOps) return;
208 } else if (NumOps == NumOperands) {
209 if (ReservedSpace == NumOps) return;
214 ReservedSpace = NumOps;
215 Use *OldOps = OperandList;
216 Use *NewOps = allocHungoffUses(NumOps);
217 std::copy(OldOps, OldOps + e, NewOps);
218 OperandList = NewOps;
219 if (OldOps) Use::zap(OldOps, OldOps + e, true);
222 /// hasConstantValue - If the specified PHI node always merges together the same
223 /// value, return the value, otherwise return null.
225 Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
226 // If the PHI node only has one incoming value, eliminate the PHI node...
227 if (getNumIncomingValues() == 1) {
228 if (getIncomingValue(0) != this) // not X = phi X
229 return getIncomingValue(0);
231 return UndefValue::get(getType()); // Self cycle is dead.
234 // Otherwise if all of the incoming values are the same for the PHI, replace
235 // the PHI node with the incoming value.
238 bool HasUndefInput = false;
239 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
240 if (isa<UndefValue>(getIncomingValue(i))) {
241 HasUndefInput = true;
242 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
243 if (InVal && getIncomingValue(i) != InVal)
244 return 0; // Not the same, bail out.
246 InVal = getIncomingValue(i);
249 // The only case that could cause InVal to be null is if we have a PHI node
250 // that only has entries for itself. In this case, there is no entry into the
251 // loop, so kill the PHI.
253 if (InVal == 0) InVal = UndefValue::get(getType());
255 // If we have a PHI node like phi(X, undef, X), where X is defined by some
256 // instruction, we cannot always return X as the result of the PHI node. Only
257 // do this if X is not an instruction (thus it must dominate the PHI block),
258 // or if the client is prepared to deal with this possibility.
259 if (HasUndefInput && !AllowNonDominatingInstruction)
260 if (Instruction *IV = dyn_cast<Instruction>(InVal))
261 // If it's in the entry block, it dominates everything.
262 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
264 return 0; // Cannot guarantee that InVal dominates this PHINode.
266 // All of the incoming values are the same, return the value now.
271 //===----------------------------------------------------------------------===//
272 // CallInst Implementation
273 //===----------------------------------------------------------------------===//
275 CallInst::~CallInst() {
278 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
279 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
280 Use *OL = OperandList;
283 const FunctionType *FTy =
284 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
285 FTy = FTy; // silence warning.
287 assert((NumParams == FTy->getNumParams() ||
288 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
289 "Calling a function with bad signature!");
290 for (unsigned i = 0; i != NumParams; ++i) {
291 assert((i >= FTy->getNumParams() ||
292 FTy->getParamType(i) == Params[i]->getType()) &&
293 "Calling a function with a bad signature!");
298 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
299 assert(NumOperands == 3 && "NumOperands not set up?");
300 Use *OL = OperandList;
305 const FunctionType *FTy =
306 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
307 FTy = FTy; // silence warning.
309 assert((FTy->getNumParams() == 2 ||
310 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
311 "Calling a function with bad signature");
312 assert((0 >= FTy->getNumParams() ||
313 FTy->getParamType(0) == Actual1->getType()) &&
314 "Calling a function with a bad signature!");
315 assert((1 >= FTy->getNumParams() ||
316 FTy->getParamType(1) == Actual2->getType()) &&
317 "Calling a function with a bad signature!");
320 void CallInst::init(Value *Func, Value *Actual) {
321 assert(NumOperands == 2 && "NumOperands not set up?");
322 Use *OL = OperandList;
326 const FunctionType *FTy =
327 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
328 FTy = FTy; // silence warning.
330 assert((FTy->getNumParams() == 1 ||
331 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
332 "Calling a function with bad signature");
333 assert((0 == FTy->getNumParams() ||
334 FTy->getParamType(0) == Actual->getType()) &&
335 "Calling a function with a bad signature!");
338 void CallInst::init(Value *Func) {
339 assert(NumOperands == 1 && "NumOperands not set up?");
340 Use *OL = OperandList;
343 const FunctionType *FTy =
344 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
345 FTy = FTy; // silence warning.
347 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
350 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
351 Instruction *InsertBefore)
352 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
353 ->getElementType())->getReturnType(),
355 OperandTraits<CallInst>::op_end(this) - 2,
361 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
362 BasicBlock *InsertAtEnd)
363 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
364 ->getElementType())->getReturnType(),
366 OperandTraits<CallInst>::op_end(this) - 2,
371 CallInst::CallInst(Value *Func, const std::string &Name,
372 Instruction *InsertBefore)
373 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
374 ->getElementType())->getReturnType(),
376 OperandTraits<CallInst>::op_end(this) - 1,
382 CallInst::CallInst(Value *Func, const std::string &Name,
383 BasicBlock *InsertAtEnd)
384 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
385 ->getElementType())->getReturnType(),
387 OperandTraits<CallInst>::op_end(this) - 1,
393 CallInst::CallInst(const CallInst &CI)
394 : Instruction(CI.getType(), Instruction::Call,
395 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
396 CI.getNumOperands()) {
397 setAttributes(CI.getAttributes());
398 SubclassData = CI.SubclassData;
399 Use *OL = OperandList;
400 Use *InOL = CI.OperandList;
401 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
405 void CallInst::addAttribute(unsigned i, Attributes attr) {
406 AttrListPtr PAL = getAttributes();
407 PAL = PAL.addAttr(i, attr);
411 void CallInst::removeAttribute(unsigned i, Attributes attr) {
412 AttrListPtr PAL = getAttributes();
413 PAL = PAL.removeAttr(i, attr);
417 bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
418 if (AttributeList.paramHasAttr(i, attr))
420 if (const Function *F = getCalledFunction())
421 return F->paramHasAttr(i, attr);
426 //===----------------------------------------------------------------------===//
427 // InvokeInst Implementation
428 //===----------------------------------------------------------------------===//
430 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
431 Value* const *Args, unsigned NumArgs) {
432 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
433 Use *OL = OperandList;
437 const FunctionType *FTy =
438 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
439 FTy = FTy; // silence warning.
441 assert(((NumArgs == FTy->getNumParams()) ||
442 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
443 "Calling a function with bad signature");
445 for (unsigned i = 0, e = NumArgs; i != e; i++) {
446 assert((i >= FTy->getNumParams() ||
447 FTy->getParamType(i) == Args[i]->getType()) &&
448 "Invoking a function with a bad signature!");
454 InvokeInst::InvokeInst(const InvokeInst &II)
455 : TerminatorInst(II.getType(), Instruction::Invoke,
456 OperandTraits<InvokeInst>::op_end(this)
457 - II.getNumOperands(),
458 II.getNumOperands()) {
459 setAttributes(II.getAttributes());
460 SubclassData = II.SubclassData;
461 Use *OL = OperandList, *InOL = II.OperandList;
462 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
466 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
467 return getSuccessor(idx);
469 unsigned InvokeInst::getNumSuccessorsV() const {
470 return getNumSuccessors();
472 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
473 return setSuccessor(idx, B);
476 bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
477 if (AttributeList.paramHasAttr(i, attr))
479 if (const Function *F = getCalledFunction())
480 return F->paramHasAttr(i, attr);
484 void InvokeInst::addAttribute(unsigned i, Attributes attr) {
485 AttrListPtr PAL = getAttributes();
486 PAL = PAL.addAttr(i, attr);
490 void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
491 AttrListPtr PAL = getAttributes();
492 PAL = PAL.removeAttr(i, attr);
497 //===----------------------------------------------------------------------===//
498 // ReturnInst Implementation
499 //===----------------------------------------------------------------------===//
501 ReturnInst::ReturnInst(const ReturnInst &RI)
502 : TerminatorInst(Type::VoidTy, Instruction::Ret,
503 OperandTraits<ReturnInst>::op_end(this) -
505 RI.getNumOperands()) {
506 if (RI.getNumOperands())
507 Op<0>() = RI.Op<0>();
510 ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
511 : TerminatorInst(Type::VoidTy, Instruction::Ret,
512 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
517 ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
518 : TerminatorInst(Type::VoidTy, Instruction::Ret,
519 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
524 ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
525 : TerminatorInst(Type::VoidTy, Instruction::Ret,
526 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
529 unsigned ReturnInst::getNumSuccessorsV() const {
530 return getNumSuccessors();
533 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
534 /// emit the vtable for the class in this translation unit.
535 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
536 assert(0 && "ReturnInst has no successors!");
539 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
540 assert(0 && "ReturnInst has no successors!");
545 ReturnInst::~ReturnInst() {
548 //===----------------------------------------------------------------------===//
549 // UnwindInst Implementation
550 //===----------------------------------------------------------------------===//
552 UnwindInst::UnwindInst(Instruction *InsertBefore)
553 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
555 UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
556 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
560 unsigned UnwindInst::getNumSuccessorsV() const {
561 return getNumSuccessors();
564 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
565 assert(0 && "UnwindInst has no successors!");
568 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
569 assert(0 && "UnwindInst has no successors!");
574 //===----------------------------------------------------------------------===//
575 // UnreachableInst Implementation
576 //===----------------------------------------------------------------------===//
578 UnreachableInst::UnreachableInst(Instruction *InsertBefore)
579 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
581 UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
582 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
585 unsigned UnreachableInst::getNumSuccessorsV() const {
586 return getNumSuccessors();
589 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
590 assert(0 && "UnwindInst has no successors!");
593 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
594 assert(0 && "UnwindInst has no successors!");
599 //===----------------------------------------------------------------------===//
600 // BranchInst Implementation
601 //===----------------------------------------------------------------------===//
603 void BranchInst::AssertOK() {
605 assert(getCondition()->getType() == Type::Int1Ty &&
606 "May only branch on boolean predicates!");
609 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
610 : TerminatorInst(Type::VoidTy, Instruction::Br,
611 OperandTraits<BranchInst>::op_end(this) - 1,
613 assert(IfTrue != 0 && "Branch destination may not be null!");
616 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
617 Instruction *InsertBefore)
618 : TerminatorInst(Type::VoidTy, Instruction::Br,
619 OperandTraits<BranchInst>::op_end(this) - 3,
629 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
630 : TerminatorInst(Type::VoidTy, Instruction::Br,
631 OperandTraits<BranchInst>::op_end(this) - 1,
633 assert(IfTrue != 0 && "Branch destination may not be null!");
637 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
638 BasicBlock *InsertAtEnd)
639 : TerminatorInst(Type::VoidTy, Instruction::Br,
640 OperandTraits<BranchInst>::op_end(this) - 3,
651 BranchInst::BranchInst(const BranchInst &BI) :
652 TerminatorInst(Type::VoidTy, Instruction::Br,
653 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
654 BI.getNumOperands()) {
655 OperandList[0] = BI.getOperand(0);
656 if (BI.getNumOperands() != 1) {
657 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
658 OperandList[1] = BI.getOperand(1);
659 OperandList[2] = BI.getOperand(2);
663 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
664 return getSuccessor(idx);
666 unsigned BranchInst::getNumSuccessorsV() const {
667 return getNumSuccessors();
669 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
670 setSuccessor(idx, B);
674 //===----------------------------------------------------------------------===//
675 // AllocationInst Implementation
676 //===----------------------------------------------------------------------===//
678 static Value *getAISize(Value *Amt) {
680 Amt = ConstantInt::get(Type::Int32Ty, 1);
682 assert(!isa<BasicBlock>(Amt) &&
683 "Passed basic block into allocation size parameter! Use other ctor");
684 assert(Amt->getType() == Type::Int32Ty &&
685 "Malloc/Allocation array size is not a 32-bit integer!");
690 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
691 unsigned Align, const std::string &Name,
692 Instruction *InsertBefore)
693 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
696 assert(Ty != Type::VoidTy && "Cannot allocate void!");
700 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
701 unsigned Align, const std::string &Name,
702 BasicBlock *InsertAtEnd)
703 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
706 assert(Ty != Type::VoidTy && "Cannot allocate void!");
710 // Out of line virtual method, so the vtable, etc has a home.
711 AllocationInst::~AllocationInst() {
714 void AllocationInst::setAlignment(unsigned Align) {
715 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
716 SubclassData = Log2_32(Align) + 1;
717 assert(getAlignment() == Align && "Alignment representation error!");
720 bool AllocationInst::isArrayAllocation() const {
721 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
722 return CI->getZExtValue() != 1;
726 const Type *AllocationInst::getAllocatedType() const {
727 return getType()->getElementType();
730 AllocaInst::AllocaInst(const AllocaInst &AI)
731 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
732 Instruction::Alloca, AI.getAlignment()) {
735 MallocInst::MallocInst(const MallocInst &MI)
736 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
737 Instruction::Malloc, MI.getAlignment()) {
740 //===----------------------------------------------------------------------===//
741 // FreeInst Implementation
742 //===----------------------------------------------------------------------===//
744 void FreeInst::AssertOK() {
745 assert(isa<PointerType>(getOperand(0)->getType()) &&
746 "Can not free something of nonpointer type!");
749 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
750 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
754 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
755 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
760 //===----------------------------------------------------------------------===//
761 // LoadInst Implementation
762 //===----------------------------------------------------------------------===//
764 void LoadInst::AssertOK() {
765 assert(isa<PointerType>(getOperand(0)->getType()) &&
766 "Ptr must have pointer type.");
769 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
770 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
771 Load, Ptr, InsertBef) {
778 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
779 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
780 Load, Ptr, InsertAE) {
787 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
788 Instruction *InsertBef)
789 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
790 Load, Ptr, InsertBef) {
791 setVolatile(isVolatile);
797 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
798 unsigned Align, Instruction *InsertBef)
799 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
800 Load, Ptr, InsertBef) {
801 setVolatile(isVolatile);
807 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
808 unsigned Align, BasicBlock *InsertAE)
809 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
810 Load, Ptr, InsertAE) {
811 setVolatile(isVolatile);
817 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
818 BasicBlock *InsertAE)
819 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
820 Load, Ptr, InsertAE) {
821 setVolatile(isVolatile);
829 LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
830 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
831 Load, Ptr, InsertBef) {
835 if (Name && Name[0]) setName(Name);
838 LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
839 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
840 Load, Ptr, InsertAE) {
844 if (Name && Name[0]) setName(Name);
847 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
848 Instruction *InsertBef)
849 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
850 Load, Ptr, InsertBef) {
851 setVolatile(isVolatile);
854 if (Name && Name[0]) setName(Name);
857 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
858 BasicBlock *InsertAE)
859 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
860 Load, Ptr, InsertAE) {
861 setVolatile(isVolatile);
864 if (Name && Name[0]) setName(Name);
867 void LoadInst::setAlignment(unsigned Align) {
868 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
869 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
872 //===----------------------------------------------------------------------===//
873 // StoreInst Implementation
874 //===----------------------------------------------------------------------===//
876 void StoreInst::AssertOK() {
877 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
878 assert(isa<PointerType>(getOperand(1)->getType()) &&
879 "Ptr must have pointer type!");
880 assert(getOperand(0)->getType() ==
881 cast<PointerType>(getOperand(1)->getType())->getElementType()
882 && "Ptr must be a pointer to Val type!");
886 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
887 : Instruction(Type::VoidTy, Store,
888 OperandTraits<StoreInst>::op_begin(this),
889 OperandTraits<StoreInst>::operands(this),
898 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
899 : Instruction(Type::VoidTy, Store,
900 OperandTraits<StoreInst>::op_begin(this),
901 OperandTraits<StoreInst>::operands(this),
910 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
911 Instruction *InsertBefore)
912 : Instruction(Type::VoidTy, Store,
913 OperandTraits<StoreInst>::op_begin(this),
914 OperandTraits<StoreInst>::operands(this),
918 setVolatile(isVolatile);
923 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
924 unsigned Align, Instruction *InsertBefore)
925 : Instruction(Type::VoidTy, Store,
926 OperandTraits<StoreInst>::op_begin(this),
927 OperandTraits<StoreInst>::operands(this),
931 setVolatile(isVolatile);
936 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
937 unsigned Align, BasicBlock *InsertAtEnd)
938 : Instruction(Type::VoidTy, Store,
939 OperandTraits<StoreInst>::op_begin(this),
940 OperandTraits<StoreInst>::operands(this),
944 setVolatile(isVolatile);
949 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
950 BasicBlock *InsertAtEnd)
951 : Instruction(Type::VoidTy, Store,
952 OperandTraits<StoreInst>::op_begin(this),
953 OperandTraits<StoreInst>::operands(this),
957 setVolatile(isVolatile);
962 void StoreInst::setAlignment(unsigned Align) {
963 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
964 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
967 //===----------------------------------------------------------------------===//
968 // GetElementPtrInst Implementation
969 //===----------------------------------------------------------------------===//
971 static unsigned retrieveAddrSpace(const Value *Val) {
972 return cast<PointerType>(Val->getType())->getAddressSpace();
975 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
976 const std::string &Name) {
977 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
978 Use *OL = OperandList;
981 for (unsigned i = 0; i != NumIdx; ++i)
987 void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
988 assert(NumOperands == 2 && "NumOperands not initialized?");
989 Use *OL = OperandList;
996 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
997 : Instruction(GEPI.getType(), GetElementPtr,
998 OperandTraits<GetElementPtrInst>::op_end(this)
999 - GEPI.getNumOperands(),
1000 GEPI.getNumOperands()) {
1001 Use *OL = OperandList;
1002 Use *GEPIOL = GEPI.OperandList;
1003 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1007 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1008 const std::string &Name, Instruction *InBe)
1009 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
1010 retrieveAddrSpace(Ptr)),
1012 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1014 init(Ptr, Idx, Name);
1017 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1018 const std::string &Name, BasicBlock *IAE)
1019 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
1020 retrieveAddrSpace(Ptr)),
1022 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1024 init(Ptr, Idx, Name);
1027 // getIndexedType - Returns the type of the element that would be loaded with
1028 // a load instruction with the specified parameters.
1030 // The Idxs pointer should point to a continuous piece of memory containing the
1031 // indices, either as Value* or uint64_t.
1033 // A null type is returned if the indices are invalid for the specified
1036 template <typename IndexTy>
1037 static const Type* getIndexedTypeInternal(const Type *Ptr,
1038 IndexTy const *Idxs,
1040 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1041 if (!PTy) return 0; // Type isn't a pointer type!
1042 const Type *Agg = PTy->getElementType();
1044 // Handle the special case of the empty set index set...
1048 unsigned CurIdx = 1;
1049 for (; CurIdx != NumIdx; ++CurIdx) {
1050 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1051 if (!CT || isa<PointerType>(CT)) return 0;
1052 IndexTy Index = Idxs[CurIdx];
1053 if (!CT->indexValid(Index)) return 0;
1054 Agg = CT->getTypeAtIndex(Index);
1056 // If the new type forwards to another type, then it is in the middle
1057 // of being refined to another type (and hence, may have dropped all
1058 // references to what it was using before). So, use the new forwarded
1060 if (const Type *Ty = Agg->getForwardedType())
1063 return CurIdx == NumIdx ? Agg : 0;
1066 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1069 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1072 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1073 uint64_t const *Idxs,
1075 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1078 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1079 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1080 if (!PTy) return 0; // Type isn't a pointer type!
1082 // Check the pointer index.
1083 if (!PTy->indexValid(Idx)) return 0;
1085 return PTy->getElementType();
1089 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1090 /// zeros. If so, the result pointer and the first operand have the same
1091 /// value, just potentially different types.
1092 bool GetElementPtrInst::hasAllZeroIndices() const {
1093 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1094 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1095 if (!CI->isZero()) return false;
1103 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1104 /// constant integers. If so, the result pointer and the first operand have
1105 /// a constant offset between them.
1106 bool GetElementPtrInst::hasAllConstantIndices() const {
1107 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1108 if (!isa<ConstantInt>(getOperand(i)))
1115 //===----------------------------------------------------------------------===//
1116 // ExtractElementInst Implementation
1117 //===----------------------------------------------------------------------===//
1119 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1120 const std::string &Name,
1121 Instruction *InsertBef)
1122 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1124 OperandTraits<ExtractElementInst>::op_begin(this),
1126 assert(isValidOperands(Val, Index) &&
1127 "Invalid extractelement instruction operands!");
1133 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1134 const std::string &Name,
1135 Instruction *InsertBef)
1136 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1138 OperandTraits<ExtractElementInst>::op_begin(this),
1140 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1141 assert(isValidOperands(Val, Index) &&
1142 "Invalid extractelement instruction operands!");
1149 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1150 const std::string &Name,
1151 BasicBlock *InsertAE)
1152 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1154 OperandTraits<ExtractElementInst>::op_begin(this),
1156 assert(isValidOperands(Val, Index) &&
1157 "Invalid extractelement instruction operands!");
1164 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1165 const std::string &Name,
1166 BasicBlock *InsertAE)
1167 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1169 OperandTraits<ExtractElementInst>::op_begin(this),
1171 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1172 assert(isValidOperands(Val, Index) &&
1173 "Invalid extractelement instruction operands!");
1181 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1182 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
1188 //===----------------------------------------------------------------------===//
1189 // InsertElementInst Implementation
1190 //===----------------------------------------------------------------------===//
1192 InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1193 : Instruction(IE.getType(), InsertElement,
1194 OperandTraits<InsertElementInst>::op_begin(this), 3) {
1195 Op<0>() = IE.Op<0>();
1196 Op<1>() = IE.Op<1>();
1197 Op<2>() = IE.Op<2>();
1199 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1200 const std::string &Name,
1201 Instruction *InsertBef)
1202 : Instruction(Vec->getType(), InsertElement,
1203 OperandTraits<InsertElementInst>::op_begin(this),
1205 assert(isValidOperands(Vec, Elt, Index) &&
1206 "Invalid insertelement instruction operands!");
1213 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1214 const std::string &Name,
1215 Instruction *InsertBef)
1216 : Instruction(Vec->getType(), InsertElement,
1217 OperandTraits<InsertElementInst>::op_begin(this),
1219 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1220 assert(isValidOperands(Vec, Elt, Index) &&
1221 "Invalid insertelement instruction operands!");
1229 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1230 const std::string &Name,
1231 BasicBlock *InsertAE)
1232 : Instruction(Vec->getType(), InsertElement,
1233 OperandTraits<InsertElementInst>::op_begin(this),
1235 assert(isValidOperands(Vec, Elt, Index) &&
1236 "Invalid insertelement instruction operands!");
1244 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1245 const std::string &Name,
1246 BasicBlock *InsertAE)
1247 : Instruction(Vec->getType(), InsertElement,
1248 OperandTraits<InsertElementInst>::op_begin(this),
1250 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1251 assert(isValidOperands(Vec, Elt, Index) &&
1252 "Invalid insertelement instruction operands!");
1260 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1261 const Value *Index) {
1262 if (!isa<VectorType>(Vec->getType()))
1263 return false; // First operand of insertelement must be vector type.
1265 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1266 return false;// Second operand of insertelement must be vector element type.
1268 if (Index->getType() != Type::Int32Ty)
1269 return false; // Third operand of insertelement must be uint.
1274 //===----------------------------------------------------------------------===//
1275 // ShuffleVectorInst Implementation
1276 //===----------------------------------------------------------------------===//
1278 ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1279 : Instruction(SV.getType(), ShuffleVector,
1280 OperandTraits<ShuffleVectorInst>::op_begin(this),
1281 OperandTraits<ShuffleVectorInst>::operands(this)) {
1282 Op<0>() = SV.Op<0>();
1283 Op<1>() = SV.Op<1>();
1284 Op<2>() = SV.Op<2>();
1287 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1288 const std::string &Name,
1289 Instruction *InsertBefore)
1290 : Instruction(V1->getType(), ShuffleVector,
1291 OperandTraits<ShuffleVectorInst>::op_begin(this),
1292 OperandTraits<ShuffleVectorInst>::operands(this),
1294 assert(isValidOperands(V1, V2, Mask) &&
1295 "Invalid shuffle vector instruction operands!");
1302 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1303 const std::string &Name,
1304 BasicBlock *InsertAtEnd)
1305 : Instruction(V1->getType(), ShuffleVector,
1306 OperandTraits<ShuffleVectorInst>::op_begin(this),
1307 OperandTraits<ShuffleVectorInst>::operands(this),
1309 assert(isValidOperands(V1, V2, Mask) &&
1310 "Invalid shuffle vector instruction operands!");
1318 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1319 const Value *Mask) {
1320 if (!isa<VectorType>(V1->getType()) ||
1321 V1->getType() != V2->getType())
1324 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1325 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1326 MaskTy->getElementType() != Type::Int32Ty ||
1327 MaskTy->getNumElements() !=
1328 cast<VectorType>(V1->getType())->getNumElements())
1333 /// getMaskValue - Return the index from the shuffle mask for the specified
1334 /// output result. This is either -1 if the element is undef or a number less
1335 /// than 2*numelements.
1336 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1337 const Constant *Mask = cast<Constant>(getOperand(2));
1338 if (isa<UndefValue>(Mask)) return -1;
1339 if (isa<ConstantAggregateZero>(Mask)) return 0;
1340 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1341 assert(i < MaskCV->getNumOperands() && "Index out of range");
1343 if (isa<UndefValue>(MaskCV->getOperand(i)))
1345 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1348 //===----------------------------------------------------------------------===//
1349 // InsertValueInst Class
1350 //===----------------------------------------------------------------------===//
1352 void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1353 unsigned NumIdx, const std::string &Name) {
1354 assert(NumOperands == 2 && "NumOperands not initialized?");
1358 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
1362 void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1363 const std::string &Name) {
1364 assert(NumOperands == 2 && "NumOperands not initialized?");
1368 Indices.push_back(Idx);
1372 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1373 : Instruction(IVI.getType(), InsertValue,
1374 OperandTraits<InsertValueInst>::op_begin(this), 2),
1375 Indices(IVI.Indices) {
1376 Op<0>() = IVI.getOperand(0);
1377 Op<1>() = IVI.getOperand(1);
1380 InsertValueInst::InsertValueInst(Value *Agg,
1383 const std::string &Name,
1384 Instruction *InsertBefore)
1385 : Instruction(Agg->getType(), InsertValue,
1386 OperandTraits<InsertValueInst>::op_begin(this),
1388 init(Agg, Val, Idx, Name);
1391 InsertValueInst::InsertValueInst(Value *Agg,
1394 const std::string &Name,
1395 BasicBlock *InsertAtEnd)
1396 : Instruction(Agg->getType(), InsertValue,
1397 OperandTraits<InsertValueInst>::op_begin(this),
1399 init(Agg, Val, Idx, Name);
1402 //===----------------------------------------------------------------------===//
1403 // ExtractValueInst Class
1404 //===----------------------------------------------------------------------===//
1406 void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
1407 const std::string &Name) {
1408 assert(NumOperands == 1 && "NumOperands not initialized?");
1410 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
1414 void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
1415 assert(NumOperands == 1 && "NumOperands not initialized?");
1417 Indices.push_back(Idx);
1421 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1422 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
1423 Indices(EVI.Indices) {
1426 // getIndexedType - Returns the type of the element that would be extracted
1427 // with an extractvalue instruction with the specified parameters.
1429 // A null type is returned if the indices are invalid for the specified
1432 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1433 const unsigned *Idxs,
1435 unsigned CurIdx = 0;
1436 for (; CurIdx != NumIdx; ++CurIdx) {
1437 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1438 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1439 unsigned Index = Idxs[CurIdx];
1440 if (!CT->indexValid(Index)) return 0;
1441 Agg = CT->getTypeAtIndex(Index);
1443 // If the new type forwards to another type, then it is in the middle
1444 // of being refined to another type (and hence, may have dropped all
1445 // references to what it was using before). So, use the new forwarded
1447 if (const Type *Ty = Agg->getForwardedType())
1450 return CurIdx == NumIdx ? Agg : 0;
1453 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1455 return getIndexedType(Agg, &Idx, 1);
1458 //===----------------------------------------------------------------------===//
1459 // BinaryOperator Class
1460 //===----------------------------------------------------------------------===//
1462 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1463 const Type *Ty, const std::string &Name,
1464 Instruction *InsertBefore)
1465 : Instruction(Ty, iType,
1466 OperandTraits<BinaryOperator>::op_begin(this),
1467 OperandTraits<BinaryOperator>::operands(this),
1475 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1476 const Type *Ty, const std::string &Name,
1477 BasicBlock *InsertAtEnd)
1478 : Instruction(Ty, iType,
1479 OperandTraits<BinaryOperator>::op_begin(this),
1480 OperandTraits<BinaryOperator>::operands(this),
1489 void BinaryOperator::init(BinaryOps iType) {
1490 Value *LHS = getOperand(0), *RHS = getOperand(1);
1491 LHS = LHS; RHS = RHS; // Silence warnings.
1492 assert(LHS->getType() == RHS->getType() &&
1493 "Binary operator operand types must match!");
1498 assert(getType() == LHS->getType() &&
1499 "Arithmetic operation should return same type as operands!");
1500 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
1501 isa<VectorType>(getType())) &&
1502 "Tried to create an arithmetic operation on a non-arithmetic type!");
1506 assert(getType() == LHS->getType() &&
1507 "Arithmetic operation should return same type as operands!");
1508 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1509 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1510 "Incorrect operand type (not integer) for S/UDIV");
1513 assert(getType() == LHS->getType() &&
1514 "Arithmetic operation should return same type as operands!");
1515 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1516 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
1517 && "Incorrect operand type (not floating point) for FDIV");
1521 assert(getType() == LHS->getType() &&
1522 "Arithmetic operation should return same type as operands!");
1523 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1524 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1525 "Incorrect operand type (not integer) for S/UREM");
1528 assert(getType() == LHS->getType() &&
1529 "Arithmetic operation should return same type as operands!");
1530 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1531 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
1532 && "Incorrect operand type (not floating point) for FREM");
1537 assert(getType() == LHS->getType() &&
1538 "Shift operation should return same type as operands!");
1539 assert((getType()->isInteger() ||
1540 (isa<VectorType>(getType()) &&
1541 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1542 "Tried to create a shift operation on a non-integral type!");
1546 assert(getType() == LHS->getType() &&
1547 "Logical operation should return same type as operands!");
1548 assert((getType()->isInteger() ||
1549 (isa<VectorType>(getType()) &&
1550 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1551 "Tried to create a logical operation on a non-integral type!");
1559 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1560 const std::string &Name,
1561 Instruction *InsertBefore) {
1562 assert(S1->getType() == S2->getType() &&
1563 "Cannot create binary operator with two operands of differing type!");
1564 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1567 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1568 const std::string &Name,
1569 BasicBlock *InsertAtEnd) {
1570 BinaryOperator *Res = Create(Op, S1, S2, Name);
1571 InsertAtEnd->getInstList().push_back(Res);
1575 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
1576 Instruction *InsertBefore) {
1577 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1578 return new BinaryOperator(Instruction::Sub,
1580 Op->getType(), Name, InsertBefore);
1583 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
1584 BasicBlock *InsertAtEnd) {
1585 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1586 return new BinaryOperator(Instruction::Sub,
1588 Op->getType(), Name, InsertAtEnd);
1591 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
1592 Instruction *InsertBefore) {
1594 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1595 C = ConstantInt::getAllOnesValue(PTy->getElementType());
1596 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
1598 C = ConstantInt::getAllOnesValue(Op->getType());
1601 return new BinaryOperator(Instruction::Xor, Op, C,
1602 Op->getType(), Name, InsertBefore);
1605 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
1606 BasicBlock *InsertAtEnd) {
1608 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1609 // Create a vector of all ones values.
1610 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
1612 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1614 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
1617 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1618 Op->getType(), Name, InsertAtEnd);
1622 // isConstantAllOnes - Helper function for several functions below
1623 static inline bool isConstantAllOnes(const Value *V) {
1624 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1625 return CI->isAllOnesValue();
1626 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1627 return CV->isAllOnesValue();
1631 bool BinaryOperator::isNeg(const Value *V) {
1632 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1633 if (Bop->getOpcode() == Instruction::Sub)
1634 return Bop->getOperand(0) ==
1635 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
1639 bool BinaryOperator::isNot(const Value *V) {
1640 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1641 return (Bop->getOpcode() == Instruction::Xor &&
1642 (isConstantAllOnes(Bop->getOperand(1)) ||
1643 isConstantAllOnes(Bop->getOperand(0))));
1647 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1648 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1649 return cast<BinaryOperator>(BinOp)->getOperand(1);
1652 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1653 return getNegArgument(const_cast<Value*>(BinOp));
1656 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1657 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1658 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1659 Value *Op0 = BO->getOperand(0);
1660 Value *Op1 = BO->getOperand(1);
1661 if (isConstantAllOnes(Op0)) return Op1;
1663 assert(isConstantAllOnes(Op1));
1667 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1668 return getNotArgument(const_cast<Value*>(BinOp));
1672 // swapOperands - Exchange the two operands to this instruction. This
1673 // instruction is safe to use on any binary instruction and does not
1674 // modify the semantics of the instruction. If the instruction is
1675 // order dependent (SetLT f.e.) the opcode is changed.
1677 bool BinaryOperator::swapOperands() {
1678 if (!isCommutative())
1679 return true; // Can't commute operands
1680 Op<0>().swap(Op<1>());
1684 //===----------------------------------------------------------------------===//
1686 //===----------------------------------------------------------------------===//
1688 // Just determine if this cast only deals with integral->integral conversion.
1689 bool CastInst::isIntegerCast() const {
1690 switch (getOpcode()) {
1691 default: return false;
1692 case Instruction::ZExt:
1693 case Instruction::SExt:
1694 case Instruction::Trunc:
1696 case Instruction::BitCast:
1697 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
1701 bool CastInst::isLosslessCast() const {
1702 // Only BitCast can be lossless, exit fast if we're not BitCast
1703 if (getOpcode() != Instruction::BitCast)
1706 // Identity cast is always lossless
1707 const Type* SrcTy = getOperand(0)->getType();
1708 const Type* DstTy = getType();
1712 // Pointer to pointer is always lossless.
1713 if (isa<PointerType>(SrcTy))
1714 return isa<PointerType>(DstTy);
1715 return false; // Other types have no identity values
1718 /// This function determines if the CastInst does not require any bits to be
1719 /// changed in order to effect the cast. Essentially, it identifies cases where
1720 /// no code gen is necessary for the cast, hence the name no-op cast. For
1721 /// example, the following are all no-op casts:
1722 /// # bitcast i32* %x to i8*
1723 /// # bitcast <2 x i32> %x to <4 x i16>
1724 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1725 /// @brief Determine if a cast is a no-op.
1726 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1727 switch (getOpcode()) {
1729 assert(!"Invalid CastOp");
1730 case Instruction::Trunc:
1731 case Instruction::ZExt:
1732 case Instruction::SExt:
1733 case Instruction::FPTrunc:
1734 case Instruction::FPExt:
1735 case Instruction::UIToFP:
1736 case Instruction::SIToFP:
1737 case Instruction::FPToUI:
1738 case Instruction::FPToSI:
1739 return false; // These always modify bits
1740 case Instruction::BitCast:
1741 return true; // BitCast never modifies bits.
1742 case Instruction::PtrToInt:
1743 return IntPtrTy->getPrimitiveSizeInBits() ==
1744 getType()->getPrimitiveSizeInBits();
1745 case Instruction::IntToPtr:
1746 return IntPtrTy->getPrimitiveSizeInBits() ==
1747 getOperand(0)->getType()->getPrimitiveSizeInBits();
1751 /// This function determines if a pair of casts can be eliminated and what
1752 /// opcode should be used in the elimination. This assumes that there are two
1753 /// instructions like this:
1754 /// * %F = firstOpcode SrcTy %x to MidTy
1755 /// * %S = secondOpcode MidTy %F to DstTy
1756 /// The function returns a resultOpcode so these two casts can be replaced with:
1757 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1758 /// If no such cast is permited, the function returns 0.
1759 unsigned CastInst::isEliminableCastPair(
1760 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1761 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1763 // Define the 144 possibilities for these two cast instructions. The values
1764 // in this matrix determine what to do in a given situation and select the
1765 // case in the switch below. The rows correspond to firstOp, the columns
1766 // correspond to secondOp. In looking at the table below, keep in mind
1767 // the following cast properties:
1769 // Size Compare Source Destination
1770 // Operator Src ? Size Type Sign Type Sign
1771 // -------- ------------ ------------------- ---------------------
1772 // TRUNC > Integer Any Integral Any
1773 // ZEXT < Integral Unsigned Integer Any
1774 // SEXT < Integral Signed Integer Any
1775 // FPTOUI n/a FloatPt n/a Integral Unsigned
1776 // FPTOSI n/a FloatPt n/a Integral Signed
1777 // UITOFP n/a Integral Unsigned FloatPt n/a
1778 // SITOFP n/a Integral Signed FloatPt n/a
1779 // FPTRUNC > FloatPt n/a FloatPt n/a
1780 // FPEXT < FloatPt n/a FloatPt n/a
1781 // PTRTOINT n/a Pointer n/a Integral Unsigned
1782 // INTTOPTR n/a Integral Unsigned Pointer n/a
1783 // BITCONVERT = FirstClass n/a FirstClass n/a
1785 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1786 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1787 // into "fptoui double to ulong", but this loses information about the range
1788 // of the produced value (we no longer know the top-part is all zeros).
1789 // Further this conversion is often much more expensive for typical hardware,
1790 // and causes issues when building libgcc. We disallow fptosi+sext for the
1792 const unsigned numCastOps =
1793 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1794 static const uint8_t CastResults[numCastOps][numCastOps] = {
1795 // T F F U S F F P I B -+
1796 // R Z S P P I I T P 2 N T |
1797 // U E E 2 2 2 2 R E I T C +- secondOp
1798 // N X X U S F F N X N 2 V |
1799 // C T T I I P P C T T P T -+
1800 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1801 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1802 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
1803 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1804 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
1805 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1806 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1807 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1808 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1809 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1810 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1811 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1814 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1815 [secondOp-Instruction::CastOpsBegin];
1818 // categorically disallowed
1821 // allowed, use first cast's opcode
1824 // allowed, use second cast's opcode
1827 // no-op cast in second op implies firstOp as long as the DestTy
1829 if (DstTy->isInteger())
1833 // no-op cast in second op implies firstOp as long as the DestTy
1834 // is floating point
1835 if (DstTy->isFloatingPoint())
1839 // no-op cast in first op implies secondOp as long as the SrcTy
1841 if (SrcTy->isInteger())
1845 // no-op cast in first op implies secondOp as long as the SrcTy
1846 // is a floating point
1847 if (SrcTy->isFloatingPoint())
1851 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1852 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1853 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1854 if (MidSize >= PtrSize)
1855 return Instruction::BitCast;
1859 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1860 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1861 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1862 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1863 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1864 if (SrcSize == DstSize)
1865 return Instruction::BitCast;
1866 else if (SrcSize < DstSize)
1870 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1871 return Instruction::ZExt;
1873 // fpext followed by ftrunc is allowed if the bit size returned to is
1874 // the same as the original, in which case its just a bitcast
1876 return Instruction::BitCast;
1877 return 0; // If the types are not the same we can't eliminate it.
1879 // bitcast followed by ptrtoint is allowed as long as the bitcast
1880 // is a pointer to pointer cast.
1881 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1885 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1886 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1890 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1891 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1892 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1893 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1894 if (SrcSize <= PtrSize && SrcSize == DstSize)
1895 return Instruction::BitCast;
1899 // cast combination can't happen (error in input). This is for all cases
1900 // where the MidTy is not the same for the two cast instructions.
1901 assert(!"Invalid Cast Combination");
1904 assert(!"Error in CastResults table!!!");
1910 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
1911 const std::string &Name, Instruction *InsertBefore) {
1912 // Construct and return the appropriate CastInst subclass
1914 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1915 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1916 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1917 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1918 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1919 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1920 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1921 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1922 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1923 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1924 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1925 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1927 assert(!"Invalid opcode provided");
1932 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
1933 const std::string &Name, BasicBlock *InsertAtEnd) {
1934 // Construct and return the appropriate CastInst subclass
1936 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1937 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1938 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1939 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1940 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1941 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1942 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1943 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1944 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1945 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1946 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1947 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1949 assert(!"Invalid opcode provided");
1954 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
1955 const std::string &Name,
1956 Instruction *InsertBefore) {
1957 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1958 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1959 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1962 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
1963 const std::string &Name,
1964 BasicBlock *InsertAtEnd) {
1965 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1966 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1967 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1970 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
1971 const std::string &Name,
1972 Instruction *InsertBefore) {
1973 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1974 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1975 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
1978 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
1979 const std::string &Name,
1980 BasicBlock *InsertAtEnd) {
1981 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1982 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1983 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1986 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
1987 const std::string &Name,
1988 Instruction *InsertBefore) {
1989 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1990 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1991 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1994 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
1995 const std::string &Name,
1996 BasicBlock *InsertAtEnd) {
1997 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1998 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1999 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
2002 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2003 const std::string &Name,
2004 BasicBlock *InsertAtEnd) {
2005 assert(isa<PointerType>(S->getType()) && "Invalid cast");
2006 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
2009 if (Ty->isInteger())
2010 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2011 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2014 /// @brief Create a BitCast or a PtrToInt cast instruction
2015 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
2016 const std::string &Name,
2017 Instruction *InsertBefore) {
2018 assert(isa<PointerType>(S->getType()) && "Invalid cast");
2019 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
2022 if (Ty->isInteger())
2023 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2024 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2027 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2028 bool isSigned, const std::string &Name,
2029 Instruction *InsertBefore) {
2030 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
2031 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2032 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2033 Instruction::CastOps opcode =
2034 (SrcBits == DstBits ? Instruction::BitCast :
2035 (SrcBits > DstBits ? Instruction::Trunc :
2036 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2037 return Create(opcode, C, Ty, Name, InsertBefore);
2040 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2041 bool isSigned, const std::string &Name,
2042 BasicBlock *InsertAtEnd) {
2043 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
2044 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2045 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2046 Instruction::CastOps opcode =
2047 (SrcBits == DstBits ? Instruction::BitCast :
2048 (SrcBits > DstBits ? Instruction::Trunc :
2049 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2050 return Create(opcode, C, Ty, Name, InsertAtEnd);
2053 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2054 const std::string &Name,
2055 Instruction *InsertBefore) {
2056 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2058 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2059 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2060 Instruction::CastOps opcode =
2061 (SrcBits == DstBits ? Instruction::BitCast :
2062 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2063 return Create(opcode, C, Ty, Name, InsertBefore);
2066 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2067 const std::string &Name,
2068 BasicBlock *InsertAtEnd) {
2069 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2071 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2072 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2073 Instruction::CastOps opcode =
2074 (SrcBits == DstBits ? Instruction::BitCast :
2075 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2076 return Create(opcode, C, Ty, Name, InsertAtEnd);
2079 // Check whether it is valid to call getCastOpcode for these types.
2080 // This routine must be kept in sync with getCastOpcode.
2081 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2082 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2085 if (SrcTy == DestTy)
2088 // Get the bit sizes, we'll need these
2089 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2090 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2092 // Run through the possibilities ...
2093 if (DestTy->isInteger()) { // Casting to integral
2094 if (SrcTy->isInteger()) { // Casting from integral
2096 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2098 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2099 // Casting from vector
2100 return DestBits == PTy->getBitWidth();
2101 } else { // Casting from something else
2102 return isa<PointerType>(SrcTy);
2104 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2105 if (SrcTy->isInteger()) { // Casting from integral
2107 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2109 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2110 // Casting from vector
2111 return DestBits == PTy->getBitWidth();
2112 } else { // Casting from something else
2115 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2116 // Casting to vector
2117 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2118 // Casting from vector
2119 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2120 } else { // Casting from something else
2121 return DestPTy->getBitWidth() == SrcBits;
2123 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2124 if (isa<PointerType>(SrcTy)) { // Casting from pointer
2126 } else if (SrcTy->isInteger()) { // Casting from integral
2128 } else { // Casting from something else
2131 } else { // Casting to something else
2136 // Provide a way to get a "cast" where the cast opcode is inferred from the
2137 // types and size of the operand. This, basically, is a parallel of the
2138 // logic in the castIsValid function below. This axiom should hold:
2139 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2140 // should not assert in castIsValid. In other words, this produces a "correct"
2141 // casting opcode for the arguments passed to it.
2142 // This routine must be kept in sync with isCastable.
2143 Instruction::CastOps
2144 CastInst::getCastOpcode(
2145 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
2146 // Get the bit sizes, we'll need these
2147 const Type *SrcTy = Src->getType();
2148 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2149 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2151 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2152 "Only first class types are castable!");
2154 // Run through the possibilities ...
2155 if (DestTy->isInteger()) { // Casting to integral
2156 if (SrcTy->isInteger()) { // Casting from integral
2157 if (DestBits < SrcBits)
2158 return Trunc; // int -> smaller int
2159 else if (DestBits > SrcBits) { // its an extension
2161 return SExt; // signed -> SEXT
2163 return ZExt; // unsigned -> ZEXT
2165 return BitCast; // Same size, No-op cast
2167 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2169 return FPToSI; // FP -> sint
2171 return FPToUI; // FP -> uint
2172 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2173 assert(DestBits == PTy->getBitWidth() &&
2174 "Casting vector to integer of different width");
2176 return BitCast; // Same size, no-op cast
2178 assert(isa<PointerType>(SrcTy) &&
2179 "Casting from a value that is not first-class type");
2180 return PtrToInt; // ptr -> int
2182 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2183 if (SrcTy->isInteger()) { // Casting from integral
2185 return SIToFP; // sint -> FP
2187 return UIToFP; // uint -> FP
2188 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2189 if (DestBits < SrcBits) {
2190 return FPTrunc; // FP -> smaller FP
2191 } else if (DestBits > SrcBits) {
2192 return FPExt; // FP -> larger FP
2194 return BitCast; // same size, no-op cast
2196 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2197 assert(DestBits == PTy->getBitWidth() &&
2198 "Casting vector to floating point of different width");
2200 return BitCast; // same size, no-op cast
2202 assert(0 && "Casting pointer or non-first class to float");
2204 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2205 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2206 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
2207 "Casting vector to vector of different widths");
2208 return BitCast; // vector -> vector
2209 } else if (DestPTy->getBitWidth() == SrcBits) {
2210 return BitCast; // float/int -> vector
2212 assert(!"Illegal cast to vector (wrong type or size)");
2214 } else if (isa<PointerType>(DestTy)) {
2215 if (isa<PointerType>(SrcTy)) {
2216 return BitCast; // ptr -> ptr
2217 } else if (SrcTy->isInteger()) {
2218 return IntToPtr; // int -> ptr
2220 assert(!"Casting pointer to other than pointer or int");
2223 assert(!"Casting to type that is not first-class");
2226 // If we fall through to here we probably hit an assertion cast above
2227 // and assertions are not turned on. Anything we return is an error, so
2228 // BitCast is as good a choice as any.
2232 //===----------------------------------------------------------------------===//
2233 // CastInst SubClass Constructors
2234 //===----------------------------------------------------------------------===//
2236 /// Check that the construction parameters for a CastInst are correct. This
2237 /// could be broken out into the separate constructors but it is useful to have
2238 /// it in one place and to eliminate the redundant code for getting the sizes
2239 /// of the types involved.
2241 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
2243 // Check for type sanity on the arguments
2244 const Type *SrcTy = S->getType();
2245 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2248 // Get the size of the types in bits, we'll need this later
2249 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2250 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2252 // Switch on the opcode provided
2254 default: return false; // This is an input error
2255 case Instruction::Trunc:
2256 return SrcTy->isIntOrIntVector() &&
2257 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
2258 case Instruction::ZExt:
2259 return SrcTy->isIntOrIntVector() &&
2260 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
2261 case Instruction::SExt:
2262 return SrcTy->isIntOrIntVector() &&
2263 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
2264 case Instruction::FPTrunc:
2265 return SrcTy->isFPOrFPVector() &&
2266 DstTy->isFPOrFPVector() &&
2267 SrcBitSize > DstBitSize;
2268 case Instruction::FPExt:
2269 return SrcTy->isFPOrFPVector() &&
2270 DstTy->isFPOrFPVector() &&
2271 SrcBitSize < DstBitSize;
2272 case Instruction::UIToFP:
2273 case Instruction::SIToFP:
2274 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2275 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2276 return SVTy->getElementType()->isIntOrIntVector() &&
2277 DVTy->getElementType()->isFPOrFPVector() &&
2278 SVTy->getNumElements() == DVTy->getNumElements();
2281 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
2282 case Instruction::FPToUI:
2283 case Instruction::FPToSI:
2284 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2285 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2286 return SVTy->getElementType()->isFPOrFPVector() &&
2287 DVTy->getElementType()->isIntOrIntVector() &&
2288 SVTy->getNumElements() == DVTy->getNumElements();
2291 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
2292 case Instruction::PtrToInt:
2293 return isa<PointerType>(SrcTy) && DstTy->isInteger();
2294 case Instruction::IntToPtr:
2295 return SrcTy->isInteger() && isa<PointerType>(DstTy);
2296 case Instruction::BitCast:
2297 // BitCast implies a no-op cast of type only. No bits change.
2298 // However, you can't cast pointers to anything but pointers.
2299 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2302 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2303 // these cases, the cast is okay if the source and destination bit widths
2305 return SrcBitSize == DstBitSize;
2309 TruncInst::TruncInst(
2310 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2311 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2312 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2315 TruncInst::TruncInst(
2316 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2317 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2318 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2322 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2323 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2324 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2328 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2329 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2330 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2333 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2334 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2335 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2339 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2340 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2341 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2344 FPTruncInst::FPTruncInst(
2345 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2346 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2347 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2350 FPTruncInst::FPTruncInst(
2351 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2352 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2353 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2356 FPExtInst::FPExtInst(
2357 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2358 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2359 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2362 FPExtInst::FPExtInst(
2363 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2364 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2365 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2368 UIToFPInst::UIToFPInst(
2369 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2370 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2371 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2374 UIToFPInst::UIToFPInst(
2375 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2376 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2377 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2380 SIToFPInst::SIToFPInst(
2381 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2382 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2383 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2386 SIToFPInst::SIToFPInst(
2387 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2388 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2389 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2392 FPToUIInst::FPToUIInst(
2393 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2394 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2395 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2398 FPToUIInst::FPToUIInst(
2399 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2400 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2401 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2404 FPToSIInst::FPToSIInst(
2405 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2406 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2407 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2410 FPToSIInst::FPToSIInst(
2411 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2412 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2413 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2416 PtrToIntInst::PtrToIntInst(
2417 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2418 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2419 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2422 PtrToIntInst::PtrToIntInst(
2423 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2424 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2425 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2428 IntToPtrInst::IntToPtrInst(
2429 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2430 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2431 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2434 IntToPtrInst::IntToPtrInst(
2435 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2436 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2437 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2440 BitCastInst::BitCastInst(
2441 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2442 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2443 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2446 BitCastInst::BitCastInst(
2447 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2448 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2449 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2452 //===----------------------------------------------------------------------===//
2454 //===----------------------------------------------------------------------===//
2456 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2457 Value *LHS, Value *RHS, const std::string &Name,
2458 Instruction *InsertBefore)
2459 : Instruction(ty, op,
2460 OperandTraits<CmpInst>::op_begin(this),
2461 OperandTraits<CmpInst>::operands(this),
2465 SubclassData = predicate;
2469 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2470 Value *LHS, Value *RHS, const std::string &Name,
2471 BasicBlock *InsertAtEnd)
2472 : Instruction(ty, op,
2473 OperandTraits<CmpInst>::op_begin(this),
2474 OperandTraits<CmpInst>::operands(this),
2478 SubclassData = predicate;
2483 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2484 const std::string &Name, Instruction *InsertBefore) {
2485 if (Op == Instruction::ICmp) {
2486 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2489 if (Op == Instruction::FCmp) {
2490 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2493 if (Op == Instruction::VICmp) {
2494 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2497 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2502 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2503 const std::string &Name, BasicBlock *InsertAtEnd) {
2504 if (Op == Instruction::ICmp) {
2505 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2508 if (Op == Instruction::FCmp) {
2509 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2512 if (Op == Instruction::VICmp) {
2513 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2516 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2520 void CmpInst::swapOperands() {
2521 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2524 cast<FCmpInst>(this)->swapOperands();
2527 bool CmpInst::isCommutative() {
2528 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2529 return IC->isCommutative();
2530 return cast<FCmpInst>(this)->isCommutative();
2533 bool CmpInst::isEquality() {
2534 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2535 return IC->isEquality();
2536 return cast<FCmpInst>(this)->isEquality();
2540 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
2542 default: assert(!"Unknown cmp predicate!");
2543 case ICMP_EQ: return ICMP_NE;
2544 case ICMP_NE: return ICMP_EQ;
2545 case ICMP_UGT: return ICMP_ULE;
2546 case ICMP_ULT: return ICMP_UGE;
2547 case ICMP_UGE: return ICMP_ULT;
2548 case ICMP_ULE: return ICMP_UGT;
2549 case ICMP_SGT: return ICMP_SLE;
2550 case ICMP_SLT: return ICMP_SGE;
2551 case ICMP_SGE: return ICMP_SLT;
2552 case ICMP_SLE: return ICMP_SGT;
2554 case FCMP_OEQ: return FCMP_UNE;
2555 case FCMP_ONE: return FCMP_UEQ;
2556 case FCMP_OGT: return FCMP_ULE;
2557 case FCMP_OLT: return FCMP_UGE;
2558 case FCMP_OGE: return FCMP_ULT;
2559 case FCMP_OLE: return FCMP_UGT;
2560 case FCMP_UEQ: return FCMP_ONE;
2561 case FCMP_UNE: return FCMP_OEQ;
2562 case FCMP_UGT: return FCMP_OLE;
2563 case FCMP_ULT: return FCMP_OGE;
2564 case FCMP_UGE: return FCMP_OLT;
2565 case FCMP_ULE: return FCMP_OGT;
2566 case FCMP_ORD: return FCMP_UNO;
2567 case FCMP_UNO: return FCMP_ORD;
2568 case FCMP_TRUE: return FCMP_FALSE;
2569 case FCMP_FALSE: return FCMP_TRUE;
2573 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2575 default: assert(! "Unknown icmp predicate!");
2576 case ICMP_EQ: case ICMP_NE:
2577 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2579 case ICMP_UGT: return ICMP_SGT;
2580 case ICMP_ULT: return ICMP_SLT;
2581 case ICMP_UGE: return ICMP_SGE;
2582 case ICMP_ULE: return ICMP_SLE;
2586 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2588 default: assert(! "Unknown icmp predicate!");
2589 case ICMP_EQ: case ICMP_NE:
2590 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2592 case ICMP_SGT: return ICMP_UGT;
2593 case ICMP_SLT: return ICMP_ULT;
2594 case ICMP_SGE: return ICMP_UGE;
2595 case ICMP_SLE: return ICMP_ULE;
2599 bool ICmpInst::isSignedPredicate(Predicate pred) {
2601 default: assert(! "Unknown icmp predicate!");
2602 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2604 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2605 case ICMP_UGE: case ICMP_ULE:
2610 /// Initialize a set of values that all satisfy the condition with C.
2613 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2616 uint32_t BitWidth = C.getBitWidth();
2618 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2619 case ICmpInst::ICMP_EQ: Upper++; break;
2620 case ICmpInst::ICMP_NE: Lower++; break;
2621 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2622 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2623 case ICmpInst::ICMP_UGT:
2624 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2626 case ICmpInst::ICMP_SGT:
2627 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2629 case ICmpInst::ICMP_ULE:
2630 Lower = APInt::getMinValue(BitWidth); Upper++;
2632 case ICmpInst::ICMP_SLE:
2633 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2635 case ICmpInst::ICMP_UGE:
2636 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2638 case ICmpInst::ICMP_SGE:
2639 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2642 return ConstantRange(Lower, Upper);
2645 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
2647 default: assert(!"Unknown cmp predicate!");
2648 case ICMP_EQ: case ICMP_NE:
2650 case ICMP_SGT: return ICMP_SLT;
2651 case ICMP_SLT: return ICMP_SGT;
2652 case ICMP_SGE: return ICMP_SLE;
2653 case ICMP_SLE: return ICMP_SGE;
2654 case ICMP_UGT: return ICMP_ULT;
2655 case ICMP_ULT: return ICMP_UGT;
2656 case ICMP_UGE: return ICMP_ULE;
2657 case ICMP_ULE: return ICMP_UGE;
2659 case FCMP_FALSE: case FCMP_TRUE:
2660 case FCMP_OEQ: case FCMP_ONE:
2661 case FCMP_UEQ: case FCMP_UNE:
2662 case FCMP_ORD: case FCMP_UNO:
2664 case FCMP_OGT: return FCMP_OLT;
2665 case FCMP_OLT: return FCMP_OGT;
2666 case FCMP_OGE: return FCMP_OLE;
2667 case FCMP_OLE: return FCMP_OGE;
2668 case FCMP_UGT: return FCMP_ULT;
2669 case FCMP_ULT: return FCMP_UGT;
2670 case FCMP_UGE: return FCMP_ULE;
2671 case FCMP_ULE: return FCMP_UGE;
2675 bool CmpInst::isUnsigned(unsigned short predicate) {
2676 switch (predicate) {
2677 default: return false;
2678 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2679 case ICmpInst::ICMP_UGE: return true;
2683 bool CmpInst::isSigned(unsigned short predicate){
2684 switch (predicate) {
2685 default: return false;
2686 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2687 case ICmpInst::ICMP_SGE: return true;
2691 bool CmpInst::isOrdered(unsigned short predicate) {
2692 switch (predicate) {
2693 default: return false;
2694 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2695 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2696 case FCmpInst::FCMP_ORD: return true;
2700 bool CmpInst::isUnordered(unsigned short predicate) {
2701 switch (predicate) {
2702 default: return false;
2703 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2704 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2705 case FCmpInst::FCMP_UNO: return true;
2709 //===----------------------------------------------------------------------===//
2710 // SwitchInst Implementation
2711 //===----------------------------------------------------------------------===//
2713 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
2714 assert(Value && Default);
2715 ReservedSpace = 2+NumCases*2;
2717 OperandList = allocHungoffUses(ReservedSpace);
2719 OperandList[0] = Value;
2720 OperandList[1] = Default;
2723 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2724 /// switch on and a default destination. The number of additional cases can
2725 /// be specified here to make memory allocation more efficient. This
2726 /// constructor can also autoinsert before another instruction.
2727 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2728 Instruction *InsertBefore)
2729 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2730 init(Value, Default, NumCases);
2733 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2734 /// switch on and a default destination. The number of additional cases can
2735 /// be specified here to make memory allocation more efficient. This
2736 /// constructor also autoinserts at the end of the specified BasicBlock.
2737 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2738 BasicBlock *InsertAtEnd)
2739 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2740 init(Value, Default, NumCases);
2743 SwitchInst::SwitchInst(const SwitchInst &SI)
2744 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2745 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
2746 Use *OL = OperandList, *InOL = SI.OperandList;
2747 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2749 OL[i+1] = InOL[i+1];
2753 SwitchInst::~SwitchInst() {
2754 dropHungoffUses(OperandList);
2758 /// addCase - Add an entry to the switch instruction...
2760 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
2761 unsigned OpNo = NumOperands;
2762 if (OpNo+2 > ReservedSpace)
2763 resizeOperands(0); // Get more space!
2764 // Initialize some new operands.
2765 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
2766 NumOperands = OpNo+2;
2767 OperandList[OpNo] = OnVal;
2768 OperandList[OpNo+1] = Dest;
2771 /// removeCase - This method removes the specified successor from the switch
2772 /// instruction. Note that this cannot be used to remove the default
2773 /// destination (successor #0).
2775 void SwitchInst::removeCase(unsigned idx) {
2776 assert(idx != 0 && "Cannot remove the default case!");
2777 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2779 unsigned NumOps = getNumOperands();
2780 Use *OL = OperandList;
2782 // Move everything after this operand down.
2784 // FIXME: we could just swap with the end of the list, then erase. However,
2785 // client might not expect this to happen. The code as it is thrashes the
2786 // use/def lists, which is kinda lame.
2787 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2789 OL[i-2+1] = OL[i+1];
2792 // Nuke the last value.
2793 OL[NumOps-2].set(0);
2794 OL[NumOps-2+1].set(0);
2795 NumOperands = NumOps-2;
2798 /// resizeOperands - resize operands - This adjusts the length of the operands
2799 /// list according to the following behavior:
2800 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
2801 /// of operation. This grows the number of ops by 3 times.
2802 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2803 /// 3. If NumOps == NumOperands, trim the reserved space.
2805 void SwitchInst::resizeOperands(unsigned NumOps) {
2806 unsigned e = getNumOperands();
2809 } else if (NumOps*2 > NumOperands) {
2810 // No resize needed.
2811 if (ReservedSpace >= NumOps) return;
2812 } else if (NumOps == NumOperands) {
2813 if (ReservedSpace == NumOps) return;
2818 ReservedSpace = NumOps;
2819 Use *NewOps = allocHungoffUses(NumOps);
2820 Use *OldOps = OperandList;
2821 for (unsigned i = 0; i != e; ++i) {
2822 NewOps[i] = OldOps[i];
2824 OperandList = NewOps;
2825 if (OldOps) Use::zap(OldOps, OldOps + e, true);
2829 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2830 return getSuccessor(idx);
2832 unsigned SwitchInst::getNumSuccessorsV() const {
2833 return getNumSuccessors();
2835 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2836 setSuccessor(idx, B);
2839 // Define these methods here so vtables don't get emitted into every translation
2840 // unit that uses these classes.
2842 GetElementPtrInst *GetElementPtrInst::clone() const {
2843 return new(getNumOperands()) GetElementPtrInst(*this);
2846 BinaryOperator *BinaryOperator::clone() const {
2847 return Create(getOpcode(), Op<0>(), Op<1>());
2850 FCmpInst* FCmpInst::clone() const {
2851 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
2853 ICmpInst* ICmpInst::clone() const {
2854 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
2857 VFCmpInst* VFCmpInst::clone() const {
2858 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2860 VICmpInst* VICmpInst::clone() const {
2861 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2864 ExtractValueInst *ExtractValueInst::clone() const {
2865 return new ExtractValueInst(*this);
2867 InsertValueInst *InsertValueInst::clone() const {
2868 return new InsertValueInst(*this);
2872 MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2873 AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2874 FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2875 LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2876 StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2877 CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2878 CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2879 CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2880 CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2881 CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2882 CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2883 CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2884 CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2885 CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2886 CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2887 CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2888 CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2889 CallInst *CallInst::clone() const {
2890 return new(getNumOperands()) CallInst(*this);
2892 SelectInst *SelectInst::clone() const {
2893 return new(getNumOperands()) SelectInst(*this);
2895 VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2897 ExtractElementInst *ExtractElementInst::clone() const {
2898 return new ExtractElementInst(*this);
2900 InsertElementInst *InsertElementInst::clone() const {
2901 return InsertElementInst::Create(*this);
2903 ShuffleVectorInst *ShuffleVectorInst::clone() const {
2904 return new ShuffleVectorInst(*this);
2906 PHINode *PHINode::clone() const { return new PHINode(*this); }
2907 ReturnInst *ReturnInst::clone() const {
2908 return new(getNumOperands()) ReturnInst(*this);
2910 BranchInst *BranchInst::clone() const {
2911 return new(getNumOperands()) BranchInst(*this);
2913 SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2914 InvokeInst *InvokeInst::clone() const {
2915 return new(getNumOperands()) InvokeInst(*this);
2917 UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
2918 UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}