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/SmallVector.h"
21 #include "llvm/IR/Attributes.h"
22 #include "llvm/IR/CallingConv.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/InstrTypes.h"
25 #include "llvm/Support/ErrorHandling.h"
40 // Consume = 3, // Not specified yet.
44 SequentiallyConsistent = 7
47 enum SynchronizationScope {
52 //===----------------------------------------------------------------------===//
54 //===----------------------------------------------------------------------===//
56 /// AllocaInst - an instruction to allocate memory on the stack
58 class AllocaInst : public UnaryInstruction {
60 AllocaInst *clone_impl() const override;
62 explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
63 const Twine &Name = "", Instruction *InsertBefore = 0);
64 AllocaInst(Type *Ty, Value *ArraySize,
65 const Twine &Name, BasicBlock *InsertAtEnd);
67 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
68 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
70 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
71 const Twine &Name = "", Instruction *InsertBefore = 0);
72 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
73 const Twine &Name, BasicBlock *InsertAtEnd);
75 // Out of line virtual method, so the vtable, etc. has a home.
76 virtual ~AllocaInst();
78 /// isArrayAllocation - Return true if there is an allocation size parameter
79 /// to the allocation instruction that is not 1.
81 bool isArrayAllocation() const;
83 /// getArraySize - Get the number of elements allocated. For a simple
84 /// allocation of a single element, this will return a constant 1 value.
86 const Value *getArraySize() const { return getOperand(0); }
87 Value *getArraySize() { return getOperand(0); }
89 /// getType - Overload to return most specific pointer type
91 PointerType *getType() const {
92 return cast<PointerType>(Instruction::getType());
95 /// getAllocatedType - Return the type that is being allocated by the
98 Type *getAllocatedType() const;
100 /// getAlignment - Return the alignment of the memory that is being allocated
101 /// by the instruction.
103 unsigned getAlignment() const {
104 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
106 void setAlignment(unsigned Align);
108 /// isStaticAlloca - Return true if this alloca is in the entry block of the
109 /// function and is a constant size. If so, the code generator will fold it
110 /// into the prolog/epilog code, so it is basically free.
111 bool isStaticAlloca() const;
113 /// \brief Return true if this alloca is used as an inalloca argument to a
114 /// call. Such allocas are never considered static even if they are in the
116 bool isUsedWithInAlloca() const {
117 return getSubclassDataFromInstruction() & 32;
120 /// \brief Specify whether this alloca is used to represent a the arguments to
122 void setUsedWithInAlloca(bool V) {
123 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
127 // Methods for support type inquiry through isa, cast, and dyn_cast:
128 static inline bool classof(const Instruction *I) {
129 return (I->getOpcode() == Instruction::Alloca);
131 static inline bool classof(const Value *V) {
132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
135 // Shadow Instruction::setInstructionSubclassData with a private forwarding
136 // method so that subclasses cannot accidentally use it.
137 void setInstructionSubclassData(unsigned short D) {
138 Instruction::setInstructionSubclassData(D);
143 //===----------------------------------------------------------------------===//
145 //===----------------------------------------------------------------------===//
147 /// LoadInst - an instruction for reading from memory. This uses the
148 /// SubclassData field in Value to store whether or not the load is volatile.
150 class LoadInst : public UnaryInstruction {
153 LoadInst *clone_impl() const override;
155 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
156 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
157 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
158 Instruction *InsertBefore = 0);
159 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
160 BasicBlock *InsertAtEnd);
161 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
162 unsigned Align, Instruction *InsertBefore = 0);
163 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
164 unsigned Align, BasicBlock *InsertAtEnd);
165 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
166 unsigned Align, AtomicOrdering Order,
167 SynchronizationScope SynchScope = CrossThread,
168 Instruction *InsertBefore = 0);
169 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
170 unsigned Align, AtomicOrdering Order,
171 SynchronizationScope SynchScope,
172 BasicBlock *InsertAtEnd);
174 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
175 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
176 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
177 bool isVolatile = false, Instruction *InsertBefore = 0);
178 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
179 BasicBlock *InsertAtEnd);
181 /// isVolatile - Return true if this is a load from a volatile memory
184 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
186 /// setVolatile - Specify whether this is a volatile load or not.
188 void setVolatile(bool V) {
189 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
193 /// getAlignment - Return the alignment of the access that is being performed
195 unsigned getAlignment() const {
196 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
199 void setAlignment(unsigned Align);
201 /// Returns the ordering effect of this fence.
202 AtomicOrdering getOrdering() const {
203 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
206 /// Set the ordering constraint on this load. May not be Release or
208 void setOrdering(AtomicOrdering Ordering) {
209 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
213 SynchronizationScope getSynchScope() const {
214 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
217 /// Specify whether this load is ordered with respect to all
218 /// concurrently executing threads, or only with respect to signal handlers
219 /// executing in the same thread.
220 void setSynchScope(SynchronizationScope xthread) {
221 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
225 bool isAtomic() const { return getOrdering() != NotAtomic; }
226 void setAtomic(AtomicOrdering Ordering,
227 SynchronizationScope SynchScope = CrossThread) {
228 setOrdering(Ordering);
229 setSynchScope(SynchScope);
232 bool isSimple() const { return !isAtomic() && !isVolatile(); }
233 bool isUnordered() const {
234 return getOrdering() <= Unordered && !isVolatile();
237 Value *getPointerOperand() { return getOperand(0); }
238 const Value *getPointerOperand() const { return getOperand(0); }
239 static unsigned getPointerOperandIndex() { return 0U; }
241 /// \brief Returns the address space of the pointer operand.
242 unsigned getPointerAddressSpace() const {
243 return getPointerOperand()->getType()->getPointerAddressSpace();
247 // Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const Instruction *I) {
249 return I->getOpcode() == Instruction::Load;
251 static inline bool classof(const Value *V) {
252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
255 // Shadow Instruction::setInstructionSubclassData with a private forwarding
256 // method so that subclasses cannot accidentally use it.
257 void setInstructionSubclassData(unsigned short D) {
258 Instruction::setInstructionSubclassData(D);
263 //===----------------------------------------------------------------------===//
265 //===----------------------------------------------------------------------===//
267 /// StoreInst - an instruction for storing to memory
269 class StoreInst : public Instruction {
270 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
273 StoreInst *clone_impl() const override;
275 // allocate space for exactly two operands
276 void *operator new(size_t s) {
277 return User::operator new(s, 2);
279 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
280 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
281 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
282 Instruction *InsertBefore = 0);
283 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
284 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
285 unsigned Align, Instruction *InsertBefore = 0);
286 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
287 unsigned Align, BasicBlock *InsertAtEnd);
288 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
289 unsigned Align, AtomicOrdering Order,
290 SynchronizationScope SynchScope = CrossThread,
291 Instruction *InsertBefore = 0);
292 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
293 unsigned Align, AtomicOrdering Order,
294 SynchronizationScope SynchScope,
295 BasicBlock *InsertAtEnd);
298 /// isVolatile - Return true if this is a store to a volatile memory
301 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
303 /// setVolatile - Specify whether this is a volatile store or not.
305 void setVolatile(bool V) {
306 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
310 /// Transparently provide more efficient getOperand methods.
311 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
313 /// getAlignment - Return the alignment of the access that is being performed
315 unsigned getAlignment() const {
316 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
319 void setAlignment(unsigned Align);
321 /// Returns the ordering effect of this store.
322 AtomicOrdering getOrdering() const {
323 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
326 /// Set the ordering constraint on this store. May not be Acquire or
328 void setOrdering(AtomicOrdering Ordering) {
329 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
333 SynchronizationScope getSynchScope() const {
334 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
337 /// Specify whether this store instruction is ordered with respect to all
338 /// concurrently executing threads, or only with respect to signal handlers
339 /// executing in the same thread.
340 void setSynchScope(SynchronizationScope xthread) {
341 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
345 bool isAtomic() const { return getOrdering() != NotAtomic; }
346 void setAtomic(AtomicOrdering Ordering,
347 SynchronizationScope SynchScope = CrossThread) {
348 setOrdering(Ordering);
349 setSynchScope(SynchScope);
352 bool isSimple() const { return !isAtomic() && !isVolatile(); }
353 bool isUnordered() const {
354 return getOrdering() <= Unordered && !isVolatile();
357 Value *getValueOperand() { return getOperand(0); }
358 const Value *getValueOperand() const { return getOperand(0); }
360 Value *getPointerOperand() { return getOperand(1); }
361 const Value *getPointerOperand() const { return getOperand(1); }
362 static unsigned getPointerOperandIndex() { return 1U; }
364 /// \brief Returns the address space of the pointer operand.
365 unsigned getPointerAddressSpace() const {
366 return getPointerOperand()->getType()->getPointerAddressSpace();
369 // Methods for support type inquiry through isa, cast, and dyn_cast:
370 static inline bool classof(const Instruction *I) {
371 return I->getOpcode() == Instruction::Store;
373 static inline bool classof(const Value *V) {
374 return isa<Instruction>(V) && classof(cast<Instruction>(V));
377 // Shadow Instruction::setInstructionSubclassData with a private forwarding
378 // method so that subclasses cannot accidentally use it.
379 void setInstructionSubclassData(unsigned short D) {
380 Instruction::setInstructionSubclassData(D);
385 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
388 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
390 //===----------------------------------------------------------------------===//
392 //===----------------------------------------------------------------------===//
394 /// FenceInst - an instruction for ordering other memory operations
396 class FenceInst : public Instruction {
397 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
398 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
400 FenceInst *clone_impl() const override;
402 // allocate space for exactly zero operands
403 void *operator new(size_t s) {
404 return User::operator new(s, 0);
407 // Ordering may only be Acquire, Release, AcquireRelease, or
408 // SequentiallyConsistent.
409 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
410 SynchronizationScope SynchScope = CrossThread,
411 Instruction *InsertBefore = 0);
412 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
413 SynchronizationScope SynchScope,
414 BasicBlock *InsertAtEnd);
416 /// Returns the ordering effect of this fence.
417 AtomicOrdering getOrdering() const {
418 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
421 /// Set the ordering constraint on this fence. May only be Acquire, Release,
422 /// AcquireRelease, or SequentiallyConsistent.
423 void setOrdering(AtomicOrdering Ordering) {
424 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
428 SynchronizationScope getSynchScope() const {
429 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
432 /// Specify whether this fence orders other operations with respect to all
433 /// concurrently executing threads, or only with respect to signal handlers
434 /// executing in the same thread.
435 void setSynchScope(SynchronizationScope xthread) {
436 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
440 // Methods for support type inquiry through isa, cast, and dyn_cast:
441 static inline bool classof(const Instruction *I) {
442 return I->getOpcode() == Instruction::Fence;
444 static inline bool classof(const Value *V) {
445 return isa<Instruction>(V) && classof(cast<Instruction>(V));
448 // Shadow Instruction::setInstructionSubclassData with a private forwarding
449 // method so that subclasses cannot accidentally use it.
450 void setInstructionSubclassData(unsigned short D) {
451 Instruction::setInstructionSubclassData(D);
455 //===----------------------------------------------------------------------===//
456 // AtomicCmpXchgInst Class
457 //===----------------------------------------------------------------------===//
459 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
460 /// specified value is in a memory location, and, if it is, stores a new value
461 /// there. Returns the value that was loaded.
463 class AtomicCmpXchgInst : public Instruction {
464 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
465 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
466 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
467 SynchronizationScope SynchScope);
469 AtomicCmpXchgInst *clone_impl() const override;
471 // allocate space for exactly three operands
472 void *operator new(size_t s) {
473 return User::operator new(s, 3);
475 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
476 AtomicOrdering SuccessOrdering,
477 AtomicOrdering FailureOrdering,
478 SynchronizationScope SynchScope,
479 Instruction *InsertBefore = 0);
480 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
481 AtomicOrdering SuccessOrdering,
482 AtomicOrdering FailureOrdering,
483 SynchronizationScope SynchScope,
484 BasicBlock *InsertAtEnd);
486 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
489 bool isVolatile() const {
490 return getSubclassDataFromInstruction() & 1;
493 /// setVolatile - Specify whether this is a volatile cmpxchg.
495 void setVolatile(bool V) {
496 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
500 /// Transparently provide more efficient getOperand methods.
501 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
503 /// Set the ordering constraint on this cmpxchg.
504 void setSuccessOrdering(AtomicOrdering Ordering) {
505 assert(Ordering != NotAtomic &&
506 "CmpXchg instructions can only be atomic.");
507 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
511 void setFailureOrdering(AtomicOrdering Ordering) {
512 assert(Ordering != NotAtomic &&
513 "CmpXchg instructions can only be atomic.");
514 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
518 /// Specify whether this cmpxchg is atomic and orders other operations with
519 /// respect to all concurrently executing threads, or only with respect to
520 /// signal handlers executing in the same thread.
521 void setSynchScope(SynchronizationScope SynchScope) {
522 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
526 /// Returns the ordering constraint on this cmpxchg.
527 AtomicOrdering getSuccessOrdering() const {
528 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
531 /// Returns the ordering constraint on this cmpxchg.
532 AtomicOrdering getFailureOrdering() const {
533 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
536 /// Returns whether this cmpxchg is atomic between threads or only within a
538 SynchronizationScope getSynchScope() const {
539 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
542 Value *getPointerOperand() { return getOperand(0); }
543 const Value *getPointerOperand() const { return getOperand(0); }
544 static unsigned getPointerOperandIndex() { return 0U; }
546 Value *getCompareOperand() { return getOperand(1); }
547 const Value *getCompareOperand() const { return getOperand(1); }
549 Value *getNewValOperand() { return getOperand(2); }
550 const Value *getNewValOperand() const { return getOperand(2); }
552 /// \brief Returns the address space of the pointer operand.
553 unsigned getPointerAddressSpace() const {
554 return getPointerOperand()->getType()->getPointerAddressSpace();
557 /// \brief Returns the strongest permitted ordering on failure, given the
558 /// desired ordering on success.
560 /// If the comparison in a cmpxchg operation fails, there is no atomic store
561 /// so release semantics cannot be provided. So this function drops explicit
562 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
563 /// operation would remain SequentiallyConsistent.
564 static AtomicOrdering
565 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
566 switch (SuccessOrdering) {
567 default: llvm_unreachable("invalid cmpxchg success ordering");
574 case SequentiallyConsistent:
575 return SequentiallyConsistent;
579 // Methods for support type inquiry through isa, cast, and dyn_cast:
580 static inline bool classof(const Instruction *I) {
581 return I->getOpcode() == Instruction::AtomicCmpXchg;
583 static inline bool classof(const Value *V) {
584 return isa<Instruction>(V) && classof(cast<Instruction>(V));
587 // Shadow Instruction::setInstructionSubclassData with a private forwarding
588 // method so that subclasses cannot accidentally use it.
589 void setInstructionSubclassData(unsigned short D) {
590 Instruction::setInstructionSubclassData(D);
595 struct OperandTraits<AtomicCmpXchgInst> :
596 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
599 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
601 //===----------------------------------------------------------------------===//
602 // AtomicRMWInst Class
603 //===----------------------------------------------------------------------===//
605 /// AtomicRMWInst - an instruction that atomically reads a memory location,
606 /// combines it with another value, and then stores the result back. Returns
609 class AtomicRMWInst : public Instruction {
610 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
612 AtomicRMWInst *clone_impl() const override;
614 /// This enumeration lists the possible modifications atomicrmw can make. In
615 /// the descriptions, 'p' is the pointer to the instruction's memory location,
616 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
617 /// instruction. These instructions always return 'old'.
633 /// *p = old >signed v ? old : v
635 /// *p = old <signed v ? old : v
637 /// *p = old >unsigned v ? old : v
639 /// *p = old <unsigned v ? old : v
647 // allocate space for exactly two operands
648 void *operator new(size_t s) {
649 return User::operator new(s, 2);
651 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
652 AtomicOrdering Ordering, SynchronizationScope SynchScope,
653 Instruction *InsertBefore = 0);
654 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
655 AtomicOrdering Ordering, SynchronizationScope SynchScope,
656 BasicBlock *InsertAtEnd);
658 BinOp getOperation() const {
659 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
662 void setOperation(BinOp Operation) {
663 unsigned short SubclassData = getSubclassDataFromInstruction();
664 setInstructionSubclassData((SubclassData & 31) |
668 /// isVolatile - Return true if this is a RMW on a volatile memory location.
670 bool isVolatile() const {
671 return getSubclassDataFromInstruction() & 1;
674 /// setVolatile - Specify whether this is a volatile RMW or not.
676 void setVolatile(bool V) {
677 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
681 /// Transparently provide more efficient getOperand methods.
682 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
684 /// Set the ordering constraint on this RMW.
685 void setOrdering(AtomicOrdering Ordering) {
686 assert(Ordering != NotAtomic &&
687 "atomicrmw instructions can only be atomic.");
688 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
692 /// Specify whether this RMW orders other operations with respect to all
693 /// concurrently executing threads, or only with respect to signal handlers
694 /// executing in the same thread.
695 void setSynchScope(SynchronizationScope SynchScope) {
696 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
700 /// Returns the ordering constraint on this RMW.
701 AtomicOrdering getOrdering() const {
702 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
705 /// Returns whether this RMW is atomic between threads or only within a
707 SynchronizationScope getSynchScope() const {
708 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
711 Value *getPointerOperand() { return getOperand(0); }
712 const Value *getPointerOperand() const { return getOperand(0); }
713 static unsigned getPointerOperandIndex() { return 0U; }
715 Value *getValOperand() { return getOperand(1); }
716 const Value *getValOperand() const { return getOperand(1); }
718 /// \brief Returns the address space of the pointer operand.
719 unsigned getPointerAddressSpace() const {
720 return getPointerOperand()->getType()->getPointerAddressSpace();
723 // Methods for support type inquiry through isa, cast, and dyn_cast:
724 static inline bool classof(const Instruction *I) {
725 return I->getOpcode() == Instruction::AtomicRMW;
727 static inline bool classof(const Value *V) {
728 return isa<Instruction>(V) && classof(cast<Instruction>(V));
731 void Init(BinOp Operation, Value *Ptr, Value *Val,
732 AtomicOrdering Ordering, SynchronizationScope SynchScope);
733 // Shadow Instruction::setInstructionSubclassData with a private forwarding
734 // method so that subclasses cannot accidentally use it.
735 void setInstructionSubclassData(unsigned short D) {
736 Instruction::setInstructionSubclassData(D);
741 struct OperandTraits<AtomicRMWInst>
742 : public FixedNumOperandTraits<AtomicRMWInst,2> {
745 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
747 //===----------------------------------------------------------------------===//
748 // GetElementPtrInst Class
749 //===----------------------------------------------------------------------===//
751 // checkGEPType - Simple wrapper function to give a better assertion failure
752 // message on bad indexes for a gep instruction.
754 inline Type *checkGEPType(Type *Ty) {
755 assert(Ty && "Invalid GetElementPtrInst indices for type!");
759 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
760 /// access elements of arrays and structs
762 class GetElementPtrInst : public Instruction {
763 GetElementPtrInst(const GetElementPtrInst &GEPI);
764 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
766 /// Constructors - Create a getelementptr instruction with a base pointer an
767 /// list of indices. The first ctor can optionally insert before an existing
768 /// instruction, the second appends the new instruction to the specified
770 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
771 unsigned Values, const Twine &NameStr,
772 Instruction *InsertBefore);
773 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
774 unsigned Values, const Twine &NameStr,
775 BasicBlock *InsertAtEnd);
777 GetElementPtrInst *clone_impl() const override;
779 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
780 const Twine &NameStr = "",
781 Instruction *InsertBefore = 0) {
782 unsigned Values = 1 + unsigned(IdxList.size());
784 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
786 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
787 const Twine &NameStr,
788 BasicBlock *InsertAtEnd) {
789 unsigned Values = 1 + unsigned(IdxList.size());
791 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
794 /// Create an "inbounds" getelementptr. See the documentation for the
795 /// "inbounds" flag in LangRef.html for details.
796 static GetElementPtrInst *CreateInBounds(Value *Ptr,
797 ArrayRef<Value *> IdxList,
798 const Twine &NameStr = "",
799 Instruction *InsertBefore = 0) {
800 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
801 GEP->setIsInBounds(true);
804 static GetElementPtrInst *CreateInBounds(Value *Ptr,
805 ArrayRef<Value *> IdxList,
806 const Twine &NameStr,
807 BasicBlock *InsertAtEnd) {
808 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
809 GEP->setIsInBounds(true);
813 /// Transparently provide more efficient getOperand methods.
814 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
816 // getType - Overload to return most specific sequential type.
817 SequentialType *getType() const {
818 return cast<SequentialType>(Instruction::getType());
821 /// \brief Returns the address space of this instruction's pointer type.
822 unsigned getAddressSpace() const {
823 // Note that this is always the same as the pointer operand's address space
824 // and that is cheaper to compute, so cheat here.
825 return getPointerAddressSpace();
828 /// getIndexedType - Returns the type of the element that would be loaded with
829 /// a load instruction with the specified parameters.
831 /// Null is returned if the indices are invalid for the specified
834 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
835 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
836 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
838 inline op_iterator idx_begin() { return op_begin()+1; }
839 inline const_op_iterator idx_begin() const { return op_begin()+1; }
840 inline op_iterator idx_end() { return op_end(); }
841 inline const_op_iterator idx_end() const { return op_end(); }
843 Value *getPointerOperand() {
844 return getOperand(0);
846 const Value *getPointerOperand() const {
847 return getOperand(0);
849 static unsigned getPointerOperandIndex() {
850 return 0U; // get index for modifying correct operand.
853 /// getPointerOperandType - Method to return the pointer operand as a
855 Type *getPointerOperandType() const {
856 return getPointerOperand()->getType();
859 /// \brief Returns the address space of the pointer operand.
860 unsigned getPointerAddressSpace() const {
861 return getPointerOperandType()->getPointerAddressSpace();
864 /// GetGEPReturnType - Returns the pointer type returned by the GEP
865 /// instruction, which may be a vector of pointers.
866 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
867 Type *PtrTy = PointerType::get(checkGEPType(
868 getIndexedType(Ptr->getType(), IdxList)),
869 Ptr->getType()->getPointerAddressSpace());
871 if (Ptr->getType()->isVectorTy()) {
872 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
873 return VectorType::get(PtrTy, NumElem);
880 unsigned getNumIndices() const { // Note: always non-negative
881 return getNumOperands() - 1;
884 bool hasIndices() const {
885 return getNumOperands() > 1;
888 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
889 /// zeros. If so, the result pointer and the first operand have the same
890 /// value, just potentially different types.
891 bool hasAllZeroIndices() const;
893 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
894 /// constant integers. If so, the result pointer and the first operand have
895 /// a constant offset between them.
896 bool hasAllConstantIndices() const;
898 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
899 /// See LangRef.html for the meaning of inbounds on a getelementptr.
900 void setIsInBounds(bool b = true);
902 /// isInBounds - Determine whether the GEP has the inbounds flag.
903 bool isInBounds() const;
905 /// \brief Accumulate the constant address offset of this GEP if possible.
907 /// This routine accepts an APInt into which it will accumulate the constant
908 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
909 /// all-constant, it returns false and the value of the offset APInt is
910 /// undefined (it is *not* preserved!). The APInt passed into this routine
911 /// must be at least as wide as the IntPtr type for the address space of
912 /// the base GEP pointer.
913 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
915 // Methods for support type inquiry through isa, cast, and dyn_cast:
916 static inline bool classof(const Instruction *I) {
917 return (I->getOpcode() == Instruction::GetElementPtr);
919 static inline bool classof(const Value *V) {
920 return isa<Instruction>(V) && classof(cast<Instruction>(V));
925 struct OperandTraits<GetElementPtrInst> :
926 public VariadicOperandTraits<GetElementPtrInst, 1> {
929 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
930 ArrayRef<Value *> IdxList,
932 const Twine &NameStr,
933 Instruction *InsertBefore)
934 : Instruction(getGEPReturnType(Ptr, IdxList),
936 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
937 Values, InsertBefore) {
938 init(Ptr, IdxList, NameStr);
940 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
941 ArrayRef<Value *> IdxList,
943 const Twine &NameStr,
944 BasicBlock *InsertAtEnd)
945 : Instruction(getGEPReturnType(Ptr, IdxList),
947 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
948 Values, InsertAtEnd) {
949 init(Ptr, IdxList, NameStr);
953 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
956 //===----------------------------------------------------------------------===//
958 //===----------------------------------------------------------------------===//
960 /// This instruction compares its operands according to the predicate given
961 /// to the constructor. It only operates on integers or pointers. The operands
962 /// must be identical types.
963 /// \brief Represent an integer comparison operator.
964 class ICmpInst: public CmpInst {
966 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
967 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
968 "Invalid ICmp predicate value");
969 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
970 "Both operands to ICmp instruction are not of the same type!");
971 // Check that the operands are the right type
972 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
973 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
974 "Invalid operand types for ICmp instruction");
978 /// \brief Clone an identical ICmpInst
979 ICmpInst *clone_impl() const override;
981 /// \brief Constructor with insert-before-instruction semantics.
983 Instruction *InsertBefore, ///< Where to insert
984 Predicate pred, ///< The predicate to use for the comparison
985 Value *LHS, ///< The left-hand-side of the expression
986 Value *RHS, ///< The right-hand-side of the expression
987 const Twine &NameStr = "" ///< Name of the instruction
988 ) : CmpInst(makeCmpResultType(LHS->getType()),
989 Instruction::ICmp, pred, LHS, RHS, NameStr,
996 /// \brief Constructor with insert-at-end semantics.
998 BasicBlock &InsertAtEnd, ///< Block to insert into.
999 Predicate pred, ///< The predicate to use for the comparison
1000 Value *LHS, ///< The left-hand-side of the expression
1001 Value *RHS, ///< The right-hand-side of the expression
1002 const Twine &NameStr = "" ///< Name of the instruction
1003 ) : CmpInst(makeCmpResultType(LHS->getType()),
1004 Instruction::ICmp, pred, LHS, RHS, NameStr,
1011 /// \brief Constructor with no-insertion semantics
1013 Predicate pred, ///< The predicate to use for the comparison
1014 Value *LHS, ///< The left-hand-side of the expression
1015 Value *RHS, ///< The right-hand-side of the expression
1016 const Twine &NameStr = "" ///< Name of the instruction
1017 ) : CmpInst(makeCmpResultType(LHS->getType()),
1018 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1024 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1025 /// @returns the predicate that would be the result if the operand were
1026 /// regarded as signed.
1027 /// \brief Return the signed version of the predicate
1028 Predicate getSignedPredicate() const {
1029 return getSignedPredicate(getPredicate());
1032 /// This is a static version that you can use without an instruction.
1033 /// \brief Return the signed version of the predicate.
1034 static Predicate getSignedPredicate(Predicate pred);
1036 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1037 /// @returns the predicate that would be the result if the operand were
1038 /// regarded as unsigned.
1039 /// \brief Return the unsigned version of the predicate
1040 Predicate getUnsignedPredicate() const {
1041 return getUnsignedPredicate(getPredicate());
1044 /// This is a static version that you can use without an instruction.
1045 /// \brief Return the unsigned version of the predicate.
1046 static Predicate getUnsignedPredicate(Predicate pred);
1048 /// isEquality - Return true if this predicate is either EQ or NE. This also
1049 /// tests for commutativity.
1050 static bool isEquality(Predicate P) {
1051 return P == ICMP_EQ || P == ICMP_NE;
1054 /// isEquality - Return true if this predicate is either EQ or NE. This also
1055 /// tests for commutativity.
1056 bool isEquality() const {
1057 return isEquality(getPredicate());
1060 /// @returns true if the predicate of this ICmpInst is commutative
1061 /// \brief Determine if this relation is commutative.
1062 bool isCommutative() const { return isEquality(); }
1064 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1066 bool isRelational() const {
1067 return !isEquality();
1070 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1072 static bool isRelational(Predicate P) {
1073 return !isEquality(P);
1076 /// Initialize a set of values that all satisfy the predicate with C.
1077 /// \brief Make a ConstantRange for a relation with a constant value.
1078 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1080 /// Exchange the two operands to this instruction in such a way that it does
1081 /// not modify the semantics of the instruction. The predicate value may be
1082 /// changed to retain the same result if the predicate is order dependent
1084 /// \brief Swap operands and adjust predicate.
1085 void swapOperands() {
1086 setPredicate(getSwappedPredicate());
1087 Op<0>().swap(Op<1>());
1090 // Methods for support type inquiry through isa, cast, and dyn_cast:
1091 static inline bool classof(const Instruction *I) {
1092 return I->getOpcode() == Instruction::ICmp;
1094 static inline bool classof(const Value *V) {
1095 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1100 //===----------------------------------------------------------------------===//
1102 //===----------------------------------------------------------------------===//
1104 /// This instruction compares its operands according to the predicate given
1105 /// to the constructor. It only operates on floating point values or packed
1106 /// vectors of floating point values. The operands must be identical types.
1107 /// \brief Represents a floating point comparison operator.
1108 class FCmpInst: public CmpInst {
1110 /// \brief Clone an identical FCmpInst
1111 FCmpInst *clone_impl() const override;
1113 /// \brief Constructor with insert-before-instruction semantics.
1115 Instruction *InsertBefore, ///< Where to insert
1116 Predicate pred, ///< The predicate to use for the comparison
1117 Value *LHS, ///< The left-hand-side of the expression
1118 Value *RHS, ///< The right-hand-side of the expression
1119 const Twine &NameStr = "" ///< Name of the instruction
1120 ) : CmpInst(makeCmpResultType(LHS->getType()),
1121 Instruction::FCmp, pred, LHS, RHS, NameStr,
1123 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1124 "Invalid FCmp predicate value");
1125 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1126 "Both operands to FCmp instruction are not of the same type!");
1127 // Check that the operands are the right type
1128 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1129 "Invalid operand types for FCmp instruction");
1132 /// \brief Constructor with insert-at-end semantics.
1134 BasicBlock &InsertAtEnd, ///< Block to insert into.
1135 Predicate pred, ///< The predicate to use for the comparison
1136 Value *LHS, ///< The left-hand-side of the expression
1137 Value *RHS, ///< The right-hand-side of the expression
1138 const Twine &NameStr = "" ///< Name of the instruction
1139 ) : CmpInst(makeCmpResultType(LHS->getType()),
1140 Instruction::FCmp, pred, LHS, RHS, NameStr,
1142 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1143 "Invalid FCmp predicate value");
1144 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1145 "Both operands to FCmp instruction are not of the same type!");
1146 // Check that the operands are the right type
1147 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1148 "Invalid operand types for FCmp instruction");
1151 /// \brief Constructor with no-insertion semantics
1153 Predicate pred, ///< The predicate to use for the comparison
1154 Value *LHS, ///< The left-hand-side of the expression
1155 Value *RHS, ///< The right-hand-side of the expression
1156 const Twine &NameStr = "" ///< Name of the instruction
1157 ) : CmpInst(makeCmpResultType(LHS->getType()),
1158 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1159 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1160 "Invalid FCmp predicate value");
1161 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1162 "Both operands to FCmp instruction are not of the same type!");
1163 // Check that the operands are the right type
1164 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1165 "Invalid operand types for FCmp instruction");
1168 /// @returns true if the predicate of this instruction is EQ or NE.
1169 /// \brief Determine if this is an equality predicate.
1170 bool isEquality() const {
1171 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1172 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1175 /// @returns true if the predicate of this instruction is commutative.
1176 /// \brief Determine if this is a commutative predicate.
1177 bool isCommutative() const {
1178 return isEquality() ||
1179 getPredicate() == FCMP_FALSE ||
1180 getPredicate() == FCMP_TRUE ||
1181 getPredicate() == FCMP_ORD ||
1182 getPredicate() == FCMP_UNO;
1185 /// @returns true if the predicate is relational (not EQ or NE).
1186 /// \brief Determine if this a relational predicate.
1187 bool isRelational() const { return !isEquality(); }
1189 /// Exchange the two operands to this instruction in such a way that it does
1190 /// not modify the semantics of the instruction. The predicate value may be
1191 /// changed to retain the same result if the predicate is order dependent
1193 /// \brief Swap operands and adjust predicate.
1194 void swapOperands() {
1195 setPredicate(getSwappedPredicate());
1196 Op<0>().swap(Op<1>());
1199 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1200 static inline bool classof(const Instruction *I) {
1201 return I->getOpcode() == Instruction::FCmp;
1203 static inline bool classof(const Value *V) {
1204 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1208 //===----------------------------------------------------------------------===//
1209 /// CallInst - This class represents a function call, abstracting a target
1210 /// machine's calling convention. This class uses low bit of the SubClassData
1211 /// field to indicate whether or not this is a tail call. The rest of the bits
1212 /// hold the calling convention of the call.
1214 class CallInst : public Instruction {
1215 AttributeSet AttributeList; ///< parameter attributes for call
1216 CallInst(const CallInst &CI);
1217 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1218 void init(Value *Func, const Twine &NameStr);
1220 /// Construct a CallInst given a range of arguments.
1221 /// \brief Construct a CallInst from a range of arguments
1222 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1223 const Twine &NameStr, Instruction *InsertBefore);
1225 /// Construct a CallInst given a range of arguments.
1226 /// \brief Construct a CallInst from a range of arguments
1227 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1228 const Twine &NameStr, BasicBlock *InsertAtEnd);
1230 explicit CallInst(Value *F, const Twine &NameStr,
1231 Instruction *InsertBefore);
1232 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1234 CallInst *clone_impl() const override;
1236 static CallInst *Create(Value *Func,
1237 ArrayRef<Value *> Args,
1238 const Twine &NameStr = "",
1239 Instruction *InsertBefore = 0) {
1240 return new(unsigned(Args.size() + 1))
1241 CallInst(Func, Args, NameStr, InsertBefore);
1243 static CallInst *Create(Value *Func,
1244 ArrayRef<Value *> Args,
1245 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1246 return new(unsigned(Args.size() + 1))
1247 CallInst(Func, Args, NameStr, InsertAtEnd);
1249 static CallInst *Create(Value *F, const Twine &NameStr = "",
1250 Instruction *InsertBefore = 0) {
1251 return new(1) CallInst(F, NameStr, InsertBefore);
1253 static CallInst *Create(Value *F, const Twine &NameStr,
1254 BasicBlock *InsertAtEnd) {
1255 return new(1) CallInst(F, NameStr, InsertAtEnd);
1257 /// CreateMalloc - Generate the IR for a call to malloc:
1258 /// 1. Compute the malloc call's argument as the specified type's size,
1259 /// possibly multiplied by the array size if the array size is not
1261 /// 2. Call malloc with that argument.
1262 /// 3. Bitcast the result of the malloc call to the specified type.
1263 static Instruction *CreateMalloc(Instruction *InsertBefore,
1264 Type *IntPtrTy, Type *AllocTy,
1265 Value *AllocSize, Value *ArraySize = 0,
1266 Function* MallocF = 0,
1267 const Twine &Name = "");
1268 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1269 Type *IntPtrTy, Type *AllocTy,
1270 Value *AllocSize, Value *ArraySize = 0,
1271 Function* MallocF = 0,
1272 const Twine &Name = "");
1273 /// CreateFree - Generate the IR for a call to the builtin free function.
1274 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1275 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1279 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
1280 void setTailCall(bool isTC = true) {
1281 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1285 /// Provide fast operand accessors
1286 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1288 /// getNumArgOperands - Return the number of call arguments.
1290 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1292 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1294 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1295 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1297 /// \brief Wrappers for getting the \c Use of a call argument.
1298 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1299 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1301 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1303 CallingConv::ID getCallingConv() const {
1304 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
1306 void setCallingConv(CallingConv::ID CC) {
1307 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1308 (static_cast<unsigned>(CC) << 1));
1311 /// getAttributes - Return the parameter attributes for this call.
1313 const AttributeSet &getAttributes() const { return AttributeList; }
1315 /// setAttributes - Set the parameter attributes for this call.
1317 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1319 /// addAttribute - adds the attribute to the list of attributes.
1320 void addAttribute(unsigned i, Attribute::AttrKind attr);
1322 /// removeAttribute - removes the attribute from the list of attributes.
1323 void removeAttribute(unsigned i, Attribute attr);
1325 /// \brief Determine whether this call has the given attribute.
1326 bool hasFnAttr(Attribute::AttrKind A) const {
1327 assert(A != Attribute::NoBuiltin &&
1328 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1329 return hasFnAttrImpl(A);
1332 /// \brief Determine whether the call or the callee has the given attributes.
1333 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1335 /// \brief Extract the alignment for a call or parameter (0=unknown).
1336 unsigned getParamAlignment(unsigned i) const {
1337 return AttributeList.getParamAlignment(i);
1340 /// \brief Return true if the call should not be treated as a call to a
1342 bool isNoBuiltin() const {
1343 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1344 !hasFnAttrImpl(Attribute::Builtin);
1347 /// \brief Return true if the call should not be inlined.
1348 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1349 void setIsNoInline() {
1350 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1353 /// \brief Return true if the call can return twice
1354 bool canReturnTwice() const {
1355 return hasFnAttr(Attribute::ReturnsTwice);
1357 void setCanReturnTwice() {
1358 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1361 /// \brief Determine if the call does not access memory.
1362 bool doesNotAccessMemory() const {
1363 return hasFnAttr(Attribute::ReadNone);
1365 void setDoesNotAccessMemory() {
1366 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1369 /// \brief Determine if the call does not access or only reads memory.
1370 bool onlyReadsMemory() const {
1371 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1373 void setOnlyReadsMemory() {
1374 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1377 /// \brief Determine if the call cannot return.
1378 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1379 void setDoesNotReturn() {
1380 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1383 /// \brief Determine if the call cannot unwind.
1384 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1385 void setDoesNotThrow() {
1386 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1389 /// \brief Determine if the call cannot be duplicated.
1390 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1391 void setCannotDuplicate() {
1392 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1395 /// \brief Determine if the call returns a structure through first
1396 /// pointer argument.
1397 bool hasStructRetAttr() const {
1398 // Be friendly and also check the callee.
1399 return paramHasAttr(1, Attribute::StructRet);
1402 /// \brief Determine if any call argument is an aggregate passed by value.
1403 bool hasByValArgument() const {
1404 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1407 /// getCalledFunction - Return the function called, or null if this is an
1408 /// indirect function invocation.
1410 Function *getCalledFunction() const {
1411 return dyn_cast<Function>(Op<-1>());
1414 /// getCalledValue - Get a pointer to the function that is invoked by this
1416 const Value *getCalledValue() const { return Op<-1>(); }
1417 Value *getCalledValue() { return Op<-1>(); }
1419 /// setCalledFunction - Set the function called.
1420 void setCalledFunction(Value* Fn) {
1424 /// isInlineAsm - Check if this call is an inline asm statement.
1425 bool isInlineAsm() const {
1426 return isa<InlineAsm>(Op<-1>());
1429 // Methods for support type inquiry through isa, cast, and dyn_cast:
1430 static inline bool classof(const Instruction *I) {
1431 return I->getOpcode() == Instruction::Call;
1433 static inline bool classof(const Value *V) {
1434 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1438 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1440 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1441 // method so that subclasses cannot accidentally use it.
1442 void setInstructionSubclassData(unsigned short D) {
1443 Instruction::setInstructionSubclassData(D);
1448 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1451 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1452 const Twine &NameStr, BasicBlock *InsertAtEnd)
1453 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1454 ->getElementType())->getReturnType(),
1456 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1457 unsigned(Args.size() + 1), InsertAtEnd) {
1458 init(Func, Args, NameStr);
1461 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1462 const Twine &NameStr, Instruction *InsertBefore)
1463 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1464 ->getElementType())->getReturnType(),
1466 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1467 unsigned(Args.size() + 1), InsertBefore) {
1468 init(Func, Args, NameStr);
1472 // Note: if you get compile errors about private methods then
1473 // please update your code to use the high-level operand
1474 // interfaces. See line 943 above.
1475 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1477 //===----------------------------------------------------------------------===//
1479 //===----------------------------------------------------------------------===//
1481 /// SelectInst - This class represents the LLVM 'select' instruction.
1483 class SelectInst : public Instruction {
1484 void init(Value *C, Value *S1, Value *S2) {
1485 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1491 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1492 Instruction *InsertBefore)
1493 : Instruction(S1->getType(), Instruction::Select,
1494 &Op<0>(), 3, InsertBefore) {
1498 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1499 BasicBlock *InsertAtEnd)
1500 : Instruction(S1->getType(), Instruction::Select,
1501 &Op<0>(), 3, InsertAtEnd) {
1506 SelectInst *clone_impl() const override;
1508 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1509 const Twine &NameStr = "",
1510 Instruction *InsertBefore = 0) {
1511 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1513 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1514 const Twine &NameStr,
1515 BasicBlock *InsertAtEnd) {
1516 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1519 const Value *getCondition() const { return Op<0>(); }
1520 const Value *getTrueValue() const { return Op<1>(); }
1521 const Value *getFalseValue() const { return Op<2>(); }
1522 Value *getCondition() { return Op<0>(); }
1523 Value *getTrueValue() { return Op<1>(); }
1524 Value *getFalseValue() { return Op<2>(); }
1526 /// areInvalidOperands - Return a string if the specified operands are invalid
1527 /// for a select operation, otherwise return null.
1528 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1530 /// Transparently provide more efficient getOperand methods.
1531 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1533 OtherOps getOpcode() const {
1534 return static_cast<OtherOps>(Instruction::getOpcode());
1537 // Methods for support type inquiry through isa, cast, and dyn_cast:
1538 static inline bool classof(const Instruction *I) {
1539 return I->getOpcode() == Instruction::Select;
1541 static inline bool classof(const Value *V) {
1542 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1547 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1550 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1552 //===----------------------------------------------------------------------===//
1554 //===----------------------------------------------------------------------===//
1556 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1557 /// an argument of the specified type given a va_list and increments that list
1559 class VAArgInst : public UnaryInstruction {
1561 VAArgInst *clone_impl() const override;
1564 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1565 Instruction *InsertBefore = 0)
1566 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1569 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1570 BasicBlock *InsertAtEnd)
1571 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1575 Value *getPointerOperand() { return getOperand(0); }
1576 const Value *getPointerOperand() const { return getOperand(0); }
1577 static unsigned getPointerOperandIndex() { return 0U; }
1579 // Methods for support type inquiry through isa, cast, and dyn_cast:
1580 static inline bool classof(const Instruction *I) {
1581 return I->getOpcode() == VAArg;
1583 static inline bool classof(const Value *V) {
1584 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1588 //===----------------------------------------------------------------------===//
1589 // ExtractElementInst Class
1590 //===----------------------------------------------------------------------===//
1592 /// ExtractElementInst - This instruction extracts a single (scalar)
1593 /// element from a VectorType value
1595 class ExtractElementInst : public Instruction {
1596 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1597 Instruction *InsertBefore = 0);
1598 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1599 BasicBlock *InsertAtEnd);
1601 ExtractElementInst *clone_impl() const override;
1604 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1605 const Twine &NameStr = "",
1606 Instruction *InsertBefore = 0) {
1607 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1609 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1610 const Twine &NameStr,
1611 BasicBlock *InsertAtEnd) {
1612 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1615 /// isValidOperands - Return true if an extractelement instruction can be
1616 /// formed with the specified operands.
1617 static bool isValidOperands(const Value *Vec, const Value *Idx);
1619 Value *getVectorOperand() { return Op<0>(); }
1620 Value *getIndexOperand() { return Op<1>(); }
1621 const Value *getVectorOperand() const { return Op<0>(); }
1622 const Value *getIndexOperand() const { return Op<1>(); }
1624 VectorType *getVectorOperandType() const {
1625 return cast<VectorType>(getVectorOperand()->getType());
1629 /// Transparently provide more efficient getOperand methods.
1630 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1632 // Methods for support type inquiry through isa, cast, and dyn_cast:
1633 static inline bool classof(const Instruction *I) {
1634 return I->getOpcode() == Instruction::ExtractElement;
1636 static inline bool classof(const Value *V) {
1637 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1642 struct OperandTraits<ExtractElementInst> :
1643 public FixedNumOperandTraits<ExtractElementInst, 2> {
1646 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1648 //===----------------------------------------------------------------------===//
1649 // InsertElementInst Class
1650 //===----------------------------------------------------------------------===//
1652 /// InsertElementInst - This instruction inserts a single (scalar)
1653 /// element into a VectorType value
1655 class InsertElementInst : public Instruction {
1656 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1657 const Twine &NameStr = "",
1658 Instruction *InsertBefore = 0);
1659 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1660 const Twine &NameStr, BasicBlock *InsertAtEnd);
1662 InsertElementInst *clone_impl() const override;
1665 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1666 const Twine &NameStr = "",
1667 Instruction *InsertBefore = 0) {
1668 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1670 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1671 const Twine &NameStr,
1672 BasicBlock *InsertAtEnd) {
1673 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1676 /// isValidOperands - Return true if an insertelement instruction can be
1677 /// formed with the specified operands.
1678 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1681 /// getType - Overload to return most specific vector type.
1683 VectorType *getType() const {
1684 return cast<VectorType>(Instruction::getType());
1687 /// Transparently provide more efficient getOperand methods.
1688 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1690 // Methods for support type inquiry through isa, cast, and dyn_cast:
1691 static inline bool classof(const Instruction *I) {
1692 return I->getOpcode() == Instruction::InsertElement;
1694 static inline bool classof(const Value *V) {
1695 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1700 struct OperandTraits<InsertElementInst> :
1701 public FixedNumOperandTraits<InsertElementInst, 3> {
1704 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1706 //===----------------------------------------------------------------------===//
1707 // ShuffleVectorInst Class
1708 //===----------------------------------------------------------------------===//
1710 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1713 class ShuffleVectorInst : public Instruction {
1715 ShuffleVectorInst *clone_impl() const override;
1718 // allocate space for exactly three operands
1719 void *operator new(size_t s) {
1720 return User::operator new(s, 3);
1722 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1723 const Twine &NameStr = "",
1724 Instruction *InsertBefor = 0);
1725 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1726 const Twine &NameStr, BasicBlock *InsertAtEnd);
1728 /// isValidOperands - Return true if a shufflevector instruction can be
1729 /// formed with the specified operands.
1730 static bool isValidOperands(const Value *V1, const Value *V2,
1733 /// getType - Overload to return most specific vector type.
1735 VectorType *getType() const {
1736 return cast<VectorType>(Instruction::getType());
1739 /// Transparently provide more efficient getOperand methods.
1740 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1742 Constant *getMask() const {
1743 return cast<Constant>(getOperand(2));
1746 /// getMaskValue - Return the index from the shuffle mask for the specified
1747 /// output result. This is either -1 if the element is undef or a number less
1748 /// than 2*numelements.
1749 static int getMaskValue(Constant *Mask, unsigned i);
1751 int getMaskValue(unsigned i) const {
1752 return getMaskValue(getMask(), i);
1755 /// getShuffleMask - Return the full mask for this instruction, where each
1756 /// element is the element number and undef's are returned as -1.
1757 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1759 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1760 return getShuffleMask(getMask(), Result);
1763 SmallVector<int, 16> getShuffleMask() const {
1764 SmallVector<int, 16> Mask;
1765 getShuffleMask(Mask);
1770 // Methods for support type inquiry through isa, cast, and dyn_cast:
1771 static inline bool classof(const Instruction *I) {
1772 return I->getOpcode() == Instruction::ShuffleVector;
1774 static inline bool classof(const Value *V) {
1775 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1780 struct OperandTraits<ShuffleVectorInst> :
1781 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1784 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1786 //===----------------------------------------------------------------------===//
1787 // ExtractValueInst Class
1788 //===----------------------------------------------------------------------===//
1790 /// ExtractValueInst - This instruction extracts a struct member or array
1791 /// element value from an aggregate value.
1793 class ExtractValueInst : public UnaryInstruction {
1794 SmallVector<unsigned, 4> Indices;
1796 ExtractValueInst(const ExtractValueInst &EVI);
1797 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1799 /// Constructors - Create a extractvalue instruction with a base aggregate
1800 /// value and a list of indices. The first ctor can optionally insert before
1801 /// an existing instruction, the second appends the new instruction to the
1802 /// specified BasicBlock.
1803 inline ExtractValueInst(Value *Agg,
1804 ArrayRef<unsigned> Idxs,
1805 const Twine &NameStr,
1806 Instruction *InsertBefore);
1807 inline ExtractValueInst(Value *Agg,
1808 ArrayRef<unsigned> Idxs,
1809 const Twine &NameStr, BasicBlock *InsertAtEnd);
1811 // allocate space for exactly one operand
1812 void *operator new(size_t s) {
1813 return User::operator new(s, 1);
1816 ExtractValueInst *clone_impl() const override;
1819 static ExtractValueInst *Create(Value *Agg,
1820 ArrayRef<unsigned> Idxs,
1821 const Twine &NameStr = "",
1822 Instruction *InsertBefore = 0) {
1824 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1826 static ExtractValueInst *Create(Value *Agg,
1827 ArrayRef<unsigned> Idxs,
1828 const Twine &NameStr,
1829 BasicBlock *InsertAtEnd) {
1830 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1833 /// getIndexedType - Returns the type of the element that would be extracted
1834 /// with an extractvalue instruction with the specified parameters.
1836 /// Null is returned if the indices are invalid for the specified type.
1837 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1839 typedef const unsigned* idx_iterator;
1840 inline idx_iterator idx_begin() const { return Indices.begin(); }
1841 inline idx_iterator idx_end() const { return Indices.end(); }
1843 Value *getAggregateOperand() {
1844 return getOperand(0);
1846 const Value *getAggregateOperand() const {
1847 return getOperand(0);
1849 static unsigned getAggregateOperandIndex() {
1850 return 0U; // get index for modifying correct operand
1853 ArrayRef<unsigned> getIndices() const {
1857 unsigned getNumIndices() const {
1858 return (unsigned)Indices.size();
1861 bool hasIndices() const {
1865 // Methods for support type inquiry through isa, cast, and dyn_cast:
1866 static inline bool classof(const Instruction *I) {
1867 return I->getOpcode() == Instruction::ExtractValue;
1869 static inline bool classof(const Value *V) {
1870 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1874 ExtractValueInst::ExtractValueInst(Value *Agg,
1875 ArrayRef<unsigned> Idxs,
1876 const Twine &NameStr,
1877 Instruction *InsertBefore)
1878 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1879 ExtractValue, Agg, InsertBefore) {
1880 init(Idxs, NameStr);
1882 ExtractValueInst::ExtractValueInst(Value *Agg,
1883 ArrayRef<unsigned> Idxs,
1884 const Twine &NameStr,
1885 BasicBlock *InsertAtEnd)
1886 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1887 ExtractValue, Agg, InsertAtEnd) {
1888 init(Idxs, NameStr);
1892 //===----------------------------------------------------------------------===//
1893 // InsertValueInst Class
1894 //===----------------------------------------------------------------------===//
1896 /// InsertValueInst - This instruction inserts a struct field of array element
1897 /// value into an aggregate value.
1899 class InsertValueInst : public Instruction {
1900 SmallVector<unsigned, 4> Indices;
1902 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1903 InsertValueInst(const InsertValueInst &IVI);
1904 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1905 const Twine &NameStr);
1907 /// Constructors - Create a insertvalue instruction with a base aggregate
1908 /// value, a value to insert, and a list of indices. The first ctor can
1909 /// optionally insert before an existing instruction, the second appends
1910 /// the new instruction to the specified BasicBlock.
1911 inline InsertValueInst(Value *Agg, Value *Val,
1912 ArrayRef<unsigned> Idxs,
1913 const Twine &NameStr,
1914 Instruction *InsertBefore);
1915 inline InsertValueInst(Value *Agg, Value *Val,
1916 ArrayRef<unsigned> Idxs,
1917 const Twine &NameStr, BasicBlock *InsertAtEnd);
1919 /// Constructors - These two constructors are convenience methods because one
1920 /// and two index insertvalue instructions are so common.
1921 InsertValueInst(Value *Agg, Value *Val,
1922 unsigned Idx, const Twine &NameStr = "",
1923 Instruction *InsertBefore = 0);
1924 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1925 const Twine &NameStr, BasicBlock *InsertAtEnd);
1927 InsertValueInst *clone_impl() const override;
1929 // allocate space for exactly two operands
1930 void *operator new(size_t s) {
1931 return User::operator new(s, 2);
1934 static InsertValueInst *Create(Value *Agg, Value *Val,
1935 ArrayRef<unsigned> Idxs,
1936 const Twine &NameStr = "",
1937 Instruction *InsertBefore = 0) {
1938 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1940 static InsertValueInst *Create(Value *Agg, Value *Val,
1941 ArrayRef<unsigned> Idxs,
1942 const Twine &NameStr,
1943 BasicBlock *InsertAtEnd) {
1944 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1947 /// Transparently provide more efficient getOperand methods.
1948 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1950 typedef const unsigned* idx_iterator;
1951 inline idx_iterator idx_begin() const { return Indices.begin(); }
1952 inline idx_iterator idx_end() const { return Indices.end(); }
1954 Value *getAggregateOperand() {
1955 return getOperand(0);
1957 const Value *getAggregateOperand() const {
1958 return getOperand(0);
1960 static unsigned getAggregateOperandIndex() {
1961 return 0U; // get index for modifying correct operand
1964 Value *getInsertedValueOperand() {
1965 return getOperand(1);
1967 const Value *getInsertedValueOperand() const {
1968 return getOperand(1);
1970 static unsigned getInsertedValueOperandIndex() {
1971 return 1U; // get index for modifying correct operand
1974 ArrayRef<unsigned> getIndices() const {
1978 unsigned getNumIndices() const {
1979 return (unsigned)Indices.size();
1982 bool hasIndices() const {
1986 // Methods for support type inquiry through isa, cast, and dyn_cast:
1987 static inline bool classof(const Instruction *I) {
1988 return I->getOpcode() == Instruction::InsertValue;
1990 static inline bool classof(const Value *V) {
1991 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1996 struct OperandTraits<InsertValueInst> :
1997 public FixedNumOperandTraits<InsertValueInst, 2> {
2000 InsertValueInst::InsertValueInst(Value *Agg,
2002 ArrayRef<unsigned> Idxs,
2003 const Twine &NameStr,
2004 Instruction *InsertBefore)
2005 : Instruction(Agg->getType(), InsertValue,
2006 OperandTraits<InsertValueInst>::op_begin(this),
2008 init(Agg, Val, Idxs, NameStr);
2010 InsertValueInst::InsertValueInst(Value *Agg,
2012 ArrayRef<unsigned> Idxs,
2013 const Twine &NameStr,
2014 BasicBlock *InsertAtEnd)
2015 : Instruction(Agg->getType(), InsertValue,
2016 OperandTraits<InsertValueInst>::op_begin(this),
2018 init(Agg, Val, Idxs, NameStr);
2021 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2023 //===----------------------------------------------------------------------===//
2025 //===----------------------------------------------------------------------===//
2027 // PHINode - The PHINode class is used to represent the magical mystical PHI
2028 // node, that can not exist in nature, but can be synthesized in a computer
2029 // scientist's overactive imagination.
2031 class PHINode : public Instruction {
2032 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2033 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2034 /// the number actually in use.
2035 unsigned ReservedSpace;
2036 PHINode(const PHINode &PN);
2037 // allocate space for exactly zero operands
2038 void *operator new(size_t s) {
2039 return User::operator new(s, 0);
2041 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2042 const Twine &NameStr = "", Instruction *InsertBefore = 0)
2043 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
2044 ReservedSpace(NumReservedValues) {
2046 OperandList = allocHungoffUses(ReservedSpace);
2049 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2050 BasicBlock *InsertAtEnd)
2051 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
2052 ReservedSpace(NumReservedValues) {
2054 OperandList = allocHungoffUses(ReservedSpace);
2057 // allocHungoffUses - this is more complicated than the generic
2058 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2059 // values and pointers to the incoming blocks, all in one allocation.
2060 Use *allocHungoffUses(unsigned) const;
2062 PHINode *clone_impl() const override;
2064 /// Constructors - NumReservedValues is a hint for the number of incoming
2065 /// edges that this phi node will have (use 0 if you really have no idea).
2066 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2067 const Twine &NameStr = "",
2068 Instruction *InsertBefore = 0) {
2069 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2071 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2072 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2073 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2077 /// Provide fast operand accessors
2078 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2080 // Block iterator interface. This provides access to the list of incoming
2081 // basic blocks, which parallels the list of incoming values.
2083 typedef BasicBlock **block_iterator;
2084 typedef BasicBlock * const *const_block_iterator;
2086 block_iterator block_begin() {
2088 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2089 return reinterpret_cast<block_iterator>(ref + 1);
2092 const_block_iterator block_begin() const {
2093 const Use::UserRef *ref =
2094 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2095 return reinterpret_cast<const_block_iterator>(ref + 1);
2098 block_iterator block_end() {
2099 return block_begin() + getNumOperands();
2102 const_block_iterator block_end() const {
2103 return block_begin() + getNumOperands();
2106 /// getNumIncomingValues - Return the number of incoming edges
2108 unsigned getNumIncomingValues() const { return getNumOperands(); }
2110 /// getIncomingValue - Return incoming value number x
2112 Value *getIncomingValue(unsigned i) const {
2113 return getOperand(i);
2115 void setIncomingValue(unsigned i, Value *V) {
2118 static unsigned getOperandNumForIncomingValue(unsigned i) {
2121 static unsigned getIncomingValueNumForOperand(unsigned i) {
2125 /// getIncomingBlock - Return incoming basic block number @p i.
2127 BasicBlock *getIncomingBlock(unsigned i) const {
2128 return block_begin()[i];
2131 /// getIncomingBlock - Return incoming basic block corresponding
2132 /// to an operand of the PHI.
2134 BasicBlock *getIncomingBlock(const Use &U) const {
2135 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2136 return getIncomingBlock(unsigned(&U - op_begin()));
2139 /// getIncomingBlock - Return incoming basic block corresponding
2140 /// to value use iterator.
2142 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2143 return getIncomingBlock(I.getUse());
2146 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2147 block_begin()[i] = BB;
2150 /// addIncoming - Add an incoming value to the end of the PHI list
2152 void addIncoming(Value *V, BasicBlock *BB) {
2153 assert(V && "PHI node got a null value!");
2154 assert(BB && "PHI node got a null basic block!");
2155 assert(getType() == V->getType() &&
2156 "All operands to PHI node must be the same type as the PHI node!");
2157 if (NumOperands == ReservedSpace)
2158 growOperands(); // Get more space!
2159 // Initialize some new operands.
2161 setIncomingValue(NumOperands - 1, V);
2162 setIncomingBlock(NumOperands - 1, BB);
2165 /// removeIncomingValue - Remove an incoming value. This is useful if a
2166 /// predecessor basic block is deleted. The value removed is returned.
2168 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2169 /// is true), the PHI node is destroyed and any uses of it are replaced with
2170 /// dummy values. The only time there should be zero incoming values to a PHI
2171 /// node is when the block is dead, so this strategy is sound.
2173 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2175 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2176 int Idx = getBasicBlockIndex(BB);
2177 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2178 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2181 /// getBasicBlockIndex - Return the first index of the specified basic
2182 /// block in the value list for this PHI. Returns -1 if no instance.
2184 int getBasicBlockIndex(const BasicBlock *BB) const {
2185 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2186 if (block_begin()[i] == BB)
2191 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2192 int Idx = getBasicBlockIndex(BB);
2193 assert(Idx >= 0 && "Invalid basic block argument!");
2194 return getIncomingValue(Idx);
2197 /// hasConstantValue - If the specified PHI node always merges together the
2198 /// same value, return the value, otherwise return null.
2199 Value *hasConstantValue() const;
2201 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2202 static inline bool classof(const Instruction *I) {
2203 return I->getOpcode() == Instruction::PHI;
2205 static inline bool classof(const Value *V) {
2206 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2209 void growOperands();
2213 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2216 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2218 //===----------------------------------------------------------------------===//
2219 // LandingPadInst Class
2220 //===----------------------------------------------------------------------===//
2222 //===---------------------------------------------------------------------------
2223 /// LandingPadInst - The landingpad instruction holds all of the information
2224 /// necessary to generate correct exception handling. The landingpad instruction
2225 /// cannot be moved from the top of a landing pad block, which itself is
2226 /// accessible only from the 'unwind' edge of an invoke. This uses the
2227 /// SubclassData field in Value to store whether or not the landingpad is a
2230 class LandingPadInst : public Instruction {
2231 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2232 /// the number actually in use.
2233 unsigned ReservedSpace;
2234 LandingPadInst(const LandingPadInst &LP);
2236 enum ClauseType { Catch, Filter };
2238 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2239 // Allocate space for exactly zero operands.
2240 void *operator new(size_t s) {
2241 return User::operator new(s, 0);
2243 void growOperands(unsigned Size);
2244 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2246 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2247 unsigned NumReservedValues, const Twine &NameStr,
2248 Instruction *InsertBefore);
2249 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2250 unsigned NumReservedValues, const Twine &NameStr,
2251 BasicBlock *InsertAtEnd);
2253 LandingPadInst *clone_impl() const override;
2255 /// Constructors - NumReservedClauses is a hint for the number of incoming
2256 /// clauses that this landingpad will have (use 0 if you really have no idea).
2257 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2258 unsigned NumReservedClauses,
2259 const Twine &NameStr = "",
2260 Instruction *InsertBefore = 0);
2261 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2262 unsigned NumReservedClauses,
2263 const Twine &NameStr, BasicBlock *InsertAtEnd);
2266 /// Provide fast operand accessors
2267 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2269 /// getPersonalityFn - Get the personality function associated with this
2271 Value *getPersonalityFn() const { return getOperand(0); }
2273 /// isCleanup - Return 'true' if this landingpad instruction is a
2274 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2275 /// doesn't catch the exception.
2276 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2278 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2279 void setCleanup(bool V) {
2280 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2284 /// addClause - Add a catch or filter clause to the landing pad.
2285 void addClause(Value *ClauseVal);
2287 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2288 /// to determine what type of clause this is.
2289 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2291 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2292 bool isCatch(unsigned Idx) const {
2293 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2296 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2297 bool isFilter(unsigned Idx) const {
2298 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2301 /// getNumClauses - Get the number of clauses for this landing pad.
2302 unsigned getNumClauses() const { return getNumOperands() - 1; }
2304 /// reserveClauses - Grow the size of the operand list to accommodate the new
2305 /// number of clauses.
2306 void reserveClauses(unsigned Size) { growOperands(Size); }
2308 // Methods for support type inquiry through isa, cast, and dyn_cast:
2309 static inline bool classof(const Instruction *I) {
2310 return I->getOpcode() == Instruction::LandingPad;
2312 static inline bool classof(const Value *V) {
2313 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2318 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2321 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2323 //===----------------------------------------------------------------------===//
2325 //===----------------------------------------------------------------------===//
2327 //===---------------------------------------------------------------------------
2328 /// ReturnInst - Return a value (possibly void), from a function. Execution
2329 /// does not continue in this function any longer.
2331 class ReturnInst : public TerminatorInst {
2332 ReturnInst(const ReturnInst &RI);
2335 // ReturnInst constructors:
2336 // ReturnInst() - 'ret void' instruction
2337 // ReturnInst( null) - 'ret void' instruction
2338 // ReturnInst(Value* X) - 'ret X' instruction
2339 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2340 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2341 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2342 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2344 // NOTE: If the Value* passed is of type void then the constructor behaves as
2345 // if it was passed NULL.
2346 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2347 Instruction *InsertBefore = 0);
2348 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2349 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2351 ReturnInst *clone_impl() const override;
2353 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2354 Instruction *InsertBefore = 0) {
2355 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2357 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2358 BasicBlock *InsertAtEnd) {
2359 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2361 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2362 return new(0) ReturnInst(C, InsertAtEnd);
2364 virtual ~ReturnInst();
2366 /// Provide fast operand accessors
2367 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2369 /// Convenience accessor. Returns null if there is no return value.
2370 Value *getReturnValue() const {
2371 return getNumOperands() != 0 ? getOperand(0) : 0;
2374 unsigned getNumSuccessors() const { return 0; }
2376 // Methods for support type inquiry through isa, cast, and dyn_cast:
2377 static inline bool classof(const Instruction *I) {
2378 return (I->getOpcode() == Instruction::Ret);
2380 static inline bool classof(const Value *V) {
2381 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2384 BasicBlock *getSuccessorV(unsigned idx) const override;
2385 unsigned getNumSuccessorsV() const override;
2386 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2390 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2393 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2395 //===----------------------------------------------------------------------===//
2397 //===----------------------------------------------------------------------===//
2399 //===---------------------------------------------------------------------------
2400 /// BranchInst - Conditional or Unconditional Branch instruction.
2402 class BranchInst : public TerminatorInst {
2403 /// Ops list - Branches are strange. The operands are ordered:
2404 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2405 /// they don't have to check for cond/uncond branchness. These are mostly
2406 /// accessed relative from op_end().
2407 BranchInst(const BranchInst &BI);
2409 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2410 // BranchInst(BB *B) - 'br B'
2411 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2412 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2413 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2414 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2415 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2416 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2417 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2418 Instruction *InsertBefore = 0);
2419 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2420 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2421 BasicBlock *InsertAtEnd);
2423 BranchInst *clone_impl() const override;
2425 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2426 return new(1) BranchInst(IfTrue, InsertBefore);
2428 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2429 Value *Cond, Instruction *InsertBefore = 0) {
2430 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2432 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2433 return new(1) BranchInst(IfTrue, InsertAtEnd);
2435 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2436 Value *Cond, BasicBlock *InsertAtEnd) {
2437 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2440 /// Transparently provide more efficient getOperand methods.
2441 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2443 bool isUnconditional() const { return getNumOperands() == 1; }
2444 bool isConditional() const { return getNumOperands() == 3; }
2446 Value *getCondition() const {
2447 assert(isConditional() && "Cannot get condition of an uncond branch!");
2451 void setCondition(Value *V) {
2452 assert(isConditional() && "Cannot set condition of unconditional branch!");
2456 unsigned getNumSuccessors() const { return 1+isConditional(); }
2458 BasicBlock *getSuccessor(unsigned i) const {
2459 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2460 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2463 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2464 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2465 *(&Op<-1>() - idx) = (Value*)NewSucc;
2468 /// \brief Swap the successors of this branch instruction.
2470 /// Swaps the successors of the branch instruction. This also swaps any
2471 /// branch weight metadata associated with the instruction so that it
2472 /// continues to map correctly to each operand.
2473 void swapSuccessors();
2475 // Methods for support type inquiry through isa, cast, and dyn_cast:
2476 static inline bool classof(const Instruction *I) {
2477 return (I->getOpcode() == Instruction::Br);
2479 static inline bool classof(const Value *V) {
2480 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2483 BasicBlock *getSuccessorV(unsigned idx) const override;
2484 unsigned getNumSuccessorsV() const override;
2485 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2489 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2492 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2494 //===----------------------------------------------------------------------===//
2496 //===----------------------------------------------------------------------===//
2498 //===---------------------------------------------------------------------------
2499 /// SwitchInst - Multiway switch
2501 class SwitchInst : public TerminatorInst {
2502 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2503 unsigned ReservedSpace;
2504 // Operand[0] = Value to switch on
2505 // Operand[1] = Default basic block destination
2506 // Operand[2n ] = Value to match
2507 // Operand[2n+1] = BasicBlock to go to on match
2508 SwitchInst(const SwitchInst &SI);
2509 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2510 void growOperands();
2511 // allocate space for exactly zero operands
2512 void *operator new(size_t s) {
2513 return User::operator new(s, 0);
2515 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2516 /// switch on and a default destination. The number of additional cases can
2517 /// be specified here to make memory allocation more efficient. This
2518 /// constructor can also autoinsert before another instruction.
2519 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2520 Instruction *InsertBefore);
2522 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2523 /// switch on and a default destination. The number of additional cases can
2524 /// be specified here to make memory allocation more efficient. This
2525 /// constructor also autoinserts at the end of the specified BasicBlock.
2526 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2527 BasicBlock *InsertAtEnd);
2529 SwitchInst *clone_impl() const override;
2533 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2535 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2536 class CaseIteratorT {
2544 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2546 /// Initializes case iterator for given SwitchInst and for given
2548 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2553 /// Initializes case iterator for given SwitchInst and for given
2554 /// TerminatorInst's successor index.
2555 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2556 assert(SuccessorIndex < SI->getNumSuccessors() &&
2557 "Successor index # out of range!");
2558 return SuccessorIndex != 0 ?
2559 Self(SI, SuccessorIndex - 1) :
2560 Self(SI, DefaultPseudoIndex);
2563 /// Resolves case value for current case.
2564 ConstantIntTy *getCaseValue() {
2565 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2566 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2569 /// Resolves successor for current case.
2570 BasicBlockTy *getCaseSuccessor() {
2571 assert((Index < SI->getNumCases() ||
2572 Index == DefaultPseudoIndex) &&
2573 "Index out the number of cases.");
2574 return SI->getSuccessor(getSuccessorIndex());
2577 /// Returns number of current case.
2578 unsigned getCaseIndex() const { return Index; }
2580 /// Returns TerminatorInst's successor index for current case successor.
2581 unsigned getSuccessorIndex() const {
2582 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2583 "Index out the number of cases.");
2584 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2588 // Check index correctness after increment.
2589 // Note: Index == getNumCases() means end().
2590 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2594 Self operator++(int) {
2600 // Check index correctness after decrement.
2601 // Note: Index == getNumCases() means end().
2602 // Also allow "-1" iterator here. That will became valid after ++.
2603 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2604 "Index out the number of cases.");
2608 Self operator--(int) {
2613 bool operator==(const Self& RHS) const {
2614 assert(RHS.SI == SI && "Incompatible operators.");
2615 return RHS.Index == Index;
2617 bool operator!=(const Self& RHS) const {
2618 assert(RHS.SI == SI && "Incompatible operators.");
2619 return RHS.Index != Index;
2623 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2626 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2628 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2632 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2633 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2635 /// Sets the new value for current case.
2636 void setValue(ConstantInt *V) {
2637 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2638 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2641 /// Sets the new successor for current case.
2642 void setSuccessor(BasicBlock *S) {
2643 SI->setSuccessor(getSuccessorIndex(), S);
2647 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2648 unsigned NumCases, Instruction *InsertBefore = 0) {
2649 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2651 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2652 unsigned NumCases, BasicBlock *InsertAtEnd) {
2653 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2658 /// Provide fast operand accessors
2659 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2661 // Accessor Methods for Switch stmt
2662 Value *getCondition() const { return getOperand(0); }
2663 void setCondition(Value *V) { setOperand(0, V); }
2665 BasicBlock *getDefaultDest() const {
2666 return cast<BasicBlock>(getOperand(1));
2669 void setDefaultDest(BasicBlock *DefaultCase) {
2670 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2673 /// getNumCases - return the number of 'cases' in this switch instruction,
2674 /// except the default case
2675 unsigned getNumCases() const {
2676 return getNumOperands()/2 - 1;
2679 /// Returns a read/write iterator that points to the first
2680 /// case in SwitchInst.
2681 CaseIt case_begin() {
2682 return CaseIt(this, 0);
2684 /// Returns a read-only iterator that points to the first
2685 /// case in the SwitchInst.
2686 ConstCaseIt case_begin() const {
2687 return ConstCaseIt(this, 0);
2690 /// Returns a read/write iterator that points one past the last
2691 /// in the SwitchInst.
2693 return CaseIt(this, getNumCases());
2695 /// Returns a read-only iterator that points one past the last
2696 /// in the SwitchInst.
2697 ConstCaseIt case_end() const {
2698 return ConstCaseIt(this, getNumCases());
2700 /// Returns an iterator that points to the default case.
2701 /// Note: this iterator allows to resolve successor only. Attempt
2702 /// to resolve case value causes an assertion.
2703 /// Also note, that increment and decrement also causes an assertion and
2704 /// makes iterator invalid.
2705 CaseIt case_default() {
2706 return CaseIt(this, DefaultPseudoIndex);
2708 ConstCaseIt case_default() const {
2709 return ConstCaseIt(this, DefaultPseudoIndex);
2712 /// findCaseValue - Search all of the case values for the specified constant.
2713 /// If it is explicitly handled, return the case iterator of it, otherwise
2714 /// return default case iterator to indicate
2715 /// that it is handled by the default handler.
2716 CaseIt findCaseValue(const ConstantInt *C) {
2717 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2718 if (i.getCaseValue() == C)
2720 return case_default();
2722 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2723 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2724 if (i.getCaseValue() == C)
2726 return case_default();
2729 /// findCaseDest - Finds the unique case value for a given successor. Returns
2730 /// null if the successor is not found, not unique, or is the default case.
2731 ConstantInt *findCaseDest(BasicBlock *BB) {
2732 if (BB == getDefaultDest()) return NULL;
2734 ConstantInt *CI = NULL;
2735 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2736 if (i.getCaseSuccessor() == BB) {
2737 if (CI) return NULL; // Multiple cases lead to BB.
2738 else CI = i.getCaseValue();
2744 /// addCase - Add an entry to the switch instruction...
2746 /// This action invalidates case_end(). Old case_end() iterator will
2747 /// point to the added case.
2748 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2750 /// removeCase - This method removes the specified case and its successor
2751 /// from the switch instruction. Note that this operation may reorder the
2752 /// remaining cases at index idx and above.
2754 /// This action invalidates iterators for all cases following the one removed,
2755 /// including the case_end() iterator.
2756 void removeCase(CaseIt i);
2758 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2759 BasicBlock *getSuccessor(unsigned idx) const {
2760 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2761 return cast<BasicBlock>(getOperand(idx*2+1));
2763 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2764 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2765 setOperand(idx*2+1, (Value*)NewSucc);
2768 // Methods for support type inquiry through isa, cast, and dyn_cast:
2769 static inline bool classof(const Instruction *I) {
2770 return I->getOpcode() == Instruction::Switch;
2772 static inline bool classof(const Value *V) {
2773 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2776 BasicBlock *getSuccessorV(unsigned idx) const override;
2777 unsigned getNumSuccessorsV() const override;
2778 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2782 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2785 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2788 //===----------------------------------------------------------------------===//
2789 // IndirectBrInst Class
2790 //===----------------------------------------------------------------------===//
2792 //===---------------------------------------------------------------------------
2793 /// IndirectBrInst - Indirect Branch Instruction.
2795 class IndirectBrInst : public TerminatorInst {
2796 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2797 unsigned ReservedSpace;
2798 // Operand[0] = Value to switch on
2799 // Operand[1] = Default basic block destination
2800 // Operand[2n ] = Value to match
2801 // Operand[2n+1] = BasicBlock to go to on match
2802 IndirectBrInst(const IndirectBrInst &IBI);
2803 void init(Value *Address, unsigned NumDests);
2804 void growOperands();
2805 // allocate space for exactly zero operands
2806 void *operator new(size_t s) {
2807 return User::operator new(s, 0);
2809 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2810 /// Address to jump to. The number of expected destinations can be specified
2811 /// here to make memory allocation more efficient. This constructor can also
2812 /// autoinsert before another instruction.
2813 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2815 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2816 /// Address to jump to. The number of expected destinations can be specified
2817 /// here to make memory allocation more efficient. This constructor also
2818 /// autoinserts at the end of the specified BasicBlock.
2819 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2821 IndirectBrInst *clone_impl() const override;
2823 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2824 Instruction *InsertBefore = 0) {
2825 return new IndirectBrInst(Address, NumDests, InsertBefore);
2827 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2828 BasicBlock *InsertAtEnd) {
2829 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2833 /// Provide fast operand accessors.
2834 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2836 // Accessor Methods for IndirectBrInst instruction.
2837 Value *getAddress() { return getOperand(0); }
2838 const Value *getAddress() const { return getOperand(0); }
2839 void setAddress(Value *V) { setOperand(0, V); }
2842 /// getNumDestinations - return the number of possible destinations in this
2843 /// indirectbr instruction.
2844 unsigned getNumDestinations() const { return getNumOperands()-1; }
2846 /// getDestination - Return the specified destination.
2847 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2848 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2850 /// addDestination - Add a destination.
2852 void addDestination(BasicBlock *Dest);
2854 /// removeDestination - This method removes the specified successor from the
2855 /// indirectbr instruction.
2856 void removeDestination(unsigned i);
2858 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2859 BasicBlock *getSuccessor(unsigned i) const {
2860 return cast<BasicBlock>(getOperand(i+1));
2862 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2863 setOperand(i+1, (Value*)NewSucc);
2866 // Methods for support type inquiry through isa, cast, and dyn_cast:
2867 static inline bool classof(const Instruction *I) {
2868 return I->getOpcode() == Instruction::IndirectBr;
2870 static inline bool classof(const Value *V) {
2871 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2874 BasicBlock *getSuccessorV(unsigned idx) const override;
2875 unsigned getNumSuccessorsV() const override;
2876 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2880 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2883 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2886 //===----------------------------------------------------------------------===//
2888 //===----------------------------------------------------------------------===//
2890 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2891 /// calling convention of the call.
2893 class InvokeInst : public TerminatorInst {
2894 AttributeSet AttributeList;
2895 InvokeInst(const InvokeInst &BI);
2896 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2897 ArrayRef<Value *> Args, const Twine &NameStr);
2899 /// Construct an InvokeInst given a range of arguments.
2901 /// \brief Construct an InvokeInst from a range of arguments
2902 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2903 ArrayRef<Value *> Args, unsigned Values,
2904 const Twine &NameStr, Instruction *InsertBefore);
2906 /// Construct an InvokeInst given a range of arguments.
2908 /// \brief Construct an InvokeInst from a range of arguments
2909 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2910 ArrayRef<Value *> Args, unsigned Values,
2911 const Twine &NameStr, BasicBlock *InsertAtEnd);
2913 InvokeInst *clone_impl() const override;
2915 static InvokeInst *Create(Value *Func,
2916 BasicBlock *IfNormal, BasicBlock *IfException,
2917 ArrayRef<Value *> Args, const Twine &NameStr = "",
2918 Instruction *InsertBefore = 0) {
2919 unsigned Values = unsigned(Args.size()) + 3;
2920 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2921 Values, NameStr, InsertBefore);
2923 static InvokeInst *Create(Value *Func,
2924 BasicBlock *IfNormal, BasicBlock *IfException,
2925 ArrayRef<Value *> Args, const Twine &NameStr,
2926 BasicBlock *InsertAtEnd) {
2927 unsigned Values = unsigned(Args.size()) + 3;
2928 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2929 Values, NameStr, InsertAtEnd);
2932 /// Provide fast operand accessors
2933 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2935 /// getNumArgOperands - Return the number of invoke arguments.
2937 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2939 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2941 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2942 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2944 /// \brief Wrappers for getting the \c Use of a call argument.
2945 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
2946 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
2948 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2950 CallingConv::ID getCallingConv() const {
2951 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2953 void setCallingConv(CallingConv::ID CC) {
2954 setInstructionSubclassData(static_cast<unsigned>(CC));
2957 /// getAttributes - Return the parameter attributes for this invoke.
2959 const AttributeSet &getAttributes() const { return AttributeList; }
2961 /// setAttributes - Set the parameter attributes for this invoke.
2963 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
2965 /// addAttribute - adds the attribute to the list of attributes.
2966 void addAttribute(unsigned i, Attribute::AttrKind attr);
2968 /// removeAttribute - removes the attribute from the list of attributes.
2969 void removeAttribute(unsigned i, Attribute attr);
2971 /// \brief Determine whether this call has the given attribute.
2972 bool hasFnAttr(Attribute::AttrKind A) const {
2973 assert(A != Attribute::NoBuiltin &&
2974 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
2975 return hasFnAttrImpl(A);
2978 /// \brief Determine whether the call or the callee has the given attributes.
2979 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
2981 /// \brief Extract the alignment for a call or parameter (0=unknown).
2982 unsigned getParamAlignment(unsigned i) const {
2983 return AttributeList.getParamAlignment(i);
2986 /// \brief Return true if the call should not be treated as a call to a
2988 bool isNoBuiltin() const {
2989 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
2990 // to check it by hand.
2991 return hasFnAttrImpl(Attribute::NoBuiltin) &&
2992 !hasFnAttrImpl(Attribute::Builtin);
2995 /// \brief Return true if the call should not be inlined.
2996 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
2997 void setIsNoInline() {
2998 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3001 /// \brief Determine if the call does not access memory.
3002 bool doesNotAccessMemory() const {
3003 return hasFnAttr(Attribute::ReadNone);
3005 void setDoesNotAccessMemory() {
3006 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3009 /// \brief Determine if the call does not access or only reads memory.
3010 bool onlyReadsMemory() const {
3011 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3013 void setOnlyReadsMemory() {
3014 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3017 /// \brief Determine if the call cannot return.
3018 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3019 void setDoesNotReturn() {
3020 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3023 /// \brief Determine if the call cannot unwind.
3024 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3025 void setDoesNotThrow() {
3026 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3029 /// \brief Determine if the invoke cannot be duplicated.
3030 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3031 void setCannotDuplicate() {
3032 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3035 /// \brief Determine if the call returns a structure through first
3036 /// pointer argument.
3037 bool hasStructRetAttr() const {
3038 // Be friendly and also check the callee.
3039 return paramHasAttr(1, Attribute::StructRet);
3042 /// \brief Determine if any call argument is an aggregate passed by value.
3043 bool hasByValArgument() const {
3044 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3047 /// getCalledFunction - Return the function called, or null if this is an
3048 /// indirect function invocation.
3050 Function *getCalledFunction() const {
3051 return dyn_cast<Function>(Op<-3>());
3054 /// getCalledValue - Get a pointer to the function that is invoked by this
3056 const Value *getCalledValue() const { return Op<-3>(); }
3057 Value *getCalledValue() { return Op<-3>(); }
3059 /// setCalledFunction - Set the function called.
3060 void setCalledFunction(Value* Fn) {
3064 // get*Dest - Return the destination basic blocks...
3065 BasicBlock *getNormalDest() const {
3066 return cast<BasicBlock>(Op<-2>());
3068 BasicBlock *getUnwindDest() const {
3069 return cast<BasicBlock>(Op<-1>());
3071 void setNormalDest(BasicBlock *B) {
3072 Op<-2>() = reinterpret_cast<Value*>(B);
3074 void setUnwindDest(BasicBlock *B) {
3075 Op<-1>() = reinterpret_cast<Value*>(B);
3078 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3079 /// block (the unwind destination).
3080 LandingPadInst *getLandingPadInst() const;
3082 BasicBlock *getSuccessor(unsigned i) const {
3083 assert(i < 2 && "Successor # out of range for invoke!");
3084 return i == 0 ? getNormalDest() : getUnwindDest();
3087 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3088 assert(idx < 2 && "Successor # out of range for invoke!");
3089 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3092 unsigned getNumSuccessors() const { return 2; }
3094 // Methods for support type inquiry through isa, cast, and dyn_cast:
3095 static inline bool classof(const Instruction *I) {
3096 return (I->getOpcode() == Instruction::Invoke);
3098 static inline bool classof(const Value *V) {
3099 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3103 BasicBlock *getSuccessorV(unsigned idx) const override;
3104 unsigned getNumSuccessorsV() const override;
3105 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3107 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3109 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3110 // method so that subclasses cannot accidentally use it.
3111 void setInstructionSubclassData(unsigned short D) {
3112 Instruction::setInstructionSubclassData(D);
3117 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3120 InvokeInst::InvokeInst(Value *Func,
3121 BasicBlock *IfNormal, BasicBlock *IfException,
3122 ArrayRef<Value *> Args, unsigned Values,
3123 const Twine &NameStr, Instruction *InsertBefore)
3124 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3125 ->getElementType())->getReturnType(),
3126 Instruction::Invoke,
3127 OperandTraits<InvokeInst>::op_end(this) - Values,
3128 Values, InsertBefore) {
3129 init(Func, IfNormal, IfException, Args, NameStr);
3131 InvokeInst::InvokeInst(Value *Func,
3132 BasicBlock *IfNormal, BasicBlock *IfException,
3133 ArrayRef<Value *> Args, unsigned Values,
3134 const Twine &NameStr, BasicBlock *InsertAtEnd)
3135 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3136 ->getElementType())->getReturnType(),
3137 Instruction::Invoke,
3138 OperandTraits<InvokeInst>::op_end(this) - Values,
3139 Values, InsertAtEnd) {
3140 init(Func, IfNormal, IfException, Args, NameStr);
3143 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3145 //===----------------------------------------------------------------------===//
3147 //===----------------------------------------------------------------------===//
3149 //===---------------------------------------------------------------------------
3150 /// ResumeInst - Resume the propagation of an exception.
3152 class ResumeInst : public TerminatorInst {
3153 ResumeInst(const ResumeInst &RI);
3155 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3156 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3158 ResumeInst *clone_impl() const override;
3160 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3161 return new(1) ResumeInst(Exn, InsertBefore);
3163 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3164 return new(1) ResumeInst(Exn, InsertAtEnd);
3167 /// Provide fast operand accessors
3168 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3170 /// Convenience accessor.
3171 Value *getValue() const { return Op<0>(); }
3173 unsigned getNumSuccessors() const { return 0; }
3175 // Methods for support type inquiry through isa, cast, and dyn_cast:
3176 static inline bool classof(const Instruction *I) {
3177 return I->getOpcode() == Instruction::Resume;
3179 static inline bool classof(const Value *V) {
3180 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3183 BasicBlock *getSuccessorV(unsigned idx) const override;
3184 unsigned getNumSuccessorsV() const override;
3185 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3189 struct OperandTraits<ResumeInst> :
3190 public FixedNumOperandTraits<ResumeInst, 1> {
3193 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3195 //===----------------------------------------------------------------------===//
3196 // UnreachableInst Class
3197 //===----------------------------------------------------------------------===//
3199 //===---------------------------------------------------------------------------
3200 /// UnreachableInst - This function has undefined behavior. In particular, the
3201 /// presence of this instruction indicates some higher level knowledge that the
3202 /// end of the block cannot be reached.
3204 class UnreachableInst : public TerminatorInst {
3205 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3207 UnreachableInst *clone_impl() const override;
3210 // allocate space for exactly zero operands
3211 void *operator new(size_t s) {
3212 return User::operator new(s, 0);
3214 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3215 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3217 unsigned getNumSuccessors() const { return 0; }
3219 // Methods for support type inquiry through isa, cast, and dyn_cast:
3220 static inline bool classof(const Instruction *I) {
3221 return I->getOpcode() == Instruction::Unreachable;
3223 static inline bool classof(const Value *V) {
3224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3227 BasicBlock *getSuccessorV(unsigned idx) const override;
3228 unsigned getNumSuccessorsV() const override;
3229 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3232 //===----------------------------------------------------------------------===//
3234 //===----------------------------------------------------------------------===//
3236 /// \brief This class represents a truncation of integer types.
3237 class TruncInst : public CastInst {
3239 /// \brief Clone an identical TruncInst
3240 TruncInst *clone_impl() const override;
3243 /// \brief Constructor with insert-before-instruction semantics
3245 Value *S, ///< The value to be truncated
3246 Type *Ty, ///< The (smaller) type to truncate to
3247 const Twine &NameStr = "", ///< A name for the new instruction
3248 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3251 /// \brief Constructor with insert-at-end-of-block semantics
3253 Value *S, ///< The value to be truncated
3254 Type *Ty, ///< The (smaller) type to truncate to
3255 const Twine &NameStr, ///< A name for the new instruction
3256 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3259 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3260 static inline bool classof(const Instruction *I) {
3261 return I->getOpcode() == Trunc;
3263 static inline bool classof(const Value *V) {
3264 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3268 //===----------------------------------------------------------------------===//
3270 //===----------------------------------------------------------------------===//
3272 /// \brief This class represents zero extension of integer types.
3273 class ZExtInst : public CastInst {
3275 /// \brief Clone an identical ZExtInst
3276 ZExtInst *clone_impl() const override;
3279 /// \brief Constructor with insert-before-instruction semantics
3281 Value *S, ///< The value to be zero extended
3282 Type *Ty, ///< The type to zero extend to
3283 const Twine &NameStr = "", ///< A name for the new instruction
3284 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3287 /// \brief Constructor with insert-at-end semantics.
3289 Value *S, ///< The value to be zero extended
3290 Type *Ty, ///< The type to zero extend to
3291 const Twine &NameStr, ///< A name for the new instruction
3292 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3295 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3296 static inline bool classof(const Instruction *I) {
3297 return I->getOpcode() == ZExt;
3299 static inline bool classof(const Value *V) {
3300 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3304 //===----------------------------------------------------------------------===//
3306 //===----------------------------------------------------------------------===//
3308 /// \brief This class represents a sign extension of integer types.
3309 class SExtInst : public CastInst {
3311 /// \brief Clone an identical SExtInst
3312 SExtInst *clone_impl() const override;
3315 /// \brief Constructor with insert-before-instruction semantics
3317 Value *S, ///< The value to be sign extended
3318 Type *Ty, ///< The type to sign extend to
3319 const Twine &NameStr = "", ///< A name for the new instruction
3320 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3323 /// \brief Constructor with insert-at-end-of-block semantics
3325 Value *S, ///< The value to be sign extended
3326 Type *Ty, ///< The type to sign extend to
3327 const Twine &NameStr, ///< A name for the new instruction
3328 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3331 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3332 static inline bool classof(const Instruction *I) {
3333 return I->getOpcode() == SExt;
3335 static inline bool classof(const Value *V) {
3336 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3340 //===----------------------------------------------------------------------===//
3341 // FPTruncInst Class
3342 //===----------------------------------------------------------------------===//
3344 /// \brief This class represents a truncation of floating point types.
3345 class FPTruncInst : public CastInst {
3347 /// \brief Clone an identical FPTruncInst
3348 FPTruncInst *clone_impl() const override;
3351 /// \brief Constructor with insert-before-instruction semantics
3353 Value *S, ///< The value to be truncated
3354 Type *Ty, ///< The type to truncate to
3355 const Twine &NameStr = "", ///< A name for the new instruction
3356 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3359 /// \brief Constructor with insert-before-instruction semantics
3361 Value *S, ///< The value to be truncated
3362 Type *Ty, ///< The type to truncate to
3363 const Twine &NameStr, ///< A name for the new instruction
3364 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3367 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3368 static inline bool classof(const Instruction *I) {
3369 return I->getOpcode() == FPTrunc;
3371 static inline bool classof(const Value *V) {
3372 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3376 //===----------------------------------------------------------------------===//
3378 //===----------------------------------------------------------------------===//
3380 /// \brief This class represents an extension of floating point types.
3381 class FPExtInst : public CastInst {
3383 /// \brief Clone an identical FPExtInst
3384 FPExtInst *clone_impl() const override;
3387 /// \brief Constructor with insert-before-instruction semantics
3389 Value *S, ///< The value to be extended
3390 Type *Ty, ///< The type to extend to
3391 const Twine &NameStr = "", ///< A name for the new instruction
3392 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3395 /// \brief Constructor with insert-at-end-of-block semantics
3397 Value *S, ///< The value to be extended
3398 Type *Ty, ///< The type to extend to
3399 const Twine &NameStr, ///< A name for the new instruction
3400 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3403 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3404 static inline bool classof(const Instruction *I) {
3405 return I->getOpcode() == FPExt;
3407 static inline bool classof(const Value *V) {
3408 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3412 //===----------------------------------------------------------------------===//
3414 //===----------------------------------------------------------------------===//
3416 /// \brief This class represents a cast unsigned integer to floating point.
3417 class UIToFPInst : public CastInst {
3419 /// \brief Clone an identical UIToFPInst
3420 UIToFPInst *clone_impl() const override;
3423 /// \brief Constructor with insert-before-instruction semantics
3425 Value *S, ///< The value to be converted
3426 Type *Ty, ///< The type to convert to
3427 const Twine &NameStr = "", ///< A name for the new instruction
3428 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3431 /// \brief Constructor with insert-at-end-of-block semantics
3433 Value *S, ///< The value to be converted
3434 Type *Ty, ///< The type to convert to
3435 const Twine &NameStr, ///< A name for the new instruction
3436 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3439 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3440 static inline bool classof(const Instruction *I) {
3441 return I->getOpcode() == UIToFP;
3443 static inline bool classof(const Value *V) {
3444 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3448 //===----------------------------------------------------------------------===//
3450 //===----------------------------------------------------------------------===//
3452 /// \brief This class represents a cast from signed integer to floating point.
3453 class SIToFPInst : public CastInst {
3455 /// \brief Clone an identical SIToFPInst
3456 SIToFPInst *clone_impl() const override;
3459 /// \brief Constructor with insert-before-instruction semantics
3461 Value *S, ///< The value to be converted
3462 Type *Ty, ///< The type to convert to
3463 const Twine &NameStr = "", ///< A name for the new instruction
3464 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3467 /// \brief Constructor with insert-at-end-of-block semantics
3469 Value *S, ///< The value to be converted
3470 Type *Ty, ///< The type to convert to
3471 const Twine &NameStr, ///< A name for the new instruction
3472 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3475 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3476 static inline bool classof(const Instruction *I) {
3477 return I->getOpcode() == SIToFP;
3479 static inline bool classof(const Value *V) {
3480 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3484 //===----------------------------------------------------------------------===//
3486 //===----------------------------------------------------------------------===//
3488 /// \brief This class represents a cast from floating point to unsigned integer
3489 class FPToUIInst : public CastInst {
3491 /// \brief Clone an identical FPToUIInst
3492 FPToUIInst *clone_impl() const override;
3495 /// \brief Constructor with insert-before-instruction semantics
3497 Value *S, ///< The value to be converted
3498 Type *Ty, ///< The type to convert to
3499 const Twine &NameStr = "", ///< A name for the new instruction
3500 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3503 /// \brief Constructor with insert-at-end-of-block semantics
3505 Value *S, ///< The value to be converted
3506 Type *Ty, ///< The type to convert to
3507 const Twine &NameStr, ///< A name for the new instruction
3508 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3511 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3512 static inline bool classof(const Instruction *I) {
3513 return I->getOpcode() == FPToUI;
3515 static inline bool classof(const Value *V) {
3516 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3520 //===----------------------------------------------------------------------===//
3522 //===----------------------------------------------------------------------===//
3524 /// \brief This class represents a cast from floating point to signed integer.
3525 class FPToSIInst : public CastInst {
3527 /// \brief Clone an identical FPToSIInst
3528 FPToSIInst *clone_impl() const override;
3531 /// \brief Constructor with insert-before-instruction semantics
3533 Value *S, ///< The value to be converted
3534 Type *Ty, ///< The type to convert to
3535 const Twine &NameStr = "", ///< A name for the new instruction
3536 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3539 /// \brief Constructor with insert-at-end-of-block semantics
3541 Value *S, ///< The value to be converted
3542 Type *Ty, ///< The type to convert to
3543 const Twine &NameStr, ///< A name for the new instruction
3544 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3547 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3548 static inline bool classof(const Instruction *I) {
3549 return I->getOpcode() == FPToSI;
3551 static inline bool classof(const Value *V) {
3552 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3556 //===----------------------------------------------------------------------===//
3557 // IntToPtrInst Class
3558 //===----------------------------------------------------------------------===//
3560 /// \brief This class represents a cast from an integer to a pointer.
3561 class IntToPtrInst : public CastInst {
3563 /// \brief Constructor with insert-before-instruction semantics
3565 Value *S, ///< The value to be converted
3566 Type *Ty, ///< The type to convert to
3567 const Twine &NameStr = "", ///< A name for the new instruction
3568 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3571 /// \brief Constructor with insert-at-end-of-block semantics
3573 Value *S, ///< The value to be converted
3574 Type *Ty, ///< The type to convert to
3575 const Twine &NameStr, ///< A name for the new instruction
3576 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3579 /// \brief Clone an identical IntToPtrInst
3580 IntToPtrInst *clone_impl() const override;
3582 /// \brief Returns the address space of this instruction's pointer type.
3583 unsigned getAddressSpace() const {
3584 return getType()->getPointerAddressSpace();
3587 // Methods for support type inquiry through isa, cast, and dyn_cast:
3588 static inline bool classof(const Instruction *I) {
3589 return I->getOpcode() == IntToPtr;
3591 static inline bool classof(const Value *V) {
3592 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3596 //===----------------------------------------------------------------------===//
3597 // PtrToIntInst Class
3598 //===----------------------------------------------------------------------===//
3600 /// \brief This class represents a cast from a pointer to an integer
3601 class PtrToIntInst : public CastInst {
3603 /// \brief Clone an identical PtrToIntInst
3604 PtrToIntInst *clone_impl() const override;
3607 /// \brief Constructor with insert-before-instruction semantics
3609 Value *S, ///< The value to be converted
3610 Type *Ty, ///< The type to convert to
3611 const Twine &NameStr = "", ///< A name for the new instruction
3612 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3615 /// \brief Constructor with insert-at-end-of-block semantics
3617 Value *S, ///< The value to be converted
3618 Type *Ty, ///< The type to convert to
3619 const Twine &NameStr, ///< A name for the new instruction
3620 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3623 /// \brief Gets the pointer operand.
3624 Value *getPointerOperand() { return getOperand(0); }
3625 /// \brief Gets the pointer operand.
3626 const Value *getPointerOperand() const { return getOperand(0); }
3627 /// \brief Gets the operand index of the pointer operand.
3628 static unsigned getPointerOperandIndex() { return 0U; }
3630 /// \brief Returns the address space of the pointer operand.
3631 unsigned getPointerAddressSpace() const {
3632 return getPointerOperand()->getType()->getPointerAddressSpace();
3635 // Methods for support type inquiry through isa, cast, and dyn_cast:
3636 static inline bool classof(const Instruction *I) {
3637 return I->getOpcode() == PtrToInt;
3639 static inline bool classof(const Value *V) {
3640 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3644 //===----------------------------------------------------------------------===//
3645 // BitCastInst Class
3646 //===----------------------------------------------------------------------===//
3648 /// \brief This class represents a no-op cast from one type to another.
3649 class BitCastInst : public CastInst {
3651 /// \brief Clone an identical BitCastInst
3652 BitCastInst *clone_impl() const override;
3655 /// \brief Constructor with insert-before-instruction semantics
3657 Value *S, ///< The value to be casted
3658 Type *Ty, ///< The type to casted to
3659 const Twine &NameStr = "", ///< A name for the new instruction
3660 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3663 /// \brief Constructor with insert-at-end-of-block semantics
3665 Value *S, ///< The value to be casted
3666 Type *Ty, ///< The type to casted to
3667 const Twine &NameStr, ///< A name for the new instruction
3668 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3671 // Methods for support type inquiry through isa, cast, and dyn_cast:
3672 static inline bool classof(const Instruction *I) {
3673 return I->getOpcode() == BitCast;
3675 static inline bool classof(const Value *V) {
3676 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3680 //===----------------------------------------------------------------------===//
3681 // AddrSpaceCastInst Class
3682 //===----------------------------------------------------------------------===//
3684 /// \brief This class represents a conversion between pointers from
3685 /// one address space to another.
3686 class AddrSpaceCastInst : public CastInst {
3688 /// \brief Clone an identical AddrSpaceCastInst
3689 AddrSpaceCastInst *clone_impl() const override;
3692 /// \brief Constructor with insert-before-instruction semantics
3694 Value *S, ///< The value to be casted
3695 Type *Ty, ///< The type to casted to
3696 const Twine &NameStr = "", ///< A name for the new instruction
3697 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3700 /// \brief Constructor with insert-at-end-of-block semantics
3702 Value *S, ///< The value to be casted
3703 Type *Ty, ///< The type to casted to
3704 const Twine &NameStr, ///< A name for the new instruction
3705 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3708 // Methods for support type inquiry through isa, cast, and dyn_cast:
3709 static inline bool classof(const Instruction *I) {
3710 return I->getOpcode() == AddrSpaceCast;
3712 static inline bool classof(const Value *V) {
3713 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3717 } // End llvm namespace