1 //===-- DifferenceEngine.h - Module comparator ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines the interface to the LLVM difference engine,
11 // which structurally compares functions within a module.
13 //===----------------------------------------------------------------------===//
15 #ifndef _LLVM_DIFFERENCE_ENGINE_H_
16 #define _LLVM_DIFFERENCE_ENGINE_H_
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
32 /// A class for performing structural comparisons of LLVM assembly.
33 class DifferenceEngine {
35 /// A temporary-object class for building up log messages.
37 DifferenceEngine &Engine;
39 /// The use of a stored StringRef here is okay because
40 /// LogBuilder should be used only as a temporary, and as a
41 /// temporary it will be destructed before whatever temporary
42 /// might be initializing this format.
45 SmallVector<Value*, 4> Arguments;
48 LogBuilder(DifferenceEngine &Engine, StringRef Format)
49 : Engine(Engine), Format(Format) {}
51 LogBuilder &operator<<(Value *V) {
52 Arguments.push_back(V);
57 Engine.consumer.logf(*this);
60 StringRef getFormat() const { return Format; }
62 unsigned getNumArguments() const { return Arguments.size(); }
63 Value *getArgument(unsigned I) const { return Arguments[I]; }
66 enum DiffChange { DC_match, DC_left, DC_right };
68 /// A temporary-object class for building up diff messages.
69 class DiffLogBuilder {
70 typedef std::pair<Instruction*,Instruction*> DiffRecord;
71 SmallVector<DiffRecord, 20> Diff;
73 DifferenceEngine &Engine;
76 DiffLogBuilder(DifferenceEngine &Engine) : Engine(Engine) {}
77 ~DiffLogBuilder() { Engine.consumer.logd(*this); }
79 void addMatch(Instruction *L, Instruction *R) {
80 Diff.push_back(DiffRecord(L, R));
82 void addLeft(Instruction *L) { Diff.push_back(DiffRecord(L, 0)); }
83 void addRight(Instruction *R) { Diff.push_back(DiffRecord(0, R)); }
85 unsigned getNumLines() const { return Diff.size(); }
86 DiffChange getLineKind(unsigned I) const {
87 return (Diff[I].first ? (Diff[I].second ? DC_match : DC_left)
90 Instruction *getLeft(unsigned I) const { return Diff[I].first; }
91 Instruction *getRight(unsigned I) const { return Diff[I].second; }
94 /// The interface for consumers of difference data.
96 /// Record that a local context has been entered. Left and
97 /// Right are IR "containers" of some sort which are being
98 /// considered for structural equivalence: global variables,
99 /// functions, blocks, instructions, etc.
100 virtual void enterContext(Value *Left, Value *Right) = 0;
102 /// Record that a local context has been exited.
103 virtual void exitContext() = 0;
105 /// Record a difference within the current context.
106 virtual void log(StringRef Text) = 0;
108 /// Record a formatted difference within the current context.
109 virtual void logf(const LogBuilder &Log) = 0;
111 /// Record a line-by-line instruction diff.
112 virtual void logd(const DiffLogBuilder &Log) = 0;
118 /// A RAII object for recording the current context.
120 Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) {
121 Engine.consumer.enterContext(L, R);
125 Engine.consumer.exitContext();
129 DifferenceEngine &Engine;
132 /// An oracle for answering whether two values are equivalent as
135 virtual bool operator()(Value *L, Value *R) = 0;
141 DifferenceEngine(LLVMContext &context, Consumer &consumer)
142 : context(context), consumer(consumer), globalValueOracle(0) {}
144 void diff(Module *L, Module *R);
145 void diff(Function *L, Function *R);
147 void log(StringRef text) {
151 LogBuilder logf(StringRef text) {
152 return LogBuilder(*this, text);
155 /// Installs an oracle to decide whether two global values are
156 /// equivalent as operands. Without an oracle, global values are
157 /// considered equivalent as operands precisely when they have the
159 void setGlobalValueOracle(Oracle *oracle) {
160 globalValueOracle = oracle;
163 /// Determines whether two global values are equivalent.
164 bool equivalentAsOperands(GlobalValue *L, GlobalValue *R);
167 LLVMContext &context;
169 Oracle *globalValueOracle;