LLVM
8.0.1
|
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 Types | |
using | FunctionHash = uint64_t |
Hash a function. More... | |
Public Member Functions | |
FunctionComparator (const Function *F1, const Function *F2, GlobalNumberState *GN) | |
int | compare () |
Test whether the two functions have equivalent behaviour. More... | |
Static Public Member Functions | |
static FunctionHash | functionHash (Function &) |
Protected Member Functions | |
void | beginCompare () |
Start the comparison. More... | |
int | compareSignature () const |
Compares the signature and other general attributes of the two functions. More... | |
int | cmpBasicBlocks (const BasicBlock *BBL, const BasicBlock *BBR) const |
Test whether two basic blocks have equivalent behaviour. More... | |
int | cmpConstants (const Constant *L, const Constant *R) const |
Constants comparison. More... | |
int | cmpGlobalValues (GlobalValue *L, GlobalValue *R) const |
Compares two global values by number. More... | |
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. More... | |
int | cmpOperations (const Instruction *L, const Instruction *R, bool &needToCmpOperands) const |
Compare two Instructions for equivalence, similar to Instruction::isSameOperationAs. More... | |
int | cmpTypes (Type *TyL, Type *TyR) const |
cmpType - compares two types, defines total ordering among the types set. More... | |
int | cmpNumbers (uint64_t L, uint64_t 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 94 of file FunctionComparator.h.
using llvm::FunctionComparator::FunctionHash = uint64_t |
Hash a function.
Equivalent functions will have the same hash, and unequal functions will have different hashes with high probability.
Definition at line 105 of file FunctionComparator.h.
|
inline |
Definition at line 96 of file FunctionComparator.h.
References llvm::ScaledNumbers::compare().
|
inlineprotected |
Definition at line 75 of file FunctionComparator.cpp.
References llvm::APFloat::bitcastToAPInt(), cmpAPInts(), cmpNumbers(), llvm::APFloat::getSemantics(), llvm::APFloatBase::semanticsMaxExponent(), llvm::APFloatBase::semanticsMinExponent(), llvm::APFloatBase::semanticsPrecision(), and llvm::APFloatBase::semanticsSizeInBits().
Referenced by cmpConstants().
Definition at line 67 of file FunctionComparator.cpp.
References cmpNumbers(), llvm::APInt::getBitWidth(), and llvm::APInt::ugt().
Referenced by cmpAPFloats(), cmpConstants(), cmpMem(), and cmpOperations().
|
protected |
Test whether two basic blocks have equivalent behaviour.
Definition at line 761 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 188 of file FunctionComparator.cpp.
References assert(), cmpAPFloats(), cmpAPInts(), cmpGlobalValues(), cmpMem(), cmpNumbers(), cmpTypes(), cmpValues(), llvm::dbgs(), llvm::dyn_cast(), F(), FnL, FnR, llvm::PointerType::getAddressSpace(), llvm::BlockAddress::getBasicBlock(), llvm::Function::getBasicBlockList(), llvm::Function::getFunction(), llvm::BlockAddress::getFunction(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::Value::getType(), llvm::Value::getValueID(), llvm::Type::isFirstClassType(), llvm::Constant::isNullValue(), llvm::AArch64CC::LE, LLVM_DEBUG, llvm_unreachable, llvm::AArch64CC::LS, and RA.
Referenced by cmpValues().
|
protected |
Compares two global values by number.
Uses the GlobalNumbersState to identify the same gobals across function calls.
Definition at line 385 of file FunctionComparator.cpp.
References cmpNumbers(), and llvm::GlobalNumberState::getNumber().
Referenced by cmpConstants().
Definition at line 94 of file FunctionComparator.cpp.
References assert(), llvm::AttributeSet::begin(), cmpAPInts(), cmpNumbers(), llvm::StringRef::compare(), llvm::AttributeSet::end(), llvm::AttributeList::getAttributes(), llvm::AttributeList::getNumAttrSets(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::getNumOperandBundles(), llvm::MDNode::getNumOperands(), llvm::MDNode::getOperand(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::getOperandBundleAt(), llvm::ConstantInt::getValue(), I, llvm::AttributeList::index_begin(), llvm::AttributeList::index_end(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::isCall(), llvm::AArch64CC::LE, RA, and llvm::StringRef::size().
Referenced by cmpConstants(), cmpOperations(), and compareSignature().
|
protected |
Definition at line 55 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 482 of file FunctionComparator.cpp.
References llvm::GEPOperator::accumulateConstantOffset(), llvm::AArch64_AM::ASR, assert(), cmpAPInts(), cmpMem(), cmpNumbers(), cmpTypes(), cmpValues(), FnL, getAlignment(), llvm::InlineAsm::getAsmString(), llvm::Intrinsic::getAttributes(), llvm::InlineAsm::getConstraintString(), llvm::Module::getDataLayout(), llvm::InlineAsm::getDialect(), llvm::InlineAsm::getFunctionType(), llvm::PHINode::getIncomingBlock(), llvm::User::getNumOperands(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::GlobalValue::getParent(), llvm::GEPOperator::getPointerAddressSpace(), llvm::GetElementPtrInst::getPointerOperand(), llvm::DataLayout::getPointerSizeInBits(), llvm::PPC::getPredicate(), llvm::Value::getRawSubclassOptionalData(), llvm::GEPOperator::getSourceElementType(), llvm::Value::getType(), llvm::InlineAsm::hasSideEffects(), llvm::InlineAsm::isAlignStack(), isVolatile(), isWeak(), llvm::LLVMContext::MD_range, SI, 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 394 of file FunctionComparator.cpp.
References llvm::Type::ArrayTyID, assert(), cmpNumbers(), llvm::Type::DoubleTyID, llvm::dyn_cast(), llvm::Type::FloatTyID, FnL, llvm::Type::FP128TyID, llvm::Type::FunctionTyID, llvm::PointerType::getAddressSpace(), getBitWidth(), llvm::Module::getDataLayout(), llvm::StructType::getElementType(), llvm::StructType::getNumElements(), llvm::FunctionType::getNumParams(), llvm::FunctionType::getParamType(), llvm::GlobalValue::getParent(), 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::StructTyID, llvm::Type::TokenTyID, llvm::Type::VectorTyID, llvm::Type::VoidTyID, and llvm::Type::X86_FP80TyID.
Referenced by cmpBasicBlocks(), cmpConstants(), cmpOperations(), 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 718 of file FunctionComparator.cpp.
References cmpConstants(), cmpNumbers(), llvm::dyn_cast(), FnL, and FnR.
Referenced by cmpBasicBlocks(), cmpConstants(), cmpOperations(), compare(), and compareSignature().
int FunctionComparator::compare | ( | ) |
Test whether the two functions have equivalent behaviour.
Definition at line 841 of file FunctionComparator.cpp.
References llvm::ARM_AM::add, assert(), beginCompare(), cmpBasicBlocks(), cmpValues(), compareSignature(), llvm::SmallVectorBase::empty(), FnL, FnR, llvm::Function::getEntryBlock(), llvm::Instruction::getNumSuccessors(), llvm::Instruction::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::hashing::detail::hash_16_bytes(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T >::push_back(), and second.
Referenced by llvm::createMergeFunctionsPass().
|
protected |
Compares the signature and other general attributes of the two functions.
Definition at line 794 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().
|
static |
Definition at line 919 of file FunctionComparator.cpp.
References llvm::Function::arg_size(), llvm::SmallVectorBase::empty(), llvm::Function::getEntryBlock(), llvm::Instruction::getNumSuccessors(), llvm::Instruction::getSuccessor(), H, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Function::isVarArg(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T >::push_back(), and second.
Referenced by llvm::createMergeFunctionsPass().
Definition at line 329 of file FunctionComparator.h.
Referenced by cmpConstants(), cmpOperations(), cmpTypes(), cmpValues(), compare(), and compareSignature().
Definition at line 329 of file FunctionComparator.h.
Referenced by cmpConstants(), cmpValues(), compare(), and compareSignature().