26 #define DEBUG_TYPE "functioncomparator"
35 if ((
int)L < (
int)R)
return -1;
36 if ((
int)L > (
int)R)
return 1;
43 if (L.
ugt(R))
return 1;
44 if (R.
ugt(L))
return -1;
87 for (; LI !=
LE && RI != RE; ++LI, ++RI) {
103 int FunctionComparator::cmpRangeMetadata(
const MDNode *L,
130 int FunctionComparator::cmpOperandBundlesSchema(
const Instruction *L,
135 assert(LCS && RCS &&
"Must be calls or invokes!");
136 assert(LCS.isCall() == RCS.isCall() &&
"Can't compare otherwise!");
139 cmpNumbers(LCS.getNumOperandBundles(), RCS.getNumOperandBundles()))
142 for (
unsigned i = 0, e = LCS.getNumOperandBundles(); i != e; ++i) {
143 auto OBL = LCS.getOperandBundleAt(i);
144 auto OBR = RCS.getOperandBundleAt(i);
146 if (
int Res = OBL.getTagName().compare(OBR.getTagName()))
149 if (
int Res =
cmpNumbers(OBL.Inputs.size(), OBR.Inputs.size()))
188 unsigned TyLWidth = 0;
189 unsigned TyRWidth = 0;
191 if (
auto *VecTyL = dyn_cast<VectorType>(TyL))
192 TyLWidth = VecTyL->getBitWidth();
193 if (
auto *VecTyR = dyn_cast<VectorType>(TyR))
194 TyRWidth = VecTyR->getBitWidth();
196 if (TyLWidth != TyRWidth)
205 unsigned AddrSpaceR = PTyR->getAddressSpace();
206 if (
int Res =
cmpNumbers(AddrSpaceL, AddrSpaceR))
231 if (GlobalValueL && GlobalValueR) {
238 if (
const auto *SeqL = dyn_cast<ConstantDataSequential>(L)) {
239 const auto *SeqR = cast<ConstantDataSequential>(R);
245 return cmpMem(SeqL->getRawDataValues(), SeqR->getRawDataValues());
249 case Value::UndefValueVal:
250 case Value::ConstantTokenNoneVal:
252 case Value::ConstantIntVal: {
253 const APInt &LInt = cast<ConstantInt>(
L)->getValue();
254 const APInt &RInt = cast<ConstantInt>(R)->getValue();
257 case Value::ConstantFPVal: {
258 const APFloat &LAPF = cast<ConstantFP>(
L)->getValueAPF();
259 const APFloat &RAPF = cast<ConstantFP>(R)->getValueAPF();
262 case Value::ConstantArrayVal: {
265 uint64_t NumElementsL = cast<ArrayType>(TyL)->getNumElements();
266 uint64_t NumElementsR = cast<ArrayType>(TyR)->getNumElements();
267 if (
int Res =
cmpNumbers(NumElementsL, NumElementsR))
269 for (uint64_t i = 0; i < NumElementsL; ++i) {
271 cast<Constant>(RA->getOperand(i))))
276 case Value::ConstantStructVal: {
279 unsigned NumElementsL = cast<StructType>(TyL)->getNumElements();
280 unsigned NumElementsR = cast<StructType>(TyR)->getNumElements();
281 if (
int Res =
cmpNumbers(NumElementsL, NumElementsR))
283 for (
unsigned i = 0; i != NumElementsL; ++i) {
285 cast<Constant>(RS->getOperand(i))))
290 case Value::ConstantVectorVal: {
293 unsigned NumElementsL = cast<VectorType>(TyL)->getNumElements();
294 unsigned NumElementsR = cast<VectorType>(TyR)->getNumElements();
295 if (
int Res =
cmpNumbers(NumElementsL, NumElementsR))
297 for (uint64_t i = 0; i < NumElementsL; ++i) {
299 cast<Constant>(RV->getOperand(i))))
304 case Value::ConstantExprVal: {
308 unsigned NumOperandsR = RE->getNumOperands();
309 if (
int Res =
cmpNumbers(NumOperandsL, NumOperandsR))
311 for (
unsigned i = 0; i < NumOperandsL; ++i) {
313 cast<Constant>(RE->getOperand(i))))
318 case Value::BlockAddressVal: {
360 uint64_t LNumber = GlobalNumbers->
getNumber(L);
361 uint64_t RNumber = GlobalNumbers->
getNumber(R);
374 TyL = DL.getIntPtrType(TyL);
375 if (PTyR && PTyR->getAddressSpace() == 0)
376 TyR = DL.getIntPtrType(TyR);
405 assert(PTyL && PTyR &&
"Both types must be pointers here.");
415 if (STyL->
isPacked() != STyR->isPacked())
431 if (FTyL->
isVarArg() != FTyR->isVarArg())
437 for (
unsigned i = 0, e = FTyL->
getNumParams(); i != e; ++i) {
446 auto *STyL = cast<SequentialType>(TyL);
447 auto *STyR = cast<SequentialType>(TyR);
448 if (STyL->getNumElements() != STyR->getNumElements())
449 return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
450 return cmpTypes(STyL->getElementType(), STyR->getElementType());
461 bool &needToCmpOperands)
const {
462 needToCmpOperands =
true;
474 needToCmpOperands =
false;
479 return cmpGEPs(GEPL, GEPR);
501 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(L)) {
502 if (
int Res =
cmpTypes(AI->getAllocatedType(),
503 cast<AllocaInst>(R)->getAllocatedType()))
507 if (
const LoadInst *LI = dyn_cast<LoadInst>(L)) {
514 cmpOrderings(LI->getOrdering(), cast<LoadInst>(R)->getOrdering()))
516 if (
int Res =
cmpNumbers(LI->getSyncScopeID(),
517 cast<LoadInst>(R)->getSyncScopeID()))
522 if (
const StoreInst *
SI = dyn_cast<StoreInst>(L)) {
530 cmpOrderings(
SI->getOrdering(), cast<StoreInst>(R)->getOrdering()))
533 cast<StoreInst>(R)->getSyncScopeID());
535 if (
const CmpInst *CI = dyn_cast<CmpInst>(L))
536 return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate());
537 if (
const CallInst *CI = dyn_cast<CallInst>(L)) {
538 if (
int Res =
cmpNumbers(CI->getCallingConv(),
539 cast<CallInst>(R)->getCallingConv()))
542 cmpAttrs(CI->getAttributes(), cast<CallInst>(R)->
getAttributes()))
544 if (
int Res = cmpOperandBundlesSchema(CI, R))
546 return cmpRangeMetadata(
550 if (
const InvokeInst *II = dyn_cast<InvokeInst>(L)) {
551 if (
int Res =
cmpNumbers(II->getCallingConv(),
552 cast<InvokeInst>(R)->getCallingConv()))
555 cmpAttrs(II->getAttributes(), cast<InvokeInst>(R)->
getAttributes()))
557 if (
int Res = cmpOperandBundlesSchema(II, R))
559 return cmpRangeMetadata(
568 for (
size_t i = 0, e = LIndices.
size(); i != e; ++i) {
569 if (
int Res =
cmpNumbers(LIndices[i], RIndices[i]))
579 for (
size_t i = 0, e = LIndices.
size(); i != e; ++i) {
580 if (
int Res =
cmpNumbers(LIndices[i], RIndices[i]))
584 if (
const FenceInst *FI = dyn_cast<FenceInst>(L)) {
586 cmpOrderings(FI->getOrdering(), cast<FenceInst>(R)->getOrdering()))
589 cast<FenceInst>(R)->getSyncScopeID());
596 cast<AtomicCmpXchgInst>(R)->
isWeak()))
599 cmpOrderings(CXI->getSuccessOrdering(),
600 cast<AtomicCmpXchgInst>(R)->getSuccessOrdering()))
603 cmpOrderings(CXI->getFailureOrdering(),
604 cast<AtomicCmpXchgInst>(R)->getFailureOrdering()))
607 cast<AtomicCmpXchgInst>(R)->getSyncScopeID());
609 if (
const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) {
610 if (
int Res =
cmpNumbers(RMWI->getOperation(),
611 cast<AtomicRMWInst>(R)->getOperation()))
616 if (
int Res = cmpOrderings(RMWI->getOrdering(),
617 cast<AtomicRMWInst>(R)->getOrdering()))
620 cast<AtomicRMWInst>(R)->getSyncScopeID());
622 if (
const PHINode *PNL = dyn_cast<PHINode>(L)) {
623 const PHINode *PNR = cast<PHINode>(R);
627 for (
unsigned i = 0, e = PNL->getNumIncomingValues(); i != e; ++i) {
638 int FunctionComparator::cmpGEPs(
const GEPOperator *GEPL,
651 APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0);
670 int FunctionComparator::cmpInlineAsm(
const InlineAsm *L,
711 if (ConstL && ConstR) {
725 if (InlineAsmL && InlineAsmR)
726 return cmpInlineAsm(InlineAsmL, InlineAsmR);
732 auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())),
733 RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size()));
735 return cmpNumbers(LeftSN.first->second, RightSN.first->second);
745 bool needToCmpOperands =
true;
746 if (
int Res =
cmpOperations(&*InstL, &*InstR, needToCmpOperands))
748 if (needToCmpOperands) {
749 assert(InstL->getNumOperands() == InstR->getNumOperands());
751 for (
unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) {
752 Value *OpL = InstL->getOperand(i);
753 Value *OpR = InstR->getOperand(i);
763 }
while (InstL != InstLE && InstR != InstRE);
765 if (InstL != InstLE && InstR == InstRE)
767 if (InstL == InstLE && InstR != InstRE)
804 "Identically typed functions have different numbers of args!");
811 ArgLI != ArgLE; ++ArgLI, ++ArgRI) {
835 VisitedBBs.
insert(FnLBBs[0]);
836 while (!FnLBBs.
empty()) {
870 class HashAccumulator64 {
874 HashAccumulator64() {
Hash = 0x6acaa36bef8325c5ULL; }
875 void add(uint64_t V) {
879 uint64_t getHash() {
return Hash; }
904 VisitedBBs.
insert(BBs[0]);
905 while (!BBs.
empty()) {
910 for (
auto &Inst : *BB) {
911 H.add(Inst.getOpcode());
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Type * getSourceElementType() const
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
AsmDialect getDialect() const
Value * getPointerOperand()
This class is the base class for the comparison instructions.
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 ...
This class represents an incoming formal argument to a Function.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
2: 32-bit floating point type
An instruction for ordering other memory operations.
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
unsigned getNumOperands() const
unsigned getNumOperands() const
Return number of MDNode operands.
This class represents a function call, abstracting a target machine's calling convention.
const std::string & getAsmString() const
const std::string & getConstraintString() const
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
unsigned getAlignment() const
Returns the alignment field of an attribute as a byte alignment value.
4: 80-bit floating point type (X87)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
An instruction for reading from memory.
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
int cmpAPInts(const APInt &L, const APInt &R) const
int cmpOperations(const Instruction *L, const Instruction *R, bool &needToCmpOperands) const
Compare two Instructions for equivalence, similar to Instruction::isSameOperationAs.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
iterator begin()
Instruction iterator methods.
AttributeList getAttributes() const
Return the attribute list for this Function.
The address of a basic block.
static uint32_t getAlignment(const MCSectionCOFF &Sec)
int cmpGlobalValues(GlobalValue *L, GlobalValue *R) const
Compares two global values by number.
bool hasGC() const
hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm to use during code generatio...
const APInt & getValue() const
Return the constant as an APInt value reference.
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Class to represent struct types.
APInt bitcastToAPInt() const
static ExponentType semanticsMaxExponent(const fltSemantics &)
LLVM_NODISCARD bool empty() const
AtomicOrdering
Atomic ordering for LLVM's memory model.
AttributeList getAttributes(LLVMContext &C, ID id)
Return the attributes for an intrinsic.
void beginCompare()
Start the comparison.
Class to represent function types.
A constant value that is initialized with an expression using other constant values.
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
static unsigned int semanticsSizeInBits(const fltSemantics &)
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value. ...
StringRef getSection() const
Get the custom section of this global if it has one.
TypeID getTypeID() const
Return the type id for the type.
AttributeSet getAttributes(unsigned Index) const
The attributes for the specified index are returned.
uint64_t hash_16_bytes(uint64_t low, uint64_t high)
An instruction for storing to memory.
uint64_t getNumber(GlobalValue *Global)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
size_t size() const
size - Get the array size.
static ExponentType semanticsMinExponent(const fltSemantics &)
Class to represent pointers.
static FunctionHash functionHash(Function &)
11: Arbitrary bit width integers
bool hasSection() const
Check if this global has a custom object file section.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Type * getParamType(unsigned i) const
Parameter type accessors.
Subclasses of this class are all able to terminate a basic block.
bool hasSideEffects() const
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
FunctionType * getFunctionType() const
Returns the FunctionType for me.
unsigned index_end() const
Type * getElementType(unsigned N) const
This is an important base class in LLVM.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
unsigned getBitWidth() const
Return the number of bits in the APInt.
unsigned getValueID() const
Return an ID for the concrete type of this object.
6: 128-bit floating point type (two 64-bits, PowerPC)
Value * getOperand(unsigned i) const
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Constant Vector Declarations.
int cmpNumbers(uint64_t L, uint64_t R) const
unsigned index_begin() const
Use these to iterate over the valid attribute indices.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
int cmpMem(StringRef L, StringRef R) const
unsigned getNumAttrSets() const
uint64_t FunctionHash
Hash a function.
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
int compareSignature() const
Compares the signature and other general attributes of the two functions.
const MDOperand & getOperand(unsigned I) const
FunctionType * getFunctionType() const
getFunctionType - InlineAsm's are always pointers to functions.
Iterator for intrusive lists based on ilist_node.
const BasicBlockListType & getBasicBlockList() const
Get the underlying elements of the Function...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
int cmpTypes(Type *TyL, Type *TyR) const
cmpType - compares two types, defines total ordering among the types set.
16: SIMD 'packed' format, or other vector type
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
LLVM_NODISCARD T pop_back_val()
BasicBlock * getBasicBlock() const
const BasicBlock & getEntryBlock() const
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
static unsigned int semanticsPrecision(const fltSemantics &)
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
ConstantArray - Constant Array Declarations.
Class for arbitrary precision integers.
static bool isWeak(const MCSymbolELF &Sym)
int compare()
Test whether the two functions have equivalent behaviour.
llvm_unreachable("Unknown RegBank!")
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
int cmpConstants(const Constant *L, const Constant *R) const
Constants comparison.
unsigned getRawSubclassOptionalData() const
Return the raw optional flags value contained in this value.
Establish a view to a call site for examination.
const std::string & getGC() const
int cmpBasicBlocks(const BasicBlock *BBL, const BasicBlock *BBR) const
Test whether two basic blocks have equivalent behaviour.
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getPointerAddressSpace() const
Method to return the address space of the pointer operand.
3: 64-bit floating point type
Type * getReturnType() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Function * getFunction() const
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
int cmpAPFloats(const APFloat &L, const APFloat &R) const
std::string Hash(const Unit &U)
StringRef - Represent a constant reference to a string, i.e.
static bool isVolatile(Instruction *Inst)
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
unsigned getNumElements() const
Random access to the elements.
bool isAlignStack() const
const fltSemantics & getSemantics() const
an instruction to allocate memory on the stack
This instruction inserts a struct field of array element value into an aggregate value.
5: 128-bit floating point type (112-bit mantissa)