blob: bc3313173449e5cc9602ae8ef21fa28b5464ac6f [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
John McCall73b21b72010-07-29 18:08:23 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringRef.h"
20
John McCall3dd706b2010-07-29 07:53:27 +000021#include <utility>
John McCall3dd706b2010-07-29 07:53:27 +000022
23namespace llvm {
John McCall73b21b72010-07-29 18:08:23 +000024 class Function;
25 class GlobalValue;
26 class Instruction;
John McCall3dd706b2010-07-29 07:53:27 +000027 class LLVMContext;
28 class Module;
John McCall3dd706b2010-07-29 07:53:27 +000029 class Twine;
30 class Value;
John McCall3dd706b2010-07-29 07:53:27 +000031
32 /// A class for performing structural comparisons of LLVM assembly.
33 class DifferenceEngine {
34 public:
35 /// A temporary-object class for building up log messages.
36 class LogBuilder {
37 DifferenceEngine &Engine;
38
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.
43 StringRef Format;
44
45 SmallVector<Value*, 4> Arguments;
46
47 public:
48 LogBuilder(DifferenceEngine &Engine, StringRef Format)
49 : Engine(Engine), Format(Format) {}
50
51 LogBuilder &operator<<(Value *V) {
52 Arguments.push_back(V);
53 return *this;
54 }
55
56 ~LogBuilder() {
57 Engine.consumer.logf(*this);
58 }
59
60 StringRef getFormat() const { return Format; }
61
62 unsigned getNumArguments() const { return Arguments.size(); }
63 Value *getArgument(unsigned I) const { return Arguments[I]; }
64 };
65
66 enum DiffChange { DC_match, DC_left, DC_right };
67
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;
72
73 DifferenceEngine &Engine;
74
75 public:
76 DiffLogBuilder(DifferenceEngine &Engine) : Engine(Engine) {}
77 ~DiffLogBuilder() { Engine.consumer.logd(*this); }
78
79 void addMatch(Instruction *L, Instruction *R) {
80 Diff.push_back(DiffRecord(L, R));
81 }
82 void addLeft(Instruction *L) { Diff.push_back(DiffRecord(L, 0)); }
83 void addRight(Instruction *R) { Diff.push_back(DiffRecord(0, R)); }
84
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)
88 : DC_right);
89 }
90 Instruction *getLeft(unsigned I) const { return Diff[I].first; }
91 Instruction *getRight(unsigned I) const { return Diff[I].second; }
92 };
93
94 /// The interface for consumers of difference data.
95 struct Consumer {
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;
101
102 /// Record that a local context has been exited.
103 virtual void exitContext() = 0;
104
105 /// Record a difference within the current context.
106 virtual void log(StringRef Text) = 0;
107
108 /// Record a formatted difference within the current context.
109 virtual void logf(const LogBuilder &Log) = 0;
110
111 /// Record a line-by-line instruction diff.
112 virtual void logd(const DiffLogBuilder &Log) = 0;
113
114 protected:
115 ~Consumer() {}
116 };
117
118 /// A RAII object for recording the current context.
119 struct Context {
120 Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) {
121 Engine.consumer.enterContext(L, R);
122 }
123
124 ~Context() {
125 Engine.consumer.exitContext();
126 }
127
128 private:
129 DifferenceEngine &Engine;
130 };
131
132 /// An oracle for answering whether two values are equivalent as
133 /// operands.
134 struct Oracle {
135 virtual bool operator()(Value *L, Value *R) = 0;
136
137 protected:
138 ~Oracle() {}
139 };
140
141 DifferenceEngine(LLVMContext &context, Consumer &consumer)
142 : context(context), consumer(consumer), globalValueOracle(0) {}
143
144 void diff(Module *L, Module *R);
145 void diff(Function *L, Function *R);
146
147 void log(StringRef text) {
148 consumer.log(text);
149 }
150
151 LogBuilder logf(StringRef text) {
152 return LogBuilder(*this, text);
153 }
154
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
158 /// same name.
159 void setGlobalValueOracle(Oracle *oracle) {
160 globalValueOracle = oracle;
161 }
162
163 /// Determines whether two global values are equivalent.
164 bool equivalentAsOperands(GlobalValue *L, GlobalValue *R);
165
166 private:
167 LLVMContext &context;
168 Consumer &consumer;
169 Oracle *globalValueOracle;
170 };
171}
172
173#endif