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 /// Returns true if the ordering is at least as strong as acquire
54 /// (i.e. acquire, acq_rel or seq_cst)
55 inline bool isAtLeastAcquire(AtomicOrdering Ord) {
56 return (Ord == Acquire ||
57 Ord == AcquireRelease ||
58 Ord == SequentiallyConsistent);
61 /// Returns true if the ordering is at least as strong as release
62 /// (i.e. release, acq_rel or seq_cst)
63 inline bool isAtLeastRelease(AtomicOrdering Ord) {
64 return (Ord == Release ||
65 Ord == AcquireRelease ||
66 Ord == SequentiallyConsistent);
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 /// AllocaInst - an instruction to allocate memory on the stack
75 class AllocaInst : public UnaryInstruction {
77 AllocaInst *clone_impl() const override;
79 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
80 const Twine &Name = "",
81 Instruction *InsertBefore = nullptr);
82 AllocaInst(Type *Ty, Value *ArraySize,
83 const Twine &Name, BasicBlock *InsertAtEnd);
85 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
86 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
88 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
89 const Twine &Name = "", Instruction *InsertBefore = nullptr);
90 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
91 const Twine &Name, BasicBlock *InsertAtEnd);
93 // Out of line virtual method, so the vtable, etc. has a home.
94 virtual ~AllocaInst();
96 /// isArrayAllocation - Return true if there is an allocation size parameter
97 /// to the allocation instruction that is not 1.
99 bool isArrayAllocation() const;
101 /// getArraySize - Get the number of elements allocated. For a simple
102 /// allocation of a single element, this will return a constant 1 value.
104 const Value *getArraySize() const { return getOperand(0); }
105 Value *getArraySize() { return getOperand(0); }
107 /// getType - Overload to return most specific pointer type
109 PointerType *getType() const {
110 return cast<PointerType>(Instruction::getType());
113 /// getAllocatedType - Return the type that is being allocated by the
116 Type *getAllocatedType() const;
118 /// getAlignment - Return the alignment of the memory that is being allocated
119 /// by the instruction.
121 unsigned getAlignment() const {
122 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
124 void setAlignment(unsigned Align);
126 /// isStaticAlloca - Return true if this alloca is in the entry block of the
127 /// function and is a constant size. If so, the code generator will fold it
128 /// into the prolog/epilog code, so it is basically free.
129 bool isStaticAlloca() const;
131 /// \brief Return true if this alloca is used as an inalloca argument to a
132 /// call. Such allocas are never considered static even if they are in the
134 bool isUsedWithInAlloca() const {
135 return getSubclassDataFromInstruction() & 32;
138 /// \brief Specify whether this alloca is used to represent the arguments to
140 void setUsedWithInAlloca(bool V) {
141 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
145 // Methods for support type inquiry through isa, cast, and dyn_cast:
146 static inline bool classof(const Instruction *I) {
147 return (I->getOpcode() == Instruction::Alloca);
149 static inline bool classof(const Value *V) {
150 return isa<Instruction>(V) && classof(cast<Instruction>(V));
153 // Shadow Instruction::setInstructionSubclassData with a private forwarding
154 // method so that subclasses cannot accidentally use it.
155 void setInstructionSubclassData(unsigned short D) {
156 Instruction::setInstructionSubclassData(D);
161 //===----------------------------------------------------------------------===//
163 //===----------------------------------------------------------------------===//
165 /// LoadInst - an instruction for reading from memory. This uses the
166 /// SubclassData field in Value to store whether or not the load is volatile.
168 class LoadInst : public UnaryInstruction {
171 LoadInst *clone_impl() const override;
173 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
174 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
175 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
176 Instruction *InsertBefore = nullptr);
177 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
178 BasicBlock *InsertAtEnd);
179 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
180 unsigned Align, Instruction *InsertBefore = nullptr);
181 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
182 unsigned Align, BasicBlock *InsertAtEnd);
183 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
184 unsigned Align, AtomicOrdering Order,
185 SynchronizationScope SynchScope = CrossThread,
186 Instruction *InsertBefore = nullptr);
187 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
188 unsigned Align, AtomicOrdering Order,
189 SynchronizationScope SynchScope,
190 BasicBlock *InsertAtEnd);
192 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
193 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
194 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
195 bool isVolatile = false,
196 Instruction *InsertBefore = nullptr);
197 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
198 BasicBlock *InsertAtEnd);
200 /// isVolatile - Return true if this is a load from a volatile memory
203 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
205 /// setVolatile - Specify whether this is a volatile load or not.
207 void setVolatile(bool V) {
208 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
212 /// getAlignment - Return the alignment of the access that is being performed
214 unsigned getAlignment() const {
215 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
218 void setAlignment(unsigned Align);
220 /// Returns the ordering effect of this fence.
221 AtomicOrdering getOrdering() const {
222 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
225 /// Set the ordering constraint on this load. May not be Release or
227 void setOrdering(AtomicOrdering Ordering) {
228 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
232 SynchronizationScope getSynchScope() const {
233 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
236 /// Specify whether this load is ordered with respect to all
237 /// concurrently executing threads, or only with respect to signal handlers
238 /// executing in the same thread.
239 void setSynchScope(SynchronizationScope xthread) {
240 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
244 void setAtomic(AtomicOrdering Ordering,
245 SynchronizationScope SynchScope = CrossThread) {
246 setOrdering(Ordering);
247 setSynchScope(SynchScope);
250 bool isSimple() const { return !isAtomic() && !isVolatile(); }
251 bool isUnordered() const {
252 return getOrdering() <= Unordered && !isVolatile();
255 Value *getPointerOperand() { return getOperand(0); }
256 const Value *getPointerOperand() const { return getOperand(0); }
257 static unsigned getPointerOperandIndex() { return 0U; }
259 /// \brief Returns the address space of the pointer operand.
260 unsigned getPointerAddressSpace() const {
261 return getPointerOperand()->getType()->getPointerAddressSpace();
265 // Methods for support type inquiry through isa, cast, and dyn_cast:
266 static inline bool classof(const Instruction *I) {
267 return I->getOpcode() == Instruction::Load;
269 static inline bool classof(const Value *V) {
270 return isa<Instruction>(V) && classof(cast<Instruction>(V));
273 // Shadow Instruction::setInstructionSubclassData with a private forwarding
274 // method so that subclasses cannot accidentally use it.
275 void setInstructionSubclassData(unsigned short D) {
276 Instruction::setInstructionSubclassData(D);
281 //===----------------------------------------------------------------------===//
283 //===----------------------------------------------------------------------===//
285 /// StoreInst - an instruction for storing to memory
287 class StoreInst : public Instruction {
288 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
291 StoreInst *clone_impl() const override;
293 // allocate space for exactly two operands
294 void *operator new(size_t s) {
295 return User::operator new(s, 2);
297 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
298 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
299 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
300 Instruction *InsertBefore = nullptr);
301 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
302 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
303 unsigned Align, Instruction *InsertBefore = nullptr);
304 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
305 unsigned Align, BasicBlock *InsertAtEnd);
306 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
307 unsigned Align, AtomicOrdering Order,
308 SynchronizationScope SynchScope = CrossThread,
309 Instruction *InsertBefore = nullptr);
310 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
311 unsigned Align, AtomicOrdering Order,
312 SynchronizationScope SynchScope,
313 BasicBlock *InsertAtEnd);
316 /// isVolatile - Return true if this is a store to a volatile memory
319 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
321 /// setVolatile - Specify whether this is a volatile store or not.
323 void setVolatile(bool V) {
324 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
328 /// Transparently provide more efficient getOperand methods.
329 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
331 /// getAlignment - Return the alignment of the access that is being performed
333 unsigned getAlignment() const {
334 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
337 void setAlignment(unsigned Align);
339 /// Returns the ordering effect of this store.
340 AtomicOrdering getOrdering() const {
341 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
344 /// Set the ordering constraint on this store. May not be Acquire or
346 void setOrdering(AtomicOrdering Ordering) {
347 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
351 SynchronizationScope getSynchScope() const {
352 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
355 /// Specify whether this store instruction is ordered with respect to all
356 /// concurrently executing threads, or only with respect to signal handlers
357 /// executing in the same thread.
358 void setSynchScope(SynchronizationScope xthread) {
359 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
363 void setAtomic(AtomicOrdering Ordering,
364 SynchronizationScope SynchScope = CrossThread) {
365 setOrdering(Ordering);
366 setSynchScope(SynchScope);
369 bool isSimple() const { return !isAtomic() && !isVolatile(); }
370 bool isUnordered() const {
371 return getOrdering() <= Unordered && !isVolatile();
374 Value *getValueOperand() { return getOperand(0); }
375 const Value *getValueOperand() const { return getOperand(0); }
377 Value *getPointerOperand() { return getOperand(1); }
378 const Value *getPointerOperand() const { return getOperand(1); }
379 static unsigned getPointerOperandIndex() { return 1U; }
381 /// \brief Returns the address space of the pointer operand.
382 unsigned getPointerAddressSpace() const {
383 return getPointerOperand()->getType()->getPointerAddressSpace();
386 // Methods for support type inquiry through isa, cast, and dyn_cast:
387 static inline bool classof(const Instruction *I) {
388 return I->getOpcode() == Instruction::Store;
390 static inline bool classof(const Value *V) {
391 return isa<Instruction>(V) && classof(cast<Instruction>(V));
394 // Shadow Instruction::setInstructionSubclassData with a private forwarding
395 // method so that subclasses cannot accidentally use it.
396 void setInstructionSubclassData(unsigned short D) {
397 Instruction::setInstructionSubclassData(D);
402 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
405 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
407 //===----------------------------------------------------------------------===//
409 //===----------------------------------------------------------------------===//
411 /// FenceInst - an instruction for ordering other memory operations
413 class FenceInst : public Instruction {
414 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
415 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
417 FenceInst *clone_impl() const override;
419 // allocate space for exactly zero operands
420 void *operator new(size_t s) {
421 return User::operator new(s, 0);
424 // Ordering may only be Acquire, Release, AcquireRelease, or
425 // SequentiallyConsistent.
426 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
427 SynchronizationScope SynchScope = CrossThread,
428 Instruction *InsertBefore = nullptr);
429 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
430 SynchronizationScope SynchScope,
431 BasicBlock *InsertAtEnd);
433 /// Returns the ordering effect of this fence.
434 AtomicOrdering getOrdering() const {
435 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
438 /// Set the ordering constraint on this fence. May only be Acquire, Release,
439 /// AcquireRelease, or SequentiallyConsistent.
440 void setOrdering(AtomicOrdering Ordering) {
441 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
445 SynchronizationScope getSynchScope() const {
446 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
449 /// Specify whether this fence orders other operations with respect to all
450 /// concurrently executing threads, or only with respect to signal handlers
451 /// executing in the same thread.
452 void setSynchScope(SynchronizationScope xthread) {
453 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
457 // Methods for support type inquiry through isa, cast, and dyn_cast:
458 static inline bool classof(const Instruction *I) {
459 return I->getOpcode() == Instruction::Fence;
461 static inline bool classof(const Value *V) {
462 return isa<Instruction>(V) && classof(cast<Instruction>(V));
465 // Shadow Instruction::setInstructionSubclassData with a private forwarding
466 // method so that subclasses cannot accidentally use it.
467 void setInstructionSubclassData(unsigned short D) {
468 Instruction::setInstructionSubclassData(D);
472 //===----------------------------------------------------------------------===//
473 // AtomicCmpXchgInst Class
474 //===----------------------------------------------------------------------===//
476 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
477 /// specified value is in a memory location, and, if it is, stores a new value
478 /// there. Returns the value that was loaded.
480 class AtomicCmpXchgInst : public Instruction {
481 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
482 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
483 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
484 SynchronizationScope SynchScope);
486 AtomicCmpXchgInst *clone_impl() const override;
488 // allocate space for exactly three operands
489 void *operator new(size_t s) {
490 return User::operator new(s, 3);
492 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
493 AtomicOrdering SuccessOrdering,
494 AtomicOrdering FailureOrdering,
495 SynchronizationScope SynchScope,
496 Instruction *InsertBefore = nullptr);
497 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
498 AtomicOrdering SuccessOrdering,
499 AtomicOrdering FailureOrdering,
500 SynchronizationScope SynchScope,
501 BasicBlock *InsertAtEnd);
503 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
506 bool isVolatile() const {
507 return getSubclassDataFromInstruction() & 1;
510 /// setVolatile - Specify whether this is a volatile cmpxchg.
512 void setVolatile(bool V) {
513 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
517 /// Return true if this cmpxchg may spuriously fail.
518 bool isWeak() const {
519 return getSubclassDataFromInstruction() & 0x100;
522 void setWeak(bool IsWeak) {
523 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
527 /// Transparently provide more efficient getOperand methods.
528 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
530 /// Set the ordering constraint on this cmpxchg.
531 void setSuccessOrdering(AtomicOrdering Ordering) {
532 assert(Ordering != NotAtomic &&
533 "CmpXchg instructions can only be atomic.");
534 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
538 void setFailureOrdering(AtomicOrdering Ordering) {
539 assert(Ordering != NotAtomic &&
540 "CmpXchg instructions can only be atomic.");
541 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
545 /// Specify whether this cmpxchg is atomic and orders other operations with
546 /// respect to all concurrently executing threads, or only with respect to
547 /// signal handlers executing in the same thread.
548 void setSynchScope(SynchronizationScope SynchScope) {
549 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
553 /// Returns the ordering constraint on this cmpxchg.
554 AtomicOrdering getSuccessOrdering() const {
555 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
558 /// Returns the ordering constraint on this cmpxchg.
559 AtomicOrdering getFailureOrdering() const {
560 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
563 /// Returns whether this cmpxchg is atomic between threads or only within a
565 SynchronizationScope getSynchScope() const {
566 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
569 Value *getPointerOperand() { return getOperand(0); }
570 const Value *getPointerOperand() const { return getOperand(0); }
571 static unsigned getPointerOperandIndex() { return 0U; }
573 Value *getCompareOperand() { return getOperand(1); }
574 const Value *getCompareOperand() const { return getOperand(1); }
576 Value *getNewValOperand() { return getOperand(2); }
577 const Value *getNewValOperand() const { return getOperand(2); }
579 /// \brief Returns the address space of the pointer operand.
580 unsigned getPointerAddressSpace() const {
581 return getPointerOperand()->getType()->getPointerAddressSpace();
584 /// \brief Returns the strongest permitted ordering on failure, given the
585 /// desired ordering on success.
587 /// If the comparison in a cmpxchg operation fails, there is no atomic store
588 /// so release semantics cannot be provided. So this function drops explicit
589 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
590 /// operation would remain SequentiallyConsistent.
591 static AtomicOrdering
592 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
593 switch (SuccessOrdering) {
594 default: llvm_unreachable("invalid cmpxchg success ordering");
601 case SequentiallyConsistent:
602 return SequentiallyConsistent;
606 // Methods for support type inquiry through isa, cast, and dyn_cast:
607 static inline bool classof(const Instruction *I) {
608 return I->getOpcode() == Instruction::AtomicCmpXchg;
610 static inline bool classof(const Value *V) {
611 return isa<Instruction>(V) && classof(cast<Instruction>(V));
614 // Shadow Instruction::setInstructionSubclassData with a private forwarding
615 // method so that subclasses cannot accidentally use it.
616 void setInstructionSubclassData(unsigned short D) {
617 Instruction::setInstructionSubclassData(D);
622 struct OperandTraits<AtomicCmpXchgInst> :
623 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
626 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
628 //===----------------------------------------------------------------------===//
629 // AtomicRMWInst Class
630 //===----------------------------------------------------------------------===//
632 /// AtomicRMWInst - an instruction that atomically reads a memory location,
633 /// combines it with another value, and then stores the result back. Returns
636 class AtomicRMWInst : public Instruction {
637 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
639 AtomicRMWInst *clone_impl() const override;
641 /// This enumeration lists the possible modifications atomicrmw can make. In
642 /// the descriptions, 'p' is the pointer to the instruction's memory location,
643 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
644 /// instruction. These instructions always return 'old'.
660 /// *p = old >signed v ? old : v
662 /// *p = old <signed v ? old : v
664 /// *p = old >unsigned v ? old : v
666 /// *p = old <unsigned v ? old : v
674 // allocate space for exactly two operands
675 void *operator new(size_t s) {
676 return User::operator new(s, 2);
678 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
679 AtomicOrdering Ordering, SynchronizationScope SynchScope,
680 Instruction *InsertBefore = nullptr);
681 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
682 AtomicOrdering Ordering, SynchronizationScope SynchScope,
683 BasicBlock *InsertAtEnd);
685 BinOp getOperation() const {
686 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
689 void setOperation(BinOp Operation) {
690 unsigned short SubclassData = getSubclassDataFromInstruction();
691 setInstructionSubclassData((SubclassData & 31) |
695 /// isVolatile - Return true if this is a RMW on a volatile memory location.
697 bool isVolatile() const {
698 return getSubclassDataFromInstruction() & 1;
701 /// setVolatile - Specify whether this is a volatile RMW or not.
703 void setVolatile(bool V) {
704 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
708 /// Transparently provide more efficient getOperand methods.
709 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
711 /// Set the ordering constraint on this RMW.
712 void setOrdering(AtomicOrdering Ordering) {
713 assert(Ordering != NotAtomic &&
714 "atomicrmw instructions can only be atomic.");
715 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
719 /// Specify whether this RMW orders other operations with respect to all
720 /// concurrently executing threads, or only with respect to signal handlers
721 /// executing in the same thread.
722 void setSynchScope(SynchronizationScope SynchScope) {
723 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
727 /// Returns the ordering constraint on this RMW.
728 AtomicOrdering getOrdering() const {
729 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
732 /// Returns whether this RMW is atomic between threads or only within a
734 SynchronizationScope getSynchScope() const {
735 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
738 Value *getPointerOperand() { return getOperand(0); }
739 const Value *getPointerOperand() const { return getOperand(0); }
740 static unsigned getPointerOperandIndex() { return 0U; }
742 Value *getValOperand() { return getOperand(1); }
743 const Value *getValOperand() const { return getOperand(1); }
745 /// \brief Returns the address space of the pointer operand.
746 unsigned getPointerAddressSpace() const {
747 return getPointerOperand()->getType()->getPointerAddressSpace();
750 // Methods for support type inquiry through isa, cast, and dyn_cast:
751 static inline bool classof(const Instruction *I) {
752 return I->getOpcode() == Instruction::AtomicRMW;
754 static inline bool classof(const Value *V) {
755 return isa<Instruction>(V) && classof(cast<Instruction>(V));
758 void Init(BinOp Operation, Value *Ptr, Value *Val,
759 AtomicOrdering Ordering, SynchronizationScope SynchScope);
760 // Shadow Instruction::setInstructionSubclassData with a private forwarding
761 // method so that subclasses cannot accidentally use it.
762 void setInstructionSubclassData(unsigned short D) {
763 Instruction::setInstructionSubclassData(D);
768 struct OperandTraits<AtomicRMWInst>
769 : public FixedNumOperandTraits<AtomicRMWInst,2> {
772 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
774 //===----------------------------------------------------------------------===//
775 // GetElementPtrInst Class
776 //===----------------------------------------------------------------------===//
778 // checkGEPType - Simple wrapper function to give a better assertion failure
779 // message on bad indexes for a gep instruction.
781 inline Type *checkGEPType(Type *Ty) {
782 assert(Ty && "Invalid GetElementPtrInst indices for type!");
786 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
787 /// access elements of arrays and structs
789 class GetElementPtrInst : public Instruction {
790 GetElementPtrInst(const GetElementPtrInst &GEPI);
791 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
793 /// Constructors - Create a getelementptr instruction with a base pointer an
794 /// list of indices. The first ctor can optionally insert before an existing
795 /// instruction, the second appends the new instruction to the specified
797 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
798 unsigned Values, const Twine &NameStr,
799 Instruction *InsertBefore);
800 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
801 unsigned Values, const Twine &NameStr,
802 BasicBlock *InsertAtEnd);
804 GetElementPtrInst *clone_impl() const override;
806 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
807 const Twine &NameStr = "",
808 Instruction *InsertBefore = nullptr) {
809 unsigned Values = 1 + unsigned(IdxList.size());
811 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
813 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
814 const Twine &NameStr,
815 BasicBlock *InsertAtEnd) {
816 unsigned Values = 1 + unsigned(IdxList.size());
818 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
821 /// Create an "inbounds" getelementptr. See the documentation for the
822 /// "inbounds" flag in LangRef.html for details.
823 static GetElementPtrInst *CreateInBounds(Value *Ptr,
824 ArrayRef<Value *> IdxList,
825 const Twine &NameStr = "",
826 Instruction *InsertBefore = nullptr){
827 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
828 GEP->setIsInBounds(true);
831 static GetElementPtrInst *CreateInBounds(Value *Ptr,
832 ArrayRef<Value *> IdxList,
833 const Twine &NameStr,
834 BasicBlock *InsertAtEnd) {
835 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
836 GEP->setIsInBounds(true);
840 /// Transparently provide more efficient getOperand methods.
841 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
843 // getType - Overload to return most specific sequential type.
844 SequentialType *getType() const {
845 return cast<SequentialType>(Instruction::getType());
848 /// \brief Returns the address space of this instruction's pointer type.
849 unsigned getAddressSpace() const {
850 // Note that this is always the same as the pointer operand's address space
851 // and that is cheaper to compute, so cheat here.
852 return getPointerAddressSpace();
855 /// getIndexedType - Returns the type of the element that would be loaded with
856 /// a load instruction with the specified parameters.
858 /// Null is returned if the indices are invalid for the specified
861 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
862 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
863 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
865 inline op_iterator idx_begin() { return op_begin()+1; }
866 inline const_op_iterator idx_begin() const { return op_begin()+1; }
867 inline op_iterator idx_end() { return op_end(); }
868 inline const_op_iterator idx_end() const { return op_end(); }
870 Value *getPointerOperand() {
871 return getOperand(0);
873 const Value *getPointerOperand() const {
874 return getOperand(0);
876 static unsigned getPointerOperandIndex() {
877 return 0U; // get index for modifying correct operand.
880 /// getPointerOperandType - Method to return the pointer operand as a
882 Type *getPointerOperandType() const {
883 return getPointerOperand()->getType();
886 /// \brief Returns the address space of the pointer operand.
887 unsigned getPointerAddressSpace() const {
888 return getPointerOperandType()->getPointerAddressSpace();
891 /// GetGEPReturnType - Returns the pointer type returned by the GEP
892 /// instruction, which may be a vector of pointers.
893 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
894 Type *PtrTy = PointerType::get(checkGEPType(
895 getIndexedType(Ptr->getType(), IdxList)),
896 Ptr->getType()->getPointerAddressSpace());
898 if (Ptr->getType()->isVectorTy()) {
899 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
900 return VectorType::get(PtrTy, NumElem);
907 unsigned getNumIndices() const { // Note: always non-negative
908 return getNumOperands() - 1;
911 bool hasIndices() const {
912 return getNumOperands() > 1;
915 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
916 /// zeros. If so, the result pointer and the first operand have the same
917 /// value, just potentially different types.
918 bool hasAllZeroIndices() const;
920 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
921 /// constant integers. If so, the result pointer and the first operand have
922 /// a constant offset between them.
923 bool hasAllConstantIndices() const;
925 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
926 /// See LangRef.html for the meaning of inbounds on a getelementptr.
927 void setIsInBounds(bool b = true);
929 /// isInBounds - Determine whether the GEP has the inbounds flag.
930 bool isInBounds() const;
932 /// \brief Accumulate the constant address offset of this GEP if possible.
934 /// This routine accepts an APInt into which it will accumulate the constant
935 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
936 /// all-constant, it returns false and the value of the offset APInt is
937 /// undefined (it is *not* preserved!). The APInt passed into this routine
938 /// must be at least as wide as the IntPtr type for the address space of
939 /// the base GEP pointer.
940 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
942 // Methods for support type inquiry through isa, cast, and dyn_cast:
943 static inline bool classof(const Instruction *I) {
944 return (I->getOpcode() == Instruction::GetElementPtr);
946 static inline bool classof(const Value *V) {
947 return isa<Instruction>(V) && classof(cast<Instruction>(V));
952 struct OperandTraits<GetElementPtrInst> :
953 public VariadicOperandTraits<GetElementPtrInst, 1> {
956 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
957 ArrayRef<Value *> IdxList,
959 const Twine &NameStr,
960 Instruction *InsertBefore)
961 : Instruction(getGEPReturnType(Ptr, IdxList),
963 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
964 Values, InsertBefore) {
965 init(Ptr, IdxList, NameStr);
967 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
968 ArrayRef<Value *> IdxList,
970 const Twine &NameStr,
971 BasicBlock *InsertAtEnd)
972 : Instruction(getGEPReturnType(Ptr, IdxList),
974 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
975 Values, InsertAtEnd) {
976 init(Ptr, IdxList, NameStr);
980 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
983 //===----------------------------------------------------------------------===//
985 //===----------------------------------------------------------------------===//
987 /// This instruction compares its operands according to the predicate given
988 /// to the constructor. It only operates on integers or pointers. The operands
989 /// must be identical types.
990 /// \brief Represent an integer comparison operator.
991 class ICmpInst: public CmpInst {
993 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
994 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
995 "Invalid ICmp predicate value");
996 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
997 "Both operands to ICmp instruction are not of the same type!");
998 // Check that the operands are the right type
999 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1000 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1001 "Invalid operand types for ICmp instruction");
1005 /// \brief Clone an identical ICmpInst
1006 ICmpInst *clone_impl() const override;
1008 /// \brief Constructor with insert-before-instruction semantics.
1010 Instruction *InsertBefore, ///< Where to insert
1011 Predicate pred, ///< The predicate to use for the comparison
1012 Value *LHS, ///< The left-hand-side of the expression
1013 Value *RHS, ///< The right-hand-side of the expression
1014 const Twine &NameStr = "" ///< Name of the instruction
1015 ) : CmpInst(makeCmpResultType(LHS->getType()),
1016 Instruction::ICmp, pred, LHS, RHS, NameStr,
1023 /// \brief Constructor with insert-at-end semantics.
1025 BasicBlock &InsertAtEnd, ///< Block to insert into.
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,
1038 /// \brief Constructor with no-insertion semantics
1040 Predicate pred, ///< The predicate to use for the comparison
1041 Value *LHS, ///< The left-hand-side of the expression
1042 Value *RHS, ///< The right-hand-side of the expression
1043 const Twine &NameStr = "" ///< Name of the instruction
1044 ) : CmpInst(makeCmpResultType(LHS->getType()),
1045 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1051 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1052 /// @returns the predicate that would be the result if the operand were
1053 /// regarded as signed.
1054 /// \brief Return the signed version of the predicate
1055 Predicate getSignedPredicate() const {
1056 return getSignedPredicate(getPredicate());
1059 /// This is a static version that you can use without an instruction.
1060 /// \brief Return the signed version of the predicate.
1061 static Predicate getSignedPredicate(Predicate pred);
1063 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1064 /// @returns the predicate that would be the result if the operand were
1065 /// regarded as unsigned.
1066 /// \brief Return the unsigned version of the predicate
1067 Predicate getUnsignedPredicate() const {
1068 return getUnsignedPredicate(getPredicate());
1071 /// This is a static version that you can use without an instruction.
1072 /// \brief Return the unsigned version of the predicate.
1073 static Predicate getUnsignedPredicate(Predicate pred);
1075 /// isEquality - Return true if this predicate is either EQ or NE. This also
1076 /// tests for commutativity.
1077 static bool isEquality(Predicate P) {
1078 return P == ICMP_EQ || P == ICMP_NE;
1081 /// isEquality - Return true if this predicate is either EQ or NE. This also
1082 /// tests for commutativity.
1083 bool isEquality() const {
1084 return isEquality(getPredicate());
1087 /// @returns true if the predicate of this ICmpInst is commutative
1088 /// \brief Determine if this relation is commutative.
1089 bool isCommutative() const { return isEquality(); }
1091 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1093 bool isRelational() const {
1094 return !isEquality();
1097 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1099 static bool isRelational(Predicate P) {
1100 return !isEquality(P);
1103 /// Initialize a set of values that all satisfy the predicate with C.
1104 /// \brief Make a ConstantRange for a relation with a constant value.
1105 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1107 /// Exchange the two operands to this instruction in such a way that it does
1108 /// not modify the semantics of the instruction. The predicate value may be
1109 /// changed to retain the same result if the predicate is order dependent
1111 /// \brief Swap operands and adjust predicate.
1112 void swapOperands() {
1113 setPredicate(getSwappedPredicate());
1114 Op<0>().swap(Op<1>());
1117 // Methods for support type inquiry through isa, cast, and dyn_cast:
1118 static inline bool classof(const Instruction *I) {
1119 return I->getOpcode() == Instruction::ICmp;
1121 static inline bool classof(const Value *V) {
1122 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1127 //===----------------------------------------------------------------------===//
1129 //===----------------------------------------------------------------------===//
1131 /// This instruction compares its operands according to the predicate given
1132 /// to the constructor. It only operates on floating point values or packed
1133 /// vectors of floating point values. The operands must be identical types.
1134 /// \brief Represents a floating point comparison operator.
1135 class FCmpInst: public CmpInst {
1137 /// \brief Clone an identical FCmpInst
1138 FCmpInst *clone_impl() const override;
1140 /// \brief Constructor with insert-before-instruction semantics.
1142 Instruction *InsertBefore, ///< Where to insert
1143 Predicate pred, ///< The predicate to use for the comparison
1144 Value *LHS, ///< The left-hand-side of the expression
1145 Value *RHS, ///< The right-hand-side of the expression
1146 const Twine &NameStr = "" ///< Name of the instruction
1147 ) : CmpInst(makeCmpResultType(LHS->getType()),
1148 Instruction::FCmp, pred, LHS, RHS, NameStr,
1150 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1151 "Invalid FCmp predicate value");
1152 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1153 "Both operands to FCmp instruction are not of the same type!");
1154 // Check that the operands are the right type
1155 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1156 "Invalid operand types for FCmp instruction");
1159 /// \brief Constructor with insert-at-end semantics.
1161 BasicBlock &InsertAtEnd, ///< Block to insert into.
1162 Predicate pred, ///< The predicate to use for the comparison
1163 Value *LHS, ///< The left-hand-side of the expression
1164 Value *RHS, ///< The right-hand-side of the expression
1165 const Twine &NameStr = "" ///< Name of the instruction
1166 ) : CmpInst(makeCmpResultType(LHS->getType()),
1167 Instruction::FCmp, pred, LHS, RHS, NameStr,
1169 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1170 "Invalid FCmp predicate value");
1171 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1172 "Both operands to FCmp instruction are not of the same type!");
1173 // Check that the operands are the right type
1174 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1175 "Invalid operand types for FCmp instruction");
1178 /// \brief Constructor with no-insertion semantics
1180 Predicate pred, ///< The predicate to use for the comparison
1181 Value *LHS, ///< The left-hand-side of the expression
1182 Value *RHS, ///< The right-hand-side of the expression
1183 const Twine &NameStr = "" ///< Name of the instruction
1184 ) : CmpInst(makeCmpResultType(LHS->getType()),
1185 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1186 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1187 "Invalid FCmp predicate value");
1188 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1189 "Both operands to FCmp instruction are not of the same type!");
1190 // Check that the operands are the right type
1191 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1192 "Invalid operand types for FCmp instruction");
1195 /// @returns true if the predicate of this instruction is EQ or NE.
1196 /// \brief Determine if this is an equality predicate.
1197 bool isEquality() const {
1198 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1199 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1202 /// @returns true if the predicate of this instruction is commutative.
1203 /// \brief Determine if this is a commutative predicate.
1204 bool isCommutative() const {
1205 return isEquality() ||
1206 getPredicate() == FCMP_FALSE ||
1207 getPredicate() == FCMP_TRUE ||
1208 getPredicate() == FCMP_ORD ||
1209 getPredicate() == FCMP_UNO;
1212 /// @returns true if the predicate is relational (not EQ or NE).
1213 /// \brief Determine if this a relational predicate.
1214 bool isRelational() const { return !isEquality(); }
1216 /// Exchange the two operands to this instruction in such a way that it does
1217 /// not modify the semantics of the instruction. The predicate value may be
1218 /// changed to retain the same result if the predicate is order dependent
1220 /// \brief Swap operands and adjust predicate.
1221 void swapOperands() {
1222 setPredicate(getSwappedPredicate());
1223 Op<0>().swap(Op<1>());
1226 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1227 static inline bool classof(const Instruction *I) {
1228 return I->getOpcode() == Instruction::FCmp;
1230 static inline bool classof(const Value *V) {
1231 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1235 //===----------------------------------------------------------------------===//
1236 /// CallInst - This class represents a function call, abstracting a target
1237 /// machine's calling convention. This class uses low bit of the SubClassData
1238 /// field to indicate whether or not this is a tail call. The rest of the bits
1239 /// hold the calling convention of the call.
1241 class CallInst : public Instruction {
1242 AttributeSet AttributeList; ///< parameter attributes for call
1243 CallInst(const CallInst &CI);
1244 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1245 void init(Value *Func, const Twine &NameStr);
1247 /// Construct a CallInst given a range of arguments.
1248 /// \brief Construct a CallInst from a range of arguments
1249 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1250 const Twine &NameStr, Instruction *InsertBefore);
1252 /// Construct a CallInst given a range of arguments.
1253 /// \brief Construct a CallInst from a range of arguments
1254 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1255 const Twine &NameStr, BasicBlock *InsertAtEnd);
1257 explicit CallInst(Value *F, const Twine &NameStr,
1258 Instruction *InsertBefore);
1259 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1261 CallInst *clone_impl() const override;
1263 static CallInst *Create(Value *Func,
1264 ArrayRef<Value *> Args,
1265 const Twine &NameStr = "",
1266 Instruction *InsertBefore = nullptr) {
1267 return new(unsigned(Args.size() + 1))
1268 CallInst(Func, Args, NameStr, InsertBefore);
1270 static CallInst *Create(Value *Func,
1271 ArrayRef<Value *> Args,
1272 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1273 return new(unsigned(Args.size() + 1))
1274 CallInst(Func, Args, NameStr, InsertAtEnd);
1276 static CallInst *Create(Value *F, const Twine &NameStr = "",
1277 Instruction *InsertBefore = nullptr) {
1278 return new(1) CallInst(F, NameStr, InsertBefore);
1280 static CallInst *Create(Value *F, const Twine &NameStr,
1281 BasicBlock *InsertAtEnd) {
1282 return new(1) CallInst(F, NameStr, InsertAtEnd);
1284 /// CreateMalloc - Generate the IR for a call to malloc:
1285 /// 1. Compute the malloc call's argument as the specified type's size,
1286 /// possibly multiplied by the array size if the array size is not
1288 /// 2. Call malloc with that argument.
1289 /// 3. Bitcast the result of the malloc call to the specified type.
1290 static Instruction *CreateMalloc(Instruction *InsertBefore,
1291 Type *IntPtrTy, Type *AllocTy,
1292 Value *AllocSize, Value *ArraySize = nullptr,
1293 Function* MallocF = nullptr,
1294 const Twine &Name = "");
1295 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1296 Type *IntPtrTy, Type *AllocTy,
1297 Value *AllocSize, Value *ArraySize = nullptr,
1298 Function* MallocF = nullptr,
1299 const Twine &Name = "");
1300 /// CreateFree - Generate the IR for a call to the builtin free function.
1301 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1302 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1306 // Note that 'musttail' implies 'tail'.
1307 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1308 TailCallKind getTailCallKind() const {
1309 return TailCallKind(getSubclassDataFromInstruction() & 3);
1311 bool isTailCall() const {
1312 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1314 bool isMustTailCall() const {
1315 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1317 void setTailCall(bool isTC = true) {
1318 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1319 unsigned(isTC ? TCK_Tail : TCK_None));
1321 void setTailCallKind(TailCallKind TCK) {
1322 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1326 /// Provide fast operand accessors
1327 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1329 /// getNumArgOperands - Return the number of call arguments.
1331 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1333 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1335 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1336 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1338 /// arg_operands - iteration adapter for range-for loops.
1339 iterator_range<op_iterator> arg_operands() {
1340 // The last operand in the op list is the callee - it's not one of the args
1341 // so we don't want to iterate over it.
1342 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1345 /// arg_operands - iteration adapter for range-for loops.
1346 iterator_range<const_op_iterator> arg_operands() const {
1347 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1350 /// \brief Wrappers for getting the \c Use of a call argument.
1351 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1352 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1354 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1356 CallingConv::ID getCallingConv() const {
1357 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1359 void setCallingConv(CallingConv::ID CC) {
1360 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1361 (static_cast<unsigned>(CC) << 2));
1364 /// getAttributes - Return the parameter attributes for this call.
1366 const AttributeSet &getAttributes() const { return AttributeList; }
1368 /// setAttributes - Set the parameter attributes for this call.
1370 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1372 /// addAttribute - adds the attribute to the list of attributes.
1373 void addAttribute(unsigned i, Attribute::AttrKind attr);
1375 /// removeAttribute - removes the attribute from the list of attributes.
1376 void removeAttribute(unsigned i, Attribute attr);
1378 /// \brief Determine whether this call has the given attribute.
1379 bool hasFnAttr(Attribute::AttrKind A) const {
1380 assert(A != Attribute::NoBuiltin &&
1381 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1382 return hasFnAttrImpl(A);
1385 /// \brief Determine whether the call or the callee has the given attributes.
1386 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1388 /// \brief Extract the alignment for a call or parameter (0=unknown).
1389 unsigned getParamAlignment(unsigned i) const {
1390 return AttributeList.getParamAlignment(i);
1393 /// \brief Extract the number of dereferenceable bytes for a call or
1394 /// parameter (0=unknown).
1395 uint64_t getDereferenceableBytes(unsigned i) const {
1396 return AttributeList.getDereferenceableBytes(i);
1399 /// \brief Return true if the call should not be treated as a call to a
1401 bool isNoBuiltin() const {
1402 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1403 !hasFnAttrImpl(Attribute::Builtin);
1406 /// \brief Return true if the call should not be inlined.
1407 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1408 void setIsNoInline() {
1409 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1412 /// \brief Return true if the call can return twice
1413 bool canReturnTwice() const {
1414 return hasFnAttr(Attribute::ReturnsTwice);
1416 void setCanReturnTwice() {
1417 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1420 /// \brief Determine if the call does not access memory.
1421 bool doesNotAccessMemory() const {
1422 return hasFnAttr(Attribute::ReadNone);
1424 void setDoesNotAccessMemory() {
1425 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1428 /// \brief Determine if the call does not access or only reads memory.
1429 bool onlyReadsMemory() const {
1430 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1432 void setOnlyReadsMemory() {
1433 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1436 /// \brief Determine if the call cannot return.
1437 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1438 void setDoesNotReturn() {
1439 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1442 /// \brief Determine if the call cannot unwind.
1443 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1444 void setDoesNotThrow() {
1445 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1448 /// \brief Determine if the call cannot be duplicated.
1449 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1450 void setCannotDuplicate() {
1451 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1454 /// \brief Determine if the call returns a structure through first
1455 /// pointer argument.
1456 bool hasStructRetAttr() const {
1457 // Be friendly and also check the callee.
1458 return paramHasAttr(1, Attribute::StructRet);
1461 /// \brief Determine if any call argument is an aggregate passed by value.
1462 bool hasByValArgument() const {
1463 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1466 /// getCalledFunction - Return the function called, or null if this is an
1467 /// indirect function invocation.
1469 Function *getCalledFunction() const {
1470 return dyn_cast<Function>(Op<-1>());
1473 /// getCalledValue - Get a pointer to the function that is invoked by this
1475 const Value *getCalledValue() const { return Op<-1>(); }
1476 Value *getCalledValue() { return Op<-1>(); }
1478 /// setCalledFunction - Set the function called.
1479 void setCalledFunction(Value* Fn) {
1483 /// isInlineAsm - Check if this call is an inline asm statement.
1484 bool isInlineAsm() const {
1485 return isa<InlineAsm>(Op<-1>());
1488 // Methods for support type inquiry through isa, cast, and dyn_cast:
1489 static inline bool classof(const Instruction *I) {
1490 return I->getOpcode() == Instruction::Call;
1492 static inline bool classof(const Value *V) {
1493 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1497 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1499 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1500 // method so that subclasses cannot accidentally use it.
1501 void setInstructionSubclassData(unsigned short D) {
1502 Instruction::setInstructionSubclassData(D);
1507 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1510 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1511 const Twine &NameStr, BasicBlock *InsertAtEnd)
1512 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1513 ->getElementType())->getReturnType(),
1515 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1516 unsigned(Args.size() + 1), InsertAtEnd) {
1517 init(Func, Args, NameStr);
1520 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1521 const Twine &NameStr, Instruction *InsertBefore)
1522 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1523 ->getElementType())->getReturnType(),
1525 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1526 unsigned(Args.size() + 1), InsertBefore) {
1527 init(Func, Args, NameStr);
1531 // Note: if you get compile errors about private methods then
1532 // please update your code to use the high-level operand
1533 // interfaces. See line 943 above.
1534 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1536 //===----------------------------------------------------------------------===//
1538 //===----------------------------------------------------------------------===//
1540 /// SelectInst - This class represents the LLVM 'select' instruction.
1542 class SelectInst : public Instruction {
1543 void init(Value *C, Value *S1, Value *S2) {
1544 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1550 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1551 Instruction *InsertBefore)
1552 : Instruction(S1->getType(), Instruction::Select,
1553 &Op<0>(), 3, InsertBefore) {
1557 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1558 BasicBlock *InsertAtEnd)
1559 : Instruction(S1->getType(), Instruction::Select,
1560 &Op<0>(), 3, InsertAtEnd) {
1565 SelectInst *clone_impl() const override;
1567 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1568 const Twine &NameStr = "",
1569 Instruction *InsertBefore = nullptr) {
1570 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1572 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1573 const Twine &NameStr,
1574 BasicBlock *InsertAtEnd) {
1575 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1578 const Value *getCondition() const { return Op<0>(); }
1579 const Value *getTrueValue() const { return Op<1>(); }
1580 const Value *getFalseValue() const { return Op<2>(); }
1581 Value *getCondition() { return Op<0>(); }
1582 Value *getTrueValue() { return Op<1>(); }
1583 Value *getFalseValue() { return Op<2>(); }
1585 /// areInvalidOperands - Return a string if the specified operands are invalid
1586 /// for a select operation, otherwise return null.
1587 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1589 /// Transparently provide more efficient getOperand methods.
1590 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1592 OtherOps getOpcode() const {
1593 return static_cast<OtherOps>(Instruction::getOpcode());
1596 // Methods for support type inquiry through isa, cast, and dyn_cast:
1597 static inline bool classof(const Instruction *I) {
1598 return I->getOpcode() == Instruction::Select;
1600 static inline bool classof(const Value *V) {
1601 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1606 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1609 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1611 //===----------------------------------------------------------------------===//
1613 //===----------------------------------------------------------------------===//
1615 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1616 /// an argument of the specified type given a va_list and increments that list
1618 class VAArgInst : public UnaryInstruction {
1620 VAArgInst *clone_impl() const override;
1623 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1624 Instruction *InsertBefore = nullptr)
1625 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1628 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1629 BasicBlock *InsertAtEnd)
1630 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1634 Value *getPointerOperand() { return getOperand(0); }
1635 const Value *getPointerOperand() const { return getOperand(0); }
1636 static unsigned getPointerOperandIndex() { return 0U; }
1638 // Methods for support type inquiry through isa, cast, and dyn_cast:
1639 static inline bool classof(const Instruction *I) {
1640 return I->getOpcode() == VAArg;
1642 static inline bool classof(const Value *V) {
1643 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1647 //===----------------------------------------------------------------------===//
1648 // ExtractElementInst Class
1649 //===----------------------------------------------------------------------===//
1651 /// ExtractElementInst - This instruction extracts a single (scalar)
1652 /// element from a VectorType value
1654 class ExtractElementInst : public Instruction {
1655 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1656 Instruction *InsertBefore = nullptr);
1657 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1658 BasicBlock *InsertAtEnd);
1660 ExtractElementInst *clone_impl() const override;
1663 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1664 const Twine &NameStr = "",
1665 Instruction *InsertBefore = nullptr) {
1666 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1668 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1669 const Twine &NameStr,
1670 BasicBlock *InsertAtEnd) {
1671 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1674 /// isValidOperands - Return true if an extractelement instruction can be
1675 /// formed with the specified operands.
1676 static bool isValidOperands(const Value *Vec, const Value *Idx);
1678 Value *getVectorOperand() { return Op<0>(); }
1679 Value *getIndexOperand() { return Op<1>(); }
1680 const Value *getVectorOperand() const { return Op<0>(); }
1681 const Value *getIndexOperand() const { return Op<1>(); }
1683 VectorType *getVectorOperandType() const {
1684 return cast<VectorType>(getVectorOperand()->getType());
1688 /// Transparently provide more efficient getOperand methods.
1689 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1691 // Methods for support type inquiry through isa, cast, and dyn_cast:
1692 static inline bool classof(const Instruction *I) {
1693 return I->getOpcode() == Instruction::ExtractElement;
1695 static inline bool classof(const Value *V) {
1696 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1701 struct OperandTraits<ExtractElementInst> :
1702 public FixedNumOperandTraits<ExtractElementInst, 2> {
1705 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1707 //===----------------------------------------------------------------------===//
1708 // InsertElementInst Class
1709 //===----------------------------------------------------------------------===//
1711 /// InsertElementInst - This instruction inserts a single (scalar)
1712 /// element into a VectorType value
1714 class InsertElementInst : public Instruction {
1715 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1716 const Twine &NameStr = "",
1717 Instruction *InsertBefore = nullptr);
1718 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1719 const Twine &NameStr, BasicBlock *InsertAtEnd);
1721 InsertElementInst *clone_impl() const override;
1724 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1725 const Twine &NameStr = "",
1726 Instruction *InsertBefore = nullptr) {
1727 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1729 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1730 const Twine &NameStr,
1731 BasicBlock *InsertAtEnd) {
1732 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1735 /// isValidOperands - Return true if an insertelement instruction can be
1736 /// formed with the specified operands.
1737 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1740 /// getType - Overload to return most specific vector type.
1742 VectorType *getType() const {
1743 return cast<VectorType>(Instruction::getType());
1746 /// Transparently provide more efficient getOperand methods.
1747 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1749 // Methods for support type inquiry through isa, cast, and dyn_cast:
1750 static inline bool classof(const Instruction *I) {
1751 return I->getOpcode() == Instruction::InsertElement;
1753 static inline bool classof(const Value *V) {
1754 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1759 struct OperandTraits<InsertElementInst> :
1760 public FixedNumOperandTraits<InsertElementInst, 3> {
1763 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1765 //===----------------------------------------------------------------------===//
1766 // ShuffleVectorInst Class
1767 //===----------------------------------------------------------------------===//
1769 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1772 class ShuffleVectorInst : public Instruction {
1774 ShuffleVectorInst *clone_impl() const override;
1777 // allocate space for exactly three operands
1778 void *operator new(size_t s) {
1779 return User::operator new(s, 3);
1781 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1782 const Twine &NameStr = "",
1783 Instruction *InsertBefor = nullptr);
1784 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1785 const Twine &NameStr, BasicBlock *InsertAtEnd);
1787 /// isValidOperands - Return true if a shufflevector instruction can be
1788 /// formed with the specified operands.
1789 static bool isValidOperands(const Value *V1, const Value *V2,
1792 /// getType - Overload to return most specific vector type.
1794 VectorType *getType() const {
1795 return cast<VectorType>(Instruction::getType());
1798 /// Transparently provide more efficient getOperand methods.
1799 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1801 Constant *getMask() const {
1802 return cast<Constant>(getOperand(2));
1805 /// getMaskValue - Return the index from the shuffle mask for the specified
1806 /// output result. This is either -1 if the element is undef or a number less
1807 /// than 2*numelements.
1808 static int getMaskValue(Constant *Mask, unsigned i);
1810 int getMaskValue(unsigned i) const {
1811 return getMaskValue(getMask(), i);
1814 /// getShuffleMask - Return the full mask for this instruction, where each
1815 /// element is the element number and undef's are returned as -1.
1816 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1818 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1819 return getShuffleMask(getMask(), Result);
1822 SmallVector<int, 16> getShuffleMask() const {
1823 SmallVector<int, 16> Mask;
1824 getShuffleMask(Mask);
1829 // Methods for support type inquiry through isa, cast, and dyn_cast:
1830 static inline bool classof(const Instruction *I) {
1831 return I->getOpcode() == Instruction::ShuffleVector;
1833 static inline bool classof(const Value *V) {
1834 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1839 struct OperandTraits<ShuffleVectorInst> :
1840 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1843 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1845 //===----------------------------------------------------------------------===//
1846 // ExtractValueInst Class
1847 //===----------------------------------------------------------------------===//
1849 /// ExtractValueInst - This instruction extracts a struct member or array
1850 /// element value from an aggregate value.
1852 class ExtractValueInst : public UnaryInstruction {
1853 SmallVector<unsigned, 4> Indices;
1855 ExtractValueInst(const ExtractValueInst &EVI);
1856 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1858 /// Constructors - Create a extractvalue instruction with a base aggregate
1859 /// value and a list of indices. The first ctor can optionally insert before
1860 /// an existing instruction, the second appends the new instruction to the
1861 /// specified BasicBlock.
1862 inline ExtractValueInst(Value *Agg,
1863 ArrayRef<unsigned> Idxs,
1864 const Twine &NameStr,
1865 Instruction *InsertBefore);
1866 inline ExtractValueInst(Value *Agg,
1867 ArrayRef<unsigned> Idxs,
1868 const Twine &NameStr, BasicBlock *InsertAtEnd);
1870 // allocate space for exactly one operand
1871 void *operator new(size_t s) {
1872 return User::operator new(s, 1);
1875 ExtractValueInst *clone_impl() const override;
1878 static ExtractValueInst *Create(Value *Agg,
1879 ArrayRef<unsigned> Idxs,
1880 const Twine &NameStr = "",
1881 Instruction *InsertBefore = nullptr) {
1883 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1885 static ExtractValueInst *Create(Value *Agg,
1886 ArrayRef<unsigned> Idxs,
1887 const Twine &NameStr,
1888 BasicBlock *InsertAtEnd) {
1889 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1892 /// getIndexedType - Returns the type of the element that would be extracted
1893 /// with an extractvalue instruction with the specified parameters.
1895 /// Null is returned if the indices are invalid for the specified type.
1896 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1898 typedef const unsigned* idx_iterator;
1899 inline idx_iterator idx_begin() const { return Indices.begin(); }
1900 inline idx_iterator idx_end() const { return Indices.end(); }
1902 Value *getAggregateOperand() {
1903 return getOperand(0);
1905 const Value *getAggregateOperand() const {
1906 return getOperand(0);
1908 static unsigned getAggregateOperandIndex() {
1909 return 0U; // get index for modifying correct operand
1912 ArrayRef<unsigned> getIndices() const {
1916 unsigned getNumIndices() const {
1917 return (unsigned)Indices.size();
1920 bool hasIndices() const {
1924 // Methods for support type inquiry through isa, cast, and dyn_cast:
1925 static inline bool classof(const Instruction *I) {
1926 return I->getOpcode() == Instruction::ExtractValue;
1928 static inline bool classof(const Value *V) {
1929 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1933 ExtractValueInst::ExtractValueInst(Value *Agg,
1934 ArrayRef<unsigned> Idxs,
1935 const Twine &NameStr,
1936 Instruction *InsertBefore)
1937 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1938 ExtractValue, Agg, InsertBefore) {
1939 init(Idxs, NameStr);
1941 ExtractValueInst::ExtractValueInst(Value *Agg,
1942 ArrayRef<unsigned> Idxs,
1943 const Twine &NameStr,
1944 BasicBlock *InsertAtEnd)
1945 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1946 ExtractValue, Agg, InsertAtEnd) {
1947 init(Idxs, NameStr);
1951 //===----------------------------------------------------------------------===//
1952 // InsertValueInst Class
1953 //===----------------------------------------------------------------------===//
1955 /// InsertValueInst - This instruction inserts a struct field of array element
1956 /// value into an aggregate value.
1958 class InsertValueInst : public Instruction {
1959 SmallVector<unsigned, 4> Indices;
1961 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1962 InsertValueInst(const InsertValueInst &IVI);
1963 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1964 const Twine &NameStr);
1966 /// Constructors - Create a insertvalue instruction with a base aggregate
1967 /// value, a value to insert, and a list of indices. The first ctor can
1968 /// optionally insert before an existing instruction, the second appends
1969 /// the new instruction to the specified BasicBlock.
1970 inline InsertValueInst(Value *Agg, Value *Val,
1971 ArrayRef<unsigned> Idxs,
1972 const Twine &NameStr,
1973 Instruction *InsertBefore);
1974 inline InsertValueInst(Value *Agg, Value *Val,
1975 ArrayRef<unsigned> Idxs,
1976 const Twine &NameStr, BasicBlock *InsertAtEnd);
1978 /// Constructors - These two constructors are convenience methods because one
1979 /// and two index insertvalue instructions are so common.
1980 InsertValueInst(Value *Agg, Value *Val,
1981 unsigned Idx, const Twine &NameStr = "",
1982 Instruction *InsertBefore = nullptr);
1983 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1984 const Twine &NameStr, BasicBlock *InsertAtEnd);
1986 InsertValueInst *clone_impl() const override;
1988 // allocate space for exactly two operands
1989 void *operator new(size_t s) {
1990 return User::operator new(s, 2);
1993 static InsertValueInst *Create(Value *Agg, Value *Val,
1994 ArrayRef<unsigned> Idxs,
1995 const Twine &NameStr = "",
1996 Instruction *InsertBefore = nullptr) {
1997 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1999 static InsertValueInst *Create(Value *Agg, Value *Val,
2000 ArrayRef<unsigned> Idxs,
2001 const Twine &NameStr,
2002 BasicBlock *InsertAtEnd) {
2003 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2006 /// Transparently provide more efficient getOperand methods.
2007 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2009 typedef const unsigned* idx_iterator;
2010 inline idx_iterator idx_begin() const { return Indices.begin(); }
2011 inline idx_iterator idx_end() const { return Indices.end(); }
2013 Value *getAggregateOperand() {
2014 return getOperand(0);
2016 const Value *getAggregateOperand() const {
2017 return getOperand(0);
2019 static unsigned getAggregateOperandIndex() {
2020 return 0U; // get index for modifying correct operand
2023 Value *getInsertedValueOperand() {
2024 return getOperand(1);
2026 const Value *getInsertedValueOperand() const {
2027 return getOperand(1);
2029 static unsigned getInsertedValueOperandIndex() {
2030 return 1U; // get index for modifying correct operand
2033 ArrayRef<unsigned> getIndices() const {
2037 unsigned getNumIndices() const {
2038 return (unsigned)Indices.size();
2041 bool hasIndices() const {
2045 // Methods for support type inquiry through isa, cast, and dyn_cast:
2046 static inline bool classof(const Instruction *I) {
2047 return I->getOpcode() == Instruction::InsertValue;
2049 static inline bool classof(const Value *V) {
2050 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2055 struct OperandTraits<InsertValueInst> :
2056 public FixedNumOperandTraits<InsertValueInst, 2> {
2059 InsertValueInst::InsertValueInst(Value *Agg,
2061 ArrayRef<unsigned> Idxs,
2062 const Twine &NameStr,
2063 Instruction *InsertBefore)
2064 : Instruction(Agg->getType(), InsertValue,
2065 OperandTraits<InsertValueInst>::op_begin(this),
2067 init(Agg, Val, Idxs, NameStr);
2069 InsertValueInst::InsertValueInst(Value *Agg,
2071 ArrayRef<unsigned> Idxs,
2072 const Twine &NameStr,
2073 BasicBlock *InsertAtEnd)
2074 : Instruction(Agg->getType(), InsertValue,
2075 OperandTraits<InsertValueInst>::op_begin(this),
2077 init(Agg, Val, Idxs, NameStr);
2080 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2082 //===----------------------------------------------------------------------===//
2084 //===----------------------------------------------------------------------===//
2086 // PHINode - The PHINode class is used to represent the magical mystical PHI
2087 // node, that can not exist in nature, but can be synthesized in a computer
2088 // scientist's overactive imagination.
2090 class PHINode : public Instruction {
2091 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2092 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2093 /// the number actually in use.
2094 unsigned ReservedSpace;
2095 PHINode(const PHINode &PN);
2096 // allocate space for exactly zero operands
2097 void *operator new(size_t s) {
2098 return User::operator new(s, 0);
2100 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2101 const Twine &NameStr = "",
2102 Instruction *InsertBefore = nullptr)
2103 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2104 ReservedSpace(NumReservedValues) {
2106 OperandList = allocHungoffUses(ReservedSpace);
2109 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2110 BasicBlock *InsertAtEnd)
2111 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2112 ReservedSpace(NumReservedValues) {
2114 OperandList = allocHungoffUses(ReservedSpace);
2117 // allocHungoffUses - this is more complicated than the generic
2118 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2119 // values and pointers to the incoming blocks, all in one allocation.
2120 Use *allocHungoffUses(unsigned) const;
2122 PHINode *clone_impl() const override;
2124 /// Constructors - NumReservedValues is a hint for the number of incoming
2125 /// edges that this phi node will have (use 0 if you really have no idea).
2126 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2127 const Twine &NameStr = "",
2128 Instruction *InsertBefore = nullptr) {
2129 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2131 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2132 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2133 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2137 /// Provide fast operand accessors
2138 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2140 // Block iterator interface. This provides access to the list of incoming
2141 // basic blocks, which parallels the list of incoming values.
2143 typedef BasicBlock **block_iterator;
2144 typedef BasicBlock * const *const_block_iterator;
2146 block_iterator block_begin() {
2148 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2149 return reinterpret_cast<block_iterator>(ref + 1);
2152 const_block_iterator block_begin() const {
2153 const Use::UserRef *ref =
2154 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2155 return reinterpret_cast<const_block_iterator>(ref + 1);
2158 block_iterator block_end() {
2159 return block_begin() + getNumOperands();
2162 const_block_iterator block_end() const {
2163 return block_begin() + getNumOperands();
2166 /// getNumIncomingValues - Return the number of incoming edges
2168 unsigned getNumIncomingValues() const { return getNumOperands(); }
2170 /// getIncomingValue - Return incoming value number x
2172 Value *getIncomingValue(unsigned i) const {
2173 return getOperand(i);
2175 void setIncomingValue(unsigned i, Value *V) {
2178 static unsigned getOperandNumForIncomingValue(unsigned i) {
2181 static unsigned getIncomingValueNumForOperand(unsigned i) {
2185 /// getIncomingBlock - Return incoming basic block number @p i.
2187 BasicBlock *getIncomingBlock(unsigned i) const {
2188 return block_begin()[i];
2191 /// getIncomingBlock - Return incoming basic block corresponding
2192 /// to an operand of the PHI.
2194 BasicBlock *getIncomingBlock(const Use &U) const {
2195 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2196 return getIncomingBlock(unsigned(&U - op_begin()));
2199 /// getIncomingBlock - Return incoming basic block corresponding
2200 /// to value use iterator.
2202 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2203 return getIncomingBlock(I.getUse());
2206 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2207 block_begin()[i] = BB;
2210 /// addIncoming - Add an incoming value to the end of the PHI list
2212 void addIncoming(Value *V, BasicBlock *BB) {
2213 assert(V && "PHI node got a null value!");
2214 assert(BB && "PHI node got a null basic block!");
2215 assert(getType() == V->getType() &&
2216 "All operands to PHI node must be the same type as the PHI node!");
2217 if (NumOperands == ReservedSpace)
2218 growOperands(); // Get more space!
2219 // Initialize some new operands.
2221 setIncomingValue(NumOperands - 1, V);
2222 setIncomingBlock(NumOperands - 1, BB);
2225 /// removeIncomingValue - Remove an incoming value. This is useful if a
2226 /// predecessor basic block is deleted. The value removed is returned.
2228 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2229 /// is true), the PHI node is destroyed and any uses of it are replaced with
2230 /// dummy values. The only time there should be zero incoming values to a PHI
2231 /// node is when the block is dead, so this strategy is sound.
2233 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2235 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2236 int Idx = getBasicBlockIndex(BB);
2237 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2238 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2241 /// getBasicBlockIndex - Return the first index of the specified basic
2242 /// block in the value list for this PHI. Returns -1 if no instance.
2244 int getBasicBlockIndex(const BasicBlock *BB) const {
2245 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2246 if (block_begin()[i] == BB)
2251 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2252 int Idx = getBasicBlockIndex(BB);
2253 assert(Idx >= 0 && "Invalid basic block argument!");
2254 return getIncomingValue(Idx);
2257 /// hasConstantValue - If the specified PHI node always merges together the
2258 /// same value, return the value, otherwise return null.
2259 Value *hasConstantValue() const;
2261 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2262 static inline bool classof(const Instruction *I) {
2263 return I->getOpcode() == Instruction::PHI;
2265 static inline bool classof(const Value *V) {
2266 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2269 void growOperands();
2273 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2276 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2278 //===----------------------------------------------------------------------===//
2279 // LandingPadInst Class
2280 //===----------------------------------------------------------------------===//
2282 //===---------------------------------------------------------------------------
2283 /// LandingPadInst - The landingpad instruction holds all of the information
2284 /// necessary to generate correct exception handling. The landingpad instruction
2285 /// cannot be moved from the top of a landing pad block, which itself is
2286 /// accessible only from the 'unwind' edge of an invoke. This uses the
2287 /// SubclassData field in Value to store whether or not the landingpad is a
2290 class LandingPadInst : public Instruction {
2291 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2292 /// the number actually in use.
2293 unsigned ReservedSpace;
2294 LandingPadInst(const LandingPadInst &LP);
2296 enum ClauseType { Catch, Filter };
2298 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2299 // Allocate space for exactly zero operands.
2300 void *operator new(size_t s) {
2301 return User::operator new(s, 0);
2303 void growOperands(unsigned Size);
2304 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2306 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2307 unsigned NumReservedValues, const Twine &NameStr,
2308 Instruction *InsertBefore);
2309 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2310 unsigned NumReservedValues, const Twine &NameStr,
2311 BasicBlock *InsertAtEnd);
2313 LandingPadInst *clone_impl() const override;
2315 /// Constructors - NumReservedClauses is a hint for the number of incoming
2316 /// clauses that this landingpad will have (use 0 if you really have no idea).
2317 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2318 unsigned NumReservedClauses,
2319 const Twine &NameStr = "",
2320 Instruction *InsertBefore = nullptr);
2321 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2322 unsigned NumReservedClauses,
2323 const Twine &NameStr, BasicBlock *InsertAtEnd);
2326 /// Provide fast operand accessors
2327 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2329 /// getPersonalityFn - Get the personality function associated with this
2331 Value *getPersonalityFn() const { return getOperand(0); }
2333 /// isCleanup - Return 'true' if this landingpad instruction is a
2334 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2335 /// doesn't catch the exception.
2336 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2338 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2339 void setCleanup(bool V) {
2340 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2344 /// Add a catch or filter clause to the landing pad.
2345 void addClause(Constant *ClauseVal);
2347 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2348 /// determine what type of clause this is.
2349 Constant *getClause(unsigned Idx) const {
2350 return cast<Constant>(OperandList[Idx + 1]);
2353 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2354 bool isCatch(unsigned Idx) const {
2355 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2358 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2359 bool isFilter(unsigned Idx) const {
2360 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2363 /// getNumClauses - Get the number of clauses for this landing pad.
2364 unsigned getNumClauses() const { return getNumOperands() - 1; }
2366 /// reserveClauses - Grow the size of the operand list to accommodate the new
2367 /// number of clauses.
2368 void reserveClauses(unsigned Size) { growOperands(Size); }
2370 // Methods for support type inquiry through isa, cast, and dyn_cast:
2371 static inline bool classof(const Instruction *I) {
2372 return I->getOpcode() == Instruction::LandingPad;
2374 static inline bool classof(const Value *V) {
2375 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2380 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2383 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2385 //===----------------------------------------------------------------------===//
2387 //===----------------------------------------------------------------------===//
2389 //===---------------------------------------------------------------------------
2390 /// ReturnInst - Return a value (possibly void), from a function. Execution
2391 /// does not continue in this function any longer.
2393 class ReturnInst : public TerminatorInst {
2394 ReturnInst(const ReturnInst &RI);
2397 // ReturnInst constructors:
2398 // ReturnInst() - 'ret void' instruction
2399 // ReturnInst( null) - 'ret void' instruction
2400 // ReturnInst(Value* X) - 'ret X' instruction
2401 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2402 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2403 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2404 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2406 // NOTE: If the Value* passed is of type void then the constructor behaves as
2407 // if it was passed NULL.
2408 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2409 Instruction *InsertBefore = nullptr);
2410 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2411 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2413 ReturnInst *clone_impl() const override;
2415 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2416 Instruction *InsertBefore = nullptr) {
2417 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2419 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2420 BasicBlock *InsertAtEnd) {
2421 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2423 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2424 return new(0) ReturnInst(C, InsertAtEnd);
2426 virtual ~ReturnInst();
2428 /// Provide fast operand accessors
2429 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2431 /// Convenience accessor. Returns null if there is no return value.
2432 Value *getReturnValue() const {
2433 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2436 unsigned getNumSuccessors() const { return 0; }
2438 // Methods for support type inquiry through isa, cast, and dyn_cast:
2439 static inline bool classof(const Instruction *I) {
2440 return (I->getOpcode() == Instruction::Ret);
2442 static inline bool classof(const Value *V) {
2443 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2446 BasicBlock *getSuccessorV(unsigned idx) const override;
2447 unsigned getNumSuccessorsV() const override;
2448 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2452 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2455 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2457 //===----------------------------------------------------------------------===//
2459 //===----------------------------------------------------------------------===//
2461 //===---------------------------------------------------------------------------
2462 /// BranchInst - Conditional or Unconditional Branch instruction.
2464 class BranchInst : public TerminatorInst {
2465 /// Ops list - Branches are strange. The operands are ordered:
2466 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2467 /// they don't have to check for cond/uncond branchness. These are mostly
2468 /// accessed relative from op_end().
2469 BranchInst(const BranchInst &BI);
2471 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2472 // BranchInst(BB *B) - 'br B'
2473 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2474 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2475 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2476 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2477 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2478 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2479 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2480 Instruction *InsertBefore = nullptr);
2481 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2482 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2483 BasicBlock *InsertAtEnd);
2485 BranchInst *clone_impl() const override;
2487 static BranchInst *Create(BasicBlock *IfTrue,
2488 Instruction *InsertBefore = nullptr) {
2489 return new(1) BranchInst(IfTrue, InsertBefore);
2491 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2492 Value *Cond, Instruction *InsertBefore = nullptr) {
2493 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2495 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2496 return new(1) BranchInst(IfTrue, InsertAtEnd);
2498 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2499 Value *Cond, BasicBlock *InsertAtEnd) {
2500 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2503 /// Transparently provide more efficient getOperand methods.
2504 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2506 bool isUnconditional() const { return getNumOperands() == 1; }
2507 bool isConditional() const { return getNumOperands() == 3; }
2509 Value *getCondition() const {
2510 assert(isConditional() && "Cannot get condition of an uncond branch!");
2514 void setCondition(Value *V) {
2515 assert(isConditional() && "Cannot set condition of unconditional branch!");
2519 unsigned getNumSuccessors() const { return 1+isConditional(); }
2521 BasicBlock *getSuccessor(unsigned i) const {
2522 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2523 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2526 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2527 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2528 *(&Op<-1>() - idx) = (Value*)NewSucc;
2531 /// \brief Swap the successors of this branch instruction.
2533 /// Swaps the successors of the branch instruction. This also swaps any
2534 /// branch weight metadata associated with the instruction so that it
2535 /// continues to map correctly to each operand.
2536 void swapSuccessors();
2538 /// \brief Retrieve the probabilities of a conditional branch. Returns true on
2539 /// success, or returns false if no or invalid metadata was found.
2540 bool getBranchWeights(uint64_t &TrueWeight, uint64_t &FalseWeight) const;
2542 // Methods for support type inquiry through isa, cast, and dyn_cast:
2543 static inline bool classof(const Instruction *I) {
2544 return (I->getOpcode() == Instruction::Br);
2546 static inline bool classof(const Value *V) {
2547 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2550 BasicBlock *getSuccessorV(unsigned idx) const override;
2551 unsigned getNumSuccessorsV() const override;
2552 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2556 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2559 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2561 //===----------------------------------------------------------------------===//
2563 //===----------------------------------------------------------------------===//
2565 //===---------------------------------------------------------------------------
2566 /// SwitchInst - Multiway switch
2568 class SwitchInst : public TerminatorInst {
2569 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2570 unsigned ReservedSpace;
2571 // Operand[0] = Value to switch on
2572 // Operand[1] = Default basic block destination
2573 // Operand[2n ] = Value to match
2574 // Operand[2n+1] = BasicBlock to go to on match
2575 SwitchInst(const SwitchInst &SI);
2576 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2577 void growOperands();
2578 // allocate space for exactly zero operands
2579 void *operator new(size_t s) {
2580 return User::operator new(s, 0);
2582 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2583 /// switch on and a default destination. The number of additional cases can
2584 /// be specified here to make memory allocation more efficient. This
2585 /// constructor can also autoinsert before another instruction.
2586 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2587 Instruction *InsertBefore);
2589 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2590 /// switch on and a default destination. The number of additional cases can
2591 /// be specified here to make memory allocation more efficient. This
2592 /// constructor also autoinserts at the end of the specified BasicBlock.
2593 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2594 BasicBlock *InsertAtEnd);
2596 SwitchInst *clone_impl() const override;
2600 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2602 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2603 class CaseIteratorT {
2611 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2613 /// Initializes case iterator for given SwitchInst and for given
2615 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2620 /// Initializes case iterator for given SwitchInst and for given
2621 /// TerminatorInst's successor index.
2622 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2623 assert(SuccessorIndex < SI->getNumSuccessors() &&
2624 "Successor index # out of range!");
2625 return SuccessorIndex != 0 ?
2626 Self(SI, SuccessorIndex - 1) :
2627 Self(SI, DefaultPseudoIndex);
2630 /// Resolves case value for current case.
2631 ConstantIntTy *getCaseValue() {
2632 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2633 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2636 /// Resolves successor for current case.
2637 BasicBlockTy *getCaseSuccessor() {
2638 assert((Index < SI->getNumCases() ||
2639 Index == DefaultPseudoIndex) &&
2640 "Index out the number of cases.");
2641 return SI->getSuccessor(getSuccessorIndex());
2644 /// Returns number of current case.
2645 unsigned getCaseIndex() const { return Index; }
2647 /// Returns TerminatorInst's successor index for current case successor.
2648 unsigned getSuccessorIndex() const {
2649 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2650 "Index out the number of cases.");
2651 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2655 // Check index correctness after increment.
2656 // Note: Index == getNumCases() means end().
2657 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2661 Self operator++(int) {
2667 // Check index correctness after decrement.
2668 // Note: Index == getNumCases() means end().
2669 // Also allow "-1" iterator here. That will became valid after ++.
2670 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2671 "Index out the number of cases.");
2675 Self operator--(int) {
2680 bool operator==(const Self& RHS) const {
2681 assert(RHS.SI == SI && "Incompatible operators.");
2682 return RHS.Index == Index;
2684 bool operator!=(const Self& RHS) const {
2685 assert(RHS.SI == SI && "Incompatible operators.");
2686 return RHS.Index != Index;
2693 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2696 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2698 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2702 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2703 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2705 /// Sets the new value for current case.
2706 void setValue(ConstantInt *V) {
2707 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2708 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2711 /// Sets the new successor for current case.
2712 void setSuccessor(BasicBlock *S) {
2713 SI->setSuccessor(getSuccessorIndex(), S);
2717 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2719 Instruction *InsertBefore = nullptr) {
2720 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2722 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2723 unsigned NumCases, BasicBlock *InsertAtEnd) {
2724 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2729 /// Provide fast operand accessors
2730 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2732 // Accessor Methods for Switch stmt
2733 Value *getCondition() const { return getOperand(0); }
2734 void setCondition(Value *V) { setOperand(0, V); }
2736 BasicBlock *getDefaultDest() const {
2737 return cast<BasicBlock>(getOperand(1));
2740 void setDefaultDest(BasicBlock *DefaultCase) {
2741 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2744 /// getNumCases - return the number of 'cases' in this switch instruction,
2745 /// except the default case
2746 unsigned getNumCases() const {
2747 return getNumOperands()/2 - 1;
2750 /// Returns a read/write iterator that points to the first
2751 /// case in SwitchInst.
2752 CaseIt case_begin() {
2753 return CaseIt(this, 0);
2755 /// Returns a read-only iterator that points to the first
2756 /// case in the SwitchInst.
2757 ConstCaseIt case_begin() const {
2758 return ConstCaseIt(this, 0);
2761 /// Returns a read/write iterator that points one past the last
2762 /// in the SwitchInst.
2764 return CaseIt(this, getNumCases());
2766 /// Returns a read-only iterator that points one past the last
2767 /// in the SwitchInst.
2768 ConstCaseIt case_end() const {
2769 return ConstCaseIt(this, getNumCases());
2772 /// cases - iteration adapter for range-for loops.
2773 iterator_range<CaseIt> cases() {
2774 return iterator_range<CaseIt>(case_begin(), case_end());
2777 /// cases - iteration adapter for range-for loops.
2778 iterator_range<ConstCaseIt> cases() const {
2779 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2782 /// Returns an iterator that points to the default case.
2783 /// Note: this iterator allows to resolve successor only. Attempt
2784 /// to resolve case value causes an assertion.
2785 /// Also note, that increment and decrement also causes an assertion and
2786 /// makes iterator invalid.
2787 CaseIt case_default() {
2788 return CaseIt(this, DefaultPseudoIndex);
2790 ConstCaseIt case_default() const {
2791 return ConstCaseIt(this, DefaultPseudoIndex);
2794 /// findCaseValue - Search all of the case values for the specified constant.
2795 /// If it is explicitly handled, return the case iterator of it, otherwise
2796 /// return default case iterator to indicate
2797 /// that it is handled by the default handler.
2798 CaseIt findCaseValue(const ConstantInt *C) {
2799 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2800 if (i.getCaseValue() == C)
2802 return case_default();
2804 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2805 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2806 if (i.getCaseValue() == C)
2808 return case_default();
2811 /// findCaseDest - Finds the unique case value for a given successor. Returns
2812 /// null if the successor is not found, not unique, or is the default case.
2813 ConstantInt *findCaseDest(BasicBlock *BB) {
2814 if (BB == getDefaultDest()) return nullptr;
2816 ConstantInt *CI = nullptr;
2817 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2818 if (i.getCaseSuccessor() == BB) {
2819 if (CI) return nullptr; // Multiple cases lead to BB.
2820 else CI = i.getCaseValue();
2826 /// addCase - Add an entry to the switch instruction...
2828 /// This action invalidates case_end(). Old case_end() iterator will
2829 /// point to the added case.
2830 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2832 /// removeCase - This method removes the specified case and its successor
2833 /// from the switch instruction. Note that this operation may reorder the
2834 /// remaining cases at index idx and above.
2836 /// This action invalidates iterators for all cases following the one removed,
2837 /// including the case_end() iterator.
2838 void removeCase(CaseIt i);
2840 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2841 BasicBlock *getSuccessor(unsigned idx) const {
2842 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2843 return cast<BasicBlock>(getOperand(idx*2+1));
2845 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2846 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2847 setOperand(idx*2+1, (Value*)NewSucc);
2850 // Methods for support type inquiry through isa, cast, and dyn_cast:
2851 static inline bool classof(const Instruction *I) {
2852 return I->getOpcode() == Instruction::Switch;
2854 static inline bool classof(const Value *V) {
2855 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2858 BasicBlock *getSuccessorV(unsigned idx) const override;
2859 unsigned getNumSuccessorsV() const override;
2860 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2864 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2867 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2870 //===----------------------------------------------------------------------===//
2871 // IndirectBrInst Class
2872 //===----------------------------------------------------------------------===//
2874 //===---------------------------------------------------------------------------
2875 /// IndirectBrInst - Indirect Branch Instruction.
2877 class IndirectBrInst : public TerminatorInst {
2878 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2879 unsigned ReservedSpace;
2880 // Operand[0] = Value to switch on
2881 // Operand[1] = Default basic block destination
2882 // Operand[2n ] = Value to match
2883 // Operand[2n+1] = BasicBlock to go to on match
2884 IndirectBrInst(const IndirectBrInst &IBI);
2885 void init(Value *Address, unsigned NumDests);
2886 void growOperands();
2887 // allocate space for exactly zero operands
2888 void *operator new(size_t s) {
2889 return User::operator new(s, 0);
2891 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2892 /// Address to jump to. The number of expected destinations can be specified
2893 /// here to make memory allocation more efficient. This constructor can also
2894 /// autoinsert before another instruction.
2895 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2897 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2898 /// Address to jump to. The number of expected destinations can be specified
2899 /// here to make memory allocation more efficient. This constructor also
2900 /// autoinserts at the end of the specified BasicBlock.
2901 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2903 IndirectBrInst *clone_impl() const override;
2905 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2906 Instruction *InsertBefore = nullptr) {
2907 return new IndirectBrInst(Address, NumDests, InsertBefore);
2909 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2910 BasicBlock *InsertAtEnd) {
2911 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2915 /// Provide fast operand accessors.
2916 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2918 // Accessor Methods for IndirectBrInst instruction.
2919 Value *getAddress() { return getOperand(0); }
2920 const Value *getAddress() const { return getOperand(0); }
2921 void setAddress(Value *V) { setOperand(0, V); }
2924 /// getNumDestinations - return the number of possible destinations in this
2925 /// indirectbr instruction.
2926 unsigned getNumDestinations() const { return getNumOperands()-1; }
2928 /// getDestination - Return the specified destination.
2929 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2930 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2932 /// addDestination - Add a destination.
2934 void addDestination(BasicBlock *Dest);
2936 /// removeDestination - This method removes the specified successor from the
2937 /// indirectbr instruction.
2938 void removeDestination(unsigned i);
2940 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2941 BasicBlock *getSuccessor(unsigned i) const {
2942 return cast<BasicBlock>(getOperand(i+1));
2944 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2945 setOperand(i+1, (Value*)NewSucc);
2948 // Methods for support type inquiry through isa, cast, and dyn_cast:
2949 static inline bool classof(const Instruction *I) {
2950 return I->getOpcode() == Instruction::IndirectBr;
2952 static inline bool classof(const Value *V) {
2953 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2956 BasicBlock *getSuccessorV(unsigned idx) const override;
2957 unsigned getNumSuccessorsV() const override;
2958 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2962 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2965 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2968 //===----------------------------------------------------------------------===//
2970 //===----------------------------------------------------------------------===//
2972 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2973 /// calling convention of the call.
2975 class InvokeInst : public TerminatorInst {
2976 AttributeSet AttributeList;
2977 InvokeInst(const InvokeInst &BI);
2978 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2979 ArrayRef<Value *> Args, const Twine &NameStr);
2981 /// Construct an InvokeInst given a range of arguments.
2983 /// \brief Construct an InvokeInst from a range of arguments
2984 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2985 ArrayRef<Value *> Args, unsigned Values,
2986 const Twine &NameStr, Instruction *InsertBefore);
2988 /// Construct an InvokeInst given a range of arguments.
2990 /// \brief Construct an InvokeInst from a range of arguments
2991 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2992 ArrayRef<Value *> Args, unsigned Values,
2993 const Twine &NameStr, BasicBlock *InsertAtEnd);
2995 InvokeInst *clone_impl() const override;
2997 static InvokeInst *Create(Value *Func,
2998 BasicBlock *IfNormal, BasicBlock *IfException,
2999 ArrayRef<Value *> Args, const Twine &NameStr = "",
3000 Instruction *InsertBefore = nullptr) {
3001 unsigned Values = unsigned(Args.size()) + 3;
3002 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3003 Values, NameStr, InsertBefore);
3005 static InvokeInst *Create(Value *Func,
3006 BasicBlock *IfNormal, BasicBlock *IfException,
3007 ArrayRef<Value *> Args, const Twine &NameStr,
3008 BasicBlock *InsertAtEnd) {
3009 unsigned Values = unsigned(Args.size()) + 3;
3010 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3011 Values, NameStr, InsertAtEnd);
3014 /// Provide fast operand accessors
3015 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3017 /// getNumArgOperands - Return the number of invoke arguments.
3019 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3021 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3023 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3024 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3026 /// arg_operands - iteration adapter for range-for loops.
3027 iterator_range<op_iterator> arg_operands() {
3028 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3031 /// arg_operands - iteration adapter for range-for loops.
3032 iterator_range<const_op_iterator> arg_operands() const {
3033 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3036 /// \brief Wrappers for getting the \c Use of a invoke argument.
3037 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3038 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3040 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3042 CallingConv::ID getCallingConv() const {
3043 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3045 void setCallingConv(CallingConv::ID CC) {
3046 setInstructionSubclassData(static_cast<unsigned>(CC));
3049 /// getAttributes - Return the parameter attributes for this invoke.
3051 const AttributeSet &getAttributes() const { return AttributeList; }
3053 /// setAttributes - Set the parameter attributes for this invoke.
3055 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3057 /// addAttribute - adds the attribute to the list of attributes.
3058 void addAttribute(unsigned i, Attribute::AttrKind attr);
3060 /// removeAttribute - removes the attribute from the list of attributes.
3061 void removeAttribute(unsigned i, Attribute attr);
3063 /// \brief Determine whether this call has the given attribute.
3064 bool hasFnAttr(Attribute::AttrKind A) const {
3065 assert(A != Attribute::NoBuiltin &&
3066 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3067 return hasFnAttrImpl(A);
3070 /// \brief Determine whether the call or the callee has the given attributes.
3071 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3073 /// \brief Extract the alignment for a call or parameter (0=unknown).
3074 unsigned getParamAlignment(unsigned i) const {
3075 return AttributeList.getParamAlignment(i);
3078 /// \brief Extract the number of dereferenceable bytes for a call or
3079 /// parameter (0=unknown).
3080 uint64_t getDereferenceableBytes(unsigned i) const {
3081 return AttributeList.getDereferenceableBytes(i);
3084 /// \brief Return true if the call should not be treated as a call to a
3086 bool isNoBuiltin() const {
3087 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3088 // to check it by hand.
3089 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3090 !hasFnAttrImpl(Attribute::Builtin);
3093 /// \brief Return true if the call should not be inlined.
3094 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3095 void setIsNoInline() {
3096 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3099 /// \brief Determine if the call does not access memory.
3100 bool doesNotAccessMemory() const {
3101 return hasFnAttr(Attribute::ReadNone);
3103 void setDoesNotAccessMemory() {
3104 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3107 /// \brief Determine if the call does not access or only reads memory.
3108 bool onlyReadsMemory() const {
3109 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3111 void setOnlyReadsMemory() {
3112 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3115 /// \brief Determine if the call cannot return.
3116 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3117 void setDoesNotReturn() {
3118 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3121 /// \brief Determine if the call cannot unwind.
3122 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3123 void setDoesNotThrow() {
3124 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3127 /// \brief Determine if the invoke cannot be duplicated.
3128 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3129 void setCannotDuplicate() {
3130 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3133 /// \brief Determine if the call returns a structure through first
3134 /// pointer argument.
3135 bool hasStructRetAttr() const {
3136 // Be friendly and also check the callee.
3137 return paramHasAttr(1, Attribute::StructRet);
3140 /// \brief Determine if any call argument is an aggregate passed by value.
3141 bool hasByValArgument() const {
3142 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3145 /// getCalledFunction - Return the function called, or null if this is an
3146 /// indirect function invocation.
3148 Function *getCalledFunction() const {
3149 return dyn_cast<Function>(Op<-3>());
3152 /// getCalledValue - Get a pointer to the function that is invoked by this
3154 const Value *getCalledValue() const { return Op<-3>(); }
3155 Value *getCalledValue() { return Op<-3>(); }
3157 /// setCalledFunction - Set the function called.
3158 void setCalledFunction(Value* Fn) {
3162 // get*Dest - Return the destination basic blocks...
3163 BasicBlock *getNormalDest() const {
3164 return cast<BasicBlock>(Op<-2>());
3166 BasicBlock *getUnwindDest() const {
3167 return cast<BasicBlock>(Op<-1>());
3169 void setNormalDest(BasicBlock *B) {
3170 Op<-2>() = reinterpret_cast<Value*>(B);
3172 void setUnwindDest(BasicBlock *B) {
3173 Op<-1>() = reinterpret_cast<Value*>(B);
3176 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3177 /// block (the unwind destination).
3178 LandingPadInst *getLandingPadInst() const;
3180 BasicBlock *getSuccessor(unsigned i) const {
3181 assert(i < 2 && "Successor # out of range for invoke!");
3182 return i == 0 ? getNormalDest() : getUnwindDest();
3185 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3186 assert(idx < 2 && "Successor # out of range for invoke!");
3187 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3190 unsigned getNumSuccessors() const { return 2; }
3192 // Methods for support type inquiry through isa, cast, and dyn_cast:
3193 static inline bool classof(const Instruction *I) {
3194 return (I->getOpcode() == Instruction::Invoke);
3196 static inline bool classof(const Value *V) {
3197 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3201 BasicBlock *getSuccessorV(unsigned idx) const override;
3202 unsigned getNumSuccessorsV() const override;
3203 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3205 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3207 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3208 // method so that subclasses cannot accidentally use it.
3209 void setInstructionSubclassData(unsigned short D) {
3210 Instruction::setInstructionSubclassData(D);
3215 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3218 InvokeInst::InvokeInst(Value *Func,
3219 BasicBlock *IfNormal, BasicBlock *IfException,
3220 ArrayRef<Value *> Args, unsigned Values,
3221 const Twine &NameStr, Instruction *InsertBefore)
3222 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3223 ->getElementType())->getReturnType(),
3224 Instruction::Invoke,
3225 OperandTraits<InvokeInst>::op_end(this) - Values,
3226 Values, InsertBefore) {
3227 init(Func, IfNormal, IfException, Args, NameStr);
3229 InvokeInst::InvokeInst(Value *Func,
3230 BasicBlock *IfNormal, BasicBlock *IfException,
3231 ArrayRef<Value *> Args, unsigned Values,
3232 const Twine &NameStr, BasicBlock *InsertAtEnd)
3233 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3234 ->getElementType())->getReturnType(),
3235 Instruction::Invoke,
3236 OperandTraits<InvokeInst>::op_end(this) - Values,
3237 Values, InsertAtEnd) {
3238 init(Func, IfNormal, IfException, Args, NameStr);
3241 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3243 //===----------------------------------------------------------------------===//
3245 //===----------------------------------------------------------------------===//
3247 //===---------------------------------------------------------------------------
3248 /// ResumeInst - Resume the propagation of an exception.
3250 class ResumeInst : public TerminatorInst {
3251 ResumeInst(const ResumeInst &RI);
3253 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3254 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3256 ResumeInst *clone_impl() const override;
3258 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3259 return new(1) ResumeInst(Exn, InsertBefore);
3261 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3262 return new(1) ResumeInst(Exn, InsertAtEnd);
3265 /// Provide fast operand accessors
3266 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3268 /// Convenience accessor.
3269 Value *getValue() const { return Op<0>(); }
3271 unsigned getNumSuccessors() const { return 0; }
3273 // Methods for support type inquiry through isa, cast, and dyn_cast:
3274 static inline bool classof(const Instruction *I) {
3275 return I->getOpcode() == Instruction::Resume;
3277 static inline bool classof(const Value *V) {
3278 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3281 BasicBlock *getSuccessorV(unsigned idx) const override;
3282 unsigned getNumSuccessorsV() const override;
3283 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3287 struct OperandTraits<ResumeInst> :
3288 public FixedNumOperandTraits<ResumeInst, 1> {
3291 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3293 //===----------------------------------------------------------------------===//
3294 // UnreachableInst Class
3295 //===----------------------------------------------------------------------===//
3297 //===---------------------------------------------------------------------------
3298 /// UnreachableInst - This function has undefined behavior. In particular, the
3299 /// presence of this instruction indicates some higher level knowledge that the
3300 /// end of the block cannot be reached.
3302 class UnreachableInst : public TerminatorInst {
3303 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3305 UnreachableInst *clone_impl() const override;
3308 // allocate space for exactly zero operands
3309 void *operator new(size_t s) {
3310 return User::operator new(s, 0);
3312 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3313 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3315 unsigned getNumSuccessors() const { return 0; }
3317 // Methods for support type inquiry through isa, cast, and dyn_cast:
3318 static inline bool classof(const Instruction *I) {
3319 return I->getOpcode() == Instruction::Unreachable;
3321 static inline bool classof(const Value *V) {
3322 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3325 BasicBlock *getSuccessorV(unsigned idx) const override;
3326 unsigned getNumSuccessorsV() const override;
3327 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3330 //===----------------------------------------------------------------------===//
3332 //===----------------------------------------------------------------------===//
3334 /// \brief This class represents a truncation of integer types.
3335 class TruncInst : public CastInst {
3337 /// \brief Clone an identical TruncInst
3338 TruncInst *clone_impl() const override;
3341 /// \brief Constructor with insert-before-instruction semantics
3343 Value *S, ///< The value to be truncated
3344 Type *Ty, ///< The (smaller) type to truncate to
3345 const Twine &NameStr = "", ///< A name for the new instruction
3346 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3349 /// \brief Constructor with insert-at-end-of-block semantics
3351 Value *S, ///< The value to be truncated
3352 Type *Ty, ///< The (smaller) type to truncate to
3353 const Twine &NameStr, ///< A name for the new instruction
3354 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3357 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3358 static inline bool classof(const Instruction *I) {
3359 return I->getOpcode() == Trunc;
3361 static inline bool classof(const Value *V) {
3362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3366 //===----------------------------------------------------------------------===//
3368 //===----------------------------------------------------------------------===//
3370 /// \brief This class represents zero extension of integer types.
3371 class ZExtInst : public CastInst {
3373 /// \brief Clone an identical ZExtInst
3374 ZExtInst *clone_impl() const override;
3377 /// \brief Constructor with insert-before-instruction semantics
3379 Value *S, ///< The value to be zero extended
3380 Type *Ty, ///< The type to zero extend to
3381 const Twine &NameStr = "", ///< A name for the new instruction
3382 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3385 /// \brief Constructor with insert-at-end semantics.
3387 Value *S, ///< The value to be zero extended
3388 Type *Ty, ///< The type to zero extend to
3389 const Twine &NameStr, ///< A name for the new instruction
3390 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3393 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3394 static inline bool classof(const Instruction *I) {
3395 return I->getOpcode() == ZExt;
3397 static inline bool classof(const Value *V) {
3398 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3402 //===----------------------------------------------------------------------===//
3404 //===----------------------------------------------------------------------===//
3406 /// \brief This class represents a sign extension of integer types.
3407 class SExtInst : public CastInst {
3409 /// \brief Clone an identical SExtInst
3410 SExtInst *clone_impl() const override;
3413 /// \brief Constructor with insert-before-instruction semantics
3415 Value *S, ///< The value to be sign extended
3416 Type *Ty, ///< The type to sign extend to
3417 const Twine &NameStr = "", ///< A name for the new instruction
3418 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3421 /// \brief Constructor with insert-at-end-of-block semantics
3423 Value *S, ///< The value to be sign extended
3424 Type *Ty, ///< The type to sign extend to
3425 const Twine &NameStr, ///< A name for the new instruction
3426 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3429 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3430 static inline bool classof(const Instruction *I) {
3431 return I->getOpcode() == SExt;
3433 static inline bool classof(const Value *V) {
3434 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3438 //===----------------------------------------------------------------------===//
3439 // FPTruncInst Class
3440 //===----------------------------------------------------------------------===//
3442 /// \brief This class represents a truncation of floating point types.
3443 class FPTruncInst : public CastInst {
3445 /// \brief Clone an identical FPTruncInst
3446 FPTruncInst *clone_impl() const override;
3449 /// \brief Constructor with insert-before-instruction semantics
3451 Value *S, ///< The value to be truncated
3452 Type *Ty, ///< The type to truncate to
3453 const Twine &NameStr = "", ///< A name for the new instruction
3454 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3457 /// \brief Constructor with insert-before-instruction semantics
3459 Value *S, ///< The value to be truncated
3460 Type *Ty, ///< The type to truncate to
3461 const Twine &NameStr, ///< A name for the new instruction
3462 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3465 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3466 static inline bool classof(const Instruction *I) {
3467 return I->getOpcode() == FPTrunc;
3469 static inline bool classof(const Value *V) {
3470 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3474 //===----------------------------------------------------------------------===//
3476 //===----------------------------------------------------------------------===//
3478 /// \brief This class represents an extension of floating point types.
3479 class FPExtInst : public CastInst {
3481 /// \brief Clone an identical FPExtInst
3482 FPExtInst *clone_impl() const override;
3485 /// \brief Constructor with insert-before-instruction semantics
3487 Value *S, ///< The value to be extended
3488 Type *Ty, ///< The type to extend to
3489 const Twine &NameStr = "", ///< A name for the new instruction
3490 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3493 /// \brief Constructor with insert-at-end-of-block semantics
3495 Value *S, ///< The value to be extended
3496 Type *Ty, ///< The type to extend to
3497 const Twine &NameStr, ///< A name for the new instruction
3498 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3501 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3502 static inline bool classof(const Instruction *I) {
3503 return I->getOpcode() == FPExt;
3505 static inline bool classof(const Value *V) {
3506 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3510 //===----------------------------------------------------------------------===//
3512 //===----------------------------------------------------------------------===//
3514 /// \brief This class represents a cast unsigned integer to floating point.
3515 class UIToFPInst : public CastInst {
3517 /// \brief Clone an identical UIToFPInst
3518 UIToFPInst *clone_impl() const override;
3521 /// \brief Constructor with insert-before-instruction semantics
3523 Value *S, ///< The value to be converted
3524 Type *Ty, ///< The type to convert to
3525 const Twine &NameStr = "", ///< A name for the new instruction
3526 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3529 /// \brief Constructor with insert-at-end-of-block semantics
3531 Value *S, ///< The value to be converted
3532 Type *Ty, ///< The type to convert to
3533 const Twine &NameStr, ///< A name for the new instruction
3534 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3537 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3538 static inline bool classof(const Instruction *I) {
3539 return I->getOpcode() == UIToFP;
3541 static inline bool classof(const Value *V) {
3542 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3546 //===----------------------------------------------------------------------===//
3548 //===----------------------------------------------------------------------===//
3550 /// \brief This class represents a cast from signed integer to floating point.
3551 class SIToFPInst : public CastInst {
3553 /// \brief Clone an identical SIToFPInst
3554 SIToFPInst *clone_impl() const override;
3557 /// \brief Constructor with insert-before-instruction semantics
3559 Value *S, ///< The value to be converted
3560 Type *Ty, ///< The type to convert to
3561 const Twine &NameStr = "", ///< A name for the new instruction
3562 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3565 /// \brief Constructor with insert-at-end-of-block semantics
3567 Value *S, ///< The value to be converted
3568 Type *Ty, ///< The type to convert to
3569 const Twine &NameStr, ///< A name for the new instruction
3570 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3573 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3574 static inline bool classof(const Instruction *I) {
3575 return I->getOpcode() == SIToFP;
3577 static inline bool classof(const Value *V) {
3578 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3582 //===----------------------------------------------------------------------===//
3584 //===----------------------------------------------------------------------===//
3586 /// \brief This class represents a cast from floating point to unsigned integer
3587 class FPToUIInst : public CastInst {
3589 /// \brief Clone an identical FPToUIInst
3590 FPToUIInst *clone_impl() const override;
3593 /// \brief Constructor with insert-before-instruction semantics
3595 Value *S, ///< The value to be converted
3596 Type *Ty, ///< The type to convert to
3597 const Twine &NameStr = "", ///< A name for the new instruction
3598 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3601 /// \brief Constructor with insert-at-end-of-block semantics
3603 Value *S, ///< The value to be converted
3604 Type *Ty, ///< The type to convert to
3605 const Twine &NameStr, ///< A name for the new instruction
3606 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3609 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3610 static inline bool classof(const Instruction *I) {
3611 return I->getOpcode() == FPToUI;
3613 static inline bool classof(const Value *V) {
3614 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3618 //===----------------------------------------------------------------------===//
3620 //===----------------------------------------------------------------------===//
3622 /// \brief This class represents a cast from floating point to signed integer.
3623 class FPToSIInst : public CastInst {
3625 /// \brief Clone an identical FPToSIInst
3626 FPToSIInst *clone_impl() const override;
3629 /// \brief Constructor with insert-before-instruction semantics
3631 Value *S, ///< The value to be converted
3632 Type *Ty, ///< The type to convert to
3633 const Twine &NameStr = "", ///< A name for the new instruction
3634 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3637 /// \brief Constructor with insert-at-end-of-block semantics
3639 Value *S, ///< The value to be converted
3640 Type *Ty, ///< The type to convert to
3641 const Twine &NameStr, ///< A name for the new instruction
3642 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3645 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3646 static inline bool classof(const Instruction *I) {
3647 return I->getOpcode() == FPToSI;
3649 static inline bool classof(const Value *V) {
3650 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3654 //===----------------------------------------------------------------------===//
3655 // IntToPtrInst Class
3656 //===----------------------------------------------------------------------===//
3658 /// \brief This class represents a cast from an integer to a pointer.
3659 class IntToPtrInst : public CastInst {
3661 /// \brief Constructor with insert-before-instruction semantics
3663 Value *S, ///< The value to be converted
3664 Type *Ty, ///< The type to convert to
3665 const Twine &NameStr = "", ///< A name for the new instruction
3666 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3669 /// \brief Constructor with insert-at-end-of-block semantics
3671 Value *S, ///< The value to be converted
3672 Type *Ty, ///< The type to convert to
3673 const Twine &NameStr, ///< A name for the new instruction
3674 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3677 /// \brief Clone an identical IntToPtrInst
3678 IntToPtrInst *clone_impl() const override;
3680 /// \brief Returns the address space of this instruction's pointer type.
3681 unsigned getAddressSpace() const {
3682 return getType()->getPointerAddressSpace();
3685 // Methods for support type inquiry through isa, cast, and dyn_cast:
3686 static inline bool classof(const Instruction *I) {
3687 return I->getOpcode() == IntToPtr;
3689 static inline bool classof(const Value *V) {
3690 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3694 //===----------------------------------------------------------------------===//
3695 // PtrToIntInst Class
3696 //===----------------------------------------------------------------------===//
3698 /// \brief This class represents a cast from a pointer to an integer
3699 class PtrToIntInst : public CastInst {
3701 /// \brief Clone an identical PtrToIntInst
3702 PtrToIntInst *clone_impl() const override;
3705 /// \brief Constructor with insert-before-instruction semantics
3707 Value *S, ///< The value to be converted
3708 Type *Ty, ///< The type to convert to
3709 const Twine &NameStr = "", ///< A name for the new instruction
3710 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3713 /// \brief Constructor with insert-at-end-of-block semantics
3715 Value *S, ///< The value to be converted
3716 Type *Ty, ///< The type to convert to
3717 const Twine &NameStr, ///< A name for the new instruction
3718 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3721 /// \brief Gets the pointer operand.
3722 Value *getPointerOperand() { return getOperand(0); }
3723 /// \brief Gets the pointer operand.
3724 const Value *getPointerOperand() const { return getOperand(0); }
3725 /// \brief Gets the operand index of the pointer operand.
3726 static unsigned getPointerOperandIndex() { return 0U; }
3728 /// \brief Returns the address space of the pointer operand.
3729 unsigned getPointerAddressSpace() const {
3730 return getPointerOperand()->getType()->getPointerAddressSpace();
3733 // Methods for support type inquiry through isa, cast, and dyn_cast:
3734 static inline bool classof(const Instruction *I) {
3735 return I->getOpcode() == PtrToInt;
3737 static inline bool classof(const Value *V) {
3738 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3742 //===----------------------------------------------------------------------===//
3743 // BitCastInst Class
3744 //===----------------------------------------------------------------------===//
3746 /// \brief This class represents a no-op cast from one type to another.
3747 class BitCastInst : public CastInst {
3749 /// \brief Clone an identical BitCastInst
3750 BitCastInst *clone_impl() const override;
3753 /// \brief Constructor with insert-before-instruction semantics
3755 Value *S, ///< The value to be casted
3756 Type *Ty, ///< The type to casted to
3757 const Twine &NameStr = "", ///< A name for the new instruction
3758 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3761 /// \brief Constructor with insert-at-end-of-block semantics
3763 Value *S, ///< The value to be casted
3764 Type *Ty, ///< The type to casted to
3765 const Twine &NameStr, ///< A name for the new instruction
3766 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3769 // Methods for support type inquiry through isa, cast, and dyn_cast:
3770 static inline bool classof(const Instruction *I) {
3771 return I->getOpcode() == BitCast;
3773 static inline bool classof(const Value *V) {
3774 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3778 //===----------------------------------------------------------------------===//
3779 // AddrSpaceCastInst Class
3780 //===----------------------------------------------------------------------===//
3782 /// \brief This class represents a conversion between pointers from
3783 /// one address space to another.
3784 class AddrSpaceCastInst : public CastInst {
3786 /// \brief Clone an identical AddrSpaceCastInst
3787 AddrSpaceCastInst *clone_impl() const override;
3790 /// \brief Constructor with insert-before-instruction semantics
3792 Value *S, ///< The value to be casted
3793 Type *Ty, ///< The type to casted to
3794 const Twine &NameStr = "", ///< A name for the new instruction
3795 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3798 /// \brief Constructor with insert-at-end-of-block semantics
3800 Value *S, ///< The value to be casted
3801 Type *Ty, ///< The type to casted to
3802 const Twine &NameStr, ///< A name for the new instruction
3803 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3806 // Methods for support type inquiry through isa, cast, and dyn_cast:
3807 static inline bool classof(const Instruction *I) {
3808 return I->getOpcode() == AddrSpaceCast;
3810 static inline bool classof(const Value *V) {
3811 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3815 } // End llvm namespace