1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_INSTRUCTIONS_H
17 #define LLVM_IR_INSTRUCTIONS_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/InstrTypes.h"
26 #include "llvm/Support/ErrorHandling.h"
41 // Consume = 3, // Not specified yet.
45 SequentiallyConsistent = 7
48 enum SynchronizationScope {
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 /// AllocaInst - an instruction to allocate memory on the stack
59 class AllocaInst : public UnaryInstruction {
61 AllocaInst *clone_impl() const override;
63 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
64 const Twine &Name = "",
65 Instruction *InsertBefore = nullptr);
66 AllocaInst(Type *Ty, Value *ArraySize,
67 const Twine &Name, BasicBlock *InsertAtEnd);
69 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
70 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
72 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
73 const Twine &Name = "", Instruction *InsertBefore = nullptr);
74 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
75 const Twine &Name, BasicBlock *InsertAtEnd);
77 // Out of line virtual method, so the vtable, etc. has a home.
78 virtual ~AllocaInst();
80 /// isArrayAllocation - Return true if there is an allocation size parameter
81 /// to the allocation instruction that is not 1.
83 bool isArrayAllocation() const;
85 /// getArraySize - Get the number of elements allocated. For a simple
86 /// allocation of a single element, this will return a constant 1 value.
88 const Value *getArraySize() const { return getOperand(0); }
89 Value *getArraySize() { return getOperand(0); }
91 /// getType - Overload to return most specific pointer type
93 PointerType *getType() const {
94 return cast<PointerType>(Instruction::getType());
97 /// getAllocatedType - Return the type that is being allocated by the
100 Type *getAllocatedType() const;
102 /// getAlignment - Return the alignment of the memory that is being allocated
103 /// by the instruction.
105 unsigned getAlignment() const {
106 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
108 void setAlignment(unsigned Align);
110 /// isStaticAlloca - Return true if this alloca is in the entry block of the
111 /// function and is a constant size. If so, the code generator will fold it
112 /// into the prolog/epilog code, so it is basically free.
113 bool isStaticAlloca() const;
115 /// \brief Return true if this alloca is used as an inalloca argument to a
116 /// call. Such allocas are never considered static even if they are in the
118 bool isUsedWithInAlloca() const {
119 return getSubclassDataFromInstruction() & 32;
122 /// \brief Specify whether this alloca is used to represent a the arguments to
124 void setUsedWithInAlloca(bool V) {
125 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
129 // Methods for support type inquiry through isa, cast, and dyn_cast:
130 static inline bool classof(const Instruction *I) {
131 return (I->getOpcode() == Instruction::Alloca);
133 static inline bool classof(const Value *V) {
134 return isa<Instruction>(V) && classof(cast<Instruction>(V));
137 // Shadow Instruction::setInstructionSubclassData with a private forwarding
138 // method so that subclasses cannot accidentally use it.
139 void setInstructionSubclassData(unsigned short D) {
140 Instruction::setInstructionSubclassData(D);
145 //===----------------------------------------------------------------------===//
147 //===----------------------------------------------------------------------===//
149 /// LoadInst - an instruction for reading from memory. This uses the
150 /// SubclassData field in Value to store whether or not the load is volatile.
152 class LoadInst : public UnaryInstruction {
155 LoadInst *clone_impl() const override;
157 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
158 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
159 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
160 Instruction *InsertBefore = nullptr);
161 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
162 BasicBlock *InsertAtEnd);
163 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
164 unsigned Align, Instruction *InsertBefore = nullptr);
165 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
166 unsigned Align, BasicBlock *InsertAtEnd);
167 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
168 unsigned Align, AtomicOrdering Order,
169 SynchronizationScope SynchScope = CrossThread,
170 Instruction *InsertBefore = nullptr);
171 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
172 unsigned Align, AtomicOrdering Order,
173 SynchronizationScope SynchScope,
174 BasicBlock *InsertAtEnd);
176 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
177 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
178 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
179 bool isVolatile = false,
180 Instruction *InsertBefore = nullptr);
181 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
182 BasicBlock *InsertAtEnd);
184 /// isVolatile - Return true if this is a load from a volatile memory
187 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
189 /// setVolatile - Specify whether this is a volatile load or not.
191 void setVolatile(bool V) {
192 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
196 /// getAlignment - Return the alignment of the access that is being performed
198 unsigned getAlignment() const {
199 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
202 void setAlignment(unsigned Align);
204 /// Returns the ordering effect of this fence.
205 AtomicOrdering getOrdering() const {
206 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
209 /// Set the ordering constraint on this load. May not be Release or
211 void setOrdering(AtomicOrdering Ordering) {
212 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
216 SynchronizationScope getSynchScope() const {
217 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
220 /// Specify whether this load is ordered with respect to all
221 /// concurrently executing threads, or only with respect to signal handlers
222 /// executing in the same thread.
223 void setSynchScope(SynchronizationScope xthread) {
224 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
228 bool isAtomic() const { return getOrdering() != NotAtomic; }
229 void setAtomic(AtomicOrdering Ordering,
230 SynchronizationScope SynchScope = CrossThread) {
231 setOrdering(Ordering);
232 setSynchScope(SynchScope);
235 bool isSimple() const { return !isAtomic() && !isVolatile(); }
236 bool isUnordered() const {
237 return getOrdering() <= Unordered && !isVolatile();
240 Value *getPointerOperand() { return getOperand(0); }
241 const Value *getPointerOperand() const { return getOperand(0); }
242 static unsigned getPointerOperandIndex() { return 0U; }
244 /// \brief Returns the address space of the pointer operand.
245 unsigned getPointerAddressSpace() const {
246 return getPointerOperand()->getType()->getPointerAddressSpace();
250 // Methods for support type inquiry through isa, cast, and dyn_cast:
251 static inline bool classof(const Instruction *I) {
252 return I->getOpcode() == Instruction::Load;
254 static inline bool classof(const Value *V) {
255 return isa<Instruction>(V) && classof(cast<Instruction>(V));
258 // Shadow Instruction::setInstructionSubclassData with a private forwarding
259 // method so that subclasses cannot accidentally use it.
260 void setInstructionSubclassData(unsigned short D) {
261 Instruction::setInstructionSubclassData(D);
266 //===----------------------------------------------------------------------===//
268 //===----------------------------------------------------------------------===//
270 /// StoreInst - an instruction for storing to memory
272 class StoreInst : public Instruction {
273 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
276 StoreInst *clone_impl() const override;
278 // allocate space for exactly two operands
279 void *operator new(size_t s) {
280 return User::operator new(s, 2);
282 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
283 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
284 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
285 Instruction *InsertBefore = nullptr);
286 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
287 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
288 unsigned Align, Instruction *InsertBefore = nullptr);
289 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
290 unsigned Align, BasicBlock *InsertAtEnd);
291 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
292 unsigned Align, AtomicOrdering Order,
293 SynchronizationScope SynchScope = CrossThread,
294 Instruction *InsertBefore = nullptr);
295 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
296 unsigned Align, AtomicOrdering Order,
297 SynchronizationScope SynchScope,
298 BasicBlock *InsertAtEnd);
301 /// isVolatile - Return true if this is a store to a volatile memory
304 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
306 /// setVolatile - Specify whether this is a volatile store or not.
308 void setVolatile(bool V) {
309 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
313 /// Transparently provide more efficient getOperand methods.
314 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
316 /// getAlignment - Return the alignment of the access that is being performed
318 unsigned getAlignment() const {
319 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
322 void setAlignment(unsigned Align);
324 /// Returns the ordering effect of this store.
325 AtomicOrdering getOrdering() const {
326 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
329 /// Set the ordering constraint on this store. May not be Acquire or
331 void setOrdering(AtomicOrdering Ordering) {
332 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
336 SynchronizationScope getSynchScope() const {
337 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
340 /// Specify whether this store instruction is ordered with respect to all
341 /// concurrently executing threads, or only with respect to signal handlers
342 /// executing in the same thread.
343 void setSynchScope(SynchronizationScope xthread) {
344 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
348 bool isAtomic() const { return getOrdering() != NotAtomic; }
349 void setAtomic(AtomicOrdering Ordering,
350 SynchronizationScope SynchScope = CrossThread) {
351 setOrdering(Ordering);
352 setSynchScope(SynchScope);
355 bool isSimple() const { return !isAtomic() && !isVolatile(); }
356 bool isUnordered() const {
357 return getOrdering() <= Unordered && !isVolatile();
360 Value *getValueOperand() { return getOperand(0); }
361 const Value *getValueOperand() const { return getOperand(0); }
363 Value *getPointerOperand() { return getOperand(1); }
364 const Value *getPointerOperand() const { return getOperand(1); }
365 static unsigned getPointerOperandIndex() { return 1U; }
367 /// \brief Returns the address space of the pointer operand.
368 unsigned getPointerAddressSpace() const {
369 return getPointerOperand()->getType()->getPointerAddressSpace();
372 // Methods for support type inquiry through isa, cast, and dyn_cast:
373 static inline bool classof(const Instruction *I) {
374 return I->getOpcode() == Instruction::Store;
376 static inline bool classof(const Value *V) {
377 return isa<Instruction>(V) && classof(cast<Instruction>(V));
380 // Shadow Instruction::setInstructionSubclassData with a private forwarding
381 // method so that subclasses cannot accidentally use it.
382 void setInstructionSubclassData(unsigned short D) {
383 Instruction::setInstructionSubclassData(D);
388 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
391 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
393 //===----------------------------------------------------------------------===//
395 //===----------------------------------------------------------------------===//
397 /// FenceInst - an instruction for ordering other memory operations
399 class FenceInst : public Instruction {
400 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
401 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
403 FenceInst *clone_impl() const override;
405 // allocate space for exactly zero operands
406 void *operator new(size_t s) {
407 return User::operator new(s, 0);
410 // Ordering may only be Acquire, Release, AcquireRelease, or
411 // SequentiallyConsistent.
412 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
413 SynchronizationScope SynchScope = CrossThread,
414 Instruction *InsertBefore = nullptr);
415 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
416 SynchronizationScope SynchScope,
417 BasicBlock *InsertAtEnd);
419 /// Returns the ordering effect of this fence.
420 AtomicOrdering getOrdering() const {
421 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
424 /// Set the ordering constraint on this fence. May only be Acquire, Release,
425 /// AcquireRelease, or SequentiallyConsistent.
426 void setOrdering(AtomicOrdering Ordering) {
427 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
431 SynchronizationScope getSynchScope() const {
432 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
435 /// Specify whether this fence orders other operations with respect to all
436 /// concurrently executing threads, or only with respect to signal handlers
437 /// executing in the same thread.
438 void setSynchScope(SynchronizationScope xthread) {
439 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
443 // Methods for support type inquiry through isa, cast, and dyn_cast:
444 static inline bool classof(const Instruction *I) {
445 return I->getOpcode() == Instruction::Fence;
447 static inline bool classof(const Value *V) {
448 return isa<Instruction>(V) && classof(cast<Instruction>(V));
451 // Shadow Instruction::setInstructionSubclassData with a private forwarding
452 // method so that subclasses cannot accidentally use it.
453 void setInstructionSubclassData(unsigned short D) {
454 Instruction::setInstructionSubclassData(D);
458 //===----------------------------------------------------------------------===//
459 // AtomicCmpXchgInst Class
460 //===----------------------------------------------------------------------===//
462 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
463 /// specified value is in a memory location, and, if it is, stores a new value
464 /// there. Returns the value that was loaded.
466 class AtomicCmpXchgInst : public Instruction {
467 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
468 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
469 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
470 SynchronizationScope SynchScope);
472 AtomicCmpXchgInst *clone_impl() const override;
474 // allocate space for exactly three operands
475 void *operator new(size_t s) {
476 return User::operator new(s, 3);
478 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
479 AtomicOrdering SuccessOrdering,
480 AtomicOrdering FailureOrdering,
481 SynchronizationScope SynchScope,
482 Instruction *InsertBefore = nullptr);
483 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
484 AtomicOrdering SuccessOrdering,
485 AtomicOrdering FailureOrdering,
486 SynchronizationScope SynchScope,
487 BasicBlock *InsertAtEnd);
489 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
492 bool isVolatile() const {
493 return getSubclassDataFromInstruction() & 1;
496 /// setVolatile - Specify whether this is a volatile cmpxchg.
498 void setVolatile(bool V) {
499 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
503 /// Transparently provide more efficient getOperand methods.
504 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
506 /// Set the ordering constraint on this cmpxchg.
507 void setSuccessOrdering(AtomicOrdering Ordering) {
508 assert(Ordering != NotAtomic &&
509 "CmpXchg instructions can only be atomic.");
510 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
514 void setFailureOrdering(AtomicOrdering Ordering) {
515 assert(Ordering != NotAtomic &&
516 "CmpXchg instructions can only be atomic.");
517 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
521 /// Specify whether this cmpxchg is atomic and orders other operations with
522 /// respect to all concurrently executing threads, or only with respect to
523 /// signal handlers executing in the same thread.
524 void setSynchScope(SynchronizationScope SynchScope) {
525 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
529 /// Returns the ordering constraint on this cmpxchg.
530 AtomicOrdering getSuccessOrdering() const {
531 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
534 /// Returns the ordering constraint on this cmpxchg.
535 AtomicOrdering getFailureOrdering() const {
536 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
539 /// Returns whether this cmpxchg is atomic between threads or only within a
541 SynchronizationScope getSynchScope() const {
542 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
545 Value *getPointerOperand() { return getOperand(0); }
546 const Value *getPointerOperand() const { return getOperand(0); }
547 static unsigned getPointerOperandIndex() { return 0U; }
549 Value *getCompareOperand() { return getOperand(1); }
550 const Value *getCompareOperand() const { return getOperand(1); }
552 Value *getNewValOperand() { return getOperand(2); }
553 const Value *getNewValOperand() const { return getOperand(2); }
555 /// \brief Returns the address space of the pointer operand.
556 unsigned getPointerAddressSpace() const {
557 return getPointerOperand()->getType()->getPointerAddressSpace();
560 /// \brief Returns the strongest permitted ordering on failure, given the
561 /// desired ordering on success.
563 /// If the comparison in a cmpxchg operation fails, there is no atomic store
564 /// so release semantics cannot be provided. So this function drops explicit
565 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
566 /// operation would remain SequentiallyConsistent.
567 static AtomicOrdering
568 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
569 switch (SuccessOrdering) {
570 default: llvm_unreachable("invalid cmpxchg success ordering");
577 case SequentiallyConsistent:
578 return SequentiallyConsistent;
582 // Methods for support type inquiry through isa, cast, and dyn_cast:
583 static inline bool classof(const Instruction *I) {
584 return I->getOpcode() == Instruction::AtomicCmpXchg;
586 static inline bool classof(const Value *V) {
587 return isa<Instruction>(V) && classof(cast<Instruction>(V));
590 // Shadow Instruction::setInstructionSubclassData with a private forwarding
591 // method so that subclasses cannot accidentally use it.
592 void setInstructionSubclassData(unsigned short D) {
593 Instruction::setInstructionSubclassData(D);
598 struct OperandTraits<AtomicCmpXchgInst> :
599 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
602 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
604 //===----------------------------------------------------------------------===//
605 // AtomicRMWInst Class
606 //===----------------------------------------------------------------------===//
608 /// AtomicRMWInst - an instruction that atomically reads a memory location,
609 /// combines it with another value, and then stores the result back. Returns
612 class AtomicRMWInst : public Instruction {
613 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
615 AtomicRMWInst *clone_impl() const override;
617 /// This enumeration lists the possible modifications atomicrmw can make. In
618 /// the descriptions, 'p' is the pointer to the instruction's memory location,
619 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
620 /// instruction. These instructions always return 'old'.
636 /// *p = old >signed v ? old : v
638 /// *p = old <signed v ? old : v
640 /// *p = old >unsigned v ? old : v
642 /// *p = old <unsigned v ? old : v
650 // allocate space for exactly two operands
651 void *operator new(size_t s) {
652 return User::operator new(s, 2);
654 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
655 AtomicOrdering Ordering, SynchronizationScope SynchScope,
656 Instruction *InsertBefore = nullptr);
657 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
658 AtomicOrdering Ordering, SynchronizationScope SynchScope,
659 BasicBlock *InsertAtEnd);
661 BinOp getOperation() const {
662 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
665 void setOperation(BinOp Operation) {
666 unsigned short SubclassData = getSubclassDataFromInstruction();
667 setInstructionSubclassData((SubclassData & 31) |
671 /// isVolatile - Return true if this is a RMW on a volatile memory location.
673 bool isVolatile() const {
674 return getSubclassDataFromInstruction() & 1;
677 /// setVolatile - Specify whether this is a volatile RMW or not.
679 void setVolatile(bool V) {
680 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
684 /// Transparently provide more efficient getOperand methods.
685 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
687 /// Set the ordering constraint on this RMW.
688 void setOrdering(AtomicOrdering Ordering) {
689 assert(Ordering != NotAtomic &&
690 "atomicrmw instructions can only be atomic.");
691 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
695 /// Specify whether this RMW orders other operations with respect to all
696 /// concurrently executing threads, or only with respect to signal handlers
697 /// executing in the same thread.
698 void setSynchScope(SynchronizationScope SynchScope) {
699 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
703 /// Returns the ordering constraint on this RMW.
704 AtomicOrdering getOrdering() const {
705 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
708 /// Returns whether this RMW is atomic between threads or only within a
710 SynchronizationScope getSynchScope() const {
711 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
714 Value *getPointerOperand() { return getOperand(0); }
715 const Value *getPointerOperand() const { return getOperand(0); }
716 static unsigned getPointerOperandIndex() { return 0U; }
718 Value *getValOperand() { return getOperand(1); }
719 const Value *getValOperand() const { return getOperand(1); }
721 /// \brief Returns the address space of the pointer operand.
722 unsigned getPointerAddressSpace() const {
723 return getPointerOperand()->getType()->getPointerAddressSpace();
726 // Methods for support type inquiry through isa, cast, and dyn_cast:
727 static inline bool classof(const Instruction *I) {
728 return I->getOpcode() == Instruction::AtomicRMW;
730 static inline bool classof(const Value *V) {
731 return isa<Instruction>(V) && classof(cast<Instruction>(V));
734 void Init(BinOp Operation, Value *Ptr, Value *Val,
735 AtomicOrdering Ordering, SynchronizationScope SynchScope);
736 // Shadow Instruction::setInstructionSubclassData with a private forwarding
737 // method so that subclasses cannot accidentally use it.
738 void setInstructionSubclassData(unsigned short D) {
739 Instruction::setInstructionSubclassData(D);
744 struct OperandTraits<AtomicRMWInst>
745 : public FixedNumOperandTraits<AtomicRMWInst,2> {
748 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
750 //===----------------------------------------------------------------------===//
751 // GetElementPtrInst Class
752 //===----------------------------------------------------------------------===//
754 // checkGEPType - Simple wrapper function to give a better assertion failure
755 // message on bad indexes for a gep instruction.
757 inline Type *checkGEPType(Type *Ty) {
758 assert(Ty && "Invalid GetElementPtrInst indices for type!");
762 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
763 /// access elements of arrays and structs
765 class GetElementPtrInst : public Instruction {
766 GetElementPtrInst(const GetElementPtrInst &GEPI);
767 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
769 /// Constructors - Create a getelementptr instruction with a base pointer an
770 /// list of indices. The first ctor can optionally insert before an existing
771 /// instruction, the second appends the new instruction to the specified
773 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
774 unsigned Values, const Twine &NameStr,
775 Instruction *InsertBefore);
776 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
777 unsigned Values, const Twine &NameStr,
778 BasicBlock *InsertAtEnd);
780 GetElementPtrInst *clone_impl() const override;
782 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
783 const Twine &NameStr = "",
784 Instruction *InsertBefore = nullptr) {
785 unsigned Values = 1 + unsigned(IdxList.size());
787 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
789 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
790 const Twine &NameStr,
791 BasicBlock *InsertAtEnd) {
792 unsigned Values = 1 + unsigned(IdxList.size());
794 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
797 /// Create an "inbounds" getelementptr. See the documentation for the
798 /// "inbounds" flag in LangRef.html for details.
799 static GetElementPtrInst *CreateInBounds(Value *Ptr,
800 ArrayRef<Value *> IdxList,
801 const Twine &NameStr = "",
802 Instruction *InsertBefore = nullptr){
803 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
804 GEP->setIsInBounds(true);
807 static GetElementPtrInst *CreateInBounds(Value *Ptr,
808 ArrayRef<Value *> IdxList,
809 const Twine &NameStr,
810 BasicBlock *InsertAtEnd) {
811 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
812 GEP->setIsInBounds(true);
816 /// Transparently provide more efficient getOperand methods.
817 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
819 // getType - Overload to return most specific sequential type.
820 SequentialType *getType() const {
821 return cast<SequentialType>(Instruction::getType());
824 /// \brief Returns the address space of this instruction's pointer type.
825 unsigned getAddressSpace() const {
826 // Note that this is always the same as the pointer operand's address space
827 // and that is cheaper to compute, so cheat here.
828 return getPointerAddressSpace();
831 /// getIndexedType - Returns the type of the element that would be loaded with
832 /// a load instruction with the specified parameters.
834 /// Null is returned if the indices are invalid for the specified
837 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
838 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
839 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
841 inline op_iterator idx_begin() { return op_begin()+1; }
842 inline const_op_iterator idx_begin() const { return op_begin()+1; }
843 inline op_iterator idx_end() { return op_end(); }
844 inline const_op_iterator idx_end() const { return op_end(); }
846 Value *getPointerOperand() {
847 return getOperand(0);
849 const Value *getPointerOperand() const {
850 return getOperand(0);
852 static unsigned getPointerOperandIndex() {
853 return 0U; // get index for modifying correct operand.
856 /// getPointerOperandType - Method to return the pointer operand as a
858 Type *getPointerOperandType() const {
859 return getPointerOperand()->getType();
862 /// \brief Returns the address space of the pointer operand.
863 unsigned getPointerAddressSpace() const {
864 return getPointerOperandType()->getPointerAddressSpace();
867 /// GetGEPReturnType - Returns the pointer type returned by the GEP
868 /// instruction, which may be a vector of pointers.
869 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
870 Type *PtrTy = PointerType::get(checkGEPType(
871 getIndexedType(Ptr->getType(), IdxList)),
872 Ptr->getType()->getPointerAddressSpace());
874 if (Ptr->getType()->isVectorTy()) {
875 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
876 return VectorType::get(PtrTy, NumElem);
883 unsigned getNumIndices() const { // Note: always non-negative
884 return getNumOperands() - 1;
887 bool hasIndices() const {
888 return getNumOperands() > 1;
891 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
892 /// zeros. If so, the result pointer and the first operand have the same
893 /// value, just potentially different types.
894 bool hasAllZeroIndices() const;
896 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
897 /// constant integers. If so, the result pointer and the first operand have
898 /// a constant offset between them.
899 bool hasAllConstantIndices() const;
901 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
902 /// See LangRef.html for the meaning of inbounds on a getelementptr.
903 void setIsInBounds(bool b = true);
905 /// isInBounds - Determine whether the GEP has the inbounds flag.
906 bool isInBounds() const;
908 /// \brief Accumulate the constant address offset of this GEP if possible.
910 /// This routine accepts an APInt into which it will accumulate the constant
911 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
912 /// all-constant, it returns false and the value of the offset APInt is
913 /// undefined (it is *not* preserved!). The APInt passed into this routine
914 /// must be at least as wide as the IntPtr type for the address space of
915 /// the base GEP pointer.
916 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
918 // Methods for support type inquiry through isa, cast, and dyn_cast:
919 static inline bool classof(const Instruction *I) {
920 return (I->getOpcode() == Instruction::GetElementPtr);
922 static inline bool classof(const Value *V) {
923 return isa<Instruction>(V) && classof(cast<Instruction>(V));
928 struct OperandTraits<GetElementPtrInst> :
929 public VariadicOperandTraits<GetElementPtrInst, 1> {
932 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
933 ArrayRef<Value *> IdxList,
935 const Twine &NameStr,
936 Instruction *InsertBefore)
937 : Instruction(getGEPReturnType(Ptr, IdxList),
939 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
940 Values, InsertBefore) {
941 init(Ptr, IdxList, NameStr);
943 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
944 ArrayRef<Value *> IdxList,
946 const Twine &NameStr,
947 BasicBlock *InsertAtEnd)
948 : Instruction(getGEPReturnType(Ptr, IdxList),
950 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
951 Values, InsertAtEnd) {
952 init(Ptr, IdxList, NameStr);
956 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
959 //===----------------------------------------------------------------------===//
961 //===----------------------------------------------------------------------===//
963 /// This instruction compares its operands according to the predicate given
964 /// to the constructor. It only operates on integers or pointers. The operands
965 /// must be identical types.
966 /// \brief Represent an integer comparison operator.
967 class ICmpInst: public CmpInst {
969 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
970 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
971 "Invalid ICmp predicate value");
972 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
973 "Both operands to ICmp instruction are not of the same type!");
974 // Check that the operands are the right type
975 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
976 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
977 "Invalid operand types for ICmp instruction");
981 /// \brief Clone an identical ICmpInst
982 ICmpInst *clone_impl() const override;
984 /// \brief Constructor with insert-before-instruction semantics.
986 Instruction *InsertBefore, ///< Where to insert
987 Predicate pred, ///< The predicate to use for the comparison
988 Value *LHS, ///< The left-hand-side of the expression
989 Value *RHS, ///< The right-hand-side of the expression
990 const Twine &NameStr = "" ///< Name of the instruction
991 ) : CmpInst(makeCmpResultType(LHS->getType()),
992 Instruction::ICmp, pred, LHS, RHS, NameStr,
999 /// \brief Constructor with insert-at-end semantics.
1001 BasicBlock &InsertAtEnd, ///< Block to insert into.
1002 Predicate pred, ///< The predicate to use for the comparison
1003 Value *LHS, ///< The left-hand-side of the expression
1004 Value *RHS, ///< The right-hand-side of the expression
1005 const Twine &NameStr = "" ///< Name of the instruction
1006 ) : CmpInst(makeCmpResultType(LHS->getType()),
1007 Instruction::ICmp, pred, LHS, RHS, NameStr,
1014 /// \brief Constructor with no-insertion semantics
1016 Predicate pred, ///< The predicate to use for the comparison
1017 Value *LHS, ///< The left-hand-side of the expression
1018 Value *RHS, ///< The right-hand-side of the expression
1019 const Twine &NameStr = "" ///< Name of the instruction
1020 ) : CmpInst(makeCmpResultType(LHS->getType()),
1021 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1027 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1028 /// @returns the predicate that would be the result if the operand were
1029 /// regarded as signed.
1030 /// \brief Return the signed version of the predicate
1031 Predicate getSignedPredicate() const {
1032 return getSignedPredicate(getPredicate());
1035 /// This is a static version that you can use without an instruction.
1036 /// \brief Return the signed version of the predicate.
1037 static Predicate getSignedPredicate(Predicate pred);
1039 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1040 /// @returns the predicate that would be the result if the operand were
1041 /// regarded as unsigned.
1042 /// \brief Return the unsigned version of the predicate
1043 Predicate getUnsignedPredicate() const {
1044 return getUnsignedPredicate(getPredicate());
1047 /// This is a static version that you can use without an instruction.
1048 /// \brief Return the unsigned version of the predicate.
1049 static Predicate getUnsignedPredicate(Predicate pred);
1051 /// isEquality - Return true if this predicate is either EQ or NE. This also
1052 /// tests for commutativity.
1053 static bool isEquality(Predicate P) {
1054 return P == ICMP_EQ || P == ICMP_NE;
1057 /// isEquality - Return true if this predicate is either EQ or NE. This also
1058 /// tests for commutativity.
1059 bool isEquality() const {
1060 return isEquality(getPredicate());
1063 /// @returns true if the predicate of this ICmpInst is commutative
1064 /// \brief Determine if this relation is commutative.
1065 bool isCommutative() const { return isEquality(); }
1067 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1069 bool isRelational() const {
1070 return !isEquality();
1073 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1075 static bool isRelational(Predicate P) {
1076 return !isEquality(P);
1079 /// Initialize a set of values that all satisfy the predicate with C.
1080 /// \brief Make a ConstantRange for a relation with a constant value.
1081 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1083 /// Exchange the two operands to this instruction in such a way that it does
1084 /// not modify the semantics of the instruction. The predicate value may be
1085 /// changed to retain the same result if the predicate is order dependent
1087 /// \brief Swap operands and adjust predicate.
1088 void swapOperands() {
1089 setPredicate(getSwappedPredicate());
1090 Op<0>().swap(Op<1>());
1093 // Methods for support type inquiry through isa, cast, and dyn_cast:
1094 static inline bool classof(const Instruction *I) {
1095 return I->getOpcode() == Instruction::ICmp;
1097 static inline bool classof(const Value *V) {
1098 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1103 //===----------------------------------------------------------------------===//
1105 //===----------------------------------------------------------------------===//
1107 /// This instruction compares its operands according to the predicate given
1108 /// to the constructor. It only operates on floating point values or packed
1109 /// vectors of floating point values. The operands must be identical types.
1110 /// \brief Represents a floating point comparison operator.
1111 class FCmpInst: public CmpInst {
1113 /// \brief Clone an identical FCmpInst
1114 FCmpInst *clone_impl() const override;
1116 /// \brief Constructor with insert-before-instruction semantics.
1118 Instruction *InsertBefore, ///< Where to insert
1119 Predicate pred, ///< The predicate to use for the comparison
1120 Value *LHS, ///< The left-hand-side of the expression
1121 Value *RHS, ///< The right-hand-side of the expression
1122 const Twine &NameStr = "" ///< Name of the instruction
1123 ) : CmpInst(makeCmpResultType(LHS->getType()),
1124 Instruction::FCmp, pred, LHS, RHS, NameStr,
1126 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1127 "Invalid FCmp predicate value");
1128 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1129 "Both operands to FCmp instruction are not of the same type!");
1130 // Check that the operands are the right type
1131 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1132 "Invalid operand types for FCmp instruction");
1135 /// \brief Constructor with insert-at-end semantics.
1137 BasicBlock &InsertAtEnd, ///< Block to insert into.
1138 Predicate pred, ///< The predicate to use for the comparison
1139 Value *LHS, ///< The left-hand-side of the expression
1140 Value *RHS, ///< The right-hand-side of the expression
1141 const Twine &NameStr = "" ///< Name of the instruction
1142 ) : CmpInst(makeCmpResultType(LHS->getType()),
1143 Instruction::FCmp, pred, LHS, RHS, NameStr,
1145 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1146 "Invalid FCmp predicate value");
1147 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1148 "Both operands to FCmp instruction are not of the same type!");
1149 // Check that the operands are the right type
1150 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1151 "Invalid operand types for FCmp instruction");
1154 /// \brief Constructor with no-insertion semantics
1156 Predicate pred, ///< The predicate to use for the comparison
1157 Value *LHS, ///< The left-hand-side of the expression
1158 Value *RHS, ///< The right-hand-side of the expression
1159 const Twine &NameStr = "" ///< Name of the instruction
1160 ) : CmpInst(makeCmpResultType(LHS->getType()),
1161 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1162 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1163 "Invalid FCmp predicate value");
1164 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1165 "Both operands to FCmp instruction are not of the same type!");
1166 // Check that the operands are the right type
1167 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1168 "Invalid operand types for FCmp instruction");
1171 /// @returns true if the predicate of this instruction is EQ or NE.
1172 /// \brief Determine if this is an equality predicate.
1173 bool isEquality() const {
1174 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1175 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1178 /// @returns true if the predicate of this instruction is commutative.
1179 /// \brief Determine if this is a commutative predicate.
1180 bool isCommutative() const {
1181 return isEquality() ||
1182 getPredicate() == FCMP_FALSE ||
1183 getPredicate() == FCMP_TRUE ||
1184 getPredicate() == FCMP_ORD ||
1185 getPredicate() == FCMP_UNO;
1188 /// @returns true if the predicate is relational (not EQ or NE).
1189 /// \brief Determine if this a relational predicate.
1190 bool isRelational() const { return !isEquality(); }
1192 /// Exchange the two operands to this instruction in such a way that it does
1193 /// not modify the semantics of the instruction. The predicate value may be
1194 /// changed to retain the same result if the predicate is order dependent
1196 /// \brief Swap operands and adjust predicate.
1197 void swapOperands() {
1198 setPredicate(getSwappedPredicate());
1199 Op<0>().swap(Op<1>());
1202 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1203 static inline bool classof(const Instruction *I) {
1204 return I->getOpcode() == Instruction::FCmp;
1206 static inline bool classof(const Value *V) {
1207 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1211 //===----------------------------------------------------------------------===//
1212 /// CallInst - This class represents a function call, abstracting a target
1213 /// machine's calling convention. This class uses low bit of the SubClassData
1214 /// field to indicate whether or not this is a tail call. The rest of the bits
1215 /// hold the calling convention of the call.
1217 class CallInst : public Instruction {
1218 AttributeSet AttributeList; ///< parameter attributes for call
1219 CallInst(const CallInst &CI);
1220 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1221 void init(Value *Func, const Twine &NameStr);
1223 /// Construct a CallInst given a range of arguments.
1224 /// \brief Construct a CallInst from a range of arguments
1225 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1226 const Twine &NameStr, Instruction *InsertBefore);
1228 /// Construct a CallInst given a range of arguments.
1229 /// \brief Construct a CallInst from a range of arguments
1230 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1231 const Twine &NameStr, BasicBlock *InsertAtEnd);
1233 explicit CallInst(Value *F, const Twine &NameStr,
1234 Instruction *InsertBefore);
1235 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1237 CallInst *clone_impl() const override;
1239 static CallInst *Create(Value *Func,
1240 ArrayRef<Value *> Args,
1241 const Twine &NameStr = "",
1242 Instruction *InsertBefore = nullptr) {
1243 return new(unsigned(Args.size() + 1))
1244 CallInst(Func, Args, NameStr, InsertBefore);
1246 static CallInst *Create(Value *Func,
1247 ArrayRef<Value *> Args,
1248 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1249 return new(unsigned(Args.size() + 1))
1250 CallInst(Func, Args, NameStr, InsertAtEnd);
1252 static CallInst *Create(Value *F, const Twine &NameStr = "",
1253 Instruction *InsertBefore = nullptr) {
1254 return new(1) CallInst(F, NameStr, InsertBefore);
1256 static CallInst *Create(Value *F, const Twine &NameStr,
1257 BasicBlock *InsertAtEnd) {
1258 return new(1) CallInst(F, NameStr, InsertAtEnd);
1260 /// CreateMalloc - Generate the IR for a call to malloc:
1261 /// 1. Compute the malloc call's argument as the specified type's size,
1262 /// possibly multiplied by the array size if the array size is not
1264 /// 2. Call malloc with that argument.
1265 /// 3. Bitcast the result of the malloc call to the specified type.
1266 static Instruction *CreateMalloc(Instruction *InsertBefore,
1267 Type *IntPtrTy, Type *AllocTy,
1268 Value *AllocSize, Value *ArraySize = nullptr,
1269 Function* MallocF = nullptr,
1270 const Twine &Name = "");
1271 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1272 Type *IntPtrTy, Type *AllocTy,
1273 Value *AllocSize, Value *ArraySize = nullptr,
1274 Function* MallocF = nullptr,
1275 const Twine &Name = "");
1276 /// CreateFree - Generate the IR for a call to the builtin free function.
1277 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1278 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1282 // Note that 'musttail' implies 'tail'.
1283 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1284 TailCallKind getTailCallKind() const {
1285 return TailCallKind(getSubclassDataFromInstruction() & 3);
1287 bool isTailCall() const {
1288 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1290 bool isMustTailCall() const {
1291 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1293 void setTailCall(bool isTC = true) {
1294 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1295 unsigned(isTC ? TCK_Tail : TCK_None));
1297 void setTailCallKind(TailCallKind TCK) {
1298 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1302 /// Provide fast operand accessors
1303 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1305 /// getNumArgOperands - Return the number of call arguments.
1307 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1309 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1311 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1312 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1314 /// arg_operands - iteration adapter for range-for loops.
1315 iterator_range<op_iterator> arg_operands() {
1316 // The last operand in the op list is the callee - it's not one of the args
1317 // so we don't want to iterate over it.
1318 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1321 /// arg_operands - iteration adapter for range-for loops.
1322 iterator_range<const_op_iterator> arg_operands() const {
1323 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1326 /// \brief Wrappers for getting the \c Use of a call argument.
1327 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1328 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1330 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1332 CallingConv::ID getCallingConv() const {
1333 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1335 void setCallingConv(CallingConv::ID CC) {
1336 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1337 (static_cast<unsigned>(CC) << 2));
1340 /// getAttributes - Return the parameter attributes for this call.
1342 const AttributeSet &getAttributes() const { return AttributeList; }
1344 /// setAttributes - Set the parameter attributes for this call.
1346 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1348 /// addAttribute - adds the attribute to the list of attributes.
1349 void addAttribute(unsigned i, Attribute::AttrKind attr);
1351 /// removeAttribute - removes the attribute from the list of attributes.
1352 void removeAttribute(unsigned i, Attribute attr);
1354 /// \brief Determine whether this call has the given attribute.
1355 bool hasFnAttr(Attribute::AttrKind A) const {
1356 assert(A != Attribute::NoBuiltin &&
1357 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1358 return hasFnAttrImpl(A);
1361 /// \brief Determine whether the call or the callee has the given attributes.
1362 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1364 /// \brief Extract the alignment for a call or parameter (0=unknown).
1365 unsigned getParamAlignment(unsigned i) const {
1366 return AttributeList.getParamAlignment(i);
1369 /// \brief Return true if the call should not be treated as a call to a
1371 bool isNoBuiltin() const {
1372 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1373 !hasFnAttrImpl(Attribute::Builtin);
1376 /// \brief Return true if the call should not be inlined.
1377 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1378 void setIsNoInline() {
1379 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1382 /// \brief Return true if the call can return twice
1383 bool canReturnTwice() const {
1384 return hasFnAttr(Attribute::ReturnsTwice);
1386 void setCanReturnTwice() {
1387 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1390 /// \brief Determine if the call does not access memory.
1391 bool doesNotAccessMemory() const {
1392 return hasFnAttr(Attribute::ReadNone);
1394 void setDoesNotAccessMemory() {
1395 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1398 /// \brief Determine if the call does not access or only reads memory.
1399 bool onlyReadsMemory() const {
1400 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1402 void setOnlyReadsMemory() {
1403 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1406 /// \brief Determine if the call cannot return.
1407 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1408 void setDoesNotReturn() {
1409 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1412 /// \brief Determine if the call cannot unwind.
1413 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1414 void setDoesNotThrow() {
1415 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1418 /// \brief Determine if the call cannot be duplicated.
1419 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1420 void setCannotDuplicate() {
1421 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1424 /// \brief Determine if the call returns a structure through first
1425 /// pointer argument.
1426 bool hasStructRetAttr() const {
1427 // Be friendly and also check the callee.
1428 return paramHasAttr(1, Attribute::StructRet);
1431 /// \brief Determine if any call argument is an aggregate passed by value.
1432 bool hasByValArgument() const {
1433 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1436 /// getCalledFunction - Return the function called, or null if this is an
1437 /// indirect function invocation.
1439 Function *getCalledFunction() const {
1440 return dyn_cast<Function>(Op<-1>());
1443 /// getCalledValue - Get a pointer to the function that is invoked by this
1445 const Value *getCalledValue() const { return Op<-1>(); }
1446 Value *getCalledValue() { return Op<-1>(); }
1448 /// setCalledFunction - Set the function called.
1449 void setCalledFunction(Value* Fn) {
1453 /// isInlineAsm - Check if this call is an inline asm statement.
1454 bool isInlineAsm() const {
1455 return isa<InlineAsm>(Op<-1>());
1458 // Methods for support type inquiry through isa, cast, and dyn_cast:
1459 static inline bool classof(const Instruction *I) {
1460 return I->getOpcode() == Instruction::Call;
1462 static inline bool classof(const Value *V) {
1463 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1467 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1469 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1470 // method so that subclasses cannot accidentally use it.
1471 void setInstructionSubclassData(unsigned short D) {
1472 Instruction::setInstructionSubclassData(D);
1477 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1480 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1481 const Twine &NameStr, BasicBlock *InsertAtEnd)
1482 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1483 ->getElementType())->getReturnType(),
1485 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1486 unsigned(Args.size() + 1), InsertAtEnd) {
1487 init(Func, Args, NameStr);
1490 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1491 const Twine &NameStr, Instruction *InsertBefore)
1492 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1493 ->getElementType())->getReturnType(),
1495 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1496 unsigned(Args.size() + 1), InsertBefore) {
1497 init(Func, Args, NameStr);
1501 // Note: if you get compile errors about private methods then
1502 // please update your code to use the high-level operand
1503 // interfaces. See line 943 above.
1504 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1506 //===----------------------------------------------------------------------===//
1508 //===----------------------------------------------------------------------===//
1510 /// SelectInst - This class represents the LLVM 'select' instruction.
1512 class SelectInst : public Instruction {
1513 void init(Value *C, Value *S1, Value *S2) {
1514 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1520 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1521 Instruction *InsertBefore)
1522 : Instruction(S1->getType(), Instruction::Select,
1523 &Op<0>(), 3, InsertBefore) {
1527 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1528 BasicBlock *InsertAtEnd)
1529 : Instruction(S1->getType(), Instruction::Select,
1530 &Op<0>(), 3, InsertAtEnd) {
1535 SelectInst *clone_impl() const override;
1537 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1538 const Twine &NameStr = "",
1539 Instruction *InsertBefore = nullptr) {
1540 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1542 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1543 const Twine &NameStr,
1544 BasicBlock *InsertAtEnd) {
1545 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1548 const Value *getCondition() const { return Op<0>(); }
1549 const Value *getTrueValue() const { return Op<1>(); }
1550 const Value *getFalseValue() const { return Op<2>(); }
1551 Value *getCondition() { return Op<0>(); }
1552 Value *getTrueValue() { return Op<1>(); }
1553 Value *getFalseValue() { return Op<2>(); }
1555 /// areInvalidOperands - Return a string if the specified operands are invalid
1556 /// for a select operation, otherwise return null.
1557 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1559 /// Transparently provide more efficient getOperand methods.
1560 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1562 OtherOps getOpcode() const {
1563 return static_cast<OtherOps>(Instruction::getOpcode());
1566 // Methods for support type inquiry through isa, cast, and dyn_cast:
1567 static inline bool classof(const Instruction *I) {
1568 return I->getOpcode() == Instruction::Select;
1570 static inline bool classof(const Value *V) {
1571 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1576 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1579 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1581 //===----------------------------------------------------------------------===//
1583 //===----------------------------------------------------------------------===//
1585 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1586 /// an argument of the specified type given a va_list and increments that list
1588 class VAArgInst : public UnaryInstruction {
1590 VAArgInst *clone_impl() const override;
1593 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1594 Instruction *InsertBefore = nullptr)
1595 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1598 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1599 BasicBlock *InsertAtEnd)
1600 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1604 Value *getPointerOperand() { return getOperand(0); }
1605 const Value *getPointerOperand() const { return getOperand(0); }
1606 static unsigned getPointerOperandIndex() { return 0U; }
1608 // Methods for support type inquiry through isa, cast, and dyn_cast:
1609 static inline bool classof(const Instruction *I) {
1610 return I->getOpcode() == VAArg;
1612 static inline bool classof(const Value *V) {
1613 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1617 //===----------------------------------------------------------------------===//
1618 // ExtractElementInst Class
1619 //===----------------------------------------------------------------------===//
1621 /// ExtractElementInst - This instruction extracts a single (scalar)
1622 /// element from a VectorType value
1624 class ExtractElementInst : public Instruction {
1625 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1626 Instruction *InsertBefore = nullptr);
1627 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1628 BasicBlock *InsertAtEnd);
1630 ExtractElementInst *clone_impl() const override;
1633 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1634 const Twine &NameStr = "",
1635 Instruction *InsertBefore = nullptr) {
1636 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1638 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1639 const Twine &NameStr,
1640 BasicBlock *InsertAtEnd) {
1641 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1644 /// isValidOperands - Return true if an extractelement instruction can be
1645 /// formed with the specified operands.
1646 static bool isValidOperands(const Value *Vec, const Value *Idx);
1648 Value *getVectorOperand() { return Op<0>(); }
1649 Value *getIndexOperand() { return Op<1>(); }
1650 const Value *getVectorOperand() const { return Op<0>(); }
1651 const Value *getIndexOperand() const { return Op<1>(); }
1653 VectorType *getVectorOperandType() const {
1654 return cast<VectorType>(getVectorOperand()->getType());
1658 /// Transparently provide more efficient getOperand methods.
1659 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1661 // Methods for support type inquiry through isa, cast, and dyn_cast:
1662 static inline bool classof(const Instruction *I) {
1663 return I->getOpcode() == Instruction::ExtractElement;
1665 static inline bool classof(const Value *V) {
1666 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1671 struct OperandTraits<ExtractElementInst> :
1672 public FixedNumOperandTraits<ExtractElementInst, 2> {
1675 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1677 //===----------------------------------------------------------------------===//
1678 // InsertElementInst Class
1679 //===----------------------------------------------------------------------===//
1681 /// InsertElementInst - This instruction inserts a single (scalar)
1682 /// element into a VectorType value
1684 class InsertElementInst : public Instruction {
1685 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1686 const Twine &NameStr = "",
1687 Instruction *InsertBefore = nullptr);
1688 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1689 const Twine &NameStr, BasicBlock *InsertAtEnd);
1691 InsertElementInst *clone_impl() const override;
1694 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1695 const Twine &NameStr = "",
1696 Instruction *InsertBefore = nullptr) {
1697 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1699 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1700 const Twine &NameStr,
1701 BasicBlock *InsertAtEnd) {
1702 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1705 /// isValidOperands - Return true if an insertelement instruction can be
1706 /// formed with the specified operands.
1707 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1710 /// getType - Overload to return most specific vector type.
1712 VectorType *getType() const {
1713 return cast<VectorType>(Instruction::getType());
1716 /// Transparently provide more efficient getOperand methods.
1717 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1719 // Methods for support type inquiry through isa, cast, and dyn_cast:
1720 static inline bool classof(const Instruction *I) {
1721 return I->getOpcode() == Instruction::InsertElement;
1723 static inline bool classof(const Value *V) {
1724 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1729 struct OperandTraits<InsertElementInst> :
1730 public FixedNumOperandTraits<InsertElementInst, 3> {
1733 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1735 //===----------------------------------------------------------------------===//
1736 // ShuffleVectorInst Class
1737 //===----------------------------------------------------------------------===//
1739 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1742 class ShuffleVectorInst : public Instruction {
1744 ShuffleVectorInst *clone_impl() const override;
1747 // allocate space for exactly three operands
1748 void *operator new(size_t s) {
1749 return User::operator new(s, 3);
1751 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1752 const Twine &NameStr = "",
1753 Instruction *InsertBefor = nullptr);
1754 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1755 const Twine &NameStr, BasicBlock *InsertAtEnd);
1757 /// isValidOperands - Return true if a shufflevector instruction can be
1758 /// formed with the specified operands.
1759 static bool isValidOperands(const Value *V1, const Value *V2,
1762 /// getType - Overload to return most specific vector type.
1764 VectorType *getType() const {
1765 return cast<VectorType>(Instruction::getType());
1768 /// Transparently provide more efficient getOperand methods.
1769 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1771 Constant *getMask() const {
1772 return cast<Constant>(getOperand(2));
1775 /// getMaskValue - Return the index from the shuffle mask for the specified
1776 /// output result. This is either -1 if the element is undef or a number less
1777 /// than 2*numelements.
1778 static int getMaskValue(Constant *Mask, unsigned i);
1780 int getMaskValue(unsigned i) const {
1781 return getMaskValue(getMask(), i);
1784 /// getShuffleMask - Return the full mask for this instruction, where each
1785 /// element is the element number and undef's are returned as -1.
1786 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1788 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1789 return getShuffleMask(getMask(), Result);
1792 SmallVector<int, 16> getShuffleMask() const {
1793 SmallVector<int, 16> Mask;
1794 getShuffleMask(Mask);
1799 // Methods for support type inquiry through isa, cast, and dyn_cast:
1800 static inline bool classof(const Instruction *I) {
1801 return I->getOpcode() == Instruction::ShuffleVector;
1803 static inline bool classof(const Value *V) {
1804 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1809 struct OperandTraits<ShuffleVectorInst> :
1810 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1813 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1815 //===----------------------------------------------------------------------===//
1816 // ExtractValueInst Class
1817 //===----------------------------------------------------------------------===//
1819 /// ExtractValueInst - This instruction extracts a struct member or array
1820 /// element value from an aggregate value.
1822 class ExtractValueInst : public UnaryInstruction {
1823 SmallVector<unsigned, 4> Indices;
1825 ExtractValueInst(const ExtractValueInst &EVI);
1826 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1828 /// Constructors - Create a extractvalue instruction with a base aggregate
1829 /// value and a list of indices. The first ctor can optionally insert before
1830 /// an existing instruction, the second appends the new instruction to the
1831 /// specified BasicBlock.
1832 inline ExtractValueInst(Value *Agg,
1833 ArrayRef<unsigned> Idxs,
1834 const Twine &NameStr,
1835 Instruction *InsertBefore);
1836 inline ExtractValueInst(Value *Agg,
1837 ArrayRef<unsigned> Idxs,
1838 const Twine &NameStr, BasicBlock *InsertAtEnd);
1840 // allocate space for exactly one operand
1841 void *operator new(size_t s) {
1842 return User::operator new(s, 1);
1845 ExtractValueInst *clone_impl() const override;
1848 static ExtractValueInst *Create(Value *Agg,
1849 ArrayRef<unsigned> Idxs,
1850 const Twine &NameStr = "",
1851 Instruction *InsertBefore = nullptr) {
1853 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1855 static ExtractValueInst *Create(Value *Agg,
1856 ArrayRef<unsigned> Idxs,
1857 const Twine &NameStr,
1858 BasicBlock *InsertAtEnd) {
1859 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1862 /// getIndexedType - Returns the type of the element that would be extracted
1863 /// with an extractvalue instruction with the specified parameters.
1865 /// Null is returned if the indices are invalid for the specified type.
1866 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1868 typedef const unsigned* idx_iterator;
1869 inline idx_iterator idx_begin() const { return Indices.begin(); }
1870 inline idx_iterator idx_end() const { return Indices.end(); }
1872 Value *getAggregateOperand() {
1873 return getOperand(0);
1875 const Value *getAggregateOperand() const {
1876 return getOperand(0);
1878 static unsigned getAggregateOperandIndex() {
1879 return 0U; // get index for modifying correct operand
1882 ArrayRef<unsigned> getIndices() const {
1886 unsigned getNumIndices() const {
1887 return (unsigned)Indices.size();
1890 bool hasIndices() const {
1894 // Methods for support type inquiry through isa, cast, and dyn_cast:
1895 static inline bool classof(const Instruction *I) {
1896 return I->getOpcode() == Instruction::ExtractValue;
1898 static inline bool classof(const Value *V) {
1899 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1903 ExtractValueInst::ExtractValueInst(Value *Agg,
1904 ArrayRef<unsigned> Idxs,
1905 const Twine &NameStr,
1906 Instruction *InsertBefore)
1907 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1908 ExtractValue, Agg, InsertBefore) {
1909 init(Idxs, NameStr);
1911 ExtractValueInst::ExtractValueInst(Value *Agg,
1912 ArrayRef<unsigned> Idxs,
1913 const Twine &NameStr,
1914 BasicBlock *InsertAtEnd)
1915 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1916 ExtractValue, Agg, InsertAtEnd) {
1917 init(Idxs, NameStr);
1921 //===----------------------------------------------------------------------===//
1922 // InsertValueInst Class
1923 //===----------------------------------------------------------------------===//
1925 /// InsertValueInst - This instruction inserts a struct field of array element
1926 /// value into an aggregate value.
1928 class InsertValueInst : public Instruction {
1929 SmallVector<unsigned, 4> Indices;
1931 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1932 InsertValueInst(const InsertValueInst &IVI);
1933 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1934 const Twine &NameStr);
1936 /// Constructors - Create a insertvalue instruction with a base aggregate
1937 /// value, a value to insert, and a list of indices. The first ctor can
1938 /// optionally insert before an existing instruction, the second appends
1939 /// the new instruction to the specified BasicBlock.
1940 inline InsertValueInst(Value *Agg, Value *Val,
1941 ArrayRef<unsigned> Idxs,
1942 const Twine &NameStr,
1943 Instruction *InsertBefore);
1944 inline InsertValueInst(Value *Agg, Value *Val,
1945 ArrayRef<unsigned> Idxs,
1946 const Twine &NameStr, BasicBlock *InsertAtEnd);
1948 /// Constructors - These two constructors are convenience methods because one
1949 /// and two index insertvalue instructions are so common.
1950 InsertValueInst(Value *Agg, Value *Val,
1951 unsigned Idx, const Twine &NameStr = "",
1952 Instruction *InsertBefore = nullptr);
1953 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1954 const Twine &NameStr, BasicBlock *InsertAtEnd);
1956 InsertValueInst *clone_impl() const override;
1958 // allocate space for exactly two operands
1959 void *operator new(size_t s) {
1960 return User::operator new(s, 2);
1963 static InsertValueInst *Create(Value *Agg, Value *Val,
1964 ArrayRef<unsigned> Idxs,
1965 const Twine &NameStr = "",
1966 Instruction *InsertBefore = nullptr) {
1967 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1969 static InsertValueInst *Create(Value *Agg, Value *Val,
1970 ArrayRef<unsigned> Idxs,
1971 const Twine &NameStr,
1972 BasicBlock *InsertAtEnd) {
1973 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1976 /// Transparently provide more efficient getOperand methods.
1977 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1979 typedef const unsigned* idx_iterator;
1980 inline idx_iterator idx_begin() const { return Indices.begin(); }
1981 inline idx_iterator idx_end() const { return Indices.end(); }
1983 Value *getAggregateOperand() {
1984 return getOperand(0);
1986 const Value *getAggregateOperand() const {
1987 return getOperand(0);
1989 static unsigned getAggregateOperandIndex() {
1990 return 0U; // get index for modifying correct operand
1993 Value *getInsertedValueOperand() {
1994 return getOperand(1);
1996 const Value *getInsertedValueOperand() const {
1997 return getOperand(1);
1999 static unsigned getInsertedValueOperandIndex() {
2000 return 1U; // get index for modifying correct operand
2003 ArrayRef<unsigned> getIndices() const {
2007 unsigned getNumIndices() const {
2008 return (unsigned)Indices.size();
2011 bool hasIndices() const {
2015 // Methods for support type inquiry through isa, cast, and dyn_cast:
2016 static inline bool classof(const Instruction *I) {
2017 return I->getOpcode() == Instruction::InsertValue;
2019 static inline bool classof(const Value *V) {
2020 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2025 struct OperandTraits<InsertValueInst> :
2026 public FixedNumOperandTraits<InsertValueInst, 2> {
2029 InsertValueInst::InsertValueInst(Value *Agg,
2031 ArrayRef<unsigned> Idxs,
2032 const Twine &NameStr,
2033 Instruction *InsertBefore)
2034 : Instruction(Agg->getType(), InsertValue,
2035 OperandTraits<InsertValueInst>::op_begin(this),
2037 init(Agg, Val, Idxs, NameStr);
2039 InsertValueInst::InsertValueInst(Value *Agg,
2041 ArrayRef<unsigned> Idxs,
2042 const Twine &NameStr,
2043 BasicBlock *InsertAtEnd)
2044 : Instruction(Agg->getType(), InsertValue,
2045 OperandTraits<InsertValueInst>::op_begin(this),
2047 init(Agg, Val, Idxs, NameStr);
2050 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2052 //===----------------------------------------------------------------------===//
2054 //===----------------------------------------------------------------------===//
2056 // PHINode - The PHINode class is used to represent the magical mystical PHI
2057 // node, that can not exist in nature, but can be synthesized in a computer
2058 // scientist's overactive imagination.
2060 class PHINode : public Instruction {
2061 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2062 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2063 /// the number actually in use.
2064 unsigned ReservedSpace;
2065 PHINode(const PHINode &PN);
2066 // allocate space for exactly zero operands
2067 void *operator new(size_t s) {
2068 return User::operator new(s, 0);
2070 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2071 const Twine &NameStr = "",
2072 Instruction *InsertBefore = nullptr)
2073 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2074 ReservedSpace(NumReservedValues) {
2076 OperandList = allocHungoffUses(ReservedSpace);
2079 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2080 BasicBlock *InsertAtEnd)
2081 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2082 ReservedSpace(NumReservedValues) {
2084 OperandList = allocHungoffUses(ReservedSpace);
2087 // allocHungoffUses - this is more complicated than the generic
2088 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2089 // values and pointers to the incoming blocks, all in one allocation.
2090 Use *allocHungoffUses(unsigned) const;
2092 PHINode *clone_impl() const override;
2094 /// Constructors - NumReservedValues is a hint for the number of incoming
2095 /// edges that this phi node will have (use 0 if you really have no idea).
2096 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2097 const Twine &NameStr = "",
2098 Instruction *InsertBefore = nullptr) {
2099 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2101 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2102 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2103 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2107 /// Provide fast operand accessors
2108 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2110 // Block iterator interface. This provides access to the list of incoming
2111 // basic blocks, which parallels the list of incoming values.
2113 typedef BasicBlock **block_iterator;
2114 typedef BasicBlock * const *const_block_iterator;
2116 block_iterator block_begin() {
2118 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2119 return reinterpret_cast<block_iterator>(ref + 1);
2122 const_block_iterator block_begin() const {
2123 const Use::UserRef *ref =
2124 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2125 return reinterpret_cast<const_block_iterator>(ref + 1);
2128 block_iterator block_end() {
2129 return block_begin() + getNumOperands();
2132 const_block_iterator block_end() const {
2133 return block_begin() + getNumOperands();
2136 /// getNumIncomingValues - Return the number of incoming edges
2138 unsigned getNumIncomingValues() const { return getNumOperands(); }
2140 /// getIncomingValue - Return incoming value number x
2142 Value *getIncomingValue(unsigned i) const {
2143 return getOperand(i);
2145 void setIncomingValue(unsigned i, Value *V) {
2148 static unsigned getOperandNumForIncomingValue(unsigned i) {
2151 static unsigned getIncomingValueNumForOperand(unsigned i) {
2155 /// getIncomingBlock - Return incoming basic block number @p i.
2157 BasicBlock *getIncomingBlock(unsigned i) const {
2158 return block_begin()[i];
2161 /// getIncomingBlock - Return incoming basic block corresponding
2162 /// to an operand of the PHI.
2164 BasicBlock *getIncomingBlock(const Use &U) const {
2165 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2166 return getIncomingBlock(unsigned(&U - op_begin()));
2169 /// getIncomingBlock - Return incoming basic block corresponding
2170 /// to value use iterator.
2172 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2173 return getIncomingBlock(I.getUse());
2176 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2177 block_begin()[i] = BB;
2180 /// addIncoming - Add an incoming value to the end of the PHI list
2182 void addIncoming(Value *V, BasicBlock *BB) {
2183 assert(V && "PHI node got a null value!");
2184 assert(BB && "PHI node got a null basic block!");
2185 assert(getType() == V->getType() &&
2186 "All operands to PHI node must be the same type as the PHI node!");
2187 if (NumOperands == ReservedSpace)
2188 growOperands(); // Get more space!
2189 // Initialize some new operands.
2191 setIncomingValue(NumOperands - 1, V);
2192 setIncomingBlock(NumOperands - 1, BB);
2195 /// removeIncomingValue - Remove an incoming value. This is useful if a
2196 /// predecessor basic block is deleted. The value removed is returned.
2198 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2199 /// is true), the PHI node is destroyed and any uses of it are replaced with
2200 /// dummy values. The only time there should be zero incoming values to a PHI
2201 /// node is when the block is dead, so this strategy is sound.
2203 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2205 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2206 int Idx = getBasicBlockIndex(BB);
2207 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2208 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2211 /// getBasicBlockIndex - Return the first index of the specified basic
2212 /// block in the value list for this PHI. Returns -1 if no instance.
2214 int getBasicBlockIndex(const BasicBlock *BB) const {
2215 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2216 if (block_begin()[i] == BB)
2221 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2222 int Idx = getBasicBlockIndex(BB);
2223 assert(Idx >= 0 && "Invalid basic block argument!");
2224 return getIncomingValue(Idx);
2227 /// hasConstantValue - If the specified PHI node always merges together the
2228 /// same value, return the value, otherwise return null.
2229 Value *hasConstantValue() const;
2231 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2232 static inline bool classof(const Instruction *I) {
2233 return I->getOpcode() == Instruction::PHI;
2235 static inline bool classof(const Value *V) {
2236 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2239 void growOperands();
2243 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2246 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2248 //===----------------------------------------------------------------------===//
2249 // LandingPadInst Class
2250 //===----------------------------------------------------------------------===//
2252 //===---------------------------------------------------------------------------
2253 /// LandingPadInst - The landingpad instruction holds all of the information
2254 /// necessary to generate correct exception handling. The landingpad instruction
2255 /// cannot be moved from the top of a landing pad block, which itself is
2256 /// accessible only from the 'unwind' edge of an invoke. This uses the
2257 /// SubclassData field in Value to store whether or not the landingpad is a
2260 class LandingPadInst : public Instruction {
2261 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2262 /// the number actually in use.
2263 unsigned ReservedSpace;
2264 LandingPadInst(const LandingPadInst &LP);
2266 enum ClauseType { Catch, Filter };
2268 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2269 // Allocate space for exactly zero operands.
2270 void *operator new(size_t s) {
2271 return User::operator new(s, 0);
2273 void growOperands(unsigned Size);
2274 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2276 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2277 unsigned NumReservedValues, const Twine &NameStr,
2278 Instruction *InsertBefore);
2279 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2280 unsigned NumReservedValues, const Twine &NameStr,
2281 BasicBlock *InsertAtEnd);
2283 LandingPadInst *clone_impl() const override;
2285 /// Constructors - NumReservedClauses is a hint for the number of incoming
2286 /// clauses that this landingpad will have (use 0 if you really have no idea).
2287 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2288 unsigned NumReservedClauses,
2289 const Twine &NameStr = "",
2290 Instruction *InsertBefore = nullptr);
2291 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2292 unsigned NumReservedClauses,
2293 const Twine &NameStr, BasicBlock *InsertAtEnd);
2296 /// Provide fast operand accessors
2297 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2299 /// getPersonalityFn - Get the personality function associated with this
2301 Value *getPersonalityFn() const { return getOperand(0); }
2303 /// isCleanup - Return 'true' if this landingpad instruction is a
2304 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2305 /// doesn't catch the exception.
2306 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2308 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2309 void setCleanup(bool V) {
2310 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2314 /// addClause - Add a catch or filter clause to the landing pad.
2315 void addClause(Value *ClauseVal);
2317 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2318 /// to determine what type of clause this is.
2319 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2321 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2322 bool isCatch(unsigned Idx) const {
2323 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2326 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2327 bool isFilter(unsigned Idx) const {
2328 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2331 /// getNumClauses - Get the number of clauses for this landing pad.
2332 unsigned getNumClauses() const { return getNumOperands() - 1; }
2334 /// reserveClauses - Grow the size of the operand list to accommodate the new
2335 /// number of clauses.
2336 void reserveClauses(unsigned Size) { growOperands(Size); }
2338 // Methods for support type inquiry through isa, cast, and dyn_cast:
2339 static inline bool classof(const Instruction *I) {
2340 return I->getOpcode() == Instruction::LandingPad;
2342 static inline bool classof(const Value *V) {
2343 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2348 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2351 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2353 //===----------------------------------------------------------------------===//
2355 //===----------------------------------------------------------------------===//
2357 //===---------------------------------------------------------------------------
2358 /// ReturnInst - Return a value (possibly void), from a function. Execution
2359 /// does not continue in this function any longer.
2361 class ReturnInst : public TerminatorInst {
2362 ReturnInst(const ReturnInst &RI);
2365 // ReturnInst constructors:
2366 // ReturnInst() - 'ret void' instruction
2367 // ReturnInst( null) - 'ret void' instruction
2368 // ReturnInst(Value* X) - 'ret X' instruction
2369 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2370 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2371 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2372 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2374 // NOTE: If the Value* passed is of type void then the constructor behaves as
2375 // if it was passed NULL.
2376 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2377 Instruction *InsertBefore = nullptr);
2378 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2379 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2381 ReturnInst *clone_impl() const override;
2383 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2384 Instruction *InsertBefore = nullptr) {
2385 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2387 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2388 BasicBlock *InsertAtEnd) {
2389 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2391 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2392 return new(0) ReturnInst(C, InsertAtEnd);
2394 virtual ~ReturnInst();
2396 /// Provide fast operand accessors
2397 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2399 /// Convenience accessor. Returns null if there is no return value.
2400 Value *getReturnValue() const {
2401 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2404 unsigned getNumSuccessors() const { return 0; }
2406 // Methods for support type inquiry through isa, cast, and dyn_cast:
2407 static inline bool classof(const Instruction *I) {
2408 return (I->getOpcode() == Instruction::Ret);
2410 static inline bool classof(const Value *V) {
2411 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2414 BasicBlock *getSuccessorV(unsigned idx) const override;
2415 unsigned getNumSuccessorsV() const override;
2416 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2420 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2423 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2425 //===----------------------------------------------------------------------===//
2427 //===----------------------------------------------------------------------===//
2429 //===---------------------------------------------------------------------------
2430 /// BranchInst - Conditional or Unconditional Branch instruction.
2432 class BranchInst : public TerminatorInst {
2433 /// Ops list - Branches are strange. The operands are ordered:
2434 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2435 /// they don't have to check for cond/uncond branchness. These are mostly
2436 /// accessed relative from op_end().
2437 BranchInst(const BranchInst &BI);
2439 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2440 // BranchInst(BB *B) - 'br B'
2441 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2442 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2443 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2444 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2445 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2446 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2447 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2448 Instruction *InsertBefore = nullptr);
2449 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2450 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2451 BasicBlock *InsertAtEnd);
2453 BranchInst *clone_impl() const override;
2455 static BranchInst *Create(BasicBlock *IfTrue,
2456 Instruction *InsertBefore = nullptr) {
2457 return new(1) BranchInst(IfTrue, InsertBefore);
2459 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2460 Value *Cond, Instruction *InsertBefore = nullptr) {
2461 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2463 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2464 return new(1) BranchInst(IfTrue, InsertAtEnd);
2466 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2467 Value *Cond, BasicBlock *InsertAtEnd) {
2468 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2471 /// Transparently provide more efficient getOperand methods.
2472 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2474 bool isUnconditional() const { return getNumOperands() == 1; }
2475 bool isConditional() const { return getNumOperands() == 3; }
2477 Value *getCondition() const {
2478 assert(isConditional() && "Cannot get condition of an uncond branch!");
2482 void setCondition(Value *V) {
2483 assert(isConditional() && "Cannot set condition of unconditional branch!");
2487 unsigned getNumSuccessors() const { return 1+isConditional(); }
2489 BasicBlock *getSuccessor(unsigned i) const {
2490 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2491 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2494 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2495 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2496 *(&Op<-1>() - idx) = (Value*)NewSucc;
2499 /// \brief Swap the successors of this branch instruction.
2501 /// Swaps the successors of the branch instruction. This also swaps any
2502 /// branch weight metadata associated with the instruction so that it
2503 /// continues to map correctly to each operand.
2504 void swapSuccessors();
2506 // Methods for support type inquiry through isa, cast, and dyn_cast:
2507 static inline bool classof(const Instruction *I) {
2508 return (I->getOpcode() == Instruction::Br);
2510 static inline bool classof(const Value *V) {
2511 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2514 BasicBlock *getSuccessorV(unsigned idx) const override;
2515 unsigned getNumSuccessorsV() const override;
2516 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2520 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2523 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2525 //===----------------------------------------------------------------------===//
2527 //===----------------------------------------------------------------------===//
2529 //===---------------------------------------------------------------------------
2530 /// SwitchInst - Multiway switch
2532 class SwitchInst : public TerminatorInst {
2533 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2534 unsigned ReservedSpace;
2535 // Operand[0] = Value to switch on
2536 // Operand[1] = Default basic block destination
2537 // Operand[2n ] = Value to match
2538 // Operand[2n+1] = BasicBlock to go to on match
2539 SwitchInst(const SwitchInst &SI);
2540 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2541 void growOperands();
2542 // allocate space for exactly zero operands
2543 void *operator new(size_t s) {
2544 return User::operator new(s, 0);
2546 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2547 /// switch on and a default destination. The number of additional cases can
2548 /// be specified here to make memory allocation more efficient. This
2549 /// constructor can also autoinsert before another instruction.
2550 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2551 Instruction *InsertBefore);
2553 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2554 /// switch on and a default destination. The number of additional cases can
2555 /// be specified here to make memory allocation more efficient. This
2556 /// constructor also autoinserts at the end of the specified BasicBlock.
2557 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2558 BasicBlock *InsertAtEnd);
2560 SwitchInst *clone_impl() const override;
2564 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2566 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2567 class CaseIteratorT {
2575 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2577 /// Initializes case iterator for given SwitchInst and for given
2579 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2584 /// Initializes case iterator for given SwitchInst and for given
2585 /// TerminatorInst's successor index.
2586 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2587 assert(SuccessorIndex < SI->getNumSuccessors() &&
2588 "Successor index # out of range!");
2589 return SuccessorIndex != 0 ?
2590 Self(SI, SuccessorIndex - 1) :
2591 Self(SI, DefaultPseudoIndex);
2594 /// Resolves case value for current case.
2595 ConstantIntTy *getCaseValue() {
2596 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2597 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2600 /// Resolves successor for current case.
2601 BasicBlockTy *getCaseSuccessor() {
2602 assert((Index < SI->getNumCases() ||
2603 Index == DefaultPseudoIndex) &&
2604 "Index out the number of cases.");
2605 return SI->getSuccessor(getSuccessorIndex());
2608 /// Returns number of current case.
2609 unsigned getCaseIndex() const { return Index; }
2611 /// Returns TerminatorInst's successor index for current case successor.
2612 unsigned getSuccessorIndex() const {
2613 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2614 "Index out the number of cases.");
2615 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2619 // Check index correctness after increment.
2620 // Note: Index == getNumCases() means end().
2621 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2625 Self operator++(int) {
2631 // Check index correctness after decrement.
2632 // Note: Index == getNumCases() means end().
2633 // Also allow "-1" iterator here. That will became valid after ++.
2634 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2635 "Index out the number of cases.");
2639 Self operator--(int) {
2644 bool operator==(const Self& RHS) const {
2645 assert(RHS.SI == SI && "Incompatible operators.");
2646 return RHS.Index == Index;
2648 bool operator!=(const Self& RHS) const {
2649 assert(RHS.SI == SI && "Incompatible operators.");
2650 return RHS.Index != Index;
2654 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2657 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2659 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2663 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2664 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2666 /// Sets the new value for current case.
2667 void setValue(ConstantInt *V) {
2668 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2669 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2672 /// Sets the new successor for current case.
2673 void setSuccessor(BasicBlock *S) {
2674 SI->setSuccessor(getSuccessorIndex(), S);
2678 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2680 Instruction *InsertBefore = nullptr) {
2681 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2683 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2684 unsigned NumCases, BasicBlock *InsertAtEnd) {
2685 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2690 /// Provide fast operand accessors
2691 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2693 // Accessor Methods for Switch stmt
2694 Value *getCondition() const { return getOperand(0); }
2695 void setCondition(Value *V) { setOperand(0, V); }
2697 BasicBlock *getDefaultDest() const {
2698 return cast<BasicBlock>(getOperand(1));
2701 void setDefaultDest(BasicBlock *DefaultCase) {
2702 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2705 /// getNumCases - return the number of 'cases' in this switch instruction,
2706 /// except the default case
2707 unsigned getNumCases() const {
2708 return getNumOperands()/2 - 1;
2711 /// Returns a read/write iterator that points to the first
2712 /// case in SwitchInst.
2713 CaseIt case_begin() {
2714 return CaseIt(this, 0);
2716 /// Returns a read-only iterator that points to the first
2717 /// case in the SwitchInst.
2718 ConstCaseIt case_begin() const {
2719 return ConstCaseIt(this, 0);
2722 /// Returns a read/write iterator that points one past the last
2723 /// in the SwitchInst.
2725 return CaseIt(this, getNumCases());
2727 /// Returns a read-only iterator that points one past the last
2728 /// in the SwitchInst.
2729 ConstCaseIt case_end() const {
2730 return ConstCaseIt(this, getNumCases());
2732 /// Returns an iterator that points to the default case.
2733 /// Note: this iterator allows to resolve successor only. Attempt
2734 /// to resolve case value causes an assertion.
2735 /// Also note, that increment and decrement also causes an assertion and
2736 /// makes iterator invalid.
2737 CaseIt case_default() {
2738 return CaseIt(this, DefaultPseudoIndex);
2740 ConstCaseIt case_default() const {
2741 return ConstCaseIt(this, DefaultPseudoIndex);
2744 /// findCaseValue - Search all of the case values for the specified constant.
2745 /// If it is explicitly handled, return the case iterator of it, otherwise
2746 /// return default case iterator to indicate
2747 /// that it is handled by the default handler.
2748 CaseIt findCaseValue(const ConstantInt *C) {
2749 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2750 if (i.getCaseValue() == C)
2752 return case_default();
2754 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2755 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2756 if (i.getCaseValue() == C)
2758 return case_default();
2761 /// findCaseDest - Finds the unique case value for a given successor. Returns
2762 /// null if the successor is not found, not unique, or is the default case.
2763 ConstantInt *findCaseDest(BasicBlock *BB) {
2764 if (BB == getDefaultDest()) return nullptr;
2766 ConstantInt *CI = nullptr;
2767 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2768 if (i.getCaseSuccessor() == BB) {
2769 if (CI) return nullptr; // Multiple cases lead to BB.
2770 else CI = i.getCaseValue();
2776 /// addCase - Add an entry to the switch instruction...
2778 /// This action invalidates case_end(). Old case_end() iterator will
2779 /// point to the added case.
2780 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2782 /// removeCase - This method removes the specified case and its successor
2783 /// from the switch instruction. Note that this operation may reorder the
2784 /// remaining cases at index idx and above.
2786 /// This action invalidates iterators for all cases following the one removed,
2787 /// including the case_end() iterator.
2788 void removeCase(CaseIt i);
2790 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2791 BasicBlock *getSuccessor(unsigned idx) const {
2792 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2793 return cast<BasicBlock>(getOperand(idx*2+1));
2795 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2796 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2797 setOperand(idx*2+1, (Value*)NewSucc);
2800 // Methods for support type inquiry through isa, cast, and dyn_cast:
2801 static inline bool classof(const Instruction *I) {
2802 return I->getOpcode() == Instruction::Switch;
2804 static inline bool classof(const Value *V) {
2805 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2808 BasicBlock *getSuccessorV(unsigned idx) const override;
2809 unsigned getNumSuccessorsV() const override;
2810 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2814 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2817 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2820 //===----------------------------------------------------------------------===//
2821 // IndirectBrInst Class
2822 //===----------------------------------------------------------------------===//
2824 //===---------------------------------------------------------------------------
2825 /// IndirectBrInst - Indirect Branch Instruction.
2827 class IndirectBrInst : public TerminatorInst {
2828 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2829 unsigned ReservedSpace;
2830 // Operand[0] = Value to switch on
2831 // Operand[1] = Default basic block destination
2832 // Operand[2n ] = Value to match
2833 // Operand[2n+1] = BasicBlock to go to on match
2834 IndirectBrInst(const IndirectBrInst &IBI);
2835 void init(Value *Address, unsigned NumDests);
2836 void growOperands();
2837 // allocate space for exactly zero operands
2838 void *operator new(size_t s) {
2839 return User::operator new(s, 0);
2841 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2842 /// Address to jump to. The number of expected destinations can be specified
2843 /// here to make memory allocation more efficient. This constructor can also
2844 /// autoinsert before another instruction.
2845 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2847 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2848 /// Address to jump to. The number of expected destinations can be specified
2849 /// here to make memory allocation more efficient. This constructor also
2850 /// autoinserts at the end of the specified BasicBlock.
2851 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2853 IndirectBrInst *clone_impl() const override;
2855 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2856 Instruction *InsertBefore = nullptr) {
2857 return new IndirectBrInst(Address, NumDests, InsertBefore);
2859 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2860 BasicBlock *InsertAtEnd) {
2861 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2865 /// Provide fast operand accessors.
2866 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2868 // Accessor Methods for IndirectBrInst instruction.
2869 Value *getAddress() { return getOperand(0); }
2870 const Value *getAddress() const { return getOperand(0); }
2871 void setAddress(Value *V) { setOperand(0, V); }
2874 /// getNumDestinations - return the number of possible destinations in this
2875 /// indirectbr instruction.
2876 unsigned getNumDestinations() const { return getNumOperands()-1; }
2878 /// getDestination - Return the specified destination.
2879 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2880 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2882 /// addDestination - Add a destination.
2884 void addDestination(BasicBlock *Dest);
2886 /// removeDestination - This method removes the specified successor from the
2887 /// indirectbr instruction.
2888 void removeDestination(unsigned i);
2890 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2891 BasicBlock *getSuccessor(unsigned i) const {
2892 return cast<BasicBlock>(getOperand(i+1));
2894 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2895 setOperand(i+1, (Value*)NewSucc);
2898 // Methods for support type inquiry through isa, cast, and dyn_cast:
2899 static inline bool classof(const Instruction *I) {
2900 return I->getOpcode() == Instruction::IndirectBr;
2902 static inline bool classof(const Value *V) {
2903 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2906 BasicBlock *getSuccessorV(unsigned idx) const override;
2907 unsigned getNumSuccessorsV() const override;
2908 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2912 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2915 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2918 //===----------------------------------------------------------------------===//
2920 //===----------------------------------------------------------------------===//
2922 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2923 /// calling convention of the call.
2925 class InvokeInst : public TerminatorInst {
2926 AttributeSet AttributeList;
2927 InvokeInst(const InvokeInst &BI);
2928 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2929 ArrayRef<Value *> Args, const Twine &NameStr);
2931 /// Construct an InvokeInst given a range of arguments.
2933 /// \brief Construct an InvokeInst from a range of arguments
2934 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2935 ArrayRef<Value *> Args, unsigned Values,
2936 const Twine &NameStr, Instruction *InsertBefore);
2938 /// Construct an InvokeInst given a range of arguments.
2940 /// \brief Construct an InvokeInst from a range of arguments
2941 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2942 ArrayRef<Value *> Args, unsigned Values,
2943 const Twine &NameStr, BasicBlock *InsertAtEnd);
2945 InvokeInst *clone_impl() const override;
2947 static InvokeInst *Create(Value *Func,
2948 BasicBlock *IfNormal, BasicBlock *IfException,
2949 ArrayRef<Value *> Args, const Twine &NameStr = "",
2950 Instruction *InsertBefore = nullptr) {
2951 unsigned Values = unsigned(Args.size()) + 3;
2952 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2953 Values, NameStr, InsertBefore);
2955 static InvokeInst *Create(Value *Func,
2956 BasicBlock *IfNormal, BasicBlock *IfException,
2957 ArrayRef<Value *> Args, const Twine &NameStr,
2958 BasicBlock *InsertAtEnd) {
2959 unsigned Values = unsigned(Args.size()) + 3;
2960 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2961 Values, NameStr, InsertAtEnd);
2964 /// Provide fast operand accessors
2965 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2967 /// getNumArgOperands - Return the number of invoke arguments.
2969 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2971 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2973 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2974 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2976 /// arg_operands - iteration adapter for range-for loops.
2977 iterator_range<op_iterator> arg_operands() {
2978 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
2981 /// arg_operands - iteration adapter for range-for loops.
2982 iterator_range<const_op_iterator> arg_operands() const {
2983 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
2986 /// \brief Wrappers for getting the \c Use of a invoke argument.
2987 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
2988 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
2990 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2992 CallingConv::ID getCallingConv() const {
2993 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2995 void setCallingConv(CallingConv::ID CC) {
2996 setInstructionSubclassData(static_cast<unsigned>(CC));
2999 /// getAttributes - Return the parameter attributes for this invoke.
3001 const AttributeSet &getAttributes() const { return AttributeList; }
3003 /// setAttributes - Set the parameter attributes for this invoke.
3005 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3007 /// addAttribute - adds the attribute to the list of attributes.
3008 void addAttribute(unsigned i, Attribute::AttrKind attr);
3010 /// removeAttribute - removes the attribute from the list of attributes.
3011 void removeAttribute(unsigned i, Attribute attr);
3013 /// \brief Determine whether this call has the given attribute.
3014 bool hasFnAttr(Attribute::AttrKind A) const {
3015 assert(A != Attribute::NoBuiltin &&
3016 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3017 return hasFnAttrImpl(A);
3020 /// \brief Determine whether the call or the callee has the given attributes.
3021 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3023 /// \brief Extract the alignment for a call or parameter (0=unknown).
3024 unsigned getParamAlignment(unsigned i) const {
3025 return AttributeList.getParamAlignment(i);
3028 /// \brief Return true if the call should not be treated as a call to a
3030 bool isNoBuiltin() const {
3031 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3032 // to check it by hand.
3033 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3034 !hasFnAttrImpl(Attribute::Builtin);
3037 /// \brief Return true if the call should not be inlined.
3038 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3039 void setIsNoInline() {
3040 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3043 /// \brief Determine if the call does not access memory.
3044 bool doesNotAccessMemory() const {
3045 return hasFnAttr(Attribute::ReadNone);
3047 void setDoesNotAccessMemory() {
3048 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3051 /// \brief Determine if the call does not access or only reads memory.
3052 bool onlyReadsMemory() const {
3053 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3055 void setOnlyReadsMemory() {
3056 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3059 /// \brief Determine if the call cannot return.
3060 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3061 void setDoesNotReturn() {
3062 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3065 /// \brief Determine if the call cannot unwind.
3066 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3067 void setDoesNotThrow() {
3068 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3071 /// \brief Determine if the invoke cannot be duplicated.
3072 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3073 void setCannotDuplicate() {
3074 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3077 /// \brief Determine if the call returns a structure through first
3078 /// pointer argument.
3079 bool hasStructRetAttr() const {
3080 // Be friendly and also check the callee.
3081 return paramHasAttr(1, Attribute::StructRet);
3084 /// \brief Determine if any call argument is an aggregate passed by value.
3085 bool hasByValArgument() const {
3086 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3089 /// getCalledFunction - Return the function called, or null if this is an
3090 /// indirect function invocation.
3092 Function *getCalledFunction() const {
3093 return dyn_cast<Function>(Op<-3>());
3096 /// getCalledValue - Get a pointer to the function that is invoked by this
3098 const Value *getCalledValue() const { return Op<-3>(); }
3099 Value *getCalledValue() { return Op<-3>(); }
3101 /// setCalledFunction - Set the function called.
3102 void setCalledFunction(Value* Fn) {
3106 // get*Dest - Return the destination basic blocks...
3107 BasicBlock *getNormalDest() const {
3108 return cast<BasicBlock>(Op<-2>());
3110 BasicBlock *getUnwindDest() const {
3111 return cast<BasicBlock>(Op<-1>());
3113 void setNormalDest(BasicBlock *B) {
3114 Op<-2>() = reinterpret_cast<Value*>(B);
3116 void setUnwindDest(BasicBlock *B) {
3117 Op<-1>() = reinterpret_cast<Value*>(B);
3120 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3121 /// block (the unwind destination).
3122 LandingPadInst *getLandingPadInst() const;
3124 BasicBlock *getSuccessor(unsigned i) const {
3125 assert(i < 2 && "Successor # out of range for invoke!");
3126 return i == 0 ? getNormalDest() : getUnwindDest();
3129 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3130 assert(idx < 2 && "Successor # out of range for invoke!");
3131 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3134 unsigned getNumSuccessors() const { return 2; }
3136 // Methods for support type inquiry through isa, cast, and dyn_cast:
3137 static inline bool classof(const Instruction *I) {
3138 return (I->getOpcode() == Instruction::Invoke);
3140 static inline bool classof(const Value *V) {
3141 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3145 BasicBlock *getSuccessorV(unsigned idx) const override;
3146 unsigned getNumSuccessorsV() const override;
3147 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3149 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3151 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3152 // method so that subclasses cannot accidentally use it.
3153 void setInstructionSubclassData(unsigned short D) {
3154 Instruction::setInstructionSubclassData(D);
3159 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3162 InvokeInst::InvokeInst(Value *Func,
3163 BasicBlock *IfNormal, BasicBlock *IfException,
3164 ArrayRef<Value *> Args, unsigned Values,
3165 const Twine &NameStr, Instruction *InsertBefore)
3166 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3167 ->getElementType())->getReturnType(),
3168 Instruction::Invoke,
3169 OperandTraits<InvokeInst>::op_end(this) - Values,
3170 Values, InsertBefore) {
3171 init(Func, IfNormal, IfException, Args, NameStr);
3173 InvokeInst::InvokeInst(Value *Func,
3174 BasicBlock *IfNormal, BasicBlock *IfException,
3175 ArrayRef<Value *> Args, unsigned Values,
3176 const Twine &NameStr, BasicBlock *InsertAtEnd)
3177 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3178 ->getElementType())->getReturnType(),
3179 Instruction::Invoke,
3180 OperandTraits<InvokeInst>::op_end(this) - Values,
3181 Values, InsertAtEnd) {
3182 init(Func, IfNormal, IfException, Args, NameStr);
3185 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3187 //===----------------------------------------------------------------------===//
3189 //===----------------------------------------------------------------------===//
3191 //===---------------------------------------------------------------------------
3192 /// ResumeInst - Resume the propagation of an exception.
3194 class ResumeInst : public TerminatorInst {
3195 ResumeInst(const ResumeInst &RI);
3197 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3198 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3200 ResumeInst *clone_impl() const override;
3202 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3203 return new(1) ResumeInst(Exn, InsertBefore);
3205 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3206 return new(1) ResumeInst(Exn, InsertAtEnd);
3209 /// Provide fast operand accessors
3210 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3212 /// Convenience accessor.
3213 Value *getValue() const { return Op<0>(); }
3215 unsigned getNumSuccessors() const { return 0; }
3217 // Methods for support type inquiry through isa, cast, and dyn_cast:
3218 static inline bool classof(const Instruction *I) {
3219 return I->getOpcode() == Instruction::Resume;
3221 static inline bool classof(const Value *V) {
3222 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3225 BasicBlock *getSuccessorV(unsigned idx) const override;
3226 unsigned getNumSuccessorsV() const override;
3227 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3231 struct OperandTraits<ResumeInst> :
3232 public FixedNumOperandTraits<ResumeInst, 1> {
3235 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3237 //===----------------------------------------------------------------------===//
3238 // UnreachableInst Class
3239 //===----------------------------------------------------------------------===//
3241 //===---------------------------------------------------------------------------
3242 /// UnreachableInst - This function has undefined behavior. In particular, the
3243 /// presence of this instruction indicates some higher level knowledge that the
3244 /// end of the block cannot be reached.
3246 class UnreachableInst : public TerminatorInst {
3247 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3249 UnreachableInst *clone_impl() const override;
3252 // allocate space for exactly zero operands
3253 void *operator new(size_t s) {
3254 return User::operator new(s, 0);
3256 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3257 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3259 unsigned getNumSuccessors() const { return 0; }
3261 // Methods for support type inquiry through isa, cast, and dyn_cast:
3262 static inline bool classof(const Instruction *I) {
3263 return I->getOpcode() == Instruction::Unreachable;
3265 static inline bool classof(const Value *V) {
3266 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3269 BasicBlock *getSuccessorV(unsigned idx) const override;
3270 unsigned getNumSuccessorsV() const override;
3271 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3274 //===----------------------------------------------------------------------===//
3276 //===----------------------------------------------------------------------===//
3278 /// \brief This class represents a truncation of integer types.
3279 class TruncInst : public CastInst {
3281 /// \brief Clone an identical TruncInst
3282 TruncInst *clone_impl() const override;
3285 /// \brief Constructor with insert-before-instruction semantics
3287 Value *S, ///< The value to be truncated
3288 Type *Ty, ///< The (smaller) type to truncate to
3289 const Twine &NameStr = "", ///< A name for the new instruction
3290 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3293 /// \brief Constructor with insert-at-end-of-block semantics
3295 Value *S, ///< The value to be truncated
3296 Type *Ty, ///< The (smaller) type to truncate to
3297 const Twine &NameStr, ///< A name for the new instruction
3298 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3301 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3302 static inline bool classof(const Instruction *I) {
3303 return I->getOpcode() == Trunc;
3305 static inline bool classof(const Value *V) {
3306 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3310 //===----------------------------------------------------------------------===//
3312 //===----------------------------------------------------------------------===//
3314 /// \brief This class represents zero extension of integer types.
3315 class ZExtInst : public CastInst {
3317 /// \brief Clone an identical ZExtInst
3318 ZExtInst *clone_impl() const override;
3321 /// \brief Constructor with insert-before-instruction semantics
3323 Value *S, ///< The value to be zero extended
3324 Type *Ty, ///< The type to zero extend to
3325 const Twine &NameStr = "", ///< A name for the new instruction
3326 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3329 /// \brief Constructor with insert-at-end semantics.
3331 Value *S, ///< The value to be zero extended
3332 Type *Ty, ///< The type to zero extend to
3333 const Twine &NameStr, ///< A name for the new instruction
3334 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3337 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3338 static inline bool classof(const Instruction *I) {
3339 return I->getOpcode() == ZExt;
3341 static inline bool classof(const Value *V) {
3342 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3346 //===----------------------------------------------------------------------===//
3348 //===----------------------------------------------------------------------===//
3350 /// \brief This class represents a sign extension of integer types.
3351 class SExtInst : public CastInst {
3353 /// \brief Clone an identical SExtInst
3354 SExtInst *clone_impl() const override;
3357 /// \brief Constructor with insert-before-instruction semantics
3359 Value *S, ///< The value to be sign extended
3360 Type *Ty, ///< The type to sign extend to
3361 const Twine &NameStr = "", ///< A name for the new instruction
3362 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3365 /// \brief Constructor with insert-at-end-of-block semantics
3367 Value *S, ///< The value to be sign extended
3368 Type *Ty, ///< The type to sign extend to
3369 const Twine &NameStr, ///< A name for the new instruction
3370 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3373 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3374 static inline bool classof(const Instruction *I) {
3375 return I->getOpcode() == SExt;
3377 static inline bool classof(const Value *V) {
3378 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3382 //===----------------------------------------------------------------------===//
3383 // FPTruncInst Class
3384 //===----------------------------------------------------------------------===//
3386 /// \brief This class represents a truncation of floating point types.
3387 class FPTruncInst : public CastInst {
3389 /// \brief Clone an identical FPTruncInst
3390 FPTruncInst *clone_impl() const override;
3393 /// \brief Constructor with insert-before-instruction semantics
3395 Value *S, ///< The value to be truncated
3396 Type *Ty, ///< The type to truncate to
3397 const Twine &NameStr = "", ///< A name for the new instruction
3398 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3401 /// \brief Constructor with insert-before-instruction semantics
3403 Value *S, ///< The value to be truncated
3404 Type *Ty, ///< The type to truncate to
3405 const Twine &NameStr, ///< A name for the new instruction
3406 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3409 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3410 static inline bool classof(const Instruction *I) {
3411 return I->getOpcode() == FPTrunc;
3413 static inline bool classof(const Value *V) {
3414 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3418 //===----------------------------------------------------------------------===//
3420 //===----------------------------------------------------------------------===//
3422 /// \brief This class represents an extension of floating point types.
3423 class FPExtInst : public CastInst {
3425 /// \brief Clone an identical FPExtInst
3426 FPExtInst *clone_impl() const override;
3429 /// \brief Constructor with insert-before-instruction semantics
3431 Value *S, ///< The value to be extended
3432 Type *Ty, ///< The type to extend to
3433 const Twine &NameStr = "", ///< A name for the new instruction
3434 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3437 /// \brief Constructor with insert-at-end-of-block semantics
3439 Value *S, ///< The value to be extended
3440 Type *Ty, ///< The type to extend to
3441 const Twine &NameStr, ///< A name for the new instruction
3442 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3445 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3446 static inline bool classof(const Instruction *I) {
3447 return I->getOpcode() == FPExt;
3449 static inline bool classof(const Value *V) {
3450 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3454 //===----------------------------------------------------------------------===//
3456 //===----------------------------------------------------------------------===//
3458 /// \brief This class represents a cast unsigned integer to floating point.
3459 class UIToFPInst : public CastInst {
3461 /// \brief Clone an identical UIToFPInst
3462 UIToFPInst *clone_impl() const override;
3465 /// \brief Constructor with insert-before-instruction semantics
3467 Value *S, ///< The value to be converted
3468 Type *Ty, ///< The type to convert to
3469 const Twine &NameStr = "", ///< A name for the new instruction
3470 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3473 /// \brief Constructor with insert-at-end-of-block semantics
3475 Value *S, ///< The value to be converted
3476 Type *Ty, ///< The type to convert to
3477 const Twine &NameStr, ///< A name for the new instruction
3478 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3481 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3482 static inline bool classof(const Instruction *I) {
3483 return I->getOpcode() == UIToFP;
3485 static inline bool classof(const Value *V) {
3486 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3490 //===----------------------------------------------------------------------===//
3492 //===----------------------------------------------------------------------===//
3494 /// \brief This class represents a cast from signed integer to floating point.
3495 class SIToFPInst : public CastInst {
3497 /// \brief Clone an identical SIToFPInst
3498 SIToFPInst *clone_impl() const override;
3501 /// \brief Constructor with insert-before-instruction semantics
3503 Value *S, ///< The value to be converted
3504 Type *Ty, ///< The type to convert to
3505 const Twine &NameStr = "", ///< A name for the new instruction
3506 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3509 /// \brief Constructor with insert-at-end-of-block semantics
3511 Value *S, ///< The value to be converted
3512 Type *Ty, ///< The type to convert to
3513 const Twine &NameStr, ///< A name for the new instruction
3514 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3517 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3518 static inline bool classof(const Instruction *I) {
3519 return I->getOpcode() == SIToFP;
3521 static inline bool classof(const Value *V) {
3522 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3526 //===----------------------------------------------------------------------===//
3528 //===----------------------------------------------------------------------===//
3530 /// \brief This class represents a cast from floating point to unsigned integer
3531 class FPToUIInst : public CastInst {
3533 /// \brief Clone an identical FPToUIInst
3534 FPToUIInst *clone_impl() const override;
3537 /// \brief Constructor with insert-before-instruction semantics
3539 Value *S, ///< The value to be converted
3540 Type *Ty, ///< The type to convert to
3541 const Twine &NameStr = "", ///< A name for the new instruction
3542 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3545 /// \brief Constructor with insert-at-end-of-block semantics
3547 Value *S, ///< The value to be converted
3548 Type *Ty, ///< The type to convert to
3549 const Twine &NameStr, ///< A name for the new instruction
3550 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3553 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3554 static inline bool classof(const Instruction *I) {
3555 return I->getOpcode() == FPToUI;
3557 static inline bool classof(const Value *V) {
3558 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3562 //===----------------------------------------------------------------------===//
3564 //===----------------------------------------------------------------------===//
3566 /// \brief This class represents a cast from floating point to signed integer.
3567 class FPToSIInst : public CastInst {
3569 /// \brief Clone an identical FPToSIInst
3570 FPToSIInst *clone_impl() const override;
3573 /// \brief Constructor with insert-before-instruction semantics
3575 Value *S, ///< The value to be converted
3576 Type *Ty, ///< The type to convert to
3577 const Twine &NameStr = "", ///< A name for the new instruction
3578 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3581 /// \brief Constructor with insert-at-end-of-block semantics
3583 Value *S, ///< The value to be converted
3584 Type *Ty, ///< The type to convert to
3585 const Twine &NameStr, ///< A name for the new instruction
3586 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3589 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3590 static inline bool classof(const Instruction *I) {
3591 return I->getOpcode() == FPToSI;
3593 static inline bool classof(const Value *V) {
3594 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3598 //===----------------------------------------------------------------------===//
3599 // IntToPtrInst Class
3600 //===----------------------------------------------------------------------===//
3602 /// \brief This class represents a cast from an integer to a pointer.
3603 class IntToPtrInst : public CastInst {
3605 /// \brief Constructor with insert-before-instruction semantics
3607 Value *S, ///< The value to be converted
3608 Type *Ty, ///< The type to convert to
3609 const Twine &NameStr = "", ///< A name for the new instruction
3610 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3613 /// \brief Constructor with insert-at-end-of-block semantics
3615 Value *S, ///< The value to be converted
3616 Type *Ty, ///< The type to convert to
3617 const Twine &NameStr, ///< A name for the new instruction
3618 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3621 /// \brief Clone an identical IntToPtrInst
3622 IntToPtrInst *clone_impl() const override;
3624 /// \brief Returns the address space of this instruction's pointer type.
3625 unsigned getAddressSpace() const {
3626 return getType()->getPointerAddressSpace();
3629 // Methods for support type inquiry through isa, cast, and dyn_cast:
3630 static inline bool classof(const Instruction *I) {
3631 return I->getOpcode() == IntToPtr;
3633 static inline bool classof(const Value *V) {
3634 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3638 //===----------------------------------------------------------------------===//
3639 // PtrToIntInst Class
3640 //===----------------------------------------------------------------------===//
3642 /// \brief This class represents a cast from a pointer to an integer
3643 class PtrToIntInst : public CastInst {
3645 /// \brief Clone an identical PtrToIntInst
3646 PtrToIntInst *clone_impl() const override;
3649 /// \brief Constructor with insert-before-instruction semantics
3651 Value *S, ///< The value to be converted
3652 Type *Ty, ///< The type to convert to
3653 const Twine &NameStr = "", ///< A name for the new instruction
3654 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3657 /// \brief Constructor with insert-at-end-of-block semantics
3659 Value *S, ///< The value to be converted
3660 Type *Ty, ///< The type to convert to
3661 const Twine &NameStr, ///< A name for the new instruction
3662 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3665 /// \brief Gets the pointer operand.
3666 Value *getPointerOperand() { return getOperand(0); }
3667 /// \brief Gets the pointer operand.
3668 const Value *getPointerOperand() const { return getOperand(0); }
3669 /// \brief Gets the operand index of the pointer operand.
3670 static unsigned getPointerOperandIndex() { return 0U; }
3672 /// \brief Returns the address space of the pointer operand.
3673 unsigned getPointerAddressSpace() const {
3674 return getPointerOperand()->getType()->getPointerAddressSpace();
3677 // Methods for support type inquiry through isa, cast, and dyn_cast:
3678 static inline bool classof(const Instruction *I) {
3679 return I->getOpcode() == PtrToInt;
3681 static inline bool classof(const Value *V) {
3682 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3686 //===----------------------------------------------------------------------===//
3687 // BitCastInst Class
3688 //===----------------------------------------------------------------------===//
3690 /// \brief This class represents a no-op cast from one type to another.
3691 class BitCastInst : public CastInst {
3693 /// \brief Clone an identical BitCastInst
3694 BitCastInst *clone_impl() const override;
3697 /// \brief Constructor with insert-before-instruction semantics
3699 Value *S, ///< The value to be casted
3700 Type *Ty, ///< The type to casted to
3701 const Twine &NameStr = "", ///< A name for the new instruction
3702 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3705 /// \brief Constructor with insert-at-end-of-block semantics
3707 Value *S, ///< The value to be casted
3708 Type *Ty, ///< The type to casted to
3709 const Twine &NameStr, ///< A name for the new instruction
3710 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3713 // Methods for support type inquiry through isa, cast, and dyn_cast:
3714 static inline bool classof(const Instruction *I) {
3715 return I->getOpcode() == BitCast;
3717 static inline bool classof(const Value *V) {
3718 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3722 //===----------------------------------------------------------------------===//
3723 // AddrSpaceCastInst Class
3724 //===----------------------------------------------------------------------===//
3726 /// \brief This class represents a conversion between pointers from
3727 /// one address space to another.
3728 class AddrSpaceCastInst : public CastInst {
3730 /// \brief Clone an identical AddrSpaceCastInst
3731 AddrSpaceCastInst *clone_impl() const override;
3734 /// \brief Constructor with insert-before-instruction semantics
3736 Value *S, ///< The value to be casted
3737 Type *Ty, ///< The type to casted to
3738 const Twine &NameStr = "", ///< A name for the new instruction
3739 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3742 /// \brief Constructor with insert-at-end-of-block semantics
3744 Value *S, ///< The value to be casted
3745 Type *Ty, ///< The type to casted to
3746 const Twine &NameStr, ///< A name for the new instruction
3747 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3750 // Methods for support type inquiry through isa, cast, and dyn_cast:
3751 static inline bool classof(const Instruction *I) {
3752 return I->getOpcode() == AddrSpaceCast;
3754 static inline bool classof(const Value *V) {
3755 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3759 } // End llvm namespace