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