blob: 9d0ebecc53d152fff629b8745b82d1b95084bd8c [file] [log] [blame]
John McCall3dd706b2010-07-29 07:53:27 +00001//===-- 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
18#include <utility>
19#include <llvm/ADT/SmallVector.h>
20#include <llvm/ADT/StringRef.h>
21
22namespace llvm {
23 class LLVMContext;
24 class Module;
25 class Function;
26 class Twine;
27 class Value;
28 class GlobalValue;
29
30 /// A class for performing structural comparisons of LLVM assembly.
31 class DifferenceEngine {
32 public:
33 /// A temporary-object class for building up log messages.
34 class LogBuilder {
35 DifferenceEngine &Engine;
36
37 /// The use of a stored StringRef here is okay because
38 /// LogBuilder should be used only as a temporary, and as a
39 /// temporary it will be destructed before whatever temporary
40 /// might be initializing this format.
41 StringRef Format;
42
43 SmallVector<Value*, 4> Arguments;
44
45 public:
46 LogBuilder(DifferenceEngine &Engine, StringRef Format)
47 : Engine(Engine), Format(Format) {}
48
49 LogBuilder &operator<<(Value *V) {
50 Arguments.push_back(V);
51 return *this;
52 }
53
54 ~LogBuilder() {
55 Engine.consumer.logf(*this);
56 }
57
58 StringRef getFormat() const { return Format; }
59
60 unsigned getNumArguments() const { return Arguments.size(); }
61 Value *getArgument(unsigned I) const { return Arguments[I]; }
62 };
63
64 enum DiffChange { DC_match, DC_left, DC_right };
65
66 /// A temporary-object class for building up diff messages.
67 class DiffLogBuilder {
68 typedef std::pair<Instruction*,Instruction*> DiffRecord;
69 SmallVector<DiffRecord, 20> Diff;
70
71 DifferenceEngine &Engine;
72
73 public:
74 DiffLogBuilder(DifferenceEngine &Engine) : Engine(Engine) {}
75 ~DiffLogBuilder() { Engine.consumer.logd(*this); }
76
77 void addMatch(Instruction *L, Instruction *R) {
78 Diff.push_back(DiffRecord(L, R));
79 }
80 void addLeft(Instruction *L) { Diff.push_back(DiffRecord(L, 0)); }
81 void addRight(Instruction *R) { Diff.push_back(DiffRecord(0, R)); }
82
83 unsigned getNumLines() const { return Diff.size(); }
84 DiffChange getLineKind(unsigned I) const {
85 return (Diff[I].first ? (Diff[I].second ? DC_match : DC_left)
86 : DC_right);
87 }
88 Instruction *getLeft(unsigned I) const { return Diff[I].first; }
89 Instruction *getRight(unsigned I) const { return Diff[I].second; }
90 };
91
92 /// The interface for consumers of difference data.
93 struct Consumer {
94 /// Record that a local context has been entered. Left and
95 /// Right are IR "containers" of some sort which are being
96 /// considered for structural equivalence: global variables,
97 /// functions, blocks, instructions, etc.
98 virtual void enterContext(Value *Left, Value *Right) = 0;
99
100 /// Record that a local context has been exited.
101 virtual void exitContext() = 0;
102
103 /// Record a difference within the current context.
104 virtual void log(StringRef Text) = 0;
105
106 /// Record a formatted difference within the current context.
107 virtual void logf(const LogBuilder &Log) = 0;
108
109 /// Record a line-by-line instruction diff.
110 virtual void logd(const DiffLogBuilder &Log) = 0;
111
112 protected:
113 ~Consumer() {}
114 };
115
116 /// A RAII object for recording the current context.
117 struct Context {
118 Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) {
119 Engine.consumer.enterContext(L, R);
120 }
121
122 ~Context() {
123 Engine.consumer.exitContext();
124 }
125
126 private:
127 DifferenceEngine &Engine;
128 };
129
130 /// An oracle for answering whether two values are equivalent as
131 /// operands.
132 struct Oracle {
133 virtual bool operator()(Value *L, Value *R) = 0;
134
135 protected:
136 ~Oracle() {}
137 };
138
139 DifferenceEngine(LLVMContext &context, Consumer &consumer)
140 : context(context), consumer(consumer), globalValueOracle(0) {}
141
142 void diff(Module *L, Module *R);
143 void diff(Function *L, Function *R);
144
145 void log(StringRef text) {
146 consumer.log(text);
147 }
148
149 LogBuilder logf(StringRef text) {
150 return LogBuilder(*this, text);
151 }
152
153 /// Installs an oracle to decide whether two global values are
154 /// equivalent as operands. Without an oracle, global values are
155 /// considered equivalent as operands precisely when they have the
156 /// same name.
157 void setGlobalValueOracle(Oracle *oracle) {
158 globalValueOracle = oracle;
159 }
160
161 /// Determines whether two global values are equivalent.
162 bool equivalentAsOperands(GlobalValue *L, GlobalValue *R);
163
164 private:
165 LLVMContext &context;
166 Consumer &consumer;
167 Oracle *globalValueOracle;
168 };
169}
170
171#endif