blob: 7f084a377f0c7a1f4d127e19b20b73d5064d6bc5 [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
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000015#ifndef LLVM_TOOLS_LLVM_DIFF_DIFFERENCEENGINE_H
16#define LLVM_TOOLS_LLVM_DIFF_DIFFERENCEENGINE_H
John McCall3dd706b2010-07-29 07:53:27 +000017
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/StringRef.h"
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:
John McCall3dd706b2010-07-29 07:53:27 +000035 /// A RAII object for recording the current context.
36 struct Context {
37 Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) {
38 Engine.consumer.enterContext(L, R);
39 }
40
41 ~Context() {
42 Engine.consumer.exitContext();
43 }
44
45 private:
46 DifferenceEngine &Engine;
47 };
48
49 /// An oracle for answering whether two values are equivalent as
50 /// operands.
David Blaikie2d24e2a2011-12-20 02:50:00 +000051 class Oracle {
52 virtual void anchor();
53 public:
John McCall3dd706b2010-07-29 07:53:27 +000054 virtual bool operator()(Value *L, Value *R) = 0;
55
56 protected:
Bill Wendlingac530612010-09-03 18:41:20 +000057 virtual ~Oracle() {}
John McCall3dd706b2010-07-29 07:53:27 +000058 };
59
Benjamin Kramera7542d52012-06-06 18:25:08 +000060 DifferenceEngine(Consumer &consumer)
Craig Topperc34a25d2014-04-28 04:05:08 +000061 : consumer(consumer), globalValueOracle(nullptr) {}
John McCall3dd706b2010-07-29 07:53:27 +000062
63 void diff(Module *L, Module *R);
64 void diff(Function *L, Function *R);
John McCall3dd706b2010-07-29 07:53:27 +000065 void log(StringRef text) {
66 consumer.log(text);
67 }
John McCall3dd706b2010-07-29 07:53:27 +000068 LogBuilder logf(StringRef text) {
Renato Golin7d4fc4f2011-03-14 22:22:46 +000069 return LogBuilder(consumer, text);
John McCall3dd706b2010-07-29 07:53:27 +000070 }
Renato Golin7d4fc4f2011-03-14 22:22:46 +000071 Consumer& getConsumer() const { return consumer; }
John McCall3dd706b2010-07-29 07:53:27 +000072
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:
John McCall3dd706b2010-07-29 07:53:27 +000085 Consumer &consumer;
86 Oracle *globalValueOracle;
87 };
88}
89
90#endif