2
0

DifferenceEngine.h 2.5 KB

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