1 //===- llvm/Instructions.h - Instruction subclass definitions ---*- C++ -*-===//
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 exposes the class definitions of all of the subclasses of the
10 // Instruction class. This is meant to be an easy way to get access to all
11 // instruction subclasses.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_INSTRUCTIONS_H
16 #define LLVM_IR_INSTRUCTIONS_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/ADT/iterator.h"
25 #include "llvm/ADT/iterator_range.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/CallingConv.h"
29 #include "llvm/IR/Constant.h"
30 #include "llvm/IR/DerivedTypes.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/InstrTypes.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/OperandTraits.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/IR/Use.h"
37 #include "llvm/IR/User.h"
38 #include "llvm/IR/Value.h"
39 #include "llvm/Support/AtomicOrdering.h"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/ErrorHandling.h"
54 //===----------------------------------------------------------------------===//
56 //===----------------------------------------------------------------------===//
58 /// an instruction to allocate memory on the stack
59 class AllocaInst
: public UnaryInstruction
{
63 // Note: Instruction needs to be a friend here to call cloneImpl.
64 friend class Instruction
;
66 AllocaInst
*cloneImpl() const;
69 explicit AllocaInst(Type
*Ty
, unsigned AddrSpace
,
70 Value
*ArraySize
= nullptr,
71 const Twine
&Name
= "",
72 Instruction
*InsertBefore
= nullptr);
73 AllocaInst(Type
*Ty
, unsigned AddrSpace
, Value
*ArraySize
,
74 const Twine
&Name
, BasicBlock
*InsertAtEnd
);
76 AllocaInst(Type
*Ty
, unsigned AddrSpace
,
77 const Twine
&Name
, Instruction
*InsertBefore
= nullptr);
78 AllocaInst(Type
*Ty
, unsigned AddrSpace
,
79 const Twine
&Name
, BasicBlock
*InsertAtEnd
);
81 AllocaInst(Type
*Ty
, unsigned AddrSpace
, Value
*ArraySize
, unsigned Align
,
82 const Twine
&Name
= "", Instruction
*InsertBefore
= nullptr);
83 AllocaInst(Type
*Ty
, unsigned AddrSpace
, Value
*ArraySize
, unsigned Align
,
84 const Twine
&Name
, BasicBlock
*InsertAtEnd
);
86 /// Return true if there is an allocation size parameter to the allocation
87 /// instruction that is not 1.
88 bool isArrayAllocation() const;
90 /// Get the number of elements allocated. For a simple allocation of a single
91 /// element, this will return a constant 1 value.
92 const Value
*getArraySize() const { return getOperand(0); }
93 Value
*getArraySize() { return getOperand(0); }
95 /// Overload to return most specific pointer type.
96 PointerType
*getType() const {
97 return cast
<PointerType
>(Instruction::getType());
100 /// Get allocation size in bits. Returns None if size can't be determined,
101 /// e.g. in case of a VLA.
102 Optional
<uint64_t> getAllocationSizeInBits(const DataLayout
&DL
) const;
104 /// Return the type that is being allocated by the instruction.
105 Type
*getAllocatedType() const { return AllocatedType
; }
106 /// for use only in special circumstances that need to generically
107 /// transform a whole instruction (eg: IR linking and vectorization).
108 void setAllocatedType(Type
*Ty
) { AllocatedType
= Ty
; }
110 /// Return the alignment of the memory that is being allocated by the
112 unsigned getAlignment() const {
113 if (const auto MA
= decodeMaybeAlign(getSubclassDataFromInstruction() & 31))
117 // FIXME: Remove once migration to Align is over.
118 void setAlignment(unsigned Align
);
119 void setAlignment(MaybeAlign Align
);
121 /// Return true if this alloca is in the entry block of the function and is a
122 /// constant size. If so, the code generator will fold it into the
123 /// prolog/epilog code, so it is basically free.
124 bool isStaticAlloca() const;
126 /// Return true if this alloca is used as an inalloca argument to a call. Such
127 /// allocas are never considered static even if they are in the entry block.
128 bool isUsedWithInAlloca() const {
129 return getSubclassDataFromInstruction() & 32;
132 /// Specify whether this alloca is used to represent the arguments to a call.
133 void setUsedWithInAlloca(bool V
) {
134 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
138 /// Return true if this alloca is used as a swifterror argument to a call.
139 bool isSwiftError() const {
140 return getSubclassDataFromInstruction() & 64;
143 /// Specify whether this alloca is used to represent a swifterror.
144 void setSwiftError(bool V
) {
145 setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) |
149 // Methods for support type inquiry through isa, cast, and dyn_cast:
150 static bool classof(const Instruction
*I
) {
151 return (I
->getOpcode() == Instruction::Alloca
);
153 static bool classof(const Value
*V
) {
154 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
158 // Shadow Instruction::setInstructionSubclassData with a private forwarding
159 // method so that subclasses cannot accidentally use it.
160 void setInstructionSubclassData(unsigned short D
) {
161 Instruction::setInstructionSubclassData(D
);
165 //===----------------------------------------------------------------------===//
167 //===----------------------------------------------------------------------===//
169 /// An instruction for reading from memory. This uses the SubclassData field in
170 /// Value to store whether or not the load is volatile.
171 class LoadInst
: public UnaryInstruction
{
175 // Note: Instruction needs to be a friend here to call cloneImpl.
176 friend class Instruction
;
178 LoadInst
*cloneImpl() const;
181 LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&NameStr
= "",
182 Instruction
*InsertBefore
= nullptr);
183 LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&NameStr
, BasicBlock
*InsertAtEnd
);
184 LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
,
185 Instruction
*InsertBefore
= nullptr);
186 LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
,
187 BasicBlock
*InsertAtEnd
);
188 LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
,
189 unsigned Align
, Instruction
*InsertBefore
= nullptr);
190 LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
,
191 unsigned Align
, BasicBlock
*InsertAtEnd
);
192 LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
,
193 unsigned Align
, AtomicOrdering Order
,
194 SyncScope::ID SSID
= SyncScope::System
,
195 Instruction
*InsertBefore
= nullptr);
196 LoadInst(Type
*Ty
, Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
,
197 unsigned Align
, AtomicOrdering Order
, SyncScope::ID SSID
,
198 BasicBlock
*InsertAtEnd
);
200 // Deprecated [opaque pointer types]
201 explicit LoadInst(Value
*Ptr
, const Twine
&NameStr
= "",
202 Instruction
*InsertBefore
= nullptr)
203 : LoadInst(Ptr
->getType()->getPointerElementType(), Ptr
, NameStr
,
205 LoadInst(Value
*Ptr
, const Twine
&NameStr
, BasicBlock
*InsertAtEnd
)
206 : LoadInst(Ptr
->getType()->getPointerElementType(), Ptr
, NameStr
,
208 LoadInst(Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
,
209 Instruction
*InsertBefore
= nullptr)
210 : LoadInst(Ptr
->getType()->getPointerElementType(), Ptr
, NameStr
,
211 isVolatile
, InsertBefore
) {}
212 LoadInst(Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
,
213 BasicBlock
*InsertAtEnd
)
214 : LoadInst(Ptr
->getType()->getPointerElementType(), Ptr
, NameStr
,
215 isVolatile
, InsertAtEnd
) {}
216 LoadInst(Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
, unsigned Align
,
217 Instruction
*InsertBefore
= nullptr)
218 : LoadInst(Ptr
->getType()->getPointerElementType(), Ptr
, NameStr
,
219 isVolatile
, Align
, InsertBefore
) {}
220 LoadInst(Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
, unsigned Align
,
221 BasicBlock
*InsertAtEnd
)
222 : LoadInst(Ptr
->getType()->getPointerElementType(), Ptr
, NameStr
,
223 isVolatile
, Align
, InsertAtEnd
) {}
224 LoadInst(Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
, unsigned Align
,
225 AtomicOrdering Order
, SyncScope::ID SSID
= SyncScope::System
,
226 Instruction
*InsertBefore
= nullptr)
227 : LoadInst(Ptr
->getType()->getPointerElementType(), Ptr
, NameStr
,
228 isVolatile
, Align
, Order
, SSID
, InsertBefore
) {}
229 LoadInst(Value
*Ptr
, const Twine
&NameStr
, bool isVolatile
, unsigned Align
,
230 AtomicOrdering Order
, SyncScope::ID SSID
, BasicBlock
*InsertAtEnd
)
231 : LoadInst(Ptr
->getType()->getPointerElementType(), Ptr
, NameStr
,
232 isVolatile
, Align
, Order
, SSID
, InsertAtEnd
) {}
234 /// Return true if this is a load from a volatile memory location.
235 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
237 /// Specify whether this is a volatile load or not.
238 void setVolatile(bool V
) {
239 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
243 /// Return the alignment of the access that is being performed.
244 unsigned getAlignment() const {
246 decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31))
251 void setAlignment(MaybeAlign Align
);
253 /// Returns the ordering constraint of this load instruction.
254 AtomicOrdering
getOrdering() const {
255 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
258 /// Sets the ordering constraint of this load instruction. May not be Release
259 /// or AcquireRelease.
260 void setOrdering(AtomicOrdering Ordering
) {
261 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
262 ((unsigned)Ordering
<< 7));
265 /// Returns the synchronization scope ID of this load instruction.
266 SyncScope::ID
getSyncScopeID() const {
270 /// Sets the synchronization scope ID of this load instruction.
271 void setSyncScopeID(SyncScope::ID SSID
) {
275 /// Sets the ordering constraint and the synchronization scope ID of this load
277 void setAtomic(AtomicOrdering Ordering
,
278 SyncScope::ID SSID
= SyncScope::System
) {
279 setOrdering(Ordering
);
280 setSyncScopeID(SSID
);
283 bool isSimple() const { return !isAtomic() && !isVolatile(); }
285 bool isUnordered() const {
286 return (getOrdering() == AtomicOrdering::NotAtomic
||
287 getOrdering() == AtomicOrdering::Unordered
) &&
291 Value
*getPointerOperand() { return getOperand(0); }
292 const Value
*getPointerOperand() const { return getOperand(0); }
293 static unsigned getPointerOperandIndex() { return 0U; }
294 Type
*getPointerOperandType() const { return getPointerOperand()->getType(); }
296 /// Returns the address space of the pointer operand.
297 unsigned getPointerAddressSpace() const {
298 return getPointerOperandType()->getPointerAddressSpace();
301 // Methods for support type inquiry through isa, cast, and dyn_cast:
302 static bool classof(const Instruction
*I
) {
303 return I
->getOpcode() == Instruction::Load
;
305 static bool classof(const Value
*V
) {
306 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
310 // Shadow Instruction::setInstructionSubclassData with a private forwarding
311 // method so that subclasses cannot accidentally use it.
312 void setInstructionSubclassData(unsigned short D
) {
313 Instruction::setInstructionSubclassData(D
);
316 /// The synchronization scope ID of this load instruction. Not quite enough
317 /// room in SubClassData for everything, so synchronization scope ID gets its
322 //===----------------------------------------------------------------------===//
324 //===----------------------------------------------------------------------===//
326 /// An instruction for storing to memory.
327 class StoreInst
: public Instruction
{
331 // Note: Instruction needs to be a friend here to call cloneImpl.
332 friend class Instruction
;
334 StoreInst
*cloneImpl() const;
337 StoreInst(Value
*Val
, Value
*Ptr
, Instruction
*InsertBefore
);
338 StoreInst(Value
*Val
, Value
*Ptr
, BasicBlock
*InsertAtEnd
);
339 StoreInst(Value
*Val
, Value
*Ptr
, bool isVolatile
= false,
340 Instruction
*InsertBefore
= nullptr);
341 StoreInst(Value
*Val
, Value
*Ptr
, bool isVolatile
, BasicBlock
*InsertAtEnd
);
342 StoreInst(Value
*Val
, Value
*Ptr
, bool isVolatile
,
343 unsigned Align
, Instruction
*InsertBefore
= nullptr);
344 StoreInst(Value
*Val
, Value
*Ptr
, bool isVolatile
,
345 unsigned Align
, BasicBlock
*InsertAtEnd
);
346 StoreInst(Value
*Val
, Value
*Ptr
, bool isVolatile
,
347 unsigned Align
, AtomicOrdering Order
,
348 SyncScope::ID SSID
= SyncScope::System
,
349 Instruction
*InsertBefore
= nullptr);
350 StoreInst(Value
*Val
, Value
*Ptr
, bool isVolatile
,
351 unsigned Align
, AtomicOrdering Order
, SyncScope::ID SSID
,
352 BasicBlock
*InsertAtEnd
);
354 // allocate space for exactly two operands
355 void *operator new(size_t s
) {
356 return User::operator new(s
, 2);
359 /// Return true if this is a store to a volatile memory location.
360 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
362 /// Specify whether this is a volatile store or not.
363 void setVolatile(bool V
) {
364 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
368 /// Transparently provide more efficient getOperand methods.
369 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
371 /// Return the alignment of the access that is being performed
372 unsigned getAlignment() const {
374 decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31))
379 // FIXME: Remove once migration to Align is over.
380 void setAlignment(unsigned Align
);
381 void setAlignment(MaybeAlign Align
);
383 /// Returns the ordering constraint of this store instruction.
384 AtomicOrdering
getOrdering() const {
385 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
388 /// Sets the ordering constraint of this store instruction. May not be
389 /// Acquire or AcquireRelease.
390 void setOrdering(AtomicOrdering Ordering
) {
391 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
392 ((unsigned)Ordering
<< 7));
395 /// Returns the synchronization scope ID of this store instruction.
396 SyncScope::ID
getSyncScopeID() const {
400 /// Sets the synchronization scope ID of this store instruction.
401 void setSyncScopeID(SyncScope::ID SSID
) {
405 /// Sets the ordering constraint and the synchronization scope ID of this
406 /// store instruction.
407 void setAtomic(AtomicOrdering Ordering
,
408 SyncScope::ID SSID
= SyncScope::System
) {
409 setOrdering(Ordering
);
410 setSyncScopeID(SSID
);
413 bool isSimple() const { return !isAtomic() && !isVolatile(); }
415 bool isUnordered() const {
416 return (getOrdering() == AtomicOrdering::NotAtomic
||
417 getOrdering() == AtomicOrdering::Unordered
) &&
421 Value
*getValueOperand() { return getOperand(0); }
422 const Value
*getValueOperand() const { return getOperand(0); }
424 Value
*getPointerOperand() { return getOperand(1); }
425 const Value
*getPointerOperand() const { return getOperand(1); }
426 static unsigned getPointerOperandIndex() { return 1U; }
427 Type
*getPointerOperandType() const { return getPointerOperand()->getType(); }
429 /// Returns the address space of the pointer operand.
430 unsigned getPointerAddressSpace() const {
431 return getPointerOperandType()->getPointerAddressSpace();
434 // Methods for support type inquiry through isa, cast, and dyn_cast:
435 static bool classof(const Instruction
*I
) {
436 return I
->getOpcode() == Instruction::Store
;
438 static bool classof(const Value
*V
) {
439 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
443 // Shadow Instruction::setInstructionSubclassData with a private forwarding
444 // method so that subclasses cannot accidentally use it.
445 void setInstructionSubclassData(unsigned short D
) {
446 Instruction::setInstructionSubclassData(D
);
449 /// The synchronization scope ID of this store instruction. Not quite enough
450 /// room in SubClassData for everything, so synchronization scope ID gets its
456 struct OperandTraits
<StoreInst
> : public FixedNumOperandTraits
<StoreInst
, 2> {
459 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst
, Value
)
461 //===----------------------------------------------------------------------===//
463 //===----------------------------------------------------------------------===//
465 /// An instruction for ordering other memory operations.
466 class FenceInst
: public Instruction
{
467 void Init(AtomicOrdering Ordering
, SyncScope::ID SSID
);
470 // Note: Instruction needs to be a friend here to call cloneImpl.
471 friend class Instruction
;
473 FenceInst
*cloneImpl() const;
476 // Ordering may only be Acquire, Release, AcquireRelease, or
477 // SequentiallyConsistent.
478 FenceInst(LLVMContext
&C
, AtomicOrdering Ordering
,
479 SyncScope::ID SSID
= SyncScope::System
,
480 Instruction
*InsertBefore
= nullptr);
481 FenceInst(LLVMContext
&C
, AtomicOrdering Ordering
, SyncScope::ID SSID
,
482 BasicBlock
*InsertAtEnd
);
484 // allocate space for exactly zero operands
485 void *operator new(size_t s
) {
486 return User::operator new(s
, 0);
489 /// Returns the ordering constraint of this fence instruction.
490 AtomicOrdering
getOrdering() const {
491 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
494 /// Sets the ordering constraint of this fence instruction. May only be
495 /// Acquire, Release, AcquireRelease, or SequentiallyConsistent.
496 void setOrdering(AtomicOrdering Ordering
) {
497 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
498 ((unsigned)Ordering
<< 1));
501 /// Returns the synchronization scope ID of this fence instruction.
502 SyncScope::ID
getSyncScopeID() const {
506 /// Sets the synchronization scope ID of this fence instruction.
507 void setSyncScopeID(SyncScope::ID SSID
) {
511 // Methods for support type inquiry through isa, cast, and dyn_cast:
512 static bool classof(const Instruction
*I
) {
513 return I
->getOpcode() == Instruction::Fence
;
515 static bool classof(const Value
*V
) {
516 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
520 // Shadow Instruction::setInstructionSubclassData with a private forwarding
521 // method so that subclasses cannot accidentally use it.
522 void setInstructionSubclassData(unsigned short D
) {
523 Instruction::setInstructionSubclassData(D
);
526 /// The synchronization scope ID of this fence instruction. Not quite enough
527 /// room in SubClassData for everything, so synchronization scope ID gets its
532 //===----------------------------------------------------------------------===//
533 // AtomicCmpXchgInst Class
534 //===----------------------------------------------------------------------===//
536 /// An instruction that atomically checks whether a
537 /// specified value is in a memory location, and, if it is, stores a new value
538 /// there. The value returned by this instruction is a pair containing the
539 /// original value as first element, and an i1 indicating success (true) or
540 /// failure (false) as second element.
542 class AtomicCmpXchgInst
: public Instruction
{
543 void Init(Value
*Ptr
, Value
*Cmp
, Value
*NewVal
,
544 AtomicOrdering SuccessOrdering
, AtomicOrdering FailureOrdering
,
548 // Note: Instruction needs to be a friend here to call cloneImpl.
549 friend class Instruction
;
551 AtomicCmpXchgInst
*cloneImpl() const;
554 AtomicCmpXchgInst(Value
*Ptr
, Value
*Cmp
, Value
*NewVal
,
555 AtomicOrdering SuccessOrdering
,
556 AtomicOrdering FailureOrdering
,
557 SyncScope::ID SSID
, Instruction
*InsertBefore
= nullptr);
558 AtomicCmpXchgInst(Value
*Ptr
, Value
*Cmp
, Value
*NewVal
,
559 AtomicOrdering SuccessOrdering
,
560 AtomicOrdering FailureOrdering
,
561 SyncScope::ID SSID
, BasicBlock
*InsertAtEnd
);
563 // allocate space for exactly three operands
564 void *operator new(size_t s
) {
565 return User::operator new(s
, 3);
568 /// Return true if this is a cmpxchg from a volatile memory
571 bool isVolatile() const {
572 return getSubclassDataFromInstruction() & 1;
575 /// Specify whether this is a volatile cmpxchg.
577 void setVolatile(bool V
) {
578 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
582 /// Return true if this cmpxchg may spuriously fail.
583 bool isWeak() const {
584 return getSubclassDataFromInstruction() & 0x100;
587 void setWeak(bool IsWeak
) {
588 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
592 /// Transparently provide more efficient getOperand methods.
593 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
595 /// Returns the success ordering constraint of this cmpxchg instruction.
596 AtomicOrdering
getSuccessOrdering() const {
597 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
600 /// Sets the success ordering constraint of this cmpxchg instruction.
601 void setSuccessOrdering(AtomicOrdering Ordering
) {
602 assert(Ordering
!= AtomicOrdering::NotAtomic
&&
603 "CmpXchg instructions can only be atomic.");
604 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
605 ((unsigned)Ordering
<< 2));
608 /// Returns the failure ordering constraint of this cmpxchg instruction.
609 AtomicOrdering
getFailureOrdering() const {
610 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
613 /// Sets the failure ordering constraint of this cmpxchg instruction.
614 void setFailureOrdering(AtomicOrdering Ordering
) {
615 assert(Ordering
!= AtomicOrdering::NotAtomic
&&
616 "CmpXchg instructions can only be atomic.");
617 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
618 ((unsigned)Ordering
<< 5));
621 /// Returns the synchronization scope ID of this cmpxchg instruction.
622 SyncScope::ID
getSyncScopeID() const {
626 /// Sets the synchronization scope ID of this cmpxchg instruction.
627 void setSyncScopeID(SyncScope::ID SSID
) {
631 Value
*getPointerOperand() { return getOperand(0); }
632 const Value
*getPointerOperand() const { return getOperand(0); }
633 static unsigned getPointerOperandIndex() { return 0U; }
635 Value
*getCompareOperand() { return getOperand(1); }
636 const Value
*getCompareOperand() const { return getOperand(1); }
638 Value
*getNewValOperand() { return getOperand(2); }
639 const Value
*getNewValOperand() const { return getOperand(2); }
641 /// Returns the address space of the pointer operand.
642 unsigned getPointerAddressSpace() const {
643 return getPointerOperand()->getType()->getPointerAddressSpace();
646 /// Returns the strongest permitted ordering on failure, given the
647 /// desired ordering on success.
649 /// If the comparison in a cmpxchg operation fails, there is no atomic store
650 /// so release semantics cannot be provided. So this function drops explicit
651 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
652 /// operation would remain SequentiallyConsistent.
653 static AtomicOrdering
654 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering
) {
655 switch (SuccessOrdering
) {
657 llvm_unreachable("invalid cmpxchg success ordering");
658 case AtomicOrdering::Release
:
659 case AtomicOrdering::Monotonic
:
660 return AtomicOrdering::Monotonic
;
661 case AtomicOrdering::AcquireRelease
:
662 case AtomicOrdering::Acquire
:
663 return AtomicOrdering::Acquire
;
664 case AtomicOrdering::SequentiallyConsistent
:
665 return AtomicOrdering::SequentiallyConsistent
;
669 // Methods for support type inquiry through isa, cast, and dyn_cast:
670 static bool classof(const Instruction
*I
) {
671 return I
->getOpcode() == Instruction::AtomicCmpXchg
;
673 static bool classof(const Value
*V
) {
674 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
678 // Shadow Instruction::setInstructionSubclassData with a private forwarding
679 // method so that subclasses cannot accidentally use it.
680 void setInstructionSubclassData(unsigned short D
) {
681 Instruction::setInstructionSubclassData(D
);
684 /// The synchronization scope ID of this cmpxchg instruction. Not quite
685 /// enough room in SubClassData for everything, so synchronization scope ID
686 /// gets its own field.
691 struct OperandTraits
<AtomicCmpXchgInst
> :
692 public FixedNumOperandTraits
<AtomicCmpXchgInst
, 3> {
695 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst
, Value
)
697 //===----------------------------------------------------------------------===//
698 // AtomicRMWInst Class
699 //===----------------------------------------------------------------------===//
701 /// an instruction that atomically reads a memory location,
702 /// combines it with another value, and then stores the result back. Returns
705 class AtomicRMWInst
: public Instruction
{
707 // Note: Instruction needs to be a friend here to call cloneImpl.
708 friend class Instruction
;
710 AtomicRMWInst
*cloneImpl() const;
713 /// This enumeration lists the possible modifications atomicrmw can make. In
714 /// the descriptions, 'p' is the pointer to the instruction's memory location,
715 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
716 /// instruction. These instructions always return 'old'.
732 /// *p = old >signed v ? old : v
734 /// *p = old <signed v ? old : v
736 /// *p = old >unsigned v ? old : v
738 /// *p = old <unsigned v ? old : v
752 AtomicRMWInst(BinOp Operation
, Value
*Ptr
, Value
*Val
,
753 AtomicOrdering Ordering
, SyncScope::ID SSID
,
754 Instruction
*InsertBefore
= nullptr);
755 AtomicRMWInst(BinOp Operation
, Value
*Ptr
, Value
*Val
,
756 AtomicOrdering Ordering
, SyncScope::ID SSID
,
757 BasicBlock
*InsertAtEnd
);
759 // allocate space for exactly two operands
760 void *operator new(size_t s
) {
761 return User::operator new(s
, 2);
764 BinOp
getOperation() const {
765 return static_cast<BinOp
>(getSubclassDataFromInstruction() >> 5);
768 static StringRef
getOperationName(BinOp Op
);
770 static bool isFPOperation(BinOp Op
) {
772 case AtomicRMWInst::FAdd
:
773 case AtomicRMWInst::FSub
:
780 void setOperation(BinOp Operation
) {
781 unsigned short SubclassData
= getSubclassDataFromInstruction();
782 setInstructionSubclassData((SubclassData
& 31) |
786 /// Return true if this is a RMW on a volatile memory location.
788 bool isVolatile() const {
789 return getSubclassDataFromInstruction() & 1;
792 /// Specify whether this is a volatile RMW or not.
794 void setVolatile(bool V
) {
795 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
799 /// Transparently provide more efficient getOperand methods.
800 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
802 /// Returns the ordering constraint of this rmw instruction.
803 AtomicOrdering
getOrdering() const {
804 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
807 /// Sets the ordering constraint of this rmw instruction.
808 void setOrdering(AtomicOrdering Ordering
) {
809 assert(Ordering
!= AtomicOrdering::NotAtomic
&&
810 "atomicrmw instructions can only be atomic.");
811 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
812 ((unsigned)Ordering
<< 2));
815 /// Returns the synchronization scope ID of this rmw instruction.
816 SyncScope::ID
getSyncScopeID() const {
820 /// Sets the synchronization scope ID of this rmw instruction.
821 void setSyncScopeID(SyncScope::ID SSID
) {
825 Value
*getPointerOperand() { return getOperand(0); }
826 const Value
*getPointerOperand() const { return getOperand(0); }
827 static unsigned getPointerOperandIndex() { return 0U; }
829 Value
*getValOperand() { return getOperand(1); }
830 const Value
*getValOperand() const { return getOperand(1); }
832 /// Returns the address space of the pointer operand.
833 unsigned getPointerAddressSpace() const {
834 return getPointerOperand()->getType()->getPointerAddressSpace();
837 bool isFloatingPointOperation() const {
838 return isFPOperation(getOperation());
841 // Methods for support type inquiry through isa, cast, and dyn_cast:
842 static bool classof(const Instruction
*I
) {
843 return I
->getOpcode() == Instruction::AtomicRMW
;
845 static bool classof(const Value
*V
) {
846 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
850 void Init(BinOp Operation
, Value
*Ptr
, Value
*Val
,
851 AtomicOrdering Ordering
, SyncScope::ID SSID
);
853 // Shadow Instruction::setInstructionSubclassData with a private forwarding
854 // method so that subclasses cannot accidentally use it.
855 void setInstructionSubclassData(unsigned short D
) {
856 Instruction::setInstructionSubclassData(D
);
859 /// The synchronization scope ID of this rmw instruction. Not quite enough
860 /// room in SubClassData for everything, so synchronization scope ID gets its
866 struct OperandTraits
<AtomicRMWInst
>
867 : public FixedNumOperandTraits
<AtomicRMWInst
,2> {
870 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst
, Value
)
872 //===----------------------------------------------------------------------===//
873 // GetElementPtrInst Class
874 //===----------------------------------------------------------------------===//
876 // checkGEPType - Simple wrapper function to give a better assertion failure
877 // message on bad indexes for a gep instruction.
879 inline Type
*checkGEPType(Type
*Ty
) {
880 assert(Ty
&& "Invalid GetElementPtrInst indices for type!");
884 /// an instruction for type-safe pointer arithmetic to
885 /// access elements of arrays and structs
887 class GetElementPtrInst
: public Instruction
{
888 Type
*SourceElementType
;
889 Type
*ResultElementType
;
891 GetElementPtrInst(const GetElementPtrInst
&GEPI
);
893 /// Constructors - Create a getelementptr instruction with a base pointer an
894 /// list of indices. The first ctor can optionally insert before an existing
895 /// instruction, the second appends the new instruction to the specified
897 inline GetElementPtrInst(Type
*PointeeType
, Value
*Ptr
,
898 ArrayRef
<Value
*> IdxList
, unsigned Values
,
899 const Twine
&NameStr
, Instruction
*InsertBefore
);
900 inline GetElementPtrInst(Type
*PointeeType
, Value
*Ptr
,
901 ArrayRef
<Value
*> IdxList
, unsigned Values
,
902 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
);
904 void init(Value
*Ptr
, ArrayRef
<Value
*> IdxList
, const Twine
&NameStr
);
907 // Note: Instruction needs to be a friend here to call cloneImpl.
908 friend class Instruction
;
910 GetElementPtrInst
*cloneImpl() const;
913 static GetElementPtrInst
*Create(Type
*PointeeType
, Value
*Ptr
,
914 ArrayRef
<Value
*> IdxList
,
915 const Twine
&NameStr
= "",
916 Instruction
*InsertBefore
= nullptr) {
917 unsigned Values
= 1 + unsigned(IdxList
.size());
920 cast
<PointerType
>(Ptr
->getType()->getScalarType())->getElementType();
924 cast
<PointerType
>(Ptr
->getType()->getScalarType())->getElementType());
925 return new (Values
) GetElementPtrInst(PointeeType
, Ptr
, IdxList
, Values
,
926 NameStr
, InsertBefore
);
929 static GetElementPtrInst
*Create(Type
*PointeeType
, Value
*Ptr
,
930 ArrayRef
<Value
*> IdxList
,
931 const Twine
&NameStr
,
932 BasicBlock
*InsertAtEnd
) {
933 unsigned Values
= 1 + unsigned(IdxList
.size());
936 cast
<PointerType
>(Ptr
->getType()->getScalarType())->getElementType();
940 cast
<PointerType
>(Ptr
->getType()->getScalarType())->getElementType());
941 return new (Values
) GetElementPtrInst(PointeeType
, Ptr
, IdxList
, Values
,
942 NameStr
, InsertAtEnd
);
945 /// Create an "inbounds" getelementptr. See the documentation for the
946 /// "inbounds" flag in LangRef.html for details.
947 static GetElementPtrInst
*CreateInBounds(Value
*Ptr
,
948 ArrayRef
<Value
*> IdxList
,
949 const Twine
&NameStr
= "",
950 Instruction
*InsertBefore
= nullptr){
951 return CreateInBounds(nullptr, Ptr
, IdxList
, NameStr
, InsertBefore
);
954 static GetElementPtrInst
*
955 CreateInBounds(Type
*PointeeType
, Value
*Ptr
, ArrayRef
<Value
*> IdxList
,
956 const Twine
&NameStr
= "",
957 Instruction
*InsertBefore
= nullptr) {
958 GetElementPtrInst
*GEP
=
959 Create(PointeeType
, Ptr
, IdxList
, NameStr
, InsertBefore
);
960 GEP
->setIsInBounds(true);
964 static GetElementPtrInst
*CreateInBounds(Value
*Ptr
,
965 ArrayRef
<Value
*> IdxList
,
966 const Twine
&NameStr
,
967 BasicBlock
*InsertAtEnd
) {
968 return CreateInBounds(nullptr, Ptr
, IdxList
, NameStr
, InsertAtEnd
);
971 static GetElementPtrInst
*CreateInBounds(Type
*PointeeType
, Value
*Ptr
,
972 ArrayRef
<Value
*> IdxList
,
973 const Twine
&NameStr
,
974 BasicBlock
*InsertAtEnd
) {
975 GetElementPtrInst
*GEP
=
976 Create(PointeeType
, Ptr
, IdxList
, NameStr
, InsertAtEnd
);
977 GEP
->setIsInBounds(true);
981 /// Transparently provide more efficient getOperand methods.
982 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
984 Type
*getSourceElementType() const { return SourceElementType
; }
986 void setSourceElementType(Type
*Ty
) { SourceElementType
= Ty
; }
987 void setResultElementType(Type
*Ty
) { ResultElementType
= Ty
; }
989 Type
*getResultElementType() const {
990 assert(ResultElementType
==
991 cast
<PointerType
>(getType()->getScalarType())->getElementType());
992 return ResultElementType
;
995 /// Returns the address space of this instruction's pointer type.
996 unsigned getAddressSpace() const {
997 // Note that this is always the same as the pointer operand's address space
998 // and that is cheaper to compute, so cheat here.
999 return getPointerAddressSpace();
1002 /// Returns the type of the element that would be loaded with
1003 /// a load instruction with the specified parameters.
1005 /// Null is returned if the indices are invalid for the specified
1008 static Type
*getIndexedType(Type
*Ty
, ArrayRef
<Value
*> IdxList
);
1009 static Type
*getIndexedType(Type
*Ty
, ArrayRef
<Constant
*> IdxList
);
1010 static Type
*getIndexedType(Type
*Ty
, ArrayRef
<uint64_t> IdxList
);
1012 inline op_iterator
idx_begin() { return op_begin()+1; }
1013 inline const_op_iterator
idx_begin() const { return op_begin()+1; }
1014 inline op_iterator
idx_end() { return op_end(); }
1015 inline const_op_iterator
idx_end() const { return op_end(); }
1017 inline iterator_range
<op_iterator
> indices() {
1018 return make_range(idx_begin(), idx_end());
1021 inline iterator_range
<const_op_iterator
> indices() const {
1022 return make_range(idx_begin(), idx_end());
1025 Value
*getPointerOperand() {
1026 return getOperand(0);
1028 const Value
*getPointerOperand() const {
1029 return getOperand(0);
1031 static unsigned getPointerOperandIndex() {
1032 return 0U; // get index for modifying correct operand.
1035 /// Method to return the pointer operand as a
1037 Type
*getPointerOperandType() const {
1038 return getPointerOperand()->getType();
1041 /// Returns the address space of the pointer operand.
1042 unsigned getPointerAddressSpace() const {
1043 return getPointerOperandType()->getPointerAddressSpace();
1046 /// Returns the pointer type returned by the GEP
1047 /// instruction, which may be a vector of pointers.
1048 static Type
*getGEPReturnType(Value
*Ptr
, ArrayRef
<Value
*> IdxList
) {
1049 return getGEPReturnType(
1050 cast
<PointerType
>(Ptr
->getType()->getScalarType())->getElementType(),
1053 static Type
*getGEPReturnType(Type
*ElTy
, Value
*Ptr
,
1054 ArrayRef
<Value
*> IdxList
) {
1055 Type
*PtrTy
= PointerType::get(checkGEPType(getIndexedType(ElTy
, IdxList
)),
1056 Ptr
->getType()->getPointerAddressSpace());
1058 if (Ptr
->getType()->isVectorTy()) {
1059 unsigned NumElem
= Ptr
->getType()->getVectorNumElements();
1060 return VectorType::get(PtrTy
, NumElem
);
1062 for (Value
*Index
: IdxList
)
1063 if (Index
->getType()->isVectorTy()) {
1064 unsigned NumElem
= Index
->getType()->getVectorNumElements();
1065 return VectorType::get(PtrTy
, NumElem
);
1071 unsigned getNumIndices() const { // Note: always non-negative
1072 return getNumOperands() - 1;
1075 bool hasIndices() const {
1076 return getNumOperands() > 1;
1079 /// Return true if all of the indices of this GEP are
1080 /// zeros. If so, the result pointer and the first operand have the same
1081 /// value, just potentially different types.
1082 bool hasAllZeroIndices() const;
1084 /// Return true if all of the indices of this GEP are
1085 /// constant integers. If so, the result pointer and the first operand have
1086 /// a constant offset between them.
1087 bool hasAllConstantIndices() const;
1089 /// Set or clear the inbounds flag on this GEP instruction.
1090 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1091 void setIsInBounds(bool b
= true);
1093 /// Determine whether the GEP has the inbounds flag.
1094 bool isInBounds() const;
1096 /// Accumulate the constant address offset of this GEP if possible.
1098 /// This routine accepts an APInt into which it will accumulate the constant
1099 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1100 /// all-constant, it returns false and the value of the offset APInt is
1101 /// undefined (it is *not* preserved!). The APInt passed into this routine
1102 /// must be at least as wide as the IntPtr type for the address space of
1103 /// the base GEP pointer.
1104 bool accumulateConstantOffset(const DataLayout
&DL
, APInt
&Offset
) const;
1106 // Methods for support type inquiry through isa, cast, and dyn_cast:
1107 static bool classof(const Instruction
*I
) {
1108 return (I
->getOpcode() == Instruction::GetElementPtr
);
1110 static bool classof(const Value
*V
) {
1111 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
1116 struct OperandTraits
<GetElementPtrInst
> :
1117 public VariadicOperandTraits
<GetElementPtrInst
, 1> {
1120 GetElementPtrInst::GetElementPtrInst(Type
*PointeeType
, Value
*Ptr
,
1121 ArrayRef
<Value
*> IdxList
, unsigned Values
,
1122 const Twine
&NameStr
,
1123 Instruction
*InsertBefore
)
1124 : Instruction(getGEPReturnType(PointeeType
, Ptr
, IdxList
), GetElementPtr
,
1125 OperandTraits
<GetElementPtrInst
>::op_end(this) - Values
,
1126 Values
, InsertBefore
),
1127 SourceElementType(PointeeType
),
1128 ResultElementType(getIndexedType(PointeeType
, IdxList
)) {
1129 assert(ResultElementType
==
1130 cast
<PointerType
>(getType()->getScalarType())->getElementType());
1131 init(Ptr
, IdxList
, NameStr
);
1134 GetElementPtrInst::GetElementPtrInst(Type
*PointeeType
, Value
*Ptr
,
1135 ArrayRef
<Value
*> IdxList
, unsigned Values
,
1136 const Twine
&NameStr
,
1137 BasicBlock
*InsertAtEnd
)
1138 : Instruction(getGEPReturnType(PointeeType
, Ptr
, IdxList
), GetElementPtr
,
1139 OperandTraits
<GetElementPtrInst
>::op_end(this) - Values
,
1140 Values
, InsertAtEnd
),
1141 SourceElementType(PointeeType
),
1142 ResultElementType(getIndexedType(PointeeType
, IdxList
)) {
1143 assert(ResultElementType
==
1144 cast
<PointerType
>(getType()->getScalarType())->getElementType());
1145 init(Ptr
, IdxList
, NameStr
);
1148 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst
, Value
)
1150 //===----------------------------------------------------------------------===//
1152 //===----------------------------------------------------------------------===//
1154 /// This instruction compares its operands according to the predicate given
1155 /// to the constructor. It only operates on integers or pointers. The operands
1156 /// must be identical types.
1157 /// Represent an integer comparison operator.
1158 class ICmpInst
: public CmpInst
{
1160 assert(isIntPredicate() &&
1161 "Invalid ICmp predicate value");
1162 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1163 "Both operands to ICmp instruction are not of the same type!");
1164 // Check that the operands are the right type
1165 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1166 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1167 "Invalid operand types for ICmp instruction");
1171 // Note: Instruction needs to be a friend here to call cloneImpl.
1172 friend class Instruction
;
1174 /// Clone an identical ICmpInst
1175 ICmpInst
*cloneImpl() const;
1178 /// Constructor with insert-before-instruction semantics.
1180 Instruction
*InsertBefore
, ///< Where to insert
1181 Predicate pred
, ///< The predicate to use for the comparison
1182 Value
*LHS
, ///< The left-hand-side of the expression
1183 Value
*RHS
, ///< The right-hand-side of the expression
1184 const Twine
&NameStr
= "" ///< Name of the instruction
1185 ) : CmpInst(makeCmpResultType(LHS
->getType()),
1186 Instruction::ICmp
, pred
, LHS
, RHS
, NameStr
,
1193 /// Constructor with insert-at-end semantics.
1195 BasicBlock
&InsertAtEnd
, ///< Block to insert into.
1196 Predicate pred
, ///< The predicate to use for the comparison
1197 Value
*LHS
, ///< The left-hand-side of the expression
1198 Value
*RHS
, ///< The right-hand-side of the expression
1199 const Twine
&NameStr
= "" ///< Name of the instruction
1200 ) : CmpInst(makeCmpResultType(LHS
->getType()),
1201 Instruction::ICmp
, pred
, LHS
, RHS
, NameStr
,
1208 /// Constructor with no-insertion semantics
1210 Predicate pred
, ///< The predicate to use for the comparison
1211 Value
*LHS
, ///< The left-hand-side of the expression
1212 Value
*RHS
, ///< The right-hand-side of the expression
1213 const Twine
&NameStr
= "" ///< Name of the instruction
1214 ) : CmpInst(makeCmpResultType(LHS
->getType()),
1215 Instruction::ICmp
, pred
, LHS
, RHS
, NameStr
) {
1221 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1222 /// @returns the predicate that would be the result if the operand were
1223 /// regarded as signed.
1224 /// Return the signed version of the predicate
1225 Predicate
getSignedPredicate() const {
1226 return getSignedPredicate(getPredicate());
1229 /// This is a static version that you can use without an instruction.
1230 /// Return the signed version of the predicate.
1231 static Predicate
getSignedPredicate(Predicate pred
);
1233 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1234 /// @returns the predicate that would be the result if the operand were
1235 /// regarded as unsigned.
1236 /// Return the unsigned version of the predicate
1237 Predicate
getUnsignedPredicate() const {
1238 return getUnsignedPredicate(getPredicate());
1241 /// This is a static version that you can use without an instruction.
1242 /// Return the unsigned version of the predicate.
1243 static Predicate
getUnsignedPredicate(Predicate pred
);
1245 /// Return true if this predicate is either EQ or NE. This also
1246 /// tests for commutativity.
1247 static bool isEquality(Predicate P
) {
1248 return P
== ICMP_EQ
|| P
== ICMP_NE
;
1251 /// Return true if this predicate is either EQ or NE. This also
1252 /// tests for commutativity.
1253 bool isEquality() const {
1254 return isEquality(getPredicate());
1257 /// @returns true if the predicate of this ICmpInst is commutative
1258 /// Determine if this relation is commutative.
1259 bool isCommutative() const { return isEquality(); }
1261 /// Return true if the predicate is relational (not EQ or NE).
1263 bool isRelational() const {
1264 return !isEquality();
1267 /// Return true if the predicate is relational (not EQ or NE).
1269 static bool isRelational(Predicate P
) {
1270 return !isEquality(P
);
1273 /// Exchange the two operands to this instruction in such a way that it does
1274 /// not modify the semantics of the instruction. The predicate value may be
1275 /// changed to retain the same result if the predicate is order dependent
1277 /// Swap operands and adjust predicate.
1278 void swapOperands() {
1279 setPredicate(getSwappedPredicate());
1280 Op
<0>().swap(Op
<1>());
1283 // Methods for support type inquiry through isa, cast, and dyn_cast:
1284 static bool classof(const Instruction
*I
) {
1285 return I
->getOpcode() == Instruction::ICmp
;
1287 static bool classof(const Value
*V
) {
1288 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
1292 //===----------------------------------------------------------------------===//
1294 //===----------------------------------------------------------------------===//
1296 /// This instruction compares its operands according to the predicate given
1297 /// to the constructor. It only operates on floating point values or packed
1298 /// vectors of floating point values. The operands must be identical types.
1299 /// Represents a floating point comparison operator.
1300 class FCmpInst
: public CmpInst
{
1302 assert(isFPPredicate() && "Invalid FCmp predicate value");
1303 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1304 "Both operands to FCmp instruction are not of the same type!");
1305 // Check that the operands are the right type
1306 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1307 "Invalid operand types for FCmp instruction");
1311 // Note: Instruction needs to be a friend here to call cloneImpl.
1312 friend class Instruction
;
1314 /// Clone an identical FCmpInst
1315 FCmpInst
*cloneImpl() const;
1318 /// Constructor with insert-before-instruction semantics.
1320 Instruction
*InsertBefore
, ///< Where to insert
1321 Predicate pred
, ///< The predicate to use for the comparison
1322 Value
*LHS
, ///< The left-hand-side of the expression
1323 Value
*RHS
, ///< The right-hand-side of the expression
1324 const Twine
&NameStr
= "" ///< Name of the instruction
1325 ) : CmpInst(makeCmpResultType(LHS
->getType()),
1326 Instruction::FCmp
, pred
, LHS
, RHS
, NameStr
,
1331 /// Constructor with insert-at-end semantics.
1333 BasicBlock
&InsertAtEnd
, ///< Block to insert into.
1334 Predicate pred
, ///< The predicate to use for the comparison
1335 Value
*LHS
, ///< The left-hand-side of the expression
1336 Value
*RHS
, ///< The right-hand-side of the expression
1337 const Twine
&NameStr
= "" ///< Name of the instruction
1338 ) : CmpInst(makeCmpResultType(LHS
->getType()),
1339 Instruction::FCmp
, pred
, LHS
, RHS
, NameStr
,
1344 /// Constructor with no-insertion semantics
1346 Predicate Pred
, ///< The predicate to use for the comparison
1347 Value
*LHS
, ///< The left-hand-side of the expression
1348 Value
*RHS
, ///< The right-hand-side of the expression
1349 const Twine
&NameStr
= "", ///< Name of the instruction
1350 Instruction
*FlagsSource
= nullptr
1351 ) : CmpInst(makeCmpResultType(LHS
->getType()), Instruction::FCmp
, Pred
, LHS
,
1352 RHS
, NameStr
, nullptr, FlagsSource
) {
1356 /// @returns true if the predicate of this instruction is EQ or NE.
1357 /// Determine if this is an equality predicate.
1358 static bool isEquality(Predicate Pred
) {
1359 return Pred
== FCMP_OEQ
|| Pred
== FCMP_ONE
|| Pred
== FCMP_UEQ
||
1363 /// @returns true if the predicate of this instruction is EQ or NE.
1364 /// Determine if this is an equality predicate.
1365 bool isEquality() const { return isEquality(getPredicate()); }
1367 /// @returns true if the predicate of this instruction is commutative.
1368 /// Determine if this is a commutative predicate.
1369 bool isCommutative() const {
1370 return isEquality() ||
1371 getPredicate() == FCMP_FALSE
||
1372 getPredicate() == FCMP_TRUE
||
1373 getPredicate() == FCMP_ORD
||
1374 getPredicate() == FCMP_UNO
;
1377 /// @returns true if the predicate is relational (not EQ or NE).
1378 /// Determine if this a relational predicate.
1379 bool isRelational() const { return !isEquality(); }
1381 /// Exchange the two operands to this instruction in such a way that it does
1382 /// not modify the semantics of the instruction. The predicate value may be
1383 /// changed to retain the same result if the predicate is order dependent
1385 /// Swap operands and adjust predicate.
1386 void swapOperands() {
1387 setPredicate(getSwappedPredicate());
1388 Op
<0>().swap(Op
<1>());
1391 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1392 static bool classof(const Instruction
*I
) {
1393 return I
->getOpcode() == Instruction::FCmp
;
1395 static bool classof(const Value
*V
) {
1396 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
1400 //===----------------------------------------------------------------------===//
1401 /// This class represents a function call, abstracting a target
1402 /// machine's calling convention. This class uses low bit of the SubClassData
1403 /// field to indicate whether or not this is a tail call. The rest of the bits
1404 /// hold the calling convention of the call.
1406 class CallInst
: public CallBase
{
1407 CallInst(const CallInst
&CI
);
1409 /// Construct a CallInst given a range of arguments.
1410 /// Construct a CallInst from a range of arguments
1411 inline CallInst(FunctionType
*Ty
, Value
*Func
, ArrayRef
<Value
*> Args
,
1412 ArrayRef
<OperandBundleDef
> Bundles
, const Twine
&NameStr
,
1413 Instruction
*InsertBefore
);
1415 inline CallInst(FunctionType
*Ty
, Value
*Func
, ArrayRef
<Value
*> Args
,
1416 const Twine
&NameStr
, Instruction
*InsertBefore
)
1417 : CallInst(Ty
, Func
, Args
, None
, NameStr
, InsertBefore
) {}
1419 /// Construct a CallInst given a range of arguments.
1420 /// Construct a CallInst from a range of arguments
1421 inline CallInst(FunctionType
*Ty
, Value
*Func
, ArrayRef
<Value
*> Args
,
1422 ArrayRef
<OperandBundleDef
> Bundles
, const Twine
&NameStr
,
1423 BasicBlock
*InsertAtEnd
);
1425 explicit CallInst(FunctionType
*Ty
, Value
*F
, const Twine
&NameStr
,
1426 Instruction
*InsertBefore
);
1428 CallInst(FunctionType
*ty
, Value
*F
, const Twine
&NameStr
,
1429 BasicBlock
*InsertAtEnd
);
1431 void init(FunctionType
*FTy
, Value
*Func
, ArrayRef
<Value
*> Args
,
1432 ArrayRef
<OperandBundleDef
> Bundles
, const Twine
&NameStr
);
1433 void init(FunctionType
*FTy
, Value
*Func
, const Twine
&NameStr
);
1435 /// Compute the number of operands to allocate.
1436 static int ComputeNumOperands(int NumArgs
, int NumBundleInputs
= 0) {
1437 // We need one operand for the called function, plus the input operand
1439 return 1 + NumArgs
+ NumBundleInputs
;
1443 // Note: Instruction needs to be a friend here to call cloneImpl.
1444 friend class Instruction
;
1446 CallInst
*cloneImpl() const;
1449 static CallInst
*Create(FunctionType
*Ty
, Value
*F
, const Twine
&NameStr
= "",
1450 Instruction
*InsertBefore
= nullptr) {
1451 return new (ComputeNumOperands(0)) CallInst(Ty
, F
, NameStr
, InsertBefore
);
1454 static CallInst
*Create(FunctionType
*Ty
, Value
*Func
, ArrayRef
<Value
*> Args
,
1455 const Twine
&NameStr
,
1456 Instruction
*InsertBefore
= nullptr) {
1457 return new (ComputeNumOperands(Args
.size()))
1458 CallInst(Ty
, Func
, Args
, None
, NameStr
, InsertBefore
);
1461 static CallInst
*Create(FunctionType
*Ty
, Value
*Func
, ArrayRef
<Value
*> Args
,
1462 ArrayRef
<OperandBundleDef
> Bundles
= None
,
1463 const Twine
&NameStr
= "",
1464 Instruction
*InsertBefore
= nullptr) {
1465 const int NumOperands
=
1466 ComputeNumOperands(Args
.size(), CountBundleInputs(Bundles
));
1467 const unsigned DescriptorBytes
= Bundles
.size() * sizeof(BundleOpInfo
);
1469 return new (NumOperands
, DescriptorBytes
)
1470 CallInst(Ty
, Func
, Args
, Bundles
, NameStr
, InsertBefore
);
1473 static CallInst
*Create(FunctionType
*Ty
, Value
*F
, const Twine
&NameStr
,
1474 BasicBlock
*InsertAtEnd
) {
1475 return new (ComputeNumOperands(0)) CallInst(Ty
, F
, NameStr
, InsertAtEnd
);
1478 static CallInst
*Create(FunctionType
*Ty
, Value
*Func
, ArrayRef
<Value
*> Args
,
1479 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
1480 return new (ComputeNumOperands(Args
.size()))
1481 CallInst(Ty
, Func
, Args
, None
, NameStr
, InsertAtEnd
);
1484 static CallInst
*Create(FunctionType
*Ty
, Value
*Func
, ArrayRef
<Value
*> Args
,
1485 ArrayRef
<OperandBundleDef
> Bundles
,
1486 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
1487 const int NumOperands
=
1488 ComputeNumOperands(Args
.size(), CountBundleInputs(Bundles
));
1489 const unsigned DescriptorBytes
= Bundles
.size() * sizeof(BundleOpInfo
);
1491 return new (NumOperands
, DescriptorBytes
)
1492 CallInst(Ty
, Func
, Args
, Bundles
, NameStr
, InsertAtEnd
);
1495 static CallInst
*Create(FunctionCallee Func
, const Twine
&NameStr
= "",
1496 Instruction
*InsertBefore
= nullptr) {
1497 return Create(Func
.getFunctionType(), Func
.getCallee(), NameStr
,
1501 static CallInst
*Create(FunctionCallee Func
, ArrayRef
<Value
*> Args
,
1502 ArrayRef
<OperandBundleDef
> Bundles
= None
,
1503 const Twine
&NameStr
= "",
1504 Instruction
*InsertBefore
= nullptr) {
1505 return Create(Func
.getFunctionType(), Func
.getCallee(), Args
, Bundles
,
1506 NameStr
, InsertBefore
);
1509 static CallInst
*Create(FunctionCallee Func
, ArrayRef
<Value
*> Args
,
1510 const Twine
&NameStr
,
1511 Instruction
*InsertBefore
= nullptr) {
1512 return Create(Func
.getFunctionType(), Func
.getCallee(), Args
, NameStr
,
1516 static CallInst
*Create(FunctionCallee Func
, const Twine
&NameStr
,
1517 BasicBlock
*InsertAtEnd
) {
1518 return Create(Func
.getFunctionType(), Func
.getCallee(), NameStr
,
1522 static CallInst
*Create(FunctionCallee Func
, ArrayRef
<Value
*> Args
,
1523 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
1524 return Create(Func
.getFunctionType(), Func
.getCallee(), Args
, NameStr
,
1528 static CallInst
*Create(FunctionCallee Func
, ArrayRef
<Value
*> Args
,
1529 ArrayRef
<OperandBundleDef
> Bundles
,
1530 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
1531 return Create(Func
.getFunctionType(), Func
.getCallee(), Args
, Bundles
,
1532 NameStr
, InsertAtEnd
);
1535 // Deprecated [opaque pointer types]
1536 static CallInst
*Create(Value
*Func
, const Twine
&NameStr
= "",
1537 Instruction
*InsertBefore
= nullptr) {
1538 return Create(cast
<FunctionType
>(
1539 cast
<PointerType
>(Func
->getType())->getElementType()),
1540 Func
, NameStr
, InsertBefore
);
1543 // Deprecated [opaque pointer types]
1544 static CallInst
*Create(Value
*Func
, ArrayRef
<Value
*> Args
,
1545 const Twine
&NameStr
,
1546 Instruction
*InsertBefore
= nullptr) {
1547 return Create(cast
<FunctionType
>(
1548 cast
<PointerType
>(Func
->getType())->getElementType()),
1549 Func
, Args
, NameStr
, InsertBefore
);
1552 // Deprecated [opaque pointer types]
1553 static CallInst
*Create(Value
*Func
, ArrayRef
<Value
*> Args
,
1554 ArrayRef
<OperandBundleDef
> Bundles
= None
,
1555 const Twine
&NameStr
= "",
1556 Instruction
*InsertBefore
= nullptr) {
1557 return Create(cast
<FunctionType
>(
1558 cast
<PointerType
>(Func
->getType())->getElementType()),
1559 Func
, Args
, Bundles
, NameStr
, InsertBefore
);
1562 // Deprecated [opaque pointer types]
1563 static CallInst
*Create(Value
*Func
, const Twine
&NameStr
,
1564 BasicBlock
*InsertAtEnd
) {
1565 return Create(cast
<FunctionType
>(
1566 cast
<PointerType
>(Func
->getType())->getElementType()),
1567 Func
, NameStr
, InsertAtEnd
);
1570 // Deprecated [opaque pointer types]
1571 static CallInst
*Create(Value
*Func
, ArrayRef
<Value
*> Args
,
1572 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
1573 return Create(cast
<FunctionType
>(
1574 cast
<PointerType
>(Func
->getType())->getElementType()),
1575 Func
, Args
, NameStr
, InsertAtEnd
);
1578 // Deprecated [opaque pointer types]
1579 static CallInst
*Create(Value
*Func
, ArrayRef
<Value
*> Args
,
1580 ArrayRef
<OperandBundleDef
> Bundles
,
1581 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
1582 return Create(cast
<FunctionType
>(
1583 cast
<PointerType
>(Func
->getType())->getElementType()),
1584 Func
, Args
, Bundles
, NameStr
, InsertAtEnd
);
1587 /// Create a clone of \p CI with a different set of operand bundles and
1588 /// insert it before \p InsertPt.
1590 /// The returned call instruction is identical \p CI in every way except that
1591 /// the operand bundles for the new instruction are set to the operand bundles
1593 static CallInst
*Create(CallInst
*CI
, ArrayRef
<OperandBundleDef
> Bundles
,
1594 Instruction
*InsertPt
= nullptr);
1596 /// Generate the IR for a call to malloc:
1597 /// 1. Compute the malloc call's argument as the specified type's size,
1598 /// possibly multiplied by the array size if the array size is not
1600 /// 2. Call malloc with that argument.
1601 /// 3. Bitcast the result of the malloc call to the specified type.
1602 static Instruction
*CreateMalloc(Instruction
*InsertBefore
, Type
*IntPtrTy
,
1603 Type
*AllocTy
, Value
*AllocSize
,
1604 Value
*ArraySize
= nullptr,
1605 Function
*MallocF
= nullptr,
1606 const Twine
&Name
= "");
1607 static Instruction
*CreateMalloc(BasicBlock
*InsertAtEnd
, Type
*IntPtrTy
,
1608 Type
*AllocTy
, Value
*AllocSize
,
1609 Value
*ArraySize
= nullptr,
1610 Function
*MallocF
= nullptr,
1611 const Twine
&Name
= "");
1612 static Instruction
*CreateMalloc(Instruction
*InsertBefore
, Type
*IntPtrTy
,
1613 Type
*AllocTy
, Value
*AllocSize
,
1614 Value
*ArraySize
= nullptr,
1615 ArrayRef
<OperandBundleDef
> Bundles
= None
,
1616 Function
*MallocF
= nullptr,
1617 const Twine
&Name
= "");
1618 static Instruction
*CreateMalloc(BasicBlock
*InsertAtEnd
, Type
*IntPtrTy
,
1619 Type
*AllocTy
, Value
*AllocSize
,
1620 Value
*ArraySize
= nullptr,
1621 ArrayRef
<OperandBundleDef
> Bundles
= None
,
1622 Function
*MallocF
= nullptr,
1623 const Twine
&Name
= "");
1624 /// Generate the IR for a call to the builtin free function.
1625 static Instruction
*CreateFree(Value
*Source
, Instruction
*InsertBefore
);
1626 static Instruction
*CreateFree(Value
*Source
, BasicBlock
*InsertAtEnd
);
1627 static Instruction
*CreateFree(Value
*Source
,
1628 ArrayRef
<OperandBundleDef
> Bundles
,
1629 Instruction
*InsertBefore
);
1630 static Instruction
*CreateFree(Value
*Source
,
1631 ArrayRef
<OperandBundleDef
> Bundles
,
1632 BasicBlock
*InsertAtEnd
);
1634 // Note that 'musttail' implies 'tail'.
1641 TailCallKind
getTailCallKind() const {
1642 return TailCallKind(getSubclassDataFromInstruction() & 3);
1645 bool isTailCall() const {
1646 unsigned Kind
= getSubclassDataFromInstruction() & 3;
1647 return Kind
== TCK_Tail
|| Kind
== TCK_MustTail
;
1650 bool isMustTailCall() const {
1651 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail
;
1654 bool isNoTailCall() const {
1655 return (getSubclassDataFromInstruction() & 3) == TCK_NoTail
;
1658 void setTailCall(bool isTC
= true) {
1659 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1660 unsigned(isTC
? TCK_Tail
: TCK_None
));
1663 void setTailCallKind(TailCallKind TCK
) {
1664 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1668 /// Return true if the call can return twice
1669 bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice
); }
1670 void setCanReturnTwice() {
1671 addAttribute(AttributeList::FunctionIndex
, Attribute::ReturnsTwice
);
1674 // Methods for support type inquiry through isa, cast, and dyn_cast:
1675 static bool classof(const Instruction
*I
) {
1676 return I
->getOpcode() == Instruction::Call
;
1678 static bool classof(const Value
*V
) {
1679 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
1682 /// Updates profile metadata by scaling it by \p S / \p T.
1683 void updateProfWeight(uint64_t S
, uint64_t T
);
1686 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1687 // method so that subclasses cannot accidentally use it.
1688 void setInstructionSubclassData(unsigned short D
) {
1689 Instruction::setInstructionSubclassData(D
);
1693 CallInst::CallInst(FunctionType
*Ty
, Value
*Func
, ArrayRef
<Value
*> Args
,
1694 ArrayRef
<OperandBundleDef
> Bundles
, const Twine
&NameStr
,
1695 BasicBlock
*InsertAtEnd
)
1696 : CallBase(Ty
->getReturnType(), Instruction::Call
,
1697 OperandTraits
<CallBase
>::op_end(this) -
1698 (Args
.size() + CountBundleInputs(Bundles
) + 1),
1699 unsigned(Args
.size() + CountBundleInputs(Bundles
) + 1),
1701 init(Ty
, Func
, Args
, Bundles
, NameStr
);
1704 CallInst::CallInst(FunctionType
*Ty
, Value
*Func
, ArrayRef
<Value
*> Args
,
1705 ArrayRef
<OperandBundleDef
> Bundles
, const Twine
&NameStr
,
1706 Instruction
*InsertBefore
)
1707 : CallBase(Ty
->getReturnType(), Instruction::Call
,
1708 OperandTraits
<CallBase
>::op_end(this) -
1709 (Args
.size() + CountBundleInputs(Bundles
) + 1),
1710 unsigned(Args
.size() + CountBundleInputs(Bundles
) + 1),
1712 init(Ty
, Func
, Args
, Bundles
, NameStr
);
1715 //===----------------------------------------------------------------------===//
1717 //===----------------------------------------------------------------------===//
1719 /// This class represents the LLVM 'select' instruction.
1721 class SelectInst
: public Instruction
{
1722 SelectInst(Value
*C
, Value
*S1
, Value
*S2
, const Twine
&NameStr
,
1723 Instruction
*InsertBefore
)
1724 : Instruction(S1
->getType(), Instruction::Select
,
1725 &Op
<0>(), 3, InsertBefore
) {
1730 SelectInst(Value
*C
, Value
*S1
, Value
*S2
, const Twine
&NameStr
,
1731 BasicBlock
*InsertAtEnd
)
1732 : Instruction(S1
->getType(), Instruction::Select
,
1733 &Op
<0>(), 3, InsertAtEnd
) {
1738 void init(Value
*C
, Value
*S1
, Value
*S2
) {
1739 assert(!areInvalidOperands(C
, S1
, S2
) && "Invalid operands for select");
1746 // Note: Instruction needs to be a friend here to call cloneImpl.
1747 friend class Instruction
;
1749 SelectInst
*cloneImpl() const;
1752 static SelectInst
*Create(Value
*C
, Value
*S1
, Value
*S2
,
1753 const Twine
&NameStr
= "",
1754 Instruction
*InsertBefore
= nullptr,
1755 Instruction
*MDFrom
= nullptr) {
1756 SelectInst
*Sel
= new(3) SelectInst(C
, S1
, S2
, NameStr
, InsertBefore
);
1758 Sel
->copyMetadata(*MDFrom
);
1762 static SelectInst
*Create(Value
*C
, Value
*S1
, Value
*S2
,
1763 const Twine
&NameStr
,
1764 BasicBlock
*InsertAtEnd
) {
1765 return new(3) SelectInst(C
, S1
, S2
, NameStr
, InsertAtEnd
);
1768 const Value
*getCondition() const { return Op
<0>(); }
1769 const Value
*getTrueValue() const { return Op
<1>(); }
1770 const Value
*getFalseValue() const { return Op
<2>(); }
1771 Value
*getCondition() { return Op
<0>(); }
1772 Value
*getTrueValue() { return Op
<1>(); }
1773 Value
*getFalseValue() { return Op
<2>(); }
1775 void setCondition(Value
*V
) { Op
<0>() = V
; }
1776 void setTrueValue(Value
*V
) { Op
<1>() = V
; }
1777 void setFalseValue(Value
*V
) { Op
<2>() = V
; }
1779 /// Swap the true and false values of the select instruction.
1780 /// This doesn't swap prof metadata.
1781 void swapValues() { Op
<1>().swap(Op
<2>()); }
1783 /// Return a string if the specified operands are invalid
1784 /// for a select operation, otherwise return null.
1785 static const char *areInvalidOperands(Value
*Cond
, Value
*True
, Value
*False
);
1787 /// Transparently provide more efficient getOperand methods.
1788 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
1790 OtherOps
getOpcode() const {
1791 return static_cast<OtherOps
>(Instruction::getOpcode());
1794 // Methods for support type inquiry through isa, cast, and dyn_cast:
1795 static bool classof(const Instruction
*I
) {
1796 return I
->getOpcode() == Instruction::Select
;
1798 static bool classof(const Value
*V
) {
1799 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
1804 struct OperandTraits
<SelectInst
> : public FixedNumOperandTraits
<SelectInst
, 3> {
1807 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst
, Value
)
1809 //===----------------------------------------------------------------------===//
1811 //===----------------------------------------------------------------------===//
1813 /// This class represents the va_arg llvm instruction, which returns
1814 /// an argument of the specified type given a va_list and increments that list
1816 class VAArgInst
: public UnaryInstruction
{
1818 // Note: Instruction needs to be a friend here to call cloneImpl.
1819 friend class Instruction
;
1821 VAArgInst
*cloneImpl() const;
1824 VAArgInst(Value
*List
, Type
*Ty
, const Twine
&NameStr
= "",
1825 Instruction
*InsertBefore
= nullptr)
1826 : UnaryInstruction(Ty
, VAArg
, List
, InsertBefore
) {
1830 VAArgInst(Value
*List
, Type
*Ty
, const Twine
&NameStr
,
1831 BasicBlock
*InsertAtEnd
)
1832 : UnaryInstruction(Ty
, VAArg
, List
, InsertAtEnd
) {
1836 Value
*getPointerOperand() { return getOperand(0); }
1837 const Value
*getPointerOperand() const { return getOperand(0); }
1838 static unsigned getPointerOperandIndex() { return 0U; }
1840 // Methods for support type inquiry through isa, cast, and dyn_cast:
1841 static bool classof(const Instruction
*I
) {
1842 return I
->getOpcode() == VAArg
;
1844 static bool classof(const Value
*V
) {
1845 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
1849 //===----------------------------------------------------------------------===//
1850 // ExtractElementInst Class
1851 //===----------------------------------------------------------------------===//
1853 /// This instruction extracts a single (scalar)
1854 /// element from a VectorType value
1856 class ExtractElementInst
: public Instruction
{
1857 ExtractElementInst(Value
*Vec
, Value
*Idx
, const Twine
&NameStr
= "",
1858 Instruction
*InsertBefore
= nullptr);
1859 ExtractElementInst(Value
*Vec
, Value
*Idx
, const Twine
&NameStr
,
1860 BasicBlock
*InsertAtEnd
);
1863 // Note: Instruction needs to be a friend here to call cloneImpl.
1864 friend class Instruction
;
1866 ExtractElementInst
*cloneImpl() const;
1869 static ExtractElementInst
*Create(Value
*Vec
, Value
*Idx
,
1870 const Twine
&NameStr
= "",
1871 Instruction
*InsertBefore
= nullptr) {
1872 return new(2) ExtractElementInst(Vec
, Idx
, NameStr
, InsertBefore
);
1875 static ExtractElementInst
*Create(Value
*Vec
, Value
*Idx
,
1876 const Twine
&NameStr
,
1877 BasicBlock
*InsertAtEnd
) {
1878 return new(2) ExtractElementInst(Vec
, Idx
, NameStr
, InsertAtEnd
);
1881 /// Return true if an extractelement instruction can be
1882 /// formed with the specified operands.
1883 static bool isValidOperands(const Value
*Vec
, const Value
*Idx
);
1885 Value
*getVectorOperand() { return Op
<0>(); }
1886 Value
*getIndexOperand() { return Op
<1>(); }
1887 const Value
*getVectorOperand() const { return Op
<0>(); }
1888 const Value
*getIndexOperand() const { return Op
<1>(); }
1890 VectorType
*getVectorOperandType() const {
1891 return cast
<VectorType
>(getVectorOperand()->getType());
1894 /// Transparently provide more efficient getOperand methods.
1895 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
1897 // Methods for support type inquiry through isa, cast, and dyn_cast:
1898 static bool classof(const Instruction
*I
) {
1899 return I
->getOpcode() == Instruction::ExtractElement
;
1901 static bool classof(const Value
*V
) {
1902 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
1907 struct OperandTraits
<ExtractElementInst
> :
1908 public FixedNumOperandTraits
<ExtractElementInst
, 2> {
1911 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst
, Value
)
1913 //===----------------------------------------------------------------------===//
1914 // InsertElementInst Class
1915 //===----------------------------------------------------------------------===//
1917 /// This instruction inserts a single (scalar)
1918 /// element into a VectorType value
1920 class InsertElementInst
: public Instruction
{
1921 InsertElementInst(Value
*Vec
, Value
*NewElt
, Value
*Idx
,
1922 const Twine
&NameStr
= "",
1923 Instruction
*InsertBefore
= nullptr);
1924 InsertElementInst(Value
*Vec
, Value
*NewElt
, Value
*Idx
, const Twine
&NameStr
,
1925 BasicBlock
*InsertAtEnd
);
1928 // Note: Instruction needs to be a friend here to call cloneImpl.
1929 friend class Instruction
;
1931 InsertElementInst
*cloneImpl() const;
1934 static InsertElementInst
*Create(Value
*Vec
, Value
*NewElt
, Value
*Idx
,
1935 const Twine
&NameStr
= "",
1936 Instruction
*InsertBefore
= nullptr) {
1937 return new(3) InsertElementInst(Vec
, NewElt
, Idx
, NameStr
, InsertBefore
);
1940 static InsertElementInst
*Create(Value
*Vec
, Value
*NewElt
, Value
*Idx
,
1941 const Twine
&NameStr
,
1942 BasicBlock
*InsertAtEnd
) {
1943 return new(3) InsertElementInst(Vec
, NewElt
, Idx
, NameStr
, InsertAtEnd
);
1946 /// Return true if an insertelement instruction can be
1947 /// formed with the specified operands.
1948 static bool isValidOperands(const Value
*Vec
, const Value
*NewElt
,
1951 /// Overload to return most specific vector type.
1953 VectorType
*getType() const {
1954 return cast
<VectorType
>(Instruction::getType());
1957 /// Transparently provide more efficient getOperand methods.
1958 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
1960 // Methods for support type inquiry through isa, cast, and dyn_cast:
1961 static bool classof(const Instruction
*I
) {
1962 return I
->getOpcode() == Instruction::InsertElement
;
1964 static bool classof(const Value
*V
) {
1965 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
1970 struct OperandTraits
<InsertElementInst
> :
1971 public FixedNumOperandTraits
<InsertElementInst
, 3> {
1974 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst
, Value
)
1976 //===----------------------------------------------------------------------===//
1977 // ShuffleVectorInst Class
1978 //===----------------------------------------------------------------------===//
1980 /// This instruction constructs a fixed permutation of two
1983 class ShuffleVectorInst
: public Instruction
{
1985 // Note: Instruction needs to be a friend here to call cloneImpl.
1986 friend class Instruction
;
1988 ShuffleVectorInst
*cloneImpl() const;
1991 ShuffleVectorInst(Value
*V1
, Value
*V2
, Value
*Mask
,
1992 const Twine
&NameStr
= "",
1993 Instruction
*InsertBefor
= nullptr);
1994 ShuffleVectorInst(Value
*V1
, Value
*V2
, Value
*Mask
,
1995 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
);
1997 // allocate space for exactly three operands
1998 void *operator new(size_t s
) {
1999 return User::operator new(s
, 3);
2002 /// Swap the first 2 operands and adjust the mask to preserve the semantics
2003 /// of the instruction.
2006 /// Return true if a shufflevector instruction can be
2007 /// formed with the specified operands.
2008 static bool isValidOperands(const Value
*V1
, const Value
*V2
,
2011 /// Overload to return most specific vector type.
2013 VectorType
*getType() const {
2014 return cast
<VectorType
>(Instruction::getType());
2017 /// Transparently provide more efficient getOperand methods.
2018 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
2020 Constant
*getMask() const {
2021 return cast
<Constant
>(getOperand(2));
2024 /// Return the shuffle mask value for the specified element of the mask.
2025 /// Return -1 if the element is undef.
2026 static int getMaskValue(const Constant
*Mask
, unsigned Elt
);
2028 /// Return the shuffle mask value of this instruction for the given element
2029 /// index. Return -1 if the element is undef.
2030 int getMaskValue(unsigned Elt
) const {
2031 return getMaskValue(getMask(), Elt
);
2034 /// Convert the input shuffle mask operand to a vector of integers. Undefined
2035 /// elements of the mask are returned as -1.
2036 static void getShuffleMask(const Constant
*Mask
,
2037 SmallVectorImpl
<int> &Result
);
2039 /// Return the mask for this instruction as a vector of integers. Undefined
2040 /// elements of the mask are returned as -1.
2041 void getShuffleMask(SmallVectorImpl
<int> &Result
) const {
2042 return getShuffleMask(getMask(), Result
);
2045 SmallVector
<int, 16> getShuffleMask() const {
2046 SmallVector
<int, 16> Mask
;
2047 getShuffleMask(Mask
);
2051 /// Return true if this shuffle returns a vector with a different number of
2052 /// elements than its source vectors.
2053 /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3>
2054 /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5>
2055 bool changesLength() const {
2056 unsigned NumSourceElts
= Op
<0>()->getType()->getVectorNumElements();
2057 unsigned NumMaskElts
= getMask()->getType()->getVectorNumElements();
2058 return NumSourceElts
!= NumMaskElts
;
2061 /// Return true if this shuffle returns a vector with a greater number of
2062 /// elements than its source vectors.
2063 /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3>
2064 bool increasesLength() const {
2065 unsigned NumSourceElts
= Op
<0>()->getType()->getVectorNumElements();
2066 unsigned NumMaskElts
= getMask()->getType()->getVectorNumElements();
2067 return NumSourceElts
< NumMaskElts
;
2070 /// Return true if this shuffle mask chooses elements from exactly one source
2072 /// Example: <7,5,undef,7>
2073 /// This assumes that vector operands are the same length as the mask.
2074 static bool isSingleSourceMask(ArrayRef
<int> Mask
);
2075 static bool isSingleSourceMask(const Constant
*Mask
) {
2076 assert(Mask
->getType()->isVectorTy() && "Shuffle needs vector constant.");
2077 SmallVector
<int, 16> MaskAsInts
;
2078 getShuffleMask(Mask
, MaskAsInts
);
2079 return isSingleSourceMask(MaskAsInts
);
2082 /// Return true if this shuffle chooses elements from exactly one source
2083 /// vector without changing the length of that vector.
2084 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
2085 /// TODO: Optionally allow length-changing shuffles.
2086 bool isSingleSource() const {
2087 return !changesLength() && isSingleSourceMask(getMask());
2090 /// Return true if this shuffle mask chooses elements from exactly one source
2091 /// vector without lane crossings. A shuffle using this mask is not
2092 /// necessarily a no-op because it may change the number of elements from its
2093 /// input vectors or it may provide demanded bits knowledge via undef lanes.
2094 /// Example: <undef,undef,2,3>
2095 static bool isIdentityMask(ArrayRef
<int> Mask
);
2096 static bool isIdentityMask(const Constant
*Mask
) {
2097 assert(Mask
->getType()->isVectorTy() && "Shuffle needs vector constant.");
2098 SmallVector
<int, 16> MaskAsInts
;
2099 getShuffleMask(Mask
, MaskAsInts
);
2100 return isIdentityMask(MaskAsInts
);
2103 /// Return true if this shuffle chooses elements from exactly one source
2104 /// vector without lane crossings and does not change the number of elements
2105 /// from its input vectors.
2106 /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef>
2107 bool isIdentity() const {
2108 return !changesLength() && isIdentityMask(getShuffleMask());
2111 /// Return true if this shuffle lengthens exactly one source vector with
2112 /// undefs in the high elements.
2113 bool isIdentityWithPadding() const;
2115 /// Return true if this shuffle extracts the first N elements of exactly one
2117 bool isIdentityWithExtract() const;
2119 /// Return true if this shuffle concatenates its 2 source vectors. This
2120 /// returns false if either input is undefined. In that case, the shuffle is
2121 /// is better classified as an identity with padding operation.
2122 bool isConcat() const;
2124 /// Return true if this shuffle mask chooses elements from its source vectors
2125 /// without lane crossings. A shuffle using this mask would be
2126 /// equivalent to a vector select with a constant condition operand.
2127 /// Example: <4,1,6,undef>
2128 /// This returns false if the mask does not choose from both input vectors.
2129 /// In that case, the shuffle is better classified as an identity shuffle.
2130 /// This assumes that vector operands are the same length as the mask
2131 /// (a length-changing shuffle can never be equivalent to a vector select).
2132 static bool isSelectMask(ArrayRef
<int> Mask
);
2133 static bool isSelectMask(const Constant
*Mask
) {
2134 assert(Mask
->getType()->isVectorTy() && "Shuffle needs vector constant.");
2135 SmallVector
<int, 16> MaskAsInts
;
2136 getShuffleMask(Mask
, MaskAsInts
);
2137 return isSelectMask(MaskAsInts
);
2140 /// Return true if this shuffle chooses elements from its source vectors
2141 /// without lane crossings and all operands have the same number of elements.
2142 /// In other words, this shuffle is equivalent to a vector select with a
2143 /// constant condition operand.
2144 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3>
2145 /// This returns false if the mask does not choose from both input vectors.
2146 /// In that case, the shuffle is better classified as an identity shuffle.
2147 /// TODO: Optionally allow length-changing shuffles.
2148 bool isSelect() const {
2149 return !changesLength() && isSelectMask(getMask());
2152 /// Return true if this shuffle mask swaps the order of elements from exactly
2153 /// one source vector.
2154 /// Example: <7,6,undef,4>
2155 /// This assumes that vector operands are the same length as the mask.
2156 static bool isReverseMask(ArrayRef
<int> Mask
);
2157 static bool isReverseMask(const Constant
*Mask
) {
2158 assert(Mask
->getType()->isVectorTy() && "Shuffle needs vector constant.");
2159 SmallVector
<int, 16> MaskAsInts
;
2160 getShuffleMask(Mask
, MaskAsInts
);
2161 return isReverseMask(MaskAsInts
);
2164 /// Return true if this shuffle swaps the order of elements from exactly
2165 /// one source vector.
2166 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
2167 /// TODO: Optionally allow length-changing shuffles.
2168 bool isReverse() const {
2169 return !changesLength() && isReverseMask(getMask());
2172 /// Return true if this shuffle mask chooses all elements with the same value
2173 /// as the first element of exactly one source vector.
2174 /// Example: <4,undef,undef,4>
2175 /// This assumes that vector operands are the same length as the mask.
2176 static bool isZeroEltSplatMask(ArrayRef
<int> Mask
);
2177 static bool isZeroEltSplatMask(const Constant
*Mask
) {
2178 assert(Mask
->getType()->isVectorTy() && "Shuffle needs vector constant.");
2179 SmallVector
<int, 16> MaskAsInts
;
2180 getShuffleMask(Mask
, MaskAsInts
);
2181 return isZeroEltSplatMask(MaskAsInts
);
2184 /// Return true if all elements of this shuffle are the same value as the
2185 /// first element of exactly one source vector without changing the length
2187 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0>
2188 /// TODO: Optionally allow length-changing shuffles.
2189 /// TODO: Optionally allow splats from other elements.
2190 bool isZeroEltSplat() const {
2191 return !changesLength() && isZeroEltSplatMask(getMask());
2194 /// Return true if this shuffle mask is a transpose mask.
2195 /// Transpose vector masks transpose a 2xn matrix. They read corresponding
2196 /// even- or odd-numbered vector elements from two n-dimensional source
2197 /// vectors and write each result into consecutive elements of an
2198 /// n-dimensional destination vector. Two shuffles are necessary to complete
2199 /// the transpose, one for the even elements and another for the odd elements.
2200 /// This description closely follows how the TRN1 and TRN2 AArch64
2201 /// instructions operate.
2203 /// For example, a simple 2x2 matrix can be transposed with:
2205 /// ; Original matrix
2209 /// ; Transposed matrix
2210 /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 >
2211 /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 >
2213 /// For matrices having greater than n columns, the resulting nx2 transposed
2214 /// matrix is stored in two result vectors such that one vector contains
2215 /// interleaved elements from all the even-numbered rows and the other vector
2216 /// contains interleaved elements from all the odd-numbered rows. For example,
2217 /// a 2x4 matrix can be transposed with:
2219 /// ; Original matrix
2220 /// m0 = < a, b, c, d >
2221 /// m1 = < e, f, g, h >
2223 /// ; Transposed matrix
2224 /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 >
2225 /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 >
2226 static bool isTransposeMask(ArrayRef
<int> Mask
);
2227 static bool isTransposeMask(const Constant
*Mask
) {
2228 assert(Mask
->getType()->isVectorTy() && "Shuffle needs vector constant.");
2229 SmallVector
<int, 16> MaskAsInts
;
2230 getShuffleMask(Mask
, MaskAsInts
);
2231 return isTransposeMask(MaskAsInts
);
2234 /// Return true if this shuffle transposes the elements of its inputs without
2235 /// changing the length of the vectors. This operation may also be known as a
2236 /// merge or interleave. See the description for isTransposeMask() for the
2237 /// exact specification.
2238 /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
2239 bool isTranspose() const {
2240 return !changesLength() && isTransposeMask(getMask());
2243 /// Return true if this shuffle mask is an extract subvector mask.
2244 /// A valid extract subvector mask returns a smaller vector from a single
2245 /// source operand. The base extraction index is returned as well.
2246 static bool isExtractSubvectorMask(ArrayRef
<int> Mask
, int NumSrcElts
,
2248 static bool isExtractSubvectorMask(const Constant
*Mask
, int NumSrcElts
,
2250 assert(Mask
->getType()->isVectorTy() && "Shuffle needs vector constant.");
2251 SmallVector
<int, 16> MaskAsInts
;
2252 getShuffleMask(Mask
, MaskAsInts
);
2253 return isExtractSubvectorMask(MaskAsInts
, NumSrcElts
, Index
);
2256 /// Return true if this shuffle mask is an extract subvector mask.
2257 bool isExtractSubvectorMask(int &Index
) const {
2258 int NumSrcElts
= Op
<0>()->getType()->getVectorNumElements();
2259 return isExtractSubvectorMask(getMask(), NumSrcElts
, Index
);
2262 /// Change values in a shuffle permute mask assuming the two vector operands
2263 /// of length InVecNumElts have swapped position.
2264 static void commuteShuffleMask(MutableArrayRef
<int> Mask
,
2265 unsigned InVecNumElts
) {
2266 for (int &Idx
: Mask
) {
2269 Idx
= Idx
< (int)InVecNumElts
? Idx
+ InVecNumElts
: Idx
- InVecNumElts
;
2270 assert(Idx
>= 0 && Idx
< (int)InVecNumElts
* 2 &&
2271 "shufflevector mask index out of range");
2275 // Methods for support type inquiry through isa, cast, and dyn_cast:
2276 static bool classof(const Instruction
*I
) {
2277 return I
->getOpcode() == Instruction::ShuffleVector
;
2279 static bool classof(const Value
*V
) {
2280 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
2285 struct OperandTraits
<ShuffleVectorInst
> :
2286 public FixedNumOperandTraits
<ShuffleVectorInst
, 3> {
2289 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst
, Value
)
2291 //===----------------------------------------------------------------------===//
2292 // ExtractValueInst Class
2293 //===----------------------------------------------------------------------===//
2295 /// This instruction extracts a struct member or array
2296 /// element value from an aggregate value.
2298 class ExtractValueInst
: public UnaryInstruction
{
2299 SmallVector
<unsigned, 4> Indices
;
2301 ExtractValueInst(const ExtractValueInst
&EVI
);
2303 /// Constructors - Create a extractvalue instruction with a base aggregate
2304 /// value and a list of indices. The first ctor can optionally insert before
2305 /// an existing instruction, the second appends the new instruction to the
2306 /// specified BasicBlock.
2307 inline ExtractValueInst(Value
*Agg
,
2308 ArrayRef
<unsigned> Idxs
,
2309 const Twine
&NameStr
,
2310 Instruction
*InsertBefore
);
2311 inline ExtractValueInst(Value
*Agg
,
2312 ArrayRef
<unsigned> Idxs
,
2313 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
);
2315 void init(ArrayRef
<unsigned> Idxs
, const Twine
&NameStr
);
2318 // Note: Instruction needs to be a friend here to call cloneImpl.
2319 friend class Instruction
;
2321 ExtractValueInst
*cloneImpl() const;
2324 static ExtractValueInst
*Create(Value
*Agg
,
2325 ArrayRef
<unsigned> Idxs
,
2326 const Twine
&NameStr
= "",
2327 Instruction
*InsertBefore
= nullptr) {
2329 ExtractValueInst(Agg
, Idxs
, NameStr
, InsertBefore
);
2332 static ExtractValueInst
*Create(Value
*Agg
,
2333 ArrayRef
<unsigned> Idxs
,
2334 const Twine
&NameStr
,
2335 BasicBlock
*InsertAtEnd
) {
2336 return new ExtractValueInst(Agg
, Idxs
, NameStr
, InsertAtEnd
);
2339 /// Returns the type of the element that would be extracted
2340 /// with an extractvalue instruction with the specified parameters.
2342 /// Null is returned if the indices are invalid for the specified type.
2343 static Type
*getIndexedType(Type
*Agg
, ArrayRef
<unsigned> Idxs
);
2345 using idx_iterator
= const unsigned*;
2347 inline idx_iterator
idx_begin() const { return Indices
.begin(); }
2348 inline idx_iterator
idx_end() const { return Indices
.end(); }
2349 inline iterator_range
<idx_iterator
> indices() const {
2350 return make_range(idx_begin(), idx_end());
2353 Value
*getAggregateOperand() {
2354 return getOperand(0);
2356 const Value
*getAggregateOperand() const {
2357 return getOperand(0);
2359 static unsigned getAggregateOperandIndex() {
2360 return 0U; // get index for modifying correct operand
2363 ArrayRef
<unsigned> getIndices() const {
2367 unsigned getNumIndices() const {
2368 return (unsigned)Indices
.size();
2371 bool hasIndices() const {
2375 // Methods for support type inquiry through isa, cast, and dyn_cast:
2376 static bool classof(const Instruction
*I
) {
2377 return I
->getOpcode() == Instruction::ExtractValue
;
2379 static bool classof(const Value
*V
) {
2380 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
2384 ExtractValueInst::ExtractValueInst(Value
*Agg
,
2385 ArrayRef
<unsigned> Idxs
,
2386 const Twine
&NameStr
,
2387 Instruction
*InsertBefore
)
2388 : UnaryInstruction(checkGEPType(getIndexedType(Agg
->getType(), Idxs
)),
2389 ExtractValue
, Agg
, InsertBefore
) {
2390 init(Idxs
, NameStr
);
2393 ExtractValueInst::ExtractValueInst(Value
*Agg
,
2394 ArrayRef
<unsigned> Idxs
,
2395 const Twine
&NameStr
,
2396 BasicBlock
*InsertAtEnd
)
2397 : UnaryInstruction(checkGEPType(getIndexedType(Agg
->getType(), Idxs
)),
2398 ExtractValue
, Agg
, InsertAtEnd
) {
2399 init(Idxs
, NameStr
);
2402 //===----------------------------------------------------------------------===//
2403 // InsertValueInst Class
2404 //===----------------------------------------------------------------------===//
2406 /// This instruction inserts a struct field of array element
2407 /// value into an aggregate value.
2409 class InsertValueInst
: public Instruction
{
2410 SmallVector
<unsigned, 4> Indices
;
2412 InsertValueInst(const InsertValueInst
&IVI
);
2414 /// Constructors - Create a insertvalue instruction with a base aggregate
2415 /// value, a value to insert, and a list of indices. The first ctor can
2416 /// optionally insert before an existing instruction, the second appends
2417 /// the new instruction to the specified BasicBlock.
2418 inline InsertValueInst(Value
*Agg
, Value
*Val
,
2419 ArrayRef
<unsigned> Idxs
,
2420 const Twine
&NameStr
,
2421 Instruction
*InsertBefore
);
2422 inline InsertValueInst(Value
*Agg
, Value
*Val
,
2423 ArrayRef
<unsigned> Idxs
,
2424 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
);
2426 /// Constructors - These two constructors are convenience methods because one
2427 /// and two index insertvalue instructions are so common.
2428 InsertValueInst(Value
*Agg
, Value
*Val
, unsigned Idx
,
2429 const Twine
&NameStr
= "",
2430 Instruction
*InsertBefore
= nullptr);
2431 InsertValueInst(Value
*Agg
, Value
*Val
, unsigned Idx
, const Twine
&NameStr
,
2432 BasicBlock
*InsertAtEnd
);
2434 void init(Value
*Agg
, Value
*Val
, ArrayRef
<unsigned> Idxs
,
2435 const Twine
&NameStr
);
2438 // Note: Instruction needs to be a friend here to call cloneImpl.
2439 friend class Instruction
;
2441 InsertValueInst
*cloneImpl() const;
2444 // allocate space for exactly two operands
2445 void *operator new(size_t s
) {
2446 return User::operator new(s
, 2);
2449 static InsertValueInst
*Create(Value
*Agg
, Value
*Val
,
2450 ArrayRef
<unsigned> Idxs
,
2451 const Twine
&NameStr
= "",
2452 Instruction
*InsertBefore
= nullptr) {
2453 return new InsertValueInst(Agg
, Val
, Idxs
, NameStr
, InsertBefore
);
2456 static InsertValueInst
*Create(Value
*Agg
, Value
*Val
,
2457 ArrayRef
<unsigned> Idxs
,
2458 const Twine
&NameStr
,
2459 BasicBlock
*InsertAtEnd
) {
2460 return new InsertValueInst(Agg
, Val
, Idxs
, NameStr
, InsertAtEnd
);
2463 /// Transparently provide more efficient getOperand methods.
2464 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
2466 using idx_iterator
= const unsigned*;
2468 inline idx_iterator
idx_begin() const { return Indices
.begin(); }
2469 inline idx_iterator
idx_end() const { return Indices
.end(); }
2470 inline iterator_range
<idx_iterator
> indices() const {
2471 return make_range(idx_begin(), idx_end());
2474 Value
*getAggregateOperand() {
2475 return getOperand(0);
2477 const Value
*getAggregateOperand() const {
2478 return getOperand(0);
2480 static unsigned getAggregateOperandIndex() {
2481 return 0U; // get index for modifying correct operand
2484 Value
*getInsertedValueOperand() {
2485 return getOperand(1);
2487 const Value
*getInsertedValueOperand() const {
2488 return getOperand(1);
2490 static unsigned getInsertedValueOperandIndex() {
2491 return 1U; // get index for modifying correct operand
2494 ArrayRef
<unsigned> getIndices() const {
2498 unsigned getNumIndices() const {
2499 return (unsigned)Indices
.size();
2502 bool hasIndices() const {
2506 // Methods for support type inquiry through isa, cast, and dyn_cast:
2507 static bool classof(const Instruction
*I
) {
2508 return I
->getOpcode() == Instruction::InsertValue
;
2510 static bool classof(const Value
*V
) {
2511 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
2516 struct OperandTraits
<InsertValueInst
> :
2517 public FixedNumOperandTraits
<InsertValueInst
, 2> {
2520 InsertValueInst::InsertValueInst(Value
*Agg
,
2522 ArrayRef
<unsigned> Idxs
,
2523 const Twine
&NameStr
,
2524 Instruction
*InsertBefore
)
2525 : Instruction(Agg
->getType(), InsertValue
,
2526 OperandTraits
<InsertValueInst
>::op_begin(this),
2528 init(Agg
, Val
, Idxs
, NameStr
);
2531 InsertValueInst::InsertValueInst(Value
*Agg
,
2533 ArrayRef
<unsigned> Idxs
,
2534 const Twine
&NameStr
,
2535 BasicBlock
*InsertAtEnd
)
2536 : Instruction(Agg
->getType(), InsertValue
,
2537 OperandTraits
<InsertValueInst
>::op_begin(this),
2539 init(Agg
, Val
, Idxs
, NameStr
);
2542 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst
, Value
)
2544 //===----------------------------------------------------------------------===//
2546 //===----------------------------------------------------------------------===//
2548 // PHINode - The PHINode class is used to represent the magical mystical PHI
2549 // node, that can not exist in nature, but can be synthesized in a computer
2550 // scientist's overactive imagination.
2552 class PHINode
: public Instruction
{
2553 /// The number of operands actually allocated. NumOperands is
2554 /// the number actually in use.
2555 unsigned ReservedSpace
;
2557 PHINode(const PHINode
&PN
);
2559 explicit PHINode(Type
*Ty
, unsigned NumReservedValues
,
2560 const Twine
&NameStr
= "",
2561 Instruction
*InsertBefore
= nullptr)
2562 : Instruction(Ty
, Instruction::PHI
, nullptr, 0, InsertBefore
),
2563 ReservedSpace(NumReservedValues
) {
2565 allocHungoffUses(ReservedSpace
);
2568 PHINode(Type
*Ty
, unsigned NumReservedValues
, const Twine
&NameStr
,
2569 BasicBlock
*InsertAtEnd
)
2570 : Instruction(Ty
, Instruction::PHI
, nullptr, 0, InsertAtEnd
),
2571 ReservedSpace(NumReservedValues
) {
2573 allocHungoffUses(ReservedSpace
);
2577 // Note: Instruction needs to be a friend here to call cloneImpl.
2578 friend class Instruction
;
2580 PHINode
*cloneImpl() const;
2582 // allocHungoffUses - this is more complicated than the generic
2583 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2584 // values and pointers to the incoming blocks, all in one allocation.
2585 void allocHungoffUses(unsigned N
) {
2586 User::allocHungoffUses(N
, /* IsPhi */ true);
2590 /// Constructors - NumReservedValues is a hint for the number of incoming
2591 /// edges that this phi node will have (use 0 if you really have no idea).
2592 static PHINode
*Create(Type
*Ty
, unsigned NumReservedValues
,
2593 const Twine
&NameStr
= "",
2594 Instruction
*InsertBefore
= nullptr) {
2595 return new PHINode(Ty
, NumReservedValues
, NameStr
, InsertBefore
);
2598 static PHINode
*Create(Type
*Ty
, unsigned NumReservedValues
,
2599 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
2600 return new PHINode(Ty
, NumReservedValues
, NameStr
, InsertAtEnd
);
2603 /// Provide fast operand accessors
2604 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
2606 // Block iterator interface. This provides access to the list of incoming
2607 // basic blocks, which parallels the list of incoming values.
2609 using block_iterator
= BasicBlock
**;
2610 using const_block_iterator
= BasicBlock
* const *;
2612 block_iterator
block_begin() {
2614 reinterpret_cast<Use::UserRef
*>(op_begin() + ReservedSpace
);
2615 return reinterpret_cast<block_iterator
>(ref
+ 1);
2618 const_block_iterator
block_begin() const {
2619 const Use::UserRef
*ref
=
2620 reinterpret_cast<const Use::UserRef
*>(op_begin() + ReservedSpace
);
2621 return reinterpret_cast<const_block_iterator
>(ref
+ 1);
2624 block_iterator
block_end() {
2625 return block_begin() + getNumOperands();
2628 const_block_iterator
block_end() const {
2629 return block_begin() + getNumOperands();
2632 iterator_range
<block_iterator
> blocks() {
2633 return make_range(block_begin(), block_end());
2636 iterator_range
<const_block_iterator
> blocks() const {
2637 return make_range(block_begin(), block_end());
2640 op_range
incoming_values() { return operands(); }
2642 const_op_range
incoming_values() const { return operands(); }
2644 /// Return the number of incoming edges
2646 unsigned getNumIncomingValues() const { return getNumOperands(); }
2648 /// Return incoming value number x
2650 Value
*getIncomingValue(unsigned i
) const {
2651 return getOperand(i
);
2653 void setIncomingValue(unsigned i
, Value
*V
) {
2654 assert(V
&& "PHI node got a null value!");
2655 assert(getType() == V
->getType() &&
2656 "All operands to PHI node must be the same type as the PHI node!");
2660 static unsigned getOperandNumForIncomingValue(unsigned i
) {
2664 static unsigned getIncomingValueNumForOperand(unsigned i
) {
2668 /// Return incoming basic block number @p i.
2670 BasicBlock
*getIncomingBlock(unsigned i
) const {
2671 return block_begin()[i
];
2674 /// Return incoming basic block corresponding
2675 /// to an operand of the PHI.
2677 BasicBlock
*getIncomingBlock(const Use
&U
) const {
2678 assert(this == U
.getUser() && "Iterator doesn't point to PHI's Uses?");
2679 return getIncomingBlock(unsigned(&U
- op_begin()));
2682 /// Return incoming basic block corresponding
2683 /// to value use iterator.
2685 BasicBlock
*getIncomingBlock(Value::const_user_iterator I
) const {
2686 return getIncomingBlock(I
.getUse());
2689 void setIncomingBlock(unsigned i
, BasicBlock
*BB
) {
2690 assert(BB
&& "PHI node got a null basic block!");
2691 block_begin()[i
] = BB
;
2694 /// Replace every incoming basic block \p Old to basic block \p New.
2695 void replaceIncomingBlockWith(const BasicBlock
*Old
, BasicBlock
*New
) {
2696 assert(New
&& Old
&& "PHI node got a null basic block!");
2697 for (unsigned Op
= 0, NumOps
= getNumOperands(); Op
!= NumOps
; ++Op
)
2698 if (getIncomingBlock(Op
) == Old
)
2699 setIncomingBlock(Op
, New
);
2702 /// Add an incoming value to the end of the PHI list
2704 void addIncoming(Value
*V
, BasicBlock
*BB
) {
2705 if (getNumOperands() == ReservedSpace
)
2706 growOperands(); // Get more space!
2707 // Initialize some new operands.
2708 setNumHungOffUseOperands(getNumOperands() + 1);
2709 setIncomingValue(getNumOperands() - 1, V
);
2710 setIncomingBlock(getNumOperands() - 1, BB
);
2713 /// Remove an incoming value. This is useful if a
2714 /// predecessor basic block is deleted. The value removed is returned.
2716 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2717 /// is true), the PHI node is destroyed and any uses of it are replaced with
2718 /// dummy values. The only time there should be zero incoming values to a PHI
2719 /// node is when the block is dead, so this strategy is sound.
2721 Value
*removeIncomingValue(unsigned Idx
, bool DeletePHIIfEmpty
= true);
2723 Value
*removeIncomingValue(const BasicBlock
*BB
, bool DeletePHIIfEmpty
=true) {
2724 int Idx
= getBasicBlockIndex(BB
);
2725 assert(Idx
>= 0 && "Invalid basic block argument to remove!");
2726 return removeIncomingValue(Idx
, DeletePHIIfEmpty
);
2729 /// Return the first index of the specified basic
2730 /// block in the value list for this PHI. Returns -1 if no instance.
2732 int getBasicBlockIndex(const BasicBlock
*BB
) const {
2733 for (unsigned i
= 0, e
= getNumOperands(); i
!= e
; ++i
)
2734 if (block_begin()[i
] == BB
)
2739 Value
*getIncomingValueForBlock(const BasicBlock
*BB
) const {
2740 int Idx
= getBasicBlockIndex(BB
);
2741 assert(Idx
>= 0 && "Invalid basic block argument!");
2742 return getIncomingValue(Idx
);
2745 /// Set every incoming value(s) for block \p BB to \p V.
2746 void setIncomingValueForBlock(const BasicBlock
*BB
, Value
*V
) {
2747 assert(BB
&& "PHI node got a null basic block!");
2749 for (unsigned Op
= 0, NumOps
= getNumOperands(); Op
!= NumOps
; ++Op
)
2750 if (getIncomingBlock(Op
) == BB
) {
2752 setIncomingValue(Op
, V
);
2755 assert(Found
&& "Invalid basic block argument to set!");
2758 /// If the specified PHI node always merges together the
2759 /// same value, return the value, otherwise return null.
2760 Value
*hasConstantValue() const;
2762 /// Whether the specified PHI node always merges
2763 /// together the same value, assuming undefs are equal to a unique
2764 /// non-undef value.
2765 bool hasConstantOrUndefValue() const;
2767 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2768 static bool classof(const Instruction
*I
) {
2769 return I
->getOpcode() == Instruction::PHI
;
2771 static bool classof(const Value
*V
) {
2772 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
2776 void growOperands();
2780 struct OperandTraits
<PHINode
> : public HungoffOperandTraits
<2> {
2783 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode
, Value
)
2785 //===----------------------------------------------------------------------===//
2786 // LandingPadInst Class
2787 //===----------------------------------------------------------------------===//
2789 //===---------------------------------------------------------------------------
2790 /// The landingpad instruction holds all of the information
2791 /// necessary to generate correct exception handling. The landingpad instruction
2792 /// cannot be moved from the top of a landing pad block, which itself is
2793 /// accessible only from the 'unwind' edge of an invoke. This uses the
2794 /// SubclassData field in Value to store whether or not the landingpad is a
2797 class LandingPadInst
: public Instruction
{
2798 /// The number of operands actually allocated. NumOperands is
2799 /// the number actually in use.
2800 unsigned ReservedSpace
;
2802 LandingPadInst(const LandingPadInst
&LP
);
2805 enum ClauseType
{ Catch
, Filter
};
2808 explicit LandingPadInst(Type
*RetTy
, unsigned NumReservedValues
,
2809 const Twine
&NameStr
, Instruction
*InsertBefore
);
2810 explicit LandingPadInst(Type
*RetTy
, unsigned NumReservedValues
,
2811 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
);
2813 // Allocate space for exactly zero operands.
2814 void *operator new(size_t s
) {
2815 return User::operator new(s
);
2818 void growOperands(unsigned Size
);
2819 void init(unsigned NumReservedValues
, const Twine
&NameStr
);
2822 // Note: Instruction needs to be a friend here to call cloneImpl.
2823 friend class Instruction
;
2825 LandingPadInst
*cloneImpl() const;
2828 /// Constructors - NumReservedClauses is a hint for the number of incoming
2829 /// clauses that this landingpad will have (use 0 if you really have no idea).
2830 static LandingPadInst
*Create(Type
*RetTy
, unsigned NumReservedClauses
,
2831 const Twine
&NameStr
= "",
2832 Instruction
*InsertBefore
= nullptr);
2833 static LandingPadInst
*Create(Type
*RetTy
, unsigned NumReservedClauses
,
2834 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
);
2836 /// Provide fast operand accessors
2837 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
2839 /// Return 'true' if this landingpad instruction is a
2840 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2841 /// doesn't catch the exception.
2842 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2844 /// Indicate that this landingpad instruction is a cleanup.
2845 void setCleanup(bool V
) {
2846 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2850 /// Add a catch or filter clause to the landing pad.
2851 void addClause(Constant
*ClauseVal
);
2853 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2854 /// determine what type of clause this is.
2855 Constant
*getClause(unsigned Idx
) const {
2856 return cast
<Constant
>(getOperandList()[Idx
]);
2859 /// Return 'true' if the clause and index Idx is a catch clause.
2860 bool isCatch(unsigned Idx
) const {
2861 return !isa
<ArrayType
>(getOperandList()[Idx
]->getType());
2864 /// Return 'true' if the clause and index Idx is a filter clause.
2865 bool isFilter(unsigned Idx
) const {
2866 return isa
<ArrayType
>(getOperandList()[Idx
]->getType());
2869 /// Get the number of clauses for this landing pad.
2870 unsigned getNumClauses() const { return getNumOperands(); }
2872 /// Grow the size of the operand list to accommodate the new
2873 /// number of clauses.
2874 void reserveClauses(unsigned Size
) { growOperands(Size
); }
2876 // Methods for support type inquiry through isa, cast, and dyn_cast:
2877 static bool classof(const Instruction
*I
) {
2878 return I
->getOpcode() == Instruction::LandingPad
;
2880 static bool classof(const Value
*V
) {
2881 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
2886 struct OperandTraits
<LandingPadInst
> : public HungoffOperandTraits
<1> {
2889 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst
, Value
)
2891 //===----------------------------------------------------------------------===//
2893 //===----------------------------------------------------------------------===//
2895 //===---------------------------------------------------------------------------
2896 /// Return a value (possibly void), from a function. Execution
2897 /// does not continue in this function any longer.
2899 class ReturnInst
: public Instruction
{
2900 ReturnInst(const ReturnInst
&RI
);
2903 // ReturnInst constructors:
2904 // ReturnInst() - 'ret void' instruction
2905 // ReturnInst( null) - 'ret void' instruction
2906 // ReturnInst(Value* X) - 'ret X' instruction
2907 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2908 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2909 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2910 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2912 // NOTE: If the Value* passed is of type void then the constructor behaves as
2913 // if it was passed NULL.
2914 explicit ReturnInst(LLVMContext
&C
, Value
*retVal
= nullptr,
2915 Instruction
*InsertBefore
= nullptr);
2916 ReturnInst(LLVMContext
&C
, Value
*retVal
, BasicBlock
*InsertAtEnd
);
2917 explicit ReturnInst(LLVMContext
&C
, BasicBlock
*InsertAtEnd
);
2920 // Note: Instruction needs to be a friend here to call cloneImpl.
2921 friend class Instruction
;
2923 ReturnInst
*cloneImpl() const;
2926 static ReturnInst
* Create(LLVMContext
&C
, Value
*retVal
= nullptr,
2927 Instruction
*InsertBefore
= nullptr) {
2928 return new(!!retVal
) ReturnInst(C
, retVal
, InsertBefore
);
2931 static ReturnInst
* Create(LLVMContext
&C
, Value
*retVal
,
2932 BasicBlock
*InsertAtEnd
) {
2933 return new(!!retVal
) ReturnInst(C
, retVal
, InsertAtEnd
);
2936 static ReturnInst
* Create(LLVMContext
&C
, BasicBlock
*InsertAtEnd
) {
2937 return new(0) ReturnInst(C
, InsertAtEnd
);
2940 /// Provide fast operand accessors
2941 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
2943 /// Convenience accessor. Returns null if there is no return value.
2944 Value
*getReturnValue() const {
2945 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2948 unsigned getNumSuccessors() const { return 0; }
2950 // Methods for support type inquiry through isa, cast, and dyn_cast:
2951 static bool classof(const Instruction
*I
) {
2952 return (I
->getOpcode() == Instruction::Ret
);
2954 static bool classof(const Value
*V
) {
2955 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
2959 BasicBlock
*getSuccessor(unsigned idx
) const {
2960 llvm_unreachable("ReturnInst has no successors!");
2963 void setSuccessor(unsigned idx
, BasicBlock
*B
) {
2964 llvm_unreachable("ReturnInst has no successors!");
2969 struct OperandTraits
<ReturnInst
> : public VariadicOperandTraits
<ReturnInst
> {
2972 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst
, Value
)
2974 //===----------------------------------------------------------------------===//
2976 //===----------------------------------------------------------------------===//
2978 //===---------------------------------------------------------------------------
2979 /// Conditional or Unconditional Branch instruction.
2981 class BranchInst
: public Instruction
{
2982 /// Ops list - Branches are strange. The operands are ordered:
2983 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2984 /// they don't have to check for cond/uncond branchness. These are mostly
2985 /// accessed relative from op_end().
2986 BranchInst(const BranchInst
&BI
);
2987 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2988 // BranchInst(BB *B) - 'br B'
2989 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2990 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2991 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2992 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2993 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2994 explicit BranchInst(BasicBlock
*IfTrue
, Instruction
*InsertBefore
= nullptr);
2995 BranchInst(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
, Value
*Cond
,
2996 Instruction
*InsertBefore
= nullptr);
2997 BranchInst(BasicBlock
*IfTrue
, BasicBlock
*InsertAtEnd
);
2998 BranchInst(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
, Value
*Cond
,
2999 BasicBlock
*InsertAtEnd
);
3004 // Note: Instruction needs to be a friend here to call cloneImpl.
3005 friend class Instruction
;
3007 BranchInst
*cloneImpl() const;
3010 /// Iterator type that casts an operand to a basic block.
3012 /// This only makes sense because the successors are stored as adjacent
3013 /// operands for branch instructions.
3014 struct succ_op_iterator
3015 : iterator_adaptor_base
<succ_op_iterator
, value_op_iterator
,
3016 std::random_access_iterator_tag
, BasicBlock
*,
3017 ptrdiff_t, BasicBlock
*, BasicBlock
*> {
3018 explicit succ_op_iterator(value_op_iterator I
) : iterator_adaptor_base(I
) {}
3020 BasicBlock
*operator*() const { return cast
<BasicBlock
>(*I
); }
3021 BasicBlock
*operator->() const { return operator*(); }
3024 /// The const version of `succ_op_iterator`.
3025 struct const_succ_op_iterator
3026 : iterator_adaptor_base
<const_succ_op_iterator
, const_value_op_iterator
,
3027 std::random_access_iterator_tag
,
3028 const BasicBlock
*, ptrdiff_t, const BasicBlock
*,
3029 const BasicBlock
*> {
3030 explicit const_succ_op_iterator(const_value_op_iterator I
)
3031 : iterator_adaptor_base(I
) {}
3033 const BasicBlock
*operator*() const { return cast
<BasicBlock
>(*I
); }
3034 const BasicBlock
*operator->() const { return operator*(); }
3037 static BranchInst
*Create(BasicBlock
*IfTrue
,
3038 Instruction
*InsertBefore
= nullptr) {
3039 return new(1) BranchInst(IfTrue
, InsertBefore
);
3042 static BranchInst
*Create(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
,
3043 Value
*Cond
, Instruction
*InsertBefore
= nullptr) {
3044 return new(3) BranchInst(IfTrue
, IfFalse
, Cond
, InsertBefore
);
3047 static BranchInst
*Create(BasicBlock
*IfTrue
, BasicBlock
*InsertAtEnd
) {
3048 return new(1) BranchInst(IfTrue
, InsertAtEnd
);
3051 static BranchInst
*Create(BasicBlock
*IfTrue
, BasicBlock
*IfFalse
,
3052 Value
*Cond
, BasicBlock
*InsertAtEnd
) {
3053 return new(3) BranchInst(IfTrue
, IfFalse
, Cond
, InsertAtEnd
);
3056 /// Transparently provide more efficient getOperand methods.
3057 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
3059 bool isUnconditional() const { return getNumOperands() == 1; }
3060 bool isConditional() const { return getNumOperands() == 3; }
3062 Value
*getCondition() const {
3063 assert(isConditional() && "Cannot get condition of an uncond branch!");
3067 void setCondition(Value
*V
) {
3068 assert(isConditional() && "Cannot set condition of unconditional branch!");
3072 unsigned getNumSuccessors() const { return 1+isConditional(); }
3074 BasicBlock
*getSuccessor(unsigned i
) const {
3075 assert(i
< getNumSuccessors() && "Successor # out of range for Branch!");
3076 return cast_or_null
<BasicBlock
>((&Op
<-1>() - i
)->get());
3079 void setSuccessor(unsigned idx
, BasicBlock
*NewSucc
) {
3080 assert(idx
< getNumSuccessors() && "Successor # out of range for Branch!");
3081 *(&Op
<-1>() - idx
) = NewSucc
;
3084 /// Swap the successors of this branch instruction.
3086 /// Swaps the successors of the branch instruction. This also swaps any
3087 /// branch weight metadata associated with the instruction so that it
3088 /// continues to map correctly to each operand.
3089 void swapSuccessors();
3091 iterator_range
<succ_op_iterator
> successors() {
3093 succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)),
3094 succ_op_iterator(value_op_end()));
3097 iterator_range
<const_succ_op_iterator
> successors() const {
3098 return make_range(const_succ_op_iterator(
3099 std::next(value_op_begin(), isConditional() ? 1 : 0)),
3100 const_succ_op_iterator(value_op_end()));
3103 // Methods for support type inquiry through isa, cast, and dyn_cast:
3104 static bool classof(const Instruction
*I
) {
3105 return (I
->getOpcode() == Instruction::Br
);
3107 static bool classof(const Value
*V
) {
3108 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
3113 struct OperandTraits
<BranchInst
> : public VariadicOperandTraits
<BranchInst
, 1> {
3116 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst
, Value
)
3118 //===----------------------------------------------------------------------===//
3120 //===----------------------------------------------------------------------===//
3122 //===---------------------------------------------------------------------------
3125 class SwitchInst
: public Instruction
{
3126 unsigned ReservedSpace
;
3128 // Operand[0] = Value to switch on
3129 // Operand[1] = Default basic block destination
3130 // Operand[2n ] = Value to match
3131 // Operand[2n+1] = BasicBlock to go to on match
3132 SwitchInst(const SwitchInst
&SI
);
3134 /// Create a new switch instruction, specifying a value to switch on and a
3135 /// default destination. The number of additional cases can be specified here
3136 /// to make memory allocation more efficient. This constructor can also
3137 /// auto-insert before another instruction.
3138 SwitchInst(Value
*Value
, BasicBlock
*Default
, unsigned NumCases
,
3139 Instruction
*InsertBefore
);
3141 /// Create a new switch instruction, specifying a value to switch on and a
3142 /// default destination. The number of additional cases can be specified here
3143 /// to make memory allocation more efficient. This constructor also
3144 /// auto-inserts at the end of the specified BasicBlock.
3145 SwitchInst(Value
*Value
, BasicBlock
*Default
, unsigned NumCases
,
3146 BasicBlock
*InsertAtEnd
);
3148 // allocate space for exactly zero operands
3149 void *operator new(size_t s
) {
3150 return User::operator new(s
);
3153 void init(Value
*Value
, BasicBlock
*Default
, unsigned NumReserved
);
3154 void growOperands();
3157 // Note: Instruction needs to be a friend here to call cloneImpl.
3158 friend class Instruction
;
3160 SwitchInst
*cloneImpl() const;
3164 static const unsigned DefaultPseudoIndex
= static_cast<unsigned>(~0L-1);
3166 template <typename CaseHandleT
> class CaseIteratorImpl
;
3168 /// A handle to a particular switch case. It exposes a convenient interface
3169 /// to both the case value and the successor block.
3171 /// We define this as a template and instantiate it to form both a const and
3172 /// non-const handle.
3173 template <typename SwitchInstT
, typename ConstantIntT
, typename BasicBlockT
>
3174 class CaseHandleImpl
{
3175 // Directly befriend both const and non-const iterators.
3176 friend class SwitchInst::CaseIteratorImpl
<
3177 CaseHandleImpl
<SwitchInstT
, ConstantIntT
, BasicBlockT
>>;
3180 // Expose the switch type we're parameterized with to the iterator.
3181 using SwitchInstType
= SwitchInstT
;
3186 CaseHandleImpl() = default;
3187 CaseHandleImpl(SwitchInstT
*SI
, ptrdiff_t Index
) : SI(SI
), Index(Index
) {}
3190 /// Resolves case value for current case.
3191 ConstantIntT
*getCaseValue() const {
3192 assert((unsigned)Index
< SI
->getNumCases() &&
3193 "Index out the number of cases.");
3194 return reinterpret_cast<ConstantIntT
*>(SI
->getOperand(2 + Index
* 2));
3197 /// Resolves successor for current case.
3198 BasicBlockT
*getCaseSuccessor() const {
3199 assert(((unsigned)Index
< SI
->getNumCases() ||
3200 (unsigned)Index
== DefaultPseudoIndex
) &&
3201 "Index out the number of cases.");
3202 return SI
->getSuccessor(getSuccessorIndex());
3205 /// Returns number of current case.
3206 unsigned getCaseIndex() const { return Index
; }
3208 /// Returns successor index for current case successor.
3209 unsigned getSuccessorIndex() const {
3210 assert(((unsigned)Index
== DefaultPseudoIndex
||
3211 (unsigned)Index
< SI
->getNumCases()) &&
3212 "Index out the number of cases.");
3213 return (unsigned)Index
!= DefaultPseudoIndex
? Index
+ 1 : 0;
3216 bool operator==(const CaseHandleImpl
&RHS
) const {
3217 assert(SI
== RHS
.SI
&& "Incompatible operators.");
3218 return Index
== RHS
.Index
;
3222 using ConstCaseHandle
=
3223 CaseHandleImpl
<const SwitchInst
, const ConstantInt
, const BasicBlock
>;
3226 : public CaseHandleImpl
<SwitchInst
, ConstantInt
, BasicBlock
> {
3227 friend class SwitchInst::CaseIteratorImpl
<CaseHandle
>;
3230 CaseHandle(SwitchInst
*SI
, ptrdiff_t Index
) : CaseHandleImpl(SI
, Index
) {}
3232 /// Sets the new value for current case.
3233 void setValue(ConstantInt
*V
) {
3234 assert((unsigned)Index
< SI
->getNumCases() &&
3235 "Index out the number of cases.");
3236 SI
->setOperand(2 + Index
*2, reinterpret_cast<Value
*>(V
));
3239 /// Sets the new successor for current case.
3240 void setSuccessor(BasicBlock
*S
) {
3241 SI
->setSuccessor(getSuccessorIndex(), S
);
3245 template <typename CaseHandleT
>
3246 class CaseIteratorImpl
3247 : public iterator_facade_base
<CaseIteratorImpl
<CaseHandleT
>,
3248 std::random_access_iterator_tag
,
3250 using SwitchInstT
= typename
CaseHandleT::SwitchInstType
;
3255 /// Default constructed iterator is in an invalid state until assigned to
3256 /// a case for a particular switch.
3257 CaseIteratorImpl() = default;
3259 /// Initializes case iterator for given SwitchInst and for given
3261 CaseIteratorImpl(SwitchInstT
*SI
, unsigned CaseNum
) : Case(SI
, CaseNum
) {}
3263 /// Initializes case iterator for given SwitchInst and for given
3264 /// successor index.
3265 static CaseIteratorImpl
fromSuccessorIndex(SwitchInstT
*SI
,
3266 unsigned SuccessorIndex
) {
3267 assert(SuccessorIndex
< SI
->getNumSuccessors() &&
3268 "Successor index # out of range!");
3269 return SuccessorIndex
!= 0 ? CaseIteratorImpl(SI
, SuccessorIndex
- 1)
3270 : CaseIteratorImpl(SI
, DefaultPseudoIndex
);
3273 /// Support converting to the const variant. This will be a no-op for const
3275 operator CaseIteratorImpl
<ConstCaseHandle
>() const {
3276 return CaseIteratorImpl
<ConstCaseHandle
>(Case
.SI
, Case
.Index
);
3279 CaseIteratorImpl
&operator+=(ptrdiff_t N
) {
3280 // Check index correctness after addition.
3281 // Note: Index == getNumCases() means end().
3282 assert(Case
.Index
+ N
>= 0 &&
3283 (unsigned)(Case
.Index
+ N
) <= Case
.SI
->getNumCases() &&
3284 "Case.Index out the number of cases.");
3288 CaseIteratorImpl
&operator-=(ptrdiff_t N
) {
3289 // Check index correctness after subtraction.
3290 // Note: Case.Index == getNumCases() means end().
3291 assert(Case
.Index
- N
>= 0 &&
3292 (unsigned)(Case
.Index
- N
) <= Case
.SI
->getNumCases() &&
3293 "Case.Index out the number of cases.");
3297 ptrdiff_t operator-(const CaseIteratorImpl
&RHS
) const {
3298 assert(Case
.SI
== RHS
.Case
.SI
&& "Incompatible operators.");
3299 return Case
.Index
- RHS
.Case
.Index
;
3301 bool operator==(const CaseIteratorImpl
&RHS
) const {
3302 return Case
== RHS
.Case
;
3304 bool operator<(const CaseIteratorImpl
&RHS
) const {
3305 assert(Case
.SI
== RHS
.Case
.SI
&& "Incompatible operators.");
3306 return Case
.Index
< RHS
.Case
.Index
;
3308 CaseHandleT
&operator*() { return Case
; }
3309 const CaseHandleT
&operator*() const { return Case
; }
3312 using CaseIt
= CaseIteratorImpl
<CaseHandle
>;
3313 using ConstCaseIt
= CaseIteratorImpl
<ConstCaseHandle
>;
3315 static SwitchInst
*Create(Value
*Value
, BasicBlock
*Default
,
3317 Instruction
*InsertBefore
= nullptr) {
3318 return new SwitchInst(Value
, Default
, NumCases
, InsertBefore
);
3321 static SwitchInst
*Create(Value
*Value
, BasicBlock
*Default
,
3322 unsigned NumCases
, BasicBlock
*InsertAtEnd
) {
3323 return new SwitchInst(Value
, Default
, NumCases
, InsertAtEnd
);
3326 /// Provide fast operand accessors
3327 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
3329 // Accessor Methods for Switch stmt
3330 Value
*getCondition() const { return getOperand(0); }
3331 void setCondition(Value
*V
) { setOperand(0, V
); }
3333 BasicBlock
*getDefaultDest() const {
3334 return cast
<BasicBlock
>(getOperand(1));
3337 void setDefaultDest(BasicBlock
*DefaultCase
) {
3338 setOperand(1, reinterpret_cast<Value
*>(DefaultCase
));
3341 /// Return the number of 'cases' in this switch instruction, excluding the
3343 unsigned getNumCases() const {
3344 return getNumOperands()/2 - 1;
3347 /// Returns a read/write iterator that points to the first case in the
3349 CaseIt
case_begin() {
3350 return CaseIt(this, 0);
3353 /// Returns a read-only iterator that points to the first case in the
3355 ConstCaseIt
case_begin() const {
3356 return ConstCaseIt(this, 0);
3359 /// Returns a read/write iterator that points one past the last in the
3362 return CaseIt(this, getNumCases());
3365 /// Returns a read-only iterator that points one past the last in the
3367 ConstCaseIt
case_end() const {
3368 return ConstCaseIt(this, getNumCases());
3371 /// Iteration adapter for range-for loops.
3372 iterator_range
<CaseIt
> cases() {
3373 return make_range(case_begin(), case_end());
3376 /// Constant iteration adapter for range-for loops.
3377 iterator_range
<ConstCaseIt
> cases() const {
3378 return make_range(case_begin(), case_end());
3381 /// Returns an iterator that points to the default case.
3382 /// Note: this iterator allows to resolve successor only. Attempt
3383 /// to resolve case value causes an assertion.
3384 /// Also note, that increment and decrement also causes an assertion and
3385 /// makes iterator invalid.
3386 CaseIt
case_default() {
3387 return CaseIt(this, DefaultPseudoIndex
);
3389 ConstCaseIt
case_default() const {
3390 return ConstCaseIt(this, DefaultPseudoIndex
);
3393 /// Search all of the case values for the specified constant. If it is
3394 /// explicitly handled, return the case iterator of it, otherwise return
3395 /// default case iterator to indicate that it is handled by the default
3397 CaseIt
findCaseValue(const ConstantInt
*C
) {
3398 CaseIt I
= llvm::find_if(
3399 cases(), [C
](CaseHandle
&Case
) { return Case
.getCaseValue() == C
; });
3400 if (I
!= case_end())
3403 return case_default();
3405 ConstCaseIt
findCaseValue(const ConstantInt
*C
) const {
3406 ConstCaseIt I
= llvm::find_if(cases(), [C
](ConstCaseHandle
&Case
) {
3407 return Case
.getCaseValue() == C
;
3409 if (I
!= case_end())
3412 return case_default();
3415 /// Finds the unique case value for a given successor. Returns null if the
3416 /// successor is not found, not unique, or is the default case.
3417 ConstantInt
*findCaseDest(BasicBlock
*BB
) {
3418 if (BB
== getDefaultDest())
3421 ConstantInt
*CI
= nullptr;
3422 for (auto Case
: cases()) {
3423 if (Case
.getCaseSuccessor() != BB
)
3427 return nullptr; // Multiple cases lead to BB.
3429 CI
= Case
.getCaseValue();
3435 /// Add an entry to the switch instruction.
3437 /// This action invalidates case_end(). Old case_end() iterator will
3438 /// point to the added case.
3439 void addCase(ConstantInt
*OnVal
, BasicBlock
*Dest
);
3441 /// This method removes the specified case and its successor from the switch
3442 /// instruction. Note that this operation may reorder the remaining cases at
3443 /// index idx and above.
3445 /// This action invalidates iterators for all cases following the one removed,
3446 /// including the case_end() iterator. It returns an iterator for the next
3448 CaseIt
removeCase(CaseIt I
);
3450 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3451 BasicBlock
*getSuccessor(unsigned idx
) const {
3452 assert(idx
< getNumSuccessors() &&"Successor idx out of range for switch!");
3453 return cast
<BasicBlock
>(getOperand(idx
*2+1));
3455 void setSuccessor(unsigned idx
, BasicBlock
*NewSucc
) {
3456 assert(idx
< getNumSuccessors() && "Successor # out of range for switch!");
3457 setOperand(idx
* 2 + 1, NewSucc
);
3460 // Methods for support type inquiry through isa, cast, and dyn_cast:
3461 static bool classof(const Instruction
*I
) {
3462 return I
->getOpcode() == Instruction::Switch
;
3464 static bool classof(const Value
*V
) {
3465 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
3469 /// A wrapper class to simplify modification of SwitchInst cases along with
3470 /// their prof branch_weights metadata.
3471 class SwitchInstProfUpdateWrapper
{
3473 Optional
<SmallVector
<uint32_t, 8> > Weights
= None
;
3474 bool Changed
= false;
3477 static MDNode
*getProfBranchWeightsMD(const SwitchInst
&SI
);
3479 MDNode
*buildProfBranchWeightsMD();
3484 using CaseWeightOpt
= Optional
<uint32_t>;
3485 SwitchInst
*operator->() { return &SI
; }
3486 SwitchInst
&operator*() { return SI
; }
3487 operator SwitchInst
*() { return &SI
; }
3489 SwitchInstProfUpdateWrapper(SwitchInst
&SI
) : SI(SI
) { init(); }
3491 ~SwitchInstProfUpdateWrapper() {
3493 SI
.setMetadata(LLVMContext::MD_prof
, buildProfBranchWeightsMD());
3496 /// Delegate the call to the underlying SwitchInst::removeCase() and remove
3497 /// correspondent branch weight.
3498 SwitchInst::CaseIt
removeCase(SwitchInst::CaseIt I
);
3500 /// Delegate the call to the underlying SwitchInst::addCase() and set the
3501 /// specified branch weight for the added case.
3502 void addCase(ConstantInt
*OnVal
, BasicBlock
*Dest
, CaseWeightOpt W
);
3504 /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark
3505 /// this object to not touch the underlying SwitchInst in destructor.
3506 SymbolTableList
<Instruction
>::iterator
eraseFromParent();
3508 void setSuccessorWeight(unsigned idx
, CaseWeightOpt W
);
3509 CaseWeightOpt
getSuccessorWeight(unsigned idx
);
3511 static CaseWeightOpt
getSuccessorWeight(const SwitchInst
&SI
, unsigned idx
);
3515 struct OperandTraits
<SwitchInst
> : public HungoffOperandTraits
<2> {
3518 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst
, Value
)
3520 //===----------------------------------------------------------------------===//
3521 // IndirectBrInst Class
3522 //===----------------------------------------------------------------------===//
3524 //===---------------------------------------------------------------------------
3525 /// Indirect Branch Instruction.
3527 class IndirectBrInst
: public Instruction
{
3528 unsigned ReservedSpace
;
3530 // Operand[0] = Address to jump to
3531 // Operand[n+1] = n-th destination
3532 IndirectBrInst(const IndirectBrInst
&IBI
);
3534 /// Create a new indirectbr instruction, specifying an
3535 /// Address to jump to. The number of expected destinations can be specified
3536 /// here to make memory allocation more efficient. This constructor can also
3537 /// autoinsert before another instruction.
3538 IndirectBrInst(Value
*Address
, unsigned NumDests
, Instruction
*InsertBefore
);
3540 /// Create a new indirectbr instruction, specifying an
3541 /// Address to jump to. The number of expected destinations can be specified
3542 /// here to make memory allocation more efficient. This constructor also
3543 /// autoinserts at the end of the specified BasicBlock.
3544 IndirectBrInst(Value
*Address
, unsigned NumDests
, BasicBlock
*InsertAtEnd
);
3546 // allocate space for exactly zero operands
3547 void *operator new(size_t s
) {
3548 return User::operator new(s
);
3551 void init(Value
*Address
, unsigned NumDests
);
3552 void growOperands();
3555 // Note: Instruction needs to be a friend here to call cloneImpl.
3556 friend class Instruction
;
3558 IndirectBrInst
*cloneImpl() const;
3561 /// Iterator type that casts an operand to a basic block.
3563 /// This only makes sense because the successors are stored as adjacent
3564 /// operands for indirectbr instructions.
3565 struct succ_op_iterator
3566 : iterator_adaptor_base
<succ_op_iterator
, value_op_iterator
,
3567 std::random_access_iterator_tag
, BasicBlock
*,
3568 ptrdiff_t, BasicBlock
*, BasicBlock
*> {
3569 explicit succ_op_iterator(value_op_iterator I
) : iterator_adaptor_base(I
) {}
3571 BasicBlock
*operator*() const { return cast
<BasicBlock
>(*I
); }
3572 BasicBlock
*operator->() const { return operator*(); }
3575 /// The const version of `succ_op_iterator`.
3576 struct const_succ_op_iterator
3577 : iterator_adaptor_base
<const_succ_op_iterator
, const_value_op_iterator
,
3578 std::random_access_iterator_tag
,
3579 const BasicBlock
*, ptrdiff_t, const BasicBlock
*,
3580 const BasicBlock
*> {
3581 explicit const_succ_op_iterator(const_value_op_iterator I
)
3582 : iterator_adaptor_base(I
) {}
3584 const BasicBlock
*operator*() const { return cast
<BasicBlock
>(*I
); }
3585 const BasicBlock
*operator->() const { return operator*(); }
3588 static IndirectBrInst
*Create(Value
*Address
, unsigned NumDests
,
3589 Instruction
*InsertBefore
= nullptr) {
3590 return new IndirectBrInst(Address
, NumDests
, InsertBefore
);
3593 static IndirectBrInst
*Create(Value
*Address
, unsigned NumDests
,
3594 BasicBlock
*InsertAtEnd
) {
3595 return new IndirectBrInst(Address
, NumDests
, InsertAtEnd
);
3598 /// Provide fast operand accessors.
3599 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
3601 // Accessor Methods for IndirectBrInst instruction.
3602 Value
*getAddress() { return getOperand(0); }
3603 const Value
*getAddress() const { return getOperand(0); }
3604 void setAddress(Value
*V
) { setOperand(0, V
); }
3606 /// return the number of possible destinations in this
3607 /// indirectbr instruction.
3608 unsigned getNumDestinations() const { return getNumOperands()-1; }
3610 /// Return the specified destination.
3611 BasicBlock
*getDestination(unsigned i
) { return getSuccessor(i
); }
3612 const BasicBlock
*getDestination(unsigned i
) const { return getSuccessor(i
); }
3614 /// Add a destination.
3616 void addDestination(BasicBlock
*Dest
);
3618 /// This method removes the specified successor from the
3619 /// indirectbr instruction.
3620 void removeDestination(unsigned i
);
3622 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3623 BasicBlock
*getSuccessor(unsigned i
) const {
3624 return cast
<BasicBlock
>(getOperand(i
+1));
3626 void setSuccessor(unsigned i
, BasicBlock
*NewSucc
) {
3627 setOperand(i
+ 1, NewSucc
);
3630 iterator_range
<succ_op_iterator
> successors() {
3631 return make_range(succ_op_iterator(std::next(value_op_begin())),
3632 succ_op_iterator(value_op_end()));
3635 iterator_range
<const_succ_op_iterator
> successors() const {
3636 return make_range(const_succ_op_iterator(std::next(value_op_begin())),
3637 const_succ_op_iterator(value_op_end()));
3640 // Methods for support type inquiry through isa, cast, and dyn_cast:
3641 static bool classof(const Instruction
*I
) {
3642 return I
->getOpcode() == Instruction::IndirectBr
;
3644 static bool classof(const Value
*V
) {
3645 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
3650 struct OperandTraits
<IndirectBrInst
> : public HungoffOperandTraits
<1> {
3653 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst
, Value
)
3655 //===----------------------------------------------------------------------===//
3657 //===----------------------------------------------------------------------===//
3659 /// Invoke instruction. The SubclassData field is used to hold the
3660 /// calling convention of the call.
3662 class InvokeInst
: public CallBase
{
3663 /// The number of operands for this call beyond the called function,
3664 /// arguments, and operand bundles.
3665 static constexpr int NumExtraOperands
= 2;
3667 /// The index from the end of the operand array to the normal destination.
3668 static constexpr int NormalDestOpEndIdx
= -3;
3670 /// The index from the end of the operand array to the unwind destination.
3671 static constexpr int UnwindDestOpEndIdx
= -2;
3673 InvokeInst(const InvokeInst
&BI
);
3675 /// Construct an InvokeInst given a range of arguments.
3677 /// Construct an InvokeInst from a range of arguments
3678 inline InvokeInst(FunctionType
*Ty
, Value
*Func
, BasicBlock
*IfNormal
,
3679 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3680 ArrayRef
<OperandBundleDef
> Bundles
, int NumOperands
,
3681 const Twine
&NameStr
, Instruction
*InsertBefore
);
3683 inline InvokeInst(FunctionType
*Ty
, Value
*Func
, BasicBlock
*IfNormal
,
3684 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3685 ArrayRef
<OperandBundleDef
> Bundles
, int NumOperands
,
3686 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
);
3688 void init(FunctionType
*Ty
, Value
*Func
, BasicBlock
*IfNormal
,
3689 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3690 ArrayRef
<OperandBundleDef
> Bundles
, const Twine
&NameStr
);
3692 /// Compute the number of operands to allocate.
3693 static int ComputeNumOperands(int NumArgs
, int NumBundleInputs
= 0) {
3694 // We need one operand for the called function, plus our extra operands and
3695 // the input operand counts provided.
3696 return 1 + NumExtraOperands
+ NumArgs
+ NumBundleInputs
;
3700 // Note: Instruction needs to be a friend here to call cloneImpl.
3701 friend class Instruction
;
3703 InvokeInst
*cloneImpl() const;
3706 static InvokeInst
*Create(FunctionType
*Ty
, Value
*Func
, BasicBlock
*IfNormal
,
3707 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3708 const Twine
&NameStr
,
3709 Instruction
*InsertBefore
= nullptr) {
3710 int NumOperands
= ComputeNumOperands(Args
.size());
3711 return new (NumOperands
)
3712 InvokeInst(Ty
, Func
, IfNormal
, IfException
, Args
, None
, NumOperands
,
3713 NameStr
, InsertBefore
);
3716 static InvokeInst
*Create(FunctionType
*Ty
, Value
*Func
, BasicBlock
*IfNormal
,
3717 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3718 ArrayRef
<OperandBundleDef
> Bundles
= None
,
3719 const Twine
&NameStr
= "",
3720 Instruction
*InsertBefore
= nullptr) {
3722 ComputeNumOperands(Args
.size(), CountBundleInputs(Bundles
));
3723 unsigned DescriptorBytes
= Bundles
.size() * sizeof(BundleOpInfo
);
3725 return new (NumOperands
, DescriptorBytes
)
3726 InvokeInst(Ty
, Func
, IfNormal
, IfException
, Args
, Bundles
, NumOperands
,
3727 NameStr
, InsertBefore
);
3730 static InvokeInst
*Create(FunctionType
*Ty
, Value
*Func
, BasicBlock
*IfNormal
,
3731 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3732 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
3733 int NumOperands
= ComputeNumOperands(Args
.size());
3734 return new (NumOperands
)
3735 InvokeInst(Ty
, Func
, IfNormal
, IfException
, Args
, None
, NumOperands
,
3736 NameStr
, InsertAtEnd
);
3739 static InvokeInst
*Create(FunctionType
*Ty
, Value
*Func
, BasicBlock
*IfNormal
,
3740 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3741 ArrayRef
<OperandBundleDef
> Bundles
,
3742 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
3744 ComputeNumOperands(Args
.size(), CountBundleInputs(Bundles
));
3745 unsigned DescriptorBytes
= Bundles
.size() * sizeof(BundleOpInfo
);
3747 return new (NumOperands
, DescriptorBytes
)
3748 InvokeInst(Ty
, Func
, IfNormal
, IfException
, Args
, Bundles
, NumOperands
,
3749 NameStr
, InsertAtEnd
);
3752 static InvokeInst
*Create(FunctionCallee Func
, BasicBlock
*IfNormal
,
3753 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3754 const Twine
&NameStr
,
3755 Instruction
*InsertBefore
= nullptr) {
3756 return Create(Func
.getFunctionType(), Func
.getCallee(), IfNormal
,
3757 IfException
, Args
, None
, NameStr
, InsertBefore
);
3760 static InvokeInst
*Create(FunctionCallee Func
, BasicBlock
*IfNormal
,
3761 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3762 ArrayRef
<OperandBundleDef
> Bundles
= None
,
3763 const Twine
&NameStr
= "",
3764 Instruction
*InsertBefore
= nullptr) {
3765 return Create(Func
.getFunctionType(), Func
.getCallee(), IfNormal
,
3766 IfException
, Args
, Bundles
, NameStr
, InsertBefore
);
3769 static InvokeInst
*Create(FunctionCallee Func
, BasicBlock
*IfNormal
,
3770 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3771 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
3772 return Create(Func
.getFunctionType(), Func
.getCallee(), IfNormal
,
3773 IfException
, Args
, NameStr
, InsertAtEnd
);
3776 static InvokeInst
*Create(FunctionCallee Func
, BasicBlock
*IfNormal
,
3777 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3778 ArrayRef
<OperandBundleDef
> Bundles
,
3779 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
3780 return Create(Func
.getFunctionType(), Func
.getCallee(), IfNormal
,
3781 IfException
, Args
, Bundles
, NameStr
, InsertAtEnd
);
3784 // Deprecated [opaque pointer types]
3785 static InvokeInst
*Create(Value
*Func
, BasicBlock
*IfNormal
,
3786 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3787 const Twine
&NameStr
,
3788 Instruction
*InsertBefore
= nullptr) {
3789 return Create(cast
<FunctionType
>(
3790 cast
<PointerType
>(Func
->getType())->getElementType()),
3791 Func
, IfNormal
, IfException
, Args
, None
, NameStr
,
3795 // Deprecated [opaque pointer types]
3796 static InvokeInst
*Create(Value
*Func
, BasicBlock
*IfNormal
,
3797 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3798 ArrayRef
<OperandBundleDef
> Bundles
= None
,
3799 const Twine
&NameStr
= "",
3800 Instruction
*InsertBefore
= nullptr) {
3801 return Create(cast
<FunctionType
>(
3802 cast
<PointerType
>(Func
->getType())->getElementType()),
3803 Func
, IfNormal
, IfException
, Args
, Bundles
, NameStr
,
3807 // Deprecated [opaque pointer types]
3808 static InvokeInst
*Create(Value
*Func
, BasicBlock
*IfNormal
,
3809 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3810 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
3811 return Create(cast
<FunctionType
>(
3812 cast
<PointerType
>(Func
->getType())->getElementType()),
3813 Func
, IfNormal
, IfException
, Args
, NameStr
, InsertAtEnd
);
3816 // Deprecated [opaque pointer types]
3817 static InvokeInst
*Create(Value
*Func
, BasicBlock
*IfNormal
,
3818 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3819 ArrayRef
<OperandBundleDef
> Bundles
,
3820 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
3821 return Create(cast
<FunctionType
>(
3822 cast
<PointerType
>(Func
->getType())->getElementType()),
3823 Func
, IfNormal
, IfException
, Args
, Bundles
, NameStr
,
3827 /// Create a clone of \p II with a different set of operand bundles and
3828 /// insert it before \p InsertPt.
3830 /// The returned invoke instruction is identical to \p II in every way except
3831 /// that the operand bundles for the new instruction are set to the operand
3832 /// bundles in \p Bundles.
3833 static InvokeInst
*Create(InvokeInst
*II
, ArrayRef
<OperandBundleDef
> Bundles
,
3834 Instruction
*InsertPt
= nullptr);
3836 /// Determine if the call should not perform indirect branch tracking.
3837 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck
); }
3839 /// Determine if the call cannot unwind.
3840 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind
); }
3841 void setDoesNotThrow() {
3842 addAttribute(AttributeList::FunctionIndex
, Attribute::NoUnwind
);
3845 // get*Dest - Return the destination basic blocks...
3846 BasicBlock
*getNormalDest() const {
3847 return cast
<BasicBlock
>(Op
<NormalDestOpEndIdx
>());
3849 BasicBlock
*getUnwindDest() const {
3850 return cast
<BasicBlock
>(Op
<UnwindDestOpEndIdx
>());
3852 void setNormalDest(BasicBlock
*B
) {
3853 Op
<NormalDestOpEndIdx
>() = reinterpret_cast<Value
*>(B
);
3855 void setUnwindDest(BasicBlock
*B
) {
3856 Op
<UnwindDestOpEndIdx
>() = reinterpret_cast<Value
*>(B
);
3859 /// Get the landingpad instruction from the landing pad
3860 /// block (the unwind destination).
3861 LandingPadInst
*getLandingPadInst() const;
3863 BasicBlock
*getSuccessor(unsigned i
) const {
3864 assert(i
< 2 && "Successor # out of range for invoke!");
3865 return i
== 0 ? getNormalDest() : getUnwindDest();
3868 void setSuccessor(unsigned i
, BasicBlock
*NewSucc
) {
3869 assert(i
< 2 && "Successor # out of range for invoke!");
3871 setNormalDest(NewSucc
);
3873 setUnwindDest(NewSucc
);
3876 unsigned getNumSuccessors() const { return 2; }
3878 // Methods for support type inquiry through isa, cast, and dyn_cast:
3879 static bool classof(const Instruction
*I
) {
3880 return (I
->getOpcode() == Instruction::Invoke
);
3882 static bool classof(const Value
*V
) {
3883 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
3888 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3889 // method so that subclasses cannot accidentally use it.
3890 void setInstructionSubclassData(unsigned short D
) {
3891 Instruction::setInstructionSubclassData(D
);
3895 InvokeInst::InvokeInst(FunctionType
*Ty
, Value
*Func
, BasicBlock
*IfNormal
,
3896 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3897 ArrayRef
<OperandBundleDef
> Bundles
, int NumOperands
,
3898 const Twine
&NameStr
, Instruction
*InsertBefore
)
3899 : CallBase(Ty
->getReturnType(), Instruction::Invoke
,
3900 OperandTraits
<CallBase
>::op_end(this) - NumOperands
, NumOperands
,
3902 init(Ty
, Func
, IfNormal
, IfException
, Args
, Bundles
, NameStr
);
3905 InvokeInst::InvokeInst(FunctionType
*Ty
, Value
*Func
, BasicBlock
*IfNormal
,
3906 BasicBlock
*IfException
, ArrayRef
<Value
*> Args
,
3907 ArrayRef
<OperandBundleDef
> Bundles
, int NumOperands
,
3908 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
)
3909 : CallBase(Ty
->getReturnType(), Instruction::Invoke
,
3910 OperandTraits
<CallBase
>::op_end(this) - NumOperands
, NumOperands
,
3912 init(Ty
, Func
, IfNormal
, IfException
, Args
, Bundles
, NameStr
);
3915 //===----------------------------------------------------------------------===//
3917 //===----------------------------------------------------------------------===//
3919 /// CallBr instruction, tracking function calls that may not return control but
3920 /// instead transfer it to a third location. The SubclassData field is used to
3921 /// hold the calling convention of the call.
3923 class CallBrInst
: public CallBase
{
3925 unsigned NumIndirectDests
;
3927 CallBrInst(const CallBrInst
&BI
);
3929 /// Construct a CallBrInst given a range of arguments.
3931 /// Construct a CallBrInst from a range of arguments
3932 inline CallBrInst(FunctionType
*Ty
, Value
*Func
, BasicBlock
*DefaultDest
,
3933 ArrayRef
<BasicBlock
*> IndirectDests
,
3934 ArrayRef
<Value
*> Args
,
3935 ArrayRef
<OperandBundleDef
> Bundles
, int NumOperands
,
3936 const Twine
&NameStr
, Instruction
*InsertBefore
);
3938 inline CallBrInst(FunctionType
*Ty
, Value
*Func
, BasicBlock
*DefaultDest
,
3939 ArrayRef
<BasicBlock
*> IndirectDests
,
3940 ArrayRef
<Value
*> Args
,
3941 ArrayRef
<OperandBundleDef
> Bundles
, int NumOperands
,
3942 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
);
3944 void init(FunctionType
*FTy
, Value
*Func
, BasicBlock
*DefaultDest
,
3945 ArrayRef
<BasicBlock
*> IndirectDests
, ArrayRef
<Value
*> Args
,
3946 ArrayRef
<OperandBundleDef
> Bundles
, const Twine
&NameStr
);
3948 /// Should the Indirect Destinations change, scan + update the Arg list.
3949 void updateArgBlockAddresses(unsigned i
, BasicBlock
*B
);
3951 /// Compute the number of operands to allocate.
3952 static int ComputeNumOperands(int NumArgs
, int NumIndirectDests
,
3953 int NumBundleInputs
= 0) {
3954 // We need one operand for the called function, plus our extra operands and
3955 // the input operand counts provided.
3956 return 2 + NumIndirectDests
+ NumArgs
+ NumBundleInputs
;
3960 // Note: Instruction needs to be a friend here to call cloneImpl.
3961 friend class Instruction
;
3963 CallBrInst
*cloneImpl() const;
3966 static CallBrInst
*Create(FunctionType
*Ty
, Value
*Func
,
3967 BasicBlock
*DefaultDest
,
3968 ArrayRef
<BasicBlock
*> IndirectDests
,
3969 ArrayRef
<Value
*> Args
, const Twine
&NameStr
,
3970 Instruction
*InsertBefore
= nullptr) {
3971 int NumOperands
= ComputeNumOperands(Args
.size(), IndirectDests
.size());
3972 return new (NumOperands
)
3973 CallBrInst(Ty
, Func
, DefaultDest
, IndirectDests
, Args
, None
,
3974 NumOperands
, NameStr
, InsertBefore
);
3977 static CallBrInst
*Create(FunctionType
*Ty
, Value
*Func
,
3978 BasicBlock
*DefaultDest
,
3979 ArrayRef
<BasicBlock
*> IndirectDests
,
3980 ArrayRef
<Value
*> Args
,
3981 ArrayRef
<OperandBundleDef
> Bundles
= None
,
3982 const Twine
&NameStr
= "",
3983 Instruction
*InsertBefore
= nullptr) {
3984 int NumOperands
= ComputeNumOperands(Args
.size(), IndirectDests
.size(),
3985 CountBundleInputs(Bundles
));
3986 unsigned DescriptorBytes
= Bundles
.size() * sizeof(BundleOpInfo
);
3988 return new (NumOperands
, DescriptorBytes
)
3989 CallBrInst(Ty
, Func
, DefaultDest
, IndirectDests
, Args
, Bundles
,
3990 NumOperands
, NameStr
, InsertBefore
);
3993 static CallBrInst
*Create(FunctionType
*Ty
, Value
*Func
,
3994 BasicBlock
*DefaultDest
,
3995 ArrayRef
<BasicBlock
*> IndirectDests
,
3996 ArrayRef
<Value
*> Args
, const Twine
&NameStr
,
3997 BasicBlock
*InsertAtEnd
) {
3998 int NumOperands
= ComputeNumOperands(Args
.size(), IndirectDests
.size());
3999 return new (NumOperands
)
4000 CallBrInst(Ty
, Func
, DefaultDest
, IndirectDests
, Args
, None
,
4001 NumOperands
, NameStr
, InsertAtEnd
);
4004 static CallBrInst
*Create(FunctionType
*Ty
, Value
*Func
,
4005 BasicBlock
*DefaultDest
,
4006 ArrayRef
<BasicBlock
*> IndirectDests
,
4007 ArrayRef
<Value
*> Args
,
4008 ArrayRef
<OperandBundleDef
> Bundles
,
4009 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
4010 int NumOperands
= ComputeNumOperands(Args
.size(), IndirectDests
.size(),
4011 CountBundleInputs(Bundles
));
4012 unsigned DescriptorBytes
= Bundles
.size() * sizeof(BundleOpInfo
);
4014 return new (NumOperands
, DescriptorBytes
)
4015 CallBrInst(Ty
, Func
, DefaultDest
, IndirectDests
, Args
, Bundles
,
4016 NumOperands
, NameStr
, InsertAtEnd
);
4019 static CallBrInst
*Create(FunctionCallee Func
, BasicBlock
*DefaultDest
,
4020 ArrayRef
<BasicBlock
*> IndirectDests
,
4021 ArrayRef
<Value
*> Args
, const Twine
&NameStr
,
4022 Instruction
*InsertBefore
= nullptr) {
4023 return Create(Func
.getFunctionType(), Func
.getCallee(), DefaultDest
,
4024 IndirectDests
, Args
, NameStr
, InsertBefore
);
4027 static CallBrInst
*Create(FunctionCallee Func
, BasicBlock
*DefaultDest
,
4028 ArrayRef
<BasicBlock
*> IndirectDests
,
4029 ArrayRef
<Value
*> Args
,
4030 ArrayRef
<OperandBundleDef
> Bundles
= None
,
4031 const Twine
&NameStr
= "",
4032 Instruction
*InsertBefore
= nullptr) {
4033 return Create(Func
.getFunctionType(), Func
.getCallee(), DefaultDest
,
4034 IndirectDests
, Args
, Bundles
, NameStr
, InsertBefore
);
4037 static CallBrInst
*Create(FunctionCallee Func
, BasicBlock
*DefaultDest
,
4038 ArrayRef
<BasicBlock
*> IndirectDests
,
4039 ArrayRef
<Value
*> Args
, const Twine
&NameStr
,
4040 BasicBlock
*InsertAtEnd
) {
4041 return Create(Func
.getFunctionType(), Func
.getCallee(), DefaultDest
,
4042 IndirectDests
, Args
, NameStr
, InsertAtEnd
);
4045 static CallBrInst
*Create(FunctionCallee Func
,
4046 BasicBlock
*DefaultDest
,
4047 ArrayRef
<BasicBlock
*> IndirectDests
,
4048 ArrayRef
<Value
*> Args
,
4049 ArrayRef
<OperandBundleDef
> Bundles
,
4050 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
4051 return Create(Func
.getFunctionType(), Func
.getCallee(), DefaultDest
,
4052 IndirectDests
, Args
, Bundles
, NameStr
, InsertAtEnd
);
4055 /// Create a clone of \p CBI with a different set of operand bundles and
4056 /// insert it before \p InsertPt.
4058 /// The returned callbr instruction is identical to \p CBI in every way
4059 /// except that the operand bundles for the new instruction are set to the
4060 /// operand bundles in \p Bundles.
4061 static CallBrInst
*Create(CallBrInst
*CBI
,
4062 ArrayRef
<OperandBundleDef
> Bundles
,
4063 Instruction
*InsertPt
= nullptr);
4065 /// Return the number of callbr indirect dest labels.
4067 unsigned getNumIndirectDests() const { return NumIndirectDests
; }
4069 /// getIndirectDestLabel - Return the i-th indirect dest label.
4071 Value
*getIndirectDestLabel(unsigned i
) const {
4072 assert(i
< getNumIndirectDests() && "Out of bounds!");
4073 return getOperand(i
+ getNumArgOperands() + getNumTotalBundleOperands() +
4077 Value
*getIndirectDestLabelUse(unsigned i
) const {
4078 assert(i
< getNumIndirectDests() && "Out of bounds!");
4079 return getOperandUse(i
+ getNumArgOperands() + getNumTotalBundleOperands() +
4083 // Return the destination basic blocks...
4084 BasicBlock
*getDefaultDest() const {
4085 return cast
<BasicBlock
>(*(&Op
<-1>() - getNumIndirectDests() - 1));
4087 BasicBlock
*getIndirectDest(unsigned i
) const {
4088 return cast_or_null
<BasicBlock
>(*(&Op
<-1>() - getNumIndirectDests() + i
));
4090 SmallVector
<BasicBlock
*, 16> getIndirectDests() const {
4091 SmallVector
<BasicBlock
*, 16> IndirectDests
;
4092 for (unsigned i
= 0, e
= getNumIndirectDests(); i
< e
; ++i
)
4093 IndirectDests
.push_back(getIndirectDest(i
));
4094 return IndirectDests
;
4096 void setDefaultDest(BasicBlock
*B
) {
4097 *(&Op
<-1>() - getNumIndirectDests() - 1) = reinterpret_cast<Value
*>(B
);
4099 void setIndirectDest(unsigned i
, BasicBlock
*B
) {
4100 updateArgBlockAddresses(i
, B
);
4101 *(&Op
<-1>() - getNumIndirectDests() + i
) = reinterpret_cast<Value
*>(B
);
4104 BasicBlock
*getSuccessor(unsigned i
) const {
4105 assert(i
< getNumSuccessors() + 1 &&
4106 "Successor # out of range for callbr!");
4107 return i
== 0 ? getDefaultDest() : getIndirectDest(i
- 1);
4110 void setSuccessor(unsigned i
, BasicBlock
*NewSucc
) {
4111 assert(i
< getNumIndirectDests() + 1 &&
4112 "Successor # out of range for callbr!");
4113 return i
== 0 ? setDefaultDest(NewSucc
) : setIndirectDest(i
- 1, NewSucc
);
4116 unsigned getNumSuccessors() const { return getNumIndirectDests() + 1; }
4118 // Methods for support type inquiry through isa, cast, and dyn_cast:
4119 static bool classof(const Instruction
*I
) {
4120 return (I
->getOpcode() == Instruction::CallBr
);
4122 static bool classof(const Value
*V
) {
4123 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4128 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4129 // method so that subclasses cannot accidentally use it.
4130 void setInstructionSubclassData(unsigned short D
) {
4131 Instruction::setInstructionSubclassData(D
);
4135 CallBrInst::CallBrInst(FunctionType
*Ty
, Value
*Func
, BasicBlock
*DefaultDest
,
4136 ArrayRef
<BasicBlock
*> IndirectDests
,
4137 ArrayRef
<Value
*> Args
,
4138 ArrayRef
<OperandBundleDef
> Bundles
, int NumOperands
,
4139 const Twine
&NameStr
, Instruction
*InsertBefore
)
4140 : CallBase(Ty
->getReturnType(), Instruction::CallBr
,
4141 OperandTraits
<CallBase
>::op_end(this) - NumOperands
, NumOperands
,
4143 init(Ty
, Func
, DefaultDest
, IndirectDests
, Args
, Bundles
, NameStr
);
4146 CallBrInst::CallBrInst(FunctionType
*Ty
, Value
*Func
, BasicBlock
*DefaultDest
,
4147 ArrayRef
<BasicBlock
*> IndirectDests
,
4148 ArrayRef
<Value
*> Args
,
4149 ArrayRef
<OperandBundleDef
> Bundles
, int NumOperands
,
4150 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
)
4153 cast
<PointerType
>(Func
->getType())->getElementType())
4155 Instruction::CallBr
,
4156 OperandTraits
<CallBase
>::op_end(this) - NumOperands
, NumOperands
,
4158 init(Ty
, Func
, DefaultDest
, IndirectDests
, Args
, Bundles
, NameStr
);
4161 //===----------------------------------------------------------------------===//
4163 //===----------------------------------------------------------------------===//
4165 //===---------------------------------------------------------------------------
4166 /// Resume the propagation of an exception.
4168 class ResumeInst
: public Instruction
{
4169 ResumeInst(const ResumeInst
&RI
);
4171 explicit ResumeInst(Value
*Exn
, Instruction
*InsertBefore
=nullptr);
4172 ResumeInst(Value
*Exn
, BasicBlock
*InsertAtEnd
);
4175 // Note: Instruction needs to be a friend here to call cloneImpl.
4176 friend class Instruction
;
4178 ResumeInst
*cloneImpl() const;
4181 static ResumeInst
*Create(Value
*Exn
, Instruction
*InsertBefore
= nullptr) {
4182 return new(1) ResumeInst(Exn
, InsertBefore
);
4185 static ResumeInst
*Create(Value
*Exn
, BasicBlock
*InsertAtEnd
) {
4186 return new(1) ResumeInst(Exn
, InsertAtEnd
);
4189 /// Provide fast operand accessors
4190 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
4192 /// Convenience accessor.
4193 Value
*getValue() const { return Op
<0>(); }
4195 unsigned getNumSuccessors() const { return 0; }
4197 // Methods for support type inquiry through isa, cast, and dyn_cast:
4198 static bool classof(const Instruction
*I
) {
4199 return I
->getOpcode() == Instruction::Resume
;
4201 static bool classof(const Value
*V
) {
4202 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4206 BasicBlock
*getSuccessor(unsigned idx
) const {
4207 llvm_unreachable("ResumeInst has no successors!");
4210 void setSuccessor(unsigned idx
, BasicBlock
*NewSucc
) {
4211 llvm_unreachable("ResumeInst has no successors!");
4216 struct OperandTraits
<ResumeInst
> :
4217 public FixedNumOperandTraits
<ResumeInst
, 1> {
4220 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst
, Value
)
4222 //===----------------------------------------------------------------------===//
4223 // CatchSwitchInst Class
4224 //===----------------------------------------------------------------------===//
4225 class CatchSwitchInst
: public Instruction
{
4226 /// The number of operands actually allocated. NumOperands is
4227 /// the number actually in use.
4228 unsigned ReservedSpace
;
4230 // Operand[0] = Outer scope
4231 // Operand[1] = Unwind block destination
4232 // Operand[n] = BasicBlock to go to on match
4233 CatchSwitchInst(const CatchSwitchInst
&CSI
);
4235 /// Create a new switch instruction, specifying a
4236 /// default destination. The number of additional handlers can be specified
4237 /// here to make memory allocation more efficient.
4238 /// This constructor can also autoinsert before another instruction.
4239 CatchSwitchInst(Value
*ParentPad
, BasicBlock
*UnwindDest
,
4240 unsigned NumHandlers
, const Twine
&NameStr
,
4241 Instruction
*InsertBefore
);
4243 /// Create a new switch instruction, specifying a
4244 /// default destination. The number of additional handlers can be specified
4245 /// here to make memory allocation more efficient.
4246 /// This constructor also autoinserts at the end of the specified BasicBlock.
4247 CatchSwitchInst(Value
*ParentPad
, BasicBlock
*UnwindDest
,
4248 unsigned NumHandlers
, const Twine
&NameStr
,
4249 BasicBlock
*InsertAtEnd
);
4251 // allocate space for exactly zero operands
4252 void *operator new(size_t s
) { return User::operator new(s
); }
4254 void init(Value
*ParentPad
, BasicBlock
*UnwindDest
, unsigned NumReserved
);
4255 void growOperands(unsigned Size
);
4258 // Note: Instruction needs to be a friend here to call cloneImpl.
4259 friend class Instruction
;
4261 CatchSwitchInst
*cloneImpl() const;
4264 static CatchSwitchInst
*Create(Value
*ParentPad
, BasicBlock
*UnwindDest
,
4265 unsigned NumHandlers
,
4266 const Twine
&NameStr
= "",
4267 Instruction
*InsertBefore
= nullptr) {
4268 return new CatchSwitchInst(ParentPad
, UnwindDest
, NumHandlers
, NameStr
,
4272 static CatchSwitchInst
*Create(Value
*ParentPad
, BasicBlock
*UnwindDest
,
4273 unsigned NumHandlers
, const Twine
&NameStr
,
4274 BasicBlock
*InsertAtEnd
) {
4275 return new CatchSwitchInst(ParentPad
, UnwindDest
, NumHandlers
, NameStr
,
4279 /// Provide fast operand accessors
4280 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
4282 // Accessor Methods for CatchSwitch stmt
4283 Value
*getParentPad() const { return getOperand(0); }
4284 void setParentPad(Value
*ParentPad
) { setOperand(0, ParentPad
); }
4286 // Accessor Methods for CatchSwitch stmt
4287 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4288 bool unwindsToCaller() const { return !hasUnwindDest(); }
4289 BasicBlock
*getUnwindDest() const {
4290 if (hasUnwindDest())
4291 return cast
<BasicBlock
>(getOperand(1));
4294 void setUnwindDest(BasicBlock
*UnwindDest
) {
4296 assert(hasUnwindDest());
4297 setOperand(1, UnwindDest
);
4300 /// return the number of 'handlers' in this catchswitch
4301 /// instruction, except the default handler
4302 unsigned getNumHandlers() const {
4303 if (hasUnwindDest())
4304 return getNumOperands() - 2;
4305 return getNumOperands() - 1;
4309 static BasicBlock
*handler_helper(Value
*V
) { return cast
<BasicBlock
>(V
); }
4310 static const BasicBlock
*handler_helper(const Value
*V
) {
4311 return cast
<BasicBlock
>(V
);
4315 using DerefFnTy
= BasicBlock
*(*)(Value
*);
4316 using handler_iterator
= mapped_iterator
<op_iterator
, DerefFnTy
>;
4317 using handler_range
= iterator_range
<handler_iterator
>;
4318 using ConstDerefFnTy
= const BasicBlock
*(*)(const Value
*);
4319 using const_handler_iterator
=
4320 mapped_iterator
<const_op_iterator
, ConstDerefFnTy
>;
4321 using const_handler_range
= iterator_range
<const_handler_iterator
>;
4323 /// Returns an iterator that points to the first handler in CatchSwitchInst.
4324 handler_iterator
handler_begin() {
4325 op_iterator It
= op_begin() + 1;
4326 if (hasUnwindDest())
4328 return handler_iterator(It
, DerefFnTy(handler_helper
));
4331 /// Returns an iterator that points to the first handler in the
4332 /// CatchSwitchInst.
4333 const_handler_iterator
handler_begin() const {
4334 const_op_iterator It
= op_begin() + 1;
4335 if (hasUnwindDest())
4337 return const_handler_iterator(It
, ConstDerefFnTy(handler_helper
));
4340 /// Returns a read-only iterator that points one past the last
4341 /// handler in the CatchSwitchInst.
4342 handler_iterator
handler_end() {
4343 return handler_iterator(op_end(), DerefFnTy(handler_helper
));
4346 /// Returns an iterator that points one past the last handler in the
4347 /// CatchSwitchInst.
4348 const_handler_iterator
handler_end() const {
4349 return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper
));
4352 /// iteration adapter for range-for loops.
4353 handler_range
handlers() {
4354 return make_range(handler_begin(), handler_end());
4357 /// iteration adapter for range-for loops.
4358 const_handler_range
handlers() const {
4359 return make_range(handler_begin(), handler_end());
4362 /// Add an entry to the switch instruction...
4364 /// This action invalidates handler_end(). Old handler_end() iterator will
4365 /// point to the added handler.
4366 void addHandler(BasicBlock
*Dest
);
4368 void removeHandler(handler_iterator HI
);
4370 unsigned getNumSuccessors() const { return getNumOperands() - 1; }
4371 BasicBlock
*getSuccessor(unsigned Idx
) const {
4372 assert(Idx
< getNumSuccessors() &&
4373 "Successor # out of range for catchswitch!");
4374 return cast
<BasicBlock
>(getOperand(Idx
+ 1));
4376 void setSuccessor(unsigned Idx
, BasicBlock
*NewSucc
) {
4377 assert(Idx
< getNumSuccessors() &&
4378 "Successor # out of range for catchswitch!");
4379 setOperand(Idx
+ 1, NewSucc
);
4382 // Methods for support type inquiry through isa, cast, and dyn_cast:
4383 static bool classof(const Instruction
*I
) {
4384 return I
->getOpcode() == Instruction::CatchSwitch
;
4386 static bool classof(const Value
*V
) {
4387 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4392 struct OperandTraits
<CatchSwitchInst
> : public HungoffOperandTraits
<2> {};
4394 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst
, Value
)
4396 //===----------------------------------------------------------------------===//
4397 // CleanupPadInst Class
4398 //===----------------------------------------------------------------------===//
4399 class CleanupPadInst
: public FuncletPadInst
{
4401 explicit CleanupPadInst(Value
*ParentPad
, ArrayRef
<Value
*> Args
,
4402 unsigned Values
, const Twine
&NameStr
,
4403 Instruction
*InsertBefore
)
4404 : FuncletPadInst(Instruction::CleanupPad
, ParentPad
, Args
, Values
,
4405 NameStr
, InsertBefore
) {}
4406 explicit CleanupPadInst(Value
*ParentPad
, ArrayRef
<Value
*> Args
,
4407 unsigned Values
, const Twine
&NameStr
,
4408 BasicBlock
*InsertAtEnd
)
4409 : FuncletPadInst(Instruction::CleanupPad
, ParentPad
, Args
, Values
,
4410 NameStr
, InsertAtEnd
) {}
4413 static CleanupPadInst
*Create(Value
*ParentPad
, ArrayRef
<Value
*> Args
= None
,
4414 const Twine
&NameStr
= "",
4415 Instruction
*InsertBefore
= nullptr) {
4416 unsigned Values
= 1 + Args
.size();
4418 CleanupPadInst(ParentPad
, Args
, Values
, NameStr
, InsertBefore
);
4421 static CleanupPadInst
*Create(Value
*ParentPad
, ArrayRef
<Value
*> Args
,
4422 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
4423 unsigned Values
= 1 + Args
.size();
4425 CleanupPadInst(ParentPad
, Args
, Values
, NameStr
, InsertAtEnd
);
4428 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4429 static bool classof(const Instruction
*I
) {
4430 return I
->getOpcode() == Instruction::CleanupPad
;
4432 static bool classof(const Value
*V
) {
4433 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4437 //===----------------------------------------------------------------------===//
4438 // CatchPadInst Class
4439 //===----------------------------------------------------------------------===//
4440 class CatchPadInst
: public FuncletPadInst
{
4442 explicit CatchPadInst(Value
*CatchSwitch
, ArrayRef
<Value
*> Args
,
4443 unsigned Values
, const Twine
&NameStr
,
4444 Instruction
*InsertBefore
)
4445 : FuncletPadInst(Instruction::CatchPad
, CatchSwitch
, Args
, Values
,
4446 NameStr
, InsertBefore
) {}
4447 explicit CatchPadInst(Value
*CatchSwitch
, ArrayRef
<Value
*> Args
,
4448 unsigned Values
, const Twine
&NameStr
,
4449 BasicBlock
*InsertAtEnd
)
4450 : FuncletPadInst(Instruction::CatchPad
, CatchSwitch
, Args
, Values
,
4451 NameStr
, InsertAtEnd
) {}
4454 static CatchPadInst
*Create(Value
*CatchSwitch
, ArrayRef
<Value
*> Args
,
4455 const Twine
&NameStr
= "",
4456 Instruction
*InsertBefore
= nullptr) {
4457 unsigned Values
= 1 + Args
.size();
4459 CatchPadInst(CatchSwitch
, Args
, Values
, NameStr
, InsertBefore
);
4462 static CatchPadInst
*Create(Value
*CatchSwitch
, ArrayRef
<Value
*> Args
,
4463 const Twine
&NameStr
, BasicBlock
*InsertAtEnd
) {
4464 unsigned Values
= 1 + Args
.size();
4466 CatchPadInst(CatchSwitch
, Args
, Values
, NameStr
, InsertAtEnd
);
4469 /// Convenience accessors
4470 CatchSwitchInst
*getCatchSwitch() const {
4471 return cast
<CatchSwitchInst
>(Op
<-1>());
4473 void setCatchSwitch(Value
*CatchSwitch
) {
4474 assert(CatchSwitch
);
4475 Op
<-1>() = CatchSwitch
;
4478 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4479 static bool classof(const Instruction
*I
) {
4480 return I
->getOpcode() == Instruction::CatchPad
;
4482 static bool classof(const Value
*V
) {
4483 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4487 //===----------------------------------------------------------------------===//
4488 // CatchReturnInst Class
4489 //===----------------------------------------------------------------------===//
4491 class CatchReturnInst
: public Instruction
{
4492 CatchReturnInst(const CatchReturnInst
&RI
);
4493 CatchReturnInst(Value
*CatchPad
, BasicBlock
*BB
, Instruction
*InsertBefore
);
4494 CatchReturnInst(Value
*CatchPad
, BasicBlock
*BB
, BasicBlock
*InsertAtEnd
);
4496 void init(Value
*CatchPad
, BasicBlock
*BB
);
4499 // Note: Instruction needs to be a friend here to call cloneImpl.
4500 friend class Instruction
;
4502 CatchReturnInst
*cloneImpl() const;
4505 static CatchReturnInst
*Create(Value
*CatchPad
, BasicBlock
*BB
,
4506 Instruction
*InsertBefore
= nullptr) {
4509 return new (2) CatchReturnInst(CatchPad
, BB
, InsertBefore
);
4512 static CatchReturnInst
*Create(Value
*CatchPad
, BasicBlock
*BB
,
4513 BasicBlock
*InsertAtEnd
) {
4516 return new (2) CatchReturnInst(CatchPad
, BB
, InsertAtEnd
);
4519 /// Provide fast operand accessors
4520 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
4522 /// Convenience accessors.
4523 CatchPadInst
*getCatchPad() const { return cast
<CatchPadInst
>(Op
<0>()); }
4524 void setCatchPad(CatchPadInst
*CatchPad
) {
4529 BasicBlock
*getSuccessor() const { return cast
<BasicBlock
>(Op
<1>()); }
4530 void setSuccessor(BasicBlock
*NewSucc
) {
4534 unsigned getNumSuccessors() const { return 1; }
4536 /// Get the parentPad of this catchret's catchpad's catchswitch.
4537 /// The successor block is implicitly a member of this funclet.
4538 Value
*getCatchSwitchParentPad() const {
4539 return getCatchPad()->getCatchSwitch()->getParentPad();
4542 // Methods for support type inquiry through isa, cast, and dyn_cast:
4543 static bool classof(const Instruction
*I
) {
4544 return (I
->getOpcode() == Instruction::CatchRet
);
4546 static bool classof(const Value
*V
) {
4547 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4551 BasicBlock
*getSuccessor(unsigned Idx
) const {
4552 assert(Idx
< getNumSuccessors() && "Successor # out of range for catchret!");
4553 return getSuccessor();
4556 void setSuccessor(unsigned Idx
, BasicBlock
*B
) {
4557 assert(Idx
< getNumSuccessors() && "Successor # out of range for catchret!");
4563 struct OperandTraits
<CatchReturnInst
>
4564 : public FixedNumOperandTraits
<CatchReturnInst
, 2> {};
4566 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst
, Value
)
4568 //===----------------------------------------------------------------------===//
4569 // CleanupReturnInst Class
4570 //===----------------------------------------------------------------------===//
4572 class CleanupReturnInst
: public Instruction
{
4574 CleanupReturnInst(const CleanupReturnInst
&RI
);
4575 CleanupReturnInst(Value
*CleanupPad
, BasicBlock
*UnwindBB
, unsigned Values
,
4576 Instruction
*InsertBefore
= nullptr);
4577 CleanupReturnInst(Value
*CleanupPad
, BasicBlock
*UnwindBB
, unsigned Values
,
4578 BasicBlock
*InsertAtEnd
);
4580 void init(Value
*CleanupPad
, BasicBlock
*UnwindBB
);
4583 // Note: Instruction needs to be a friend here to call cloneImpl.
4584 friend class Instruction
;
4586 CleanupReturnInst
*cloneImpl() const;
4589 static CleanupReturnInst
*Create(Value
*CleanupPad
,
4590 BasicBlock
*UnwindBB
= nullptr,
4591 Instruction
*InsertBefore
= nullptr) {
4593 unsigned Values
= 1;
4597 CleanupReturnInst(CleanupPad
, UnwindBB
, Values
, InsertBefore
);
4600 static CleanupReturnInst
*Create(Value
*CleanupPad
, BasicBlock
*UnwindBB
,
4601 BasicBlock
*InsertAtEnd
) {
4603 unsigned Values
= 1;
4607 CleanupReturnInst(CleanupPad
, UnwindBB
, Values
, InsertAtEnd
);
4610 /// Provide fast operand accessors
4611 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
4613 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4614 bool unwindsToCaller() const { return !hasUnwindDest(); }
4616 /// Convenience accessor.
4617 CleanupPadInst
*getCleanupPad() const {
4618 return cast
<CleanupPadInst
>(Op
<0>());
4620 void setCleanupPad(CleanupPadInst
*CleanupPad
) {
4622 Op
<0>() = CleanupPad
;
4625 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4627 BasicBlock
*getUnwindDest() const {
4628 return hasUnwindDest() ? cast
<BasicBlock
>(Op
<1>()) : nullptr;
4630 void setUnwindDest(BasicBlock
*NewDest
) {
4632 assert(hasUnwindDest());
4636 // Methods for support type inquiry through isa, cast, and dyn_cast:
4637 static bool classof(const Instruction
*I
) {
4638 return (I
->getOpcode() == Instruction::CleanupRet
);
4640 static bool classof(const Value
*V
) {
4641 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4645 BasicBlock
*getSuccessor(unsigned Idx
) const {
4647 return getUnwindDest();
4650 void setSuccessor(unsigned Idx
, BasicBlock
*B
) {
4655 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4656 // method so that subclasses cannot accidentally use it.
4657 void setInstructionSubclassData(unsigned short D
) {
4658 Instruction::setInstructionSubclassData(D
);
4663 struct OperandTraits
<CleanupReturnInst
>
4664 : public VariadicOperandTraits
<CleanupReturnInst
, /*MINARITY=*/1> {};
4666 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst
, Value
)
4668 //===----------------------------------------------------------------------===//
4669 // UnreachableInst Class
4670 //===----------------------------------------------------------------------===//
4672 //===---------------------------------------------------------------------------
4673 /// This function has undefined behavior. In particular, the
4674 /// presence of this instruction indicates some higher level knowledge that the
4675 /// end of the block cannot be reached.
4677 class UnreachableInst
: public Instruction
{
4679 // Note: Instruction needs to be a friend here to call cloneImpl.
4680 friend class Instruction
;
4682 UnreachableInst
*cloneImpl() const;
4685 explicit UnreachableInst(LLVMContext
&C
, Instruction
*InsertBefore
= nullptr);
4686 explicit UnreachableInst(LLVMContext
&C
, BasicBlock
*InsertAtEnd
);
4688 // allocate space for exactly zero operands
4689 void *operator new(size_t s
) {
4690 return User::operator new(s
, 0);
4693 unsigned getNumSuccessors() const { return 0; }
4695 // Methods for support type inquiry through isa, cast, and dyn_cast:
4696 static bool classof(const Instruction
*I
) {
4697 return I
->getOpcode() == Instruction::Unreachable
;
4699 static bool classof(const Value
*V
) {
4700 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4704 BasicBlock
*getSuccessor(unsigned idx
) const {
4705 llvm_unreachable("UnreachableInst has no successors!");
4708 void setSuccessor(unsigned idx
, BasicBlock
*B
) {
4709 llvm_unreachable("UnreachableInst has no successors!");
4713 //===----------------------------------------------------------------------===//
4715 //===----------------------------------------------------------------------===//
4717 /// This class represents a truncation of integer types.
4718 class TruncInst
: public CastInst
{
4720 // Note: Instruction needs to be a friend here to call cloneImpl.
4721 friend class Instruction
;
4723 /// Clone an identical TruncInst
4724 TruncInst
*cloneImpl() const;
4727 /// Constructor with insert-before-instruction semantics
4729 Value
*S
, ///< The value to be truncated
4730 Type
*Ty
, ///< The (smaller) type to truncate to
4731 const Twine
&NameStr
= "", ///< A name for the new instruction
4732 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
4735 /// Constructor with insert-at-end-of-block semantics
4737 Value
*S
, ///< The value to be truncated
4738 Type
*Ty
, ///< The (smaller) type to truncate to
4739 const Twine
&NameStr
, ///< A name for the new instruction
4740 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
4743 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4744 static bool classof(const Instruction
*I
) {
4745 return I
->getOpcode() == Trunc
;
4747 static bool classof(const Value
*V
) {
4748 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4752 //===----------------------------------------------------------------------===//
4754 //===----------------------------------------------------------------------===//
4756 /// This class represents zero extension of integer types.
4757 class ZExtInst
: public CastInst
{
4759 // Note: Instruction needs to be a friend here to call cloneImpl.
4760 friend class Instruction
;
4762 /// Clone an identical ZExtInst
4763 ZExtInst
*cloneImpl() const;
4766 /// Constructor with insert-before-instruction semantics
4768 Value
*S
, ///< The value to be zero extended
4769 Type
*Ty
, ///< The type to zero extend to
4770 const Twine
&NameStr
= "", ///< A name for the new instruction
4771 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
4774 /// Constructor with insert-at-end semantics.
4776 Value
*S
, ///< The value to be zero extended
4777 Type
*Ty
, ///< The type to zero extend to
4778 const Twine
&NameStr
, ///< A name for the new instruction
4779 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
4782 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4783 static bool classof(const Instruction
*I
) {
4784 return I
->getOpcode() == ZExt
;
4786 static bool classof(const Value
*V
) {
4787 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4791 //===----------------------------------------------------------------------===//
4793 //===----------------------------------------------------------------------===//
4795 /// This class represents a sign extension of integer types.
4796 class SExtInst
: public CastInst
{
4798 // Note: Instruction needs to be a friend here to call cloneImpl.
4799 friend class Instruction
;
4801 /// Clone an identical SExtInst
4802 SExtInst
*cloneImpl() const;
4805 /// Constructor with insert-before-instruction semantics
4807 Value
*S
, ///< The value to be sign extended
4808 Type
*Ty
, ///< The type to sign extend to
4809 const Twine
&NameStr
= "", ///< A name for the new instruction
4810 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
4813 /// Constructor with insert-at-end-of-block semantics
4815 Value
*S
, ///< The value to be sign extended
4816 Type
*Ty
, ///< The type to sign extend to
4817 const Twine
&NameStr
, ///< A name for the new instruction
4818 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
4821 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4822 static bool classof(const Instruction
*I
) {
4823 return I
->getOpcode() == SExt
;
4825 static bool classof(const Value
*V
) {
4826 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4830 //===----------------------------------------------------------------------===//
4831 // FPTruncInst Class
4832 //===----------------------------------------------------------------------===//
4834 /// This class represents a truncation of floating point types.
4835 class FPTruncInst
: public CastInst
{
4837 // Note: Instruction needs to be a friend here to call cloneImpl.
4838 friend class Instruction
;
4840 /// Clone an identical FPTruncInst
4841 FPTruncInst
*cloneImpl() const;
4844 /// Constructor with insert-before-instruction semantics
4846 Value
*S
, ///< The value to be truncated
4847 Type
*Ty
, ///< The type to truncate to
4848 const Twine
&NameStr
= "", ///< A name for the new instruction
4849 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
4852 /// Constructor with insert-before-instruction semantics
4854 Value
*S
, ///< The value to be truncated
4855 Type
*Ty
, ///< The type to truncate to
4856 const Twine
&NameStr
, ///< A name for the new instruction
4857 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
4860 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4861 static bool classof(const Instruction
*I
) {
4862 return I
->getOpcode() == FPTrunc
;
4864 static bool classof(const Value
*V
) {
4865 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4869 //===----------------------------------------------------------------------===//
4871 //===----------------------------------------------------------------------===//
4873 /// This class represents an extension of floating point types.
4874 class FPExtInst
: public CastInst
{
4876 // Note: Instruction needs to be a friend here to call cloneImpl.
4877 friend class Instruction
;
4879 /// Clone an identical FPExtInst
4880 FPExtInst
*cloneImpl() const;
4883 /// Constructor with insert-before-instruction semantics
4885 Value
*S
, ///< The value to be extended
4886 Type
*Ty
, ///< The type to extend to
4887 const Twine
&NameStr
= "", ///< A name for the new instruction
4888 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
4891 /// Constructor with insert-at-end-of-block semantics
4893 Value
*S
, ///< The value to be extended
4894 Type
*Ty
, ///< The type to extend to
4895 const Twine
&NameStr
, ///< A name for the new instruction
4896 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
4899 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4900 static bool classof(const Instruction
*I
) {
4901 return I
->getOpcode() == FPExt
;
4903 static bool classof(const Value
*V
) {
4904 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4908 //===----------------------------------------------------------------------===//
4910 //===----------------------------------------------------------------------===//
4912 /// This class represents a cast unsigned integer to floating point.
4913 class UIToFPInst
: public CastInst
{
4915 // Note: Instruction needs to be a friend here to call cloneImpl.
4916 friend class Instruction
;
4918 /// Clone an identical UIToFPInst
4919 UIToFPInst
*cloneImpl() const;
4922 /// Constructor with insert-before-instruction semantics
4924 Value
*S
, ///< The value to be converted
4925 Type
*Ty
, ///< The type to convert to
4926 const Twine
&NameStr
= "", ///< A name for the new instruction
4927 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
4930 /// Constructor with insert-at-end-of-block semantics
4932 Value
*S
, ///< The value to be converted
4933 Type
*Ty
, ///< The type to convert to
4934 const Twine
&NameStr
, ///< A name for the new instruction
4935 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
4938 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4939 static bool classof(const Instruction
*I
) {
4940 return I
->getOpcode() == UIToFP
;
4942 static bool classof(const Value
*V
) {
4943 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4947 //===----------------------------------------------------------------------===//
4949 //===----------------------------------------------------------------------===//
4951 /// This class represents a cast from signed integer to floating point.
4952 class SIToFPInst
: public CastInst
{
4954 // Note: Instruction needs to be a friend here to call cloneImpl.
4955 friend class Instruction
;
4957 /// Clone an identical SIToFPInst
4958 SIToFPInst
*cloneImpl() const;
4961 /// Constructor with insert-before-instruction semantics
4963 Value
*S
, ///< The value to be converted
4964 Type
*Ty
, ///< The type to convert to
4965 const Twine
&NameStr
= "", ///< A name for the new instruction
4966 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
4969 /// Constructor with insert-at-end-of-block semantics
4971 Value
*S
, ///< The value to be converted
4972 Type
*Ty
, ///< The type to convert to
4973 const Twine
&NameStr
, ///< A name for the new instruction
4974 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
4977 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4978 static bool classof(const Instruction
*I
) {
4979 return I
->getOpcode() == SIToFP
;
4981 static bool classof(const Value
*V
) {
4982 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
4986 //===----------------------------------------------------------------------===//
4988 //===----------------------------------------------------------------------===//
4990 /// This class represents a cast from floating point to unsigned integer
4991 class FPToUIInst
: public CastInst
{
4993 // Note: Instruction needs to be a friend here to call cloneImpl.
4994 friend class Instruction
;
4996 /// Clone an identical FPToUIInst
4997 FPToUIInst
*cloneImpl() const;
5000 /// Constructor with insert-before-instruction semantics
5002 Value
*S
, ///< The value to be converted
5003 Type
*Ty
, ///< The type to convert to
5004 const Twine
&NameStr
= "", ///< A name for the new instruction
5005 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
5008 /// Constructor with insert-at-end-of-block semantics
5010 Value
*S
, ///< The value to be converted
5011 Type
*Ty
, ///< The type to convert to
5012 const Twine
&NameStr
, ///< A name for the new instruction
5013 BasicBlock
*InsertAtEnd
///< Where to insert the new instruction
5016 /// Methods for support type inquiry through isa, cast, and dyn_cast:
5017 static bool classof(const Instruction
*I
) {
5018 return I
->getOpcode() == FPToUI
;
5020 static bool classof(const Value
*V
) {
5021 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
5025 //===----------------------------------------------------------------------===//
5027 //===----------------------------------------------------------------------===//
5029 /// This class represents a cast from floating point to signed integer.
5030 class FPToSIInst
: public CastInst
{
5032 // Note: Instruction needs to be a friend here to call cloneImpl.
5033 friend class Instruction
;
5035 /// Clone an identical FPToSIInst
5036 FPToSIInst
*cloneImpl() const;
5039 /// Constructor with insert-before-instruction semantics
5041 Value
*S
, ///< The value to be converted
5042 Type
*Ty
, ///< The type to convert to
5043 const Twine
&NameStr
= "", ///< A name for the new instruction
5044 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
5047 /// Constructor with insert-at-end-of-block semantics
5049 Value
*S
, ///< The value to be converted
5050 Type
*Ty
, ///< The type to convert to
5051 const Twine
&NameStr
, ///< A name for the new instruction
5052 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
5055 /// Methods for support type inquiry through isa, cast, and dyn_cast:
5056 static bool classof(const Instruction
*I
) {
5057 return I
->getOpcode() == FPToSI
;
5059 static bool classof(const Value
*V
) {
5060 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
5064 //===----------------------------------------------------------------------===//
5065 // IntToPtrInst Class
5066 //===----------------------------------------------------------------------===//
5068 /// This class represents a cast from an integer to a pointer.
5069 class IntToPtrInst
: public CastInst
{
5071 // Note: Instruction needs to be a friend here to call cloneImpl.
5072 friend class Instruction
;
5074 /// Constructor with insert-before-instruction semantics
5076 Value
*S
, ///< The value to be converted
5077 Type
*Ty
, ///< The type to convert to
5078 const Twine
&NameStr
= "", ///< A name for the new instruction
5079 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
5082 /// Constructor with insert-at-end-of-block semantics
5084 Value
*S
, ///< The value to be converted
5085 Type
*Ty
, ///< The type to convert to
5086 const Twine
&NameStr
, ///< A name for the new instruction
5087 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
5090 /// Clone an identical IntToPtrInst.
5091 IntToPtrInst
*cloneImpl() const;
5093 /// Returns the address space of this instruction's pointer type.
5094 unsigned getAddressSpace() const {
5095 return getType()->getPointerAddressSpace();
5098 // Methods for support type inquiry through isa, cast, and dyn_cast:
5099 static bool classof(const Instruction
*I
) {
5100 return I
->getOpcode() == IntToPtr
;
5102 static bool classof(const Value
*V
) {
5103 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
5107 //===----------------------------------------------------------------------===//
5108 // PtrToIntInst Class
5109 //===----------------------------------------------------------------------===//
5111 /// This class represents a cast from a pointer to an integer.
5112 class PtrToIntInst
: public CastInst
{
5114 // Note: Instruction needs to be a friend here to call cloneImpl.
5115 friend class Instruction
;
5117 /// Clone an identical PtrToIntInst.
5118 PtrToIntInst
*cloneImpl() const;
5121 /// Constructor with insert-before-instruction semantics
5123 Value
*S
, ///< The value to be converted
5124 Type
*Ty
, ///< The type to convert to
5125 const Twine
&NameStr
= "", ///< A name for the new instruction
5126 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
5129 /// Constructor with insert-at-end-of-block semantics
5131 Value
*S
, ///< The value to be converted
5132 Type
*Ty
, ///< The type to convert to
5133 const Twine
&NameStr
, ///< A name for the new instruction
5134 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
5137 /// Gets the pointer operand.
5138 Value
*getPointerOperand() { return getOperand(0); }
5139 /// Gets the pointer operand.
5140 const Value
*getPointerOperand() const { return getOperand(0); }
5141 /// Gets the operand index of the pointer operand.
5142 static unsigned getPointerOperandIndex() { return 0U; }
5144 /// Returns the address space of the pointer operand.
5145 unsigned getPointerAddressSpace() const {
5146 return getPointerOperand()->getType()->getPointerAddressSpace();
5149 // Methods for support type inquiry through isa, cast, and dyn_cast:
5150 static bool classof(const Instruction
*I
) {
5151 return I
->getOpcode() == PtrToInt
;
5153 static bool classof(const Value
*V
) {
5154 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
5158 //===----------------------------------------------------------------------===//
5159 // BitCastInst Class
5160 //===----------------------------------------------------------------------===//
5162 /// This class represents a no-op cast from one type to another.
5163 class BitCastInst
: public CastInst
{
5165 // Note: Instruction needs to be a friend here to call cloneImpl.
5166 friend class Instruction
;
5168 /// Clone an identical BitCastInst.
5169 BitCastInst
*cloneImpl() const;
5172 /// Constructor with insert-before-instruction semantics
5174 Value
*S
, ///< The value to be casted
5175 Type
*Ty
, ///< The type to casted to
5176 const Twine
&NameStr
= "", ///< A name for the new instruction
5177 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
5180 /// Constructor with insert-at-end-of-block semantics
5182 Value
*S
, ///< The value to be casted
5183 Type
*Ty
, ///< The type to casted to
5184 const Twine
&NameStr
, ///< A name for the new instruction
5185 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
5188 // Methods for support type inquiry through isa, cast, and dyn_cast:
5189 static bool classof(const Instruction
*I
) {
5190 return I
->getOpcode() == BitCast
;
5192 static bool classof(const Value
*V
) {
5193 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
5197 //===----------------------------------------------------------------------===//
5198 // AddrSpaceCastInst Class
5199 //===----------------------------------------------------------------------===//
5201 /// This class represents a conversion between pointers from one address space
5203 class AddrSpaceCastInst
: public CastInst
{
5205 // Note: Instruction needs to be a friend here to call cloneImpl.
5206 friend class Instruction
;
5208 /// Clone an identical AddrSpaceCastInst.
5209 AddrSpaceCastInst
*cloneImpl() const;
5212 /// Constructor with insert-before-instruction semantics
5214 Value
*S
, ///< The value to be casted
5215 Type
*Ty
, ///< The type to casted to
5216 const Twine
&NameStr
= "", ///< A name for the new instruction
5217 Instruction
*InsertBefore
= nullptr ///< Where to insert the new instruction
5220 /// Constructor with insert-at-end-of-block semantics
5222 Value
*S
, ///< The value to be casted
5223 Type
*Ty
, ///< The type to casted to
5224 const Twine
&NameStr
, ///< A name for the new instruction
5225 BasicBlock
*InsertAtEnd
///< The block to insert the instruction into
5228 // Methods for support type inquiry through isa, cast, and dyn_cast:
5229 static bool classof(const Instruction
*I
) {
5230 return I
->getOpcode() == AddrSpaceCast
;
5232 static bool classof(const Value
*V
) {
5233 return isa
<Instruction
>(V
) && classof(cast
<Instruction
>(V
));
5236 /// Gets the pointer operand.
5237 Value
*getPointerOperand() {
5238 return getOperand(0);
5241 /// Gets the pointer operand.
5242 const Value
*getPointerOperand() const {
5243 return getOperand(0);
5246 /// Gets the operand index of the pointer operand.
5247 static unsigned getPointerOperandIndex() {
5251 /// Returns the address space of the pointer operand.
5252 unsigned getSrcAddressSpace() const {
5253 return getPointerOperand()->getType()->getPointerAddressSpace();
5256 /// Returns the address space of the result.
5257 unsigned getDestAddressSpace() const {
5258 return getType()->getPointerAddressSpace();
5262 /// A helper function that returns the pointer operand of a load or store
5263 /// instruction. Returns nullptr if not load or store.
5264 inline const Value
*getLoadStorePointerOperand(const Value
*V
) {
5265 if (auto *Load
= dyn_cast
<LoadInst
>(V
))
5266 return Load
->getPointerOperand();
5267 if (auto *Store
= dyn_cast
<StoreInst
>(V
))
5268 return Store
->getPointerOperand();
5271 inline Value
*getLoadStorePointerOperand(Value
*V
) {
5272 return const_cast<Value
*>(
5273 getLoadStorePointerOperand(static_cast<const Value
*>(V
)));
5276 /// A helper function that returns the pointer operand of a load, store
5277 /// or GEP instruction. Returns nullptr if not load, store, or GEP.
5278 inline const Value
*getPointerOperand(const Value
*V
) {
5279 if (auto *Ptr
= getLoadStorePointerOperand(V
))
5281 if (auto *Gep
= dyn_cast
<GetElementPtrInst
>(V
))
5282 return Gep
->getPointerOperand();
5285 inline Value
*getPointerOperand(Value
*V
) {
5286 return const_cast<Value
*>(getPointerOperand(static_cast<const Value
*>(V
)));
5289 /// A helper function that returns the alignment of load or store instruction.
5290 inline unsigned getLoadStoreAlignment(Value
*I
) {
5291 assert((isa
<LoadInst
>(I
) || isa
<StoreInst
>(I
)) &&
5292 "Expected Load or Store instruction");
5293 if (auto *LI
= dyn_cast
<LoadInst
>(I
))
5294 return LI
->getAlignment();
5295 return cast
<StoreInst
>(I
)->getAlignment();
5298 /// A helper function that returns the address space of the pointer operand of
5299 /// load or store instruction.
5300 inline unsigned getLoadStoreAddressSpace(Value
*I
) {
5301 assert((isa
<LoadInst
>(I
) || isa
<StoreInst
>(I
)) &&
5302 "Expected Load or Store instruction");
5303 if (auto *LI
= dyn_cast
<LoadInst
>(I
))
5304 return LI
->getPointerAddressSpace();
5305 return cast
<StoreInst
>(I
)->getPointerAddressSpace();
5308 } // end namespace llvm
5310 #endif // LLVM_IR_INSTRUCTIONS_H