John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 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 | |
| 15 | #ifndef _LLVM_DIFFERENCE_ENGINE_H_ |
| 16 | #define _LLVM_DIFFERENCE_ENGINE_H_ |
| 17 | |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 18 | #include "DiffConsumer.h" |
| 19 | #include "DiffLog.h" |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 22 | #include <utility> |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
John McCall | 73b21b7 | 2010-07-29 18:08:23 +0000 | [diff] [blame] | 25 | class Function; |
| 26 | class GlobalValue; |
| 27 | class Instruction; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 28 | class LLVMContext; |
| 29 | class Module; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 30 | class Twine; |
| 31 | class Value; |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 32 | |
| 33 | /// A class for performing structural comparisons of LLVM assembly. |
| 34 | class DifferenceEngine { |
| 35 | public: |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 36 | /// A RAII object for recording the current context. |
| 37 | struct Context { |
| 38 | Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) { |
| 39 | Engine.consumer.enterContext(L, R); |
| 40 | } |
| 41 | |
| 42 | ~Context() { |
| 43 | Engine.consumer.exitContext(); |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | DifferenceEngine &Engine; |
| 48 | }; |
| 49 | |
| 50 | /// An oracle for answering whether two values are equivalent as |
| 51 | /// operands. |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 52 | class Oracle { |
| 53 | virtual void anchor(); |
| 54 | public: |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 55 | virtual bool operator()(Value *L, Value *R) = 0; |
| 56 | |
| 57 | protected: |
Bill Wendling | ac53061 | 2010-09-03 18:41:20 +0000 | [diff] [blame] | 58 | virtual ~Oracle() {} |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Benjamin Kramer | a7542d5 | 2012-06-06 18:25:08 +0000 | [diff] [blame] | 61 | DifferenceEngine(Consumer &consumer) |
| 62 | : consumer(consumer), globalValueOracle(0) {} |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 63 | |
| 64 | void diff(Module *L, Module *R); |
| 65 | void diff(Function *L, Function *R); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 66 | void log(StringRef text) { |
| 67 | consumer.log(text); |
| 68 | } |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 69 | LogBuilder logf(StringRef text) { |
Renato Golin | 7d4fc4f | 2011-03-14 22:22:46 +0000 | [diff] [blame] | 70 | return LogBuilder(consumer, text); |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 71 | } |
Renato Golin | 7d4fc4f | 2011-03-14 22:22:46 +0000 | [diff] [blame] | 72 | Consumer& getConsumer() const { return consumer; } |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 73 | |
| 74 | /// Installs an oracle to decide whether two global values are |
| 75 | /// equivalent as operands. Without an oracle, global values are |
| 76 | /// considered equivalent as operands precisely when they have the |
| 77 | /// same name. |
| 78 | void setGlobalValueOracle(Oracle *oracle) { |
| 79 | globalValueOracle = oracle; |
| 80 | } |
| 81 | |
| 82 | /// Determines whether two global values are equivalent. |
| 83 | bool equivalentAsOperands(GlobalValue *L, GlobalValue *R); |
| 84 | |
| 85 | private: |
John McCall | 3dd706b | 2010-07-29 07:53:27 +0000 | [diff] [blame] | 86 | Consumer &consumer; |
| 87 | Oracle *globalValueOracle; |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | #endif |