#include "llvm/Analysis/IRSimilarityIdentifier.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/User.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/SuffixTree.h"
usingnamespacellvm;
usingnamespaceIRSimilarity;
namespace llvm {
cl::opt<bool>
DisableBranches("no-ir-sim-branch-matching", cl::init(false),
cl::ReallyHidden,
cl::desc("disable similarity matching, and outlining, "
"across branches for debugging purposes."));
cl::opt<bool>
DisableIndirectCalls("no-ir-sim-indirect-calls", cl::init(false),
cl::ReallyHidden,
cl::desc("disable outlining indirect calls."));
cl::opt<bool>
MatchCallsByName("ir-sim-calls-by-name", cl::init(false), cl::ReallyHidden,
cl::desc("only allow matching call instructions if the "
"name and type signature match."));
cl::opt<bool>
DisableIntrinsics("no-ir-sim-intrinsics", cl::init(false), cl::ReallyHidden,
cl::desc("Don't match or outline intrinsics"));
}
IRInstructionData::IRInstructionData(Instruction &I, bool Legality,
IRInstructionDataList &IDList)
: … { … }
void IRInstructionData::initializeInstruction() { … }
IRInstructionData::IRInstructionData(IRInstructionDataList &IDList)
: … { … }
void IRInstructionData::setBranchSuccessors(
DenseMap<BasicBlock *, unsigned> &BasicBlockToInteger) { … }
ArrayRef<Value *> IRInstructionData::getBlockOperVals() { … }
void IRInstructionData::setCalleeName(bool MatchByName) { … }
void IRInstructionData::setPHIPredecessors(
DenseMap<BasicBlock *, unsigned> &BasicBlockToInteger) { … }
CmpInst::Predicate IRInstructionData::predicateForConsistency(CmpInst *CI) { … }
CmpInst::Predicate IRInstructionData::getPredicate() const { … }
StringRef IRInstructionData::getCalleeName() const { … }
bool IRSimilarity::isClose(const IRInstructionData &A,
const IRInstructionData &B) { … }
void IRInstructionMapper::convertToUnsignedVec(
BasicBlock &BB, std::vector<IRInstructionData *> &InstrList,
std::vector<unsigned> &IntegerMapping) { … }
unsigned IRInstructionMapper::mapToLegalUnsigned(
BasicBlock::iterator &It, std::vector<unsigned> &IntegerMappingForBB,
std::vector<IRInstructionData *> &InstrListForBB) { … }
IRInstructionData *
IRInstructionMapper::allocateIRInstructionData(Instruction &I, bool Legality,
IRInstructionDataList &IDL) { … }
IRInstructionData *
IRInstructionMapper::allocateIRInstructionData(IRInstructionDataList &IDL) { … }
IRInstructionDataList *
IRInstructionMapper::allocateIRInstructionDataList() { … }
unsigned IRInstructionMapper::mapToIllegalUnsigned(
BasicBlock::iterator &It, std::vector<unsigned> &IntegerMappingForBB,
std::vector<IRInstructionData *> &InstrListForBB, bool End) { … }
IRSimilarityCandidate::IRSimilarityCandidate(unsigned StartIdx, unsigned Len,
IRInstructionData *FirstInstIt,
IRInstructionData *LastInstIt)
: … { … }
bool IRSimilarityCandidate::isSimilar(const IRSimilarityCandidate &A,
const IRSimilarityCandidate &B) { … }
static bool checkNumberingAndReplaceCommutative(
const DenseMap<Value *, unsigned> &SourceValueToNumberMapping,
DenseMap<unsigned, DenseSet<unsigned>> &CurrentSrcTgtNumberMapping,
ArrayRef<Value *> &SourceOperands,
DenseSet<unsigned> &TargetValueNumbers){ … }
bool checkNumberingAndReplace(
DenseMap<unsigned, DenseSet<unsigned>> &CurrentSrcTgtNumberMapping,
unsigned SourceArgVal, unsigned TargetArgVal) { … }
bool IRSimilarityCandidate::compareNonCommutativeOperandMapping(
OperandMapping A, OperandMapping B) { … }
bool IRSimilarityCandidate::compareCommutativeOperandMapping(
OperandMapping A, OperandMapping B) { … }
bool IRSimilarityCandidate::compareAssignmentMapping(
const unsigned InstValA, const unsigned &InstValB,
DenseMap<unsigned, DenseSet<unsigned>> &ValueNumberMappingA,
DenseMap<unsigned, DenseSet<unsigned>> &ValueNumberMappingB) { … }
bool IRSimilarityCandidate::checkRelativeLocations(RelativeLocMapping A,
RelativeLocMapping B) { … }
bool IRSimilarityCandidate::compareStructure(const IRSimilarityCandidate &A,
const IRSimilarityCandidate &B) { … }
ZippedRelativeLocationsT;
bool IRSimilarityCandidate::compareStructure(
const IRSimilarityCandidate &A, const IRSimilarityCandidate &B,
DenseMap<unsigned, DenseSet<unsigned>> &ValueNumberMappingA,
DenseMap<unsigned, DenseSet<unsigned>> &ValueNumberMappingB) { … }
bool IRSimilarityCandidate::overlap(const IRSimilarityCandidate &A,
const IRSimilarityCandidate &B) { … }
void IRSimilarityIdentifier::populateMapper(
Module &M, std::vector<IRInstructionData *> &InstrList,
std::vector<unsigned> &IntegerMapping) { … }
void IRSimilarityIdentifier::populateMapper(
ArrayRef<std::unique_ptr<Module>> &Modules,
std::vector<IRInstructionData *> &InstrList,
std::vector<unsigned> &IntegerMapping) { … }
static void createCandidatesFromSuffixTree(
const IRInstructionMapper& Mapper, std::vector<IRInstructionData *> &InstrList,
std::vector<unsigned> &IntegerMapping, SuffixTree::RepeatedSubstring &RS,
std::vector<IRSimilarityCandidate> &CandsForRepSubstring) { … }
void IRSimilarityCandidate::createCanonicalRelationFrom(
IRSimilarityCandidate &SourceCand,
DenseMap<unsigned, DenseSet<unsigned>> &ToSourceMapping,
DenseMap<unsigned, DenseSet<unsigned>> &FromSourceMapping) { … }
void IRSimilarityCandidate::createCanonicalRelationFrom(
IRSimilarityCandidate &SourceCand, IRSimilarityCandidate &SourceCandLarge,
IRSimilarityCandidate &TargetCandLarge) { … }
void IRSimilarityCandidate::createCanonicalMappingFor(
IRSimilarityCandidate &CurrCand) { … }
static std::optional<
std::pair<IRSimilarityCandidate *, IRSimilarityCandidate *>>
CheckLargerCands(
IRSimilarityCandidate &CandA, IRSimilarityCandidate &CandB,
DenseMap<unsigned, DenseSet<IRSimilarityCandidate *>> &IndexToIncludedCand,
DenseMap<IRSimilarityCandidate *, unsigned> &CandToGroup) { … }
static void findCandidateStructures(
std::vector<IRSimilarityCandidate> &CandsForRepSubstring,
DenseMap<unsigned, SimilarityGroup> &StructuralGroups,
DenseMap<unsigned, DenseSet<IRSimilarityCandidate *>> &IndexToIncludedCand,
DenseMap<IRSimilarityCandidate *, unsigned> &CandToOverallGroup
) { … }
void IRSimilarityIdentifier::findCandidates(
std::vector<IRInstructionData *> &InstrList,
std::vector<unsigned> &IntegerMapping) { … }
SimilarityGroupList &IRSimilarityIdentifier::findSimilarity(
ArrayRef<std::unique_ptr<Module>> Modules) { … }
SimilarityGroupList &IRSimilarityIdentifier::findSimilarity(Module &M) { … }
INITIALIZE_PASS(…)
IRSimilarityIdentifierWrapperPass::IRSimilarityIdentifierWrapperPass()
: … { … }
bool IRSimilarityIdentifierWrapperPass::doInitialization(Module &M) { … }
bool IRSimilarityIdentifierWrapperPass::doFinalization(Module &M) { … }
bool IRSimilarityIdentifierWrapperPass::runOnModule(Module &M) { … }
AnalysisKey IRSimilarityAnalysis::Key;
IRSimilarityIdentifier IRSimilarityAnalysis::run(Module &M,
ModuleAnalysisManager &) { … }
PreservedAnalyses
IRSimilarityAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) { … }
char IRSimilarityIdentifierWrapperPass::ID = …;