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