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.

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