LLVM 22.0.0git
llvm::IRSimilarity::IRInstructionData Struct Reference

This provides the utilities for hashing an Instruction to an unsigned integer. More...

#include "llvm/Analysis/IRSimilarityIdentifier.h"

Inheritance diagram for llvm::IRSimilarity::IRInstructionData:
[legend]

Public Member Functions

LLVM_ABI IRInstructionData (Instruction &I, bool Legality, IRInstructionDataList &IDL)
 Gather the information that is difficult to gather for an Instruction, or is changed.
LLVM_ABI IRInstructionData (IRInstructionDataList &IDL)
LLVM_ABI void initializeInstruction ()
 Fills data stuctures for IRInstructionData when it is constructed from a.
LLVM_ABI CmpInst::Predicate getPredicate () const
 Get the predicate that the compare instruction is using for hashing the instruction.
LLVM_ABI StringRef getCalleeName () const
 Get the callee name that the call instruction is using for hashing the instruction.
LLVM_ABI void setBranchSuccessors (DenseMap< BasicBlock *, unsigned > &BasicBlockToInteger)
 For an IRInstructionData containing a branch, finds the relative distances from the source basic block to the target by taking the difference of the number assigned to the current basic block and the target basic block of the branch.
LLVM_ABI void setCalleeName (bool MatchByName=true)
 For an IRInstructionData containing a CallInst, set the function name appropriately.
LLVM_ABI void setPHIPredecessors (DenseMap< BasicBlock *, unsigned > &BasicBlockToInteger)
 For an IRInstructionData containing a PHINode, finds the relative distances from the incoming basic block to the current block by taking the difference of the number assigned to the current basic block and the incoming basic block of the branch.
LLVM_ABI ArrayRef< Value * > getBlockOperVals ()
 Get the BasicBlock based operands for PHINodes and BranchInsts.
Public Member Functions inherited from llvm::ilist_node_impl< ilist_detail::compute_node_options< IRInstructionData, Options... >::type >
self_iterator getIterator ()
reverse_self_iterator getReverseIterator ()
std::enable_if_t< T::is_sentinel_tracking_explicit, boolisSentinel () const
 Check whether this is the sentinel node.
Public Member Functions inherited from llvm::ilist_detail::node_parent_access< ilist_node_impl< ilist_detail::compute_node_options< IRInstructionData, Options... >::type >, ilist_detail::compute_node_options< IRInstructionData, Options... >::type::parent_ty >
const ilist_detail::compute_node_options< IRInstructionData, Options... >::type::parent_ty * getParent () const
void setParent (ilist_detail::compute_node_options< IRInstructionData, Options... >::type::parent_ty *Parent)

Static Public Member Functions

static LLVM_ABI CmpInst::Predicate predicateForConsistency (CmpInst *CI)
 A function that swaps the predicates to their less than form if they are in a greater than form.

Public Attributes

InstructionInst = nullptr
 The source Instruction that is being wrapped.
SmallVector< Value *, 4 > OperVals
 The values of the operands in the Instruction.
bool Legal = false
 The legality of the wrapped instruction.
std::optional< CmpInst::PredicateRevisedPredicate
 This is only relevant if we are wrapping a CmpInst where we needed to change the predicate of a compare instruction from a greater than form to a less than form.
std::optional< std::string > CalleeName
 This is only relevant if we are wrapping a CallInst.
SmallVector< int, 4 > RelativeBlockLocations
 This structure holds the distances of how far "ahead of" or "behind" the target blocks of a branch, or the incoming blocks of a phi nodes are.
IRInstructionDataListIDL = nullptr

Friends

hash_code hash_value (const IRInstructionData &ID)
 Hashes Value based on its opcode, types, and operand types.

Additional Inherited Members

Protected Types inherited from llvm::ilist_node_impl< ilist_detail::compute_node_options< IRInstructionData, Options... >::type >
using self_iterator
using const_self_iterator
using reverse_self_iterator
using const_reverse_self_iterator
Protected Member Functions inherited from llvm::ilist_node_impl< ilist_detail::compute_node_options< IRInstructionData, Options... >::type >
 ilist_node_impl ()=default

Detailed Description

This provides the utilities for hashing an Instruction to an unsigned integer.

Two IRInstructionDatas produce the same hash value when their underlying Instructions perform the same operation (even if they don't have the same input operands.) As a more concrete example, consider the following:

%add1 = add i32 %a, %b
%add2 = add i32 %c, %d
%add3 = add i64 %e, %f

so:

; These two adds have the same types and operand types, so they hash to the
; same number.
%add1 = add i32 %a, %b ; Hash: 1
%add2 = add i32 %c, %d ; Hash: 1
; This add produces an i64. This differentiates it from %add1 and %add2. So,
; it hashes to a different number.
%add3 = add i64 %e, %f; Hash: 2

This hashing scheme will be used to represent the program as a very long string. This string can then be placed in a data structure which can be used for similarity queries.

TODO: Handle types of Instructions which can be equal even with different operands. (E.g. comparisons with swapped predicates.) TODO: Handle CallInsts, which are only checked for function type by isSameOperationAs. TODO: Handle GetElementPtrInsts, as some of the operands have to be the exact same, and some do not.

Definition at line 115 of file IRSimilarityIdentifier.h.

Constructor & Destructor Documentation

◆ IRInstructionData() [1/2]

IRInstructionData::IRInstructionData ( Instruction & I,
bool Legality,
IRInstructionDataList & IDL )

Gather the information that is difficult to gather for an Instruction, or is changed.

i.e. the operands of an Instruction and the Types of those operands. This extra information allows for similarity matching to make assertions that allow for more flexibility when checking for whether an Instruction performs the same operation.

Definition at line 49 of file IRSimilarityIdentifier.cpp.

References I, IDL, initializeInstruction(), Inst, and Legal.

Referenced by hash_value.

◆ IRInstructionData() [2/2]

IRInstructionData::IRInstructionData ( IRInstructionDataList & IDL)

Definition at line 84 of file IRSimilarityIdentifier.cpp.

References IDL.

Member Function Documentation

◆ getBlockOperVals()

ArrayRef< Value * > IRInstructionData::getBlockOperVals ( )

Get the BasicBlock based operands for PHINodes and BranchInsts.

Returns
A list of relevant BasicBlocks.

Definition at line 112 of file IRSimilarityIdentifier.cpp.

References llvm::ArrayRef(), assert(), llvm::dyn_cast(), Inst, llvm::isa(), and OperVals.

Referenced by setBranchSuccessors().

◆ getCalleeName()

StringRef IRInstructionData::getCalleeName ( ) const

Get the callee name that the call instruction is using for hashing the instruction.

The IRInstructionData must be wrapping a CallInst.

Definition at line 211 of file IRSimilarityIdentifier.cpp.

References assert(), CalleeName, Inst, and llvm::isa().

◆ getPredicate()

CmpInst::Predicate IRInstructionData::getPredicate ( ) const

Get the predicate that the compare instruction is using for hashing the instruction.

the IRInstructionData must be wrapping a CmpInst.

Definition at line 201 of file IRSimilarityIdentifier.cpp.

References assert(), llvm::cast(), Inst, llvm::isa(), and RevisedPredicate.

◆ initializeInstruction()

void IRInstructionData::initializeInstruction ( )

Fills data stuctures for IRInstructionData when it is constructed from a.

Definition at line 55 of file IRSimilarityIdentifier.cpp.

References llvm::append_range(), llvm::CallingConv::C, llvm::dyn_cast(), Inst, llvm::isa(), OperVals, predicateForConsistency(), and RevisedPredicate.

Referenced by IRInstructionData().

◆ predicateForConsistency()

CmpInst::Predicate IRInstructionData::predicateForConsistency ( CmpInst * CI)
static

A function that swaps the predicates to their less than form if they are in a greater than form.

Otherwise, the predicate is unchanged.

Parameters
CI- The comparison operation to find a consistent preidcate for.
Returns
the consistent comparison predicate.

Definition at line 185 of file IRSimilarityIdentifier.cpp.

References llvm::CmpInst::FCMP_OGE, llvm::CmpInst::FCMP_OGT, llvm::CmpInst::FCMP_UGE, llvm::CmpInst::FCMP_UGT, llvm::CmpInst::getPredicate(), llvm::CmpInst::getSwappedPredicate(), llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_UGE, and llvm::CmpInst::ICMP_UGT.

Referenced by initializeInstruction().

◆ setBranchSuccessors()

void IRInstructionData::setBranchSuccessors ( DenseMap< BasicBlock *, unsigned > & BasicBlockToInteger)

For an IRInstructionData containing a branch, finds the relative distances from the source basic block to the target by taking the difference of the number assigned to the current basic block and the target basic block of the branch.

Parameters
BasicBlockToInteger- The mapping of basic blocks to their location in the module.

Definition at line 87 of file IRSimilarityIdentifier.cpp.

References assert(), llvm::cast(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), getBlockOperVals(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), Inst, llvm::isa(), RelativeBlockLocations, and llvm::Successor.

◆ setCalleeName()

void IRInstructionData::setCalleeName ( bool MatchByName = true)

For an IRInstructionData containing a CallInst, set the function name appropriately.

This will be an empty string if it is an indirect call, or we are not matching by name of the called function. It will be the name of the function if MatchByName is true and it is not an indirect call. We may decide not to match by name in order to expand the size of the regions we can match. If a function name has the same type signature, but the different name, the region of code is still almost the same. Since function names can be treated as constants, the name itself could be extrapolated away. However, matching by name provides a specificity and more "identical" code than not matching by name.

Parameters
MatchByName- A flag to mark whether we are using the called function name as a differentiating parameter.

Definition at line 131 of file IRSimilarityIdentifier.cpp.

References assert(), CalleeName, llvm::dyn_cast(), llvm::CallBase::getCalledFunction(), llvm::Intrinsic::getName(), llvm::Value::getName(), II, Inst, llvm::CallBase::isIndirectCall(), llvm::Intrinsic::isOverloaded(), llvm::FunctionType::params(), and llvm::StringRef::str().

◆ setPHIPredecessors()

void IRInstructionData::setPHIPredecessors ( DenseMap< BasicBlock *, unsigned > & BasicBlockToInteger)

For an IRInstructionData containing a PHINode, finds the relative distances from the incoming basic block to the current block by taking the difference of the number assigned to the current basic block and the incoming basic block of the branch.

Parameters
BasicBlockToInteger- The mapping of basic blocks to their location in the module.

Definition at line 158 of file IRSimilarityIdentifier.cpp.

References assert(), llvm::cast(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getNumIncomingValues(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), Inst, llvm::isa(), and RelativeBlockLocations.

◆ hash_value

hash_code hash_value ( const IRInstructionData & ID)
friend

Hashes Value based on its opcode, types, and operand types.

Two IRInstructionData instances produce the same hash when they perform the same operation.

As a simple example, consider the following instructions.

%add1 = add i32 %x1, %y1
%add2 = add i32 %x2, %y2
%sub = sub i32 %x1, %y1
%add_i64 = add i64 %x2, %y2

Because the first two adds operate the same types, and are performing the same action, they will be hashed to the same value.

However, the subtraction instruction is not the same as an addition, and will be hashed to a different value.

Finally, the last add has a different type compared to the first two add instructions, so it will also be hashed to a different value that any of the previous instructions.

Parameters
[in]ID- The IRInstructionData instance to be hashed.
Returns
A hash_value of the IRInstructionData.

Definition at line 262 of file IRSimilarityIdentifier.h.

References llvm::dyn_cast(), llvm::hash_combine(), llvm::hash_combine_range(), llvm::hash_value(), II, IRInstructionData(), llvm::isa(), and llvm::SmallVectorTemplateBase< T, bool >::push_back().

Member Data Documentation

◆ CalleeName

std::optional<std::string> llvm::IRSimilarity::IRInstructionData::CalleeName

This is only relevant if we are wrapping a CallInst.

If we are requiring that the function calls have matching names as well as types, and the call is not an indirect call, this will hold the name of the function. If it is an indirect string, it will be the empty string. However, if this requirement is not in place it will be the empty string regardless of the function call type. The value held here is used to create the hash of the instruction, and check to make sure two instructions are close to one another.

Definition at line 141 of file IRSimilarityIdentifier.h.

Referenced by getCalleeName(), and setCalleeName().

◆ IDL

IRInstructionDataList* llvm::IRSimilarity::IRInstructionData::IDL = nullptr

Definition at line 299 of file IRSimilarityIdentifier.h.

Referenced by IRInstructionData(), and IRInstructionData().

◆ Inst

Instruction* llvm::IRSimilarity::IRInstructionData::Inst = nullptr

◆ Legal

bool llvm::IRSimilarity::IRInstructionData::Legal = false

The legality of the wrapped instruction.

This is informed by InstrType, and is used when checking when two instructions are considered similar. If either instruction is not legal, the instructions are automatically not considered similar.

Definition at line 126 of file IRSimilarityIdentifier.h.

Referenced by IRInstructionData().

◆ OperVals

SmallVector<Value *, 4> llvm::IRSimilarity::IRInstructionData::OperVals

The values of the operands in the Instruction.

Definition at line 121 of file IRSimilarityIdentifier.h.

Referenced by getBlockOperVals(), and initializeInstruction().

◆ RelativeBlockLocations

SmallVector<int, 4> llvm::IRSimilarity::IRInstructionData::RelativeBlockLocations

This structure holds the distances of how far "ahead of" or "behind" the target blocks of a branch, or the incoming blocks of a phi nodes are.

If the value is negative, it means that the block was registered before the block of this instruction in terms of blocks in the function. Code Example:

block_1:
br i1 %0, label %block_2, label %block_3
block_2:
br i1 %1, label %block_1, label %block_2
block_3:
br i1 %2, label %block_2, label %block_1
; Replacing the labels with relative values, this becomes:
block_1:
br i1 %0, distance 1, distance 2
block_2:
br i1 %1, distance -1, distance 0
block_3:
br i1 %2, distance -1, distance -2

Taking block_2 as our example, block_1 is "behind" block_2, and block_2 is "ahead" of block_2.

Definition at line 165 of file IRSimilarityIdentifier.h.

Referenced by setBranchSuccessors(), and setPHIPredecessors().

◆ RevisedPredicate

std::optional<CmpInst::Predicate> llvm::IRSimilarity::IRInstructionData::RevisedPredicate

This is only relevant if we are wrapping a CmpInst where we needed to change the predicate of a compare instruction from a greater than form to a less than form.

It is std::nullopt otherwise.

Definition at line 131 of file IRSimilarityIdentifier.h.

Referenced by getPredicate(), and initializeInstruction().


The documentation for this struct was generated from the following files: