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) {
83 // HACK: VS 2010 has a bug in the stdlib that requires this.
84 Diff.push_back(DiffRecord(L, DiffRecord::second_type(0)));
86 void addRight(Instruction *R) {
87 // HACK: VS 2010 has a bug in the stdlib that requires this.
88 Diff.push_back(DiffRecord(DiffRecord::first_type(0), R));
91 unsigned getNumLines() const { return Diff.size(); }
92 DiffChange getLineKind(unsigned I) const {
93 return (Diff[I].first ? (Diff[I].second ? DC_match : DC_left)
96 Instruction *getLeft(unsigned I) const { return Diff[I].first; }
97 Instruction *getRight(unsigned I) const { return Diff[I].second; }
100 /// The interface for consumers of difference data.
102 /// Record that a local context has been entered. Left and
103 /// Right are IR "containers" of some sort which are being
104 /// considered for structural equivalence: global variables,
105 /// functions, blocks, instructions, etc.
106 virtual void enterContext(Value *Left, Value *Right) = 0;
108 /// Record that a local context has been exited.
109 virtual void exitContext() = 0;
111 /// Record a difference within the current context.
112 virtual void log(StringRef Text) = 0;
114 /// Record a formatted difference within the current context.
115 virtual void logf(const LogBuilder &Log) = 0;
117 /// Record a line-by-line instruction diff.
118 virtual void logd(const DiffLogBuilder &Log) = 0;
121 virtual ~Consumer() {}
124 /// A RAII object for recording the current context.
126 Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) {
127 Engine.consumer.enterContext(L, R);
131 Engine.consumer.exitContext();
135 DifferenceEngine &Engine;
138 /// An oracle for answering whether two values are equivalent as
141 virtual bool operator()(Value *L, Value *R) = 0;
147 DifferenceEngine(LLVMContext &context, Consumer &consumer)
148 : context(context), consumer(consumer), globalValueOracle(0) {}
150 void diff(Module *L, Module *R);
151 void diff(Function *L, Function *R);
153 void log(StringRef text) {
157 LogBuilder logf(StringRef text) {
158 return LogBuilder(*this, text);
161 /// Installs an oracle to decide whether two global values are
162 /// equivalent as operands. Without an oracle, global values are
163 /// considered equivalent as operands precisely when they have the
165 void setGlobalValueOracle(Oracle *oracle) {
166 globalValueOracle = oracle;
169 /// Determines whether two global values are equivalent.
170 bool equivalentAsOperands(GlobalValue *L, GlobalValue *R);
173 LLVMContext &context;
175 Oracle *globalValueOracle;