DifferenceEngine.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, Value *L, Value *R) : Engine(Engine) {
  33. Engine.consumer.enterContext(L, R);
  34. }
  35. ~Context() {
  36. Engine.consumer.exitContext();
  37. }
  38. private:
  39. DifferenceEngine &Engine;
  40. };
  41. /// An oracle for answering whether two values are equivalent as
  42. /// operands.
  43. class Oracle {
  44. virtual void anchor();
  45. public:
  46. virtual bool operator()(Value *L, Value *R) = 0;
  47. protected:
  48. virtual ~Oracle() {}
  49. };
  50. DifferenceEngine(Consumer &consumer)
  51. : consumer(consumer), globalValueOracle(nullptr) {}
  52. void diff(Module *L, Module *R);
  53. void diff(Function *L, Function *R);
  54. void log(StringRef text) {
  55. consumer.log(text);
  56. }
  57. LogBuilder logf(StringRef text) {
  58. return LogBuilder(consumer, text);
  59. }
  60. Consumer& getConsumer() const { return consumer; }
  61. /// Installs an oracle to decide whether two global values are
  62. /// equivalent as operands. Without an oracle, global values are
  63. /// considered equivalent as operands precisely when they have the
  64. /// same name.
  65. void setGlobalValueOracle(Oracle *oracle) {
  66. globalValueOracle = oracle;
  67. }
  68. /// Determines whether two global values are equivalent.
  69. bool equivalentAsOperands(GlobalValue *L, GlobalValue *R);
  70. private:
  71. Consumer &consumer;
  72. Oracle *globalValueOracle;
  73. };
  74. }
  75. #endif