Fix the msvs 2010 build.
[oota-llvm.git] / tools / llvm-diff / DifferenceEngine.h
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
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20
21 #include <utility>
22
23 namespace llvm {
24   class Function;
25   class GlobalValue;
26   class Instruction;
27   class LLVMContext;
28   class Module;
29   class Twine;
30   class Value;
31
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) {
83         // HACK: VS 2010 has a bug in the stdlib that requires this.
84         Diff.push_back(DiffRecord(L, DiffRecord::second_type(0)));
85       }
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));
89       }
90
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)
94                               : DC_right);
95       }
96       Instruction *getLeft(unsigned I) const { return Diff[I].first; }
97       Instruction *getRight(unsigned I) const { return Diff[I].second; }
98     };
99
100     /// The interface for consumers of difference data.
101     struct Consumer {
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;
107
108       /// Record that a local context has been exited.
109       virtual void exitContext() = 0;
110
111       /// Record a difference within the current context.
112       virtual void log(StringRef Text) = 0;
113
114       /// Record a formatted difference within the current context.
115       virtual void logf(const LogBuilder &Log) = 0;
116
117       /// Record a line-by-line instruction diff.
118       virtual void logd(const DiffLogBuilder &Log) = 0;
119
120     protected:
121       ~Consumer() {}
122     };
123
124     /// A RAII object for recording the current context.
125     struct Context {
126       Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) {
127         Engine.consumer.enterContext(L, R);
128       }
129
130       ~Context() {
131         Engine.consumer.exitContext();
132       }
133
134     private:
135       DifferenceEngine &Engine;
136     };
137
138     /// An oracle for answering whether two values are equivalent as
139     /// operands.
140     struct Oracle {
141       virtual bool operator()(Value *L, Value *R) = 0;
142
143     protected:
144       ~Oracle() {}
145     };
146
147     DifferenceEngine(LLVMContext &context, Consumer &consumer)
148       : context(context), consumer(consumer), globalValueOracle(0) {}
149
150     void diff(Module *L, Module *R);
151     void diff(Function *L, Function *R);
152
153     void log(StringRef text) {
154       consumer.log(text);
155     }
156
157     LogBuilder logf(StringRef text) {
158       return LogBuilder(*this, text);
159     }
160
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
164     /// same name.
165     void setGlobalValueOracle(Oracle *oracle) {
166       globalValueOracle = oracle;
167     }
168
169     /// Determines whether two global values are equivalent.
170     bool equivalentAsOperands(GlobalValue *L, GlobalValue *R);
171
172   private:
173     LLVMContext &context;
174     Consumer &consumer;
175     Oracle *globalValueOracle;
176   };
177 }
178
179 #endif