DifferenceEngine &Engine;
/// The current mapping from old local values to new local values.
- DenseMap<Value*, Value*> Values;
+ DenseMap<const Value *, const Value *> Values;
/// The current mapping from old blocks to new blocks.
- DenseMap<BasicBlock*, BasicBlock*> Blocks;
+ DenseMap<const BasicBlock *, const BasicBlock *> Blocks;
- DenseSet<std::pair<Value*, Value*> > TentativeValues;
+ DenseSet<std::pair<const Value *, const Value *>> TentativeValues;
- unsigned getUnprocPredCount(BasicBlock *Block) const {
+ unsigned getUnprocPredCount(const BasicBlock *Block) const {
unsigned Count = 0;
- for (pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; ++I)
+ for (const_pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E;
+ ++I)
if (!Blocks.count(*I)) Count++;
return Count;
}
- typedef std::pair<BasicBlock*, BasicBlock*> BlockPair;
+ typedef std::pair<const BasicBlock *, const BasicBlock *> BlockPair;
/// A type which sorts a priority queue by the number of unprocessed
/// predecessor blocks it has remaining.
const FunctionDifferenceEngine &fde;
explicit QueueSorter(const FunctionDifferenceEngine &fde) : fde(fde) {}
- bool operator()(const BlockPair &Old, const BlockPair &New) {
+ bool operator()(BlockPair &Old, BlockPair &New) {
return fde.getUnprocPredCount(Old.first)
< fde.getUnprocPredCount(New.first);
}
/// 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];
+ bool tryUnify(const BasicBlock *L, const BasicBlock *R) {
+ const BasicBlock *&Ref = Blocks[L];
if (Ref) {
if (Ref == R) return false;
Queue.insert(BlockPair(L, R));
return false;
}
-
+
/// Unifies two instructions, given that they're known not to have
/// structural differences.
- void unify(Instruction *L, Instruction *R) {
+ void unify(const Instruction *L, const Instruction *R) {
DifferenceEngine::Context C(Engine, L, R);
bool Result = diff(L, R, true, true);
}
}
- void diff(BasicBlock *L, BasicBlock *R) {
+ void diff(const BasicBlock *L, const BasicBlock *R) {
DifferenceEngine::Context C(Engine, L, R);
- BasicBlock::iterator LI = L->begin(), LE = L->end();
- BasicBlock::iterator RI = R->begin();
+ BasicBlock::const_iterator LI = L->begin(), LE = L->end();
+ BasicBlock::const_iterator RI = R->begin();
do {
assert(LI != LE && RI != R->end());
- Instruction *LeftI = &*LI, *RightI = &*RI;
+ const Instruction *LeftI = &*LI, *RightI = &*RI;
// If the instructions differ, start the more sophisticated diff
// algorithm at the start of the block.
unify(&*LI, &*RI);
}
- bool matchForBlockDiff(Instruction *L, Instruction *R);
- void runBlockDiff(BasicBlock::iterator LI, BasicBlock::iterator RI);
+ bool matchForBlockDiff(const Instruction *L, const Instruction *R);
+ void runBlockDiff(BasicBlock::const_iterator LI,
+ BasicBlock::const_iterator RI);
- bool diffCallSites(CallBase &L, CallBase &R, bool Complain) {
+ bool diffCallSites(const CallBase &L, const CallBase &R, bool Complain) {
// FIXME: call attributes
if (!equivalentAsOperands(L.getCalledOperand(), R.getCalledOperand())) {
if (Complain) Engine.log("called functions differ");
return false;
}
- bool diff(Instruction *L, Instruction *R, bool Complain, bool TryUnify) {
+ bool diff(const Instruction *L, const Instruction *R, bool Complain,
+ bool TryUnify) {
// FIXME: metadata (if Complain is set)
// Different opcodes always imply different operations.
// Terminators.
} else if (isa<InvokeInst>(L)) {
- InvokeInst &LI = cast<InvokeInst>(*L);
- InvokeInst &RI = cast<InvokeInst>(*R);
+ const InvokeInst &LI = cast<InvokeInst>(*L);
+ const InvokeInst &RI = cast<InvokeInst>(*R);
if (diffCallSites(LI, RI, Complain))
return true;
return false;
} else if (isa<BranchInst>(L)) {
- BranchInst *LI = cast<BranchInst>(L);
- BranchInst *RI = cast<BranchInst>(R);
+ const BranchInst *LI = cast<BranchInst>(L);
+ const BranchInst *RI = cast<BranchInst>(R);
if (LI->isConditional() != RI->isConditional()) {
if (Complain) Engine.log("branch conditionality differs");
return true;
return false;
} else if (isa<IndirectBrInst>(L)) {
- IndirectBrInst *LI = cast<IndirectBrInst>(L);
- IndirectBrInst *RI = cast<IndirectBrInst>(R);
+ const IndirectBrInst *LI = cast<IndirectBrInst>(L);
+ const IndirectBrInst *RI = cast<IndirectBrInst>(R);
if (LI->getNumDestinations() != RI->getNumDestinations()) {
if (Complain) Engine.log("indirectbr # of destinations differ");
return true;
return false;
} else if (isa<SwitchInst>(L)) {
- SwitchInst *LI = cast<SwitchInst>(L);
- SwitchInst *RI = cast<SwitchInst>(R);
+ const SwitchInst *LI = cast<SwitchInst>(L);
+ const SwitchInst *RI = cast<SwitchInst>(R);
if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) {
if (Complain) Engine.log("switch conditions differ");
return true;
bool Difference = false;
- DenseMap<ConstantInt*,BasicBlock*> LCases;
+ DenseMap<const ConstantInt *, const BasicBlock *> LCases;
for (auto Case : LI->cases())
LCases[Case.getCaseValue()] = Case.getCaseSuccessor();
for (auto Case : RI->cases()) {
- ConstantInt *CaseValue = Case.getCaseValue();
- BasicBlock *LCase = LCases[CaseValue];
+ const ConstantInt *CaseValue = Case.getCaseValue();
+ const BasicBlock *LCase = LCases[CaseValue];
if (LCase) {
if (TryUnify)
tryUnify(LCase, Case.getCaseSuccessor());
}
}
if (!Difference)
- for (DenseMap<ConstantInt*,BasicBlock*>::iterator
- I = LCases.begin(), E = LCases.end(); I != E; ++I) {
+ for (DenseMap<const ConstantInt *, const BasicBlock *>::iterator
+ I = LCases.begin(),
+ E = LCases.end();
+ I != E; ++I) {
if (Complain)
Engine.logf("left switch has extra case %l") << I->first;
Difference = true;
return false;
}
- bool equivalentAsOperands(Constant *L, Constant *R) {
+ bool equivalentAsOperands(const Constant *L, const Constant *R) {
// Use equality as a preliminary filter.
if (L == R)
return true;
// If L and R are ConstantVectors, compare each element
if (isa<ConstantVector>(L)) {
- ConstantVector *CVL = cast<ConstantVector>(L);
- ConstantVector *CVR = cast<ConstantVector>(R);
+ const ConstantVector *CVL = cast<ConstantVector>(L);
+ const ConstantVector *CVR = cast<ConstantVector>(R);
if (CVL->getType()->getNumElements() != CVR->getType()->getNumElements())
return false;
for (unsigned i = 0; i < CVL->getType()->getNumElements(); i++) {
return false;
}
- bool equivalentAsOperands(ConstantExpr *L, ConstantExpr *R) {
+ bool equivalentAsOperands(const ConstantExpr *L, const ConstantExpr *R) {
if (L == R)
return true;
if (L->getOpcode() != R->getOpcode())
return true;
}
- bool equivalentAsOperands(Value *L, Value *R) {
+ bool equivalentAsOperands(const Value *L, const Value *R) {
// Fall out if the values have different kind.
// This possibly shouldn't take priority over oracles.
if (L->getValueID() != R->getValueID())
FunctionDifferenceEngine(DifferenceEngine &Engine) :
Engine(Engine), Queue(QueueSorter(*this_())) {}
- void diff(Function *L, Function *R) {
+ void diff(const Function *L, const 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();
+ for (Function::const_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;
llvm::SmallVector<char, 8> Path; // actually of DifferenceEngine::DiffChange
};
-bool FunctionDifferenceEngine::matchForBlockDiff(Instruction *L,
- Instruction *R) {
+bool FunctionDifferenceEngine::matchForBlockDiff(const Instruction *L,
+ const 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();
+void FunctionDifferenceEngine::runBlockDiff(BasicBlock::const_iterator LStart,
+ BasicBlock::const_iterator RStart) {
+ BasicBlock::const_iterator LE = LStart->getParent()->end();
+ BasicBlock::const_iterator RE = RStart->getParent()->end();
unsigned NL = std::distance(LStart, LE);
Cur[I].Path.push_back(DC_left);
}
- for (BasicBlock::iterator RI = RStart; RI != RE; ++RI) {
+ for (BasicBlock::const_iterator RI = RStart; RI != RE; ++RI) {
// Initialize the first row.
Next[0] = Cur[0];
Next[0].Cost += RightCost;
Next[0].Path.push_back(DC_right);
unsigned Index = 1;
- for (BasicBlock::iterator LI = LStart; LI != LE; ++LI, ++Index) {
+ for (BasicBlock::const_iterator LI = LStart; LI != LE; ++LI, ++Index) {
if (matchForBlockDiff(&*LI, &*RI)) {
Next[Index] = Cur[Index-1];
Next[Index].Cost += MatchCost;
TentativeValues.clear();
SmallVectorImpl<char> &Path = Cur[NL].Path;
- BasicBlock::iterator LI = LStart, RI = RStart;
+ BasicBlock::const_iterator LI = LStart, RI = RStart;
DiffLogBuilder Diff(Engine.getConsumer());
case DC_match:
assert(LI != LE && RI != RE);
{
- Instruction *L = &*LI, *R = &*RI;
+ const Instruction *L = &*LI, *R = &*RI;
unify(L, R);
Diff.addMatch(L, R);
}
// 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.
- Instruction *LTerm = LStart->getParent()->getTerminator();
- Instruction *RTerm = RStart->getParent()->getTerminator();
+ const Instruction *LTerm = LStart->getParent()->getTerminator();
+ const Instruction *RTerm = RStart->getParent()->getTerminator();
if (isa<BranchInst>(LTerm) && isa<InvokeInst>(RTerm)) {
if (cast<BranchInst>(LTerm)->isConditional()) return;
- BasicBlock::iterator I = LTerm->getIterator();
+ BasicBlock::const_iterator I = LTerm->getIterator();
if (I == LStart->getParent()->begin()) return;
--I;
if (!isa<CallInst>(*I)) return;
- CallInst *LCall = cast<CallInst>(&*I);
- InvokeInst *RInvoke = cast<InvokeInst>(RTerm);
+ const CallInst *LCall = cast<CallInst>(&*I);
+ const InvokeInst *RInvoke = cast<InvokeInst>(RTerm);
if (!equivalentAsOperands(LCall->getCalledOperand(),
RInvoke->getCalledOperand()))
return;
tryUnify(LTerm->getSuccessor(0), RInvoke->getNormalDest());
} else if (isa<InvokeInst>(LTerm) && isa<BranchInst>(RTerm)) {
if (cast<BranchInst>(RTerm)->isConditional()) return;
- BasicBlock::iterator I = RTerm->getIterator();
+ BasicBlock::const_iterator I = RTerm->getIterator();
if (I == RStart->getParent()->begin()) return;
--I;
if (!isa<CallInst>(*I)) return;
- CallInst *RCall = cast<CallInst>(I);
- InvokeInst *LInvoke = cast<InvokeInst>(LTerm);
+ const CallInst *RCall = cast<CallInst>(I);
+ const InvokeInst *LInvoke = cast<InvokeInst>(LTerm);
if (!equivalentAsOperands(LInvoke->getCalledOperand(),
RCall->getCalledOperand()))
return;
tryUnify(LInvoke->getNormalDest(), RTerm->getSuccessor(0));
}
}
-
}
void DifferenceEngine::Oracle::anchor() { }
-void DifferenceEngine::diff(Function *L, Function *R) {
+void DifferenceEngine::diff(const Function *L, const Function *R) {
Context C(*this, L, R);
// FIXME: types
FunctionDifferenceEngine(*this).diff(L, R);
}
-void DifferenceEngine::diff(Module *L, Module *R) {
+void DifferenceEngine::diff(const Module *L, const Module *R) {
StringSet<> LNames;
- SmallVector<std::pair<Function*,Function*>, 20> Queue;
+ SmallVector<std::pair<const Function *, const Function *>, 20> Queue;
unsigned LeftAnonCount = 0;
unsigned RightAnonCount = 0;
- for (Module::iterator I = L->begin(), E = L->end(); I != E; ++I) {
- Function *LFn = &*I;
+ for (Module::const_iterator I = L->begin(), E = L->end(); I != E; ++I) {
+ const Function *LFn = &*I;
StringRef Name = LFn->getName();
if (Name.empty()) {
++LeftAnonCount;
logf("function %l exists only in left module") << LFn;
}
- for (Module::iterator I = R->begin(), E = R->end(); I != E; ++I) {
- Function *RFn = &*I;
+ for (Module::const_iterator I = R->begin(), E = R->end(); I != E; ++I) {
+ const Function *RFn = &*I;
StringRef Name = RFn->getName();
if (Name.empty()) {
++RightAnonCount;
logf("function %r exists only in right module") << RFn;
}
-
if (LeftAnonCount != 0 || RightAnonCount != 0) {
SmallString<32> Tmp;
logf(("not comparing " + Twine(LeftAnonCount) +
.toStringRef(Tmp));
}
- for (SmallVectorImpl<std::pair<Function*,Function*> >::iterator
- I = Queue.begin(), E = Queue.end(); I != E; ++I)
+ for (SmallVectorImpl<std::pair<const Function *, const Function *>>::iterator
+ I = Queue.begin(),
+ E = Queue.end();
+ I != E; ++I)
diff(I->first, I->second);
}
-bool DifferenceEngine::equivalentAsOperands(GlobalValue *L, GlobalValue *R) {
+bool DifferenceEngine::equivalentAsOperands(const GlobalValue *L,
+ const GlobalValue *R) {
if (globalValueOracle) return (*globalValueOracle)(L, R);
if (isa<GlobalVariable>(L) && isa<GlobalVariable>(R)) {
- GlobalVariable *GVL = cast<GlobalVariable>(L);
- GlobalVariable *GVR = cast<GlobalVariable>(R);
+ const GlobalVariable *GVL = cast<GlobalVariable>(L);
+ const GlobalVariable *GVR = cast<GlobalVariable>(R);
if (GVL->hasLocalLinkage() && GVL->hasUniqueInitializer() &&
GVR->hasLocalLinkage() && GVR->hasUniqueInitializer())
return GVL->getInitializer() == GVR->getInitializer();