LLVM 20.0.0git
|
FunctionComparator - Compares two functions to determine whether or not they will generate machine code with the same behaviour. More...
#include "llvm/Transforms/Utils/FunctionComparator.h"
Public Member Functions | |
FunctionComparator (const Function *F1, const Function *F2, GlobalNumberState *GN) | |
int | compare () |
Test whether the two functions have equivalent behaviour. | |
Protected Member Functions | |
void | beginCompare () |
Start the comparison. | |
int | compareSignature () const |
Compares the signature and other general attributes of the two functions. | |
int | cmpBasicBlocks (const BasicBlock *BBL, const BasicBlock *BBR) const |
Test whether two basic blocks have equivalent behaviour. | |
int | cmpConstants (const Constant *L, const Constant *R) const |
Constants comparison. | |
int | cmpGlobalValues (GlobalValue *L, GlobalValue *R) const |
Compares two global values by number. | |
int | cmpValues (const Value *L, const Value *R) const |
Assign or look up previously assigned numbers for the two values, and return whether the numbers are equal. | |
int | cmpOperations (const Instruction *L, const Instruction *R, bool &needToCmpOperands) const |
Compare two Instructions for equivalence, similar to Instruction::isSameOperationAs. | |
int | cmpTypes (Type *TyL, Type *TyR) const |
cmpType - compares two types, defines total ordering among the types set. | |
int | cmpNumbers (uint64_t L, uint64_t R) const |
int | cmpAligns (Align L, Align R) const |
int | cmpAPInts (const APInt &L, const APInt &R) const |
int | cmpAPFloats (const APFloat &L, const APFloat &R) const |
int | cmpMem (StringRef L, StringRef R) const |
Protected Attributes | |
const Function * | FnL |
const Function * | FnR |
FunctionComparator - Compares two functions to determine whether or not they will generate machine code with the same behaviour.
DataLayout is used if available. The comparator always fails conservatively (erring on the side of claiming that two functions are different).
Definition at line 93 of file FunctionComparator.h.
|
inline |
Definition at line 95 of file FunctionComparator.h.
|
inlineprotected |
Definition at line 61 of file FunctionComparator.cpp.
Referenced by cmpOperations().
Definition at line 87 of file FunctionComparator.cpp.
References cmpAPInts(), cmpNumbers(), llvm::APFloatBase::semanticsMaxExponent(), llvm::APFloatBase::semanticsMinExponent(), llvm::APFloatBase::semanticsPrecision(), and llvm::APFloatBase::semanticsSizeInBits().
Referenced by cmpConstants().
Definition at line 77 of file FunctionComparator.cpp.
References cmpNumbers().
Referenced by cmpAPFloats(), and cmpConstants().
|
protected |
Test whether two basic blocks have equivalent behaviour.
Definition at line 913 of file FunctionComparator.cpp.
References assert(), llvm::BasicBlock::begin(), cmpOperations(), cmpTypes(), cmpValues(), llvm::BasicBlock::end(), and llvm::Value::getType().
Referenced by compare().
Constants comparison.
Constants comparison:
Its analog to lexicographical comparison between hypothetical numbers of next format: <bitcastability-trait><raw-bit-contents>
Just for clearance, let's see how the set of constants could look on single dimension axis:
[NFCT], [FCT, "others"], [FCT, pointers], [FCT, vectors] Where: NFCT - Not a FirstClassType FCT - FirstClassTyp:
Now look, what could be inside [FCT, "others"], for example: [FCT, "others"] = [ [double 0.1], [double 1.23], [i32 1], [i32 2], { double 1.0 }, ; StructTyID, NumElements = 1 { i32 1 }, ; StructTyID, NumElements = 1 { double 1, i32 1 }, ; StructTyID, NumElements = 2 { i32 1, double 1 } ; StructTyID, NumElements = 2 ]
Let's explain the order. Float numbers will be less than integers, just because of cmpType terms: FloatTyID < IntegerTyID. Floats (with same fltSemantics) are sorted according to their value. Then you can see integers, and they are, like a floats, could be easy sorted among each others. The structures. Structures are grouped at the tail, again because of their TypeID: StructTyID > IntegerTyID > FloatTyID. Structures with greater number of elements are greater. Structures with greater elements going first are greater. The same logic with vectors, arrays and other possible complex types.
Bitcastable constants. Let's assume, that some constant, belongs to some group of "so-called-equal" values with different types, and at the same time belongs to another group of constants with equal types and "really" equal values.
Now, prove that this is impossible:
If constant A with type TyA is bitcastable to B with type TyB, then:
In another words, for pointers and vectors, we ignore top-level type and look at their particular properties (bit-width for vectors, and address space for pointers). If these properties are equal - compare their contents.
Definition at line 274 of file FunctionComparator.cpp.
References assert(), cmpAPFloats(), cmpAPInts(), cmpConstants(), cmpGlobalValues(), cmpMem(), cmpNumbers(), cmpTypes(), cmpValues(), llvm::dbgs(), F, FnL, FnR, llvm::PointerType::getAddressSpace(), llvm::BlockAddress::getBasicBlock(), llvm::BlockAddress::getFunction(), llvm::User::getNumOperands(), llvm::ConstantExpr::getOpcode(), llvm::User::getOperand(), llvm::Type::isFirstClassType(), LLVM_DEBUG, llvm_unreachable, OBOL, and RA.
Referenced by cmpConstants(), and cmpValues().
|
protected |
Compares two global values by number.
Uses the GlobalNumbersState to identify the same gobals across function calls.
Definition at line 514 of file FunctionComparator.cpp.
References cmpNumbers(), and llvm::GlobalNumberState::getNumber().
Referenced by cmpConstants().
Definition at line 106 of file FunctionComparator.cpp.
References cmpNumbers().
Referenced by cmpConstants(), and compareSignature().
Definition at line 53 of file FunctionComparator.cpp.
Referenced by cmpAPFloats(), cmpAPInts(), cmpConstants(), cmpGlobalValues(), cmpMem(), cmpOperations(), cmpTypes(), cmpValues(), and compareSignature().
|
protected |
Compare two Instructions for equivalence, similar to Instruction::isSameOperationAs.
Stages are listed in "most significant stage first" order: On each stage below, we do comparison between some left and right operation parts. If parts are non-equal, we assign parts comparison result to the operation comparison result and exit from method. Otherwise we proceed to the next stage. Stages:
Sets needToCmpOperands
to true if the operands of the instructions still must be compared afterwards. In this case it's already guaranteed that both instructions have the same number of operands.
Definition at line 623 of file FunctionComparator.cpp.
References cmpAligns(), cmpNumbers(), cmpTypes(), cmpValues(), llvm::PHINode::getIncomingBlock(), llvm::GetElementPtrInst::getPointerOperand(), and llvm::ArrayRef< T >::size().
Referenced by cmpBasicBlocks().
cmpType - compares two types, defines total ordering among the types set.
Return values: 0 if types are equal, -1 if Left is less than Right, +1 if Left is greater than Right.
Description: Comparison is broken onto stages. Like in lexicographical comparison stage coming first has higher priority. On each explanation stage keep in mind total ordering properties.
0. Before comparison we coerce pointer types of 0 address space to integer. We also don't bother with same type at left and right, so just return 0 in this case.
See method declaration comments for more details.
Definition at line 523 of file FunctionComparator.cpp.
References llvm::Type::ArrayTyID, assert(), cmpNumbers(), cmpTypes(), DL, llvm::Type::DoubleTyID, llvm::Type::FixedVectorTyID, llvm::Type::FloatTyID, FnL, llvm::Type::FP128TyID, llvm::Type::FunctionTyID, llvm::PointerType::getAddressSpace(), getBitWidth(), llvm::Function::getDataLayout(), llvm::StructType::getElementType(), llvm::StructType::getNumElements(), llvm::FunctionType::getNumParams(), llvm::FunctionType::getParamType(), llvm::FunctionType::getReturnType(), llvm::Type::getTypeID(), llvm::Type::IntegerTyID, llvm::StructType::isPacked(), llvm::FunctionType::isVarArg(), llvm::Type::LabelTyID, llvm_unreachable, llvm::Type::MetadataTyID, llvm::Type::PointerTyID, llvm::Type::PPC_FP128TyID, llvm::Type::ScalableVectorTyID, llvm::Type::StructTyID, llvm::Type::TokenTyID, llvm::Type::VoidTyID, and llvm::Type::X86_FP80TyID.
Referenced by cmpBasicBlocks(), cmpConstants(), cmpOperations(), cmpTypes(), and compareSignature().
Assign or look up previously assigned numbers for the two values, and return whether the numbers are equal.
Compare two values used by the two functions under pair-wise comparison.
Numbers are assigned in the order visited. Comparison order: Stage 0: Value that is function itself is always greater then others. If left and right values are references to their functions, then they are equal. Stage 1: Constants are greater than non-constants. If both left and right are constants, then the result of cmpConstants is used as cmpValues result. Stage 2: InlineAsm instances are greater than others. If both left and right are InlineAsm instances, InlineAsm* pointers casted to integers and compared as numbers. Stage 3: For all other cases we compare order we meet these values in their functions. If right value was met first during scanning, then left value is greater. In another words, we compare serial numbers, for more details see comments for sn_mapL and sn_mapR.
If this is the first time the values are seen, they're added to the mapping so that we will detect mismatches on next use. See comments in declaration for more details.
Definition at line 855 of file FunctionComparator.cpp.
References cmpConstants(), cmpNumbers(), FnL, FnR, and llvm::MetadataAsValue::getMetadata().
Referenced by cmpBasicBlocks(), cmpConstants(), cmpOperations(), compare(), and compareSignature().
int FunctionComparator::compare | ( | ) |
Test whether the two functions have equivalent behaviour.
Definition at line 993 of file FunctionComparator.cpp.
References assert(), beginCompare(), cmpBasicBlocks(), cmpValues(), compareSignature(), llvm::SmallVectorBase< Size_T >::empty(), FnL, FnR, llvm::Function::getEntryBlock(), llvm::Instruction::getNumSuccessors(), llvm::Instruction::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::SmallVectorImpl< T >::pop_back_val(), and llvm::SmallVectorTemplateBase< T, bool >::push_back().
|
protected |
Compares the signature and other general attributes of the two functions.
Definition at line 946 of file FunctionComparator.cpp.
References llvm::Function::arg_begin(), llvm::Function::arg_end(), llvm::Function::arg_size(), assert(), cmpMem(), cmpNumbers(), cmpTypes(), cmpValues(), FnL, FnR, llvm::Function::getAttributes(), llvm::Function::getCallingConv(), llvm::Function::getFunctionType(), llvm::Function::getGC(), llvm::GlobalObject::getSection(), llvm::Function::hasGC(), llvm::GlobalObject::hasSection(), llvm::Function::isVarArg(), and llvm_unreachable.
Referenced by compare().
Definition at line 324 of file FunctionComparator.h.
Referenced by cmpConstants(), cmpTypes(), cmpValues(), compare(), and compareSignature().
Definition at line 324 of file FunctionComparator.h.
Referenced by cmpConstants(), cmpValues(), compare(), and compareSignature().