1 //===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements all of the non-inline methods for the LLVM instruction
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Support/CallSite.h"
20 #include "llvm/Support/ConstantRange.h"
21 #include "llvm/Support/MathExtras.h"
24 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
28 CallSite::CallSite(Instruction *C) {
29 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
32 unsigned CallSite::getCallingConv() const {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 return CI->getCallingConv();
36 return cast<InvokeInst>(I)->getCallingConv();
38 void CallSite::setCallingConv(unsigned CC) {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 CI->setCallingConv(CC);
42 cast<InvokeInst>(I)->setCallingConv(CC);
44 const PAListPtr &CallSite::getParamAttrs() const {
45 if (CallInst *CI = dyn_cast<CallInst>(I))
46 return CI->getParamAttrs();
48 return cast<InvokeInst>(I)->getParamAttrs();
50 void CallSite::setParamAttrs(const PAListPtr &PAL) {
51 if (CallInst *CI = dyn_cast<CallInst>(I))
52 CI->setParamAttrs(PAL);
54 cast<InvokeInst>(I)->setParamAttrs(PAL);
56 bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
57 if (CallInst *CI = dyn_cast<CallInst>(I))
58 return CI->paramHasAttr(i, attr);
60 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
62 uint16_t CallSite::getParamAlignment(uint16_t i) const {
63 if (CallInst *CI = dyn_cast<CallInst>(I))
64 return CI->getParamAlignment(i);
66 return cast<InvokeInst>(I)->getParamAlignment(i);
69 bool CallSite::doesNotAccessMemory() const {
70 if (CallInst *CI = dyn_cast<CallInst>(I))
71 return CI->doesNotAccessMemory();
73 return cast<InvokeInst>(I)->doesNotAccessMemory();
75 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 setParamAttrs(CI.getParamAttrs());
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::addParamAttr(unsigned i, ParameterAttributes attr) {
406 PAListPtr PAL = getParamAttrs();
407 PAL = PAL.addAttr(i, attr);
411 void CallInst::removeParamAttr(unsigned i, ParameterAttributes attr) {
412 PAListPtr PAL = getParamAttrs();
413 PAL = PAL.removeAttr(i, attr);
417 bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
418 if (ParamAttrs.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 setParamAttrs(II.getParamAttrs());
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, ParameterAttributes attr) const {
477 if (ParamAttrs.paramHasAttr(i, attr))
479 if (const Function *F = getCalledFunction())
480 return F->paramHasAttr(i, attr);
484 void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
485 PAListPtr PAL = getParamAttrs();
486 PAL = PAL.addAttr(i, attr);
490 void InvokeInst::removeParamAttr(unsigned i, ParameterAttributes attr) {
491 PAListPtr PAL = getParamAttrs();
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(isa<PointerType>(getOperand(1)->getType()) &&
878 "Ptr must have pointer type!");
879 assert(getOperand(0)->getType() ==
880 cast<PointerType>(getOperand(1)->getType())->getElementType()
881 && "Ptr must be a pointer to Val type!");
885 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
886 : Instruction(Type::VoidTy, Store,
887 OperandTraits<StoreInst>::op_begin(this),
888 OperandTraits<StoreInst>::operands(this),
897 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
898 : Instruction(Type::VoidTy, Store,
899 OperandTraits<StoreInst>::op_begin(this),
900 OperandTraits<StoreInst>::operands(this),
909 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
910 Instruction *InsertBefore)
911 : Instruction(Type::VoidTy, Store,
912 OperandTraits<StoreInst>::op_begin(this),
913 OperandTraits<StoreInst>::operands(this),
917 setVolatile(isVolatile);
922 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
923 unsigned Align, Instruction *InsertBefore)
924 : Instruction(Type::VoidTy, Store,
925 OperandTraits<StoreInst>::op_begin(this),
926 OperandTraits<StoreInst>::operands(this),
930 setVolatile(isVolatile);
935 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
936 unsigned Align, BasicBlock *InsertAtEnd)
937 : Instruction(Type::VoidTy, Store,
938 OperandTraits<StoreInst>::op_begin(this),
939 OperandTraits<StoreInst>::operands(this),
943 setVolatile(isVolatile);
948 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
949 BasicBlock *InsertAtEnd)
950 : Instruction(Type::VoidTy, Store,
951 OperandTraits<StoreInst>::op_begin(this),
952 OperandTraits<StoreInst>::operands(this),
956 setVolatile(isVolatile);
961 void StoreInst::setAlignment(unsigned Align) {
962 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
963 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
966 //===----------------------------------------------------------------------===//
967 // GetElementPtrInst Implementation
968 //===----------------------------------------------------------------------===//
970 static unsigned retrieveAddrSpace(const Value *Val) {
971 return cast<PointerType>(Val->getType())->getAddressSpace();
974 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
975 const std::string &Name) {
976 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
977 Use *OL = OperandList;
980 for (unsigned i = 0; i != NumIdx; ++i)
986 void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
987 assert(NumOperands == 2 && "NumOperands not initialized?");
988 Use *OL = OperandList;
995 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
996 : Instruction(GEPI.getType(), GetElementPtr,
997 OperandTraits<GetElementPtrInst>::op_end(this)
998 - GEPI.getNumOperands(),
999 GEPI.getNumOperands()) {
1000 Use *OL = OperandList;
1001 Use *GEPIOL = GEPI.OperandList;
1002 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1006 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1007 const std::string &Name, Instruction *InBe)
1008 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
1009 retrieveAddrSpace(Ptr)),
1011 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1013 init(Ptr, Idx, Name);
1016 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1017 const std::string &Name, BasicBlock *IAE)
1018 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
1019 retrieveAddrSpace(Ptr)),
1021 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1023 init(Ptr, Idx, Name);
1026 // getIndexedType - Returns the type of the element that would be loaded with
1027 // a load instruction with the specified parameters.
1029 // A null type is returned if the indices are invalid for the specified
1032 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1035 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1036 if (!PTy) return 0; // Type isn't a pointer type!
1037 const Type *Agg = PTy->getElementType();
1039 // Handle the special case of the empty set index set...
1043 unsigned CurIdx = 1;
1044 for (; CurIdx != NumIdx; ++CurIdx) {
1045 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1046 if (!CT || isa<PointerType>(CT)) return 0;
1047 Value *Index = Idxs[CurIdx];
1048 if (!CT->indexValid(Index)) return 0;
1049 Agg = CT->getTypeAtIndex(Index);
1051 // If the new type forwards to another type, then it is in the middle
1052 // of being refined to another type (and hence, may have dropped all
1053 // references to what it was using before). So, use the new forwarded
1055 if (const Type *Ty = Agg->getForwardedType())
1058 return CurIdx == NumIdx ? Agg : 0;
1061 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1062 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1063 if (!PTy) return 0; // Type isn't a pointer type!
1065 // Check the pointer index.
1066 if (!PTy->indexValid(Idx)) return 0;
1068 return PTy->getElementType();
1072 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1073 /// zeros. If so, the result pointer and the first operand have the same
1074 /// value, just potentially different types.
1075 bool GetElementPtrInst::hasAllZeroIndices() const {
1076 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1077 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1078 if (!CI->isZero()) return false;
1086 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1087 /// constant integers. If so, the result pointer and the first operand have
1088 /// a constant offset between them.
1089 bool GetElementPtrInst::hasAllConstantIndices() const {
1090 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1091 if (!isa<ConstantInt>(getOperand(i)))
1098 //===----------------------------------------------------------------------===//
1099 // ExtractElementInst Implementation
1100 //===----------------------------------------------------------------------===//
1102 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1103 const std::string &Name,
1104 Instruction *InsertBef)
1105 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1107 OperandTraits<ExtractElementInst>::op_begin(this),
1109 assert(isValidOperands(Val, Index) &&
1110 "Invalid extractelement instruction operands!");
1116 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1117 const std::string &Name,
1118 Instruction *InsertBef)
1119 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1121 OperandTraits<ExtractElementInst>::op_begin(this),
1123 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1124 assert(isValidOperands(Val, Index) &&
1125 "Invalid extractelement instruction operands!");
1132 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1133 const std::string &Name,
1134 BasicBlock *InsertAE)
1135 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1137 OperandTraits<ExtractElementInst>::op_begin(this),
1139 assert(isValidOperands(Val, Index) &&
1140 "Invalid extractelement instruction operands!");
1147 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1148 const std::string &Name,
1149 BasicBlock *InsertAE)
1150 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1152 OperandTraits<ExtractElementInst>::op_begin(this),
1154 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1155 assert(isValidOperands(Val, Index) &&
1156 "Invalid extractelement instruction operands!");
1164 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1165 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
1171 //===----------------------------------------------------------------------===//
1172 // InsertElementInst Implementation
1173 //===----------------------------------------------------------------------===//
1175 InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1176 : Instruction(IE.getType(), InsertElement,
1177 OperandTraits<InsertElementInst>::op_begin(this), 3) {
1178 Op<0>() = IE.Op<0>();
1179 Op<1>() = IE.Op<1>();
1180 Op<2>() = IE.Op<2>();
1182 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1183 const std::string &Name,
1184 Instruction *InsertBef)
1185 : Instruction(Vec->getType(), InsertElement,
1186 OperandTraits<InsertElementInst>::op_begin(this),
1188 assert(isValidOperands(Vec, Elt, Index) &&
1189 "Invalid insertelement instruction operands!");
1196 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1197 const std::string &Name,
1198 Instruction *InsertBef)
1199 : Instruction(Vec->getType(), InsertElement,
1200 OperandTraits<InsertElementInst>::op_begin(this),
1202 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1203 assert(isValidOperands(Vec, Elt, Index) &&
1204 "Invalid insertelement instruction operands!");
1212 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1213 const std::string &Name,
1214 BasicBlock *InsertAE)
1215 : Instruction(Vec->getType(), InsertElement,
1216 OperandTraits<InsertElementInst>::op_begin(this),
1218 assert(isValidOperands(Vec, Elt, Index) &&
1219 "Invalid insertelement instruction operands!");
1227 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1228 const std::string &Name,
1229 BasicBlock *InsertAE)
1230 : Instruction(Vec->getType(), InsertElement,
1231 OperandTraits<InsertElementInst>::op_begin(this),
1233 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1234 assert(isValidOperands(Vec, Elt, Index) &&
1235 "Invalid insertelement instruction operands!");
1243 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1244 const Value *Index) {
1245 if (!isa<VectorType>(Vec->getType()))
1246 return false; // First operand of insertelement must be vector type.
1248 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1249 return false;// Second operand of insertelement must be vector element type.
1251 if (Index->getType() != Type::Int32Ty)
1252 return false; // Third operand of insertelement must be uint.
1257 //===----------------------------------------------------------------------===//
1258 // ShuffleVectorInst Implementation
1259 //===----------------------------------------------------------------------===//
1261 ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1262 : Instruction(SV.getType(), ShuffleVector,
1263 OperandTraits<ShuffleVectorInst>::op_begin(this),
1264 OperandTraits<ShuffleVectorInst>::operands(this)) {
1265 Op<0>() = SV.Op<0>();
1266 Op<1>() = SV.Op<1>();
1267 Op<2>() = SV.Op<2>();
1270 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1271 const std::string &Name,
1272 Instruction *InsertBefore)
1273 : Instruction(V1->getType(), ShuffleVector,
1274 OperandTraits<ShuffleVectorInst>::op_begin(this),
1275 OperandTraits<ShuffleVectorInst>::operands(this),
1277 assert(isValidOperands(V1, V2, Mask) &&
1278 "Invalid shuffle vector instruction operands!");
1285 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1286 const std::string &Name,
1287 BasicBlock *InsertAtEnd)
1288 : Instruction(V1->getType(), ShuffleVector,
1289 OperandTraits<ShuffleVectorInst>::op_begin(this),
1290 OperandTraits<ShuffleVectorInst>::operands(this),
1292 assert(isValidOperands(V1, V2, Mask) &&
1293 "Invalid shuffle vector instruction operands!");
1301 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1302 const Value *Mask) {
1303 if (!isa<VectorType>(V1->getType()) ||
1304 V1->getType() != V2->getType())
1307 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1308 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1309 MaskTy->getElementType() != Type::Int32Ty ||
1310 MaskTy->getNumElements() !=
1311 cast<VectorType>(V1->getType())->getNumElements())
1316 /// getMaskValue - Return the index from the shuffle mask for the specified
1317 /// output result. This is either -1 if the element is undef or a number less
1318 /// than 2*numelements.
1319 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1320 const Constant *Mask = cast<Constant>(getOperand(2));
1321 if (isa<UndefValue>(Mask)) return -1;
1322 if (isa<ConstantAggregateZero>(Mask)) return 0;
1323 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1324 assert(i < MaskCV->getNumOperands() && "Index out of range");
1326 if (isa<UndefValue>(MaskCV->getOperand(i)))
1328 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1331 //===----------------------------------------------------------------------===//
1332 // InsertValueInst Class
1333 //===----------------------------------------------------------------------===//
1335 void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1336 unsigned NumIdx, const std::string &Name) {
1337 assert(NumOperands == 2 && "NumOperands not initialized?");
1341 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
1345 void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1346 const std::string &Name) {
1347 assert(NumOperands == 2 && "NumOperands not initialized?");
1351 Indices.push_back(Idx);
1355 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1356 : Instruction(IVI.getType(), InsertValue,
1357 OperandTraits<InsertValueInst>::op_begin(this), 2),
1358 Indices(IVI.Indices) {
1359 Op<0>() = IVI.getOperand(0);
1360 Op<1>() = IVI.getOperand(1);
1363 InsertValueInst::InsertValueInst(Value *Agg,
1366 const std::string &Name,
1367 Instruction *InsertBefore)
1368 : Instruction(Agg->getType(), InsertValue,
1369 OperandTraits<InsertValueInst>::op_begin(this),
1371 init(Agg, Val, Idx, Name);
1374 InsertValueInst::InsertValueInst(Value *Agg,
1377 const std::string &Name,
1378 BasicBlock *InsertAtEnd)
1379 : Instruction(Agg->getType(), InsertValue,
1380 OperandTraits<InsertValueInst>::op_begin(this),
1382 init(Agg, Val, Idx, Name);
1385 //===----------------------------------------------------------------------===//
1386 // ExtractValueInst Class
1387 //===----------------------------------------------------------------------===//
1389 void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
1390 const std::string &Name) {
1391 assert(NumOperands == 1 && "NumOperands not initialized?");
1393 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
1397 void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
1398 assert(NumOperands == 1 && "NumOperands not initialized?");
1400 Indices.push_back(Idx);
1404 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1405 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
1406 Indices(EVI.Indices) {
1409 // getIndexedType - Returns the type of the element that would be extracted
1410 // with an extractvalue instruction with the specified parameters.
1412 // A null type is returned if the indices are invalid for the specified
1415 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1416 const unsigned *Idxs,
1418 unsigned CurIdx = 0;
1419 for (; CurIdx != NumIdx; ++CurIdx) {
1420 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1421 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1422 unsigned Index = Idxs[CurIdx];
1423 if (!CT->indexValid(Index)) return 0;
1424 Agg = CT->getTypeAtIndex(Index);
1426 // If the new type forwards to another type, then it is in the middle
1427 // of being refined to another type (and hence, may have dropped all
1428 // references to what it was using before). So, use the new forwarded
1430 if (const Type *Ty = Agg->getForwardedType())
1433 return CurIdx == NumIdx ? Agg : 0;
1436 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1438 return getIndexedType(Agg, &Idx, 1);
1441 //===----------------------------------------------------------------------===//
1442 // BinaryOperator Class
1443 //===----------------------------------------------------------------------===//
1445 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1446 const Type *Ty, const std::string &Name,
1447 Instruction *InsertBefore)
1448 : Instruction(Ty, iType,
1449 OperandTraits<BinaryOperator>::op_begin(this),
1450 OperandTraits<BinaryOperator>::operands(this),
1458 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1459 const Type *Ty, const std::string &Name,
1460 BasicBlock *InsertAtEnd)
1461 : Instruction(Ty, iType,
1462 OperandTraits<BinaryOperator>::op_begin(this),
1463 OperandTraits<BinaryOperator>::operands(this),
1472 void BinaryOperator::init(BinaryOps iType) {
1473 Value *LHS = getOperand(0), *RHS = getOperand(1);
1474 LHS = LHS; RHS = RHS; // Silence warnings.
1475 assert(LHS->getType() == RHS->getType() &&
1476 "Binary operator operand types must match!");
1481 assert(getType() == LHS->getType() &&
1482 "Arithmetic operation should return same type as operands!");
1483 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
1484 isa<VectorType>(getType())) &&
1485 "Tried to create an arithmetic operation on a non-arithmetic type!");
1489 assert(getType() == LHS->getType() &&
1490 "Arithmetic operation should return same type as operands!");
1491 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1492 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1493 "Incorrect operand type (not integer) for S/UDIV");
1496 assert(getType() == LHS->getType() &&
1497 "Arithmetic operation should return same type as operands!");
1498 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1499 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
1500 && "Incorrect operand type (not floating point) for FDIV");
1504 assert(getType() == LHS->getType() &&
1505 "Arithmetic operation should return same type as operands!");
1506 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1507 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1508 "Incorrect operand type (not integer) for S/UREM");
1511 assert(getType() == LHS->getType() &&
1512 "Arithmetic operation should return same type as operands!");
1513 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1514 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
1515 && "Incorrect operand type (not floating point) for FREM");
1520 assert(getType() == LHS->getType() &&
1521 "Shift operation should return same type as operands!");
1522 assert(getType()->isInteger() &&
1523 "Shift operation requires integer operands");
1527 assert(getType() == LHS->getType() &&
1528 "Logical operation should return same type as operands!");
1529 assert((getType()->isInteger() ||
1530 (isa<VectorType>(getType()) &&
1531 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1532 "Tried to create a logical operation on a non-integral type!");
1540 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1541 const std::string &Name,
1542 Instruction *InsertBefore) {
1543 assert(S1->getType() == S2->getType() &&
1544 "Cannot create binary operator with two operands of differing type!");
1545 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1548 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1549 const std::string &Name,
1550 BasicBlock *InsertAtEnd) {
1551 BinaryOperator *Res = Create(Op, S1, S2, Name);
1552 InsertAtEnd->getInstList().push_back(Res);
1556 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
1557 Instruction *InsertBefore) {
1558 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1559 return new BinaryOperator(Instruction::Sub,
1561 Op->getType(), Name, InsertBefore);
1564 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
1565 BasicBlock *InsertAtEnd) {
1566 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1567 return new BinaryOperator(Instruction::Sub,
1569 Op->getType(), Name, InsertAtEnd);
1572 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
1573 Instruction *InsertBefore) {
1575 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1576 C = ConstantInt::getAllOnesValue(PTy->getElementType());
1577 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
1579 C = ConstantInt::getAllOnesValue(Op->getType());
1582 return new BinaryOperator(Instruction::Xor, Op, C,
1583 Op->getType(), Name, InsertBefore);
1586 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
1587 BasicBlock *InsertAtEnd) {
1589 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
1590 // Create a vector of all ones values.
1591 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
1593 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1595 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
1598 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1599 Op->getType(), Name, InsertAtEnd);
1603 // isConstantAllOnes - Helper function for several functions below
1604 static inline bool isConstantAllOnes(const Value *V) {
1605 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1606 return CI->isAllOnesValue();
1607 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1608 return CV->isAllOnesValue();
1612 bool BinaryOperator::isNeg(const Value *V) {
1613 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1614 if (Bop->getOpcode() == Instruction::Sub)
1615 return Bop->getOperand(0) ==
1616 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
1620 bool BinaryOperator::isNot(const Value *V) {
1621 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1622 return (Bop->getOpcode() == Instruction::Xor &&
1623 (isConstantAllOnes(Bop->getOperand(1)) ||
1624 isConstantAllOnes(Bop->getOperand(0))));
1628 Value *BinaryOperator::getNegArgument(Value *BinOp) {
1629 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1630 return cast<BinaryOperator>(BinOp)->getOperand(1);
1633 const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1634 return getNegArgument(const_cast<Value*>(BinOp));
1637 Value *BinaryOperator::getNotArgument(Value *BinOp) {
1638 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1639 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1640 Value *Op0 = BO->getOperand(0);
1641 Value *Op1 = BO->getOperand(1);
1642 if (isConstantAllOnes(Op0)) return Op1;
1644 assert(isConstantAllOnes(Op1));
1648 const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1649 return getNotArgument(const_cast<Value*>(BinOp));
1653 // swapOperands - Exchange the two operands to this instruction. This
1654 // instruction is safe to use on any binary instruction and does not
1655 // modify the semantics of the instruction. If the instruction is
1656 // order dependent (SetLT f.e.) the opcode is changed.
1658 bool BinaryOperator::swapOperands() {
1659 if (!isCommutative())
1660 return true; // Can't commute operands
1661 Op<0>().swap(Op<1>());
1665 //===----------------------------------------------------------------------===//
1667 //===----------------------------------------------------------------------===//
1669 // Just determine if this cast only deals with integral->integral conversion.
1670 bool CastInst::isIntegerCast() const {
1671 switch (getOpcode()) {
1672 default: return false;
1673 case Instruction::ZExt:
1674 case Instruction::SExt:
1675 case Instruction::Trunc:
1677 case Instruction::BitCast:
1678 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
1682 bool CastInst::isLosslessCast() const {
1683 // Only BitCast can be lossless, exit fast if we're not BitCast
1684 if (getOpcode() != Instruction::BitCast)
1687 // Identity cast is always lossless
1688 const Type* SrcTy = getOperand(0)->getType();
1689 const Type* DstTy = getType();
1693 // Pointer to pointer is always lossless.
1694 if (isa<PointerType>(SrcTy))
1695 return isa<PointerType>(DstTy);
1696 return false; // Other types have no identity values
1699 /// This function determines if the CastInst does not require any bits to be
1700 /// changed in order to effect the cast. Essentially, it identifies cases where
1701 /// no code gen is necessary for the cast, hence the name no-op cast. For
1702 /// example, the following are all no-op casts:
1703 /// # bitcast i32* %x to i8*
1704 /// # bitcast <2 x i32> %x to <4 x i16>
1705 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1706 /// @brief Determine if a cast is a no-op.
1707 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1708 switch (getOpcode()) {
1710 assert(!"Invalid CastOp");
1711 case Instruction::Trunc:
1712 case Instruction::ZExt:
1713 case Instruction::SExt:
1714 case Instruction::FPTrunc:
1715 case Instruction::FPExt:
1716 case Instruction::UIToFP:
1717 case Instruction::SIToFP:
1718 case Instruction::FPToUI:
1719 case Instruction::FPToSI:
1720 return false; // These always modify bits
1721 case Instruction::BitCast:
1722 return true; // BitCast never modifies bits.
1723 case Instruction::PtrToInt:
1724 return IntPtrTy->getPrimitiveSizeInBits() ==
1725 getType()->getPrimitiveSizeInBits();
1726 case Instruction::IntToPtr:
1727 return IntPtrTy->getPrimitiveSizeInBits() ==
1728 getOperand(0)->getType()->getPrimitiveSizeInBits();
1732 /// This function determines if a pair of casts can be eliminated and what
1733 /// opcode should be used in the elimination. This assumes that there are two
1734 /// instructions like this:
1735 /// * %F = firstOpcode SrcTy %x to MidTy
1736 /// * %S = secondOpcode MidTy %F to DstTy
1737 /// The function returns a resultOpcode so these two casts can be replaced with:
1738 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1739 /// If no such cast is permited, the function returns 0.
1740 unsigned CastInst::isEliminableCastPair(
1741 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1742 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1744 // Define the 144 possibilities for these two cast instructions. The values
1745 // in this matrix determine what to do in a given situation and select the
1746 // case in the switch below. The rows correspond to firstOp, the columns
1747 // correspond to secondOp. In looking at the table below, keep in mind
1748 // the following cast properties:
1750 // Size Compare Source Destination
1751 // Operator Src ? Size Type Sign Type Sign
1752 // -------- ------------ ------------------- ---------------------
1753 // TRUNC > Integer Any Integral Any
1754 // ZEXT < Integral Unsigned Integer Any
1755 // SEXT < Integral Signed Integer Any
1756 // FPTOUI n/a FloatPt n/a Integral Unsigned
1757 // FPTOSI n/a FloatPt n/a Integral Signed
1758 // UITOFP n/a Integral Unsigned FloatPt n/a
1759 // SITOFP n/a Integral Signed FloatPt n/a
1760 // FPTRUNC > FloatPt n/a FloatPt n/a
1761 // FPEXT < FloatPt n/a FloatPt n/a
1762 // PTRTOINT n/a Pointer n/a Integral Unsigned
1763 // INTTOPTR n/a Integral Unsigned Pointer n/a
1764 // BITCONVERT = FirstClass n/a FirstClass n/a
1766 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1767 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1768 // into "fptoui double to ulong", but this loses information about the range
1769 // of the produced value (we no longer know the top-part is all zeros).
1770 // Further this conversion is often much more expensive for typical hardware,
1771 // and causes issues when building libgcc. We disallow fptosi+sext for the
1773 const unsigned numCastOps =
1774 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1775 static const uint8_t CastResults[numCastOps][numCastOps] = {
1776 // T F F U S F F P I B -+
1777 // R Z S P P I I T P 2 N T |
1778 // U E E 2 2 2 2 R E I T C +- secondOp
1779 // N X X U S F F N X N 2 V |
1780 // C T T I I P P C T T P T -+
1781 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1782 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1783 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
1784 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1785 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
1786 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1787 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1788 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1789 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1790 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1791 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1792 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1795 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1796 [secondOp-Instruction::CastOpsBegin];
1799 // categorically disallowed
1802 // allowed, use first cast's opcode
1805 // allowed, use second cast's opcode
1808 // no-op cast in second op implies firstOp as long as the DestTy
1810 if (DstTy->isInteger())
1814 // no-op cast in second op implies firstOp as long as the DestTy
1815 // is floating point
1816 if (DstTy->isFloatingPoint())
1820 // no-op cast in first op implies secondOp as long as the SrcTy
1822 if (SrcTy->isInteger())
1826 // no-op cast in first op implies secondOp as long as the SrcTy
1827 // is a floating point
1828 if (SrcTy->isFloatingPoint())
1832 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1833 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1834 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1835 if (MidSize >= PtrSize)
1836 return Instruction::BitCast;
1840 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1841 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1842 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1843 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1844 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1845 if (SrcSize == DstSize)
1846 return Instruction::BitCast;
1847 else if (SrcSize < DstSize)
1851 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1852 return Instruction::ZExt;
1854 // fpext followed by ftrunc is allowed if the bit size returned to is
1855 // the same as the original, in which case its just a bitcast
1857 return Instruction::BitCast;
1858 return 0; // If the types are not the same we can't eliminate it.
1860 // bitcast followed by ptrtoint is allowed as long as the bitcast
1861 // is a pointer to pointer cast.
1862 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1866 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1867 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1871 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1872 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1873 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1874 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1875 if (SrcSize <= PtrSize && SrcSize == DstSize)
1876 return Instruction::BitCast;
1880 // cast combination can't happen (error in input). This is for all cases
1881 // where the MidTy is not the same for the two cast instructions.
1882 assert(!"Invalid Cast Combination");
1885 assert(!"Error in CastResults table!!!");
1891 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
1892 const std::string &Name, Instruction *InsertBefore) {
1893 // Construct and return the appropriate CastInst subclass
1895 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1896 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1897 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1898 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1899 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1900 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1901 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1902 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1903 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1904 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1905 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1906 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1908 assert(!"Invalid opcode provided");
1913 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
1914 const std::string &Name, BasicBlock *InsertAtEnd) {
1915 // Construct and return the appropriate CastInst subclass
1917 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1918 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1919 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1920 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1921 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1922 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1923 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1924 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1925 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1926 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1927 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1928 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1930 assert(!"Invalid opcode provided");
1935 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
1936 const std::string &Name,
1937 Instruction *InsertBefore) {
1938 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1939 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1940 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1943 CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
1944 const std::string &Name,
1945 BasicBlock *InsertAtEnd) {
1946 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1947 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1948 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1951 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
1952 const std::string &Name,
1953 Instruction *InsertBefore) {
1954 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1955 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1956 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
1959 CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
1960 const std::string &Name,
1961 BasicBlock *InsertAtEnd) {
1962 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1963 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1964 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1967 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
1968 const std::string &Name,
1969 Instruction *InsertBefore) {
1970 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1971 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1972 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1975 CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
1976 const std::string &Name,
1977 BasicBlock *InsertAtEnd) {
1978 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1979 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1980 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1983 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
1984 const std::string &Name,
1985 BasicBlock *InsertAtEnd) {
1986 assert(isa<PointerType>(S->getType()) && "Invalid cast");
1987 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
1990 if (Ty->isInteger())
1991 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1992 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1995 /// @brief Create a BitCast or a PtrToInt cast instruction
1996 CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
1997 const std::string &Name,
1998 Instruction *InsertBefore) {
1999 assert(isa<PointerType>(S->getType()) && "Invalid cast");
2000 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
2003 if (Ty->isInteger())
2004 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2005 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2008 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2009 bool isSigned, const std::string &Name,
2010 Instruction *InsertBefore) {
2011 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
2012 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2013 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2014 Instruction::CastOps opcode =
2015 (SrcBits == DstBits ? Instruction::BitCast :
2016 (SrcBits > DstBits ? Instruction::Trunc :
2017 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2018 return Create(opcode, C, Ty, Name, InsertBefore);
2021 CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
2022 bool isSigned, const std::string &Name,
2023 BasicBlock *InsertAtEnd) {
2024 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
2025 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2026 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2027 Instruction::CastOps opcode =
2028 (SrcBits == DstBits ? Instruction::BitCast :
2029 (SrcBits > DstBits ? Instruction::Trunc :
2030 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2031 return Create(opcode, C, Ty, Name, InsertAtEnd);
2034 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2035 const std::string &Name,
2036 Instruction *InsertBefore) {
2037 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2039 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2040 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2041 Instruction::CastOps opcode =
2042 (SrcBits == DstBits ? Instruction::BitCast :
2043 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2044 return Create(opcode, C, Ty, Name, InsertBefore);
2047 CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
2048 const std::string &Name,
2049 BasicBlock *InsertAtEnd) {
2050 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2052 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2053 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2054 Instruction::CastOps opcode =
2055 (SrcBits == DstBits ? Instruction::BitCast :
2056 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2057 return Create(opcode, C, Ty, Name, InsertAtEnd);
2060 // Check whether it is valid to call getCastOpcode for these types.
2061 // This routine must be kept in sync with getCastOpcode.
2062 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2063 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2066 if (SrcTy == DestTy)
2069 // Get the bit sizes, we'll need these
2070 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2071 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2073 // Run through the possibilities ...
2074 if (DestTy->isInteger()) { // Casting to integral
2075 if (SrcTy->isInteger()) { // Casting from integral
2077 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2079 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2080 // Casting from vector
2081 return DestBits == PTy->getBitWidth();
2082 } else { // Casting from something else
2083 return isa<PointerType>(SrcTy);
2085 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2086 if (SrcTy->isInteger()) { // Casting from integral
2088 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2090 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2091 // Casting from vector
2092 return DestBits == PTy->getBitWidth();
2093 } else { // Casting from something else
2096 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2097 // Casting to vector
2098 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2099 // Casting from vector
2100 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2101 } else { // Casting from something else
2102 return DestPTy->getBitWidth() == SrcBits;
2104 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2105 if (isa<PointerType>(SrcTy)) { // Casting from pointer
2107 } else if (SrcTy->isInteger()) { // Casting from integral
2109 } else { // Casting from something else
2112 } else { // Casting to something else
2117 // Provide a way to get a "cast" where the cast opcode is inferred from the
2118 // types and size of the operand. This, basically, is a parallel of the
2119 // logic in the castIsValid function below. This axiom should hold:
2120 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2121 // should not assert in castIsValid. In other words, this produces a "correct"
2122 // casting opcode for the arguments passed to it.
2123 // This routine must be kept in sync with isCastable.
2124 Instruction::CastOps
2125 CastInst::getCastOpcode(
2126 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
2127 // Get the bit sizes, we'll need these
2128 const Type *SrcTy = Src->getType();
2129 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2130 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2132 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2133 "Only first class types are castable!");
2135 // Run through the possibilities ...
2136 if (DestTy->isInteger()) { // Casting to integral
2137 if (SrcTy->isInteger()) { // Casting from integral
2138 if (DestBits < SrcBits)
2139 return Trunc; // int -> smaller int
2140 else if (DestBits > SrcBits) { // its an extension
2142 return SExt; // signed -> SEXT
2144 return ZExt; // unsigned -> ZEXT
2146 return BitCast; // Same size, No-op cast
2148 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2150 return FPToSI; // FP -> sint
2152 return FPToUI; // FP -> uint
2153 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2154 assert(DestBits == PTy->getBitWidth() &&
2155 "Casting vector to integer of different width");
2156 return BitCast; // Same size, no-op cast
2158 assert(isa<PointerType>(SrcTy) &&
2159 "Casting from a value that is not first-class type");
2160 return PtrToInt; // ptr -> int
2162 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2163 if (SrcTy->isInteger()) { // Casting from integral
2165 return SIToFP; // sint -> FP
2167 return UIToFP; // uint -> FP
2168 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2169 if (DestBits < SrcBits) {
2170 return FPTrunc; // FP -> smaller FP
2171 } else if (DestBits > SrcBits) {
2172 return FPExt; // FP -> larger FP
2174 return BitCast; // same size, no-op cast
2176 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2177 assert(DestBits == PTy->getBitWidth() &&
2178 "Casting vector to floating point of different width");
2179 return BitCast; // same size, no-op cast
2181 assert(0 && "Casting pointer or non-first class to float");
2183 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2184 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2185 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
2186 "Casting vector to vector of different widths");
2187 return BitCast; // vector -> vector
2188 } else if (DestPTy->getBitWidth() == SrcBits) {
2189 return BitCast; // float/int -> vector
2191 assert(!"Illegal cast to vector (wrong type or size)");
2193 } else if (isa<PointerType>(DestTy)) {
2194 if (isa<PointerType>(SrcTy)) {
2195 return BitCast; // ptr -> ptr
2196 } else if (SrcTy->isInteger()) {
2197 return IntToPtr; // int -> ptr
2199 assert(!"Casting pointer to other than pointer or int");
2202 assert(!"Casting to type that is not first-class");
2205 // If we fall through to here we probably hit an assertion cast above
2206 // and assertions are not turned on. Anything we return is an error, so
2207 // BitCast is as good a choice as any.
2211 //===----------------------------------------------------------------------===//
2212 // CastInst SubClass Constructors
2213 //===----------------------------------------------------------------------===//
2215 /// Check that the construction parameters for a CastInst are correct. This
2216 /// could be broken out into the separate constructors but it is useful to have
2217 /// it in one place and to eliminate the redundant code for getting the sizes
2218 /// of the types involved.
2220 CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
2222 // Check for type sanity on the arguments
2223 const Type *SrcTy = S->getType();
2224 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2227 // Get the size of the types in bits, we'll need this later
2228 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2229 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2231 // Switch on the opcode provided
2233 default: return false; // This is an input error
2234 case Instruction::Trunc:
2235 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
2236 case Instruction::ZExt:
2237 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
2238 case Instruction::SExt:
2239 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
2240 case Instruction::FPTrunc:
2241 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2242 SrcBitSize > DstBitSize;
2243 case Instruction::FPExt:
2244 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2245 SrcBitSize < DstBitSize;
2246 case Instruction::UIToFP:
2247 case Instruction::SIToFP:
2248 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2249 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2250 return SVTy->getElementType()->isInteger() &&
2251 DVTy->getElementType()->isFloatingPoint() &&
2252 SVTy->getNumElements() == DVTy->getNumElements();
2255 return SrcTy->isInteger() && DstTy->isFloatingPoint();
2256 case Instruction::FPToUI:
2257 case Instruction::FPToSI:
2258 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2259 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2260 return SVTy->getElementType()->isFloatingPoint() &&
2261 DVTy->getElementType()->isInteger() &&
2262 SVTy->getNumElements() == DVTy->getNumElements();
2265 return SrcTy->isFloatingPoint() && DstTy->isInteger();
2266 case Instruction::PtrToInt:
2267 return isa<PointerType>(SrcTy) && DstTy->isInteger();
2268 case Instruction::IntToPtr:
2269 return SrcTy->isInteger() && isa<PointerType>(DstTy);
2270 case Instruction::BitCast:
2271 // BitCast implies a no-op cast of type only. No bits change.
2272 // However, you can't cast pointers to anything but pointers.
2273 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2276 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2277 // these cases, the cast is okay if the source and destination bit widths
2279 return SrcBitSize == DstBitSize;
2283 TruncInst::TruncInst(
2284 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2285 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2286 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2289 TruncInst::TruncInst(
2290 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2291 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2292 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2296 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2297 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2298 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2302 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2303 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2304 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2307 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2308 ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2309 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2313 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2314 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2315 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2318 FPTruncInst::FPTruncInst(
2319 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2320 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2321 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2324 FPTruncInst::FPTruncInst(
2325 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2326 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2327 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2330 FPExtInst::FPExtInst(
2331 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2332 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2333 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2336 FPExtInst::FPExtInst(
2337 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2338 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2339 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2342 UIToFPInst::UIToFPInst(
2343 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2344 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2345 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2348 UIToFPInst::UIToFPInst(
2349 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2350 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2351 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2354 SIToFPInst::SIToFPInst(
2355 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2356 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2357 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2360 SIToFPInst::SIToFPInst(
2361 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2362 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2363 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2366 FPToUIInst::FPToUIInst(
2367 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2368 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2369 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2372 FPToUIInst::FPToUIInst(
2373 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2374 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2375 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2378 FPToSIInst::FPToSIInst(
2379 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2380 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2381 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2384 FPToSIInst::FPToSIInst(
2385 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2386 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2387 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2390 PtrToIntInst::PtrToIntInst(
2391 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2392 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2393 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2396 PtrToIntInst::PtrToIntInst(
2397 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2398 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2399 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2402 IntToPtrInst::IntToPtrInst(
2403 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2404 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2405 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2408 IntToPtrInst::IntToPtrInst(
2409 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2410 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2411 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2414 BitCastInst::BitCastInst(
2415 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2416 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2417 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2420 BitCastInst::BitCastInst(
2421 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2422 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2423 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2426 //===----------------------------------------------------------------------===//
2428 //===----------------------------------------------------------------------===//
2430 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2431 Value *LHS, Value *RHS, const std::string &Name,
2432 Instruction *InsertBefore)
2433 : Instruction(ty, op,
2434 OperandTraits<CmpInst>::op_begin(this),
2435 OperandTraits<CmpInst>::operands(this),
2439 SubclassData = predicate;
2443 CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2444 Value *LHS, Value *RHS, const std::string &Name,
2445 BasicBlock *InsertAtEnd)
2446 : Instruction(ty, op,
2447 OperandTraits<CmpInst>::op_begin(this),
2448 OperandTraits<CmpInst>::operands(this),
2452 SubclassData = predicate;
2457 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2458 const std::string &Name, Instruction *InsertBefore) {
2459 if (Op == Instruction::ICmp) {
2460 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2463 if (Op == Instruction::FCmp) {
2464 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2467 if (Op == Instruction::VICmp) {
2468 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2471 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2476 CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2477 const std::string &Name, BasicBlock *InsertAtEnd) {
2478 if (Op == Instruction::ICmp) {
2479 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2482 if (Op == Instruction::FCmp) {
2483 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2486 if (Op == Instruction::VICmp) {
2487 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2490 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2494 void CmpInst::swapOperands() {
2495 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2498 cast<FCmpInst>(this)->swapOperands();
2501 bool CmpInst::isCommutative() {
2502 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2503 return IC->isCommutative();
2504 return cast<FCmpInst>(this)->isCommutative();
2507 bool CmpInst::isEquality() {
2508 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2509 return IC->isEquality();
2510 return cast<FCmpInst>(this)->isEquality();
2514 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
2516 default: assert(!"Unknown cmp predicate!");
2517 case ICMP_EQ: return ICMP_NE;
2518 case ICMP_NE: return ICMP_EQ;
2519 case ICMP_UGT: return ICMP_ULE;
2520 case ICMP_ULT: return ICMP_UGE;
2521 case ICMP_UGE: return ICMP_ULT;
2522 case ICMP_ULE: return ICMP_UGT;
2523 case ICMP_SGT: return ICMP_SLE;
2524 case ICMP_SLT: return ICMP_SGE;
2525 case ICMP_SGE: return ICMP_SLT;
2526 case ICMP_SLE: return ICMP_SGT;
2528 case FCMP_OEQ: return FCMP_UNE;
2529 case FCMP_ONE: return FCMP_UEQ;
2530 case FCMP_OGT: return FCMP_ULE;
2531 case FCMP_OLT: return FCMP_UGE;
2532 case FCMP_OGE: return FCMP_ULT;
2533 case FCMP_OLE: return FCMP_UGT;
2534 case FCMP_UEQ: return FCMP_ONE;
2535 case FCMP_UNE: return FCMP_OEQ;
2536 case FCMP_UGT: return FCMP_OLE;
2537 case FCMP_ULT: return FCMP_OGE;
2538 case FCMP_UGE: return FCMP_OLT;
2539 case FCMP_ULE: return FCMP_OGT;
2540 case FCMP_ORD: return FCMP_UNO;
2541 case FCMP_UNO: return FCMP_ORD;
2542 case FCMP_TRUE: return FCMP_FALSE;
2543 case FCMP_FALSE: return FCMP_TRUE;
2547 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2549 default: assert(! "Unknown icmp predicate!");
2550 case ICMP_EQ: case ICMP_NE:
2551 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2553 case ICMP_UGT: return ICMP_SGT;
2554 case ICMP_ULT: return ICMP_SLT;
2555 case ICMP_UGE: return ICMP_SGE;
2556 case ICMP_ULE: return ICMP_SLE;
2560 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2562 default: assert(! "Unknown icmp predicate!");
2563 case ICMP_EQ: case ICMP_NE:
2564 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2566 case ICMP_SGT: return ICMP_UGT;
2567 case ICMP_SLT: return ICMP_ULT;
2568 case ICMP_SGE: return ICMP_UGE;
2569 case ICMP_SLE: return ICMP_ULE;
2573 bool ICmpInst::isSignedPredicate(Predicate pred) {
2575 default: assert(! "Unknown icmp predicate!");
2576 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2578 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2579 case ICMP_UGE: case ICMP_ULE:
2584 /// Initialize a set of values that all satisfy the condition with C.
2587 ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2590 uint32_t BitWidth = C.getBitWidth();
2592 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2593 case ICmpInst::ICMP_EQ: Upper++; break;
2594 case ICmpInst::ICMP_NE: Lower++; break;
2595 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2596 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2597 case ICmpInst::ICMP_UGT:
2598 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2600 case ICmpInst::ICMP_SGT:
2601 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2603 case ICmpInst::ICMP_ULE:
2604 Lower = APInt::getMinValue(BitWidth); Upper++;
2606 case ICmpInst::ICMP_SLE:
2607 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2609 case ICmpInst::ICMP_UGE:
2610 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2612 case ICmpInst::ICMP_SGE:
2613 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2616 return ConstantRange(Lower, Upper);
2619 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
2621 default: assert(!"Unknown cmp predicate!");
2622 case ICMP_EQ: case ICMP_NE:
2624 case ICMP_SGT: return ICMP_SLT;
2625 case ICMP_SLT: return ICMP_SGT;
2626 case ICMP_SGE: return ICMP_SLE;
2627 case ICMP_SLE: return ICMP_SGE;
2628 case ICMP_UGT: return ICMP_ULT;
2629 case ICMP_ULT: return ICMP_UGT;
2630 case ICMP_UGE: return ICMP_ULE;
2631 case ICMP_ULE: return ICMP_UGE;
2633 case FCMP_FALSE: case FCMP_TRUE:
2634 case FCMP_OEQ: case FCMP_ONE:
2635 case FCMP_UEQ: case FCMP_UNE:
2636 case FCMP_ORD: case FCMP_UNO:
2638 case FCMP_OGT: return FCMP_OLT;
2639 case FCMP_OLT: return FCMP_OGT;
2640 case FCMP_OGE: return FCMP_OLE;
2641 case FCMP_OLE: return FCMP_OGE;
2642 case FCMP_UGT: return FCMP_ULT;
2643 case FCMP_ULT: return FCMP_UGT;
2644 case FCMP_UGE: return FCMP_ULE;
2645 case FCMP_ULE: return FCMP_UGE;
2649 bool CmpInst::isUnsigned(unsigned short predicate) {
2650 switch (predicate) {
2651 default: return false;
2652 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2653 case ICmpInst::ICMP_UGE: return true;
2657 bool CmpInst::isSigned(unsigned short predicate){
2658 switch (predicate) {
2659 default: return false;
2660 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2661 case ICmpInst::ICMP_SGE: return true;
2665 bool CmpInst::isOrdered(unsigned short predicate) {
2666 switch (predicate) {
2667 default: return false;
2668 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2669 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2670 case FCmpInst::FCMP_ORD: return true;
2674 bool CmpInst::isUnordered(unsigned short predicate) {
2675 switch (predicate) {
2676 default: return false;
2677 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2678 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2679 case FCmpInst::FCMP_UNO: return true;
2683 //===----------------------------------------------------------------------===//
2684 // SwitchInst Implementation
2685 //===----------------------------------------------------------------------===//
2687 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
2688 assert(Value && Default);
2689 ReservedSpace = 2+NumCases*2;
2691 OperandList = allocHungoffUses(ReservedSpace);
2693 OperandList[0] = Value;
2694 OperandList[1] = Default;
2697 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2698 /// switch on and a default destination. The number of additional cases can
2699 /// be specified here to make memory allocation more efficient. This
2700 /// constructor can also autoinsert before another instruction.
2701 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2702 Instruction *InsertBefore)
2703 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2704 init(Value, Default, NumCases);
2707 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2708 /// switch on and a default destination. The number of additional cases can
2709 /// be specified here to make memory allocation more efficient. This
2710 /// constructor also autoinserts at the end of the specified BasicBlock.
2711 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2712 BasicBlock *InsertAtEnd)
2713 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2714 init(Value, Default, NumCases);
2717 SwitchInst::SwitchInst(const SwitchInst &SI)
2718 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2719 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
2720 Use *OL = OperandList, *InOL = SI.OperandList;
2721 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2723 OL[i+1] = InOL[i+1];
2727 SwitchInst::~SwitchInst() {
2728 dropHungoffUses(OperandList);
2732 /// addCase - Add an entry to the switch instruction...
2734 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
2735 unsigned OpNo = NumOperands;
2736 if (OpNo+2 > ReservedSpace)
2737 resizeOperands(0); // Get more space!
2738 // Initialize some new operands.
2739 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
2740 NumOperands = OpNo+2;
2741 OperandList[OpNo] = OnVal;
2742 OperandList[OpNo+1] = Dest;
2745 /// removeCase - This method removes the specified successor from the switch
2746 /// instruction. Note that this cannot be used to remove the default
2747 /// destination (successor #0).
2749 void SwitchInst::removeCase(unsigned idx) {
2750 assert(idx != 0 && "Cannot remove the default case!");
2751 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2753 unsigned NumOps = getNumOperands();
2754 Use *OL = OperandList;
2756 // Move everything after this operand down.
2758 // FIXME: we could just swap with the end of the list, then erase. However,
2759 // client might not expect this to happen. The code as it is thrashes the
2760 // use/def lists, which is kinda lame.
2761 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2763 OL[i-2+1] = OL[i+1];
2766 // Nuke the last value.
2767 OL[NumOps-2].set(0);
2768 OL[NumOps-2+1].set(0);
2769 NumOperands = NumOps-2;
2772 /// resizeOperands - resize operands - This adjusts the length of the operands
2773 /// list according to the following behavior:
2774 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
2775 /// of operation. This grows the number of ops by 3 times.
2776 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2777 /// 3. If NumOps == NumOperands, trim the reserved space.
2779 void SwitchInst::resizeOperands(unsigned NumOps) {
2780 unsigned e = getNumOperands();
2783 } else if (NumOps*2 > NumOperands) {
2784 // No resize needed.
2785 if (ReservedSpace >= NumOps) return;
2786 } else if (NumOps == NumOperands) {
2787 if (ReservedSpace == NumOps) return;
2792 ReservedSpace = NumOps;
2793 Use *NewOps = allocHungoffUses(NumOps);
2794 Use *OldOps = OperandList;
2795 for (unsigned i = 0; i != e; ++i) {
2796 NewOps[i] = OldOps[i];
2798 OperandList = NewOps;
2799 if (OldOps) Use::zap(OldOps, OldOps + e, true);
2803 BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2804 return getSuccessor(idx);
2806 unsigned SwitchInst::getNumSuccessorsV() const {
2807 return getNumSuccessors();
2809 void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2810 setSuccessor(idx, B);
2813 // Define these methods here so vtables don't get emitted into every translation
2814 // unit that uses these classes.
2816 GetElementPtrInst *GetElementPtrInst::clone() const {
2817 return new(getNumOperands()) GetElementPtrInst(*this);
2820 BinaryOperator *BinaryOperator::clone() const {
2821 return Create(getOpcode(), Op<0>(), Op<1>());
2824 FCmpInst* FCmpInst::clone() const {
2825 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
2827 ICmpInst* ICmpInst::clone() const {
2828 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
2831 VFCmpInst* VFCmpInst::clone() const {
2832 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2834 VICmpInst* VICmpInst::clone() const {
2835 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2838 ExtractValueInst *ExtractValueInst::clone() const {
2839 return new ExtractValueInst(*this);
2841 InsertValueInst *InsertValueInst::clone() const {
2842 return new InsertValueInst(*this);
2846 MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2847 AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2848 FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2849 LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2850 StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2851 CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2852 CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2853 CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2854 CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2855 CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2856 CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2857 CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2858 CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2859 CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2860 CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2861 CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2862 CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2863 CallInst *CallInst::clone() const {
2864 return new(getNumOperands()) CallInst(*this);
2866 SelectInst *SelectInst::clone() const {
2867 return new(getNumOperands()) SelectInst(*this);
2869 VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2871 ExtractElementInst *ExtractElementInst::clone() const {
2872 return new ExtractElementInst(*this);
2874 InsertElementInst *InsertElementInst::clone() const {
2875 return InsertElementInst::Create(*this);
2877 ShuffleVectorInst *ShuffleVectorInst::clone() const {
2878 return new ShuffleVectorInst(*this);
2880 PHINode *PHINode::clone() const { return new PHINode(*this); }
2881 ReturnInst *ReturnInst::clone() const {
2882 return new(getNumOperands()) ReturnInst(*this);
2884 BranchInst *BranchInst::clone() const {
2885 return new(getNumOperands()) BranchInst(*this);
2887 SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2888 InvokeInst *InvokeInst::clone() const {
2889 return new(getNumOperands()) InvokeInst(*this);
2891 UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
2892 UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}