//===-- llvm-sim.cpp - Find similar sections of programs -------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This program finds similar sections of a Module, and exports them as a JSON // file. // // To find similarities contained across multiple modules, please use llvm-link // first to merge the modules. // //===----------------------------------------------------------------------===// #include "llvm/Analysis/IRSimilarityIdentifier.h" #include "llvm/IRReader/IRReader.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/InitLLVM.h" #include "llvm/Support/JSON.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/ToolOutputFile.h" usingnamespacellvm; usingnamespaceIRSimilarity; static cl::opt<std::string> OutputFilename("o", cl::desc("Output Filename"), cl::init("-"), cl::value_desc("filename")); static cl::opt<std::string> InputSourceFile(cl::Positional, cl::desc("<Source file>"), cl::init("-"), cl::value_desc("filename")); /// Retrieve the unique number \p I was mapped to in parseBitcodeFile. /// /// \param I - The Instruction to find the instruction number for. /// \param LLVMInstNum - The mapping of Instructions to their location in the /// module represented by an unsigned integer. /// \returns The instruction number for \p I if it exists. std::optional<unsigned> getPositionInModule(const Instruction *I, const DenseMap<Instruction *, unsigned> &LLVMInstNum) { … } /// Exports the given SimilarityGroups to a JSON file at \p FilePath. /// /// \param FilePath - The path to the output location. /// \param SimSections - The similarity groups to process. /// \param LLVMInstNum - The mapping of Instructions to their location in the /// module represented by an unsigned integer. /// \returns A nonzero error code if there was a failure creating the file. std::error_code exportToFile(const StringRef FilePath, const SimilarityGroupList &SimSections, const DenseMap<Instruction *, unsigned> &LLVMInstNum) { … } int main(int argc, const char *argv[]) { … }