2010-07-29 09:53:27 +02:00
|
|
|
//===-- DifferenceEngine.cpp - Structural function/module comparison ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-07-29 20:08:23 +02:00
|
|
|
// This header defines the implementation of the LLVM difference
|
|
|
|
// engine, which structurally compares global values within a module.
|
2010-07-29 09:53:27 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-07-29 20:08:23 +02:00
|
|
|
#include "DifferenceEngine.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSet.h"
|
2014-03-04 12:45:46 +01:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-03-04 12:01:28 +01:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2010-07-29 20:08:23 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/type_traits.h"
|
|
|
|
#include <utility>
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// A priority queue, implemented as a heap.
|
|
|
|
template <class T, class Sorter, unsigned InlineCapacity>
|
|
|
|
class PriorityQueue {
|
|
|
|
Sorter Precedes;
|
|
|
|
llvm::SmallVector<T, InlineCapacity> Storage;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PriorityQueue(const Sorter &Precedes) : Precedes(Precedes) {}
|
|
|
|
|
|
|
|
/// Checks whether the heap is empty.
|
|
|
|
bool empty() const { return Storage.empty(); }
|
|
|
|
|
|
|
|
/// Insert a new value on the heap.
|
|
|
|
void insert(const T &V) {
|
|
|
|
unsigned Index = Storage.size();
|
|
|
|
Storage.push_back(V);
|
|
|
|
if (Index == 0) return;
|
|
|
|
|
|
|
|
T *data = Storage.data();
|
|
|
|
while (true) {
|
|
|
|
unsigned Target = (Index + 1) / 2 - 1;
|
|
|
|
if (!Precedes(data[Index], data[Target])) return;
|
|
|
|
std::swap(data[Index], data[Target]);
|
|
|
|
if (Target == 0) return;
|
|
|
|
Index = Target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the minimum value in the heap. Only valid on a non-empty heap.
|
|
|
|
T remove_min() {
|
|
|
|
assert(!empty());
|
|
|
|
T tmp = Storage[0];
|
|
|
|
|
|
|
|
unsigned NewSize = Storage.size() - 1;
|
|
|
|
if (NewSize) {
|
|
|
|
// Move the slot at the end to the beginning.
|
|
|
|
if (isPodLike<T>::value)
|
|
|
|
Storage[0] = Storage[NewSize];
|
|
|
|
else
|
|
|
|
std::swap(Storage[0], Storage[NewSize]);
|
|
|
|
|
|
|
|
// Bubble the root up as necessary.
|
|
|
|
unsigned Index = 0;
|
|
|
|
while (true) {
|
|
|
|
// With a 1-based index, the children would be Index*2 and Index*2+1.
|
|
|
|
unsigned R = (Index + 1) * 2;
|
|
|
|
unsigned L = R - 1;
|
|
|
|
|
|
|
|
// If R is out of bounds, we're done after this in any case.
|
|
|
|
if (R >= NewSize) {
|
|
|
|
// If L is also out of bounds, we're done immediately.
|
|
|
|
if (L >= NewSize) break;
|
|
|
|
|
|
|
|
// Otherwise, test whether we should swap L and Index.
|
|
|
|
if (Precedes(Storage[L], Storage[Index]))
|
|
|
|
std::swap(Storage[L], Storage[Index]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we need to compare with the smaller of L and R.
|
|
|
|
// Prefer R because it's closer to the end of the array.
|
|
|
|
unsigned IndexToTest = (Precedes(Storage[L], Storage[R]) ? L : R);
|
|
|
|
|
|
|
|
// If Index is >= the min of L and R, then heap ordering is restored.
|
|
|
|
if (!Precedes(Storage[IndexToTest], Storage[Index]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Otherwise, keep bubbling up.
|
|
|
|
std::swap(Storage[IndexToTest], Storage[Index]);
|
|
|
|
Index = IndexToTest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Storage.pop_back();
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A function-scope difference engine.
|
|
|
|
class FunctionDifferenceEngine {
|
|
|
|
DifferenceEngine &Engine;
|
|
|
|
|
|
|
|
/// The current mapping from old local values to new local values.
|
|
|
|
DenseMap<Value*, Value*> Values;
|
|
|
|
|
|
|
|
/// The current mapping from old blocks to new blocks.
|
|
|
|
DenseMap<BasicBlock*, BasicBlock*> Blocks;
|
|
|
|
|
2010-07-29 10:53:59 +02:00
|
|
|
DenseSet<std::pair<Value*, Value*> > TentativeValues;
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
unsigned getUnprocPredCount(BasicBlock *Block) const {
|
|
|
|
unsigned Count = 0;
|
2014-07-21 19:06:51 +02:00
|
|
|
for (pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; ++I)
|
|
|
|
if (!Blocks.count(*I)) Count++;
|
2010-07-29 09:53:27 +02:00
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::pair<BasicBlock*, BasicBlock*> BlockPair;
|
|
|
|
|
|
|
|
/// A type which sorts a priority queue by the number of unprocessed
|
|
|
|
/// predecessor blocks it has remaining.
|
|
|
|
///
|
|
|
|
/// This is actually really expensive to calculate.
|
|
|
|
struct QueueSorter {
|
|
|
|
const FunctionDifferenceEngine &fde;
|
|
|
|
explicit QueueSorter(const FunctionDifferenceEngine &fde) : fde(fde) {}
|
|
|
|
|
|
|
|
bool operator()(const BlockPair &Old, const BlockPair &New) {
|
|
|
|
return fde.getUnprocPredCount(Old.first)
|
|
|
|
< fde.getUnprocPredCount(New.first);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A queue of unified blocks to process.
|
|
|
|
PriorityQueue<BlockPair, QueueSorter, 20> Queue;
|
|
|
|
|
|
|
|
/// Try to unify the given two blocks. Enqueues them for processing
|
|
|
|
/// if they haven't already been processed.
|
|
|
|
///
|
|
|
|
/// Returns true if there was a problem unifying them.
|
|
|
|
bool tryUnify(BasicBlock *L, BasicBlock *R) {
|
|
|
|
BasicBlock *&Ref = Blocks[L];
|
|
|
|
|
|
|
|
if (Ref) {
|
|
|
|
if (Ref == R) return false;
|
|
|
|
|
2010-07-29 10:14:41 +02:00
|
|
|
Engine.logf("successor %l cannot be equivalent to %r; "
|
|
|
|
"it's already equivalent to %r")
|
2010-07-29 09:53:27 +02:00
|
|
|
<< L << R << Ref;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref = R;
|
|
|
|
Queue.insert(BlockPair(L, R));
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-29 11:20:34 +02:00
|
|
|
|
|
|
|
/// Unifies two instructions, given that they're known not to have
|
|
|
|
/// structural differences.
|
|
|
|
void unify(Instruction *L, Instruction *R) {
|
|
|
|
DifferenceEngine::Context C(Engine, L, R);
|
|
|
|
|
|
|
|
bool Result = diff(L, R, true, true);
|
|
|
|
assert(!Result && "structural differences second time around?");
|
|
|
|
(void) Result;
|
|
|
|
if (!L->use_empty())
|
|
|
|
Values[L] = R;
|
|
|
|
}
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
void processQueue() {
|
|
|
|
while (!Queue.empty()) {
|
|
|
|
BlockPair Pair = Queue.remove_min();
|
|
|
|
diff(Pair.first, Pair.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void diff(BasicBlock *L, BasicBlock *R) {
|
|
|
|
DifferenceEngine::Context C(Engine, L, R);
|
|
|
|
|
|
|
|
BasicBlock::iterator LI = L->begin(), LE = L->end();
|
2011-08-12 16:54:45 +02:00
|
|
|
BasicBlock::iterator RI = R->begin();
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
do {
|
2011-08-12 16:54:45 +02:00
|
|
|
assert(LI != LE && RI != R->end());
|
2010-07-29 09:53:27 +02:00
|
|
|
Instruction *LeftI = &*LI, *RightI = &*RI;
|
|
|
|
|
|
|
|
// If the instructions differ, start the more sophisticated diff
|
2010-07-29 10:53:59 +02:00
|
|
|
// algorithm at the start of the block.
|
2010-07-29 11:04:45 +02:00
|
|
|
if (diff(LeftI, RightI, false, false)) {
|
2010-07-29 10:53:59 +02:00
|
|
|
TentativeValues.clear();
|
|
|
|
return runBlockDiff(L->begin(), R->begin());
|
|
|
|
}
|
2010-07-29 09:53:27 +02:00
|
|
|
|
2010-07-29 10:53:59 +02:00
|
|
|
// Otherwise, tentatively unify them.
|
2010-07-29 09:53:27 +02:00
|
|
|
if (!LeftI->use_empty())
|
2010-07-29 10:53:59 +02:00
|
|
|
TentativeValues.insert(std::make_pair(LeftI, RightI));
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
++LI, ++RI;
|
|
|
|
} while (LI != LE); // This is sufficient: we can't get equality of
|
|
|
|
// terminators if there are residual instructions.
|
2010-07-29 10:53:59 +02:00
|
|
|
|
2010-07-29 11:20:34 +02:00
|
|
|
// Unify everything in the block, non-tentatively this time.
|
2010-07-29 10:53:59 +02:00
|
|
|
TentativeValues.clear();
|
2010-07-29 11:20:34 +02:00
|
|
|
for (LI = L->begin(), RI = R->begin(); LI != LE; ++LI, ++RI)
|
|
|
|
unify(&*LI, &*RI);
|
2010-07-29 09:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool matchForBlockDiff(Instruction *L, Instruction *R);
|
|
|
|
void runBlockDiff(BasicBlock::iterator LI, BasicBlock::iterator RI);
|
|
|
|
|
|
|
|
bool diffCallSites(CallSite L, CallSite R, bool Complain) {
|
|
|
|
// FIXME: call attributes
|
|
|
|
if (!equivalentAsOperands(L.getCalledValue(), R.getCalledValue())) {
|
|
|
|
if (Complain) Engine.log("called functions differ");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (L.arg_size() != R.arg_size()) {
|
|
|
|
if (Complain) Engine.log("argument counts differ");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (unsigned I = 0, E = L.arg_size(); I != E; ++I)
|
|
|
|
if (!equivalentAsOperands(L.getArgument(I), R.getArgument(I))) {
|
|
|
|
if (Complain)
|
2010-07-29 10:14:41 +02:00
|
|
|
Engine.logf("arguments %l and %r differ")
|
2010-07-29 09:53:27 +02:00
|
|
|
<< L.getArgument(I) << R.getArgument(I);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool diff(Instruction *L, Instruction *R, bool Complain, bool TryUnify) {
|
|
|
|
// FIXME: metadata (if Complain is set)
|
|
|
|
|
|
|
|
// Different opcodes always imply different operations.
|
|
|
|
if (L->getOpcode() != R->getOpcode()) {
|
|
|
|
if (Complain) Engine.log("different instruction types");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<CmpInst>(L)) {
|
|
|
|
if (cast<CmpInst>(L)->getPredicate()
|
|
|
|
!= cast<CmpInst>(R)->getPredicate()) {
|
|
|
|
if (Complain) Engine.log("different predicates");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (isa<CallInst>(L)) {
|
|
|
|
return diffCallSites(CallSite(L), CallSite(R), Complain);
|
|
|
|
} else if (isa<PHINode>(L)) {
|
|
|
|
// FIXME: implement.
|
|
|
|
|
2011-04-15 07:18:47 +02:00
|
|
|
// This is really weird; type uniquing is broken?
|
2010-07-29 09:53:27 +02:00
|
|
|
if (L->getType() != R->getType()) {
|
|
|
|
if (!L->getType()->isPointerTy() || !R->getType()->isPointerTy()) {
|
|
|
|
if (Complain) Engine.log("different phi types");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Terminators.
|
|
|
|
} else if (isa<InvokeInst>(L)) {
|
|
|
|
InvokeInst *LI = cast<InvokeInst>(L);
|
|
|
|
InvokeInst *RI = cast<InvokeInst>(R);
|
|
|
|
if (diffCallSites(CallSite(LI), CallSite(RI), Complain))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (TryUnify) {
|
|
|
|
tryUnify(LI->getNormalDest(), RI->getNormalDest());
|
|
|
|
tryUnify(LI->getUnwindDest(), RI->getUnwindDest());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else if (isa<BranchInst>(L)) {
|
|
|
|
BranchInst *LI = cast<BranchInst>(L);
|
|
|
|
BranchInst *RI = cast<BranchInst>(R);
|
|
|
|
if (LI->isConditional() != RI->isConditional()) {
|
|
|
|
if (Complain) Engine.log("branch conditionality differs");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LI->isConditional()) {
|
|
|
|
if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) {
|
|
|
|
if (Complain) Engine.log("branch conditions differ");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (TryUnify) tryUnify(LI->getSuccessor(1), RI->getSuccessor(1));
|
|
|
|
}
|
|
|
|
if (TryUnify) tryUnify(LI->getSuccessor(0), RI->getSuccessor(0));
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else if (isa<SwitchInst>(L)) {
|
|
|
|
SwitchInst *LI = cast<SwitchInst>(L);
|
|
|
|
SwitchInst *RI = cast<SwitchInst>(R);
|
|
|
|
if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) {
|
|
|
|
if (Complain) Engine.log("switch conditions differ");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (TryUnify) tryUnify(LI->getDefaultDest(), RI->getDefaultDest());
|
|
|
|
|
|
|
|
bool Difference = false;
|
|
|
|
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
llvm-svn: 190328
2013-09-09 21:14:35 +02:00
|
|
|
DenseMap<ConstantInt*,BasicBlock*> LCases;
|
2012-03-08 08:06:20 +01:00
|
|
|
|
2012-03-11 07:09:17 +01:00
|
|
|
for (SwitchInst::CaseIt I = LI->case_begin(), E = LI->case_end();
|
2012-03-08 08:06:20 +01:00
|
|
|
I != E; ++I)
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
llvm-svn: 190328
2013-09-09 21:14:35 +02:00
|
|
|
LCases[I.getCaseValue()] = I.getCaseSuccessor();
|
2012-03-08 08:06:20 +01:00
|
|
|
|
2012-03-11 07:09:17 +01:00
|
|
|
for (SwitchInst::CaseIt I = RI->case_begin(), E = RI->case_end();
|
2012-03-08 08:06:20 +01:00
|
|
|
I != E; ++I) {
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
llvm-svn: 190328
2013-09-09 21:14:35 +02:00
|
|
|
ConstantInt *CaseValue = I.getCaseValue();
|
2010-07-29 09:53:27 +02:00
|
|
|
BasicBlock *LCase = LCases[CaseValue];
|
|
|
|
if (LCase) {
|
2012-03-08 08:06:20 +01:00
|
|
|
if (TryUnify) tryUnify(LCase, I.getCaseSuccessor());
|
2010-07-29 09:53:27 +02:00
|
|
|
LCases.erase(CaseValue);
|
2011-11-08 07:53:04 +01:00
|
|
|
} else if (Complain || !Difference) {
|
2010-07-29 09:53:27 +02:00
|
|
|
if (Complain)
|
2010-07-29 10:14:41 +02:00
|
|
|
Engine.logf("right switch has extra case %r") << CaseValue;
|
2010-07-29 09:53:27 +02:00
|
|
|
Difference = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!Difference)
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
llvm-svn: 190328
2013-09-09 21:14:35 +02:00
|
|
|
for (DenseMap<ConstantInt*,BasicBlock*>::iterator
|
2010-07-29 09:53:27 +02:00
|
|
|
I = LCases.begin(), E = LCases.end(); I != E; ++I) {
|
|
|
|
if (Complain)
|
2010-07-29 10:14:41 +02:00
|
|
|
Engine.logf("left switch has extra case %l") << I->first;
|
2010-07-29 09:53:27 +02:00
|
|
|
Difference = true;
|
|
|
|
}
|
|
|
|
return Difference;
|
|
|
|
} else if (isa<UnreachableInst>(L)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (L->getNumOperands() != R->getNumOperands()) {
|
|
|
|
if (Complain) Engine.log("instructions have different operand counts");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) {
|
|
|
|
Value *LO = L->getOperand(I), *RO = R->getOperand(I);
|
|
|
|
if (!equivalentAsOperands(LO, RO)) {
|
2010-07-29 10:14:41 +02:00
|
|
|
if (Complain) Engine.logf("operands %l and %r differ") << LO << RO;
|
2010-07-29 09:53:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equivalentAsOperands(Constant *L, Constant *R) {
|
|
|
|
// Use equality as a preliminary filter.
|
|
|
|
if (L == R)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (L->getValueID() != R->getValueID())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Ask the engine about global values.
|
|
|
|
if (isa<GlobalValue>(L))
|
|
|
|
return Engine.equivalentAsOperands(cast<GlobalValue>(L),
|
|
|
|
cast<GlobalValue>(R));
|
|
|
|
|
|
|
|
// Compare constant expressions structurally.
|
|
|
|
if (isa<ConstantExpr>(L))
|
|
|
|
return equivalentAsOperands(cast<ConstantExpr>(L),
|
|
|
|
cast<ConstantExpr>(R));
|
|
|
|
|
|
|
|
// Nulls of the "same type" don't always actually have the same
|
|
|
|
// type; I don't know why. Just white-list them.
|
|
|
|
if (isa<ConstantPointerNull>(L))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Block addresses only match if we've already encountered the
|
|
|
|
// block. FIXME: tentative matches?
|
|
|
|
if (isa<BlockAddress>(L))
|
|
|
|
return Blocks[cast<BlockAddress>(L)->getBasicBlock()]
|
|
|
|
== cast<BlockAddress>(R)->getBasicBlock();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equivalentAsOperands(ConstantExpr *L, ConstantExpr *R) {
|
|
|
|
if (L == R)
|
|
|
|
return true;
|
|
|
|
if (L->getOpcode() != R->getOpcode())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (L->getOpcode()) {
|
|
|
|
case Instruction::ICmp:
|
|
|
|
case Instruction::FCmp:
|
|
|
|
if (L->getPredicate() != R->getPredicate())
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
// FIXME: inbounds?
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (L->getNumOperands() != R->getNumOperands())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I)
|
|
|
|
if (!equivalentAsOperands(L->getOperand(I), R->getOperand(I)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equivalentAsOperands(Value *L, Value *R) {
|
|
|
|
// Fall out if the values have different kind.
|
|
|
|
// This possibly shouldn't take priority over oracles.
|
|
|
|
if (L->getValueID() != R->getValueID())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Value subtypes: Argument, Constant, Instruction, BasicBlock,
|
|
|
|
// InlineAsm, MDNode, MDString, PseudoSourceValue
|
|
|
|
|
|
|
|
if (isa<Constant>(L))
|
|
|
|
return equivalentAsOperands(cast<Constant>(L), cast<Constant>(R));
|
|
|
|
|
|
|
|
if (isa<Instruction>(L))
|
2010-07-29 10:53:59 +02:00
|
|
|
return Values[L] == R || TentativeValues.count(std::make_pair(L, R));
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
if (isa<Argument>(L))
|
|
|
|
return Values[L] == R;
|
|
|
|
|
|
|
|
if (isa<BasicBlock>(L))
|
|
|
|
return Blocks[cast<BasicBlock>(L)] != R;
|
|
|
|
|
|
|
|
// Pretend everything else is identical.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid a gcc warning about accessing 'this' in an initializer.
|
|
|
|
FunctionDifferenceEngine *this_() { return this; }
|
|
|
|
|
|
|
|
public:
|
|
|
|
FunctionDifferenceEngine(DifferenceEngine &Engine) :
|
|
|
|
Engine(Engine), Queue(QueueSorter(*this_())) {}
|
|
|
|
|
|
|
|
void diff(Function *L, Function *R) {
|
|
|
|
if (L->arg_size() != R->arg_size())
|
|
|
|
Engine.log("different argument counts");
|
|
|
|
|
|
|
|
// Map the arguments.
|
|
|
|
for (Function::arg_iterator
|
|
|
|
LI = L->arg_begin(), LE = L->arg_end(),
|
|
|
|
RI = R->arg_begin(), RE = R->arg_end();
|
|
|
|
LI != LE && RI != RE; ++LI, ++RI)
|
|
|
|
Values[&*LI] = &*RI;
|
|
|
|
|
|
|
|
tryUnify(&*L->begin(), &*R->begin());
|
|
|
|
processQueue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DiffEntry {
|
|
|
|
DiffEntry() : Cost(0) {}
|
|
|
|
|
|
|
|
unsigned Cost;
|
|
|
|
llvm::SmallVector<char, 8> Path; // actually of DifferenceEngine::DiffChange
|
|
|
|
};
|
|
|
|
|
|
|
|
bool FunctionDifferenceEngine::matchForBlockDiff(Instruction *L,
|
|
|
|
Instruction *R) {
|
|
|
|
return !diff(L, R, false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionDifferenceEngine::runBlockDiff(BasicBlock::iterator LStart,
|
|
|
|
BasicBlock::iterator RStart) {
|
|
|
|
BasicBlock::iterator LE = LStart->getParent()->end();
|
|
|
|
BasicBlock::iterator RE = RStart->getParent()->end();
|
|
|
|
|
|
|
|
unsigned NL = std::distance(LStart, LE);
|
|
|
|
|
|
|
|
SmallVector<DiffEntry, 20> Paths1(NL+1);
|
|
|
|
SmallVector<DiffEntry, 20> Paths2(NL+1);
|
|
|
|
|
|
|
|
DiffEntry *Cur = Paths1.data();
|
|
|
|
DiffEntry *Next = Paths2.data();
|
|
|
|
|
2010-07-29 10:53:59 +02:00
|
|
|
const unsigned LeftCost = 2;
|
|
|
|
const unsigned RightCost = 2;
|
|
|
|
const unsigned MatchCost = 0;
|
|
|
|
|
|
|
|
assert(TentativeValues.empty());
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
// Initialize the first column.
|
|
|
|
for (unsigned I = 0; I != NL+1; ++I) {
|
2010-07-29 10:53:59 +02:00
|
|
|
Cur[I].Cost = I * LeftCost;
|
2010-07-29 09:53:27 +02:00
|
|
|
for (unsigned J = 0; J != I; ++J)
|
2011-03-14 23:22:46 +01:00
|
|
|
Cur[I].Path.push_back(DC_left);
|
2010-07-29 09:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (BasicBlock::iterator RI = RStart; RI != RE; ++RI) {
|
|
|
|
// Initialize the first row.
|
|
|
|
Next[0] = Cur[0];
|
2010-07-29 10:53:59 +02:00
|
|
|
Next[0].Cost += RightCost;
|
2011-03-14 23:22:46 +01:00
|
|
|
Next[0].Path.push_back(DC_right);
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
unsigned Index = 1;
|
|
|
|
for (BasicBlock::iterator LI = LStart; LI != LE; ++LI, ++Index) {
|
|
|
|
if (matchForBlockDiff(&*LI, &*RI)) {
|
|
|
|
Next[Index] = Cur[Index-1];
|
2010-07-29 10:53:59 +02:00
|
|
|
Next[Index].Cost += MatchCost;
|
2011-03-14 23:22:46 +01:00
|
|
|
Next[Index].Path.push_back(DC_match);
|
2010-07-29 10:53:59 +02:00
|
|
|
TentativeValues.insert(std::make_pair(&*LI, &*RI));
|
2010-07-29 09:53:27 +02:00
|
|
|
} else if (Next[Index-1].Cost <= Cur[Index].Cost) {
|
|
|
|
Next[Index] = Next[Index-1];
|
2010-07-29 10:53:59 +02:00
|
|
|
Next[Index].Cost += LeftCost;
|
2011-03-14 23:22:46 +01:00
|
|
|
Next[Index].Path.push_back(DC_left);
|
2010-07-29 09:53:27 +02:00
|
|
|
} else {
|
|
|
|
Next[Index] = Cur[Index];
|
2010-07-29 10:53:59 +02:00
|
|
|
Next[Index].Cost += RightCost;
|
2011-03-14 23:22:46 +01:00
|
|
|
Next[Index].Path.push_back(DC_right);
|
2010-07-29 09:53:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::swap(Cur, Next);
|
|
|
|
}
|
|
|
|
|
2010-07-29 11:20:34 +02:00
|
|
|
// We don't need the tentative values anymore; everything from here
|
|
|
|
// on out should be non-tentative.
|
|
|
|
TentativeValues.clear();
|
|
|
|
|
2010-07-29 09:53:27 +02:00
|
|
|
SmallVectorImpl<char> &Path = Cur[NL].Path;
|
|
|
|
BasicBlock::iterator LI = LStart, RI = RStart;
|
|
|
|
|
2011-03-14 23:22:46 +01:00
|
|
|
DiffLogBuilder Diff(Engine.getConsumer());
|
2010-07-29 09:53:27 +02:00
|
|
|
|
|
|
|
// Drop trailing matches.
|
2011-03-14 23:22:46 +01:00
|
|
|
while (Path.back() == DC_match)
|
2010-07-29 09:53:27 +02:00
|
|
|
Path.pop_back();
|
|
|
|
|
2010-07-29 10:53:59 +02:00
|
|
|
// Skip leading matches.
|
|
|
|
SmallVectorImpl<char>::iterator
|
|
|
|
PI = Path.begin(), PE = Path.end();
|
2011-03-14 23:22:46 +01:00
|
|
|
while (PI != PE && *PI == DC_match) {
|
2010-07-29 11:20:34 +02:00
|
|
|
unify(&*LI, &*RI);
|
2010-07-29 10:53:59 +02:00
|
|
|
++PI, ++LI, ++RI;
|
2010-07-29 11:20:34 +02:00
|
|
|
}
|
2010-07-29 10:53:59 +02:00
|
|
|
|
|
|
|
for (; PI != PE; ++PI) {
|
2011-03-14 23:22:46 +01:00
|
|
|
switch (static_cast<DiffChange>(*PI)) {
|
|
|
|
case DC_match:
|
2010-07-29 09:53:27 +02:00
|
|
|
assert(LI != LE && RI != RE);
|
|
|
|
{
|
|
|
|
Instruction *L = &*LI, *R = &*RI;
|
2010-07-29 11:20:34 +02:00
|
|
|
unify(L, R);
|
2010-07-29 09:53:27 +02:00
|
|
|
Diff.addMatch(L, R);
|
|
|
|
}
|
|
|
|
++LI; ++RI;
|
|
|
|
break;
|
|
|
|
|
2011-03-14 23:22:46 +01:00
|
|
|
case DC_left:
|
2010-07-29 09:53:27 +02:00
|
|
|
assert(LI != LE);
|
|
|
|
Diff.addLeft(&*LI);
|
|
|
|
++LI;
|
|
|
|
break;
|
|
|
|
|
2011-03-14 23:22:46 +01:00
|
|
|
case DC_right:
|
2010-07-29 09:53:27 +02:00
|
|
|
assert(RI != RE);
|
|
|
|
Diff.addRight(&*RI);
|
|
|
|
++RI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-29 11:20:34 +02:00
|
|
|
// Finishing unifying and complaining about the tails of the block,
|
|
|
|
// which should be matches all the way through.
|
|
|
|
while (LI != LE) {
|
|
|
|
assert(RI != RE);
|
|
|
|
unify(&*LI, &*RI);
|
|
|
|
++LI, ++RI;
|
|
|
|
}
|
2010-08-24 11:16:51 +02:00
|
|
|
|
|
|
|
// If the terminators have different kinds, but one is an invoke and the
|
|
|
|
// other is an unconditional branch immediately following a call, unify
|
|
|
|
// the results and the destinations.
|
|
|
|
TerminatorInst *LTerm = LStart->getParent()->getTerminator();
|
|
|
|
TerminatorInst *RTerm = RStart->getParent()->getTerminator();
|
|
|
|
if (isa<BranchInst>(LTerm) && isa<InvokeInst>(RTerm)) {
|
|
|
|
if (cast<BranchInst>(LTerm)->isConditional()) return;
|
2015-10-20 20:17:05 +02:00
|
|
|
BasicBlock::iterator I = LTerm->getIterator();
|
2010-08-24 11:16:51 +02:00
|
|
|
if (I == LStart->getParent()->begin()) return;
|
|
|
|
--I;
|
|
|
|
if (!isa<CallInst>(*I)) return;
|
|
|
|
CallInst *LCall = cast<CallInst>(&*I);
|
|
|
|
InvokeInst *RInvoke = cast<InvokeInst>(RTerm);
|
|
|
|
if (!equivalentAsOperands(LCall->getCalledValue(), RInvoke->getCalledValue()))
|
|
|
|
return;
|
|
|
|
if (!LCall->use_empty())
|
|
|
|
Values[LCall] = RInvoke;
|
|
|
|
tryUnify(LTerm->getSuccessor(0), RInvoke->getNormalDest());
|
|
|
|
} else if (isa<InvokeInst>(LTerm) && isa<BranchInst>(RTerm)) {
|
|
|
|
if (cast<BranchInst>(RTerm)->isConditional()) return;
|
2015-10-20 20:17:05 +02:00
|
|
|
BasicBlock::iterator I = RTerm->getIterator();
|
2010-08-24 11:16:51 +02:00
|
|
|
if (I == RStart->getParent()->begin()) return;
|
|
|
|
--I;
|
|
|
|
if (!isa<CallInst>(*I)) return;
|
|
|
|
CallInst *RCall = cast<CallInst>(I);
|
|
|
|
InvokeInst *LInvoke = cast<InvokeInst>(LTerm);
|
|
|
|
if (!equivalentAsOperands(LInvoke->getCalledValue(), RCall->getCalledValue()))
|
|
|
|
return;
|
|
|
|
if (!LInvoke->use_empty())
|
|
|
|
Values[LInvoke] = RCall;
|
|
|
|
tryUnify(LInvoke->getNormalDest(), RTerm->getSuccessor(0));
|
|
|
|
}
|
2010-07-29 09:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-20 03:50:00 +01:00
|
|
|
void DifferenceEngine::Oracle::anchor() { }
|
|
|
|
|
2010-07-29 09:53:27 +02:00
|
|
|
void DifferenceEngine::diff(Function *L, Function *R) {
|
|
|
|
Context C(*this, L, R);
|
|
|
|
|
|
|
|
// FIXME: types
|
|
|
|
// FIXME: attributes and CC
|
|
|
|
// FIXME: parameter attributes
|
|
|
|
|
|
|
|
// If both are declarations, we're done.
|
|
|
|
if (L->empty() && R->empty())
|
|
|
|
return;
|
|
|
|
else if (L->empty())
|
|
|
|
log("left function is declaration, right function is definition");
|
|
|
|
else if (R->empty())
|
|
|
|
log("right function is declaration, left function is definition");
|
|
|
|
else
|
|
|
|
FunctionDifferenceEngine(*this).diff(L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DifferenceEngine::diff(Module *L, Module *R) {
|
|
|
|
StringSet<> LNames;
|
|
|
|
SmallVector<std::pair<Function*,Function*>, 20> Queue;
|
|
|
|
|
|
|
|
for (Module::iterator I = L->begin(), E = L->end(); I != E; ++I) {
|
|
|
|
Function *LFn = &*I;
|
|
|
|
LNames.insert(LFn->getName());
|
|
|
|
|
|
|
|
if (Function *RFn = R->getFunction(LFn->getName()))
|
|
|
|
Queue.push_back(std::make_pair(LFn, RFn));
|
|
|
|
else
|
2010-07-29 10:14:41 +02:00
|
|
|
logf("function %l exists only in left module") << LFn;
|
2010-07-29 09:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::iterator I = R->begin(), E = R->end(); I != E; ++I) {
|
|
|
|
Function *RFn = &*I;
|
|
|
|
if (!LNames.count(RFn->getName()))
|
2010-07-29 10:14:41 +02:00
|
|
|
logf("function %r exists only in right module") << RFn;
|
2010-07-29 09:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (SmallVectorImpl<std::pair<Function*,Function*> >::iterator
|
|
|
|
I = Queue.begin(), E = Queue.end(); I != E; ++I)
|
|
|
|
diff(I->first, I->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DifferenceEngine::equivalentAsOperands(GlobalValue *L, GlobalValue *R) {
|
|
|
|
if (globalValueOracle) return (*globalValueOracle)(L, R);
|
|
|
|
return L->getName() == R->getName();
|
|
|
|
}
|