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#include "DiffLog.h" 21#include "DiffConsumer.h" |
22 23#include <utility> 24 25namespace llvm { 26 class Function; 27 class GlobalValue; 28 class Instruction; 29 class LLVMContext; 30 class Module; 31 class Twine; 32 class Value; 33 34 /// A class for performing structural comparisons of LLVM assembly. 35 class DifferenceEngine { 36 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 virtual ~Consumer() {}
122 };
123
|
37 /// A RAII object for recording the current context. 38 struct Context { 39 Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) { 40 Engine.consumer.enterContext(L, R); 41 } 42 43 ~Context() { 44 Engine.consumer.exitContext(); 45 } 46 47 private: 48 DifferenceEngine &Engine; 49 }; 50 51 /// An oracle for answering whether two values are equivalent as 52 /// operands. 53 struct Oracle { 54 virtual bool operator()(Value *L, Value *R) = 0; 55 56 protected: 57 virtual ~Oracle() {} 58 }; 59 60 DifferenceEngine(LLVMContext &context, Consumer &consumer) 61 : context(context), consumer(consumer), globalValueOracle(0) {} 62 63 void diff(Module *L, Module *R); 64 void diff(Function *L, Function *R);
|
152
|
65 void log(StringRef text) { 66 consumer.log(text); 67 }
|
156
|
68 LogBuilder logf(StringRef text) {
|
158 return LogBuilder(*this, text);
|
69 return LogBuilder(consumer, text); |
70 }
|
71 Consumer& getConsumer() const { return consumer; } |
72 73 /// Installs an oracle to decide whether two global values are 74 /// equivalent as operands. Without an oracle, global values are 75 /// considered equivalent as operands precisely when they have the 76 /// same name. 77 void setGlobalValueOracle(Oracle *oracle) { 78 globalValueOracle = oracle; 79 } 80 81 /// Determines whether two global values are equivalent. 82 bool equivalentAsOperands(GlobalValue *L, GlobalValue *R); 83 84 private: 85 LLVMContext &context; 86 Consumer &consumer; 87 Oracle *globalValueOracle; 88 }; 89} 90 91#endif
|