blob: 9be8bd1a07bc94837a3841d779a78432e1cd79ce [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/Instructions.h - Instruction subclass definitions ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_INSTRUCTIONS_H
17#define LLVM_IR_INSTRUCTIONS_H
18
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/None.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Twine.h"
25#include "llvm/ADT/iterator.h"
26#include "llvm/ADT/iterator_range.h"
27#include "llvm/IR/Attributes.h"
28#include "llvm/IR/BasicBlock.h"
29#include "llvm/IR/CallingConv.h"
30#include "llvm/IR/Constant.h"
31#include "llvm/IR/DerivedTypes.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/InstrTypes.h"
34#include "llvm/IR/Instruction.h"
35#include "llvm/IR/OperandTraits.h"
36#include "llvm/IR/Type.h"
37#include "llvm/IR/Use.h"
38#include "llvm/IR/User.h"
39#include "llvm/IR/Value.h"
40#include "llvm/Support/AtomicOrdering.h"
41#include "llvm/Support/Casting.h"
42#include "llvm/Support/ErrorHandling.h"
43#include <cassert>
44#include <cstddef>
45#include <cstdint>
46#include <iterator>
47
48namespace llvm {
49
50class APInt;
51class ConstantInt;
52class DataLayout;
53class LLVMContext;
54
55//===----------------------------------------------------------------------===//
56// AllocaInst Class
57//===----------------------------------------------------------------------===//
58
59/// an instruction to allocate memory on the stack
60class AllocaInst : public UnaryInstruction {
61 Type *AllocatedType;
62
63protected:
64 // Note: Instruction needs to be a friend here to call cloneImpl.
65 friend class Instruction;
66
67 AllocaInst *cloneImpl() const;
68
69public:
70 explicit AllocaInst(Type *Ty, unsigned AddrSpace,
71 Value *ArraySize = nullptr,
72 const Twine &Name = "",
73 Instruction *InsertBefore = nullptr);
74 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
75 const Twine &Name, BasicBlock *InsertAtEnd);
76
77 AllocaInst(Type *Ty, unsigned AddrSpace,
78 const Twine &Name, Instruction *InsertBefore = nullptr);
79 AllocaInst(Type *Ty, unsigned AddrSpace,
80 const Twine &Name, BasicBlock *InsertAtEnd);
81
82 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, unsigned Align,
83 const Twine &Name = "", Instruction *InsertBefore = nullptr);
84 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, unsigned Align,
85 const Twine &Name, BasicBlock *InsertAtEnd);
86
87 /// Return true if there is an allocation size parameter to the allocation
88 /// instruction that is not 1.
89 bool isArrayAllocation() const;
90
91 /// Get the number of elements allocated. For a simple allocation of a single
92 /// element, this will return a constant 1 value.
93 const Value *getArraySize() const { return getOperand(0); }
94 Value *getArraySize() { return getOperand(0); }
95
96 /// Overload to return most specific pointer type.
97 PointerType *getType() const {
98 return cast<PointerType>(Instruction::getType());
99 }
100
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100101 /// Get allocation size in bits. Returns None if size can't be determined,
102 /// e.g. in case of a VLA.
103 Optional<uint64_t> getAllocationSizeInBits(const DataLayout &DL) const;
104
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100105 /// Return the type that is being allocated by the instruction.
106 Type *getAllocatedType() const { return AllocatedType; }
107 /// for use only in special circumstances that need to generically
108 /// transform a whole instruction (eg: IR linking and vectorization).
109 void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
110
111 /// Return the alignment of the memory that is being allocated by the
112 /// instruction.
113 unsigned getAlignment() const {
114 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
115 }
116 void setAlignment(unsigned Align);
117
118 /// Return true if this alloca is in the entry block of the function and is a
119 /// constant size. If so, the code generator will fold it into the
120 /// prolog/epilog code, so it is basically free.
121 bool isStaticAlloca() const;
122
123 /// Return true if this alloca is used as an inalloca argument to a call. Such
124 /// allocas are never considered static even if they are in the entry block.
125 bool isUsedWithInAlloca() const {
126 return getSubclassDataFromInstruction() & 32;
127 }
128
129 /// Specify whether this alloca is used to represent the arguments to a call.
130 void setUsedWithInAlloca(bool V) {
131 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
132 (V ? 32 : 0));
133 }
134
135 /// Return true if this alloca is used as a swifterror argument to a call.
136 bool isSwiftError() const {
137 return getSubclassDataFromInstruction() & 64;
138 }
139
140 /// Specify whether this alloca is used to represent a swifterror.
141 void setSwiftError(bool V) {
142 setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) |
143 (V ? 64 : 0));
144 }
145
146 // Methods for support type inquiry through isa, cast, and dyn_cast:
147 static bool classof(const Instruction *I) {
148 return (I->getOpcode() == Instruction::Alloca);
149 }
150 static bool classof(const Value *V) {
151 return isa<Instruction>(V) && classof(cast<Instruction>(V));
152 }
153
154private:
155 // Shadow Instruction::setInstructionSubclassData with a private forwarding
156 // method so that subclasses cannot accidentally use it.
157 void setInstructionSubclassData(unsigned short D) {
158 Instruction::setInstructionSubclassData(D);
159 }
160};
161
162//===----------------------------------------------------------------------===//
163// LoadInst Class
164//===----------------------------------------------------------------------===//
165
166/// An instruction for reading from memory. This uses the SubclassData field in
167/// Value to store whether or not the load is volatile.
168class LoadInst : public UnaryInstruction {
169 void AssertOK();
170
171protected:
172 // Note: Instruction needs to be a friend here to call cloneImpl.
173 friend class Instruction;
174
175 LoadInst *cloneImpl() const;
176
177public:
178 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
179 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
180 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile = false,
181 Instruction *InsertBefore = nullptr);
182 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
183 Instruction *InsertBefore = nullptr)
184 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
185 NameStr, isVolatile, InsertBefore) {}
186 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
187 BasicBlock *InsertAtEnd);
188 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
189 Instruction *InsertBefore = nullptr)
190 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
191 NameStr, isVolatile, Align, InsertBefore) {}
192 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
193 unsigned Align, Instruction *InsertBefore = nullptr);
194 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
195 unsigned Align, BasicBlock *InsertAtEnd);
196 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
197 AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System,
198 Instruction *InsertBefore = nullptr)
199 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
200 NameStr, isVolatile, Align, Order, SSID, InsertBefore) {}
201 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
202 unsigned Align, AtomicOrdering Order,
203 SyncScope::ID SSID = SyncScope::System,
204 Instruction *InsertBefore = nullptr);
205 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
206 unsigned Align, AtomicOrdering Order, SyncScope::ID SSID,
207 BasicBlock *InsertAtEnd);
208 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
209 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
210 LoadInst(Type *Ty, Value *Ptr, const char *NameStr = nullptr,
211 bool isVolatile = false, Instruction *InsertBefore = nullptr);
212 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
213 bool isVolatile = false,
214 Instruction *InsertBefore = nullptr)
215 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
216 NameStr, isVolatile, InsertBefore) {}
217 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
218 BasicBlock *InsertAtEnd);
219
220 /// Return true if this is a load from a volatile memory location.
221 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
222
223 /// Specify whether this is a volatile load or not.
224 void setVolatile(bool V) {
225 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
226 (V ? 1 : 0));
227 }
228
229 /// Return the alignment of the access that is being performed.
230 unsigned getAlignment() const {
231 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
232 }
233
234 void setAlignment(unsigned Align);
235
236 /// Returns the ordering constraint of this load instruction.
237 AtomicOrdering getOrdering() const {
238 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
239 }
240
241 /// Sets the ordering constraint of this load instruction. May not be Release
242 /// or AcquireRelease.
243 void setOrdering(AtomicOrdering Ordering) {
244 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
245 ((unsigned)Ordering << 7));
246 }
247
248 /// Returns the synchronization scope ID of this load instruction.
249 SyncScope::ID getSyncScopeID() const {
250 return SSID;
251 }
252
253 /// Sets the synchronization scope ID of this load instruction.
254 void setSyncScopeID(SyncScope::ID SSID) {
255 this->SSID = SSID;
256 }
257
258 /// Sets the ordering constraint and the synchronization scope ID of this load
259 /// instruction.
260 void setAtomic(AtomicOrdering Ordering,
261 SyncScope::ID SSID = SyncScope::System) {
262 setOrdering(Ordering);
263 setSyncScopeID(SSID);
264 }
265
266 bool isSimple() const { return !isAtomic() && !isVolatile(); }
267
268 bool isUnordered() const {
269 return (getOrdering() == AtomicOrdering::NotAtomic ||
270 getOrdering() == AtomicOrdering::Unordered) &&
271 !isVolatile();
272 }
273
274 Value *getPointerOperand() { return getOperand(0); }
275 const Value *getPointerOperand() const { return getOperand(0); }
276 static unsigned getPointerOperandIndex() { return 0U; }
277 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
278
279 /// Returns the address space of the pointer operand.
280 unsigned getPointerAddressSpace() const {
281 return getPointerOperandType()->getPointerAddressSpace();
282 }
283
284 // Methods for support type inquiry through isa, cast, and dyn_cast:
285 static bool classof(const Instruction *I) {
286 return I->getOpcode() == Instruction::Load;
287 }
288 static bool classof(const Value *V) {
289 return isa<Instruction>(V) && classof(cast<Instruction>(V));
290 }
291
292private:
293 // Shadow Instruction::setInstructionSubclassData with a private forwarding
294 // method so that subclasses cannot accidentally use it.
295 void setInstructionSubclassData(unsigned short D) {
296 Instruction::setInstructionSubclassData(D);
297 }
298
299 /// The synchronization scope ID of this load instruction. Not quite enough
300 /// room in SubClassData for everything, so synchronization scope ID gets its
301 /// own field.
302 SyncScope::ID SSID;
303};
304
305//===----------------------------------------------------------------------===//
306// StoreInst Class
307//===----------------------------------------------------------------------===//
308
309/// An instruction for storing to memory.
310class StoreInst : public Instruction {
311 void AssertOK();
312
313protected:
314 // Note: Instruction needs to be a friend here to call cloneImpl.
315 friend class Instruction;
316
317 StoreInst *cloneImpl() const;
318
319public:
320 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
321 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
322 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
323 Instruction *InsertBefore = nullptr);
324 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
325 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
326 unsigned Align, Instruction *InsertBefore = nullptr);
327 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
328 unsigned Align, BasicBlock *InsertAtEnd);
329 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
330 unsigned Align, AtomicOrdering Order,
331 SyncScope::ID SSID = SyncScope::System,
332 Instruction *InsertBefore = nullptr);
333 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
334 unsigned Align, AtomicOrdering Order, SyncScope::ID SSID,
335 BasicBlock *InsertAtEnd);
336
337 // allocate space for exactly two operands
338 void *operator new(size_t s) {
339 return User::operator new(s, 2);
340 }
341
342 /// Return true if this is a store to a volatile memory location.
343 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
344
345 /// Specify whether this is a volatile store or not.
346 void setVolatile(bool V) {
347 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
348 (V ? 1 : 0));
349 }
350
351 /// Transparently provide more efficient getOperand methods.
352 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
353
354 /// Return the alignment of the access that is being performed
355 unsigned getAlignment() const {
356 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
357 }
358
359 void setAlignment(unsigned Align);
360
361 /// Returns the ordering constraint of this store instruction.
362 AtomicOrdering getOrdering() const {
363 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
364 }
365
366 /// Sets the ordering constraint of this store instruction. May not be
367 /// Acquire or AcquireRelease.
368 void setOrdering(AtomicOrdering Ordering) {
369 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
370 ((unsigned)Ordering << 7));
371 }
372
373 /// Returns the synchronization scope ID of this store instruction.
374 SyncScope::ID getSyncScopeID() const {
375 return SSID;
376 }
377
378 /// Sets the synchronization scope ID of this store instruction.
379 void setSyncScopeID(SyncScope::ID SSID) {
380 this->SSID = SSID;
381 }
382
383 /// Sets the ordering constraint and the synchronization scope ID of this
384 /// store instruction.
385 void setAtomic(AtomicOrdering Ordering,
386 SyncScope::ID SSID = SyncScope::System) {
387 setOrdering(Ordering);
388 setSyncScopeID(SSID);
389 }
390
391 bool isSimple() const { return !isAtomic() && !isVolatile(); }
392
393 bool isUnordered() const {
394 return (getOrdering() == AtomicOrdering::NotAtomic ||
395 getOrdering() == AtomicOrdering::Unordered) &&
396 !isVolatile();
397 }
398
399 Value *getValueOperand() { return getOperand(0); }
400 const Value *getValueOperand() const { return getOperand(0); }
401
402 Value *getPointerOperand() { return getOperand(1); }
403 const Value *getPointerOperand() const { return getOperand(1); }
404 static unsigned getPointerOperandIndex() { return 1U; }
405 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
406
407 /// Returns the address space of the pointer operand.
408 unsigned getPointerAddressSpace() const {
409 return getPointerOperandType()->getPointerAddressSpace();
410 }
411
412 // Methods for support type inquiry through isa, cast, and dyn_cast:
413 static bool classof(const Instruction *I) {
414 return I->getOpcode() == Instruction::Store;
415 }
416 static bool classof(const Value *V) {
417 return isa<Instruction>(V) && classof(cast<Instruction>(V));
418 }
419
420private:
421 // Shadow Instruction::setInstructionSubclassData with a private forwarding
422 // method so that subclasses cannot accidentally use it.
423 void setInstructionSubclassData(unsigned short D) {
424 Instruction::setInstructionSubclassData(D);
425 }
426
427 /// The synchronization scope ID of this store instruction. Not quite enough
428 /// room in SubClassData for everything, so synchronization scope ID gets its
429 /// own field.
430 SyncScope::ID SSID;
431};
432
433template <>
434struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
435};
436
437DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
438
439//===----------------------------------------------------------------------===//
440// FenceInst Class
441//===----------------------------------------------------------------------===//
442
443/// An instruction for ordering other memory operations.
444class FenceInst : public Instruction {
445 void Init(AtomicOrdering Ordering, SyncScope::ID SSID);
446
447protected:
448 // Note: Instruction needs to be a friend here to call cloneImpl.
449 friend class Instruction;
450
451 FenceInst *cloneImpl() const;
452
453public:
454 // Ordering may only be Acquire, Release, AcquireRelease, or
455 // SequentiallyConsistent.
456 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
457 SyncScope::ID SSID = SyncScope::System,
458 Instruction *InsertBefore = nullptr);
459 FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID,
460 BasicBlock *InsertAtEnd);
461
462 // allocate space for exactly zero operands
463 void *operator new(size_t s) {
464 return User::operator new(s, 0);
465 }
466
467 /// Returns the ordering constraint of this fence instruction.
468 AtomicOrdering getOrdering() const {
469 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
470 }
471
472 /// Sets the ordering constraint of this fence instruction. May only be
473 /// Acquire, Release, AcquireRelease, or SequentiallyConsistent.
474 void setOrdering(AtomicOrdering Ordering) {
475 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
476 ((unsigned)Ordering << 1));
477 }
478
479 /// Returns the synchronization scope ID of this fence instruction.
480 SyncScope::ID getSyncScopeID() const {
481 return SSID;
482 }
483
484 /// Sets the synchronization scope ID of this fence instruction.
485 void setSyncScopeID(SyncScope::ID SSID) {
486 this->SSID = SSID;
487 }
488
489 // Methods for support type inquiry through isa, cast, and dyn_cast:
490 static bool classof(const Instruction *I) {
491 return I->getOpcode() == Instruction::Fence;
492 }
493 static bool classof(const Value *V) {
494 return isa<Instruction>(V) && classof(cast<Instruction>(V));
495 }
496
497private:
498 // Shadow Instruction::setInstructionSubclassData with a private forwarding
499 // method so that subclasses cannot accidentally use it.
500 void setInstructionSubclassData(unsigned short D) {
501 Instruction::setInstructionSubclassData(D);
502 }
503
504 /// The synchronization scope ID of this fence instruction. Not quite enough
505 /// room in SubClassData for everything, so synchronization scope ID gets its
506 /// own field.
507 SyncScope::ID SSID;
508};
509
510//===----------------------------------------------------------------------===//
511// AtomicCmpXchgInst Class
512//===----------------------------------------------------------------------===//
513
514/// an instruction that atomically checks whether a
515/// specified value is in a memory location, and, if it is, stores a new value
516/// there. Returns the value that was loaded.
517///
518class AtomicCmpXchgInst : public Instruction {
519 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
520 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
521 SyncScope::ID SSID);
522
523protected:
524 // Note: Instruction needs to be a friend here to call cloneImpl.
525 friend class Instruction;
526
527 AtomicCmpXchgInst *cloneImpl() const;
528
529public:
530 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
531 AtomicOrdering SuccessOrdering,
532 AtomicOrdering FailureOrdering,
533 SyncScope::ID SSID, Instruction *InsertBefore = nullptr);
534 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
535 AtomicOrdering SuccessOrdering,
536 AtomicOrdering FailureOrdering,
537 SyncScope::ID SSID, BasicBlock *InsertAtEnd);
538
539 // allocate space for exactly three operands
540 void *operator new(size_t s) {
541 return User::operator new(s, 3);
542 }
543
544 /// Return true if this is a cmpxchg from a volatile memory
545 /// location.
546 ///
547 bool isVolatile() const {
548 return getSubclassDataFromInstruction() & 1;
549 }
550
551 /// Specify whether this is a volatile cmpxchg.
552 ///
553 void setVolatile(bool V) {
554 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
555 (unsigned)V);
556 }
557
558 /// Return true if this cmpxchg may spuriously fail.
559 bool isWeak() const {
560 return getSubclassDataFromInstruction() & 0x100;
561 }
562
563 void setWeak(bool IsWeak) {
564 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
565 (IsWeak << 8));
566 }
567
568 /// Transparently provide more efficient getOperand methods.
569 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
570
571 /// Returns the success ordering constraint of this cmpxchg instruction.
572 AtomicOrdering getSuccessOrdering() const {
573 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
574 }
575
576 /// Sets the success ordering constraint of this cmpxchg instruction.
577 void setSuccessOrdering(AtomicOrdering Ordering) {
578 assert(Ordering != AtomicOrdering::NotAtomic &&
579 "CmpXchg instructions can only be atomic.");
580 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
581 ((unsigned)Ordering << 2));
582 }
583
584 /// Returns the failure ordering constraint of this cmpxchg instruction.
585 AtomicOrdering getFailureOrdering() const {
586 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
587 }
588
589 /// Sets the failure ordering constraint of this cmpxchg instruction.
590 void setFailureOrdering(AtomicOrdering Ordering) {
591 assert(Ordering != AtomicOrdering::NotAtomic &&
592 "CmpXchg instructions can only be atomic.");
593 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
594 ((unsigned)Ordering << 5));
595 }
596
597 /// Returns the synchronization scope ID of this cmpxchg instruction.
598 SyncScope::ID getSyncScopeID() const {
599 return SSID;
600 }
601
602 /// Sets the synchronization scope ID of this cmpxchg instruction.
603 void setSyncScopeID(SyncScope::ID SSID) {
604 this->SSID = SSID;
605 }
606
607 Value *getPointerOperand() { return getOperand(0); }
608 const Value *getPointerOperand() const { return getOperand(0); }
609 static unsigned getPointerOperandIndex() { return 0U; }
610
611 Value *getCompareOperand() { return getOperand(1); }
612 const Value *getCompareOperand() const { return getOperand(1); }
613
614 Value *getNewValOperand() { return getOperand(2); }
615 const Value *getNewValOperand() const { return getOperand(2); }
616
617 /// Returns the address space of the pointer operand.
618 unsigned getPointerAddressSpace() const {
619 return getPointerOperand()->getType()->getPointerAddressSpace();
620 }
621
622 /// Returns the strongest permitted ordering on failure, given the
623 /// desired ordering on success.
624 ///
625 /// If the comparison in a cmpxchg operation fails, there is no atomic store
626 /// so release semantics cannot be provided. So this function drops explicit
627 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
628 /// operation would remain SequentiallyConsistent.
629 static AtomicOrdering
630 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
631 switch (SuccessOrdering) {
632 default:
633 llvm_unreachable("invalid cmpxchg success ordering");
634 case AtomicOrdering::Release:
635 case AtomicOrdering::Monotonic:
636 return AtomicOrdering::Monotonic;
637 case AtomicOrdering::AcquireRelease:
638 case AtomicOrdering::Acquire:
639 return AtomicOrdering::Acquire;
640 case AtomicOrdering::SequentiallyConsistent:
641 return AtomicOrdering::SequentiallyConsistent;
642 }
643 }
644
645 // Methods for support type inquiry through isa, cast, and dyn_cast:
646 static bool classof(const Instruction *I) {
647 return I->getOpcode() == Instruction::AtomicCmpXchg;
648 }
649 static bool classof(const Value *V) {
650 return isa<Instruction>(V) && classof(cast<Instruction>(V));
651 }
652
653private:
654 // Shadow Instruction::setInstructionSubclassData with a private forwarding
655 // method so that subclasses cannot accidentally use it.
656 void setInstructionSubclassData(unsigned short D) {
657 Instruction::setInstructionSubclassData(D);
658 }
659
660 /// The synchronization scope ID of this cmpxchg instruction. Not quite
661 /// enough room in SubClassData for everything, so synchronization scope ID
662 /// gets its own field.
663 SyncScope::ID SSID;
664};
665
666template <>
667struct OperandTraits<AtomicCmpXchgInst> :
668 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
669};
670
671DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
672
673//===----------------------------------------------------------------------===//
674// AtomicRMWInst Class
675//===----------------------------------------------------------------------===//
676
677/// an instruction that atomically reads a memory location,
678/// combines it with another value, and then stores the result back. Returns
679/// the old value.
680///
681class AtomicRMWInst : public Instruction {
682protected:
683 // Note: Instruction needs to be a friend here to call cloneImpl.
684 friend class Instruction;
685
686 AtomicRMWInst *cloneImpl() const;
687
688public:
689 /// This enumeration lists the possible modifications atomicrmw can make. In
690 /// the descriptions, 'p' is the pointer to the instruction's memory location,
691 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
692 /// instruction. These instructions always return 'old'.
693 enum BinOp {
694 /// *p = v
695 Xchg,
696 /// *p = old + v
697 Add,
698 /// *p = old - v
699 Sub,
700 /// *p = old & v
701 And,
702 /// *p = ~(old & v)
703 Nand,
704 /// *p = old | v
705 Or,
706 /// *p = old ^ v
707 Xor,
708 /// *p = old >signed v ? old : v
709 Max,
710 /// *p = old <signed v ? old : v
711 Min,
712 /// *p = old >unsigned v ? old : v
713 UMax,
714 /// *p = old <unsigned v ? old : v
715 UMin,
716
717 FIRST_BINOP = Xchg,
718 LAST_BINOP = UMin,
719 BAD_BINOP
720 };
721
722 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
723 AtomicOrdering Ordering, SyncScope::ID SSID,
724 Instruction *InsertBefore = nullptr);
725 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
726 AtomicOrdering Ordering, SyncScope::ID SSID,
727 BasicBlock *InsertAtEnd);
728
729 // allocate space for exactly two operands
730 void *operator new(size_t s) {
731 return User::operator new(s, 2);
732 }
733
734 BinOp getOperation() const {
735 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
736 }
737
738 void setOperation(BinOp Operation) {
739 unsigned short SubclassData = getSubclassDataFromInstruction();
740 setInstructionSubclassData((SubclassData & 31) |
741 (Operation << 5));
742 }
743
744 /// Return true if this is a RMW on a volatile memory location.
745 ///
746 bool isVolatile() const {
747 return getSubclassDataFromInstruction() & 1;
748 }
749
750 /// Specify whether this is a volatile RMW or not.
751 ///
752 void setVolatile(bool V) {
753 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
754 (unsigned)V);
755 }
756
757 /// Transparently provide more efficient getOperand methods.
758 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
759
760 /// Returns the ordering constraint of this rmw instruction.
761 AtomicOrdering getOrdering() const {
762 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
763 }
764
765 /// Sets the ordering constraint of this rmw instruction.
766 void setOrdering(AtomicOrdering Ordering) {
767 assert(Ordering != AtomicOrdering::NotAtomic &&
768 "atomicrmw instructions can only be atomic.");
769 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
770 ((unsigned)Ordering << 2));
771 }
772
773 /// Returns the synchronization scope ID of this rmw instruction.
774 SyncScope::ID getSyncScopeID() const {
775 return SSID;
776 }
777
778 /// Sets the synchronization scope ID of this rmw instruction.
779 void setSyncScopeID(SyncScope::ID SSID) {
780 this->SSID = SSID;
781 }
782
783 Value *getPointerOperand() { return getOperand(0); }
784 const Value *getPointerOperand() const { return getOperand(0); }
785 static unsigned getPointerOperandIndex() { return 0U; }
786
787 Value *getValOperand() { return getOperand(1); }
788 const Value *getValOperand() const { return getOperand(1); }
789
790 /// Returns the address space of the pointer operand.
791 unsigned getPointerAddressSpace() const {
792 return getPointerOperand()->getType()->getPointerAddressSpace();
793 }
794
795 // Methods for support type inquiry through isa, cast, and dyn_cast:
796 static bool classof(const Instruction *I) {
797 return I->getOpcode() == Instruction::AtomicRMW;
798 }
799 static bool classof(const Value *V) {
800 return isa<Instruction>(V) && classof(cast<Instruction>(V));
801 }
802
803private:
804 void Init(BinOp Operation, Value *Ptr, Value *Val,
805 AtomicOrdering Ordering, SyncScope::ID SSID);
806
807 // Shadow Instruction::setInstructionSubclassData with a private forwarding
808 // method so that subclasses cannot accidentally use it.
809 void setInstructionSubclassData(unsigned short D) {
810 Instruction::setInstructionSubclassData(D);
811 }
812
813 /// The synchronization scope ID of this rmw instruction. Not quite enough
814 /// room in SubClassData for everything, so synchronization scope ID gets its
815 /// own field.
816 SyncScope::ID SSID;
817};
818
819template <>
820struct OperandTraits<AtomicRMWInst>
821 : public FixedNumOperandTraits<AtomicRMWInst,2> {
822};
823
824DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
825
826//===----------------------------------------------------------------------===//
827// GetElementPtrInst Class
828//===----------------------------------------------------------------------===//
829
830// checkGEPType - Simple wrapper function to give a better assertion failure
831// message on bad indexes for a gep instruction.
832//
833inline Type *checkGEPType(Type *Ty) {
834 assert(Ty && "Invalid GetElementPtrInst indices for type!");
835 return Ty;
836}
837
838/// an instruction for type-safe pointer arithmetic to
839/// access elements of arrays and structs
840///
841class GetElementPtrInst : public Instruction {
842 Type *SourceElementType;
843 Type *ResultElementType;
844
845 GetElementPtrInst(const GetElementPtrInst &GEPI);
846
847 /// Constructors - Create a getelementptr instruction with a base pointer an
848 /// list of indices. The first ctor can optionally insert before an existing
849 /// instruction, the second appends the new instruction to the specified
850 /// BasicBlock.
851 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
852 ArrayRef<Value *> IdxList, unsigned Values,
853 const Twine &NameStr, Instruction *InsertBefore);
854 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
855 ArrayRef<Value *> IdxList, unsigned Values,
856 const Twine &NameStr, BasicBlock *InsertAtEnd);
857
858 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
859
860protected:
861 // Note: Instruction needs to be a friend here to call cloneImpl.
862 friend class Instruction;
863
864 GetElementPtrInst *cloneImpl() const;
865
866public:
867 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
868 ArrayRef<Value *> IdxList,
869 const Twine &NameStr = "",
870 Instruction *InsertBefore = nullptr) {
871 unsigned Values = 1 + unsigned(IdxList.size());
872 if (!PointeeType)
873 PointeeType =
874 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
875 else
876 assert(
877 PointeeType ==
878 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
879 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
880 NameStr, InsertBefore);
881 }
882
883 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
884 ArrayRef<Value *> IdxList,
885 const Twine &NameStr,
886 BasicBlock *InsertAtEnd) {
887 unsigned Values = 1 + unsigned(IdxList.size());
888 if (!PointeeType)
889 PointeeType =
890 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
891 else
892 assert(
893 PointeeType ==
894 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
895 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
896 NameStr, InsertAtEnd);
897 }
898
899 /// Create an "inbounds" getelementptr. See the documentation for the
900 /// "inbounds" flag in LangRef.html for details.
901 static GetElementPtrInst *CreateInBounds(Value *Ptr,
902 ArrayRef<Value *> IdxList,
903 const Twine &NameStr = "",
904 Instruction *InsertBefore = nullptr){
905 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
906 }
907
908 static GetElementPtrInst *
909 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
910 const Twine &NameStr = "",
911 Instruction *InsertBefore = nullptr) {
912 GetElementPtrInst *GEP =
913 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
914 GEP->setIsInBounds(true);
915 return GEP;
916 }
917
918 static GetElementPtrInst *CreateInBounds(Value *Ptr,
919 ArrayRef<Value *> IdxList,
920 const Twine &NameStr,
921 BasicBlock *InsertAtEnd) {
922 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
923 }
924
925 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
926 ArrayRef<Value *> IdxList,
927 const Twine &NameStr,
928 BasicBlock *InsertAtEnd) {
929 GetElementPtrInst *GEP =
930 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
931 GEP->setIsInBounds(true);
932 return GEP;
933 }
934
935 /// Transparently provide more efficient getOperand methods.
936 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
937
938 Type *getSourceElementType() const { return SourceElementType; }
939
940 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
941 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
942
943 Type *getResultElementType() const {
944 assert(ResultElementType ==
945 cast<PointerType>(getType()->getScalarType())->getElementType());
946 return ResultElementType;
947 }
948
949 /// Returns the address space of this instruction's pointer type.
950 unsigned getAddressSpace() const {
951 // Note that this is always the same as the pointer operand's address space
952 // and that is cheaper to compute, so cheat here.
953 return getPointerAddressSpace();
954 }
955
956 /// Returns the type of the element that would be loaded with
957 /// a load instruction with the specified parameters.
958 ///
959 /// Null is returned if the indices are invalid for the specified
960 /// pointer type.
961 ///
962 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
963 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
964 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
965
966 inline op_iterator idx_begin() { return op_begin()+1; }
967 inline const_op_iterator idx_begin() const { return op_begin()+1; }
968 inline op_iterator idx_end() { return op_end(); }
969 inline const_op_iterator idx_end() const { return op_end(); }
970
971 inline iterator_range<op_iterator> indices() {
972 return make_range(idx_begin(), idx_end());
973 }
974
975 inline iterator_range<const_op_iterator> indices() const {
976 return make_range(idx_begin(), idx_end());
977 }
978
979 Value *getPointerOperand() {
980 return getOperand(0);
981 }
982 const Value *getPointerOperand() const {
983 return getOperand(0);
984 }
985 static unsigned getPointerOperandIndex() {
986 return 0U; // get index for modifying correct operand.
987 }
988
989 /// Method to return the pointer operand as a
990 /// PointerType.
991 Type *getPointerOperandType() const {
992 return getPointerOperand()->getType();
993 }
994
995 /// Returns the address space of the pointer operand.
996 unsigned getPointerAddressSpace() const {
997 return getPointerOperandType()->getPointerAddressSpace();
998 }
999
1000 /// Returns the pointer type returned by the GEP
1001 /// instruction, which may be a vector of pointers.
1002 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
1003 return getGEPReturnType(
1004 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(),
1005 Ptr, IdxList);
1006 }
1007 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
1008 ArrayRef<Value *> IdxList) {
1009 Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
1010 Ptr->getType()->getPointerAddressSpace());
1011 // Vector GEP
1012 if (Ptr->getType()->isVectorTy()) {
1013 unsigned NumElem = Ptr->getType()->getVectorNumElements();
1014 return VectorType::get(PtrTy, NumElem);
1015 }
1016 for (Value *Index : IdxList)
1017 if (Index->getType()->isVectorTy()) {
1018 unsigned NumElem = Index->getType()->getVectorNumElements();
1019 return VectorType::get(PtrTy, NumElem);
1020 }
1021 // Scalar GEP
1022 return PtrTy;
1023 }
1024
1025 unsigned getNumIndices() const { // Note: always non-negative
1026 return getNumOperands() - 1;
1027 }
1028
1029 bool hasIndices() const {
1030 return getNumOperands() > 1;
1031 }
1032
1033 /// Return true if all of the indices of this GEP are
1034 /// zeros. If so, the result pointer and the first operand have the same
1035 /// value, just potentially different types.
1036 bool hasAllZeroIndices() const;
1037
1038 /// Return true if all of the indices of this GEP are
1039 /// constant integers. If so, the result pointer and the first operand have
1040 /// a constant offset between them.
1041 bool hasAllConstantIndices() const;
1042
1043 /// Set or clear the inbounds flag on this GEP instruction.
1044 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1045 void setIsInBounds(bool b = true);
1046
1047 /// Determine whether the GEP has the inbounds flag.
1048 bool isInBounds() const;
1049
1050 /// Accumulate the constant address offset of this GEP if possible.
1051 ///
1052 /// This routine accepts an APInt into which it will accumulate the constant
1053 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1054 /// all-constant, it returns false and the value of the offset APInt is
1055 /// undefined (it is *not* preserved!). The APInt passed into this routine
1056 /// must be at least as wide as the IntPtr type for the address space of
1057 /// the base GEP pointer.
1058 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1059
1060 // Methods for support type inquiry through isa, cast, and dyn_cast:
1061 static bool classof(const Instruction *I) {
1062 return (I->getOpcode() == Instruction::GetElementPtr);
1063 }
1064 static bool classof(const Value *V) {
1065 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1066 }
1067};
1068
1069template <>
1070struct OperandTraits<GetElementPtrInst> :
1071 public VariadicOperandTraits<GetElementPtrInst, 1> {
1072};
1073
1074GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1075 ArrayRef<Value *> IdxList, unsigned Values,
1076 const Twine &NameStr,
1077 Instruction *InsertBefore)
1078 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1079 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1080 Values, InsertBefore),
1081 SourceElementType(PointeeType),
1082 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1083 assert(ResultElementType ==
1084 cast<PointerType>(getType()->getScalarType())->getElementType());
1085 init(Ptr, IdxList, NameStr);
1086}
1087
1088GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1089 ArrayRef<Value *> IdxList, unsigned Values,
1090 const Twine &NameStr,
1091 BasicBlock *InsertAtEnd)
1092 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1093 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1094 Values, InsertAtEnd),
1095 SourceElementType(PointeeType),
1096 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1097 assert(ResultElementType ==
1098 cast<PointerType>(getType()->getScalarType())->getElementType());
1099 init(Ptr, IdxList, NameStr);
1100}
1101
1102DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1103
1104//===----------------------------------------------------------------------===//
1105// ICmpInst Class
1106//===----------------------------------------------------------------------===//
1107
1108/// This instruction compares its operands according to the predicate given
1109/// to the constructor. It only operates on integers or pointers. The operands
1110/// must be identical types.
1111/// Represent an integer comparison operator.
1112class ICmpInst: public CmpInst {
1113 void AssertOK() {
1114 assert(isIntPredicate() &&
1115 "Invalid ICmp predicate value");
1116 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1117 "Both operands to ICmp instruction are not of the same type!");
1118 // Check that the operands are the right type
1119 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1120 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1121 "Invalid operand types for ICmp instruction");
1122 }
1123
1124protected:
1125 // Note: Instruction needs to be a friend here to call cloneImpl.
1126 friend class Instruction;
1127
1128 /// Clone an identical ICmpInst
1129 ICmpInst *cloneImpl() const;
1130
1131public:
1132 /// Constructor with insert-before-instruction semantics.
1133 ICmpInst(
1134 Instruction *InsertBefore, ///< Where to insert
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::ICmp, pred, LHS, RHS, NameStr,
1141 InsertBefore) {
1142#ifndef NDEBUG
1143 AssertOK();
1144#endif
1145 }
1146
1147 /// Constructor with insert-at-end semantics.
1148 ICmpInst(
1149 BasicBlock &InsertAtEnd, ///< Block to insert into.
1150 Predicate pred, ///< The predicate to use for the comparison
1151 Value *LHS, ///< The left-hand-side of the expression
1152 Value *RHS, ///< The right-hand-side of the expression
1153 const Twine &NameStr = "" ///< Name of the instruction
1154 ) : CmpInst(makeCmpResultType(LHS->getType()),
1155 Instruction::ICmp, pred, LHS, RHS, NameStr,
1156 &InsertAtEnd) {
1157#ifndef NDEBUG
1158 AssertOK();
1159#endif
1160 }
1161
1162 /// Constructor with no-insertion semantics
1163 ICmpInst(
1164 Predicate pred, ///< The predicate to use for the comparison
1165 Value *LHS, ///< The left-hand-side of the expression
1166 Value *RHS, ///< The right-hand-side of the expression
1167 const Twine &NameStr = "" ///< Name of the instruction
1168 ) : CmpInst(makeCmpResultType(LHS->getType()),
1169 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1170#ifndef NDEBUG
1171 AssertOK();
1172#endif
1173 }
1174
1175 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1176 /// @returns the predicate that would be the result if the operand were
1177 /// regarded as signed.
1178 /// Return the signed version of the predicate
1179 Predicate getSignedPredicate() const {
1180 return getSignedPredicate(getPredicate());
1181 }
1182
1183 /// This is a static version that you can use without an instruction.
1184 /// Return the signed version of the predicate.
1185 static Predicate getSignedPredicate(Predicate pred);
1186
1187 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1188 /// @returns the predicate that would be the result if the operand were
1189 /// regarded as unsigned.
1190 /// Return the unsigned version of the predicate
1191 Predicate getUnsignedPredicate() const {
1192 return getUnsignedPredicate(getPredicate());
1193 }
1194
1195 /// This is a static version that you can use without an instruction.
1196 /// Return the unsigned version of the predicate.
1197 static Predicate getUnsignedPredicate(Predicate pred);
1198
1199 /// Return true if this predicate is either EQ or NE. This also
1200 /// tests for commutativity.
1201 static bool isEquality(Predicate P) {
1202 return P == ICMP_EQ || P == ICMP_NE;
1203 }
1204
1205 /// Return true if this predicate is either EQ or NE. This also
1206 /// tests for commutativity.
1207 bool isEquality() const {
1208 return isEquality(getPredicate());
1209 }
1210
1211 /// @returns true if the predicate of this ICmpInst is commutative
1212 /// Determine if this relation is commutative.
1213 bool isCommutative() const { return isEquality(); }
1214
1215 /// Return true if the predicate is relational (not EQ or NE).
1216 ///
1217 bool isRelational() const {
1218 return !isEquality();
1219 }
1220
1221 /// Return true if the predicate is relational (not EQ or NE).
1222 ///
1223 static bool isRelational(Predicate P) {
1224 return !isEquality(P);
1225 }
1226
1227 /// Exchange the two operands to this instruction in such a way that it does
1228 /// not modify the semantics of the instruction. The predicate value may be
1229 /// changed to retain the same result if the predicate is order dependent
1230 /// (e.g. ult).
1231 /// Swap operands and adjust predicate.
1232 void swapOperands() {
1233 setPredicate(getSwappedPredicate());
1234 Op<0>().swap(Op<1>());
1235 }
1236
1237 // Methods for support type inquiry through isa, cast, and dyn_cast:
1238 static bool classof(const Instruction *I) {
1239 return I->getOpcode() == Instruction::ICmp;
1240 }
1241 static bool classof(const Value *V) {
1242 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1243 }
1244};
1245
1246//===----------------------------------------------------------------------===//
1247// FCmpInst Class
1248//===----------------------------------------------------------------------===//
1249
1250/// This instruction compares its operands according to the predicate given
1251/// to the constructor. It only operates on floating point values or packed
1252/// vectors of floating point values. The operands must be identical types.
1253/// Represents a floating point comparison operator.
1254class FCmpInst: public CmpInst {
1255 void AssertOK() {
1256 assert(isFPPredicate() && "Invalid FCmp predicate value");
1257 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1258 "Both operands to FCmp instruction are not of the same type!");
1259 // Check that the operands are the right type
1260 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1261 "Invalid operand types for FCmp instruction");
1262 }
1263
1264protected:
1265 // Note: Instruction needs to be a friend here to call cloneImpl.
1266 friend class Instruction;
1267
1268 /// Clone an identical FCmpInst
1269 FCmpInst *cloneImpl() const;
1270
1271public:
1272 /// Constructor with insert-before-instruction semantics.
1273 FCmpInst(
1274 Instruction *InsertBefore, ///< Where to insert
1275 Predicate pred, ///< The predicate to use for the comparison
1276 Value *LHS, ///< The left-hand-side of the expression
1277 Value *RHS, ///< The right-hand-side of the expression
1278 const Twine &NameStr = "" ///< Name of the instruction
1279 ) : CmpInst(makeCmpResultType(LHS->getType()),
1280 Instruction::FCmp, pred, LHS, RHS, NameStr,
1281 InsertBefore) {
1282 AssertOK();
1283 }
1284
1285 /// Constructor with insert-at-end semantics.
1286 FCmpInst(
1287 BasicBlock &InsertAtEnd, ///< Block to insert into.
1288 Predicate pred, ///< The predicate to use for the comparison
1289 Value *LHS, ///< The left-hand-side of the expression
1290 Value *RHS, ///< The right-hand-side of the expression
1291 const Twine &NameStr = "" ///< Name of the instruction
1292 ) : CmpInst(makeCmpResultType(LHS->getType()),
1293 Instruction::FCmp, pred, LHS, RHS, NameStr,
1294 &InsertAtEnd) {
1295 AssertOK();
1296 }
1297
1298 /// Constructor with no-insertion semantics
1299 FCmpInst(
1300 Predicate pred, ///< The predicate to use for the comparison
1301 Value *LHS, ///< The left-hand-side of the expression
1302 Value *RHS, ///< The right-hand-side of the expression
1303 const Twine &NameStr = "" ///< Name of the instruction
1304 ) : CmpInst(makeCmpResultType(LHS->getType()),
1305 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1306 AssertOK();
1307 }
1308
1309 /// @returns true if the predicate of this instruction is EQ or NE.
1310 /// Determine if this is an equality predicate.
1311 static bool isEquality(Predicate Pred) {
1312 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1313 Pred == FCMP_UNE;
1314 }
1315
1316 /// @returns true if the predicate of this instruction is EQ or NE.
1317 /// Determine if this is an equality predicate.
1318 bool isEquality() const { return isEquality(getPredicate()); }
1319
1320 /// @returns true if the predicate of this instruction is commutative.
1321 /// Determine if this is a commutative predicate.
1322 bool isCommutative() const {
1323 return isEquality() ||
1324 getPredicate() == FCMP_FALSE ||
1325 getPredicate() == FCMP_TRUE ||
1326 getPredicate() == FCMP_ORD ||
1327 getPredicate() == FCMP_UNO;
1328 }
1329
1330 /// @returns true if the predicate is relational (not EQ or NE).
1331 /// Determine if this a relational predicate.
1332 bool isRelational() const { return !isEquality(); }
1333
1334 /// Exchange the two operands to this instruction in such a way that it does
1335 /// not modify the semantics of the instruction. The predicate value may be
1336 /// changed to retain the same result if the predicate is order dependent
1337 /// (e.g. ult).
1338 /// Swap operands and adjust predicate.
1339 void swapOperands() {
1340 setPredicate(getSwappedPredicate());
1341 Op<0>().swap(Op<1>());
1342 }
1343
1344 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1345 static bool classof(const Instruction *I) {
1346 return I->getOpcode() == Instruction::FCmp;
1347 }
1348 static bool classof(const Value *V) {
1349 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1350 }
1351};
1352
1353class CallInst;
1354class InvokeInst;
1355
1356template <class T> struct CallBaseParent { using type = Instruction; };
1357
1358template <> struct CallBaseParent<InvokeInst> { using type = TerminatorInst; };
1359
1360//===----------------------------------------------------------------------===//
1361/// Base class for all callable instructions (InvokeInst and CallInst)
1362/// Holds everything related to calling a function, abstracting from the base
1363/// type @p BaseInstTy and the concrete instruction @p InstTy
1364///
1365template <class InstTy>
1366class CallBase : public CallBaseParent<InstTy>::type,
1367 public OperandBundleUser<InstTy, User::op_iterator> {
1368protected:
1369 AttributeList Attrs; ///< parameter attributes for callable
1370 FunctionType *FTy;
1371 using BaseInstTy = typename CallBaseParent<InstTy>::type;
1372
1373 template <class... ArgsTy>
1374 CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args)
1375 : BaseInstTy(std::forward<ArgsTy>(Args)...), Attrs(A), FTy(FT) {}
1376 bool hasDescriptor() const { return Value::HasDescriptor; }
1377
1378 using BaseInstTy::BaseInstTy;
1379
1380 using OperandBundleUser<InstTy,
1381 User::op_iterator>::isFnAttrDisallowedByOpBundle;
1382 using OperandBundleUser<InstTy, User::op_iterator>::getNumTotalBundleOperands;
1383 using OperandBundleUser<InstTy, User::op_iterator>::bundleOperandHasAttr;
1384 using Instruction::getSubclassDataFromInstruction;
1385 using Instruction::setInstructionSubclassData;
1386
1387public:
1388 using Instruction::getContext;
1389 using OperandBundleUser<InstTy, User::op_iterator>::hasOperandBundles;
1390 using OperandBundleUser<InstTy,
1391 User::op_iterator>::getBundleOperandsStartIndex;
1392
1393 static bool classof(const Instruction *I) {
1394 llvm_unreachable(
1395 "CallBase is not meant to be used as part of the classof hierarchy");
1396 }
1397
1398public:
1399 /// Return the parameter attributes for this call.
1400 ///
1401 AttributeList getAttributes() const { return Attrs; }
1402
1403 /// Set the parameter attributes for this call.
1404 ///
1405 void setAttributes(AttributeList A) { Attrs = A; }
1406
1407 FunctionType *getFunctionType() const { return FTy; }
1408
1409 void mutateFunctionType(FunctionType *FTy) {
1410 Value::mutateType(FTy->getReturnType());
1411 this->FTy = FTy;
1412 }
1413
1414 /// Return the number of call arguments.
1415 ///
1416 unsigned getNumArgOperands() const {
1417 return getNumOperands() - getNumTotalBundleOperands() - InstTy::ArgOffset;
1418 }
1419
1420 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1421 ///
1422 Value *getArgOperand(unsigned i) const {
1423 assert(i < getNumArgOperands() && "Out of bounds!");
1424 return getOperand(i);
1425 }
1426 void setArgOperand(unsigned i, Value *v) {
1427 assert(i < getNumArgOperands() && "Out of bounds!");
1428 setOperand(i, v);
1429 }
1430
1431 /// Return the iterator pointing to the beginning of the argument list.
1432 User::op_iterator arg_begin() { return op_begin(); }
1433
1434 /// Return the iterator pointing to the end of the argument list.
1435 User::op_iterator arg_end() {
1436 // [ call args ], [ operand bundles ], callee
1437 return op_end() - getNumTotalBundleOperands() - InstTy::ArgOffset;
1438 }
1439
1440 /// Iteration adapter for range-for loops.
1441 iterator_range<User::op_iterator> arg_operands() {
1442 return make_range(arg_begin(), arg_end());
1443 }
1444
1445 /// Return the iterator pointing to the beginning of the argument list.
1446 User::const_op_iterator arg_begin() const { return op_begin(); }
1447
1448 /// Return the iterator pointing to the end of the argument list.
1449 User::const_op_iterator arg_end() const {
1450 // [ call args ], [ operand bundles ], callee
1451 return op_end() - getNumTotalBundleOperands() - InstTy::ArgOffset;
1452 }
1453
1454 /// Iteration adapter for range-for loops.
1455 iterator_range<User::const_op_iterator> arg_operands() const {
1456 return make_range(arg_begin(), arg_end());
1457 }
1458
1459 /// Wrappers for getting the \c Use of a call argument.
1460 const Use &getArgOperandUse(unsigned i) const {
1461 assert(i < getNumArgOperands() && "Out of bounds!");
1462 return User::getOperandUse(i);
1463 }
1464 Use &getArgOperandUse(unsigned i) {
1465 assert(i < getNumArgOperands() && "Out of bounds!");
1466 return User::getOperandUse(i);
1467 }
1468
1469 /// If one of the arguments has the 'returned' attribute, return its
1470 /// operand value. Otherwise, return nullptr.
1471 Value *getReturnedArgOperand() const {
1472 unsigned Index;
1473
1474 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
1475 return getArgOperand(Index - AttributeList::FirstArgIndex);
1476 if (const Function *F = getCalledFunction())
1477 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
1478 Index)
1479 return getArgOperand(Index - AttributeList::FirstArgIndex);
1480
1481 return nullptr;
1482 }
1483
1484 User::op_iterator op_begin() {
1485 return OperandTraits<CallBase>::op_begin(this);
1486 }
1487
1488 User::const_op_iterator op_begin() const {
1489 return OperandTraits<CallBase>::op_begin(const_cast<CallBase *>(this));
1490 }
1491
1492 User::op_iterator op_end() { return OperandTraits<CallBase>::op_end(this); }
1493
1494 User::const_op_iterator op_end() const {
1495 return OperandTraits<CallBase>::op_end(const_cast<CallBase *>(this));
1496 }
1497
1498 Value *getOperand(unsigned i_nocapture) const {
1499 assert(i_nocapture < OperandTraits<CallBase>::operands(this) &&
1500 "getOperand() out of range!");
1501 return cast_or_null<Value>(OperandTraits<CallBase>::op_begin(
1502 const_cast<CallBase *>(this))[i_nocapture]
1503 .get());
1504 }
1505
1506 void setOperand(unsigned i_nocapture, Value *Val_nocapture) {
1507 assert(i_nocapture < OperandTraits<CallBase>::operands(this) &&
1508 "setOperand() out of range!");
1509 OperandTraits<CallBase>::op_begin(this)[i_nocapture] = Val_nocapture;
1510 }
1511
1512 unsigned getNumOperands() const {
1513 return OperandTraits<CallBase>::operands(this);
1514 }
1515 template <int Idx_nocapture> Use &Op() {
1516 return User::OpFrom<Idx_nocapture>(this);
1517 }
1518 template <int Idx_nocapture> const Use &Op() const {
1519 return User::OpFrom<Idx_nocapture>(this);
1520 }
1521
1522 /// Return the function called, or null if this is an
1523 /// indirect function invocation.
1524 ///
1525 Function *getCalledFunction() const {
1526 return dyn_cast<Function>(Op<-InstTy::ArgOffset>());
1527 }
1528
1529 /// Determine whether this call has the given attribute.
1530 bool hasFnAttr(Attribute::AttrKind Kind) const {
1531 assert(Kind != Attribute::NoBuiltin &&
1532 "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin");
1533 return hasFnAttrImpl(Kind);
1534 }
1535
1536 /// Determine whether this call has the given attribute.
1537 bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); }
1538
1539 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1540 /// function call.
1541 CallingConv::ID getCallingConv() const {
1542 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1543 }
1544 void setCallingConv(CallingConv::ID CC) {
1545 auto ID = static_cast<unsigned>(CC);
1546 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
1547 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1548 (ID << 2));
1549 }
1550
1551
1552 /// adds the attribute to the list of attributes.
1553 void addAttribute(unsigned i, Attribute::AttrKind Kind) {
1554 AttributeList PAL = getAttributes();
1555 PAL = PAL.addAttribute(getContext(), i, Kind);
1556 setAttributes(PAL);
1557 }
1558
1559 /// adds the attribute to the list of attributes.
1560 void addAttribute(unsigned i, Attribute Attr) {
1561 AttributeList PAL = getAttributes();
1562 PAL = PAL.addAttribute(getContext(), i, Attr);
1563 setAttributes(PAL);
1564 }
1565
1566 /// Adds the attribute to the indicated argument
1567 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1568 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1569 AttributeList PAL = getAttributes();
1570 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
1571 setAttributes(PAL);
1572 }
1573
1574 /// Adds the attribute to the indicated argument
1575 void addParamAttr(unsigned ArgNo, Attribute Attr) {
1576 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1577 AttributeList PAL = getAttributes();
1578 PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
1579 setAttributes(PAL);
1580 }
1581
1582 /// removes the attribute from the list of attributes.
1583 void removeAttribute(unsigned i, Attribute::AttrKind Kind) {
1584 AttributeList PAL = getAttributes();
1585 PAL = PAL.removeAttribute(getContext(), i, Kind);
1586 setAttributes(PAL);
1587 }
1588
1589 /// removes the attribute from the list of attributes.
1590 void removeAttribute(unsigned i, StringRef Kind) {
1591 AttributeList PAL = getAttributes();
1592 PAL = PAL.removeAttribute(getContext(), i, Kind);
1593 setAttributes(PAL);
1594 }
1595
1596 /// Removes the attribute from the given argument
1597 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1598 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1599 AttributeList PAL = getAttributes();
1600 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1601 setAttributes(PAL);
1602 }
1603
1604 /// Removes the attribute from the given argument
1605 void removeParamAttr(unsigned ArgNo, StringRef Kind) {
1606 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1607 AttributeList PAL = getAttributes();
1608 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1609 setAttributes(PAL);
1610 }
1611
1612 /// adds the dereferenceable attribute to the list of attributes.
1613 void addDereferenceableAttr(unsigned i, uint64_t Bytes) {
1614 AttributeList PAL = getAttributes();
1615 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
1616 setAttributes(PAL);
1617 }
1618
1619 /// adds the dereferenceable_or_null attribute to the list of
1620 /// attributes.
1621 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
1622 AttributeList PAL = getAttributes();
1623 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
1624 setAttributes(PAL);
1625 }
1626
1627 /// Determine whether the return value has the given attribute.
1628 bool hasRetAttr(Attribute::AttrKind Kind) const {
1629 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
1630 return true;
1631
1632 // Look at the callee, if available.
1633 if (const Function *F = getCalledFunction())
1634 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
1635 return false;
1636 }
1637
1638 /// Determine whether the argument or parameter has the given attribute.
1639 bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
1640 assert(ArgNo < getNumArgOperands() && "Param index out of bounds!");
1641
1642 if (Attrs.hasParamAttribute(ArgNo, Kind))
1643 return true;
1644 if (const Function *F = getCalledFunction())
1645 return F->getAttributes().hasParamAttribute(ArgNo, Kind);
1646 return false;
1647 }
1648
1649 /// Get the attribute of a given kind at a position.
1650 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
1651 return getAttributes().getAttribute(i, Kind);
1652 }
1653
1654 /// Get the attribute of a given kind at a position.
1655 Attribute getAttribute(unsigned i, StringRef Kind) const {
1656 return getAttributes().getAttribute(i, Kind);
1657 }
1658
1659 /// Get the attribute of a given kind from a given arg
1660 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
1661 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1662 return getAttributes().getParamAttr(ArgNo, Kind);
1663 }
1664
1665 /// Get the attribute of a given kind from a given arg
1666 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
1667 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1668 return getAttributes().getParamAttr(ArgNo, Kind);
1669 }
1670 /// Return true if the data operand at index \p i has the attribute \p
1671 /// A.
1672 ///
1673 /// Data operands include call arguments and values used in operand bundles,
1674 /// but does not include the callee operand. This routine dispatches to the
1675 /// underlying AttributeList or the OperandBundleUser as appropriate.
1676 ///
1677 /// The index \p i is interpreted as
1678 ///
1679 /// \p i == Attribute::ReturnIndex -> the return value
1680 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
1681 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
1682 /// (\p i - 1) in the operand list.
1683 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
1684 // There are getNumOperands() - (InstTy::ArgOffset - 1) data operands.
1685 // The last operand is the callee.
1686 assert(i < (getNumOperands() - InstTy::ArgOffset + 1) &&
1687 "Data operand index out of bounds!");
1688
1689 // The attribute A can either be directly specified, if the operand in
1690 // question is a call argument; or be indirectly implied by the kind of its
1691 // containing operand bundle, if the operand is a bundle operand.
1692
1693 if (i == AttributeList::ReturnIndex)
1694 return hasRetAttr(Kind);
1695
1696 // FIXME: Avoid these i - 1 calculations and update the API to use
1697 // zero-based indices.
1698 if (i < (getNumArgOperands() + 1))
1699 return paramHasAttr(i - 1, Kind);
1700
1701 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
1702 "Must be either a call argument or an operand bundle!");
1703 return bundleOperandHasAttr(i - 1, Kind);
1704 }
1705
1706 /// Extract the alignment of the return value.
1707 unsigned getRetAlignment() const { return Attrs.getRetAlignment(); }
1708
1709 /// Extract the alignment for a call or parameter (0=unknown).
1710 unsigned getParamAlignment(unsigned ArgNo) const {
1711 return Attrs.getParamAlignment(ArgNo);
1712 }
1713
1714 /// Extract the number of dereferenceable bytes for a call or
1715 /// parameter (0=unknown).
1716 uint64_t getDereferenceableBytes(unsigned i) const {
1717 return Attrs.getDereferenceableBytes(i);
1718 }
1719
1720 /// Extract the number of dereferenceable_or_null bytes for a call or
1721 /// parameter (0=unknown).
1722 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1723 return Attrs.getDereferenceableOrNullBytes(i);
1724 }
1725
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001726 /// Determine if the return value is marked with NoAlias attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001727 bool returnDoesNotAlias() const {
1728 return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
1729 }
1730
1731 /// Return true if the call should not be treated as a call to a
1732 /// builtin.
1733 bool isNoBuiltin() const {
1734 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1735 !hasFnAttrImpl(Attribute::Builtin);
1736 }
1737
1738 /// Determine if the call requires strict floating point semantics.
1739 bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); }
1740
1741 /// Return true if the call should not be inlined.
1742 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1743 void setIsNoInline() {
1744 addAttribute(AttributeList::FunctionIndex, Attribute::NoInline);
1745 }
1746 /// Determine if the call does not access memory.
1747 bool doesNotAccessMemory() const {
1748 return hasFnAttr(Attribute::ReadNone);
1749 }
1750 void setDoesNotAccessMemory() {
1751 addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
1752 }
1753
1754 /// Determine if the call does not access or only reads memory.
1755 bool onlyReadsMemory() const {
1756 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1757 }
1758 void setOnlyReadsMemory() {
1759 addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly);
1760 }
1761
1762 /// Determine if the call does not access or only writes memory.
1763 bool doesNotReadMemory() const {
1764 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
1765 }
1766 void setDoesNotReadMemory() {
1767 addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly);
1768 }
1769
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001770 /// Determine if the call can access memmory only using pointers based
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001771 /// on its arguments.
1772 bool onlyAccessesArgMemory() const {
1773 return hasFnAttr(Attribute::ArgMemOnly);
1774 }
1775 void setOnlyAccessesArgMemory() {
1776 addAttribute(AttributeList::FunctionIndex, Attribute::ArgMemOnly);
1777 }
1778
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001779 /// Determine if the function may only access memory that is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001780 /// inaccessible from the IR.
1781 bool onlyAccessesInaccessibleMemory() const {
1782 return hasFnAttr(Attribute::InaccessibleMemOnly);
1783 }
1784 void setOnlyAccessesInaccessibleMemory() {
1785 addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOnly);
1786 }
1787
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001788 /// Determine if the function may only access memory that is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001789 /// either inaccessible from the IR or pointed to by its arguments.
1790 bool onlyAccessesInaccessibleMemOrArgMem() const {
1791 return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
1792 }
1793 void setOnlyAccessesInaccessibleMemOrArgMem() {
1794 addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOrArgMemOnly);
1795 }
1796 /// Determine if the call cannot return.
1797 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1798 void setDoesNotReturn() {
1799 addAttribute(AttributeList::FunctionIndex, Attribute::NoReturn);
1800 }
1801
1802 /// Determine if the call should not perform indirect branch tracking.
1803 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
1804
1805 /// Determine if the call cannot unwind.
1806 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1807 void setDoesNotThrow() {
1808 addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
1809 }
1810
1811 /// Determine if the invoke cannot be duplicated.
1812 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1813 void setCannotDuplicate() {
1814 addAttribute(AttributeList::FunctionIndex, Attribute::NoDuplicate);
1815 }
1816
1817 /// Determine if the invoke is convergent
1818 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
1819 void setConvergent() {
1820 addAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1821 }
1822 void setNotConvergent() {
1823 removeAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1824 }
1825
1826 /// Determine if the call returns a structure through first
1827 /// pointer argument.
1828 bool hasStructRetAttr() const {
1829 if (getNumArgOperands() == 0)
1830 return false;
1831
1832 // Be friendly and also check the callee.
1833 return paramHasAttr(0, Attribute::StructRet);
1834 }
1835
1836 /// Determine if any call argument is an aggregate passed by value.
1837 bool hasByValArgument() const {
1838 return Attrs.hasAttrSomewhere(Attribute::ByVal);
1839 }
1840 /// Get a pointer to the function that is invoked by this
1841 /// instruction.
1842 const Value *getCalledValue() const { return Op<-InstTy::ArgOffset>(); }
1843 Value *getCalledValue() { return Op<-InstTy::ArgOffset>(); }
1844
1845 /// Set the function called.
1846 void setCalledFunction(Value* Fn) {
1847 setCalledFunction(
1848 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
1849 Fn);
1850 }
1851 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1852 this->FTy = FTy;
1853 assert(FTy == cast<FunctionType>(
1854 cast<PointerType>(Fn->getType())->getElementType()));
1855 Op<-InstTy::ArgOffset>() = Fn;
1856 }
1857
1858protected:
1859 template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const {
1860 if (Attrs.hasAttribute(AttributeList::FunctionIndex, Kind))
1861 return true;
1862
1863 // Operand bundles override attributes on the called function, but don't
1864 // override attributes directly present on the call instruction.
1865 if (isFnAttrDisallowedByOpBundle(Kind))
1866 return false;
1867
1868 if (const Function *F = getCalledFunction())
1869 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex,
1870 Kind);
1871 return false;
1872 }
1873};
1874
1875//===----------------------------------------------------------------------===//
1876/// This class represents a function call, abstracting a target
1877/// machine's calling convention. This class uses low bit of the SubClassData
1878/// field to indicate whether or not this is a tail call. The rest of the bits
1879/// hold the calling convention of the call.
1880///
1881class CallInst : public CallBase<CallInst> {
1882 friend class OperandBundleUser<CallInst, User::op_iterator>;
1883
1884 CallInst(const CallInst &CI);
1885
1886 /// Construct a CallInst given a range of arguments.
1887 /// Construct a CallInst from a range of arguments
1888 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1889 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1890 Instruction *InsertBefore);
1891
1892 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1893 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1894 Instruction *InsertBefore)
1895 : CallInst(cast<FunctionType>(
1896 cast<PointerType>(Func->getType())->getElementType()),
1897 Func, Args, Bundles, NameStr, InsertBefore) {}
1898
1899 inline CallInst(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr,
1900 Instruction *InsertBefore)
1901 : CallInst(Func, Args, None, NameStr, InsertBefore) {}
1902
1903 /// Construct a CallInst given a range of arguments.
1904 /// Construct a CallInst from a range of arguments
1905 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1906 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1907 BasicBlock *InsertAtEnd);
1908
1909 explicit CallInst(Value *F, const Twine &NameStr, Instruction *InsertBefore);
1910
1911 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1912
1913 void init(Value *Func, ArrayRef<Value *> Args,
1914 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
1915 init(cast<FunctionType>(
1916 cast<PointerType>(Func->getType())->getElementType()),
1917 Func, Args, Bundles, NameStr);
1918 }
1919 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1920 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
1921 void init(Value *Func, const Twine &NameStr);
1922
1923protected:
1924 // Note: Instruction needs to be a friend here to call cloneImpl.
1925 friend class Instruction;
1926
1927 CallInst *cloneImpl() const;
1928
1929public:
1930 static constexpr int ArgOffset = 1;
1931
1932 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1933 ArrayRef<OperandBundleDef> Bundles = None,
1934 const Twine &NameStr = "",
1935 Instruction *InsertBefore = nullptr) {
1936 return Create(cast<FunctionType>(
1937 cast<PointerType>(Func->getType())->getElementType()),
1938 Func, Args, Bundles, NameStr, InsertBefore);
1939 }
1940
1941 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1942 const Twine &NameStr,
1943 Instruction *InsertBefore = nullptr) {
1944 return Create(cast<FunctionType>(
1945 cast<PointerType>(Func->getType())->getElementType()),
1946 Func, Args, None, NameStr, InsertBefore);
1947 }
1948
1949 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1950 const Twine &NameStr,
1951 Instruction *InsertBefore = nullptr) {
1952 return new (unsigned(Args.size() + 1))
1953 CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
1954 }
1955
1956 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1957 ArrayRef<OperandBundleDef> Bundles = None,
1958 const Twine &NameStr = "",
1959 Instruction *InsertBefore = nullptr) {
1960 const unsigned TotalOps =
1961 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1962 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1963
1964 return new (TotalOps, DescriptorBytes)
1965 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
1966 }
1967
1968 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1969 ArrayRef<OperandBundleDef> Bundles,
1970 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1971 const unsigned TotalOps =
1972 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1973 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1974
1975 return new (TotalOps, DescriptorBytes)
1976 CallInst(Func, Args, Bundles, NameStr, InsertAtEnd);
1977 }
1978
1979 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1980 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1981 return new (unsigned(Args.size() + 1))
1982 CallInst(Func, Args, None, NameStr, InsertAtEnd);
1983 }
1984
1985 static CallInst *Create(Value *F, const Twine &NameStr = "",
1986 Instruction *InsertBefore = nullptr) {
1987 return new (1) CallInst(F, NameStr, InsertBefore);
1988 }
1989
1990 static CallInst *Create(Value *F, const Twine &NameStr,
1991 BasicBlock *InsertAtEnd) {
1992 return new (1) CallInst(F, NameStr, InsertAtEnd);
1993 }
1994
1995 /// Create a clone of \p CI with a different set of operand bundles and
1996 /// insert it before \p InsertPt.
1997 ///
1998 /// The returned call instruction is identical \p CI in every way except that
1999 /// the operand bundles for the new instruction are set to the operand bundles
2000 /// in \p Bundles.
2001 static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles,
2002 Instruction *InsertPt = nullptr);
2003
2004 /// Generate the IR for a call to malloc:
2005 /// 1. Compute the malloc call's argument as the specified type's size,
2006 /// possibly multiplied by the array size if the array size is not
2007 /// constant 1.
2008 /// 2. Call malloc with that argument.
2009 /// 3. Bitcast the result of the malloc call to the specified type.
2010 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
2011 Type *AllocTy, Value *AllocSize,
2012 Value *ArraySize = nullptr,
2013 Function *MallocF = nullptr,
2014 const Twine &Name = "");
2015 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
2016 Type *AllocTy, Value *AllocSize,
2017 Value *ArraySize = nullptr,
2018 Function *MallocF = nullptr,
2019 const Twine &Name = "");
2020 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
2021 Type *AllocTy, Value *AllocSize,
2022 Value *ArraySize = nullptr,
2023 ArrayRef<OperandBundleDef> Bundles = None,
2024 Function *MallocF = nullptr,
2025 const Twine &Name = "");
2026 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
2027 Type *AllocTy, Value *AllocSize,
2028 Value *ArraySize = nullptr,
2029 ArrayRef<OperandBundleDef> Bundles = None,
2030 Function *MallocF = nullptr,
2031 const Twine &Name = "");
2032 /// Generate the IR for a call to the builtin free function.
2033 static Instruction *CreateFree(Value *Source, Instruction *InsertBefore);
2034 static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd);
2035 static Instruction *CreateFree(Value *Source,
2036 ArrayRef<OperandBundleDef> Bundles,
2037 Instruction *InsertBefore);
2038 static Instruction *CreateFree(Value *Source,
2039 ArrayRef<OperandBundleDef> Bundles,
2040 BasicBlock *InsertAtEnd);
2041
2042 // Note that 'musttail' implies 'tail'.
2043 enum TailCallKind {
2044 TCK_None = 0,
2045 TCK_Tail = 1,
2046 TCK_MustTail = 2,
2047 TCK_NoTail = 3
2048 };
2049 TailCallKind getTailCallKind() const {
2050 return TailCallKind(getSubclassDataFromInstruction() & 3);
2051 }
2052
2053 bool isTailCall() const {
2054 unsigned Kind = getSubclassDataFromInstruction() & 3;
2055 return Kind == TCK_Tail || Kind == TCK_MustTail;
2056 }
2057
2058 bool isMustTailCall() const {
2059 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
2060 }
2061
2062 bool isNoTailCall() const {
2063 return (getSubclassDataFromInstruction() & 3) == TCK_NoTail;
2064 }
2065
2066 void setTailCall(bool isTC = true) {
2067 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
2068 unsigned(isTC ? TCK_Tail : TCK_None));
2069 }
2070
2071 void setTailCallKind(TailCallKind TCK) {
2072 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
2073 unsigned(TCK));
2074 }
2075
2076 /// Return true if the call can return twice
2077 bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); }
2078 void setCanReturnTwice() {
2079 addAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice);
2080 }
2081
2082 /// Check if this call is an inline asm statement.
2083 bool isInlineAsm() const { return isa<InlineAsm>(Op<-1>()); }
2084
2085 // Methods for support type inquiry through isa, cast, and dyn_cast:
2086 static bool classof(const Instruction *I) {
2087 return I->getOpcode() == Instruction::Call;
2088 }
2089 static bool classof(const Value *V) {
2090 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2091 }
2092
2093private:
2094 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2095 // method so that subclasses cannot accidentally use it.
2096 void setInstructionSubclassData(unsigned short D) {
2097 Instruction::setInstructionSubclassData(D);
2098 }
2099};
2100
2101template <>
2102struct OperandTraits<CallBase<CallInst>>
2103 : public VariadicOperandTraits<CallBase<CallInst>, 1> {};
2104
2105CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
2106 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
2107 BasicBlock *InsertAtEnd)
2108 : CallBase<CallInst>(
2109 cast<FunctionType>(
2110 cast<PointerType>(Func->getType())->getElementType())
2111 ->getReturnType(),
2112 Instruction::Call,
2113 OperandTraits<CallBase<CallInst>>::op_end(this) -
2114 (Args.size() + CountBundleInputs(Bundles) + 1),
2115 unsigned(Args.size() + CountBundleInputs(Bundles) + 1), InsertAtEnd) {
2116 init(Func, Args, Bundles, NameStr);
2117}
2118
2119CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
2120 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
2121 Instruction *InsertBefore)
2122 : CallBase<CallInst>(Ty->getReturnType(), Instruction::Call,
2123 OperandTraits<CallBase<CallInst>>::op_end(this) -
2124 (Args.size() + CountBundleInputs(Bundles) + 1),
2125 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
2126 InsertBefore) {
2127 init(Ty, Func, Args, Bundles, NameStr);
2128}
2129
2130//===----------------------------------------------------------------------===//
2131// SelectInst Class
2132//===----------------------------------------------------------------------===//
2133
2134/// This class represents the LLVM 'select' instruction.
2135///
2136class SelectInst : public Instruction {
2137 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
2138 Instruction *InsertBefore)
2139 : Instruction(S1->getType(), Instruction::Select,
2140 &Op<0>(), 3, InsertBefore) {
2141 init(C, S1, S2);
2142 setName(NameStr);
2143 }
2144
2145 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
2146 BasicBlock *InsertAtEnd)
2147 : Instruction(S1->getType(), Instruction::Select,
2148 &Op<0>(), 3, InsertAtEnd) {
2149 init(C, S1, S2);
2150 setName(NameStr);
2151 }
2152
2153 void init(Value *C, Value *S1, Value *S2) {
2154 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
2155 Op<0>() = C;
2156 Op<1>() = S1;
2157 Op<2>() = S2;
2158 }
2159
2160protected:
2161 // Note: Instruction needs to be a friend here to call cloneImpl.
2162 friend class Instruction;
2163
2164 SelectInst *cloneImpl() const;
2165
2166public:
2167 static SelectInst *Create(Value *C, Value *S1, Value *S2,
2168 const Twine &NameStr = "",
2169 Instruction *InsertBefore = nullptr,
2170 Instruction *MDFrom = nullptr) {
2171 SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
2172 if (MDFrom)
2173 Sel->copyMetadata(*MDFrom);
2174 return Sel;
2175 }
2176
2177 static SelectInst *Create(Value *C, Value *S1, Value *S2,
2178 const Twine &NameStr,
2179 BasicBlock *InsertAtEnd) {
2180 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
2181 }
2182
2183 const Value *getCondition() const { return Op<0>(); }
2184 const Value *getTrueValue() const { return Op<1>(); }
2185 const Value *getFalseValue() const { return Op<2>(); }
2186 Value *getCondition() { return Op<0>(); }
2187 Value *getTrueValue() { return Op<1>(); }
2188 Value *getFalseValue() { return Op<2>(); }
2189
2190 void setCondition(Value *V) { Op<0>() = V; }
2191 void setTrueValue(Value *V) { Op<1>() = V; }
2192 void setFalseValue(Value *V) { Op<2>() = V; }
2193
2194 /// Return a string if the specified operands are invalid
2195 /// for a select operation, otherwise return null.
2196 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
2197
2198 /// Transparently provide more efficient getOperand methods.
2199 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2200
2201 OtherOps getOpcode() const {
2202 return static_cast<OtherOps>(Instruction::getOpcode());
2203 }
2204
2205 // Methods for support type inquiry through isa, cast, and dyn_cast:
2206 static bool classof(const Instruction *I) {
2207 return I->getOpcode() == Instruction::Select;
2208 }
2209 static bool classof(const Value *V) {
2210 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2211 }
2212};
2213
2214template <>
2215struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
2216};
2217
2218DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
2219
2220//===----------------------------------------------------------------------===//
2221// VAArgInst Class
2222//===----------------------------------------------------------------------===//
2223
2224/// This class represents the va_arg llvm instruction, which returns
2225/// an argument of the specified type given a va_list and increments that list
2226///
2227class VAArgInst : public UnaryInstruction {
2228protected:
2229 // Note: Instruction needs to be a friend here to call cloneImpl.
2230 friend class Instruction;
2231
2232 VAArgInst *cloneImpl() const;
2233
2234public:
2235 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
2236 Instruction *InsertBefore = nullptr)
2237 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
2238 setName(NameStr);
2239 }
2240
2241 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
2242 BasicBlock *InsertAtEnd)
2243 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
2244 setName(NameStr);
2245 }
2246
2247 Value *getPointerOperand() { return getOperand(0); }
2248 const Value *getPointerOperand() const { return getOperand(0); }
2249 static unsigned getPointerOperandIndex() { return 0U; }
2250
2251 // Methods for support type inquiry through isa, cast, and dyn_cast:
2252 static bool classof(const Instruction *I) {
2253 return I->getOpcode() == VAArg;
2254 }
2255 static bool classof(const Value *V) {
2256 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2257 }
2258};
2259
2260//===----------------------------------------------------------------------===//
2261// ExtractElementInst Class
2262//===----------------------------------------------------------------------===//
2263
2264/// This instruction extracts a single (scalar)
2265/// element from a VectorType value
2266///
2267class ExtractElementInst : public Instruction {
2268 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
2269 Instruction *InsertBefore = nullptr);
2270 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
2271 BasicBlock *InsertAtEnd);
2272
2273protected:
2274 // Note: Instruction needs to be a friend here to call cloneImpl.
2275 friend class Instruction;
2276
2277 ExtractElementInst *cloneImpl() const;
2278
2279public:
2280 static ExtractElementInst *Create(Value *Vec, Value *Idx,
2281 const Twine &NameStr = "",
2282 Instruction *InsertBefore = nullptr) {
2283 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
2284 }
2285
2286 static ExtractElementInst *Create(Value *Vec, Value *Idx,
2287 const Twine &NameStr,
2288 BasicBlock *InsertAtEnd) {
2289 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
2290 }
2291
2292 /// Return true if an extractelement instruction can be
2293 /// formed with the specified operands.
2294 static bool isValidOperands(const Value *Vec, const Value *Idx);
2295
2296 Value *getVectorOperand() { return Op<0>(); }
2297 Value *getIndexOperand() { return Op<1>(); }
2298 const Value *getVectorOperand() const { return Op<0>(); }
2299 const Value *getIndexOperand() const { return Op<1>(); }
2300
2301 VectorType *getVectorOperandType() const {
2302 return cast<VectorType>(getVectorOperand()->getType());
2303 }
2304
2305 /// Transparently provide more efficient getOperand methods.
2306 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2307
2308 // Methods for support type inquiry through isa, cast, and dyn_cast:
2309 static bool classof(const Instruction *I) {
2310 return I->getOpcode() == Instruction::ExtractElement;
2311 }
2312 static bool classof(const Value *V) {
2313 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2314 }
2315};
2316
2317template <>
2318struct OperandTraits<ExtractElementInst> :
2319 public FixedNumOperandTraits<ExtractElementInst, 2> {
2320};
2321
2322DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
2323
2324//===----------------------------------------------------------------------===//
2325// InsertElementInst Class
2326//===----------------------------------------------------------------------===//
2327
2328/// This instruction inserts a single (scalar)
2329/// element into a VectorType value
2330///
2331class InsertElementInst : public Instruction {
2332 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
2333 const Twine &NameStr = "",
2334 Instruction *InsertBefore = nullptr);
2335 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr,
2336 BasicBlock *InsertAtEnd);
2337
2338protected:
2339 // Note: Instruction needs to be a friend here to call cloneImpl.
2340 friend class Instruction;
2341
2342 InsertElementInst *cloneImpl() const;
2343
2344public:
2345 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2346 const Twine &NameStr = "",
2347 Instruction *InsertBefore = nullptr) {
2348 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
2349 }
2350
2351 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2352 const Twine &NameStr,
2353 BasicBlock *InsertAtEnd) {
2354 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
2355 }
2356
2357 /// Return true if an insertelement instruction can be
2358 /// formed with the specified operands.
2359 static bool isValidOperands(const Value *Vec, const Value *NewElt,
2360 const Value *Idx);
2361
2362 /// Overload to return most specific vector type.
2363 ///
2364 VectorType *getType() const {
2365 return cast<VectorType>(Instruction::getType());
2366 }
2367
2368 /// Transparently provide more efficient getOperand methods.
2369 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2370
2371 // Methods for support type inquiry through isa, cast, and dyn_cast:
2372 static bool classof(const Instruction *I) {
2373 return I->getOpcode() == Instruction::InsertElement;
2374 }
2375 static bool classof(const Value *V) {
2376 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2377 }
2378};
2379
2380template <>
2381struct OperandTraits<InsertElementInst> :
2382 public FixedNumOperandTraits<InsertElementInst, 3> {
2383};
2384
2385DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
2386
2387//===----------------------------------------------------------------------===//
2388// ShuffleVectorInst Class
2389//===----------------------------------------------------------------------===//
2390
2391/// This instruction constructs a fixed permutation of two
2392/// input vectors.
2393///
2394class ShuffleVectorInst : public Instruction {
2395protected:
2396 // Note: Instruction needs to be a friend here to call cloneImpl.
2397 friend class Instruction;
2398
2399 ShuffleVectorInst *cloneImpl() const;
2400
2401public:
2402 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2403 const Twine &NameStr = "",
2404 Instruction *InsertBefor = nullptr);
2405 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2406 const Twine &NameStr, BasicBlock *InsertAtEnd);
2407
2408 // allocate space for exactly three operands
2409 void *operator new(size_t s) {
2410 return User::operator new(s, 3);
2411 }
2412
2413 /// Return true if a shufflevector instruction can be
2414 /// formed with the specified operands.
2415 static bool isValidOperands(const Value *V1, const Value *V2,
2416 const Value *Mask);
2417
2418 /// Overload to return most specific vector type.
2419 ///
2420 VectorType *getType() const {
2421 return cast<VectorType>(Instruction::getType());
2422 }
2423
2424 /// Transparently provide more efficient getOperand methods.
2425 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2426
2427 Constant *getMask() const {
2428 return cast<Constant>(getOperand(2));
2429 }
2430
2431 /// Return the shuffle mask value for the specified element of the mask.
2432 /// Return -1 if the element is undef.
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002433 static int getMaskValue(const Constant *Mask, unsigned Elt);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002434
2435 /// Return the shuffle mask value of this instruction for the given element
2436 /// index. Return -1 if the element is undef.
2437 int getMaskValue(unsigned Elt) const {
2438 return getMaskValue(getMask(), Elt);
2439 }
2440
2441 /// Convert the input shuffle mask operand to a vector of integers. Undefined
2442 /// elements of the mask are returned as -1.
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002443 static void getShuffleMask(const Constant *Mask,
2444 SmallVectorImpl<int> &Result);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002445
2446 /// Return the mask for this instruction as a vector of integers. Undefined
2447 /// elements of the mask are returned as -1.
2448 void getShuffleMask(SmallVectorImpl<int> &Result) const {
2449 return getShuffleMask(getMask(), Result);
2450 }
2451
2452 SmallVector<int, 16> getShuffleMask() const {
2453 SmallVector<int, 16> Mask;
2454 getShuffleMask(Mask);
2455 return Mask;
2456 }
2457
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002458 /// Return true if this shuffle returns a vector with a different number of
2459 /// elements than its source elements.
2460 /// Example: shufflevector <4 x n> A, <4 x n> B, <1,2>
2461 bool changesLength() const {
2462 unsigned NumSourceElts = Op<0>()->getType()->getVectorNumElements();
2463 unsigned NumMaskElts = getMask()->getType()->getVectorNumElements();
2464 return NumSourceElts != NumMaskElts;
2465 }
2466
2467 /// Return true if this shuffle mask chooses elements from exactly one source
2468 /// vector.
2469 /// Example: <7,5,undef,7>
2470 /// This assumes that vector operands are the same length as the mask.
2471 static bool isSingleSourceMask(ArrayRef<int> Mask);
2472 static bool isSingleSourceMask(const Constant *Mask) {
2473 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2474 SmallVector<int, 16> MaskAsInts;
2475 getShuffleMask(Mask, MaskAsInts);
2476 return isSingleSourceMask(MaskAsInts);
2477 }
2478
2479 /// Return true if this shuffle chooses elements from exactly one source
2480 /// vector without changing the length of that vector.
2481 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
2482 /// TODO: Optionally allow length-changing shuffles.
2483 bool isSingleSource() const {
2484 return !changesLength() && isSingleSourceMask(getMask());
2485 }
2486
2487 /// Return true if this shuffle mask chooses elements from exactly one source
2488 /// vector without lane crossings. A shuffle using this mask is not
2489 /// necessarily a no-op because it may change the number of elements from its
2490 /// input vectors or it may provide demanded bits knowledge via undef lanes.
2491 /// Example: <undef,undef,2,3>
2492 static bool isIdentityMask(ArrayRef<int> Mask);
2493 static bool isIdentityMask(const Constant *Mask) {
2494 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2495 SmallVector<int, 16> MaskAsInts;
2496 getShuffleMask(Mask, MaskAsInts);
2497 return isIdentityMask(MaskAsInts);
2498 }
2499
2500 /// Return true if this shuffle mask chooses elements from exactly one source
2501 /// vector without lane crossings and does not change the number of elements
2502 /// from its input vectors.
2503 /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef>
2504 /// TODO: Optionally allow length-changing shuffles.
2505 bool isIdentity() const {
2506 return !changesLength() && isIdentityMask(getShuffleMask());
2507 }
2508
2509 /// Return true if this shuffle mask chooses elements from its source vectors
2510 /// without lane crossings. A shuffle using this mask would be
2511 /// equivalent to a vector select with a constant condition operand.
2512 /// Example: <4,1,6,undef>
2513 /// This returns false if the mask does not choose from both input vectors.
2514 /// In that case, the shuffle is better classified as an identity shuffle.
2515 /// This assumes that vector operands are the same length as the mask
2516 /// (a length-changing shuffle can never be equivalent to a vector select).
2517 static bool isSelectMask(ArrayRef<int> Mask);
2518 static bool isSelectMask(const Constant *Mask) {
2519 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2520 SmallVector<int, 16> MaskAsInts;
2521 getShuffleMask(Mask, MaskAsInts);
2522 return isSelectMask(MaskAsInts);
2523 }
2524
2525 /// Return true if this shuffle chooses elements from its source vectors
2526 /// without lane crossings and all operands have the same number of elements.
2527 /// In other words, this shuffle is equivalent to a vector select with a
2528 /// constant condition operand.
2529 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3>
2530 /// This returns false if the mask does not choose from both input vectors.
2531 /// In that case, the shuffle is better classified as an identity shuffle.
2532 /// TODO: Optionally allow length-changing shuffles.
2533 bool isSelect() const {
2534 return !changesLength() && isSelectMask(getMask());
2535 }
2536
2537 /// Return true if this shuffle mask swaps the order of elements from exactly
2538 /// one source vector.
2539 /// Example: <7,6,undef,4>
2540 /// This assumes that vector operands are the same length as the mask.
2541 static bool isReverseMask(ArrayRef<int> Mask);
2542 static bool isReverseMask(const Constant *Mask) {
2543 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2544 SmallVector<int, 16> MaskAsInts;
2545 getShuffleMask(Mask, MaskAsInts);
2546 return isReverseMask(MaskAsInts);
2547 }
2548
2549 /// Return true if this shuffle swaps the order of elements from exactly
2550 /// one source vector.
2551 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
2552 /// TODO: Optionally allow length-changing shuffles.
2553 bool isReverse() const {
2554 return !changesLength() && isReverseMask(getMask());
2555 }
2556
2557 /// Return true if this shuffle mask chooses all elements with the same value
2558 /// as the first element of exactly one source vector.
2559 /// Example: <4,undef,undef,4>
2560 /// This assumes that vector operands are the same length as the mask.
2561 static bool isZeroEltSplatMask(ArrayRef<int> Mask);
2562 static bool isZeroEltSplatMask(const Constant *Mask) {
2563 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2564 SmallVector<int, 16> MaskAsInts;
2565 getShuffleMask(Mask, MaskAsInts);
2566 return isZeroEltSplatMask(MaskAsInts);
2567 }
2568
2569 /// Return true if all elements of this shuffle are the same value as the
2570 /// first element of exactly one source vector without changing the length
2571 /// of that vector.
2572 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0>
2573 /// TODO: Optionally allow length-changing shuffles.
2574 /// TODO: Optionally allow splats from other elements.
2575 bool isZeroEltSplat() const {
2576 return !changesLength() && isZeroEltSplatMask(getMask());
2577 }
2578
2579 /// Return true if this shuffle mask is a transpose mask.
2580 /// Transpose vector masks transpose a 2xn matrix. They read corresponding
2581 /// even- or odd-numbered vector elements from two n-dimensional source
2582 /// vectors and write each result into consecutive elements of an
2583 /// n-dimensional destination vector. Two shuffles are necessary to complete
2584 /// the transpose, one for the even elements and another for the odd elements.
2585 /// This description closely follows how the TRN1 and TRN2 AArch64
2586 /// instructions operate.
2587 ///
2588 /// For example, a simple 2x2 matrix can be transposed with:
2589 ///
2590 /// ; Original matrix
2591 /// m0 = < a, b >
2592 /// m1 = < c, d >
2593 ///
2594 /// ; Transposed matrix
2595 /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 >
2596 /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 >
2597 ///
2598 /// For matrices having greater than n columns, the resulting nx2 transposed
2599 /// matrix is stored in two result vectors such that one vector contains
2600 /// interleaved elements from all the even-numbered rows and the other vector
2601 /// contains interleaved elements from all the odd-numbered rows. For example,
2602 /// a 2x4 matrix can be transposed with:
2603 ///
2604 /// ; Original matrix
2605 /// m0 = < a, b, c, d >
2606 /// m1 = < e, f, g, h >
2607 ///
2608 /// ; Transposed matrix
2609 /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 >
2610 /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 >
2611 static bool isTransposeMask(ArrayRef<int> Mask);
2612 static bool isTransposeMask(const Constant *Mask) {
2613 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2614 SmallVector<int, 16> MaskAsInts;
2615 getShuffleMask(Mask, MaskAsInts);
2616 return isTransposeMask(MaskAsInts);
2617 }
2618
2619 /// Return true if this shuffle transposes the elements of its inputs without
2620 /// changing the length of the vectors. This operation may also be known as a
2621 /// merge or interleave. See the description for isTransposeMask() for the
2622 /// exact specification.
2623 /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
2624 bool isTranspose() const {
2625 return !changesLength() && isTransposeMask(getMask());
2626 }
2627
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002628 /// Change values in a shuffle permute mask assuming the two vector operands
2629 /// of length InVecNumElts have swapped position.
2630 static void commuteShuffleMask(MutableArrayRef<int> Mask,
2631 unsigned InVecNumElts) {
2632 for (int &Idx : Mask) {
2633 if (Idx == -1)
2634 continue;
2635 Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts;
2636 assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&
2637 "shufflevector mask index out of range");
2638 }
2639 }
2640
2641 // Methods for support type inquiry through isa, cast, and dyn_cast:
2642 static bool classof(const Instruction *I) {
2643 return I->getOpcode() == Instruction::ShuffleVector;
2644 }
2645 static bool classof(const Value *V) {
2646 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2647 }
2648};
2649
2650template <>
2651struct OperandTraits<ShuffleVectorInst> :
2652 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
2653};
2654
2655DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
2656
2657//===----------------------------------------------------------------------===//
2658// ExtractValueInst Class
2659//===----------------------------------------------------------------------===//
2660
2661/// This instruction extracts a struct member or array
2662/// element value from an aggregate value.
2663///
2664class ExtractValueInst : public UnaryInstruction {
2665 SmallVector<unsigned, 4> Indices;
2666
2667 ExtractValueInst(const ExtractValueInst &EVI);
2668
2669 /// Constructors - Create a extractvalue instruction with a base aggregate
2670 /// value and a list of indices. The first ctor can optionally insert before
2671 /// an existing instruction, the second appends the new instruction to the
2672 /// specified BasicBlock.
2673 inline ExtractValueInst(Value *Agg,
2674 ArrayRef<unsigned> Idxs,
2675 const Twine &NameStr,
2676 Instruction *InsertBefore);
2677 inline ExtractValueInst(Value *Agg,
2678 ArrayRef<unsigned> Idxs,
2679 const Twine &NameStr, BasicBlock *InsertAtEnd);
2680
2681 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2682
2683protected:
2684 // Note: Instruction needs to be a friend here to call cloneImpl.
2685 friend class Instruction;
2686
2687 ExtractValueInst *cloneImpl() const;
2688
2689public:
2690 static ExtractValueInst *Create(Value *Agg,
2691 ArrayRef<unsigned> Idxs,
2692 const Twine &NameStr = "",
2693 Instruction *InsertBefore = nullptr) {
2694 return new
2695 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2696 }
2697
2698 static ExtractValueInst *Create(Value *Agg,
2699 ArrayRef<unsigned> Idxs,
2700 const Twine &NameStr,
2701 BasicBlock *InsertAtEnd) {
2702 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2703 }
2704
2705 /// Returns the type of the element that would be extracted
2706 /// with an extractvalue instruction with the specified parameters.
2707 ///
2708 /// Null is returned if the indices are invalid for the specified type.
2709 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2710
2711 using idx_iterator = const unsigned*;
2712
2713 inline idx_iterator idx_begin() const { return Indices.begin(); }
2714 inline idx_iterator idx_end() const { return Indices.end(); }
2715 inline iterator_range<idx_iterator> indices() const {
2716 return make_range(idx_begin(), idx_end());
2717 }
2718
2719 Value *getAggregateOperand() {
2720 return getOperand(0);
2721 }
2722 const Value *getAggregateOperand() const {
2723 return getOperand(0);
2724 }
2725 static unsigned getAggregateOperandIndex() {
2726 return 0U; // get index for modifying correct operand
2727 }
2728
2729 ArrayRef<unsigned> getIndices() const {
2730 return Indices;
2731 }
2732
2733 unsigned getNumIndices() const {
2734 return (unsigned)Indices.size();
2735 }
2736
2737 bool hasIndices() const {
2738 return true;
2739 }
2740
2741 // Methods for support type inquiry through isa, cast, and dyn_cast:
2742 static bool classof(const Instruction *I) {
2743 return I->getOpcode() == Instruction::ExtractValue;
2744 }
2745 static bool classof(const Value *V) {
2746 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2747 }
2748};
2749
2750ExtractValueInst::ExtractValueInst(Value *Agg,
2751 ArrayRef<unsigned> Idxs,
2752 const Twine &NameStr,
2753 Instruction *InsertBefore)
2754 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2755 ExtractValue, Agg, InsertBefore) {
2756 init(Idxs, NameStr);
2757}
2758
2759ExtractValueInst::ExtractValueInst(Value *Agg,
2760 ArrayRef<unsigned> Idxs,
2761 const Twine &NameStr,
2762 BasicBlock *InsertAtEnd)
2763 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2764 ExtractValue, Agg, InsertAtEnd) {
2765 init(Idxs, NameStr);
2766}
2767
2768//===----------------------------------------------------------------------===//
2769// InsertValueInst Class
2770//===----------------------------------------------------------------------===//
2771
2772/// This instruction inserts a struct field of array element
2773/// value into an aggregate value.
2774///
2775class InsertValueInst : public Instruction {
2776 SmallVector<unsigned, 4> Indices;
2777
2778 InsertValueInst(const InsertValueInst &IVI);
2779
2780 /// Constructors - Create a insertvalue instruction with a base aggregate
2781 /// value, a value to insert, and a list of indices. The first ctor can
2782 /// optionally insert before an existing instruction, the second appends
2783 /// the new instruction to the specified BasicBlock.
2784 inline InsertValueInst(Value *Agg, Value *Val,
2785 ArrayRef<unsigned> Idxs,
2786 const Twine &NameStr,
2787 Instruction *InsertBefore);
2788 inline InsertValueInst(Value *Agg, Value *Val,
2789 ArrayRef<unsigned> Idxs,
2790 const Twine &NameStr, BasicBlock *InsertAtEnd);
2791
2792 /// Constructors - These two constructors are convenience methods because one
2793 /// and two index insertvalue instructions are so common.
2794 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2795 const Twine &NameStr = "",
2796 Instruction *InsertBefore = nullptr);
2797 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr,
2798 BasicBlock *InsertAtEnd);
2799
2800 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2801 const Twine &NameStr);
2802
2803protected:
2804 // Note: Instruction needs to be a friend here to call cloneImpl.
2805 friend class Instruction;
2806
2807 InsertValueInst *cloneImpl() const;
2808
2809public:
2810 // allocate space for exactly two operands
2811 void *operator new(size_t s) {
2812 return User::operator new(s, 2);
2813 }
2814
2815 static InsertValueInst *Create(Value *Agg, Value *Val,
2816 ArrayRef<unsigned> Idxs,
2817 const Twine &NameStr = "",
2818 Instruction *InsertBefore = nullptr) {
2819 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2820 }
2821
2822 static InsertValueInst *Create(Value *Agg, Value *Val,
2823 ArrayRef<unsigned> Idxs,
2824 const Twine &NameStr,
2825 BasicBlock *InsertAtEnd) {
2826 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2827 }
2828
2829 /// Transparently provide more efficient getOperand methods.
2830 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2831
2832 using idx_iterator = const unsigned*;
2833
2834 inline idx_iterator idx_begin() const { return Indices.begin(); }
2835 inline idx_iterator idx_end() const { return Indices.end(); }
2836 inline iterator_range<idx_iterator> indices() const {
2837 return make_range(idx_begin(), idx_end());
2838 }
2839
2840 Value *getAggregateOperand() {
2841 return getOperand(0);
2842 }
2843 const Value *getAggregateOperand() const {
2844 return getOperand(0);
2845 }
2846 static unsigned getAggregateOperandIndex() {
2847 return 0U; // get index for modifying correct operand
2848 }
2849
2850 Value *getInsertedValueOperand() {
2851 return getOperand(1);
2852 }
2853 const Value *getInsertedValueOperand() const {
2854 return getOperand(1);
2855 }
2856 static unsigned getInsertedValueOperandIndex() {
2857 return 1U; // get index for modifying correct operand
2858 }
2859
2860 ArrayRef<unsigned> getIndices() const {
2861 return Indices;
2862 }
2863
2864 unsigned getNumIndices() const {
2865 return (unsigned)Indices.size();
2866 }
2867
2868 bool hasIndices() const {
2869 return true;
2870 }
2871
2872 // Methods for support type inquiry through isa, cast, and dyn_cast:
2873 static bool classof(const Instruction *I) {
2874 return I->getOpcode() == Instruction::InsertValue;
2875 }
2876 static bool classof(const Value *V) {
2877 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2878 }
2879};
2880
2881template <>
2882struct OperandTraits<InsertValueInst> :
2883 public FixedNumOperandTraits<InsertValueInst, 2> {
2884};
2885
2886InsertValueInst::InsertValueInst(Value *Agg,
2887 Value *Val,
2888 ArrayRef<unsigned> Idxs,
2889 const Twine &NameStr,
2890 Instruction *InsertBefore)
2891 : Instruction(Agg->getType(), InsertValue,
2892 OperandTraits<InsertValueInst>::op_begin(this),
2893 2, InsertBefore) {
2894 init(Agg, Val, Idxs, NameStr);
2895}
2896
2897InsertValueInst::InsertValueInst(Value *Agg,
2898 Value *Val,
2899 ArrayRef<unsigned> Idxs,
2900 const Twine &NameStr,
2901 BasicBlock *InsertAtEnd)
2902 : Instruction(Agg->getType(), InsertValue,
2903 OperandTraits<InsertValueInst>::op_begin(this),
2904 2, InsertAtEnd) {
2905 init(Agg, Val, Idxs, NameStr);
2906}
2907
2908DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2909
2910//===----------------------------------------------------------------------===//
2911// PHINode Class
2912//===----------------------------------------------------------------------===//
2913
2914// PHINode - The PHINode class is used to represent the magical mystical PHI
2915// node, that can not exist in nature, but can be synthesized in a computer
2916// scientist's overactive imagination.
2917//
2918class PHINode : public Instruction {
2919 /// The number of operands actually allocated. NumOperands is
2920 /// the number actually in use.
2921 unsigned ReservedSpace;
2922
2923 PHINode(const PHINode &PN);
2924
2925 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2926 const Twine &NameStr = "",
2927 Instruction *InsertBefore = nullptr)
2928 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2929 ReservedSpace(NumReservedValues) {
2930 setName(NameStr);
2931 allocHungoffUses(ReservedSpace);
2932 }
2933
2934 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2935 BasicBlock *InsertAtEnd)
2936 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2937 ReservedSpace(NumReservedValues) {
2938 setName(NameStr);
2939 allocHungoffUses(ReservedSpace);
2940 }
2941
2942protected:
2943 // Note: Instruction needs to be a friend here to call cloneImpl.
2944 friend class Instruction;
2945
2946 PHINode *cloneImpl() const;
2947
2948 // allocHungoffUses - this is more complicated than the generic
2949 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2950 // values and pointers to the incoming blocks, all in one allocation.
2951 void allocHungoffUses(unsigned N) {
2952 User::allocHungoffUses(N, /* IsPhi */ true);
2953 }
2954
2955public:
2956 /// Constructors - NumReservedValues is a hint for the number of incoming
2957 /// edges that this phi node will have (use 0 if you really have no idea).
2958 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2959 const Twine &NameStr = "",
2960 Instruction *InsertBefore = nullptr) {
2961 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2962 }
2963
2964 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2965 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2966 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2967 }
2968
2969 /// Provide fast operand accessors
2970 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2971
2972 // Block iterator interface. This provides access to the list of incoming
2973 // basic blocks, which parallels the list of incoming values.
2974
2975 using block_iterator = BasicBlock **;
2976 using const_block_iterator = BasicBlock * const *;
2977
2978 block_iterator block_begin() {
2979 Use::UserRef *ref =
2980 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2981 return reinterpret_cast<block_iterator>(ref + 1);
2982 }
2983
2984 const_block_iterator block_begin() const {
2985 const Use::UserRef *ref =
2986 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2987 return reinterpret_cast<const_block_iterator>(ref + 1);
2988 }
2989
2990 block_iterator block_end() {
2991 return block_begin() + getNumOperands();
2992 }
2993
2994 const_block_iterator block_end() const {
2995 return block_begin() + getNumOperands();
2996 }
2997
2998 iterator_range<block_iterator> blocks() {
2999 return make_range(block_begin(), block_end());
3000 }
3001
3002 iterator_range<const_block_iterator> blocks() const {
3003 return make_range(block_begin(), block_end());
3004 }
3005
3006 op_range incoming_values() { return operands(); }
3007
3008 const_op_range incoming_values() const { return operands(); }
3009
3010 /// Return the number of incoming edges
3011 ///
3012 unsigned getNumIncomingValues() const { return getNumOperands(); }
3013
3014 /// Return incoming value number x
3015 ///
3016 Value *getIncomingValue(unsigned i) const {
3017 return getOperand(i);
3018 }
3019 void setIncomingValue(unsigned i, Value *V) {
3020 assert(V && "PHI node got a null value!");
3021 assert(getType() == V->getType() &&
3022 "All operands to PHI node must be the same type as the PHI node!");
3023 setOperand(i, V);
3024 }
3025
3026 static unsigned getOperandNumForIncomingValue(unsigned i) {
3027 return i;
3028 }
3029
3030 static unsigned getIncomingValueNumForOperand(unsigned i) {
3031 return i;
3032 }
3033
3034 /// Return incoming basic block number @p i.
3035 ///
3036 BasicBlock *getIncomingBlock(unsigned i) const {
3037 return block_begin()[i];
3038 }
3039
3040 /// Return incoming basic block corresponding
3041 /// to an operand of the PHI.
3042 ///
3043 BasicBlock *getIncomingBlock(const Use &U) const {
3044 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
3045 return getIncomingBlock(unsigned(&U - op_begin()));
3046 }
3047
3048 /// Return incoming basic block corresponding
3049 /// to value use iterator.
3050 ///
3051 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
3052 return getIncomingBlock(I.getUse());
3053 }
3054
3055 void setIncomingBlock(unsigned i, BasicBlock *BB) {
3056 assert(BB && "PHI node got a null basic block!");
3057 block_begin()[i] = BB;
3058 }
3059
3060 /// Add an incoming value to the end of the PHI list
3061 ///
3062 void addIncoming(Value *V, BasicBlock *BB) {
3063 if (getNumOperands() == ReservedSpace)
3064 growOperands(); // Get more space!
3065 // Initialize some new operands.
3066 setNumHungOffUseOperands(getNumOperands() + 1);
3067 setIncomingValue(getNumOperands() - 1, V);
3068 setIncomingBlock(getNumOperands() - 1, BB);
3069 }
3070
3071 /// Remove an incoming value. This is useful if a
3072 /// predecessor basic block is deleted. The value removed is returned.
3073 ///
3074 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
3075 /// is true), the PHI node is destroyed and any uses of it are replaced with
3076 /// dummy values. The only time there should be zero incoming values to a PHI
3077 /// node is when the block is dead, so this strategy is sound.
3078 ///
3079 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
3080
3081 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
3082 int Idx = getBasicBlockIndex(BB);
3083 assert(Idx >= 0 && "Invalid basic block argument to remove!");
3084 return removeIncomingValue(Idx, DeletePHIIfEmpty);
3085 }
3086
3087 /// Return the first index of the specified basic
3088 /// block in the value list for this PHI. Returns -1 if no instance.
3089 ///
3090 int getBasicBlockIndex(const BasicBlock *BB) const {
3091 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3092 if (block_begin()[i] == BB)
3093 return i;
3094 return -1;
3095 }
3096
3097 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
3098 int Idx = getBasicBlockIndex(BB);
3099 assert(Idx >= 0 && "Invalid basic block argument!");
3100 return getIncomingValue(Idx);
3101 }
3102
3103 /// If the specified PHI node always merges together the
3104 /// same value, return the value, otherwise return null.
3105 Value *hasConstantValue() const;
3106
3107 /// Whether the specified PHI node always merges
3108 /// together the same value, assuming undefs are equal to a unique
3109 /// non-undef value.
3110 bool hasConstantOrUndefValue() const;
3111
3112 /// Methods for support type inquiry through isa, cast, and dyn_cast:
3113 static bool classof(const Instruction *I) {
3114 return I->getOpcode() == Instruction::PHI;
3115 }
3116 static bool classof(const Value *V) {
3117 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3118 }
3119
3120private:
3121 void growOperands();
3122};
3123
3124template <>
3125struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
3126};
3127
3128DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
3129
3130//===----------------------------------------------------------------------===//
3131// LandingPadInst Class
3132//===----------------------------------------------------------------------===//
3133
3134//===---------------------------------------------------------------------------
3135/// The landingpad instruction holds all of the information
3136/// necessary to generate correct exception handling. The landingpad instruction
3137/// cannot be moved from the top of a landing pad block, which itself is
3138/// accessible only from the 'unwind' edge of an invoke. This uses the
3139/// SubclassData field in Value to store whether or not the landingpad is a
3140/// cleanup.
3141///
3142class LandingPadInst : public Instruction {
3143 /// The number of operands actually allocated. NumOperands is
3144 /// the number actually in use.
3145 unsigned ReservedSpace;
3146
3147 LandingPadInst(const LandingPadInst &LP);
3148
3149public:
3150 enum ClauseType { Catch, Filter };
3151
3152private:
3153 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
3154 const Twine &NameStr, Instruction *InsertBefore);
3155 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
3156 const Twine &NameStr, BasicBlock *InsertAtEnd);
3157
3158 // Allocate space for exactly zero operands.
3159 void *operator new(size_t s) {
3160 return User::operator new(s);
3161 }
3162
3163 void growOperands(unsigned Size);
3164 void init(unsigned NumReservedValues, const Twine &NameStr);
3165
3166protected:
3167 // Note: Instruction needs to be a friend here to call cloneImpl.
3168 friend class Instruction;
3169
3170 LandingPadInst *cloneImpl() const;
3171
3172public:
3173 /// Constructors - NumReservedClauses is a hint for the number of incoming
3174 /// clauses that this landingpad will have (use 0 if you really have no idea).
3175 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
3176 const Twine &NameStr = "",
3177 Instruction *InsertBefore = nullptr);
3178 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
3179 const Twine &NameStr, BasicBlock *InsertAtEnd);
3180
3181 /// Provide fast operand accessors
3182 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3183
3184 /// Return 'true' if this landingpad instruction is a
3185 /// cleanup. I.e., it should be run when unwinding even if its landing pad
3186 /// doesn't catch the exception.
3187 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
3188
3189 /// Indicate that this landingpad instruction is a cleanup.
3190 void setCleanup(bool V) {
3191 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
3192 (V ? 1 : 0));
3193 }
3194
3195 /// Add a catch or filter clause to the landing pad.
3196 void addClause(Constant *ClauseVal);
3197
3198 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
3199 /// determine what type of clause this is.
3200 Constant *getClause(unsigned Idx) const {
3201 return cast<Constant>(getOperandList()[Idx]);
3202 }
3203
3204 /// Return 'true' if the clause and index Idx is a catch clause.
3205 bool isCatch(unsigned Idx) const {
3206 return !isa<ArrayType>(getOperandList()[Idx]->getType());
3207 }
3208
3209 /// Return 'true' if the clause and index Idx is a filter clause.
3210 bool isFilter(unsigned Idx) const {
3211 return isa<ArrayType>(getOperandList()[Idx]->getType());
3212 }
3213
3214 /// Get the number of clauses for this landing pad.
3215 unsigned getNumClauses() const { return getNumOperands(); }
3216
3217 /// Grow the size of the operand list to accommodate the new
3218 /// number of clauses.
3219 void reserveClauses(unsigned Size) { growOperands(Size); }
3220
3221 // Methods for support type inquiry through isa, cast, and dyn_cast:
3222 static bool classof(const Instruction *I) {
3223 return I->getOpcode() == Instruction::LandingPad;
3224 }
3225 static bool classof(const Value *V) {
3226 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3227 }
3228};
3229
3230template <>
3231struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
3232};
3233
3234DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
3235
3236//===----------------------------------------------------------------------===//
3237// ReturnInst Class
3238//===----------------------------------------------------------------------===//
3239
3240//===---------------------------------------------------------------------------
3241/// Return a value (possibly void), from a function. Execution
3242/// does not continue in this function any longer.
3243///
3244class ReturnInst : public TerminatorInst {
3245 ReturnInst(const ReturnInst &RI);
3246
3247private:
3248 // ReturnInst constructors:
3249 // ReturnInst() - 'ret void' instruction
3250 // ReturnInst( null) - 'ret void' instruction
3251 // ReturnInst(Value* X) - 'ret X' instruction
3252 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
3253 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
3254 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
3255 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
3256 //
3257 // NOTE: If the Value* passed is of type void then the constructor behaves as
3258 // if it was passed NULL.
3259 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
3260 Instruction *InsertBefore = nullptr);
3261 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
3262 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3263
3264protected:
3265 // Note: Instruction needs to be a friend here to call cloneImpl.
3266 friend class Instruction;
3267
3268 ReturnInst *cloneImpl() const;
3269
3270public:
3271 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
3272 Instruction *InsertBefore = nullptr) {
3273 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
3274 }
3275
3276 static ReturnInst* Create(LLVMContext &C, Value *retVal,
3277 BasicBlock *InsertAtEnd) {
3278 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
3279 }
3280
3281 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
3282 return new(0) ReturnInst(C, InsertAtEnd);
3283 }
3284
3285 /// Provide fast operand accessors
3286 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3287
3288 /// Convenience accessor. Returns null if there is no return value.
3289 Value *getReturnValue() const {
3290 return getNumOperands() != 0 ? getOperand(0) : nullptr;
3291 }
3292
3293 unsigned getNumSuccessors() const { return 0; }
3294
3295 // Methods for support type inquiry through isa, cast, and dyn_cast:
3296 static bool classof(const Instruction *I) {
3297 return (I->getOpcode() == Instruction::Ret);
3298 }
3299 static bool classof(const Value *V) {
3300 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3301 }
3302
3303private:
3304 friend TerminatorInst;
3305
3306 BasicBlock *getSuccessor(unsigned idx) const {
3307 llvm_unreachable("ReturnInst has no successors!");
3308 }
3309
3310 void setSuccessor(unsigned idx, BasicBlock *B) {
3311 llvm_unreachable("ReturnInst has no successors!");
3312 }
3313};
3314
3315template <>
3316struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
3317};
3318
3319DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
3320
3321//===----------------------------------------------------------------------===//
3322// BranchInst Class
3323//===----------------------------------------------------------------------===//
3324
3325//===---------------------------------------------------------------------------
3326/// Conditional or Unconditional Branch instruction.
3327///
3328class BranchInst : public TerminatorInst {
3329 /// Ops list - Branches are strange. The operands are ordered:
3330 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
3331 /// they don't have to check for cond/uncond branchness. These are mostly
3332 /// accessed relative from op_end().
3333 BranchInst(const BranchInst &BI);
3334 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
3335 // BranchInst(BB *B) - 'br B'
3336 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
3337 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
3338 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
3339 // BranchInst(BB* B, BB *I) - 'br B' insert at end
3340 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
3341 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
3342 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
3343 Instruction *InsertBefore = nullptr);
3344 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
3345 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
3346 BasicBlock *InsertAtEnd);
3347
3348 void AssertOK();
3349
3350protected:
3351 // Note: Instruction needs to be a friend here to call cloneImpl.
3352 friend class Instruction;
3353
3354 BranchInst *cloneImpl() const;
3355
3356public:
3357 static BranchInst *Create(BasicBlock *IfTrue,
3358 Instruction *InsertBefore = nullptr) {
3359 return new(1) BranchInst(IfTrue, InsertBefore);
3360 }
3361
3362 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3363 Value *Cond, Instruction *InsertBefore = nullptr) {
3364 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
3365 }
3366
3367 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
3368 return new(1) BranchInst(IfTrue, InsertAtEnd);
3369 }
3370
3371 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3372 Value *Cond, BasicBlock *InsertAtEnd) {
3373 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
3374 }
3375
3376 /// Transparently provide more efficient getOperand methods.
3377 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3378
3379 bool isUnconditional() const { return getNumOperands() == 1; }
3380 bool isConditional() const { return getNumOperands() == 3; }
3381
3382 Value *getCondition() const {
3383 assert(isConditional() && "Cannot get condition of an uncond branch!");
3384 return Op<-3>();
3385 }
3386
3387 void setCondition(Value *V) {
3388 assert(isConditional() && "Cannot set condition of unconditional branch!");
3389 Op<-3>() = V;
3390 }
3391
3392 unsigned getNumSuccessors() const { return 1+isConditional(); }
3393
3394 BasicBlock *getSuccessor(unsigned i) const {
3395 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
3396 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
3397 }
3398
3399 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3400 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
3401 *(&Op<-1>() - idx) = NewSucc;
3402 }
3403
3404 /// Swap the successors of this branch instruction.
3405 ///
3406 /// Swaps the successors of the branch instruction. This also swaps any
3407 /// branch weight metadata associated with the instruction so that it
3408 /// continues to map correctly to each operand.
3409 void swapSuccessors();
3410
3411 // Methods for support type inquiry through isa, cast, and dyn_cast:
3412 static bool classof(const Instruction *I) {
3413 return (I->getOpcode() == Instruction::Br);
3414 }
3415 static bool classof(const Value *V) {
3416 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3417 }
3418};
3419
3420template <>
3421struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
3422};
3423
3424DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
3425
3426//===----------------------------------------------------------------------===//
3427// SwitchInst Class
3428//===----------------------------------------------------------------------===//
3429
3430//===---------------------------------------------------------------------------
3431/// Multiway switch
3432///
3433class SwitchInst : public TerminatorInst {
3434 unsigned ReservedSpace;
3435
3436 // Operand[0] = Value to switch on
3437 // Operand[1] = Default basic block destination
3438 // Operand[2n ] = Value to match
3439 // Operand[2n+1] = BasicBlock to go to on match
3440 SwitchInst(const SwitchInst &SI);
3441
3442 /// Create a new switch instruction, specifying a value to switch on and a
3443 /// default destination. The number of additional cases can be specified here
3444 /// to make memory allocation more efficient. This constructor can also
3445 /// auto-insert before another instruction.
3446 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3447 Instruction *InsertBefore);
3448
3449 /// Create a new switch instruction, specifying a value to switch on and a
3450 /// default destination. The number of additional cases can be specified here
3451 /// to make memory allocation more efficient. This constructor also
3452 /// auto-inserts at the end of the specified BasicBlock.
3453 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3454 BasicBlock *InsertAtEnd);
3455
3456 // allocate space for exactly zero operands
3457 void *operator new(size_t s) {
3458 return User::operator new(s);
3459 }
3460
3461 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
3462 void growOperands();
3463
3464protected:
3465 // Note: Instruction needs to be a friend here to call cloneImpl.
3466 friend class Instruction;
3467
3468 SwitchInst *cloneImpl() const;
3469
3470public:
3471 // -2
3472 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
3473
3474 template <typename CaseHandleT> class CaseIteratorImpl;
3475
3476 /// A handle to a particular switch case. It exposes a convenient interface
3477 /// to both the case value and the successor block.
3478 ///
3479 /// We define this as a template and instantiate it to form both a const and
3480 /// non-const handle.
3481 template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT>
3482 class CaseHandleImpl {
3483 // Directly befriend both const and non-const iterators.
3484 friend class SwitchInst::CaseIteratorImpl<
3485 CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>;
3486
3487 protected:
3488 // Expose the switch type we're parameterized with to the iterator.
3489 using SwitchInstType = SwitchInstT;
3490
3491 SwitchInstT *SI;
3492 ptrdiff_t Index;
3493
3494 CaseHandleImpl() = default;
3495 CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {}
3496
3497 public:
3498 /// Resolves case value for current case.
3499 ConstantIntT *getCaseValue() const {
3500 assert((unsigned)Index < SI->getNumCases() &&
3501 "Index out the number of cases.");
3502 return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2));
3503 }
3504
3505 /// Resolves successor for current case.
3506 BasicBlockT *getCaseSuccessor() const {
3507 assert(((unsigned)Index < SI->getNumCases() ||
3508 (unsigned)Index == DefaultPseudoIndex) &&
3509 "Index out the number of cases.");
3510 return SI->getSuccessor(getSuccessorIndex());
3511 }
3512
3513 /// Returns number of current case.
3514 unsigned getCaseIndex() const { return Index; }
3515
3516 /// Returns TerminatorInst's successor index for current case successor.
3517 unsigned getSuccessorIndex() const {
3518 assert(((unsigned)Index == DefaultPseudoIndex ||
3519 (unsigned)Index < SI->getNumCases()) &&
3520 "Index out the number of cases.");
3521 return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0;
3522 }
3523
3524 bool operator==(const CaseHandleImpl &RHS) const {
3525 assert(SI == RHS.SI && "Incompatible operators.");
3526 return Index == RHS.Index;
3527 }
3528 };
3529
3530 using ConstCaseHandle =
3531 CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>;
3532
3533 class CaseHandle
3534 : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> {
3535 friend class SwitchInst::CaseIteratorImpl<CaseHandle>;
3536
3537 public:
3538 CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {}
3539
3540 /// Sets the new value for current case.
3541 void setValue(ConstantInt *V) {
3542 assert((unsigned)Index < SI->getNumCases() &&
3543 "Index out the number of cases.");
3544 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3545 }
3546
3547 /// Sets the new successor for current case.
3548 void setSuccessor(BasicBlock *S) {
3549 SI->setSuccessor(getSuccessorIndex(), S);
3550 }
3551 };
3552
3553 template <typename CaseHandleT>
3554 class CaseIteratorImpl
3555 : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>,
3556 std::random_access_iterator_tag,
3557 CaseHandleT> {
3558 using SwitchInstT = typename CaseHandleT::SwitchInstType;
3559
3560 CaseHandleT Case;
3561
3562 public:
3563 /// Default constructed iterator is in an invalid state until assigned to
3564 /// a case for a particular switch.
3565 CaseIteratorImpl() = default;
3566
3567 /// Initializes case iterator for given SwitchInst and for given
3568 /// case number.
3569 CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {}
3570
3571 /// Initializes case iterator for given SwitchInst and for given
3572 /// TerminatorInst's successor index.
3573 static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI,
3574 unsigned SuccessorIndex) {
3575 assert(SuccessorIndex < SI->getNumSuccessors() &&
3576 "Successor index # out of range!");
3577 return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1)
3578 : CaseIteratorImpl(SI, DefaultPseudoIndex);
3579 }
3580
3581 /// Support converting to the const variant. This will be a no-op for const
3582 /// variant.
3583 operator CaseIteratorImpl<ConstCaseHandle>() const {
3584 return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index);
3585 }
3586
3587 CaseIteratorImpl &operator+=(ptrdiff_t N) {
3588 // Check index correctness after addition.
3589 // Note: Index == getNumCases() means end().
3590 assert(Case.Index + N >= 0 &&
3591 (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&
3592 "Case.Index out the number of cases.");
3593 Case.Index += N;
3594 return *this;
3595 }
3596 CaseIteratorImpl &operator-=(ptrdiff_t N) {
3597 // Check index correctness after subtraction.
3598 // Note: Case.Index == getNumCases() means end().
3599 assert(Case.Index - N >= 0 &&
3600 (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&
3601 "Case.Index out the number of cases.");
3602 Case.Index -= N;
3603 return *this;
3604 }
3605 ptrdiff_t operator-(const CaseIteratorImpl &RHS) const {
3606 assert(Case.SI == RHS.Case.SI && "Incompatible operators.");
3607 return Case.Index - RHS.Case.Index;
3608 }
3609 bool operator==(const CaseIteratorImpl &RHS) const {
3610 return Case == RHS.Case;
3611 }
3612 bool operator<(const CaseIteratorImpl &RHS) const {
3613 assert(Case.SI == RHS.Case.SI && "Incompatible operators.");
3614 return Case.Index < RHS.Case.Index;
3615 }
3616 CaseHandleT &operator*() { return Case; }
3617 const CaseHandleT &operator*() const { return Case; }
3618 };
3619
3620 using CaseIt = CaseIteratorImpl<CaseHandle>;
3621 using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>;
3622
3623 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3624 unsigned NumCases,
3625 Instruction *InsertBefore = nullptr) {
3626 return new SwitchInst(Value, Default, NumCases, InsertBefore);
3627 }
3628
3629 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3630 unsigned NumCases, BasicBlock *InsertAtEnd) {
3631 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
3632 }
3633
3634 /// Provide fast operand accessors
3635 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3636
3637 // Accessor Methods for Switch stmt
3638 Value *getCondition() const { return getOperand(0); }
3639 void setCondition(Value *V) { setOperand(0, V); }
3640
3641 BasicBlock *getDefaultDest() const {
3642 return cast<BasicBlock>(getOperand(1));
3643 }
3644
3645 void setDefaultDest(BasicBlock *DefaultCase) {
3646 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3647 }
3648
3649 /// Return the number of 'cases' in this switch instruction, excluding the
3650 /// default case.
3651 unsigned getNumCases() const {
3652 return getNumOperands()/2 - 1;
3653 }
3654
3655 /// Returns a read/write iterator that points to the first case in the
3656 /// SwitchInst.
3657 CaseIt case_begin() {
3658 return CaseIt(this, 0);
3659 }
3660
3661 /// Returns a read-only iterator that points to the first case in the
3662 /// SwitchInst.
3663 ConstCaseIt case_begin() const {
3664 return ConstCaseIt(this, 0);
3665 }
3666
3667 /// Returns a read/write iterator that points one past the last in the
3668 /// SwitchInst.
3669 CaseIt case_end() {
3670 return CaseIt(this, getNumCases());
3671 }
3672
3673 /// Returns a read-only iterator that points one past the last in the
3674 /// SwitchInst.
3675 ConstCaseIt case_end() const {
3676 return ConstCaseIt(this, getNumCases());
3677 }
3678
3679 /// Iteration adapter for range-for loops.
3680 iterator_range<CaseIt> cases() {
3681 return make_range(case_begin(), case_end());
3682 }
3683
3684 /// Constant iteration adapter for range-for loops.
3685 iterator_range<ConstCaseIt> cases() const {
3686 return make_range(case_begin(), case_end());
3687 }
3688
3689 /// Returns an iterator that points to the default case.
3690 /// Note: this iterator allows to resolve successor only. Attempt
3691 /// to resolve case value causes an assertion.
3692 /// Also note, that increment and decrement also causes an assertion and
3693 /// makes iterator invalid.
3694 CaseIt case_default() {
3695 return CaseIt(this, DefaultPseudoIndex);
3696 }
3697 ConstCaseIt case_default() const {
3698 return ConstCaseIt(this, DefaultPseudoIndex);
3699 }
3700
3701 /// Search all of the case values for the specified constant. If it is
3702 /// explicitly handled, return the case iterator of it, otherwise return
3703 /// default case iterator to indicate that it is handled by the default
3704 /// handler.
3705 CaseIt findCaseValue(const ConstantInt *C) {
3706 CaseIt I = llvm::find_if(
3707 cases(), [C](CaseHandle &Case) { return Case.getCaseValue() == C; });
3708 if (I != case_end())
3709 return I;
3710
3711 return case_default();
3712 }
3713 ConstCaseIt findCaseValue(const ConstantInt *C) const {
3714 ConstCaseIt I = llvm::find_if(cases(), [C](ConstCaseHandle &Case) {
3715 return Case.getCaseValue() == C;
3716 });
3717 if (I != case_end())
3718 return I;
3719
3720 return case_default();
3721 }
3722
3723 /// Finds the unique case value for a given successor. Returns null if the
3724 /// successor is not found, not unique, or is the default case.
3725 ConstantInt *findCaseDest(BasicBlock *BB) {
3726 if (BB == getDefaultDest())
3727 return nullptr;
3728
3729 ConstantInt *CI = nullptr;
3730 for (auto Case : cases()) {
3731 if (Case.getCaseSuccessor() != BB)
3732 continue;
3733
3734 if (CI)
3735 return nullptr; // Multiple cases lead to BB.
3736
3737 CI = Case.getCaseValue();
3738 }
3739
3740 return CI;
3741 }
3742
3743 /// Add an entry to the switch instruction.
3744 /// Note:
3745 /// This action invalidates case_end(). Old case_end() iterator will
3746 /// point to the added case.
3747 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3748
3749 /// This method removes the specified case and its successor from the switch
3750 /// instruction. Note that this operation may reorder the remaining cases at
3751 /// index idx and above.
3752 /// Note:
3753 /// This action invalidates iterators for all cases following the one removed,
3754 /// including the case_end() iterator. It returns an iterator for the next
3755 /// case.
3756 CaseIt removeCase(CaseIt I);
3757
3758 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3759 BasicBlock *getSuccessor(unsigned idx) const {
3760 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
3761 return cast<BasicBlock>(getOperand(idx*2+1));
3762 }
3763 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3764 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
3765 setOperand(idx * 2 + 1, NewSucc);
3766 }
3767
3768 // Methods for support type inquiry through isa, cast, and dyn_cast:
3769 static bool classof(const Instruction *I) {
3770 return I->getOpcode() == Instruction::Switch;
3771 }
3772 static bool classof(const Value *V) {
3773 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3774 }
3775};
3776
3777template <>
3778struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3779};
3780
3781DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
3782
3783//===----------------------------------------------------------------------===//
3784// IndirectBrInst Class
3785//===----------------------------------------------------------------------===//
3786
3787//===---------------------------------------------------------------------------
3788/// Indirect Branch Instruction.
3789///
3790class IndirectBrInst : public TerminatorInst {
3791 unsigned ReservedSpace;
3792
3793 // Operand[0] = Address to jump to
3794 // Operand[n+1] = n-th destination
3795 IndirectBrInst(const IndirectBrInst &IBI);
3796
3797 /// Create a new indirectbr instruction, specifying an
3798 /// Address to jump to. The number of expected destinations can be specified
3799 /// here to make memory allocation more efficient. This constructor can also
3800 /// autoinsert before another instruction.
3801 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3802
3803 /// Create a new indirectbr instruction, specifying an
3804 /// Address to jump to. The number of expected destinations can be specified
3805 /// here to make memory allocation more efficient. This constructor also
3806 /// autoinserts at the end of the specified BasicBlock.
3807 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3808
3809 // allocate space for exactly zero operands
3810 void *operator new(size_t s) {
3811 return User::operator new(s);
3812 }
3813
3814 void init(Value *Address, unsigned NumDests);
3815 void growOperands();
3816
3817protected:
3818 // Note: Instruction needs to be a friend here to call cloneImpl.
3819 friend class Instruction;
3820
3821 IndirectBrInst *cloneImpl() const;
3822
3823public:
3824 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3825 Instruction *InsertBefore = nullptr) {
3826 return new IndirectBrInst(Address, NumDests, InsertBefore);
3827 }
3828
3829 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3830 BasicBlock *InsertAtEnd) {
3831 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3832 }
3833
3834 /// Provide fast operand accessors.
3835 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3836
3837 // Accessor Methods for IndirectBrInst instruction.
3838 Value *getAddress() { return getOperand(0); }
3839 const Value *getAddress() const { return getOperand(0); }
3840 void setAddress(Value *V) { setOperand(0, V); }
3841
3842 /// return the number of possible destinations in this
3843 /// indirectbr instruction.
3844 unsigned getNumDestinations() const { return getNumOperands()-1; }
3845
3846 /// Return the specified destination.
3847 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3848 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3849
3850 /// Add a destination.
3851 ///
3852 void addDestination(BasicBlock *Dest);
3853
3854 /// This method removes the specified successor from the
3855 /// indirectbr instruction.
3856 void removeDestination(unsigned i);
3857
3858 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3859 BasicBlock *getSuccessor(unsigned i) const {
3860 return cast<BasicBlock>(getOperand(i+1));
3861 }
3862 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3863 setOperand(i + 1, NewSucc);
3864 }
3865
3866 // Methods for support type inquiry through isa, cast, and dyn_cast:
3867 static bool classof(const Instruction *I) {
3868 return I->getOpcode() == Instruction::IndirectBr;
3869 }
3870 static bool classof(const Value *V) {
3871 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3872 }
3873};
3874
3875template <>
3876struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3877};
3878
3879DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3880
3881//===----------------------------------------------------------------------===//
3882// InvokeInst Class
3883//===----------------------------------------------------------------------===//
3884
3885/// Invoke instruction. The SubclassData field is used to hold the
3886/// calling convention of the call.
3887///
3888class InvokeInst : public CallBase<InvokeInst> {
3889 friend class OperandBundleUser<InvokeInst, User::op_iterator>;
3890
3891 InvokeInst(const InvokeInst &BI);
3892
3893 /// Construct an InvokeInst given a range of arguments.
3894 ///
3895 /// Construct an InvokeInst from a range of arguments
3896 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3897 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3898 unsigned Values, const Twine &NameStr,
3899 Instruction *InsertBefore)
3900 : InvokeInst(cast<FunctionType>(
3901 cast<PointerType>(Func->getType())->getElementType()),
3902 Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3903 InsertBefore) {}
3904
3905 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3906 BasicBlock *IfException, ArrayRef<Value *> Args,
3907 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3908 const Twine &NameStr, Instruction *InsertBefore);
3909 /// Construct an InvokeInst given a range of arguments.
3910 ///
3911 /// Construct an InvokeInst from a range of arguments
3912 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3913 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3914 unsigned Values, const Twine &NameStr,
3915 BasicBlock *InsertAtEnd);
3916
3917
3918 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3919 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3920 const Twine &NameStr) {
3921 init(cast<FunctionType>(
3922 cast<PointerType>(Func->getType())->getElementType()),
3923 Func, IfNormal, IfException, Args, Bundles, NameStr);
3924 }
3925
3926 void init(FunctionType *FTy, Value *Func, BasicBlock *IfNormal,
3927 BasicBlock *IfException, ArrayRef<Value *> Args,
3928 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3929
3930protected:
3931 // Note: Instruction needs to be a friend here to call cloneImpl.
3932 friend class Instruction;
3933
3934 InvokeInst *cloneImpl() const;
3935
3936public:
3937 static constexpr int ArgOffset = 3;
3938 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3939 BasicBlock *IfException, ArrayRef<Value *> Args,
3940 const Twine &NameStr,
3941 Instruction *InsertBefore = nullptr) {
3942 return Create(cast<FunctionType>(
3943 cast<PointerType>(Func->getType())->getElementType()),
3944 Func, IfNormal, IfException, Args, None, NameStr,
3945 InsertBefore);
3946 }
3947
3948 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3949 BasicBlock *IfException, ArrayRef<Value *> Args,
3950 ArrayRef<OperandBundleDef> Bundles = None,
3951 const Twine &NameStr = "",
3952 Instruction *InsertBefore = nullptr) {
3953 return Create(cast<FunctionType>(
3954 cast<PointerType>(Func->getType())->getElementType()),
3955 Func, IfNormal, IfException, Args, Bundles, NameStr,
3956 InsertBefore);
3957 }
3958
3959 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3960 BasicBlock *IfException, ArrayRef<Value *> Args,
3961 const Twine &NameStr,
3962 Instruction *InsertBefore = nullptr) {
3963 unsigned Values = unsigned(Args.size()) + 3;
3964 return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args, None,
3965 Values, NameStr, InsertBefore);
3966 }
3967
3968 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3969 BasicBlock *IfException, ArrayRef<Value *> Args,
3970 ArrayRef<OperandBundleDef> Bundles = None,
3971 const Twine &NameStr = "",
3972 Instruction *InsertBefore = nullptr) {
3973 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3974 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3975
3976 return new (Values, DescriptorBytes)
3977 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, Values,
3978 NameStr, InsertBefore);
3979 }
3980
3981 static InvokeInst *Create(Value *Func,
3982 BasicBlock *IfNormal, BasicBlock *IfException,
3983 ArrayRef<Value *> Args, const Twine &NameStr,
3984 BasicBlock *InsertAtEnd) {
3985 unsigned Values = unsigned(Args.size()) + 3;
3986 return new (Values) InvokeInst(Func, IfNormal, IfException, Args, None,
3987 Values, NameStr, InsertAtEnd);
3988 }
3989
3990 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3991 BasicBlock *IfException, ArrayRef<Value *> Args,
3992 ArrayRef<OperandBundleDef> Bundles,
3993 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3994 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3995 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3996
3997 return new (Values, DescriptorBytes)
3998 InvokeInst(Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3999 InsertAtEnd);
4000 }
4001
4002 /// Create a clone of \p II with a different set of operand bundles and
4003 /// insert it before \p InsertPt.
4004 ///
4005 /// The returned invoke instruction is identical to \p II in every way except
4006 /// that the operand bundles for the new instruction are set to the operand
4007 /// bundles in \p Bundles.
4008 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles,
4009 Instruction *InsertPt = nullptr);
4010
4011 /// Determine if the call should not perform indirect branch tracking.
4012 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
4013
4014 /// Determine if the call cannot unwind.
4015 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
4016 void setDoesNotThrow() {
4017 addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
4018 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +01004019
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01004020 /// Return the function called, or null if this is an
4021 /// indirect function invocation.
4022 ///
4023 Function *getCalledFunction() const {
4024 return dyn_cast<Function>(Op<-3>());
4025 }
4026
4027 /// Get a pointer to the function that is invoked by this
4028 /// instruction
4029 const Value *getCalledValue() const { return Op<-3>(); }
4030 Value *getCalledValue() { return Op<-3>(); }
4031
4032 /// Set the function called.
4033 void setCalledFunction(Value* Fn) {
4034 setCalledFunction(
4035 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
4036 Fn);
4037 }
4038 void setCalledFunction(FunctionType *FTy, Value *Fn) {
4039 this->FTy = FTy;
4040 assert(FTy == cast<FunctionType>(
4041 cast<PointerType>(Fn->getType())->getElementType()));
4042 Op<-3>() = Fn;
4043 }
4044
4045 // get*Dest - Return the destination basic blocks...
4046 BasicBlock *getNormalDest() const {
4047 return cast<BasicBlock>(Op<-2>());
4048 }
4049 BasicBlock *getUnwindDest() const {
4050 return cast<BasicBlock>(Op<-1>());
4051 }
4052 void setNormalDest(BasicBlock *B) {
4053 Op<-2>() = reinterpret_cast<Value*>(B);
4054 }
4055 void setUnwindDest(BasicBlock *B) {
4056 Op<-1>() = reinterpret_cast<Value*>(B);
4057 }
4058
4059 /// Get the landingpad instruction from the landing pad
4060 /// block (the unwind destination).
4061 LandingPadInst *getLandingPadInst() const;
4062
4063 BasicBlock *getSuccessor(unsigned i) const {
4064 assert(i < 2 && "Successor # out of range for invoke!");
4065 return i == 0 ? getNormalDest() : getUnwindDest();
4066 }
4067
4068 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
4069 assert(idx < 2 && "Successor # out of range for invoke!");
4070 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
4071 }
4072
4073 unsigned getNumSuccessors() const { return 2; }
4074
4075 // Methods for support type inquiry through isa, cast, and dyn_cast:
4076 static bool classof(const Instruction *I) {
4077 return (I->getOpcode() == Instruction::Invoke);
4078 }
4079 static bool classof(const Value *V) {
4080 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4081 }
4082
4083private:
4084
4085 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4086 // method so that subclasses cannot accidentally use it.
4087 void setInstructionSubclassData(unsigned short D) {
4088 Instruction::setInstructionSubclassData(D);
4089 }
4090};
4091
4092template <>
4093struct OperandTraits<CallBase<InvokeInst>>
4094 : public VariadicOperandTraits<CallBase<InvokeInst>, 3> {};
4095
4096InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
4097 BasicBlock *IfException, ArrayRef<Value *> Args,
4098 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
4099 const Twine &NameStr, Instruction *InsertBefore)
4100 : CallBase<InvokeInst>(Ty->getReturnType(), Instruction::Invoke,
4101 OperandTraits<CallBase<InvokeInst>>::op_end(this) -
4102 Values,
4103 Values, InsertBefore) {
4104 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
4105}
4106
4107InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
4108 BasicBlock *IfException, ArrayRef<Value *> Args,
4109 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
4110 const Twine &NameStr, BasicBlock *InsertAtEnd)
4111 : CallBase<InvokeInst>(
4112 cast<FunctionType>(
4113 cast<PointerType>(Func->getType())->getElementType())
4114 ->getReturnType(),
4115 Instruction::Invoke,
4116 OperandTraits<CallBase<InvokeInst>>::op_end(this) - Values, Values,
4117 InsertAtEnd) {
4118 init(Func, IfNormal, IfException, Args, Bundles, NameStr);
4119}
4120
4121
4122//===----------------------------------------------------------------------===//
4123// ResumeInst Class
4124//===----------------------------------------------------------------------===//
4125
4126//===---------------------------------------------------------------------------
4127/// Resume the propagation of an exception.
4128///
4129class ResumeInst : public TerminatorInst {
4130 ResumeInst(const ResumeInst &RI);
4131
4132 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
4133 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
4134
4135protected:
4136 // Note: Instruction needs to be a friend here to call cloneImpl.
4137 friend class Instruction;
4138
4139 ResumeInst *cloneImpl() const;
4140
4141public:
4142 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
4143 return new(1) ResumeInst(Exn, InsertBefore);
4144 }
4145
4146 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
4147 return new(1) ResumeInst(Exn, InsertAtEnd);
4148 }
4149
4150 /// Provide fast operand accessors
4151 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4152
4153 /// Convenience accessor.
4154 Value *getValue() const { return Op<0>(); }
4155
4156 unsigned getNumSuccessors() const { return 0; }
4157
4158 // Methods for support type inquiry through isa, cast, and dyn_cast:
4159 static bool classof(const Instruction *I) {
4160 return I->getOpcode() == Instruction::Resume;
4161 }
4162 static bool classof(const Value *V) {
4163 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4164 }
4165
4166private:
4167 friend TerminatorInst;
4168
4169 BasicBlock *getSuccessor(unsigned idx) const {
4170 llvm_unreachable("ResumeInst has no successors!");
4171 }
4172
4173 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
4174 llvm_unreachable("ResumeInst has no successors!");
4175 }
4176};
4177
4178template <>
4179struct OperandTraits<ResumeInst> :
4180 public FixedNumOperandTraits<ResumeInst, 1> {
4181};
4182
4183DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
4184
4185//===----------------------------------------------------------------------===//
4186// CatchSwitchInst Class
4187//===----------------------------------------------------------------------===//
4188class CatchSwitchInst : public TerminatorInst {
4189 /// The number of operands actually allocated. NumOperands is
4190 /// the number actually in use.
4191 unsigned ReservedSpace;
4192
4193 // Operand[0] = Outer scope
4194 // Operand[1] = Unwind block destination
4195 // Operand[n] = BasicBlock to go to on match
4196 CatchSwitchInst(const CatchSwitchInst &CSI);
4197
4198 /// Create a new switch instruction, specifying a
4199 /// default destination. The number of additional handlers can be specified
4200 /// here to make memory allocation more efficient.
4201 /// This constructor can also autoinsert before another instruction.
4202 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4203 unsigned NumHandlers, const Twine &NameStr,
4204 Instruction *InsertBefore);
4205
4206 /// Create a new switch instruction, specifying a
4207 /// default destination. The number of additional handlers can be specified
4208 /// here to make memory allocation more efficient.
4209 /// This constructor also autoinserts at the end of the specified BasicBlock.
4210 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4211 unsigned NumHandlers, const Twine &NameStr,
4212 BasicBlock *InsertAtEnd);
4213
4214 // allocate space for exactly zero operands
4215 void *operator new(size_t s) { return User::operator new(s); }
4216
4217 void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved);
4218 void growOperands(unsigned Size);
4219
4220protected:
4221 // Note: Instruction needs to be a friend here to call cloneImpl.
4222 friend class Instruction;
4223
4224 CatchSwitchInst *cloneImpl() const;
4225
4226public:
4227 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4228 unsigned NumHandlers,
4229 const Twine &NameStr = "",
4230 Instruction *InsertBefore = nullptr) {
4231 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4232 InsertBefore);
4233 }
4234
4235 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4236 unsigned NumHandlers, const Twine &NameStr,
4237 BasicBlock *InsertAtEnd) {
4238 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4239 InsertAtEnd);
4240 }
4241
4242 /// Provide fast operand accessors
4243 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4244
4245 // Accessor Methods for CatchSwitch stmt
4246 Value *getParentPad() const { return getOperand(0); }
4247 void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); }
4248
4249 // Accessor Methods for CatchSwitch stmt
4250 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4251 bool unwindsToCaller() const { return !hasUnwindDest(); }
4252 BasicBlock *getUnwindDest() const {
4253 if (hasUnwindDest())
4254 return cast<BasicBlock>(getOperand(1));
4255 return nullptr;
4256 }
4257 void setUnwindDest(BasicBlock *UnwindDest) {
4258 assert(UnwindDest);
4259 assert(hasUnwindDest());
4260 setOperand(1, UnwindDest);
4261 }
4262
4263 /// return the number of 'handlers' in this catchswitch
4264 /// instruction, except the default handler
4265 unsigned getNumHandlers() const {
4266 if (hasUnwindDest())
4267 return getNumOperands() - 2;
4268 return getNumOperands() - 1;
4269 }
4270
4271private:
4272 static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); }
4273 static const BasicBlock *handler_helper(const Value *V) {
4274 return cast<BasicBlock>(V);
4275 }
4276
4277public:
4278 using DerefFnTy = BasicBlock *(*)(Value *);
4279 using handler_iterator = mapped_iterator<op_iterator, DerefFnTy>;
4280 using handler_range = iterator_range<handler_iterator>;
4281 using ConstDerefFnTy = const BasicBlock *(*)(const Value *);
4282 using const_handler_iterator =
4283 mapped_iterator<const_op_iterator, ConstDerefFnTy>;
4284 using const_handler_range = iterator_range<const_handler_iterator>;
4285
4286 /// Returns an iterator that points to the first handler in CatchSwitchInst.
4287 handler_iterator handler_begin() {
4288 op_iterator It = op_begin() + 1;
4289 if (hasUnwindDest())
4290 ++It;
4291 return handler_iterator(It, DerefFnTy(handler_helper));
4292 }
4293
4294 /// Returns an iterator that points to the first handler in the
4295 /// CatchSwitchInst.
4296 const_handler_iterator handler_begin() const {
4297 const_op_iterator It = op_begin() + 1;
4298 if (hasUnwindDest())
4299 ++It;
4300 return const_handler_iterator(It, ConstDerefFnTy(handler_helper));
4301 }
4302
4303 /// Returns a read-only iterator that points one past the last
4304 /// handler in the CatchSwitchInst.
4305 handler_iterator handler_end() {
4306 return handler_iterator(op_end(), DerefFnTy(handler_helper));
4307 }
4308
4309 /// Returns an iterator that points one past the last handler in the
4310 /// CatchSwitchInst.
4311 const_handler_iterator handler_end() const {
4312 return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper));
4313 }
4314
4315 /// iteration adapter for range-for loops.
4316 handler_range handlers() {
4317 return make_range(handler_begin(), handler_end());
4318 }
4319
4320 /// iteration adapter for range-for loops.
4321 const_handler_range handlers() const {
4322 return make_range(handler_begin(), handler_end());
4323 }
4324
4325 /// Add an entry to the switch instruction...
4326 /// Note:
4327 /// This action invalidates handler_end(). Old handler_end() iterator will
4328 /// point to the added handler.
4329 void addHandler(BasicBlock *Dest);
4330
4331 void removeHandler(handler_iterator HI);
4332
4333 unsigned getNumSuccessors() const { return getNumOperands() - 1; }
4334 BasicBlock *getSuccessor(unsigned Idx) const {
4335 assert(Idx < getNumSuccessors() &&
4336 "Successor # out of range for catchswitch!");
4337 return cast<BasicBlock>(getOperand(Idx + 1));
4338 }
4339 void setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
4340 assert(Idx < getNumSuccessors() &&
4341 "Successor # out of range for catchswitch!");
4342 setOperand(Idx + 1, NewSucc);
4343 }
4344
4345 // Methods for support type inquiry through isa, cast, and dyn_cast:
4346 static bool classof(const Instruction *I) {
4347 return I->getOpcode() == Instruction::CatchSwitch;
4348 }
4349 static bool classof(const Value *V) {
4350 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4351 }
4352};
4353
4354template <>
4355struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {};
4356
4357DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)
4358
4359//===----------------------------------------------------------------------===//
4360// CleanupPadInst Class
4361//===----------------------------------------------------------------------===//
4362class CleanupPadInst : public FuncletPadInst {
4363private:
4364 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4365 unsigned Values, const Twine &NameStr,
4366 Instruction *InsertBefore)
4367 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4368 NameStr, InsertBefore) {}
4369 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4370 unsigned Values, const Twine &NameStr,
4371 BasicBlock *InsertAtEnd)
4372 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4373 NameStr, InsertAtEnd) {}
4374
4375public:
4376 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None,
4377 const Twine &NameStr = "",
4378 Instruction *InsertBefore = nullptr) {
4379 unsigned Values = 1 + Args.size();
4380 return new (Values)
4381 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore);
4382 }
4383
4384 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args,
4385 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4386 unsigned Values = 1 + Args.size();
4387 return new (Values)
4388 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd);
4389 }
4390
4391 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4392 static bool classof(const Instruction *I) {
4393 return I->getOpcode() == Instruction::CleanupPad;
4394 }
4395 static bool classof(const Value *V) {
4396 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4397 }
4398};
4399
4400//===----------------------------------------------------------------------===//
4401// CatchPadInst Class
4402//===----------------------------------------------------------------------===//
4403class CatchPadInst : public FuncletPadInst {
4404private:
4405 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4406 unsigned Values, const Twine &NameStr,
4407 Instruction *InsertBefore)
4408 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4409 NameStr, InsertBefore) {}
4410 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4411 unsigned Values, const Twine &NameStr,
4412 BasicBlock *InsertAtEnd)
4413 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4414 NameStr, InsertAtEnd) {}
4415
4416public:
4417 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4418 const Twine &NameStr = "",
4419 Instruction *InsertBefore = nullptr) {
4420 unsigned Values = 1 + Args.size();
4421 return new (Values)
4422 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore);
4423 }
4424
4425 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4426 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4427 unsigned Values = 1 + Args.size();
4428 return new (Values)
4429 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd);
4430 }
4431
4432 /// Convenience accessors
4433 CatchSwitchInst *getCatchSwitch() const {
4434 return cast<CatchSwitchInst>(Op<-1>());
4435 }
4436 void setCatchSwitch(Value *CatchSwitch) {
4437 assert(CatchSwitch);
4438 Op<-1>() = CatchSwitch;
4439 }
4440
4441 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4442 static bool classof(const Instruction *I) {
4443 return I->getOpcode() == Instruction::CatchPad;
4444 }
4445 static bool classof(const Value *V) {
4446 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4447 }
4448};
4449
4450//===----------------------------------------------------------------------===//
4451// CatchReturnInst Class
4452//===----------------------------------------------------------------------===//
4453
4454class CatchReturnInst : public TerminatorInst {
4455 CatchReturnInst(const CatchReturnInst &RI);
4456 CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore);
4457 CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd);
4458
4459 void init(Value *CatchPad, BasicBlock *BB);
4460
4461protected:
4462 // Note: Instruction needs to be a friend here to call cloneImpl.
4463 friend class Instruction;
4464
4465 CatchReturnInst *cloneImpl() const;
4466
4467public:
4468 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4469 Instruction *InsertBefore = nullptr) {
4470 assert(CatchPad);
4471 assert(BB);
4472 return new (2) CatchReturnInst(CatchPad, BB, InsertBefore);
4473 }
4474
4475 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4476 BasicBlock *InsertAtEnd) {
4477 assert(CatchPad);
4478 assert(BB);
4479 return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd);
4480 }
4481
4482 /// Provide fast operand accessors
4483 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4484
4485 /// Convenience accessors.
4486 CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); }
4487 void setCatchPad(CatchPadInst *CatchPad) {
4488 assert(CatchPad);
4489 Op<0>() = CatchPad;
4490 }
4491
4492 BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); }
4493 void setSuccessor(BasicBlock *NewSucc) {
4494 assert(NewSucc);
4495 Op<1>() = NewSucc;
4496 }
4497 unsigned getNumSuccessors() const { return 1; }
4498
4499 /// Get the parentPad of this catchret's catchpad's catchswitch.
4500 /// The successor block is implicitly a member of this funclet.
4501 Value *getCatchSwitchParentPad() const {
4502 return getCatchPad()->getCatchSwitch()->getParentPad();
4503 }
4504
4505 // Methods for support type inquiry through isa, cast, and dyn_cast:
4506 static bool classof(const Instruction *I) {
4507 return (I->getOpcode() == Instruction::CatchRet);
4508 }
4509 static bool classof(const Value *V) {
4510 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4511 }
4512
4513private:
4514 friend TerminatorInst;
4515
4516 BasicBlock *getSuccessor(unsigned Idx) const {
4517 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
4518 return getSuccessor();
4519 }
4520
4521 void setSuccessor(unsigned Idx, BasicBlock *B) {
4522 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
4523 setSuccessor(B);
4524 }
4525};
4526
4527template <>
4528struct OperandTraits<CatchReturnInst>
4529 : public FixedNumOperandTraits<CatchReturnInst, 2> {};
4530
4531DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)
4532
4533//===----------------------------------------------------------------------===//
4534// CleanupReturnInst Class
4535//===----------------------------------------------------------------------===//
4536
4537class CleanupReturnInst : public TerminatorInst {
4538private:
4539 CleanupReturnInst(const CleanupReturnInst &RI);
4540 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4541 Instruction *InsertBefore = nullptr);
4542 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4543 BasicBlock *InsertAtEnd);
4544
4545 void init(Value *CleanupPad, BasicBlock *UnwindBB);
4546
4547protected:
4548 // Note: Instruction needs to be a friend here to call cloneImpl.
4549 friend class Instruction;
4550
4551 CleanupReturnInst *cloneImpl() const;
4552
4553public:
4554 static CleanupReturnInst *Create(Value *CleanupPad,
4555 BasicBlock *UnwindBB = nullptr,
4556 Instruction *InsertBefore = nullptr) {
4557 assert(CleanupPad);
4558 unsigned Values = 1;
4559 if (UnwindBB)
4560 ++Values;
4561 return new (Values)
4562 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore);
4563 }
4564
4565 static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB,
4566 BasicBlock *InsertAtEnd) {
4567 assert(CleanupPad);
4568 unsigned Values = 1;
4569 if (UnwindBB)
4570 ++Values;
4571 return new (Values)
4572 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd);
4573 }
4574
4575 /// Provide fast operand accessors
4576 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4577
4578 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4579 bool unwindsToCaller() const { return !hasUnwindDest(); }
4580
4581 /// Convenience accessor.
4582 CleanupPadInst *getCleanupPad() const {
4583 return cast<CleanupPadInst>(Op<0>());
4584 }
4585 void setCleanupPad(CleanupPadInst *CleanupPad) {
4586 assert(CleanupPad);
4587 Op<0>() = CleanupPad;
4588 }
4589
4590 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4591
4592 BasicBlock *getUnwindDest() const {
4593 return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr;
4594 }
4595 void setUnwindDest(BasicBlock *NewDest) {
4596 assert(NewDest);
4597 assert(hasUnwindDest());
4598 Op<1>() = NewDest;
4599 }
4600
4601 // Methods for support type inquiry through isa, cast, and dyn_cast:
4602 static bool classof(const Instruction *I) {
4603 return (I->getOpcode() == Instruction::CleanupRet);
4604 }
4605 static bool classof(const Value *V) {
4606 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4607 }
4608
4609private:
4610 friend TerminatorInst;
4611
4612 BasicBlock *getSuccessor(unsigned Idx) const {
4613 assert(Idx == 0);
4614 return getUnwindDest();
4615 }
4616
4617 void setSuccessor(unsigned Idx, BasicBlock *B) {
4618 assert(Idx == 0);
4619 setUnwindDest(B);
4620 }
4621
4622 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4623 // method so that subclasses cannot accidentally use it.
4624 void setInstructionSubclassData(unsigned short D) {
4625 Instruction::setInstructionSubclassData(D);
4626 }
4627};
4628
4629template <>
4630struct OperandTraits<CleanupReturnInst>
4631 : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {};
4632
4633DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)
4634
4635//===----------------------------------------------------------------------===//
4636// UnreachableInst Class
4637//===----------------------------------------------------------------------===//
4638
4639//===---------------------------------------------------------------------------
4640/// This function has undefined behavior. In particular, the
4641/// presence of this instruction indicates some higher level knowledge that the
4642/// end of the block cannot be reached.
4643///
4644class UnreachableInst : public TerminatorInst {
4645protected:
4646 // Note: Instruction needs to be a friend here to call cloneImpl.
4647 friend class Instruction;
4648
4649 UnreachableInst *cloneImpl() const;
4650
4651public:
4652 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
4653 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
4654
4655 // allocate space for exactly zero operands
4656 void *operator new(size_t s) {
4657 return User::operator new(s, 0);
4658 }
4659
4660 unsigned getNumSuccessors() const { return 0; }
4661
4662 // Methods for support type inquiry through isa, cast, and dyn_cast:
4663 static bool classof(const Instruction *I) {
4664 return I->getOpcode() == Instruction::Unreachable;
4665 }
4666 static bool classof(const Value *V) {
4667 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4668 }
4669
4670private:
4671 friend TerminatorInst;
4672
4673 BasicBlock *getSuccessor(unsigned idx) const {
4674 llvm_unreachable("UnreachableInst has no successors!");
4675 }
4676
4677 void setSuccessor(unsigned idx, BasicBlock *B) {
4678 llvm_unreachable("UnreachableInst has no successors!");
4679 }
4680};
4681
4682//===----------------------------------------------------------------------===//
4683// TruncInst Class
4684//===----------------------------------------------------------------------===//
4685
4686/// This class represents a truncation of integer types.
4687class TruncInst : public CastInst {
4688protected:
4689 // Note: Instruction needs to be a friend here to call cloneImpl.
4690 friend class Instruction;
4691
4692 /// Clone an identical TruncInst
4693 TruncInst *cloneImpl() const;
4694
4695public:
4696 /// Constructor with insert-before-instruction semantics
4697 TruncInst(
4698 Value *S, ///< The value to be truncated
4699 Type *Ty, ///< The (smaller) type to truncate to
4700 const Twine &NameStr = "", ///< A name for the new instruction
4701 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4702 );
4703
4704 /// Constructor with insert-at-end-of-block semantics
4705 TruncInst(
4706 Value *S, ///< The value to be truncated
4707 Type *Ty, ///< The (smaller) type to truncate to
4708 const Twine &NameStr, ///< A name for the new instruction
4709 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4710 );
4711
4712 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4713 static bool classof(const Instruction *I) {
4714 return I->getOpcode() == Trunc;
4715 }
4716 static bool classof(const Value *V) {
4717 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4718 }
4719};
4720
4721//===----------------------------------------------------------------------===//
4722// ZExtInst Class
4723//===----------------------------------------------------------------------===//
4724
4725/// This class represents zero extension of integer types.
4726class ZExtInst : public CastInst {
4727protected:
4728 // Note: Instruction needs to be a friend here to call cloneImpl.
4729 friend class Instruction;
4730
4731 /// Clone an identical ZExtInst
4732 ZExtInst *cloneImpl() const;
4733
4734public:
4735 /// Constructor with insert-before-instruction semantics
4736 ZExtInst(
4737 Value *S, ///< The value to be zero extended
4738 Type *Ty, ///< The type to zero extend to
4739 const Twine &NameStr = "", ///< A name for the new instruction
4740 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4741 );
4742
4743 /// Constructor with insert-at-end semantics.
4744 ZExtInst(
4745 Value *S, ///< The value to be zero extended
4746 Type *Ty, ///< The type to zero extend to
4747 const Twine &NameStr, ///< A name for the new instruction
4748 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4749 );
4750
4751 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4752 static bool classof(const Instruction *I) {
4753 return I->getOpcode() == ZExt;
4754 }
4755 static bool classof(const Value *V) {
4756 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4757 }
4758};
4759
4760//===----------------------------------------------------------------------===//
4761// SExtInst Class
4762//===----------------------------------------------------------------------===//
4763
4764/// This class represents a sign extension of integer types.
4765class SExtInst : public CastInst {
4766protected:
4767 // Note: Instruction needs to be a friend here to call cloneImpl.
4768 friend class Instruction;
4769
4770 /// Clone an identical SExtInst
4771 SExtInst *cloneImpl() const;
4772
4773public:
4774 /// Constructor with insert-before-instruction semantics
4775 SExtInst(
4776 Value *S, ///< The value to be sign extended
4777 Type *Ty, ///< The type to sign extend to
4778 const Twine &NameStr = "", ///< A name for the new instruction
4779 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4780 );
4781
4782 /// Constructor with insert-at-end-of-block semantics
4783 SExtInst(
4784 Value *S, ///< The value to be sign extended
4785 Type *Ty, ///< The type to sign extend to
4786 const Twine &NameStr, ///< A name for the new instruction
4787 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4788 );
4789
4790 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4791 static bool classof(const Instruction *I) {
4792 return I->getOpcode() == SExt;
4793 }
4794 static bool classof(const Value *V) {
4795 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4796 }
4797};
4798
4799//===----------------------------------------------------------------------===//
4800// FPTruncInst Class
4801//===----------------------------------------------------------------------===//
4802
4803/// This class represents a truncation of floating point types.
4804class FPTruncInst : public CastInst {
4805protected:
4806 // Note: Instruction needs to be a friend here to call cloneImpl.
4807 friend class Instruction;
4808
4809 /// Clone an identical FPTruncInst
4810 FPTruncInst *cloneImpl() const;
4811
4812public:
4813 /// Constructor with insert-before-instruction semantics
4814 FPTruncInst(
4815 Value *S, ///< The value to be truncated
4816 Type *Ty, ///< The type to truncate to
4817 const Twine &NameStr = "", ///< A name for the new instruction
4818 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4819 );
4820
4821 /// Constructor with insert-before-instruction semantics
4822 FPTruncInst(
4823 Value *S, ///< The value to be truncated
4824 Type *Ty, ///< The type to truncate to
4825 const Twine &NameStr, ///< A name for the new instruction
4826 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4827 );
4828
4829 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4830 static bool classof(const Instruction *I) {
4831 return I->getOpcode() == FPTrunc;
4832 }
4833 static bool classof(const Value *V) {
4834 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4835 }
4836};
4837
4838//===----------------------------------------------------------------------===//
4839// FPExtInst Class
4840//===----------------------------------------------------------------------===//
4841
4842/// This class represents an extension of floating point types.
4843class FPExtInst : public CastInst {
4844protected:
4845 // Note: Instruction needs to be a friend here to call cloneImpl.
4846 friend class Instruction;
4847
4848 /// Clone an identical FPExtInst
4849 FPExtInst *cloneImpl() const;
4850
4851public:
4852 /// Constructor with insert-before-instruction semantics
4853 FPExtInst(
4854 Value *S, ///< The value to be extended
4855 Type *Ty, ///< The type to extend to
4856 const Twine &NameStr = "", ///< A name for the new instruction
4857 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4858 );
4859
4860 /// Constructor with insert-at-end-of-block semantics
4861 FPExtInst(
4862 Value *S, ///< The value to be extended
4863 Type *Ty, ///< The type to extend to
4864 const Twine &NameStr, ///< A name for the new instruction
4865 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4866 );
4867
4868 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4869 static bool classof(const Instruction *I) {
4870 return I->getOpcode() == FPExt;
4871 }
4872 static bool classof(const Value *V) {
4873 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4874 }
4875};
4876
4877//===----------------------------------------------------------------------===//
4878// UIToFPInst Class
4879//===----------------------------------------------------------------------===//
4880
4881/// This class represents a cast unsigned integer to floating point.
4882class UIToFPInst : public CastInst {
4883protected:
4884 // Note: Instruction needs to be a friend here to call cloneImpl.
4885 friend class Instruction;
4886
4887 /// Clone an identical UIToFPInst
4888 UIToFPInst *cloneImpl() const;
4889
4890public:
4891 /// Constructor with insert-before-instruction semantics
4892 UIToFPInst(
4893 Value *S, ///< The value to be converted
4894 Type *Ty, ///< The type to convert to
4895 const Twine &NameStr = "", ///< A name for the new instruction
4896 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4897 );
4898
4899 /// Constructor with insert-at-end-of-block semantics
4900 UIToFPInst(
4901 Value *S, ///< The value to be converted
4902 Type *Ty, ///< The type to convert to
4903 const Twine &NameStr, ///< A name for the new instruction
4904 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4905 );
4906
4907 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4908 static bool classof(const Instruction *I) {
4909 return I->getOpcode() == UIToFP;
4910 }
4911 static bool classof(const Value *V) {
4912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4913 }
4914};
4915
4916//===----------------------------------------------------------------------===//
4917// SIToFPInst Class
4918//===----------------------------------------------------------------------===//
4919
4920/// This class represents a cast from signed integer to floating point.
4921class SIToFPInst : public CastInst {
4922protected:
4923 // Note: Instruction needs to be a friend here to call cloneImpl.
4924 friend class Instruction;
4925
4926 /// Clone an identical SIToFPInst
4927 SIToFPInst *cloneImpl() const;
4928
4929public:
4930 /// Constructor with insert-before-instruction semantics
4931 SIToFPInst(
4932 Value *S, ///< The value to be converted
4933 Type *Ty, ///< The type to convert to
4934 const Twine &NameStr = "", ///< A name for the new instruction
4935 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4936 );
4937
4938 /// Constructor with insert-at-end-of-block semantics
4939 SIToFPInst(
4940 Value *S, ///< The value to be converted
4941 Type *Ty, ///< The type to convert to
4942 const Twine &NameStr, ///< A name for the new instruction
4943 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4944 );
4945
4946 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4947 static bool classof(const Instruction *I) {
4948 return I->getOpcode() == SIToFP;
4949 }
4950 static bool classof(const Value *V) {
4951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4952 }
4953};
4954
4955//===----------------------------------------------------------------------===//
4956// FPToUIInst Class
4957//===----------------------------------------------------------------------===//
4958
4959/// This class represents a cast from floating point to unsigned integer
4960class FPToUIInst : public CastInst {
4961protected:
4962 // Note: Instruction needs to be a friend here to call cloneImpl.
4963 friend class Instruction;
4964
4965 /// Clone an identical FPToUIInst
4966 FPToUIInst *cloneImpl() const;
4967
4968public:
4969 /// Constructor with insert-before-instruction semantics
4970 FPToUIInst(
4971 Value *S, ///< The value to be converted
4972 Type *Ty, ///< The type to convert to
4973 const Twine &NameStr = "", ///< A name for the new instruction
4974 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4975 );
4976
4977 /// Constructor with insert-at-end-of-block semantics
4978 FPToUIInst(
4979 Value *S, ///< The value to be converted
4980 Type *Ty, ///< The type to convert to
4981 const Twine &NameStr, ///< A name for the new instruction
4982 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
4983 );
4984
4985 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4986 static bool classof(const Instruction *I) {
4987 return I->getOpcode() == FPToUI;
4988 }
4989 static bool classof(const Value *V) {
4990 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4991 }
4992};
4993
4994//===----------------------------------------------------------------------===//
4995// FPToSIInst Class
4996//===----------------------------------------------------------------------===//
4997
4998/// This class represents a cast from floating point to signed integer.
4999class FPToSIInst : public CastInst {
5000protected:
5001 // Note: Instruction needs to be a friend here to call cloneImpl.
5002 friend class Instruction;
5003
5004 /// Clone an identical FPToSIInst
5005 FPToSIInst *cloneImpl() const;
5006
5007public:
5008 /// Constructor with insert-before-instruction semantics
5009 FPToSIInst(
5010 Value *S, ///< The value to be converted
5011 Type *Ty, ///< The type to convert to
5012 const Twine &NameStr = "", ///< A name for the new instruction
5013 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5014 );
5015
5016 /// Constructor with insert-at-end-of-block semantics
5017 FPToSIInst(
5018 Value *S, ///< The value to be converted
5019 Type *Ty, ///< The type to convert to
5020 const Twine &NameStr, ///< A name for the new instruction
5021 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5022 );
5023
5024 /// Methods for support type inquiry through isa, cast, and dyn_cast:
5025 static bool classof(const Instruction *I) {
5026 return I->getOpcode() == FPToSI;
5027 }
5028 static bool classof(const Value *V) {
5029 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5030 }
5031};
5032
5033//===----------------------------------------------------------------------===//
5034// IntToPtrInst Class
5035//===----------------------------------------------------------------------===//
5036
5037/// This class represents a cast from an integer to a pointer.
5038class IntToPtrInst : public CastInst {
5039public:
5040 // Note: Instruction needs to be a friend here to call cloneImpl.
5041 friend class Instruction;
5042
5043 /// Constructor with insert-before-instruction semantics
5044 IntToPtrInst(
5045 Value *S, ///< The value to be converted
5046 Type *Ty, ///< The type to convert to
5047 const Twine &NameStr = "", ///< A name for the new instruction
5048 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5049 );
5050
5051 /// Constructor with insert-at-end-of-block semantics
5052 IntToPtrInst(
5053 Value *S, ///< The value to be converted
5054 Type *Ty, ///< The type to convert to
5055 const Twine &NameStr, ///< A name for the new instruction
5056 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5057 );
5058
5059 /// Clone an identical IntToPtrInst.
5060 IntToPtrInst *cloneImpl() const;
5061
5062 /// Returns the address space of this instruction's pointer type.
5063 unsigned getAddressSpace() const {
5064 return getType()->getPointerAddressSpace();
5065 }
5066
5067 // Methods for support type inquiry through isa, cast, and dyn_cast:
5068 static bool classof(const Instruction *I) {
5069 return I->getOpcode() == IntToPtr;
5070 }
5071 static bool classof(const Value *V) {
5072 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5073 }
5074};
5075
5076//===----------------------------------------------------------------------===//
5077// PtrToIntInst Class
5078//===----------------------------------------------------------------------===//
5079
5080/// This class represents a cast from a pointer to an integer.
5081class PtrToIntInst : public CastInst {
5082protected:
5083 // Note: Instruction needs to be a friend here to call cloneImpl.
5084 friend class Instruction;
5085
5086 /// Clone an identical PtrToIntInst.
5087 PtrToIntInst *cloneImpl() const;
5088
5089public:
5090 /// Constructor with insert-before-instruction semantics
5091 PtrToIntInst(
5092 Value *S, ///< The value to be converted
5093 Type *Ty, ///< The type to convert to
5094 const Twine &NameStr = "", ///< A name for the new instruction
5095 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5096 );
5097
5098 /// Constructor with insert-at-end-of-block semantics
5099 PtrToIntInst(
5100 Value *S, ///< The value to be converted
5101 Type *Ty, ///< The type to convert to
5102 const Twine &NameStr, ///< A name for the new instruction
5103 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5104 );
5105
5106 /// Gets the pointer operand.
5107 Value *getPointerOperand() { return getOperand(0); }
5108 /// Gets the pointer operand.
5109 const Value *getPointerOperand() const { return getOperand(0); }
5110 /// Gets the operand index of the pointer operand.
5111 static unsigned getPointerOperandIndex() { return 0U; }
5112
5113 /// Returns the address space of the pointer operand.
5114 unsigned getPointerAddressSpace() const {
5115 return getPointerOperand()->getType()->getPointerAddressSpace();
5116 }
5117
5118 // Methods for support type inquiry through isa, cast, and dyn_cast:
5119 static bool classof(const Instruction *I) {
5120 return I->getOpcode() == PtrToInt;
5121 }
5122 static bool classof(const Value *V) {
5123 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5124 }
5125};
5126
5127//===----------------------------------------------------------------------===//
5128// BitCastInst Class
5129//===----------------------------------------------------------------------===//
5130
5131/// This class represents a no-op cast from one type to another.
5132class BitCastInst : public CastInst {
5133protected:
5134 // Note: Instruction needs to be a friend here to call cloneImpl.
5135 friend class Instruction;
5136
5137 /// Clone an identical BitCastInst.
5138 BitCastInst *cloneImpl() const;
5139
5140public:
5141 /// Constructor with insert-before-instruction semantics
5142 BitCastInst(
5143 Value *S, ///< The value to be casted
5144 Type *Ty, ///< The type to casted to
5145 const Twine &NameStr = "", ///< A name for the new instruction
5146 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5147 );
5148
5149 /// Constructor with insert-at-end-of-block semantics
5150 BitCastInst(
5151 Value *S, ///< The value to be casted
5152 Type *Ty, ///< The type to casted to
5153 const Twine &NameStr, ///< A name for the new instruction
5154 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5155 );
5156
5157 // Methods for support type inquiry through isa, cast, and dyn_cast:
5158 static bool classof(const Instruction *I) {
5159 return I->getOpcode() == BitCast;
5160 }
5161 static bool classof(const Value *V) {
5162 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5163 }
5164};
5165
5166//===----------------------------------------------------------------------===//
5167// AddrSpaceCastInst Class
5168//===----------------------------------------------------------------------===//
5169
5170/// This class represents a conversion between pointers from one address space
5171/// to another.
5172class AddrSpaceCastInst : public CastInst {
5173protected:
5174 // Note: Instruction needs to be a friend here to call cloneImpl.
5175 friend class Instruction;
5176
5177 /// Clone an identical AddrSpaceCastInst.
5178 AddrSpaceCastInst *cloneImpl() const;
5179
5180public:
5181 /// Constructor with insert-before-instruction semantics
5182 AddrSpaceCastInst(
5183 Value *S, ///< The value to be casted
5184 Type *Ty, ///< The type to casted to
5185 const Twine &NameStr = "", ///< A name for the new instruction
5186 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5187 );
5188
5189 /// Constructor with insert-at-end-of-block semantics
5190 AddrSpaceCastInst(
5191 Value *S, ///< The value to be casted
5192 Type *Ty, ///< The type to casted to
5193 const Twine &NameStr, ///< A name for the new instruction
5194 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5195 );
5196
5197 // Methods for support type inquiry through isa, cast, and dyn_cast:
5198 static bool classof(const Instruction *I) {
5199 return I->getOpcode() == AddrSpaceCast;
5200 }
5201 static bool classof(const Value *V) {
5202 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5203 }
5204
5205 /// Gets the pointer operand.
5206 Value *getPointerOperand() {
5207 return getOperand(0);
5208 }
5209
5210 /// Gets the pointer operand.
5211 const Value *getPointerOperand() const {
5212 return getOperand(0);
5213 }
5214
5215 /// Gets the operand index of the pointer operand.
5216 static unsigned getPointerOperandIndex() {
5217 return 0U;
5218 }
5219
5220 /// Returns the address space of the pointer operand.
5221 unsigned getSrcAddressSpace() const {
5222 return getPointerOperand()->getType()->getPointerAddressSpace();
5223 }
5224
5225 /// Returns the address space of the result.
5226 unsigned getDestAddressSpace() const {
5227 return getType()->getPointerAddressSpace();
5228 }
5229};
5230
5231/// A helper function that returns the pointer operand of a load or store
5232/// instruction. Returns nullptr if not load or store.
5233inline Value *getLoadStorePointerOperand(Value *V) {
5234 if (auto *Load = dyn_cast<LoadInst>(V))
5235 return Load->getPointerOperand();
5236 if (auto *Store = dyn_cast<StoreInst>(V))
5237 return Store->getPointerOperand();
5238 return nullptr;
5239}
5240
5241/// A helper function that returns the pointer operand of a load, store
5242/// or GEP instruction. Returns nullptr if not load, store, or GEP.
5243inline Value *getPointerOperand(Value *V) {
5244 if (auto *Ptr = getLoadStorePointerOperand(V))
5245 return Ptr;
5246 if (auto *Gep = dyn_cast<GetElementPtrInst>(V))
5247 return Gep->getPointerOperand();
5248 return nullptr;
5249}
5250
5251} // end namespace llvm
5252
5253#endif // LLVM_IR_INSTRUCTIONS_H