InstrTypes.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- 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 defines various meta classes of instructions that exist in the VM
  11. // representation. Specific concrete subclasses of these may be found in the
  12. // i*.h files...
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_IR_INSTRTYPES_H
  16. #define LLVM_IR_INSTRTYPES_H
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/IR/DerivedTypes.h"
  19. #include "llvm/IR/Instruction.h"
  20. #include "llvm/IR/OperandTraits.h"
  21. namespace llvm {
  22. class LLVMContext;
  23. //===----------------------------------------------------------------------===//
  24. // TerminatorInst Class
  25. //===----------------------------------------------------------------------===//
  26. /// Subclasses of this class are all able to terminate a basic
  27. /// block. Thus, these are all the flow control type of operations.
  28. ///
  29. class TerminatorInst : public Instruction {
  30. protected:
  31. TerminatorInst(Type *Ty, Instruction::TermOps iType,
  32. Use *Ops, unsigned NumOps,
  33. Instruction *InsertBefore = nullptr)
  34. : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
  35. TerminatorInst(Type *Ty, Instruction::TermOps iType,
  36. Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
  37. : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
  38. // Out of line virtual method, so the vtable, etc has a home.
  39. ~TerminatorInst() override;
  40. /// Virtual methods - Terminators should overload these and provide inline
  41. /// overrides of non-V methods.
  42. virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
  43. virtual unsigned getNumSuccessorsV() const = 0;
  44. virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
  45. public:
  46. /// Return the number of successors that this terminator has.
  47. unsigned getNumSuccessors() const {
  48. return getNumSuccessorsV();
  49. }
  50. /// Return the specified successor.
  51. BasicBlock *getSuccessor(unsigned idx) const {
  52. return getSuccessorV(idx);
  53. }
  54. /// Update the specified successor to point at the provided block.
  55. void setSuccessor(unsigned idx, BasicBlock *B) {
  56. setSuccessorV(idx, B);
  57. }
  58. // Methods for support type inquiry through isa, cast, and dyn_cast:
  59. static inline bool classof(const Instruction *I) {
  60. return I->isTerminator();
  61. }
  62. static inline bool classof(const Value *V) {
  63. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  64. }
  65. };
  66. //===----------------------------------------------------------------------===//
  67. // UnaryInstruction Class
  68. //===----------------------------------------------------------------------===//
  69. class UnaryInstruction : public Instruction {
  70. void *operator new(size_t, unsigned) = delete;
  71. protected:
  72. UnaryInstruction(Type *Ty, unsigned iType, Value *V,
  73. Instruction *IB = nullptr)
  74. : Instruction(Ty, iType, &Op<0>(), 1, IB) {
  75. Op<0>() = V;
  76. }
  77. UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
  78. : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
  79. Op<0>() = V;
  80. }
  81. public:
  82. // allocate space for exactly one operand
  83. void *operator new(size_t s) {
  84. return User::operator new(s, 1);
  85. }
  86. // Out of line virtual method, so the vtable, etc has a home.
  87. ~UnaryInstruction() override;
  88. /// Transparently provide more efficient getOperand methods.
  89. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  90. // Methods for support type inquiry through isa, cast, and dyn_cast:
  91. static inline bool classof(const Instruction *I) {
  92. return I->getOpcode() == Instruction::Alloca ||
  93. I->getOpcode() == Instruction::Load ||
  94. I->getOpcode() == Instruction::VAArg ||
  95. I->getOpcode() == Instruction::ExtractValue ||
  96. (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
  97. }
  98. static inline bool classof(const Value *V) {
  99. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  100. }
  101. };
  102. template <>
  103. struct OperandTraits<UnaryInstruction> :
  104. public FixedNumOperandTraits<UnaryInstruction, 1> {
  105. };
  106. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
  107. //===----------------------------------------------------------------------===//
  108. // BinaryOperator Class
  109. //===----------------------------------------------------------------------===//
  110. class BinaryOperator : public Instruction {
  111. void *operator new(size_t, unsigned) = delete;
  112. protected:
  113. void init(BinaryOps iType);
  114. BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
  115. const Twine &Name, Instruction *InsertBefore);
  116. BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
  117. const Twine &Name, BasicBlock *InsertAtEnd);
  118. // Note: Instruction needs to be a friend here to call cloneImpl.
  119. friend class Instruction;
  120. BinaryOperator *cloneImpl() const;
  121. public:
  122. // allocate space for exactly two operands
  123. void *operator new(size_t s) {
  124. return User::operator new(s, 2);
  125. }
  126. /// Transparently provide more efficient getOperand methods.
  127. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  128. /// Construct a binary instruction, given the opcode and the two
  129. /// operands. Optionally (if InstBefore is specified) insert the instruction
  130. /// into a BasicBlock right before the specified instruction. The specified
  131. /// Instruction is allowed to be a dereferenced end iterator.
  132. ///
  133. static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
  134. const Twine &Name = Twine(),
  135. Instruction *InsertBefore = nullptr);
  136. /// Construct a binary instruction, given the opcode and the two
  137. /// operands. Also automatically insert this instruction to the end of the
  138. /// BasicBlock specified.
  139. ///
  140. static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
  141. const Twine &Name, BasicBlock *InsertAtEnd);
  142. /// These methods just forward to Create, and are useful when you
  143. /// statically know what type of instruction you're going to create. These
  144. /// helpers just save some typing.
  145. #define HANDLE_BINARY_INST(N, OPC, CLASS) \
  146. static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
  147. const Twine &Name = "") {\
  148. return Create(Instruction::OPC, V1, V2, Name);\
  149. }
  150. #include "llvm/IR/Instruction.def"
  151. #define HANDLE_BINARY_INST(N, OPC, CLASS) \
  152. static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
  153. const Twine &Name, BasicBlock *BB) {\
  154. return Create(Instruction::OPC, V1, V2, Name, BB);\
  155. }
  156. #include "llvm/IR/Instruction.def"
  157. #define HANDLE_BINARY_INST(N, OPC, CLASS) \
  158. static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
  159. const Twine &Name, Instruction *I) {\
  160. return Create(Instruction::OPC, V1, V2, Name, I);\
  161. }
  162. #include "llvm/IR/Instruction.def"
  163. static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
  164. const Twine &Name = "") {
  165. BinaryOperator *BO = Create(Opc, V1, V2, Name);
  166. BO->setHasNoSignedWrap(true);
  167. return BO;
  168. }
  169. static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
  170. const Twine &Name, BasicBlock *BB) {
  171. BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
  172. BO->setHasNoSignedWrap(true);
  173. return BO;
  174. }
  175. static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
  176. const Twine &Name, Instruction *I) {
  177. BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
  178. BO->setHasNoSignedWrap(true);
  179. return BO;
  180. }
  181. static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
  182. const Twine &Name = "") {
  183. BinaryOperator *BO = Create(Opc, V1, V2, Name);
  184. BO->setHasNoUnsignedWrap(true);
  185. return BO;
  186. }
  187. static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
  188. const Twine &Name, BasicBlock *BB) {
  189. BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
  190. BO->setHasNoUnsignedWrap(true);
  191. return BO;
  192. }
  193. static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
  194. const Twine &Name, Instruction *I) {
  195. BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
  196. BO->setHasNoUnsignedWrap(true);
  197. return BO;
  198. }
  199. static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
  200. const Twine &Name = "") {
  201. BinaryOperator *BO = Create(Opc, V1, V2, Name);
  202. BO->setIsExact(true);
  203. return BO;
  204. }
  205. static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
  206. const Twine &Name, BasicBlock *BB) {
  207. BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
  208. BO->setIsExact(true);
  209. return BO;
  210. }
  211. static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
  212. const Twine &Name, Instruction *I) {
  213. BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
  214. BO->setIsExact(true);
  215. return BO;
  216. }
  217. #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
  218. static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
  219. (Value *V1, Value *V2, const Twine &Name = "") { \
  220. return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \
  221. } \
  222. static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
  223. (Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \
  224. return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \
  225. } \
  226. static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
  227. (Value *V1, Value *V2, const Twine &Name, Instruction *I) { \
  228. return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \
  229. }
  230. DEFINE_HELPERS(Add, NSW) // CreateNSWAdd
  231. DEFINE_HELPERS(Add, NUW) // CreateNUWAdd
  232. DEFINE_HELPERS(Sub, NSW) // CreateNSWSub
  233. DEFINE_HELPERS(Sub, NUW) // CreateNUWSub
  234. DEFINE_HELPERS(Mul, NSW) // CreateNSWMul
  235. DEFINE_HELPERS(Mul, NUW) // CreateNUWMul
  236. DEFINE_HELPERS(Shl, NSW) // CreateNSWShl
  237. DEFINE_HELPERS(Shl, NUW) // CreateNUWShl
  238. DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv
  239. DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv
  240. DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
  241. DEFINE_HELPERS(LShr, Exact) // CreateExactLShr
  242. #undef DEFINE_HELPERS
  243. /// Helper functions to construct and inspect unary operations (NEG and NOT)
  244. /// via binary operators SUB and XOR:
  245. ///
  246. /// Create the NEG and NOT instructions out of SUB and XOR instructions.
  247. ///
  248. static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
  249. Instruction *InsertBefore = nullptr);
  250. static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
  251. BasicBlock *InsertAtEnd);
  252. static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
  253. Instruction *InsertBefore = nullptr);
  254. static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
  255. BasicBlock *InsertAtEnd);
  256. static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
  257. Instruction *InsertBefore = nullptr);
  258. static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
  259. BasicBlock *InsertAtEnd);
  260. static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
  261. Instruction *InsertBefore = nullptr);
  262. static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
  263. BasicBlock *InsertAtEnd);
  264. static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
  265. Instruction *InsertBefore = nullptr);
  266. static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
  267. BasicBlock *InsertAtEnd);
  268. /// Check if the given Value is a NEG, FNeg, or NOT instruction.
  269. ///
  270. static bool isNeg(const Value *V);
  271. static bool isFNeg(const Value *V, bool IgnoreZeroSign=false);
  272. static bool isNot(const Value *V);
  273. /// Helper functions to extract the unary argument of a NEG, FNEG or NOT
  274. /// operation implemented via Sub, FSub, or Xor.
  275. ///
  276. static const Value *getNegArgument(const Value *BinOp);
  277. static Value *getNegArgument( Value *BinOp);
  278. static const Value *getFNegArgument(const Value *BinOp);
  279. static Value *getFNegArgument( Value *BinOp);
  280. static const Value *getNotArgument(const Value *BinOp);
  281. static Value *getNotArgument( Value *BinOp);
  282. BinaryOps getOpcode() const {
  283. return static_cast<BinaryOps>(Instruction::getOpcode());
  284. }
  285. /// Exchange the two operands to this instruction.
  286. /// This instruction is safe to use on any binary instruction and
  287. /// does not modify the semantics of the instruction. If the instruction
  288. /// cannot be reversed (ie, it's a Div), then return true.
  289. ///
  290. bool swapOperands();
  291. /// Set or clear the nsw flag on this instruction, which must be an operator
  292. /// which supports this flag. See LangRef.html for the meaning of this flag.
  293. void setHasNoUnsignedWrap(bool b = true);
  294. /// Set or clear the nsw flag on this instruction, which must be an operator
  295. /// which supports this flag. See LangRef.html for the meaning of this flag.
  296. void setHasNoSignedWrap(bool b = true);
  297. /// Set or clear the exact flag on this instruction, which must be an operator
  298. /// which supports this flag. See LangRef.html for the meaning of this flag.
  299. void setIsExact(bool b = true);
  300. /// Determine whether the no unsigned wrap flag is set.
  301. bool hasNoUnsignedWrap() const;
  302. /// Determine whether the no signed wrap flag is set.
  303. bool hasNoSignedWrap() const;
  304. /// Determine whether the exact flag is set.
  305. bool isExact() const;
  306. /// Convenience method to copy supported wrapping, exact, and fast-math flags
  307. /// from V to this instruction.
  308. void copyIRFlags(const Value *V);
  309. /// Logical 'and' of any supported wrapping, exact, and fast-math flags of
  310. /// V and this instruction.
  311. void andIRFlags(const Value *V);
  312. // Methods for support type inquiry through isa, cast, and dyn_cast:
  313. static inline bool classof(const Instruction *I) {
  314. return I->isBinaryOp();
  315. }
  316. static inline bool classof(const Value *V) {
  317. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  318. }
  319. };
  320. template <>
  321. struct OperandTraits<BinaryOperator> :
  322. public FixedNumOperandTraits<BinaryOperator, 2> {
  323. };
  324. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
  325. //===----------------------------------------------------------------------===//
  326. // CastInst Class
  327. //===----------------------------------------------------------------------===//
  328. /// This is the base class for all instructions that perform data
  329. /// casts. It is simply provided so that instruction category testing
  330. /// can be performed with code like:
  331. ///
  332. /// if (isa<CastInst>(Instr)) { ... }
  333. /// @brief Base class of casting instructions.
  334. class CastInst : public UnaryInstruction {
  335. void anchor() override;
  336. protected:
  337. /// @brief Constructor with insert-before-instruction semantics for subclasses
  338. CastInst(Type *Ty, unsigned iType, Value *S,
  339. const Twine &NameStr = "", Instruction *InsertBefore = nullptr)
  340. : UnaryInstruction(Ty, iType, S, InsertBefore) {
  341. setName(NameStr);
  342. }
  343. /// @brief Constructor with insert-at-end-of-block semantics for subclasses
  344. CastInst(Type *Ty, unsigned iType, Value *S,
  345. const Twine &NameStr, BasicBlock *InsertAtEnd)
  346. : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
  347. setName(NameStr);
  348. }
  349. public:
  350. /// Provides a way to construct any of the CastInst subclasses using an
  351. /// opcode instead of the subclass's constructor. The opcode must be in the
  352. /// CastOps category (Instruction::isCast(opcode) returns true). This
  353. /// constructor has insert-before-instruction semantics to automatically
  354. /// insert the new CastInst before InsertBefore (if it is non-null).
  355. /// @brief Construct any of the CastInst subclasses
  356. static CastInst *Create(
  357. Instruction::CastOps, ///< The opcode of the cast instruction
  358. Value *S, ///< The value to be casted (operand 0)
  359. Type *Ty, ///< The type to which cast should be made
  360. const Twine &Name = "", ///< Name for the instruction
  361. Instruction *InsertBefore = nullptr ///< Place to insert the instruction
  362. );
  363. /// Provides a way to construct any of the CastInst subclasses using an
  364. /// opcode instead of the subclass's constructor. The opcode must be in the
  365. /// CastOps category. This constructor has insert-at-end-of-block semantics
  366. /// to automatically insert the new CastInst at the end of InsertAtEnd (if
  367. /// its non-null).
  368. /// @brief Construct any of the CastInst subclasses
  369. static CastInst *Create(
  370. Instruction::CastOps, ///< The opcode for the cast instruction
  371. Value *S, ///< The value to be casted (operand 0)
  372. Type *Ty, ///< The type to which operand is casted
  373. const Twine &Name, ///< The name for the instruction
  374. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  375. );
  376. /// @brief Create a ZExt or BitCast cast instruction
  377. static CastInst *CreateZExtOrBitCast(
  378. Value *S, ///< The value to be casted (operand 0)
  379. Type *Ty, ///< The type to which cast should be made
  380. const Twine &Name = "", ///< Name for the instruction
  381. Instruction *InsertBefore = nullptr ///< Place to insert the instruction
  382. );
  383. /// @brief Create a ZExt or BitCast cast instruction
  384. static CastInst *CreateZExtOrBitCast(
  385. Value *S, ///< The value to be casted (operand 0)
  386. Type *Ty, ///< The type to which operand is casted
  387. const Twine &Name, ///< The name for the instruction
  388. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  389. );
  390. /// @brief Create a SExt or BitCast cast instruction
  391. static CastInst *CreateSExtOrBitCast(
  392. Value *S, ///< The value to be casted (operand 0)
  393. Type *Ty, ///< The type to which cast should be made
  394. const Twine &Name = "", ///< Name for the instruction
  395. Instruction *InsertBefore = nullptr ///< Place to insert the instruction
  396. );
  397. /// @brief Create a SExt or BitCast cast instruction
  398. static CastInst *CreateSExtOrBitCast(
  399. Value *S, ///< The value to be casted (operand 0)
  400. Type *Ty, ///< The type to which operand is casted
  401. const Twine &Name, ///< The name for the instruction
  402. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  403. );
  404. /// @brief Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
  405. static CastInst *CreatePointerCast(
  406. Value *S, ///< The pointer value to be casted (operand 0)
  407. Type *Ty, ///< The type to which operand is casted
  408. const Twine &Name, ///< The name for the instruction
  409. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  410. );
  411. /// @brief Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
  412. static CastInst *CreatePointerCast(
  413. Value *S, ///< The pointer value to be casted (operand 0)
  414. Type *Ty, ///< The type to which cast should be made
  415. const Twine &Name = "", ///< Name for the instruction
  416. Instruction *InsertBefore = nullptr ///< Place to insert the instruction
  417. );
  418. /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
  419. static CastInst *CreatePointerBitCastOrAddrSpaceCast(
  420. Value *S, ///< The pointer value to be casted (operand 0)
  421. Type *Ty, ///< The type to which operand is casted
  422. const Twine &Name, ///< The name for the instruction
  423. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  424. );
  425. /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
  426. static CastInst *CreatePointerBitCastOrAddrSpaceCast(
  427. Value *S, ///< The pointer value to be casted (operand 0)
  428. Type *Ty, ///< The type to which cast should be made
  429. const Twine &Name = "", ///< Name for the instruction
  430. Instruction *InsertBefore = 0 ///< Place to insert the instruction
  431. );
  432. /// @brief Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
  433. ///
  434. /// If the value is a pointer type and the destination an integer type,
  435. /// creates a PtrToInt cast. If the value is an integer type and the
  436. /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates
  437. /// a bitcast.
  438. static CastInst *CreateBitOrPointerCast(
  439. Value *S, ///< The pointer value to be casted (operand 0)
  440. Type *Ty, ///< The type to which cast should be made
  441. const Twine &Name = "", ///< Name for the instruction
  442. Instruction *InsertBefore = 0 ///< Place to insert the instruction
  443. );
  444. /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
  445. static CastInst *CreateIntegerCast(
  446. Value *S, ///< The pointer value to be casted (operand 0)
  447. Type *Ty, ///< The type to which cast should be made
  448. bool isSigned, ///< Whether to regard S as signed or not
  449. const Twine &Name = "", ///< Name for the instruction
  450. Instruction *InsertBefore = nullptr ///< Place to insert the instruction
  451. );
  452. /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
  453. static CastInst *CreateIntegerCast(
  454. Value *S, ///< The integer value to be casted (operand 0)
  455. Type *Ty, ///< The integer type to which operand is casted
  456. bool isSigned, ///< Whether to regard S as signed or not
  457. const Twine &Name, ///< The name for the instruction
  458. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  459. );
  460. /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
  461. static CastInst *CreateFPCast(
  462. Value *S, ///< The floating point value to be casted
  463. Type *Ty, ///< The floating point type to cast to
  464. const Twine &Name = "", ///< Name for the instruction
  465. Instruction *InsertBefore = nullptr ///< Place to insert the instruction
  466. );
  467. /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
  468. static CastInst *CreateFPCast(
  469. Value *S, ///< The floating point value to be casted
  470. Type *Ty, ///< The floating point type to cast to
  471. const Twine &Name, ///< The name for the instruction
  472. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  473. );
  474. /// @brief Create a Trunc or BitCast cast instruction
  475. static CastInst *CreateTruncOrBitCast(
  476. Value *S, ///< The value to be casted (operand 0)
  477. Type *Ty, ///< The type to which cast should be made
  478. const Twine &Name = "", ///< Name for the instruction
  479. Instruction *InsertBefore = nullptr ///< Place to insert the instruction
  480. );
  481. /// @brief Create a Trunc or BitCast cast instruction
  482. static CastInst *CreateTruncOrBitCast(
  483. Value *S, ///< The value to be casted (operand 0)
  484. Type *Ty, ///< The type to which operand is casted
  485. const Twine &Name, ///< The name for the instruction
  486. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  487. );
  488. /// @brief Check whether it is valid to call getCastOpcode for these types.
  489. static bool isCastable(
  490. Type *SrcTy, ///< The Type from which the value should be cast.
  491. Type *DestTy ///< The Type to which the value should be cast.
  492. );
  493. /// @brief Check whether a bitcast between these types is valid
  494. static bool isBitCastable(
  495. Type *SrcTy, ///< The Type from which the value should be cast.
  496. Type *DestTy ///< The Type to which the value should be cast.
  497. );
  498. /// @brief Check whether a bitcast, inttoptr, or ptrtoint cast between these
  499. /// types is valid and a no-op.
  500. ///
  501. /// This ensures that any pointer<->integer cast has enough bits in the
  502. /// integer and any other cast is a bitcast.
  503. static bool isBitOrNoopPointerCastable(
  504. Type *SrcTy, ///< The Type from which the value should be cast.
  505. Type *DestTy, ///< The Type to which the value should be cast.
  506. const DataLayout &DL);
  507. /// Returns the opcode necessary to cast Val into Ty using usual casting
  508. /// rules.
  509. /// @brief Infer the opcode for cast operand and type
  510. static Instruction::CastOps getCastOpcode(
  511. const Value *Val, ///< The value to cast
  512. bool SrcIsSigned, ///< Whether to treat the source as signed
  513. Type *Ty, ///< The Type to which the value should be casted
  514. bool DstIsSigned ///< Whether to treate the dest. as signed
  515. );
  516. /// There are several places where we need to know if a cast instruction
  517. /// only deals with integer source and destination types. To simplify that
  518. /// logic, this method is provided.
  519. /// @returns true iff the cast has only integral typed operand and dest type.
  520. /// @brief Determine if this is an integer-only cast.
  521. bool isIntegerCast() const;
  522. /// A lossless cast is one that does not alter the basic value. It implies
  523. /// a no-op cast but is more stringent, preventing things like int->float,
  524. /// long->double, or int->ptr.
  525. /// @returns true iff the cast is lossless.
  526. /// @brief Determine if this is a lossless cast.
  527. bool isLosslessCast() const;
  528. /// A no-op cast is one that can be effected without changing any bits.
  529. /// It implies that the source and destination types are the same size. The
  530. /// IntPtrTy argument is used to make accurate determinations for casts
  531. /// involving Integer and Pointer types. They are no-op casts if the integer
  532. /// is the same size as the pointer. However, pointer size varies with
  533. /// platform. Generally, the result of DataLayout::getIntPtrType() should be
  534. /// passed in. If that's not available, use Type::Int64Ty, which will make
  535. /// the isNoopCast call conservative.
  536. /// @brief Determine if the described cast is a no-op cast.
  537. static bool isNoopCast(
  538. Instruction::CastOps Opcode, ///< Opcode of cast
  539. Type *SrcTy, ///< SrcTy of cast
  540. Type *DstTy, ///< DstTy of cast
  541. Type *IntPtrTy ///< Integer type corresponding to Ptr types
  542. );
  543. /// @brief Determine if this cast is a no-op cast.
  544. bool isNoopCast(
  545. Type *IntPtrTy ///< Integer type corresponding to pointer
  546. ) const;
  547. /// @brief Determine if this cast is a no-op cast.
  548. ///
  549. /// \param DL is the DataLayout to get the Int Ptr type from.
  550. bool isNoopCast(const DataLayout &DL) const;
  551. /// Determine how a pair of casts can be eliminated, if they can be at all.
  552. /// This is a helper function for both CastInst and ConstantExpr.
  553. /// @returns 0 if the CastInst pair can't be eliminated, otherwise
  554. /// returns Instruction::CastOps value for a cast that can replace
  555. /// the pair, casting SrcTy to DstTy.
  556. /// @brief Determine if a cast pair is eliminable
  557. static unsigned isEliminableCastPair(
  558. Instruction::CastOps firstOpcode, ///< Opcode of first cast
  559. Instruction::CastOps secondOpcode, ///< Opcode of second cast
  560. Type *SrcTy, ///< SrcTy of 1st cast
  561. Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
  562. Type *DstTy, ///< DstTy of 2nd cast
  563. Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
  564. Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
  565. Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null
  566. );
  567. /// @brief Return the opcode of this CastInst
  568. Instruction::CastOps getOpcode() const {
  569. return Instruction::CastOps(Instruction::getOpcode());
  570. }
  571. /// @brief Return the source type, as a convenience
  572. Type* getSrcTy() const { return getOperand(0)->getType(); }
  573. /// @brief Return the destination type, as a convenience
  574. Type* getDestTy() const { return getType(); }
  575. /// This method can be used to determine if a cast from S to DstTy using
  576. /// Opcode op is valid or not.
  577. /// @returns true iff the proposed cast is valid.
  578. /// @brief Determine if a cast is valid without creating one.
  579. static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
  580. /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
  581. static inline bool classof(const Instruction *I) {
  582. return I->isCast();
  583. }
  584. static inline bool classof(const Value *V) {
  585. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  586. }
  587. };
  588. //===----------------------------------------------------------------------===//
  589. // CmpInst Class
  590. // //
  591. ///////////////////////////////////////////////////////////////////////////////
  592. /// This class is the base class for the comparison instructions.
  593. /// @brief Abstract base class of comparison instructions.
  594. class CmpInst : public Instruction {
  595. void *operator new(size_t, unsigned) = delete;
  596. CmpInst() = delete;
  597. protected:
  598. CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
  599. Value *LHS, Value *RHS, const Twine &Name = "",
  600. Instruction *InsertBefore = nullptr);
  601. CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
  602. Value *LHS, Value *RHS, const Twine &Name,
  603. BasicBlock *InsertAtEnd);
  604. void anchor() override; // Out of line virtual method.
  605. public:
  606. /// This enumeration lists the possible predicates for CmpInst subclasses.
  607. /// Values in the range 0-31 are reserved for FCmpInst, while values in the
  608. /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
  609. /// predicate values are not overlapping between the classes.
  610. enum Predicate {
  611. // Opcode U L G E Intuitive operation
  612. FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
  613. FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
  614. FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
  615. FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
  616. FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
  617. FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
  618. FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
  619. FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
  620. FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
  621. FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
  622. FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
  623. FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
  624. FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
  625. FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
  626. FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
  627. FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
  628. FIRST_FCMP_PREDICATE = FCMP_FALSE,
  629. LAST_FCMP_PREDICATE = FCMP_TRUE,
  630. BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
  631. ICMP_EQ = 32, ///< equal
  632. ICMP_NE = 33, ///< not equal
  633. ICMP_UGT = 34, ///< unsigned greater than
  634. ICMP_UGE = 35, ///< unsigned greater or equal
  635. ICMP_ULT = 36, ///< unsigned less than
  636. ICMP_ULE = 37, ///< unsigned less or equal
  637. ICMP_SGT = 38, ///< signed greater than
  638. ICMP_SGE = 39, ///< signed greater or equal
  639. ICMP_SLT = 40, ///< signed less than
  640. ICMP_SLE = 41, ///< signed less or equal
  641. FIRST_ICMP_PREDICATE = ICMP_EQ,
  642. LAST_ICMP_PREDICATE = ICMP_SLE,
  643. BAD_ICMP_PREDICATE = ICMP_SLE + 1
  644. };
  645. // allocate space for exactly two operands
  646. void *operator new(size_t s) {
  647. return User::operator new(s, 2);
  648. }
  649. /// Construct a compare instruction, given the opcode, the predicate and
  650. /// the two operands. Optionally (if InstBefore is specified) insert the
  651. /// instruction into a BasicBlock right before the specified instruction.
  652. /// The specified Instruction is allowed to be a dereferenced end iterator.
  653. /// @brief Create a CmpInst
  654. static CmpInst *Create(OtherOps Op,
  655. unsigned short predicate, Value *S1,
  656. Value *S2, const Twine &Name = "",
  657. Instruction *InsertBefore = nullptr);
  658. /// Construct a compare instruction, given the opcode, the predicate and the
  659. /// two operands. Also automatically insert this instruction to the end of
  660. /// the BasicBlock specified.
  661. /// @brief Create a CmpInst
  662. static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
  663. Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
  664. /// @brief Get the opcode casted to the right type
  665. OtherOps getOpcode() const {
  666. return static_cast<OtherOps>(Instruction::getOpcode());
  667. }
  668. /// @brief Return the predicate for this instruction.
  669. Predicate getPredicate() const {
  670. return Predicate(getSubclassDataFromInstruction());
  671. }
  672. /// @brief Set the predicate for this instruction to the specified value.
  673. void setPredicate(Predicate P) { setInstructionSubclassData(P); }
  674. static bool isFPPredicate(Predicate P) {
  675. return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
  676. }
  677. static bool isIntPredicate(Predicate P) {
  678. return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
  679. }
  680. bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
  681. bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
  682. /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
  683. /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
  684. /// @returns the inverse predicate for the instruction's current predicate.
  685. /// @brief Return the inverse of the instruction's predicate.
  686. Predicate getInversePredicate() const {
  687. return getInversePredicate(getPredicate());
  688. }
  689. /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
  690. /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
  691. /// @returns the inverse predicate for predicate provided in \p pred.
  692. /// @brief Return the inverse of a given predicate
  693. static Predicate getInversePredicate(Predicate pred);
  694. /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
  695. /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
  696. /// @returns the predicate that would be the result of exchanging the two
  697. /// operands of the CmpInst instruction without changing the result
  698. /// produced.
  699. /// @brief Return the predicate as if the operands were swapped
  700. Predicate getSwappedPredicate() const {
  701. return getSwappedPredicate(getPredicate());
  702. }
  703. /// This is a static version that you can use without an instruction
  704. /// available.
  705. /// @brief Return the predicate as if the operands were swapped.
  706. static Predicate getSwappedPredicate(Predicate pred);
  707. /// @brief Provide more efficient getOperand methods.
  708. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  709. /// This is just a convenience that dispatches to the subclasses.
  710. /// @brief Swap the operands and adjust predicate accordingly to retain
  711. /// the same comparison.
  712. void swapOperands();
  713. /// This is just a convenience that dispatches to the subclasses.
  714. /// @brief Determine if this CmpInst is commutative.
  715. bool isCommutative() const;
  716. /// This is just a convenience that dispatches to the subclasses.
  717. /// @brief Determine if this is an equals/not equals predicate.
  718. bool isEquality() const;
  719. /// @returns true if the comparison is signed, false otherwise.
  720. /// @brief Determine if this instruction is using a signed comparison.
  721. bool isSigned() const {
  722. return isSigned(getPredicate());
  723. }
  724. /// @returns true if the comparison is unsigned, false otherwise.
  725. /// @brief Determine if this instruction is using an unsigned comparison.
  726. bool isUnsigned() const {
  727. return isUnsigned(getPredicate());
  728. }
  729. /// This is just a convenience.
  730. /// @brief Determine if this is true when both operands are the same.
  731. bool isTrueWhenEqual() const {
  732. return isTrueWhenEqual(getPredicate());
  733. }
  734. /// This is just a convenience.
  735. /// @brief Determine if this is false when both operands are the same.
  736. bool isFalseWhenEqual() const {
  737. return isFalseWhenEqual(getPredicate());
  738. }
  739. /// @returns true if the predicate is unsigned, false otherwise.
  740. /// @brief Determine if the predicate is an unsigned operation.
  741. static bool isUnsigned(unsigned short predicate);
  742. /// @returns true if the predicate is signed, false otherwise.
  743. /// @brief Determine if the predicate is an signed operation.
  744. static bool isSigned(unsigned short predicate);
  745. /// @brief Determine if the predicate is an ordered operation.
  746. static bool isOrdered(unsigned short predicate);
  747. /// @brief Determine if the predicate is an unordered operation.
  748. static bool isUnordered(unsigned short predicate);
  749. /// Determine if the predicate is true when comparing a value with itself.
  750. static bool isTrueWhenEqual(unsigned short predicate);
  751. /// Determine if the predicate is false when comparing a value with itself.
  752. static bool isFalseWhenEqual(unsigned short predicate);
  753. /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
  754. static inline bool classof(const Instruction *I) {
  755. return I->getOpcode() == Instruction::ICmp ||
  756. I->getOpcode() == Instruction::FCmp;
  757. }
  758. static inline bool classof(const Value *V) {
  759. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  760. }
  761. /// @brief Create a result type for fcmp/icmp
  762. static Type* makeCmpResultType(Type* opnd_type) {
  763. if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
  764. return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
  765. vt->getNumElements());
  766. }
  767. return Type::getInt1Ty(opnd_type->getContext());
  768. }
  769. private:
  770. // Shadow Value::setValueSubclassData with a private forwarding method so that
  771. // subclasses cannot accidentally use it.
  772. void setValueSubclassData(unsigned short D) {
  773. Value::setValueSubclassData(D);
  774. }
  775. };
  776. // FIXME: these are redundant if CmpInst < BinaryOperator
  777. template <>
  778. struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
  779. };
  780. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
  781. } // End llvm namespace
  782. #endif