LLVM API Documentation
00001 //===-- llvm/Instruction.h - Instruction class definition -------*- 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 contains the declaration of the Instruction class, which is the 00011 // base class for all of the LLVM instructions. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_IR_INSTRUCTION_H 00016 #define LLVM_IR_INSTRUCTION_H 00017 00018 #include "llvm/ADT/ilist_node.h" 00019 #include "llvm/IR/User.h" 00020 #include "llvm/Support/DebugLoc.h" 00021 00022 namespace llvm { 00023 00024 class FastMathFlags; 00025 class LLVMContext; 00026 class MDNode; 00027 00028 template<typename ValueSubClass, typename ItemParentClass> 00029 class SymbolTableListTraits; 00030 00031 class Instruction : public User, public ilist_node<Instruction> { 00032 void operator=(const Instruction &) LLVM_DELETED_FUNCTION; 00033 Instruction(const Instruction &) LLVM_DELETED_FUNCTION; 00034 00035 BasicBlock *Parent; 00036 DebugLoc DbgLoc; // 'dbg' Metadata cache. 00037 00038 enum { 00039 /// HasMetadataBit - This is a bit stored in the SubClassData field which 00040 /// indicates whether this instruction has metadata attached to it or not. 00041 HasMetadataBit = 1 << 15 00042 }; 00043 public: 00044 // Out of line virtual method, so the vtable, etc has a home. 00045 ~Instruction(); 00046 00047 /// use_back - Specialize the methods defined in Value, as we know that an 00048 /// instruction can only be used by other instructions. 00049 Instruction *use_back() { return cast<Instruction>(*use_begin());} 00050 const Instruction *use_back() const { return cast<Instruction>(*use_begin());} 00051 00052 inline const BasicBlock *getParent() const { return Parent; } 00053 inline BasicBlock *getParent() { return Parent; } 00054 00055 /// removeFromParent - This method unlinks 'this' from the containing basic 00056 /// block, but does not delete it. 00057 /// 00058 void removeFromParent(); 00059 00060 /// eraseFromParent - This method unlinks 'this' from the containing basic 00061 /// block and deletes it. 00062 /// 00063 void eraseFromParent(); 00064 00065 /// insertBefore - Insert an unlinked instructions into a basic block 00066 /// immediately before the specified instruction. 00067 void insertBefore(Instruction *InsertPos); 00068 00069 /// insertAfter - Insert an unlinked instructions into a basic block 00070 /// immediately after the specified instruction. 00071 void insertAfter(Instruction *InsertPos); 00072 00073 /// moveBefore - Unlink this instruction from its current basic block and 00074 /// insert it into the basic block that MovePos lives in, right before 00075 /// MovePos. 00076 void moveBefore(Instruction *MovePos); 00077 00078 //===--------------------------------------------------------------------===// 00079 // Subclass classification. 00080 //===--------------------------------------------------------------------===// 00081 00082 /// getOpcode() returns a member of one of the enums like Instruction::Add. 00083 unsigned getOpcode() const { return getValueID() - InstructionVal; } 00084 00085 const char *getOpcodeName() const { return getOpcodeName(getOpcode()); } 00086 bool isTerminator() const { return isTerminator(getOpcode()); } 00087 bool isBinaryOp() const { return isBinaryOp(getOpcode()); } 00088 bool isShift() { return isShift(getOpcode()); } 00089 bool isCast() const { return isCast(getOpcode()); } 00090 00091 static const char* getOpcodeName(unsigned OpCode); 00092 00093 static inline bool isTerminator(unsigned OpCode) { 00094 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd; 00095 } 00096 00097 static inline bool isBinaryOp(unsigned Opcode) { 00098 return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd; 00099 } 00100 00101 /// @brief Determine if the Opcode is one of the shift instructions. 00102 static inline bool isShift(unsigned Opcode) { 00103 return Opcode >= Shl && Opcode <= AShr; 00104 } 00105 00106 /// isLogicalShift - Return true if this is a logical shift left or a logical 00107 /// shift right. 00108 inline bool isLogicalShift() const { 00109 return getOpcode() == Shl || getOpcode() == LShr; 00110 } 00111 00112 /// isArithmeticShift - Return true if this is an arithmetic shift right. 00113 inline bool isArithmeticShift() const { 00114 return getOpcode() == AShr; 00115 } 00116 00117 /// @brief Determine if the OpCode is one of the CastInst instructions. 00118 static inline bool isCast(unsigned OpCode) { 00119 return OpCode >= CastOpsBegin && OpCode < CastOpsEnd; 00120 } 00121 00122 //===--------------------------------------------------------------------===// 00123 // Metadata manipulation. 00124 //===--------------------------------------------------------------------===// 00125 00126 /// hasMetadata() - Return true if this instruction has any metadata attached 00127 /// to it. 00128 bool hasMetadata() const { 00129 return !DbgLoc.isUnknown() || hasMetadataHashEntry(); 00130 } 00131 00132 /// hasMetadataOtherThanDebugLoc - Return true if this instruction has 00133 /// metadata attached to it other than a debug location. 00134 bool hasMetadataOtherThanDebugLoc() const { 00135 return hasMetadataHashEntry(); 00136 } 00137 00138 /// getMetadata - Get the metadata of given kind attached to this Instruction. 00139 /// If the metadata is not found then return null. 00140 MDNode *getMetadata(unsigned KindID) const { 00141 if (!hasMetadata()) return 0; 00142 return getMetadataImpl(KindID); 00143 } 00144 00145 /// getMetadata - Get the metadata of given kind attached to this Instruction. 00146 /// If the metadata is not found then return null. 00147 MDNode *getMetadata(StringRef Kind) const { 00148 if (!hasMetadata()) return 0; 00149 return getMetadataImpl(Kind); 00150 } 00151 00152 /// getAllMetadata - Get all metadata attached to this Instruction. The first 00153 /// element of each pair returned is the KindID, the second element is the 00154 /// metadata value. This list is returned sorted by the KindID. 00155 void getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs)const{ 00156 if (hasMetadata()) 00157 getAllMetadataImpl(MDs); 00158 } 00159 00160 /// getAllMetadataOtherThanDebugLoc - This does the same thing as 00161 /// getAllMetadata, except that it filters out the debug location. 00162 void getAllMetadataOtherThanDebugLoc(SmallVectorImpl<std::pair<unsigned, 00163 MDNode*> > &MDs) const { 00164 if (hasMetadataOtherThanDebugLoc()) 00165 getAllMetadataOtherThanDebugLocImpl(MDs); 00166 } 00167 00168 /// setMetadata - Set the metadata of the specified kind to the specified 00169 /// node. This updates/replaces metadata if already present, or removes it if 00170 /// Node is null. 00171 void setMetadata(unsigned KindID, MDNode *Node); 00172 void setMetadata(StringRef Kind, MDNode *Node); 00173 00174 /// setDebugLoc - Set the debug location information for this instruction. 00175 void setDebugLoc(const DebugLoc &Loc) { DbgLoc = Loc; } 00176 00177 /// getDebugLoc - Return the debug location for this node as a DebugLoc. 00178 const DebugLoc &getDebugLoc() const { return DbgLoc; } 00179 00180 /// Set or clear the unsafe-algebra flag on this instruction, which must be an 00181 /// operator which supports this flag. See LangRef.html for the meaning of 00182 /// this flag. 00183 void setHasUnsafeAlgebra(bool B); 00184 00185 /// Set or clear the no-nans flag on this instruction, which must be an 00186 /// operator which supports this flag. See LangRef.html for the meaning of 00187 /// this flag. 00188 void setHasNoNaNs(bool B); 00189 00190 /// Set or clear the no-infs flag on this instruction, which must be an 00191 /// operator which supports this flag. See LangRef.html for the meaning of 00192 /// this flag. 00193 void setHasNoInfs(bool B); 00194 00195 /// Set or clear the no-signed-zeros flag on this instruction, which must be 00196 /// an operator which supports this flag. See LangRef.html for the meaning of 00197 /// this flag. 00198 void setHasNoSignedZeros(bool B); 00199 00200 /// Set or clear the allow-reciprocal flag on this instruction, which must be 00201 /// an operator which supports this flag. See LangRef.html for the meaning of 00202 /// this flag. 00203 void setHasAllowReciprocal(bool B); 00204 00205 /// Convenience function for setting all the fast-math flags on this 00206 /// instruction, which must be an operator which supports these flags. See 00207 /// LangRef.html for the meaning of these flats. 00208 void setFastMathFlags(FastMathFlags FMF); 00209 00210 /// Determine whether the unsafe-algebra flag is set. 00211 bool hasUnsafeAlgebra() const; 00212 00213 /// Determine whether the no-NaNs flag is set. 00214 bool hasNoNaNs() const; 00215 00216 /// Determine whether the no-infs flag is set. 00217 bool hasNoInfs() const; 00218 00219 /// Determine whether the no-signed-zeros flag is set. 00220 bool hasNoSignedZeros() const; 00221 00222 /// Determine whether the allow-reciprocal flag is set. 00223 bool hasAllowReciprocal() const; 00224 00225 /// Convenience function for getting all the fast-math flags, which must be an 00226 /// operator which supports these flags. See LangRef.html for the meaning of 00227 /// these flats. 00228 FastMathFlags getFastMathFlags() const; 00229 00230 /// Copy I's fast-math flags 00231 void copyFastMathFlags(const Instruction *I); 00232 00233 private: 00234 /// hasMetadataHashEntry - Return true if we have an entry in the on-the-side 00235 /// metadata hash. 00236 bool hasMetadataHashEntry() const { 00237 return (getSubclassDataFromValue() & HasMetadataBit) != 0; 00238 } 00239 00240 // These are all implemented in Metadata.cpp. 00241 MDNode *getMetadataImpl(unsigned KindID) const; 00242 MDNode *getMetadataImpl(StringRef Kind) const; 00243 void getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,MDNode*> > &)const; 00244 void getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned, 00245 MDNode*> > &) const; 00246 void clearMetadataHashEntries(); 00247 public: 00248 //===--------------------------------------------------------------------===// 00249 // Predicates and helper methods. 00250 //===--------------------------------------------------------------------===// 00251 00252 00253 /// isAssociative - Return true if the instruction is associative: 00254 /// 00255 /// Associative operators satisfy: x op (y op z) === (x op y) op z 00256 /// 00257 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative. 00258 /// 00259 bool isAssociative() const; 00260 static bool isAssociative(unsigned op); 00261 00262 /// isCommutative - Return true if the instruction is commutative: 00263 /// 00264 /// Commutative operators satisfy: (x op y) === (y op x) 00265 /// 00266 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when 00267 /// applied to any type. 00268 /// 00269 bool isCommutative() const { return isCommutative(getOpcode()); } 00270 static bool isCommutative(unsigned op); 00271 00272 /// isIdempotent - Return true if the instruction is idempotent: 00273 /// 00274 /// Idempotent operators satisfy: x op x === x 00275 /// 00276 /// In LLVM, the And and Or operators are idempotent. 00277 /// 00278 bool isIdempotent() const { return isIdempotent(getOpcode()); } 00279 static bool isIdempotent(unsigned op); 00280 00281 /// isNilpotent - Return true if the instruction is nilpotent: 00282 /// 00283 /// Nilpotent operators satisfy: x op x === Id, 00284 /// 00285 /// where Id is the identity for the operator, i.e. a constant such that 00286 /// x op Id === x and Id op x === x for all x. 00287 /// 00288 /// In LLVM, the Xor operator is nilpotent. 00289 /// 00290 bool isNilpotent() const { return isNilpotent(getOpcode()); } 00291 static bool isNilpotent(unsigned op); 00292 00293 /// mayWriteToMemory - Return true if this instruction may modify memory. 00294 /// 00295 bool mayWriteToMemory() const; 00296 00297 /// mayReadFromMemory - Return true if this instruction may read memory. 00298 /// 00299 bool mayReadFromMemory() const; 00300 00301 /// mayReadOrWriteMemory - Return true if this instruction may read or 00302 /// write memory. 00303 /// 00304 bool mayReadOrWriteMemory() const { 00305 return mayReadFromMemory() || mayWriteToMemory(); 00306 } 00307 00308 /// mayThrow - Return true if this instruction may throw an exception. 00309 /// 00310 bool mayThrow() const; 00311 00312 /// mayReturn - Return true if this is a function that may return. 00313 /// this is true for all normal instructions. The only exception 00314 /// is functions that are marked with the 'noreturn' attribute. 00315 /// 00316 bool mayReturn() const; 00317 00318 /// mayHaveSideEffects - Return true if the instruction may have side effects. 00319 /// 00320 /// Note that this does not consider malloc and alloca to have side 00321 /// effects because the newly allocated memory is completely invisible to 00322 /// instructions which don't used the returned value. For cases where this 00323 /// matters, isSafeToSpeculativelyExecute may be more appropriate. 00324 bool mayHaveSideEffects() const { 00325 return mayWriteToMemory() || mayThrow() || !mayReturn(); 00326 } 00327 00328 /// clone() - Create a copy of 'this' instruction that is identical in all 00329 /// ways except the following: 00330 /// * The instruction has no parent 00331 /// * The instruction has no name 00332 /// 00333 Instruction *clone() const; 00334 00335 /// isIdenticalTo - Return true if the specified instruction is exactly 00336 /// identical to the current one. This means that all operands match and any 00337 /// extra information (e.g. load is volatile) agree. 00338 bool isIdenticalTo(const Instruction *I) const; 00339 00340 /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it 00341 /// ignores the SubclassOptionalData flags, which specify conditions 00342 /// under which the instruction's result is undefined. 00343 bool isIdenticalToWhenDefined(const Instruction *I) const; 00344 00345 /// When checking for operation equivalence (using isSameOperationAs) it is 00346 /// sometimes useful to ignore certain attributes. 00347 enum OperationEquivalenceFlags { 00348 /// Check for equivalence ignoring load/store alignment. 00349 CompareIgnoringAlignment = 1<<0, 00350 /// Check for equivalence treating a type and a vector of that type 00351 /// as equivalent. 00352 CompareUsingScalarTypes = 1<<1 00353 }; 00354 00355 /// This function determines if the specified instruction executes the same 00356 /// operation as the current one. This means that the opcodes, type, operand 00357 /// types and any other factors affecting the operation must be the same. This 00358 /// is similar to isIdenticalTo except the operands themselves don't have to 00359 /// be identical. 00360 /// @returns true if the specified instruction is the same operation as 00361 /// the current one. 00362 /// @brief Determine if one instruction is the same operation as another. 00363 bool isSameOperationAs(const Instruction *I, unsigned flags = 0) const; 00364 00365 /// isUsedOutsideOfBlock - Return true if there are any uses of this 00366 /// instruction in blocks other than the specified block. Note that PHI nodes 00367 /// are considered to evaluate their operands in the corresponding predecessor 00368 /// block. 00369 bool isUsedOutsideOfBlock(const BasicBlock *BB) const; 00370 00371 00372 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00373 static inline bool classof(const Value *V) { 00374 return V->getValueID() >= Value::InstructionVal; 00375 } 00376 00377 //---------------------------------------------------------------------- 00378 // Exported enumerations. 00379 // 00380 enum TermOps { // These terminate basic blocks 00381 #define FIRST_TERM_INST(N) TermOpsBegin = N, 00382 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N, 00383 #define LAST_TERM_INST(N) TermOpsEnd = N+1 00384 #include "llvm/IR/Instruction.def" 00385 }; 00386 00387 enum BinaryOps { 00388 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N, 00389 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N, 00390 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1 00391 #include "llvm/IR/Instruction.def" 00392 }; 00393 00394 enum MemoryOps { 00395 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N, 00396 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N, 00397 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1 00398 #include "llvm/IR/Instruction.def" 00399 }; 00400 00401 enum CastOps { 00402 #define FIRST_CAST_INST(N) CastOpsBegin = N, 00403 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N, 00404 #define LAST_CAST_INST(N) CastOpsEnd = N+1 00405 #include "llvm/IR/Instruction.def" 00406 }; 00407 00408 enum OtherOps { 00409 #define FIRST_OTHER_INST(N) OtherOpsBegin = N, 00410 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N, 00411 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1 00412 #include "llvm/IR/Instruction.def" 00413 }; 00414 private: 00415 // Shadow Value::setValueSubclassData with a private forwarding method so that 00416 // subclasses cannot accidentally use it. 00417 void setValueSubclassData(unsigned short D) { 00418 Value::setValueSubclassData(D); 00419 } 00420 unsigned short getSubclassDataFromValue() const { 00421 return Value::getSubclassDataFromValue(); 00422 } 00423 00424 void setHasMetadataHashEntry(bool V) { 00425 setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) | 00426 (V ? HasMetadataBit : 0)); 00427 } 00428 00429 friend class SymbolTableListTraits<Instruction, BasicBlock>; 00430 void setParent(BasicBlock *P); 00431 protected: 00432 // Instruction subclasses can stick up to 15 bits of stuff into the 00433 // SubclassData field of instruction with these members. 00434 00435 // Verify that only the low 15 bits are used. 00436 void setInstructionSubclassData(unsigned short D) { 00437 assert((D & HasMetadataBit) == 0 && "Out of range value put into field"); 00438 setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D); 00439 } 00440 00441 unsigned getSubclassDataFromInstruction() const { 00442 return getSubclassDataFromValue() & ~HasMetadataBit; 00443 } 00444 00445 Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, 00446 Instruction *InsertBefore = 0); 00447 Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, 00448 BasicBlock *InsertAtEnd); 00449 virtual Instruction *clone_impl() const = 0; 00450 00451 }; 00452 00453 // Instruction* is only 4-byte aligned. 00454 template<> 00455 class PointerLikeTypeTraits<Instruction*> { 00456 typedef Instruction* PT; 00457 public: 00458 static inline void *getAsVoidPointer(PT P) { return P; } 00459 static inline PT getFromVoidPointer(void *P) { 00460 return static_cast<PT>(P); 00461 } 00462 enum { NumLowBitsAvailable = 2 }; 00463 }; 00464 00465 } // End llvm namespace 00466 00467 #endif