DifferenceEngine.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===-- DifferenceEngine.h - Module comparator ------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This header defines the interface to the LLVM difference engine,
  10. // which structurally compares functions within a module.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TOOLS_LLVM_DIFF_DIFFERENCEENGINE_H
  14. #define LLVM_TOOLS_LLVM_DIFF_DIFFERENCEENGINE_H
  15. #include "DiffConsumer.h"
  16. #include "DiffLog.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include <utility>
  19. namespace llvm {
  20. class Function;
  21. class GlobalValue;
  22. class Instruction;
  23. class LLVMContext;
  24. class Module;
  25. class Twine;
  26. class Value;
  27. /// A class for performing structural comparisons of LLVM assembly.
  28. class DifferenceEngine {
  29. public:
  30. /// A RAII object for recording the current context.
  31. struct Context {
  32. Context(DifferenceEngine &Engine, const Value *L, const Value *R)
  33. : Engine(Engine) {
  34. Engine.consumer.enterContext(L, R);
  35. }
  36. ~Context() {
  37. Engine.consumer.exitContext();
  38. }
  39. private:
  40. DifferenceEngine &Engine;
  41. };
  42. /// An oracle for answering whether two values are equivalent as
  43. /// operands.
  44. class Oracle {
  45. virtual void anchor();
  46. public:
  47. virtual bool operator()(const Value *L, const Value *R) = 0;
  48. protected:
  49. virtual ~Oracle() {}
  50. };
  51. DifferenceEngine(Consumer &consumer)
  52. : consumer(consumer), globalValueOracle(nullptr) {}
  53. void diff(const Module *L, const Module *R);
  54. void diff(const Function *L, const Function *R);
  55. void log(StringRef text) {
  56. consumer.log(text);
  57. }
  58. LogBuilder logf(StringRef text) {
  59. return LogBuilder(consumer, text);
  60. }
  61. Consumer& getConsumer() const { return consumer; }
  62. /// Installs an oracle to decide whether two global values are
  63. /// equivalent as operands. Without an oracle, global values are
  64. /// considered equivalent as operands precisely when they have the
  65. /// same name.
  66. void setGlobalValueOracle(Oracle *oracle) {
  67. globalValueOracle = oracle;
  68. }
  69. /// Determines whether two global values are equivalent.
  70. bool equivalentAsOperands(const GlobalValue *L, const GlobalValue *R);
  71. private:
  72. Consumer &consumer;
  73. Oracle *globalValueOracle;
  74. };
  75. }
  76. #endif