Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3716 lines
140 KiB

  1. //===-- 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. #ifndef LLVM_IR_INSTRUCTIONS_H
  16. #define LLVM_IR_INSTRUCTIONS_H
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/IR/Attributes.h"
  20. #include "llvm/IR/CallingConv.h"
  21. #include "llvm/IR/DerivedTypes.h"
  22. #include "llvm/IR/InstrTypes.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include "llvm/Support/IntegersSubset.h"
  25. #include "llvm/Support/IntegersSubsetMapping.h"
  26. #include <iterator>
  27. namespace llvm {
  28. class APInt;
  29. class ConstantInt;
  30. class ConstantRange;
  31. class DataLayout;
  32. class LLVMContext;
  33. enum AtomicOrdering {
  34. NotAtomic = 0,
  35. Unordered = 1,
  36. Monotonic = 2,
  37. // Consume = 3, // Not specified yet.
  38. Acquire = 4,
  39. Release = 5,
  40. AcquireRelease = 6,
  41. SequentiallyConsistent = 7
  42. };
  43. enum SynchronizationScope {
  44. SingleThread = 0,
  45. CrossThread = 1
  46. };
  47. //===----------------------------------------------------------------------===//
  48. // AllocaInst Class
  49. //===----------------------------------------------------------------------===//
  50. /// AllocaInst - an instruction to allocate memory on the stack
  51. ///
  52. class AllocaInst : public UnaryInstruction {
  53. protected:
  54. virtual AllocaInst *clone_impl() const;
  55. public:
  56. explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
  57. const Twine &Name = "", Instruction *InsertBefore = 0);
  58. AllocaInst(Type *Ty, Value *ArraySize,
  59. const Twine &Name, BasicBlock *InsertAtEnd);
  60. AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
  61. AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
  62. AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
  63. const Twine &Name = "", Instruction *InsertBefore = 0);
  64. AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
  65. const Twine &Name, BasicBlock *InsertAtEnd);
  66. // Out of line virtual method, so the vtable, etc. has a home.
  67. virtual ~AllocaInst();
  68. /// isArrayAllocation - Return true if there is an allocation size parameter
  69. /// to the allocation instruction that is not 1.
  70. ///
  71. bool isArrayAllocation() const;
  72. /// getArraySize - Get the number of elements allocated. For a simple
  73. /// allocation of a single element, this will return a constant 1 value.
  74. ///
  75. const Value *getArraySize() const { return getOperand(0); }
  76. Value *getArraySize() { return getOperand(0); }
  77. /// getType - Overload to return most specific pointer type
  78. ///
  79. PointerType *getType() const {
  80. return cast<PointerType>(Instruction::getType());
  81. }
  82. /// getAllocatedType - Return the type that is being allocated by the
  83. /// instruction.
  84. ///
  85. Type *getAllocatedType() const;
  86. /// getAlignment - Return the alignment of the memory that is being allocated
  87. /// by the instruction.
  88. ///
  89. unsigned getAlignment() const {
  90. return (1u << getSubclassDataFromInstruction()) >> 1;
  91. }
  92. void setAlignment(unsigned Align);
  93. /// isStaticAlloca - Return true if this alloca is in the entry block of the
  94. /// function and is a constant size. If so, the code generator will fold it
  95. /// into the prolog/epilog code, so it is basically free.
  96. bool isStaticAlloca() const;
  97. // Methods for support type inquiry through isa, cast, and dyn_cast:
  98. static inline bool classof(const Instruction *I) {
  99. return (I->getOpcode() == Instruction::Alloca);
  100. }
  101. static inline bool classof(const Value *V) {
  102. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  103. }
  104. private:
  105. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  106. // method so that subclasses cannot accidentally use it.
  107. void setInstructionSubclassData(unsigned short D) {
  108. Instruction::setInstructionSubclassData(D);
  109. }
  110. };
  111. //===----------------------------------------------------------------------===//
  112. // LoadInst Class
  113. //===----------------------------------------------------------------------===//
  114. /// LoadInst - an instruction for reading from memory. This uses the
  115. /// SubclassData field in Value to store whether or not the load is volatile.
  116. ///
  117. class LoadInst : public UnaryInstruction {
  118. void AssertOK();
  119. protected:
  120. virtual LoadInst *clone_impl() const;
  121. public:
  122. LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
  123. LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
  124. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
  125. Instruction *InsertBefore = 0);
  126. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
  127. BasicBlock *InsertAtEnd);
  128. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
  129. unsigned Align, Instruction *InsertBefore = 0);
  130. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
  131. unsigned Align, BasicBlock *InsertAtEnd);
  132. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
  133. unsigned Align, AtomicOrdering Order,
  134. SynchronizationScope SynchScope = CrossThread,
  135. Instruction *InsertBefore = 0);
  136. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
  137. unsigned Align, AtomicOrdering Order,
  138. SynchronizationScope SynchScope,
  139. BasicBlock *InsertAtEnd);
  140. LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
  141. LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
  142. explicit LoadInst(Value *Ptr, const char *NameStr = 0,
  143. bool isVolatile = false, Instruction *InsertBefore = 0);
  144. LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
  145. BasicBlock *InsertAtEnd);
  146. /// isVolatile - Return true if this is a load from a volatile memory
  147. /// location.
  148. ///
  149. bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
  150. /// setVolatile - Specify whether this is a volatile load or not.
  151. ///
  152. void setVolatile(bool V) {
  153. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  154. (V ? 1 : 0));
  155. }
  156. /// getAlignment - Return the alignment of the access that is being performed
  157. ///
  158. unsigned getAlignment() const {
  159. return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
  160. }
  161. void setAlignment(unsigned Align);
  162. /// Returns the ordering effect of this fence.
  163. AtomicOrdering getOrdering() const {
  164. return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
  165. }
  166. /// Set the ordering constraint on this load. May not be Release or
  167. /// AcquireRelease.
  168. void setOrdering(AtomicOrdering Ordering) {
  169. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
  170. (Ordering << 7));
  171. }
  172. SynchronizationScope getSynchScope() const {
  173. return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
  174. }
  175. /// Specify whether this load is ordered with respect to all
  176. /// concurrently executing threads, or only with respect to signal handlers
  177. /// executing in the same thread.
  178. void setSynchScope(SynchronizationScope xthread) {
  179. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
  180. (xthread << 6));
  181. }
  182. bool isAtomic() const { return getOrdering() != NotAtomic; }
  183. void setAtomic(AtomicOrdering Ordering,
  184. SynchronizationScope SynchScope = CrossThread) {
  185. setOrdering(Ordering);
  186. setSynchScope(SynchScope);
  187. }
  188. bool isSimple() const { return !isAtomic() && !isVolatile(); }
  189. bool isUnordered() const {
  190. return getOrdering() <= Unordered && !isVolatile();
  191. }
  192. Value *getPointerOperand() { return getOperand(0); }
  193. const Value *getPointerOperand() const { return getOperand(0); }
  194. static unsigned getPointerOperandIndex() { return 0U; }
  195. /// \brief Returns the address space of the pointer operand.
  196. unsigned getPointerAddressSpace() const {
  197. return getPointerOperand()->getType()->getPointerAddressSpace();
  198. }
  199. // Methods for support type inquiry through isa, cast, and dyn_cast:
  200. static inline bool classof(const Instruction *I) {
  201. return I->getOpcode() == Instruction::Load;
  202. }
  203. static inline bool classof(const Value *V) {
  204. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  205. }
  206. private:
  207. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  208. // method so that subclasses cannot accidentally use it.
  209. void setInstructionSubclassData(unsigned short D) {
  210. Instruction::setInstructionSubclassData(D);
  211. }
  212. };
  213. //===----------------------------------------------------------------------===//
  214. // StoreInst Class
  215. //===----------------------------------------------------------------------===//
  216. /// StoreInst - an instruction for storing to memory
  217. ///
  218. class StoreInst : public Instruction {
  219. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  220. void AssertOK();
  221. protected:
  222. virtual StoreInst *clone_impl() const;
  223. public:
  224. // allocate space for exactly two operands
  225. void *operator new(size_t s) {
  226. return User::operator new(s, 2);
  227. }
  228. StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
  229. StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
  230. StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
  231. Instruction *InsertBefore = 0);
  232. StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
  233. StoreInst(Value *Val, Value *Ptr, bool isVolatile,
  234. unsigned Align, Instruction *InsertBefore = 0);
  235. StoreInst(Value *Val, Value *Ptr, bool isVolatile,
  236. unsigned Align, BasicBlock *InsertAtEnd);
  237. StoreInst(Value *Val, Value *Ptr, bool isVolatile,
  238. unsigned Align, AtomicOrdering Order,
  239. SynchronizationScope SynchScope = CrossThread,
  240. Instruction *InsertBefore = 0);
  241. StoreInst(Value *Val, Value *Ptr, bool isVolatile,
  242. unsigned Align, AtomicOrdering Order,
  243. SynchronizationScope SynchScope,
  244. BasicBlock *InsertAtEnd);
  245. /// isVolatile - Return true if this is a store to a volatile memory
  246. /// location.
  247. ///
  248. bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
  249. /// setVolatile - Specify whether this is a volatile store or not.
  250. ///
  251. void setVolatile(bool V) {
  252. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  253. (V ? 1 : 0));
  254. }
  255. /// Transparently provide more efficient getOperand methods.
  256. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  257. /// getAlignment - Return the alignment of the access that is being performed
  258. ///
  259. unsigned getAlignment() const {
  260. return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
  261. }
  262. void setAlignment(unsigned Align);
  263. /// Returns the ordering effect of this store.
  264. AtomicOrdering getOrdering() const {
  265. return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
  266. }
  267. /// Set the ordering constraint on this store. May not be Acquire or
  268. /// AcquireRelease.
  269. void setOrdering(AtomicOrdering Ordering) {
  270. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
  271. (Ordering << 7));
  272. }
  273. SynchronizationScope getSynchScope() const {
  274. return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
  275. }
  276. /// Specify whether this store instruction is ordered with respect to all
  277. /// concurrently executing threads, or only with respect to signal handlers
  278. /// executing in the same thread.
  279. void setSynchScope(SynchronizationScope xthread) {
  280. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
  281. (xthread << 6));
  282. }
  283. bool isAtomic() const { return getOrdering() != NotAtomic; }
  284. void setAtomic(AtomicOrdering Ordering,
  285. SynchronizationScope SynchScope = CrossThread) {
  286. setOrdering(Ordering);
  287. setSynchScope(SynchScope);
  288. }
  289. bool isSimple() const { return !isAtomic() && !isVolatile(); }
  290. bool isUnordered() const {
  291. return getOrdering() <= Unordered && !isVolatile();
  292. }
  293. Value *getValueOperand() { return getOperand(0); }
  294. const Value *getValueOperand() const { return getOperand(0); }
  295. Value *getPointerOperand() { return getOperand(1); }
  296. const Value *getPointerOperand() const { return getOperand(1); }
  297. static unsigned getPointerOperandIndex() { return 1U; }
  298. /// \brief Returns the address space of the pointer operand.
  299. unsigned getPointerAddressSpace() const {
  300. return getPointerOperand()->getType()->getPointerAddressSpace();
  301. }
  302. // Methods for support type inquiry through isa, cast, and dyn_cast:
  303. static inline bool classof(const Instruction *I) {
  304. return I->getOpcode() == Instruction::Store;
  305. }
  306. static inline bool classof(const Value *V) {
  307. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  308. }
  309. private:
  310. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  311. // method so that subclasses cannot accidentally use it.
  312. void setInstructionSubclassData(unsigned short D) {
  313. Instruction::setInstructionSubclassData(D);
  314. }
  315. };
  316. template <>
  317. struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
  318. };
  319. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
  320. //===----------------------------------------------------------------------===//
  321. // FenceInst Class
  322. //===----------------------------------------------------------------------===//
  323. /// FenceInst - an instruction for ordering other memory operations
  324. ///
  325. class FenceInst : public Instruction {
  326. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  327. void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
  328. protected:
  329. virtual FenceInst *clone_impl() const;
  330. public:
  331. // allocate space for exactly zero operands
  332. void *operator new(size_t s) {
  333. return User::operator new(s, 0);
  334. }
  335. // Ordering may only be Acquire, Release, AcquireRelease, or
  336. // SequentiallyConsistent.
  337. FenceInst(LLVMContext &C, AtomicOrdering Ordering,
  338. SynchronizationScope SynchScope = CrossThread,
  339. Instruction *InsertBefore = 0);
  340. FenceInst(LLVMContext &C, AtomicOrdering Ordering,
  341. SynchronizationScope SynchScope,
  342. BasicBlock *InsertAtEnd);
  343. /// Returns the ordering effect of this fence.
  344. AtomicOrdering getOrdering() const {
  345. return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
  346. }
  347. /// Set the ordering constraint on this fence. May only be Acquire, Release,
  348. /// AcquireRelease, or SequentiallyConsistent.
  349. void setOrdering(AtomicOrdering Ordering) {
  350. setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
  351. (Ordering << 1));
  352. }
  353. SynchronizationScope getSynchScope() const {
  354. return SynchronizationScope(getSubclassDataFromInstruction() & 1);
  355. }
  356. /// Specify whether this fence orders other operations with respect to all
  357. /// concurrently executing threads, or only with respect to signal handlers
  358. /// executing in the same thread.
  359. void setSynchScope(SynchronizationScope xthread) {
  360. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  361. xthread);
  362. }
  363. // Methods for support type inquiry through isa, cast, and dyn_cast:
  364. static inline bool classof(const Instruction *I) {
  365. return I->getOpcode() == Instruction::Fence;
  366. }
  367. static inline bool classof(const Value *V) {
  368. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  369. }
  370. private:
  371. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  372. // method so that subclasses cannot accidentally use it.
  373. void setInstructionSubclassData(unsigned short D) {
  374. Instruction::setInstructionSubclassData(D);
  375. }
  376. };
  377. //===----------------------------------------------------------------------===//
  378. // AtomicCmpXchgInst Class
  379. //===----------------------------------------------------------------------===//
  380. /// AtomicCmpXchgInst - an instruction that atomically checks whether a
  381. /// specified value is in a memory location, and, if it is, stores a new value
  382. /// there. Returns the value that was loaded.
  383. ///
  384. class AtomicCmpXchgInst : public Instruction {
  385. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  386. void Init(Value *Ptr, Value *Cmp, Value *NewVal,
  387. AtomicOrdering Ordering, SynchronizationScope SynchScope);
  388. protected:
  389. virtual AtomicCmpXchgInst *clone_impl() const;
  390. public:
  391. // allocate space for exactly three operands
  392. void *operator new(size_t s) {
  393. return User::operator new(s, 3);
  394. }
  395. AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
  396. AtomicOrdering Ordering, SynchronizationScope SynchScope,
  397. Instruction *InsertBefore = 0);
  398. AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
  399. AtomicOrdering Ordering, SynchronizationScope SynchScope,
  400. BasicBlock *InsertAtEnd);
  401. /// isVolatile - Return true if this is a cmpxchg from a volatile memory
  402. /// location.
  403. ///
  404. bool isVolatile() const {
  405. return getSubclassDataFromInstruction() & 1;
  406. }
  407. /// setVolatile - Specify whether this is a volatile cmpxchg.
  408. ///
  409. void setVolatile(bool V) {
  410. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  411. (unsigned)V);
  412. }
  413. /// Transparently provide more efficient getOperand methods.
  414. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  415. /// Set the ordering constraint on this cmpxchg.
  416. void setOrdering(AtomicOrdering Ordering) {
  417. assert(Ordering != NotAtomic &&
  418. "CmpXchg instructions can only be atomic.");
  419. setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
  420. (Ordering << 2));
  421. }
  422. /// Specify whether this cmpxchg is atomic and orders other operations with
  423. /// respect to all concurrently executing threads, or only with respect to
  424. /// signal handlers executing in the same thread.
  425. void setSynchScope(SynchronizationScope SynchScope) {
  426. setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
  427. (SynchScope << 1));
  428. }
  429. /// Returns the ordering constraint on this cmpxchg.
  430. AtomicOrdering getOrdering() const {
  431. return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
  432. }
  433. /// Returns whether this cmpxchg is atomic between threads or only within a
  434. /// single thread.
  435. SynchronizationScope getSynchScope() const {
  436. return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
  437. }
  438. Value *getPointerOperand() { return getOperand(0); }
  439. const Value *getPointerOperand() const { return getOperand(0); }
  440. static unsigned getPointerOperandIndex() { return 0U; }
  441. Value *getCompareOperand() { return getOperand(1); }
  442. const Value *getCompareOperand() const { return getOperand(1); }
  443. Value *getNewValOperand() { return getOperand(2); }
  444. const Value *getNewValOperand() const { return getOperand(2); }
  445. /// \brief Returns the address space of the pointer operand.
  446. unsigned getPointerAddressSpace() const {
  447. return getPointerOperand()->getType()->getPointerAddressSpace();
  448. }
  449. // Methods for support type inquiry through isa, cast, and dyn_cast:
  450. static inline bool classof(const Instruction *I) {
  451. return I->getOpcode() == Instruction::AtomicCmpXchg;
  452. }
  453. static inline bool classof(const Value *V) {
  454. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  455. }
  456. private:
  457. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  458. // method so that subclasses cannot accidentally use it.
  459. void setInstructionSubclassData(unsigned short D) {
  460. Instruction::setInstructionSubclassData(D);
  461. }
  462. };
  463. template <>
  464. struct OperandTraits<AtomicCmpXchgInst> :
  465. public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
  466. };
  467. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
  468. //===----------------------------------------------------------------------===//
  469. // AtomicRMWInst Class
  470. //===----------------------------------------------------------------------===//
  471. /// AtomicRMWInst - an instruction that atomically reads a memory location,
  472. /// combines it with another value, and then stores the result back. Returns
  473. /// the old value.
  474. ///
  475. class AtomicRMWInst : public Instruction {
  476. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  477. protected:
  478. virtual AtomicRMWInst *clone_impl() const;
  479. public:
  480. /// This enumeration lists the possible modifications atomicrmw can make. In
  481. /// the descriptions, 'p' is the pointer to the instruction's memory location,
  482. /// 'old' is the initial value of *p, and 'v' is the other value passed to the
  483. /// instruction. These instructions always return 'old'.
  484. enum BinOp {
  485. /// *p = v
  486. Xchg,
  487. /// *p = old + v
  488. Add,
  489. /// *p = old - v
  490. Sub,
  491. /// *p = old & v
  492. And,
  493. /// *p = ~old & v
  494. Nand,
  495. /// *p = old | v
  496. Or,
  497. /// *p = old ^ v
  498. Xor,
  499. /// *p = old >signed v ? old : v
  500. Max,
  501. /// *p = old <signed v ? old : v
  502. Min,
  503. /// *p = old >unsigned v ? old : v
  504. UMax,
  505. /// *p = old <unsigned v ? old : v
  506. UMin,
  507. FIRST_BINOP = Xchg,
  508. LAST_BINOP = UMin,
  509. BAD_BINOP
  510. };
  511. // allocate space for exactly two operands
  512. void *operator new(size_t s) {
  513. return User::operator new(s, 2);
  514. }
  515. AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
  516. AtomicOrdering Ordering, SynchronizationScope SynchScope,
  517. Instruction *InsertBefore = 0);
  518. AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
  519. AtomicOrdering Ordering, SynchronizationScope SynchScope,
  520. BasicBlock *InsertAtEnd);
  521. BinOp getOperation() const {
  522. return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
  523. }
  524. void setOperation(BinOp Operation) {
  525. unsigned short SubclassData = getSubclassDataFromInstruction();
  526. setInstructionSubclassData((SubclassData & 31) |
  527. (Operation << 5));
  528. }
  529. /// isVolatile - Return true if this is a RMW on a volatile memory location.
  530. ///
  531. bool isVolatile() const {
  532. return getSubclassDataFromInstruction() & 1;
  533. }
  534. /// setVolatile - Specify whether this is a volatile RMW or not.
  535. ///
  536. void setVolatile(bool V) {
  537. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  538. (unsigned)V);
  539. }
  540. /// Transparently provide more efficient getOperand methods.
  541. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  542. /// Set the ordering constraint on this RMW.
  543. void setOrdering(AtomicOrdering Ordering) {
  544. assert(Ordering != NotAtomic &&
  545. "atomicrmw instructions can only be atomic.");
  546. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
  547. (Ordering << 2));
  548. }
  549. /// Specify whether this RMW orders other operations with respect to all
  550. /// concurrently executing threads, or only with respect to signal handlers
  551. /// executing in the same thread.
  552. void setSynchScope(SynchronizationScope SynchScope) {
  553. setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
  554. (SynchScope << 1));
  555. }
  556. /// Returns the ordering constraint on this RMW.
  557. AtomicOrdering getOrdering() const {
  558. return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
  559. }
  560. /// Returns whether this RMW is atomic between threads or only within a
  561. /// single thread.
  562. SynchronizationScope getSynchScope() const {
  563. return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
  564. }
  565. Value *getPointerOperand() { return getOperand(0); }
  566. const Value *getPointerOperand() const { return getOperand(0); }
  567. static unsigned getPointerOperandIndex() { return 0U; }
  568. Value *getValOperand() { return getOperand(1); }
  569. const Value *getValOperand() const { return getOperand(1); }
  570. /// \brief Returns the address space of the pointer operand.
  571. unsigned getPointerAddressSpace() const {
  572. return getPointerOperand()->getType()->getPointerAddressSpace();
  573. }
  574. // Methods for support type inquiry through isa, cast, and dyn_cast:
  575. static inline bool classof(const Instruction *I) {
  576. return I->getOpcode() == Instruction::AtomicRMW;
  577. }
  578. static inline bool classof(const Value *V) {
  579. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  580. }
  581. private:
  582. void Init(BinOp Operation, Value *Ptr, Value *Val,
  583. AtomicOrdering Ordering, SynchronizationScope SynchScope);
  584. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  585. // method so that subclasses cannot accidentally use it.
  586. void setInstructionSubclassData(unsigned short D) {
  587. Instruction::setInstructionSubclassData(D);
  588. }
  589. };
  590. template <>
  591. struct OperandTraits<AtomicRMWInst>
  592. : public FixedNumOperandTraits<AtomicRMWInst,2> {
  593. };
  594. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
  595. //===----------------------------------------------------------------------===//
  596. // GetElementPtrInst Class
  597. //===----------------------------------------------------------------------===//
  598. // checkGEPType - Simple wrapper function to give a better assertion failure
  599. // message on bad indexes for a gep instruction.
  600. //
  601. inline Type *checkGEPType(Type *Ty) {
  602. assert(Ty && "Invalid GetElementPtrInst indices for type!");
  603. return Ty;
  604. }
  605. /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
  606. /// access elements of arrays and structs
  607. ///
  608. class GetElementPtrInst : public Instruction {
  609. GetElementPtrInst(const GetElementPtrInst &GEPI);
  610. void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
  611. /// Constructors - Create a getelementptr instruction with a base pointer an
  612. /// list of indices. The first ctor can optionally insert before an existing
  613. /// instruction, the second appends the new instruction to the specified
  614. /// BasicBlock.
  615. inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
  616. unsigned Values, const Twine &NameStr,
  617. Instruction *InsertBefore);
  618. inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
  619. unsigned Values, const Twine &NameStr,
  620. BasicBlock *InsertAtEnd);
  621. protected:
  622. virtual GetElementPtrInst *clone_impl() const;
  623. public:
  624. static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
  625. const Twine &NameStr = "",
  626. Instruction *InsertBefore = 0) {
  627. unsigned Values = 1 + unsigned(IdxList.size());
  628. return new(Values)
  629. GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
  630. }
  631. static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
  632. const Twine &NameStr,
  633. BasicBlock *InsertAtEnd) {
  634. unsigned Values = 1 + unsigned(IdxList.size());
  635. return new(Values)
  636. GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
  637. }
  638. /// Create an "inbounds" getelementptr. See the documentation for the
  639. /// "inbounds" flag in LangRef.html for details.
  640. static GetElementPtrInst *CreateInBounds(Value *Ptr,
  641. ArrayRef<Value *> IdxList,
  642. const Twine &NameStr = "",
  643. Instruction *InsertBefore = 0) {
  644. GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
  645. GEP->setIsInBounds(true);
  646. return GEP;
  647. }
  648. static GetElementPtrInst *CreateInBounds(Value *Ptr,
  649. ArrayRef<Value *> IdxList,
  650. const Twine &NameStr,
  651. BasicBlock *InsertAtEnd) {
  652. GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
  653. GEP->setIsInBounds(true);
  654. return GEP;
  655. }
  656. /// Transparently provide more efficient getOperand methods.
  657. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  658. // getType - Overload to return most specific sequential type.
  659. SequentialType *getType() const {
  660. return cast<SequentialType>(Instruction::getType());
  661. }
  662. /// \brief Returns the address space of this instruction's pointer type.
  663. unsigned getAddressSpace() const {
  664. // Note that this is always the same as the pointer operand's address space
  665. // and that is cheaper to compute, so cheat here.
  666. return getPointerAddressSpace();
  667. }
  668. /// getIndexedType - Returns the type of the element that would be loaded with
  669. /// a load instruction with the specified parameters.
  670. ///
  671. /// Null is returned if the indices are invalid for the specified
  672. /// pointer type.
  673. ///
  674. static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
  675. static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
  676. static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
  677. inline op_iterator idx_begin() { return op_begin()+1; }
  678. inline const_op_iterator idx_begin() const { return op_begin()+1; }
  679. inline op_iterator idx_end() { return op_end(); }
  680. inline const_op_iterator idx_end() const { return op_end(); }
  681. Value *getPointerOperand() {
  682. return getOperand(0);
  683. }
  684. const Value *getPointerOperand() const {
  685. return getOperand(0);
  686. }
  687. static unsigned getPointerOperandIndex() {
  688. return 0U; // get index for modifying correct operand.
  689. }
  690. /// getPointerOperandType - Method to return the pointer operand as a
  691. /// PointerType.
  692. Type *getPointerOperandType() const {
  693. return getPointerOperand()->getType();
  694. }
  695. /// \brief Returns the address space of the pointer operand.
  696. unsigned getPointerAddressSpace() const {
  697. return getPointerOperandType()->getPointerAddressSpace();
  698. }
  699. /// GetGEPReturnType - Returns the pointer type returned by the GEP
  700. /// instruction, which may be a vector of pointers.
  701. static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
  702. Type *PtrTy = PointerType::get(checkGEPType(
  703. getIndexedType(Ptr->getType(), IdxList)),
  704. Ptr->getType()->getPointerAddressSpace());
  705. // Vector GEP
  706. if (Ptr->getType()->isVectorTy()) {
  707. unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
  708. return VectorType::get(PtrTy, NumElem);
  709. }
  710. // Scalar GEP
  711. return PtrTy;
  712. }
  713. unsigned getNumIndices() const { // Note: always non-negative
  714. return getNumOperands() - 1;
  715. }
  716. bool hasIndices() const {
  717. return getNumOperands() > 1;
  718. }
  719. /// hasAllZeroIndices - Return true if all of the indices of this GEP are
  720. /// zeros. If so, the result pointer and the first operand have the same
  721. /// value, just potentially different types.
  722. bool hasAllZeroIndices() const;
  723. /// hasAllConstantIndices - Return true if all of the indices of this GEP are
  724. /// constant integers. If so, the result pointer and the first operand have
  725. /// a constant offset between them.
  726. bool hasAllConstantIndices() const;
  727. /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
  728. /// See LangRef.html for the meaning of inbounds on a getelementptr.
  729. void setIsInBounds(bool b = true);
  730. /// isInBounds - Determine whether the GEP has the inbounds flag.
  731. bool isInBounds() const;
  732. /// \brief Accumulate the constant address offset of this GEP if possible.
  733. ///
  734. /// This routine accepts an APInt into which it will accumulate the constant
  735. /// offset of this GEP if the GEP is in fact constant. If the GEP is not
  736. /// all-constant, it returns false and the value of the offset APInt is
  737. /// undefined (it is *not* preserved!). The APInt passed into this routine
  738. /// must be at least as wide as the IntPtr type for the address space of
  739. /// the base GEP pointer.
  740. bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
  741. // Methods for support type inquiry through isa, cast, and dyn_cast:
  742. static inline bool classof(const Instruction *I) {
  743. return (I->getOpcode() == Instruction::GetElementPtr);
  744. }
  745. static inline bool classof(const Value *V) {
  746. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  747. }
  748. };
  749. template <>
  750. struct OperandTraits<GetElementPtrInst> :
  751. public VariadicOperandTraits<GetElementPtrInst, 1> {
  752. };
  753. GetElementPtrInst::GetElementPtrInst(Value *Ptr,
  754. ArrayRef<Value *> IdxList,
  755. unsigned Values,
  756. const Twine &NameStr,
  757. Instruction *InsertBefore)
  758. : Instruction(getGEPReturnType(Ptr, IdxList),
  759. GetElementPtr,
  760. OperandTraits<GetElementPtrInst>::op_end(this) - Values,
  761. Values, InsertBefore) {
  762. init(Ptr, IdxList, NameStr);
  763. }
  764. GetElementPtrInst::GetElementPtrInst(Value *Ptr,
  765. ArrayRef<Value *> IdxList,
  766. unsigned Values,
  767. const Twine &NameStr,
  768. BasicBlock *InsertAtEnd)
  769. : Instruction(getGEPReturnType(Ptr, IdxList),
  770. GetElementPtr,
  771. OperandTraits<GetElementPtrInst>::op_end(this) - Values,
  772. Values, InsertAtEnd) {
  773. init(Ptr, IdxList, NameStr);
  774. }
  775. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
  776. //===----------------------------------------------------------------------===//
  777. // ICmpInst Class
  778. //===----------------------------------------------------------------------===//
  779. /// This instruction compares its operands according to the predicate given
  780. /// to the constructor. It only operates on integers or pointers. The operands
  781. /// must be identical types.
  782. /// \brief Represent an integer comparison operator.
  783. class ICmpInst: public CmpInst {
  784. protected:
  785. /// \brief Clone an identical ICmpInst
  786. virtual ICmpInst *clone_impl() const;
  787. public:
  788. /// \brief Constructor with insert-before-instruction semantics.
  789. ICmpInst(
  790. Instruction *InsertBefore, ///< Where to insert
  791. Predicate pred, ///< The predicate to use for the comparison
  792. Value *LHS, ///< The left-hand-side of the expression
  793. Value *RHS, ///< The right-hand-side of the expression
  794. const Twine &NameStr = "" ///< Name of the instruction
  795. ) : CmpInst(makeCmpResultType(LHS->getType()),
  796. Instruction::ICmp, pred, LHS, RHS, NameStr,
  797. InsertBefore) {
  798. assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
  799. pred <= CmpInst::LAST_ICMP_PREDICATE &&
  800. "Invalid ICmp predicate value");
  801. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  802. "Both operands to ICmp instruction are not of the same type!");
  803. // Check that the operands are the right type
  804. assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
  805. getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
  806. "Invalid operand types for ICmp instruction");
  807. }
  808. /// \brief Constructor with insert-at-end semantics.
  809. ICmpInst(
  810. BasicBlock &InsertAtEnd, ///< Block to insert into.
  811. Predicate pred, ///< The predicate to use for the comparison
  812. Value *LHS, ///< The left-hand-side of the expression
  813. Value *RHS, ///< The right-hand-side of the expression
  814. const Twine &NameStr = "" ///< Name of the instruction
  815. ) : CmpInst(makeCmpResultType(LHS->getType()),
  816. Instruction::ICmp, pred, LHS, RHS, NameStr,
  817. &InsertAtEnd) {
  818. assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
  819. pred <= CmpInst::LAST_ICMP_PREDICATE &&
  820. "Invalid ICmp predicate value");
  821. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  822. "Both operands to ICmp instruction are not of the same type!");
  823. // Check that the operands are the right type
  824. assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
  825. getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
  826. "Invalid operand types for ICmp instruction");
  827. }
  828. /// \brief Constructor with no-insertion semantics
  829. ICmpInst(
  830. Predicate pred, ///< The predicate to use for the comparison
  831. Value *LHS, ///< The left-hand-side of the expression
  832. Value *RHS, ///< The right-hand-side of the expression
  833. const Twine &NameStr = "" ///< Name of the instruction
  834. ) : CmpInst(makeCmpResultType(LHS->getType()),
  835. Instruction::ICmp, pred, LHS, RHS, NameStr) {
  836. assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
  837. pred <= CmpInst::LAST_ICMP_PREDICATE &&
  838. "Invalid ICmp predicate value");
  839. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  840. "Both operands to ICmp instruction are not of the same type!");
  841. // Check that the operands are the right type
  842. assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
  843. getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
  844. "Invalid operand types for ICmp instruction");
  845. }
  846. /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
  847. /// @returns the predicate that would be the result if the operand were
  848. /// regarded as signed.
  849. /// \brief Return the signed version of the predicate
  850. Predicate getSignedPredicate() const {
  851. return getSignedPredicate(getPredicate());
  852. }
  853. /// This is a static version that you can use without an instruction.
  854. /// \brief Return the signed version of the predicate.
  855. static Predicate getSignedPredicate(Predicate pred);
  856. /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
  857. /// @returns the predicate that would be the result if the operand were
  858. /// regarded as unsigned.
  859. /// \brief Return the unsigned version of the predicate
  860. Predicate getUnsignedPredicate() const {
  861. return getUnsignedPredicate(getPredicate());
  862. }
  863. /// This is a static version that you can use without an instruction.
  864. /// \brief Return the unsigned version of the predicate.
  865. static Predicate getUnsignedPredicate(Predicate pred);
  866. /// isEquality - Return true if this predicate is either EQ or NE. This also
  867. /// tests for commutativity.
  868. static bool isEquality(Predicate P) {
  869. return P == ICMP_EQ || P == ICMP_NE;
  870. }
  871. /// isEquality - Return true if this predicate is either EQ or NE. This also
  872. /// tests for commutativity.
  873. bool isEquality() const {
  874. return isEquality(getPredicate());
  875. }
  876. /// @returns true if the predicate of this ICmpInst is commutative
  877. /// \brief Determine if this relation is commutative.
  878. bool isCommutative() const { return isEquality(); }
  879. /// isRelational - Return true if the predicate is relational (not EQ or NE).
  880. ///
  881. bool isRelational() const {
  882. return !isEquality();
  883. }
  884. /// isRelational - Return true if the predicate is relational (not EQ or NE).
  885. ///
  886. static bool isRelational(Predicate P) {
  887. return !isEquality(P);
  888. }
  889. /// Initialize a set of values that all satisfy the predicate with C.
  890. /// \brief Make a ConstantRange for a relation with a constant value.
  891. static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
  892. /// Exchange the two operands to this instruction in such a way that it does
  893. /// not modify the semantics of the instruction. The predicate value may be
  894. /// changed to retain the same result if the predicate is order dependent
  895. /// (e.g. ult).
  896. /// \brief Swap operands and adjust predicate.
  897. void swapOperands() {
  898. setPredicate(getSwappedPredicate());
  899. Op<0>().swap(Op<1>());
  900. }
  901. // Methods for support type inquiry through isa, cast, and dyn_cast:
  902. static inline bool classof(const Instruction *I) {
  903. return I->getOpcode() == Instruction::ICmp;
  904. }
  905. static inline bool classof(const Value *V) {
  906. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  907. }
  908. };
  909. //===----------------------------------------------------------------------===//
  910. // FCmpInst Class
  911. //===----------------------------------------------------------------------===//
  912. /// This instruction compares its operands according to the predicate given
  913. /// to the constructor. It only operates on floating point values or packed
  914. /// vectors of floating point values. The operands must be identical types.
  915. /// \brief Represents a floating point comparison operator.
  916. class FCmpInst: public CmpInst {
  917. protected:
  918. /// \brief Clone an identical FCmpInst
  919. virtual FCmpInst *clone_impl() const;
  920. public:
  921. /// \brief Constructor with insert-before-instruction semantics.
  922. FCmpInst(
  923. Instruction *InsertBefore, ///< Where to insert
  924. Predicate pred, ///< The predicate to use for the comparison
  925. Value *LHS, ///< The left-hand-side of the expression
  926. Value *RHS, ///< The right-hand-side of the expression
  927. const Twine &NameStr = "" ///< Name of the instruction
  928. ) : CmpInst(makeCmpResultType(LHS->getType()),
  929. Instruction::FCmp, pred, LHS, RHS, NameStr,
  930. InsertBefore) {
  931. assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
  932. "Invalid FCmp predicate value");
  933. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  934. "Both operands to FCmp instruction are not of the same type!");
  935. // Check that the operands are the right type
  936. assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
  937. "Invalid operand types for FCmp instruction");
  938. }
  939. /// \brief Constructor with insert-at-end semantics.
  940. FCmpInst(
  941. BasicBlock &InsertAtEnd, ///< Block to insert into.
  942. Predicate pred, ///< The predicate to use for the comparison
  943. Value *LHS, ///< The left-hand-side of the expression
  944. Value *RHS, ///< The right-hand-side of the expression
  945. const Twine &NameStr = "" ///< Name of the instruction
  946. ) : CmpInst(makeCmpResultType(LHS->getType()),
  947. Instruction::FCmp, pred, LHS, RHS, NameStr,
  948. &InsertAtEnd) {
  949. assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
  950. "Invalid FCmp predicate value");
  951. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  952. "Both operands to FCmp instruction are not of the same type!");
  953. // Check that the operands are the right type
  954. assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
  955. "Invalid operand types for FCmp instruction");
  956. }
  957. /// \brief Constructor with no-insertion semantics
  958. FCmpInst(
  959. Predicate pred, ///< The predicate to use for the comparison
  960. Value *LHS, ///< The left-hand-side of the expression
  961. Value *RHS, ///< The right-hand-side of the expression
  962. const Twine &NameStr = "" ///< Name of the instruction
  963. ) : CmpInst(makeCmpResultType(LHS->getType()),
  964. Instruction::FCmp, pred, LHS, RHS, NameStr) {
  965. assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
  966. "Invalid FCmp predicate value");
  967. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  968. "Both operands to FCmp instruction are not of the same type!");
  969. // Check that the operands are the right type
  970. assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
  971. "Invalid operand types for FCmp instruction");
  972. }
  973. /// @returns true if the predicate of this instruction is EQ or NE.
  974. /// \brief Determine if this is an equality predicate.
  975. bool isEquality() const {
  976. return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
  977. getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
  978. }
  979. /// @returns true if the predicate of this instruction is commutative.
  980. /// \brief Determine if this is a commutative predicate.
  981. bool isCommutative() const {
  982. return isEquality() ||
  983. getPredicate() == FCMP_FALSE ||
  984. getPredicate() == FCMP_TRUE ||
  985. getPredicate() == FCMP_ORD ||
  986. getPredicate() == FCMP_UNO;
  987. }
  988. /// @returns true if the predicate is relational (not EQ or NE).
  989. /// \brief Determine if this a relational predicate.
  990. bool isRelational() const { return !isEquality(); }
  991. /// Exchange the two operands to this instruction in such a way that it does
  992. /// not modify the semantics of the instruction. The predicate value may be
  993. /// changed to retain the same result if the predicate is order dependent
  994. /// (e.g. ult).
  995. /// \brief Swap operands and adjust predicate.
  996. void swapOperands() {
  997. setPredicate(getSwappedPredicate());
  998. Op<0>().swap(Op<1>());
  999. }
  1000. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  1001. static inline bool classof(const Instruction *I) {
  1002. return I->getOpcode() == Instruction::FCmp;
  1003. }
  1004. static inline bool classof(const Value *V) {
  1005. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1006. }
  1007. };
  1008. //===----------------------------------------------------------------------===//
  1009. /// CallInst - This class represents a function call, abstracting a target
  1010. /// machine's calling convention. This class uses low bit of the SubClassData
  1011. /// field to indicate whether or not this is a tail call. The rest of the bits
  1012. /// hold the calling convention of the call.
  1013. ///
  1014. class CallInst : public Instruction {
  1015. AttributeSet AttributeList; ///< parameter attributes for call
  1016. CallInst(const CallInst &CI);
  1017. void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
  1018. void init(Value *Func, const Twine &NameStr);
  1019. /// Construct a CallInst given a range of arguments.
  1020. /// \brief Construct a CallInst from a range of arguments
  1021. inline CallInst(Value *Func, ArrayRef<Value *> Args,
  1022. const Twine &NameStr, Instruction *InsertBefore);
  1023. /// Construct a CallInst given a range of arguments.
  1024. /// \brief Construct a CallInst from a range of arguments
  1025. inline CallInst(Value *Func, ArrayRef<Value *> Args,
  1026. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1027. CallInst(Value *F, Value *Actual, const Twine &NameStr,
  1028. Instruction *InsertBefore);
  1029. CallInst(Value *F, Value *Actual, const Twine &NameStr,
  1030. BasicBlock *InsertAtEnd);
  1031. explicit CallInst(Value *F, const Twine &NameStr,
  1032. Instruction *InsertBefore);
  1033. CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
  1034. protected:
  1035. virtual CallInst *clone_impl() const;
  1036. public:
  1037. static CallInst *Create(Value *Func,
  1038. ArrayRef<Value *> Args,
  1039. const Twine &NameStr = "",
  1040. Instruction *InsertBefore = 0) {
  1041. return new(unsigned(Args.size() + 1))
  1042. CallInst(Func, Args, NameStr, InsertBefore);
  1043. }
  1044. static CallInst *Create(Value *Func,
  1045. ArrayRef<Value *> Args,
  1046. const Twine &NameStr, BasicBlock *InsertAtEnd) {
  1047. return new(unsigned(Args.size() + 1))
  1048. CallInst(Func, Args, NameStr, InsertAtEnd);
  1049. }
  1050. static CallInst *Create(Value *F, const Twine &NameStr = "",
  1051. Instruction *InsertBefore = 0) {
  1052. return new(1) CallInst(F, NameStr, InsertBefore);
  1053. }
  1054. static CallInst *Create(Value *F, const Twine &NameStr,
  1055. BasicBlock *InsertAtEnd) {
  1056. return new(1) CallInst(F, NameStr, InsertAtEnd);
  1057. }
  1058. /// CreateMalloc - Generate the IR for a call to malloc:
  1059. /// 1. Compute the malloc call's argument as the specified type's size,
  1060. /// possibly multiplied by the array size if the array size is not
  1061. /// constant 1.
  1062. /// 2. Call malloc with that argument.
  1063. /// 3. Bitcast the result of the malloc call to the specified type.
  1064. static Instruction *CreateMalloc(Instruction *InsertBefore,
  1065. Type *IntPtrTy, Type *AllocTy,
  1066. Value *AllocSize, Value *ArraySize = 0,
  1067. Function* MallocF = 0,
  1068. const Twine &Name = "");
  1069. static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
  1070. Type *IntPtrTy, Type *AllocTy,
  1071. Value *AllocSize, Value *ArraySize = 0,
  1072. Function* MallocF = 0,
  1073. const Twine &Name = "");
  1074. /// CreateFree - Generate the IR for a call to the builtin free function.
  1075. static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
  1076. static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
  1077. ~CallInst();
  1078. bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
  1079. void setTailCall(bool isTC = true) {
  1080. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  1081. unsigned(isTC));
  1082. }
  1083. /// Provide fast operand accessors
  1084. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1085. /// getNumArgOperands - Return the number of call arguments.
  1086. ///
  1087. unsigned getNumArgOperands() const { return getNumOperands() - 1; }
  1088. /// getArgOperand/setArgOperand - Return/set the i-th call argument.
  1089. ///
  1090. Value *getArgOperand(unsigned i) const { return getOperand(i); }
  1091. void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
  1092. /// getCallingConv/setCallingConv - Get or set the calling convention of this
  1093. /// function call.
  1094. CallingConv::ID getCallingConv() const {
  1095. return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
  1096. }
  1097. void setCallingConv(CallingConv::ID CC) {
  1098. setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
  1099. (static_cast<unsigned>(CC) << 1));
  1100. }
  1101. /// getAttributes - Return the parameter attributes for this call.
  1102. ///
  1103. const AttributeSet &getAttributes() const { return AttributeList; }
  1104. /// setAttributes - Set the parameter attributes for this call.
  1105. ///
  1106. void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
  1107. /// addAttribute - adds the attribute to the list of attributes.
  1108. void addAttribute(unsigned i, Attribute::AttrKind attr);
  1109. /// removeAttribute - removes the attribute from the list of attributes.
  1110. void removeAttribute(unsigned i, Attribute attr);
  1111. /// \brief Determine whether this call has the given attribute.
  1112. bool hasFnAttr(Attribute::AttrKind A) const;
  1113. /// \brief Determine whether the call or the callee has the given attributes.
  1114. bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
  1115. /// \brief Extract the alignment for a call or parameter (0=unknown).
  1116. unsigned getParamAlignment(unsigned i) const {
  1117. return AttributeList.getParamAlignment(i);
  1118. }
  1119. /// \brief Return true if the call should not be inlined.
  1120. bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
  1121. void setIsNoInline() {
  1122. addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
  1123. }
  1124. /// \brief Return true if the call can return twice
  1125. bool canReturnTwice() const {
  1126. return hasFnAttr(Attribute::ReturnsTwice);
  1127. }
  1128. void setCanReturnTwice() {
  1129. addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
  1130. }
  1131. /// \brief Determine if the call does not access memory.
  1132. bool doesNotAccessMemory() const {
  1133. return hasFnAttr(Attribute::ReadNone);
  1134. }
  1135. void setDoesNotAccessMemory() {
  1136. addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
  1137. }
  1138. /// \brief Determine if the call does not access or only reads memory.
  1139. bool onlyReadsMemory() const {
  1140. return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
  1141. }
  1142. void setOnlyReadsMemory() {
  1143. addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
  1144. }
  1145. /// \brief Determine if the call cannot return.
  1146. bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
  1147. void setDoesNotReturn() {
  1148. addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
  1149. }
  1150. /// \brief Determine if the call cannot unwind.
  1151. bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
  1152. void setDoesNotThrow() {
  1153. addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
  1154. }
  1155. /// \brief Determine if the call cannot be duplicated.
  1156. bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
  1157. void setCannotDuplicate() {
  1158. addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
  1159. }
  1160. /// \brief Determine if the call returns a structure through first
  1161. /// pointer argument.
  1162. bool hasStructRetAttr() const {
  1163. // Be friendly and also check the callee.
  1164. return paramHasAttr(1, Attribute::StructRet);
  1165. }
  1166. /// \brief Determine if any call argument is an aggregate passed by value.
  1167. bool hasByValArgument() const {
  1168. return AttributeList.hasAttrSomewhere(Attribute::ByVal);
  1169. }
  1170. /// getCalledFunction - Return the function called, or null if this is an
  1171. /// indirect function invocation.
  1172. ///
  1173. Function *getCalledFunction() const {
  1174. return dyn_cast<Function>(Op<-1>());
  1175. }
  1176. /// getCalledValue - Get a pointer to the function that is invoked by this
  1177. /// instruction.
  1178. const Value *getCalledValue() const { return Op<-1>(); }
  1179. Value *getCalledValue() { return Op<-1>(); }
  1180. /// setCalledFunction - Set the function called.
  1181. void setCalledFunction(Value* Fn) {
  1182. Op<-1>() = Fn;
  1183. }
  1184. /// isInlineAsm - Check if this call is an inline asm statement.
  1185. bool isInlineAsm() const {
  1186. return isa<InlineAsm>(Op<-1>());
  1187. }
  1188. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1189. static inline bool classof(const Instruction *I) {
  1190. return I->getOpcode() == Instruction::Call;
  1191. }
  1192. static inline bool classof(const Value *V) {
  1193. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1194. }
  1195. private:
  1196. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  1197. // method so that subclasses cannot accidentally use it.
  1198. void setInstructionSubclassData(unsigned short D) {
  1199. Instruction::setInstructionSubclassData(D);
  1200. }
  1201. };
  1202. template <>
  1203. struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
  1204. };
  1205. CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
  1206. const Twine &NameStr, BasicBlock *InsertAtEnd)
  1207. : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
  1208. ->getElementType())->getReturnType(),
  1209. Instruction::Call,
  1210. OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
  1211. unsigned(Args.size() + 1), InsertAtEnd) {
  1212. init(Func, Args, NameStr);
  1213. }
  1214. CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
  1215. const Twine &NameStr, Instruction *InsertBefore)
  1216. : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
  1217. ->getElementType())->getReturnType(),
  1218. Instruction::Call,
  1219. OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
  1220. unsigned(Args.size() + 1), InsertBefore) {
  1221. init(Func, Args, NameStr);
  1222. }
  1223. // Note: if you get compile errors about private methods then
  1224. // please update your code to use the high-level operand
  1225. // interfaces. See line 943 above.
  1226. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
  1227. //===----------------------------------------------------------------------===//
  1228. // SelectInst Class
  1229. //===----------------------------------------------------------------------===//
  1230. /// SelectInst - This class represents the LLVM 'select' instruction.
  1231. ///
  1232. class SelectInst : public Instruction {
  1233. void init(Value *C, Value *S1, Value *S2) {
  1234. assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
  1235. Op<0>() = C;
  1236. Op<1>() = S1;
  1237. Op<2>() = S2;
  1238. }
  1239. SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
  1240. Instruction *InsertBefore)
  1241. : Instruction(S1->getType(), Instruction::Select,
  1242. &Op<0>(), 3, InsertBefore) {
  1243. init(C, S1, S2);
  1244. setName(NameStr);
  1245. }
  1246. SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
  1247. BasicBlock *InsertAtEnd)
  1248. : Instruction(S1->getType(), Instruction::Select,
  1249. &Op<0>(), 3, InsertAtEnd) {
  1250. init(C, S1, S2);
  1251. setName(NameStr);
  1252. }
  1253. protected:
  1254. virtual SelectInst *clone_impl() const;
  1255. public:
  1256. static SelectInst *Create(Value *C, Value *S1, Value *S2,
  1257. const Twine &NameStr = "",
  1258. Instruction *InsertBefore = 0) {
  1259. return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
  1260. }
  1261. static SelectInst *Create(Value *C, Value *S1, Value *S2,
  1262. const Twine &NameStr,
  1263. BasicBlock *InsertAtEnd) {
  1264. return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
  1265. }
  1266. const Value *getCondition() const { return Op<0>(); }
  1267. const Value *getTrueValue() const { return Op<1>(); }
  1268. const Value *getFalseValue() const { return Op<2>(); }
  1269. Value *getCondition() { return Op<0>(); }
  1270. Value *getTrueValue() { return Op<1>(); }
  1271. Value *getFalseValue() { return Op<2>(); }
  1272. /// areInvalidOperands - Return a string if the specified operands are invalid
  1273. /// for a select operation, otherwise return null.
  1274. static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
  1275. /// Transparently provide more efficient getOperand methods.
  1276. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1277. OtherOps getOpcode() const {
  1278. return static_cast<OtherOps>(Instruction::getOpcode());
  1279. }
  1280. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1281. static inline bool classof(const Instruction *I) {
  1282. return I->getOpcode() == Instruction::Select;
  1283. }
  1284. static inline bool classof(const Value *V) {
  1285. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1286. }
  1287. };
  1288. template <>
  1289. struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
  1290. };
  1291. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
  1292. //===----------------------------------------------------------------------===//
  1293. // VAArgInst Class
  1294. //===----------------------------------------------------------------------===//
  1295. /// VAArgInst - This class represents the va_arg llvm instruction, which returns
  1296. /// an argument of the specified type given a va_list and increments that list
  1297. ///
  1298. class VAArgInst : public UnaryInstruction {
  1299. protected:
  1300. virtual VAArgInst *clone_impl() const;
  1301. public:
  1302. VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
  1303. Instruction *InsertBefore = 0)
  1304. : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
  1305. setName(NameStr);
  1306. }
  1307. VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
  1308. BasicBlock *InsertAtEnd)
  1309. : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
  1310. setName(NameStr);
  1311. }
  1312. Value *getPointerOperand() { return getOperand(0); }
  1313. const Value *getPointerOperand() const { return getOperand(0); }
  1314. static unsigned getPointerOperandIndex() { return 0U; }
  1315. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1316. static inline bool classof(const Instruction *I) {
  1317. return I->getOpcode() == VAArg;
  1318. }
  1319. static inline bool classof(const Value *V) {
  1320. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1321. }
  1322. };
  1323. //===----------------------------------------------------------------------===//
  1324. // ExtractElementInst Class
  1325. //===----------------------------------------------------------------------===//
  1326. /// ExtractElementInst - This instruction extracts a single (scalar)
  1327. /// element from a VectorType value
  1328. ///
  1329. class ExtractElementInst : public Instruction {
  1330. ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
  1331. Instruction *InsertBefore = 0);
  1332. ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
  1333. BasicBlock *InsertAtEnd);
  1334. protected:
  1335. virtual ExtractElementInst *clone_impl() const;
  1336. public:
  1337. static ExtractElementInst *Create(Value *Vec, Value *Idx,
  1338. const Twine &NameStr = "",
  1339. Instruction *InsertBefore = 0) {
  1340. return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
  1341. }
  1342. static ExtractElementInst *Create(Value *Vec, Value *Idx,
  1343. const Twine &NameStr,
  1344. BasicBlock *InsertAtEnd) {
  1345. return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
  1346. }
  1347. /// isValidOperands - Return true if an extractelement instruction can be
  1348. /// formed with the specified operands.
  1349. static bool isValidOperands(const Value *Vec, const Value *Idx);
  1350. Value *getVectorOperand() { return Op<0>(); }
  1351. Value *getIndexOperand() { return Op<1>(); }
  1352. const Value *getVectorOperand() const { return Op<0>(); }
  1353. const Value *getIndexOperand() const { return Op<1>(); }
  1354. VectorType *getVectorOperandType() const {
  1355. return cast<VectorType>(getVectorOperand()->getType());
  1356. }
  1357. /// Transparently provide more efficient getOperand methods.
  1358. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1359. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1360. static inline bool classof(const Instruction *I) {
  1361. return I->getOpcode() == Instruction::ExtractElement;
  1362. }
  1363. static inline bool classof(const Value *V) {
  1364. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1365. }
  1366. };
  1367. template <>
  1368. struct OperandTraits<ExtractElementInst> :
  1369. public FixedNumOperandTraits<ExtractElementInst, 2> {
  1370. };
  1371. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
  1372. //===----------------------------------------------------------------------===//
  1373. // InsertElementInst Class
  1374. //===----------------------------------------------------------------------===//
  1375. /// InsertElementInst - This instruction inserts a single (scalar)
  1376. /// element into a VectorType value
  1377. ///
  1378. class InsertElementInst : public Instruction {
  1379. InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
  1380. const Twine &NameStr = "",
  1381. Instruction *InsertBefore = 0);
  1382. InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
  1383. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1384. protected:
  1385. virtual InsertElementInst *clone_impl() const;
  1386. public:
  1387. static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
  1388. const Twine &NameStr = "",
  1389. Instruction *InsertBefore = 0) {
  1390. return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
  1391. }
  1392. static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
  1393. const Twine &NameStr,
  1394. BasicBlock *InsertAtEnd) {
  1395. return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
  1396. }
  1397. /// isValidOperands - Return true if an insertelement instruction can be
  1398. /// formed with the specified operands.
  1399. static bool isValidOperands(const Value *Vec, const Value *NewElt,
  1400. const Value *Idx);
  1401. /// getType - Overload to return most specific vector type.
  1402. ///
  1403. VectorType *getType() const {
  1404. return cast<VectorType>(Instruction::getType());
  1405. }
  1406. /// Transparently provide more efficient getOperand methods.
  1407. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1408. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1409. static inline bool classof(const Instruction *I) {
  1410. return I->getOpcode() == Instruction::InsertElement;
  1411. }
  1412. static inline bool classof(const Value *V) {
  1413. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1414. }
  1415. };
  1416. template <>
  1417. struct OperandTraits<InsertElementInst> :
  1418. public FixedNumOperandTraits<InsertElementInst, 3> {
  1419. };
  1420. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
  1421. //===----------------------------------------------------------------------===//
  1422. // ShuffleVectorInst Class
  1423. //===----------------------------------------------------------------------===//
  1424. /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
  1425. /// input vectors.
  1426. ///
  1427. class ShuffleVectorInst : public Instruction {
  1428. protected:
  1429. virtual ShuffleVectorInst *clone_impl() const;
  1430. public:
  1431. // allocate space for exactly three operands
  1432. void *operator new(size_t s) {
  1433. return User::operator new(s, 3);
  1434. }
  1435. ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
  1436. const Twine &NameStr = "",
  1437. Instruction *InsertBefor = 0);
  1438. ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
  1439. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1440. /// isValidOperands - Return true if a shufflevector instruction can be
  1441. /// formed with the specified operands.
  1442. static bool isValidOperands(const Value *V1, const Value *V2,
  1443. const Value *Mask);
  1444. /// getType - Overload to return most specific vector type.
  1445. ///
  1446. VectorType *getType() const {
  1447. return cast<VectorType>(Instruction::getType());
  1448. }
  1449. /// Transparently provide more efficient getOperand methods.
  1450. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1451. Constant *getMask() const {
  1452. return cast<Constant>(getOperand(2));
  1453. }
  1454. /// getMaskValue - Return the index from the shuffle mask for the specified
  1455. /// output result. This is either -1 if the element is undef or a number less
  1456. /// than 2*numelements.
  1457. static int getMaskValue(Constant *Mask, unsigned i);
  1458. int getMaskValue(unsigned i) const {
  1459. return getMaskValue(getMask(), i);
  1460. }
  1461. /// getShuffleMask - Return the full mask for this instruction, where each
  1462. /// element is the element number and undef's are returned as -1.
  1463. static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
  1464. void getShuffleMask(SmallVectorImpl<int> &Result) const {
  1465. return getShuffleMask(getMask(), Result);
  1466. }
  1467. SmallVector<int, 16> getShuffleMask() const {
  1468. SmallVector<int, 16> Mask;
  1469. getShuffleMask(Mask);
  1470. return Mask;
  1471. }
  1472. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1473. static inline bool classof(const Instruction *I) {
  1474. return I->getOpcode() == Instruction::ShuffleVector;
  1475. }
  1476. static inline bool classof(const Value *V) {
  1477. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1478. }
  1479. };
  1480. template <>
  1481. struct OperandTraits<ShuffleVectorInst> :
  1482. public FixedNumOperandTraits<ShuffleVectorInst, 3> {
  1483. };
  1484. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
  1485. //===----------------------------------------------------------------------===//
  1486. // ExtractValueInst Class
  1487. //===----------------------------------------------------------------------===//
  1488. /// ExtractValueInst - This instruction extracts a struct member or array
  1489. /// element value from an aggregate value.
  1490. ///
  1491. class ExtractValueInst : public UnaryInstruction {
  1492. SmallVector<unsigned, 4> Indices;
  1493. ExtractValueInst(const ExtractValueInst &EVI);
  1494. void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
  1495. /// Constructors - Create a extractvalue instruction with a base aggregate
  1496. /// value and a list of indices. The first ctor can optionally insert before
  1497. /// an existing instruction, the second appends the new instruction to the
  1498. /// specified BasicBlock.
  1499. inline ExtractValueInst(Value *Agg,
  1500. ArrayRef<unsigned> Idxs,
  1501. const Twine &NameStr,
  1502. Instruction *InsertBefore);
  1503. inline ExtractValueInst(Value *Agg,
  1504. ArrayRef<unsigned> Idxs,
  1505. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1506. // allocate space for exactly one operand
  1507. void *operator new(size_t s) {
  1508. return User::operator new(s, 1);
  1509. }
  1510. protected:
  1511. virtual ExtractValueInst *clone_impl() const;
  1512. public:
  1513. static ExtractValueInst *Create(Value *Agg,
  1514. ArrayRef<unsigned> Idxs,
  1515. const Twine &NameStr = "",
  1516. Instruction *InsertBefore = 0) {
  1517. return new
  1518. ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
  1519. }
  1520. static ExtractValueInst *Create(Value *Agg,
  1521. ArrayRef<unsigned> Idxs,
  1522. const Twine &NameStr,
  1523. BasicBlock *InsertAtEnd) {
  1524. return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
  1525. }
  1526. /// getIndexedType - Returns the type of the element that would be extracted
  1527. /// with an extractvalue instruction with the specified parameters.
  1528. ///
  1529. /// Null is returned if the indices are invalid for the specified type.
  1530. static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
  1531. typedef const unsigned* idx_iterator;
  1532. inline idx_iterator idx_begin() const { return Indices.begin(); }
  1533. inline idx_iterator idx_end() const { return Indices.end(); }
  1534. Value *getAggregateOperand() {
  1535. return getOperand(0);
  1536. }
  1537. const Value *getAggregateOperand() const {
  1538. return getOperand(0);
  1539. }
  1540. static unsigned getAggregateOperandIndex() {
  1541. return 0U; // get index for modifying correct operand
  1542. }
  1543. ArrayRef<unsigned> getIndices() const {
  1544. return Indices;
  1545. }
  1546. unsigned getNumIndices() const {
  1547. return (unsigned)Indices.size();
  1548. }
  1549. bool hasIndices() const {
  1550. return true;
  1551. }
  1552. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1553. static inline bool classof(const Instruction *I) {
  1554. return I->getOpcode() == Instruction::ExtractValue;
  1555. }
  1556. static inline bool classof(const Value *V) {
  1557. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1558. }
  1559. };
  1560. ExtractValueInst::ExtractValueInst(Value *Agg,
  1561. ArrayRef<unsigned> Idxs,
  1562. const Twine &NameStr,
  1563. Instruction *InsertBefore)
  1564. : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
  1565. ExtractValue, Agg, InsertBefore) {
  1566. init(Idxs, NameStr);
  1567. }
  1568. ExtractValueInst::ExtractValueInst(Value *Agg,
  1569. ArrayRef<unsigned> Idxs,
  1570. const Twine &NameStr,
  1571. BasicBlock *InsertAtEnd)
  1572. : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
  1573. ExtractValue, Agg, InsertAtEnd) {
  1574. init(Idxs, NameStr);
  1575. }
  1576. //===----------------------------------------------------------------------===//
  1577. // InsertValueInst Class
  1578. //===----------------------------------------------------------------------===//
  1579. /// InsertValueInst - This instruction inserts a struct field of array element
  1580. /// value into an aggregate value.
  1581. ///
  1582. class InsertValueInst : public Instruction {
  1583. SmallVector<unsigned, 4> Indices;
  1584. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  1585. InsertValueInst(const InsertValueInst &IVI);
  1586. void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
  1587. const Twine &NameStr);
  1588. /// Constructors - Create a insertvalue instruction with a base aggregate
  1589. /// value, a value to insert, and a list of indices. The first ctor can
  1590. /// optionally insert before an existing instruction, the second appends
  1591. /// the new instruction to the specified BasicBlock.
  1592. inline InsertValueInst(Value *Agg, Value *Val,
  1593. ArrayRef<unsigned> Idxs,
  1594. const Twine &NameStr,
  1595. Instruction *InsertBefore);
  1596. inline InsertValueInst(Value *Agg, Value *Val,
  1597. ArrayRef<unsigned> Idxs,
  1598. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1599. /// Constructors - These two constructors are convenience methods because one
  1600. /// and two index insertvalue instructions are so common.
  1601. InsertValueInst(Value *Agg, Value *Val,
  1602. unsigned Idx, const Twine &NameStr = "",
  1603. Instruction *InsertBefore = 0);
  1604. InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
  1605. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1606. protected:
  1607. virtual InsertValueInst *clone_impl() const;
  1608. public:
  1609. // allocate space for exactly two operands
  1610. void *operator new(size_t s) {
  1611. return User::operator new(s, 2);
  1612. }
  1613. static InsertValueInst *Create(Value *Agg, Value *Val,
  1614. ArrayRef<unsigned> Idxs,
  1615. const Twine &NameStr = "",
  1616. Instruction *InsertBefore = 0) {
  1617. return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
  1618. }
  1619. static InsertValueInst *Create(Value *Agg, Value *Val,
  1620. ArrayRef<unsigned> Idxs,
  1621. const Twine &NameStr,
  1622. BasicBlock *InsertAtEnd) {
  1623. return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
  1624. }
  1625. /// Transparently provide more efficient getOperand methods.
  1626. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1627. typedef const unsigned* idx_iterator;
  1628. inline idx_iterator idx_begin() const { return Indices.begin(); }
  1629. inline idx_iterator idx_end() const { return Indices.end(); }
  1630. Value *getAggregateOperand() {
  1631. return getOperand(0);
  1632. }
  1633. const Value *getAggregateOperand() const {
  1634. return getOperand(0);
  1635. }
  1636. static unsigned getAggregateOperandIndex() {
  1637. return 0U; // get index for modifying correct operand
  1638. }
  1639. Value *getInsertedValueOperand() {
  1640. return getOperand(1);
  1641. }
  1642. const Value *getInsertedValueOperand() const {
  1643. return getOperand(1);
  1644. }
  1645. static unsigned getInsertedValueOperandIndex() {
  1646. return 1U; // get index for modifying correct operand
  1647. }
  1648. ArrayRef<unsigned> getIndices() const {
  1649. return Indices;
  1650. }
  1651. unsigned getNumIndices() const {
  1652. return (unsigned)Indices.size();
  1653. }
  1654. bool hasIndices() const {
  1655. return true;
  1656. }
  1657. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1658. static inline bool classof(const Instruction *I) {
  1659. return I->getOpcode() == Instruction::InsertValue;
  1660. }
  1661. static inline bool classof(const Value *V) {
  1662. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1663. }
  1664. };
  1665. template <>
  1666. struct OperandTraits<InsertValueInst> :
  1667. public FixedNumOperandTraits<InsertValueInst, 2> {
  1668. };
  1669. InsertValueInst::InsertValueInst(Value *Agg,
  1670. Value *Val,
  1671. ArrayRef<unsigned> Idxs,
  1672. const Twine &NameStr,
  1673. Instruction *InsertBefore)
  1674. : Instruction(Agg->getType(), InsertValue,
  1675. OperandTraits<InsertValueInst>::op_begin(this),
  1676. 2, InsertBefore) {
  1677. init(Agg, Val, Idxs, NameStr);
  1678. }
  1679. InsertValueInst::InsertValueInst(Value *Agg,
  1680. Value *Val,
  1681. ArrayRef<unsigned> Idxs,
  1682. const Twine &NameStr,
  1683. BasicBlock *InsertAtEnd)
  1684. : Instruction(Agg->getType(), InsertValue,
  1685. OperandTraits<InsertValueInst>::op_begin(this),
  1686. 2, InsertAtEnd) {
  1687. init(Agg, Val, Idxs, NameStr);
  1688. }
  1689. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
  1690. //===----------------------------------------------------------------------===//
  1691. // PHINode Class
  1692. //===----------------------------------------------------------------------===//
  1693. // PHINode - The PHINode class is used to represent the magical mystical PHI
  1694. // node, that can not exist in nature, but can be synthesized in a computer
  1695. // scientist's overactive imagination.
  1696. //
  1697. class PHINode : public Instruction {
  1698. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  1699. /// ReservedSpace - The number of operands actually allocated. NumOperands is
  1700. /// the number actually in use.
  1701. unsigned ReservedSpace;
  1702. PHINode(const PHINode &PN);
  1703. // allocate space for exactly zero operands
  1704. void *operator new(size_t s) {
  1705. return User::operator new(s, 0);
  1706. }
  1707. explicit PHINode(Type *Ty, unsigned NumReservedValues,
  1708. const Twine &NameStr = "", Instruction *InsertBefore = 0)
  1709. : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
  1710. ReservedSpace(NumReservedValues) {
  1711. setName(NameStr);
  1712. OperandList = allocHungoffUses(ReservedSpace);
  1713. }
  1714. PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
  1715. BasicBlock *InsertAtEnd)
  1716. : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
  1717. ReservedSpace(NumReservedValues) {
  1718. setName(NameStr);
  1719. OperandList = allocHungoffUses(ReservedSpace);
  1720. }
  1721. protected:
  1722. // allocHungoffUses - this is more complicated than the generic
  1723. // User::allocHungoffUses, because we have to allocate Uses for the incoming
  1724. // values and pointers to the incoming blocks, all in one allocation.
  1725. Use *allocHungoffUses(unsigned) const;
  1726. virtual PHINode *clone_impl() const;
  1727. public:
  1728. /// Constructors - NumReservedValues is a hint for the number of incoming
  1729. /// edges that this phi node will have (use 0 if you really have no idea).
  1730. static PHINode *Create(Type *Ty, unsigned NumReservedValues,
  1731. const Twine &NameStr = "",
  1732. Instruction *InsertBefore = 0) {
  1733. return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
  1734. }
  1735. static PHINode *Create(Type *Ty, unsigned NumReservedValues,
  1736. const Twine &NameStr, BasicBlock *InsertAtEnd) {
  1737. return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
  1738. }
  1739. ~PHINode();
  1740. /// Provide fast operand accessors
  1741. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1742. // Block iterator interface. This provides access to the list of incoming
  1743. // basic blocks, which parallels the list of incoming values.
  1744. typedef BasicBlock **block_iterator;
  1745. typedef BasicBlock * const *const_block_iterator;
  1746. block_iterator block_begin() {
  1747. Use::UserRef *ref =
  1748. reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
  1749. return reinterpret_cast<block_iterator>(ref + 1);
  1750. }
  1751. const_block_iterator block_begin() const {
  1752. const Use::UserRef *ref =
  1753. reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
  1754. return reinterpret_cast<const_block_iterator>(ref + 1);
  1755. }
  1756. block_iterator block_end() {
  1757. return block_begin() + getNumOperands();
  1758. }
  1759. const_block_iterator block_end() const {
  1760. return block_begin() + getNumOperands();
  1761. }
  1762. /// getNumIncomingValues - Return the number of incoming edges
  1763. ///
  1764. unsigned getNumIncomingValues() const { return getNumOperands(); }
  1765. /// getIncomingValue - Return incoming value number x
  1766. ///
  1767. Value *getIncomingValue(unsigned i) const {
  1768. return getOperand(i);
  1769. }
  1770. void setIncomingValue(unsigned i, Value *V) {
  1771. setOperand(i, V);
  1772. }
  1773. static unsigned getOperandNumForIncomingValue(unsigned i) {
  1774. return i;
  1775. }
  1776. static unsigned getIncomingValueNumForOperand(unsigned i) {
  1777. return i;
  1778. }
  1779. /// getIncomingBlock - Return incoming basic block number @p i.
  1780. ///
  1781. BasicBlock *getIncomingBlock(unsigned i) const {
  1782. return block_begin()[i];
  1783. }
  1784. /// getIncomingBlock - Return incoming basic block corresponding
  1785. /// to an operand of the PHI.
  1786. ///
  1787. BasicBlock *getIncomingBlock(const Use &U) const {
  1788. assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
  1789. return getIncomingBlock(unsigned(&U - op_begin()));
  1790. }
  1791. /// getIncomingBlock - Return incoming basic block corresponding
  1792. /// to value use iterator.
  1793. ///
  1794. template <typename U>
  1795. BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
  1796. return getIncomingBlock(I.getUse());
  1797. }
  1798. void setIncomingBlock(unsigned i, BasicBlock *BB) {
  1799. block_begin()[i] = BB;
  1800. }
  1801. /// addIncoming - Add an incoming value to the end of the PHI list
  1802. ///
  1803. void addIncoming(Value *V, BasicBlock *BB) {
  1804. assert(V && "PHI node got a null value!");
  1805. assert(BB && "PHI node got a null basic block!");
  1806. assert(getType() == V->getType() &&
  1807. "All operands to PHI node must be the same type as the PHI node!");
  1808. if (NumOperands == ReservedSpace)
  1809. growOperands(); // Get more space!
  1810. // Initialize some new operands.
  1811. ++NumOperands;
  1812. setIncomingValue(NumOperands - 1, V);
  1813. setIncomingBlock(NumOperands - 1, BB);
  1814. }
  1815. /// removeIncomingValue - Remove an incoming value. This is useful if a
  1816. /// predecessor basic block is deleted. The value removed is returned.
  1817. ///
  1818. /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
  1819. /// is true), the PHI node is destroyed and any uses of it are replaced with
  1820. /// dummy values. The only time there should be zero incoming values to a PHI
  1821. /// node is when the block is dead, so this strategy is sound.
  1822. ///
  1823. Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
  1824. Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
  1825. int Idx = getBasicBlockIndex(BB);
  1826. assert(Idx >= 0 && "Invalid basic block argument to remove!");
  1827. return removeIncomingValue(Idx, DeletePHIIfEmpty);
  1828. }
  1829. /// getBasicBlockIndex - Return the first index of the specified basic
  1830. /// block in the value list for this PHI. Returns -1 if no instance.
  1831. ///
  1832. int getBasicBlockIndex(const BasicBlock *BB) const {
  1833. for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
  1834. if (block_begin()[i] == BB)
  1835. return i;
  1836. return -1;
  1837. }
  1838. Value *getIncomingValueForBlock(const BasicBlock *BB) const {
  1839. int Idx = getBasicBlockIndex(BB);
  1840. assert(Idx >= 0 && "Invalid basic block argument!");
  1841. return getIncomingValue(Idx);
  1842. }
  1843. /// hasConstantValue - If the specified PHI node always merges together the
  1844. /// same value, return the value, otherwise return null.
  1845. Value *hasConstantValue() const;
  1846. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  1847. static inline bool classof(const Instruction *I) {
  1848. return I->getOpcode() == Instruction::PHI;
  1849. }
  1850. static inline bool classof(const Value *V) {
  1851. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1852. }
  1853. private:
  1854. void growOperands();
  1855. };
  1856. template <>
  1857. struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
  1858. };
  1859. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
  1860. //===----------------------------------------------------------------------===//
  1861. // LandingPadInst Class
  1862. //===----------------------------------------------------------------------===//
  1863. //===---------------------------------------------------------------------------
  1864. /// LandingPadInst - The landingpad instruction holds all of the information
  1865. /// necessary to generate correct exception handling. The landingpad instruction
  1866. /// cannot be moved from the top of a landing pad block, which itself is
  1867. /// accessible only from the 'unwind' edge of an invoke. This uses the
  1868. /// SubclassData field in Value to store whether or not the landingpad is a
  1869. /// cleanup.
  1870. ///
  1871. class LandingPadInst : public Instruction {
  1872. /// ReservedSpace - The number of operands actually allocated. NumOperands is
  1873. /// the number actually in use.
  1874. unsigned ReservedSpace;
  1875. LandingPadInst(const LandingPadInst &LP);
  1876. public:
  1877. enum ClauseType { Catch, Filter };
  1878. private:
  1879. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  1880. // Allocate space for exactly zero operands.
  1881. void *operator new(size_t s) {
  1882. return User::operator new(s, 0);
  1883. }
  1884. void growOperands(unsigned Size);
  1885. void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
  1886. explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
  1887. unsigned NumReservedValues, const Twine &NameStr,
  1888. Instruction *InsertBefore);
  1889. explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
  1890. unsigned NumReservedValues, const Twine &NameStr,
  1891. BasicBlock *InsertAtEnd);
  1892. protected:
  1893. virtual LandingPadInst *clone_impl() const;
  1894. public:
  1895. /// Constructors - NumReservedClauses is a hint for the number of incoming
  1896. /// clauses that this landingpad will have (use 0 if you really have no idea).
  1897. static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
  1898. unsigned NumReservedClauses,
  1899. const Twine &NameStr = "",
  1900. Instruction *InsertBefore = 0);
  1901. static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
  1902. unsigned NumReservedClauses,
  1903. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1904. ~LandingPadInst();
  1905. /// Provide fast operand accessors
  1906. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1907. /// getPersonalityFn - Get the personality function associated with this
  1908. /// landing pad.
  1909. Value *getPersonalityFn() const { return getOperand(0); }
  1910. /// isCleanup - Return 'true' if this landingpad instruction is a
  1911. /// cleanup. I.e., it should be run when unwinding even if its landing pad
  1912. /// doesn't catch the exception.
  1913. bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
  1914. /// setCleanup - Indicate that this landingpad instruction is a cleanup.
  1915. void setCleanup(bool V) {
  1916. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  1917. (V ? 1 : 0));
  1918. }
  1919. /// addClause - Add a catch or filter clause to the landing pad.
  1920. void addClause(Value *ClauseVal);
  1921. /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
  1922. /// to determine what type of clause this is.
  1923. Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
  1924. /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
  1925. bool isCatch(unsigned Idx) const {
  1926. return !isa<ArrayType>(OperandList[Idx + 1]->getType());
  1927. }
  1928. /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
  1929. bool isFilter(unsigned Idx) const {
  1930. return isa<ArrayType>(OperandList[Idx + 1]->getType());
  1931. }
  1932. /// getNumClauses - Get the number of clauses for this landing pad.
  1933. unsigned getNumClauses() const { return getNumOperands() - 1; }
  1934. /// reserveClauses - Grow the size of the operand list to accommodate the new
  1935. /// number of clauses.
  1936. void reserveClauses(unsigned Size) { growOperands(Size); }
  1937. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1938. static inline bool classof(const Instruction *I) {
  1939. return I->getOpcode() == Instruction::LandingPad;
  1940. }
  1941. static inline bool classof(const Value *V) {
  1942. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1943. }
  1944. };
  1945. template <>
  1946. struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
  1947. };
  1948. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
  1949. //===----------------------------------------------------------------------===//
  1950. // ReturnInst Class
  1951. //===----------------------------------------------------------------------===//
  1952. //===---------------------------------------------------------------------------
  1953. /// ReturnInst - Return a value (possibly void), from a function. Execution
  1954. /// does not continue in this function any longer.
  1955. ///
  1956. class ReturnInst : public TerminatorInst {
  1957. ReturnInst(const ReturnInst &RI);
  1958. private:
  1959. // ReturnInst constructors:
  1960. // ReturnInst() - 'ret void' instruction
  1961. // ReturnInst( null) - 'ret void' instruction
  1962. // ReturnInst(Value* X) - 'ret X' instruction
  1963. // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
  1964. // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
  1965. // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
  1966. // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
  1967. //
  1968. // NOTE: If the Value* passed is of type void then the constructor behaves as
  1969. // if it was passed NULL.
  1970. explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
  1971. Instruction *InsertBefore = 0);
  1972. ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
  1973. explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
  1974. protected:
  1975. virtual ReturnInst *clone_impl() const;
  1976. public:
  1977. static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
  1978. Instruction *InsertBefore = 0) {
  1979. return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
  1980. }
  1981. static ReturnInst* Create(LLVMContext &C, Value *retVal,
  1982. BasicBlock *InsertAtEnd) {
  1983. return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
  1984. }
  1985. static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
  1986. return new(0) ReturnInst(C, InsertAtEnd);
  1987. }
  1988. virtual ~ReturnInst();
  1989. /// Provide fast operand accessors
  1990. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1991. /// Convenience accessor. Returns null if there is no return value.
  1992. Value *getReturnValue() const {
  1993. return getNumOperands() != 0 ? getOperand(0) : 0;
  1994. }
  1995. unsigned getNumSuccessors() const { return 0; }
  1996. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1997. static inline bool classof(const Instruction *I) {
  1998. return (I->getOpcode() == Instruction::Ret);
  1999. }
  2000. static inline bool classof(const Value *V) {
  2001. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2002. }
  2003. private:
  2004. virtual BasicBlock *getSuccessorV(unsigned idx) const;
  2005. virtual unsigned getNumSuccessorsV() const;
  2006. virtual void setSuccessorV(unsigned idx, BasicBlock *B);
  2007. };
  2008. template <>
  2009. struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
  2010. };
  2011. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
  2012. //===----------------------------------------------------------------------===//
  2013. // BranchInst Class
  2014. //===----------------------------------------------------------------------===//
  2015. //===---------------------------------------------------------------------------
  2016. /// BranchInst - Conditional or Unconditional Branch instruction.
  2017. ///
  2018. class BranchInst : public TerminatorInst {
  2019. /// Ops list - Branches are strange. The operands are ordered:
  2020. /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
  2021. /// they don't have to check for cond/uncond branchness. These are mostly
  2022. /// accessed relative from op_end().
  2023. BranchInst(const BranchInst &BI);
  2024. void AssertOK();
  2025. // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
  2026. // BranchInst(BB *B) - 'br B'
  2027. // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
  2028. // BranchInst(BB* B, Inst *I) - 'br B' insert before I
  2029. // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
  2030. // BranchInst(BB* B, BB *I) - 'br B' insert at end
  2031. // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
  2032. explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
  2033. BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
  2034. Instruction *InsertBefore = 0);
  2035. BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
  2036. BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
  2037. BasicBlock *InsertAtEnd);
  2038. protected:
  2039. virtual BranchInst *clone_impl() const;
  2040. public:
  2041. static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
  2042. return new(1) BranchInst(IfTrue, InsertBefore);
  2043. }
  2044. static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
  2045. Value *Cond, Instruction *InsertBefore = 0) {
  2046. return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
  2047. }
  2048. static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
  2049. return new(1) BranchInst(IfTrue, InsertAtEnd);
  2050. }
  2051. static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
  2052. Value *Cond, BasicBlock *InsertAtEnd) {
  2053. return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
  2054. }
  2055. /// Transparently provide more efficient getOperand methods.
  2056. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2057. bool isUnconditional() const { return getNumOperands() == 1; }
  2058. bool isConditional() const { return getNumOperands() == 3; }
  2059. Value *getCondition() const {
  2060. assert(isConditional() && "Cannot get condition of an uncond branch!");
  2061. return Op<-3>();
  2062. }
  2063. void setCondition(Value *V) {
  2064. assert(isConditional() && "Cannot set condition of unconditional branch!");
  2065. Op<-3>() = V;
  2066. }
  2067. unsigned getNumSuccessors() const { return 1+isConditional(); }
  2068. BasicBlock *getSuccessor(unsigned i) const {
  2069. assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
  2070. return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
  2071. }
  2072. void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
  2073. assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
  2074. *(&Op<-1>() - idx) = (Value*)NewSucc;
  2075. }
  2076. /// \brief Swap the successors of this branch instruction.
  2077. ///
  2078. /// Swaps the successors of the branch instruction. This also swaps any
  2079. /// branch weight metadata associated with the instruction so that it
  2080. /// continues to map correctly to each operand.
  2081. void swapSuccessors();
  2082. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2083. static inline bool classof(const Instruction *I) {
  2084. return (I->getOpcode() == Instruction::Br);
  2085. }
  2086. static inline bool classof(const Value *V) {
  2087. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2088. }
  2089. private:
  2090. virtual BasicBlock *getSuccessorV(unsigned idx) const;
  2091. virtual unsigned getNumSuccessorsV() const;
  2092. virtual void setSuccessorV(unsigned idx, BasicBlock *B);
  2093. };
  2094. template <>
  2095. struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
  2096. };
  2097. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
  2098. //===----------------------------------------------------------------------===//
  2099. // SwitchInst Class
  2100. //===----------------------------------------------------------------------===//
  2101. //===---------------------------------------------------------------------------
  2102. /// SwitchInst - Multiway switch
  2103. ///
  2104. class SwitchInst : public TerminatorInst {
  2105. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  2106. unsigned ReservedSpace;
  2107. // Operands format:
  2108. // Operand[0] = Value to switch on
  2109. // Operand[1] = Default basic block destination
  2110. // Operand[2n ] = Value to match
  2111. // Operand[2n+1] = BasicBlock to go to on match
  2112. // Store case values separately from operands list. We needn't User-Use
  2113. // concept here, since it is just a case value, it will always constant,
  2114. // and case value couldn't reused with another instructions/values.
  2115. // Additionally:
  2116. // It allows us to use custom type for case values that is not inherited
  2117. // from Value. Since case value is a complex type that implements
  2118. // the subset of integers, we needn't extract sub-constants within
  2119. // slow getAggregateElement method.
  2120. // For case values we will use std::list to by two reasons:
  2121. // 1. It allows to add/remove cases without whole collection reallocation.
  2122. // 2. In most of cases we needn't random access.
  2123. // Currently case values are also stored in Operands List, but it will moved
  2124. // out in future commits.
  2125. typedef std::list<IntegersSubset> Subsets;
  2126. typedef Subsets::iterator SubsetsIt;
  2127. typedef Subsets::const_iterator SubsetsConstIt;
  2128. Subsets TheSubsets;
  2129. SwitchInst(const SwitchInst &SI);
  2130. void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
  2131. void growOperands();
  2132. // allocate space for exactly zero operands
  2133. void *operator new(size_t s) {
  2134. return User::operator new(s, 0);
  2135. }
  2136. /// SwitchInst ctor - Create a new switch instruction, specifying a value to
  2137. /// switch on and a default destination. The number of additional cases can
  2138. /// be specified here to make memory allocation more efficient. This
  2139. /// constructor can also autoinsert before another instruction.
  2140. SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
  2141. Instruction *InsertBefore);
  2142. /// SwitchInst ctor - Create a new switch instruction, specifying a value to
  2143. /// switch on and a default destination. The number of additional cases can
  2144. /// be specified here to make memory allocation more efficient. This
  2145. /// constructor also autoinserts at the end of the specified BasicBlock.
  2146. SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
  2147. BasicBlock *InsertAtEnd);
  2148. protected:
  2149. virtual SwitchInst *clone_impl() const;
  2150. public:
  2151. // FIXME: Currently there are a lot of unclean template parameters,
  2152. // we need to make refactoring in future.
  2153. // All these parameters are used to implement both iterator and const_iterator
  2154. // without code duplication.
  2155. // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
  2156. // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
  2157. // SubsetsItTy may be SubsetsConstIt or SubsetsIt
  2158. // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
  2159. template <class SwitchInstTy, class ConstantIntTy,
  2160. class SubsetsItTy, class BasicBlockTy>
  2161. class CaseIteratorT;
  2162. typedef CaseIteratorT<const SwitchInst, const ConstantInt,
  2163. SubsetsConstIt, const BasicBlock> ConstCaseIt;
  2164. class CaseIt;
  2165. // -2
  2166. static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
  2167. static SwitchInst *Create(Value *Value, BasicBlock *Default,
  2168. unsigned NumCases, Instruction *InsertBefore = 0) {
  2169. return new SwitchInst(Value, Default, NumCases, InsertBefore);
  2170. }
  2171. static SwitchInst *Create(Value *Value, BasicBlock *Default,
  2172. unsigned NumCases, BasicBlock *InsertAtEnd) {
  2173. return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
  2174. }
  2175. ~SwitchInst();
  2176. /// Provide fast operand accessors
  2177. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2178. // Accessor Methods for Switch stmt
  2179. Value *getCondition() const { return getOperand(0); }
  2180. void setCondition(Value *V) { setOperand(0, V); }
  2181. BasicBlock *getDefaultDest() const {
  2182. return cast<BasicBlock>(getOperand(1));
  2183. }
  2184. void setDefaultDest(BasicBlock *DefaultCase) {
  2185. setOperand(1, reinterpret_cast<Value*>(DefaultCase));
  2186. }
  2187. /// getNumCases - return the number of 'cases' in this switch instruction,
  2188. /// except the default case
  2189. unsigned getNumCases() const {
  2190. return getNumOperands()/2 - 1;
  2191. }
  2192. /// Returns a read/write iterator that points to the first
  2193. /// case in SwitchInst.
  2194. CaseIt case_begin() {
  2195. return CaseIt(this, 0, TheSubsets.begin());
  2196. }
  2197. /// Returns a read-only iterator that points to the first
  2198. /// case in the SwitchInst.
  2199. ConstCaseIt case_begin() const {
  2200. return ConstCaseIt(this, 0, TheSubsets.begin());
  2201. }
  2202. /// Returns a read/write iterator that points one past the last
  2203. /// in the SwitchInst.
  2204. CaseIt case_end() {
  2205. return CaseIt(this, getNumCases(), TheSubsets.end());
  2206. }
  2207. /// Returns a read-only iterator that points one past the last
  2208. /// in the SwitchInst.
  2209. ConstCaseIt case_end() const {
  2210. return ConstCaseIt(this, getNumCases(), TheSubsets.end());
  2211. }
  2212. /// Returns an iterator that points to the default case.
  2213. /// Note: this iterator allows to resolve successor only. Attempt
  2214. /// to resolve case value causes an assertion.
  2215. /// Also note, that increment and decrement also causes an assertion and
  2216. /// makes iterator invalid.
  2217. CaseIt case_default() {
  2218. return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
  2219. }
  2220. ConstCaseIt case_default() const {
  2221. return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
  2222. }
  2223. /// findCaseValue - Search all of the case values for the specified constant.
  2224. /// If it is explicitly handled, return the case iterator of it, otherwise
  2225. /// return default case iterator to indicate
  2226. /// that it is handled by the default handler.
  2227. CaseIt findCaseValue(const ConstantInt *C) {
  2228. for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
  2229. if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
  2230. return i;
  2231. return case_default();
  2232. }
  2233. ConstCaseIt findCaseValue(const ConstantInt *C) const {
  2234. for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
  2235. if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
  2236. return i;
  2237. return case_default();
  2238. }
  2239. /// findCaseDest - Finds the unique case value for a given successor. Returns
  2240. /// null if the successor is not found, not unique, or is the default case.
  2241. ConstantInt *findCaseDest(BasicBlock *BB) {
  2242. if (BB == getDefaultDest()) return NULL;
  2243. ConstantInt *CI = NULL;
  2244. for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
  2245. if (i.getCaseSuccessor() == BB) {
  2246. if (CI) return NULL; // Multiple cases lead to BB.
  2247. else CI = i.getCaseValue();
  2248. }
  2249. }
  2250. return CI;
  2251. }
  2252. /// addCase - Add an entry to the switch instruction...
  2253. /// @deprecated
  2254. /// Note:
  2255. /// This action invalidates case_end(). Old case_end() iterator will
  2256. /// point to the added case.
  2257. void addCase(ConstantInt *OnVal, BasicBlock *Dest);
  2258. /// addCase - Add an entry to the switch instruction.
  2259. /// Note:
  2260. /// This action invalidates case_end(). Old case_end() iterator will
  2261. /// point to the added case.
  2262. void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
  2263. /// removeCase - This method removes the specified case and its successor
  2264. /// from the switch instruction. Note that this operation may reorder the
  2265. /// remaining cases at index idx and above.
  2266. /// Note:
  2267. /// This action invalidates iterators for all cases following the one removed,
  2268. /// including the case_end() iterator.
  2269. void removeCase(CaseIt& i);
  2270. unsigned getNumSuccessors() const { return getNumOperands()/2; }
  2271. BasicBlock *getSuccessor(unsigned idx) const {
  2272. assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
  2273. return cast<BasicBlock>(getOperand(idx*2+1));
  2274. }
  2275. void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
  2276. assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
  2277. setOperand(idx*2+1, (Value*)NewSucc);
  2278. }
  2279. uint16_t hash() const {
  2280. uint32_t NumberOfCases = (uint32_t)getNumCases();
  2281. uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
  2282. for (ConstCaseIt i = case_begin(), e = case_end();
  2283. i != e; ++i) {
  2284. uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
  2285. Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
  2286. }
  2287. return Hash;
  2288. }
  2289. // Case iterators definition.
  2290. template <class SwitchInstTy, class ConstantIntTy,
  2291. class SubsetsItTy, class BasicBlockTy>
  2292. class CaseIteratorT {
  2293. protected:
  2294. SwitchInstTy *SI;
  2295. unsigned Index;
  2296. SubsetsItTy SubsetIt;
  2297. /// Initializes case iterator for given SwitchInst and for given
  2298. /// case number.
  2299. friend class SwitchInst;
  2300. CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
  2301. SubsetsItTy CaseValueIt) {
  2302. this->SI = SI;
  2303. Index = SuccessorIndex;
  2304. this->SubsetIt = CaseValueIt;
  2305. }
  2306. public:
  2307. typedef typename SubsetsItTy::reference IntegersSubsetRef;
  2308. typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
  2309. SubsetsItTy, BasicBlockTy> Self;
  2310. CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
  2311. this->SI = SI;
  2312. Index = CaseNum;
  2313. SubsetIt = SI->TheSubsets.begin();
  2314. std::advance(SubsetIt, CaseNum);
  2315. }
  2316. /// Initializes case iterator for given SwitchInst and for given
  2317. /// TerminatorInst's successor index.
  2318. static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
  2319. assert(SuccessorIndex < SI->getNumSuccessors() &&
  2320. "Successor index # out of range!");
  2321. return SuccessorIndex != 0 ?
  2322. Self(SI, SuccessorIndex - 1) :
  2323. Self(SI, DefaultPseudoIndex);
  2324. }
  2325. /// Resolves case value for current case.
  2326. /// @deprecated
  2327. ConstantIntTy *getCaseValue() {
  2328. assert(Index < SI->getNumCases() && "Index out the number of cases.");
  2329. IntegersSubsetRef CaseRanges = *SubsetIt;
  2330. // FIXME: Currently we work with ConstantInt based cases.
  2331. // So return CaseValue as ConstantInt.
  2332. return CaseRanges.getSingleNumber(0).toConstantInt();
  2333. }
  2334. /// Resolves case value for current case.
  2335. IntegersSubsetRef getCaseValueEx() {
  2336. assert(Index < SI->getNumCases() && "Index out the number of cases.");
  2337. return *SubsetIt;
  2338. }
  2339. /// Resolves successor for current case.
  2340. BasicBlockTy *getCaseSuccessor() {
  2341. assert((Index < SI->getNumCases() ||
  2342. Index == DefaultPseudoIndex) &&
  2343. "Index out the number of cases.");
  2344. return SI->getSuccessor(getSuccessorIndex());
  2345. }
  2346. /// Returns number of current case.
  2347. unsigned getCaseIndex() const { return Index; }
  2348. /// Returns TerminatorInst's successor index for current case successor.
  2349. unsigned getSuccessorIndex() const {
  2350. assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
  2351. "Index out the number of cases.");
  2352. return Index != DefaultPseudoIndex ? Index + 1 : 0;
  2353. }
  2354. Self operator++() {
  2355. // Check index correctness after increment.
  2356. // Note: Index == getNumCases() means end().
  2357. assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
  2358. ++Index;
  2359. if (Index == 0)
  2360. SubsetIt = SI->TheSubsets.begin();
  2361. else
  2362. ++SubsetIt;
  2363. return *this;
  2364. }
  2365. Self operator++(int) {
  2366. Self tmp = *this;
  2367. ++(*this);
  2368. return tmp;
  2369. }
  2370. Self operator--() {
  2371. // Check index correctness after decrement.
  2372. // Note: Index == getNumCases() means end().
  2373. // Also allow "-1" iterator here. That will became valid after ++.
  2374. unsigned NumCases = SI->getNumCases();
  2375. assert((Index == 0 || Index-1 <= NumCases) &&
  2376. "Index out the number of cases.");
  2377. --Index;
  2378. if (Index == NumCases) {
  2379. SubsetIt = SI->TheSubsets.end();
  2380. return *this;
  2381. }
  2382. if (Index != -1U)
  2383. --SubsetIt;
  2384. return *this;
  2385. }
  2386. Self operator--(int) {
  2387. Self tmp = *this;
  2388. --(*this);
  2389. return tmp;
  2390. }
  2391. bool operator==(const Self& RHS) const {
  2392. assert(RHS.SI == SI && "Incompatible operators.");
  2393. return RHS.Index == Index;
  2394. }
  2395. bool operator!=(const Self& RHS) const {
  2396. assert(RHS.SI == SI && "Incompatible operators.");
  2397. return RHS.Index != Index;
  2398. }
  2399. };
  2400. class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
  2401. SubsetsIt, BasicBlock> {
  2402. typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
  2403. ParentTy;
  2404. protected:
  2405. friend class SwitchInst;
  2406. CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
  2407. ParentTy(SI, CaseNum, SubsetIt) {}
  2408. void updateCaseValueOperand(IntegersSubset& V) {
  2409. SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
  2410. }
  2411. public:
  2412. CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
  2413. CaseIt(const ParentTy& Src) : ParentTy(Src) {}
  2414. /// Sets the new value for current case.
  2415. /// @deprecated.
  2416. void setValue(ConstantInt *V) {
  2417. assert(Index < SI->getNumCases() && "Index out the number of cases.");
  2418. IntegersSubsetToBB Mapping;
  2419. // FIXME: Currently we work with ConstantInt based cases.
  2420. // So inititalize IntItem container directly from ConstantInt.
  2421. Mapping.add(IntItem::fromConstantInt(V));
  2422. *SubsetIt = Mapping.getCase();
  2423. updateCaseValueOperand(*SubsetIt);
  2424. }
  2425. /// Sets the new value for current case.
  2426. void setValueEx(IntegersSubset& V) {
  2427. assert(Index < SI->getNumCases() && "Index out the number of cases.");
  2428. *SubsetIt = V;
  2429. updateCaseValueOperand(*SubsetIt);
  2430. }
  2431. /// Sets the new successor for current case.
  2432. void setSuccessor(BasicBlock *S) {
  2433. SI->setSuccessor(getSuccessorIndex(), S);
  2434. }
  2435. };
  2436. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2437. static inline bool classof(const Instruction *I) {
  2438. return I->getOpcode() == Instruction::Switch;
  2439. }
  2440. static inline bool classof(const Value *V) {
  2441. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2442. }
  2443. private:
  2444. virtual BasicBlock *getSuccessorV(unsigned idx) const;
  2445. virtual unsigned getNumSuccessorsV() const;
  2446. virtual void setSuccessorV(unsigned idx, BasicBlock *B);
  2447. };
  2448. template <>
  2449. struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
  2450. };
  2451. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
  2452. //===----------------------------------------------------------------------===//
  2453. // IndirectBrInst Class
  2454. //===----------------------------------------------------------------------===//
  2455. //===---------------------------------------------------------------------------
  2456. /// IndirectBrInst - Indirect Branch Instruction.
  2457. ///
  2458. class IndirectBrInst : public TerminatorInst {
  2459. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  2460. unsigned ReservedSpace;
  2461. // Operand[0] = Value to switch on
  2462. // Operand[1] = Default basic block destination
  2463. // Operand[2n ] = Value to match
  2464. // Operand[2n+1] = BasicBlock to go to on match
  2465. IndirectBrInst(const IndirectBrInst &IBI);
  2466. void init(Value *Address, unsigned NumDests);
  2467. void growOperands();
  2468. // allocate space for exactly zero operands
  2469. void *operator new(size_t s) {
  2470. return User::operator new(s, 0);
  2471. }
  2472. /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
  2473. /// Address to jump to. The number of expected destinations can be specified
  2474. /// here to make memory allocation more efficient. This constructor can also
  2475. /// autoinsert before another instruction.
  2476. IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
  2477. /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
  2478. /// Address to jump to. The number of expected destinations can be specified
  2479. /// here to make memory allocation more efficient. This constructor also
  2480. /// autoinserts at the end of the specified BasicBlock.
  2481. IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
  2482. protected:
  2483. virtual IndirectBrInst *clone_impl() const;
  2484. public:
  2485. static IndirectBrInst *Create(Value *Address, unsigned NumDests,
  2486. Instruction *InsertBefore = 0) {
  2487. return new IndirectBrInst(Address, NumDests, InsertBefore);
  2488. }
  2489. static IndirectBrInst *Create(Value *Address, unsigned NumDests,
  2490. BasicBlock *InsertAtEnd) {
  2491. return new IndirectBrInst(Address, NumDests, InsertAtEnd);
  2492. }
  2493. ~IndirectBrInst();
  2494. /// Provide fast operand accessors.
  2495. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2496. // Accessor Methods for IndirectBrInst instruction.
  2497. Value *getAddress() { return getOperand(0); }
  2498. const Value *getAddress() const { return getOperand(0); }
  2499. void setAddress(Value *V) { setOperand(0, V); }
  2500. /// getNumDestinations - return the number of possible destinations in this
  2501. /// indirectbr instruction.
  2502. unsigned getNumDestinations() const { return getNumOperands()-1; }
  2503. /// getDestination - Return the specified destination.
  2504. BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
  2505. const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
  2506. /// addDestination - Add a destination.
  2507. ///
  2508. void addDestination(BasicBlock *Dest);
  2509. /// removeDestination - This method removes the specified successor from the
  2510. /// indirectbr instruction.
  2511. void removeDestination(unsigned i);
  2512. unsigned getNumSuccessors() const { return getNumOperands()-1; }
  2513. BasicBlock *getSuccessor(unsigned i) const {
  2514. return cast<BasicBlock>(getOperand(i+1));
  2515. }
  2516. void setSuccessor(unsigned i, BasicBlock *NewSucc) {
  2517. setOperand(i+1, (Value*)NewSucc);
  2518. }
  2519. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2520. static inline bool classof(const Instruction *I) {
  2521. return I->getOpcode() == Instruction::IndirectBr;
  2522. }
  2523. static inline bool classof(const Value *V) {
  2524. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2525. }
  2526. private:
  2527. virtual BasicBlock *getSuccessorV(unsigned idx) const;
  2528. virtual unsigned getNumSuccessorsV() const;
  2529. virtual void setSuccessorV(unsigned idx, BasicBlock *B);
  2530. };
  2531. template <>
  2532. struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
  2533. };
  2534. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
  2535. //===----------------------------------------------------------------------===//
  2536. // InvokeInst Class
  2537. //===----------------------------------------------------------------------===//
  2538. /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
  2539. /// calling convention of the call.
  2540. ///
  2541. class InvokeInst : public TerminatorInst {
  2542. AttributeSet AttributeList;
  2543. InvokeInst(const InvokeInst &BI);
  2544. void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
  2545. ArrayRef<Value *> Args, const Twine &NameStr);
  2546. /// Construct an InvokeInst given a range of arguments.
  2547. ///
  2548. /// \brief Construct an InvokeInst from a range of arguments
  2549. inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
  2550. ArrayRef<Value *> Args, unsigned Values,
  2551. const Twine &NameStr, Instruction *InsertBefore);
  2552. /// Construct an InvokeInst given a range of arguments.
  2553. ///
  2554. /// \brief Construct an InvokeInst from a range of arguments
  2555. inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
  2556. ArrayRef<Value *> Args, unsigned Values,
  2557. const Twine &NameStr, BasicBlock *InsertAtEnd);
  2558. protected:
  2559. virtual InvokeInst *clone_impl() const;
  2560. public:
  2561. static InvokeInst *Create(Value *Func,
  2562. BasicBlock *IfNormal, BasicBlock *IfException,
  2563. ArrayRef<Value *> Args, const Twine &NameStr = "",
  2564. Instruction *InsertBefore = 0) {
  2565. unsigned Values = unsigned(Args.size()) + 3;
  2566. return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
  2567. Values, NameStr, InsertBefore);
  2568. }
  2569. static InvokeInst *Create(Value *Func,
  2570. BasicBlock *IfNormal, BasicBlock *IfException,
  2571. ArrayRef<Value *> Args, const Twine &NameStr,
  2572. BasicBlock *InsertAtEnd) {
  2573. unsigned Values = unsigned(Args.size()) + 3;
  2574. return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
  2575. Values, NameStr, InsertAtEnd);
  2576. }
  2577. /// Provide fast operand accessors
  2578. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2579. /// getNumArgOperands - Return the number of invoke arguments.
  2580. ///
  2581. unsigned getNumArgOperands() const { return getNumOperands() - 3; }
  2582. /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
  2583. ///
  2584. Value *getArgOperand(unsigned i) const { return getOperand(i); }
  2585. void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
  2586. /// getCallingConv/setCallingConv - Get or set the calling convention of this
  2587. /// function call.
  2588. CallingConv::ID getCallingConv() const {
  2589. return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
  2590. }
  2591. void setCallingConv(CallingConv::ID CC) {
  2592. setInstructionSubclassData(static_cast<unsigned>(CC));
  2593. }
  2594. /// getAttributes - Return the parameter attributes for this invoke.
  2595. ///
  2596. const AttributeSet &getAttributes() const { return AttributeList; }
  2597. /// setAttributes - Set the parameter attributes for this invoke.
  2598. ///
  2599. void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
  2600. /// addAttribute - adds the attribute to the list of attributes.
  2601. void addAttribute(unsigned i, Attribute::AttrKind attr);
  2602. /// removeAttribute - removes the attribute from the list of attributes.
  2603. void removeAttribute(unsigned i, Attribute attr);
  2604. /// \brief Determine whether this call has the NoAlias attribute.
  2605. bool hasFnAttr(Attribute::AttrKind A) const;
  2606. /// \brief Determine whether the call or the callee has the given attributes.
  2607. bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
  2608. /// \brief Extract the alignment for a call or parameter (0=unknown).
  2609. unsigned getParamAlignment(unsigned i) const {
  2610. return AttributeList.getParamAlignment(i);
  2611. }
  2612. /// \brief Return true if the call should not be inlined.
  2613. bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
  2614. void setIsNoInline() {
  2615. addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
  2616. }
  2617. /// \brief Determine if the call does not access memory.
  2618. bool doesNotAccessMemory() const {
  2619. return hasFnAttr(Attribute::ReadNone);
  2620. }
  2621. void setDoesNotAccessMemory() {
  2622. addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
  2623. }
  2624. /// \brief Determine if the call does not access or only reads memory.
  2625. bool onlyReadsMemory() const {
  2626. return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
  2627. }
  2628. void setOnlyReadsMemory() {
  2629. addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
  2630. }
  2631. /// \brief Determine if the call cannot return.
  2632. bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
  2633. void setDoesNotReturn() {
  2634. addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
  2635. }
  2636. /// \brief Determine if the call cannot unwind.
  2637. bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
  2638. void setDoesNotThrow() {
  2639. addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
  2640. }
  2641. /// \brief Determine if the call returns a structure through first
  2642. /// pointer argument.
  2643. bool hasStructRetAttr() const {
  2644. // Be friendly and also check the callee.
  2645. return paramHasAttr(1, Attribute::StructRet);
  2646. }
  2647. /// \brief Determine if any call argument is an aggregate passed by value.
  2648. bool hasByValArgument() const {
  2649. return AttributeList.hasAttrSomewhere(Attribute::ByVal);
  2650. }
  2651. /// getCalledFunction - Return the function called, or null if this is an
  2652. /// indirect function invocation.
  2653. ///
  2654. Function *getCalledFunction() const {
  2655. return dyn_cast<Function>(Op<-3>());
  2656. }
  2657. /// getCalledValue - Get a pointer to the function that is invoked by this
  2658. /// instruction
  2659. const Value *getCalledValue() const { return Op<-3>(); }
  2660. Value *getCalledValue() { return Op<-3>(); }
  2661. /// setCalledFunction - Set the function called.
  2662. void setCalledFunction(Value* Fn) {
  2663. Op<-3>() = Fn;
  2664. }
  2665. // get*Dest - Return the destination basic blocks...
  2666. BasicBlock *getNormalDest() const {
  2667. return cast<BasicBlock>(Op<-2>());
  2668. }
  2669. BasicBlock *getUnwindDest() const {
  2670. return cast<BasicBlock>(Op<-1>());
  2671. }
  2672. void setNormalDest(BasicBlock *B) {
  2673. Op<-2>() = reinterpret_cast<Value*>(B);
  2674. }
  2675. void setUnwindDest(BasicBlock *B) {
  2676. Op<-1>() = reinterpret_cast<Value*>(B);
  2677. }
  2678. /// getLandingPadInst - Get the landingpad instruction from the landing pad
  2679. /// block (the unwind destination).
  2680. LandingPadInst *getLandingPadInst() const;
  2681. BasicBlock *getSuccessor(unsigned i) const {
  2682. assert(i < 2 && "Successor # out of range for invoke!");
  2683. return i == 0 ? getNormalDest() : getUnwindDest();
  2684. }
  2685. void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
  2686. assert(idx < 2 && "Successor # out of range for invoke!");
  2687. *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
  2688. }
  2689. unsigned getNumSuccessors() const { return 2; }
  2690. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2691. static inline bool classof(const Instruction *I) {
  2692. return (I->getOpcode() == Instruction::Invoke);
  2693. }
  2694. static inline bool classof(const Value *V) {
  2695. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2696. }
  2697. private:
  2698. virtual BasicBlock *getSuccessorV(unsigned idx) const;
  2699. virtual unsigned getNumSuccessorsV() const;
  2700. virtual void setSuccessorV(unsigned idx, BasicBlock *B);
  2701. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  2702. // method so that subclasses cannot accidentally use it.
  2703. void setInstructionSubclassData(unsigned short D) {
  2704. Instruction::setInstructionSubclassData(D);
  2705. }
  2706. };
  2707. template <>
  2708. struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
  2709. };
  2710. InvokeInst::InvokeInst(Value *Func,
  2711. BasicBlock *IfNormal, BasicBlock *IfException,
  2712. ArrayRef<Value *> Args, unsigned Values,
  2713. const Twine &NameStr, Instruction *InsertBefore)
  2714. : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
  2715. ->getElementType())->getReturnType(),
  2716. Instruction::Invoke,
  2717. OperandTraits<InvokeInst>::op_end(this) - Values,
  2718. Values, InsertBefore) {
  2719. init(Func, IfNormal, IfException, Args, NameStr);
  2720. }
  2721. InvokeInst::InvokeInst(Value *Func,
  2722. BasicBlock *IfNormal, BasicBlock *IfException,
  2723. ArrayRef<Value *> Args, unsigned Values,
  2724. const Twine &NameStr, BasicBlock *InsertAtEnd)
  2725. : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
  2726. ->getElementType())->getReturnType(),
  2727. Instruction::Invoke,
  2728. OperandTraits<InvokeInst>::op_end(this) - Values,
  2729. Values, InsertAtEnd) {
  2730. init(Func, IfNormal, IfException, Args, NameStr);
  2731. }
  2732. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
  2733. //===----------------------------------------------------------------------===//
  2734. // ResumeInst Class
  2735. //===----------------------------------------------------------------------===//
  2736. //===---------------------------------------------------------------------------
  2737. /// ResumeInst - Resume the propagation of an exception.
  2738. ///
  2739. class ResumeInst : public TerminatorInst {
  2740. ResumeInst(const ResumeInst &RI);
  2741. explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
  2742. ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
  2743. protected:
  2744. virtual ResumeInst *clone_impl() const;
  2745. public:
  2746. static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
  2747. return new(1) ResumeInst(Exn, InsertBefore);
  2748. }
  2749. static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
  2750. return new(1) ResumeInst(Exn, InsertAtEnd);
  2751. }
  2752. /// Provide fast operand accessors
  2753. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2754. /// Convenience accessor.
  2755. Value *getValue() const { return Op<0>(); }
  2756. unsigned getNumSuccessors() const { return 0; }
  2757. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2758. static inline bool classof(const Instruction *I) {
  2759. return I->getOpcode() == Instruction::Resume;
  2760. }
  2761. static inline bool classof(const Value *V) {
  2762. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2763. }
  2764. private:
  2765. virtual BasicBlock *getSuccessorV(unsigned idx) const;
  2766. virtual unsigned getNumSuccessorsV() const;
  2767. virtual void setSuccessorV(unsigned idx, BasicBlock *B);
  2768. };
  2769. template <>
  2770. struct OperandTraits<ResumeInst> :
  2771. public FixedNumOperandTraits<ResumeInst, 1> {
  2772. };
  2773. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
  2774. //===----------------------------------------------------------------------===//
  2775. // UnreachableInst Class
  2776. //===----------------------------------------------------------------------===//
  2777. //===---------------------------------------------------------------------------
  2778. /// UnreachableInst - This function has undefined behavior. In particular, the
  2779. /// presence of this instruction indicates some higher level knowledge that the
  2780. /// end of the block cannot be reached.
  2781. ///
  2782. class UnreachableInst : public TerminatorInst {
  2783. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  2784. protected:
  2785. virtual UnreachableInst *clone_impl() const;
  2786. public:
  2787. // allocate space for exactly zero operands
  2788. void *operator new(size_t s) {
  2789. return User::operator new(s, 0);
  2790. }
  2791. explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
  2792. explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
  2793. unsigned getNumSuccessors() const { return 0; }
  2794. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2795. static inline bool classof(const Instruction *I) {
  2796. return I->getOpcode() == Instruction::Unreachable;
  2797. }
  2798. static inline bool classof(const Value *V) {
  2799. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2800. }
  2801. private:
  2802. virtual BasicBlock *getSuccessorV(unsigned idx) const;
  2803. virtual unsigned getNumSuccessorsV() const;
  2804. virtual void setSuccessorV(unsigned idx, BasicBlock *B);
  2805. };
  2806. //===----------------------------------------------------------------------===//
  2807. // TruncInst Class
  2808. //===----------------------------------------------------------------------===//
  2809. /// \brief This class represents a truncation of integer types.
  2810. class TruncInst : public CastInst {
  2811. protected:
  2812. /// \brief Clone an identical TruncInst
  2813. virtual TruncInst *clone_impl() const;
  2814. public:
  2815. /// \brief Constructor with insert-before-instruction semantics
  2816. TruncInst(
  2817. Value *S, ///< The value to be truncated
  2818. Type *Ty, ///< The (smaller) type to truncate to
  2819. const Twine &NameStr = "", ///< A name for the new instruction
  2820. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  2821. );
  2822. /// \brief Constructor with insert-at-end-of-block semantics
  2823. TruncInst(
  2824. Value *S, ///< The value to be truncated
  2825. Type *Ty, ///< The (smaller) type to truncate to
  2826. const Twine &NameStr, ///< A name for the new instruction
  2827. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  2828. );
  2829. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  2830. static inline bool classof(const Instruction *I) {
  2831. return I->getOpcode() == Trunc;
  2832. }
  2833. static inline bool classof(const Value *V) {
  2834. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2835. }
  2836. };
  2837. //===----------------------------------------------------------------------===//
  2838. // ZExtInst Class
  2839. //===----------------------------------------------------------------------===//
  2840. /// \brief This class represents zero extension of integer types.
  2841. class ZExtInst : public CastInst {
  2842. protected:
  2843. /// \brief Clone an identical ZExtInst
  2844. virtual ZExtInst *clone_impl() const;
  2845. public:
  2846. /// \brief Constructor with insert-before-instruction semantics
  2847. ZExtInst(
  2848. Value *S, ///< The value to be zero extended
  2849. Type *Ty, ///< The type to zero extend to
  2850. const Twine &NameStr = "", ///< A name for the new instruction
  2851. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  2852. );
  2853. /// \brief Constructor with insert-at-end semantics.
  2854. ZExtInst(
  2855. Value *S, ///< The value to be zero extended
  2856. Type *Ty, ///< The type to zero extend to
  2857. const Twine &NameStr, ///< A name for the new instruction
  2858. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  2859. );
  2860. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  2861. static inline bool classof(const Instruction *I) {
  2862. return I->getOpcode() == ZExt;
  2863. }
  2864. static inline bool classof(const Value *V) {
  2865. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2866. }
  2867. };
  2868. //===----------------------------------------------------------------------===//
  2869. // SExtInst Class
  2870. //===----------------------------------------------------------------------===//
  2871. /// \brief This class represents a sign extension of integer types.
  2872. class SExtInst : public CastInst {
  2873. protected:
  2874. /// \brief Clone an identical SExtInst
  2875. virtual SExtInst *clone_impl() const;
  2876. public:
  2877. /// \brief Constructor with insert-before-instruction semantics
  2878. SExtInst(
  2879. Value *S, ///< The value to be sign extended
  2880. Type *Ty, ///< The type to sign extend to
  2881. const Twine &NameStr = "", ///< A name for the new instruction
  2882. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  2883. );
  2884. /// \brief Constructor with insert-at-end-of-block semantics
  2885. SExtInst(
  2886. Value *S, ///< The value to be sign extended
  2887. Type *Ty, ///< The type to sign extend to
  2888. const Twine &NameStr, ///< A name for the new instruction
  2889. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  2890. );
  2891. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  2892. static inline bool classof(const Instruction *I) {
  2893. return I->getOpcode() == SExt;
  2894. }
  2895. static inline bool classof(const Value *V) {
  2896. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2897. }
  2898. };
  2899. //===----------------------------------------------------------------------===//
  2900. // FPTruncInst Class
  2901. //===----------------------------------------------------------------------===//
  2902. /// \brief This class represents a truncation of floating point types.
  2903. class FPTruncInst : public CastInst {
  2904. protected:
  2905. /// \brief Clone an identical FPTruncInst
  2906. virtual FPTruncInst *clone_impl() const;
  2907. public:
  2908. /// \brief Constructor with insert-before-instruction semantics
  2909. FPTruncInst(
  2910. Value *S, ///< The value to be truncated
  2911. Type *Ty, ///< The type to truncate to
  2912. const Twine &NameStr = "", ///< A name for the new instruction
  2913. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  2914. );
  2915. /// \brief Constructor with insert-before-instruction semantics
  2916. FPTruncInst(
  2917. Value *S, ///< The value to be truncated
  2918. Type *Ty, ///< The type to truncate to
  2919. const Twine &NameStr, ///< A name for the new instruction
  2920. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  2921. );
  2922. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  2923. static inline bool classof(const Instruction *I) {
  2924. return I->getOpcode() == FPTrunc;
  2925. }
  2926. static inline bool classof(const Value *V) {
  2927. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2928. }
  2929. };
  2930. //===----------------------------------------------------------------------===//
  2931. // FPExtInst Class
  2932. //===----------------------------------------------------------------------===//
  2933. /// \brief This class represents an extension of floating point types.
  2934. class FPExtInst : public CastInst {
  2935. protected:
  2936. /// \brief Clone an identical FPExtInst
  2937. virtual FPExtInst *clone_impl() const;
  2938. public:
  2939. /// \brief Constructor with insert-before-instruction semantics
  2940. FPExtInst(
  2941. Value *S, ///< The value to be extended
  2942. Type *Ty, ///< The type to extend to
  2943. const Twine &NameStr = "", ///< A name for the new instruction
  2944. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  2945. );
  2946. /// \brief Constructor with insert-at-end-of-block semantics
  2947. FPExtInst(
  2948. Value *S, ///< The value to be extended
  2949. Type *Ty, ///< The type to extend to
  2950. const Twine &NameStr, ///< A name for the new instruction
  2951. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  2952. );
  2953. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  2954. static inline bool classof(const Instruction *I) {
  2955. return I->getOpcode() == FPExt;
  2956. }
  2957. static inline bool classof(const Value *V) {
  2958. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2959. }
  2960. };
  2961. //===----------------------------------------------------------------------===//
  2962. // UIToFPInst Class
  2963. //===----------------------------------------------------------------------===//
  2964. /// \brief This class represents a cast unsigned integer to floating point.
  2965. class UIToFPInst : public CastInst {
  2966. protected:
  2967. /// \brief Clone an identical UIToFPInst
  2968. virtual UIToFPInst *clone_impl() const;
  2969. public:
  2970. /// \brief Constructor with insert-before-instruction semantics
  2971. UIToFPInst(
  2972. Value *S, ///< The value to be converted
  2973. Type *Ty, ///< The type to convert to
  2974. const Twine &NameStr = "", ///< A name for the new instruction
  2975. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  2976. );
  2977. /// \brief Constructor with insert-at-end-of-block semantics
  2978. UIToFPInst(
  2979. Value *S, ///< The value to be converted
  2980. Type *Ty, ///< The type to convert to
  2981. const Twine &NameStr, ///< A name for the new instruction
  2982. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  2983. );
  2984. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  2985. static inline bool classof(const Instruction *I) {
  2986. return I->getOpcode() == UIToFP;
  2987. }
  2988. static inline bool classof(const Value *V) {
  2989. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2990. }
  2991. };
  2992. //===----------------------------------------------------------------------===//
  2993. // SIToFPInst Class
  2994. //===----------------------------------------------------------------------===//
  2995. /// \brief This class represents a cast from signed integer to floating point.
  2996. class SIToFPInst : public CastInst {
  2997. protected:
  2998. /// \brief Clone an identical SIToFPInst
  2999. virtual SIToFPInst *clone_impl() const;
  3000. public:
  3001. /// \brief Constructor with insert-before-instruction semantics
  3002. SIToFPInst(
  3003. Value *S, ///< The value to be converted
  3004. Type *Ty, ///< The type to convert to
  3005. const Twine &NameStr = "", ///< A name for the new instruction
  3006. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  3007. );
  3008. /// \brief Constructor with insert-at-end-of-block semantics
  3009. SIToFPInst(
  3010. Value *S, ///< The value to be converted
  3011. Type *Ty, ///< The type to convert to
  3012. const Twine &NameStr, ///< A name for the new instruction
  3013. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3014. );
  3015. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3016. static inline bool classof(const Instruction *I) {
  3017. return I->getOpcode() == SIToFP;
  3018. }
  3019. static inline bool classof(const Value *V) {
  3020. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3021. }
  3022. };
  3023. //===----------------------------------------------------------------------===//
  3024. // FPToUIInst Class
  3025. //===----------------------------------------------------------------------===//
  3026. /// \brief This class represents a cast from floating point to unsigned integer
  3027. class FPToUIInst : public CastInst {
  3028. protected:
  3029. /// \brief Clone an identical FPToUIInst
  3030. virtual FPToUIInst *clone_impl() const;
  3031. public:
  3032. /// \brief Constructor with insert-before-instruction semantics
  3033. FPToUIInst(
  3034. Value *S, ///< The value to be converted
  3035. Type *Ty, ///< The type to convert to
  3036. const Twine &NameStr = "", ///< A name for the new instruction
  3037. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  3038. );
  3039. /// \brief Constructor with insert-at-end-of-block semantics
  3040. FPToUIInst(
  3041. Value *S, ///< The value to be converted
  3042. Type *Ty, ///< The type to convert to
  3043. const Twine &NameStr, ///< A name for the new instruction
  3044. BasicBlock *InsertAtEnd ///< Where to insert the new instruction
  3045. );
  3046. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3047. static inline bool classof(const Instruction *I) {
  3048. return I->getOpcode() == FPToUI;
  3049. }
  3050. static inline bool classof(const Value *V) {
  3051. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3052. }
  3053. };
  3054. //===----------------------------------------------------------------------===//
  3055. // FPToSIInst Class
  3056. //===----------------------------------------------------------------------===//
  3057. /// \brief This class represents a cast from floating point to signed integer.
  3058. class FPToSIInst : public CastInst {
  3059. protected:
  3060. /// \brief Clone an identical FPToSIInst
  3061. virtual FPToSIInst *clone_impl() const;
  3062. public:
  3063. /// \brief Constructor with insert-before-instruction semantics
  3064. FPToSIInst(
  3065. Value *S, ///< The value to be converted
  3066. Type *Ty, ///< The type to convert to
  3067. const Twine &NameStr = "", ///< A name for the new instruction
  3068. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  3069. );
  3070. /// \brief Constructor with insert-at-end-of-block semantics
  3071. FPToSIInst(
  3072. Value *S, ///< The value to be converted
  3073. Type *Ty, ///< The type to convert to
  3074. const Twine &NameStr, ///< A name for the new instruction
  3075. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3076. );
  3077. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3078. static inline bool classof(const Instruction *I) {
  3079. return I->getOpcode() == FPToSI;
  3080. }
  3081. static inline bool classof(const Value *V) {
  3082. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3083. }
  3084. };
  3085. //===----------------------------------------------------------------------===//
  3086. // IntToPtrInst Class
  3087. //===----------------------------------------------------------------------===//
  3088. /// \brief This class represents a cast from an integer to a pointer.
  3089. class IntToPtrInst : public CastInst {
  3090. public:
  3091. /// \brief Constructor with insert-before-instruction semantics
  3092. IntToPtrInst(
  3093. Value *S, ///< The value to be converted
  3094. Type *Ty, ///< The type to convert to
  3095. const Twine &NameStr = "", ///< A name for the new instruction
  3096. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  3097. );
  3098. /// \brief Constructor with insert-at-end-of-block semantics
  3099. IntToPtrInst(
  3100. Value *S, ///< The value to be converted
  3101. Type *Ty, ///< The type to convert to
  3102. const Twine &NameStr, ///< A name for the new instruction
  3103. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3104. );
  3105. /// \brief Clone an identical IntToPtrInst
  3106. virtual IntToPtrInst *clone_impl() const;
  3107. /// \brief Returns the address space of this instruction's pointer type.
  3108. unsigned getAddressSpace() const {
  3109. return getType()->getPointerAddressSpace();
  3110. }
  3111. // Methods for support type inquiry through isa, cast, and dyn_cast:
  3112. static inline bool classof(const Instruction *I) {
  3113. return I->getOpcode() == IntToPtr;
  3114. }
  3115. static inline bool classof(const Value *V) {
  3116. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3117. }
  3118. };
  3119. //===----------------------------------------------------------------------===//
  3120. // PtrToIntInst Class
  3121. //===----------------------------------------------------------------------===//
  3122. /// \brief This class represents a cast from a pointer to an integer
  3123. class PtrToIntInst : public CastInst {
  3124. protected:
  3125. /// \brief Clone an identical PtrToIntInst
  3126. virtual PtrToIntInst *clone_impl() const;
  3127. public:
  3128. /// \brief Constructor with insert-before-instruction semantics
  3129. PtrToIntInst(
  3130. Value *S, ///< The value to be converted
  3131. Type *Ty, ///< The type to convert to
  3132. const Twine &NameStr = "", ///< A name for the new instruction
  3133. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  3134. );
  3135. /// \brief Constructor with insert-at-end-of-block semantics
  3136. PtrToIntInst(
  3137. Value *S, ///< The value to be converted
  3138. Type *Ty, ///< The type to convert to
  3139. const Twine &NameStr, ///< A name for the new instruction
  3140. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3141. );
  3142. /// \brief Gets the pointer operand.
  3143. Value *getPointerOperand() { return getOperand(0); }
  3144. /// \brief Gets the pointer operand.
  3145. const Value *getPointerOperand() const { return getOperand(0); }
  3146. /// \brief Gets the operand index of the pointer operand.
  3147. static unsigned getPointerOperandIndex() { return 0U; }
  3148. /// \brief Returns the address space of the pointer operand.
  3149. unsigned getPointerAddressSpace() const {
  3150. return getPointerOperand()->getType()->getPointerAddressSpace();
  3151. }
  3152. // Methods for support type inquiry through isa, cast, and dyn_cast:
  3153. static inline bool classof(const Instruction *I) {
  3154. return I->getOpcode() == PtrToInt;
  3155. }
  3156. static inline bool classof(const Value *V) {
  3157. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3158. }
  3159. };
  3160. //===----------------------------------------------------------------------===//
  3161. // BitCastInst Class
  3162. //===----------------------------------------------------------------------===//
  3163. /// \brief This class represents a no-op cast from one type to another.
  3164. class BitCastInst : public CastInst {
  3165. protected:
  3166. /// \brief Clone an identical BitCastInst
  3167. virtual BitCastInst *clone_impl() const;
  3168. public:
  3169. /// \brief Constructor with insert-before-instruction semantics
  3170. BitCastInst(
  3171. Value *S, ///< The value to be casted
  3172. Type *Ty, ///< The type to casted to
  3173. const Twine &NameStr = "", ///< A name for the new instruction
  3174. Instruction *InsertBefore = 0 ///< Where to insert the new instruction
  3175. );
  3176. /// \brief Constructor with insert-at-end-of-block semantics
  3177. BitCastInst(
  3178. Value *S, ///< The value to be casted
  3179. Type *Ty, ///< The type to casted to
  3180. const Twine &NameStr, ///< A name for the new instruction
  3181. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3182. );
  3183. // Methods for support type inquiry through isa, cast, and dyn_cast:
  3184. static inline bool classof(const Instruction *I) {
  3185. return I->getOpcode() == BitCast;
  3186. }
  3187. static inline bool classof(const Value *V) {
  3188. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3189. }
  3190. };
  3191. } // End llvm namespace
  3192. #endif