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 Use
*OL
= OperandList
;
91 for (unsigned i
= 0, e
= PN
.getNumOperands(); i
!= e
; i
+=2) {
92 OL
[i
] = PN
.getOperand(i
);
93 OL
[i
+1] = PN
.getOperand(i
+1);
95 SubclassOptionalData
= PN
.SubclassOptionalData
;
102 // removeIncomingValue - Remove an incoming value. This is useful if a
103 // predecessor basic block is deleted.
104 Value
*PHINode::removeIncomingValue(unsigned Idx
, bool DeletePHIIfEmpty
) {
105 unsigned NumOps
= getNumOperands();
106 Use
*OL
= OperandList
;
107 assert(Idx
*2 < NumOps
&& "BB not in PHI node!");
108 Value
*Removed
= OL
[Idx
*2];
110 // Move everything after this operand down.
112 // FIXME: we could just swap with the end of the list, then erase. However,
113 // client might not expect this to happen. The code as it is thrashes the
114 // use/def lists, which is kinda lame.
115 for (unsigned i
= (Idx
+1)*2; i
!= NumOps
; i
+= 2) {
120 // Nuke the last value.
122 OL
[NumOps
-2+1].set(0);
123 NumOperands
= NumOps
-2;
125 // If the PHI node is dead, because it has zero entries, nuke it now.
126 if (NumOps
== 2 && DeletePHIIfEmpty
) {
127 // If anyone is using this PHI, make them use a dummy value instead...
128 replaceAllUsesWith(UndefValue::get(getType()));
134 /// growOperands - grow operands - This grows the operand list in response
135 /// to a push_back style of operation. This grows the number of ops by 1.5
138 void PHINode::growOperands() {
139 unsigned e
= getNumOperands();
140 // Multiply by 1.5 and round down so the result is still even.
141 unsigned NumOps
= e
+ e
/ 4 * 2;
142 if (NumOps
< 4) NumOps
= 4; // 4 op PHI nodes are VERY common.
144 ReservedSpace
= NumOps
;
145 Use
*OldOps
= OperandList
;
146 Use
*NewOps
= allocHungoffUses(NumOps
);
147 std::copy(OldOps
, OldOps
+ e
, NewOps
);
148 OperandList
= NewOps
;
149 Use::zap(OldOps
, OldOps
+ e
, true);
152 /// hasConstantValue - If the specified PHI node always merges together the same
153 /// value, return the value, otherwise return null.
154 Value
*PHINode::hasConstantValue() const {
155 // Exploit the fact that phi nodes always have at least one entry.
156 Value
*ConstantValue
= getIncomingValue(0);
157 for (unsigned i
= 1, e
= getNumIncomingValues(); i
!= e
; ++i
)
158 if (getIncomingValue(i
) != ConstantValue
)
159 return 0; // Incoming values not all the same.
160 return ConstantValue
;
164 //===----------------------------------------------------------------------===//
165 // CallInst Implementation
166 //===----------------------------------------------------------------------===//
168 CallInst::~CallInst() {
171 void CallInst::init(Value
*Func
, Value
* const *Params
, unsigned NumParams
) {
172 assert(NumOperands
== NumParams
+1 && "NumOperands not set up?");
175 const FunctionType
*FTy
=
176 cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())->getElementType());
177 (void)FTy
; // silence warning.
179 assert((NumParams
== FTy
->getNumParams() ||
180 (FTy
->isVarArg() && NumParams
> FTy
->getNumParams())) &&
181 "Calling a function with bad signature!");
182 for (unsigned i
= 0; i
!= NumParams
; ++i
) {
183 assert((i
>= FTy
->getNumParams() ||
184 FTy
->getParamType(i
) == Params
[i
]->getType()) &&
185 "Calling a function with a bad signature!");
186 OperandList
[i
] = Params
[i
];
190 void CallInst::init(Value
*Func
, Value
*Actual1
, Value
*Actual2
) {
191 assert(NumOperands
== 3 && "NumOperands not set up?");
196 const FunctionType
*FTy
=
197 cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())->getElementType());
198 (void)FTy
; // silence warning.
200 assert((FTy
->getNumParams() == 2 ||
201 (FTy
->isVarArg() && FTy
->getNumParams() < 2)) &&
202 "Calling a function with bad signature");
203 assert((0 >= FTy
->getNumParams() ||
204 FTy
->getParamType(0) == Actual1
->getType()) &&
205 "Calling a function with a bad signature!");
206 assert((1 >= FTy
->getNumParams() ||
207 FTy
->getParamType(1) == Actual2
->getType()) &&
208 "Calling a function with a bad signature!");
211 void CallInst::init(Value
*Func
, Value
*Actual
) {
212 assert(NumOperands
== 2 && "NumOperands not set up?");
216 const FunctionType
*FTy
=
217 cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())->getElementType());
218 (void)FTy
; // silence warning.
220 assert((FTy
->getNumParams() == 1 ||
221 (FTy
->isVarArg() && FTy
->getNumParams() == 0)) &&
222 "Calling a function with bad signature");
223 assert((0 == FTy
->getNumParams() ||
224 FTy
->getParamType(0) == Actual
->getType()) &&
225 "Calling a function with a bad signature!");
228 void CallInst::init(Value
*Func
) {
229 assert(NumOperands
== 1 && "NumOperands not set up?");
232 const FunctionType
*FTy
=
233 cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())->getElementType());
234 (void)FTy
; // silence warning.
236 assert(FTy
->getNumParams() == 0 && "Calling a function with bad signature");
239 CallInst::CallInst(Value
*Func
, Value
* Actual
, const Twine
&Name
,
240 Instruction
*InsertBefore
)
241 : Instruction(cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())
242 ->getElementType())->getReturnType(),
244 OperandTraits
<CallInst
>::op_end(this) - 2,
250 CallInst::CallInst(Value
*Func
, Value
* Actual
, const Twine
&Name
,
251 BasicBlock
*InsertAtEnd
)
252 : Instruction(cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())
253 ->getElementType())->getReturnType(),
255 OperandTraits
<CallInst
>::op_end(this) - 2,
260 CallInst::CallInst(Value
*Func
, const Twine
&Name
,
261 Instruction
*InsertBefore
)
262 : Instruction(cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())
263 ->getElementType())->getReturnType(),
265 OperandTraits
<CallInst
>::op_end(this) - 1,
271 CallInst::CallInst(Value
*Func
, const Twine
&Name
,
272 BasicBlock
*InsertAtEnd
)
273 : Instruction(cast
<FunctionType
>(cast
<PointerType
>(Func
->getType())
274 ->getElementType())->getReturnType(),
276 OperandTraits
<CallInst
>::op_end(this) - 1,
282 CallInst::CallInst(const CallInst
&CI
)
283 : Instruction(CI
.getType(), Instruction::Call
,
284 OperandTraits
<CallInst
>::op_end(this) - CI
.getNumOperands(),
285 CI
.getNumOperands()) {
286 setAttributes(CI
.getAttributes());
287 setTailCall(CI
.isTailCall());
288 setCallingConv(CI
.getCallingConv());
290 Use
*OL
= OperandList
;
291 Use
*InOL
= CI
.OperandList
;
292 for (unsigned i
= 0, e
= CI
.getNumOperands(); i
!= e
; ++i
)
294 SubclassOptionalData
= CI
.SubclassOptionalData
;
297 void CallInst::addAttribute(unsigned i
, Attributes attr
) {
298 AttrListPtr PAL
= getAttributes();
299 PAL
= PAL
.addAttr(i
, attr
);
303 void CallInst::removeAttribute(unsigned i
, Attributes attr
) {
304 AttrListPtr PAL
= getAttributes();
305 PAL
= PAL
.removeAttr(i
, attr
);
309 bool CallInst::paramHasAttr(unsigned i
, Attributes attr
) const {
310 if (AttributeList
.paramHasAttr(i
, attr
))
312 if (const Function
*F
= getCalledFunction())
313 return F
->paramHasAttr(i
, attr
);
317 /// IsConstantOne - Return true only if val is constant int 1
318 static bool IsConstantOne(Value
*val
) {
319 assert(val
&& "IsConstantOne does not work with NULL val");
320 return isa
<ConstantInt
>(val
) && cast
<ConstantInt
>(val
)->isOne();
323 static Instruction
*createMalloc(Instruction
*InsertBefore
,
324 BasicBlock
*InsertAtEnd
, const Type
*IntPtrTy
,
325 const Type
*AllocTy
, Value
*AllocSize
,
326 Value
*ArraySize
, Function
*MallocF
,
328 assert(((!InsertBefore
&& InsertAtEnd
) || (InsertBefore
&& !InsertAtEnd
)) &&
329 "createMalloc needs either InsertBefore or InsertAtEnd");
331 // malloc(type) becomes:
332 // bitcast (i8* malloc(typeSize)) to type*
333 // malloc(type, arraySize) becomes:
334 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
336 ArraySize
= ConstantInt::get(IntPtrTy
, 1);
337 else if (ArraySize
->getType() != IntPtrTy
) {
339 ArraySize
= CastInst::CreateIntegerCast(ArraySize
, IntPtrTy
, false,
342 ArraySize
= CastInst::CreateIntegerCast(ArraySize
, IntPtrTy
, false,
346 if (!IsConstantOne(ArraySize
)) {
347 if (IsConstantOne(AllocSize
)) {
348 AllocSize
= ArraySize
; // Operand * 1 = Operand
349 } else if (Constant
*CO
= dyn_cast
<Constant
>(ArraySize
)) {
350 Constant
*Scale
= ConstantExpr::getIntegerCast(CO
, IntPtrTy
,
352 // Malloc arg is constant product of type size and array size
353 AllocSize
= ConstantExpr::getMul(Scale
, cast
<Constant
>(AllocSize
));
355 // Multiply type size by the array size...
357 AllocSize
= BinaryOperator::CreateMul(ArraySize
, AllocSize
,
358 "mallocsize", InsertBefore
);
360 AllocSize
= BinaryOperator::CreateMul(ArraySize
, AllocSize
,
361 "mallocsize", InsertAtEnd
);
365 assert(AllocSize
->getType() == IntPtrTy
&& "malloc arg is wrong size");
366 // Create the call to Malloc.
367 BasicBlock
* BB
= InsertBefore
? InsertBefore
->getParent() : InsertAtEnd
;
368 Module
* M
= BB
->getParent()->getParent();
369 const Type
*BPTy
= Type::getInt8PtrTy(BB
->getContext());
370 Value
*MallocFunc
= MallocF
;
372 // prototype malloc as "void *malloc(size_t)"
373 MallocFunc
= M
->getOrInsertFunction("malloc", BPTy
, IntPtrTy
, NULL
);
374 const PointerType
*AllocPtrType
= PointerType::getUnqual(AllocTy
);
375 CallInst
*MCall
= NULL
;
376 Instruction
*Result
= NULL
;
378 MCall
= CallInst::Create(MallocFunc
, AllocSize
, "malloccall", InsertBefore
);
380 if (Result
->getType() != AllocPtrType
)
381 // Create a cast instruction to convert to the right type...
382 Result
= new BitCastInst(MCall
, AllocPtrType
, Name
, InsertBefore
);
384 MCall
= CallInst::Create(MallocFunc
, AllocSize
, "malloccall");
386 if (Result
->getType() != AllocPtrType
) {
387 InsertAtEnd
->getInstList().push_back(MCall
);
388 // Create a cast instruction to convert to the right type...
389 Result
= new BitCastInst(MCall
, AllocPtrType
, Name
);
392 MCall
->setTailCall();
393 if (Function
*F
= dyn_cast
<Function
>(MallocFunc
)) {
394 MCall
->setCallingConv(F
->getCallingConv());
395 if (!F
->doesNotAlias(0)) F
->setDoesNotAlias(0);
397 assert(!MCall
->getType()->isVoidTy() && "Malloc has void return type");
402 /// CreateMalloc - Generate the IR for a call to malloc:
403 /// 1. Compute the malloc call's argument as the specified type's size,
404 /// possibly multiplied by the array size if the array size is not
406 /// 2. Call malloc with that argument.
407 /// 3. Bitcast the result of the malloc call to the specified type.
408 Instruction
*CallInst::CreateMalloc(Instruction
*InsertBefore
,
409 const Type
*IntPtrTy
, const Type
*AllocTy
,
410 Value
*AllocSize
, Value
*ArraySize
,
413 return createMalloc(InsertBefore
, NULL
, IntPtrTy
, AllocTy
, AllocSize
,
414 ArraySize
, MallocF
, Name
);
417 /// CreateMalloc - Generate the IR for a call to malloc:
418 /// 1. Compute the malloc call's argument as the specified type's size,
419 /// possibly multiplied by the array size if the array size is not
421 /// 2. Call malloc with that argument.
422 /// 3. Bitcast the result of the malloc call to the specified type.
423 /// Note: This function does not add the bitcast to the basic block, that is the
424 /// responsibility of the caller.
425 Instruction
*CallInst::CreateMalloc(BasicBlock
*InsertAtEnd
,
426 const Type
*IntPtrTy
, const Type
*AllocTy
,
427 Value
*AllocSize
, Value
*ArraySize
,
428 Function
*MallocF
, const Twine
&Name
) {
429 return createMalloc(NULL
, InsertAtEnd
, IntPtrTy
, AllocTy
, AllocSize
,
430 ArraySize
, MallocF
, Name
);
433 static Instruction
* createFree(Value
* Source
, Instruction
*InsertBefore
,
434 BasicBlock
*InsertAtEnd
) {
435 assert(((!InsertBefore
&& InsertAtEnd
) || (InsertBefore
&& !InsertAtEnd
)) &&
436 "createFree needs either InsertBefore or InsertAtEnd");
437 assert(Source
->getType()->isPointerTy() &&
438 "Can not free something of nonpointer type!");
440 BasicBlock
* BB
= InsertBefore
? InsertBefore
->getParent() : InsertAtEnd
;
441 Module
* M
= BB
->getParent()->getParent();
443 const Type
*VoidTy
= Type::getVoidTy(M
->getContext());
444 const Type
*IntPtrTy
= Type::getInt8PtrTy(M
->getContext());
445 // prototype free as "void free(void*)"
446 Value
*FreeFunc
= M
->getOrInsertFunction("free", VoidTy
, IntPtrTy
, NULL
);
447 CallInst
* Result
= NULL
;
448 Value
*PtrCast
= Source
;
450 if (Source
->getType() != IntPtrTy
)
451 PtrCast
= new BitCastInst(Source
, IntPtrTy
, "", InsertBefore
);
452 Result
= CallInst::Create(FreeFunc
, PtrCast
, "", InsertBefore
);
454 if (Source
->getType() != IntPtrTy
)
455 PtrCast
= new BitCastInst(Source
, IntPtrTy
, "", InsertAtEnd
);
456 Result
= CallInst::Create(FreeFunc
, PtrCast
, "");
458 Result
->setTailCall();
459 if (Function
*F
= dyn_cast
<Function
>(FreeFunc
))
460 Result
->setCallingConv(F
->getCallingConv());
465 /// CreateFree - Generate the IR for a call to the builtin free function.
466 Instruction
* CallInst::CreateFree(Value
* Source
, Instruction
*InsertBefore
) {
467 return createFree(Source
, InsertBefore
, NULL
);
470 /// CreateFree - Generate the IR for a call to the builtin free function.
471 /// Note: This function does not add the call to the basic block, that is the
472 /// responsibility of the caller.
473 Instruction
* CallInst::CreateFree(Value
* Source
, BasicBlock
*InsertAtEnd
) {
474 Instruction
* FreeCall
= createFree(Source
, NULL
, InsertAtEnd
);
475 assert(FreeCall
&& "CreateFree did not create a CallInst");
479 //===----------------------------------------------------------------------===//
480 // InvokeInst Implementation
481 //===----------------------------------------------------------------------===//
483 void InvokeInst::init(Value
*Fn
, BasicBlock
*IfNormal
, BasicBlock
*IfException
,
484 Value
* const *Args
, unsigned NumArgs
) {
485 assert(NumOperands
== 3+NumArgs
&& "NumOperands not set up?");
488 Op
<-1>() = IfException
;
489 const FunctionType
*FTy
=
490 cast
<FunctionType
>(cast
<PointerType
>(Fn
->getType())->getElementType());
491 (void)FTy
; // silence warning.
493 assert(((NumArgs
== FTy
->getNumParams()) ||
494 (FTy
->isVarArg() && NumArgs
> FTy
->getNumParams())) &&
495 "Invoking a function with bad signature");
497 Use
*OL
= OperandList
;
498 for (unsigned i
= 0, e
= NumArgs
; i
!= e
; i
++) {
499 assert((i
>= FTy
->getNumParams() ||
500 FTy
->getParamType(i
) == Args
[i
]->getType()) &&
501 "Invoking a function with a bad signature!");
507 InvokeInst::InvokeInst(const InvokeInst
&II
)
508 : TerminatorInst(II
.getType(), Instruction::Invoke
,
509 OperandTraits
<InvokeInst
>::op_end(this)
510 - II
.getNumOperands(),
511 II
.getNumOperands()) {
512 setAttributes(II
.getAttributes());
513 setCallingConv(II
.getCallingConv());
514 Use
*OL
= OperandList
, *InOL
= II
.OperandList
;
515 for (unsigned i
= 0, e
= II
.getNumOperands(); i
!= e
; ++i
)
517 SubclassOptionalData
= II
.SubclassOptionalData
;
520 BasicBlock
*InvokeInst::getSuccessorV(unsigned idx
) const {
521 return getSuccessor(idx
);
523 unsigned InvokeInst::getNumSuccessorsV() const {
524 return getNumSuccessors();
526 void InvokeInst::setSuccessorV(unsigned idx
, BasicBlock
*B
) {
527 return setSuccessor(idx
, B
);
530 bool InvokeInst::paramHasAttr(unsigned i
, Attributes attr
) const {
531 if (AttributeList
.paramHasAttr(i
, attr
))
533 if (const Function
*F
= getCalledFunction())
534 return F
->paramHasAttr(i
, attr
);
538 void InvokeInst::addAttribute(unsigned i
, Attributes attr
) {
539 AttrListPtr PAL
= getAttributes();
540 PAL
= PAL
.addAttr(i
, attr
);
544 void InvokeInst::removeAttribute(unsigned i
, Attributes attr
) {
545 AttrListPtr PAL
= getAttributes();
546 PAL
= PAL
.removeAttr(i
, attr
);
551 //===----------------------------------------------------------------------===//
552 // ReturnInst Implementation
553 //===----------------------------------------------------------------------===//
555 ReturnInst::ReturnInst(const ReturnInst
&RI
)
556 : TerminatorInst(Type::getVoidTy(RI
.getContext()), Instruction::Ret
,
557 OperandTraits
<ReturnInst
>::op_end(this) -
559 RI
.getNumOperands()) {
560 if (RI
.getNumOperands())
561 Op
<0>() = RI
.Op
<0>();
562 SubclassOptionalData
= RI
.SubclassOptionalData
;
565 ReturnInst::ReturnInst(LLVMContext
&C
, Value
*retVal
, Instruction
*InsertBefore
)
566 : TerminatorInst(Type::getVoidTy(C
), Instruction::Ret
,
567 OperandTraits
<ReturnInst
>::op_end(this) - !!retVal
, !!retVal
,
572 ReturnInst::ReturnInst(LLVMContext
&C
, Value
*retVal
, BasicBlock
*InsertAtEnd
)
573 : TerminatorInst(Type::getVoidTy(C
), Instruction::Ret
,
574 OperandTraits
<ReturnInst
>::op_end(this) - !!retVal
, !!retVal
,
579 ReturnInst::ReturnInst(LLVMContext
&Context
, BasicBlock
*InsertAtEnd
)
580 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Ret
,
581 OperandTraits
<ReturnInst
>::op_end(this), 0, InsertAtEnd
) {
584 unsigned ReturnInst::getNumSuccessorsV() const {
585 return getNumSuccessors();
588 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
589 /// emit the vtable for the class in this translation unit.
590 void ReturnInst::setSuccessorV(unsigned idx
, BasicBlock
*NewSucc
) {
591 llvm_unreachable("ReturnInst has no successors!");
594 BasicBlock
*ReturnInst::getSuccessorV(unsigned idx
) const {
595 llvm_unreachable("ReturnInst has no successors!");
599 ReturnInst::~ReturnInst() {
602 //===----------------------------------------------------------------------===//
603 // UnwindInst Implementation
604 //===----------------------------------------------------------------------===//
606 UnwindInst::UnwindInst(LLVMContext
&Context
, Instruction
*InsertBefore
)
607 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Unwind
,
608 0, 0, InsertBefore
) {
610 UnwindInst::UnwindInst(LLVMContext
&Context
, BasicBlock
*InsertAtEnd
)
611 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Unwind
,
616 unsigned UnwindInst::getNumSuccessorsV() const {
617 return getNumSuccessors();
620 void UnwindInst::setSuccessorV(unsigned idx
, BasicBlock
*NewSucc
) {
621 llvm_unreachable("UnwindInst has no successors!");
624 BasicBlock
*UnwindInst::getSuccessorV(unsigned idx
) const {
625 llvm_unreachable("UnwindInst has no successors!");
629 //===----------------------------------------------------------------------===//
630 // UnreachableInst Implementation
631 //===----------------------------------------------------------------------===//
633 UnreachableInst::UnreachableInst(LLVMContext
&Context
,
634 Instruction
*InsertBefore
)
635 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Unreachable
,
636 0, 0, InsertBefore
) {
638 UnreachableInst::UnreachableInst(LLVMContext
&Context
, BasicBlock
*InsertAtEnd
)
639 : TerminatorInst(Type::getVoidTy(Context
), Instruction::Unreachable
,
643 unsigned UnreachableInst::getNumSuccessorsV() const {
644 return getNumSuccessors();
647 void UnreachableInst::setSuccessorV(unsigned idx
, BasicBlock
*NewSucc
) {
648 llvm_unreachable("UnwindInst has no successors!");
651 BasicBlock
*UnreachableInst::getSuccessorV(unsigned idx
) const {
652 llvm_unreachable("UnwindInst has no successors!");
656 //===----------------------------------------------------------------------===//
657 // BranchInst Implementation
658 //===----------------------------------------------------------------------===//
660 void BranchInst::AssertOK() {
662 assert(getCondition()->getType()->isIntegerTy(1) &&
663 "May only branch on boolean predicates!");
666 BranchInst::BranchInst(BasicBlock
*IfTrue
, Instruction
*InsertBefore
)
667 : TerminatorInst(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
668 OperandTraits
<BranchInst
>::op_end(this) - 1,
670 assert(IfTrue
!= 0 && "Branch destination may not be null!");
673 BranchInst::BranchInst(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
, Value
*Cond
,
674 Instruction
*InsertBefore
)
675 : TerminatorInst(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
676 OperandTraits
<BranchInst
>::op_end(this) - 3,
686 BranchInst::BranchInst(BasicBlock
*IfTrue
, BasicBlock
*InsertAtEnd
)
687 : TerminatorInst(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
688 OperandTraits
<BranchInst
>::op_end(this) - 1,
690 assert(IfTrue
!= 0 && "Branch destination may not be null!");
694 BranchInst::BranchInst(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
, Value
*Cond
,
695 BasicBlock
*InsertAtEnd
)
696 : TerminatorInst(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
697 OperandTraits
<BranchInst
>::op_end(this) - 3,
708 BranchInst::BranchInst(const BranchInst
&BI
) :
709 TerminatorInst(Type::getVoidTy(BI
.getContext()), Instruction::Br
,
710 OperandTraits
<BranchInst
>::op_end(this) - BI
.getNumOperands(),
711 BI
.getNumOperands()) {
712 Op
<-1>() = BI
.Op
<-1>();
713 if (BI
.getNumOperands() != 1) {
714 assert(BI
.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
715 Op
<-3>() = BI
.Op
<-3>();
716 Op
<-2>() = BI
.Op
<-2>();
718 SubclassOptionalData
= BI
.SubclassOptionalData
;
721 BasicBlock
*BranchInst::getSuccessorV(unsigned idx
) const {
722 return getSuccessor(idx
);
724 unsigned BranchInst::getNumSuccessorsV() const {
725 return getNumSuccessors();
727 void BranchInst::setSuccessorV(unsigned idx
, BasicBlock
*B
) {
728 setSuccessor(idx
, B
);
732 //===----------------------------------------------------------------------===//
733 // AllocaInst Implementation
734 //===----------------------------------------------------------------------===//
736 static Value
*getAISize(LLVMContext
&Context
, Value
*Amt
) {
738 Amt
= ConstantInt::get(Type::getInt32Ty(Context
), 1);
740 assert(!isa
<BasicBlock
>(Amt
) &&
741 "Passed basic block into allocation size parameter! Use other ctor");
742 assert(Amt
->getType()->isIntegerTy() &&
743 "Allocation array size is not an integer!");
748 AllocaInst::AllocaInst(const Type
*Ty
, Value
*ArraySize
,
749 const Twine
&Name
, Instruction
*InsertBefore
)
750 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
751 getAISize(Ty
->getContext(), ArraySize
), InsertBefore
) {
753 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
757 AllocaInst::AllocaInst(const Type
*Ty
, Value
*ArraySize
,
758 const Twine
&Name
, BasicBlock
*InsertAtEnd
)
759 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
760 getAISize(Ty
->getContext(), ArraySize
), InsertAtEnd
) {
762 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
766 AllocaInst::AllocaInst(const Type
*Ty
, const Twine
&Name
,
767 Instruction
*InsertBefore
)
768 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
769 getAISize(Ty
->getContext(), 0), InsertBefore
) {
771 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
775 AllocaInst::AllocaInst(const Type
*Ty
, const Twine
&Name
,
776 BasicBlock
*InsertAtEnd
)
777 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
778 getAISize(Ty
->getContext(), 0), InsertAtEnd
) {
780 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
784 AllocaInst::AllocaInst(const Type
*Ty
, Value
*ArraySize
, unsigned Align
,
785 const Twine
&Name
, Instruction
*InsertBefore
)
786 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
787 getAISize(Ty
->getContext(), ArraySize
), InsertBefore
) {
789 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
793 AllocaInst::AllocaInst(const Type
*Ty
, Value
*ArraySize
, unsigned Align
,
794 const Twine
&Name
, BasicBlock
*InsertAtEnd
)
795 : UnaryInstruction(PointerType::getUnqual(Ty
), Alloca
,
796 getAISize(Ty
->getContext(), ArraySize
), InsertAtEnd
) {
798 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
802 // Out of line virtual method, so the vtable, etc has a home.
803 AllocaInst::~AllocaInst() {
806 void AllocaInst::setAlignment(unsigned Align
) {
807 assert((Align
& (Align
-1)) == 0 && "Alignment is not a power of 2!");
808 assert(Align
<= MaximumAlignment
&&
809 "Alignment is greater than MaximumAlignment!");
810 setInstructionSubclassData(Log2_32(Align
) + 1);
811 assert(getAlignment() == Align
&& "Alignment representation error!");
814 bool AllocaInst::isArrayAllocation() const {
815 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(getOperand(0)))
820 const Type
*AllocaInst::getAllocatedType() const {
821 return getType()->getElementType();
824 /// isStaticAlloca - Return true if this alloca is in the entry block of the
825 /// function and is a constant size. If so, the code generator will fold it
826 /// into the prolog/epilog code, so it is basically free.
827 bool AllocaInst::isStaticAlloca() const {
828 // Must be constant size.
829 if (!isa
<ConstantInt
>(getArraySize())) return false;
831 // Must be in the entry block.
832 const BasicBlock
*Parent
= getParent();
833 return Parent
== &Parent
->getParent()->front();
836 //===----------------------------------------------------------------------===//
837 // LoadInst Implementation
838 //===----------------------------------------------------------------------===//
840 void LoadInst::AssertOK() {
841 assert(getOperand(0)->getType()->isPointerTy() &&
842 "Ptr must have pointer type.");
845 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, Instruction
*InsertBef
)
846 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
847 Load
, Ptr
, InsertBef
) {
854 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, BasicBlock
*InsertAE
)
855 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
856 Load
, Ptr
, InsertAE
) {
863 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
864 Instruction
*InsertBef
)
865 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
866 Load
, Ptr
, InsertBef
) {
867 setVolatile(isVolatile
);
873 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
874 unsigned Align
, Instruction
*InsertBef
)
875 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
876 Load
, Ptr
, InsertBef
) {
877 setVolatile(isVolatile
);
883 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
884 unsigned Align
, BasicBlock
*InsertAE
)
885 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
886 Load
, Ptr
, InsertAE
) {
887 setVolatile(isVolatile
);
893 LoadInst::LoadInst(Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
894 BasicBlock
*InsertAE
)
895 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
896 Load
, Ptr
, InsertAE
) {
897 setVolatile(isVolatile
);
905 LoadInst::LoadInst(Value
*Ptr
, const char *Name
, Instruction
*InsertBef
)
906 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
907 Load
, Ptr
, InsertBef
) {
911 if (Name
&& Name
[0]) setName(Name
);
914 LoadInst::LoadInst(Value
*Ptr
, const char *Name
, BasicBlock
*InsertAE
)
915 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
916 Load
, Ptr
, InsertAE
) {
920 if (Name
&& Name
[0]) setName(Name
);
923 LoadInst::LoadInst(Value
*Ptr
, const char *Name
, bool isVolatile
,
924 Instruction
*InsertBef
)
925 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
926 Load
, Ptr
, InsertBef
) {
927 setVolatile(isVolatile
);
930 if (Name
&& Name
[0]) setName(Name
);
933 LoadInst::LoadInst(Value
*Ptr
, const char *Name
, bool isVolatile
,
934 BasicBlock
*InsertAE
)
935 : UnaryInstruction(cast
<PointerType
>(Ptr
->getType())->getElementType(),
936 Load
, Ptr
, InsertAE
) {
937 setVolatile(isVolatile
);
940 if (Name
&& Name
[0]) setName(Name
);
943 void LoadInst::setAlignment(unsigned Align
) {
944 assert((Align
& (Align
-1)) == 0 && "Alignment is not a power of 2!");
945 assert(Align
<= MaximumAlignment
&&
946 "Alignment is greater than MaximumAlignment!");
947 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
948 ((Log2_32(Align
)+1)<<1));
949 assert(getAlignment() == Align
&& "Alignment representation error!");
952 //===----------------------------------------------------------------------===//
953 // StoreInst Implementation
954 //===----------------------------------------------------------------------===//
956 void StoreInst::AssertOK() {
957 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
958 assert(getOperand(1)->getType()->isPointerTy() &&
959 "Ptr must have pointer type!");
960 assert(getOperand(0)->getType() ==
961 cast
<PointerType
>(getOperand(1)->getType())->getElementType()
962 && "Ptr must be a pointer to Val type!");
966 StoreInst::StoreInst(Value
*val
, Value
*addr
, Instruction
*InsertBefore
)
967 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
968 OperandTraits
<StoreInst
>::op_begin(this),
969 OperandTraits
<StoreInst
>::operands(this),
978 StoreInst::StoreInst(Value
*val
, Value
*addr
, BasicBlock
*InsertAtEnd
)
979 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
980 OperandTraits
<StoreInst
>::op_begin(this),
981 OperandTraits
<StoreInst
>::operands(this),
990 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
991 Instruction
*InsertBefore
)
992 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
993 OperandTraits
<StoreInst
>::op_begin(this),
994 OperandTraits
<StoreInst
>::operands(this),
998 setVolatile(isVolatile
);
1003 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
1004 unsigned Align
, Instruction
*InsertBefore
)
1005 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
1006 OperandTraits
<StoreInst
>::op_begin(this),
1007 OperandTraits
<StoreInst
>::operands(this),
1011 setVolatile(isVolatile
);
1012 setAlignment(Align
);
1016 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
1017 unsigned Align
, BasicBlock
*InsertAtEnd
)
1018 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
1019 OperandTraits
<StoreInst
>::op_begin(this),
1020 OperandTraits
<StoreInst
>::operands(this),
1024 setVolatile(isVolatile
);
1025 setAlignment(Align
);
1029 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
1030 BasicBlock
*InsertAtEnd
)
1031 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
1032 OperandTraits
<StoreInst
>::op_begin(this),
1033 OperandTraits
<StoreInst
>::operands(this),
1037 setVolatile(isVolatile
);
1042 void StoreInst::setAlignment(unsigned Align
) {
1043 assert((Align
& (Align
-1)) == 0 && "Alignment is not a power of 2!");
1044 assert(Align
<= MaximumAlignment
&&
1045 "Alignment is greater than MaximumAlignment!");
1046 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1047 ((Log2_32(Align
)+1) << 1));
1048 assert(getAlignment() == Align
&& "Alignment representation error!");
1051 //===----------------------------------------------------------------------===//
1052 // GetElementPtrInst Implementation
1053 //===----------------------------------------------------------------------===//
1055 static unsigned retrieveAddrSpace(const Value
*Val
) {
1056 return cast
<PointerType
>(Val
->getType())->getAddressSpace();
1059 void GetElementPtrInst::init(Value
*Ptr
, Value
* const *Idx
, unsigned NumIdx
,
1060 const Twine
&Name
) {
1061 assert(NumOperands
== 1+NumIdx
&& "NumOperands not initialized?");
1062 Use
*OL
= OperandList
;
1065 for (unsigned i
= 0; i
!= NumIdx
; ++i
)
1071 void GetElementPtrInst::init(Value
*Ptr
, Value
*Idx
, const Twine
&Name
) {
1072 assert(NumOperands
== 2 && "NumOperands not initialized?");
1073 Use
*OL
= OperandList
;
1080 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst
&GEPI
)
1081 : Instruction(GEPI
.getType(), GetElementPtr
,
1082 OperandTraits
<GetElementPtrInst
>::op_end(this)
1083 - GEPI
.getNumOperands(),
1084 GEPI
.getNumOperands()) {
1085 Use
*OL
= OperandList
;
1086 Use
*GEPIOL
= GEPI
.OperandList
;
1087 for (unsigned i
= 0, E
= NumOperands
; i
!= E
; ++i
)
1089 SubclassOptionalData
= GEPI
.SubclassOptionalData
;
1092 GetElementPtrInst::GetElementPtrInst(Value
*Ptr
, Value
*Idx
,
1093 const Twine
&Name
, Instruction
*InBe
)
1094 : Instruction(PointerType::get(
1095 checkType(getIndexedType(Ptr
->getType(),Idx
)), retrieveAddrSpace(Ptr
)),
1097 OperandTraits
<GetElementPtrInst
>::op_end(this) - 2,
1099 init(Ptr
, Idx
, Name
);
1102 GetElementPtrInst::GetElementPtrInst(Value
*Ptr
, Value
*Idx
,
1103 const Twine
&Name
, BasicBlock
*IAE
)
1104 : Instruction(PointerType::get(
1105 checkType(getIndexedType(Ptr
->getType(),Idx
)),
1106 retrieveAddrSpace(Ptr
)),
1108 OperandTraits
<GetElementPtrInst
>::op_end(this) - 2,
1110 init(Ptr
, Idx
, Name
);
1113 /// getIndexedType - Returns the type of the element that would be accessed with
1114 /// a gep instruction with the specified parameters.
1116 /// The Idxs pointer should point to a continuous piece of memory containing the
1117 /// indices, either as Value* or uint64_t.
1119 /// A null type is returned if the indices are invalid for the specified
1122 template <typename IndexTy
>
1123 static const Type
* getIndexedTypeInternal(const Type
*Ptr
, IndexTy
const *Idxs
,
1125 const PointerType
*PTy
= dyn_cast
<PointerType
>(Ptr
);
1126 if (!PTy
) return 0; // Type isn't a pointer type!
1127 const Type
*Agg
= PTy
->getElementType();
1129 // Handle the special case of the empty set index set, which is always valid.
1133 // If there is at least one index, the top level type must be sized, otherwise
1134 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1135 // that contain opaque types) under the assumption that it will be resolved to
1136 // a sane type later.
1137 if (!Agg
->isSized() && !Agg
->isAbstract())
1140 unsigned CurIdx
= 1;
1141 for (; CurIdx
!= NumIdx
; ++CurIdx
) {
1142 const CompositeType
*CT
= dyn_cast
<CompositeType
>(Agg
);
1143 if (!CT
|| CT
->isPointerTy()) return 0;
1144 IndexTy Index
= Idxs
[CurIdx
];
1145 if (!CT
->indexValid(Index
)) return 0;
1146 Agg
= CT
->getTypeAtIndex(Index
);
1148 // If the new type forwards to another type, then it is in the middle
1149 // of being refined to another type (and hence, may have dropped all
1150 // references to what it was using before). So, use the new forwarded
1152 if (const Type
*Ty
= Agg
->getForwardedType())
1155 return CurIdx
== NumIdx
? Agg
: 0;
1158 const Type
* GetElementPtrInst::getIndexedType(const Type
*Ptr
,
1161 return getIndexedTypeInternal(Ptr
, Idxs
, NumIdx
);
1164 const Type
* GetElementPtrInst::getIndexedType(const Type
*Ptr
,
1165 Constant
* const *Idxs
,
1167 return getIndexedTypeInternal(Ptr
, Idxs
, NumIdx
);
1170 const Type
* GetElementPtrInst::getIndexedType(const Type
*Ptr
,
1171 uint64_t const *Idxs
,
1173 return getIndexedTypeInternal(Ptr
, Idxs
, NumIdx
);
1176 const Type
* GetElementPtrInst::getIndexedType(const Type
*Ptr
, Value
*Idx
) {
1177 const PointerType
*PTy
= dyn_cast
<PointerType
>(Ptr
);
1178 if (!PTy
) return 0; // Type isn't a pointer type!
1180 // Check the pointer index.
1181 if (!PTy
->indexValid(Idx
)) return 0;
1183 return PTy
->getElementType();
1187 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1188 /// zeros. If so, the result pointer and the first operand have the same
1189 /// value, just potentially different types.
1190 bool GetElementPtrInst::hasAllZeroIndices() const {
1191 for (unsigned i
= 1, e
= getNumOperands(); i
!= e
; ++i
) {
1192 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(getOperand(i
))) {
1193 if (!CI
->isZero()) return false;
1201 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1202 /// constant integers. If so, the result pointer and the first operand have
1203 /// a constant offset between them.
1204 bool GetElementPtrInst::hasAllConstantIndices() const {
1205 for (unsigned i
= 1, e
= getNumOperands(); i
!= e
; ++i
) {
1206 if (!isa
<ConstantInt
>(getOperand(i
)))
1212 void GetElementPtrInst::setIsInBounds(bool B
) {
1213 cast
<GEPOperator
>(this)->setIsInBounds(B
);
1216 bool GetElementPtrInst::isInBounds() const {
1217 return cast
<GEPOperator
>(this)->isInBounds();
1220 //===----------------------------------------------------------------------===//
1221 // ExtractElementInst Implementation
1222 //===----------------------------------------------------------------------===//
1224 ExtractElementInst::ExtractElementInst(Value
*Val
, Value
*Index
,
1226 Instruction
*InsertBef
)
1227 : Instruction(cast
<VectorType
>(Val
->getType())->getElementType(),
1229 OperandTraits
<ExtractElementInst
>::op_begin(this),
1231 assert(isValidOperands(Val
, Index
) &&
1232 "Invalid extractelement instruction operands!");
1238 ExtractElementInst::ExtractElementInst(Value
*Val
, Value
*Index
,
1240 BasicBlock
*InsertAE
)
1241 : Instruction(cast
<VectorType
>(Val
->getType())->getElementType(),
1243 OperandTraits
<ExtractElementInst
>::op_begin(this),
1245 assert(isValidOperands(Val
, Index
) &&
1246 "Invalid extractelement instruction operands!");
1254 bool ExtractElementInst::isValidOperands(const Value
*Val
, const Value
*Index
) {
1255 if (!Val
->getType()->isVectorTy() || !Index
->getType()->isIntegerTy(32))
1261 //===----------------------------------------------------------------------===//
1262 // InsertElementInst Implementation
1263 //===----------------------------------------------------------------------===//
1265 InsertElementInst::InsertElementInst(Value
*Vec
, Value
*Elt
, Value
*Index
,
1267 Instruction
*InsertBef
)
1268 : Instruction(Vec
->getType(), InsertElement
,
1269 OperandTraits
<InsertElementInst
>::op_begin(this),
1271 assert(isValidOperands(Vec
, Elt
, Index
) &&
1272 "Invalid insertelement instruction operands!");
1279 InsertElementInst::InsertElementInst(Value
*Vec
, Value
*Elt
, Value
*Index
,
1281 BasicBlock
*InsertAE
)
1282 : Instruction(Vec
->getType(), InsertElement
,
1283 OperandTraits
<InsertElementInst
>::op_begin(this),
1285 assert(isValidOperands(Vec
, Elt
, Index
) &&
1286 "Invalid insertelement instruction operands!");
1294 bool InsertElementInst::isValidOperands(const Value
*Vec
, const Value
*Elt
,
1295 const Value
*Index
) {
1296 if (!Vec
->getType()->isVectorTy())
1297 return false; // First operand of insertelement must be vector type.
1299 if (Elt
->getType() != cast
<VectorType
>(Vec
->getType())->getElementType())
1300 return false;// Second operand of insertelement must be vector element type.
1302 if (!Index
->getType()->isIntegerTy(32))
1303 return false; // Third operand of insertelement must be i32.
1308 //===----------------------------------------------------------------------===//
1309 // ShuffleVectorInst Implementation
1310 //===----------------------------------------------------------------------===//
1312 ShuffleVectorInst::ShuffleVectorInst(Value
*V1
, Value
*V2
, Value
*Mask
,
1314 Instruction
*InsertBefore
)
1315 : Instruction(VectorType::get(cast
<VectorType
>(V1
->getType())->getElementType(),
1316 cast
<VectorType
>(Mask
->getType())->getNumElements()),
1318 OperandTraits
<ShuffleVectorInst
>::op_begin(this),
1319 OperandTraits
<ShuffleVectorInst
>::operands(this),
1321 assert(isValidOperands(V1
, V2
, Mask
) &&
1322 "Invalid shuffle vector instruction operands!");
1329 ShuffleVectorInst::ShuffleVectorInst(Value
*V1
, Value
*V2
, Value
*Mask
,
1331 BasicBlock
*InsertAtEnd
)
1332 : Instruction(VectorType::get(cast
<VectorType
>(V1
->getType())->getElementType(),
1333 cast
<VectorType
>(Mask
->getType())->getNumElements()),
1335 OperandTraits
<ShuffleVectorInst
>::op_begin(this),
1336 OperandTraits
<ShuffleVectorInst
>::operands(this),
1338 assert(isValidOperands(V1
, V2
, Mask
) &&
1339 "Invalid shuffle vector instruction operands!");
1347 bool ShuffleVectorInst::isValidOperands(const Value
*V1
, const Value
*V2
,
1348 const Value
*Mask
) {
1349 if (!V1
->getType()->isVectorTy() || V1
->getType() != V2
->getType())
1352 const VectorType
*MaskTy
= dyn_cast
<VectorType
>(Mask
->getType());
1353 if (MaskTy
== 0 || !MaskTy
->getElementType()->isIntegerTy(32))
1356 // Check to see if Mask is valid.
1357 if (const ConstantVector
*MV
= dyn_cast
<ConstantVector
>(Mask
)) {
1358 const VectorType
*VTy
= cast
<VectorType
>(V1
->getType());
1359 for (unsigned i
= 0, e
= MV
->getNumOperands(); i
!= e
; ++i
) {
1360 if (ConstantInt
* CI
= dyn_cast
<ConstantInt
>(MV
->getOperand(i
))) {
1361 if (CI
->uge(VTy
->getNumElements()*2))
1363 } else if (!isa
<UndefValue
>(MV
->getOperand(i
))) {
1368 else if (!isa
<UndefValue
>(Mask
) && !isa
<ConstantAggregateZero
>(Mask
))
1374 /// getMaskValue - Return the index from the shuffle mask for the specified
1375 /// output result. This is either -1 if the element is undef or a number less
1376 /// than 2*numelements.
1377 int ShuffleVectorInst::getMaskValue(unsigned i
) const {
1378 const Constant
*Mask
= cast
<Constant
>(getOperand(2));
1379 if (isa
<UndefValue
>(Mask
)) return -1;
1380 if (isa
<ConstantAggregateZero
>(Mask
)) return 0;
1381 const ConstantVector
*MaskCV
= cast
<ConstantVector
>(Mask
);
1382 assert(i
< MaskCV
->getNumOperands() && "Index out of range");
1384 if (isa
<UndefValue
>(MaskCV
->getOperand(i
)))
1386 return cast
<ConstantInt
>(MaskCV
->getOperand(i
))->getZExtValue();
1389 //===----------------------------------------------------------------------===//
1390 // InsertValueInst Class
1391 //===----------------------------------------------------------------------===//
1393 void InsertValueInst::init(Value
*Agg
, Value
*Val
, const unsigned *Idx
,
1394 unsigned NumIdx
, const Twine
&Name
) {
1395 assert(NumOperands
== 2 && "NumOperands not initialized?");
1396 assert(ExtractValueInst::getIndexedType(Agg
->getType(), Idx
, Idx
+ NumIdx
) ==
1397 Val
->getType() && "Inserted value must match indexed type!");
1401 Indices
.append(Idx
, Idx
+ NumIdx
);
1405 void InsertValueInst::init(Value
*Agg
, Value
*Val
, unsigned Idx
,
1406 const Twine
&Name
) {
1407 assert(NumOperands
== 2 && "NumOperands not initialized?");
1408 assert(ExtractValueInst::getIndexedType(Agg
->getType(), Idx
) == Val
->getType()
1409 && "Inserted value must match indexed type!");
1413 Indices
.push_back(Idx
);
1417 InsertValueInst::InsertValueInst(const InsertValueInst
&IVI
)
1418 : Instruction(IVI
.getType(), InsertValue
,
1419 OperandTraits
<InsertValueInst
>::op_begin(this), 2),
1420 Indices(IVI
.Indices
) {
1421 Op
<0>() = IVI
.getOperand(0);
1422 Op
<1>() = IVI
.getOperand(1);
1423 SubclassOptionalData
= IVI
.SubclassOptionalData
;
1426 InsertValueInst::InsertValueInst(Value
*Agg
,
1430 Instruction
*InsertBefore
)
1431 : Instruction(Agg
->getType(), InsertValue
,
1432 OperandTraits
<InsertValueInst
>::op_begin(this),
1434 init(Agg
, Val
, Idx
, Name
);
1437 InsertValueInst::InsertValueInst(Value
*Agg
,
1441 BasicBlock
*InsertAtEnd
)
1442 : Instruction(Agg
->getType(), InsertValue
,
1443 OperandTraits
<InsertValueInst
>::op_begin(this),
1445 init(Agg
, Val
, Idx
, Name
);
1448 //===----------------------------------------------------------------------===//
1449 // ExtractValueInst Class
1450 //===----------------------------------------------------------------------===//
1452 void ExtractValueInst::init(const unsigned *Idx
, unsigned NumIdx
,
1453 const Twine
&Name
) {
1454 assert(NumOperands
== 1 && "NumOperands not initialized?");
1456 Indices
.append(Idx
, Idx
+ NumIdx
);
1460 void ExtractValueInst::init(unsigned Idx
, const Twine
&Name
) {
1461 assert(NumOperands
== 1 && "NumOperands not initialized?");
1463 Indices
.push_back(Idx
);
1467 ExtractValueInst::ExtractValueInst(const ExtractValueInst
&EVI
)
1468 : UnaryInstruction(EVI
.getType(), ExtractValue
, EVI
.getOperand(0)),
1469 Indices(EVI
.Indices
) {
1470 SubclassOptionalData
= EVI
.SubclassOptionalData
;
1473 // getIndexedType - Returns the type of the element that would be extracted
1474 // with an extractvalue instruction with the specified parameters.
1476 // A null type is returned if the indices are invalid for the specified
1479 const Type
* ExtractValueInst::getIndexedType(const Type
*Agg
,
1480 const unsigned *Idxs
,
1482 for (unsigned CurIdx
= 0; CurIdx
!= NumIdx
; ++CurIdx
) {
1483 unsigned Index
= Idxs
[CurIdx
];
1484 // We can't use CompositeType::indexValid(Index) here.
1485 // indexValid() always returns true for arrays because getelementptr allows
1486 // out-of-bounds indices. Since we don't allow those for extractvalue and
1487 // insertvalue we need to check array indexing manually.
1488 // Since the only other types we can index into are struct types it's just
1489 // as easy to check those manually as well.
1490 if (const ArrayType
*AT
= dyn_cast
<ArrayType
>(Agg
)) {
1491 if (Index
>= AT
->getNumElements())
1493 } else if (const StructType
*ST
= dyn_cast
<StructType
>(Agg
)) {
1494 if (Index
>= ST
->getNumElements())
1497 // Not a valid type to index into.
1501 Agg
= cast
<CompositeType
>(Agg
)->getTypeAtIndex(Index
);
1503 // If the new type forwards to another type, then it is in the middle
1504 // of being refined to another type (and hence, may have dropped all
1505 // references to what it was using before). So, use the new forwarded
1507 if (const Type
*Ty
= Agg
->getForwardedType())
1513 const Type
* ExtractValueInst::getIndexedType(const Type
*Agg
,
1515 return getIndexedType(Agg
, &Idx
, 1);
1518 //===----------------------------------------------------------------------===//
1519 // BinaryOperator Class
1520 //===----------------------------------------------------------------------===//
1522 BinaryOperator::BinaryOperator(BinaryOps iType
, Value
*S1
, Value
*S2
,
1523 const Type
*Ty
, const Twine
&Name
,
1524 Instruction
*InsertBefore
)
1525 : Instruction(Ty
, iType
,
1526 OperandTraits
<BinaryOperator
>::op_begin(this),
1527 OperandTraits
<BinaryOperator
>::operands(this),
1535 BinaryOperator::BinaryOperator(BinaryOps iType
, Value
*S1
, Value
*S2
,
1536 const Type
*Ty
, const Twine
&Name
,
1537 BasicBlock
*InsertAtEnd
)
1538 : Instruction(Ty
, iType
,
1539 OperandTraits
<BinaryOperator
>::op_begin(this),
1540 OperandTraits
<BinaryOperator
>::operands(this),
1549 void BinaryOperator::init(BinaryOps iType
) {
1550 Value
*LHS
= getOperand(0), *RHS
= getOperand(1);
1551 (void)LHS
; (void)RHS
; // Silence warnings.
1552 assert(LHS
->getType() == RHS
->getType() &&
1553 "Binary operator operand types must match!");
1558 assert(getType() == LHS
->getType() &&
1559 "Arithmetic operation should return same type as operands!");
1560 assert(getType()->isIntOrIntVectorTy() &&
1561 "Tried to create an integer operation on a non-integer type!");
1563 case FAdd
: case FSub
:
1565 assert(getType() == LHS
->getType() &&
1566 "Arithmetic operation should return same type as operands!");
1567 assert(getType()->isFPOrFPVectorTy() &&
1568 "Tried to create a floating-point operation on a "
1569 "non-floating-point type!");
1573 assert(getType() == LHS
->getType() &&
1574 "Arithmetic operation should return same type as operands!");
1575 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1576 cast
<VectorType
>(getType())->getElementType()->isIntegerTy())) &&
1577 "Incorrect operand type (not integer) for S/UDIV");
1580 assert(getType() == LHS
->getType() &&
1581 "Arithmetic operation should return same type as operands!");
1582 assert(getType()->isFPOrFPVectorTy() &&
1583 "Incorrect operand type (not floating point) for FDIV");
1587 assert(getType() == LHS
->getType() &&
1588 "Arithmetic operation should return same type as operands!");
1589 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1590 cast
<VectorType
>(getType())->getElementType()->isIntegerTy())) &&
1591 "Incorrect operand type (not integer) for S/UREM");
1594 assert(getType() == LHS
->getType() &&
1595 "Arithmetic operation should return same type as operands!");
1596 assert(getType()->isFPOrFPVectorTy() &&
1597 "Incorrect operand type (not floating point) for FREM");
1602 assert(getType() == LHS
->getType() &&
1603 "Shift operation should return same type as operands!");
1604 assert((getType()->isIntegerTy() ||
1605 (getType()->isVectorTy() &&
1606 cast
<VectorType
>(getType())->getElementType()->isIntegerTy())) &&
1607 "Tried to create a shift operation on a non-integral type!");
1611 assert(getType() == LHS
->getType() &&
1612 "Logical operation should return same type as operands!");
1613 assert((getType()->isIntegerTy() ||
1614 (getType()->isVectorTy() &&
1615 cast
<VectorType
>(getType())->getElementType()->isIntegerTy())) &&
1616 "Tried to create a logical operation on a non-integral type!");
1624 BinaryOperator
*BinaryOperator::Create(BinaryOps Op
, Value
*S1
, Value
*S2
,
1626 Instruction
*InsertBefore
) {
1627 assert(S1
->getType() == S2
->getType() &&
1628 "Cannot create binary operator with two operands of differing type!");
1629 return new BinaryOperator(Op
, S1
, S2
, S1
->getType(), Name
, InsertBefore
);
1632 BinaryOperator
*BinaryOperator::Create(BinaryOps Op
, Value
*S1
, Value
*S2
,
1634 BasicBlock
*InsertAtEnd
) {
1635 BinaryOperator
*Res
= Create(Op
, S1
, S2
, Name
);
1636 InsertAtEnd
->getInstList().push_back(Res
);
1640 BinaryOperator
*BinaryOperator::CreateNeg(Value
*Op
, const Twine
&Name
,
1641 Instruction
*InsertBefore
) {
1642 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1643 return new BinaryOperator(Instruction::Sub
,
1645 Op
->getType(), Name
, InsertBefore
);
1648 BinaryOperator
*BinaryOperator::CreateNeg(Value
*Op
, const Twine
&Name
,
1649 BasicBlock
*InsertAtEnd
) {
1650 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1651 return new BinaryOperator(Instruction::Sub
,
1653 Op
->getType(), Name
, InsertAtEnd
);
1656 BinaryOperator
*BinaryOperator::CreateNSWNeg(Value
*Op
, const Twine
&Name
,
1657 Instruction
*InsertBefore
) {
1658 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1659 return BinaryOperator::CreateNSWSub(zero
, Op
, Name
, InsertBefore
);
1662 BinaryOperator
*BinaryOperator::CreateNSWNeg(Value
*Op
, const Twine
&Name
,
1663 BasicBlock
*InsertAtEnd
) {
1664 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1665 return BinaryOperator::CreateNSWSub(zero
, Op
, Name
, InsertAtEnd
);
1668 BinaryOperator
*BinaryOperator::CreateNUWNeg(Value
*Op
, const Twine
&Name
,
1669 Instruction
*InsertBefore
) {
1670 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1671 return BinaryOperator::CreateNUWSub(zero
, Op
, Name
, InsertBefore
);
1674 BinaryOperator
*BinaryOperator::CreateNUWNeg(Value
*Op
, const Twine
&Name
,
1675 BasicBlock
*InsertAtEnd
) {
1676 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1677 return BinaryOperator::CreateNUWSub(zero
, Op
, Name
, InsertAtEnd
);
1680 BinaryOperator
*BinaryOperator::CreateFNeg(Value
*Op
, const Twine
&Name
,
1681 Instruction
*InsertBefore
) {
1682 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1683 return new BinaryOperator(Instruction::FSub
,
1685 Op
->getType(), Name
, InsertBefore
);
1688 BinaryOperator
*BinaryOperator::CreateFNeg(Value
*Op
, const Twine
&Name
,
1689 BasicBlock
*InsertAtEnd
) {
1690 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
1691 return new BinaryOperator(Instruction::FSub
,
1693 Op
->getType(), Name
, InsertAtEnd
);
1696 BinaryOperator
*BinaryOperator::CreateNot(Value
*Op
, const Twine
&Name
,
1697 Instruction
*InsertBefore
) {
1699 if (const VectorType
*PTy
= dyn_cast
<VectorType
>(Op
->getType())) {
1700 C
= Constant::getAllOnesValue(PTy
->getElementType());
1701 C
= ConstantVector::get(
1702 std::vector
<Constant
*>(PTy
->getNumElements(), C
));
1704 C
= Constant::getAllOnesValue(Op
->getType());
1707 return new BinaryOperator(Instruction::Xor
, Op
, C
,
1708 Op
->getType(), Name
, InsertBefore
);
1711 BinaryOperator
*BinaryOperator::CreateNot(Value
*Op
, const Twine
&Name
,
1712 BasicBlock
*InsertAtEnd
) {
1714 if (const VectorType
*PTy
= dyn_cast
<VectorType
>(Op
->getType())) {
1715 // Create a vector of all ones values.
1716 Constant
*Elt
= Constant::getAllOnesValue(PTy
->getElementType());
1717 AllOnes
= ConstantVector::get(
1718 std::vector
<Constant
*>(PTy
->getNumElements(), Elt
));
1720 AllOnes
= Constant::getAllOnesValue(Op
->getType());
1723 return new BinaryOperator(Instruction::Xor
, Op
, AllOnes
,
1724 Op
->getType(), Name
, InsertAtEnd
);
1728 // isConstantAllOnes - Helper function for several functions below
1729 static inline bool isConstantAllOnes(const Value
*V
) {
1730 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(V
))
1731 return CI
->isAllOnesValue();
1732 if (const ConstantVector
*CV
= dyn_cast
<ConstantVector
>(V
))
1733 return CV
->isAllOnesValue();
1737 bool BinaryOperator::isNeg(const Value
*V
) {
1738 if (const BinaryOperator
*Bop
= dyn_cast
<BinaryOperator
>(V
))
1739 if (Bop
->getOpcode() == Instruction::Sub
)
1740 if (Constant
* C
= dyn_cast
<Constant
>(Bop
->getOperand(0)))
1741 return C
->isNegativeZeroValue();
1745 bool BinaryOperator::isFNeg(const Value
*V
) {
1746 if (const BinaryOperator
*Bop
= dyn_cast
<BinaryOperator
>(V
))
1747 if (Bop
->getOpcode() == Instruction::FSub
)
1748 if (Constant
* C
= dyn_cast
<Constant
>(Bop
->getOperand(0)))
1749 return C
->isNegativeZeroValue();
1753 bool BinaryOperator::isNot(const Value
*V
) {
1754 if (const BinaryOperator
*Bop
= dyn_cast
<BinaryOperator
>(V
))
1755 return (Bop
->getOpcode() == Instruction::Xor
&&
1756 (isConstantAllOnes(Bop
->getOperand(1)) ||
1757 isConstantAllOnes(Bop
->getOperand(0))));
1761 Value
*BinaryOperator::getNegArgument(Value
*BinOp
) {
1762 return cast
<BinaryOperator
>(BinOp
)->getOperand(1);
1765 const Value
*BinaryOperator::getNegArgument(const Value
*BinOp
) {
1766 return getNegArgument(const_cast<Value
*>(BinOp
));
1769 Value
*BinaryOperator::getFNegArgument(Value
*BinOp
) {
1770 return cast
<BinaryOperator
>(BinOp
)->getOperand(1);
1773 const Value
*BinaryOperator::getFNegArgument(const Value
*BinOp
) {
1774 return getFNegArgument(const_cast<Value
*>(BinOp
));
1777 Value
*BinaryOperator::getNotArgument(Value
*BinOp
) {
1778 assert(isNot(BinOp
) && "getNotArgument on non-'not' instruction!");
1779 BinaryOperator
*BO
= cast
<BinaryOperator
>(BinOp
);
1780 Value
*Op0
= BO
->getOperand(0);
1781 Value
*Op1
= BO
->getOperand(1);
1782 if (isConstantAllOnes(Op0
)) return Op1
;
1784 assert(isConstantAllOnes(Op1
));
1788 const Value
*BinaryOperator::getNotArgument(const Value
*BinOp
) {
1789 return getNotArgument(const_cast<Value
*>(BinOp
));
1793 // swapOperands - Exchange the two operands to this instruction. This
1794 // instruction is safe to use on any binary instruction and does not
1795 // modify the semantics of the instruction. If the instruction is
1796 // order dependent (SetLT f.e.) the opcode is changed.
1798 bool BinaryOperator::swapOperands() {
1799 if (!isCommutative())
1800 return true; // Can't commute operands
1801 Op
<0>().swap(Op
<1>());
1805 void BinaryOperator::setHasNoUnsignedWrap(bool b
) {
1806 cast
<OverflowingBinaryOperator
>(this)->setHasNoUnsignedWrap(b
);
1809 void BinaryOperator::setHasNoSignedWrap(bool b
) {
1810 cast
<OverflowingBinaryOperator
>(this)->setHasNoSignedWrap(b
);
1813 void BinaryOperator::setIsExact(bool b
) {
1814 cast
<PossiblyExactOperator
>(this)->setIsExact(b
);
1817 bool BinaryOperator::hasNoUnsignedWrap() const {
1818 return cast
<OverflowingBinaryOperator
>(this)->hasNoUnsignedWrap();
1821 bool BinaryOperator::hasNoSignedWrap() const {
1822 return cast
<OverflowingBinaryOperator
>(this)->hasNoSignedWrap();
1825 bool BinaryOperator::isExact() const {
1826 return cast
<PossiblyExactOperator
>(this)->isExact();
1829 //===----------------------------------------------------------------------===//
1831 //===----------------------------------------------------------------------===//
1833 // Just determine if this cast only deals with integral->integral conversion.
1834 bool CastInst::isIntegerCast() const {
1835 switch (getOpcode()) {
1836 default: return false;
1837 case Instruction::ZExt
:
1838 case Instruction::SExt
:
1839 case Instruction::Trunc
:
1841 case Instruction::BitCast
:
1842 return getOperand(0)->getType()->isIntegerTy() &&
1843 getType()->isIntegerTy();
1847 bool CastInst::isLosslessCast() const {
1848 // Only BitCast can be lossless, exit fast if we're not BitCast
1849 if (getOpcode() != Instruction::BitCast
)
1852 // Identity cast is always lossless
1853 const Type
* SrcTy
= getOperand(0)->getType();
1854 const Type
* DstTy
= getType();
1858 // Pointer to pointer is always lossless.
1859 if (SrcTy
->isPointerTy())
1860 return DstTy
->isPointerTy();
1861 return false; // Other types have no identity values
1864 /// This function determines if the CastInst does not require any bits to be
1865 /// changed in order to effect the cast. Essentially, it identifies cases where
1866 /// no code gen is necessary for the cast, hence the name no-op cast. For
1867 /// example, the following are all no-op casts:
1868 /// # bitcast i32* %x to i8*
1869 /// # bitcast <2 x i32> %x to <4 x i16>
1870 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
1871 /// @brief Determine if the described cast is a no-op.
1872 bool CastInst::isNoopCast(Instruction::CastOps Opcode
,
1875 const Type
*IntPtrTy
) {
1878 assert(!"Invalid CastOp");
1879 case Instruction::Trunc
:
1880 case Instruction::ZExt
:
1881 case Instruction::SExt
:
1882 case Instruction::FPTrunc
:
1883 case Instruction::FPExt
:
1884 case Instruction::UIToFP
:
1885 case Instruction::SIToFP
:
1886 case Instruction::FPToUI
:
1887 case Instruction::FPToSI
:
1888 return false; // These always modify bits
1889 case Instruction::BitCast
:
1890 return true; // BitCast never modifies bits.
1891 case Instruction::PtrToInt
:
1892 return IntPtrTy
->getScalarSizeInBits() ==
1893 DestTy
->getScalarSizeInBits();
1894 case Instruction::IntToPtr
:
1895 return IntPtrTy
->getScalarSizeInBits() ==
1896 SrcTy
->getScalarSizeInBits();
1900 /// @brief Determine if a cast is a no-op.
1901 bool CastInst::isNoopCast(const Type
*IntPtrTy
) const {
1902 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy
);
1905 /// This function determines if a pair of casts can be eliminated and what
1906 /// opcode should be used in the elimination. This assumes that there are two
1907 /// instructions like this:
1908 /// * %F = firstOpcode SrcTy %x to MidTy
1909 /// * %S = secondOpcode MidTy %F to DstTy
1910 /// The function returns a resultOpcode so these two casts can be replaced with:
1911 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
1912 /// If no such cast is permited, the function returns 0.
1913 unsigned CastInst::isEliminableCastPair(
1914 Instruction::CastOps firstOp
, Instruction::CastOps secondOp
,
1915 const Type
*SrcTy
, const Type
*MidTy
, const Type
*DstTy
, const Type
*IntPtrTy
)
1917 // Define the 144 possibilities for these two cast instructions. The values
1918 // in this matrix determine what to do in a given situation and select the
1919 // case in the switch below. The rows correspond to firstOp, the columns
1920 // correspond to secondOp. In looking at the table below, keep in mind
1921 // the following cast properties:
1923 // Size Compare Source Destination
1924 // Operator Src ? Size Type Sign Type Sign
1925 // -------- ------------ ------------------- ---------------------
1926 // TRUNC > Integer Any Integral Any
1927 // ZEXT < Integral Unsigned Integer Any
1928 // SEXT < Integral Signed Integer Any
1929 // FPTOUI n/a FloatPt n/a Integral Unsigned
1930 // FPTOSI n/a FloatPt n/a Integral Signed
1931 // UITOFP n/a Integral Unsigned FloatPt n/a
1932 // SITOFP n/a Integral Signed FloatPt n/a
1933 // FPTRUNC > FloatPt n/a FloatPt n/a
1934 // FPEXT < FloatPt n/a FloatPt n/a
1935 // PTRTOINT n/a Pointer n/a Integral Unsigned
1936 // INTTOPTR n/a Integral Unsigned Pointer n/a
1937 // BITCAST = FirstClass n/a FirstClass n/a
1939 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1940 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1941 // into "fptoui double to i64", but this loses information about the range
1942 // of the produced value (we no longer know the top-part is all zeros).
1943 // Further this conversion is often much more expensive for typical hardware,
1944 // and causes issues when building libgcc. We disallow fptosi+sext for the
1946 const unsigned numCastOps
=
1947 Instruction::CastOpsEnd
- Instruction::CastOpsBegin
;
1948 static const uint8_t CastResults
[numCastOps
][numCastOps
] = {
1949 // T F F U S F F P I B -+
1950 // R Z S P P I I T P 2 N T |
1951 // U E E 2 2 2 2 R E I T C +- secondOp
1952 // N X X U S F F N X N 2 V |
1953 // C T T I I P P C T T P T -+
1954 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1955 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1956 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
1957 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1958 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
1959 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1960 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1961 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1962 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1963 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1964 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1965 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1968 // If either of the casts are a bitcast from scalar to vector, disallow the
1970 if ((firstOp
== Instruction::BitCast
&&
1971 isa
<VectorType
>(SrcTy
) != isa
<VectorType
>(MidTy
)) ||
1972 (secondOp
== Instruction::BitCast
&&
1973 isa
<VectorType
>(MidTy
) != isa
<VectorType
>(DstTy
)))
1974 return 0; // Disallowed
1976 int ElimCase
= CastResults
[firstOp
-Instruction::CastOpsBegin
]
1977 [secondOp
-Instruction::CastOpsBegin
];
1980 // categorically disallowed
1983 // allowed, use first cast's opcode
1986 // allowed, use second cast's opcode
1989 // no-op cast in second op implies firstOp as long as the DestTy
1990 // is integer and we are not converting between a vector and a
1992 if (!SrcTy
->isVectorTy() && DstTy
->isIntegerTy())
1996 // no-op cast in second op implies firstOp as long as the DestTy
1997 // is floating point.
1998 if (DstTy
->isFloatingPointTy())
2002 // no-op cast in first op implies secondOp as long as the SrcTy
2004 if (SrcTy
->isIntegerTy())
2008 // no-op cast in first op implies secondOp as long as the SrcTy
2009 // is a floating point.
2010 if (SrcTy
->isFloatingPointTy())
2014 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
2017 unsigned PtrSize
= IntPtrTy
->getScalarSizeInBits();
2018 unsigned MidSize
= MidTy
->getScalarSizeInBits();
2019 if (MidSize
>= PtrSize
)
2020 return Instruction::BitCast
;
2024 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2025 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2026 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2027 unsigned SrcSize
= SrcTy
->getScalarSizeInBits();
2028 unsigned DstSize
= DstTy
->getScalarSizeInBits();
2029 if (SrcSize
== DstSize
)
2030 return Instruction::BitCast
;
2031 else if (SrcSize
< DstSize
)
2035 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2036 return Instruction::ZExt
;
2038 // fpext followed by ftrunc is allowed if the bit size returned to is
2039 // the same as the original, in which case its just a bitcast
2041 return Instruction::BitCast
;
2042 return 0; // If the types are not the same we can't eliminate it.
2044 // bitcast followed by ptrtoint is allowed as long as the bitcast
2045 // is a pointer to pointer cast.
2046 if (SrcTy
->isPointerTy() && MidTy
->isPointerTy())
2050 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2051 if (MidTy
->isPointerTy() && DstTy
->isPointerTy())
2055 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2058 unsigned PtrSize
= IntPtrTy
->getScalarSizeInBits();
2059 unsigned SrcSize
= SrcTy
->getScalarSizeInBits();
2060 unsigned DstSize
= DstTy
->getScalarSizeInBits();
2061 if (SrcSize
<= PtrSize
&& SrcSize
== DstSize
)
2062 return Instruction::BitCast
;
2066 // cast combination can't happen (error in input). This is for all cases
2067 // where the MidTy is not the same for the two cast instructions.
2068 assert(!"Invalid Cast Combination");
2071 assert(!"Error in CastResults table!!!");
2077 CastInst
*CastInst::Create(Instruction::CastOps op
, Value
*S
, const Type
*Ty
,
2078 const Twine
&Name
, Instruction
*InsertBefore
) {
2079 // Construct and return the appropriate CastInst subclass
2081 case Trunc
: return new TruncInst (S
, Ty
, Name
, InsertBefore
);
2082 case ZExt
: return new ZExtInst (S
, Ty
, Name
, InsertBefore
);
2083 case SExt
: return new SExtInst (S
, Ty
, Name
, InsertBefore
);
2084 case FPTrunc
: return new FPTruncInst (S
, Ty
, Name
, InsertBefore
);
2085 case FPExt
: return new FPExtInst (S
, Ty
, Name
, InsertBefore
);
2086 case UIToFP
: return new UIToFPInst (S
, Ty
, Name
, InsertBefore
);
2087 case SIToFP
: return new SIToFPInst (S
, Ty
, Name
, InsertBefore
);
2088 case FPToUI
: return new FPToUIInst (S
, Ty
, Name
, InsertBefore
);
2089 case FPToSI
: return new FPToSIInst (S
, Ty
, Name
, InsertBefore
);
2090 case PtrToInt
: return new PtrToIntInst (S
, Ty
, Name
, InsertBefore
);
2091 case IntToPtr
: return new IntToPtrInst (S
, Ty
, Name
, InsertBefore
);
2092 case BitCast
: return new BitCastInst (S
, Ty
, Name
, InsertBefore
);
2094 assert(!"Invalid opcode provided");
2099 CastInst
*CastInst::Create(Instruction::CastOps op
, Value
*S
, const Type
*Ty
,
2100 const Twine
&Name
, BasicBlock
*InsertAtEnd
) {
2101 // Construct and return the appropriate CastInst subclass
2103 case Trunc
: return new TruncInst (S
, Ty
, Name
, InsertAtEnd
);
2104 case ZExt
: return new ZExtInst (S
, Ty
, Name
, InsertAtEnd
);
2105 case SExt
: return new SExtInst (S
, Ty
, Name
, InsertAtEnd
);
2106 case FPTrunc
: return new FPTruncInst (S
, Ty
, Name
, InsertAtEnd
);
2107 case FPExt
: return new FPExtInst (S
, Ty
, Name
, InsertAtEnd
);
2108 case UIToFP
: return new UIToFPInst (S
, Ty
, Name
, InsertAtEnd
);
2109 case SIToFP
: return new SIToFPInst (S
, Ty
, Name
, InsertAtEnd
);
2110 case FPToUI
: return new FPToUIInst (S
, Ty
, Name
, InsertAtEnd
);
2111 case FPToSI
: return new FPToSIInst (S
, Ty
, Name
, InsertAtEnd
);
2112 case PtrToInt
: return new PtrToIntInst (S
, Ty
, Name
, InsertAtEnd
);
2113 case IntToPtr
: return new IntToPtrInst (S
, Ty
, Name
, InsertAtEnd
);
2114 case BitCast
: return new BitCastInst (S
, Ty
, Name
, InsertAtEnd
);
2116 assert(!"Invalid opcode provided");
2121 CastInst
*CastInst::CreateZExtOrBitCast(Value
*S
, const Type
*Ty
,
2123 Instruction
*InsertBefore
) {
2124 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2125 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2126 return Create(Instruction::ZExt
, S
, Ty
, Name
, InsertBefore
);
2129 CastInst
*CastInst::CreateZExtOrBitCast(Value
*S
, const Type
*Ty
,
2131 BasicBlock
*InsertAtEnd
) {
2132 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2133 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2134 return Create(Instruction::ZExt
, S
, Ty
, Name
, InsertAtEnd
);
2137 CastInst
*CastInst::CreateSExtOrBitCast(Value
*S
, const Type
*Ty
,
2139 Instruction
*InsertBefore
) {
2140 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2141 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2142 return Create(Instruction::SExt
, S
, Ty
, Name
, InsertBefore
);
2145 CastInst
*CastInst::CreateSExtOrBitCast(Value
*S
, const Type
*Ty
,
2147 BasicBlock
*InsertAtEnd
) {
2148 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2149 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2150 return Create(Instruction::SExt
, S
, Ty
, Name
, InsertAtEnd
);
2153 CastInst
*CastInst::CreateTruncOrBitCast(Value
*S
, const Type
*Ty
,
2155 Instruction
*InsertBefore
) {
2156 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2157 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2158 return Create(Instruction::Trunc
, S
, Ty
, Name
, InsertBefore
);
2161 CastInst
*CastInst::CreateTruncOrBitCast(Value
*S
, const Type
*Ty
,
2163 BasicBlock
*InsertAtEnd
) {
2164 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2165 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2166 return Create(Instruction::Trunc
, S
, Ty
, Name
, InsertAtEnd
);
2169 CastInst
*CastInst::CreatePointerCast(Value
*S
, const Type
*Ty
,
2171 BasicBlock
*InsertAtEnd
) {
2172 assert(S
->getType()->isPointerTy() && "Invalid cast");
2173 assert((Ty
->isIntegerTy() || Ty
->isPointerTy()) &&
2176 if (Ty
->isIntegerTy())
2177 return Create(Instruction::PtrToInt
, S
, Ty
, Name
, InsertAtEnd
);
2178 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2181 /// @brief Create a BitCast or a PtrToInt cast instruction
2182 CastInst
*CastInst::CreatePointerCast(Value
*S
, const Type
*Ty
,
2184 Instruction
*InsertBefore
) {
2185 assert(S
->getType()->isPointerTy() && "Invalid cast");
2186 assert((Ty
->isIntegerTy() || Ty
->isPointerTy()) &&
2189 if (Ty
->isIntegerTy())
2190 return Create(Instruction::PtrToInt
, S
, Ty
, Name
, InsertBefore
);
2191 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2194 CastInst
*CastInst::CreateIntegerCast(Value
*C
, const Type
*Ty
,
2195 bool isSigned
, const Twine
&Name
,
2196 Instruction
*InsertBefore
) {
2197 assert(C
->getType()->isIntOrIntVectorTy() && Ty
->isIntOrIntVectorTy() &&
2198 "Invalid integer cast");
2199 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2200 unsigned DstBits
= Ty
->getScalarSizeInBits();
2201 Instruction::CastOps opcode
=
2202 (SrcBits
== DstBits
? Instruction::BitCast
:
2203 (SrcBits
> DstBits
? Instruction::Trunc
:
2204 (isSigned
? Instruction::SExt
: Instruction::ZExt
)));
2205 return Create(opcode
, C
, Ty
, Name
, InsertBefore
);
2208 CastInst
*CastInst::CreateIntegerCast(Value
*C
, const Type
*Ty
,
2209 bool isSigned
, const Twine
&Name
,
2210 BasicBlock
*InsertAtEnd
) {
2211 assert(C
->getType()->isIntOrIntVectorTy() && Ty
->isIntOrIntVectorTy() &&
2213 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2214 unsigned DstBits
= Ty
->getScalarSizeInBits();
2215 Instruction::CastOps opcode
=
2216 (SrcBits
== DstBits
? Instruction::BitCast
:
2217 (SrcBits
> DstBits
? Instruction::Trunc
:
2218 (isSigned
? Instruction::SExt
: Instruction::ZExt
)));
2219 return Create(opcode
, C
, Ty
, Name
, InsertAtEnd
);
2222 CastInst
*CastInst::CreateFPCast(Value
*C
, const Type
*Ty
,
2224 Instruction
*InsertBefore
) {
2225 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isFPOrFPVectorTy() &&
2227 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2228 unsigned DstBits
= Ty
->getScalarSizeInBits();
2229 Instruction::CastOps opcode
=
2230 (SrcBits
== DstBits
? Instruction::BitCast
:
2231 (SrcBits
> DstBits
? Instruction::FPTrunc
: Instruction::FPExt
));
2232 return Create(opcode
, C
, Ty
, Name
, InsertBefore
);
2235 CastInst
*CastInst::CreateFPCast(Value
*C
, const Type
*Ty
,
2237 BasicBlock
*InsertAtEnd
) {
2238 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isFPOrFPVectorTy() &&
2240 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2241 unsigned DstBits
= Ty
->getScalarSizeInBits();
2242 Instruction::CastOps opcode
=
2243 (SrcBits
== DstBits
? Instruction::BitCast
:
2244 (SrcBits
> DstBits
? Instruction::FPTrunc
: Instruction::FPExt
));
2245 return Create(opcode
, C
, Ty
, Name
, InsertAtEnd
);
2248 // Check whether it is valid to call getCastOpcode for these types.
2249 // This routine must be kept in sync with getCastOpcode.
2250 bool CastInst::isCastable(const Type
*SrcTy
, const Type
*DestTy
) {
2251 if (!SrcTy
->isFirstClassType() || !DestTy
->isFirstClassType())
2254 if (SrcTy
== DestTy
)
2257 // Get the bit sizes, we'll need these
2258 unsigned SrcBits
= SrcTy
->getScalarSizeInBits(); // 0 for ptr
2259 unsigned DestBits
= DestTy
->getScalarSizeInBits(); // 0 for ptr
2261 // Run through the possibilities ...
2262 if (DestTy
->isIntegerTy()) { // Casting to integral
2263 if (SrcTy
->isIntegerTy()) { // Casting from integral
2265 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
2267 } else if (const VectorType
*PTy
= dyn_cast
<VectorType
>(SrcTy
)) {
2268 // Casting from vector
2269 return DestBits
== PTy
->getBitWidth();
2270 } else { // Casting from something else
2271 return SrcTy
->isPointerTy();
2273 } else if (DestTy
->isFloatingPointTy()) { // Casting to floating pt
2274 if (SrcTy
->isIntegerTy()) { // Casting from integral
2276 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
2278 } else if (const VectorType
*PTy
= dyn_cast
<VectorType
>(SrcTy
)) {
2279 // Casting from vector
2280 return DestBits
== PTy
->getBitWidth();
2281 } else { // Casting from something else
2284 } else if (const VectorType
*DestPTy
= dyn_cast
<VectorType
>(DestTy
)) {
2285 // Casting to vector
2286 if (const VectorType
*SrcPTy
= dyn_cast
<VectorType
>(SrcTy
)) {
2287 // Casting from vector
2288 return DestPTy
->getBitWidth() == SrcPTy
->getBitWidth();
2289 } else if (DestPTy
->getBitWidth() == SrcBits
) {
2290 return true; // float/int -> vector
2291 } else if (SrcTy
->isX86_MMXTy()) {
2292 return DestPTy
->getBitWidth() == 64; // MMX to 64-bit vector
2296 } else if (DestTy
->isPointerTy()) { // Casting to pointer
2297 if (SrcTy
->isPointerTy()) { // Casting from pointer
2299 } else if (SrcTy
->isIntegerTy()) { // Casting from integral
2301 } else { // Casting from something else
2304 } else if (DestTy
->isX86_MMXTy()) {
2305 if (const VectorType
*SrcPTy
= dyn_cast
<VectorType
>(SrcTy
)) {
2306 return SrcPTy
->getBitWidth() == 64; // 64-bit vector to MMX
2310 } else { // Casting to something else
2315 // Provide a way to get a "cast" where the cast opcode is inferred from the
2316 // types and size of the operand. This, basically, is a parallel of the
2317 // logic in the castIsValid function below. This axiom should hold:
2318 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2319 // should not assert in castIsValid. In other words, this produces a "correct"
2320 // casting opcode for the arguments passed to it.
2321 // This routine must be kept in sync with isCastable.
2322 Instruction::CastOps
2323 CastInst::getCastOpcode(
2324 const Value
*Src
, bool SrcIsSigned
, const Type
*DestTy
, bool DestIsSigned
) {
2325 // Get the bit sizes, we'll need these
2326 const Type
*SrcTy
= Src
->getType();
2327 unsigned SrcBits
= SrcTy
->getScalarSizeInBits(); // 0 for ptr
2328 unsigned DestBits
= DestTy
->getScalarSizeInBits(); // 0 for ptr
2330 assert(SrcTy
->isFirstClassType() && DestTy
->isFirstClassType() &&
2331 "Only first class types are castable!");
2333 // Run through the possibilities ...
2334 if (DestTy
->isIntegerTy()) { // Casting to integral
2335 if (SrcTy
->isIntegerTy()) { // Casting from integral
2336 if (DestBits
< SrcBits
)
2337 return Trunc
; // int -> smaller int
2338 else if (DestBits
> SrcBits
) { // its an extension
2340 return SExt
; // signed -> SEXT
2342 return ZExt
; // unsigned -> ZEXT
2344 return BitCast
; // Same size, No-op cast
2346 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
2348 return FPToSI
; // FP -> sint
2350 return FPToUI
; // FP -> uint
2351 } else if (const VectorType
*PTy
= dyn_cast
<VectorType
>(SrcTy
)) {
2352 assert(DestBits
== PTy
->getBitWidth() &&
2353 "Casting vector to integer of different width");
2355 return BitCast
; // Same size, no-op cast
2357 assert(SrcTy
->isPointerTy() &&
2358 "Casting from a value that is not first-class type");
2359 return PtrToInt
; // ptr -> int
2361 } else if (DestTy
->isFloatingPointTy()) { // Casting to floating pt
2362 if (SrcTy
->isIntegerTy()) { // Casting from integral
2364 return SIToFP
; // sint -> FP
2366 return UIToFP
; // uint -> FP
2367 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
2368 if (DestBits
< SrcBits
) {
2369 return FPTrunc
; // FP -> smaller FP
2370 } else if (DestBits
> SrcBits
) {
2371 return FPExt
; // FP -> larger FP
2373 return BitCast
; // same size, no-op cast
2375 } else if (const VectorType
*PTy
= dyn_cast
<VectorType
>(SrcTy
)) {
2376 assert(DestBits
== PTy
->getBitWidth() &&
2377 "Casting vector to floating point of different width");
2379 return BitCast
; // same size, no-op cast
2381 llvm_unreachable("Casting pointer or non-first class to float");
2383 } else if (const VectorType
*DestPTy
= dyn_cast
<VectorType
>(DestTy
)) {
2384 if (const VectorType
*SrcPTy
= dyn_cast
<VectorType
>(SrcTy
)) {
2385 assert(DestPTy
->getBitWidth() == SrcPTy
->getBitWidth() &&
2386 "Casting vector to vector of different widths");
2388 return BitCast
; // vector -> vector
2389 } else if (DestPTy
->getBitWidth() == SrcBits
) {
2390 return BitCast
; // float/int -> vector
2391 } else if (SrcTy
->isX86_MMXTy()) {
2392 assert(DestPTy
->getBitWidth()==64 &&
2393 "Casting X86_MMX to vector of wrong width");
2394 return BitCast
; // MMX to 64-bit vector
2396 assert(!"Illegal cast to vector (wrong type or size)");
2398 } else if (DestTy
->isPointerTy()) {
2399 if (SrcTy
->isPointerTy()) {
2400 return BitCast
; // ptr -> ptr
2401 } else if (SrcTy
->isIntegerTy()) {
2402 return IntToPtr
; // int -> ptr
2404 assert(!"Casting pointer to other than pointer or int");
2406 } else if (DestTy
->isX86_MMXTy()) {
2407 if (isa
<VectorType
>(SrcTy
)) {
2408 assert(cast
<VectorType
>(SrcTy
)->getBitWidth() == 64 &&
2409 "Casting vector of wrong width to X86_MMX");
2410 return BitCast
; // 64-bit vector to MMX
2412 assert(!"Illegal cast to X86_MMX");
2415 assert(!"Casting to type that is not first-class");
2418 // If we fall through to here we probably hit an assertion cast above
2419 // and assertions are not turned on. Anything we return is an error, so
2420 // BitCast is as good a choice as any.
2424 //===----------------------------------------------------------------------===//
2425 // CastInst SubClass Constructors
2426 //===----------------------------------------------------------------------===//
2428 /// Check that the construction parameters for a CastInst are correct. This
2429 /// could be broken out into the separate constructors but it is useful to have
2430 /// it in one place and to eliminate the redundant code for getting the sizes
2431 /// of the types involved.
2433 CastInst::castIsValid(Instruction::CastOps op
, Value
*S
, const Type
*DstTy
) {
2435 // Check for type sanity on the arguments
2436 const Type
*SrcTy
= S
->getType();
2437 if (!SrcTy
->isFirstClassType() || !DstTy
->isFirstClassType() ||
2438 SrcTy
->isAggregateType() || DstTy
->isAggregateType())
2441 // Get the size of the types in bits, we'll need this later
2442 unsigned SrcBitSize
= SrcTy
->getScalarSizeInBits();
2443 unsigned DstBitSize
= DstTy
->getScalarSizeInBits();
2445 // Switch on the opcode provided
2447 default: return false; // This is an input error
2448 case Instruction::Trunc
:
2449 return SrcTy
->isIntOrIntVectorTy() &&
2450 DstTy
->isIntOrIntVectorTy()&& SrcBitSize
> DstBitSize
;
2451 case Instruction::ZExt
:
2452 return SrcTy
->isIntOrIntVectorTy() &&
2453 DstTy
->isIntOrIntVectorTy()&& SrcBitSize
< DstBitSize
;
2454 case Instruction::SExt
:
2455 return SrcTy
->isIntOrIntVectorTy() &&
2456 DstTy
->isIntOrIntVectorTy()&& SrcBitSize
< DstBitSize
;
2457 case Instruction::FPTrunc
:
2458 return SrcTy
->isFPOrFPVectorTy() &&
2459 DstTy
->isFPOrFPVectorTy() &&
2460 SrcBitSize
> DstBitSize
;
2461 case Instruction::FPExt
:
2462 return SrcTy
->isFPOrFPVectorTy() &&
2463 DstTy
->isFPOrFPVectorTy() &&
2464 SrcBitSize
< DstBitSize
;
2465 case Instruction::UIToFP
:
2466 case Instruction::SIToFP
:
2467 if (const VectorType
*SVTy
= dyn_cast
<VectorType
>(SrcTy
)) {
2468 if (const VectorType
*DVTy
= dyn_cast
<VectorType
>(DstTy
)) {
2469 return SVTy
->getElementType()->isIntOrIntVectorTy() &&
2470 DVTy
->getElementType()->isFPOrFPVectorTy() &&
2471 SVTy
->getNumElements() == DVTy
->getNumElements();
2474 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isFPOrFPVectorTy();
2475 case Instruction::FPToUI
:
2476 case Instruction::FPToSI
:
2477 if (const VectorType
*SVTy
= dyn_cast
<VectorType
>(SrcTy
)) {
2478 if (const VectorType
*DVTy
= dyn_cast
<VectorType
>(DstTy
)) {
2479 return SVTy
->getElementType()->isFPOrFPVectorTy() &&
2480 DVTy
->getElementType()->isIntOrIntVectorTy() &&
2481 SVTy
->getNumElements() == DVTy
->getNumElements();
2484 return SrcTy
->isFPOrFPVectorTy() && DstTy
->isIntOrIntVectorTy();
2485 case Instruction::PtrToInt
:
2486 return SrcTy
->isPointerTy() && DstTy
->isIntegerTy();
2487 case Instruction::IntToPtr
:
2488 return SrcTy
->isIntegerTy() && DstTy
->isPointerTy();
2489 case Instruction::BitCast
:
2490 // BitCast implies a no-op cast of type only. No bits change.
2491 // However, you can't cast pointers to anything but pointers.
2492 if (SrcTy
->isPointerTy() != DstTy
->isPointerTy())
2495 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2496 // these cases, the cast is okay if the source and destination bit widths
2498 return SrcTy
->getPrimitiveSizeInBits() == DstTy
->getPrimitiveSizeInBits();
2502 TruncInst::TruncInst(
2503 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2504 ) : CastInst(Ty
, Trunc
, S
, Name
, InsertBefore
) {
2505 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal Trunc");
2508 TruncInst::TruncInst(
2509 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2510 ) : CastInst(Ty
, Trunc
, S
, Name
, InsertAtEnd
) {
2511 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal Trunc");
2515 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2516 ) : CastInst(Ty
, ZExt
, S
, Name
, InsertBefore
) {
2517 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal ZExt");
2521 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2522 ) : CastInst(Ty
, ZExt
, S
, Name
, InsertAtEnd
) {
2523 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal ZExt");
2526 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2527 ) : CastInst(Ty
, SExt
, S
, Name
, InsertBefore
) {
2528 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SExt");
2532 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2533 ) : CastInst(Ty
, SExt
, S
, Name
, InsertAtEnd
) {
2534 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SExt");
2537 FPTruncInst::FPTruncInst(
2538 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2539 ) : CastInst(Ty
, FPTrunc
, S
, Name
, InsertBefore
) {
2540 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPTrunc");
2543 FPTruncInst::FPTruncInst(
2544 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2545 ) : CastInst(Ty
, FPTrunc
, S
, Name
, InsertAtEnd
) {
2546 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPTrunc");
2549 FPExtInst::FPExtInst(
2550 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2551 ) : CastInst(Ty
, FPExt
, S
, Name
, InsertBefore
) {
2552 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPExt");
2555 FPExtInst::FPExtInst(
2556 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2557 ) : CastInst(Ty
, FPExt
, S
, Name
, InsertAtEnd
) {
2558 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPExt");
2561 UIToFPInst::UIToFPInst(
2562 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2563 ) : CastInst(Ty
, UIToFP
, S
, Name
, InsertBefore
) {
2564 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal UIToFP");
2567 UIToFPInst::UIToFPInst(
2568 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2569 ) : CastInst(Ty
, UIToFP
, S
, Name
, InsertAtEnd
) {
2570 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal UIToFP");
2573 SIToFPInst::SIToFPInst(
2574 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2575 ) : CastInst(Ty
, SIToFP
, S
, Name
, InsertBefore
) {
2576 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SIToFP");
2579 SIToFPInst::SIToFPInst(
2580 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2581 ) : CastInst(Ty
, SIToFP
, S
, Name
, InsertAtEnd
) {
2582 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SIToFP");
2585 FPToUIInst::FPToUIInst(
2586 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2587 ) : CastInst(Ty
, FPToUI
, S
, Name
, InsertBefore
) {
2588 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToUI");
2591 FPToUIInst::FPToUIInst(
2592 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2593 ) : CastInst(Ty
, FPToUI
, S
, Name
, InsertAtEnd
) {
2594 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToUI");
2597 FPToSIInst::FPToSIInst(
2598 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2599 ) : CastInst(Ty
, FPToSI
, S
, Name
, InsertBefore
) {
2600 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToSI");
2603 FPToSIInst::FPToSIInst(
2604 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2605 ) : CastInst(Ty
, FPToSI
, S
, Name
, InsertAtEnd
) {
2606 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToSI");
2609 PtrToIntInst::PtrToIntInst(
2610 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2611 ) : CastInst(Ty
, PtrToInt
, S
, Name
, InsertBefore
) {
2612 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal PtrToInt");
2615 PtrToIntInst::PtrToIntInst(
2616 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2617 ) : CastInst(Ty
, PtrToInt
, S
, Name
, InsertAtEnd
) {
2618 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal PtrToInt");
2621 IntToPtrInst::IntToPtrInst(
2622 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2623 ) : CastInst(Ty
, IntToPtr
, S
, Name
, InsertBefore
) {
2624 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal IntToPtr");
2627 IntToPtrInst::IntToPtrInst(
2628 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2629 ) : CastInst(Ty
, IntToPtr
, S
, Name
, InsertAtEnd
) {
2630 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal IntToPtr");
2633 BitCastInst::BitCastInst(
2634 Value
*S
, const Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
2635 ) : CastInst(Ty
, BitCast
, S
, Name
, InsertBefore
) {
2636 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal BitCast");
2639 BitCastInst::BitCastInst(
2640 Value
*S
, const Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
2641 ) : CastInst(Ty
, BitCast
, S
, Name
, InsertAtEnd
) {
2642 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal BitCast");
2645 //===----------------------------------------------------------------------===//
2647 //===----------------------------------------------------------------------===//
2649 void CmpInst::Anchor() const {}
2651 CmpInst::CmpInst(const Type
*ty
, OtherOps op
, unsigned short predicate
,
2652 Value
*LHS
, Value
*RHS
, const Twine
&Name
,
2653 Instruction
*InsertBefore
)
2654 : Instruction(ty
, op
,
2655 OperandTraits
<CmpInst
>::op_begin(this),
2656 OperandTraits
<CmpInst
>::operands(this),
2660 setPredicate((Predicate
)predicate
);
2664 CmpInst::CmpInst(const Type
*ty
, OtherOps op
, unsigned short predicate
,
2665 Value
*LHS
, Value
*RHS
, const Twine
&Name
,
2666 BasicBlock
*InsertAtEnd
)
2667 : Instruction(ty
, op
,
2668 OperandTraits
<CmpInst
>::op_begin(this),
2669 OperandTraits
<CmpInst
>::operands(this),
2673 setPredicate((Predicate
)predicate
);
2678 CmpInst::Create(OtherOps Op
, unsigned short predicate
,
2679 Value
*S1
, Value
*S2
,
2680 const Twine
&Name
, Instruction
*InsertBefore
) {
2681 if (Op
== Instruction::ICmp
) {
2683 return new ICmpInst(InsertBefore
, CmpInst::Predicate(predicate
),
2686 return new ICmpInst(CmpInst::Predicate(predicate
),
2691 return new FCmpInst(InsertBefore
, CmpInst::Predicate(predicate
),
2694 return new FCmpInst(CmpInst::Predicate(predicate
),
2699 CmpInst::Create(OtherOps Op
, unsigned short predicate
, Value
*S1
, Value
*S2
,
2700 const Twine
&Name
, BasicBlock
*InsertAtEnd
) {
2701 if (Op
== Instruction::ICmp
) {
2702 return new ICmpInst(*InsertAtEnd
, CmpInst::Predicate(predicate
),
2705 return new FCmpInst(*InsertAtEnd
, CmpInst::Predicate(predicate
),
2709 void CmpInst::swapOperands() {
2710 if (ICmpInst
*IC
= dyn_cast
<ICmpInst
>(this))
2713 cast
<FCmpInst
>(this)->swapOperands();
2716 bool CmpInst::isCommutative() const {
2717 if (const ICmpInst
*IC
= dyn_cast
<ICmpInst
>(this))
2718 return IC
->isCommutative();
2719 return cast
<FCmpInst
>(this)->isCommutative();
2722 bool CmpInst::isEquality() const {
2723 if (const ICmpInst
*IC
= dyn_cast
<ICmpInst
>(this))
2724 return IC
->isEquality();
2725 return cast
<FCmpInst
>(this)->isEquality();
2729 CmpInst::Predicate
CmpInst::getInversePredicate(Predicate pred
) {
2731 default: assert(!"Unknown cmp predicate!");
2732 case ICMP_EQ
: return ICMP_NE
;
2733 case ICMP_NE
: return ICMP_EQ
;
2734 case ICMP_UGT
: return ICMP_ULE
;
2735 case ICMP_ULT
: return ICMP_UGE
;
2736 case ICMP_UGE
: return ICMP_ULT
;
2737 case ICMP_ULE
: return ICMP_UGT
;
2738 case ICMP_SGT
: return ICMP_SLE
;
2739 case ICMP_SLT
: return ICMP_SGE
;
2740 case ICMP_SGE
: return ICMP_SLT
;
2741 case ICMP_SLE
: return ICMP_SGT
;
2743 case FCMP_OEQ
: return FCMP_UNE
;
2744 case FCMP_ONE
: return FCMP_UEQ
;
2745 case FCMP_OGT
: return FCMP_ULE
;
2746 case FCMP_OLT
: return FCMP_UGE
;
2747 case FCMP_OGE
: return FCMP_ULT
;
2748 case FCMP_OLE
: return FCMP_UGT
;
2749 case FCMP_UEQ
: return FCMP_ONE
;
2750 case FCMP_UNE
: return FCMP_OEQ
;
2751 case FCMP_UGT
: return FCMP_OLE
;
2752 case FCMP_ULT
: return FCMP_OGE
;
2753 case FCMP_UGE
: return FCMP_OLT
;
2754 case FCMP_ULE
: return FCMP_OGT
;
2755 case FCMP_ORD
: return FCMP_UNO
;
2756 case FCMP_UNO
: return FCMP_ORD
;
2757 case FCMP_TRUE
: return FCMP_FALSE
;
2758 case FCMP_FALSE
: return FCMP_TRUE
;
2762 ICmpInst::Predicate
ICmpInst::getSignedPredicate(Predicate pred
) {
2764 default: assert(! "Unknown icmp predicate!");
2765 case ICMP_EQ
: case ICMP_NE
:
2766 case ICMP_SGT
: case ICMP_SLT
: case ICMP_SGE
: case ICMP_SLE
:
2768 case ICMP_UGT
: return ICMP_SGT
;
2769 case ICMP_ULT
: return ICMP_SLT
;
2770 case ICMP_UGE
: return ICMP_SGE
;
2771 case ICMP_ULE
: return ICMP_SLE
;
2775 ICmpInst::Predicate
ICmpInst::getUnsignedPredicate(Predicate pred
) {
2777 default: assert(! "Unknown icmp predicate!");
2778 case ICMP_EQ
: case ICMP_NE
:
2779 case ICMP_UGT
: case ICMP_ULT
: case ICMP_UGE
: case ICMP_ULE
:
2781 case ICMP_SGT
: return ICMP_UGT
;
2782 case ICMP_SLT
: return ICMP_ULT
;
2783 case ICMP_SGE
: return ICMP_UGE
;
2784 case ICMP_SLE
: return ICMP_ULE
;
2788 /// Initialize a set of values that all satisfy the condition with C.
2791 ICmpInst::makeConstantRange(Predicate pred
, const APInt
&C
) {
2794 uint32_t BitWidth
= C
.getBitWidth();
2796 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
2797 case ICmpInst::ICMP_EQ
: Upper
++; break;
2798 case ICmpInst::ICMP_NE
: Lower
++; break;
2799 case ICmpInst::ICMP_ULT
:
2800 Lower
= APInt::getMinValue(BitWidth
);
2801 // Check for an empty-set condition.
2803 return ConstantRange(BitWidth
, /*isFullSet=*/false);
2805 case ICmpInst::ICMP_SLT
:
2806 Lower
= APInt::getSignedMinValue(BitWidth
);
2807 // Check for an empty-set condition.
2809 return ConstantRange(BitWidth
, /*isFullSet=*/false);
2811 case ICmpInst::ICMP_UGT
:
2812 Lower
++; Upper
= APInt::getMinValue(BitWidth
); // Min = Next(Max)
2813 // Check for an empty-set condition.
2815 return ConstantRange(BitWidth
, /*isFullSet=*/false);
2817 case ICmpInst::ICMP_SGT
:
2818 Lower
++; Upper
= APInt::getSignedMinValue(BitWidth
); // Min = Next(Max)
2819 // Check for an empty-set condition.
2821 return ConstantRange(BitWidth
, /*isFullSet=*/false);
2823 case ICmpInst::ICMP_ULE
:
2824 Lower
= APInt::getMinValue(BitWidth
); Upper
++;
2825 // Check for a full-set condition.
2827 return ConstantRange(BitWidth
, /*isFullSet=*/true);
2829 case ICmpInst::ICMP_SLE
:
2830 Lower
= APInt::getSignedMinValue(BitWidth
); Upper
++;
2831 // Check for a full-set condition.
2833 return ConstantRange(BitWidth
, /*isFullSet=*/true);
2835 case ICmpInst::ICMP_UGE
:
2836 Upper
= APInt::getMinValue(BitWidth
); // Min = Next(Max)
2837 // Check for a full-set condition.
2839 return ConstantRange(BitWidth
, /*isFullSet=*/true);
2841 case ICmpInst::ICMP_SGE
:
2842 Upper
= APInt::getSignedMinValue(BitWidth
); // Min = Next(Max)
2843 // Check for a full-set condition.
2845 return ConstantRange(BitWidth
, /*isFullSet=*/true);
2848 return ConstantRange(Lower
, Upper
);
2851 CmpInst::Predicate
CmpInst::getSwappedPredicate(Predicate pred
) {
2853 default: assert(!"Unknown cmp predicate!");
2854 case ICMP_EQ
: case ICMP_NE
:
2856 case ICMP_SGT
: return ICMP_SLT
;
2857 case ICMP_SLT
: return ICMP_SGT
;
2858 case ICMP_SGE
: return ICMP_SLE
;
2859 case ICMP_SLE
: return ICMP_SGE
;
2860 case ICMP_UGT
: return ICMP_ULT
;
2861 case ICMP_ULT
: return ICMP_UGT
;
2862 case ICMP_UGE
: return ICMP_ULE
;
2863 case ICMP_ULE
: return ICMP_UGE
;
2865 case FCMP_FALSE
: case FCMP_TRUE
:
2866 case FCMP_OEQ
: case FCMP_ONE
:
2867 case FCMP_UEQ
: case FCMP_UNE
:
2868 case FCMP_ORD
: case FCMP_UNO
:
2870 case FCMP_OGT
: return FCMP_OLT
;
2871 case FCMP_OLT
: return FCMP_OGT
;
2872 case FCMP_OGE
: return FCMP_OLE
;
2873 case FCMP_OLE
: return FCMP_OGE
;
2874 case FCMP_UGT
: return FCMP_ULT
;
2875 case FCMP_ULT
: return FCMP_UGT
;
2876 case FCMP_UGE
: return FCMP_ULE
;
2877 case FCMP_ULE
: return FCMP_UGE
;
2881 bool CmpInst::isUnsigned(unsigned short predicate
) {
2882 switch (predicate
) {
2883 default: return false;
2884 case ICmpInst::ICMP_ULT
: case ICmpInst::ICMP_ULE
: case ICmpInst::ICMP_UGT
:
2885 case ICmpInst::ICMP_UGE
: return true;
2889 bool CmpInst::isSigned(unsigned short predicate
) {
2890 switch (predicate
) {
2891 default: return false;
2892 case ICmpInst::ICMP_SLT
: case ICmpInst::ICMP_SLE
: case ICmpInst::ICMP_SGT
:
2893 case ICmpInst::ICMP_SGE
: return true;
2897 bool CmpInst::isOrdered(unsigned short predicate
) {
2898 switch (predicate
) {
2899 default: return false;
2900 case FCmpInst::FCMP_OEQ
: case FCmpInst::FCMP_ONE
: case FCmpInst::FCMP_OGT
:
2901 case FCmpInst::FCMP_OLT
: case FCmpInst::FCMP_OGE
: case FCmpInst::FCMP_OLE
:
2902 case FCmpInst::FCMP_ORD
: return true;
2906 bool CmpInst::isUnordered(unsigned short predicate
) {
2907 switch (predicate
) {
2908 default: return false;
2909 case FCmpInst::FCMP_UEQ
: case FCmpInst::FCMP_UNE
: case FCmpInst::FCMP_UGT
:
2910 case FCmpInst::FCMP_ULT
: case FCmpInst::FCMP_UGE
: case FCmpInst::FCMP_ULE
:
2911 case FCmpInst::FCMP_UNO
: return true;
2915 bool CmpInst::isTrueWhenEqual(unsigned short predicate
) {
2917 default: return false;
2918 case ICMP_EQ
: case ICMP_UGE
: case ICMP_ULE
: case ICMP_SGE
: case ICMP_SLE
:
2919 case FCMP_TRUE
: case FCMP_UEQ
: case FCMP_UGE
: case FCMP_ULE
: return true;
2923 bool CmpInst::isFalseWhenEqual(unsigned short predicate
) {
2925 case ICMP_NE
: case ICMP_UGT
: case ICMP_ULT
: case ICMP_SGT
: case ICMP_SLT
:
2926 case FCMP_FALSE
: case FCMP_ONE
: case FCMP_OGT
: case FCMP_OLT
: return true;
2927 default: return false;
2932 //===----------------------------------------------------------------------===//
2933 // SwitchInst Implementation
2934 //===----------------------------------------------------------------------===//
2936 void SwitchInst::init(Value
*Value
, BasicBlock
*Default
, unsigned NumReserved
) {
2937 assert(Value
&& Default
&& NumReserved
);
2938 ReservedSpace
= NumReserved
;
2940 OperandList
= allocHungoffUses(ReservedSpace
);
2942 OperandList
[0] = Value
;
2943 OperandList
[1] = Default
;
2946 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2947 /// switch on and a default destination. The number of additional cases can
2948 /// be specified here to make memory allocation more efficient. This
2949 /// constructor can also autoinsert before another instruction.
2950 SwitchInst::SwitchInst(Value
*Value
, BasicBlock
*Default
, unsigned NumCases
,
2951 Instruction
*InsertBefore
)
2952 : TerminatorInst(Type::getVoidTy(Value
->getContext()), Instruction::Switch
,
2953 0, 0, InsertBefore
) {
2954 init(Value
, Default
, 2+NumCases
*2);
2957 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2958 /// switch on and a default destination. The number of additional cases can
2959 /// be specified here to make memory allocation more efficient. This
2960 /// constructor also autoinserts at the end of the specified BasicBlock.
2961 SwitchInst::SwitchInst(Value
*Value
, BasicBlock
*Default
, unsigned NumCases
,
2962 BasicBlock
*InsertAtEnd
)
2963 : TerminatorInst(Type::getVoidTy(Value
->getContext()), Instruction::Switch
,
2964 0, 0, InsertAtEnd
) {
2965 init(Value
, Default
, 2+NumCases
*2);
2968 SwitchInst::SwitchInst(const SwitchInst
&SI
)
2969 : TerminatorInst(SI
.getType(), Instruction::Switch
, 0, 0) {
2970 init(SI
.getCondition(), SI
.getDefaultDest(), SI
.getNumOperands());
2971 NumOperands
= SI
.getNumOperands();
2972 Use
*OL
= OperandList
, *InOL
= SI
.OperandList
;
2973 for (unsigned i
= 2, E
= SI
.getNumOperands(); i
!= E
; i
+= 2) {
2975 OL
[i
+1] = InOL
[i
+1];
2977 SubclassOptionalData
= SI
.SubclassOptionalData
;
2980 SwitchInst::~SwitchInst() {
2985 /// addCase - Add an entry to the switch instruction...
2987 void SwitchInst::addCase(ConstantInt
*OnVal
, BasicBlock
*Dest
) {
2988 unsigned OpNo
= NumOperands
;
2989 if (OpNo
+2 > ReservedSpace
)
2990 growOperands(); // Get more space!
2991 // Initialize some new operands.
2992 assert(OpNo
+1 < ReservedSpace
&& "Growing didn't work!");
2993 NumOperands
= OpNo
+2;
2994 OperandList
[OpNo
] = OnVal
;
2995 OperandList
[OpNo
+1] = Dest
;
2998 /// removeCase - This method removes the specified successor from the switch
2999 /// instruction. Note that this cannot be used to remove the default
3000 /// destination (successor #0).
3002 void SwitchInst::removeCase(unsigned idx
) {
3003 assert(idx
!= 0 && "Cannot remove the default case!");
3004 assert(idx
*2 < getNumOperands() && "Successor index out of range!!!");
3006 unsigned NumOps
= getNumOperands();
3007 Use
*OL
= OperandList
;
3009 // Overwrite this case with the end of the list.
3010 if ((idx
+ 1) * 2 != NumOps
) {
3011 OL
[idx
* 2] = OL
[NumOps
- 2];
3012 OL
[idx
* 2 + 1] = OL
[NumOps
- 1];
3015 // Nuke the last value.
3016 OL
[NumOps
-2].set(0);
3017 OL
[NumOps
-2+1].set(0);
3018 NumOperands
= NumOps
-2;
3021 /// growOperands - grow operands - This grows the operand list in response
3022 /// to a push_back style of operation. This grows the number of ops by 3 times.
3024 void SwitchInst::growOperands() {
3025 unsigned e
= getNumOperands();
3026 unsigned NumOps
= e
*3;
3028 ReservedSpace
= NumOps
;
3029 Use
*NewOps
= allocHungoffUses(NumOps
);
3030 Use
*OldOps
= OperandList
;
3031 for (unsigned i
= 0; i
!= e
; ++i
) {
3032 NewOps
[i
] = OldOps
[i
];
3034 OperandList
= NewOps
;
3035 Use::zap(OldOps
, OldOps
+ e
, true);
3039 BasicBlock
*SwitchInst::getSuccessorV(unsigned idx
) const {
3040 return getSuccessor(idx
);
3042 unsigned SwitchInst::getNumSuccessorsV() const {
3043 return getNumSuccessors();
3045 void SwitchInst::setSuccessorV(unsigned idx
, BasicBlock
*B
) {
3046 setSuccessor(idx
, B
);
3049 //===----------------------------------------------------------------------===//
3050 // IndirectBrInst Implementation
3051 //===----------------------------------------------------------------------===//
3053 void IndirectBrInst::init(Value
*Address
, unsigned NumDests
) {
3054 assert(Address
&& Address
->getType()->isPointerTy() &&
3055 "Address of indirectbr must be a pointer");
3056 ReservedSpace
= 1+NumDests
;
3058 OperandList
= allocHungoffUses(ReservedSpace
);
3060 OperandList
[0] = Address
;
3064 /// growOperands - grow operands - This grows the operand list in response
3065 /// to a push_back style of operation. This grows the number of ops by 2 times.
3067 void IndirectBrInst::growOperands() {
3068 unsigned e
= getNumOperands();
3069 unsigned NumOps
= e
*2;
3071 ReservedSpace
= NumOps
;
3072 Use
*NewOps
= allocHungoffUses(NumOps
);
3073 Use
*OldOps
= OperandList
;
3074 for (unsigned i
= 0; i
!= e
; ++i
)
3075 NewOps
[i
] = OldOps
[i
];
3076 OperandList
= NewOps
;
3077 Use::zap(OldOps
, OldOps
+ e
, true);
3080 IndirectBrInst::IndirectBrInst(Value
*Address
, unsigned NumCases
,
3081 Instruction
*InsertBefore
)
3082 : TerminatorInst(Type::getVoidTy(Address
->getContext()),Instruction::IndirectBr
,
3083 0, 0, InsertBefore
) {
3084 init(Address
, NumCases
);
3087 IndirectBrInst::IndirectBrInst(Value
*Address
, unsigned NumCases
,
3088 BasicBlock
*InsertAtEnd
)
3089 : TerminatorInst(Type::getVoidTy(Address
->getContext()),Instruction::IndirectBr
,
3090 0, 0, InsertAtEnd
) {
3091 init(Address
, NumCases
);
3094 IndirectBrInst::IndirectBrInst(const IndirectBrInst
&IBI
)
3095 : TerminatorInst(Type::getVoidTy(IBI
.getContext()), Instruction::IndirectBr
,
3096 allocHungoffUses(IBI
.getNumOperands()),
3097 IBI
.getNumOperands()) {
3098 Use
*OL
= OperandList
, *InOL
= IBI
.OperandList
;
3099 for (unsigned i
= 0, E
= IBI
.getNumOperands(); i
!= E
; ++i
)
3101 SubclassOptionalData
= IBI
.SubclassOptionalData
;
3104 IndirectBrInst::~IndirectBrInst() {
3108 /// addDestination - Add a destination.
3110 void IndirectBrInst::addDestination(BasicBlock
*DestBB
) {
3111 unsigned OpNo
= NumOperands
;
3112 if (OpNo
+1 > ReservedSpace
)
3113 growOperands(); // Get more space!
3114 // Initialize some new operands.
3115 assert(OpNo
< ReservedSpace
&& "Growing didn't work!");
3116 NumOperands
= OpNo
+1;
3117 OperandList
[OpNo
] = DestBB
;
3120 /// removeDestination - This method removes the specified successor from the
3121 /// indirectbr instruction.
3122 void IndirectBrInst::removeDestination(unsigned idx
) {
3123 assert(idx
< getNumOperands()-1 && "Successor index out of range!");
3125 unsigned NumOps
= getNumOperands();
3126 Use
*OL
= OperandList
;
3128 // Replace this value with the last one.
3129 OL
[idx
+1] = OL
[NumOps
-1];
3131 // Nuke the last value.
3132 OL
[NumOps
-1].set(0);
3133 NumOperands
= NumOps
-1;
3136 BasicBlock
*IndirectBrInst::getSuccessorV(unsigned idx
) const {
3137 return getSuccessor(idx
);
3139 unsigned IndirectBrInst::getNumSuccessorsV() const {
3140 return getNumSuccessors();
3142 void IndirectBrInst::setSuccessorV(unsigned idx
, BasicBlock
*B
) {
3143 setSuccessor(idx
, B
);
3146 //===----------------------------------------------------------------------===//
3147 // clone_impl() implementations
3148 //===----------------------------------------------------------------------===//
3150 // Define these methods here so vtables don't get emitted into every translation
3151 // unit that uses these classes.
3153 GetElementPtrInst
*GetElementPtrInst::clone_impl() const {
3154 return new (getNumOperands()) GetElementPtrInst(*this);
3157 BinaryOperator
*BinaryOperator::clone_impl() const {
3158 return Create(getOpcode(), Op
<0>(), Op
<1>());
3161 FCmpInst
* FCmpInst::clone_impl() const {
3162 return new FCmpInst(getPredicate(), Op
<0>(), Op
<1>());
3165 ICmpInst
* ICmpInst::clone_impl() const {
3166 return new ICmpInst(getPredicate(), Op
<0>(), Op
<1>());
3169 ExtractValueInst
*ExtractValueInst::clone_impl() const {
3170 return new ExtractValueInst(*this);
3173 InsertValueInst
*InsertValueInst::clone_impl() const {
3174 return new InsertValueInst(*this);
3177 AllocaInst
*AllocaInst::clone_impl() const {
3178 return new AllocaInst(getAllocatedType(),
3179 (Value
*)getOperand(0),
3183 LoadInst
*LoadInst::clone_impl() const {
3184 return new LoadInst(getOperand(0),
3185 Twine(), isVolatile(),
3189 StoreInst
*StoreInst::clone_impl() const {
3190 return new StoreInst(getOperand(0), getOperand(1),
3191 isVolatile(), getAlignment());
3194 TruncInst
*TruncInst::clone_impl() const {
3195 return new TruncInst(getOperand(0), getType());
3198 ZExtInst
*ZExtInst::clone_impl() const {
3199 return new ZExtInst(getOperand(0), getType());
3202 SExtInst
*SExtInst::clone_impl() const {
3203 return new SExtInst(getOperand(0), getType());
3206 FPTruncInst
*FPTruncInst::clone_impl() const {
3207 return new FPTruncInst(getOperand(0), getType());
3210 FPExtInst
*FPExtInst::clone_impl() const {
3211 return new FPExtInst(getOperand(0), getType());
3214 UIToFPInst
*UIToFPInst::clone_impl() const {
3215 return new UIToFPInst(getOperand(0), getType());
3218 SIToFPInst
*SIToFPInst::clone_impl() const {
3219 return new SIToFPInst(getOperand(0), getType());
3222 FPToUIInst
*FPToUIInst::clone_impl() const {
3223 return new FPToUIInst(getOperand(0), getType());
3226 FPToSIInst
*FPToSIInst::clone_impl() const {
3227 return new FPToSIInst(getOperand(0), getType());
3230 PtrToIntInst
*PtrToIntInst::clone_impl() const {
3231 return new PtrToIntInst(getOperand(0), getType());
3234 IntToPtrInst
*IntToPtrInst::clone_impl() const {
3235 return new IntToPtrInst(getOperand(0), getType());
3238 BitCastInst
*BitCastInst::clone_impl() const {
3239 return new BitCastInst(getOperand(0), getType());
3242 CallInst
*CallInst::clone_impl() const {
3243 return new(getNumOperands()) CallInst(*this);
3246 SelectInst
*SelectInst::clone_impl() const {
3247 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
3250 VAArgInst
*VAArgInst::clone_impl() const {
3251 return new VAArgInst(getOperand(0), getType());
3254 ExtractElementInst
*ExtractElementInst::clone_impl() const {
3255 return ExtractElementInst::Create(getOperand(0), getOperand(1));
3258 InsertElementInst
*InsertElementInst::clone_impl() const {
3259 return InsertElementInst::Create(getOperand(0),
3264 ShuffleVectorInst
*ShuffleVectorInst::clone_impl() const {
3265 return new ShuffleVectorInst(getOperand(0),
3270 PHINode
*PHINode::clone_impl() const {
3271 return new PHINode(*this);
3274 ReturnInst
*ReturnInst::clone_impl() const {
3275 return new(getNumOperands()) ReturnInst(*this);
3278 BranchInst
*BranchInst::clone_impl() const {
3279 return new(getNumOperands()) BranchInst(*this);
3282 SwitchInst
*SwitchInst::clone_impl() const {
3283 return new SwitchInst(*this);
3286 IndirectBrInst
*IndirectBrInst::clone_impl() const {
3287 return new IndirectBrInst(*this);
3291 InvokeInst
*InvokeInst::clone_impl() const {
3292 return new(getNumOperands()) InvokeInst(*this);
3295 UnwindInst
*UnwindInst::clone_impl() const {
3296 LLVMContext
&Context
= getContext();
3297 return new UnwindInst(Context
);
3300 UnreachableInst
*UnreachableInst::clone_impl() const {
3301 LLVMContext
&Context
= getContext();
3302 return new UnreachableInst(Context
);