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