blob: 0c1e30c987ea31288635fff4fee4695e65cd4e20 [file] [log] [blame]
John McCall3dd706b2010-07-29 07:53:27 +00001//===-- DifferenceEngine.cpp - Structural function/module comparison ------===//
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//
John McCall73b21b72010-07-29 18:08:23 +000010// This header defines the implementation of the LLVM difference
11// engine, which structurally compares global values within a module.
John McCall3dd706b2010-07-29 07:53:27 +000012//
13//===----------------------------------------------------------------------===//
14
John McCall3dd706b2010-07-29 07:53:27 +000015#include "DifferenceEngine.h"
16
Jay Foad562b84b2011-04-11 09:35:34 +000017#include "llvm/Constants.h"
John McCall73b21b72010-07-29 18:08:23 +000018#include "llvm/Function.h"
19#include "llvm/Instructions.h"
20#include "llvm/Module.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/DenseSet.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/StringSet.h"
26#include "llvm/Support/CallSite.h"
27#include "llvm/Support/CFG.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/Support/type_traits.h"
31
32#include <utility>
33
John McCall3dd706b2010-07-29 07:53:27 +000034using namespace llvm;
35
36namespace {
37
38/// A priority queue, implemented as a heap.
39template <class T, class Sorter, unsigned InlineCapacity>
40class PriorityQueue {
41 Sorter Precedes;
42 llvm::SmallVector<T, InlineCapacity> Storage;
43
44public:
45 PriorityQueue(const Sorter &Precedes) : Precedes(Precedes) {}
46
47 /// Checks whether the heap is empty.
48 bool empty() const { return Storage.empty(); }
49
50 /// Insert a new value on the heap.
51 void insert(const T &V) {
52 unsigned Index = Storage.size();
53 Storage.push_back(V);
54 if (Index == 0) return;
55
56 T *data = Storage.data();
57 while (true) {
58 unsigned Target = (Index + 1) / 2 - 1;
59 if (!Precedes(data[Index], data[Target])) return;
60 std::swap(data[Index], data[Target]);
61 if (Target == 0) return;
62 Index = Target;
63 }
64 }
65
66 /// Remove the minimum value in the heap. Only valid on a non-empty heap.
67 T remove_min() {
68 assert(!empty());
69 T tmp = Storage[0];
70
71 unsigned NewSize = Storage.size() - 1;
72 if (NewSize) {
73 // Move the slot at the end to the beginning.
74 if (isPodLike<T>::value)
75 Storage[0] = Storage[NewSize];
76 else
77 std::swap(Storage[0], Storage[NewSize]);
78
79 // Bubble the root up as necessary.
80 unsigned Index = 0;
81 while (true) {
82 // With a 1-based index, the children would be Index*2 and Index*2+1.
83 unsigned R = (Index + 1) * 2;
84 unsigned L = R - 1;
85
86 // If R is out of bounds, we're done after this in any case.
87 if (R >= NewSize) {
88 // If L is also out of bounds, we're done immediately.
89 if (L >= NewSize) break;
90
91 // Otherwise, test whether we should swap L and Index.
92 if (Precedes(Storage[L], Storage[Index]))
93 std::swap(Storage[L], Storage[Index]);
94 break;
95 }
96
97 // Otherwise, we need to compare with the smaller of L and R.
98 // Prefer R because it's closer to the end of the array.
99 unsigned IndexToTest = (Precedes(Storage[L], Storage[R]) ? L : R);
100
101 // If Index is >= the min of L and R, then heap ordering is restored.
102 if (!Precedes(Storage[IndexToTest], Storage[Index]))
103 break;
104
105 // Otherwise, keep bubbling up.
106 std::swap(Storage[IndexToTest], Storage[Index]);
107 Index = IndexToTest;
108 }
109 }
110 Storage.pop_back();
111
112 return tmp;
113 }
114};
115
116/// A function-scope difference engine.
117class FunctionDifferenceEngine {
118 DifferenceEngine &Engine;
119
120 /// The current mapping from old local values to new local values.
121 DenseMap<Value*, Value*> Values;
122
123 /// The current mapping from old blocks to new blocks.
124 DenseMap<BasicBlock*, BasicBlock*> Blocks;
125
John McCalldfb44ac2010-07-29 08:53:59 +0000126 DenseSet<std::pair<Value*, Value*> > TentativeValues;
John McCall3dd706b2010-07-29 07:53:27 +0000127
128 unsigned getUnprocPredCount(BasicBlock *Block) const {
129 unsigned Count = 0;
130 for (pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; ++I)
131 if (!Blocks.count(*I)) Count++;
132 return Count;
133 }
134
135 typedef std::pair<BasicBlock*, BasicBlock*> BlockPair;
136
137 /// A type which sorts a priority queue by the number of unprocessed
138 /// predecessor blocks it has remaining.
139 ///
140 /// This is actually really expensive to calculate.
141 struct QueueSorter {
142 const FunctionDifferenceEngine &fde;
143 explicit QueueSorter(const FunctionDifferenceEngine &fde) : fde(fde) {}
144
145 bool operator()(const BlockPair &Old, const BlockPair &New) {
146 return fde.getUnprocPredCount(Old.first)
147 < fde.getUnprocPredCount(New.first);
148 }
149 };
150
151 /// A queue of unified blocks to process.
152 PriorityQueue<BlockPair, QueueSorter, 20> Queue;
153
154 /// Try to unify the given two blocks. Enqueues them for processing
155 /// if they haven't already been processed.
156 ///
157 /// Returns true if there was a problem unifying them.
158 bool tryUnify(BasicBlock *L, BasicBlock *R) {
159 BasicBlock *&Ref = Blocks[L];
160
161 if (Ref) {
162 if (Ref == R) return false;
163
John McCall62dc1f32010-07-29 08:14:41 +0000164 Engine.logf("successor %l cannot be equivalent to %r; "
165 "it's already equivalent to %r")
John McCall3dd706b2010-07-29 07:53:27 +0000166 << L << R << Ref;
167 return true;
168 }
169
170 Ref = R;
171 Queue.insert(BlockPair(L, R));
172 return false;
173 }
John McCall82bd5ea2010-07-29 09:20:34 +0000174
175 /// Unifies two instructions, given that they're known not to have
176 /// structural differences.
177 void unify(Instruction *L, Instruction *R) {
178 DifferenceEngine::Context C(Engine, L, R);
179
180 bool Result = diff(L, R, true, true);
181 assert(!Result && "structural differences second time around?");
182 (void) Result;
183 if (!L->use_empty())
184 Values[L] = R;
185 }
John McCall3dd706b2010-07-29 07:53:27 +0000186
187 void processQueue() {
188 while (!Queue.empty()) {
189 BlockPair Pair = Queue.remove_min();
190 diff(Pair.first, Pair.second);
191 }
192 }
193
194 void diff(BasicBlock *L, BasicBlock *R) {
195 DifferenceEngine::Context C(Engine, L, R);
196
197 BasicBlock::iterator LI = L->begin(), LE = L->end();
Duncan Sands1f6a3292011-08-12 14:54:45 +0000198 BasicBlock::iterator RI = R->begin();
John McCall3dd706b2010-07-29 07:53:27 +0000199
John McCalldfb44ac2010-07-29 08:53:59 +0000200 llvm::SmallVector<std::pair<Instruction*,Instruction*>, 20> TentativePairs;
201
John McCall3dd706b2010-07-29 07:53:27 +0000202 do {
Duncan Sands1f6a3292011-08-12 14:54:45 +0000203 assert(LI != LE && RI != R->end());
John McCall3dd706b2010-07-29 07:53:27 +0000204 Instruction *LeftI = &*LI, *RightI = &*RI;
205
206 // If the instructions differ, start the more sophisticated diff
John McCalldfb44ac2010-07-29 08:53:59 +0000207 // algorithm at the start of the block.
John McCalle2921432010-07-29 09:04:45 +0000208 if (diff(LeftI, RightI, false, false)) {
John McCalldfb44ac2010-07-29 08:53:59 +0000209 TentativeValues.clear();
210 return runBlockDiff(L->begin(), R->begin());
211 }
John McCall3dd706b2010-07-29 07:53:27 +0000212
John McCalldfb44ac2010-07-29 08:53:59 +0000213 // Otherwise, tentatively unify them.
John McCall3dd706b2010-07-29 07:53:27 +0000214 if (!LeftI->use_empty())
John McCalldfb44ac2010-07-29 08:53:59 +0000215 TentativeValues.insert(std::make_pair(LeftI, RightI));
John McCall3dd706b2010-07-29 07:53:27 +0000216
217 ++LI, ++RI;
218 } while (LI != LE); // This is sufficient: we can't get equality of
219 // terminators if there are residual instructions.
John McCalldfb44ac2010-07-29 08:53:59 +0000220
John McCall82bd5ea2010-07-29 09:20:34 +0000221 // Unify everything in the block, non-tentatively this time.
John McCalldfb44ac2010-07-29 08:53:59 +0000222 TentativeValues.clear();
John McCall82bd5ea2010-07-29 09:20:34 +0000223 for (LI = L->begin(), RI = R->begin(); LI != LE; ++LI, ++RI)
224 unify(&*LI, &*RI);
John McCall3dd706b2010-07-29 07:53:27 +0000225 }
226
227 bool matchForBlockDiff(Instruction *L, Instruction *R);
228 void runBlockDiff(BasicBlock::iterator LI, BasicBlock::iterator RI);
229
230 bool diffCallSites(CallSite L, CallSite R, bool Complain) {
231 // FIXME: call attributes
232 if (!equivalentAsOperands(L.getCalledValue(), R.getCalledValue())) {
233 if (Complain) Engine.log("called functions differ");
234 return true;
235 }
236 if (L.arg_size() != R.arg_size()) {
237 if (Complain) Engine.log("argument counts differ");
238 return true;
239 }
240 for (unsigned I = 0, E = L.arg_size(); I != E; ++I)
241 if (!equivalentAsOperands(L.getArgument(I), R.getArgument(I))) {
242 if (Complain)
John McCall62dc1f32010-07-29 08:14:41 +0000243 Engine.logf("arguments %l and %r differ")
John McCall3dd706b2010-07-29 07:53:27 +0000244 << L.getArgument(I) << R.getArgument(I);
245 return true;
246 }
247 return false;
248 }
249
250 bool diff(Instruction *L, Instruction *R, bool Complain, bool TryUnify) {
251 // FIXME: metadata (if Complain is set)
252
253 // Different opcodes always imply different operations.
254 if (L->getOpcode() != R->getOpcode()) {
255 if (Complain) Engine.log("different instruction types");
256 return true;
257 }
258
259 if (isa<CmpInst>(L)) {
260 if (cast<CmpInst>(L)->getPredicate()
261 != cast<CmpInst>(R)->getPredicate()) {
262 if (Complain) Engine.log("different predicates");
263 return true;
264 }
265 } else if (isa<CallInst>(L)) {
266 return diffCallSites(CallSite(L), CallSite(R), Complain);
267 } else if (isa<PHINode>(L)) {
268 // FIXME: implement.
269
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000270 // This is really weird; type uniquing is broken?
John McCall3dd706b2010-07-29 07:53:27 +0000271 if (L->getType() != R->getType()) {
272 if (!L->getType()->isPointerTy() || !R->getType()->isPointerTy()) {
273 if (Complain) Engine.log("different phi types");
274 return true;
275 }
276 }
277 return false;
278
279 // Terminators.
280 } else if (isa<InvokeInst>(L)) {
281 InvokeInst *LI = cast<InvokeInst>(L);
282 InvokeInst *RI = cast<InvokeInst>(R);
283 if (diffCallSites(CallSite(LI), CallSite(RI), Complain))
284 return true;
285
286 if (TryUnify) {
287 tryUnify(LI->getNormalDest(), RI->getNormalDest());
288 tryUnify(LI->getUnwindDest(), RI->getUnwindDest());
289 }
290 return false;
291
292 } else if (isa<BranchInst>(L)) {
293 BranchInst *LI = cast<BranchInst>(L);
294 BranchInst *RI = cast<BranchInst>(R);
295 if (LI->isConditional() != RI->isConditional()) {
296 if (Complain) Engine.log("branch conditionality differs");
297 return true;
298 }
299
300 if (LI->isConditional()) {
301 if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) {
302 if (Complain) Engine.log("branch conditions differ");
303 return true;
304 }
305 if (TryUnify) tryUnify(LI->getSuccessor(1), RI->getSuccessor(1));
306 }
307 if (TryUnify) tryUnify(LI->getSuccessor(0), RI->getSuccessor(0));
308 return false;
309
310 } else if (isa<SwitchInst>(L)) {
311 SwitchInst *LI = cast<SwitchInst>(L);
312 SwitchInst *RI = cast<SwitchInst>(R);
313 if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) {
314 if (Complain) Engine.log("switch conditions differ");
315 return true;
316 }
317 if (TryUnify) tryUnify(LI->getDefaultDest(), RI->getDefaultDest());
318
319 bool Difference = false;
320
Stepan Dyatkovskiyf4c261b2012-05-19 13:14:30 +0000321 DenseMap<Constant*, BasicBlock*> LCases;
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +0000322
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +0000323 for (SwitchInst::CaseIt I = LI->case_begin(), E = LI->case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +0000324 I != E; ++I)
Stepan Dyatkovskiyf4c261b2012-05-19 13:14:30 +0000325 LCases[I.getCaseValueEx()] = I.getCaseSuccessor();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +0000326
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +0000327 for (SwitchInst::CaseIt I = RI->case_begin(), E = RI->case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +0000328 I != E; ++I) {
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +0000329 IntegersSubset CaseValue = I.getCaseValueEx();
John McCall3dd706b2010-07-29 07:53:27 +0000330 BasicBlock *LCase = LCases[CaseValue];
331 if (LCase) {
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +0000332 if (TryUnify) tryUnify(LCase, I.getCaseSuccessor());
John McCall3dd706b2010-07-29 07:53:27 +0000333 LCases.erase(CaseValue);
John McCallbd3c5ec2011-11-08 06:53:04 +0000334 } else if (Complain || !Difference) {
John McCall3dd706b2010-07-29 07:53:27 +0000335 if (Complain)
John McCall62dc1f32010-07-29 08:14:41 +0000336 Engine.logf("right switch has extra case %r") << CaseValue;
John McCall3dd706b2010-07-29 07:53:27 +0000337 Difference = true;
338 }
339 }
340 if (!Difference)
Stepan Dyatkovskiyf4c261b2012-05-19 13:14:30 +0000341 for (DenseMap<Constant*, BasicBlock*>::iterator
John McCall3dd706b2010-07-29 07:53:27 +0000342 I = LCases.begin(), E = LCases.end(); I != E; ++I) {
343 if (Complain)
John McCall62dc1f32010-07-29 08:14:41 +0000344 Engine.logf("left switch has extra case %l") << I->first;
John McCall3dd706b2010-07-29 07:53:27 +0000345 Difference = true;
346 }
347 return Difference;
348 } else if (isa<UnreachableInst>(L)) {
349 return false;
350 }
351
352 if (L->getNumOperands() != R->getNumOperands()) {
353 if (Complain) Engine.log("instructions have different operand counts");
354 return true;
355 }
356
357 for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) {
358 Value *LO = L->getOperand(I), *RO = R->getOperand(I);
359 if (!equivalentAsOperands(LO, RO)) {
John McCall62dc1f32010-07-29 08:14:41 +0000360 if (Complain) Engine.logf("operands %l and %r differ") << LO << RO;
John McCall3dd706b2010-07-29 07:53:27 +0000361 return true;
362 }
363 }
364
365 return false;
366 }
367
368 bool equivalentAsOperands(Constant *L, Constant *R) {
369 // Use equality as a preliminary filter.
370 if (L == R)
371 return true;
372
373 if (L->getValueID() != R->getValueID())
374 return false;
375
376 // Ask the engine about global values.
377 if (isa<GlobalValue>(L))
378 return Engine.equivalentAsOperands(cast<GlobalValue>(L),
379 cast<GlobalValue>(R));
380
381 // Compare constant expressions structurally.
382 if (isa<ConstantExpr>(L))
383 return equivalentAsOperands(cast<ConstantExpr>(L),
384 cast<ConstantExpr>(R));
385
386 // Nulls of the "same type" don't always actually have the same
387 // type; I don't know why. Just white-list them.
388 if (isa<ConstantPointerNull>(L))
389 return true;
390
391 // Block addresses only match if we've already encountered the
392 // block. FIXME: tentative matches?
393 if (isa<BlockAddress>(L))
394 return Blocks[cast<BlockAddress>(L)->getBasicBlock()]
395 == cast<BlockAddress>(R)->getBasicBlock();
396
397 return false;
398 }
399
400 bool equivalentAsOperands(ConstantExpr *L, ConstantExpr *R) {
401 if (L == R)
402 return true;
403 if (L->getOpcode() != R->getOpcode())
404 return false;
405
406 switch (L->getOpcode()) {
407 case Instruction::ICmp:
408 case Instruction::FCmp:
409 if (L->getPredicate() != R->getPredicate())
410 return false;
411 break;
412
413 case Instruction::GetElementPtr:
414 // FIXME: inbounds?
415 break;
416
417 default:
418 break;
419 }
420
421 if (L->getNumOperands() != R->getNumOperands())
422 return false;
423
424 for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I)
425 if (!equivalentAsOperands(L->getOperand(I), R->getOperand(I)))
426 return false;
427
428 return true;
429 }
430
431 bool equivalentAsOperands(Value *L, Value *R) {
432 // Fall out if the values have different kind.
433 // This possibly shouldn't take priority over oracles.
434 if (L->getValueID() != R->getValueID())
435 return false;
436
437 // Value subtypes: Argument, Constant, Instruction, BasicBlock,
438 // InlineAsm, MDNode, MDString, PseudoSourceValue
439
440 if (isa<Constant>(L))
441 return equivalentAsOperands(cast<Constant>(L), cast<Constant>(R));
442
443 if (isa<Instruction>(L))
John McCalldfb44ac2010-07-29 08:53:59 +0000444 return Values[L] == R || TentativeValues.count(std::make_pair(L, R));
John McCall3dd706b2010-07-29 07:53:27 +0000445
446 if (isa<Argument>(L))
447 return Values[L] == R;
448
449 if (isa<BasicBlock>(L))
450 return Blocks[cast<BasicBlock>(L)] != R;
451
452 // Pretend everything else is identical.
453 return true;
454 }
455
456 // Avoid a gcc warning about accessing 'this' in an initializer.
457 FunctionDifferenceEngine *this_() { return this; }
458
459public:
460 FunctionDifferenceEngine(DifferenceEngine &Engine) :
461 Engine(Engine), Queue(QueueSorter(*this_())) {}
462
463 void diff(Function *L, Function *R) {
464 if (L->arg_size() != R->arg_size())
465 Engine.log("different argument counts");
466
467 // Map the arguments.
468 for (Function::arg_iterator
469 LI = L->arg_begin(), LE = L->arg_end(),
470 RI = R->arg_begin(), RE = R->arg_end();
471 LI != LE && RI != RE; ++LI, ++RI)
472 Values[&*LI] = &*RI;
473
474 tryUnify(&*L->begin(), &*R->begin());
475 processQueue();
476 }
477};
478
479struct DiffEntry {
480 DiffEntry() : Cost(0) {}
481
482 unsigned Cost;
483 llvm::SmallVector<char, 8> Path; // actually of DifferenceEngine::DiffChange
484};
485
486bool FunctionDifferenceEngine::matchForBlockDiff(Instruction *L,
487 Instruction *R) {
488 return !diff(L, R, false, false);
489}
490
491void FunctionDifferenceEngine::runBlockDiff(BasicBlock::iterator LStart,
492 BasicBlock::iterator RStart) {
493 BasicBlock::iterator LE = LStart->getParent()->end();
494 BasicBlock::iterator RE = RStart->getParent()->end();
495
496 unsigned NL = std::distance(LStart, LE);
497
498 SmallVector<DiffEntry, 20> Paths1(NL+1);
499 SmallVector<DiffEntry, 20> Paths2(NL+1);
500
501 DiffEntry *Cur = Paths1.data();
502 DiffEntry *Next = Paths2.data();
503
John McCalldfb44ac2010-07-29 08:53:59 +0000504 const unsigned LeftCost = 2;
505 const unsigned RightCost = 2;
506 const unsigned MatchCost = 0;
507
508 assert(TentativeValues.empty());
John McCall3dd706b2010-07-29 07:53:27 +0000509
510 // Initialize the first column.
511 for (unsigned I = 0; I != NL+1; ++I) {
John McCalldfb44ac2010-07-29 08:53:59 +0000512 Cur[I].Cost = I * LeftCost;
John McCall3dd706b2010-07-29 07:53:27 +0000513 for (unsigned J = 0; J != I; ++J)
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000514 Cur[I].Path.push_back(DC_left);
John McCall3dd706b2010-07-29 07:53:27 +0000515 }
516
517 for (BasicBlock::iterator RI = RStart; RI != RE; ++RI) {
518 // Initialize the first row.
519 Next[0] = Cur[0];
John McCalldfb44ac2010-07-29 08:53:59 +0000520 Next[0].Cost += RightCost;
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000521 Next[0].Path.push_back(DC_right);
John McCall3dd706b2010-07-29 07:53:27 +0000522
523 unsigned Index = 1;
524 for (BasicBlock::iterator LI = LStart; LI != LE; ++LI, ++Index) {
525 if (matchForBlockDiff(&*LI, &*RI)) {
526 Next[Index] = Cur[Index-1];
John McCalldfb44ac2010-07-29 08:53:59 +0000527 Next[Index].Cost += MatchCost;
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000528 Next[Index].Path.push_back(DC_match);
John McCalldfb44ac2010-07-29 08:53:59 +0000529 TentativeValues.insert(std::make_pair(&*LI, &*RI));
John McCall3dd706b2010-07-29 07:53:27 +0000530 } else if (Next[Index-1].Cost <= Cur[Index].Cost) {
531 Next[Index] = Next[Index-1];
John McCalldfb44ac2010-07-29 08:53:59 +0000532 Next[Index].Cost += LeftCost;
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000533 Next[Index].Path.push_back(DC_left);
John McCall3dd706b2010-07-29 07:53:27 +0000534 } else {
535 Next[Index] = Cur[Index];
John McCalldfb44ac2010-07-29 08:53:59 +0000536 Next[Index].Cost += RightCost;
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000537 Next[Index].Path.push_back(DC_right);
John McCall3dd706b2010-07-29 07:53:27 +0000538 }
539 }
540
541 std::swap(Cur, Next);
542 }
543
John McCall82bd5ea2010-07-29 09:20:34 +0000544 // We don't need the tentative values anymore; everything from here
545 // on out should be non-tentative.
546 TentativeValues.clear();
547
John McCall3dd706b2010-07-29 07:53:27 +0000548 SmallVectorImpl<char> &Path = Cur[NL].Path;
549 BasicBlock::iterator LI = LStart, RI = RStart;
550
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000551 DiffLogBuilder Diff(Engine.getConsumer());
John McCall3dd706b2010-07-29 07:53:27 +0000552
553 // Drop trailing matches.
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000554 while (Path.back() == DC_match)
John McCall3dd706b2010-07-29 07:53:27 +0000555 Path.pop_back();
556
John McCalldfb44ac2010-07-29 08:53:59 +0000557 // Skip leading matches.
558 SmallVectorImpl<char>::iterator
559 PI = Path.begin(), PE = Path.end();
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000560 while (PI != PE && *PI == DC_match) {
John McCall82bd5ea2010-07-29 09:20:34 +0000561 unify(&*LI, &*RI);
John McCalldfb44ac2010-07-29 08:53:59 +0000562 ++PI, ++LI, ++RI;
John McCall82bd5ea2010-07-29 09:20:34 +0000563 }
John McCalldfb44ac2010-07-29 08:53:59 +0000564
565 for (; PI != PE; ++PI) {
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000566 switch (static_cast<DiffChange>(*PI)) {
567 case DC_match:
John McCall3dd706b2010-07-29 07:53:27 +0000568 assert(LI != LE && RI != RE);
569 {
570 Instruction *L = &*LI, *R = &*RI;
John McCall82bd5ea2010-07-29 09:20:34 +0000571 unify(L, R);
John McCall3dd706b2010-07-29 07:53:27 +0000572 Diff.addMatch(L, R);
573 }
574 ++LI; ++RI;
575 break;
576
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000577 case DC_left:
John McCall3dd706b2010-07-29 07:53:27 +0000578 assert(LI != LE);
579 Diff.addLeft(&*LI);
580 ++LI;
581 break;
582
Renato Golin7d4fc4f2011-03-14 22:22:46 +0000583 case DC_right:
John McCall3dd706b2010-07-29 07:53:27 +0000584 assert(RI != RE);
585 Diff.addRight(&*RI);
586 ++RI;
587 break;
588 }
589 }
590
John McCall82bd5ea2010-07-29 09:20:34 +0000591 // Finishing unifying and complaining about the tails of the block,
592 // which should be matches all the way through.
593 while (LI != LE) {
594 assert(RI != RE);
595 unify(&*LI, &*RI);
596 ++LI, ++RI;
597 }
John McCallb82b4332010-08-24 09:16:51 +0000598
599 // If the terminators have different kinds, but one is an invoke and the
600 // other is an unconditional branch immediately following a call, unify
601 // the results and the destinations.
602 TerminatorInst *LTerm = LStart->getParent()->getTerminator();
603 TerminatorInst *RTerm = RStart->getParent()->getTerminator();
604 if (isa<BranchInst>(LTerm) && isa<InvokeInst>(RTerm)) {
605 if (cast<BranchInst>(LTerm)->isConditional()) return;
606 BasicBlock::iterator I = LTerm;
607 if (I == LStart->getParent()->begin()) return;
608 --I;
609 if (!isa<CallInst>(*I)) return;
610 CallInst *LCall = cast<CallInst>(&*I);
611 InvokeInst *RInvoke = cast<InvokeInst>(RTerm);
612 if (!equivalentAsOperands(LCall->getCalledValue(), RInvoke->getCalledValue()))
613 return;
614 if (!LCall->use_empty())
615 Values[LCall] = RInvoke;
616 tryUnify(LTerm->getSuccessor(0), RInvoke->getNormalDest());
617 } else if (isa<InvokeInst>(LTerm) && isa<BranchInst>(RTerm)) {
618 if (cast<BranchInst>(RTerm)->isConditional()) return;
619 BasicBlock::iterator I = RTerm;
620 if (I == RStart->getParent()->begin()) return;
621 --I;
622 if (!isa<CallInst>(*I)) return;
623 CallInst *RCall = cast<CallInst>(I);
624 InvokeInst *LInvoke = cast<InvokeInst>(LTerm);
625 if (!equivalentAsOperands(LInvoke->getCalledValue(), RCall->getCalledValue()))
626 return;
627 if (!LInvoke->use_empty())
628 Values[LInvoke] = RCall;
629 tryUnify(LInvoke->getNormalDest(), RTerm->getSuccessor(0));
630 }
John McCall3dd706b2010-07-29 07:53:27 +0000631}
632
633}
634
David Blaikie2d24e2a2011-12-20 02:50:00 +0000635void DifferenceEngine::Oracle::anchor() { }
636
John McCall3dd706b2010-07-29 07:53:27 +0000637void DifferenceEngine::diff(Function *L, Function *R) {
638 Context C(*this, L, R);
639
640 // FIXME: types
641 // FIXME: attributes and CC
642 // FIXME: parameter attributes
643
644 // If both are declarations, we're done.
645 if (L->empty() && R->empty())
646 return;
647 else if (L->empty())
648 log("left function is declaration, right function is definition");
649 else if (R->empty())
650 log("right function is declaration, left function is definition");
651 else
652 FunctionDifferenceEngine(*this).diff(L, R);
653}
654
655void DifferenceEngine::diff(Module *L, Module *R) {
656 StringSet<> LNames;
657 SmallVector<std::pair<Function*,Function*>, 20> Queue;
658
659 for (Module::iterator I = L->begin(), E = L->end(); I != E; ++I) {
660 Function *LFn = &*I;
661 LNames.insert(LFn->getName());
662
663 if (Function *RFn = R->getFunction(LFn->getName()))
664 Queue.push_back(std::make_pair(LFn, RFn));
665 else
John McCall62dc1f32010-07-29 08:14:41 +0000666 logf("function %l exists only in left module") << LFn;
John McCall3dd706b2010-07-29 07:53:27 +0000667 }
668
669 for (Module::iterator I = R->begin(), E = R->end(); I != E; ++I) {
670 Function *RFn = &*I;
671 if (!LNames.count(RFn->getName()))
John McCall62dc1f32010-07-29 08:14:41 +0000672 logf("function %r exists only in right module") << RFn;
John McCall3dd706b2010-07-29 07:53:27 +0000673 }
674
675 for (SmallVectorImpl<std::pair<Function*,Function*> >::iterator
676 I = Queue.begin(), E = Queue.end(); I != E; ++I)
677 diff(I->first, I->second);
678}
679
680bool DifferenceEngine::equivalentAsOperands(GlobalValue *L, GlobalValue *R) {
681 if (globalValueOracle) return (*globalValueOracle)(L, R);
682 return L->getName() == R->getName();
683}