LLVM API Documentation
00001 //===-- llvm/Operator.h - Operator utility subclass -------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines various classes for working with Instructions and 00011 // ConstantExprs. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_IR_OPERATOR_H 00016 #define LLVM_IR_OPERATOR_H 00017 00018 #include "llvm/IR/Constants.h" 00019 #include "llvm/IR/DataLayout.h" 00020 #include "llvm/IR/DerivedTypes.h" 00021 #include "llvm/IR/GetElementPtrTypeIterator.h" 00022 #include "llvm/IR/Instruction.h" 00023 #include "llvm/IR/Type.h" 00024 00025 namespace llvm { 00026 00027 class GetElementPtrInst; 00028 class BinaryOperator; 00029 class ConstantExpr; 00030 00031 /// Operator - This is a utility class that provides an abstraction for the 00032 /// common functionality between Instructions and ConstantExprs. 00033 /// 00034 class Operator : public User { 00035 private: 00036 // The Operator class is intended to be used as a utility, and is never itself 00037 // instantiated. 00038 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 00039 void *operator new(size_t s) LLVM_DELETED_FUNCTION; 00040 Operator() LLVM_DELETED_FUNCTION; 00041 00042 protected: 00043 // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete 00044 // an overridden method that's not deleted in the base class. Cannot leave 00045 // this unimplemented because that leads to an ODR-violation. 00046 ~Operator(); 00047 00048 public: 00049 /// getOpcode - Return the opcode for this Instruction or ConstantExpr. 00050 /// 00051 unsigned getOpcode() const { 00052 if (const Instruction *I = dyn_cast<Instruction>(this)) 00053 return I->getOpcode(); 00054 return cast<ConstantExpr>(this)->getOpcode(); 00055 } 00056 00057 /// getOpcode - If V is an Instruction or ConstantExpr, return its 00058 /// opcode. Otherwise return UserOp1. 00059 /// 00060 static unsigned getOpcode(const Value *V) { 00061 if (const Instruction *I = dyn_cast<Instruction>(V)) 00062 return I->getOpcode(); 00063 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 00064 return CE->getOpcode(); 00065 return Instruction::UserOp1; 00066 } 00067 00068 static inline bool classof(const Instruction *) { return true; } 00069 static inline bool classof(const ConstantExpr *) { return true; } 00070 static inline bool classof(const Value *V) { 00071 return isa<Instruction>(V) || isa<ConstantExpr>(V); 00072 } 00073 }; 00074 00075 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators 00076 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv, 00077 /// despite that operator having the potential for overflow. 00078 /// 00079 class OverflowingBinaryOperator : public Operator { 00080 public: 00081 enum { 00082 NoUnsignedWrap = (1 << 0), 00083 NoSignedWrap = (1 << 1) 00084 }; 00085 00086 private: 00087 friend class BinaryOperator; 00088 friend class ConstantExpr; 00089 void setHasNoUnsignedWrap(bool B) { 00090 SubclassOptionalData = 00091 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap); 00092 } 00093 void setHasNoSignedWrap(bool B) { 00094 SubclassOptionalData = 00095 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap); 00096 } 00097 00098 public: 00099 /// hasNoUnsignedWrap - Test whether this operation is known to never 00100 /// undergo unsigned overflow, aka the nuw property. 00101 bool hasNoUnsignedWrap() const { 00102 return SubclassOptionalData & NoUnsignedWrap; 00103 } 00104 00105 /// hasNoSignedWrap - Test whether this operation is known to never 00106 /// undergo signed overflow, aka the nsw property. 00107 bool hasNoSignedWrap() const { 00108 return (SubclassOptionalData & NoSignedWrap) != 0; 00109 } 00110 00111 static inline bool classof(const Instruction *I) { 00112 return I->getOpcode() == Instruction::Add || 00113 I->getOpcode() == Instruction::Sub || 00114 I->getOpcode() == Instruction::Mul || 00115 I->getOpcode() == Instruction::Shl; 00116 } 00117 static inline bool classof(const ConstantExpr *CE) { 00118 return CE->getOpcode() == Instruction::Add || 00119 CE->getOpcode() == Instruction::Sub || 00120 CE->getOpcode() == Instruction::Mul || 00121 CE->getOpcode() == Instruction::Shl; 00122 } 00123 static inline bool classof(const Value *V) { 00124 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) || 00125 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V))); 00126 } 00127 }; 00128 00129 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as 00130 /// "exact", indicating that no bits are destroyed. 00131 class PossiblyExactOperator : public Operator { 00132 public: 00133 enum { 00134 IsExact = (1 << 0) 00135 }; 00136 00137 private: 00138 friend class BinaryOperator; 00139 friend class ConstantExpr; 00140 void setIsExact(bool B) { 00141 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact); 00142 } 00143 00144 public: 00145 /// isExact - Test whether this division is known to be exact, with 00146 /// zero remainder. 00147 bool isExact() const { 00148 return SubclassOptionalData & IsExact; 00149 } 00150 00151 static bool isPossiblyExactOpcode(unsigned OpC) { 00152 return OpC == Instruction::SDiv || 00153 OpC == Instruction::UDiv || 00154 OpC == Instruction::AShr || 00155 OpC == Instruction::LShr; 00156 } 00157 static inline bool classof(const ConstantExpr *CE) { 00158 return isPossiblyExactOpcode(CE->getOpcode()); 00159 } 00160 static inline bool classof(const Instruction *I) { 00161 return isPossiblyExactOpcode(I->getOpcode()); 00162 } 00163 static inline bool classof(const Value *V) { 00164 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) || 00165 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V))); 00166 } 00167 }; 00168 00169 /// Convenience struct for specifying and reasoning about fast-math flags. 00170 class FastMathFlags { 00171 private: 00172 friend class FPMathOperator; 00173 unsigned Flags; 00174 FastMathFlags(unsigned F) : Flags(F) { } 00175 00176 public: 00177 enum { 00178 UnsafeAlgebra = (1 << 0), 00179 NoNaNs = (1 << 1), 00180 NoInfs = (1 << 2), 00181 NoSignedZeros = (1 << 3), 00182 AllowReciprocal = (1 << 4) 00183 }; 00184 00185 FastMathFlags() : Flags(0) 00186 { } 00187 00188 /// Whether any flag is set 00189 bool any() { return Flags != 0; } 00190 00191 /// Set all the flags to false 00192 void clear() { Flags = 0; } 00193 00194 /// Flag queries 00195 bool noNaNs() { return 0 != (Flags & NoNaNs); } 00196 bool noInfs() { return 0 != (Flags & NoInfs); } 00197 bool noSignedZeros() { return 0 != (Flags & NoSignedZeros); } 00198 bool allowReciprocal() { return 0 != (Flags & AllowReciprocal); } 00199 bool unsafeAlgebra() { return 0 != (Flags & UnsafeAlgebra); } 00200 00201 /// Flag setters 00202 void setNoNaNs() { Flags |= NoNaNs; } 00203 void setNoInfs() { Flags |= NoInfs; } 00204 void setNoSignedZeros() { Flags |= NoSignedZeros; } 00205 void setAllowReciprocal() { Flags |= AllowReciprocal; } 00206 void setUnsafeAlgebra() { 00207 Flags |= UnsafeAlgebra; 00208 setNoNaNs(); 00209 setNoInfs(); 00210 setNoSignedZeros(); 00211 setAllowReciprocal(); 00212 } 00213 00214 void operator&=(const FastMathFlags &OtherFlags) { 00215 Flags &= OtherFlags.Flags; 00216 } 00217 }; 00218 00219 00220 /// FPMathOperator - Utility class for floating point operations which can have 00221 /// information about relaxed accuracy requirements attached to them. 00222 class FPMathOperator : public Operator { 00223 private: 00224 friend class Instruction; 00225 00226 void setHasUnsafeAlgebra(bool B) { 00227 SubclassOptionalData = 00228 (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) | 00229 (B * FastMathFlags::UnsafeAlgebra); 00230 00231 // Unsafe algebra implies all the others 00232 if (B) { 00233 setHasNoNaNs(true); 00234 setHasNoInfs(true); 00235 setHasNoSignedZeros(true); 00236 setHasAllowReciprocal(true); 00237 } 00238 } 00239 void setHasNoNaNs(bool B) { 00240 SubclassOptionalData = 00241 (SubclassOptionalData & ~FastMathFlags::NoNaNs) | 00242 (B * FastMathFlags::NoNaNs); 00243 } 00244 void setHasNoInfs(bool B) { 00245 SubclassOptionalData = 00246 (SubclassOptionalData & ~FastMathFlags::NoInfs) | 00247 (B * FastMathFlags::NoInfs); 00248 } 00249 void setHasNoSignedZeros(bool B) { 00250 SubclassOptionalData = 00251 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) | 00252 (B * FastMathFlags::NoSignedZeros); 00253 } 00254 void setHasAllowReciprocal(bool B) { 00255 SubclassOptionalData = 00256 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) | 00257 (B * FastMathFlags::AllowReciprocal); 00258 } 00259 00260 /// Convenience function for setting multiple fast-math flags. 00261 /// FMF is a mask of the bits to set. 00262 void setFastMathFlags(FastMathFlags FMF) { 00263 SubclassOptionalData |= FMF.Flags; 00264 } 00265 00266 /// Convenience function for copying all fast-math flags. 00267 /// All values in FMF are transferred to this operator. 00268 void copyFastMathFlags(FastMathFlags FMF) { 00269 SubclassOptionalData = FMF.Flags; 00270 } 00271 00272 public: 00273 /// Test whether this operation is permitted to be 00274 /// algebraically transformed, aka the 'A' fast-math property. 00275 bool hasUnsafeAlgebra() const { 00276 return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0; 00277 } 00278 00279 /// Test whether this operation's arguments and results are to be 00280 /// treated as non-NaN, aka the 'N' fast-math property. 00281 bool hasNoNaNs() const { 00282 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0; 00283 } 00284 00285 /// Test whether this operation's arguments and results are to be 00286 /// treated as NoN-Inf, aka the 'I' fast-math property. 00287 bool hasNoInfs() const { 00288 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0; 00289 } 00290 00291 /// Test whether this operation can treat the sign of zero 00292 /// as insignificant, aka the 'S' fast-math property. 00293 bool hasNoSignedZeros() const { 00294 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0; 00295 } 00296 00297 /// Test whether this operation is permitted to use 00298 /// reciprocal instead of division, aka the 'R' fast-math property. 00299 bool hasAllowReciprocal() const { 00300 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0; 00301 } 00302 00303 /// Convenience function for getting all the fast-math flags 00304 FastMathFlags getFastMathFlags() const { 00305 return FastMathFlags(SubclassOptionalData); 00306 } 00307 00308 /// \brief Get the maximum error permitted by this operation in ULPs. An 00309 /// accuracy of 0.0 means that the operation should be performed with the 00310 /// default precision. 00311 float getFPAccuracy() const; 00312 00313 static inline bool classof(const Instruction *I) { 00314 return I->getType()->isFPOrFPVectorTy(); 00315 } 00316 static inline bool classof(const Value *V) { 00317 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00318 } 00319 }; 00320 00321 00322 /// ConcreteOperator - A helper template for defining operators for individual 00323 /// opcodes. 00324 template<typename SuperClass, unsigned Opc> 00325 class ConcreteOperator : public SuperClass { 00326 public: 00327 static inline bool classof(const Instruction *I) { 00328 return I->getOpcode() == Opc; 00329 } 00330 static inline bool classof(const ConstantExpr *CE) { 00331 return CE->getOpcode() == Opc; 00332 } 00333 static inline bool classof(const Value *V) { 00334 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) || 00335 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V))); 00336 } 00337 }; 00338 00339 class AddOperator 00340 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> { 00341 }; 00342 class SubOperator 00343 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> { 00344 }; 00345 class MulOperator 00346 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> { 00347 }; 00348 class ShlOperator 00349 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> { 00350 }; 00351 00352 00353 class SDivOperator 00354 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> { 00355 }; 00356 class UDivOperator 00357 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> { 00358 }; 00359 class AShrOperator 00360 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> { 00361 }; 00362 class LShrOperator 00363 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> { 00364 }; 00365 00366 00367 00368 class GEPOperator 00369 : public ConcreteOperator<Operator, Instruction::GetElementPtr> { 00370 enum { 00371 IsInBounds = (1 << 0) 00372 }; 00373 00374 friend class GetElementPtrInst; 00375 friend class ConstantExpr; 00376 void setIsInBounds(bool B) { 00377 SubclassOptionalData = 00378 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds); 00379 } 00380 00381 public: 00382 /// isInBounds - Test whether this is an inbounds GEP, as defined 00383 /// by LangRef.html. 00384 bool isInBounds() const { 00385 return SubclassOptionalData & IsInBounds; 00386 } 00387 00388 inline op_iterator idx_begin() { return op_begin()+1; } 00389 inline const_op_iterator idx_begin() const { return op_begin()+1; } 00390 inline op_iterator idx_end() { return op_end(); } 00391 inline const_op_iterator idx_end() const { return op_end(); } 00392 00393 Value *getPointerOperand() { 00394 return getOperand(0); 00395 } 00396 const Value *getPointerOperand() const { 00397 return getOperand(0); 00398 } 00399 static unsigned getPointerOperandIndex() { 00400 return 0U; // get index for modifying correct operand 00401 } 00402 00403 /// getPointerOperandType - Method to return the pointer operand as a 00404 /// PointerType. 00405 Type *getPointerOperandType() const { 00406 return getPointerOperand()->getType(); 00407 } 00408 00409 /// getPointerAddressSpace - Method to return the address space of the 00410 /// pointer operand. 00411 unsigned getPointerAddressSpace() const { 00412 return getPointerOperandType()->getPointerAddressSpace(); 00413 } 00414 00415 unsigned getNumIndices() const { // Note: always non-negative 00416 return getNumOperands() - 1; 00417 } 00418 00419 bool hasIndices() const { 00420 return getNumOperands() > 1; 00421 } 00422 00423 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 00424 /// zeros. If so, the result pointer and the first operand have the same 00425 /// value, just potentially different types. 00426 bool hasAllZeroIndices() const { 00427 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) { 00428 if (ConstantInt *C = dyn_cast<ConstantInt>(I)) 00429 if (C->isZero()) 00430 continue; 00431 return false; 00432 } 00433 return true; 00434 } 00435 00436 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 00437 /// constant integers. If so, the result pointer and the first operand have 00438 /// a constant offset between them. 00439 bool hasAllConstantIndices() const { 00440 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) { 00441 if (!isa<ConstantInt>(I)) 00442 return false; 00443 } 00444 return true; 00445 } 00446 00447 /// \brief Accumulate the constant address offset of this GEP if possible. 00448 /// 00449 /// This routine accepts an APInt into which it will accumulate the constant 00450 /// offset of this GEP if the GEP is in fact constant. If the GEP is not 00451 /// all-constant, it returns false and the value of the offset APInt is 00452 /// undefined (it is *not* preserved!). The APInt passed into this routine 00453 /// must be at exactly as wide as the IntPtr type for the address space of the 00454 /// base GEP pointer. 00455 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const { 00456 assert(Offset.getBitWidth() == 00457 DL.getPointerSizeInBits(getPointerAddressSpace()) && 00458 "The offset must have exactly as many bits as our pointer."); 00459 00460 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this); 00461 GTI != GTE; ++GTI) { 00462 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand()); 00463 if (!OpC) 00464 return false; 00465 if (OpC->isZero()) 00466 continue; 00467 00468 // Handle a struct index, which adds its field offset to the pointer. 00469 if (StructType *STy = dyn_cast<StructType>(*GTI)) { 00470 unsigned ElementIdx = OpC->getZExtValue(); 00471 const StructLayout *SL = DL.getStructLayout(STy); 00472 Offset += APInt(Offset.getBitWidth(), 00473 SL->getElementOffset(ElementIdx)); 00474 continue; 00475 } 00476 00477 // For array or vector indices, scale the index by the size of the type. 00478 APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth()); 00479 Offset += Index * APInt(Offset.getBitWidth(), 00480 DL.getTypeAllocSize(GTI.getIndexedType())); 00481 } 00482 return true; 00483 } 00484 00485 }; 00486 00487 class PtrToIntOperator 00488 : public ConcreteOperator<Operator, Instruction::PtrToInt> { 00489 friend class PtrToInt; 00490 friend class ConstantExpr; 00491 00492 public: 00493 Value *getPointerOperand() { 00494 return getOperand(0); 00495 } 00496 const Value *getPointerOperand() const { 00497 return getOperand(0); 00498 } 00499 static unsigned getPointerOperandIndex() { 00500 return 0U; // get index for modifying correct operand 00501 } 00502 00503 /// getPointerOperandType - Method to return the pointer operand as a 00504 /// PointerType. 00505 Type *getPointerOperandType() const { 00506 return getPointerOperand()->getType(); 00507 } 00508 00509 /// getPointerAddressSpace - Method to return the address space of the 00510 /// pointer operand. 00511 unsigned getPointerAddressSpace() const { 00512 return cast<PointerType>(getPointerOperandType())->getAddressSpace(); 00513 } 00514 }; 00515 00516 00517 } // End llvm namespace 00518 00519 #endif