Imported Upstream version 5.18.0.179

Former-commit-id: 67aa10e65b237e1c4537630979ee99ebe1374215
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-10-25 08:34:57 +00:00
parent d6bde52373
commit 8625704ad8
28485 changed files with 3866843 additions and 50 deletions

View File

@ -0,0 +1,15 @@
set(LLVM_LINK_COMPONENTS
Core
IRReader
Support
)
add_llvm_tool(llvm-diff
llvm-diff.cpp
DiffConsumer.cpp
DiffLog.cpp
DifferenceEngine.cpp
DEPENDS
intrinsics_gen
)

View File

@ -0,0 +1,214 @@
//===-- DiffConsumer.cpp - Difference Consumer ------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This files implements the LLVM difference Consumer
//
//===----------------------------------------------------------------------===//
#include "DiffConsumer.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Debug.h"
using namespace llvm;
static void ComputeNumbering(Function *F, DenseMap<Value*,unsigned> &Numbering){
unsigned IN = 0;
// Arguments get the first numbers.
for (Function::arg_iterator
AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI)
if (!AI->hasName())
Numbering[&*AI] = IN++;
// Walk the basic blocks in order.
for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
if (!FI->hasName())
Numbering[&*FI] = IN++;
// Walk the instructions in order.
for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
// void instructions don't get numbers.
if (!BI->hasName() && !BI->getType()->isVoidTy())
Numbering[&*BI] = IN++;
}
assert(!Numbering.empty() && "asked for numbering but numbering was no-op");
}
void Consumer::anchor() { }
void DiffConsumer::printValue(Value *V, bool isL) {
if (V->hasName()) {
out << (isa<GlobalValue>(V) ? '@' : '%') << V->getName();
return;
}
if (V->getType()->isVoidTy()) {
if (isa<StoreInst>(V)) {
out << "store to ";
printValue(cast<StoreInst>(V)->getPointerOperand(), isL);
} else if (isa<CallInst>(V)) {
out << "call to ";
printValue(cast<CallInst>(V)->getCalledValue(), isL);
} else if (isa<InvokeInst>(V)) {
out << "invoke to ";
printValue(cast<InvokeInst>(V)->getCalledValue(), isL);
} else {
out << *V;
}
return;
}
if (isa<Constant>(V)) {
out << *V;
return;
}
unsigned N = contexts.size();
while (N > 0) {
--N;
DiffContext &ctxt = contexts[N];
if (!ctxt.IsFunction) continue;
if (isL) {
if (ctxt.LNumbering.empty())
ComputeNumbering(cast<Function>(ctxt.L), ctxt.LNumbering);
out << '%' << ctxt.LNumbering[V];
return;
} else {
if (ctxt.RNumbering.empty())
ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering);
out << '%' << ctxt.RNumbering[V];
return;
}
}
out << "<anonymous>";
}
void DiffConsumer::header() {
if (contexts.empty()) return;
for (SmallVectorImpl<DiffContext>::iterator
I = contexts.begin(), E = contexts.end(); I != E; ++I) {
if (I->Differences) continue;
if (isa<Function>(I->L)) {
// Extra newline between functions.
if (Differences) out << "\n";
Function *L = cast<Function>(I->L);
Function *R = cast<Function>(I->R);
if (L->getName() != R->getName())
out << "in function " << L->getName()
<< " / " << R->getName() << ":\n";
else
out << "in function " << L->getName() << ":\n";
} else if (isa<BasicBlock>(I->L)) {
BasicBlock *L = cast<BasicBlock>(I->L);
BasicBlock *R = cast<BasicBlock>(I->R);
if (L->hasName() && R->hasName() && L->getName() == R->getName())
out << " in block %" << L->getName() << ":\n";
else {
out << " in block ";
printValue(L, true);
out << " / ";
printValue(R, false);
out << ":\n";
}
} else if (isa<Instruction>(I->L)) {
out << " in instruction ";
printValue(I->L, true);
out << " / ";
printValue(I->R, false);
out << ":\n";
}
I->Differences = true;
}
}
void DiffConsumer::indent() {
unsigned N = Indent;
while (N--) out << ' ';
}
bool DiffConsumer::hadDifferences() const {
return Differences;
}
void DiffConsumer::enterContext(Value *L, Value *R) {
contexts.push_back(DiffContext(L, R));
Indent += 2;
}
void DiffConsumer::exitContext() {
Differences |= contexts.back().Differences;
contexts.pop_back();
Indent -= 2;
}
void DiffConsumer::log(StringRef text) {
header();
indent();
out << text << '\n';
}
void DiffConsumer::logf(const LogBuilder &Log) {
header();
indent();
unsigned arg = 0;
StringRef format = Log.getFormat();
while (true) {
size_t percent = format.find('%');
if (percent == StringRef::npos) {
out << format;
break;
}
assert(format[percent] == '%');
if (percent > 0) out << format.substr(0, percent);
switch (format[percent+1]) {
case '%': out << '%'; break;
case 'l': printValue(Log.getArgument(arg++), true); break;
case 'r': printValue(Log.getArgument(arg++), false); break;
default: llvm_unreachable("unknown format character");
}
format = format.substr(percent+2);
}
out << '\n';
}
void DiffConsumer::logd(const DiffLogBuilder &Log) {
header();
for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) {
indent();
switch (Log.getLineKind(I)) {
case DC_match:
out << " ";
Log.getLeft(I)->print(dbgs()); dbgs() << '\n';
//printValue(Log.getLeft(I), true);
break;
case DC_left:
out << "< ";
Log.getLeft(I)->print(dbgs()); dbgs() << '\n';
//printValue(Log.getLeft(I), true);
break;
case DC_right:
out << "> ";
Log.getRight(I)->print(dbgs()); dbgs() << '\n';
//printValue(Log.getRight(I), false);
break;
}
//out << "\n";
}
}

View File

@ -0,0 +1,91 @@
//===-- DiffConsumer.h - Difference Consumer --------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This header defines the interface to the LLVM difference Consumer
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLVM_DIFF_DIFFCONSUMER_H
#define LLVM_TOOLS_LLVM_DIFF_DIFFCONSUMER_H
#include "DiffLog.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm {
class StringRef;
class Module;
class Value;
class Function;
/// The interface for consumers of difference data.
class Consumer {
virtual void anchor();
public:
/// Record that a local context has been entered. Left and
/// Right are IR "containers" of some sort which are being
/// considered for structural equivalence: global variables,
/// functions, blocks, instructions, etc.
virtual void enterContext(Value *Left, Value *Right) = 0;
/// Record that a local context has been exited.
virtual void exitContext() = 0;
/// Record a difference within the current context.
virtual void log(StringRef Text) = 0;
/// Record a formatted difference within the current context.
virtual void logf(const LogBuilder &Log) = 0;
/// Record a line-by-line instruction diff.
virtual void logd(const DiffLogBuilder &Log) = 0;
protected:
virtual ~Consumer() {}
};
class DiffConsumer : public Consumer {
private:
struct DiffContext {
DiffContext(Value *L, Value *R)
: L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
Value *L;
Value *R;
bool Differences;
bool IsFunction;
DenseMap<Value*,unsigned> LNumbering;
DenseMap<Value*,unsigned> RNumbering;
};
raw_ostream &out;
SmallVector<DiffContext, 5> contexts;
bool Differences;
unsigned Indent;
void printValue(Value *V, bool isL);
void header();
void indent();
public:
DiffConsumer()
: out(errs()), Differences(false), Indent(0) {}
bool hadDifferences() const;
void enterContext(Value *L, Value *R) override;
void exitContext() override;
void log(StringRef text) override;
void logf(const LogBuilder &Log) override;
void logd(const DiffLogBuilder &Log) override;
};
}
#endif

View File

@ -0,0 +1,51 @@
//===-- DiffLog.h - Difference Log Builder and accessories ------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This header defines the interface to the LLVM difference log builder.
//
//===----------------------------------------------------------------------===//
#include "DiffLog.h"
#include "DiffConsumer.h"
#include "llvm/ADT/StringRef.h"
using namespace llvm;
LogBuilder::~LogBuilder() {
if (consumer)
consumer->logf(*this);
}
StringRef LogBuilder::getFormat() const { return Format; }
unsigned LogBuilder::getNumArguments() const { return Arguments.size(); }
Value *LogBuilder::getArgument(unsigned I) const { return Arguments[I]; }
DiffLogBuilder::~DiffLogBuilder() { consumer.logd(*this); }
void DiffLogBuilder::addMatch(Instruction *L, Instruction *R) {
Diff.push_back(DiffRecord(L, R));
}
void DiffLogBuilder::addLeft(Instruction *L) {
// HACK: VS 2010 has a bug in the stdlib that requires this.
Diff.push_back(DiffRecord(L, DiffRecord::second_type(nullptr)));
}
void DiffLogBuilder::addRight(Instruction *R) {
// HACK: VS 2010 has a bug in the stdlib that requires this.
Diff.push_back(DiffRecord(DiffRecord::first_type(nullptr), R));
}
unsigned DiffLogBuilder::getNumLines() const { return Diff.size(); }
DiffChange DiffLogBuilder::getLineKind(unsigned I) const {
return (Diff[I].first ? (Diff[I].second ? DC_match : DC_left)
: DC_right);
}
Instruction *DiffLogBuilder::getLeft(unsigned I) const { return Diff[I].first; }
Instruction *DiffLogBuilder::getRight(unsigned I) const { return Diff[I].second; }

84
external/llvm/tools/llvm-diff/DiffLog.h vendored Normal file
View File

@ -0,0 +1,84 @@
//===-- DiffLog.h - Difference Log Builder and accessories ------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This header defines the interface to the LLVM difference log builder.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLVM_DIFF_DIFFLOG_H
#define LLVM_TOOLS_LLVM_DIFF_DIFFLOG_H
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
namespace llvm {
class Instruction;
class Value;
class Consumer;
/// Trichotomy assumption
enum DiffChange { DC_match, DC_left, DC_right };
/// A temporary-object class for building up log messages.
class LogBuilder {
Consumer *consumer;
/// The use of a stored StringRef here is okay because
/// LogBuilder should be used only as a temporary, and as a
/// temporary it will be destructed before whatever temporary
/// might be initializing this format.
StringRef Format;
SmallVector<Value*, 4> Arguments;
public:
LogBuilder(Consumer &c, StringRef Format) : consumer(&c), Format(Format) {}
LogBuilder(LogBuilder &&L)
: consumer(L.consumer), Format(L.Format),
Arguments(std::move(L.Arguments)) {
L.consumer = nullptr;
}
LogBuilder &operator<<(Value *V) {
Arguments.push_back(V);
return *this;
}
~LogBuilder();
StringRef getFormat() const;
unsigned getNumArguments() const;
Value *getArgument(unsigned I) const;
};
/// A temporary-object class for building up diff messages.
class DiffLogBuilder {
typedef std::pair<Instruction*,Instruction*> DiffRecord;
SmallVector<DiffRecord, 20> Diff;
Consumer &consumer;
public:
DiffLogBuilder(Consumer &c) : consumer(c) {}
~DiffLogBuilder();
void addMatch(Instruction *L, Instruction *R);
// HACK: VS 2010 has a bug in the stdlib that requires this.
void addLeft(Instruction *L);
void addRight(Instruction *R);
unsigned getNumLines() const;
DiffChange getLineKind(unsigned I) const;
Instruction *getLeft(unsigned I) const;
Instruction *getRight(unsigned I) const;
};
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,90 @@
//===-- DifferenceEngine.h - Module comparator ------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This header defines the interface to the LLVM difference engine,
// which structurally compares functions within a module.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLVM_DIFF_DIFFERENCEENGINE_H
#define LLVM_TOOLS_LLVM_DIFF_DIFFERENCEENGINE_H
#include "DiffConsumer.h"
#include "DiffLog.h"
#include "llvm/ADT/StringRef.h"
#include <utility>
namespace llvm {
class Function;
class GlobalValue;
class Instruction;
class LLVMContext;
class Module;
class Twine;
class Value;
/// A class for performing structural comparisons of LLVM assembly.
class DifferenceEngine {
public:
/// A RAII object for recording the current context.
struct Context {
Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) {
Engine.consumer.enterContext(L, R);
}
~Context() {
Engine.consumer.exitContext();
}
private:
DifferenceEngine &Engine;
};
/// An oracle for answering whether two values are equivalent as
/// operands.
class Oracle {
virtual void anchor();
public:
virtual bool operator()(Value *L, Value *R) = 0;
protected:
virtual ~Oracle() {}
};
DifferenceEngine(Consumer &consumer)
: consumer(consumer), globalValueOracle(nullptr) {}
void diff(Module *L, Module *R);
void diff(Function *L, Function *R);
void log(StringRef text) {
consumer.log(text);
}
LogBuilder logf(StringRef text) {
return LogBuilder(consumer, text);
}
Consumer& getConsumer() const { return consumer; }
/// Installs an oracle to decide whether two global values are
/// equivalent as operands. Without an oracle, global values are
/// considered equivalent as operands precisely when they have the
/// same name.
void setGlobalValueOracle(Oracle *oracle) {
globalValueOracle = oracle;
}
/// Determines whether two global values are equivalent.
bool equivalentAsOperands(GlobalValue *L, GlobalValue *R);
private:
Consumer &consumer;
Oracle *globalValueOracle;
};
}
#endif

View File

@ -0,0 +1,22 @@
;===- ./tools/llvm-diff/LLVMBuild.txt --------------------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[component_0]
type = Tool
name = llvm-diff
parent = Tools
required_libraries = AsmParser BitReader IRReader

View File

@ -0,0 +1,92 @@
//===-- llvm-diff.cpp - Module comparator command-line driver ---*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the command-line driver for the difference engine.
//
//===----------------------------------------------------------------------===//
#include "DiffLog.h"
#include "DifferenceEngine.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
#include <utility>
using namespace llvm;
/// Reads a module from a file. On error, messages are written to stderr
/// and null is returned.
static std::unique_ptr<Module> readModule(LLVMContext &Context,
StringRef Name) {
SMDiagnostic Diag;
std::unique_ptr<Module> M = parseIRFile(Name, Diag, Context);
if (!M)
Diag.print("llvm-diff", errs());
return M;
}
static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,
StringRef Name) {
// Drop leading sigils from the global name.
if (Name.startswith("@")) Name = Name.substr(1);
Function *LFn = L.getFunction(Name);
Function *RFn = R.getFunction(Name);
if (LFn && RFn)
Engine.diff(LFn, RFn);
else if (!LFn && !RFn)
errs() << "No function named @" << Name << " in either module\n";
else if (!LFn)
errs() << "No function named @" << Name << " in left module\n";
else
errs() << "No function named @" << Name << " in right module\n";
}
static cl::opt<std::string> LeftFilename(cl::Positional,
cl::desc("<first file>"),
cl::Required);
static cl::opt<std::string> RightFilename(cl::Positional,
cl::desc("<second file>"),
cl::Required);
static cl::list<std::string> GlobalsToCompare(cl::Positional,
cl::desc("<globals to compare>"));
int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv);
LLVMContext Context;
// Load both modules. Die if that fails.
std::unique_ptr<Module> LModule = readModule(Context, LeftFilename);
std::unique_ptr<Module> RModule = readModule(Context, RightFilename);
if (!LModule || !RModule) return 1;
DiffConsumer Consumer;
DifferenceEngine Engine(Consumer);
// If any global names were given, just diff those.
if (!GlobalsToCompare.empty()) {
for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)
diffGlobal(Engine, *LModule, *RModule, GlobalsToCompare[I]);
// Otherwise, diff everything in the module.
} else {
Engine.diff(LModule.get(), RModule.get());
}
return Consumer.hadDifferences();
}