#define LLVM_IR_INSTRUCTION_H
#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/None.h"
+#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/SymbolTableListTraits.h"
#include "llvm/IR/User.h"
+#include "llvm/IR/Value.h"
+#include "llvm/Support/Casting.h"
+#include <algorithm>
+#include <cassert>
+#include <cstdint>
+#include <utility>
namespace llvm {
+class BasicBlock;
class FastMathFlags;
-class LLVMContext;
class MDNode;
-class BasicBlock;
struct AAMDNodes;
+template <> struct ilist_alloc_traits<Instruction> {
+ static inline void deleteNode(Instruction *V);
+};
+
class Instruction : public User,
public ilist_node_with_parent<Instruction, BasicBlock> {
- void operator=(const Instruction &) = delete;
- Instruction(const Instruction &) = delete;
-
BasicBlock *Parent;
DebugLoc DbgLoc; // 'dbg' Metadata cache.
/// this instruction has metadata attached to it or not.
HasMetadataBit = 1 << 15
};
+
+protected:
+ ~Instruction(); // Use deleteValue() to delete a generic Instruction.
+
public:
- // Out of line virtual method, so the vtable, etc has a home.
- ~Instruction() override;
+ Instruction(const Instruction &) = delete;
+ Instruction &operator=(const Instruction &) = delete;
/// Specialize the methods defined in Value, as we know that an instruction
/// can only be used by other instructions.
/// Note: this is undefined behavior if the instruction does not have a
/// parent, or the parent basic block does not have a parent function.
const Module *getModule() const;
- Module *getModule();
+ Module *getModule() {
+ return const_cast<Module *>(
+ static_cast<const Instruction *>(this)->getModule());
+ }
/// Return the function this instruction belongs to.
///
/// Note: it is undefined behavior to call this on an instruction not
/// currently inserted into a function.
const Function *getFunction() const;
- Function *getFunction();
+ Function *getFunction() {
+ return const_cast<Function *>(
+ static_cast<const Instruction *>(this)->getFunction());
+ }
/// This method unlinks 'this' from the containing basic block, but does not
/// delete it.
return getOpcode() == AShr;
}
+ /// Return true if this is and/or/xor.
+ inline bool isBitwiseLogicOp() const {
+ return getOpcode() == And || getOpcode() == Or || getOpcode() == Xor;
+ }
+
/// Determine if the OpCode is one of the CastInst instructions.
static inline bool isCast(unsigned OpCode) {
return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
void setMetadata(unsigned KindID, MDNode *Node);
void setMetadata(StringRef Kind, MDNode *Node);
+ /// Copy metadata from \p SrcInst to this instruction. \p WL, if not empty,
+ /// specifies the list of meta data that needs to be copied. If \p WL is
+ /// empty, all meta data will be copied.
+ void copyMetadata(const Instruction &SrcInst,
+ ArrayRef<unsigned> WL = ArrayRef<unsigned>());
+
+ /// If the instruction has "branch_weights" MD_prof metadata and the MDNode
+ /// has three operands (including name string), swap the order of the
+ /// metadata.
+ void swapProfMetadata();
+
/// Drop all unknown metadata except for debug locations.
/// @{
/// Passes are required to drop metadata they don't understand. This is a
/// Retrieve the raw weight values of a conditional branch or select.
/// Returns true on success with profile weights filled in.
/// Returns false if no metadata or invalid metadata was found.
- bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal);
+ bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const;
/// Retrieve total raw weight values of a branch.
/// Returns true on success with profile total weights filled in.
/// Returns false if no metadata was found.
- bool extractProfTotalWeight(uint64_t &TotalVal);
+ bool extractProfTotalWeight(uint64_t &TotalVal) const;
+
+ /// Updates branch_weights metadata by scaling it by \p S / \p T.
+ void updateProfWeight(uint64_t S, uint64_t T);
+
+ /// Sets the branch_weights metadata to \p W for CallInst.
+ void setProfWeight(uint64_t W);
/// Set the debug location information for this instruction.
void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); }
/// Determine whether the no signed wrap flag is set.
bool hasNoSignedWrap() const;
+ /// Drops flags that may cause this instruction to evaluate to poison despite
+ /// having non-poison inputs.
+ void dropPoisonGeneratingFlags();
+
/// Determine whether the exact flag is set.
bool isExact() const;
/// Determine whether the allow-reciprocal flag is set.
bool hasAllowReciprocal() const;
+ /// Determine whether the allow-contract flag is set.
+ bool hasAllowContract() const;
+
/// Convenience function for getting all the fast-math flags, which must be an
/// operator which supports these flags. See LangRef.html for the meaning of
/// these flags.
SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const;
/// Clear all hashtable-based metadata from this instruction.
void clearMetadataHashEntries();
+
public:
//===--------------------------------------------------------------------===//
// Predicates and helper methods.
//===--------------------------------------------------------------------===//
-
/// Return true if the instruction is associative:
///
/// Associative operators satisfy: x op (y op z) === (x op y) op z
///
/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
///
- bool isAssociative() const;
- static bool isAssociative(unsigned op);
+ bool isAssociative() const LLVM_READONLY;
+ static bool isAssociative(unsigned Opcode) {
+ return Opcode == And || Opcode == Or || Opcode == Xor ||
+ Opcode == Add || Opcode == Mul;
+ }
/// Return true if the instruction is commutative:
///
/// Commutative operators satisfy: (x op y) === (y op x)
///
- /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
+ /// In LLVM, these are the commutative operators, plus SetEQ and SetNE, when
/// applied to any type.
///
bool isCommutative() const { return isCommutative(getOpcode()); }
- static bool isCommutative(unsigned op);
+ static bool isCommutative(unsigned Opcode) {
+ switch (Opcode) {
+ case Add: case FAdd:
+ case Mul: case FMul:
+ case And: case Or: case Xor:
+ return true;
+ default:
+ return false;
+ }
+ }
/// Return true if the instruction is idempotent:
///
/// In LLVM, the And and Or operators are idempotent.
///
bool isIdempotent() const { return isIdempotent(getOpcode()); }
- static bool isIdempotent(unsigned op);
+ static bool isIdempotent(unsigned Opcode) {
+ return Opcode == And || Opcode == Or;
+ }
/// Return true if the instruction is nilpotent:
///
/// In LLVM, the Xor operator is nilpotent.
///
bool isNilpotent() const { return isNilpotent(getOpcode()); }
- static bool isNilpotent(unsigned op);
+ static bool isNilpotent(unsigned Opcode) {
+ return Opcode == Xor;
+ }
/// Return true if this instruction may modify memory.
bool mayWriteToMemory() const;
/// higher.
bool isAtomic() const;
+ /// Return true if this atomic instruction loads from memory.
+ bool hasAtomicLoad() const;
+
+ /// Return true if this atomic instruction stores to memory.
+ bool hasAtomicStore() const;
+
/// Return true if this instruction may throw an exception.
bool mayThrow() const;
bool isIdenticalTo(const Instruction *I) const;
/// This is like isIdenticalTo, except that it ignores the
- /// SubclassOptionalData flags, which specify conditions under which the
+ /// SubclassOptionalData flags, which may specify conditions under which the
/// instruction's result is undefined.
bool isIdenticalToWhenDefined(const Instruction *I) const;
#define LAST_OTHER_INST(N) OtherOpsEnd = N+1
#include "llvm/IR/Instruction.def"
};
+
private:
+ friend class SymbolTableListTraits<Instruction>;
+
// Shadow Value::setValueSubclassData with a private forwarding method so that
// subclasses cannot accidentally use it.
void setValueSubclassData(unsigned short D) {
Value::setValueSubclassData(D);
}
+
unsigned short getSubclassDataFromValue() const {
return Value::getSubclassDataFromValue();
}
(V ? HasMetadataBit : 0));
}
- friend class SymbolTableListTraits<Instruction>;
void setParent(BasicBlock *P);
+
protected:
// Instruction subclasses can stick up to 15 bits of stuff into the
// SubclassData field of instruction with these members.
Instruction *cloneImpl() const;
};
-// Instruction* is only 4-byte aligned.
-template<>
-class PointerLikeTypeTraits<Instruction*> {
- typedef Instruction* PT;
-public:
- static inline void *getAsVoidPointer(PT P) { return P; }
- static inline PT getFromVoidPointer(void *P) {
- return static_cast<PT>(P);
- }
- enum { NumLowBitsAvailable = 2 };
-};
+inline void ilist_alloc_traits<Instruction>::deleteNode(Instruction *V) {
+ V->deleteValue();
+}
-} // End llvm namespace
+} // end namespace llvm
-#endif
+#endif // LLVM_IR_INSTRUCTION_H