blob: 73bf6eb6ea5b972c3361de1e365942c58ef1e993 [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
Chandler Carruthf010c462012-12-04 10:44:52 +000018#include "DiffConsumer.h"
19#include "DiffLog.h"
John McCall73b21b72010-07-29 18:08:23 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringRef.h"
John McCall3dd706b2010-07-29 07:53:27 +000022#include <utility>
John McCall3dd706b2010-07-29 07:53:27 +000023
24namespace llvm {
John McCall73b21b72010-07-29 18:08:23 +000025 class Function;
26 class GlobalValue;
27 class Instruction;
John McCall3dd706b2010-07-29 07:53:27 +000028 class LLVMContext;
29 class Module;
John McCall3dd706b2010-07-29 07:53:27 +000030 class Twine;
31 class Value;
John McCall3dd706b2010-07-29 07:53:27 +000032
33 /// A class for performing structural comparisons of LLVM assembly.
34 class DifferenceEngine {
35 public:
John McCall3dd706b2010-07-29 07:53:27 +000036 /// A RAII object for recording the current context.
37 struct Context {
38 Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) {
39 Engine.consumer.enterContext(L, R);
40 }
41
42 ~Context() {
43 Engine.consumer.exitContext();
44 }
45
46 private:
47 DifferenceEngine &Engine;
48 };
49
50 /// An oracle for answering whether two values are equivalent as
51 /// operands.
David Blaikie2d24e2a2011-12-20 02:50:00 +000052 class Oracle {
53 virtual void anchor();
54 public:
John McCall3dd706b2010-07-29 07:53:27 +000055 virtual bool operator()(Value *L, Value *R) = 0;
56
57 protected:
Bill Wendlingac530612010-09-03 18:41:20 +000058 virtual ~Oracle() {}
John McCall3dd706b2010-07-29 07:53:27 +000059 };
60
Benjamin Kramera7542d52012-06-06 18:25:08 +000061 DifferenceEngine(Consumer &consumer)
62 : consumer(consumer), globalValueOracle(0) {}
John McCall3dd706b2010-07-29 07:53:27 +000063
64 void diff(Module *L, Module *R);
65 void diff(Function *L, Function *R);
John McCall3dd706b2010-07-29 07:53:27 +000066 void log(StringRef text) {
67 consumer.log(text);
68 }
John McCall3dd706b2010-07-29 07:53:27 +000069 LogBuilder logf(StringRef text) {
Renato Golin7d4fc4f2011-03-14 22:22:46 +000070 return LogBuilder(consumer, text);
John McCall3dd706b2010-07-29 07:53:27 +000071 }
Renato Golin7d4fc4f2011-03-14 22:22:46 +000072 Consumer& getConsumer() const { return consumer; }
John McCall3dd706b2010-07-29 07:53:27 +000073
74 /// Installs an oracle to decide whether two global values are
75 /// equivalent as operands. Without an oracle, global values are
76 /// considered equivalent as operands precisely when they have the
77 /// same name.
78 void setGlobalValueOracle(Oracle *oracle) {
79 globalValueOracle = oracle;
80 }
81
82 /// Determines whether two global values are equivalent.
83 bool equivalentAsOperands(GlobalValue *L, GlobalValue *R);
84
85 private:
John McCall3dd706b2010-07-29 07:53:27 +000086 Consumer &consumer;
87 Oracle *globalValueOracle;
88 };
89}
90
91#endif