1 //===- Instructions.cpp - Implement the LLVM instructions -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements all of the non-inline methods for the LLVM instruction
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/Instructions.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/None.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/CallSite.h"
22 #include "llvm/IR/Constant.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/MDBuilder.h"
32 #include "llvm/IR/Metadata.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/Operator.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/IR/Value.h"
37 #include "llvm/Support/AtomicOrdering.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/TypeSize.h"
49 //===----------------------------------------------------------------------===//
51 //===----------------------------------------------------------------------===//
54 AllocaInst::getAllocationSizeInBits(const DataLayout
&DL
) const {
55 uint64_t Size
= DL
.getTypeAllocSizeInBits(getAllocatedType());
56 if (isArrayAllocation()) {
57 auto C
= dyn_cast
<ConstantInt
>(getArraySize());
60 Size
*= C
->getZExtValue();
65 //===----------------------------------------------------------------------===//
67 //===----------------------------------------------------------------------===//
69 User::op_iterator
CallSite::getCallee() const {
70 return cast
<CallBase
>(getInstruction())->op_end() - 1;
73 //===----------------------------------------------------------------------===//
75 //===----------------------------------------------------------------------===//
77 /// areInvalidOperands - Return a string if the specified operands are invalid
78 /// for a select operation, otherwise return null.
79 const char *SelectInst::areInvalidOperands(Value
*Op0
, Value
*Op1
, Value
*Op2
) {
80 if (Op1
->getType() != Op2
->getType())
81 return "both values to select must have same type";
83 if (Op1
->getType()->isTokenTy())
84 return "select values cannot have token type";
86 if (VectorType
*VT
= dyn_cast
<VectorType
>(Op0
->getType())) {
88 if (VT
->getElementType() != Type::getInt1Ty(Op0
->getContext()))
89 return "vector select condition element type must be i1";
90 VectorType
*ET
= dyn_cast
<VectorType
>(Op1
->getType());
92 return "selected values for vector select must be vectors";
93 if (ET
->getNumElements() != VT
->getNumElements())
94 return "vector select requires selected vectors to have "
95 "the same vector length as select condition";
96 } else if (Op0
->getType() != Type::getInt1Ty(Op0
->getContext())) {
97 return "select condition must be i1 or <n x i1>";
102 //===----------------------------------------------------------------------===//
104 //===----------------------------------------------------------------------===//
106 PHINode::PHINode(const PHINode
&PN
)
107 : Instruction(PN
.getType(), Instruction::PHI
, nullptr, PN
.getNumOperands()),
108 ReservedSpace(PN
.getNumOperands()) {
109 allocHungoffUses(PN
.getNumOperands());
110 std::copy(PN
.op_begin(), PN
.op_end(), op_begin());
111 std::copy(PN
.block_begin(), PN
.block_end(), block_begin());
112 SubclassOptionalData
= PN
.SubclassOptionalData
;
115 // removeIncomingValue - Remove an incoming value. This is useful if a
116 // predecessor basic block is deleted.
117 Value
*PHINode::removeIncomingValue(unsigned Idx
, bool DeletePHIIfEmpty
) {
118 Value
*Removed
= getIncomingValue(Idx
);
120 // Move everything after this operand down.
122 // FIXME: we could just swap with the end of the list, then erase. However,
123 // clients might not expect this to happen. The code as it is thrashes the
124 // use/def lists, which is kinda lame.
125 std::copy(op_begin() + Idx
+ 1, op_end(), op_begin() + Idx
);
126 std::copy(block_begin() + Idx
+ 1, block_end(), block_begin() + Idx
);
128 // Nuke the last value.
129 Op
<-1>().set(nullptr);
130 setNumHungOffUseOperands(getNumOperands() - 1);
132 // If the PHI node is dead, because it has zero entries, nuke it now.
133 if (getNumOperands() == 0 && DeletePHIIfEmpty
) {
134 // If anyone is using this PHI, make them use a dummy value instead...
135 replaceAllUsesWith(UndefValue::get(getType()));
141 /// growOperands - grow operands - This grows the operand list in response
142 /// to a push_back style of operation. This grows the number of ops by 1.5
145 void PHINode::growOperands() {
146 unsigned e
= getNumOperands();
147 unsigned NumOps
= e
+ e
/ 2;
148 if (NumOps
< 2) NumOps
= 2; // 2 op PHI nodes are VERY common.
150 ReservedSpace
= NumOps
;
151 growHungoffUses(ReservedSpace
, /* IsPhi */ true);
154 /// hasConstantValue - If the specified PHI node always merges together the same
155 /// value, return the value, otherwise return null.
156 Value
*PHINode::hasConstantValue() const {
157 // Exploit the fact that phi nodes always have at least one entry.
158 Value
*ConstantValue
= getIncomingValue(0);
159 for (unsigned i
= 1, e
= getNumIncomingValues(); i
!= e
; ++i
)
160 if (getIncomingValue(i
) != ConstantValue
&& getIncomingValue(i
) != this) {
161 if (ConstantValue
!= this)
162 return nullptr; // Incoming values not all the same.
163 // The case where the first value is this PHI.
164 ConstantValue
= getIncomingValue(i
);
166 if (ConstantValue
== this)
167 return UndefValue::get(getType());
168 return ConstantValue
;
171 /// hasConstantOrUndefValue - Whether the specified PHI node always merges
172 /// together the same value, assuming that undefs result in the same value as
174 /// Unlike \ref hasConstantValue, this does not return a value because the
175 /// unique non-undef incoming value need not dominate the PHI node.
176 bool PHINode::hasConstantOrUndefValue() const {
177 Value
*ConstantValue
= nullptr;
178 for (unsigned i
= 0, e
= getNumIncomingValues(); i
!= e
; ++i
) {
179 Value
*Incoming
= getIncomingValue(i
);
180 if (Incoming
!= this && !isa
<UndefValue
>(Incoming
)) {
181 if (ConstantValue
&& ConstantValue
!= Incoming
)
183 ConstantValue
= Incoming
;
189 //===----------------------------------------------------------------------===//
190 // LandingPadInst Implementation
191 //===----------------------------------------------------------------------===//
193 LandingPadInst::LandingPadInst(Type
*RetTy
, unsigned NumReservedValues
,
194 const Twine
&NameStr
, Instruction
*InsertBefore
)
195 : Instruction(RetTy
, Instruction::LandingPad
, nullptr, 0, InsertBefore
) {
196 init(NumReservedValues
, NameStr
);
199 LandingPadInst::LandingPadInst(Type
*RetTy
, unsigned NumReservedValues
,
200 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
)
201 : Instruction(RetTy
, Instruction::LandingPad
, nullptr, 0, InsertAtEnd
) {
202 init(NumReservedValues
, NameStr
);
205 LandingPadInst::LandingPadInst(const LandingPadInst
&LP
)
206 : Instruction(LP
.getType(), Instruction::LandingPad
, nullptr,
207 LP
.getNumOperands()),
208 ReservedSpace(LP
.getNumOperands()) {
209 allocHungoffUses(LP
.getNumOperands());
210 Use
*OL
= getOperandList();
211 const Use
*InOL
= LP
.getOperandList();
212 for (unsigned I
= 0, E
= ReservedSpace
; I
!= E
; ++I
)
215 setCleanup(LP
.isCleanup());
218 LandingPadInst
*LandingPadInst::Create(Type
*RetTy
, unsigned NumReservedClauses
,
219 const Twine
&NameStr
,
220 Instruction
*InsertBefore
) {
221 return new LandingPadInst(RetTy
, NumReservedClauses
, NameStr
, InsertBefore
);
224 LandingPadInst
*LandingPadInst::Create(Type
*RetTy
, unsigned NumReservedClauses
,
225 const Twine
&NameStr
,
226 BasicBlock
*InsertAtEnd
) {
227 return new LandingPadInst(RetTy
, NumReservedClauses
, NameStr
, InsertAtEnd
);
230 void LandingPadInst::init(unsigned NumReservedValues
, const Twine
&NameStr
) {
231 ReservedSpace
= NumReservedValues
;
232 setNumHungOffUseOperands(0);
233 allocHungoffUses(ReservedSpace
);
238 /// growOperands - grow operands - This grows the operand list in response to a
239 /// push_back style of operation. This grows the number of ops by 2 times.
240 void LandingPadInst::growOperands(unsigned Size
) {
241 unsigned e
= getNumOperands();
242 if (ReservedSpace
>= e
+ Size
) return;
243 ReservedSpace
= (std::max(e
, 1U) + Size
/ 2) * 2;
244 growHungoffUses(ReservedSpace
);
247 void LandingPadInst::addClause(Constant
*Val
) {
248 unsigned OpNo
= getNumOperands();
250 assert(OpNo
< ReservedSpace
&& "Growing didn't work!");
251 setNumHungOffUseOperands(getNumOperands() + 1);
252 getOperandList()[OpNo
] = Val
;
255 //===----------------------------------------------------------------------===//
256 // CallBase Implementation
257 //===----------------------------------------------------------------------===//
259 Function
*CallBase::getCaller() { return getParent()->getParent(); }
261 unsigned CallBase::getNumSubclassExtraOperandsDynamic() const {
262 assert(getOpcode() == Instruction::CallBr
&& "Unexpected opcode!");
263 return cast
<CallBrInst
>(this)->getNumIndirectDests() + 1;
266 bool CallBase::isIndirectCall() const {
267 const Value
*V
= getCalledValue();
268 if (isa
<Function
>(V
) || isa
<Constant
>(V
))
270 if (const CallInst
*CI
= dyn_cast
<CallInst
>(this))
271 if (CI
->isInlineAsm())
276 /// Tests if this call site must be tail call optimized. Only a CallInst can
277 /// be tail call optimized.
278 bool CallBase::isMustTailCall() const {
279 if (auto *CI
= dyn_cast
<CallInst
>(this))
280 return CI
->isMustTailCall();
284 /// Tests if this call site is marked as a tail call.
285 bool CallBase::isTailCall() const {
286 if (auto *CI
= dyn_cast
<CallInst
>(this))
287 return CI
->isTailCall();
291 Intrinsic::ID
CallBase::getIntrinsicID() const {
292 if (auto *F
= getCalledFunction())
293 return F
->getIntrinsicID();
294 return Intrinsic::not_intrinsic
;
297 bool CallBase::isReturnNonNull() const {
298 if (hasRetAttr(Attribute::NonNull
))
301 if (getDereferenceableBytes(AttributeList::ReturnIndex
) > 0 &&
302 !NullPointerIsDefined(getCaller(),
303 getType()->getPointerAddressSpace()))
309 Value
*CallBase::getReturnedArgOperand() const {
312 if (Attrs
.hasAttrSomewhere(Attribute::Returned
, &Index
) && Index
)
313 return getArgOperand(Index
- AttributeList::FirstArgIndex
);
314 if (const Function
*F
= getCalledFunction())
315 if (F
->getAttributes().hasAttrSomewhere(Attribute::Returned
, &Index
) &&
317 return getArgOperand(Index
- AttributeList::FirstArgIndex
);
322 bool CallBase::hasRetAttr(Attribute::AttrKind Kind
) const {
323 if (Attrs
.hasAttribute(AttributeList::ReturnIndex
, Kind
))
326 // Look at the callee, if available.
327 if (const Function
*F
= getCalledFunction())
328 return F
->getAttributes().hasAttribute(AttributeList::ReturnIndex
, Kind
);
332 /// Determine whether the argument or parameter has the given attribute.
333 bool CallBase::paramHasAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) const {
334 assert(ArgNo
< getNumArgOperands() && "Param index out of bounds!");
336 if (Attrs
.hasParamAttribute(ArgNo
, Kind
))
338 if (const Function
*F
= getCalledFunction())
339 return F
->getAttributes().hasParamAttribute(ArgNo
, Kind
);
343 bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind
) const {
344 if (const Function
*F
= getCalledFunction())
345 return F
->getAttributes().hasAttribute(AttributeList::FunctionIndex
, Kind
);
349 bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind
) const {
350 if (const Function
*F
= getCalledFunction())
351 return F
->getAttributes().hasAttribute(AttributeList::FunctionIndex
, Kind
);
355 CallBase::op_iterator
356 CallBase::populateBundleOperandInfos(ArrayRef
<OperandBundleDef
> Bundles
,
357 const unsigned BeginIndex
) {
358 auto It
= op_begin() + BeginIndex
;
359 for (auto &B
: Bundles
)
360 It
= std::copy(B
.input_begin(), B
.input_end(), It
);
362 auto *ContextImpl
= getContext().pImpl
;
363 auto BI
= Bundles
.begin();
364 unsigned CurrentIndex
= BeginIndex
;
366 for (auto &BOI
: bundle_op_infos()) {
367 assert(BI
!= Bundles
.end() && "Incorrect allocation?");
369 BOI
.Tag
= ContextImpl
->getOrInsertBundleTag(BI
->getTag());
370 BOI
.Begin
= CurrentIndex
;
371 BOI
.End
= CurrentIndex
+ BI
->input_size();
372 CurrentIndex
= BOI
.End
;
376 assert(BI
== Bundles
.end() && "Incorrect allocation?");
381 //===----------------------------------------------------------------------===//
382 // CallInst Implementation
383 //===----------------------------------------------------------------------===//
385 void CallInst::init(FunctionType
*FTy
, Value
*Func
, ArrayRef
<Value
*> Args
,
386 ArrayRef
<OperandBundleDef
> Bundles
, const Twine
&NameStr
) {
388 assert(getNumOperands() == Args
.size() + CountBundleInputs(Bundles
) + 1 &&
389 "NumOperands not set up?");
390 setCalledOperand(Func
);
393 assert((Args
.size() == FTy
->getNumParams() ||
394 (FTy
->isVarArg() && Args
.size() > FTy
->getNumParams())) &&
395 "Calling a function with bad signature!");
397 for (unsigned i
= 0; i
!= Args
.size(); ++i
)
398 assert((i
>= FTy
->getNumParams() ||
399 FTy
->getParamType(i
) == Args
[i
]->getType()) &&
400 "Calling a function with a bad signature!");
403 llvm::copy(Args
, op_begin());
405 auto It
= populateBundleOperandInfos(Bundles
, Args
.size());
407 assert(It
+ 1 == op_end() && "Should add up!");
412 void CallInst::init(FunctionType
*FTy
, Value
*Func
, const Twine
&NameStr
) {
414 assert(getNumOperands() == 1 && "NumOperands not set up?");
415 setCalledOperand(Func
);
417 assert(FTy
->getNumParams() == 0 && "Calling a function with bad signature");
422 CallInst::CallInst(FunctionType
*Ty
, Value
*Func
, const Twine
&Name
,
423 Instruction
*InsertBefore
)
424 : CallBase(Ty
->getReturnType(), Instruction::Call
,
425 OperandTraits
<CallBase
>::op_end(this) - 1, 1, InsertBefore
) {
426 init(Ty
, Func
, Name
);
429 CallInst::CallInst(FunctionType
*Ty
, Value
*Func
, const Twine
&Name
,
430 BasicBlock
*InsertAtEnd
)
431 : CallBase(Ty
->getReturnType(), Instruction::Call
,
432 OperandTraits
<CallBase
>::op_end(this) - 1, 1, InsertAtEnd
) {
433 init(Ty
, Func
, Name
);
436 CallInst::CallInst(const CallInst
&CI
)
437 : CallBase(CI
.Attrs
, CI
.FTy
, CI
.getType(), Instruction::Call
,
438 OperandTraits
<CallBase
>::op_end(this) - CI
.getNumOperands(),
439 CI
.getNumOperands()) {
440 setTailCallKind(CI
.getTailCallKind());
441 setCallingConv(CI
.getCallingConv());
443 std::copy(CI
.op_begin(), CI
.op_end(), op_begin());
444 std::copy(CI
.bundle_op_info_begin(), CI
.bundle_op_info_end(),
445 bundle_op_info_begin());
446 SubclassOptionalData
= CI
.SubclassOptionalData
;
449 CallInst
*CallInst::Create(CallInst
*CI
, ArrayRef
<OperandBundleDef
> OpB
,
450 Instruction
*InsertPt
) {
451 std::vector
<Value
*> Args(CI
->arg_begin(), CI
->arg_end());
453 auto *NewCI
= CallInst::Create(CI
->getFunctionType(), CI
->getCalledValue(),
454 Args
, OpB
, CI
->getName(), InsertPt
);
455 NewCI
->setTailCallKind(CI
->getTailCallKind());
456 NewCI
->setCallingConv(CI
->getCallingConv());
457 NewCI
->SubclassOptionalData
= CI
->SubclassOptionalData
;
458 NewCI
->setAttributes(CI
->getAttributes());
459 NewCI
->setDebugLoc(CI
->getDebugLoc());
463 // Update profile weight for call instruction by scaling it using the ratio
464 // of S/T. The meaning of "branch_weights" meta data for call instruction is
465 // transfered to represent call count.
466 void CallInst::updateProfWeight(uint64_t S
, uint64_t T
) {
467 auto *ProfileData
= getMetadata(LLVMContext::MD_prof
);
468 if (ProfileData
== nullptr)
471 auto *ProfDataName
= dyn_cast
<MDString
>(ProfileData
->getOperand(0));
472 if (!ProfDataName
|| (!ProfDataName
->getString().equals("branch_weights") &&
473 !ProfDataName
->getString().equals("VP")))
477 LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in "
478 "div by 0. Ignoring. Likely the function "
479 << getParent()->getParent()->getName()
480 << " has 0 entry count, and contains call instructions "
481 "with non-zero prof info.");
485 MDBuilder
MDB(getContext());
486 SmallVector
<Metadata
*, 3> Vals
;
487 Vals
.push_back(ProfileData
->getOperand(0));
488 APInt
APS(128, S
), APT(128, T
);
489 if (ProfDataName
->getString().equals("branch_weights") &&
490 ProfileData
->getNumOperands() > 0) {
491 // Using APInt::div may be expensive, but most cases should fit 64 bits.
492 APInt
Val(128, mdconst::dyn_extract
<ConstantInt
>(ProfileData
->getOperand(1))
496 Vals
.push_back(MDB
.createConstant(ConstantInt::get(
497 Type::getInt64Ty(getContext()), Val
.udiv(APT
).getLimitedValue())));
498 } else if (ProfDataName
->getString().equals("VP"))
499 for (unsigned i
= 1; i
< ProfileData
->getNumOperands(); i
+= 2) {
500 // The first value is the key of the value profile, which will not change.
501 Vals
.push_back(ProfileData
->getOperand(i
));
502 // Using APInt::div may be expensive, but most cases should fit 64 bits.
504 mdconst::dyn_extract
<ConstantInt
>(ProfileData
->getOperand(i
+ 1))
508 Vals
.push_back(MDB
.createConstant(
509 ConstantInt::get(Type::getInt64Ty(getContext()),
510 Val
.udiv(APT
).getLimitedValue())));
512 setMetadata(LLVMContext::MD_prof
, MDNode::get(getContext(), Vals
));
515 /// IsConstantOne - Return true only if val is constant int 1
516 static bool IsConstantOne(Value
*val
) {
517 assert(val
&& "IsConstantOne does not work with nullptr val");
518 const ConstantInt
*CVal
= dyn_cast
<ConstantInt
>(val
);
519 return CVal
&& CVal
->isOne();
522 static Instruction
*createMalloc(Instruction
*InsertBefore
,
523 BasicBlock
*InsertAtEnd
, Type
*IntPtrTy
,
524 Type
*AllocTy
, Value
*AllocSize
,
526 ArrayRef
<OperandBundleDef
> OpB
,
527 Function
*MallocF
, const Twine
&Name
) {
528 assert(((!InsertBefore
&& InsertAtEnd
) || (InsertBefore
&& !InsertAtEnd
)) &&
529 "createMalloc needs either InsertBefore or InsertAtEnd");
531 // malloc(type) becomes:
532 // bitcast (i8* malloc(typeSize)) to type*
533 // malloc(type, arraySize) becomes:
534 // bitcast (i8* malloc(typeSize*arraySize)) to type*
536 ArraySize
= ConstantInt::get(IntPtrTy
, 1);
537 else if (ArraySize
->getType() != IntPtrTy
) {
539 ArraySize
= CastInst::CreateIntegerCast(ArraySize
, IntPtrTy
, false,
542 ArraySize
= CastInst::CreateIntegerCast(ArraySize
, IntPtrTy
, false,
546 if (!IsConstantOne(ArraySize
)) {
547 if (IsConstantOne(AllocSize
)) {
548 AllocSize
= ArraySize
; // Operand * 1 = Operand
549 } else if (Constant
*CO
= dyn_cast
<Constant
>(ArraySize
)) {
550 Constant
*Scale
= ConstantExpr::getIntegerCast(CO
, IntPtrTy
,
552 // Malloc arg is constant product of type size and array size
553 AllocSize
= ConstantExpr::getMul(Scale
, cast
<Constant
>(AllocSize
));
555 // Multiply type size by the array size...
557 AllocSize
= BinaryOperator::CreateMul(ArraySize
, AllocSize
,
558 "mallocsize", InsertBefore
);
560 AllocSize
= BinaryOperator::CreateMul(ArraySize
, AllocSize
,
561 "mallocsize", InsertAtEnd
);
565 assert(AllocSize
->getType() == IntPtrTy
&& "malloc arg is wrong size");
566 // Create the call to Malloc.
567 BasicBlock
*BB
= InsertBefore
? InsertBefore
->getParent() : InsertAtEnd
;
568 Module
*M
= BB
->getParent()->getParent();
569 Type
*BPTy
= Type::getInt8PtrTy(BB
->getContext());
570 FunctionCallee MallocFunc
= MallocF
;
572 // prototype malloc as "void *malloc(size_t)"
573 MallocFunc
= M
->getOrInsertFunction("malloc", BPTy
, IntPtrTy
);
574 PointerType
*AllocPtrType
= PointerType::getUnqual(AllocTy
);
575 CallInst
*MCall
= nullptr;
576 Instruction
*Result
= nullptr;
578 MCall
= CallInst::Create(MallocFunc
, AllocSize
, OpB
, "malloccall",
581 if (Result
->getType() != AllocPtrType
)
582 // Create a cast instruction to convert to the right type...
583 Result
= new BitCastInst(MCall
, AllocPtrType
, Name
, InsertBefore
);
585 MCall
= CallInst::Create(MallocFunc
, AllocSize
, OpB
, "malloccall");
587 if (Result
->getType() != AllocPtrType
) {
588 InsertAtEnd
->getInstList().push_back(MCall
);
589 // Create a cast instruction to convert to the right type...
590 Result
= new BitCastInst(MCall
, AllocPtrType
, Name
);
593 MCall
->setTailCall();
594 if (Function
*F
= dyn_cast
<Function
>(MallocFunc
.getCallee())) {
595 MCall
->setCallingConv(F
->getCallingConv());
596 if (!F
->returnDoesNotAlias())
597 F
->setReturnDoesNotAlias();
599 assert(!MCall
->getType()->isVoidTy() && "Malloc has void return type");
604 /// CreateMalloc - Generate the IR for a call to malloc:
605 /// 1. Compute the malloc call's argument as the specified type's size,
606 /// possibly multiplied by the array size if the array size is not
608 /// 2. Call malloc with that argument.
609 /// 3. Bitcast the result of the malloc call to the specified type.
610 Instruction
*CallInst::CreateMalloc(Instruction
*InsertBefore
,
611 Type
*IntPtrTy
, Type
*AllocTy
,
612 Value
*AllocSize
, Value
*ArraySize
,
615 return createMalloc(InsertBefore
, nullptr, IntPtrTy
, AllocTy
, AllocSize
,
616 ArraySize
, None
, MallocF
, Name
);
618 Instruction
*CallInst::CreateMalloc(Instruction
*InsertBefore
,
619 Type
*IntPtrTy
, Type
*AllocTy
,
620 Value
*AllocSize
, Value
*ArraySize
,
621 ArrayRef
<OperandBundleDef
> OpB
,
624 return createMalloc(InsertBefore
, nullptr, IntPtrTy
, AllocTy
, AllocSize
,
625 ArraySize
, OpB
, MallocF
, Name
);
628 /// CreateMalloc - Generate the IR for a call to malloc:
629 /// 1. Compute the malloc call's argument as the specified type's size,
630 /// possibly multiplied by the array size if the array size is not
632 /// 2. Call malloc with that argument.
633 /// 3. Bitcast the result of the malloc call to the specified type.
634 /// Note: This function does not add the bitcast to the basic block, that is the
635 /// responsibility of the caller.
636 Instruction
*CallInst::CreateMalloc(BasicBlock
*InsertAtEnd
,
637 Type
*IntPtrTy
, Type
*AllocTy
,
638 Value
*AllocSize
, Value
*ArraySize
,
639 Function
*MallocF
, const Twine
&Name
) {
640 return createMalloc(nullptr, InsertAtEnd
, IntPtrTy
, AllocTy
, AllocSize
,
641 ArraySize
, None
, MallocF
, Name
);
643 Instruction
*CallInst::CreateMalloc(BasicBlock
*InsertAtEnd
,
644 Type
*IntPtrTy
, Type
*AllocTy
,
645 Value
*AllocSize
, Value
*ArraySize
,
646 ArrayRef
<OperandBundleDef
> OpB
,
647 Function
*MallocF
, const Twine
&Name
) {
648 return createMalloc(nullptr, InsertAtEnd
, IntPtrTy
, AllocTy
, AllocSize
,
649 ArraySize
, OpB
, MallocF
, Name
);
652 static Instruction
*createFree(Value
*Source
,
653 ArrayRef
<OperandBundleDef
> Bundles
,
654 Instruction
*InsertBefore
,
655 BasicBlock
*InsertAtEnd
) {
656 assert(((!InsertBefore
&& InsertAtEnd
) || (InsertBefore
&& !InsertAtEnd
)) &&
657 "createFree needs either InsertBefore or InsertAtEnd");
658 assert(Source
->getType()->isPointerTy() &&
659 "Can not free something of nonpointer type!");
661 BasicBlock
*BB
= InsertBefore
? InsertBefore
->getParent() : InsertAtEnd
;
662 Module
*M
= BB
->getParent()->getParent();
664 Type
*VoidTy
= Type::getVoidTy(M
->getContext());
665 Type
*IntPtrTy
= Type::getInt8PtrTy(M
->getContext());
666 // prototype free as "void free(void*)"
667 FunctionCallee FreeFunc
= M
->getOrInsertFunction("free", VoidTy
, IntPtrTy
);
668 CallInst
*Result
= nullptr;
669 Value
*PtrCast
= Source
;
671 if (Source
->getType() != IntPtrTy
)
672 PtrCast
= new BitCastInst(Source
, IntPtrTy
, "", InsertBefore
);
673 Result
= CallInst::Create(FreeFunc
, PtrCast
, Bundles
, "", InsertBefore
);
675 if (Source
->getType() != IntPtrTy
)
676 PtrCast
= new BitCastInst(Source
, IntPtrTy
, "", InsertAtEnd
);
677 Result
= CallInst::Create(FreeFunc
, PtrCast
, Bundles
, "");
679 Result
->setTailCall();
680 if (Function
*F
= dyn_cast
<Function
>(FreeFunc
.getCallee()))
681 Result
->setCallingConv(F
->getCallingConv());
686 /// CreateFree - Generate the IR for a call to the builtin free function.
687 Instruction
*CallInst::CreateFree(Value
*Source
, Instruction
*InsertBefore
) {
688 return createFree(Source
, None
, InsertBefore
, nullptr);
690 Instruction
*CallInst::CreateFree(Value
*Source
,
691 ArrayRef
<OperandBundleDef
> Bundles
,
692 Instruction
*InsertBefore
) {
693 return createFree(Source
, Bundles
, InsertBefore
, nullptr);
696 /// CreateFree - Generate the IR for a call to the builtin free function.
697 /// Note: This function does not add the call to the basic block, that is the
698 /// responsibility of the caller.
699 Instruction
*CallInst::CreateFree(Value
*Source
, BasicBlock
*InsertAtEnd
) {
700 Instruction
*FreeCall
= createFree(Source
, None
, nullptr, InsertAtEnd
);
701 assert(FreeCall
&& "CreateFree did not create a CallInst");
704 Instruction
*CallInst::CreateFree(Value
*Source
,
705 ArrayRef
<OperandBundleDef
> Bundles
,
706 BasicBlock
*InsertAtEnd
) {
707 Instruction
*FreeCall
= createFree(Source
, Bundles
, nullptr, InsertAtEnd
);
708 assert(FreeCall
&& "CreateFree did not create a CallInst");
712 //===----------------------------------------------------------------------===//
713 // InvokeInst Implementation
714 //===----------------------------------------------------------------------===//
716 void InvokeInst::init(FunctionType
*FTy
, Value
*Fn
, BasicBlock
*IfNormal
,
717 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
718 ArrayRef
<OperandBundleDef
> Bundles
,
719 const Twine
&NameStr
) {
722 assert((int)getNumOperands() ==
723 ComputeNumOperands(Args
.size(), CountBundleInputs(Bundles
)) &&
724 "NumOperands not set up?");
725 setNormalDest(IfNormal
);
726 setUnwindDest(IfException
);
727 setCalledOperand(Fn
);
730 assert(((Args
.size() == FTy
->getNumParams()) ||
731 (FTy
->isVarArg() && Args
.size() > FTy
->getNumParams())) &&
732 "Invoking a function with bad signature");
734 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; i
++)
735 assert((i
>= FTy
->getNumParams() ||
736 FTy
->getParamType(i
) == Args
[i
]->getType()) &&
737 "Invoking a function with a bad signature!");
740 llvm::copy(Args
, op_begin());
742 auto It
= populateBundleOperandInfos(Bundles
, Args
.size());
744 assert(It
+ 3 == op_end() && "Should add up!");
749 InvokeInst::InvokeInst(const InvokeInst
&II
)
750 : CallBase(II
.Attrs
, II
.FTy
, II
.getType(), Instruction::Invoke
,
751 OperandTraits
<CallBase
>::op_end(this) - II
.getNumOperands(),
752 II
.getNumOperands()) {
753 setCallingConv(II
.getCallingConv());
754 std::copy(II
.op_begin(), II
.op_end(), op_begin());
755 std::copy(II
.bundle_op_info_begin(), II
.bundle_op_info_end(),
756 bundle_op_info_begin());
757 SubclassOptionalData
= II
.SubclassOptionalData
;
760 InvokeInst
*InvokeInst::Create(InvokeInst
*II
, ArrayRef
<OperandBundleDef
> OpB
,
761 Instruction
*InsertPt
) {
762 std::vector
<Value
*> Args(II
->arg_begin(), II
->arg_end());
764 auto *NewII
= InvokeInst::Create(II
->getFunctionType(), II
->getCalledValue(),
765 II
->getNormalDest(), II
->getUnwindDest(),
766 Args
, OpB
, II
->getName(), InsertPt
);
767 NewII
->setCallingConv(II
->getCallingConv());
768 NewII
->SubclassOptionalData
= II
->SubclassOptionalData
;
769 NewII
->setAttributes(II
->getAttributes());
770 NewII
->setDebugLoc(II
->getDebugLoc());
775 LandingPadInst
*InvokeInst::getLandingPadInst() const {
776 return cast
<LandingPadInst
>(getUnwindDest()->getFirstNonPHI());
779 //===----------------------------------------------------------------------===//
780 // CallBrInst Implementation
781 //===----------------------------------------------------------------------===//
783 void CallBrInst::init(FunctionType
*FTy
, Value
*Fn
, BasicBlock
*Fallthrough
,
784 ArrayRef
<BasicBlock
*> IndirectDests
,
785 ArrayRef
<Value
*> Args
,
786 ArrayRef
<OperandBundleDef
> Bundles
,
787 const Twine
&NameStr
) {
790 assert((int)getNumOperands() ==
791 ComputeNumOperands(Args
.size(), IndirectDests
.size(),
792 CountBundleInputs(Bundles
)) &&
793 "NumOperands not set up?");
794 NumIndirectDests
= IndirectDests
.size();
795 setDefaultDest(Fallthrough
);
796 for (unsigned i
= 0; i
!= NumIndirectDests
; ++i
)
797 setIndirectDest(i
, IndirectDests
[i
]);
798 setCalledOperand(Fn
);
801 assert(((Args
.size() == FTy
->getNumParams()) ||
802 (FTy
->isVarArg() && Args
.size() > FTy
->getNumParams())) &&
803 "Calling a function with bad signature");
805 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; i
++)
806 assert((i
>= FTy
->getNumParams() ||
807 FTy
->getParamType(i
) == Args
[i
]->getType()) &&
808 "Calling a function with a bad signature!");
811 std::copy(Args
.begin(), Args
.end(), op_begin());
813 auto It
= populateBundleOperandInfos(Bundles
, Args
.size());
815 assert(It
+ 2 + IndirectDests
.size() == op_end() && "Should add up!");
820 void CallBrInst::updateArgBlockAddresses(unsigned i
, BasicBlock
*B
) {
821 assert(getNumIndirectDests() > i
&& "IndirectDest # out of range for callbr");
822 if (BasicBlock
*OldBB
= getIndirectDest(i
)) {
823 BlockAddress
*Old
= BlockAddress::get(OldBB
);
824 BlockAddress
*New
= BlockAddress::get(B
);
825 for (unsigned ArgNo
= 0, e
= getNumArgOperands(); ArgNo
!= e
; ++ArgNo
)
826 if (dyn_cast
<BlockAddress
>(getArgOperand(ArgNo
)) == Old
)
827 setArgOperand(ArgNo
, New
);
831 CallBrInst::CallBrInst(const CallBrInst
&CBI
)
832 : CallBase(CBI
.Attrs
, CBI
.FTy
, CBI
.getType(), Instruction::CallBr
,
833 OperandTraits
<CallBase
>::op_end(this) - CBI
.getNumOperands(),
834 CBI
.getNumOperands()) {
835 setCallingConv(CBI
.getCallingConv());
836 std::copy(CBI
.op_begin(), CBI
.op_end(), op_begin());
837 std::copy(CBI
.bundle_op_info_begin(), CBI
.bundle_op_info_end(),
838 bundle_op_info_begin());
839 SubclassOptionalData
= CBI
.SubclassOptionalData
;
840 NumIndirectDests
= CBI
.NumIndirectDests
;
843 CallBrInst
*CallBrInst::Create(CallBrInst
*CBI
, ArrayRef
<OperandBundleDef
> OpB
,
844 Instruction
*InsertPt
) {
845 std::vector
<Value
*> Args(CBI
->arg_begin(), CBI
->arg_end());
847 auto *NewCBI
= CallBrInst::Create(CBI
->getFunctionType(),
848 CBI
->getCalledValue(),
849 CBI
->getDefaultDest(),
850 CBI
->getIndirectDests(),
851 Args
, OpB
, CBI
->getName(), InsertPt
);
852 NewCBI
->setCallingConv(CBI
->getCallingConv());
853 NewCBI
->SubclassOptionalData
= CBI
->SubclassOptionalData
;
854 NewCBI
->setAttributes(CBI
->getAttributes());
855 NewCBI
->setDebugLoc(CBI
->getDebugLoc());
856 NewCBI
->NumIndirectDests
= CBI
->NumIndirectDests
;
860 //===----------------------------------------------------------------------===//
861 // ReturnInst Implementation
862 //===----------------------------------------------------------------------===//
864 ReturnInst::ReturnInst(const ReturnInst
&RI
)
865 : Instruction(Type::getVoidTy(RI
.getContext()), Instruction::Ret
,
866 OperandTraits
<ReturnInst
>::op_end(this) - RI
.getNumOperands(),
867 RI
.getNumOperands()) {
868 if (RI
.getNumOperands())
869 Op
<0>() = RI
.Op
<0>();
870 SubclassOptionalData
= RI
.SubclassOptionalData
;
873 ReturnInst::ReturnInst(LLVMContext
&C
, Value
*retVal
, Instruction
*InsertBefore
)
874 : Instruction(Type::getVoidTy(C
), Instruction::Ret
,
875 OperandTraits
<ReturnInst
>::op_end(this) - !!retVal
, !!retVal
,
881 ReturnInst::ReturnInst(LLVMContext
&C
, Value
*retVal
, BasicBlock
*InsertAtEnd
)
882 : Instruction(Type::getVoidTy(C
), Instruction::Ret
,
883 OperandTraits
<ReturnInst
>::op_end(this) - !!retVal
, !!retVal
,
889 ReturnInst::ReturnInst(LLVMContext
&Context
, BasicBlock
*InsertAtEnd
)
890 : Instruction(Type::getVoidTy(Context
), Instruction::Ret
,
891 OperandTraits
<ReturnInst
>::op_end(this), 0, InsertAtEnd
) {}
893 //===----------------------------------------------------------------------===//
894 // ResumeInst Implementation
895 //===----------------------------------------------------------------------===//
897 ResumeInst::ResumeInst(const ResumeInst
&RI
)
898 : Instruction(Type::getVoidTy(RI
.getContext()), Instruction::Resume
,
899 OperandTraits
<ResumeInst
>::op_begin(this), 1) {
900 Op
<0>() = RI
.Op
<0>();
903 ResumeInst::ResumeInst(Value
*Exn
, Instruction
*InsertBefore
)
904 : Instruction(Type::getVoidTy(Exn
->getContext()), Instruction::Resume
,
905 OperandTraits
<ResumeInst
>::op_begin(this), 1, InsertBefore
) {
909 ResumeInst::ResumeInst(Value
*Exn
, BasicBlock
*InsertAtEnd
)
910 : Instruction(Type::getVoidTy(Exn
->getContext()), Instruction::Resume
,
911 OperandTraits
<ResumeInst
>::op_begin(this), 1, InsertAtEnd
) {
915 //===----------------------------------------------------------------------===//
916 // CleanupReturnInst Implementation
917 //===----------------------------------------------------------------------===//
919 CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst
&CRI
)
920 : Instruction(CRI
.getType(), Instruction::CleanupRet
,
921 OperandTraits
<CleanupReturnInst
>::op_end(this) -
922 CRI
.getNumOperands(),
923 CRI
.getNumOperands()) {
924 setInstructionSubclassData(CRI
.getSubclassDataFromInstruction());
925 Op
<0>() = CRI
.Op
<0>();
926 if (CRI
.hasUnwindDest())
927 Op
<1>() = CRI
.Op
<1>();
930 void CleanupReturnInst::init(Value
*CleanupPad
, BasicBlock
*UnwindBB
) {
932 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
934 Op
<0>() = CleanupPad
;
939 CleanupReturnInst::CleanupReturnInst(Value
*CleanupPad
, BasicBlock
*UnwindBB
,
940 unsigned Values
, Instruction
*InsertBefore
)
941 : Instruction(Type::getVoidTy(CleanupPad
->getContext()),
942 Instruction::CleanupRet
,
943 OperandTraits
<CleanupReturnInst
>::op_end(this) - Values
,
944 Values
, InsertBefore
) {
945 init(CleanupPad
, UnwindBB
);
948 CleanupReturnInst::CleanupReturnInst(Value
*CleanupPad
, BasicBlock
*UnwindBB
,
949 unsigned Values
, BasicBlock
*InsertAtEnd
)
950 : Instruction(Type::getVoidTy(CleanupPad
->getContext()),
951 Instruction::CleanupRet
,
952 OperandTraits
<CleanupReturnInst
>::op_end(this) - Values
,
953 Values
, InsertAtEnd
) {
954 init(CleanupPad
, UnwindBB
);
957 //===----------------------------------------------------------------------===//
958 // CatchReturnInst Implementation
959 //===----------------------------------------------------------------------===//
960 void CatchReturnInst::init(Value
*CatchPad
, BasicBlock
*BB
) {
965 CatchReturnInst::CatchReturnInst(const CatchReturnInst
&CRI
)
966 : Instruction(Type::getVoidTy(CRI
.getContext()), Instruction::CatchRet
,
967 OperandTraits
<CatchReturnInst
>::op_begin(this), 2) {
968 Op
<0>() = CRI
.Op
<0>();
969 Op
<1>() = CRI
.Op
<1>();
972 CatchReturnInst::CatchReturnInst(Value
*CatchPad
, BasicBlock
*BB
,
973 Instruction
*InsertBefore
)
974 : Instruction(Type::getVoidTy(BB
->getContext()), Instruction::CatchRet
,
975 OperandTraits
<CatchReturnInst
>::op_begin(this), 2,
980 CatchReturnInst::CatchReturnInst(Value
*CatchPad
, BasicBlock
*BB
,
981 BasicBlock
*InsertAtEnd
)
982 : Instruction(Type::getVoidTy(BB
->getContext()), Instruction::CatchRet
,
983 OperandTraits
<CatchReturnInst
>::op_begin(this), 2,
988 //===----------------------------------------------------------------------===//
989 // CatchSwitchInst Implementation
990 //===----------------------------------------------------------------------===//
992 CatchSwitchInst::CatchSwitchInst(Value
*ParentPad
, BasicBlock
*UnwindDest
,
993 unsigned NumReservedValues
,
994 const Twine
&NameStr
,
995 Instruction
*InsertBefore
)
996 : Instruction(ParentPad
->getType(), Instruction::CatchSwitch
, nullptr, 0,
1000 init(ParentPad
, UnwindDest
, NumReservedValues
+ 1);
1004 CatchSwitchInst::CatchSwitchInst(Value
*ParentPad
, BasicBlock
*UnwindDest
,
1005 unsigned NumReservedValues
,
1006 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
)
1007 : Instruction(ParentPad
->getType(), Instruction::CatchSwitch
, nullptr, 0,
1010 ++NumReservedValues
;
1011 init(ParentPad
, UnwindDest
, NumReservedValues
+ 1);
1015 CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst
&CSI
)
1016 : Instruction(CSI
.getType(), Instruction::CatchSwitch
, nullptr,
1017 CSI
.getNumOperands()) {
1018 init(CSI
.getParentPad(), CSI
.getUnwindDest(), CSI
.getNumOperands());
1019 setNumHungOffUseOperands(ReservedSpace
);
1020 Use
*OL
= getOperandList();
1021 const Use
*InOL
= CSI
.getOperandList();
1022 for (unsigned I
= 1, E
= ReservedSpace
; I
!= E
; ++I
)
1026 void CatchSwitchInst::init(Value
*ParentPad
, BasicBlock
*UnwindDest
,
1027 unsigned NumReservedValues
) {
1028 assert(ParentPad
&& NumReservedValues
);
1030 ReservedSpace
= NumReservedValues
;
1031 setNumHungOffUseOperands(UnwindDest
? 2 : 1);
1032 allocHungoffUses(ReservedSpace
);
1034 Op
<0>() = ParentPad
;
1036 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1037 setUnwindDest(UnwindDest
);
1041 /// growOperands - grow operands - This grows the operand list in response to a
1042 /// push_back style of operation. This grows the number of ops by 2 times.
1043 void CatchSwitchInst::growOperands(unsigned Size
) {
1044 unsigned NumOperands
= getNumOperands();
1045 assert(NumOperands
>= 1);
1046 if (ReservedSpace
>= NumOperands
+ Size
)
1048 ReservedSpace
= (NumOperands
+ Size
/ 2) * 2;
1049 growHungoffUses(ReservedSpace
);
1052 void CatchSwitchInst::addHandler(BasicBlock
*Handler
) {
1053 unsigned OpNo
= getNumOperands();
1055 assert(OpNo
< ReservedSpace
&& "Growing didn't work!");
1056 setNumHungOffUseOperands(getNumOperands() + 1);
1057 getOperandList()[OpNo
] = Handler
;
1060 void CatchSwitchInst::removeHandler(handler_iterator HI
) {
1061 // Move all subsequent handlers up one.
1062 Use
*EndDst
= op_end() - 1;
1063 for (Use
*CurDst
= HI
.getCurrent(); CurDst
!= EndDst
; ++CurDst
)
1064 *CurDst
= *(CurDst
+ 1);
1065 // Null out the last handler use.
1068 setNumHungOffUseOperands(getNumOperands() - 1);
1071 //===----------------------------------------------------------------------===//
1072 // FuncletPadInst Implementation
1073 //===----------------------------------------------------------------------===//
1074 void FuncletPadInst::init(Value
*ParentPad
, ArrayRef
<Value
*> Args
,
1075 const Twine
&NameStr
) {
1076 assert(getNumOperands() == 1 + Args
.size() && "NumOperands not set up?");
1077 llvm::copy(Args
, op_begin());
1078 setParentPad(ParentPad
);
1082 FuncletPadInst::FuncletPadInst(const FuncletPadInst
&FPI
)
1083 : Instruction(FPI
.getType(), FPI
.getOpcode(),
1084 OperandTraits
<FuncletPadInst
>::op_end(this) -
1085 FPI
.getNumOperands(),
1086 FPI
.getNumOperands()) {
1087 std::copy(FPI
.op_begin(), FPI
.op_end(), op_begin());
1088 setParentPad(FPI
.getParentPad());
1091 FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op
, Value
*ParentPad
,
1092 ArrayRef
<Value
*> Args
, unsigned Values
,
1093 const Twine
&NameStr
, Instruction
*InsertBefore
)
1094 : Instruction(ParentPad
->getType(), Op
,
1095 OperandTraits
<FuncletPadInst
>::op_end(this) - Values
, Values
,
1097 init(ParentPad
, Args
, NameStr
);
1100 FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op
, Value
*ParentPad
,
1101 ArrayRef
<Value
*> Args
, unsigned Values
,
1102 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
)
1103 : Instruction(ParentPad
->getType(), Op
,
1104 OperandTraits
<FuncletPadInst
>::op_end(this) - Values
, Values
,
1106 init(ParentPad
, Args
, NameStr
);
1109 //===----------------------------------------------------------------------===//
1110 // UnreachableInst Implementation
1111 //===----------------------------------------------------------------------===//
1113 UnreachableInst::UnreachableInst(LLVMContext
&Context
,
1114 Instruction
*InsertBefore
)
1115 : Instruction(Type::getVoidTy(Context
), Instruction::Unreachable
, nullptr,
1117 UnreachableInst::UnreachableInst(LLVMContext
&Context
, BasicBlock
*InsertAtEnd
)
1118 : Instruction(Type::getVoidTy(Context
), Instruction::Unreachable
, nullptr,
1121 //===----------------------------------------------------------------------===//
1122 // BranchInst Implementation
1123 //===----------------------------------------------------------------------===//
1125 void BranchInst::AssertOK() {
1126 if (isConditional())
1127 assert(getCondition()->getType()->isIntegerTy(1) &&
1128 "May only branch on boolean predicates!");
1131 BranchInst::BranchInst(BasicBlock
*IfTrue
, Instruction
*InsertBefore
)
1132 : Instruction(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
1133 OperandTraits
<BranchInst
>::op_end(this) - 1, 1,
1135 assert(IfTrue
&& "Branch destination may not be null!");
1139 BranchInst::BranchInst(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
, Value
*Cond
,
1140 Instruction
*InsertBefore
)
1141 : Instruction(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
1142 OperandTraits
<BranchInst
>::op_end(this) - 3, 3,
1152 BranchInst::BranchInst(BasicBlock
*IfTrue
, BasicBlock
*InsertAtEnd
)
1153 : Instruction(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
1154 OperandTraits
<BranchInst
>::op_end(this) - 1, 1, InsertAtEnd
) {
1155 assert(IfTrue
&& "Branch destination may not be null!");
1159 BranchInst::BranchInst(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
, Value
*Cond
,
1160 BasicBlock
*InsertAtEnd
)
1161 : Instruction(Type::getVoidTy(IfTrue
->getContext()), Instruction::Br
,
1162 OperandTraits
<BranchInst
>::op_end(this) - 3, 3, InsertAtEnd
) {
1171 BranchInst::BranchInst(const BranchInst
&BI
)
1172 : Instruction(Type::getVoidTy(BI
.getContext()), Instruction::Br
,
1173 OperandTraits
<BranchInst
>::op_end(this) - BI
.getNumOperands(),
1174 BI
.getNumOperands()) {
1175 Op
<-1>() = BI
.Op
<-1>();
1176 if (BI
.getNumOperands() != 1) {
1177 assert(BI
.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
1178 Op
<-3>() = BI
.Op
<-3>();
1179 Op
<-2>() = BI
.Op
<-2>();
1181 SubclassOptionalData
= BI
.SubclassOptionalData
;
1184 void BranchInst::swapSuccessors() {
1185 assert(isConditional() &&
1186 "Cannot swap successors of an unconditional branch");
1187 Op
<-1>().swap(Op
<-2>());
1189 // Update profile metadata if present and it matches our structural
1194 //===----------------------------------------------------------------------===//
1195 // AllocaInst Implementation
1196 //===----------------------------------------------------------------------===//
1198 static Value
*getAISize(LLVMContext
&Context
, Value
*Amt
) {
1200 Amt
= ConstantInt::get(Type::getInt32Ty(Context
), 1);
1202 assert(!isa
<BasicBlock
>(Amt
) &&
1203 "Passed basic block into allocation size parameter! Use other ctor");
1204 assert(Amt
->getType()->isIntegerTy() &&
1205 "Allocation array size is not an integer!");
1210 AllocaInst::AllocaInst(Type
*Ty
, unsigned AddrSpace
, const Twine
&Name
,
1211 Instruction
*InsertBefore
)
1212 : AllocaInst(Ty
, AddrSpace
, /*ArraySize=*/nullptr, Name
, InsertBefore
) {}
1214 AllocaInst::AllocaInst(Type
*Ty
, unsigned AddrSpace
, const Twine
&Name
,
1215 BasicBlock
*InsertAtEnd
)
1216 : AllocaInst(Ty
, AddrSpace
, /*ArraySize=*/nullptr, Name
, InsertAtEnd
) {}
1218 AllocaInst::AllocaInst(Type
*Ty
, unsigned AddrSpace
, Value
*ArraySize
,
1219 const Twine
&Name
, Instruction
*InsertBefore
)
1220 : AllocaInst(Ty
, AddrSpace
, ArraySize
, /*Align=*/0, Name
, InsertBefore
) {}
1222 AllocaInst::AllocaInst(Type
*Ty
, unsigned AddrSpace
, Value
*ArraySize
,
1223 const Twine
&Name
, BasicBlock
*InsertAtEnd
)
1224 : AllocaInst(Ty
, AddrSpace
, ArraySize
, /*Align=*/0, Name
, InsertAtEnd
) {}
1226 AllocaInst::AllocaInst(Type
*Ty
, unsigned AddrSpace
, Value
*ArraySize
,
1227 unsigned Align
, const Twine
&Name
,
1228 Instruction
*InsertBefore
)
1229 : UnaryInstruction(PointerType::get(Ty
, AddrSpace
), Alloca
,
1230 getAISize(Ty
->getContext(), ArraySize
), InsertBefore
),
1232 setAlignment(MaybeAlign(Align
));
1233 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
1237 AllocaInst::AllocaInst(Type
*Ty
, unsigned AddrSpace
, Value
*ArraySize
,
1238 unsigned Align
, const Twine
&Name
,
1239 BasicBlock
*InsertAtEnd
)
1240 : UnaryInstruction(PointerType::get(Ty
, AddrSpace
), Alloca
,
1241 getAISize(Ty
->getContext(), ArraySize
), InsertAtEnd
),
1243 setAlignment(MaybeAlign(Align
));
1244 assert(!Ty
->isVoidTy() && "Cannot allocate void!");
1248 void AllocaInst::setAlignment(MaybeAlign Align
) {
1249 assert((!Align
|| *Align
<= MaximumAlignment
) &&
1250 "Alignment is greater than MaximumAlignment!");
1251 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1254 assert(getAlignment() == Align
->value() &&
1255 "Alignment representation error!");
1257 assert(getAlignment() == 0 && "Alignment representation error!");
1260 bool AllocaInst::isArrayAllocation() const {
1261 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(getOperand(0)))
1262 return !CI
->isOne();
1266 /// isStaticAlloca - Return true if this alloca is in the entry block of the
1267 /// function and is a constant size. If so, the code generator will fold it
1268 /// into the prolog/epilog code, so it is basically free.
1269 bool AllocaInst::isStaticAlloca() const {
1270 // Must be constant size.
1271 if (!isa
<ConstantInt
>(getArraySize())) return false;
1273 // Must be in the entry block.
1274 const BasicBlock
*Parent
= getParent();
1275 return Parent
== &Parent
->getParent()->front() && !isUsedWithInAlloca();
1278 //===----------------------------------------------------------------------===//
1279 // LoadInst Implementation
1280 //===----------------------------------------------------------------------===//
1282 void LoadInst::AssertOK() {
1283 assert(getOperand(0)->getType()->isPointerTy() &&
1284 "Ptr must have pointer type.");
1285 assert(!(isAtomic() && getAlignment() == 0) &&
1286 "Alignment required for atomic load");
1289 LoadInst::LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&Name
,
1290 Instruction
*InsertBef
)
1291 : LoadInst(Ty
, Ptr
, Name
, /*isVolatile=*/false, InsertBef
) {}
1293 LoadInst::LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&Name
,
1294 BasicBlock
*InsertAE
)
1295 : LoadInst(Ty
, Ptr
, Name
, /*isVolatile=*/false, InsertAE
) {}
1297 LoadInst::LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
1298 Instruction
*InsertBef
)
1299 : LoadInst(Ty
, Ptr
, Name
, isVolatile
, /*Align=*/None
, InsertBef
) {}
1301 LoadInst::LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
1302 BasicBlock
*InsertAE
)
1303 : LoadInst(Ty
, Ptr
, Name
, isVolatile
, /*Align=*/None
, InsertAE
) {}
1305 LoadInst::LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
1306 MaybeAlign Align
, Instruction
*InsertBef
)
1307 : LoadInst(Ty
, Ptr
, Name
, isVolatile
, Align
, AtomicOrdering::NotAtomic
,
1308 SyncScope::System
, InsertBef
) {}
1310 LoadInst::LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
1311 MaybeAlign Align
, BasicBlock
*InsertAE
)
1312 : LoadInst(Ty
, Ptr
, Name
, isVolatile
, Align
, AtomicOrdering::NotAtomic
,
1313 SyncScope::System
, InsertAE
) {}
1315 LoadInst::LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
1316 MaybeAlign Align
, AtomicOrdering Order
, SyncScope::ID SSID
,
1317 Instruction
*InsertBef
)
1318 : UnaryInstruction(Ty
, Load
, Ptr
, InsertBef
) {
1319 assert(Ty
== cast
<PointerType
>(Ptr
->getType())->getElementType());
1320 setVolatile(isVolatile
);
1321 setAlignment(MaybeAlign(Align
));
1322 setAtomic(Order
, SSID
);
1327 LoadInst::LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&Name
, bool isVolatile
,
1328 MaybeAlign Align
, AtomicOrdering Order
, SyncScope::ID SSID
,
1329 BasicBlock
*InsertAE
)
1330 : UnaryInstruction(Ty
, Load
, Ptr
, InsertAE
) {
1331 assert(Ty
== cast
<PointerType
>(Ptr
->getType())->getElementType());
1332 setVolatile(isVolatile
);
1333 setAlignment(Align
);
1334 setAtomic(Order
, SSID
);
1339 void LoadInst::setAlignment(MaybeAlign Align
) {
1340 assert((!Align
|| *Align
<= MaximumAlignment
) &&
1341 "Alignment is greater than MaximumAlignment!");
1342 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
1343 (encode(Align
) << 1));
1345 assert(getAlignment() == Align
->value() &&
1346 "Alignment representation error!");
1348 assert(getAlignment() == 0 && "Alignment representation error!");
1351 //===----------------------------------------------------------------------===//
1352 // StoreInst Implementation
1353 //===----------------------------------------------------------------------===//
1355 void StoreInst::AssertOK() {
1356 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
1357 assert(getOperand(1)->getType()->isPointerTy() &&
1358 "Ptr must have pointer type!");
1359 assert(getOperand(0)->getType() ==
1360 cast
<PointerType
>(getOperand(1)->getType())->getElementType()
1361 && "Ptr must be a pointer to Val type!");
1362 assert(!(isAtomic() && getAlignment() == 0) &&
1363 "Alignment required for atomic store");
1366 StoreInst::StoreInst(Value
*val
, Value
*addr
, Instruction
*InsertBefore
)
1367 : StoreInst(val
, addr
, /*isVolatile=*/false, InsertBefore
) {}
1369 StoreInst::StoreInst(Value
*val
, Value
*addr
, BasicBlock
*InsertAtEnd
)
1370 : StoreInst(val
, addr
, /*isVolatile=*/false, InsertAtEnd
) {}
1372 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
1373 Instruction
*InsertBefore
)
1374 : StoreInst(val
, addr
, isVolatile
, /*Align=*/None
, InsertBefore
) {}
1376 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
,
1377 BasicBlock
*InsertAtEnd
)
1378 : StoreInst(val
, addr
, isVolatile
, /*Align=*/None
, InsertAtEnd
) {}
1380 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
, MaybeAlign Align
,
1381 Instruction
*InsertBefore
)
1382 : StoreInst(val
, addr
, isVolatile
, Align
, AtomicOrdering::NotAtomic
,
1383 SyncScope::System
, InsertBefore
) {}
1385 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
, MaybeAlign Align
,
1386 BasicBlock
*InsertAtEnd
)
1387 : StoreInst(val
, addr
, isVolatile
, Align
, AtomicOrdering::NotAtomic
,
1388 SyncScope::System
, InsertAtEnd
) {}
1390 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
, MaybeAlign Align
,
1391 AtomicOrdering Order
, SyncScope::ID SSID
,
1392 Instruction
*InsertBefore
)
1393 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
1394 OperandTraits
<StoreInst
>::op_begin(this),
1395 OperandTraits
<StoreInst
>::operands(this), InsertBefore
) {
1398 setVolatile(isVolatile
);
1399 setAlignment(Align
);
1400 setAtomic(Order
, SSID
);
1404 StoreInst::StoreInst(Value
*val
, Value
*addr
, bool isVolatile
, MaybeAlign Align
,
1405 AtomicOrdering Order
, SyncScope::ID SSID
,
1406 BasicBlock
*InsertAtEnd
)
1407 : Instruction(Type::getVoidTy(val
->getContext()), Store
,
1408 OperandTraits
<StoreInst
>::op_begin(this),
1409 OperandTraits
<StoreInst
>::operands(this), InsertAtEnd
) {
1412 setVolatile(isVolatile
);
1413 setAlignment(Align
);
1414 setAtomic(Order
, SSID
);
1418 void StoreInst::setAlignment(MaybeAlign Align
) {
1419 assert((!Align
|| *Align
<= MaximumAlignment
) &&
1420 "Alignment is greater than MaximumAlignment!");
1421 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
1422 (encode(Align
) << 1));
1424 assert(getAlignment() == Align
->value() &&
1425 "Alignment representation error!");
1427 assert(getAlignment() == 0 && "Alignment representation error!");
1430 //===----------------------------------------------------------------------===//
1431 // AtomicCmpXchgInst Implementation
1432 //===----------------------------------------------------------------------===//
1434 void AtomicCmpXchgInst::Init(Value
*Ptr
, Value
*Cmp
, Value
*NewVal
,
1435 AtomicOrdering SuccessOrdering
,
1436 AtomicOrdering FailureOrdering
,
1437 SyncScope::ID SSID
) {
1441 setSuccessOrdering(SuccessOrdering
);
1442 setFailureOrdering(FailureOrdering
);
1443 setSyncScopeID(SSID
);
1445 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1446 "All operands must be non-null!");
1447 assert(getOperand(0)->getType()->isPointerTy() &&
1448 "Ptr must have pointer type!");
1449 assert(getOperand(1)->getType() ==
1450 cast
<PointerType
>(getOperand(0)->getType())->getElementType()
1451 && "Ptr must be a pointer to Cmp type!");
1452 assert(getOperand(2)->getType() ==
1453 cast
<PointerType
>(getOperand(0)->getType())->getElementType()
1454 && "Ptr must be a pointer to NewVal type!");
1455 assert(SuccessOrdering
!= AtomicOrdering::NotAtomic
&&
1456 "AtomicCmpXchg instructions must be atomic!");
1457 assert(FailureOrdering
!= AtomicOrdering::NotAtomic
&&
1458 "AtomicCmpXchg instructions must be atomic!");
1459 assert(!isStrongerThan(FailureOrdering
, SuccessOrdering
) &&
1460 "AtomicCmpXchg failure argument shall be no stronger than the success "
1462 assert(FailureOrdering
!= AtomicOrdering::Release
&&
1463 FailureOrdering
!= AtomicOrdering::AcquireRelease
&&
1464 "AtomicCmpXchg failure ordering cannot include release semantics");
1467 AtomicCmpXchgInst::AtomicCmpXchgInst(Value
*Ptr
, Value
*Cmp
, Value
*NewVal
,
1468 AtomicOrdering SuccessOrdering
,
1469 AtomicOrdering FailureOrdering
,
1471 Instruction
*InsertBefore
)
1473 StructType::get(Cmp
->getType(), Type::getInt1Ty(Cmp
->getContext())),
1474 AtomicCmpXchg
, OperandTraits
<AtomicCmpXchgInst
>::op_begin(this),
1475 OperandTraits
<AtomicCmpXchgInst
>::operands(this), InsertBefore
) {
1476 Init(Ptr
, Cmp
, NewVal
, SuccessOrdering
, FailureOrdering
, SSID
);
1479 AtomicCmpXchgInst::AtomicCmpXchgInst(Value
*Ptr
, Value
*Cmp
, Value
*NewVal
,
1480 AtomicOrdering SuccessOrdering
,
1481 AtomicOrdering FailureOrdering
,
1483 BasicBlock
*InsertAtEnd
)
1485 StructType::get(Cmp
->getType(), Type::getInt1Ty(Cmp
->getContext())),
1486 AtomicCmpXchg
, OperandTraits
<AtomicCmpXchgInst
>::op_begin(this),
1487 OperandTraits
<AtomicCmpXchgInst
>::operands(this), InsertAtEnd
) {
1488 Init(Ptr
, Cmp
, NewVal
, SuccessOrdering
, FailureOrdering
, SSID
);
1491 //===----------------------------------------------------------------------===//
1492 // AtomicRMWInst Implementation
1493 //===----------------------------------------------------------------------===//
1495 void AtomicRMWInst::Init(BinOp Operation
, Value
*Ptr
, Value
*Val
,
1496 AtomicOrdering Ordering
,
1497 SyncScope::ID SSID
) {
1500 setOperation(Operation
);
1501 setOrdering(Ordering
);
1502 setSyncScopeID(SSID
);
1504 assert(getOperand(0) && getOperand(1) &&
1505 "All operands must be non-null!");
1506 assert(getOperand(0)->getType()->isPointerTy() &&
1507 "Ptr must have pointer type!");
1508 assert(getOperand(1)->getType() ==
1509 cast
<PointerType
>(getOperand(0)->getType())->getElementType()
1510 && "Ptr must be a pointer to Val type!");
1511 assert(Ordering
!= AtomicOrdering::NotAtomic
&&
1512 "AtomicRMW instructions must be atomic!");
1515 AtomicRMWInst::AtomicRMWInst(BinOp Operation
, Value
*Ptr
, Value
*Val
,
1516 AtomicOrdering Ordering
,
1518 Instruction
*InsertBefore
)
1519 : Instruction(Val
->getType(), AtomicRMW
,
1520 OperandTraits
<AtomicRMWInst
>::op_begin(this),
1521 OperandTraits
<AtomicRMWInst
>::operands(this),
1523 Init(Operation
, Ptr
, Val
, Ordering
, SSID
);
1526 AtomicRMWInst::AtomicRMWInst(BinOp Operation
, Value
*Ptr
, Value
*Val
,
1527 AtomicOrdering Ordering
,
1529 BasicBlock
*InsertAtEnd
)
1530 : Instruction(Val
->getType(), AtomicRMW
,
1531 OperandTraits
<AtomicRMWInst
>::op_begin(this),
1532 OperandTraits
<AtomicRMWInst
>::operands(this),
1534 Init(Operation
, Ptr
, Val
, Ordering
, SSID
);
1537 StringRef
AtomicRMWInst::getOperationName(BinOp Op
) {
1539 case AtomicRMWInst::Xchg
:
1541 case AtomicRMWInst::Add
:
1543 case AtomicRMWInst::Sub
:
1545 case AtomicRMWInst::And
:
1547 case AtomicRMWInst::Nand
:
1549 case AtomicRMWInst::Or
:
1551 case AtomicRMWInst::Xor
:
1553 case AtomicRMWInst::Max
:
1555 case AtomicRMWInst::Min
:
1557 case AtomicRMWInst::UMax
:
1559 case AtomicRMWInst::UMin
:
1561 case AtomicRMWInst::FAdd
:
1563 case AtomicRMWInst::FSub
:
1565 case AtomicRMWInst::BAD_BINOP
:
1566 return "<invalid operation>";
1569 llvm_unreachable("invalid atomicrmw operation");
1572 //===----------------------------------------------------------------------===//
1573 // FenceInst Implementation
1574 //===----------------------------------------------------------------------===//
1576 FenceInst::FenceInst(LLVMContext
&C
, AtomicOrdering Ordering
,
1578 Instruction
*InsertBefore
)
1579 : Instruction(Type::getVoidTy(C
), Fence
, nullptr, 0, InsertBefore
) {
1580 setOrdering(Ordering
);
1581 setSyncScopeID(SSID
);
1584 FenceInst::FenceInst(LLVMContext
&C
, AtomicOrdering Ordering
,
1586 BasicBlock
*InsertAtEnd
)
1587 : Instruction(Type::getVoidTy(C
), Fence
, nullptr, 0, InsertAtEnd
) {
1588 setOrdering(Ordering
);
1589 setSyncScopeID(SSID
);
1592 //===----------------------------------------------------------------------===//
1593 // GetElementPtrInst Implementation
1594 //===----------------------------------------------------------------------===//
1596 void GetElementPtrInst::init(Value
*Ptr
, ArrayRef
<Value
*> IdxList
,
1597 const Twine
&Name
) {
1598 assert(getNumOperands() == 1 + IdxList
.size() &&
1599 "NumOperands not initialized?");
1601 llvm::copy(IdxList
, op_begin() + 1);
1605 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst
&GEPI
)
1606 : Instruction(GEPI
.getType(), GetElementPtr
,
1607 OperandTraits
<GetElementPtrInst
>::op_end(this) -
1608 GEPI
.getNumOperands(),
1609 GEPI
.getNumOperands()),
1610 SourceElementType(GEPI
.SourceElementType
),
1611 ResultElementType(GEPI
.ResultElementType
) {
1612 std::copy(GEPI
.op_begin(), GEPI
.op_end(), op_begin());
1613 SubclassOptionalData
= GEPI
.SubclassOptionalData
;
1616 /// getIndexedType - Returns the type of the element that would be accessed with
1617 /// a gep instruction with the specified parameters.
1619 /// The Idxs pointer should point to a continuous piece of memory containing the
1620 /// indices, either as Value* or uint64_t.
1622 /// A null type is returned if the indices are invalid for the specified
1625 template <typename IndexTy
>
1626 static Type
*getIndexedTypeInternal(Type
*Agg
, ArrayRef
<IndexTy
> IdxList
) {
1627 // Handle the special case of the empty set index set, which is always valid.
1628 if (IdxList
.empty())
1631 // If there is at least one index, the top level type must be sized, otherwise
1632 // it cannot be 'stepped over'.
1633 if (!Agg
->isSized())
1636 unsigned CurIdx
= 1;
1637 for (; CurIdx
!= IdxList
.size(); ++CurIdx
) {
1638 CompositeType
*CT
= dyn_cast
<CompositeType
>(Agg
);
1639 if (!CT
|| CT
->isPointerTy()) return nullptr;
1640 IndexTy Index
= IdxList
[CurIdx
];
1641 if (!CT
->indexValid(Index
)) return nullptr;
1642 Agg
= CT
->getTypeAtIndex(Index
);
1644 return CurIdx
== IdxList
.size() ? Agg
: nullptr;
1647 Type
*GetElementPtrInst::getIndexedType(Type
*Ty
, ArrayRef
<Value
*> IdxList
) {
1648 return getIndexedTypeInternal(Ty
, IdxList
);
1651 Type
*GetElementPtrInst::getIndexedType(Type
*Ty
,
1652 ArrayRef
<Constant
*> IdxList
) {
1653 return getIndexedTypeInternal(Ty
, IdxList
);
1656 Type
*GetElementPtrInst::getIndexedType(Type
*Ty
, ArrayRef
<uint64_t> IdxList
) {
1657 return getIndexedTypeInternal(Ty
, IdxList
);
1660 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1661 /// zeros. If so, the result pointer and the first operand have the same
1662 /// value, just potentially different types.
1663 bool GetElementPtrInst::hasAllZeroIndices() const {
1664 for (unsigned i
= 1, e
= getNumOperands(); i
!= e
; ++i
) {
1665 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(getOperand(i
))) {
1666 if (!CI
->isZero()) return false;
1674 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1675 /// constant integers. If so, the result pointer and the first operand have
1676 /// a constant offset between them.
1677 bool GetElementPtrInst::hasAllConstantIndices() const {
1678 for (unsigned i
= 1, e
= getNumOperands(); i
!= e
; ++i
) {
1679 if (!isa
<ConstantInt
>(getOperand(i
)))
1685 void GetElementPtrInst::setIsInBounds(bool B
) {
1686 cast
<GEPOperator
>(this)->setIsInBounds(B
);
1689 bool GetElementPtrInst::isInBounds() const {
1690 return cast
<GEPOperator
>(this)->isInBounds();
1693 bool GetElementPtrInst::accumulateConstantOffset(const DataLayout
&DL
,
1694 APInt
&Offset
) const {
1695 // Delegate to the generic GEPOperator implementation.
1696 return cast
<GEPOperator
>(this)->accumulateConstantOffset(DL
, Offset
);
1699 //===----------------------------------------------------------------------===//
1700 // ExtractElementInst Implementation
1701 //===----------------------------------------------------------------------===//
1703 ExtractElementInst::ExtractElementInst(Value
*Val
, Value
*Index
,
1705 Instruction
*InsertBef
)
1706 : Instruction(cast
<VectorType
>(Val
->getType())->getElementType(),
1708 OperandTraits
<ExtractElementInst
>::op_begin(this),
1710 assert(isValidOperands(Val
, Index
) &&
1711 "Invalid extractelement instruction operands!");
1717 ExtractElementInst::ExtractElementInst(Value
*Val
, Value
*Index
,
1719 BasicBlock
*InsertAE
)
1720 : Instruction(cast
<VectorType
>(Val
->getType())->getElementType(),
1722 OperandTraits
<ExtractElementInst
>::op_begin(this),
1724 assert(isValidOperands(Val
, Index
) &&
1725 "Invalid extractelement instruction operands!");
1732 bool ExtractElementInst::isValidOperands(const Value
*Val
, const Value
*Index
) {
1733 if (!Val
->getType()->isVectorTy() || !Index
->getType()->isIntegerTy())
1738 //===----------------------------------------------------------------------===//
1739 // InsertElementInst Implementation
1740 //===----------------------------------------------------------------------===//
1742 InsertElementInst::InsertElementInst(Value
*Vec
, Value
*Elt
, Value
*Index
,
1744 Instruction
*InsertBef
)
1745 : Instruction(Vec
->getType(), InsertElement
,
1746 OperandTraits
<InsertElementInst
>::op_begin(this),
1748 assert(isValidOperands(Vec
, Elt
, Index
) &&
1749 "Invalid insertelement instruction operands!");
1756 InsertElementInst::InsertElementInst(Value
*Vec
, Value
*Elt
, Value
*Index
,
1758 BasicBlock
*InsertAE
)
1759 : Instruction(Vec
->getType(), InsertElement
,
1760 OperandTraits
<InsertElementInst
>::op_begin(this),
1762 assert(isValidOperands(Vec
, Elt
, Index
) &&
1763 "Invalid insertelement instruction operands!");
1771 bool InsertElementInst::isValidOperands(const Value
*Vec
, const Value
*Elt
,
1772 const Value
*Index
) {
1773 if (!Vec
->getType()->isVectorTy())
1774 return false; // First operand of insertelement must be vector type.
1776 if (Elt
->getType() != cast
<VectorType
>(Vec
->getType())->getElementType())
1777 return false;// Second operand of insertelement must be vector element type.
1779 if (!Index
->getType()->isIntegerTy())
1780 return false; // Third operand of insertelement must be i32.
1784 //===----------------------------------------------------------------------===//
1785 // ShuffleVectorInst Implementation
1786 //===----------------------------------------------------------------------===//
1788 ShuffleVectorInst::ShuffleVectorInst(Value
*V1
, Value
*V2
, Value
*Mask
,
1790 Instruction
*InsertBefore
)
1791 : Instruction(VectorType::get(cast
<VectorType
>(V1
->getType())->getElementType(),
1792 cast
<VectorType
>(Mask
->getType())->getElementCount()),
1794 OperandTraits
<ShuffleVectorInst
>::op_begin(this),
1795 OperandTraits
<ShuffleVectorInst
>::operands(this),
1797 assert(isValidOperands(V1
, V2
, Mask
) &&
1798 "Invalid shuffle vector instruction operands!");
1805 ShuffleVectorInst::ShuffleVectorInst(Value
*V1
, Value
*V2
, Value
*Mask
,
1807 BasicBlock
*InsertAtEnd
)
1808 : Instruction(VectorType::get(cast
<VectorType
>(V1
->getType())->getElementType(),
1809 cast
<VectorType
>(Mask
->getType())->getElementCount()),
1811 OperandTraits
<ShuffleVectorInst
>::op_begin(this),
1812 OperandTraits
<ShuffleVectorInst
>::operands(this),
1814 assert(isValidOperands(V1
, V2
, Mask
) &&
1815 "Invalid shuffle vector instruction operands!");
1823 void ShuffleVectorInst::commute() {
1824 int NumOpElts
= Op
<0>()->getType()->getVectorNumElements();
1825 int NumMaskElts
= getMask()->getType()->getVectorNumElements();
1826 SmallVector
<Constant
*, 16> NewMask(NumMaskElts
);
1827 Type
*Int32Ty
= Type::getInt32Ty(getContext());
1828 for (int i
= 0; i
!= NumMaskElts
; ++i
) {
1829 int MaskElt
= getMaskValue(i
);
1830 if (MaskElt
== -1) {
1831 NewMask
[i
] = UndefValue::get(Int32Ty
);
1834 assert(MaskElt
>= 0 && MaskElt
< 2 * NumOpElts
&& "Out-of-range mask");
1835 MaskElt
= (MaskElt
< NumOpElts
) ? MaskElt
+ NumOpElts
: MaskElt
- NumOpElts
;
1836 NewMask
[i
] = ConstantInt::get(Int32Ty
, MaskElt
);
1838 Op
<2>() = ConstantVector::get(NewMask
);
1839 Op
<0>().swap(Op
<1>());
1842 bool ShuffleVectorInst::isValidOperands(const Value
*V1
, const Value
*V2
,
1843 const Value
*Mask
) {
1844 // V1 and V2 must be vectors of the same type.
1845 if (!V1
->getType()->isVectorTy() || V1
->getType() != V2
->getType())
1848 // Mask must be vector of i32.
1849 auto *MaskTy
= dyn_cast
<VectorType
>(Mask
->getType());
1850 if (!MaskTy
|| !MaskTy
->getElementType()->isIntegerTy(32))
1853 // Check to see if Mask is valid.
1854 if (isa
<UndefValue
>(Mask
) || isa
<ConstantAggregateZero
>(Mask
))
1857 if (const auto *MV
= dyn_cast
<ConstantVector
>(Mask
)) {
1858 unsigned V1Size
= cast
<VectorType
>(V1
->getType())->getNumElements();
1859 for (Value
*Op
: MV
->operands()) {
1860 if (auto *CI
= dyn_cast
<ConstantInt
>(Op
)) {
1861 if (CI
->uge(V1Size
*2))
1863 } else if (!isa
<UndefValue
>(Op
)) {
1870 if (const auto *CDS
= dyn_cast
<ConstantDataSequential
>(Mask
)) {
1871 unsigned V1Size
= cast
<VectorType
>(V1
->getType())->getNumElements();
1872 for (unsigned i
= 0, e
= MaskTy
->getNumElements(); i
!= e
; ++i
)
1873 if (CDS
->getElementAsInteger(i
) >= V1Size
*2)
1878 // The bitcode reader can create a place holder for a forward reference
1879 // used as the shuffle mask. When this occurs, the shuffle mask will
1880 // fall into this case and fail. To avoid this error, do this bit of
1881 // ugliness to allow such a mask pass.
1882 if (const auto *CE
= dyn_cast
<ConstantExpr
>(Mask
))
1883 if (CE
->getOpcode() == Instruction::UserOp1
)
1889 int ShuffleVectorInst::getMaskValue(const Constant
*Mask
, unsigned i
) {
1890 assert(i
< Mask
->getType()->getVectorNumElements() && "Index out of range");
1891 if (auto *CDS
= dyn_cast
<ConstantDataSequential
>(Mask
))
1892 return CDS
->getElementAsInteger(i
);
1893 Constant
*C
= Mask
->getAggregateElement(i
);
1894 if (isa
<UndefValue
>(C
))
1896 return cast
<ConstantInt
>(C
)->getZExtValue();
1899 void ShuffleVectorInst::getShuffleMask(const Constant
*Mask
,
1900 SmallVectorImpl
<int> &Result
) {
1901 unsigned NumElts
= Mask
->getType()->getVectorNumElements();
1903 if (auto *CDS
= dyn_cast
<ConstantDataSequential
>(Mask
)) {
1904 for (unsigned i
= 0; i
!= NumElts
; ++i
)
1905 Result
.push_back(CDS
->getElementAsInteger(i
));
1908 for (unsigned i
= 0; i
!= NumElts
; ++i
) {
1909 Constant
*C
= Mask
->getAggregateElement(i
);
1910 Result
.push_back(isa
<UndefValue
>(C
) ? -1 :
1911 cast
<ConstantInt
>(C
)->getZExtValue());
1915 static bool isSingleSourceMaskImpl(ArrayRef
<int> Mask
, int NumOpElts
) {
1916 assert(!Mask
.empty() && "Shuffle mask must contain elements");
1917 bool UsesLHS
= false;
1918 bool UsesRHS
= false;
1919 for (int i
= 0, NumMaskElts
= Mask
.size(); i
< NumMaskElts
; ++i
) {
1922 assert(Mask
[i
] >= 0 && Mask
[i
] < (NumOpElts
* 2) &&
1923 "Out-of-bounds shuffle mask element");
1924 UsesLHS
|= (Mask
[i
] < NumOpElts
);
1925 UsesRHS
|= (Mask
[i
] >= NumOpElts
);
1926 if (UsesLHS
&& UsesRHS
)
1929 assert((UsesLHS
^ UsesRHS
) && "Should have selected from exactly 1 source");
1933 bool ShuffleVectorInst::isSingleSourceMask(ArrayRef
<int> Mask
) {
1934 // We don't have vector operand size information, so assume operands are the
1935 // same size as the mask.
1936 return isSingleSourceMaskImpl(Mask
, Mask
.size());
1939 static bool isIdentityMaskImpl(ArrayRef
<int> Mask
, int NumOpElts
) {
1940 if (!isSingleSourceMaskImpl(Mask
, NumOpElts
))
1942 for (int i
= 0, NumMaskElts
= Mask
.size(); i
< NumMaskElts
; ++i
) {
1945 if (Mask
[i
] != i
&& Mask
[i
] != (NumOpElts
+ i
))
1951 bool ShuffleVectorInst::isIdentityMask(ArrayRef
<int> Mask
) {
1952 // We don't have vector operand size information, so assume operands are the
1953 // same size as the mask.
1954 return isIdentityMaskImpl(Mask
, Mask
.size());
1957 bool ShuffleVectorInst::isReverseMask(ArrayRef
<int> Mask
) {
1958 if (!isSingleSourceMask(Mask
))
1960 for (int i
= 0, NumElts
= Mask
.size(); i
< NumElts
; ++i
) {
1963 if (Mask
[i
] != (NumElts
- 1 - i
) && Mask
[i
] != (NumElts
+ NumElts
- 1 - i
))
1969 bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef
<int> Mask
) {
1970 if (!isSingleSourceMask(Mask
))
1972 for (int i
= 0, NumElts
= Mask
.size(); i
< NumElts
; ++i
) {
1975 if (Mask
[i
] != 0 && Mask
[i
] != NumElts
)
1981 bool ShuffleVectorInst::isSelectMask(ArrayRef
<int> Mask
) {
1982 // Select is differentiated from identity. It requires using both sources.
1983 if (isSingleSourceMask(Mask
))
1985 for (int i
= 0, NumElts
= Mask
.size(); i
< NumElts
; ++i
) {
1988 if (Mask
[i
] != i
&& Mask
[i
] != (NumElts
+ i
))
1994 bool ShuffleVectorInst::isTransposeMask(ArrayRef
<int> Mask
) {
1995 // Example masks that will return true:
1996 // v1 = <a, b, c, d>
1997 // v2 = <e, f, g, h>
1998 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1999 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
2001 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
2002 int NumElts
= Mask
.size();
2003 if (NumElts
< 2 || !isPowerOf2_32(NumElts
))
2006 // 2. The first element of the mask must be either a 0 or a 1.
2007 if (Mask
[0] != 0 && Mask
[0] != 1)
2010 // 3. The difference between the first 2 elements must be equal to the
2011 // number of elements in the mask.
2012 if ((Mask
[1] - Mask
[0]) != NumElts
)
2015 // 4. The difference between consecutive even-numbered and odd-numbered
2016 // elements must be equal to 2.
2017 for (int i
= 2; i
< NumElts
; ++i
) {
2018 int MaskEltVal
= Mask
[i
];
2019 if (MaskEltVal
== -1)
2021 int MaskEltPrevVal
= Mask
[i
- 2];
2022 if (MaskEltVal
- MaskEltPrevVal
!= 2)
2028 bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef
<int> Mask
,
2029 int NumSrcElts
, int &Index
) {
2030 // Must extract from a single source.
2031 if (!isSingleSourceMaskImpl(Mask
, NumSrcElts
))
2034 // Must be smaller (else this is an Identity shuffle).
2035 if (NumSrcElts
<= (int)Mask
.size())
2038 // Find start of extraction, accounting that we may start with an UNDEF.
2040 for (int i
= 0, e
= Mask
.size(); i
!= e
; ++i
) {
2044 int Offset
= (M
% NumSrcElts
) - i
;
2045 if (0 <= SubIndex
&& SubIndex
!= Offset
)
2050 if (0 <= SubIndex
) {
2057 bool ShuffleVectorInst::isIdentityWithPadding() const {
2058 int NumOpElts
= Op
<0>()->getType()->getVectorNumElements();
2059 int NumMaskElts
= getType()->getVectorNumElements();
2060 if (NumMaskElts
<= NumOpElts
)
2063 // The first part of the mask must choose elements from exactly 1 source op.
2064 SmallVector
<int, 16> Mask
= getShuffleMask();
2065 if (!isIdentityMaskImpl(Mask
, NumOpElts
))
2068 // All extending must be with undef elements.
2069 for (int i
= NumOpElts
; i
< NumMaskElts
; ++i
)
2076 bool ShuffleVectorInst::isIdentityWithExtract() const {
2077 int NumOpElts
= Op
<0>()->getType()->getVectorNumElements();
2078 int NumMaskElts
= getType()->getVectorNumElements();
2079 if (NumMaskElts
>= NumOpElts
)
2082 return isIdentityMaskImpl(getShuffleMask(), NumOpElts
);
2085 bool ShuffleVectorInst::isConcat() const {
2086 // Vector concatenation is differentiated from identity with padding.
2087 if (isa
<UndefValue
>(Op
<0>()) || isa
<UndefValue
>(Op
<1>()))
2090 int NumOpElts
= Op
<0>()->getType()->getVectorNumElements();
2091 int NumMaskElts
= getType()->getVectorNumElements();
2092 if (NumMaskElts
!= NumOpElts
* 2)
2095 // Use the mask length rather than the operands' vector lengths here. We
2096 // already know that the shuffle returns a vector twice as long as the inputs,
2097 // and neither of the inputs are undef vectors. If the mask picks consecutive
2098 // elements from both inputs, then this is a concatenation of the inputs.
2099 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts
);
2102 //===----------------------------------------------------------------------===//
2103 // InsertValueInst Class
2104 //===----------------------------------------------------------------------===//
2106 void InsertValueInst::init(Value
*Agg
, Value
*Val
, ArrayRef
<unsigned> Idxs
,
2107 const Twine
&Name
) {
2108 assert(getNumOperands() == 2 && "NumOperands not initialized?");
2110 // There's no fundamental reason why we require at least one index
2111 // (other than weirdness with &*IdxBegin being invalid; see
2112 // getelementptr's init routine for example). But there's no
2113 // present need to support it.
2114 assert(!Idxs
.empty() && "InsertValueInst must have at least one index");
2116 assert(ExtractValueInst::getIndexedType(Agg
->getType(), Idxs
) ==
2117 Val
->getType() && "Inserted value must match indexed type!");
2121 Indices
.append(Idxs
.begin(), Idxs
.end());
2125 InsertValueInst::InsertValueInst(const InsertValueInst
&IVI
)
2126 : Instruction(IVI
.getType(), InsertValue
,
2127 OperandTraits
<InsertValueInst
>::op_begin(this), 2),
2128 Indices(IVI
.Indices
) {
2129 Op
<0>() = IVI
.getOperand(0);
2130 Op
<1>() = IVI
.getOperand(1);
2131 SubclassOptionalData
= IVI
.SubclassOptionalData
;
2134 //===----------------------------------------------------------------------===//
2135 // ExtractValueInst Class
2136 //===----------------------------------------------------------------------===//
2138 void ExtractValueInst::init(ArrayRef
<unsigned> Idxs
, const Twine
&Name
) {
2139 assert(getNumOperands() == 1 && "NumOperands not initialized?");
2141 // There's no fundamental reason why we require at least one index.
2142 // But there's no present need to support it.
2143 assert(!Idxs
.empty() && "ExtractValueInst must have at least one index");
2145 Indices
.append(Idxs
.begin(), Idxs
.end());
2149 ExtractValueInst::ExtractValueInst(const ExtractValueInst
&EVI
)
2150 : UnaryInstruction(EVI
.getType(), ExtractValue
, EVI
.getOperand(0)),
2151 Indices(EVI
.Indices
) {
2152 SubclassOptionalData
= EVI
.SubclassOptionalData
;
2155 // getIndexedType - Returns the type of the element that would be extracted
2156 // with an extractvalue instruction with the specified parameters.
2158 // A null type is returned if the indices are invalid for the specified
2161 Type
*ExtractValueInst::getIndexedType(Type
*Agg
,
2162 ArrayRef
<unsigned> Idxs
) {
2163 for (unsigned Index
: Idxs
) {
2164 // We can't use CompositeType::indexValid(Index) here.
2165 // indexValid() always returns true for arrays because getelementptr allows
2166 // out-of-bounds indices. Since we don't allow those for extractvalue and
2167 // insertvalue we need to check array indexing manually.
2168 // Since the only other types we can index into are struct types it's just
2169 // as easy to check those manually as well.
2170 if (ArrayType
*AT
= dyn_cast
<ArrayType
>(Agg
)) {
2171 if (Index
>= AT
->getNumElements())
2173 } else if (StructType
*ST
= dyn_cast
<StructType
>(Agg
)) {
2174 if (Index
>= ST
->getNumElements())
2177 // Not a valid type to index into.
2181 Agg
= cast
<CompositeType
>(Agg
)->getTypeAtIndex(Index
);
2183 return const_cast<Type
*>(Agg
);
2186 //===----------------------------------------------------------------------===//
2187 // UnaryOperator Class
2188 //===----------------------------------------------------------------------===//
2190 UnaryOperator::UnaryOperator(UnaryOps iType
, Value
*S
,
2191 Type
*Ty
, const Twine
&Name
,
2192 Instruction
*InsertBefore
)
2193 : UnaryInstruction(Ty
, iType
, S
, InsertBefore
) {
2199 UnaryOperator::UnaryOperator(UnaryOps iType
, Value
*S
,
2200 Type
*Ty
, const Twine
&Name
,
2201 BasicBlock
*InsertAtEnd
)
2202 : UnaryInstruction(Ty
, iType
, S
, InsertAtEnd
) {
2208 UnaryOperator
*UnaryOperator::Create(UnaryOps Op
, Value
*S
,
2210 Instruction
*InsertBefore
) {
2211 return new UnaryOperator(Op
, S
, S
->getType(), Name
, InsertBefore
);
2214 UnaryOperator
*UnaryOperator::Create(UnaryOps Op
, Value
*S
,
2216 BasicBlock
*InsertAtEnd
) {
2217 UnaryOperator
*Res
= Create(Op
, S
, Name
);
2218 InsertAtEnd
->getInstList().push_back(Res
);
2222 void UnaryOperator::AssertOK() {
2223 Value
*LHS
= getOperand(0);
2224 (void)LHS
; // Silence warnings.
2226 switch (getOpcode()) {
2228 assert(getType() == LHS
->getType() &&
2229 "Unary operation should return same type as operand!");
2230 assert(getType()->isFPOrFPVectorTy() &&
2231 "Tried to create a floating-point operation on a "
2232 "non-floating-point type!");
2234 default: llvm_unreachable("Invalid opcode provided");
2239 //===----------------------------------------------------------------------===//
2240 // BinaryOperator Class
2241 //===----------------------------------------------------------------------===//
2243 BinaryOperator::BinaryOperator(BinaryOps iType
, Value
*S1
, Value
*S2
,
2244 Type
*Ty
, const Twine
&Name
,
2245 Instruction
*InsertBefore
)
2246 : Instruction(Ty
, iType
,
2247 OperandTraits
<BinaryOperator
>::op_begin(this),
2248 OperandTraits
<BinaryOperator
>::operands(this),
2256 BinaryOperator::BinaryOperator(BinaryOps iType
, Value
*S1
, Value
*S2
,
2257 Type
*Ty
, const Twine
&Name
,
2258 BasicBlock
*InsertAtEnd
)
2259 : Instruction(Ty
, iType
,
2260 OperandTraits
<BinaryOperator
>::op_begin(this),
2261 OperandTraits
<BinaryOperator
>::operands(this),
2269 void BinaryOperator::AssertOK() {
2270 Value
*LHS
= getOperand(0), *RHS
= getOperand(1);
2271 (void)LHS
; (void)RHS
; // Silence warnings.
2272 assert(LHS
->getType() == RHS
->getType() &&
2273 "Binary operator operand types must match!");
2275 switch (getOpcode()) {
2278 assert(getType() == LHS
->getType() &&
2279 "Arithmetic operation should return same type as operands!");
2280 assert(getType()->isIntOrIntVectorTy() &&
2281 "Tried to create an integer operation on a non-integer type!");
2283 case FAdd
: case FSub
:
2285 assert(getType() == LHS
->getType() &&
2286 "Arithmetic operation should return same type as operands!");
2287 assert(getType()->isFPOrFPVectorTy() &&
2288 "Tried to create a floating-point operation on a "
2289 "non-floating-point type!");
2293 assert(getType() == LHS
->getType() &&
2294 "Arithmetic operation should return same type as operands!");
2295 assert(getType()->isIntOrIntVectorTy() &&
2296 "Incorrect operand type (not integer) for S/UDIV");
2299 assert(getType() == LHS
->getType() &&
2300 "Arithmetic operation should return same type as operands!");
2301 assert(getType()->isFPOrFPVectorTy() &&
2302 "Incorrect operand type (not floating point) for FDIV");
2306 assert(getType() == LHS
->getType() &&
2307 "Arithmetic operation should return same type as operands!");
2308 assert(getType()->isIntOrIntVectorTy() &&
2309 "Incorrect operand type (not integer) for S/UREM");
2312 assert(getType() == LHS
->getType() &&
2313 "Arithmetic operation should return same type as operands!");
2314 assert(getType()->isFPOrFPVectorTy() &&
2315 "Incorrect operand type (not floating point) for FREM");
2320 assert(getType() == LHS
->getType() &&
2321 "Shift operation should return same type as operands!");
2322 assert(getType()->isIntOrIntVectorTy() &&
2323 "Tried to create a shift operation on a non-integral type!");
2327 assert(getType() == LHS
->getType() &&
2328 "Logical operation should return same type as operands!");
2329 assert(getType()->isIntOrIntVectorTy() &&
2330 "Tried to create a logical operation on a non-integral type!");
2332 default: llvm_unreachable("Invalid opcode provided");
2337 BinaryOperator
*BinaryOperator::Create(BinaryOps Op
, Value
*S1
, Value
*S2
,
2339 Instruction
*InsertBefore
) {
2340 assert(S1
->getType() == S2
->getType() &&
2341 "Cannot create binary operator with two operands of differing type!");
2342 return new BinaryOperator(Op
, S1
, S2
, S1
->getType(), Name
, InsertBefore
);
2345 BinaryOperator
*BinaryOperator::Create(BinaryOps Op
, Value
*S1
, Value
*S2
,
2347 BasicBlock
*InsertAtEnd
) {
2348 BinaryOperator
*Res
= Create(Op
, S1
, S2
, Name
);
2349 InsertAtEnd
->getInstList().push_back(Res
);
2353 BinaryOperator
*BinaryOperator::CreateNeg(Value
*Op
, const Twine
&Name
,
2354 Instruction
*InsertBefore
) {
2355 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
2356 return new BinaryOperator(Instruction::Sub
,
2358 Op
->getType(), Name
, InsertBefore
);
2361 BinaryOperator
*BinaryOperator::CreateNeg(Value
*Op
, const Twine
&Name
,
2362 BasicBlock
*InsertAtEnd
) {
2363 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
2364 return new BinaryOperator(Instruction::Sub
,
2366 Op
->getType(), Name
, InsertAtEnd
);
2369 BinaryOperator
*BinaryOperator::CreateNSWNeg(Value
*Op
, const Twine
&Name
,
2370 Instruction
*InsertBefore
) {
2371 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
2372 return BinaryOperator::CreateNSWSub(zero
, Op
, Name
, InsertBefore
);
2375 BinaryOperator
*BinaryOperator::CreateNSWNeg(Value
*Op
, const Twine
&Name
,
2376 BasicBlock
*InsertAtEnd
) {
2377 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
2378 return BinaryOperator::CreateNSWSub(zero
, Op
, Name
, InsertAtEnd
);
2381 BinaryOperator
*BinaryOperator::CreateNUWNeg(Value
*Op
, const Twine
&Name
,
2382 Instruction
*InsertBefore
) {
2383 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
2384 return BinaryOperator::CreateNUWSub(zero
, Op
, Name
, InsertBefore
);
2387 BinaryOperator
*BinaryOperator::CreateNUWNeg(Value
*Op
, const Twine
&Name
,
2388 BasicBlock
*InsertAtEnd
) {
2389 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
2390 return BinaryOperator::CreateNUWSub(zero
, Op
, Name
, InsertAtEnd
);
2393 BinaryOperator
*BinaryOperator::CreateFNeg(Value
*Op
, const Twine
&Name
,
2394 Instruction
*InsertBefore
) {
2395 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
2396 return new BinaryOperator(Instruction::FSub
, zero
, Op
,
2397 Op
->getType(), Name
, InsertBefore
);
2400 BinaryOperator
*BinaryOperator::CreateFNeg(Value
*Op
, const Twine
&Name
,
2401 BasicBlock
*InsertAtEnd
) {
2402 Value
*zero
= ConstantFP::getZeroValueForNegation(Op
->getType());
2403 return new BinaryOperator(Instruction::FSub
, zero
, Op
,
2404 Op
->getType(), Name
, InsertAtEnd
);
2407 BinaryOperator
*BinaryOperator::CreateNot(Value
*Op
, const Twine
&Name
,
2408 Instruction
*InsertBefore
) {
2409 Constant
*C
= Constant::getAllOnesValue(Op
->getType());
2410 return new BinaryOperator(Instruction::Xor
, Op
, C
,
2411 Op
->getType(), Name
, InsertBefore
);
2414 BinaryOperator
*BinaryOperator::CreateNot(Value
*Op
, const Twine
&Name
,
2415 BasicBlock
*InsertAtEnd
) {
2416 Constant
*AllOnes
= Constant::getAllOnesValue(Op
->getType());
2417 return new BinaryOperator(Instruction::Xor
, Op
, AllOnes
,
2418 Op
->getType(), Name
, InsertAtEnd
);
2421 // Exchange the two operands to this instruction. This instruction is safe to
2422 // use on any binary instruction and does not modify the semantics of the
2423 // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2425 bool BinaryOperator::swapOperands() {
2426 if (!isCommutative())
2427 return true; // Can't commute operands
2428 Op
<0>().swap(Op
<1>());
2432 //===----------------------------------------------------------------------===//
2433 // FPMathOperator Class
2434 //===----------------------------------------------------------------------===//
2436 float FPMathOperator::getFPAccuracy() const {
2438 cast
<Instruction
>(this)->getMetadata(LLVMContext::MD_fpmath
);
2441 ConstantFP
*Accuracy
= mdconst::extract
<ConstantFP
>(MD
->getOperand(0));
2442 return Accuracy
->getValueAPF().convertToFloat();
2445 //===----------------------------------------------------------------------===//
2447 //===----------------------------------------------------------------------===//
2449 // Just determine if this cast only deals with integral->integral conversion.
2450 bool CastInst::isIntegerCast() const {
2451 switch (getOpcode()) {
2452 default: return false;
2453 case Instruction::ZExt
:
2454 case Instruction::SExt
:
2455 case Instruction::Trunc
:
2457 case Instruction::BitCast
:
2458 return getOperand(0)->getType()->isIntegerTy() &&
2459 getType()->isIntegerTy();
2463 bool CastInst::isLosslessCast() const {
2464 // Only BitCast can be lossless, exit fast if we're not BitCast
2465 if (getOpcode() != Instruction::BitCast
)
2468 // Identity cast is always lossless
2469 Type
*SrcTy
= getOperand(0)->getType();
2470 Type
*DstTy
= getType();
2474 // Pointer to pointer is always lossless.
2475 if (SrcTy
->isPointerTy())
2476 return DstTy
->isPointerTy();
2477 return false; // Other types have no identity values
2480 /// This function determines if the CastInst does not require any bits to be
2481 /// changed in order to effect the cast. Essentially, it identifies cases where
2482 /// no code gen is necessary for the cast, hence the name no-op cast. For
2483 /// example, the following are all no-op casts:
2484 /// # bitcast i32* %x to i8*
2485 /// # bitcast <2 x i32> %x to <4 x i16>
2486 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
2487 /// Determine if the described cast is a no-op.
2488 bool CastInst::isNoopCast(Instruction::CastOps Opcode
,
2491 const DataLayout
&DL
) {
2493 default: llvm_unreachable("Invalid CastOp");
2494 case Instruction::Trunc
:
2495 case Instruction::ZExt
:
2496 case Instruction::SExt
:
2497 case Instruction::FPTrunc
:
2498 case Instruction::FPExt
:
2499 case Instruction::UIToFP
:
2500 case Instruction::SIToFP
:
2501 case Instruction::FPToUI
:
2502 case Instruction::FPToSI
:
2503 case Instruction::AddrSpaceCast
:
2504 // TODO: Target informations may give a more accurate answer here.
2506 case Instruction::BitCast
:
2507 return true; // BitCast never modifies bits.
2508 case Instruction::PtrToInt
:
2509 return DL
.getIntPtrType(SrcTy
)->getScalarSizeInBits() ==
2510 DestTy
->getScalarSizeInBits();
2511 case Instruction::IntToPtr
:
2512 return DL
.getIntPtrType(DestTy
)->getScalarSizeInBits() ==
2513 SrcTy
->getScalarSizeInBits();
2517 bool CastInst::isNoopCast(const DataLayout
&DL
) const {
2518 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL
);
2521 /// This function determines if a pair of casts can be eliminated and what
2522 /// opcode should be used in the elimination. This assumes that there are two
2523 /// instructions like this:
2524 /// * %F = firstOpcode SrcTy %x to MidTy
2525 /// * %S = secondOpcode MidTy %F to DstTy
2526 /// The function returns a resultOpcode so these two casts can be replaced with:
2527 /// * %Replacement = resultOpcode %SrcTy %x to DstTy
2528 /// If no such cast is permitted, the function returns 0.
2529 unsigned CastInst::isEliminableCastPair(
2530 Instruction::CastOps firstOp
, Instruction::CastOps secondOp
,
2531 Type
*SrcTy
, Type
*MidTy
, Type
*DstTy
, Type
*SrcIntPtrTy
, Type
*MidIntPtrTy
,
2532 Type
*DstIntPtrTy
) {
2533 // Define the 144 possibilities for these two cast instructions. The values
2534 // in this matrix determine what to do in a given situation and select the
2535 // case in the switch below. The rows correspond to firstOp, the columns
2536 // correspond to secondOp. In looking at the table below, keep in mind
2537 // the following cast properties:
2539 // Size Compare Source Destination
2540 // Operator Src ? Size Type Sign Type Sign
2541 // -------- ------------ ------------------- ---------------------
2542 // TRUNC > Integer Any Integral Any
2543 // ZEXT < Integral Unsigned Integer Any
2544 // SEXT < Integral Signed Integer Any
2545 // FPTOUI n/a FloatPt n/a Integral Unsigned
2546 // FPTOSI n/a FloatPt n/a Integral Signed
2547 // UITOFP n/a Integral Unsigned FloatPt n/a
2548 // SITOFP n/a Integral Signed FloatPt n/a
2549 // FPTRUNC > FloatPt n/a FloatPt n/a
2550 // FPEXT < FloatPt n/a FloatPt n/a
2551 // PTRTOINT n/a Pointer n/a Integral Unsigned
2552 // INTTOPTR n/a Integral Unsigned Pointer n/a
2553 // BITCAST = FirstClass n/a FirstClass n/a
2554 // ADDRSPCST n/a Pointer n/a Pointer n/a
2556 // NOTE: some transforms are safe, but we consider them to be non-profitable.
2557 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2558 // into "fptoui double to i64", but this loses information about the range
2559 // of the produced value (we no longer know the top-part is all zeros).
2560 // Further this conversion is often much more expensive for typical hardware,
2561 // and causes issues when building libgcc. We disallow fptosi+sext for the
2563 const unsigned numCastOps
=
2564 Instruction::CastOpsEnd
- Instruction::CastOpsBegin
;
2565 static const uint8_t CastResults
[numCastOps
][numCastOps
] = {
2566 // T F F U S F F P I B A -+
2567 // R Z S P P I I T P 2 N T S |
2568 // U E E 2 2 2 2 R E I T C C +- secondOp
2569 // N X X U S F F N X N 2 V V |
2570 // C T T I I P P C T T P T T -+
2571 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
2572 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
2573 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2574 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2575 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2576 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2577 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
2578 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
2579 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
2580 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2581 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2582 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2583 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
2586 // TODO: This logic could be encoded into the table above and handled in the
2588 // If either of the casts are a bitcast from scalar to vector, disallow the
2589 // merging. However, any pair of bitcasts are allowed.
2590 bool IsFirstBitcast
= (firstOp
== Instruction::BitCast
);
2591 bool IsSecondBitcast
= (secondOp
== Instruction::BitCast
);
2592 bool AreBothBitcasts
= IsFirstBitcast
&& IsSecondBitcast
;
2594 // Check if any of the casts convert scalars <-> vectors.
2595 if ((IsFirstBitcast
&& isa
<VectorType
>(SrcTy
) != isa
<VectorType
>(MidTy
)) ||
2596 (IsSecondBitcast
&& isa
<VectorType
>(MidTy
) != isa
<VectorType
>(DstTy
)))
2597 if (!AreBothBitcasts
)
2600 int ElimCase
= CastResults
[firstOp
-Instruction::CastOpsBegin
]
2601 [secondOp
-Instruction::CastOpsBegin
];
2604 // Categorically disallowed.
2607 // Allowed, use first cast's opcode.
2610 // Allowed, use second cast's opcode.
2613 // No-op cast in second op implies firstOp as long as the DestTy
2614 // is integer and we are not converting between a vector and a
2616 if (!SrcTy
->isVectorTy() && DstTy
->isIntegerTy())
2620 // No-op cast in second op implies firstOp as long as the DestTy
2621 // is floating point.
2622 if (DstTy
->isFloatingPointTy())
2626 // No-op cast in first op implies secondOp as long as the SrcTy
2628 if (SrcTy
->isIntegerTy())
2632 // No-op cast in first op implies secondOp as long as the SrcTy
2633 // is a floating point.
2634 if (SrcTy
->isFloatingPointTy())
2638 // Cannot simplify if address spaces are different!
2639 if (SrcTy
->getPointerAddressSpace() != DstTy
->getPointerAddressSpace())
2642 unsigned MidSize
= MidTy
->getScalarSizeInBits();
2643 // We can still fold this without knowing the actual sizes as long we
2644 // know that the intermediate pointer is the largest possible
2646 // FIXME: Is this always true?
2648 return Instruction::BitCast
;
2650 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
2651 if (!SrcIntPtrTy
|| DstIntPtrTy
!= SrcIntPtrTy
)
2653 unsigned PtrSize
= SrcIntPtrTy
->getScalarSizeInBits();
2654 if (MidSize
>= PtrSize
)
2655 return Instruction::BitCast
;
2659 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2660 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2661 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
2662 unsigned SrcSize
= SrcTy
->getScalarSizeInBits();
2663 unsigned DstSize
= DstTy
->getScalarSizeInBits();
2664 if (SrcSize
== DstSize
)
2665 return Instruction::BitCast
;
2666 else if (SrcSize
< DstSize
)
2671 // zext, sext -> zext, because sext can't sign extend after zext
2672 return Instruction::ZExt
;
2674 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2677 unsigned PtrSize
= MidIntPtrTy
->getScalarSizeInBits();
2678 unsigned SrcSize
= SrcTy
->getScalarSizeInBits();
2679 unsigned DstSize
= DstTy
->getScalarSizeInBits();
2680 if (SrcSize
<= PtrSize
&& SrcSize
== DstSize
)
2681 return Instruction::BitCast
;
2685 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2686 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2687 if (SrcTy
->getPointerAddressSpace() != DstTy
->getPointerAddressSpace())
2688 return Instruction::AddrSpaceCast
;
2689 return Instruction::BitCast
;
2691 // FIXME: this state can be merged with (1), but the following assert
2692 // is useful to check the correcteness of the sequence due to semantic
2693 // change of bitcast.
2695 SrcTy
->isPtrOrPtrVectorTy() &&
2696 MidTy
->isPtrOrPtrVectorTy() &&
2697 DstTy
->isPtrOrPtrVectorTy() &&
2698 SrcTy
->getPointerAddressSpace() != MidTy
->getPointerAddressSpace() &&
2699 MidTy
->getPointerAddressSpace() == DstTy
->getPointerAddressSpace() &&
2700 "Illegal addrspacecast, bitcast sequence!");
2701 // Allowed, use first cast's opcode
2704 // bitcast, addrspacecast -> addrspacecast if the element type of
2705 // bitcast's source is the same as that of addrspacecast's destination.
2706 if (SrcTy
->getScalarType()->getPointerElementType() ==
2707 DstTy
->getScalarType()->getPointerElementType())
2708 return Instruction::AddrSpaceCast
;
2711 // FIXME: this state can be merged with (1), but the following assert
2712 // is useful to check the correcteness of the sequence due to semantic
2713 // change of bitcast.
2715 SrcTy
->isIntOrIntVectorTy() &&
2716 MidTy
->isPtrOrPtrVectorTy() &&
2717 DstTy
->isPtrOrPtrVectorTy() &&
2718 MidTy
->getPointerAddressSpace() == DstTy
->getPointerAddressSpace() &&
2719 "Illegal inttoptr, bitcast sequence!");
2720 // Allowed, use first cast's opcode
2723 // FIXME: this state can be merged with (2), but the following assert
2724 // is useful to check the correcteness of the sequence due to semantic
2725 // change of bitcast.
2727 SrcTy
->isPtrOrPtrVectorTy() &&
2728 MidTy
->isPtrOrPtrVectorTy() &&
2729 DstTy
->isIntOrIntVectorTy() &&
2730 SrcTy
->getPointerAddressSpace() == MidTy
->getPointerAddressSpace() &&
2731 "Illegal bitcast, ptrtoint sequence!");
2732 // Allowed, use second cast's opcode
2735 // (sitofp (zext x)) -> (uitofp x)
2736 return Instruction::UIToFP
;
2738 // Cast combination can't happen (error in input). This is for all cases
2739 // where the MidTy is not the same for the two cast instructions.
2740 llvm_unreachable("Invalid Cast Combination");
2742 llvm_unreachable("Error in CastResults table!!!");
2746 CastInst
*CastInst::Create(Instruction::CastOps op
, Value
*S
, Type
*Ty
,
2747 const Twine
&Name
, Instruction
*InsertBefore
) {
2748 assert(castIsValid(op
, S
, Ty
) && "Invalid cast!");
2749 // Construct and return the appropriate CastInst subclass
2751 case Trunc
: return new TruncInst (S
, Ty
, Name
, InsertBefore
);
2752 case ZExt
: return new ZExtInst (S
, Ty
, Name
, InsertBefore
);
2753 case SExt
: return new SExtInst (S
, Ty
, Name
, InsertBefore
);
2754 case FPTrunc
: return new FPTruncInst (S
, Ty
, Name
, InsertBefore
);
2755 case FPExt
: return new FPExtInst (S
, Ty
, Name
, InsertBefore
);
2756 case UIToFP
: return new UIToFPInst (S
, Ty
, Name
, InsertBefore
);
2757 case SIToFP
: return new SIToFPInst (S
, Ty
, Name
, InsertBefore
);
2758 case FPToUI
: return new FPToUIInst (S
, Ty
, Name
, InsertBefore
);
2759 case FPToSI
: return new FPToSIInst (S
, Ty
, Name
, InsertBefore
);
2760 case PtrToInt
: return new PtrToIntInst (S
, Ty
, Name
, InsertBefore
);
2761 case IntToPtr
: return new IntToPtrInst (S
, Ty
, Name
, InsertBefore
);
2762 case BitCast
: return new BitCastInst (S
, Ty
, Name
, InsertBefore
);
2763 case AddrSpaceCast
: return new AddrSpaceCastInst (S
, Ty
, Name
, InsertBefore
);
2764 default: llvm_unreachable("Invalid opcode provided");
2768 CastInst
*CastInst::Create(Instruction::CastOps op
, Value
*S
, Type
*Ty
,
2769 const Twine
&Name
, BasicBlock
*InsertAtEnd
) {
2770 assert(castIsValid(op
, S
, Ty
) && "Invalid cast!");
2771 // Construct and return the appropriate CastInst subclass
2773 case Trunc
: return new TruncInst (S
, Ty
, Name
, InsertAtEnd
);
2774 case ZExt
: return new ZExtInst (S
, Ty
, Name
, InsertAtEnd
);
2775 case SExt
: return new SExtInst (S
, Ty
, Name
, InsertAtEnd
);
2776 case FPTrunc
: return new FPTruncInst (S
, Ty
, Name
, InsertAtEnd
);
2777 case FPExt
: return new FPExtInst (S
, Ty
, Name
, InsertAtEnd
);
2778 case UIToFP
: return new UIToFPInst (S
, Ty
, Name
, InsertAtEnd
);
2779 case SIToFP
: return new SIToFPInst (S
, Ty
, Name
, InsertAtEnd
);
2780 case FPToUI
: return new FPToUIInst (S
, Ty
, Name
, InsertAtEnd
);
2781 case FPToSI
: return new FPToSIInst (S
, Ty
, Name
, InsertAtEnd
);
2782 case PtrToInt
: return new PtrToIntInst (S
, Ty
, Name
, InsertAtEnd
);
2783 case IntToPtr
: return new IntToPtrInst (S
, Ty
, Name
, InsertAtEnd
);
2784 case BitCast
: return new BitCastInst (S
, Ty
, Name
, InsertAtEnd
);
2785 case AddrSpaceCast
: return new AddrSpaceCastInst (S
, Ty
, Name
, InsertAtEnd
);
2786 default: llvm_unreachable("Invalid opcode provided");
2790 CastInst
*CastInst::CreateZExtOrBitCast(Value
*S
, Type
*Ty
,
2792 Instruction
*InsertBefore
) {
2793 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2794 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2795 return Create(Instruction::ZExt
, S
, Ty
, Name
, InsertBefore
);
2798 CastInst
*CastInst::CreateZExtOrBitCast(Value
*S
, Type
*Ty
,
2800 BasicBlock
*InsertAtEnd
) {
2801 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2802 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2803 return Create(Instruction::ZExt
, S
, Ty
, Name
, InsertAtEnd
);
2806 CastInst
*CastInst::CreateSExtOrBitCast(Value
*S
, Type
*Ty
,
2808 Instruction
*InsertBefore
) {
2809 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2810 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2811 return Create(Instruction::SExt
, S
, Ty
, Name
, InsertBefore
);
2814 CastInst
*CastInst::CreateSExtOrBitCast(Value
*S
, Type
*Ty
,
2816 BasicBlock
*InsertAtEnd
) {
2817 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2818 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2819 return Create(Instruction::SExt
, S
, Ty
, Name
, InsertAtEnd
);
2822 CastInst
*CastInst::CreateTruncOrBitCast(Value
*S
, Type
*Ty
,
2824 Instruction
*InsertBefore
) {
2825 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2826 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2827 return Create(Instruction::Trunc
, S
, Ty
, Name
, InsertBefore
);
2830 CastInst
*CastInst::CreateTruncOrBitCast(Value
*S
, Type
*Ty
,
2832 BasicBlock
*InsertAtEnd
) {
2833 if (S
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2834 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2835 return Create(Instruction::Trunc
, S
, Ty
, Name
, InsertAtEnd
);
2838 CastInst
*CastInst::CreatePointerCast(Value
*S
, Type
*Ty
,
2840 BasicBlock
*InsertAtEnd
) {
2841 assert(S
->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2842 assert((Ty
->isIntOrIntVectorTy() || Ty
->isPtrOrPtrVectorTy()) &&
2844 assert(Ty
->isVectorTy() == S
->getType()->isVectorTy() && "Invalid cast");
2845 assert((!Ty
->isVectorTy() ||
2846 Ty
->getVectorNumElements() == S
->getType()->getVectorNumElements()) &&
2849 if (Ty
->isIntOrIntVectorTy())
2850 return Create(Instruction::PtrToInt
, S
, Ty
, Name
, InsertAtEnd
);
2852 return CreatePointerBitCastOrAddrSpaceCast(S
, Ty
, Name
, InsertAtEnd
);
2855 /// Create a BitCast or a PtrToInt cast instruction
2856 CastInst
*CastInst::CreatePointerCast(Value
*S
, Type
*Ty
,
2858 Instruction
*InsertBefore
) {
2859 assert(S
->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2860 assert((Ty
->isIntOrIntVectorTy() || Ty
->isPtrOrPtrVectorTy()) &&
2862 assert(Ty
->isVectorTy() == S
->getType()->isVectorTy() && "Invalid cast");
2863 assert((!Ty
->isVectorTy() ||
2864 Ty
->getVectorNumElements() == S
->getType()->getVectorNumElements()) &&
2867 if (Ty
->isIntOrIntVectorTy())
2868 return Create(Instruction::PtrToInt
, S
, Ty
, Name
, InsertBefore
);
2870 return CreatePointerBitCastOrAddrSpaceCast(S
, Ty
, Name
, InsertBefore
);
2873 CastInst
*CastInst::CreatePointerBitCastOrAddrSpaceCast(
2876 BasicBlock
*InsertAtEnd
) {
2877 assert(S
->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2878 assert(Ty
->isPtrOrPtrVectorTy() && "Invalid cast");
2880 if (S
->getType()->getPointerAddressSpace() != Ty
->getPointerAddressSpace())
2881 return Create(Instruction::AddrSpaceCast
, S
, Ty
, Name
, InsertAtEnd
);
2883 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertAtEnd
);
2886 CastInst
*CastInst::CreatePointerBitCastOrAddrSpaceCast(
2889 Instruction
*InsertBefore
) {
2890 assert(S
->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2891 assert(Ty
->isPtrOrPtrVectorTy() && "Invalid cast");
2893 if (S
->getType()->getPointerAddressSpace() != Ty
->getPointerAddressSpace())
2894 return Create(Instruction::AddrSpaceCast
, S
, Ty
, Name
, InsertBefore
);
2896 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2899 CastInst
*CastInst::CreateBitOrPointerCast(Value
*S
, Type
*Ty
,
2901 Instruction
*InsertBefore
) {
2902 if (S
->getType()->isPointerTy() && Ty
->isIntegerTy())
2903 return Create(Instruction::PtrToInt
, S
, Ty
, Name
, InsertBefore
);
2904 if (S
->getType()->isIntegerTy() && Ty
->isPointerTy())
2905 return Create(Instruction::IntToPtr
, S
, Ty
, Name
, InsertBefore
);
2907 return Create(Instruction::BitCast
, S
, Ty
, Name
, InsertBefore
);
2910 CastInst
*CastInst::CreateIntegerCast(Value
*C
, Type
*Ty
,
2911 bool isSigned
, const Twine
&Name
,
2912 Instruction
*InsertBefore
) {
2913 assert(C
->getType()->isIntOrIntVectorTy() && Ty
->isIntOrIntVectorTy() &&
2914 "Invalid integer cast");
2915 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2916 unsigned DstBits
= Ty
->getScalarSizeInBits();
2917 Instruction::CastOps opcode
=
2918 (SrcBits
== DstBits
? Instruction::BitCast
:
2919 (SrcBits
> DstBits
? Instruction::Trunc
:
2920 (isSigned
? Instruction::SExt
: Instruction::ZExt
)));
2921 return Create(opcode
, C
, Ty
, Name
, InsertBefore
);
2924 CastInst
*CastInst::CreateIntegerCast(Value
*C
, Type
*Ty
,
2925 bool isSigned
, const Twine
&Name
,
2926 BasicBlock
*InsertAtEnd
) {
2927 assert(C
->getType()->isIntOrIntVectorTy() && Ty
->isIntOrIntVectorTy() &&
2929 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2930 unsigned DstBits
= Ty
->getScalarSizeInBits();
2931 Instruction::CastOps opcode
=
2932 (SrcBits
== DstBits
? Instruction::BitCast
:
2933 (SrcBits
> DstBits
? Instruction::Trunc
:
2934 (isSigned
? Instruction::SExt
: Instruction::ZExt
)));
2935 return Create(opcode
, C
, Ty
, Name
, InsertAtEnd
);
2938 CastInst
*CastInst::CreateFPCast(Value
*C
, Type
*Ty
,
2940 Instruction
*InsertBefore
) {
2941 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isFPOrFPVectorTy() &&
2943 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2944 unsigned DstBits
= Ty
->getScalarSizeInBits();
2945 Instruction::CastOps opcode
=
2946 (SrcBits
== DstBits
? Instruction::BitCast
:
2947 (SrcBits
> DstBits
? Instruction::FPTrunc
: Instruction::FPExt
));
2948 return Create(opcode
, C
, Ty
, Name
, InsertBefore
);
2951 CastInst
*CastInst::CreateFPCast(Value
*C
, Type
*Ty
,
2953 BasicBlock
*InsertAtEnd
) {
2954 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isFPOrFPVectorTy() &&
2956 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2957 unsigned DstBits
= Ty
->getScalarSizeInBits();
2958 Instruction::CastOps opcode
=
2959 (SrcBits
== DstBits
? Instruction::BitCast
:
2960 (SrcBits
> DstBits
? Instruction::FPTrunc
: Instruction::FPExt
));
2961 return Create(opcode
, C
, Ty
, Name
, InsertAtEnd
);
2964 // Check whether it is valid to call getCastOpcode for these types.
2965 // This routine must be kept in sync with getCastOpcode.
2966 bool CastInst::isCastable(Type
*SrcTy
, Type
*DestTy
) {
2967 if (!SrcTy
->isFirstClassType() || !DestTy
->isFirstClassType())
2970 if (SrcTy
== DestTy
)
2973 if (VectorType
*SrcVecTy
= dyn_cast
<VectorType
>(SrcTy
))
2974 if (VectorType
*DestVecTy
= dyn_cast
<VectorType
>(DestTy
))
2975 if (SrcVecTy
->getNumElements() == DestVecTy
->getNumElements()) {
2976 // An element by element cast. Valid if casting the elements is valid.
2977 SrcTy
= SrcVecTy
->getElementType();
2978 DestTy
= DestVecTy
->getElementType();
2981 // Get the bit sizes, we'll need these
2982 TypeSize SrcBits
= SrcTy
->getPrimitiveSizeInBits(); // 0 for ptr
2983 TypeSize DestBits
= DestTy
->getPrimitiveSizeInBits(); // 0 for ptr
2985 // Run through the possibilities ...
2986 if (DestTy
->isIntegerTy()) { // Casting to integral
2987 if (SrcTy
->isIntegerTy()) // Casting from integral
2989 if (SrcTy
->isFloatingPointTy()) // Casting from floating pt
2991 if (SrcTy
->isVectorTy()) // Casting from vector
2992 return DestBits
== SrcBits
;
2993 // Casting from something else
2994 return SrcTy
->isPointerTy();
2996 if (DestTy
->isFloatingPointTy()) { // Casting to floating pt
2997 if (SrcTy
->isIntegerTy()) // Casting from integral
2999 if (SrcTy
->isFloatingPointTy()) // Casting from floating pt
3001 if (SrcTy
->isVectorTy()) // Casting from vector
3002 return DestBits
== SrcBits
;
3003 // Casting from something else
3006 if (DestTy
->isVectorTy()) // Casting to vector
3007 return DestBits
== SrcBits
;
3008 if (DestTy
->isPointerTy()) { // Casting to pointer
3009 if (SrcTy
->isPointerTy()) // Casting from pointer
3011 return SrcTy
->isIntegerTy(); // Casting from integral
3013 if (DestTy
->isX86_MMXTy()) {
3014 if (SrcTy
->isVectorTy())
3015 return DestBits
== SrcBits
; // 64-bit vector to MMX
3017 } // Casting to something else
3021 bool CastInst::isBitCastable(Type
*SrcTy
, Type
*DestTy
) {
3022 if (!SrcTy
->isFirstClassType() || !DestTy
->isFirstClassType())
3025 if (SrcTy
== DestTy
)
3028 if (VectorType
*SrcVecTy
= dyn_cast
<VectorType
>(SrcTy
)) {
3029 if (VectorType
*DestVecTy
= dyn_cast
<VectorType
>(DestTy
)) {
3030 if (SrcVecTy
->getElementCount() == DestVecTy
->getElementCount()) {
3031 // An element by element cast. Valid if casting the elements is valid.
3032 SrcTy
= SrcVecTy
->getElementType();
3033 DestTy
= DestVecTy
->getElementType();
3038 if (PointerType
*DestPtrTy
= dyn_cast
<PointerType
>(DestTy
)) {
3039 if (PointerType
*SrcPtrTy
= dyn_cast
<PointerType
>(SrcTy
)) {
3040 return SrcPtrTy
->getAddressSpace() == DestPtrTy
->getAddressSpace();
3044 TypeSize SrcBits
= SrcTy
->getPrimitiveSizeInBits(); // 0 for ptr
3045 TypeSize DestBits
= DestTy
->getPrimitiveSizeInBits(); // 0 for ptr
3047 // Could still have vectors of pointers if the number of elements doesn't
3049 if (SrcBits
.getKnownMinSize() == 0 || DestBits
.getKnownMinSize() == 0)
3052 if (SrcBits
!= DestBits
)
3055 if (DestTy
->isX86_MMXTy() || SrcTy
->isX86_MMXTy())
3061 bool CastInst::isBitOrNoopPointerCastable(Type
*SrcTy
, Type
*DestTy
,
3062 const DataLayout
&DL
) {
3063 // ptrtoint and inttoptr are not allowed on non-integral pointers
3064 if (auto *PtrTy
= dyn_cast
<PointerType
>(SrcTy
))
3065 if (auto *IntTy
= dyn_cast
<IntegerType
>(DestTy
))
3066 return (IntTy
->getBitWidth() == DL
.getPointerTypeSizeInBits(PtrTy
) &&
3067 !DL
.isNonIntegralPointerType(PtrTy
));
3068 if (auto *PtrTy
= dyn_cast
<PointerType
>(DestTy
))
3069 if (auto *IntTy
= dyn_cast
<IntegerType
>(SrcTy
))
3070 return (IntTy
->getBitWidth() == DL
.getPointerTypeSizeInBits(PtrTy
) &&
3071 !DL
.isNonIntegralPointerType(PtrTy
));
3073 return isBitCastable(SrcTy
, DestTy
);
3076 // Provide a way to get a "cast" where the cast opcode is inferred from the
3077 // types and size of the operand. This, basically, is a parallel of the
3078 // logic in the castIsValid function below. This axiom should hold:
3079 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
3080 // should not assert in castIsValid. In other words, this produces a "correct"
3081 // casting opcode for the arguments passed to it.
3082 // This routine must be kept in sync with isCastable.
3083 Instruction::CastOps
3084 CastInst::getCastOpcode(
3085 const Value
*Src
, bool SrcIsSigned
, Type
*DestTy
, bool DestIsSigned
) {
3086 Type
*SrcTy
= Src
->getType();
3088 assert(SrcTy
->isFirstClassType() && DestTy
->isFirstClassType() &&
3089 "Only first class types are castable!");
3091 if (SrcTy
== DestTy
)
3094 // FIXME: Check address space sizes here
3095 if (VectorType
*SrcVecTy
= dyn_cast
<VectorType
>(SrcTy
))
3096 if (VectorType
*DestVecTy
= dyn_cast
<VectorType
>(DestTy
))
3097 if (SrcVecTy
->getNumElements() == DestVecTy
->getNumElements()) {
3098 // An element by element cast. Find the appropriate opcode based on the
3100 SrcTy
= SrcVecTy
->getElementType();
3101 DestTy
= DestVecTy
->getElementType();
3104 // Get the bit sizes, we'll need these
3105 unsigned SrcBits
= SrcTy
->getPrimitiveSizeInBits(); // 0 for ptr
3106 unsigned DestBits
= DestTy
->getPrimitiveSizeInBits(); // 0 for ptr
3108 // Run through the possibilities ...
3109 if (DestTy
->isIntegerTy()) { // Casting to integral
3110 if (SrcTy
->isIntegerTy()) { // Casting from integral
3111 if (DestBits
< SrcBits
)
3112 return Trunc
; // int -> smaller int
3113 else if (DestBits
> SrcBits
) { // its an extension
3115 return SExt
; // signed -> SEXT
3117 return ZExt
; // unsigned -> ZEXT
3119 return BitCast
; // Same size, No-op cast
3121 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
3123 return FPToSI
; // FP -> sint
3125 return FPToUI
; // FP -> uint
3126 } else if (SrcTy
->isVectorTy()) {
3127 assert(DestBits
== SrcBits
&&
3128 "Casting vector to integer of different width");
3129 return BitCast
; // Same size, no-op cast
3131 assert(SrcTy
->isPointerTy() &&
3132 "Casting from a value that is not first-class type");
3133 return PtrToInt
; // ptr -> int
3135 } else if (DestTy
->isFloatingPointTy()) { // Casting to floating pt
3136 if (SrcTy
->isIntegerTy()) { // Casting from integral
3138 return SIToFP
; // sint -> FP
3140 return UIToFP
; // uint -> FP
3141 } else if (SrcTy
->isFloatingPointTy()) { // Casting from floating pt
3142 if (DestBits
< SrcBits
) {
3143 return FPTrunc
; // FP -> smaller FP
3144 } else if (DestBits
> SrcBits
) {
3145 return FPExt
; // FP -> larger FP
3147 return BitCast
; // same size, no-op cast
3149 } else if (SrcTy
->isVectorTy()) {
3150 assert(DestBits
== SrcBits
&&
3151 "Casting vector to floating point of different width");
3152 return BitCast
; // same size, no-op cast
3154 llvm_unreachable("Casting pointer or non-first class to float");
3155 } else if (DestTy
->isVectorTy()) {
3156 assert(DestBits
== SrcBits
&&
3157 "Illegal cast to vector (wrong type or size)");
3159 } else if (DestTy
->isPointerTy()) {
3160 if (SrcTy
->isPointerTy()) {
3161 if (DestTy
->getPointerAddressSpace() != SrcTy
->getPointerAddressSpace())
3162 return AddrSpaceCast
;
3163 return BitCast
; // ptr -> ptr
3164 } else if (SrcTy
->isIntegerTy()) {
3165 return IntToPtr
; // int -> ptr
3167 llvm_unreachable("Casting pointer to other than pointer or int");
3168 } else if (DestTy
->isX86_MMXTy()) {
3169 if (SrcTy
->isVectorTy()) {
3170 assert(DestBits
== SrcBits
&& "Casting vector of wrong width to X86_MMX");
3171 return BitCast
; // 64-bit vector to MMX
3173 llvm_unreachable("Illegal cast to X86_MMX");
3175 llvm_unreachable("Casting to type that is not first-class");
3178 //===----------------------------------------------------------------------===//
3179 // CastInst SubClass Constructors
3180 //===----------------------------------------------------------------------===//
3182 /// Check that the construction parameters for a CastInst are correct. This
3183 /// could be broken out into the separate constructors but it is useful to have
3184 /// it in one place and to eliminate the redundant code for getting the sizes
3185 /// of the types involved.
3187 CastInst::castIsValid(Instruction::CastOps op
, Value
*S
, Type
*DstTy
) {
3188 // Check for type sanity on the arguments
3189 Type
*SrcTy
= S
->getType();
3191 if (!SrcTy
->isFirstClassType() || !DstTy
->isFirstClassType() ||
3192 SrcTy
->isAggregateType() || DstTy
->isAggregateType())
3195 // Get the size of the types in bits, we'll need this later
3196 unsigned SrcBitSize
= SrcTy
->getScalarSizeInBits();
3197 unsigned DstBitSize
= DstTy
->getScalarSizeInBits();
3199 // If these are vector types, get the lengths of the vectors (using zero for
3200 // scalar types means that checking that vector lengths match also checks that
3201 // scalars are not being converted to vectors or vectors to scalars).
3202 unsigned SrcLength
= SrcTy
->isVectorTy() ?
3203 cast
<VectorType
>(SrcTy
)->getNumElements() : 0;
3204 unsigned DstLength
= DstTy
->isVectorTy() ?
3205 cast
<VectorType
>(DstTy
)->getNumElements() : 0;
3207 // Switch on the opcode provided
3209 default: return false; // This is an input error
3210 case Instruction::Trunc
:
3211 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isIntOrIntVectorTy() &&
3212 SrcLength
== DstLength
&& SrcBitSize
> DstBitSize
;
3213 case Instruction::ZExt
:
3214 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isIntOrIntVectorTy() &&
3215 SrcLength
== DstLength
&& SrcBitSize
< DstBitSize
;
3216 case Instruction::SExt
:
3217 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isIntOrIntVectorTy() &&
3218 SrcLength
== DstLength
&& SrcBitSize
< DstBitSize
;
3219 case Instruction::FPTrunc
:
3220 return SrcTy
->isFPOrFPVectorTy() && DstTy
->isFPOrFPVectorTy() &&
3221 SrcLength
== DstLength
&& SrcBitSize
> DstBitSize
;
3222 case Instruction::FPExt
:
3223 return SrcTy
->isFPOrFPVectorTy() && DstTy
->isFPOrFPVectorTy() &&
3224 SrcLength
== DstLength
&& SrcBitSize
< DstBitSize
;
3225 case Instruction::UIToFP
:
3226 case Instruction::SIToFP
:
3227 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isFPOrFPVectorTy() &&
3228 SrcLength
== DstLength
;
3229 case Instruction::FPToUI
:
3230 case Instruction::FPToSI
:
3231 return SrcTy
->isFPOrFPVectorTy() && DstTy
->isIntOrIntVectorTy() &&
3232 SrcLength
== DstLength
;
3233 case Instruction::PtrToInt
:
3234 if (isa
<VectorType
>(SrcTy
) != isa
<VectorType
>(DstTy
))
3236 if (VectorType
*VT
= dyn_cast
<VectorType
>(SrcTy
))
3237 if (VT
->getNumElements() != cast
<VectorType
>(DstTy
)->getNumElements())
3239 return SrcTy
->isPtrOrPtrVectorTy() && DstTy
->isIntOrIntVectorTy();
3240 case Instruction::IntToPtr
:
3241 if (isa
<VectorType
>(SrcTy
) != isa
<VectorType
>(DstTy
))
3243 if (VectorType
*VT
= dyn_cast
<VectorType
>(SrcTy
))
3244 if (VT
->getNumElements() != cast
<VectorType
>(DstTy
)->getNumElements())
3246 return SrcTy
->isIntOrIntVectorTy() && DstTy
->isPtrOrPtrVectorTy();
3247 case Instruction::BitCast
: {
3248 PointerType
*SrcPtrTy
= dyn_cast
<PointerType
>(SrcTy
->getScalarType());
3249 PointerType
*DstPtrTy
= dyn_cast
<PointerType
>(DstTy
->getScalarType());
3251 // BitCast implies a no-op cast of type only. No bits change.
3252 // However, you can't cast pointers to anything but pointers.
3253 if (!SrcPtrTy
!= !DstPtrTy
)
3256 // For non-pointer cases, the cast is okay if the source and destination bit
3257 // widths are identical.
3259 return SrcTy
->getPrimitiveSizeInBits() == DstTy
->getPrimitiveSizeInBits();
3261 // If both are pointers then the address spaces must match.
3262 if (SrcPtrTy
->getAddressSpace() != DstPtrTy
->getAddressSpace())
3265 // A vector of pointers must have the same number of elements.
3266 VectorType
*SrcVecTy
= dyn_cast
<VectorType
>(SrcTy
);
3267 VectorType
*DstVecTy
= dyn_cast
<VectorType
>(DstTy
);
3268 if (SrcVecTy
&& DstVecTy
)
3269 return (SrcVecTy
->getNumElements() == DstVecTy
->getNumElements());
3271 return SrcVecTy
->getNumElements() == 1;
3273 return DstVecTy
->getNumElements() == 1;
3277 case Instruction::AddrSpaceCast
: {
3278 PointerType
*SrcPtrTy
= dyn_cast
<PointerType
>(SrcTy
->getScalarType());
3282 PointerType
*DstPtrTy
= dyn_cast
<PointerType
>(DstTy
->getScalarType());
3286 if (SrcPtrTy
->getAddressSpace() == DstPtrTy
->getAddressSpace())
3289 if (VectorType
*SrcVecTy
= dyn_cast
<VectorType
>(SrcTy
)) {
3290 if (VectorType
*DstVecTy
= dyn_cast
<VectorType
>(DstTy
))
3291 return (SrcVecTy
->getNumElements() == DstVecTy
->getNumElements());
3301 TruncInst::TruncInst(
3302 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3303 ) : CastInst(Ty
, Trunc
, S
, Name
, InsertBefore
) {
3304 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal Trunc");
3307 TruncInst::TruncInst(
3308 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3309 ) : CastInst(Ty
, Trunc
, S
, Name
, InsertAtEnd
) {
3310 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal Trunc");
3314 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3315 ) : CastInst(Ty
, ZExt
, S
, Name
, InsertBefore
) {
3316 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal ZExt");
3320 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3321 ) : CastInst(Ty
, ZExt
, S
, Name
, InsertAtEnd
) {
3322 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal ZExt");
3325 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3326 ) : CastInst(Ty
, SExt
, S
, Name
, InsertBefore
) {
3327 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SExt");
3331 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3332 ) : CastInst(Ty
, SExt
, S
, Name
, InsertAtEnd
) {
3333 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SExt");
3336 FPTruncInst::FPTruncInst(
3337 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3338 ) : CastInst(Ty
, FPTrunc
, S
, Name
, InsertBefore
) {
3339 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPTrunc");
3342 FPTruncInst::FPTruncInst(
3343 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3344 ) : CastInst(Ty
, FPTrunc
, S
, Name
, InsertAtEnd
) {
3345 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPTrunc");
3348 FPExtInst::FPExtInst(
3349 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3350 ) : CastInst(Ty
, FPExt
, S
, Name
, InsertBefore
) {
3351 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPExt");
3354 FPExtInst::FPExtInst(
3355 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3356 ) : CastInst(Ty
, FPExt
, S
, Name
, InsertAtEnd
) {
3357 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPExt");
3360 UIToFPInst::UIToFPInst(
3361 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3362 ) : CastInst(Ty
, UIToFP
, S
, Name
, InsertBefore
) {
3363 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal UIToFP");
3366 UIToFPInst::UIToFPInst(
3367 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3368 ) : CastInst(Ty
, UIToFP
, S
, Name
, InsertAtEnd
) {
3369 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal UIToFP");
3372 SIToFPInst::SIToFPInst(
3373 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3374 ) : CastInst(Ty
, SIToFP
, S
, Name
, InsertBefore
) {
3375 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SIToFP");
3378 SIToFPInst::SIToFPInst(
3379 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3380 ) : CastInst(Ty
, SIToFP
, S
, Name
, InsertAtEnd
) {
3381 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal SIToFP");
3384 FPToUIInst::FPToUIInst(
3385 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3386 ) : CastInst(Ty
, FPToUI
, S
, Name
, InsertBefore
) {
3387 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToUI");
3390 FPToUIInst::FPToUIInst(
3391 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3392 ) : CastInst(Ty
, FPToUI
, S
, Name
, InsertAtEnd
) {
3393 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToUI");
3396 FPToSIInst::FPToSIInst(
3397 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3398 ) : CastInst(Ty
, FPToSI
, S
, Name
, InsertBefore
) {
3399 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToSI");
3402 FPToSIInst::FPToSIInst(
3403 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3404 ) : CastInst(Ty
, FPToSI
, S
, Name
, InsertAtEnd
) {
3405 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal FPToSI");
3408 PtrToIntInst::PtrToIntInst(
3409 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3410 ) : CastInst(Ty
, PtrToInt
, S
, Name
, InsertBefore
) {
3411 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal PtrToInt");
3414 PtrToIntInst::PtrToIntInst(
3415 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3416 ) : CastInst(Ty
, PtrToInt
, S
, Name
, InsertAtEnd
) {
3417 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal PtrToInt");
3420 IntToPtrInst::IntToPtrInst(
3421 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3422 ) : CastInst(Ty
, IntToPtr
, S
, Name
, InsertBefore
) {
3423 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal IntToPtr");
3426 IntToPtrInst::IntToPtrInst(
3427 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3428 ) : CastInst(Ty
, IntToPtr
, S
, Name
, InsertAtEnd
) {
3429 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal IntToPtr");
3432 BitCastInst::BitCastInst(
3433 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3434 ) : CastInst(Ty
, BitCast
, S
, Name
, InsertBefore
) {
3435 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal BitCast");
3438 BitCastInst::BitCastInst(
3439 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3440 ) : CastInst(Ty
, BitCast
, S
, Name
, InsertAtEnd
) {
3441 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal BitCast");
3444 AddrSpaceCastInst::AddrSpaceCastInst(
3445 Value
*S
, Type
*Ty
, const Twine
&Name
, Instruction
*InsertBefore
3446 ) : CastInst(Ty
, AddrSpaceCast
, S
, Name
, InsertBefore
) {
3447 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal AddrSpaceCast");
3450 AddrSpaceCastInst::AddrSpaceCastInst(
3451 Value
*S
, Type
*Ty
, const Twine
&Name
, BasicBlock
*InsertAtEnd
3452 ) : CastInst(Ty
, AddrSpaceCast
, S
, Name
, InsertAtEnd
) {
3453 assert(castIsValid(getOpcode(), S
, Ty
) && "Illegal AddrSpaceCast");
3456 //===----------------------------------------------------------------------===//
3458 //===----------------------------------------------------------------------===//
3460 CmpInst::CmpInst(Type
*ty
, OtherOps op
, Predicate predicate
, Value
*LHS
,
3461 Value
*RHS
, const Twine
&Name
, Instruction
*InsertBefore
,
3462 Instruction
*FlagsSource
)
3463 : Instruction(ty
, op
,
3464 OperandTraits
<CmpInst
>::op_begin(this),
3465 OperandTraits
<CmpInst
>::operands(this),
3469 setPredicate((Predicate
)predicate
);
3472 copyIRFlags(FlagsSource
);
3475 CmpInst::CmpInst(Type
*ty
, OtherOps op
, Predicate predicate
, Value
*LHS
,
3476 Value
*RHS
, const Twine
&Name
, BasicBlock
*InsertAtEnd
)
3477 : Instruction(ty
, op
,
3478 OperandTraits
<CmpInst
>::op_begin(this),
3479 OperandTraits
<CmpInst
>::operands(this),
3483 setPredicate((Predicate
)predicate
);
3488 CmpInst::Create(OtherOps Op
, Predicate predicate
, Value
*S1
, Value
*S2
,
3489 const Twine
&Name
, Instruction
*InsertBefore
) {
3490 if (Op
== Instruction::ICmp
) {
3492 return new ICmpInst(InsertBefore
, CmpInst::Predicate(predicate
),
3495 return new ICmpInst(CmpInst::Predicate(predicate
),
3500 return new FCmpInst(InsertBefore
, CmpInst::Predicate(predicate
),
3503 return new FCmpInst(CmpInst::Predicate(predicate
),
3508 CmpInst::Create(OtherOps Op
, Predicate predicate
, Value
*S1
, Value
*S2
,
3509 const Twine
&Name
, BasicBlock
*InsertAtEnd
) {
3510 if (Op
== Instruction::ICmp
) {
3511 return new ICmpInst(*InsertAtEnd
, CmpInst::Predicate(predicate
),
3514 return new FCmpInst(*InsertAtEnd
, CmpInst::Predicate(predicate
),
3518 void CmpInst::swapOperands() {
3519 if (ICmpInst
*IC
= dyn_cast
<ICmpInst
>(this))
3522 cast
<FCmpInst
>(this)->swapOperands();
3525 bool CmpInst::isCommutative() const {
3526 if (const ICmpInst
*IC
= dyn_cast
<ICmpInst
>(this))
3527 return IC
->isCommutative();
3528 return cast
<FCmpInst
>(this)->isCommutative();
3531 bool CmpInst::isEquality() const {
3532 if (const ICmpInst
*IC
= dyn_cast
<ICmpInst
>(this))
3533 return IC
->isEquality();
3534 return cast
<FCmpInst
>(this)->isEquality();
3537 CmpInst::Predicate
CmpInst::getInversePredicate(Predicate pred
) {
3539 default: llvm_unreachable("Unknown cmp predicate!");
3540 case ICMP_EQ
: return ICMP_NE
;
3541 case ICMP_NE
: return ICMP_EQ
;
3542 case ICMP_UGT
: return ICMP_ULE
;
3543 case ICMP_ULT
: return ICMP_UGE
;
3544 case ICMP_UGE
: return ICMP_ULT
;
3545 case ICMP_ULE
: return ICMP_UGT
;
3546 case ICMP_SGT
: return ICMP_SLE
;
3547 case ICMP_SLT
: return ICMP_SGE
;
3548 case ICMP_SGE
: return ICMP_SLT
;
3549 case ICMP_SLE
: return ICMP_SGT
;
3551 case FCMP_OEQ
: return FCMP_UNE
;
3552 case FCMP_ONE
: return FCMP_UEQ
;
3553 case FCMP_OGT
: return FCMP_ULE
;
3554 case FCMP_OLT
: return FCMP_UGE
;
3555 case FCMP_OGE
: return FCMP_ULT
;
3556 case FCMP_OLE
: return FCMP_UGT
;
3557 case FCMP_UEQ
: return FCMP_ONE
;
3558 case FCMP_UNE
: return FCMP_OEQ
;
3559 case FCMP_UGT
: return FCMP_OLE
;
3560 case FCMP_ULT
: return FCMP_OGE
;
3561 case FCMP_UGE
: return FCMP_OLT
;
3562 case FCMP_ULE
: return FCMP_OGT
;
3563 case FCMP_ORD
: return FCMP_UNO
;
3564 case FCMP_UNO
: return FCMP_ORD
;
3565 case FCMP_TRUE
: return FCMP_FALSE
;
3566 case FCMP_FALSE
: return FCMP_TRUE
;
3570 StringRef
CmpInst::getPredicateName(Predicate Pred
) {
3572 default: return "unknown";
3573 case FCmpInst::FCMP_FALSE
: return "false";
3574 case FCmpInst::FCMP_OEQ
: return "oeq";
3575 case FCmpInst::FCMP_OGT
: return "ogt";
3576 case FCmpInst::FCMP_OGE
: return "oge";
3577 case FCmpInst::FCMP_OLT
: return "olt";
3578 case FCmpInst::FCMP_OLE
: return "ole";
3579 case FCmpInst::FCMP_ONE
: return "one";
3580 case FCmpInst::FCMP_ORD
: return "ord";
3581 case FCmpInst::FCMP_UNO
: return "uno";
3582 case FCmpInst::FCMP_UEQ
: return "ueq";
3583 case FCmpInst::FCMP_UGT
: return "ugt";
3584 case FCmpInst::FCMP_UGE
: return "uge";
3585 case FCmpInst::FCMP_ULT
: return "ult";
3586 case FCmpInst::FCMP_ULE
: return "ule";
3587 case FCmpInst::FCMP_UNE
: return "une";
3588 case FCmpInst::FCMP_TRUE
: return "true";
3589 case ICmpInst::ICMP_EQ
: return "eq";
3590 case ICmpInst::ICMP_NE
: return "ne";
3591 case ICmpInst::ICMP_SGT
: return "sgt";
3592 case ICmpInst::ICMP_SGE
: return "sge";
3593 case ICmpInst::ICMP_SLT
: return "slt";
3594 case ICmpInst::ICMP_SLE
: return "sle";
3595 case ICmpInst::ICMP_UGT
: return "ugt";
3596 case ICmpInst::ICMP_UGE
: return "uge";
3597 case ICmpInst::ICMP_ULT
: return "ult";
3598 case ICmpInst::ICMP_ULE
: return "ule";
3602 ICmpInst::Predicate
ICmpInst::getSignedPredicate(Predicate pred
) {
3604 default: llvm_unreachable("Unknown icmp predicate!");
3605 case ICMP_EQ
: case ICMP_NE
:
3606 case ICMP_SGT
: case ICMP_SLT
: case ICMP_SGE
: case ICMP_SLE
:
3608 case ICMP_UGT
: return ICMP_SGT
;
3609 case ICMP_ULT
: return ICMP_SLT
;
3610 case ICMP_UGE
: return ICMP_SGE
;
3611 case ICMP_ULE
: return ICMP_SLE
;
3615 ICmpInst::Predicate
ICmpInst::getUnsignedPredicate(Predicate pred
) {
3617 default: llvm_unreachable("Unknown icmp predicate!");
3618 case ICMP_EQ
: case ICMP_NE
:
3619 case ICMP_UGT
: case ICMP_ULT
: case ICMP_UGE
: case ICMP_ULE
:
3621 case ICMP_SGT
: return ICMP_UGT
;
3622 case ICMP_SLT
: return ICMP_ULT
;
3623 case ICMP_SGE
: return ICMP_UGE
;
3624 case ICMP_SLE
: return ICMP_ULE
;
3628 CmpInst::Predicate
CmpInst::getFlippedStrictnessPredicate(Predicate pred
) {
3630 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3631 case ICMP_SGT
: return ICMP_SGE
;
3632 case ICMP_SLT
: return ICMP_SLE
;
3633 case ICMP_SGE
: return ICMP_SGT
;
3634 case ICMP_SLE
: return ICMP_SLT
;
3635 case ICMP_UGT
: return ICMP_UGE
;
3636 case ICMP_ULT
: return ICMP_ULE
;
3637 case ICMP_UGE
: return ICMP_UGT
;
3638 case ICMP_ULE
: return ICMP_ULT
;
3640 case FCMP_OGT
: return FCMP_OGE
;
3641 case FCMP_OLT
: return FCMP_OLE
;
3642 case FCMP_OGE
: return FCMP_OGT
;
3643 case FCMP_OLE
: return FCMP_OLT
;
3644 case FCMP_UGT
: return FCMP_UGE
;
3645 case FCMP_ULT
: return FCMP_ULE
;
3646 case FCMP_UGE
: return FCMP_UGT
;
3647 case FCMP_ULE
: return FCMP_ULT
;
3651 CmpInst::Predicate
CmpInst::getSwappedPredicate(Predicate pred
) {
3653 default: llvm_unreachable("Unknown cmp predicate!");
3654 case ICMP_EQ
: case ICMP_NE
:
3656 case ICMP_SGT
: return ICMP_SLT
;
3657 case ICMP_SLT
: return ICMP_SGT
;
3658 case ICMP_SGE
: return ICMP_SLE
;
3659 case ICMP_SLE
: return ICMP_SGE
;
3660 case ICMP_UGT
: return ICMP_ULT
;
3661 case ICMP_ULT
: return ICMP_UGT
;
3662 case ICMP_UGE
: return ICMP_ULE
;
3663 case ICMP_ULE
: return ICMP_UGE
;
3665 case FCMP_FALSE
: case FCMP_TRUE
:
3666 case FCMP_OEQ
: case FCMP_ONE
:
3667 case FCMP_UEQ
: case FCMP_UNE
:
3668 case FCMP_ORD
: case FCMP_UNO
:
3670 case FCMP_OGT
: return FCMP_OLT
;
3671 case FCMP_OLT
: return FCMP_OGT
;
3672 case FCMP_OGE
: return FCMP_OLE
;
3673 case FCMP_OLE
: return FCMP_OGE
;
3674 case FCMP_UGT
: return FCMP_ULT
;
3675 case FCMP_ULT
: return FCMP_UGT
;
3676 case FCMP_UGE
: return FCMP_ULE
;
3677 case FCMP_ULE
: return FCMP_UGE
;
3681 CmpInst::Predicate
CmpInst::getNonStrictPredicate(Predicate pred
) {
3683 case ICMP_SGT
: return ICMP_SGE
;
3684 case ICMP_SLT
: return ICMP_SLE
;
3685 case ICMP_UGT
: return ICMP_UGE
;
3686 case ICMP_ULT
: return ICMP_ULE
;
3687 case FCMP_OGT
: return FCMP_OGE
;
3688 case FCMP_OLT
: return FCMP_OLE
;
3689 case FCMP_UGT
: return FCMP_UGE
;
3690 case FCMP_ULT
: return FCMP_ULE
;
3691 default: return pred
;
3695 CmpInst::Predicate
CmpInst::getSignedPredicate(Predicate pred
) {
3696 assert(CmpInst::isUnsigned(pred
) && "Call only with signed predicates!");
3700 llvm_unreachable("Unknown predicate!");
3701 case CmpInst::ICMP_ULT
:
3702 return CmpInst::ICMP_SLT
;
3703 case CmpInst::ICMP_ULE
:
3704 return CmpInst::ICMP_SLE
;
3705 case CmpInst::ICMP_UGT
:
3706 return CmpInst::ICMP_SGT
;
3707 case CmpInst::ICMP_UGE
:
3708 return CmpInst::ICMP_SGE
;
3712 bool CmpInst::isUnsigned(Predicate predicate
) {
3713 switch (predicate
) {
3714 default: return false;
3715 case ICmpInst::ICMP_ULT
: case ICmpInst::ICMP_ULE
: case ICmpInst::ICMP_UGT
:
3716 case ICmpInst::ICMP_UGE
: return true;
3720 bool CmpInst::isSigned(Predicate predicate
) {
3721 switch (predicate
) {
3722 default: return false;
3723 case ICmpInst::ICMP_SLT
: case ICmpInst::ICMP_SLE
: case ICmpInst::ICMP_SGT
:
3724 case ICmpInst::ICMP_SGE
: return true;
3728 bool CmpInst::isOrdered(Predicate predicate
) {
3729 switch (predicate
) {
3730 default: return false;
3731 case FCmpInst::FCMP_OEQ
: case FCmpInst::FCMP_ONE
: case FCmpInst::FCMP_OGT
:
3732 case FCmpInst::FCMP_OLT
: case FCmpInst::FCMP_OGE
: case FCmpInst::FCMP_OLE
:
3733 case FCmpInst::FCMP_ORD
: return true;
3737 bool CmpInst::isUnordered(Predicate predicate
) {
3738 switch (predicate
) {
3739 default: return false;
3740 case FCmpInst::FCMP_UEQ
: case FCmpInst::FCMP_UNE
: case FCmpInst::FCMP_UGT
:
3741 case FCmpInst::FCMP_ULT
: case FCmpInst::FCMP_UGE
: case FCmpInst::FCMP_ULE
:
3742 case FCmpInst::FCMP_UNO
: return true;
3746 bool CmpInst::isTrueWhenEqual(Predicate predicate
) {
3748 default: return false;
3749 case ICMP_EQ
: case ICMP_UGE
: case ICMP_ULE
: case ICMP_SGE
: case ICMP_SLE
:
3750 case FCMP_TRUE
: case FCMP_UEQ
: case FCMP_UGE
: case FCMP_ULE
: return true;
3754 bool CmpInst::isFalseWhenEqual(Predicate predicate
) {
3756 case ICMP_NE
: case ICMP_UGT
: case ICMP_ULT
: case ICMP_SGT
: case ICMP_SLT
:
3757 case FCMP_FALSE
: case FCMP_ONE
: case FCMP_OGT
: case FCMP_OLT
: return true;
3758 default: return false;
3762 bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1
, Predicate Pred2
) {
3763 // If the predicates match, then we know the first condition implies the
3772 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
3773 return Pred2
== ICMP_UGE
|| Pred2
== ICMP_ULE
|| Pred2
== ICMP_SGE
||
3775 case ICMP_UGT
: // A >u B implies A != B and A >=u B are true.
3776 return Pred2
== ICMP_NE
|| Pred2
== ICMP_UGE
;
3777 case ICMP_ULT
: // A <u B implies A != B and A <=u B are true.
3778 return Pred2
== ICMP_NE
|| Pred2
== ICMP_ULE
;
3779 case ICMP_SGT
: // A >s B implies A != B and A >=s B are true.
3780 return Pred2
== ICMP_NE
|| Pred2
== ICMP_SGE
;
3781 case ICMP_SLT
: // A <s B implies A != B and A <=s B are true.
3782 return Pred2
== ICMP_NE
|| Pred2
== ICMP_SLE
;
3787 bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1
, Predicate Pred2
) {
3788 return isImpliedTrueByMatchingCmp(Pred1
, getInversePredicate(Pred2
));
3791 //===----------------------------------------------------------------------===//
3792 // SwitchInst Implementation
3793 //===----------------------------------------------------------------------===//
3795 void SwitchInst::init(Value
*Value
, BasicBlock
*Default
, unsigned NumReserved
) {
3796 assert(Value
&& Default
&& NumReserved
);
3797 ReservedSpace
= NumReserved
;
3798 setNumHungOffUseOperands(2);
3799 allocHungoffUses(ReservedSpace
);
3805 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
3806 /// switch on and a default destination. The number of additional cases can
3807 /// be specified here to make memory allocation more efficient. This
3808 /// constructor can also autoinsert before another instruction.
3809 SwitchInst::SwitchInst(Value
*Value
, BasicBlock
*Default
, unsigned NumCases
,
3810 Instruction
*InsertBefore
)
3811 : Instruction(Type::getVoidTy(Value
->getContext()), Instruction::Switch
,
3812 nullptr, 0, InsertBefore
) {
3813 init(Value
, Default
, 2+NumCases
*2);
3816 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
3817 /// switch on and a default destination. The number of additional cases can
3818 /// be specified here to make memory allocation more efficient. This
3819 /// constructor also autoinserts at the end of the specified BasicBlock.
3820 SwitchInst::SwitchInst(Value
*Value
, BasicBlock
*Default
, unsigned NumCases
,
3821 BasicBlock
*InsertAtEnd
)
3822 : Instruction(Type::getVoidTy(Value
->getContext()), Instruction::Switch
,
3823 nullptr, 0, InsertAtEnd
) {
3824 init(Value
, Default
, 2+NumCases
*2);
3827 SwitchInst::SwitchInst(const SwitchInst
&SI
)
3828 : Instruction(SI
.getType(), Instruction::Switch
, nullptr, 0) {
3829 init(SI
.getCondition(), SI
.getDefaultDest(), SI
.getNumOperands());
3830 setNumHungOffUseOperands(SI
.getNumOperands());
3831 Use
*OL
= getOperandList();
3832 const Use
*InOL
= SI
.getOperandList();
3833 for (unsigned i
= 2, E
= SI
.getNumOperands(); i
!= E
; i
+= 2) {
3835 OL
[i
+1] = InOL
[i
+1];
3837 SubclassOptionalData
= SI
.SubclassOptionalData
;
3840 /// addCase - Add an entry to the switch instruction...
3842 void SwitchInst::addCase(ConstantInt
*OnVal
, BasicBlock
*Dest
) {
3843 unsigned NewCaseIdx
= getNumCases();
3844 unsigned OpNo
= getNumOperands();
3845 if (OpNo
+2 > ReservedSpace
)
3846 growOperands(); // Get more space!
3847 // Initialize some new operands.
3848 assert(OpNo
+1 < ReservedSpace
&& "Growing didn't work!");
3849 setNumHungOffUseOperands(OpNo
+2);
3850 CaseHandle
Case(this, NewCaseIdx
);
3851 Case
.setValue(OnVal
);
3852 Case
.setSuccessor(Dest
);
3855 /// removeCase - This method removes the specified case and its successor
3856 /// from the switch instruction.
3857 SwitchInst::CaseIt
SwitchInst::removeCase(CaseIt I
) {
3858 unsigned idx
= I
->getCaseIndex();
3860 assert(2 + idx
*2 < getNumOperands() && "Case index out of range!!!");
3862 unsigned NumOps
= getNumOperands();
3863 Use
*OL
= getOperandList();
3865 // Overwrite this case with the end of the list.
3866 if (2 + (idx
+ 1) * 2 != NumOps
) {
3867 OL
[2 + idx
* 2] = OL
[NumOps
- 2];
3868 OL
[2 + idx
* 2 + 1] = OL
[NumOps
- 1];
3871 // Nuke the last value.
3872 OL
[NumOps
-2].set(nullptr);
3873 OL
[NumOps
-2+1].set(nullptr);
3874 setNumHungOffUseOperands(NumOps
-2);
3876 return CaseIt(this, idx
);
3879 /// growOperands - grow operands - This grows the operand list in response
3880 /// to a push_back style of operation. This grows the number of ops by 3 times.
3882 void SwitchInst::growOperands() {
3883 unsigned e
= getNumOperands();
3884 unsigned NumOps
= e
*3;
3886 ReservedSpace
= NumOps
;
3887 growHungoffUses(ReservedSpace
);
3891 SwitchInstProfUpdateWrapper::getProfBranchWeightsMD(const SwitchInst
&SI
) {
3892 if (MDNode
*ProfileData
= SI
.getMetadata(LLVMContext::MD_prof
))
3893 if (auto *MDName
= dyn_cast
<MDString
>(ProfileData
->getOperand(0)))
3894 if (MDName
->getString() == "branch_weights")
3899 MDNode
*SwitchInstProfUpdateWrapper::buildProfBranchWeightsMD() {
3900 assert(Changed
&& "called only if metadata has changed");
3905 assert(SI
.getNumSuccessors() == Weights
->size() &&
3906 "num of prof branch_weights must accord with num of successors");
3909 all_of(Weights
.getValue(), [](uint32_t W
) { return W
== 0; });
3911 if (AllZeroes
|| Weights
.getValue().size() < 2)
3914 return MDBuilder(SI
.getParent()->getContext()).createBranchWeights(*Weights
);
3917 void SwitchInstProfUpdateWrapper::init() {
3918 MDNode
*ProfileData
= getProfBranchWeightsMD(SI
);
3922 if (ProfileData
->getNumOperands() != SI
.getNumSuccessors() + 1) {
3923 llvm_unreachable("number of prof branch_weights metadata operands does "
3924 "not correspond to number of succesors");
3927 SmallVector
<uint32_t, 8> Weights
;
3928 for (unsigned CI
= 1, CE
= SI
.getNumSuccessors(); CI
<= CE
; ++CI
) {
3929 ConstantInt
*C
= mdconst::extract
<ConstantInt
>(ProfileData
->getOperand(CI
));
3930 uint32_t CW
= C
->getValue().getZExtValue();
3931 Weights
.push_back(CW
);
3933 this->Weights
= std::move(Weights
);
3937 SwitchInstProfUpdateWrapper::removeCase(SwitchInst::CaseIt I
) {
3939 assert(SI
.getNumSuccessors() == Weights
->size() &&
3940 "num of prof branch_weights must accord with num of successors");
3942 // Copy the last case to the place of the removed one and shrink.
3943 // This is tightly coupled with the way SwitchInst::removeCase() removes
3944 // the cases in SwitchInst::removeCase(CaseIt).
3945 Weights
.getValue()[I
->getCaseIndex() + 1] = Weights
.getValue().back();
3946 Weights
.getValue().pop_back();
3948 return SI
.removeCase(I
);
3951 void SwitchInstProfUpdateWrapper::addCase(
3952 ConstantInt
*OnVal
, BasicBlock
*Dest
,
3953 SwitchInstProfUpdateWrapper::CaseWeightOpt W
) {
3954 SI
.addCase(OnVal
, Dest
);
3956 if (!Weights
&& W
&& *W
) {
3958 Weights
= SmallVector
<uint32_t, 8>(SI
.getNumSuccessors(), 0);
3959 Weights
.getValue()[SI
.getNumSuccessors() - 1] = *W
;
3960 } else if (Weights
) {
3962 Weights
.getValue().push_back(W
? *W
: 0);
3965 assert(SI
.getNumSuccessors() == Weights
->size() &&
3966 "num of prof branch_weights must accord with num of successors");
3969 SymbolTableList
<Instruction
>::iterator
3970 SwitchInstProfUpdateWrapper::eraseFromParent() {
3971 // Instruction is erased. Mark as unchanged to not touch it in the destructor.
3975 return SI
.eraseFromParent();
3978 SwitchInstProfUpdateWrapper::CaseWeightOpt
3979 SwitchInstProfUpdateWrapper::getSuccessorWeight(unsigned idx
) {
3982 return Weights
.getValue()[idx
];
3985 void SwitchInstProfUpdateWrapper::setSuccessorWeight(
3986 unsigned idx
, SwitchInstProfUpdateWrapper::CaseWeightOpt W
) {
3991 Weights
= SmallVector
<uint32_t, 8>(SI
.getNumSuccessors(), 0);
3994 auto &OldW
= Weights
.getValue()[idx
];
4002 SwitchInstProfUpdateWrapper::CaseWeightOpt
4003 SwitchInstProfUpdateWrapper::getSuccessorWeight(const SwitchInst
&SI
,
4005 if (MDNode
*ProfileData
= getProfBranchWeightsMD(SI
))
4006 if (ProfileData
->getNumOperands() == SI
.getNumSuccessors() + 1)
4007 return mdconst::extract
<ConstantInt
>(ProfileData
->getOperand(idx
+ 1))
4014 //===----------------------------------------------------------------------===//
4015 // IndirectBrInst Implementation
4016 //===----------------------------------------------------------------------===//
4018 void IndirectBrInst::init(Value
*Address
, unsigned NumDests
) {
4019 assert(Address
&& Address
->getType()->isPointerTy() &&
4020 "Address of indirectbr must be a pointer");
4021 ReservedSpace
= 1+NumDests
;
4022 setNumHungOffUseOperands(1);
4023 allocHungoffUses(ReservedSpace
);
4029 /// growOperands - grow operands - This grows the operand list in response
4030 /// to a push_back style of operation. This grows the number of ops by 2 times.
4032 void IndirectBrInst::growOperands() {
4033 unsigned e
= getNumOperands();
4034 unsigned NumOps
= e
*2;
4036 ReservedSpace
= NumOps
;
4037 growHungoffUses(ReservedSpace
);
4040 IndirectBrInst::IndirectBrInst(Value
*Address
, unsigned NumCases
,
4041 Instruction
*InsertBefore
)
4042 : Instruction(Type::getVoidTy(Address
->getContext()),
4043 Instruction::IndirectBr
, nullptr, 0, InsertBefore
) {
4044 init(Address
, NumCases
);
4047 IndirectBrInst::IndirectBrInst(Value
*Address
, unsigned NumCases
,
4048 BasicBlock
*InsertAtEnd
)
4049 : Instruction(Type::getVoidTy(Address
->getContext()),
4050 Instruction::IndirectBr
, nullptr, 0, InsertAtEnd
) {
4051 init(Address
, NumCases
);
4054 IndirectBrInst::IndirectBrInst(const IndirectBrInst
&IBI
)
4055 : Instruction(Type::getVoidTy(IBI
.getContext()), Instruction::IndirectBr
,
4056 nullptr, IBI
.getNumOperands()) {
4057 allocHungoffUses(IBI
.getNumOperands());
4058 Use
*OL
= getOperandList();
4059 const Use
*InOL
= IBI
.getOperandList();
4060 for (unsigned i
= 0, E
= IBI
.getNumOperands(); i
!= E
; ++i
)
4062 SubclassOptionalData
= IBI
.SubclassOptionalData
;
4065 /// addDestination - Add a destination.
4067 void IndirectBrInst::addDestination(BasicBlock
*DestBB
) {
4068 unsigned OpNo
= getNumOperands();
4069 if (OpNo
+1 > ReservedSpace
)
4070 growOperands(); // Get more space!
4071 // Initialize some new operands.
4072 assert(OpNo
< ReservedSpace
&& "Growing didn't work!");
4073 setNumHungOffUseOperands(OpNo
+1);
4074 getOperandList()[OpNo
] = DestBB
;
4077 /// removeDestination - This method removes the specified successor from the
4078 /// indirectbr instruction.
4079 void IndirectBrInst::removeDestination(unsigned idx
) {
4080 assert(idx
< getNumOperands()-1 && "Successor index out of range!");
4082 unsigned NumOps
= getNumOperands();
4083 Use
*OL
= getOperandList();
4085 // Replace this value with the last one.
4086 OL
[idx
+1] = OL
[NumOps
-1];
4088 // Nuke the last value.
4089 OL
[NumOps
-1].set(nullptr);
4090 setNumHungOffUseOperands(NumOps
-1);
4093 //===----------------------------------------------------------------------===//
4094 // cloneImpl() implementations
4095 //===----------------------------------------------------------------------===//
4097 // Define these methods here so vtables don't get emitted into every translation
4098 // unit that uses these classes.
4100 GetElementPtrInst
*GetElementPtrInst::cloneImpl() const {
4101 return new (getNumOperands()) GetElementPtrInst(*this);
4104 UnaryOperator
*UnaryOperator::cloneImpl() const {
4105 return Create(getOpcode(), Op
<0>());
4108 BinaryOperator
*BinaryOperator::cloneImpl() const {
4109 return Create(getOpcode(), Op
<0>(), Op
<1>());
4112 FCmpInst
*FCmpInst::cloneImpl() const {
4113 return new FCmpInst(getPredicate(), Op
<0>(), Op
<1>());
4116 ICmpInst
*ICmpInst::cloneImpl() const {
4117 return new ICmpInst(getPredicate(), Op
<0>(), Op
<1>());
4120 ExtractValueInst
*ExtractValueInst::cloneImpl() const {
4121 return new ExtractValueInst(*this);
4124 InsertValueInst
*InsertValueInst::cloneImpl() const {
4125 return new InsertValueInst(*this);
4128 AllocaInst
*AllocaInst::cloneImpl() const {
4129 AllocaInst
*Result
= new AllocaInst(getAllocatedType(),
4130 getType()->getAddressSpace(),
4131 (Value
*)getOperand(0), getAlignment());
4132 Result
->setUsedWithInAlloca(isUsedWithInAlloca());
4133 Result
->setSwiftError(isSwiftError());
4137 LoadInst
*LoadInst::cloneImpl() const {
4138 return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(),
4139 MaybeAlign(getAlignment()), getOrdering(),
4143 StoreInst
*StoreInst::cloneImpl() const {
4144 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
4145 MaybeAlign(getAlignment()), getOrdering(),
4149 AtomicCmpXchgInst
*AtomicCmpXchgInst::cloneImpl() const {
4150 AtomicCmpXchgInst
*Result
=
4151 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
4152 getSuccessOrdering(), getFailureOrdering(),
4154 Result
->setVolatile(isVolatile());
4155 Result
->setWeak(isWeak());
4159 AtomicRMWInst
*AtomicRMWInst::cloneImpl() const {
4160 AtomicRMWInst
*Result
=
4161 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
4162 getOrdering(), getSyncScopeID());
4163 Result
->setVolatile(isVolatile());
4167 FenceInst
*FenceInst::cloneImpl() const {
4168 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
4171 TruncInst
*TruncInst::cloneImpl() const {
4172 return new TruncInst(getOperand(0), getType());
4175 ZExtInst
*ZExtInst::cloneImpl() const {
4176 return new ZExtInst(getOperand(0), getType());
4179 SExtInst
*SExtInst::cloneImpl() const {
4180 return new SExtInst(getOperand(0), getType());
4183 FPTruncInst
*FPTruncInst::cloneImpl() const {
4184 return new FPTruncInst(getOperand(0), getType());
4187 FPExtInst
*FPExtInst::cloneImpl() const {
4188 return new FPExtInst(getOperand(0), getType());
4191 UIToFPInst
*UIToFPInst::cloneImpl() const {
4192 return new UIToFPInst(getOperand(0), getType());
4195 SIToFPInst
*SIToFPInst::cloneImpl() const {
4196 return new SIToFPInst(getOperand(0), getType());
4199 FPToUIInst
*FPToUIInst::cloneImpl() const {
4200 return new FPToUIInst(getOperand(0), getType());
4203 FPToSIInst
*FPToSIInst::cloneImpl() const {
4204 return new FPToSIInst(getOperand(0), getType());
4207 PtrToIntInst
*PtrToIntInst::cloneImpl() const {
4208 return new PtrToIntInst(getOperand(0), getType());
4211 IntToPtrInst
*IntToPtrInst::cloneImpl() const {
4212 return new IntToPtrInst(getOperand(0), getType());
4215 BitCastInst
*BitCastInst::cloneImpl() const {
4216 return new BitCastInst(getOperand(0), getType());
4219 AddrSpaceCastInst
*AddrSpaceCastInst::cloneImpl() const {
4220 return new AddrSpaceCastInst(getOperand(0), getType());
4223 CallInst
*CallInst::cloneImpl() const {
4224 if (hasOperandBundles()) {
4225 unsigned DescriptorBytes
= getNumOperandBundles() * sizeof(BundleOpInfo
);
4226 return new(getNumOperands(), DescriptorBytes
) CallInst(*this);
4228 return new(getNumOperands()) CallInst(*this);
4231 SelectInst
*SelectInst::cloneImpl() const {
4232 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
4235 VAArgInst
*VAArgInst::cloneImpl() const {
4236 return new VAArgInst(getOperand(0), getType());
4239 ExtractElementInst
*ExtractElementInst::cloneImpl() const {
4240 return ExtractElementInst::Create(getOperand(0), getOperand(1));
4243 InsertElementInst
*InsertElementInst::cloneImpl() const {
4244 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
4247 ShuffleVectorInst
*ShuffleVectorInst::cloneImpl() const {
4248 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
4251 PHINode
*PHINode::cloneImpl() const { return new PHINode(*this); }
4253 LandingPadInst
*LandingPadInst::cloneImpl() const {
4254 return new LandingPadInst(*this);
4257 ReturnInst
*ReturnInst::cloneImpl() const {
4258 return new(getNumOperands()) ReturnInst(*this);
4261 BranchInst
*BranchInst::cloneImpl() const {
4262 return new(getNumOperands()) BranchInst(*this);
4265 SwitchInst
*SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
4267 IndirectBrInst
*IndirectBrInst::cloneImpl() const {
4268 return new IndirectBrInst(*this);
4271 InvokeInst
*InvokeInst::cloneImpl() const {
4272 if (hasOperandBundles()) {
4273 unsigned DescriptorBytes
= getNumOperandBundles() * sizeof(BundleOpInfo
);
4274 return new(getNumOperands(), DescriptorBytes
) InvokeInst(*this);
4276 return new(getNumOperands()) InvokeInst(*this);
4279 CallBrInst
*CallBrInst::cloneImpl() const {
4280 if (hasOperandBundles()) {
4281 unsigned DescriptorBytes
= getNumOperandBundles() * sizeof(BundleOpInfo
);
4282 return new (getNumOperands(), DescriptorBytes
) CallBrInst(*this);
4284 return new (getNumOperands()) CallBrInst(*this);
4287 ResumeInst
*ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
4289 CleanupReturnInst
*CleanupReturnInst::cloneImpl() const {
4290 return new (getNumOperands()) CleanupReturnInst(*this);
4293 CatchReturnInst
*CatchReturnInst::cloneImpl() const {
4294 return new (getNumOperands()) CatchReturnInst(*this);
4297 CatchSwitchInst
*CatchSwitchInst::cloneImpl() const {
4298 return new CatchSwitchInst(*this);
4301 FuncletPadInst
*FuncletPadInst::cloneImpl() const {
4302 return new (getNumOperands()) FuncletPadInst(*this);
4305 UnreachableInst
*UnreachableInst::cloneImpl() const {
4306 LLVMContext
&Context
= getContext();
4307 return new UnreachableInst(Context
);