1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_INSTRUCTIONS_H
17 #define LLVM_IR_INSTRUCTIONS_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/InstrTypes.h"
26 #include "llvm/Support/ErrorHandling.h"
41 // Consume = 3, // Not specified yet.
45 SequentiallyConsistent = 7
48 enum SynchronizationScope {
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 /// AllocaInst - an instruction to allocate memory on the stack
59 class AllocaInst : public UnaryInstruction {
61 AllocaInst *clone_impl() const override;
63 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
64 const Twine &Name = "",
65 Instruction *InsertBefore = nullptr);
66 AllocaInst(Type *Ty, Value *ArraySize,
67 const Twine &Name, BasicBlock *InsertAtEnd);
69 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
70 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
72 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
73 const Twine &Name = "", Instruction *InsertBefore = nullptr);
74 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
75 const Twine &Name, BasicBlock *InsertAtEnd);
77 // Out of line virtual method, so the vtable, etc. has a home.
78 virtual ~AllocaInst();
80 /// isArrayAllocation - Return true if there is an allocation size parameter
81 /// to the allocation instruction that is not 1.
83 bool isArrayAllocation() const;
85 /// getArraySize - Get the number of elements allocated. For a simple
86 /// allocation of a single element, this will return a constant 1 value.
88 const Value *getArraySize() const { return getOperand(0); }
89 Value *getArraySize() { return getOperand(0); }
91 /// getType - Overload to return most specific pointer type
93 PointerType *getType() const {
94 return cast<PointerType>(Instruction::getType());
97 /// getAllocatedType - Return the type that is being allocated by the
100 Type *getAllocatedType() const;
102 /// getAlignment - Return the alignment of the memory that is being allocated
103 /// by the instruction.
105 unsigned getAlignment() const {
106 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
108 void setAlignment(unsigned Align);
110 /// isStaticAlloca - Return true if this alloca is in the entry block of the
111 /// function and is a constant size. If so, the code generator will fold it
112 /// into the prolog/epilog code, so it is basically free.
113 bool isStaticAlloca() const;
115 /// \brief Return true if this alloca is used as an inalloca argument to a
116 /// call. Such allocas are never considered static even if they are in the
118 bool isUsedWithInAlloca() const {
119 return getSubclassDataFromInstruction() & 32;
122 /// \brief Specify whether this alloca is used to represent a the arguments to
124 void setUsedWithInAlloca(bool V) {
125 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
129 // Methods for support type inquiry through isa, cast, and dyn_cast:
130 static inline bool classof(const Instruction *I) {
131 return (I->getOpcode() == Instruction::Alloca);
133 static inline bool classof(const Value *V) {
134 return isa<Instruction>(V) && classof(cast<Instruction>(V));
137 // Shadow Instruction::setInstructionSubclassData with a private forwarding
138 // method so that subclasses cannot accidentally use it.
139 void setInstructionSubclassData(unsigned short D) {
140 Instruction::setInstructionSubclassData(D);
145 //===----------------------------------------------------------------------===//
147 //===----------------------------------------------------------------------===//
149 /// LoadInst - an instruction for reading from memory. This uses the
150 /// SubclassData field in Value to store whether or not the load is volatile.
152 class LoadInst : public UnaryInstruction {
155 LoadInst *clone_impl() const override;
157 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
158 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
159 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
160 Instruction *InsertBefore = nullptr);
161 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
162 BasicBlock *InsertAtEnd);
163 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
164 unsigned Align, Instruction *InsertBefore = nullptr);
165 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
166 unsigned Align, BasicBlock *InsertAtEnd);
167 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
168 unsigned Align, AtomicOrdering Order,
169 SynchronizationScope SynchScope = CrossThread,
170 Instruction *InsertBefore = nullptr);
171 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
172 unsigned Align, AtomicOrdering Order,
173 SynchronizationScope SynchScope,
174 BasicBlock *InsertAtEnd);
176 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
177 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
178 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
179 bool isVolatile = false,
180 Instruction *InsertBefore = nullptr);
181 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
182 BasicBlock *InsertAtEnd);
184 /// isVolatile - Return true if this is a load from a volatile memory
187 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
189 /// setVolatile - Specify whether this is a volatile load or not.
191 void setVolatile(bool V) {
192 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
196 /// getAlignment - Return the alignment of the access that is being performed
198 unsigned getAlignment() const {
199 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
202 void setAlignment(unsigned Align);
204 /// Returns the ordering effect of this fence.
205 AtomicOrdering getOrdering() const {
206 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
209 /// Set the ordering constraint on this load. May not be Release or
211 void setOrdering(AtomicOrdering Ordering) {
212 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
216 SynchronizationScope getSynchScope() const {
217 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
220 /// Specify whether this load is ordered with respect to all
221 /// concurrently executing threads, or only with respect to signal handlers
222 /// executing in the same thread.
223 void setSynchScope(SynchronizationScope xthread) {
224 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
228 bool isAtomic() const { return getOrdering() != NotAtomic; }
229 void setAtomic(AtomicOrdering Ordering,
230 SynchronizationScope SynchScope = CrossThread) {
231 setOrdering(Ordering);
232 setSynchScope(SynchScope);
235 bool isSimple() const { return !isAtomic() && !isVolatile(); }
236 bool isUnordered() const {
237 return getOrdering() <= Unordered && !isVolatile();
240 Value *getPointerOperand() { return getOperand(0); }
241 const Value *getPointerOperand() const { return getOperand(0); }
242 static unsigned getPointerOperandIndex() { return 0U; }
244 /// \brief Returns the address space of the pointer operand.
245 unsigned getPointerAddressSpace() const {
246 return getPointerOperand()->getType()->getPointerAddressSpace();
250 // Methods for support type inquiry through isa, cast, and dyn_cast:
251 static inline bool classof(const Instruction *I) {
252 return I->getOpcode() == Instruction::Load;
254 static inline bool classof(const Value *V) {
255 return isa<Instruction>(V) && classof(cast<Instruction>(V));
258 // Shadow Instruction::setInstructionSubclassData with a private forwarding
259 // method so that subclasses cannot accidentally use it.
260 void setInstructionSubclassData(unsigned short D) {
261 Instruction::setInstructionSubclassData(D);
266 //===----------------------------------------------------------------------===//
268 //===----------------------------------------------------------------------===//
270 /// StoreInst - an instruction for storing to memory
272 class StoreInst : public Instruction {
273 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
276 StoreInst *clone_impl() const override;
278 // allocate space for exactly two operands
279 void *operator new(size_t s) {
280 return User::operator new(s, 2);
282 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
283 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
284 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
285 Instruction *InsertBefore = nullptr);
286 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
287 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
288 unsigned Align, Instruction *InsertBefore = nullptr);
289 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
290 unsigned Align, BasicBlock *InsertAtEnd);
291 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
292 unsigned Align, AtomicOrdering Order,
293 SynchronizationScope SynchScope = CrossThread,
294 Instruction *InsertBefore = nullptr);
295 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
296 unsigned Align, AtomicOrdering Order,
297 SynchronizationScope SynchScope,
298 BasicBlock *InsertAtEnd);
301 /// isVolatile - Return true if this is a store to a volatile memory
304 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
306 /// setVolatile - Specify whether this is a volatile store or not.
308 void setVolatile(bool V) {
309 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
313 /// Transparently provide more efficient getOperand methods.
314 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
316 /// getAlignment - Return the alignment of the access that is being performed
318 unsigned getAlignment() const {
319 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
322 void setAlignment(unsigned Align);
324 /// Returns the ordering effect of this store.
325 AtomicOrdering getOrdering() const {
326 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
329 /// Set the ordering constraint on this store. May not be Acquire or
331 void setOrdering(AtomicOrdering Ordering) {
332 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
336 SynchronizationScope getSynchScope() const {
337 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
340 /// Specify whether this store instruction is ordered with respect to all
341 /// concurrently executing threads, or only with respect to signal handlers
342 /// executing in the same thread.
343 void setSynchScope(SynchronizationScope xthread) {
344 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
348 bool isAtomic() const { return getOrdering() != NotAtomic; }
349 void setAtomic(AtomicOrdering Ordering,
350 SynchronizationScope SynchScope = CrossThread) {
351 setOrdering(Ordering);
352 setSynchScope(SynchScope);
355 bool isSimple() const { return !isAtomic() && !isVolatile(); }
356 bool isUnordered() const {
357 return getOrdering() <= Unordered && !isVolatile();
360 Value *getValueOperand() { return getOperand(0); }
361 const Value *getValueOperand() const { return getOperand(0); }
363 Value *getPointerOperand() { return getOperand(1); }
364 const Value *getPointerOperand() const { return getOperand(1); }
365 static unsigned getPointerOperandIndex() { return 1U; }
367 /// \brief Returns the address space of the pointer operand.
368 unsigned getPointerAddressSpace() const {
369 return getPointerOperand()->getType()->getPointerAddressSpace();
372 // Methods for support type inquiry through isa, cast, and dyn_cast:
373 static inline bool classof(const Instruction *I) {
374 return I->getOpcode() == Instruction::Store;
376 static inline bool classof(const Value *V) {
377 return isa<Instruction>(V) && classof(cast<Instruction>(V));
380 // Shadow Instruction::setInstructionSubclassData with a private forwarding
381 // method so that subclasses cannot accidentally use it.
382 void setInstructionSubclassData(unsigned short D) {
383 Instruction::setInstructionSubclassData(D);
388 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
391 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
393 //===----------------------------------------------------------------------===//
395 //===----------------------------------------------------------------------===//
397 /// FenceInst - an instruction for ordering other memory operations
399 class FenceInst : public Instruction {
400 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
401 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
403 FenceInst *clone_impl() const override;
405 // allocate space for exactly zero operands
406 void *operator new(size_t s) {
407 return User::operator new(s, 0);
410 // Ordering may only be Acquire, Release, AcquireRelease, or
411 // SequentiallyConsistent.
412 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
413 SynchronizationScope SynchScope = CrossThread,
414 Instruction *InsertBefore = nullptr);
415 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
416 SynchronizationScope SynchScope,
417 BasicBlock *InsertAtEnd);
419 /// Returns the ordering effect of this fence.
420 AtomicOrdering getOrdering() const {
421 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
424 /// Set the ordering constraint on this fence. May only be Acquire, Release,
425 /// AcquireRelease, or SequentiallyConsistent.
426 void setOrdering(AtomicOrdering Ordering) {
427 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
431 SynchronizationScope getSynchScope() const {
432 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
435 /// Specify whether this fence orders other operations with respect to all
436 /// concurrently executing threads, or only with respect to signal handlers
437 /// executing in the same thread.
438 void setSynchScope(SynchronizationScope xthread) {
439 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
443 // Methods for support type inquiry through isa, cast, and dyn_cast:
444 static inline bool classof(const Instruction *I) {
445 return I->getOpcode() == Instruction::Fence;
447 static inline bool classof(const Value *V) {
448 return isa<Instruction>(V) && classof(cast<Instruction>(V));
451 // Shadow Instruction::setInstructionSubclassData with a private forwarding
452 // method so that subclasses cannot accidentally use it.
453 void setInstructionSubclassData(unsigned short D) {
454 Instruction::setInstructionSubclassData(D);
458 //===----------------------------------------------------------------------===//
459 // AtomicCmpXchgInst Class
460 //===----------------------------------------------------------------------===//
462 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
463 /// specified value is in a memory location, and, if it is, stores a new value
464 /// there. Returns the value that was loaded.
466 class AtomicCmpXchgInst : public Instruction {
467 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
468 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
469 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
470 SynchronizationScope SynchScope);
472 AtomicCmpXchgInst *clone_impl() const override;
474 // allocate space for exactly three operands
475 void *operator new(size_t s) {
476 return User::operator new(s, 3);
478 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
479 AtomicOrdering SuccessOrdering,
480 AtomicOrdering FailureOrdering,
481 SynchronizationScope SynchScope,
482 Instruction *InsertBefore = nullptr);
483 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
484 AtomicOrdering SuccessOrdering,
485 AtomicOrdering FailureOrdering,
486 SynchronizationScope SynchScope,
487 BasicBlock *InsertAtEnd);
489 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
492 bool isVolatile() const {
493 return getSubclassDataFromInstruction() & 1;
496 /// setVolatile - Specify whether this is a volatile cmpxchg.
498 void setVolatile(bool V) {
499 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
503 /// Return true if this cmpxchg may spuriously fail.
504 bool isWeak() const {
505 return getSubclassDataFromInstruction() & 0x100;
508 void setWeak(bool IsWeak) {
509 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
513 /// Transparently provide more efficient getOperand methods.
514 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
516 /// Set the ordering constraint on this cmpxchg.
517 void setSuccessOrdering(AtomicOrdering Ordering) {
518 assert(Ordering != NotAtomic &&
519 "CmpXchg instructions can only be atomic.");
520 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
524 void setFailureOrdering(AtomicOrdering Ordering) {
525 assert(Ordering != NotAtomic &&
526 "CmpXchg instructions can only be atomic.");
527 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
531 /// Specify whether this cmpxchg is atomic and orders other operations with
532 /// respect to all concurrently executing threads, or only with respect to
533 /// signal handlers executing in the same thread.
534 void setSynchScope(SynchronizationScope SynchScope) {
535 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
539 /// Returns the ordering constraint on this cmpxchg.
540 AtomicOrdering getSuccessOrdering() const {
541 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
544 /// Returns the ordering constraint on this cmpxchg.
545 AtomicOrdering getFailureOrdering() const {
546 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
549 /// Returns whether this cmpxchg is atomic between threads or only within a
551 SynchronizationScope getSynchScope() const {
552 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
555 Value *getPointerOperand() { return getOperand(0); }
556 const Value *getPointerOperand() const { return getOperand(0); }
557 static unsigned getPointerOperandIndex() { return 0U; }
559 Value *getCompareOperand() { return getOperand(1); }
560 const Value *getCompareOperand() const { return getOperand(1); }
562 Value *getNewValOperand() { return getOperand(2); }
563 const Value *getNewValOperand() const { return getOperand(2); }
565 /// \brief Returns the address space of the pointer operand.
566 unsigned getPointerAddressSpace() const {
567 return getPointerOperand()->getType()->getPointerAddressSpace();
570 /// \brief Returns the strongest permitted ordering on failure, given the
571 /// desired ordering on success.
573 /// If the comparison in a cmpxchg operation fails, there is no atomic store
574 /// so release semantics cannot be provided. So this function drops explicit
575 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
576 /// operation would remain SequentiallyConsistent.
577 static AtomicOrdering
578 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
579 switch (SuccessOrdering) {
580 default: llvm_unreachable("invalid cmpxchg success ordering");
587 case SequentiallyConsistent:
588 return SequentiallyConsistent;
592 // Methods for support type inquiry through isa, cast, and dyn_cast:
593 static inline bool classof(const Instruction *I) {
594 return I->getOpcode() == Instruction::AtomicCmpXchg;
596 static inline bool classof(const Value *V) {
597 return isa<Instruction>(V) && classof(cast<Instruction>(V));
600 // Shadow Instruction::setInstructionSubclassData with a private forwarding
601 // method so that subclasses cannot accidentally use it.
602 void setInstructionSubclassData(unsigned short D) {
603 Instruction::setInstructionSubclassData(D);
608 struct OperandTraits<AtomicCmpXchgInst> :
609 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
612 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
614 //===----------------------------------------------------------------------===//
615 // AtomicRMWInst Class
616 //===----------------------------------------------------------------------===//
618 /// AtomicRMWInst - an instruction that atomically reads a memory location,
619 /// combines it with another value, and then stores the result back. Returns
622 class AtomicRMWInst : public Instruction {
623 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
625 AtomicRMWInst *clone_impl() const override;
627 /// This enumeration lists the possible modifications atomicrmw can make. In
628 /// the descriptions, 'p' is the pointer to the instruction's memory location,
629 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
630 /// instruction. These instructions always return 'old'.
646 /// *p = old >signed v ? old : v
648 /// *p = old <signed v ? old : v
650 /// *p = old >unsigned v ? old : v
652 /// *p = old <unsigned v ? old : v
660 // allocate space for exactly two operands
661 void *operator new(size_t s) {
662 return User::operator new(s, 2);
664 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
665 AtomicOrdering Ordering, SynchronizationScope SynchScope,
666 Instruction *InsertBefore = nullptr);
667 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
668 AtomicOrdering Ordering, SynchronizationScope SynchScope,
669 BasicBlock *InsertAtEnd);
671 BinOp getOperation() const {
672 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
675 void setOperation(BinOp Operation) {
676 unsigned short SubclassData = getSubclassDataFromInstruction();
677 setInstructionSubclassData((SubclassData & 31) |
681 /// isVolatile - Return true if this is a RMW on a volatile memory location.
683 bool isVolatile() const {
684 return getSubclassDataFromInstruction() & 1;
687 /// setVolatile - Specify whether this is a volatile RMW or not.
689 void setVolatile(bool V) {
690 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
694 /// Transparently provide more efficient getOperand methods.
695 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
697 /// Set the ordering constraint on this RMW.
698 void setOrdering(AtomicOrdering Ordering) {
699 assert(Ordering != NotAtomic &&
700 "atomicrmw instructions can only be atomic.");
701 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
705 /// Specify whether this RMW orders other operations with respect to all
706 /// concurrently executing threads, or only with respect to signal handlers
707 /// executing in the same thread.
708 void setSynchScope(SynchronizationScope SynchScope) {
709 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
713 /// Returns the ordering constraint on this RMW.
714 AtomicOrdering getOrdering() const {
715 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
718 /// Returns whether this RMW is atomic between threads or only within a
720 SynchronizationScope getSynchScope() const {
721 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
724 Value *getPointerOperand() { return getOperand(0); }
725 const Value *getPointerOperand() const { return getOperand(0); }
726 static unsigned getPointerOperandIndex() { return 0U; }
728 Value *getValOperand() { return getOperand(1); }
729 const Value *getValOperand() const { return getOperand(1); }
731 /// \brief Returns the address space of the pointer operand.
732 unsigned getPointerAddressSpace() const {
733 return getPointerOperand()->getType()->getPointerAddressSpace();
736 // Methods for support type inquiry through isa, cast, and dyn_cast:
737 static inline bool classof(const Instruction *I) {
738 return I->getOpcode() == Instruction::AtomicRMW;
740 static inline bool classof(const Value *V) {
741 return isa<Instruction>(V) && classof(cast<Instruction>(V));
744 void Init(BinOp Operation, Value *Ptr, Value *Val,
745 AtomicOrdering Ordering, SynchronizationScope SynchScope);
746 // Shadow Instruction::setInstructionSubclassData with a private forwarding
747 // method so that subclasses cannot accidentally use it.
748 void setInstructionSubclassData(unsigned short D) {
749 Instruction::setInstructionSubclassData(D);
754 struct OperandTraits<AtomicRMWInst>
755 : public FixedNumOperandTraits<AtomicRMWInst,2> {
758 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
760 //===----------------------------------------------------------------------===//
761 // GetElementPtrInst Class
762 //===----------------------------------------------------------------------===//
764 // checkGEPType - Simple wrapper function to give a better assertion failure
765 // message on bad indexes for a gep instruction.
767 inline Type *checkGEPType(Type *Ty) {
768 assert(Ty && "Invalid GetElementPtrInst indices for type!");
772 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
773 /// access elements of arrays and structs
775 class GetElementPtrInst : public Instruction {
776 GetElementPtrInst(const GetElementPtrInst &GEPI);
777 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
779 /// Constructors - Create a getelementptr instruction with a base pointer an
780 /// list of indices. The first ctor can optionally insert before an existing
781 /// instruction, the second appends the new instruction to the specified
783 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
784 unsigned Values, const Twine &NameStr,
785 Instruction *InsertBefore);
786 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
787 unsigned Values, const Twine &NameStr,
788 BasicBlock *InsertAtEnd);
790 GetElementPtrInst *clone_impl() const override;
792 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
793 const Twine &NameStr = "",
794 Instruction *InsertBefore = nullptr) {
795 unsigned Values = 1 + unsigned(IdxList.size());
797 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
799 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
800 const Twine &NameStr,
801 BasicBlock *InsertAtEnd) {
802 unsigned Values = 1 + unsigned(IdxList.size());
804 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
807 /// Create an "inbounds" getelementptr. See the documentation for the
808 /// "inbounds" flag in LangRef.html for details.
809 static GetElementPtrInst *CreateInBounds(Value *Ptr,
810 ArrayRef<Value *> IdxList,
811 const Twine &NameStr = "",
812 Instruction *InsertBefore = nullptr){
813 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
814 GEP->setIsInBounds(true);
817 static GetElementPtrInst *CreateInBounds(Value *Ptr,
818 ArrayRef<Value *> IdxList,
819 const Twine &NameStr,
820 BasicBlock *InsertAtEnd) {
821 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
822 GEP->setIsInBounds(true);
826 /// Transparently provide more efficient getOperand methods.
827 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
829 // getType - Overload to return most specific sequential type.
830 SequentialType *getType() const {
831 return cast<SequentialType>(Instruction::getType());
834 /// \brief Returns the address space of this instruction's pointer type.
835 unsigned getAddressSpace() const {
836 // Note that this is always the same as the pointer operand's address space
837 // and that is cheaper to compute, so cheat here.
838 return getPointerAddressSpace();
841 /// getIndexedType - Returns the type of the element that would be loaded with
842 /// a load instruction with the specified parameters.
844 /// Null is returned if the indices are invalid for the specified
847 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
848 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
849 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
851 inline op_iterator idx_begin() { return op_begin()+1; }
852 inline const_op_iterator idx_begin() const { return op_begin()+1; }
853 inline op_iterator idx_end() { return op_end(); }
854 inline const_op_iterator idx_end() const { return op_end(); }
856 Value *getPointerOperand() {
857 return getOperand(0);
859 const Value *getPointerOperand() const {
860 return getOperand(0);
862 static unsigned getPointerOperandIndex() {
863 return 0U; // get index for modifying correct operand.
866 /// getPointerOperandType - Method to return the pointer operand as a
868 Type *getPointerOperandType() const {
869 return getPointerOperand()->getType();
872 /// \brief Returns the address space of the pointer operand.
873 unsigned getPointerAddressSpace() const {
874 return getPointerOperandType()->getPointerAddressSpace();
877 /// GetGEPReturnType - Returns the pointer type returned by the GEP
878 /// instruction, which may be a vector of pointers.
879 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
880 Type *PtrTy = PointerType::get(checkGEPType(
881 getIndexedType(Ptr->getType(), IdxList)),
882 Ptr->getType()->getPointerAddressSpace());
884 if (Ptr->getType()->isVectorTy()) {
885 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
886 return VectorType::get(PtrTy, NumElem);
893 unsigned getNumIndices() const { // Note: always non-negative
894 return getNumOperands() - 1;
897 bool hasIndices() const {
898 return getNumOperands() > 1;
901 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
902 /// zeros. If so, the result pointer and the first operand have the same
903 /// value, just potentially different types.
904 bool hasAllZeroIndices() const;
906 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
907 /// constant integers. If so, the result pointer and the first operand have
908 /// a constant offset between them.
909 bool hasAllConstantIndices() const;
911 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
912 /// See LangRef.html for the meaning of inbounds on a getelementptr.
913 void setIsInBounds(bool b = true);
915 /// isInBounds - Determine whether the GEP has the inbounds flag.
916 bool isInBounds() const;
918 /// \brief Accumulate the constant address offset of this GEP if possible.
920 /// This routine accepts an APInt into which it will accumulate the constant
921 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
922 /// all-constant, it returns false and the value of the offset APInt is
923 /// undefined (it is *not* preserved!). The APInt passed into this routine
924 /// must be at least as wide as the IntPtr type for the address space of
925 /// the base GEP pointer.
926 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
928 // Methods for support type inquiry through isa, cast, and dyn_cast:
929 static inline bool classof(const Instruction *I) {
930 return (I->getOpcode() == Instruction::GetElementPtr);
932 static inline bool classof(const Value *V) {
933 return isa<Instruction>(V) && classof(cast<Instruction>(V));
938 struct OperandTraits<GetElementPtrInst> :
939 public VariadicOperandTraits<GetElementPtrInst, 1> {
942 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
943 ArrayRef<Value *> IdxList,
945 const Twine &NameStr,
946 Instruction *InsertBefore)
947 : Instruction(getGEPReturnType(Ptr, IdxList),
949 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
950 Values, InsertBefore) {
951 init(Ptr, IdxList, NameStr);
953 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
954 ArrayRef<Value *> IdxList,
956 const Twine &NameStr,
957 BasicBlock *InsertAtEnd)
958 : Instruction(getGEPReturnType(Ptr, IdxList),
960 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
961 Values, InsertAtEnd) {
962 init(Ptr, IdxList, NameStr);
966 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
969 //===----------------------------------------------------------------------===//
971 //===----------------------------------------------------------------------===//
973 /// This instruction compares its operands according to the predicate given
974 /// to the constructor. It only operates on integers or pointers. The operands
975 /// must be identical types.
976 /// \brief Represent an integer comparison operator.
977 class ICmpInst: public CmpInst {
979 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
980 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
981 "Invalid ICmp predicate value");
982 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
983 "Both operands to ICmp instruction are not of the same type!");
984 // Check that the operands are the right type
985 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
986 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
987 "Invalid operand types for ICmp instruction");
991 /// \brief Clone an identical ICmpInst
992 ICmpInst *clone_impl() const override;
994 /// \brief Constructor with insert-before-instruction semantics.
996 Instruction *InsertBefore, ///< Where to insert
997 Predicate pred, ///< The predicate to use for the comparison
998 Value *LHS, ///< The left-hand-side of the expression
999 Value *RHS, ///< The right-hand-side of the expression
1000 const Twine &NameStr = "" ///< Name of the instruction
1001 ) : CmpInst(makeCmpResultType(LHS->getType()),
1002 Instruction::ICmp, pred, LHS, RHS, NameStr,
1009 /// \brief Constructor with insert-at-end semantics.
1011 BasicBlock &InsertAtEnd, ///< Block to insert into.
1012 Predicate pred, ///< The predicate to use for the comparison
1013 Value *LHS, ///< The left-hand-side of the expression
1014 Value *RHS, ///< The right-hand-side of the expression
1015 const Twine &NameStr = "" ///< Name of the instruction
1016 ) : CmpInst(makeCmpResultType(LHS->getType()),
1017 Instruction::ICmp, pred, LHS, RHS, NameStr,
1024 /// \brief Constructor with no-insertion semantics
1026 Predicate pred, ///< The predicate to use for the comparison
1027 Value *LHS, ///< The left-hand-side of the expression
1028 Value *RHS, ///< The right-hand-side of the expression
1029 const Twine &NameStr = "" ///< Name of the instruction
1030 ) : CmpInst(makeCmpResultType(LHS->getType()),
1031 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1037 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1038 /// @returns the predicate that would be the result if the operand were
1039 /// regarded as signed.
1040 /// \brief Return the signed version of the predicate
1041 Predicate getSignedPredicate() const {
1042 return getSignedPredicate(getPredicate());
1045 /// This is a static version that you can use without an instruction.
1046 /// \brief Return the signed version of the predicate.
1047 static Predicate getSignedPredicate(Predicate pred);
1049 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1050 /// @returns the predicate that would be the result if the operand were
1051 /// regarded as unsigned.
1052 /// \brief Return the unsigned version of the predicate
1053 Predicate getUnsignedPredicate() const {
1054 return getUnsignedPredicate(getPredicate());
1057 /// This is a static version that you can use without an instruction.
1058 /// \brief Return the unsigned version of the predicate.
1059 static Predicate getUnsignedPredicate(Predicate pred);
1061 /// isEquality - Return true if this predicate is either EQ or NE. This also
1062 /// tests for commutativity.
1063 static bool isEquality(Predicate P) {
1064 return P == ICMP_EQ || P == ICMP_NE;
1067 /// isEquality - Return true if this predicate is either EQ or NE. This also
1068 /// tests for commutativity.
1069 bool isEquality() const {
1070 return isEquality(getPredicate());
1073 /// @returns true if the predicate of this ICmpInst is commutative
1074 /// \brief Determine if this relation is commutative.
1075 bool isCommutative() const { return isEquality(); }
1077 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1079 bool isRelational() const {
1080 return !isEquality();
1083 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1085 static bool isRelational(Predicate P) {
1086 return !isEquality(P);
1089 /// Initialize a set of values that all satisfy the predicate with C.
1090 /// \brief Make a ConstantRange for a relation with a constant value.
1091 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1093 /// Exchange the two operands to this instruction in such a way that it does
1094 /// not modify the semantics of the instruction. The predicate value may be
1095 /// changed to retain the same result if the predicate is order dependent
1097 /// \brief Swap operands and adjust predicate.
1098 void swapOperands() {
1099 setPredicate(getSwappedPredicate());
1100 Op<0>().swap(Op<1>());
1103 // Methods for support type inquiry through isa, cast, and dyn_cast:
1104 static inline bool classof(const Instruction *I) {
1105 return I->getOpcode() == Instruction::ICmp;
1107 static inline bool classof(const Value *V) {
1108 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1113 //===----------------------------------------------------------------------===//
1115 //===----------------------------------------------------------------------===//
1117 /// This instruction compares its operands according to the predicate given
1118 /// to the constructor. It only operates on floating point values or packed
1119 /// vectors of floating point values. The operands must be identical types.
1120 /// \brief Represents a floating point comparison operator.
1121 class FCmpInst: public CmpInst {
1123 /// \brief Clone an identical FCmpInst
1124 FCmpInst *clone_impl() const override;
1126 /// \brief Constructor with insert-before-instruction semantics.
1128 Instruction *InsertBefore, ///< Where to insert
1129 Predicate pred, ///< The predicate to use for the comparison
1130 Value *LHS, ///< The left-hand-side of the expression
1131 Value *RHS, ///< The right-hand-side of the expression
1132 const Twine &NameStr = "" ///< Name of the instruction
1133 ) : CmpInst(makeCmpResultType(LHS->getType()),
1134 Instruction::FCmp, pred, LHS, RHS, NameStr,
1136 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1137 "Invalid FCmp predicate value");
1138 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1139 "Both operands to FCmp instruction are not of the same type!");
1140 // Check that the operands are the right type
1141 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1142 "Invalid operand types for FCmp instruction");
1145 /// \brief Constructor with insert-at-end semantics.
1147 BasicBlock &InsertAtEnd, ///< Block to insert into.
1148 Predicate pred, ///< The predicate to use for the comparison
1149 Value *LHS, ///< The left-hand-side of the expression
1150 Value *RHS, ///< The right-hand-side of the expression
1151 const Twine &NameStr = "" ///< Name of the instruction
1152 ) : CmpInst(makeCmpResultType(LHS->getType()),
1153 Instruction::FCmp, pred, LHS, RHS, NameStr,
1155 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1156 "Invalid FCmp predicate value");
1157 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1158 "Both operands to FCmp instruction are not of the same type!");
1159 // Check that the operands are the right type
1160 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1161 "Invalid operand types for FCmp instruction");
1164 /// \brief Constructor with no-insertion semantics
1166 Predicate pred, ///< The predicate to use for the comparison
1167 Value *LHS, ///< The left-hand-side of the expression
1168 Value *RHS, ///< The right-hand-side of the expression
1169 const Twine &NameStr = "" ///< Name of the instruction
1170 ) : CmpInst(makeCmpResultType(LHS->getType()),
1171 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1172 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1173 "Invalid FCmp predicate value");
1174 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1175 "Both operands to FCmp instruction are not of the same type!");
1176 // Check that the operands are the right type
1177 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1178 "Invalid operand types for FCmp instruction");
1181 /// @returns true if the predicate of this instruction is EQ or NE.
1182 /// \brief Determine if this is an equality predicate.
1183 bool isEquality() const {
1184 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1185 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1188 /// @returns true if the predicate of this instruction is commutative.
1189 /// \brief Determine if this is a commutative predicate.
1190 bool isCommutative() const {
1191 return isEquality() ||
1192 getPredicate() == FCMP_FALSE ||
1193 getPredicate() == FCMP_TRUE ||
1194 getPredicate() == FCMP_ORD ||
1195 getPredicate() == FCMP_UNO;
1198 /// @returns true if the predicate is relational (not EQ or NE).
1199 /// \brief Determine if this a relational predicate.
1200 bool isRelational() const { return !isEquality(); }
1202 /// Exchange the two operands to this instruction in such a way that it does
1203 /// not modify the semantics of the instruction. The predicate value may be
1204 /// changed to retain the same result if the predicate is order dependent
1206 /// \brief Swap operands and adjust predicate.
1207 void swapOperands() {
1208 setPredicate(getSwappedPredicate());
1209 Op<0>().swap(Op<1>());
1212 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1213 static inline bool classof(const Instruction *I) {
1214 return I->getOpcode() == Instruction::FCmp;
1216 static inline bool classof(const Value *V) {
1217 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1221 //===----------------------------------------------------------------------===//
1222 /// CallInst - This class represents a function call, abstracting a target
1223 /// machine's calling convention. This class uses low bit of the SubClassData
1224 /// field to indicate whether or not this is a tail call. The rest of the bits
1225 /// hold the calling convention of the call.
1227 class CallInst : public Instruction {
1228 AttributeSet AttributeList; ///< parameter attributes for call
1229 CallInst(const CallInst &CI);
1230 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1231 void init(Value *Func, const Twine &NameStr);
1233 /// Construct a CallInst given a range of arguments.
1234 /// \brief Construct a CallInst from a range of arguments
1235 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1236 const Twine &NameStr, Instruction *InsertBefore);
1238 /// Construct a CallInst given a range of arguments.
1239 /// \brief Construct a CallInst from a range of arguments
1240 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1241 const Twine &NameStr, BasicBlock *InsertAtEnd);
1243 explicit CallInst(Value *F, const Twine &NameStr,
1244 Instruction *InsertBefore);
1245 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1247 CallInst *clone_impl() const override;
1249 static CallInst *Create(Value *Func,
1250 ArrayRef<Value *> Args,
1251 const Twine &NameStr = "",
1252 Instruction *InsertBefore = nullptr) {
1253 return new(unsigned(Args.size() + 1))
1254 CallInst(Func, Args, NameStr, InsertBefore);
1256 static CallInst *Create(Value *Func,
1257 ArrayRef<Value *> Args,
1258 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1259 return new(unsigned(Args.size() + 1))
1260 CallInst(Func, Args, NameStr, InsertAtEnd);
1262 static CallInst *Create(Value *F, const Twine &NameStr = "",
1263 Instruction *InsertBefore = nullptr) {
1264 return new(1) CallInst(F, NameStr, InsertBefore);
1266 static CallInst *Create(Value *F, const Twine &NameStr,
1267 BasicBlock *InsertAtEnd) {
1268 return new(1) CallInst(F, NameStr, InsertAtEnd);
1270 /// CreateMalloc - Generate the IR for a call to malloc:
1271 /// 1. Compute the malloc call's argument as the specified type's size,
1272 /// possibly multiplied by the array size if the array size is not
1274 /// 2. Call malloc with that argument.
1275 /// 3. Bitcast the result of the malloc call to the specified type.
1276 static Instruction *CreateMalloc(Instruction *InsertBefore,
1277 Type *IntPtrTy, Type *AllocTy,
1278 Value *AllocSize, Value *ArraySize = nullptr,
1279 Function* MallocF = nullptr,
1280 const Twine &Name = "");
1281 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1282 Type *IntPtrTy, Type *AllocTy,
1283 Value *AllocSize, Value *ArraySize = nullptr,
1284 Function* MallocF = nullptr,
1285 const Twine &Name = "");
1286 /// CreateFree - Generate the IR for a call to the builtin free function.
1287 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1288 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1292 // Note that 'musttail' implies 'tail'.
1293 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1294 TailCallKind getTailCallKind() const {
1295 return TailCallKind(getSubclassDataFromInstruction() & 3);
1297 bool isTailCall() const {
1298 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1300 bool isMustTailCall() const {
1301 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1303 void setTailCall(bool isTC = true) {
1304 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1305 unsigned(isTC ? TCK_Tail : TCK_None));
1307 void setTailCallKind(TailCallKind TCK) {
1308 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1312 /// Provide fast operand accessors
1313 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1315 /// getNumArgOperands - Return the number of call arguments.
1317 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1319 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1321 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1322 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1324 /// arg_operands - iteration adapter for range-for loops.
1325 iterator_range<op_iterator> arg_operands() {
1326 // The last operand in the op list is the callee - it's not one of the args
1327 // so we don't want to iterate over it.
1328 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1331 /// arg_operands - iteration adapter for range-for loops.
1332 iterator_range<const_op_iterator> arg_operands() const {
1333 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1336 /// \brief Wrappers for getting the \c Use of a call argument.
1337 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1338 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1340 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1342 CallingConv::ID getCallingConv() const {
1343 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1345 void setCallingConv(CallingConv::ID CC) {
1346 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1347 (static_cast<unsigned>(CC) << 2));
1350 /// getAttributes - Return the parameter attributes for this call.
1352 const AttributeSet &getAttributes() const { return AttributeList; }
1354 /// setAttributes - Set the parameter attributes for this call.
1356 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1358 /// addAttribute - adds the attribute to the list of attributes.
1359 void addAttribute(unsigned i, Attribute::AttrKind attr);
1361 /// removeAttribute - removes the attribute from the list of attributes.
1362 void removeAttribute(unsigned i, Attribute attr);
1364 /// \brief Determine whether this call has the given attribute.
1365 bool hasFnAttr(Attribute::AttrKind A) const {
1366 assert(A != Attribute::NoBuiltin &&
1367 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1368 return hasFnAttrImpl(A);
1371 /// \brief Determine whether the call or the callee has the given attributes.
1372 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1374 /// \brief Extract the alignment for a call or parameter (0=unknown).
1375 unsigned getParamAlignment(unsigned i) const {
1376 return AttributeList.getParamAlignment(i);
1379 /// \brief Extract the number of dereferenceable bytes for a call or
1380 /// parameter (0=unknown).
1381 uint64_t getDereferenceableBytes(unsigned i) const {
1382 return AttributeList.getDereferenceableBytes(i);
1385 /// \brief Return true if the call should not be treated as a call to a
1387 bool isNoBuiltin() const {
1388 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1389 !hasFnAttrImpl(Attribute::Builtin);
1392 /// \brief Return true if the call should not be inlined.
1393 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1394 void setIsNoInline() {
1395 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1398 /// \brief Return true if the call can return twice
1399 bool canReturnTwice() const {
1400 return hasFnAttr(Attribute::ReturnsTwice);
1402 void setCanReturnTwice() {
1403 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1406 /// \brief Determine if the call does not access memory.
1407 bool doesNotAccessMemory() const {
1408 return hasFnAttr(Attribute::ReadNone);
1410 void setDoesNotAccessMemory() {
1411 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1414 /// \brief Determine if the call does not access or only reads memory.
1415 bool onlyReadsMemory() const {
1416 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1418 void setOnlyReadsMemory() {
1419 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1422 /// \brief Determine if the call cannot return.
1423 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1424 void setDoesNotReturn() {
1425 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1428 /// \brief Determine if the call cannot unwind.
1429 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1430 void setDoesNotThrow() {
1431 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1434 /// \brief Determine if the call cannot be duplicated.
1435 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1436 void setCannotDuplicate() {
1437 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1440 /// \brief Determine if the call returns a structure through first
1441 /// pointer argument.
1442 bool hasStructRetAttr() const {
1443 // Be friendly and also check the callee.
1444 return paramHasAttr(1, Attribute::StructRet);
1447 /// \brief Determine if any call argument is an aggregate passed by value.
1448 bool hasByValArgument() const {
1449 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1452 /// getCalledFunction - Return the function called, or null if this is an
1453 /// indirect function invocation.
1455 Function *getCalledFunction() const {
1456 return dyn_cast<Function>(Op<-1>());
1459 /// getCalledValue - Get a pointer to the function that is invoked by this
1461 const Value *getCalledValue() const { return Op<-1>(); }
1462 Value *getCalledValue() { return Op<-1>(); }
1464 /// setCalledFunction - Set the function called.
1465 void setCalledFunction(Value* Fn) {
1469 /// isInlineAsm - Check if this call is an inline asm statement.
1470 bool isInlineAsm() const {
1471 return isa<InlineAsm>(Op<-1>());
1474 // Methods for support type inquiry through isa, cast, and dyn_cast:
1475 static inline bool classof(const Instruction *I) {
1476 return I->getOpcode() == Instruction::Call;
1478 static inline bool classof(const Value *V) {
1479 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1483 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1485 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1486 // method so that subclasses cannot accidentally use it.
1487 void setInstructionSubclassData(unsigned short D) {
1488 Instruction::setInstructionSubclassData(D);
1493 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1496 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1497 const Twine &NameStr, BasicBlock *InsertAtEnd)
1498 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1499 ->getElementType())->getReturnType(),
1501 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1502 unsigned(Args.size() + 1), InsertAtEnd) {
1503 init(Func, Args, NameStr);
1506 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1507 const Twine &NameStr, Instruction *InsertBefore)
1508 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1509 ->getElementType())->getReturnType(),
1511 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1512 unsigned(Args.size() + 1), InsertBefore) {
1513 init(Func, Args, NameStr);
1517 // Note: if you get compile errors about private methods then
1518 // please update your code to use the high-level operand
1519 // interfaces. See line 943 above.
1520 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1522 //===----------------------------------------------------------------------===//
1524 //===----------------------------------------------------------------------===//
1526 /// SelectInst - This class represents the LLVM 'select' instruction.
1528 class SelectInst : public Instruction {
1529 void init(Value *C, Value *S1, Value *S2) {
1530 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1536 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1537 Instruction *InsertBefore)
1538 : Instruction(S1->getType(), Instruction::Select,
1539 &Op<0>(), 3, InsertBefore) {
1543 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1544 BasicBlock *InsertAtEnd)
1545 : Instruction(S1->getType(), Instruction::Select,
1546 &Op<0>(), 3, InsertAtEnd) {
1551 SelectInst *clone_impl() const override;
1553 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1554 const Twine &NameStr = "",
1555 Instruction *InsertBefore = nullptr) {
1556 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1558 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1559 const Twine &NameStr,
1560 BasicBlock *InsertAtEnd) {
1561 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1564 const Value *getCondition() const { return Op<0>(); }
1565 const Value *getTrueValue() const { return Op<1>(); }
1566 const Value *getFalseValue() const { return Op<2>(); }
1567 Value *getCondition() { return Op<0>(); }
1568 Value *getTrueValue() { return Op<1>(); }
1569 Value *getFalseValue() { return Op<2>(); }
1571 /// areInvalidOperands - Return a string if the specified operands are invalid
1572 /// for a select operation, otherwise return null.
1573 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1575 /// Transparently provide more efficient getOperand methods.
1576 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1578 OtherOps getOpcode() const {
1579 return static_cast<OtherOps>(Instruction::getOpcode());
1582 // Methods for support type inquiry through isa, cast, and dyn_cast:
1583 static inline bool classof(const Instruction *I) {
1584 return I->getOpcode() == Instruction::Select;
1586 static inline bool classof(const Value *V) {
1587 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1592 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1595 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1597 //===----------------------------------------------------------------------===//
1599 //===----------------------------------------------------------------------===//
1601 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1602 /// an argument of the specified type given a va_list and increments that list
1604 class VAArgInst : public UnaryInstruction {
1606 VAArgInst *clone_impl() const override;
1609 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1610 Instruction *InsertBefore = nullptr)
1611 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1614 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1615 BasicBlock *InsertAtEnd)
1616 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1620 Value *getPointerOperand() { return getOperand(0); }
1621 const Value *getPointerOperand() const { return getOperand(0); }
1622 static unsigned getPointerOperandIndex() { return 0U; }
1624 // Methods for support type inquiry through isa, cast, and dyn_cast:
1625 static inline bool classof(const Instruction *I) {
1626 return I->getOpcode() == VAArg;
1628 static inline bool classof(const Value *V) {
1629 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1633 //===----------------------------------------------------------------------===//
1634 // ExtractElementInst Class
1635 //===----------------------------------------------------------------------===//
1637 /// ExtractElementInst - This instruction extracts a single (scalar)
1638 /// element from a VectorType value
1640 class ExtractElementInst : public Instruction {
1641 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1642 Instruction *InsertBefore = nullptr);
1643 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1644 BasicBlock *InsertAtEnd);
1646 ExtractElementInst *clone_impl() const override;
1649 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1650 const Twine &NameStr = "",
1651 Instruction *InsertBefore = nullptr) {
1652 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1654 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1655 const Twine &NameStr,
1656 BasicBlock *InsertAtEnd) {
1657 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1660 /// isValidOperands - Return true if an extractelement instruction can be
1661 /// formed with the specified operands.
1662 static bool isValidOperands(const Value *Vec, const Value *Idx);
1664 Value *getVectorOperand() { return Op<0>(); }
1665 Value *getIndexOperand() { return Op<1>(); }
1666 const Value *getVectorOperand() const { return Op<0>(); }
1667 const Value *getIndexOperand() const { return Op<1>(); }
1669 VectorType *getVectorOperandType() const {
1670 return cast<VectorType>(getVectorOperand()->getType());
1674 /// Transparently provide more efficient getOperand methods.
1675 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1677 // Methods for support type inquiry through isa, cast, and dyn_cast:
1678 static inline bool classof(const Instruction *I) {
1679 return I->getOpcode() == Instruction::ExtractElement;
1681 static inline bool classof(const Value *V) {
1682 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1687 struct OperandTraits<ExtractElementInst> :
1688 public FixedNumOperandTraits<ExtractElementInst, 2> {
1691 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1693 //===----------------------------------------------------------------------===//
1694 // InsertElementInst Class
1695 //===----------------------------------------------------------------------===//
1697 /// InsertElementInst - This instruction inserts a single (scalar)
1698 /// element into a VectorType value
1700 class InsertElementInst : public Instruction {
1701 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1702 const Twine &NameStr = "",
1703 Instruction *InsertBefore = nullptr);
1704 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1705 const Twine &NameStr, BasicBlock *InsertAtEnd);
1707 InsertElementInst *clone_impl() const override;
1710 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1711 const Twine &NameStr = "",
1712 Instruction *InsertBefore = nullptr) {
1713 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1715 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1716 const Twine &NameStr,
1717 BasicBlock *InsertAtEnd) {
1718 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1721 /// isValidOperands - Return true if an insertelement instruction can be
1722 /// formed with the specified operands.
1723 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1726 /// getType - Overload to return most specific vector type.
1728 VectorType *getType() const {
1729 return cast<VectorType>(Instruction::getType());
1732 /// Transparently provide more efficient getOperand methods.
1733 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1735 // Methods for support type inquiry through isa, cast, and dyn_cast:
1736 static inline bool classof(const Instruction *I) {
1737 return I->getOpcode() == Instruction::InsertElement;
1739 static inline bool classof(const Value *V) {
1740 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1745 struct OperandTraits<InsertElementInst> :
1746 public FixedNumOperandTraits<InsertElementInst, 3> {
1749 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1751 //===----------------------------------------------------------------------===//
1752 // ShuffleVectorInst Class
1753 //===----------------------------------------------------------------------===//
1755 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1758 class ShuffleVectorInst : public Instruction {
1760 ShuffleVectorInst *clone_impl() const override;
1763 // allocate space for exactly three operands
1764 void *operator new(size_t s) {
1765 return User::operator new(s, 3);
1767 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1768 const Twine &NameStr = "",
1769 Instruction *InsertBefor = nullptr);
1770 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1771 const Twine &NameStr, BasicBlock *InsertAtEnd);
1773 /// isValidOperands - Return true if a shufflevector instruction can be
1774 /// formed with the specified operands.
1775 static bool isValidOperands(const Value *V1, const Value *V2,
1778 /// getType - Overload to return most specific vector type.
1780 VectorType *getType() const {
1781 return cast<VectorType>(Instruction::getType());
1784 /// Transparently provide more efficient getOperand methods.
1785 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1787 Constant *getMask() const {
1788 return cast<Constant>(getOperand(2));
1791 /// getMaskValue - Return the index from the shuffle mask for the specified
1792 /// output result. This is either -1 if the element is undef or a number less
1793 /// than 2*numelements.
1794 static int getMaskValue(Constant *Mask, unsigned i);
1796 int getMaskValue(unsigned i) const {
1797 return getMaskValue(getMask(), i);
1800 /// getShuffleMask - Return the full mask for this instruction, where each
1801 /// element is the element number and undef's are returned as -1.
1802 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1804 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1805 return getShuffleMask(getMask(), Result);
1808 SmallVector<int, 16> getShuffleMask() const {
1809 SmallVector<int, 16> Mask;
1810 getShuffleMask(Mask);
1815 // Methods for support type inquiry through isa, cast, and dyn_cast:
1816 static inline bool classof(const Instruction *I) {
1817 return I->getOpcode() == Instruction::ShuffleVector;
1819 static inline bool classof(const Value *V) {
1820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1825 struct OperandTraits<ShuffleVectorInst> :
1826 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1829 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1831 //===----------------------------------------------------------------------===//
1832 // ExtractValueInst Class
1833 //===----------------------------------------------------------------------===//
1835 /// ExtractValueInst - This instruction extracts a struct member or array
1836 /// element value from an aggregate value.
1838 class ExtractValueInst : public UnaryInstruction {
1839 SmallVector<unsigned, 4> Indices;
1841 ExtractValueInst(const ExtractValueInst &EVI);
1842 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1844 /// Constructors - Create a extractvalue instruction with a base aggregate
1845 /// value and a list of indices. The first ctor can optionally insert before
1846 /// an existing instruction, the second appends the new instruction to the
1847 /// specified BasicBlock.
1848 inline ExtractValueInst(Value *Agg,
1849 ArrayRef<unsigned> Idxs,
1850 const Twine &NameStr,
1851 Instruction *InsertBefore);
1852 inline ExtractValueInst(Value *Agg,
1853 ArrayRef<unsigned> Idxs,
1854 const Twine &NameStr, BasicBlock *InsertAtEnd);
1856 // allocate space for exactly one operand
1857 void *operator new(size_t s) {
1858 return User::operator new(s, 1);
1861 ExtractValueInst *clone_impl() const override;
1864 static ExtractValueInst *Create(Value *Agg,
1865 ArrayRef<unsigned> Idxs,
1866 const Twine &NameStr = "",
1867 Instruction *InsertBefore = nullptr) {
1869 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1871 static ExtractValueInst *Create(Value *Agg,
1872 ArrayRef<unsigned> Idxs,
1873 const Twine &NameStr,
1874 BasicBlock *InsertAtEnd) {
1875 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1878 /// getIndexedType - Returns the type of the element that would be extracted
1879 /// with an extractvalue instruction with the specified parameters.
1881 /// Null is returned if the indices are invalid for the specified type.
1882 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1884 typedef const unsigned* idx_iterator;
1885 inline idx_iterator idx_begin() const { return Indices.begin(); }
1886 inline idx_iterator idx_end() const { return Indices.end(); }
1888 Value *getAggregateOperand() {
1889 return getOperand(0);
1891 const Value *getAggregateOperand() const {
1892 return getOperand(0);
1894 static unsigned getAggregateOperandIndex() {
1895 return 0U; // get index for modifying correct operand
1898 ArrayRef<unsigned> getIndices() const {
1902 unsigned getNumIndices() const {
1903 return (unsigned)Indices.size();
1906 bool hasIndices() const {
1910 // Methods for support type inquiry through isa, cast, and dyn_cast:
1911 static inline bool classof(const Instruction *I) {
1912 return I->getOpcode() == Instruction::ExtractValue;
1914 static inline bool classof(const Value *V) {
1915 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1919 ExtractValueInst::ExtractValueInst(Value *Agg,
1920 ArrayRef<unsigned> Idxs,
1921 const Twine &NameStr,
1922 Instruction *InsertBefore)
1923 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1924 ExtractValue, Agg, InsertBefore) {
1925 init(Idxs, NameStr);
1927 ExtractValueInst::ExtractValueInst(Value *Agg,
1928 ArrayRef<unsigned> Idxs,
1929 const Twine &NameStr,
1930 BasicBlock *InsertAtEnd)
1931 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1932 ExtractValue, Agg, InsertAtEnd) {
1933 init(Idxs, NameStr);
1937 //===----------------------------------------------------------------------===//
1938 // InsertValueInst Class
1939 //===----------------------------------------------------------------------===//
1941 /// InsertValueInst - This instruction inserts a struct field of array element
1942 /// value into an aggregate value.
1944 class InsertValueInst : public Instruction {
1945 SmallVector<unsigned, 4> Indices;
1947 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1948 InsertValueInst(const InsertValueInst &IVI);
1949 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1950 const Twine &NameStr);
1952 /// Constructors - Create a insertvalue instruction with a base aggregate
1953 /// value, a value to insert, and a list of indices. The first ctor can
1954 /// optionally insert before an existing instruction, the second appends
1955 /// the new instruction to the specified BasicBlock.
1956 inline InsertValueInst(Value *Agg, Value *Val,
1957 ArrayRef<unsigned> Idxs,
1958 const Twine &NameStr,
1959 Instruction *InsertBefore);
1960 inline InsertValueInst(Value *Agg, Value *Val,
1961 ArrayRef<unsigned> Idxs,
1962 const Twine &NameStr, BasicBlock *InsertAtEnd);
1964 /// Constructors - These two constructors are convenience methods because one
1965 /// and two index insertvalue instructions are so common.
1966 InsertValueInst(Value *Agg, Value *Val,
1967 unsigned Idx, const Twine &NameStr = "",
1968 Instruction *InsertBefore = nullptr);
1969 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1970 const Twine &NameStr, BasicBlock *InsertAtEnd);
1972 InsertValueInst *clone_impl() const override;
1974 // allocate space for exactly two operands
1975 void *operator new(size_t s) {
1976 return User::operator new(s, 2);
1979 static InsertValueInst *Create(Value *Agg, Value *Val,
1980 ArrayRef<unsigned> Idxs,
1981 const Twine &NameStr = "",
1982 Instruction *InsertBefore = nullptr) {
1983 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1985 static InsertValueInst *Create(Value *Agg, Value *Val,
1986 ArrayRef<unsigned> Idxs,
1987 const Twine &NameStr,
1988 BasicBlock *InsertAtEnd) {
1989 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1992 /// Transparently provide more efficient getOperand methods.
1993 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1995 typedef const unsigned* idx_iterator;
1996 inline idx_iterator idx_begin() const { return Indices.begin(); }
1997 inline idx_iterator idx_end() const { return Indices.end(); }
1999 Value *getAggregateOperand() {
2000 return getOperand(0);
2002 const Value *getAggregateOperand() const {
2003 return getOperand(0);
2005 static unsigned getAggregateOperandIndex() {
2006 return 0U; // get index for modifying correct operand
2009 Value *getInsertedValueOperand() {
2010 return getOperand(1);
2012 const Value *getInsertedValueOperand() const {
2013 return getOperand(1);
2015 static unsigned getInsertedValueOperandIndex() {
2016 return 1U; // get index for modifying correct operand
2019 ArrayRef<unsigned> getIndices() const {
2023 unsigned getNumIndices() const {
2024 return (unsigned)Indices.size();
2027 bool hasIndices() const {
2031 // Methods for support type inquiry through isa, cast, and dyn_cast:
2032 static inline bool classof(const Instruction *I) {
2033 return I->getOpcode() == Instruction::InsertValue;
2035 static inline bool classof(const Value *V) {
2036 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2041 struct OperandTraits<InsertValueInst> :
2042 public FixedNumOperandTraits<InsertValueInst, 2> {
2045 InsertValueInst::InsertValueInst(Value *Agg,
2047 ArrayRef<unsigned> Idxs,
2048 const Twine &NameStr,
2049 Instruction *InsertBefore)
2050 : Instruction(Agg->getType(), InsertValue,
2051 OperandTraits<InsertValueInst>::op_begin(this),
2053 init(Agg, Val, Idxs, NameStr);
2055 InsertValueInst::InsertValueInst(Value *Agg,
2057 ArrayRef<unsigned> Idxs,
2058 const Twine &NameStr,
2059 BasicBlock *InsertAtEnd)
2060 : Instruction(Agg->getType(), InsertValue,
2061 OperandTraits<InsertValueInst>::op_begin(this),
2063 init(Agg, Val, Idxs, NameStr);
2066 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2068 //===----------------------------------------------------------------------===//
2070 //===----------------------------------------------------------------------===//
2072 // PHINode - The PHINode class is used to represent the magical mystical PHI
2073 // node, that can not exist in nature, but can be synthesized in a computer
2074 // scientist's overactive imagination.
2076 class PHINode : public Instruction {
2077 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2078 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2079 /// the number actually in use.
2080 unsigned ReservedSpace;
2081 PHINode(const PHINode &PN);
2082 // allocate space for exactly zero operands
2083 void *operator new(size_t s) {
2084 return User::operator new(s, 0);
2086 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2087 const Twine &NameStr = "",
2088 Instruction *InsertBefore = nullptr)
2089 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2090 ReservedSpace(NumReservedValues) {
2092 OperandList = allocHungoffUses(ReservedSpace);
2095 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2096 BasicBlock *InsertAtEnd)
2097 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2098 ReservedSpace(NumReservedValues) {
2100 OperandList = allocHungoffUses(ReservedSpace);
2103 // allocHungoffUses - this is more complicated than the generic
2104 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2105 // values and pointers to the incoming blocks, all in one allocation.
2106 Use *allocHungoffUses(unsigned) const;
2108 PHINode *clone_impl() const override;
2110 /// Constructors - NumReservedValues is a hint for the number of incoming
2111 /// edges that this phi node will have (use 0 if you really have no idea).
2112 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2113 const Twine &NameStr = "",
2114 Instruction *InsertBefore = nullptr) {
2115 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2117 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2118 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2119 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2123 /// Provide fast operand accessors
2124 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2126 // Block iterator interface. This provides access to the list of incoming
2127 // basic blocks, which parallels the list of incoming values.
2129 typedef BasicBlock **block_iterator;
2130 typedef BasicBlock * const *const_block_iterator;
2132 block_iterator block_begin() {
2134 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2135 return reinterpret_cast<block_iterator>(ref + 1);
2138 const_block_iterator block_begin() const {
2139 const Use::UserRef *ref =
2140 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2141 return reinterpret_cast<const_block_iterator>(ref + 1);
2144 block_iterator block_end() {
2145 return block_begin() + getNumOperands();
2148 const_block_iterator block_end() const {
2149 return block_begin() + getNumOperands();
2152 /// getNumIncomingValues - Return the number of incoming edges
2154 unsigned getNumIncomingValues() const { return getNumOperands(); }
2156 /// getIncomingValue - Return incoming value number x
2158 Value *getIncomingValue(unsigned i) const {
2159 return getOperand(i);
2161 void setIncomingValue(unsigned i, Value *V) {
2164 static unsigned getOperandNumForIncomingValue(unsigned i) {
2167 static unsigned getIncomingValueNumForOperand(unsigned i) {
2171 /// getIncomingBlock - Return incoming basic block number @p i.
2173 BasicBlock *getIncomingBlock(unsigned i) const {
2174 return block_begin()[i];
2177 /// getIncomingBlock - Return incoming basic block corresponding
2178 /// to an operand of the PHI.
2180 BasicBlock *getIncomingBlock(const Use &U) const {
2181 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2182 return getIncomingBlock(unsigned(&U - op_begin()));
2185 /// getIncomingBlock - Return incoming basic block corresponding
2186 /// to value use iterator.
2188 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2189 return getIncomingBlock(I.getUse());
2192 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2193 block_begin()[i] = BB;
2196 /// addIncoming - Add an incoming value to the end of the PHI list
2198 void addIncoming(Value *V, BasicBlock *BB) {
2199 assert(V && "PHI node got a null value!");
2200 assert(BB && "PHI node got a null basic block!");
2201 assert(getType() == V->getType() &&
2202 "All operands to PHI node must be the same type as the PHI node!");
2203 if (NumOperands == ReservedSpace)
2204 growOperands(); // Get more space!
2205 // Initialize some new operands.
2207 setIncomingValue(NumOperands - 1, V);
2208 setIncomingBlock(NumOperands - 1, BB);
2211 /// removeIncomingValue - Remove an incoming value. This is useful if a
2212 /// predecessor basic block is deleted. The value removed is returned.
2214 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2215 /// is true), the PHI node is destroyed and any uses of it are replaced with
2216 /// dummy values. The only time there should be zero incoming values to a PHI
2217 /// node is when the block is dead, so this strategy is sound.
2219 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2221 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2222 int Idx = getBasicBlockIndex(BB);
2223 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2224 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2227 /// getBasicBlockIndex - Return the first index of the specified basic
2228 /// block in the value list for this PHI. Returns -1 if no instance.
2230 int getBasicBlockIndex(const BasicBlock *BB) const {
2231 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2232 if (block_begin()[i] == BB)
2237 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2238 int Idx = getBasicBlockIndex(BB);
2239 assert(Idx >= 0 && "Invalid basic block argument!");
2240 return getIncomingValue(Idx);
2243 /// hasConstantValue - If the specified PHI node always merges together the
2244 /// same value, return the value, otherwise return null.
2245 Value *hasConstantValue() const;
2247 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2248 static inline bool classof(const Instruction *I) {
2249 return I->getOpcode() == Instruction::PHI;
2251 static inline bool classof(const Value *V) {
2252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2255 void growOperands();
2259 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2262 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2264 //===----------------------------------------------------------------------===//
2265 // LandingPadInst Class
2266 //===----------------------------------------------------------------------===//
2268 //===---------------------------------------------------------------------------
2269 /// LandingPadInst - The landingpad instruction holds all of the information
2270 /// necessary to generate correct exception handling. The landingpad instruction
2271 /// cannot be moved from the top of a landing pad block, which itself is
2272 /// accessible only from the 'unwind' edge of an invoke. This uses the
2273 /// SubclassData field in Value to store whether or not the landingpad is a
2276 class LandingPadInst : public Instruction {
2277 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2278 /// the number actually in use.
2279 unsigned ReservedSpace;
2280 LandingPadInst(const LandingPadInst &LP);
2282 enum ClauseType { Catch, Filter };
2284 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2285 // Allocate space for exactly zero operands.
2286 void *operator new(size_t s) {
2287 return User::operator new(s, 0);
2289 void growOperands(unsigned Size);
2290 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2292 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2293 unsigned NumReservedValues, const Twine &NameStr,
2294 Instruction *InsertBefore);
2295 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2296 unsigned NumReservedValues, const Twine &NameStr,
2297 BasicBlock *InsertAtEnd);
2299 LandingPadInst *clone_impl() const override;
2301 /// Constructors - NumReservedClauses is a hint for the number of incoming
2302 /// clauses that this landingpad will have (use 0 if you really have no idea).
2303 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2304 unsigned NumReservedClauses,
2305 const Twine &NameStr = "",
2306 Instruction *InsertBefore = nullptr);
2307 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2308 unsigned NumReservedClauses,
2309 const Twine &NameStr, BasicBlock *InsertAtEnd);
2312 /// Provide fast operand accessors
2313 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2315 /// getPersonalityFn - Get the personality function associated with this
2317 Value *getPersonalityFn() const { return getOperand(0); }
2319 /// isCleanup - Return 'true' if this landingpad instruction is a
2320 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2321 /// doesn't catch the exception.
2322 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2324 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2325 void setCleanup(bool V) {
2326 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2330 /// Add a catch or filter clause to the landing pad.
2331 void addClause(Constant *ClauseVal);
2333 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2334 /// determine what type of clause this is.
2335 Constant *getClause(unsigned Idx) const {
2336 return cast<Constant>(OperandList[Idx + 1]);
2339 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2340 bool isCatch(unsigned Idx) const {
2341 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2344 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2345 bool isFilter(unsigned Idx) const {
2346 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2349 /// getNumClauses - Get the number of clauses for this landing pad.
2350 unsigned getNumClauses() const { return getNumOperands() - 1; }
2352 /// reserveClauses - Grow the size of the operand list to accommodate the new
2353 /// number of clauses.
2354 void reserveClauses(unsigned Size) { growOperands(Size); }
2356 // Methods for support type inquiry through isa, cast, and dyn_cast:
2357 static inline bool classof(const Instruction *I) {
2358 return I->getOpcode() == Instruction::LandingPad;
2360 static inline bool classof(const Value *V) {
2361 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2366 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2369 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2371 //===----------------------------------------------------------------------===//
2373 //===----------------------------------------------------------------------===//
2375 //===---------------------------------------------------------------------------
2376 /// ReturnInst - Return a value (possibly void), from a function. Execution
2377 /// does not continue in this function any longer.
2379 class ReturnInst : public TerminatorInst {
2380 ReturnInst(const ReturnInst &RI);
2383 // ReturnInst constructors:
2384 // ReturnInst() - 'ret void' instruction
2385 // ReturnInst( null) - 'ret void' instruction
2386 // ReturnInst(Value* X) - 'ret X' instruction
2387 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2388 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2389 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2390 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2392 // NOTE: If the Value* passed is of type void then the constructor behaves as
2393 // if it was passed NULL.
2394 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2395 Instruction *InsertBefore = nullptr);
2396 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2397 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2399 ReturnInst *clone_impl() const override;
2401 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2402 Instruction *InsertBefore = nullptr) {
2403 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2405 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2406 BasicBlock *InsertAtEnd) {
2407 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2409 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2410 return new(0) ReturnInst(C, InsertAtEnd);
2412 virtual ~ReturnInst();
2414 /// Provide fast operand accessors
2415 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2417 /// Convenience accessor. Returns null if there is no return value.
2418 Value *getReturnValue() const {
2419 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2422 unsigned getNumSuccessors() const { return 0; }
2424 // Methods for support type inquiry through isa, cast, and dyn_cast:
2425 static inline bool classof(const Instruction *I) {
2426 return (I->getOpcode() == Instruction::Ret);
2428 static inline bool classof(const Value *V) {
2429 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2432 BasicBlock *getSuccessorV(unsigned idx) const override;
2433 unsigned getNumSuccessorsV() const override;
2434 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2438 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2441 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2443 //===----------------------------------------------------------------------===//
2445 //===----------------------------------------------------------------------===//
2447 //===---------------------------------------------------------------------------
2448 /// BranchInst - Conditional or Unconditional Branch instruction.
2450 class BranchInst : public TerminatorInst {
2451 /// Ops list - Branches are strange. The operands are ordered:
2452 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2453 /// they don't have to check for cond/uncond branchness. These are mostly
2454 /// accessed relative from op_end().
2455 BranchInst(const BranchInst &BI);
2457 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2458 // BranchInst(BB *B) - 'br B'
2459 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2460 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2461 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2462 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2463 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2464 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2465 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2466 Instruction *InsertBefore = nullptr);
2467 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2468 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2469 BasicBlock *InsertAtEnd);
2471 BranchInst *clone_impl() const override;
2473 static BranchInst *Create(BasicBlock *IfTrue,
2474 Instruction *InsertBefore = nullptr) {
2475 return new(1) BranchInst(IfTrue, InsertBefore);
2477 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2478 Value *Cond, Instruction *InsertBefore = nullptr) {
2479 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2481 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2482 return new(1) BranchInst(IfTrue, InsertAtEnd);
2484 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2485 Value *Cond, BasicBlock *InsertAtEnd) {
2486 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2489 /// Transparently provide more efficient getOperand methods.
2490 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2492 bool isUnconditional() const { return getNumOperands() == 1; }
2493 bool isConditional() const { return getNumOperands() == 3; }
2495 Value *getCondition() const {
2496 assert(isConditional() && "Cannot get condition of an uncond branch!");
2500 void setCondition(Value *V) {
2501 assert(isConditional() && "Cannot set condition of unconditional branch!");
2505 unsigned getNumSuccessors() const { return 1+isConditional(); }
2507 BasicBlock *getSuccessor(unsigned i) const {
2508 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2509 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2512 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2513 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2514 *(&Op<-1>() - idx) = (Value*)NewSucc;
2517 /// \brief Swap the successors of this branch instruction.
2519 /// Swaps the successors of the branch instruction. This also swaps any
2520 /// branch weight metadata associated with the instruction so that it
2521 /// continues to map correctly to each operand.
2522 void swapSuccessors();
2524 // Methods for support type inquiry through isa, cast, and dyn_cast:
2525 static inline bool classof(const Instruction *I) {
2526 return (I->getOpcode() == Instruction::Br);
2528 static inline bool classof(const Value *V) {
2529 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2532 BasicBlock *getSuccessorV(unsigned idx) const override;
2533 unsigned getNumSuccessorsV() const override;
2534 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2538 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2541 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2543 //===----------------------------------------------------------------------===//
2545 //===----------------------------------------------------------------------===//
2547 //===---------------------------------------------------------------------------
2548 /// SwitchInst - Multiway switch
2550 class SwitchInst : public TerminatorInst {
2551 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2552 unsigned ReservedSpace;
2553 // Operand[0] = Value to switch on
2554 // Operand[1] = Default basic block destination
2555 // Operand[2n ] = Value to match
2556 // Operand[2n+1] = BasicBlock to go to on match
2557 SwitchInst(const SwitchInst &SI);
2558 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2559 void growOperands();
2560 // allocate space for exactly zero operands
2561 void *operator new(size_t s) {
2562 return User::operator new(s, 0);
2564 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2565 /// switch on and a default destination. The number of additional cases can
2566 /// be specified here to make memory allocation more efficient. This
2567 /// constructor can also autoinsert before another instruction.
2568 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2569 Instruction *InsertBefore);
2571 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2572 /// switch on and a default destination. The number of additional cases can
2573 /// be specified here to make memory allocation more efficient. This
2574 /// constructor also autoinserts at the end of the specified BasicBlock.
2575 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2576 BasicBlock *InsertAtEnd);
2578 SwitchInst *clone_impl() const override;
2582 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2584 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2585 class CaseIteratorT {
2593 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2595 /// Initializes case iterator for given SwitchInst and for given
2597 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2602 /// Initializes case iterator for given SwitchInst and for given
2603 /// TerminatorInst's successor index.
2604 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2605 assert(SuccessorIndex < SI->getNumSuccessors() &&
2606 "Successor index # out of range!");
2607 return SuccessorIndex != 0 ?
2608 Self(SI, SuccessorIndex - 1) :
2609 Self(SI, DefaultPseudoIndex);
2612 /// Resolves case value for current case.
2613 ConstantIntTy *getCaseValue() {
2614 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2615 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2618 /// Resolves successor for current case.
2619 BasicBlockTy *getCaseSuccessor() {
2620 assert((Index < SI->getNumCases() ||
2621 Index == DefaultPseudoIndex) &&
2622 "Index out the number of cases.");
2623 return SI->getSuccessor(getSuccessorIndex());
2626 /// Returns number of current case.
2627 unsigned getCaseIndex() const { return Index; }
2629 /// Returns TerminatorInst's successor index for current case successor.
2630 unsigned getSuccessorIndex() const {
2631 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2632 "Index out the number of cases.");
2633 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2637 // Check index correctness after increment.
2638 // Note: Index == getNumCases() means end().
2639 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2643 Self operator++(int) {
2649 // Check index correctness after decrement.
2650 // Note: Index == getNumCases() means end().
2651 // Also allow "-1" iterator here. That will became valid after ++.
2652 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2653 "Index out the number of cases.");
2657 Self operator--(int) {
2662 bool operator==(const Self& RHS) const {
2663 assert(RHS.SI == SI && "Incompatible operators.");
2664 return RHS.Index == Index;
2666 bool operator!=(const Self& RHS) const {
2667 assert(RHS.SI == SI && "Incompatible operators.");
2668 return RHS.Index != Index;
2675 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2678 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2680 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2684 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2685 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2687 /// Sets the new value for current case.
2688 void setValue(ConstantInt *V) {
2689 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2690 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2693 /// Sets the new successor for current case.
2694 void setSuccessor(BasicBlock *S) {
2695 SI->setSuccessor(getSuccessorIndex(), S);
2699 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2701 Instruction *InsertBefore = nullptr) {
2702 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2704 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2705 unsigned NumCases, BasicBlock *InsertAtEnd) {
2706 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2711 /// Provide fast operand accessors
2712 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2714 // Accessor Methods for Switch stmt
2715 Value *getCondition() const { return getOperand(0); }
2716 void setCondition(Value *V) { setOperand(0, V); }
2718 BasicBlock *getDefaultDest() const {
2719 return cast<BasicBlock>(getOperand(1));
2722 void setDefaultDest(BasicBlock *DefaultCase) {
2723 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2726 /// getNumCases - return the number of 'cases' in this switch instruction,
2727 /// except the default case
2728 unsigned getNumCases() const {
2729 return getNumOperands()/2 - 1;
2732 /// Returns a read/write iterator that points to the first
2733 /// case in SwitchInst.
2734 CaseIt case_begin() {
2735 return CaseIt(this, 0);
2737 /// Returns a read-only iterator that points to the first
2738 /// case in the SwitchInst.
2739 ConstCaseIt case_begin() const {
2740 return ConstCaseIt(this, 0);
2743 /// Returns a read/write iterator that points one past the last
2744 /// in the SwitchInst.
2746 return CaseIt(this, getNumCases());
2748 /// Returns a read-only iterator that points one past the last
2749 /// in the SwitchInst.
2750 ConstCaseIt case_end() const {
2751 return ConstCaseIt(this, getNumCases());
2754 /// cases - iteration adapter for range-for loops.
2755 iterator_range<CaseIt> cases() {
2756 return iterator_range<CaseIt>(case_begin(), case_end());
2759 /// cases - iteration adapter for range-for loops.
2760 iterator_range<ConstCaseIt> cases() const {
2761 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2764 /// Returns an iterator that points to the default case.
2765 /// Note: this iterator allows to resolve successor only. Attempt
2766 /// to resolve case value causes an assertion.
2767 /// Also note, that increment and decrement also causes an assertion and
2768 /// makes iterator invalid.
2769 CaseIt case_default() {
2770 return CaseIt(this, DefaultPseudoIndex);
2772 ConstCaseIt case_default() const {
2773 return ConstCaseIt(this, DefaultPseudoIndex);
2776 /// findCaseValue - Search all of the case values for the specified constant.
2777 /// If it is explicitly handled, return the case iterator of it, otherwise
2778 /// return default case iterator to indicate
2779 /// that it is handled by the default handler.
2780 CaseIt findCaseValue(const ConstantInt *C) {
2781 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2782 if (i.getCaseValue() == C)
2784 return case_default();
2786 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2787 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2788 if (i.getCaseValue() == C)
2790 return case_default();
2793 /// findCaseDest - Finds the unique case value for a given successor. Returns
2794 /// null if the successor is not found, not unique, or is the default case.
2795 ConstantInt *findCaseDest(BasicBlock *BB) {
2796 if (BB == getDefaultDest()) return nullptr;
2798 ConstantInt *CI = nullptr;
2799 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2800 if (i.getCaseSuccessor() == BB) {
2801 if (CI) return nullptr; // Multiple cases lead to BB.
2802 else CI = i.getCaseValue();
2808 /// addCase - Add an entry to the switch instruction...
2810 /// This action invalidates case_end(). Old case_end() iterator will
2811 /// point to the added case.
2812 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2814 /// removeCase - This method removes the specified case and its successor
2815 /// from the switch instruction. Note that this operation may reorder the
2816 /// remaining cases at index idx and above.
2818 /// This action invalidates iterators for all cases following the one removed,
2819 /// including the case_end() iterator.
2820 void removeCase(CaseIt i);
2822 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2823 BasicBlock *getSuccessor(unsigned idx) const {
2824 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2825 return cast<BasicBlock>(getOperand(idx*2+1));
2827 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2828 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2829 setOperand(idx*2+1, (Value*)NewSucc);
2832 // Methods for support type inquiry through isa, cast, and dyn_cast:
2833 static inline bool classof(const Instruction *I) {
2834 return I->getOpcode() == Instruction::Switch;
2836 static inline bool classof(const Value *V) {
2837 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2840 BasicBlock *getSuccessorV(unsigned idx) const override;
2841 unsigned getNumSuccessorsV() const override;
2842 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2846 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2849 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2852 //===----------------------------------------------------------------------===//
2853 // IndirectBrInst Class
2854 //===----------------------------------------------------------------------===//
2856 //===---------------------------------------------------------------------------
2857 /// IndirectBrInst - Indirect Branch Instruction.
2859 class IndirectBrInst : public TerminatorInst {
2860 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2861 unsigned ReservedSpace;
2862 // Operand[0] = Value to switch on
2863 // Operand[1] = Default basic block destination
2864 // Operand[2n ] = Value to match
2865 // Operand[2n+1] = BasicBlock to go to on match
2866 IndirectBrInst(const IndirectBrInst &IBI);
2867 void init(Value *Address, unsigned NumDests);
2868 void growOperands();
2869 // allocate space for exactly zero operands
2870 void *operator new(size_t s) {
2871 return User::operator new(s, 0);
2873 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2874 /// Address to jump to. The number of expected destinations can be specified
2875 /// here to make memory allocation more efficient. This constructor can also
2876 /// autoinsert before another instruction.
2877 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2879 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2880 /// Address to jump to. The number of expected destinations can be specified
2881 /// here to make memory allocation more efficient. This constructor also
2882 /// autoinserts at the end of the specified BasicBlock.
2883 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2885 IndirectBrInst *clone_impl() const override;
2887 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2888 Instruction *InsertBefore = nullptr) {
2889 return new IndirectBrInst(Address, NumDests, InsertBefore);
2891 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2892 BasicBlock *InsertAtEnd) {
2893 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2897 /// Provide fast operand accessors.
2898 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2900 // Accessor Methods for IndirectBrInst instruction.
2901 Value *getAddress() { return getOperand(0); }
2902 const Value *getAddress() const { return getOperand(0); }
2903 void setAddress(Value *V) { setOperand(0, V); }
2906 /// getNumDestinations - return the number of possible destinations in this
2907 /// indirectbr instruction.
2908 unsigned getNumDestinations() const { return getNumOperands()-1; }
2910 /// getDestination - Return the specified destination.
2911 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2912 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2914 /// addDestination - Add a destination.
2916 void addDestination(BasicBlock *Dest);
2918 /// removeDestination - This method removes the specified successor from the
2919 /// indirectbr instruction.
2920 void removeDestination(unsigned i);
2922 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2923 BasicBlock *getSuccessor(unsigned i) const {
2924 return cast<BasicBlock>(getOperand(i+1));
2926 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2927 setOperand(i+1, (Value*)NewSucc);
2930 // Methods for support type inquiry through isa, cast, and dyn_cast:
2931 static inline bool classof(const Instruction *I) {
2932 return I->getOpcode() == Instruction::IndirectBr;
2934 static inline bool classof(const Value *V) {
2935 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2938 BasicBlock *getSuccessorV(unsigned idx) const override;
2939 unsigned getNumSuccessorsV() const override;
2940 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2944 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2947 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2950 //===----------------------------------------------------------------------===//
2952 //===----------------------------------------------------------------------===//
2954 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2955 /// calling convention of the call.
2957 class InvokeInst : public TerminatorInst {
2958 AttributeSet AttributeList;
2959 InvokeInst(const InvokeInst &BI);
2960 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2961 ArrayRef<Value *> Args, const Twine &NameStr);
2963 /// Construct an InvokeInst given a range of arguments.
2965 /// \brief Construct an InvokeInst from a range of arguments
2966 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2967 ArrayRef<Value *> Args, unsigned Values,
2968 const Twine &NameStr, Instruction *InsertBefore);
2970 /// Construct an InvokeInst given a range of arguments.
2972 /// \brief Construct an InvokeInst from a range of arguments
2973 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2974 ArrayRef<Value *> Args, unsigned Values,
2975 const Twine &NameStr, BasicBlock *InsertAtEnd);
2977 InvokeInst *clone_impl() const override;
2979 static InvokeInst *Create(Value *Func,
2980 BasicBlock *IfNormal, BasicBlock *IfException,
2981 ArrayRef<Value *> Args, const Twine &NameStr = "",
2982 Instruction *InsertBefore = nullptr) {
2983 unsigned Values = unsigned(Args.size()) + 3;
2984 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2985 Values, NameStr, InsertBefore);
2987 static InvokeInst *Create(Value *Func,
2988 BasicBlock *IfNormal, BasicBlock *IfException,
2989 ArrayRef<Value *> Args, const Twine &NameStr,
2990 BasicBlock *InsertAtEnd) {
2991 unsigned Values = unsigned(Args.size()) + 3;
2992 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2993 Values, NameStr, InsertAtEnd);
2996 /// Provide fast operand accessors
2997 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2999 /// getNumArgOperands - Return the number of invoke arguments.
3001 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3003 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3005 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3006 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3008 /// arg_operands - iteration adapter for range-for loops.
3009 iterator_range<op_iterator> arg_operands() {
3010 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3013 /// arg_operands - iteration adapter for range-for loops.
3014 iterator_range<const_op_iterator> arg_operands() const {
3015 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3018 /// \brief Wrappers for getting the \c Use of a invoke argument.
3019 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3020 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3022 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3024 CallingConv::ID getCallingConv() const {
3025 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3027 void setCallingConv(CallingConv::ID CC) {
3028 setInstructionSubclassData(static_cast<unsigned>(CC));
3031 /// getAttributes - Return the parameter attributes for this invoke.
3033 const AttributeSet &getAttributes() const { return AttributeList; }
3035 /// setAttributes - Set the parameter attributes for this invoke.
3037 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3039 /// addAttribute - adds the attribute to the list of attributes.
3040 void addAttribute(unsigned i, Attribute::AttrKind attr);
3042 /// removeAttribute - removes the attribute from the list of attributes.
3043 void removeAttribute(unsigned i, Attribute attr);
3045 /// \brief Determine whether this call has the given attribute.
3046 bool hasFnAttr(Attribute::AttrKind A) const {
3047 assert(A != Attribute::NoBuiltin &&
3048 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3049 return hasFnAttrImpl(A);
3052 /// \brief Determine whether the call or the callee has the given attributes.
3053 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3055 /// \brief Extract the alignment for a call or parameter (0=unknown).
3056 unsigned getParamAlignment(unsigned i) const {
3057 return AttributeList.getParamAlignment(i);
3060 /// \brief Extract the number of dereferenceable bytes for a call or
3061 /// parameter (0=unknown).
3062 uint64_t getDereferenceableBytes(unsigned i) const {
3063 return AttributeList.getDereferenceableBytes(i);
3066 /// \brief Return true if the call should not be treated as a call to a
3068 bool isNoBuiltin() const {
3069 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3070 // to check it by hand.
3071 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3072 !hasFnAttrImpl(Attribute::Builtin);
3075 /// \brief Return true if the call should not be inlined.
3076 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3077 void setIsNoInline() {
3078 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3081 /// \brief Determine if the call does not access memory.
3082 bool doesNotAccessMemory() const {
3083 return hasFnAttr(Attribute::ReadNone);
3085 void setDoesNotAccessMemory() {
3086 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3089 /// \brief Determine if the call does not access or only reads memory.
3090 bool onlyReadsMemory() const {
3091 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3093 void setOnlyReadsMemory() {
3094 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3097 /// \brief Determine if the call cannot return.
3098 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3099 void setDoesNotReturn() {
3100 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3103 /// \brief Determine if the call cannot unwind.
3104 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3105 void setDoesNotThrow() {
3106 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3109 /// \brief Determine if the invoke cannot be duplicated.
3110 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3111 void setCannotDuplicate() {
3112 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3115 /// \brief Determine if the call returns a structure through first
3116 /// pointer argument.
3117 bool hasStructRetAttr() const {
3118 // Be friendly and also check the callee.
3119 return paramHasAttr(1, Attribute::StructRet);
3122 /// \brief Determine if any call argument is an aggregate passed by value.
3123 bool hasByValArgument() const {
3124 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3127 /// getCalledFunction - Return the function called, or null if this is an
3128 /// indirect function invocation.
3130 Function *getCalledFunction() const {
3131 return dyn_cast<Function>(Op<-3>());
3134 /// getCalledValue - Get a pointer to the function that is invoked by this
3136 const Value *getCalledValue() const { return Op<-3>(); }
3137 Value *getCalledValue() { return Op<-3>(); }
3139 /// setCalledFunction - Set the function called.
3140 void setCalledFunction(Value* Fn) {
3144 // get*Dest - Return the destination basic blocks...
3145 BasicBlock *getNormalDest() const {
3146 return cast<BasicBlock>(Op<-2>());
3148 BasicBlock *getUnwindDest() const {
3149 return cast<BasicBlock>(Op<-1>());
3151 void setNormalDest(BasicBlock *B) {
3152 Op<-2>() = reinterpret_cast<Value*>(B);
3154 void setUnwindDest(BasicBlock *B) {
3155 Op<-1>() = reinterpret_cast<Value*>(B);
3158 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3159 /// block (the unwind destination).
3160 LandingPadInst *getLandingPadInst() const;
3162 BasicBlock *getSuccessor(unsigned i) const {
3163 assert(i < 2 && "Successor # out of range for invoke!");
3164 return i == 0 ? getNormalDest() : getUnwindDest();
3167 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3168 assert(idx < 2 && "Successor # out of range for invoke!");
3169 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3172 unsigned getNumSuccessors() const { return 2; }
3174 // Methods for support type inquiry through isa, cast, and dyn_cast:
3175 static inline bool classof(const Instruction *I) {
3176 return (I->getOpcode() == Instruction::Invoke);
3178 static inline bool classof(const Value *V) {
3179 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3183 BasicBlock *getSuccessorV(unsigned idx) const override;
3184 unsigned getNumSuccessorsV() const override;
3185 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3187 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3189 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3190 // method so that subclasses cannot accidentally use it.
3191 void setInstructionSubclassData(unsigned short D) {
3192 Instruction::setInstructionSubclassData(D);
3197 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3200 InvokeInst::InvokeInst(Value *Func,
3201 BasicBlock *IfNormal, BasicBlock *IfException,
3202 ArrayRef<Value *> Args, unsigned Values,
3203 const Twine &NameStr, Instruction *InsertBefore)
3204 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3205 ->getElementType())->getReturnType(),
3206 Instruction::Invoke,
3207 OperandTraits<InvokeInst>::op_end(this) - Values,
3208 Values, InsertBefore) {
3209 init(Func, IfNormal, IfException, Args, NameStr);
3211 InvokeInst::InvokeInst(Value *Func,
3212 BasicBlock *IfNormal, BasicBlock *IfException,
3213 ArrayRef<Value *> Args, unsigned Values,
3214 const Twine &NameStr, BasicBlock *InsertAtEnd)
3215 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3216 ->getElementType())->getReturnType(),
3217 Instruction::Invoke,
3218 OperandTraits<InvokeInst>::op_end(this) - Values,
3219 Values, InsertAtEnd) {
3220 init(Func, IfNormal, IfException, Args, NameStr);
3223 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3225 //===----------------------------------------------------------------------===//
3227 //===----------------------------------------------------------------------===//
3229 //===---------------------------------------------------------------------------
3230 /// ResumeInst - Resume the propagation of an exception.
3232 class ResumeInst : public TerminatorInst {
3233 ResumeInst(const ResumeInst &RI);
3235 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3236 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3238 ResumeInst *clone_impl() const override;
3240 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3241 return new(1) ResumeInst(Exn, InsertBefore);
3243 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3244 return new(1) ResumeInst(Exn, InsertAtEnd);
3247 /// Provide fast operand accessors
3248 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3250 /// Convenience accessor.
3251 Value *getValue() const { return Op<0>(); }
3253 unsigned getNumSuccessors() const { return 0; }
3255 // Methods for support type inquiry through isa, cast, and dyn_cast:
3256 static inline bool classof(const Instruction *I) {
3257 return I->getOpcode() == Instruction::Resume;
3259 static inline bool classof(const Value *V) {
3260 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3263 BasicBlock *getSuccessorV(unsigned idx) const override;
3264 unsigned getNumSuccessorsV() const override;
3265 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3269 struct OperandTraits<ResumeInst> :
3270 public FixedNumOperandTraits<ResumeInst, 1> {
3273 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3275 //===----------------------------------------------------------------------===//
3276 // UnreachableInst Class
3277 //===----------------------------------------------------------------------===//
3279 //===---------------------------------------------------------------------------
3280 /// UnreachableInst - This function has undefined behavior. In particular, the
3281 /// presence of this instruction indicates some higher level knowledge that the
3282 /// end of the block cannot be reached.
3284 class UnreachableInst : public TerminatorInst {
3285 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3287 UnreachableInst *clone_impl() const override;
3290 // allocate space for exactly zero operands
3291 void *operator new(size_t s) {
3292 return User::operator new(s, 0);
3294 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3295 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3297 unsigned getNumSuccessors() const { return 0; }
3299 // Methods for support type inquiry through isa, cast, and dyn_cast:
3300 static inline bool classof(const Instruction *I) {
3301 return I->getOpcode() == Instruction::Unreachable;
3303 static inline bool classof(const Value *V) {
3304 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3307 BasicBlock *getSuccessorV(unsigned idx) const override;
3308 unsigned getNumSuccessorsV() const override;
3309 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3312 //===----------------------------------------------------------------------===//
3314 //===----------------------------------------------------------------------===//
3316 /// \brief This class represents a truncation of integer types.
3317 class TruncInst : public CastInst {
3319 /// \brief Clone an identical TruncInst
3320 TruncInst *clone_impl() const override;
3323 /// \brief Constructor with insert-before-instruction semantics
3325 Value *S, ///< The value to be truncated
3326 Type *Ty, ///< The (smaller) type to truncate to
3327 const Twine &NameStr = "", ///< A name for the new instruction
3328 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3331 /// \brief Constructor with insert-at-end-of-block semantics
3333 Value *S, ///< The value to be truncated
3334 Type *Ty, ///< The (smaller) type to truncate to
3335 const Twine &NameStr, ///< A name for the new instruction
3336 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3339 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3340 static inline bool classof(const Instruction *I) {
3341 return I->getOpcode() == Trunc;
3343 static inline bool classof(const Value *V) {
3344 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3348 //===----------------------------------------------------------------------===//
3350 //===----------------------------------------------------------------------===//
3352 /// \brief This class represents zero extension of integer types.
3353 class ZExtInst : public CastInst {
3355 /// \brief Clone an identical ZExtInst
3356 ZExtInst *clone_impl() const override;
3359 /// \brief Constructor with insert-before-instruction semantics
3361 Value *S, ///< The value to be zero extended
3362 Type *Ty, ///< The type to zero extend to
3363 const Twine &NameStr = "", ///< A name for the new instruction
3364 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3367 /// \brief Constructor with insert-at-end semantics.
3369 Value *S, ///< The value to be zero extended
3370 Type *Ty, ///< The type to zero extend to
3371 const Twine &NameStr, ///< A name for the new instruction
3372 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3375 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3376 static inline bool classof(const Instruction *I) {
3377 return I->getOpcode() == ZExt;
3379 static inline bool classof(const Value *V) {
3380 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3384 //===----------------------------------------------------------------------===//
3386 //===----------------------------------------------------------------------===//
3388 /// \brief This class represents a sign extension of integer types.
3389 class SExtInst : public CastInst {
3391 /// \brief Clone an identical SExtInst
3392 SExtInst *clone_impl() const override;
3395 /// \brief Constructor with insert-before-instruction semantics
3397 Value *S, ///< The value to be sign extended
3398 Type *Ty, ///< The type to sign extend to
3399 const Twine &NameStr = "", ///< A name for the new instruction
3400 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3403 /// \brief Constructor with insert-at-end-of-block semantics
3405 Value *S, ///< The value to be sign extended
3406 Type *Ty, ///< The type to sign extend to
3407 const Twine &NameStr, ///< A name for the new instruction
3408 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3411 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3412 static inline bool classof(const Instruction *I) {
3413 return I->getOpcode() == SExt;
3415 static inline bool classof(const Value *V) {
3416 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3420 //===----------------------------------------------------------------------===//
3421 // FPTruncInst Class
3422 //===----------------------------------------------------------------------===//
3424 /// \brief This class represents a truncation of floating point types.
3425 class FPTruncInst : public CastInst {
3427 /// \brief Clone an identical FPTruncInst
3428 FPTruncInst *clone_impl() const override;
3431 /// \brief Constructor with insert-before-instruction semantics
3433 Value *S, ///< The value to be truncated
3434 Type *Ty, ///< The type to truncate to
3435 const Twine &NameStr = "", ///< A name for the new instruction
3436 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3439 /// \brief Constructor with insert-before-instruction semantics
3441 Value *S, ///< The value to be truncated
3442 Type *Ty, ///< The type to truncate to
3443 const Twine &NameStr, ///< A name for the new instruction
3444 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3447 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3448 static inline bool classof(const Instruction *I) {
3449 return I->getOpcode() == FPTrunc;
3451 static inline bool classof(const Value *V) {
3452 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3456 //===----------------------------------------------------------------------===//
3458 //===----------------------------------------------------------------------===//
3460 /// \brief This class represents an extension of floating point types.
3461 class FPExtInst : public CastInst {
3463 /// \brief Clone an identical FPExtInst
3464 FPExtInst *clone_impl() const override;
3467 /// \brief Constructor with insert-before-instruction semantics
3469 Value *S, ///< The value to be extended
3470 Type *Ty, ///< The type to extend to
3471 const Twine &NameStr = "", ///< A name for the new instruction
3472 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3475 /// \brief Constructor with insert-at-end-of-block semantics
3477 Value *S, ///< The value to be extended
3478 Type *Ty, ///< The type to extend to
3479 const Twine &NameStr, ///< A name for the new instruction
3480 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3483 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3484 static inline bool classof(const Instruction *I) {
3485 return I->getOpcode() == FPExt;
3487 static inline bool classof(const Value *V) {
3488 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3492 //===----------------------------------------------------------------------===//
3494 //===----------------------------------------------------------------------===//
3496 /// \brief This class represents a cast unsigned integer to floating point.
3497 class UIToFPInst : public CastInst {
3499 /// \brief Clone an identical UIToFPInst
3500 UIToFPInst *clone_impl() const override;
3503 /// \brief Constructor with insert-before-instruction semantics
3505 Value *S, ///< The value to be converted
3506 Type *Ty, ///< The type to convert to
3507 const Twine &NameStr = "", ///< A name for the new instruction
3508 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3511 /// \brief Constructor with insert-at-end-of-block semantics
3513 Value *S, ///< The value to be converted
3514 Type *Ty, ///< The type to convert to
3515 const Twine &NameStr, ///< A name for the new instruction
3516 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3519 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3520 static inline bool classof(const Instruction *I) {
3521 return I->getOpcode() == UIToFP;
3523 static inline bool classof(const Value *V) {
3524 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3528 //===----------------------------------------------------------------------===//
3530 //===----------------------------------------------------------------------===//
3532 /// \brief This class represents a cast from signed integer to floating point.
3533 class SIToFPInst : public CastInst {
3535 /// \brief Clone an identical SIToFPInst
3536 SIToFPInst *clone_impl() const override;
3539 /// \brief Constructor with insert-before-instruction semantics
3541 Value *S, ///< The value to be converted
3542 Type *Ty, ///< The type to convert to
3543 const Twine &NameStr = "", ///< A name for the new instruction
3544 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3547 /// \brief Constructor with insert-at-end-of-block semantics
3549 Value *S, ///< The value to be converted
3550 Type *Ty, ///< The type to convert to
3551 const Twine &NameStr, ///< A name for the new instruction
3552 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3555 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3556 static inline bool classof(const Instruction *I) {
3557 return I->getOpcode() == SIToFP;
3559 static inline bool classof(const Value *V) {
3560 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3564 //===----------------------------------------------------------------------===//
3566 //===----------------------------------------------------------------------===//
3568 /// \brief This class represents a cast from floating point to unsigned integer
3569 class FPToUIInst : public CastInst {
3571 /// \brief Clone an identical FPToUIInst
3572 FPToUIInst *clone_impl() const override;
3575 /// \brief Constructor with insert-before-instruction semantics
3577 Value *S, ///< The value to be converted
3578 Type *Ty, ///< The type to convert to
3579 const Twine &NameStr = "", ///< A name for the new instruction
3580 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3583 /// \brief Constructor with insert-at-end-of-block semantics
3585 Value *S, ///< The value to be converted
3586 Type *Ty, ///< The type to convert to
3587 const Twine &NameStr, ///< A name for the new instruction
3588 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3591 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3592 static inline bool classof(const Instruction *I) {
3593 return I->getOpcode() == FPToUI;
3595 static inline bool classof(const Value *V) {
3596 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3600 //===----------------------------------------------------------------------===//
3602 //===----------------------------------------------------------------------===//
3604 /// \brief This class represents a cast from floating point to signed integer.
3605 class FPToSIInst : public CastInst {
3607 /// \brief Clone an identical FPToSIInst
3608 FPToSIInst *clone_impl() const override;
3611 /// \brief Constructor with insert-before-instruction semantics
3613 Value *S, ///< The value to be converted
3614 Type *Ty, ///< The type to convert to
3615 const Twine &NameStr = "", ///< A name for the new instruction
3616 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3619 /// \brief Constructor with insert-at-end-of-block semantics
3621 Value *S, ///< The value to be converted
3622 Type *Ty, ///< The type to convert to
3623 const Twine &NameStr, ///< A name for the new instruction
3624 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3627 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3628 static inline bool classof(const Instruction *I) {
3629 return I->getOpcode() == FPToSI;
3631 static inline bool classof(const Value *V) {
3632 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3636 //===----------------------------------------------------------------------===//
3637 // IntToPtrInst Class
3638 //===----------------------------------------------------------------------===//
3640 /// \brief This class represents a cast from an integer to a pointer.
3641 class IntToPtrInst : public CastInst {
3643 /// \brief Constructor with insert-before-instruction semantics
3645 Value *S, ///< The value to be converted
3646 Type *Ty, ///< The type to convert to
3647 const Twine &NameStr = "", ///< A name for the new instruction
3648 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3651 /// \brief Constructor with insert-at-end-of-block semantics
3653 Value *S, ///< The value to be converted
3654 Type *Ty, ///< The type to convert to
3655 const Twine &NameStr, ///< A name for the new instruction
3656 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3659 /// \brief Clone an identical IntToPtrInst
3660 IntToPtrInst *clone_impl() const override;
3662 /// \brief Returns the address space of this instruction's pointer type.
3663 unsigned getAddressSpace() const {
3664 return getType()->getPointerAddressSpace();
3667 // Methods for support type inquiry through isa, cast, and dyn_cast:
3668 static inline bool classof(const Instruction *I) {
3669 return I->getOpcode() == IntToPtr;
3671 static inline bool classof(const Value *V) {
3672 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3676 //===----------------------------------------------------------------------===//
3677 // PtrToIntInst Class
3678 //===----------------------------------------------------------------------===//
3680 /// \brief This class represents a cast from a pointer to an integer
3681 class PtrToIntInst : public CastInst {
3683 /// \brief Clone an identical PtrToIntInst
3684 PtrToIntInst *clone_impl() const override;
3687 /// \brief Constructor with insert-before-instruction semantics
3689 Value *S, ///< The value to be converted
3690 Type *Ty, ///< The type to convert to
3691 const Twine &NameStr = "", ///< A name for the new instruction
3692 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3695 /// \brief Constructor with insert-at-end-of-block semantics
3697 Value *S, ///< The value to be converted
3698 Type *Ty, ///< The type to convert to
3699 const Twine &NameStr, ///< A name for the new instruction
3700 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3703 /// \brief Gets the pointer operand.
3704 Value *getPointerOperand() { return getOperand(0); }
3705 /// \brief Gets the pointer operand.
3706 const Value *getPointerOperand() const { return getOperand(0); }
3707 /// \brief Gets the operand index of the pointer operand.
3708 static unsigned getPointerOperandIndex() { return 0U; }
3710 /// \brief Returns the address space of the pointer operand.
3711 unsigned getPointerAddressSpace() const {
3712 return getPointerOperand()->getType()->getPointerAddressSpace();
3715 // Methods for support type inquiry through isa, cast, and dyn_cast:
3716 static inline bool classof(const Instruction *I) {
3717 return I->getOpcode() == PtrToInt;
3719 static inline bool classof(const Value *V) {
3720 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3724 //===----------------------------------------------------------------------===//
3725 // BitCastInst Class
3726 //===----------------------------------------------------------------------===//
3728 /// \brief This class represents a no-op cast from one type to another.
3729 class BitCastInst : public CastInst {
3731 /// \brief Clone an identical BitCastInst
3732 BitCastInst *clone_impl() const override;
3735 /// \brief Constructor with insert-before-instruction semantics
3737 Value *S, ///< The value to be casted
3738 Type *Ty, ///< The type to casted to
3739 const Twine &NameStr = "", ///< A name for the new instruction
3740 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3743 /// \brief Constructor with insert-at-end-of-block semantics
3745 Value *S, ///< The value to be casted
3746 Type *Ty, ///< The type to casted to
3747 const Twine &NameStr, ///< A name for the new instruction
3748 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3751 // Methods for support type inquiry through isa, cast, and dyn_cast:
3752 static inline bool classof(const Instruction *I) {
3753 return I->getOpcode() == BitCast;
3755 static inline bool classof(const Value *V) {
3756 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3760 //===----------------------------------------------------------------------===//
3761 // AddrSpaceCastInst Class
3762 //===----------------------------------------------------------------------===//
3764 /// \brief This class represents a conversion between pointers from
3765 /// one address space to another.
3766 class AddrSpaceCastInst : public CastInst {
3768 /// \brief Clone an identical AddrSpaceCastInst
3769 AddrSpaceCastInst *clone_impl() const override;
3772 /// \brief Constructor with insert-before-instruction semantics
3774 Value *S, ///< The value to be casted
3775 Type *Ty, ///< The type to casted to
3776 const Twine &NameStr = "", ///< A name for the new instruction
3777 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3780 /// \brief Constructor with insert-at-end-of-block semantics
3782 Value *S, ///< The value to be casted
3783 Type *Ty, ///< The type to casted to
3784 const Twine &NameStr, ///< A name for the new instruction
3785 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3788 // Methods for support type inquiry through isa, cast, and dyn_cast:
3789 static inline bool classof(const Instruction *I) {
3790 return I->getOpcode() == AddrSpaceCast;
3792 static inline bool classof(const Value *V) {
3793 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3797 } // End llvm namespace