LLVM 20.0.0git
Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
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:
Inheritance graph
[legend]

Public Member Functions

 IRInstructionData (Instruction &I, bool Legality, IRInstructionDataList &IDL)
 Gather the information that is difficult to gather for an Instruction, or is changed.
 
 IRInstructionData (IRInstructionDataList &IDL)
 
void initializeInstruction ()
 Fills data stuctures for IRInstructionData when it is constructed from a.
 
CmpInst::Predicate getPredicate () const
 Get the predicate that the compare instruction is using for hashing the instruction.
 
StringRef getCalleeName () const
 Get the callee name that the call instruction is using for hashing the instruction.
 
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.
 
void setCalleeName (bool MatchByName=true)
 For an IRInstructionData containing a CallInst, set the function name appropriately.
 
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.
 
ArrayRef< Value * > getBlockOperVals ()
 Get the BasicBlock based operands for PHINodes and BranchInsts.
 
- Public Member Functions inherited from llvm::ilist_node_impl< OptionsT >
self_iterator getIterator ()
 
const_self_iterator getIterator () const
 
reverse_self_iterator getReverseIterator ()
 
const_reverse_self_iterator getReverseIterator () const
 
bool isSentinel () const
 Check whether this is the sentinel node.
 
- Public Member Functions inherited from llvm::ilist_detail::node_parent_access< ilist_node_impl< OptionsT >, OptionsT::parent_ty >
const OptionsT::parent_ty * getParent () const
 
OptionsT::parent_ty * getParent ()
 
void setParent (OptionsT::parent_ty *Parent)
 

Static Public Member Functions

static 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< OptionsT >
using self_iterator = typename ilist_select_iterator_type< OptionsT::has_iterator_bits, OptionsT, false, false >::type
 
using const_self_iterator = typename ilist_select_iterator_type< OptionsT::has_iterator_bits, OptionsT, false, true >::type
 
using reverse_self_iterator = typename ilist_select_iterator_type< OptionsT::has_iterator_bits, OptionsT, true, false >::type
 
using const_reverse_self_iterator = typename ilist_select_iterator_type< OptionsT::has_iterator_bits, OptionsT, true, true >::type
 
- Protected Member Functions inherited from llvm::ilist_node_impl< OptionsT >
 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 initializeInstruction().

◆ IRInstructionData() [2/2]

IRInstructionData::IRInstructionData ( IRInstructionDataList IDL)

Definition at line 85 of file IRSimilarityIdentifier.cpp.

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 113 of file IRSimilarityIdentifier.cpp.

References assert(), llvm::SmallVectorTemplateCommon< T, typename >::begin(), llvm::SmallVectorTemplateCommon< T, typename >::end(), Inst, 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 212 of file IRSimilarityIdentifier.cpp.

References assert(), CalleeName, and Inst.

◆ 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 202 of file IRSimilarityIdentifier.cpp.

References assert(), Inst, and RevisedPredicate.

◆ initializeInstruction()

void IRInstructionData::initializeInstruction ( )

◆ 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 186 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 88 of file IRSimilarityIdentifier.cpp.

References assert(), 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::SmallVectorTemplateBase< T, bool >::push_back(), 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 132 of file IRSimilarityIdentifier.cpp.

References assert(), CalleeName, llvm::CallBase::getCalledFunction(), llvm::Value::getName(), llvm::Intrinsic::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 159 of file IRSimilarityIdentifier.cpp.

References assert(), 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(), Idx, Inst, llvm::SmallVectorTemplateBase< T, bool >::push_back(), and RelativeBlockLocations.

Friends And Related Function Documentation

◆ 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 261 of file IRSimilarityIdentifier.h.

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 300 of file IRSimilarityIdentifier.h.

◆ 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.

◆ 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
Mark the given Function as meaning that it cannot be changed in any way mark any values that are used as this function s parameters or by its return values(according to Uses) live as well. void DeadArgumentEliminationPass

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: