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/Instruction.h" 00022 #include "llvm/IR/Type.h" 00023 #include "llvm/Support/GetElementPtrTypeIterator.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 00215 00216 /// FPMathOperator - Utility class for floating point operations which can have 00217 /// information about relaxed accuracy requirements attached to them. 00218 class FPMathOperator : public Operator { 00219 private: 00220 friend class Instruction; 00221 00222 void setHasUnsafeAlgebra(bool B) { 00223 SubclassOptionalData = 00224 (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) | 00225 (B * FastMathFlags::UnsafeAlgebra); 00226 00227 // Unsafe algebra implies all the others 00228 if (B) { 00229 setHasNoNaNs(true); 00230 setHasNoInfs(true); 00231 setHasNoSignedZeros(true); 00232 setHasAllowReciprocal(true); 00233 } 00234 } 00235 void setHasNoNaNs(bool B) { 00236 SubclassOptionalData = 00237 (SubclassOptionalData & ~FastMathFlags::NoNaNs) | 00238 (B * FastMathFlags::NoNaNs); 00239 } 00240 void setHasNoInfs(bool B) { 00241 SubclassOptionalData = 00242 (SubclassOptionalData & ~FastMathFlags::NoInfs) | 00243 (B * FastMathFlags::NoInfs); 00244 } 00245 void setHasNoSignedZeros(bool B) { 00246 SubclassOptionalData = 00247 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) | 00248 (B * FastMathFlags::NoSignedZeros); 00249 } 00250 void setHasAllowReciprocal(bool B) { 00251 SubclassOptionalData = 00252 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) | 00253 (B * FastMathFlags::AllowReciprocal); 00254 } 00255 00256 /// Convenience function for setting all the fast-math flags 00257 void setFastMathFlags(FastMathFlags FMF) { 00258 SubclassOptionalData |= FMF.Flags; 00259 } 00260 00261 public: 00262 /// Test whether this operation is permitted to be 00263 /// algebraically transformed, aka the 'A' fast-math property. 00264 bool hasUnsafeAlgebra() const { 00265 return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0; 00266 } 00267 00268 /// Test whether this operation's arguments and results are to be 00269 /// treated as non-NaN, aka the 'N' fast-math property. 00270 bool hasNoNaNs() const { 00271 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0; 00272 } 00273 00274 /// Test whether this operation's arguments and results are to be 00275 /// treated as NoN-Inf, aka the 'I' fast-math property. 00276 bool hasNoInfs() const { 00277 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0; 00278 } 00279 00280 /// Test whether this operation can treat the sign of zero 00281 /// as insignificant, aka the 'S' fast-math property. 00282 bool hasNoSignedZeros() const { 00283 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0; 00284 } 00285 00286 /// Test whether this operation is permitted to use 00287 /// reciprocal instead of division, aka the 'R' fast-math property. 00288 bool hasAllowReciprocal() const { 00289 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0; 00290 } 00291 00292 /// Convenience function for getting all the fast-math flags 00293 FastMathFlags getFastMathFlags() const { 00294 return FastMathFlags(SubclassOptionalData); 00295 } 00296 00297 /// \brief Get the maximum error permitted by this operation in ULPs. An 00298 /// accuracy of 0.0 means that the operation should be performed with the 00299 /// default precision. 00300 float getFPAccuracy() const; 00301 00302 static inline bool classof(const Instruction *I) { 00303 return I->getType()->isFPOrFPVectorTy(); 00304 } 00305 static inline bool classof(const Value *V) { 00306 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00307 } 00308 }; 00309 00310 00311 /// ConcreteOperator - A helper template for defining operators for individual 00312 /// opcodes. 00313 template<typename SuperClass, unsigned Opc> 00314 class ConcreteOperator : public SuperClass { 00315 public: 00316 static inline bool classof(const Instruction *I) { 00317 return I->getOpcode() == Opc; 00318 } 00319 static inline bool classof(const ConstantExpr *CE) { 00320 return CE->getOpcode() == Opc; 00321 } 00322 static inline bool classof(const Value *V) { 00323 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) || 00324 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V))); 00325 } 00326 }; 00327 00328 class AddOperator 00329 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> { 00330 }; 00331 class SubOperator 00332 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> { 00333 }; 00334 class MulOperator 00335 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> { 00336 }; 00337 class ShlOperator 00338 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> { 00339 }; 00340 00341 00342 class SDivOperator 00343 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> { 00344 }; 00345 class UDivOperator 00346 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> { 00347 }; 00348 class AShrOperator 00349 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> { 00350 }; 00351 class LShrOperator 00352 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> { 00353 }; 00354 00355 00356 00357 class GEPOperator 00358 : public ConcreteOperator<Operator, Instruction::GetElementPtr> { 00359 enum { 00360 IsInBounds = (1 << 0) 00361 }; 00362 00363 friend class GetElementPtrInst; 00364 friend class ConstantExpr; 00365 void setIsInBounds(bool B) { 00366 SubclassOptionalData = 00367 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds); 00368 } 00369 00370 public: 00371 /// isInBounds - Test whether this is an inbounds GEP, as defined 00372 /// by LangRef.html. 00373 bool isInBounds() const { 00374 return SubclassOptionalData & IsInBounds; 00375 } 00376 00377 inline op_iterator idx_begin() { return op_begin()+1; } 00378 inline const_op_iterator idx_begin() const { return op_begin()+1; } 00379 inline op_iterator idx_end() { return op_end(); } 00380 inline const_op_iterator idx_end() const { return op_end(); } 00381 00382 Value *getPointerOperand() { 00383 return getOperand(0); 00384 } 00385 const Value *getPointerOperand() const { 00386 return getOperand(0); 00387 } 00388 static unsigned getPointerOperandIndex() { 00389 return 0U; // get index for modifying correct operand 00390 } 00391 00392 /// getPointerOperandType - Method to return the pointer operand as a 00393 /// PointerType. 00394 Type *getPointerOperandType() const { 00395 return getPointerOperand()->getType(); 00396 } 00397 00398 /// getPointerAddressSpace - Method to return the address space of the 00399 /// pointer operand. 00400 unsigned getPointerAddressSpace() const { 00401 return cast<PointerType>(getPointerOperandType())->getAddressSpace(); 00402 } 00403 00404 unsigned getNumIndices() const { // Note: always non-negative 00405 return getNumOperands() - 1; 00406 } 00407 00408 bool hasIndices() const { 00409 return getNumOperands() > 1; 00410 } 00411 00412 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 00413 /// zeros. If so, the result pointer and the first operand have the same 00414 /// value, just potentially different types. 00415 bool hasAllZeroIndices() const { 00416 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) { 00417 if (ConstantInt *C = dyn_cast<ConstantInt>(I)) 00418 if (C->isZero()) 00419 continue; 00420 return false; 00421 } 00422 return true; 00423 } 00424 00425 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 00426 /// constant integers. If so, the result pointer and the first operand have 00427 /// a constant offset between them. 00428 bool hasAllConstantIndices() const { 00429 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) { 00430 if (!isa<ConstantInt>(I)) 00431 return false; 00432 } 00433 return true; 00434 } 00435 00436 /// \brief Accumulate the constant address offset of this GEP if possible. 00437 /// 00438 /// This routine accepts an APInt into which it will accumulate the constant 00439 /// offset of this GEP if the GEP is in fact constant. If the GEP is not 00440 /// all-constant, it returns false and the value of the offset APInt is 00441 /// undefined (it is *not* preserved!). The APInt passed into this routine 00442 /// must be at least as wide as the IntPtr type for the address space of 00443 /// the base GEP pointer. 00444 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const { 00445 assert(Offset.getBitWidth() == 00446 DL.getPointerSizeInBits(getPointerAddressSpace()) && 00447 "The offset must have exactly as many bits as our pointer."); 00448 00449 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this); 00450 GTI != GTE; ++GTI) { 00451 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand()); 00452 if (!OpC) 00453 return false; 00454 if (OpC->isZero()) 00455 continue; 00456 00457 // Handle a struct index, which adds its field offset to the pointer. 00458 if (StructType *STy = dyn_cast<StructType>(*GTI)) { 00459 unsigned ElementIdx = OpC->getZExtValue(); 00460 const StructLayout *SL = DL.getStructLayout(STy); 00461 Offset += APInt(Offset.getBitWidth(), 00462 SL->getElementOffset(ElementIdx)); 00463 continue; 00464 } 00465 00466 // For array or vector indices, scale the index by the size of the type. 00467 APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth()); 00468 Offset += Index * APInt(Offset.getBitWidth(), 00469 DL.getTypeAllocSize(GTI.getIndexedType())); 00470 } 00471 return true; 00472 } 00473 00474 }; 00475 00476 } // End llvm namespace 00477 00478 #endif