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 "LLVMContextImpl.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Module.h"
21 #include "llvm/Operator.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/CallSite.h"
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Support/MathExtras.h"
28 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 User::op_iterator
CallSite::getCallee() const {
33 Instruction
*II(getInstruction());
35 ? cast
<CallInst
>(II
)->op_end() - 1 // Skip Callee
36 : cast
<InvokeInst
>(II
)->op_end() - 3; // Skip BB, BB, Callee
39 //===----------------------------------------------------------------------===//
40 // TerminatorInst Class
41 //===----------------------------------------------------------------------===//
43 // Out of line virtual method, so the vtable, etc has a home.
44 TerminatorInst::~TerminatorInst() {
47 //===----------------------------------------------------------------------===//
48 // UnaryInstruction Class
49 //===----------------------------------------------------------------------===//
51 // Out of line virtual method, so the vtable, etc has a home.
52 UnaryInstruction::~UnaryInstruction() {
55 //===----------------------------------------------------------------------===//
57 //===----------------------------------------------------------------------===//
59 /// areInvalidOperands - Return a string if the specified operands are invalid
60 /// for a select operation, otherwise return null.
61 const char *SelectInst::areInvalidOperands(Value
*Op0
, Value
*Op1
, Value
*Op2
) {
62 if (Op1
->getType() != Op2
->getType())
63 return "both values to select must have same type";
65 if (const VectorType
*VT
= dyn_cast
<VectorType
>(Op0
->getType())) {
67 if (VT
->getElementType() != Type::getInt1Ty(Op0
->getContext()))
68 return "vector select condition element type must be i1";
69 const VectorType
*ET
= dyn_cast
<VectorType
>(Op1
->getType());
71 return "selected values for vector select must be vectors";
72 if (ET
->getNumElements() != VT
->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
75 } else if (Op0
->getType() != Type::getInt1Ty(Op0
->getContext())) {
76 return "select condition must be i1 or <n x i1>";
82 //===----------------------------------------------------------------------===//
84 //===----------------------------------------------------------------------===//
86 PHINode::PHINode(const PHINode
&PN
)
87 : Instruction(PN
.getType(), Instruction::PHI
,
88 allocHungoffUses(PN
.getNumOperands()), PN
.getNumOperands()),
89 ReservedSpace(PN
.getNumOperands()) {
90 std::copy(PN
.op_begin(), PN
.op_end(), op_begin());
91 std::copy(PN
.block_begin(), PN
.block_end(), block_begin());
92 SubclassOptionalData
= PN
.SubclassOptionalData
;
99 Use
*PHINode::allocHungoffUses(unsigned N
) const {
100 // Allocate the array of Uses of the incoming values, followed by a pointer
101 // (with bottom bit set) to the User, followed by the array of pointers to
102 // the incoming basic blocks.
103 size_t size
= N
* sizeof(Use
) + sizeof(Use::UserRef
)
104 + N
* sizeof(BasicBlock
*);
105 Use
*Begin
= static_cast<Use
*>(::operator new(size
));
106 Use
*End
= Begin
+ N
;
107 (void) new(End
) Use::UserRef(const_cast<PHINode
*>(this), 1);
108 return Use::initTags(Begin
, End
);
111 // removeIncomingValue - Remove an incoming value. This is useful if a
112 // predecessor basic block is deleted.
113 Value
*PHINode::removeIncomingValue(unsigned Idx
, bool DeletePHIIfEmpty
) {
114 Value
*Removed
= getIncomingValue(Idx
);
116 // Move everything after this operand down.
118 // FIXME: we could just swap with the end of the list, then erase. However,
119 // clients might not expect this to happen. The code as it is thrashes the
120 // use/def lists, which is kinda lame.
121 std::copy(op_begin() + Idx
+ 1, op_end(), op_begin() + Idx
);
122 std::copy(block_begin() + Idx
+ 1, block_end(), block_begin() + Idx
);
124 // Nuke the last value.
128 // If the PHI node is dead, because it has zero entries, nuke it now.
129 if (getNumOperands() == 0 && DeletePHIIfEmpty
) {
130 // If anyone is using this PHI, make them use a dummy value instead...
131 replaceAllUsesWith(UndefValue::get(getType()));
137 /// growOperands - grow operands - This grows the operand list in response
138 /// to a push_back style of operation. This grows the number of ops by 1.5
141 void PHINode::growOperands() {
142 unsigned e
= getNumOperands();
143 unsigned NumOps
= e
+ e
/ 2;
144 if (NumOps
< 2) NumOps
= 2; // 2 op PHI nodes are VERY common.
146 Use
*OldOps
= op_begin();
147 BasicBlock
**OldBlocks
= block_begin();
149 ReservedSpace
= NumOps
;
150 OperandList
= allocHungoffUses(ReservedSpace
);
152 std::copy(OldOps
, OldOps
+ e
, op_begin());
153 std::copy(OldBlocks
, OldBlocks
+ e
, block_begin());
155 Use::zap(OldOps
, OldOps
+ e
, true);
158 /// hasConstantValue - If the specified PHI node always merges together the same
159 /// value, return the value, otherwise return null.
160 Value
*PHINode::hasConstantValue() const {
161 // Exploit the fact that phi nodes always have at least one entry.
162 Value
*ConstantValue
= getIncomingValue(0);
163 for (unsigned i
= 1, e
= getNumIncomingValues(); i
!= e
; ++i
)
164 if (getIncomingValue(i
) != ConstantValue
)
165 return 0; // Incoming values not all the same.
166 return ConstantValue
;
170 //===----------------------------------------------------------------------===//
171 // CallInst Implementation
172 //===----------------------------------------------------------------------===//
174 CallInst::~CallInst() {
177 void CallInst::init(Value
*Func
, Value
* const *Params
, unsigned NumParams
) {
178 assert(NumOperands
== NumParams
+1 && "NumOperands not set up?");
181 const FunctionType
*FTy
=
182 cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())->getElementType());
183 (void)FTy
; // silence warning.
185 assert((NumParams
== FTy
->getNumParams() ||
186 (FTy
->isVarArg() && NumParams
> FTy
->getNumParams())) &&
187 "Calling a function with bad signature!");
188 for (unsigned i
= 0; i
!= NumParams
; ++i
) {
189 assert((i
>= FTy
->getNumParams() ||
190 FTy
->getParamType(i
) == Params
[i
]->getType()) &&
191 "Calling a function with a bad signature!");
192 OperandList
[i
] = Params
[i
];
196 void CallInst::init(Value
*Func
, Value
*Actual1
, Value
*Actual2
) {
197 assert(NumOperands
== 3 && "NumOperands not set up?");
202 const FunctionType
*FTy
=
203 cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())->getElementType());
204 (void)FTy
; // silence warning.
206 assert((FTy
->getNumParams() == 2 ||
207 (FTy
->isVarArg() && FTy
->getNumParams() < 2)) &&
208 "Calling a function with bad signature");
209 assert((0 >= FTy
->getNumParams() ||
210 FTy
->getParamType(0) == Actual1
->getType()) &&
211 "Calling a function with a bad signature!");
212 assert((1 >= FTy
->getNumParams() ||
213 FTy
->getParamType(1) == Actual2
->getType()) &&
214 "Calling a function with a bad signature!");
217 void CallInst::init(Value
*Func
, Value
*Actual
) {
218 assert(NumOperands
== 2 && "NumOperands not set up?");
222 const FunctionType
*FTy
=
223 cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())->getElementType());
224 (void)FTy
; // silence warning.
226 assert((FTy
->getNumParams() == 1 ||
227 (FTy
->isVarArg() && FTy
->getNumParams() == 0)) &&
228 "Calling a function with bad signature");
229 assert((0 == FTy
->getNumParams() ||
230 FTy
->getParamType(0) == Actual
->getType()) &&
231 "Calling a function with a bad signature!");
234 void CallInst::init(Value
*Func
) {
235 assert(NumOperands
== 1 && "NumOperands not set up?");
238 const FunctionType
*FTy
=
239 cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())->getElementType());
240 (void)FTy
; // silence warning.
242 assert(FTy
->getNumParams() == 0 && "Calling a function with bad signature");
245 CallInst::CallInst(Value
*Func
, Value
* Actual
, const Twine
&Name
,
246 Instruction
*InsertBefore
)
247 : Instruction(cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())
248 ->getElementType())->getReturnType(),
250 OperandTraits
<CallInst
>::op_end(this) - 2,
256 CallInst::CallInst(Value
*Func
, Value
* Actual
, const Twine
&Name
,
257 BasicBlock
*InsertAtEnd
)
258 : Instruction(cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())
259 ->getElementType())->getReturnType(),
261 OperandTraits
<CallInst
>::op_end(this) - 2,
266 CallInst::CallInst(Value
*Func
, const Twine
&Name
,
267 Instruction
*InsertBefore
)
268 : Instruction(cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())
269 ->getElementType())->getReturnType(),
271 OperandTraits
<CallInst
>::op_end(this) - 1,
277 CallInst::CallInst(Value
*Func
, const Twine
&Name
,
278 BasicBlock
*InsertAtEnd
)
279 : Instruction(cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())
280 ->getElementType())->getReturnType(),
282 OperandTraits
<CallInst
>::op_end(this) - 1,
288 CallInst::CallInst(const CallInst
&CI
)
289 : Instruction(CI
.getType(), Instruction::Call
,
290 OperandTraits
<CallInst
>::op_end(this) - CI
.getNumOperands(),
291 CI
.getNumOperands()) {
292 setAttributes(CI
.getAttributes());
293 setTailCall(CI
.isTailCall());
294 setCallingConv(CI
.getCallingConv());
296 Use
*OL
= OperandList
;
297 Use
*InOL
= CI
.OperandList
;
298 for (unsigned i
= 0, e
= CI
.getNumOperands(); i
!= e
; ++i
)
300 SubclassOptionalData
= CI
.SubclassOptionalData
;
303 void CallInst::addAttribute(unsigned i
, Attributes attr
) {
304 AttrListPtr PAL
= getAttributes();
305 PAL
= PAL
.addAttr(i
, attr
);
309 void CallInst::removeAttribute(unsigned i
, Attributes attr
) {
310 AttrListPtr PAL
= getAttributes();
311 PAL
= PAL
.removeAttr(i
, attr
);
315 bool CallInst::paramHasAttr(unsigned i
, Attributes attr
) const {
316 if (AttributeList
.paramHasAttr(i
, attr
))
318 if (const Function
*F
= getCalledFunction())
319 return F
->paramHasAttr(i
, attr
);
323 /// IsConstantOne - Return true only if val is constant int 1
324 static bool IsConstantOne(Value
*val
) {
325 assert(val
&& "IsConstantOne does not work with NULL val");
326 return isa
<ConstantInt
>(val
) && cast
<ConstantInt
>(val
)->isOne();
329 static Instruction
*createMalloc(Instruction
*InsertBefore
,
330 BasicBlock
*InsertAtEnd
, const Type
*IntPtrTy
,
331 const Type
*AllocTy
, Value
*AllocSize
,
332 Value
*ArraySize
, Function
*MallocF
,
334 assert(((!InsertBefore
&& InsertAtEnd
) || (InsertBefore
&& !InsertAtEnd
)) &&
335 "createMalloc needs either InsertBefore or InsertAtEnd");
337 // malloc(type) becomes:
338 // bitcast (i8* malloc(typeSize)) to type*
339 // malloc(type, arraySize) becomes:
340 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
342 ArraySize
= ConstantInt::get(IntPtrTy
, 1);
343 else if (ArraySize
->getType() != IntPtrTy
) {
345 ArraySize
= CastInst::CreateIntegerCast(ArraySize
, IntPtrTy
, false,
348 ArraySize
= CastInst::CreateIntegerCast(ArraySize
, IntPtrTy
, false,
352 if (!IsConstantOne(ArraySize
)) {
353 if (IsConstantOne(AllocSize
)) {
354 AllocSize
= ArraySize
; // Operand * 1 = Operand
355 } else if (Constant
*CO
= dyn_cast
<Constant
>(ArraySize
)) {
356 Constant
*Scale
= ConstantExpr::getIntegerCast(CO
, IntPtrTy
,
358 // Malloc arg is constant product of type size and array size
359 AllocSize
= ConstantExpr::getMul(Scale
, cast
<Constant
>(AllocSize
));
361 // Multiply type size by the array size...
363 AllocSize
= BinaryOperator::CreateMul(ArraySize
, AllocSize
,
364 "mallocsize", InsertBefore
);
366 AllocSize
= BinaryOperator::CreateMul(ArraySize
, AllocSize
,
367 "mallocsize", InsertAtEnd
);
371 assert(AllocSize
->getType() == IntPtrTy
&& "malloc arg is wrong size");
372 // Create the call to Malloc.
373 BasicBlock
* BB
= InsertBefore
? InsertBefore
->getParent() : InsertAtEnd
;
374 Module
* M
= BB
->getParent()->getParent();
375 Type
*BPTy
= Type::getInt8PtrTy(BB
->getContext());
376 Value
*MallocFunc
= MallocF
;
378 // prototype malloc as "void *malloc(size_t)"
379 MallocFunc
= M
->getOrInsertFunction("malloc", BPTy
, IntPtrTy
, NULL
);
380 const PointerType
*AllocPtrType
= PointerType::getUnqual(AllocTy
);
381 CallInst
*MCall
= NULL
;
382 Instruction
*Result
= NULL
;
384 MCall
= CallInst::Create(MallocFunc
, AllocSize
, "malloccall", InsertBefore
);
386 if (Result
->getType() != AllocPtrType
)
387 // Create a cast instruction to convert to the right type...
388 Result
= new BitCastInst(MCall
, AllocPtrType
, Name
, InsertBefore
);
390 MCall
= CallInst::Create(MallocFunc
, AllocSize
, "malloccall");
392 if (Result
->getType() != AllocPtrType
) {
393 InsertAtEnd
->getInstList().push_back(MCall
);
394 // Create a cast instruction to convert to the right type...
395 Result
= new BitCastInst(MCall
, AllocPtrType
, Name
);
398 MCall
->setTailCall();
399 if (Function
*F
= dyn_cast
<Function
>(MallocFunc
)) {
400 MCall
->setCallingConv(F
->getCallingConv());
401 if (!F
->doesNotAlias(0)) F
->setDoesNotAlias(0);
403 assert(!MCall
->getType()->isVoidTy() && "Malloc has void return type");
408 /// CreateMalloc - Generate the IR for a call to malloc:
409 /// 1. Compute the malloc call's argument as the specified type's size,
410 /// possibly multiplied by the array size if the array size is not
412 /// 2. Call malloc with that argument.
413 /// 3. Bitcast the result of the malloc call to the specified type.
414 Instruction
*CallInst::CreateMalloc(Instruction
*InsertBefore
,
415 const Type
*IntPtrTy
, const Type
*AllocTy
,
416 Value
*AllocSize
, Value
*ArraySize
,
419 return createMalloc(InsertBefore
, NULL
, IntPtrTy
, AllocTy
, AllocSize
,
420 ArraySize
, MallocF
, Name
);
423 /// CreateMalloc - Generate the IR for a call to malloc:
424 /// 1. Compute the malloc call's argument as the specified type's size,
425 /// possibly multiplied by the array size if the array size is not
427 /// 2. Call malloc with that argument.
428 /// 3. Bitcast the result of the malloc call to the specified type.
429 /// Note: This function does not add the bitcast to the basic block, that is the
430 /// responsibility of the caller.
431 Instruction
*CallInst::CreateMalloc(BasicBlock
*InsertAtEnd
,
432 const Type
*IntPtrTy
, const Type
*AllocTy
,
433 Value
*AllocSize
, Value
*ArraySize
,
434 Function
*MallocF
, const Twine
&Name
) {
435 return createMalloc(NULL
, InsertAtEnd
, IntPtrTy
, AllocTy
, AllocSize
,
436 ArraySize
, MallocF
, Name
);
439 static Instruction
* createFree(Value
* Source
, Instruction
*InsertBefore
,
440 BasicBlock
*InsertAtEnd
) {
441 assert(((!InsertBefore
&& InsertAtEnd
) || (InsertBefore
&& !InsertAtEnd
)) &&
442 "createFree needs either InsertBefore or InsertAtEnd");
443 assert(Source
->getType()->isPointerTy() &&
444 "Can not free something of nonpointer type!");
446 BasicBlock
* BB
= InsertBefore
? InsertBefore
->getParent() : InsertAtEnd
;
447 Module
* M
= BB
->getParent()->getParent();
449 const Type
*VoidTy
= Type::getVoidTy(M
->getContext());
450 const Type
*IntPtrTy
= Type::getInt8PtrTy(M
->getContext());
451 // prototype free as "void free(void*)"
452 Value
*FreeFunc
= M
->getOrInsertFunction("free", VoidTy
, IntPtrTy
, NULL
);
453 CallInst
* Result
= NULL
;
454 Value
*PtrCast
= Source
;
456 if (Source
->getType() != IntPtrTy
)
457 PtrCast
= new BitCastInst(Source
, IntPtrTy
, "", InsertBefore
);
458 Result
= CallInst::Create(FreeFunc
, PtrCast
, "", InsertBefore
);
460 if (Source
->getType() != IntPtrTy
)
461 PtrCast
= new BitCastInst(Source
, IntPtrTy
, "", InsertAtEnd
);
462 Result
= CallInst::Create(FreeFunc
, PtrCast
, "");
464 Result
->setTailCall();
465 if (Function
*F
= dyn_cast
<Function
>(FreeFunc
))
466 Result
->setCallingConv(F
->getCallingConv());
471 /// CreateFree - Generate the IR for a call to the builtin free function.
472 Instruction
* CallInst::CreateFree(Value
* Source
, Instruction
*InsertBefore
) {
473 return createFree(Source
, InsertBefore
, NULL
);
476 /// CreateFree - Generate the IR for a call to the builtin free function.
477 /// Note: This function does not add the call to the basic block, that is the
478 /// responsibility of the caller.
479 Instruction
* CallInst::CreateFree(Value
* Source
, BasicBlock
*InsertAtEnd
) {
480 Instruction
* FreeCall
= createFree(Source
, NULL
, InsertAtEnd
);
481 assert(FreeCall
&& "CreateFree did not create a CallInst");
485 //===----------------------------------------------------------------------===//
486 // InvokeInst Implementation
487 //===----------------------------------------------------------------------===//
489 void InvokeInst::init(Value
*Fn
, BasicBlock
*IfNormal
, BasicBlock
*IfException
,
490 Value
* const *Args
, unsigned NumArgs
) {
491 assert(NumOperands
== 3+NumArgs
&& "NumOperands not set up?");
494 Op
<-1>() = IfException
;
495 const FunctionType
*FTy
=
496 cast
<FunctionType
>(cast
<PointerType
>(Fn
->getType())->getElementType());
497 (void)FTy
; // silence warning.
499 assert(((NumArgs
== FTy
->getNumParams()) ||
500 (FTy
->isVarArg() && NumArgs
> FTy
->getNumParams())) &&
501 "Invoking a function with bad signature");
503 Use
*OL
= OperandList
;
504 for (unsigned i
= 0, e
= NumArgs
; i
!= e
; i
++) {
505 assert((i
>= FTy
->getNumParams() ||
506 FTy
->getParamType(i
) == Args
[i
]->getType()) &&
507 "Invoking a function with a bad signature!");
513 InvokeInst::InvokeInst(const InvokeInst
&II
)
514 : TerminatorInst(II
.getType(), Instruction::Invoke
,
515 OperandTraits
<InvokeInst
>::op_end(this)
516 - II
.getNumOperands(),
517 II
.getNumOperands()) {
518 setAttributes(II
.getAttributes());
519 setCallingConv(II
.getCallingConv());
520 Use
*OL
= OperandList
, *InOL
= II
.OperandList
;
521 for (unsigned i
= 0, e
= II
.getNumOperands(); i
!= e
; ++i
)
523 SubclassOptionalData
= II
.SubclassOptionalData
;
526 BasicBlock
*InvokeInst::getSuccessorV(unsigned idx
) const {
527 return getSuccessor(idx
);
529 unsigned InvokeInst::getNumSuccessorsV() const {
530 return getNumSuccessors();
532 void InvokeInst::setSuccessorV(unsigned idx
, BasicBlock
*B
) {
533 return setSuccessor(idx
, B
);
536 bool InvokeInst::paramHasAttr(unsigned i
, Attributes attr
) const {
537 if (AttributeList
.paramHasAttr(i
, attr
))
539 if (const Function
*F
= getCalledFunction())
540 return F
->paramHasAttr(i
, attr
);
544 void InvokeInst::addAttribute(unsigned i
, Attributes attr
) {
545 AttrListPtr PAL
= getAttributes();
546 PAL
= PAL
.addAttr(i
, attr
);
550 void InvokeInst::removeAttribute(unsigned i
, Attributes attr
) {
551 AttrListPtr PAL
= getAttributes();
552 PAL
= PAL
.removeAttr(i
, attr
);
557 //===----------------------------------------------------------------------===//
558 // ReturnInst Implementation
559 //===----------------------------------------------------------------------===//
561 ReturnInst::ReturnInst(const ReturnInst
&RI
)
562 : TerminatorInst(Type::getVoidTy(RI
.getContext()), Instruction::Ret
,
563 OperandTraits
<ReturnInst
>::op_end(this) -
565 RI
.getNumOperands()) {
566 if (RI
.getNumOperands())
567 Op
<0>() = RI
.Op
<0>();
568 SubclassOptionalData
= RI
.SubclassOptionalData
;
571 ReturnInst::ReturnInst(LLVMContext
&C
, Value
*retVal
, Instruction
*InsertBefore
)
572 : TerminatorInst(Type::getVoidTy(C
), Instruction::Ret
,
573 OperandTraits
<ReturnInst
>::op_end(this) - !!retVal
, !!retVal
,
578 ReturnInst::ReturnInst(LLVMContext
&C
, Value
*retVal
, BasicBlock
*InsertAtEnd
)
579 : TerminatorInst(Type::getVoidTy(C
), Instruction::Ret
,
580 OperandTraits
<ReturnInst
>::op_end(this) - !!retVal
, !!retVal
,
585 ReturnInst::ReturnInst(LLVMContext
&Context
, BasicBlock
*InsertAtEnd
)
586 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Ret
,
587 OperandTraits
<ReturnInst
>::op_end(this), 0, InsertAtEnd
) {
590 unsigned ReturnInst::getNumSuccessorsV() const {
591 return getNumSuccessors();
594 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
595 /// emit the vtable for the class in this translation unit.
596 void ReturnInst::setSuccessorV(unsigned idx
, BasicBlock
*NewSucc
) {
597 llvm_unreachable("ReturnInst has no successors!");
600 BasicBlock
*ReturnInst::getSuccessorV(unsigned idx
) const {
601 llvm_unreachable("ReturnInst has no successors!");
605 ReturnInst::~ReturnInst() {
608 //===----------------------------------------------------------------------===//
609 // UnwindInst Implementation
610 //===----------------------------------------------------------------------===//
612 UnwindInst::UnwindInst(LLVMContext
&Context
, Instruction
*InsertBefore
)
613 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Unwind
,
614 0, 0, InsertBefore
) {
616 UnwindInst::UnwindInst(LLVMContext
&Context
, BasicBlock
*InsertAtEnd
)
617 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Unwind
,
622 unsigned UnwindInst::getNumSuccessorsV() const {
623 return getNumSuccessors();
626 void UnwindInst::setSuccessorV(unsigned idx
, BasicBlock
*NewSucc
) {
627 llvm_unreachable("UnwindInst has no successors!");
630 BasicBlock
*UnwindInst::getSuccessorV(unsigned idx
) const {
631 llvm_unreachable("UnwindInst has no successors!");
635 //===----------------------------------------------------------------------===//
636 // UnreachableInst Implementation
637 //===----------------------------------------------------------------------===//
639 UnreachableInst::UnreachableInst(LLVMContext
&Context
,
640 Instruction
*InsertBefore
)
641 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Unreachable
,
642 0, 0, InsertBefore
) {
644 UnreachableInst::UnreachableInst(LLVMContext
&Context
, BasicBlock
*InsertAtEnd
)
645 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Unreachable
,
649 unsigned UnreachableInst::getNumSuccessorsV() const {
650 return getNumSuccessors();
653 void UnreachableInst::setSuccessorV(unsigned idx
, BasicBlock
*NewSucc
) {
654 llvm_unreachable("UnwindInst has no successors!");
657 BasicBlock
*UnreachableInst::getSuccessorV(unsigned idx
) const {
658 llvm_unreachable("UnwindInst has no successors!");
662 //===----------------------------------------------------------------------===//
663 // BranchInst Implementation
664 //===----------------------------------------------------------------------===//
666 void BranchInst::AssertOK() {
668 assert(getCondition()->getType()->isIntegerTy(1) &&
669 "May only branch on boolean predicates!");
672 BranchInst::BranchInst(BasicBlock
*IfTrue
, Instruction
*InsertBefore
)
673 : TerminatorInst(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
674 OperandTraits
<BranchInst
>::op_end(this) - 1,
676 assert(IfTrue
!= 0 && "Branch destination may not be null!");
679 BranchInst::BranchInst(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
, Value
*Cond
,
680 Instruction
*InsertBefore
)
681 : TerminatorInst(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
682 OperandTraits
<BranchInst
>::op_end(this) - 3,
692 BranchInst::BranchInst(BasicBlock
*IfTrue
, BasicBlock
*InsertAtEnd
)
693 : TerminatorInst(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
694 OperandTraits
<BranchInst
>::op_end(this) - 1,
696 assert(IfTrue
!= 0 && "Branch destination may not be null!");
700 BranchInst::BranchInst(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
, Value
*Cond
,
701 BasicBlock
*InsertAtEnd
)
702 : TerminatorInst(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
703 OperandTraits
<BranchInst
>::op_end(this) - 3,
714 BranchInst::BranchInst(const BranchInst
&BI
) :
715 TerminatorInst(Type::getVoidTy(BI
.getContext()), Instruction::Br
,
716 OperandTraits
<BranchInst
>::op_end(this) - BI
.getNumOperands(),
717 BI
.getNumOperands()) {
718 Op
<-1>() = BI
.Op
<-1>();
719 if (BI
.getNumOperands() != 1) {
720 assert(BI
.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
721 Op
<-3>() = BI
.Op
<-3>();
722 Op
<-2>() = BI
.Op
<-2>();
724 SubclassOptionalData
= BI
.SubclassOptionalData
;
727 BasicBlock
*BranchInst::getSuccessorV(unsigned idx
) const {
728 return getSuccessor(idx
);
730 unsigned BranchInst::getNumSuccessorsV() const {
731 return getNumSuccessors();
733 void BranchInst::setSuccessorV(unsigned idx
, BasicBlock
*B
) {
734 setSuccessor(idx
, B
);
738 //===----------------------------------------------------------------------===//
739 // AllocaInst Implementation
740 //===----------------------------------------------------------------------===//
742 static Value
*getAISize(LLVMContext
&Context
, Value
*Amt
) {
744 Amt
= ConstantInt::get(Type::getInt32Ty(Context
), 1);
746 assert(!isa
<BasicBlock
>(Amt
) &&
747 "Passed basic block into allocation size parameter! Use other ctor");
748 assert(Amt
->getType()->isIntegerTy() &&
749 "Allocation array size is not an integer!");
754 AllocaInst::AllocaInst(const Type
*Ty
, Value
*ArraySize
,
755 const Twine
&Name
, Instruction
*InsertBefore
)
756 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
757 getAISize(Ty
->getContext(), ArraySize
), InsertBefore
) {
759 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
763 AllocaInst::AllocaInst(const Type
*Ty
, Value
*ArraySize
,
764 const Twine
&Name
, BasicBlock
*InsertAtEnd
)
765 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
766 getAISize(Ty
->getContext(), ArraySize
), InsertAtEnd
) {
768 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
772 AllocaInst::AllocaInst(const Type
*Ty
, const Twine
&Name
,
773 Instruction
*InsertBefore
)
774 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
775 getAISize(Ty
->getContext(), 0), InsertBefore
) {
777 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
781 AllocaInst::AllocaInst(const Type
*Ty
, const Twine
&Name
,
782 BasicBlock
*InsertAtEnd
)
783 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
784 getAISize(Ty
->getContext(), 0), InsertAtEnd
) {
786 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
790 AllocaInst::AllocaInst(const Type
*Ty
, Value
*ArraySize
, unsigned Align
,
791 const Twine
&Name
, Instruction
*InsertBefore
)
792 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
793 getAISize(Ty
->getContext(), ArraySize
), InsertBefore
) {
795 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
799 AllocaInst::AllocaInst(const Type
*Ty
, Value
*ArraySize
, unsigned Align
,
800 const Twine
&Name
, BasicBlock
*InsertAtEnd
)
801 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
802 getAISize(Ty
->getContext(), ArraySize
), InsertAtEnd
) {
804 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
808 // Out of line virtual method, so the vtable, etc has a home.
809 AllocaInst::~AllocaInst() {
812 void AllocaInst::setAlignment(unsigned Align
) {
813 assert((Align
& (Align
-1)) == 0 && "Alignment is not a power of 2!");
814 assert(Align
<= MaximumAlignment
&&
815 "Alignment is greater than MaximumAlignment!");
816 setInstructionSubclassData(Log2_32(Align
) + 1);
817 assert(getAlignment() == Align
&& "Alignment representation error!");
820 bool AllocaInst::isArrayAllocation() const {
821 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(getOperand(0)))
826 Type
*AllocaInst::getAllocatedType() const {
827 return getType()->getElementType();
830 /// isStaticAlloca - Return true if this alloca is in the entry block of the
831 /// function and is a constant size. If so, the code generator will fold it
832 /// into the prolog/epilog code, so it is basically free.
833 bool AllocaInst::isStaticAlloca() const {
834 // Must be constant size.
835 if (!isa
<ConstantInt
>(getArraySize())) return false;
837 // Must be in the entry block.
838 const BasicBlock
*Parent
= getParent();
839 return Parent
== &Parent
->getParent()->front();
842 //===----------------------------------------------------------------------===//
843 // LoadInst Implementation
844 //===----------------------------------------------------------------------===//
846 void LoadInst::AssertOK() {
847 assert(getOperand(0)->getType()->isPointerTy() &&
848 "Ptr must have pointer type.");
851 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, Instruction
*InsertBef
)
852 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
853 Load
, Ptr
, InsertBef
) {
860 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, BasicBlock
*InsertAE
)
861 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
862 Load
, Ptr
, InsertAE
) {
869 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
870 Instruction
*InsertBef
)
871 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
872 Load
, Ptr
, InsertBef
) {
873 setVolatile(isVolatile
);
879 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
880 unsigned Align
, Instruction
*InsertBef
)
881 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
882 Load
, Ptr
, InsertBef
) {
883 setVolatile(isVolatile
);
889 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
890 unsigned Align
, BasicBlock
*InsertAE
)
891 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
892 Load
, Ptr
, InsertAE
) {
893 setVolatile(isVolatile
);
899 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
900 BasicBlock
*InsertAE
)
901 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
902 Load
, Ptr
, InsertAE
) {
903 setVolatile(isVolatile
);
911 LoadInst::LoadInst(Value
*Ptr
, const char *Name
, Instruction
*InsertBef
)
912 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
913 Load
, Ptr
, InsertBef
) {
917 if (Name
&& Name
[0]) setName(Name
);
920 LoadInst::LoadInst(Value
*Ptr
, const char *Name
, BasicBlock
*InsertAE
)
921 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
922 Load
, Ptr
, InsertAE
) {
926 if (Name
&& Name
[0]) setName(Name
);
929 LoadInst::LoadInst(Value
*Ptr
, const char *Name
, bool isVolatile
,
930 Instruction
*InsertBef
)
931 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
932 Load
, Ptr
, InsertBef
) {
933 setVolatile(isVolatile
);
936 if (Name
&& Name
[0]) setName(Name
);
939 LoadInst::LoadInst(Value
*Ptr
, const char *Name
, bool isVolatile
,
940 BasicBlock
*InsertAE
)
941 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
942 Load
, Ptr
, InsertAE
) {
943 setVolatile(isVolatile
);
946 if (Name
&& Name
[0]) setName(Name
);
949 void LoadInst::setAlignment(unsigned Align
) {
950 assert((Align
& (Align
-1)) == 0 && "Alignment is not a power of 2!");
951 assert(Align
<= MaximumAlignment
&&
952 "Alignment is greater than MaximumAlignment!");
953 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
954 ((Log2_32(Align
)+1)<<1));
955 assert(getAlignment() == Align
&& "Alignment representation error!");
958 //===----------------------------------------------------------------------===//
959 // StoreInst Implementation
960 //===----------------------------------------------------------------------===//
962 void StoreInst::AssertOK() {
963 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
964 assert(getOperand(1)->getType()->isPointerTy() &&
965 "Ptr must have pointer type!");
966 assert(getOperand(0)->getType() ==
967 cast
<PointerType
>(getOperand(1)->getType())->getElementType()
968 && "Ptr must be a pointer to Val type!");
972 StoreInst::StoreInst(Value
*val
, Value
*addr
, Instruction
*InsertBefore
)
973 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
974 OperandTraits
<StoreInst
>::op_begin(this),
975 OperandTraits
<StoreInst
>::operands(this),
984 StoreInst::StoreInst(Value
*val
, Value
*addr
, BasicBlock
*InsertAtEnd
)
985 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
986 OperandTraits
<StoreInst
>::op_begin(this),
987 OperandTraits
<StoreInst
>::operands(this),
996 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
997 Instruction
*InsertBefore
)
998 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
999 OperandTraits
<StoreInst
>::op_begin(this),
1000 OperandTraits
<StoreInst
>::operands(this),
1004 setVolatile(isVolatile
);
1009 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
1010 unsigned Align
, Instruction
*InsertBefore
)
1011 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
1012 OperandTraits
<StoreInst
>::op_begin(this),
1013 OperandTraits
<StoreInst
>::operands(this),
1017 setVolatile(isVolatile
);
1018 setAlignment(Align
);
1022 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
1023 unsigned Align
, BasicBlock
*InsertAtEnd
)
1024 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
1025 OperandTraits
<StoreInst
>::op_begin(this),
1026 OperandTraits
<StoreInst
>::operands(this),
1030 setVolatile(isVolatile
);
1031 setAlignment(Align
);
1035 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
1036 BasicBlock
*InsertAtEnd
)
1037 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
1038 OperandTraits
<StoreInst
>::op_begin(this),
1039 OperandTraits
<StoreInst
>::operands(this),
1043 setVolatile(isVolatile
);
1048 void StoreInst::setAlignment(unsigned Align
) {
1049 assert((Align
& (Align
-1)) == 0 && "Alignment is not a power of 2!");
1050 assert(Align
<= MaximumAlignment
&&
1051 "Alignment is greater than MaximumAlignment!");
1052 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1053 ((Log2_32(Align
)+1) << 1));
1054 assert(getAlignment() == Align
&& "Alignment representation error!");
1057 //===----------------------------------------------------------------------===//
1058 // GetElementPtrInst Implementation
1059 //===----------------------------------------------------------------------===//
1061 static unsigned retrieveAddrSpace(const Value
*Val
) {
1062 return cast
<PointerType
>(Val
->getType())->getAddressSpace();
1065 void GetElementPtrInst::init(Value
*Ptr
, Value
* const *Idx
, unsigned NumIdx
,
1066 const Twine
&Name
) {
1067 assert(NumOperands
== 1+NumIdx
&& "NumOperands not initialized?");
1068 Use
*OL
= OperandList
;
1071 for (unsigned i
= 0; i
!= NumIdx
; ++i
)
1077 void GetElementPtrInst::init(Value
*Ptr
, Value
*Idx
, const Twine
&Name
) {
1078 assert(NumOperands
== 2 && "NumOperands not initialized?");
1079 Use
*OL
= OperandList
;
1086 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst
&GEPI
)
1087 : Instruction(GEPI
.getType(), GetElementPtr
,
1088 OperandTraits
<GetElementPtrInst
>::op_end(this)
1089 - GEPI
.getNumOperands(),
1090 GEPI
.getNumOperands()) {
1091 Use
*OL
= OperandList
;
1092 Use
*GEPIOL
= GEPI
.OperandList
;
1093 for (unsigned i
= 0, E
= NumOperands
; i
!= E
; ++i
)
1095 SubclassOptionalData
= GEPI
.SubclassOptionalData
;
1098 GetElementPtrInst::GetElementPtrInst(Value
*Ptr
, Value
*Idx
,
1099 const Twine
&Name
, Instruction
*InBe
)
1100 : Instruction(PointerType::get(
1101 checkGEPType(getIndexedType(Ptr
->getType(),Idx
)), retrieveAddrSpace(Ptr
)),
1103 OperandTraits
<GetElementPtrInst
>::op_end(this) - 2,
1105 init(Ptr
, Idx
, Name
);
1108 GetElementPtrInst::GetElementPtrInst(Value
*Ptr
, Value
*Idx
,
1109 const Twine
&Name
, BasicBlock
*IAE
)
1110 : Instruction(PointerType::get(
1111 checkGEPType(getIndexedType(Ptr
->getType(),Idx
)),
1112 retrieveAddrSpace(Ptr
)),
1114 OperandTraits
<GetElementPtrInst
>::op_end(this) - 2,
1116 init(Ptr
, Idx
, Name
);
1119 /// getIndexedType - Returns the type of the element that would be accessed with
1120 /// a gep instruction with the specified parameters.
1122 /// The Idxs pointer should point to a continuous piece of memory containing the
1123 /// indices, either as Value* or uint64_t.
1125 /// A null type is returned if the indices are invalid for the specified
1128 template <typename IndexTy
>
1129 static Type
*getIndexedTypeInternal(const Type
*Ptr
, IndexTy
const *Idxs
,
1131 const PointerType
*PTy
= dyn_cast
<PointerType
>(Ptr
);
1132 if (!PTy
) return 0; // Type isn't a pointer type!
1133 Type
*Agg
= PTy
->getElementType();
1135 // Handle the special case of the empty set index set, which is always valid.
1139 // If there is at least one index, the top level type must be sized, otherwise
1140 // it cannot be 'stepped over'.
1141 if (!Agg
->isSized())
1144 unsigned CurIdx
= 1;
1145 for (; CurIdx
!= NumIdx
; ++CurIdx
) {
1146 CompositeType
*CT
= dyn_cast
<CompositeType
>(Agg
);
1147 if (!CT
|| CT
->isPointerTy()) return 0;
1148 IndexTy Index
= Idxs
[CurIdx
];
1149 if (!CT
->indexValid(Index
)) return 0;
1150 Agg
= CT
->getTypeAtIndex(Index
);
1152 return CurIdx
== NumIdx
? Agg
: 0;
1155 Type
*GetElementPtrInst::getIndexedType(const Type
*Ptr
, Value
* const *Idxs
,
1157 return getIndexedTypeInternal(Ptr
, Idxs
, NumIdx
);
1160 Type
*GetElementPtrInst::getIndexedType(const Type
*Ptr
,
1161 Constant
* const *Idxs
,
1163 return getIndexedTypeInternal(Ptr
, Idxs
, NumIdx
);
1166 Type
*GetElementPtrInst::getIndexedType(const Type
*Ptr
,
1167 uint64_t const *Idxs
,
1169 return getIndexedTypeInternal(Ptr
, Idxs
, NumIdx
);
1172 Type
*GetElementPtrInst::getIndexedType(const Type
*Ptr
, Value
*Idx
) {
1173 const PointerType
*PTy
= dyn_cast
<PointerType
>(Ptr
);
1174 if (!PTy
) return 0; // Type isn't a pointer type!
1176 // Check the pointer index.
1177 if (!PTy
->indexValid(Idx
)) return 0;
1179 return PTy
->getElementType();
1183 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1184 /// zeros. If so, the result pointer and the first operand have the same
1185 /// value, just potentially different types.
1186 bool GetElementPtrInst::hasAllZeroIndices() const {
1187 for (unsigned i
= 1, e
= getNumOperands(); i
!= e
; ++i
) {
1188 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(getOperand(i
))) {
1189 if (!CI
->isZero()) return false;
1197 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1198 /// constant integers. If so, the result pointer and the first operand have
1199 /// a constant offset between them.
1200 bool GetElementPtrInst::hasAllConstantIndices() const {
1201 for (unsigned i
= 1, e
= getNumOperands(); i
!= e
; ++i
) {
1202 if (!isa
<ConstantInt
>(getOperand(i
)))
1208 void GetElementPtrInst::setIsInBounds(bool B
) {
1209 cast
<GEPOperator
>(this)->setIsInBounds(B
);
1212 bool GetElementPtrInst::isInBounds() const {
1213 return cast
<GEPOperator
>(this)->isInBounds();
1216 //===----------------------------------------------------------------------===//
1217 // ExtractElementInst Implementation
1218 //===----------------------------------------------------------------------===//
1220 ExtractElementInst::ExtractElementInst(Value
*Val
, Value
*Index
,
1222 Instruction
*InsertBef
)
1223 : Instruction(cast
<VectorType
>(Val
->getType())->getElementType(),
1225 OperandTraits
<ExtractElementInst
>::op_begin(this),
1227 assert(isValidOperands(Val
, Index
) &&
1228 "Invalid extractelement instruction operands!");
1234 ExtractElementInst::ExtractElementInst(Value
*Val
, Value
*Index
,
1236 BasicBlock
*InsertAE
)
1237 : Instruction(cast
<VectorType
>(Val
->getType())->getElementType(),
1239 OperandTraits
<ExtractElementInst
>::op_begin(this),
1241 assert(isValidOperands(Val
, Index
) &&
1242 "Invalid extractelement instruction operands!");
1250 bool ExtractElementInst::isValidOperands(const Value
*Val
, const Value
*Index
) {
1251 if (!Val
->getType()->isVectorTy() || !Index
->getType()->isIntegerTy(32))
1257 //===----------------------------------------------------------------------===//
1258 // InsertElementInst Implementation
1259 //===----------------------------------------------------------------------===//
1261 InsertElementInst::InsertElementInst(Value
*Vec
, Value
*Elt
, Value
*Index
,
1263 Instruction
*InsertBef
)
1264 : Instruction(Vec
->getType(), InsertElement
,
1265 OperandTraits
<InsertElementInst
>::op_begin(this),
1267 assert(isValidOperands(Vec
, Elt
, Index
) &&
1268 "Invalid insertelement instruction operands!");
1275 InsertElementInst::InsertElementInst(Value
*Vec
, Value
*Elt
, Value
*Index
,
1277 BasicBlock
*InsertAE
)
1278 : Instruction(Vec
->getType(), InsertElement
,
1279 OperandTraits
<InsertElementInst
>::op_begin(this),
1281 assert(isValidOperands(Vec
, Elt
, Index
) &&
1282 "Invalid insertelement instruction operands!");
1290 bool InsertElementInst::isValidOperands(const Value
*Vec
, const Value
*Elt
,
1291 const Value
*Index
) {
1292 if (!Vec
->getType()->isVectorTy())
1293 return false; // First operand of insertelement must be vector type.
1295 if (Elt
->getType() != cast
<VectorType
>(Vec
->getType())->getElementType())
1296 return false;// Second operand of insertelement must be vector element type.
1298 if (!Index
->getType()->isIntegerTy(32))
1299 return false; // Third operand of insertelement must be i32.
1304 //===----------------------------------------------------------------------===//
1305 // ShuffleVectorInst Implementation
1306 //===----------------------------------------------------------------------===//
1308 ShuffleVectorInst::ShuffleVectorInst(Value
*V1
, Value
*V2
, Value
*Mask
,
1310 Instruction
*InsertBefore
)
1311 : Instruction(VectorType::get(cast
<VectorType
>(V1
->getType())->getElementType(),
1312 cast
<VectorType
>(Mask
->getType())->getNumElements()),
1314 OperandTraits
<ShuffleVectorInst
>::op_begin(this),
1315 OperandTraits
<ShuffleVectorInst
>::operands(this),
1317 assert(isValidOperands(V1
, V2
, Mask
) &&
1318 "Invalid shuffle vector instruction operands!");
1325 ShuffleVectorInst::ShuffleVectorInst(Value
*V1
, Value
*V2
, Value
*Mask
,
1327 BasicBlock
*InsertAtEnd
)
1328 : Instruction(VectorType::get(cast
<VectorType
>(V1
->getType())->getElementType(),
1329 cast
<VectorType
>(Mask
->getType())->getNumElements()),
1331 OperandTraits
<ShuffleVectorInst
>::op_begin(this),
1332 OperandTraits
<ShuffleVectorInst
>::operands(this),
1334 assert(isValidOperands(V1
, V2
, Mask
) &&
1335 "Invalid shuffle vector instruction operands!");
1343 bool ShuffleVectorInst::isValidOperands(const Value
*V1
, const Value
*V2
,
1344 const Value
*Mask
) {
1345 if (!V1
->getType()->isVectorTy() || V1
->getType() != V2
->getType())
1348 const VectorType
*MaskTy
= dyn_cast
<VectorType
>(Mask
->getType());
1349 if (MaskTy
== 0 || !MaskTy
->getElementType()->isIntegerTy(32))
1352 // Check to see if Mask is valid.
1353 if (const ConstantVector
*MV
= dyn_cast
<ConstantVector
>(Mask
)) {
1354 const VectorType
*VTy
= cast
<VectorType
>(V1
->getType());
1355 for (unsigned i
= 0, e
= MV
->getNumOperands(); i
!= e
; ++i
) {
1356 if (ConstantInt
* CI
= dyn_cast
<ConstantInt
>(MV
->getOperand(i
))) {
1357 if (CI
->uge(VTy
->getNumElements()*2))
1359 } else if (!isa
<UndefValue
>(MV
->getOperand(i
))) {
1364 else if (!isa
<UndefValue
>(Mask
) && !isa
<ConstantAggregateZero
>(Mask
))
1370 /// getMaskValue - Return the index from the shuffle mask for the specified
1371 /// output result. This is either -1 if the element is undef or a number less
1372 /// than 2*numelements.
1373 int ShuffleVectorInst::getMaskValue(unsigned i
) const {
1374 const Constant
*Mask
= cast
<Constant
>(getOperand(2));
1375 if (isa
<UndefValue
>(Mask
)) return -1;
1376 if (isa
<ConstantAggregateZero
>(Mask
)) return 0;
1377 const ConstantVector
*MaskCV
= cast
<ConstantVector
>(Mask
);
1378 assert(i
< MaskCV
->getNumOperands() && "Index out of range");
1380 if (isa
<UndefValue
>(MaskCV
->getOperand(i
)))
1382 return cast
<ConstantInt
>(MaskCV
->getOperand(i
))->getZExtValue();
1385 //===----------------------------------------------------------------------===//
1386 // InsertValueInst Class
1387 //===----------------------------------------------------------------------===//
1389 void InsertValueInst::init(Value
*Agg
, Value
*Val
, const unsigned *Idx
,
1390 unsigned NumIdx
, const Twine
&Name
) {
1391 assert(NumOperands
== 2 && "NumOperands not initialized?");
1392 assert(ExtractValueInst::getIndexedType(Agg
->getType(), Idx
, Idx
+ NumIdx
) ==
1393 Val
->getType() && "Inserted value must match indexed type!");
1397 Indices
.append(Idx
, Idx
+ NumIdx
);
1401 void InsertValueInst::init(Value
*Agg
, Value
*Val
, unsigned Idx
,
1402 const Twine
&Name
) {
1403 assert(NumOperands
== 2 && "NumOperands not initialized?");
1404 assert(ExtractValueInst::getIndexedType(Agg
->getType(), Idx
) == Val
->getType()
1405 && "Inserted value must match indexed type!");
1409 Indices
.push_back(Idx
);
1413 InsertValueInst::InsertValueInst(const InsertValueInst
&IVI
)
1414 : Instruction(IVI
.getType(), InsertValue
,
1415 OperandTraits
<InsertValueInst
>::op_begin(this), 2),
1416 Indices(IVI
.Indices
) {
1417 Op
<0>() = IVI
.getOperand(0);
1418 Op
<1>() = IVI
.getOperand(1);
1419 SubclassOptionalData
= IVI
.SubclassOptionalData
;
1422 InsertValueInst::InsertValueInst(Value
*Agg
,
1426 Instruction
*InsertBefore
)
1427 : Instruction(Agg
->getType(), InsertValue
,
1428 OperandTraits
<InsertValueInst
>::op_begin(this),
1430 init(Agg
, Val
, Idx
, Name
);
1433 InsertValueInst::InsertValueInst(Value
*Agg
,
1437 BasicBlock
*InsertAtEnd
)
1438 : Instruction(Agg
->getType(), InsertValue
,
1439 OperandTraits
<InsertValueInst
>::op_begin(this),
1441 init(Agg
, Val
, Idx
, Name
);
1444 //===----------------------------------------------------------------------===//
1445 // ExtractValueInst Class
1446 //===----------------------------------------------------------------------===//
1448 void ExtractValueInst::init(const unsigned *Idx
, unsigned NumIdx
,
1449 const Twine
&Name
) {
1450 assert(NumOperands
== 1 && "NumOperands not initialized?");
1452 Indices
.append(Idx
, Idx
+ NumIdx
);
1456 void ExtractValueInst::init(unsigned Idx
, const Twine
&Name
) {
1457 assert(NumOperands
== 1 && "NumOperands not initialized?");
1459 Indices
.push_back(Idx
);
1463 ExtractValueInst::ExtractValueInst(const ExtractValueInst
&EVI
)
1464 : UnaryInstruction(EVI
.getType(), ExtractValue
, EVI
.getOperand(0)),
1465 Indices(EVI
.Indices
) {
1466 SubclassOptionalData
= EVI
.SubclassOptionalData
;
1469 // getIndexedType - Returns the type of the element that would be extracted
1470 // with an extractvalue instruction with the specified parameters.
1472 // A null type is returned if the indices are invalid for the specified
1475 Type
*ExtractValueInst::getIndexedType(const Type
*Agg
,
1476 const unsigned *Idxs
,
1478 for (unsigned CurIdx
= 0; CurIdx
!= NumIdx
; ++CurIdx
) {
1479 unsigned Index
= Idxs
[CurIdx
];
1480 // We can't use CompositeType::indexValid(Index) here.
1481 // indexValid() always returns true for arrays because getelementptr allows
1482 // out-of-bounds indices. Since we don't allow those for extractvalue and
1483 // insertvalue we need to check array indexing manually.
1484 // Since the only other types we can index into are struct types it's just
1485 // as easy to check those manually as well.
1486 if (const ArrayType
*AT
= dyn_cast
<ArrayType
>(Agg
)) {
1487 if (Index
>= AT
->getNumElements())
1489 } else if (const StructType
*ST
= dyn_cast
<StructType
>(Agg
)) {
1490 if (Index
>= ST
->getNumElements())
1493 // Not a valid type to index into.
1497 Agg
= cast
<CompositeType
>(Agg
)->getTypeAtIndex(Index
);
1499 return const_cast<Type
*>(Agg
);
1502 Type
*ExtractValueInst::getIndexedType(const Type
*Agg
, unsigned Idx
) {
1503 return getIndexedType(Agg
, &Idx
, 1);
1506 //===----------------------------------------------------------------------===//
1507 // BinaryOperator Class
1508 //===----------------------------------------------------------------------===//
1510 BinaryOperator::BinaryOperator(BinaryOps iType
, Value
*S1
, Value
*S2
,
1511 const Type
*Ty
, const Twine
&Name
,
1512 Instruction
*InsertBefore
)
1513 : Instruction(Ty
, iType
,
1514 OperandTraits
<BinaryOperator
>::op_begin(this),
1515 OperandTraits
<BinaryOperator
>::operands(this),
1523 BinaryOperator::BinaryOperator(BinaryOps iType
, Value
*S1
, Value
*S2
,
1524 const Type
*Ty
, const Twine
&Name
,
1525 BasicBlock
*InsertAtEnd
)
1526 : Instruction(Ty
, iType
,
1527 OperandTraits
<BinaryOperator
>::op_begin(this),
1528 OperandTraits
<BinaryOperator
>::operands(this),
1537 void BinaryOperator::init(BinaryOps iType
) {
1538 Value
*LHS
= getOperand(0), *RHS
= getOperand(1);
1539 (void)LHS
; (void)RHS
; // Silence warnings.
1540 assert(LHS
->getType() == RHS
->getType() &&
1541 "Binary operator operand types must match!");
1546 assert(getType() == LHS
->getType() &&
1547 "Arithmetic operation should return same type as operands!");
1548 assert(getType()->isIntOrIntVectorTy() &&
1549 "Tried to create an integer operation on a non-integer type!");
1551 case FAdd
: case FSub
:
1553 assert(getType() == LHS
->getType() &&
1554 "Arithmetic operation should return same type as operands!");
1555 assert(getType()->isFPOrFPVectorTy() &&
1556 "Tried to create a floating-point operation on a "
1557 "non-floating-point type!");
1561 assert(getType() == LHS
->getType() &&
1562 "Arithmetic operation should return same type as operands!");
1563 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1564 cast
<VectorType
>(getType())->getElementType()->isIntegerTy())) &&
1565 "Incorrect operand type (not integer) for S/UDIV");
1568 assert(getType() == LHS
->getType() &&
1569 "Arithmetic operation should return same type as operands!");
1570 assert(getType()->isFPOrFPVectorTy() &&
1571 "Incorrect operand type (not floating point) for FDIV");
1575 assert(getType() == LHS
->getType() &&
1576 "Arithmetic operation should return same type as operands!");
1577 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1578 cast
<VectorType
>(getType())->getElementType()->isIntegerTy())) &&
1579 "Incorrect operand type (not integer) for S/UREM");
1582 assert(getType() == LHS
->getType() &&
1583 "Arithmetic operation should return same type as operands!");
1584 assert(getType()->isFPOrFPVectorTy() &&
1585 "Incorrect operand type (not floating point) for FREM");
1590 assert(getType() == LHS
->getType() &&
1591 "Shift operation should return same type as operands!");
1592 assert((getType()->isIntegerTy() ||
1593 (getType()->isVectorTy() &&
1594 cast
<VectorType
>(getType())->getElementType()->isIntegerTy())) &&
1595 "Tried to create a shift operation on a non-integral type!");
1599 assert(getType() == LHS
->getType() &&
1600 "Logical operation should return same type as operands!");
1601 assert((getType()->isIntegerTy() ||
1602 (getType()->isVectorTy() &&
1603 cast
<VectorType
>(getType())->getElementType()->isIntegerTy())) &&
1604 "Tried to create a logical operation on a non-integral type!");
1612 BinaryOperator
*BinaryOperator::Create(BinaryOps Op
, Value
*S1
, Value
*S2
,
1614 Instruction
*InsertBefore
) {
1615 assert(S1
->getType() == S2
->getType() &&
1616 "Cannot create binary operator with two operands of differing type!");
1617 return new BinaryOperator(Op
, S1
, S2
, S1
->getType(), Name
, InsertBefore
);
1620 BinaryOperator
*BinaryOperator::Create(BinaryOps Op
, Value
*S1
, Value
*S2
,
1622 BasicBlock
*InsertAtEnd
) {
1623 BinaryOperator
*Res
= Create(Op
, S1
, S2
, Name
);
1624 InsertAtEnd
->getInstList().push_back(Res
);
1628 BinaryOperator
*BinaryOperator::CreateNeg(Value
*Op
, const Twine
&Name
,
1629 Instruction
*InsertBefore
) {
1630 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1631 return new BinaryOperator(Instruction::Sub
,
1633 Op
->getType(), Name
, InsertBefore
);
1636 BinaryOperator
*BinaryOperator::CreateNeg(Value
*Op
, const Twine
&Name
,
1637 BasicBlock
*InsertAtEnd
) {
1638 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1639 return new BinaryOperator(Instruction::Sub
,
1641 Op
->getType(), Name
, InsertAtEnd
);
1644 BinaryOperator
*BinaryOperator::CreateNSWNeg(Value
*Op
, const Twine
&Name
,
1645 Instruction
*InsertBefore
) {
1646 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1647 return BinaryOperator::CreateNSWSub(zero
, Op
, Name
, InsertBefore
);
1650 BinaryOperator
*BinaryOperator::CreateNSWNeg(Value
*Op
, const Twine
&Name
,
1651 BasicBlock
*InsertAtEnd
) {
1652 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1653 return BinaryOperator::CreateNSWSub(zero
, Op
, Name
, InsertAtEnd
);
1656 BinaryOperator
*BinaryOperator::CreateNUWNeg(Value
*Op
, const Twine
&Name
,
1657 Instruction
*InsertBefore
) {
1658 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1659 return BinaryOperator::CreateNUWSub(zero
, Op
, Name
, InsertBefore
);
1662 BinaryOperator
*BinaryOperator::CreateNUWNeg(Value
*Op
, const Twine
&Name
,
1663 BasicBlock
*InsertAtEnd
) {
1664 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1665 return BinaryOperator::CreateNUWSub(zero
, Op
, Name
, InsertAtEnd
);
1668 BinaryOperator
*BinaryOperator::CreateFNeg(Value
*Op
, const Twine
&Name
,
1669 Instruction
*InsertBefore
) {
1670 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1671 return new BinaryOperator(Instruction::FSub
,
1673 Op
->getType(), Name
, InsertBefore
);
1676 BinaryOperator
*BinaryOperator::CreateFNeg(Value
*Op
, const Twine
&Name
,
1677 BasicBlock
*InsertAtEnd
) {
1678 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1679 return new BinaryOperator(Instruction::FSub
,
1681 Op
->getType(), Name
, InsertAtEnd
);
1684 BinaryOperator
*BinaryOperator::CreateNot(Value
*Op
, const Twine
&Name
,
1685 Instruction
*InsertBefore
) {
1687 if (const VectorType
*PTy
= dyn_cast
<VectorType
>(Op
->getType())) {
1688 C
= Constant::getAllOnesValue(PTy
->getElementType());
1689 C
= ConstantVector::get(
1690 std::vector
<Constant
*>(PTy
->getNumElements(), C
));
1692 C
= Constant::getAllOnesValue(Op
->getType());
1695 return new BinaryOperator(Instruction::Xor
, Op
, C
,
1696 Op
->getType(), Name
, InsertBefore
);
1699 BinaryOperator
*BinaryOperator::CreateNot(Value
*Op
, const Twine
&Name
,
1700 BasicBlock
*InsertAtEnd
) {
1702 if (const VectorType
*PTy
= dyn_cast
<VectorType
>(Op
->getType())) {
1703 // Create a vector of all ones values.
1704 Constant
*Elt
= Constant::getAllOnesValue(PTy
->getElementType());
1705 AllOnes
= ConstantVector::get(
1706 std::vector
<Constant
*>(PTy
->getNumElements(), Elt
));
1708 AllOnes
= Constant::getAllOnesValue(Op
->getType());
1711 return new BinaryOperator(Instruction::Xor
, Op
, AllOnes
,
1712 Op
->getType(), Name
, InsertAtEnd
);
1716 // isConstantAllOnes - Helper function for several functions below
1717 static inline bool isConstantAllOnes(const Value
*V
) {
1718 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(V
))
1719 return CI
->isAllOnesValue();
1720 if (const ConstantVector
*CV
= dyn_cast
<ConstantVector
>(V
))
1721 return CV
->isAllOnesValue();
1725 bool BinaryOperator::isNeg(const Value
*V
) {
1726 if (const BinaryOperator
*Bop
= dyn_cast
<BinaryOperator
>(V
))
1727 if (Bop
->getOpcode() == Instruction::Sub
)
1728 if (Constant
* C
= dyn_cast
<Constant
>(Bop
->getOperand(0)))
1729 return C
->isNegativeZeroValue();
1733 bool BinaryOperator::isFNeg(const Value
*V
) {
1734 if (const BinaryOperator
*Bop
= dyn_cast
<BinaryOperator
>(V
))
1735 if (Bop
->getOpcode() == Instruction::FSub
)
1736 if (Constant
* C
= dyn_cast
<Constant
>(Bop
->getOperand(0)))
1737 return C
->isNegativeZeroValue();
1741 bool BinaryOperator::isNot(const Value
*V
) {
1742 if (const BinaryOperator
*Bop
= dyn_cast
<BinaryOperator
>(V
))
1743 return (Bop
->getOpcode() == Instruction::Xor
&&
1744 (isConstantAllOnes(Bop
->getOperand(1)) ||
1745 isConstantAllOnes(Bop
->getOperand(0))));
1749 Value
*BinaryOperator::getNegArgument(Value
*BinOp
) {
1750 return cast
<BinaryOperator
>(BinOp
)->getOperand(1);
1753 const Value
*BinaryOperator::getNegArgument(const Value
*BinOp
) {
1754 return getNegArgument(const_cast<Value
*>(BinOp
));
1757 Value
*BinaryOperator::getFNegArgument(Value
*BinOp
) {
1758 return cast
<BinaryOperator
>(BinOp
)->getOperand(1);
1761 const Value
*BinaryOperator::getFNegArgument(const Value
*BinOp
) {
1762 return getFNegArgument(const_cast<Value
*>(BinOp
));
1765 Value
*BinaryOperator::getNotArgument(Value
*BinOp
) {
1766 assert(isNot(BinOp
) && "getNotArgument on non-'not' instruction!");
1767 BinaryOperator
*BO
= cast
<BinaryOperator
>(BinOp
);
1768 Value
*Op0
= BO
->getOperand(0);
1769 Value
*Op1
= BO
->getOperand(1);
1770 if (isConstantAllOnes(Op0
)) return Op1
;
1772 assert(isConstantAllOnes(Op1
));
1776 const Value
*BinaryOperator::getNotArgument(const Value
*BinOp
) {
1777 return getNotArgument(const_cast<Value
*>(BinOp
));
1781 // swapOperands - Exchange the two operands to this instruction. This
1782 // instruction is safe to use on any binary instruction and does not
1783 // modify the semantics of the instruction. If the instruction is
1784 // order dependent (SetLT f.e.) the opcode is changed.
1786 bool BinaryOperator::swapOperands() {
1787 if (!isCommutative())
1788 return true; // Can't commute operands
1789 Op
<0>().swap(Op
<1>());
1793 void BinaryOperator::setHasNoUnsignedWrap(bool b
) {
1794 cast
<OverflowingBinaryOperator
>(this)->setHasNoUnsignedWrap(b
);
1797 void BinaryOperator::setHasNoSignedWrap(bool b
) {
1798 cast
<OverflowingBinaryOperator
>(this)->setHasNoSignedWrap(b
);
1801 void BinaryOperator::setIsExact(bool b
) {
1802 cast
<PossiblyExactOperator
>(this)->setIsExact(b
);
1805 bool BinaryOperator::hasNoUnsignedWrap() const {
1806 return cast
<OverflowingBinaryOperator
>(this)->hasNoUnsignedWrap();
1809 bool BinaryOperator::hasNoSignedWrap() const {
1810 return cast
<OverflowingBinaryOperator
>(this)->hasNoSignedWrap();
1813 bool BinaryOperator::isExact() const {
1814 return cast
<PossiblyExactOperator
>(this)->isExact();
1817 //===----------------------------------------------------------------------===//
1819 //===----------------------------------------------------------------------===//
1821 // Just determine if this cast only deals with integral->integral conversion.
1822 bool CastInst::isIntegerCast() const {
1823 switch (getOpcode()) {
1824 default: return false;
1825 case Instruction::ZExt
:
1826 case Instruction::SExt
:
1827 case Instruction::Trunc
:
1829 case Instruction::BitCast
:
1830 return getOperand(0)->getType()->isIntegerTy() &&
1831 getType()->isIntegerTy();
1835 bool CastInst::isLosslessCast() const {
1836 // Only BitCast can be lossless, exit fast if we're not BitCast
1837 if (getOpcode() != Instruction::BitCast
)
1840 // Identity cast is always lossless
1841 const Type
* SrcTy
= getOperand(0)->getType();
1842 const Type
* DstTy
= getType();
1846 // Pointer to pointer is always lossless.
1847 if (SrcTy
->isPointerTy())
1848 return DstTy
->isPointerTy();
1849 return false; // Other types have no identity values
1852 /// This function determines if the CastInst does not require any bits to be
1853 /// changed in order to effect the cast. Essentially, it identifies cases where
1854 /// no code gen is necessary for the cast, hence the name no-op cast. For
1855 /// example, the following are all no-op casts:
1856 /// # bitcast i32* %x to i8*
1857 /// # bitcast <2 x i32> %x to <4 x i16>
1858 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1859 /// @brief Determine if the described cast is a no-op.
1860 bool CastInst::isNoopCast(Instruction::CastOps Opcode
,
1863 const Type
*IntPtrTy
) {
1866 assert(!"Invalid CastOp");
1867 case Instruction::Trunc
:
1868 case Instruction::ZExt
:
1869 case Instruction::SExt
:
1870 case Instruction::FPTrunc
:
1871 case Instruction::FPExt
:
1872 case Instruction::UIToFP
:
1873 case Instruction::SIToFP
:
1874 case Instruction::FPToUI
:
1875 case Instruction::FPToSI
:
1876 return false; // These always modify bits
1877 case Instruction::BitCast
:
1878 return true; // BitCast never modifies bits.
1879 case Instruction::PtrToInt
:
1880 return IntPtrTy
->getScalarSizeInBits() ==
1881 DestTy
->getScalarSizeInBits();
1882 case Instruction::IntToPtr
:
1883 return IntPtrTy
->getScalarSizeInBits() ==
1884 SrcTy
->getScalarSizeInBits();
1888 /// @brief Determine if a cast is a no-op.
1889 bool CastInst::isNoopCast(const Type
*IntPtrTy
) const {
1890 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy
);
1893 /// This function determines if a pair of casts can be eliminated and what
1894 /// opcode should be used in the elimination. This assumes that there are two
1895 /// instructions like this:
1896 /// * %F = firstOpcode SrcTy %x to MidTy
1897 /// * %S = secondOpcode MidTy %F to DstTy
1898 /// The function returns a resultOpcode so these two casts can be replaced with:
1899 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1900 /// If no such cast is permited, the function returns 0.
1901 unsigned CastInst::isEliminableCastPair(
1902 Instruction::CastOps firstOp
, Instruction::CastOps secondOp
,
1903 const Type
*SrcTy
, const Type
*MidTy
, const Type
*DstTy
, const Type
*IntPtrTy
)
1905 // Define the 144 possibilities for these two cast instructions. The values
1906 // in this matrix determine what to do in a given situation and select the
1907 // case in the switch below. The rows correspond to firstOp, the columns
1908 // correspond to secondOp. In looking at the table below, keep in mind
1909 // the following cast properties:
1911 // Size Compare Source Destination
1912 // Operator Src ? Size Type Sign Type Sign
1913 // -------- ------------ ------------------- ---------------------
1914 // TRUNC > Integer Any Integral Any
1915 // ZEXT < Integral Unsigned Integer Any
1916 // SEXT < Integral Signed Integer Any
1917 // FPTOUI n/a FloatPt n/a Integral Unsigned
1918 // FPTOSI n/a FloatPt n/a Integral Signed
1919 // UITOFP n/a Integral Unsigned FloatPt n/a
1920 // SITOFP n/a Integral Signed FloatPt n/a
1921 // FPTRUNC > FloatPt n/a FloatPt n/a
1922 // FPEXT < FloatPt n/a FloatPt n/a
1923 // PTRTOINT n/a Pointer n/a Integral Unsigned
1924 // INTTOPTR n/a Integral Unsigned Pointer n/a
1925 // BITCAST = FirstClass n/a FirstClass n/a
1927 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1928 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1929 // into "fptoui double to i64", but this loses information about the range
1930 // of the produced value (we no longer know the top-part is all zeros).
1931 // Further this conversion is often much more expensive for typical hardware,
1932 // and causes issues when building libgcc. We disallow fptosi+sext for the
1934 const unsigned numCastOps
=
1935 Instruction::CastOpsEnd
- Instruction::CastOpsBegin
;
1936 static const uint8_t CastResults
[numCastOps
][numCastOps
] = {
1937 // T F F U S F F P I B -+
1938 // R Z S P P I I T P 2 N T |
1939 // U E E 2 2 2 2 R E I T C +- secondOp
1940 // N X X U S F F N X N 2 V |
1941 // C T T I I P P C T T P T -+
1942 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1943 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1944 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
1945 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1946 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
1947 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1948 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1949 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1950 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1951 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1952 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1953 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1956 // If either of the casts are a bitcast from scalar to vector, disallow the
1958 if ((firstOp
== Instruction::BitCast
&&
1959 isa
<VectorType
>(SrcTy
) != isa
<VectorType
>(MidTy
)) ||
1960 (secondOp
== Instruction::BitCast
&&
1961 isa
<VectorType
>(MidTy
) != isa
<VectorType
>(DstTy
)))
1962 return 0; // Disallowed
1964 int ElimCase
= CastResults
[firstOp
-Instruction::CastOpsBegin
]
1965 [secondOp
-Instruction::CastOpsBegin
];
1968 // categorically disallowed
1971 // allowed, use first cast's opcode
1974 // allowed, use second cast's opcode
1977 // no-op cast in second op implies firstOp as long as the DestTy
1978 // is integer and we are not converting between a vector and a
1980 if (!SrcTy
->isVectorTy() && DstTy
->isIntegerTy())
1984 // no-op cast in second op implies firstOp as long as the DestTy
1985 // is floating point.
1986 if (DstTy
->isFloatingPointTy())
1990 // no-op cast in first op implies secondOp as long as the SrcTy
1992 if (SrcTy
->isIntegerTy())
1996 // no-op cast in first op implies secondOp as long as the SrcTy
1997 // is a floating point.
1998 if (SrcTy
->isFloatingPointTy())
2002 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
2005 unsigned PtrSize
= IntPtrTy
->getScalarSizeInBits();
2006 unsigned MidSize
= MidTy
->getScalarSizeInBits();
2007 if (MidSize
>= PtrSize
)
2008 return Instruction::BitCast
;
2012 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2013 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2014 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2015 unsigned SrcSize
= SrcTy
->getScalarSizeInBits();
2016 unsigned DstSize
= DstTy
->getScalarSizeInBits();
2017 if (SrcSize
== DstSize
)
2018 return Instruction::BitCast
;
2019 else if (SrcSize
< DstSize
)
2023 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2024 return Instruction::ZExt
;
2026 // fpext followed by ftrunc is allowed if the bit size returned to is
2027 // the same as the original, in which case its just a bitcast
2029 return Instruction::BitCast
;
2030 return 0; // If the types are not the same we can't eliminate it.
2032 // bitcast followed by ptrtoint is allowed as long as the bitcast
2033 // is a pointer to pointer cast.
2034 if (SrcTy
->isPointerTy() && MidTy
->isPointerTy())
2038 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2039 if (MidTy
->isPointerTy() && DstTy
->isPointerTy())
2043 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2046 unsigned PtrSize
= IntPtrTy
->getScalarSizeInBits();
2047 unsigned SrcSize
= SrcTy
->getScalarSizeInBits();
2048 unsigned DstSize
= DstTy
->getScalarSizeInBits();
2049 if (SrcSize
<= PtrSize
&& SrcSize
== DstSize
)
2050 return Instruction::BitCast
;
2054 // cast combination can't happen (error in input). This is for all cases
2055 // where the MidTy is not the same for the two cast instructions.
2056 assert(!"Invalid Cast Combination");
2059 assert(!"Error in CastResults table!!!");
2065 CastInst
*CastInst::Create(Instruction::CastOps op
, Value
*S
, const Type
*Ty
,
2066 const Twine
&Name
, Instruction
*InsertBefore
) {
2067 assert(castIsValid(op
, S
, Ty
) && "Invalid cast!");
2068 // Construct and return the appropriate CastInst subclass
2070 case Trunc
: return new TruncInst (S
, Ty
, Name
, InsertBefore
);
2071 case ZExt
: return new ZExtInst (S
, Ty
, Name
, InsertBefore
);
2072 case SExt
: return new SExtInst (S
, Ty
, Name
, InsertBefore
);
2073 case FPTrunc
: return new FPTruncInst (S
, Ty
, Name
, InsertBefore
);
2074 case FPExt
: return new FPExtInst (S
, Ty
, Name
, InsertBefore
);
2075 case UIToFP
: return new UIToFPInst (S
, Ty
, Name
, InsertBefore
);
2076 case SIToFP
: return new SIToFPInst (S
, Ty
, Name
, InsertBefore
);
2077 case FPToUI
: return new FPToUIInst (S
, Ty
, Name
, InsertBefore
);
2078 case FPToSI
: return new FPToSIInst (S
, Ty
, Name
, InsertBefore
);
2079 case PtrToInt
: return new PtrToIntInst (S
, Ty
, Name
, InsertBefore
);
2080 case IntToPtr
: return new IntToPtrInst (S
, Ty
, Name
, InsertBefore
);
2081 case BitCast
: return new BitCastInst (S
, Ty
, Name
, InsertBefore
);
2083 assert(!"Invalid opcode provided");
2088 CastInst
*CastInst::Create(Instruction::CastOps op
, Value
*S
, const Type
*Ty
,
2089 const Twine
&Name
, BasicBlock
*InsertAtEnd
) {
2090 assert(castIsValid(op
, S
, Ty
) && "Invalid cast!");
2091 // Construct and return the appropriate CastInst subclass
2093 case Trunc
: return new TruncInst (S
, Ty
, Name
, InsertAtEnd
);
2094 case ZExt
: return new ZExtInst (S
, Ty
, Name
, InsertAtEnd
);
2095 case SExt
: return new SExtInst (S
, Ty
, Name
, InsertAtEnd
);
2096 case FPTrunc
: return new FPTruncInst (S
, Ty
, Name
, InsertAtEnd
);
2097 case FPExt
: return new FPExtInst (S
, Ty
, Name
, InsertAtEnd
);
2098 case UIToFP
: return new UIToFPInst (S
, Ty
, Name
, InsertAtEnd
);
2099 case SIToFP
: return new SIToFPInst (S
, Ty
, Name
, InsertAtEnd
);
2100 case FPToUI
: return new FPToUIInst (S
, Ty
, Name
, InsertAtEnd
);
2101 case FPToSI
: return new FPToSIInst (S
, Ty
, Name
, InsertAtEnd
);
2102 case PtrToInt
: return new PtrToIntInst (S
, Ty
, Name
, InsertAtEnd
);
2103 case IntToPtr
: return new IntToPtrInst (S
, Ty
, Name
, InsertAtEnd
);
2104 case BitCast
: return new BitCastInst (S
, Ty
, Name
, InsertAtEnd
);
2106 assert(!"Invalid opcode provided");
2111 CastInst
*CastInst::CreateZExtOrBitCast(Value
*S
, const Type
*Ty
,
2113 Instruction
*InsertBefore
) {
2114 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2115 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2116 return Create(Instruction::ZExt
, S
, Ty
, Name
, InsertBefore
);
2119 CastInst
*CastInst::CreateZExtOrBitCast(Value
*S
, const Type
*Ty
,
2121 BasicBlock
*InsertAtEnd
) {
2122 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2123 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2124 return Create(Instruction::ZExt
, S
, Ty
, Name
, InsertAtEnd
);
2127 CastInst
*CastInst::CreateSExtOrBitCast(Value
*S
, const Type
*Ty
,
2129 Instruction
*InsertBefore
) {
2130 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2131 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2132 return Create(Instruction::SExt
, S
, Ty
, Name
, InsertBefore
);
2135 CastInst
*CastInst::CreateSExtOrBitCast(Value
*S
, const Type
*Ty
,
2137 BasicBlock
*InsertAtEnd
) {
2138 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2139 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2140 return Create(Instruction::SExt
, S
, Ty
, Name
, InsertAtEnd
);
2143 CastInst
*CastInst::CreateTruncOrBitCast(Value
*S
, const Type
*Ty
,
2145 Instruction
*InsertBefore
) {
2146 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2147 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2148 return Create(Instruction::Trunc
, S
, Ty
, Name
, InsertBefore
);
2151 CastInst
*CastInst::CreateTruncOrBitCast(Value
*S
, const Type
*Ty
,
2153 BasicBlock
*InsertAtEnd
) {
2154 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2155 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2156 return Create(Instruction::Trunc
, S
, Ty
, Name
, InsertAtEnd
);
2159 CastInst
*CastInst::CreatePointerCast(Value
*S
, const Type
*Ty
,
2161 BasicBlock
*InsertAtEnd
) {
2162 assert(S
->getType()->isPointerTy() && "Invalid cast");
2163 assert((Ty
->isIntegerTy() || Ty
->isPointerTy()) &&
2166 if (Ty
->isIntegerTy())
2167 return Create(Instruction::PtrToInt
, S
, Ty
, Name
, InsertAtEnd
);
2168 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2171 /// @brief Create a BitCast or a PtrToInt cast instruction
2172 CastInst
*CastInst::CreatePointerCast(Value
*S
, const Type
*Ty
,
2174 Instruction
*InsertBefore
) {
2175 assert(S
->getType()->isPointerTy() && "Invalid cast");
2176 assert((Ty
->isIntegerTy() || Ty
->isPointerTy()) &&
2179 if (Ty
->isIntegerTy())
2180 return Create(Instruction::PtrToInt
, S
, Ty
, Name
, InsertBefore
);
2181 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2184 CastInst
*CastInst::CreateIntegerCast(Value
*C
, const Type
*Ty
,
2185 bool isSigned
, const Twine
&Name
,
2186 Instruction
*InsertBefore
) {
2187 assert(C
->getType()->isIntOrIntVectorTy() && Ty
->isIntOrIntVectorTy() &&
2188 "Invalid integer cast");
2189 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2190 unsigned DstBits
= Ty
->getScalarSizeInBits();
2191 Instruction::CastOps opcode
=
2192 (SrcBits
== DstBits
? Instruction::BitCast
:
2193 (SrcBits
> DstBits
? Instruction::Trunc
:
2194 (isSigned
? Instruction::SExt
: Instruction::ZExt
)));
2195 return Create(opcode
, C
, Ty
, Name
, InsertBefore
);
2198 CastInst
*CastInst::CreateIntegerCast(Value
*C
, const Type
*Ty
,
2199 bool isSigned
, const Twine
&Name
,
2200 BasicBlock
*InsertAtEnd
) {
2201 assert(C
->getType()->isIntOrIntVectorTy() && Ty
->isIntOrIntVectorTy() &&
2203 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2204 unsigned DstBits
= Ty
->getScalarSizeInBits();
2205 Instruction::CastOps opcode
=
2206 (SrcBits
== DstBits
? Instruction::BitCast
:
2207 (SrcBits
> DstBits
? Instruction::Trunc
:
2208 (isSigned
? Instruction::SExt
: Instruction::ZExt
)));
2209 return Create(opcode
, C
, Ty
, Name
, InsertAtEnd
);
2212 CastInst
*CastInst::CreateFPCast(Value
*C
, const Type
*Ty
,
2214 Instruction
*InsertBefore
) {
2215 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isFPOrFPVectorTy() &&
2217 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2218 unsigned DstBits
= Ty
->getScalarSizeInBits();
2219 Instruction::CastOps opcode
=
2220 (SrcBits
== DstBits
? Instruction::BitCast
:
2221 (SrcBits
> DstBits
? Instruction::FPTrunc
: Instruction::FPExt
));
2222 return Create(opcode
, C
, Ty
, Name
, InsertBefore
);
2225 CastInst
*CastInst::CreateFPCast(Value
*C
, const Type
*Ty
,
2227 BasicBlock
*InsertAtEnd
) {
2228 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isFPOrFPVectorTy() &&
2230 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2231 unsigned DstBits
= Ty
->getScalarSizeInBits();
2232 Instruction::CastOps opcode
=
2233 (SrcBits
== DstBits
? Instruction::BitCast
:
2234 (SrcBits
> DstBits
? Instruction::FPTrunc
: Instruction::FPExt
));
2235 return Create(opcode
, C
, Ty
, Name
, InsertAtEnd
);
2238 // Check whether it is valid to call getCastOpcode for these types.
2239 // This routine must be kept in sync with getCastOpcode.
2240 bool CastInst::isCastable(const Type
*SrcTy
, const Type
*DestTy
) {
2241 if (!SrcTy
->isFirstClassType() || !DestTy
->isFirstClassType())
2244 if (SrcTy
== DestTy
)
2247 if (const VectorType
*SrcVecTy
= dyn_cast
<VectorType
>(SrcTy
))
2248 if (const VectorType
*DestVecTy
= dyn_cast
<VectorType
>(DestTy
))
2249 if (SrcVecTy
->getNumElements() == DestVecTy
->getNumElements()) {
2250 // An element by element cast. Valid if casting the elements is valid.
2251 SrcTy
= SrcVecTy
->getElementType();
2252 DestTy
= DestVecTy
->getElementType();
2255 // Get the bit sizes, we'll need these
2256 unsigned SrcBits
= SrcTy
->getPrimitiveSizeInBits(); // 0 for ptr
2257 unsigned DestBits
= DestTy
->getPrimitiveSizeInBits(); // 0 for ptr
2259 // Run through the possibilities ...
2260 if (DestTy
->isIntegerTy()) { // Casting to integral
2261 if (SrcTy
->isIntegerTy()) { // Casting from integral
2263 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
2265 } else if (SrcTy
->isVectorTy()) { // Casting from vector
2266 return DestBits
== SrcBits
;
2267 } else { // Casting from something else
2268 return SrcTy
->isPointerTy();
2270 } else if (DestTy
->isFloatingPointTy()) { // Casting to floating pt
2271 if (SrcTy
->isIntegerTy()) { // Casting from integral
2273 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
2275 } else if (SrcTy
->isVectorTy()) { // Casting from vector
2276 return DestBits
== SrcBits
;
2277 } else { // Casting from something else
2280 } else if (DestTy
->isVectorTy()) { // Casting to vector
2281 return DestBits
== SrcBits
;
2282 } else if (DestTy
->isPointerTy()) { // Casting to pointer
2283 if (SrcTy
->isPointerTy()) { // Casting from pointer
2285 } else if (SrcTy
->isIntegerTy()) { // Casting from integral
2287 } else { // Casting from something else
2290 } else if (DestTy
->isX86_MMXTy()) {
2291 if (SrcTy
->isVectorTy()) {
2292 return DestBits
== SrcBits
; // 64-bit vector to MMX
2296 } else { // Casting to something else
2301 // Provide a way to get a "cast" where the cast opcode is inferred from the
2302 // types and size of the operand. This, basically, is a parallel of the
2303 // logic in the castIsValid function below. This axiom should hold:
2304 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2305 // should not assert in castIsValid. In other words, this produces a "correct"
2306 // casting opcode for the arguments passed to it.
2307 // This routine must be kept in sync with isCastable.
2308 Instruction::CastOps
2309 CastInst::getCastOpcode(
2310 const Value
*Src
, bool SrcIsSigned
, const Type
*DestTy
, bool DestIsSigned
) {
2311 const Type
*SrcTy
= Src
->getType();
2313 assert(SrcTy
->isFirstClassType() && DestTy
->isFirstClassType() &&
2314 "Only first class types are castable!");
2316 if (SrcTy
== DestTy
)
2319 if (const VectorType
*SrcVecTy
= dyn_cast
<VectorType
>(SrcTy
))
2320 if (const VectorType
*DestVecTy
= dyn_cast
<VectorType
>(DestTy
))
2321 if (SrcVecTy
->getNumElements() == DestVecTy
->getNumElements()) {
2322 // An element by element cast. Find the appropriate opcode based on the
2324 SrcTy
= SrcVecTy
->getElementType();
2325 DestTy
= DestVecTy
->getElementType();
2328 // Get the bit sizes, we'll need these
2329 unsigned SrcBits
= SrcTy
->getPrimitiveSizeInBits(); // 0 for ptr
2330 unsigned DestBits
= DestTy
->getPrimitiveSizeInBits(); // 0 for ptr
2332 // Run through the possibilities ...
2333 if (DestTy
->isIntegerTy()) { // Casting to integral
2334 if (SrcTy
->isIntegerTy()) { // Casting from integral
2335 if (DestBits
< SrcBits
)
2336 return Trunc
; // int -> smaller int
2337 else if (DestBits
> SrcBits
) { // its an extension
2339 return SExt
; // signed -> SEXT
2341 return ZExt
; // unsigned -> ZEXT
2343 return BitCast
; // Same size, No-op cast
2345 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
2347 return FPToSI
; // FP -> sint
2349 return FPToUI
; // FP -> uint
2350 } else if (SrcTy
->isVectorTy()) {
2351 assert(DestBits
== SrcBits
&&
2352 "Casting vector to integer of different width");
2353 return BitCast
; // Same size, no-op cast
2355 assert(SrcTy
->isPointerTy() &&
2356 "Casting from a value that is not first-class type");
2357 return PtrToInt
; // ptr -> int
2359 } else if (DestTy
->isFloatingPointTy()) { // Casting to floating pt
2360 if (SrcTy
->isIntegerTy()) { // Casting from integral
2362 return SIToFP
; // sint -> FP
2364 return UIToFP
; // uint -> FP
2365 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
2366 if (DestBits
< SrcBits
) {
2367 return FPTrunc
; // FP -> smaller FP
2368 } else if (DestBits
> SrcBits
) {
2369 return FPExt
; // FP -> larger FP
2371 return BitCast
; // same size, no-op cast
2373 } else if (SrcTy
->isVectorTy()) {
2374 assert(DestBits
== SrcBits
&&
2375 "Casting vector to floating point of different width");
2376 return BitCast
; // same size, no-op cast
2378 llvm_unreachable("Casting pointer or non-first class to float");
2380 } else if (DestTy
->isVectorTy()) {
2381 assert(DestBits
== SrcBits
&&
2382 "Illegal cast to vector (wrong type or size)");
2384 } else if (DestTy
->isPointerTy()) {
2385 if (SrcTy
->isPointerTy()) {
2386 return BitCast
; // ptr -> ptr
2387 } else if (SrcTy
->isIntegerTy()) {
2388 return IntToPtr
; // int -> ptr
2390 assert(!"Casting pointer to other than pointer or int");
2392 } else if (DestTy
->isX86_MMXTy()) {
2393 if (SrcTy
->isVectorTy()) {
2394 assert(DestBits
== SrcBits
&& "Casting vector of wrong width to X86_MMX");
2395 return BitCast
; // 64-bit vector to MMX
2397 assert(!"Illegal cast to X86_MMX");
2400 assert(!"Casting to type that is not first-class");
2403 // If we fall through to here we probably hit an assertion cast above
2404 // and assertions are not turned on. Anything we return is an error, so
2405 // BitCast is as good a choice as any.
2409 //===----------------------------------------------------------------------===//
2410 // CastInst SubClass Constructors
2411 //===----------------------------------------------------------------------===//
2413 /// Check that the construction parameters for a CastInst are correct. This
2414 /// could be broken out into the separate constructors but it is useful to have
2415 /// it in one place and to eliminate the redundant code for getting the sizes
2416 /// of the types involved.
2418 CastInst::castIsValid(Instruction::CastOps op
, Value
*S
, const Type
*DstTy
) {
2420 // Check for type sanity on the arguments
2421 const Type
*SrcTy
= S
->getType();
2422 if (!SrcTy
->isFirstClassType() || !DstTy
->isFirstClassType() ||
2423 SrcTy
->isAggregateType() || DstTy
->isAggregateType())
2426 // Get the size of the types in bits, we'll need this later
2427 unsigned SrcBitSize
= SrcTy
->getScalarSizeInBits();
2428 unsigned DstBitSize
= DstTy
->getScalarSizeInBits();
2430 // If these are vector types, get the lengths of the vectors (using zero for
2431 // scalar types means that checking that vector lengths match also checks that
2432 // scalars are not being converted to vectors or vectors to scalars).
2433 unsigned SrcLength
= SrcTy
->isVectorTy() ?
2434 cast
<VectorType
>(SrcTy
)->getNumElements() : 0;
2435 unsigned DstLength
= DstTy
->isVectorTy() ?
2436 cast
<VectorType
>(DstTy
)->getNumElements() : 0;
2438 // Switch on the opcode provided
2440 default: return false; // This is an input error
2441 case Instruction::Trunc
:
2442 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isIntOrIntVectorTy() &&
2443 SrcLength
== DstLength
&& SrcBitSize
> DstBitSize
;
2444 case Instruction::ZExt
:
2445 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isIntOrIntVectorTy() &&
2446 SrcLength
== DstLength
&& SrcBitSize
< DstBitSize
;
2447 case Instruction::SExt
:
2448 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isIntOrIntVectorTy() &&
2449 SrcLength
== DstLength
&& SrcBitSize
< DstBitSize
;
2450 case Instruction::FPTrunc
:
2451 return SrcTy
->isFPOrFPVectorTy() && DstTy
->isFPOrFPVectorTy() &&
2452 SrcLength
== DstLength
&& SrcBitSize
> DstBitSize
;
2453 case Instruction::FPExt
:
2454 return SrcTy
->isFPOrFPVectorTy() && DstTy
->isFPOrFPVectorTy() &&
2455 SrcLength
== DstLength
&& SrcBitSize
< DstBitSize
;
2456 case Instruction::UIToFP
:
2457 case Instruction::SIToFP
:
2458 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isFPOrFPVectorTy() &&
2459 SrcLength
== DstLength
;
2460 case Instruction::FPToUI
:
2461 case Instruction::FPToSI
:
2462 return SrcTy
->isFPOrFPVectorTy() && DstTy
->isIntOrIntVectorTy() &&
2463 SrcLength
== DstLength
;
2464 case Instruction::PtrToInt
:
2465 return SrcTy
->isPointerTy() && DstTy
->isIntegerTy();
2466 case Instruction::IntToPtr
:
2467 return SrcTy
->isIntegerTy() && DstTy
->isPointerTy();
2468 case Instruction::BitCast
:
2469 // BitCast implies a no-op cast of type only. No bits change.
2470 // However, you can't cast pointers to anything but pointers.
2471 if (SrcTy
->isPointerTy() != DstTy
->isPointerTy())
2474 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2475 // these cases, the cast is okay if the source and destination bit widths
2477 return SrcTy
->getPrimitiveSizeInBits() == DstTy
->getPrimitiveSizeInBits();
2481 TruncInst::TruncInst(
2482 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2483 ) : CastInst(Ty
, Trunc
, S
, Name
, InsertBefore
) {
2484 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal Trunc");
2487 TruncInst::TruncInst(
2488 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2489 ) : CastInst(Ty
, Trunc
, S
, Name
, InsertAtEnd
) {
2490 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal Trunc");
2494 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2495 ) : CastInst(Ty
, ZExt
, S
, Name
, InsertBefore
) {
2496 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal ZExt");
2500 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2501 ) : CastInst(Ty
, ZExt
, S
, Name
, InsertAtEnd
) {
2502 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal ZExt");
2505 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2506 ) : CastInst(Ty
, SExt
, S
, Name
, InsertBefore
) {
2507 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SExt");
2511 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2512 ) : CastInst(Ty
, SExt
, S
, Name
, InsertAtEnd
) {
2513 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SExt");
2516 FPTruncInst::FPTruncInst(
2517 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2518 ) : CastInst(Ty
, FPTrunc
, S
, Name
, InsertBefore
) {
2519 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPTrunc");
2522 FPTruncInst::FPTruncInst(
2523 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2524 ) : CastInst(Ty
, FPTrunc
, S
, Name
, InsertAtEnd
) {
2525 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPTrunc");
2528 FPExtInst::FPExtInst(
2529 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2530 ) : CastInst(Ty
, FPExt
, S
, Name
, InsertBefore
) {
2531 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPExt");
2534 FPExtInst::FPExtInst(
2535 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2536 ) : CastInst(Ty
, FPExt
, S
, Name
, InsertAtEnd
) {
2537 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPExt");
2540 UIToFPInst::UIToFPInst(
2541 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2542 ) : CastInst(Ty
, UIToFP
, S
, Name
, InsertBefore
) {
2543 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal UIToFP");
2546 UIToFPInst::UIToFPInst(
2547 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2548 ) : CastInst(Ty
, UIToFP
, S
, Name
, InsertAtEnd
) {
2549 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal UIToFP");
2552 SIToFPInst::SIToFPInst(
2553 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2554 ) : CastInst(Ty
, SIToFP
, S
, Name
, InsertBefore
) {
2555 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SIToFP");
2558 SIToFPInst::SIToFPInst(
2559 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2560 ) : CastInst(Ty
, SIToFP
, S
, Name
, InsertAtEnd
) {
2561 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SIToFP");
2564 FPToUIInst::FPToUIInst(
2565 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2566 ) : CastInst(Ty
, FPToUI
, S
, Name
, InsertBefore
) {
2567 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToUI");
2570 FPToUIInst::FPToUIInst(
2571 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2572 ) : CastInst(Ty
, FPToUI
, S
, Name
, InsertAtEnd
) {
2573 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToUI");
2576 FPToSIInst::FPToSIInst(
2577 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2578 ) : CastInst(Ty
, FPToSI
, S
, Name
, InsertBefore
) {
2579 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToSI");
2582 FPToSIInst::FPToSIInst(
2583 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2584 ) : CastInst(Ty
, FPToSI
, S
, Name
, InsertAtEnd
) {
2585 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToSI");
2588 PtrToIntInst::PtrToIntInst(
2589 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2590 ) : CastInst(Ty
, PtrToInt
, S
, Name
, InsertBefore
) {
2591 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal PtrToInt");
2594 PtrToIntInst::PtrToIntInst(
2595 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2596 ) : CastInst(Ty
, PtrToInt
, S
, Name
, InsertAtEnd
) {
2597 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal PtrToInt");
2600 IntToPtrInst::IntToPtrInst(
2601 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2602 ) : CastInst(Ty
, IntToPtr
, S
, Name
, InsertBefore
) {
2603 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal IntToPtr");
2606 IntToPtrInst::IntToPtrInst(
2607 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2608 ) : CastInst(Ty
, IntToPtr
, S
, Name
, InsertAtEnd
) {
2609 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal IntToPtr");
2612 BitCastInst::BitCastInst(
2613 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2614 ) : CastInst(Ty
, BitCast
, S
, Name
, InsertBefore
) {
2615 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal BitCast");
2618 BitCastInst::BitCastInst(
2619 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2620 ) : CastInst(Ty
, BitCast
, S
, Name
, InsertAtEnd
) {
2621 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal BitCast");
2624 //===----------------------------------------------------------------------===//
2626 //===----------------------------------------------------------------------===//
2628 void CmpInst::Anchor() const {}
2630 CmpInst::CmpInst(const Type
*ty
, OtherOps op
, unsigned short predicate
,
2631 Value
*LHS
, Value
*RHS
, const Twine
&Name
,
2632 Instruction
*InsertBefore
)
2633 : Instruction(ty
, op
,
2634 OperandTraits
<CmpInst
>::op_begin(this),
2635 OperandTraits
<CmpInst
>::operands(this),
2639 setPredicate((Predicate
)predicate
);
2643 CmpInst::CmpInst(const Type
*ty
, OtherOps op
, unsigned short predicate
,
2644 Value
*LHS
, Value
*RHS
, const Twine
&Name
,
2645 BasicBlock
*InsertAtEnd
)
2646 : Instruction(ty
, op
,
2647 OperandTraits
<CmpInst
>::op_begin(this),
2648 OperandTraits
<CmpInst
>::operands(this),
2652 setPredicate((Predicate
)predicate
);
2657 CmpInst::Create(OtherOps Op
, unsigned short predicate
,
2658 Value
*S1
, Value
*S2
,
2659 const Twine
&Name
, Instruction
*InsertBefore
) {
2660 if (Op
== Instruction::ICmp
) {
2662 return new ICmpInst(InsertBefore
, CmpInst::Predicate(predicate
),
2665 return new ICmpInst(CmpInst::Predicate(predicate
),
2670 return new FCmpInst(InsertBefore
, CmpInst::Predicate(predicate
),
2673 return new FCmpInst(CmpInst::Predicate(predicate
),
2678 CmpInst::Create(OtherOps Op
, unsigned short predicate
, Value
*S1
, Value
*S2
,
2679 const Twine
&Name
, BasicBlock
*InsertAtEnd
) {
2680 if (Op
== Instruction::ICmp
) {
2681 return new ICmpInst(*InsertAtEnd
, CmpInst::Predicate(predicate
),
2684 return new FCmpInst(*InsertAtEnd
, CmpInst::Predicate(predicate
),
2688 void CmpInst::swapOperands() {
2689 if (ICmpInst
*IC
= dyn_cast
<ICmpInst
>(this))
2692 cast
<FCmpInst
>(this)->swapOperands();
2695 bool CmpInst::isCommutative() const {
2696 if (const ICmpInst
*IC
= dyn_cast
<ICmpInst
>(this))
2697 return IC
->isCommutative();
2698 return cast
<FCmpInst
>(this)->isCommutative();
2701 bool CmpInst::isEquality() const {
2702 if (const ICmpInst
*IC
= dyn_cast
<ICmpInst
>(this))
2703 return IC
->isEquality();
2704 return cast
<FCmpInst
>(this)->isEquality();
2708 CmpInst::Predicate
CmpInst::getInversePredicate(Predicate pred
) {
2710 default: assert(!"Unknown cmp predicate!");
2711 case ICMP_EQ
: return ICMP_NE
;
2712 case ICMP_NE
: return ICMP_EQ
;
2713 case ICMP_UGT
: return ICMP_ULE
;
2714 case ICMP_ULT
: return ICMP_UGE
;
2715 case ICMP_UGE
: return ICMP_ULT
;
2716 case ICMP_ULE
: return ICMP_UGT
;
2717 case ICMP_SGT
: return ICMP_SLE
;
2718 case ICMP_SLT
: return ICMP_SGE
;
2719 case ICMP_SGE
: return ICMP_SLT
;
2720 case ICMP_SLE
: return ICMP_SGT
;
2722 case FCMP_OEQ
: return FCMP_UNE
;
2723 case FCMP_ONE
: return FCMP_UEQ
;
2724 case FCMP_OGT
: return FCMP_ULE
;
2725 case FCMP_OLT
: return FCMP_UGE
;
2726 case FCMP_OGE
: return FCMP_ULT
;
2727 case FCMP_OLE
: return FCMP_UGT
;
2728 case FCMP_UEQ
: return FCMP_ONE
;
2729 case FCMP_UNE
: return FCMP_OEQ
;
2730 case FCMP_UGT
: return FCMP_OLE
;
2731 case FCMP_ULT
: return FCMP_OGE
;
2732 case FCMP_UGE
: return FCMP_OLT
;
2733 case FCMP_ULE
: return FCMP_OGT
;
2734 case FCMP_ORD
: return FCMP_UNO
;
2735 case FCMP_UNO
: return FCMP_ORD
;
2736 case FCMP_TRUE
: return FCMP_FALSE
;
2737 case FCMP_FALSE
: return FCMP_TRUE
;
2741 ICmpInst::Predicate
ICmpInst::getSignedPredicate(Predicate pred
) {
2743 default: assert(! "Unknown icmp predicate!");
2744 case ICMP_EQ
: case ICMP_NE
:
2745 case ICMP_SGT
: case ICMP_SLT
: case ICMP_SGE
: case ICMP_SLE
:
2747 case ICMP_UGT
: return ICMP_SGT
;
2748 case ICMP_ULT
: return ICMP_SLT
;
2749 case ICMP_UGE
: return ICMP_SGE
;
2750 case ICMP_ULE
: return ICMP_SLE
;
2754 ICmpInst::Predicate
ICmpInst::getUnsignedPredicate(Predicate pred
) {
2756 default: assert(! "Unknown icmp predicate!");
2757 case ICMP_EQ
: case ICMP_NE
:
2758 case ICMP_UGT
: case ICMP_ULT
: case ICMP_UGE
: case ICMP_ULE
:
2760 case ICMP_SGT
: return ICMP_UGT
;
2761 case ICMP_SLT
: return ICMP_ULT
;
2762 case ICMP_SGE
: return ICMP_UGE
;
2763 case ICMP_SLE
: return ICMP_ULE
;
2767 /// Initialize a set of values that all satisfy the condition with C.
2770 ICmpInst::makeConstantRange(Predicate pred
, const APInt
&C
) {
2773 uint32_t BitWidth
= C
.getBitWidth();
2775 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
2776 case ICmpInst::ICMP_EQ
: Upper
++; break;
2777 case ICmpInst::ICMP_NE
: Lower
++; break;
2778 case ICmpInst::ICMP_ULT
:
2779 Lower
= APInt::getMinValue(BitWidth
);
2780 // Check for an empty-set condition.
2782 return ConstantRange(BitWidth
, /*isFullSet=*/false);
2784 case ICmpInst::ICMP_SLT
:
2785 Lower
= APInt::getSignedMinValue(BitWidth
);
2786 // Check for an empty-set condition.
2788 return ConstantRange(BitWidth
, /*isFullSet=*/false);
2790 case ICmpInst::ICMP_UGT
:
2791 Lower
++; Upper
= APInt::getMinValue(BitWidth
); // Min = Next(Max)
2792 // Check for an empty-set condition.
2794 return ConstantRange(BitWidth
, /*isFullSet=*/false);
2796 case ICmpInst::ICMP_SGT
:
2797 Lower
++; Upper
= APInt::getSignedMinValue(BitWidth
); // Min = Next(Max)
2798 // Check for an empty-set condition.
2800 return ConstantRange(BitWidth
, /*isFullSet=*/false);
2802 case ICmpInst::ICMP_ULE
:
2803 Lower
= APInt::getMinValue(BitWidth
); Upper
++;
2804 // Check for a full-set condition.
2806 return ConstantRange(BitWidth
, /*isFullSet=*/true);
2808 case ICmpInst::ICMP_SLE
:
2809 Lower
= APInt::getSignedMinValue(BitWidth
); Upper
++;
2810 // Check for a full-set condition.
2812 return ConstantRange(BitWidth
, /*isFullSet=*/true);
2814 case ICmpInst::ICMP_UGE
:
2815 Upper
= APInt::getMinValue(BitWidth
); // Min = Next(Max)
2816 // Check for a full-set condition.
2818 return ConstantRange(BitWidth
, /*isFullSet=*/true);
2820 case ICmpInst::ICMP_SGE
:
2821 Upper
= APInt::getSignedMinValue(BitWidth
); // Min = Next(Max)
2822 // Check for a full-set condition.
2824 return ConstantRange(BitWidth
, /*isFullSet=*/true);
2827 return ConstantRange(Lower
, Upper
);
2830 CmpInst::Predicate
CmpInst::getSwappedPredicate(Predicate pred
) {
2832 default: assert(!"Unknown cmp predicate!");
2833 case ICMP_EQ
: case ICMP_NE
:
2835 case ICMP_SGT
: return ICMP_SLT
;
2836 case ICMP_SLT
: return ICMP_SGT
;
2837 case ICMP_SGE
: return ICMP_SLE
;
2838 case ICMP_SLE
: return ICMP_SGE
;
2839 case ICMP_UGT
: return ICMP_ULT
;
2840 case ICMP_ULT
: return ICMP_UGT
;
2841 case ICMP_UGE
: return ICMP_ULE
;
2842 case ICMP_ULE
: return ICMP_UGE
;
2844 case FCMP_FALSE
: case FCMP_TRUE
:
2845 case FCMP_OEQ
: case FCMP_ONE
:
2846 case FCMP_UEQ
: case FCMP_UNE
:
2847 case FCMP_ORD
: case FCMP_UNO
:
2849 case FCMP_OGT
: return FCMP_OLT
;
2850 case FCMP_OLT
: return FCMP_OGT
;
2851 case FCMP_OGE
: return FCMP_OLE
;
2852 case FCMP_OLE
: return FCMP_OGE
;
2853 case FCMP_UGT
: return FCMP_ULT
;
2854 case FCMP_ULT
: return FCMP_UGT
;
2855 case FCMP_UGE
: return FCMP_ULE
;
2856 case FCMP_ULE
: return FCMP_UGE
;
2860 bool CmpInst::isUnsigned(unsigned short predicate
) {
2861 switch (predicate
) {
2862 default: return false;
2863 case ICmpInst::ICMP_ULT
: case ICmpInst::ICMP_ULE
: case ICmpInst::ICMP_UGT
:
2864 case ICmpInst::ICMP_UGE
: return true;
2868 bool CmpInst::isSigned(unsigned short predicate
) {
2869 switch (predicate
) {
2870 default: return false;
2871 case ICmpInst::ICMP_SLT
: case ICmpInst::ICMP_SLE
: case ICmpInst::ICMP_SGT
:
2872 case ICmpInst::ICMP_SGE
: return true;
2876 bool CmpInst::isOrdered(unsigned short predicate
) {
2877 switch (predicate
) {
2878 default: return false;
2879 case FCmpInst::FCMP_OEQ
: case FCmpInst::FCMP_ONE
: case FCmpInst::FCMP_OGT
:
2880 case FCmpInst::FCMP_OLT
: case FCmpInst::FCMP_OGE
: case FCmpInst::FCMP_OLE
:
2881 case FCmpInst::FCMP_ORD
: return true;
2885 bool CmpInst::isUnordered(unsigned short predicate
) {
2886 switch (predicate
) {
2887 default: return false;
2888 case FCmpInst::FCMP_UEQ
: case FCmpInst::FCMP_UNE
: case FCmpInst::FCMP_UGT
:
2889 case FCmpInst::FCMP_ULT
: case FCmpInst::FCMP_UGE
: case FCmpInst::FCMP_ULE
:
2890 case FCmpInst::FCMP_UNO
: return true;
2894 bool CmpInst::isTrueWhenEqual(unsigned short predicate
) {
2896 default: return false;
2897 case ICMP_EQ
: case ICMP_UGE
: case ICMP_ULE
: case ICMP_SGE
: case ICMP_SLE
:
2898 case FCMP_TRUE
: case FCMP_UEQ
: case FCMP_UGE
: case FCMP_ULE
: return true;
2902 bool CmpInst::isFalseWhenEqual(unsigned short predicate
) {
2904 case ICMP_NE
: case ICMP_UGT
: case ICMP_ULT
: case ICMP_SGT
: case ICMP_SLT
:
2905 case FCMP_FALSE
: case FCMP_ONE
: case FCMP_OGT
: case FCMP_OLT
: return true;
2906 default: return false;
2911 //===----------------------------------------------------------------------===//
2912 // SwitchInst Implementation
2913 //===----------------------------------------------------------------------===//
2915 void SwitchInst::init(Value
*Value
, BasicBlock
*Default
, unsigned NumReserved
) {
2916 assert(Value
&& Default
&& NumReserved
);
2917 ReservedSpace
= NumReserved
;
2919 OperandList
= allocHungoffUses(ReservedSpace
);
2921 OperandList
[0] = Value
;
2922 OperandList
[1] = Default
;
2925 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2926 /// switch on and a default destination. The number of additional cases can
2927 /// be specified here to make memory allocation more efficient. This
2928 /// constructor can also autoinsert before another instruction.
2929 SwitchInst::SwitchInst(Value
*Value
, BasicBlock
*Default
, unsigned NumCases
,
2930 Instruction
*InsertBefore
)
2931 : TerminatorInst(Type::getVoidTy(Value
->getContext()), Instruction::Switch
,
2932 0, 0, InsertBefore
) {
2933 init(Value
, Default
, 2+NumCases
*2);
2936 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2937 /// switch on and a default destination. The number of additional cases can
2938 /// be specified here to make memory allocation more efficient. This
2939 /// constructor also autoinserts at the end of the specified BasicBlock.
2940 SwitchInst::SwitchInst(Value
*Value
, BasicBlock
*Default
, unsigned NumCases
,
2941 BasicBlock
*InsertAtEnd
)
2942 : TerminatorInst(Type::getVoidTy(Value
->getContext()), Instruction::Switch
,
2943 0, 0, InsertAtEnd
) {
2944 init(Value
, Default
, 2+NumCases
*2);
2947 SwitchInst::SwitchInst(const SwitchInst
&SI
)
2948 : TerminatorInst(SI
.getType(), Instruction::Switch
, 0, 0) {
2949 init(SI
.getCondition(), SI
.getDefaultDest(), SI
.getNumOperands());
2950 NumOperands
= SI
.getNumOperands();
2951 Use
*OL
= OperandList
, *InOL
= SI
.OperandList
;
2952 for (unsigned i
= 2, E
= SI
.getNumOperands(); i
!= E
; i
+= 2) {
2954 OL
[i
+1] = InOL
[i
+1];
2956 SubclassOptionalData
= SI
.SubclassOptionalData
;
2959 SwitchInst::~SwitchInst() {
2964 /// addCase - Add an entry to the switch instruction...
2966 void SwitchInst::addCase(ConstantInt
*OnVal
, BasicBlock
*Dest
) {
2967 unsigned OpNo
= NumOperands
;
2968 if (OpNo
+2 > ReservedSpace
)
2969 growOperands(); // Get more space!
2970 // Initialize some new operands.
2971 assert(OpNo
+1 < ReservedSpace
&& "Growing didn't work!");
2972 NumOperands
= OpNo
+2;
2973 OperandList
[OpNo
] = OnVal
;
2974 OperandList
[OpNo
+1] = Dest
;
2977 /// removeCase - This method removes the specified successor from the switch
2978 /// instruction. Note that this cannot be used to remove the default
2979 /// destination (successor #0).
2981 void SwitchInst::removeCase(unsigned idx
) {
2982 assert(idx
!= 0 && "Cannot remove the default case!");
2983 assert(idx
*2 < getNumOperands() && "Successor index out of range!!!");
2985 unsigned NumOps
= getNumOperands();
2986 Use
*OL
= OperandList
;
2988 // Overwrite this case with the end of the list.
2989 if ((idx
+ 1) * 2 != NumOps
) {
2990 OL
[idx
* 2] = OL
[NumOps
- 2];
2991 OL
[idx
* 2 + 1] = OL
[NumOps
- 1];
2994 // Nuke the last value.
2995 OL
[NumOps
-2].set(0);
2996 OL
[NumOps
-2+1].set(0);
2997 NumOperands
= NumOps
-2;
3000 /// growOperands - grow operands - This grows the operand list in response
3001 /// to a push_back style of operation. This grows the number of ops by 3 times.
3003 void SwitchInst::growOperands() {
3004 unsigned e
= getNumOperands();
3005 unsigned NumOps
= e
*3;
3007 ReservedSpace
= NumOps
;
3008 Use
*NewOps
= allocHungoffUses(NumOps
);
3009 Use
*OldOps
= OperandList
;
3010 for (unsigned i
= 0; i
!= e
; ++i
) {
3011 NewOps
[i
] = OldOps
[i
];
3013 OperandList
= NewOps
;
3014 Use::zap(OldOps
, OldOps
+ e
, true);
3018 BasicBlock
*SwitchInst::getSuccessorV(unsigned idx
) const {
3019 return getSuccessor(idx
);
3021 unsigned SwitchInst::getNumSuccessorsV() const {
3022 return getNumSuccessors();
3024 void SwitchInst::setSuccessorV(unsigned idx
, BasicBlock
*B
) {
3025 setSuccessor(idx
, B
);
3028 //===----------------------------------------------------------------------===//
3029 // IndirectBrInst Implementation
3030 //===----------------------------------------------------------------------===//
3032 void IndirectBrInst::init(Value
*Address
, unsigned NumDests
) {
3033 assert(Address
&& Address
->getType()->isPointerTy() &&
3034 "Address of indirectbr must be a pointer");
3035 ReservedSpace
= 1+NumDests
;
3037 OperandList
= allocHungoffUses(ReservedSpace
);
3039 OperandList
[0] = Address
;
3043 /// growOperands - grow operands - This grows the operand list in response
3044 /// to a push_back style of operation. This grows the number of ops by 2 times.
3046 void IndirectBrInst::growOperands() {
3047 unsigned e
= getNumOperands();
3048 unsigned NumOps
= e
*2;
3050 ReservedSpace
= NumOps
;
3051 Use
*NewOps
= allocHungoffUses(NumOps
);
3052 Use
*OldOps
= OperandList
;
3053 for (unsigned i
= 0; i
!= e
; ++i
)
3054 NewOps
[i
] = OldOps
[i
];
3055 OperandList
= NewOps
;
3056 Use::zap(OldOps
, OldOps
+ e
, true);
3059 IndirectBrInst::IndirectBrInst(Value
*Address
, unsigned NumCases
,
3060 Instruction
*InsertBefore
)
3061 : TerminatorInst(Type::getVoidTy(Address
->getContext()),Instruction::IndirectBr
,
3062 0, 0, InsertBefore
) {
3063 init(Address
, NumCases
);
3066 IndirectBrInst::IndirectBrInst(Value
*Address
, unsigned NumCases
,
3067 BasicBlock
*InsertAtEnd
)
3068 : TerminatorInst(Type::getVoidTy(Address
->getContext()),Instruction::IndirectBr
,
3069 0, 0, InsertAtEnd
) {
3070 init(Address
, NumCases
);
3073 IndirectBrInst::IndirectBrInst(const IndirectBrInst
&IBI
)
3074 : TerminatorInst(Type::getVoidTy(IBI
.getContext()), Instruction::IndirectBr
,
3075 allocHungoffUses(IBI
.getNumOperands()),
3076 IBI
.getNumOperands()) {
3077 Use
*OL
= OperandList
, *InOL
= IBI
.OperandList
;
3078 for (unsigned i
= 0, E
= IBI
.getNumOperands(); i
!= E
; ++i
)
3080 SubclassOptionalData
= IBI
.SubclassOptionalData
;
3083 IndirectBrInst::~IndirectBrInst() {
3087 /// addDestination - Add a destination.
3089 void IndirectBrInst::addDestination(BasicBlock
*DestBB
) {
3090 unsigned OpNo
= NumOperands
;
3091 if (OpNo
+1 > ReservedSpace
)
3092 growOperands(); // Get more space!
3093 // Initialize some new operands.
3094 assert(OpNo
< ReservedSpace
&& "Growing didn't work!");
3095 NumOperands
= OpNo
+1;
3096 OperandList
[OpNo
] = DestBB
;
3099 /// removeDestination - This method removes the specified successor from the
3100 /// indirectbr instruction.
3101 void IndirectBrInst::removeDestination(unsigned idx
) {
3102 assert(idx
< getNumOperands()-1 && "Successor index out of range!");
3104 unsigned NumOps
= getNumOperands();
3105 Use
*OL
= OperandList
;
3107 // Replace this value with the last one.
3108 OL
[idx
+1] = OL
[NumOps
-1];
3110 // Nuke the last value.
3111 OL
[NumOps
-1].set(0);
3112 NumOperands
= NumOps
-1;
3115 BasicBlock
*IndirectBrInst::getSuccessorV(unsigned idx
) const {
3116 return getSuccessor(idx
);
3118 unsigned IndirectBrInst::getNumSuccessorsV() const {
3119 return getNumSuccessors();
3121 void IndirectBrInst::setSuccessorV(unsigned idx
, BasicBlock
*B
) {
3122 setSuccessor(idx
, B
);
3125 //===----------------------------------------------------------------------===//
3126 // clone_impl() implementations
3127 //===----------------------------------------------------------------------===//
3129 // Define these methods here so vtables don't get emitted into every translation
3130 // unit that uses these classes.
3132 GetElementPtrInst
*GetElementPtrInst::clone_impl() const {
3133 return new (getNumOperands()) GetElementPtrInst(*this);
3136 BinaryOperator
*BinaryOperator::clone_impl() const {
3137 return Create(getOpcode(), Op
<0>(), Op
<1>());
3140 FCmpInst
* FCmpInst::clone_impl() const {
3141 return new FCmpInst(getPredicate(), Op
<0>(), Op
<1>());
3144 ICmpInst
* ICmpInst::clone_impl() const {
3145 return new ICmpInst(getPredicate(), Op
<0>(), Op
<1>());
3148 ExtractValueInst
*ExtractValueInst::clone_impl() const {
3149 return new ExtractValueInst(*this);
3152 InsertValueInst
*InsertValueInst::clone_impl() const {
3153 return new InsertValueInst(*this);
3156 AllocaInst
*AllocaInst::clone_impl() const {
3157 return new AllocaInst(getAllocatedType(),
3158 (Value
*)getOperand(0),
3162 LoadInst
*LoadInst::clone_impl() const {
3163 return new LoadInst(getOperand(0),
3164 Twine(), isVolatile(),
3168 StoreInst
*StoreInst::clone_impl() const {
3169 return new StoreInst(getOperand(0), getOperand(1),
3170 isVolatile(), getAlignment());
3173 TruncInst
*TruncInst::clone_impl() const {
3174 return new TruncInst(getOperand(0), getType());
3177 ZExtInst
*ZExtInst::clone_impl() const {
3178 return new ZExtInst(getOperand(0), getType());
3181 SExtInst
*SExtInst::clone_impl() const {
3182 return new SExtInst(getOperand(0), getType());
3185 FPTruncInst
*FPTruncInst::clone_impl() const {
3186 return new FPTruncInst(getOperand(0), getType());
3189 FPExtInst
*FPExtInst::clone_impl() const {
3190 return new FPExtInst(getOperand(0), getType());
3193 UIToFPInst
*UIToFPInst::clone_impl() const {
3194 return new UIToFPInst(getOperand(0), getType());
3197 SIToFPInst
*SIToFPInst::clone_impl() const {
3198 return new SIToFPInst(getOperand(0), getType());
3201 FPToUIInst
*FPToUIInst::clone_impl() const {
3202 return new FPToUIInst(getOperand(0), getType());
3205 FPToSIInst
*FPToSIInst::clone_impl() const {
3206 return new FPToSIInst(getOperand(0), getType());
3209 PtrToIntInst
*PtrToIntInst::clone_impl() const {
3210 return new PtrToIntInst(getOperand(0), getType());
3213 IntToPtrInst
*IntToPtrInst::clone_impl() const {
3214 return new IntToPtrInst(getOperand(0), getType());
3217 BitCastInst
*BitCastInst::clone_impl() const {
3218 return new BitCastInst(getOperand(0), getType());
3221 CallInst
*CallInst::clone_impl() const {
3222 return new(getNumOperands()) CallInst(*this);
3225 SelectInst
*SelectInst::clone_impl() const {
3226 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
3229 VAArgInst
*VAArgInst::clone_impl() const {
3230 return new VAArgInst(getOperand(0), getType());
3233 ExtractElementInst
*ExtractElementInst::clone_impl() const {
3234 return ExtractElementInst::Create(getOperand(0), getOperand(1));
3237 InsertElementInst
*InsertElementInst::clone_impl() const {
3238 return InsertElementInst::Create(getOperand(0),
3243 ShuffleVectorInst
*ShuffleVectorInst::clone_impl() const {
3244 return new ShuffleVectorInst(getOperand(0),
3249 PHINode
*PHINode::clone_impl() const {
3250 return new PHINode(*this);
3253 ReturnInst
*ReturnInst::clone_impl() const {
3254 return new(getNumOperands()) ReturnInst(*this);
3257 BranchInst
*BranchInst::clone_impl() const {
3258 return new(getNumOperands()) BranchInst(*this);
3261 SwitchInst
*SwitchInst::clone_impl() const {
3262 return new SwitchInst(*this);
3265 IndirectBrInst
*IndirectBrInst::clone_impl() const {
3266 return new IndirectBrInst(*this);
3270 InvokeInst
*InvokeInst::clone_impl() const {
3271 return new(getNumOperands()) InvokeInst(*this);
3274 UnwindInst
*UnwindInst::clone_impl() const {
3275 LLVMContext
&Context
= getContext();
3276 return new UnwindInst(Context
);
3279 UnreachableInst
*UnreachableInst::clone_impl() const {
3280 LLVMContext
&Context
= getContext();
3281 return new UnreachableInst(Context
);