blob: 55b1a362a831a48e2bb1e9907f42be24f38c710b [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//
10// This header defines the interface to the LLVM difference engine,
11// which structurally compares functions within a module.
12//
13//===----------------------------------------------------------------------===//
14
15#include <utility>
16
17#include <llvm/ADT/DenseMap.h>
18#include <llvm/ADT/DenseSet.h>
19#include <llvm/ADT/SmallVector.h>
20#include <llvm/ADT/StringRef.h>
21#include <llvm/ADT/StringSet.h>
22
23#include <llvm/Module.h>
24#include <llvm/Function.h>
25#include <llvm/Instructions.h>
26#include <llvm/Support/CFG.h>
27
28#include <llvm/Support/raw_ostream.h>
29#include <llvm/Support/type_traits.h>
30#include <llvm/Support/ErrorHandling.h>
31#include <llvm/Support/CallSite.h>
32
33#include "DifferenceEngine.h"
34
35using namespace llvm;
36
37namespace {
38
39/// A priority queue, implemented as a heap.
40template <class T, class Sorter, unsigned InlineCapacity>
41class PriorityQueue {
42 Sorter Precedes;
43 llvm::SmallVector<T, InlineCapacity> Storage;
44
45public:
46 PriorityQueue(const Sorter &Precedes) : Precedes(Precedes) {}
47
48 /// Checks whether the heap is empty.
49 bool empty() const { return Storage.empty(); }
50
51 /// Insert a new value on the heap.
52 void insert(const T &V) {
53 unsigned Index = Storage.size();
54 Storage.push_back(V);
55 if (Index == 0) return;
56
57 T *data = Storage.data();
58 while (true) {
59 unsigned Target = (Index + 1) / 2 - 1;
60 if (!Precedes(data[Index], data[Target])) return;
61 std::swap(data[Index], data[Target]);
62 if (Target == 0) return;
63 Index = Target;
64 }
65 }
66
67 /// Remove the minimum value in the heap. Only valid on a non-empty heap.
68 T remove_min() {
69 assert(!empty());
70 T tmp = Storage[0];
71
72 unsigned NewSize = Storage.size() - 1;
73 if (NewSize) {
74 // Move the slot at the end to the beginning.
75 if (isPodLike<T>::value)
76 Storage[0] = Storage[NewSize];
77 else
78 std::swap(Storage[0], Storage[NewSize]);
79
80 // Bubble the root up as necessary.
81 unsigned Index = 0;
82 while (true) {
83 // With a 1-based index, the children would be Index*2 and Index*2+1.
84 unsigned R = (Index + 1) * 2;
85 unsigned L = R - 1;
86
87 // If R is out of bounds, we're done after this in any case.
88 if (R >= NewSize) {
89 // If L is also out of bounds, we're done immediately.
90 if (L >= NewSize) break;
91
92 // Otherwise, test whether we should swap L and Index.
93 if (Precedes(Storage[L], Storage[Index]))
94 std::swap(Storage[L], Storage[Index]);
95 break;
96 }
97
98 // Otherwise, we need to compare with the smaller of L and R.
99 // Prefer R because it's closer to the end of the array.
100 unsigned IndexToTest = (Precedes(Storage[L], Storage[R]) ? L : R);
101
102 // If Index is >= the min of L and R, then heap ordering is restored.
103 if (!Precedes(Storage[IndexToTest], Storage[Index]))
104 break;
105
106 // Otherwise, keep bubbling up.
107 std::swap(Storage[IndexToTest], Storage[Index]);
108 Index = IndexToTest;
109 }
110 }
111 Storage.pop_back();
112
113 return tmp;
114 }
115};
116
117/// A function-scope difference engine.
118class FunctionDifferenceEngine {
119 DifferenceEngine &Engine;
120
121 /// The current mapping from old local values to new local values.
122 DenseMap<Value*, Value*> Values;
123
124 /// The current mapping from old blocks to new blocks.
125 DenseMap<BasicBlock*, BasicBlock*> Blocks;
126
John McCalldfb44ac2010-07-29 08:53:59 +0000127 DenseSet<std::pair<Value*, Value*> > TentativeValues;
John McCall3dd706b2010-07-29 07:53:27 +0000128
129 unsigned getUnprocPredCount(BasicBlock *Block) const {
130 unsigned Count = 0;
131 for (pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; ++I)
132 if (!Blocks.count(*I)) Count++;
133 return Count;
134 }
135
136 typedef std::pair<BasicBlock*, BasicBlock*> BlockPair;
137
138 /// A type which sorts a priority queue by the number of unprocessed
139 /// predecessor blocks it has remaining.
140 ///
141 /// This is actually really expensive to calculate.
142 struct QueueSorter {
143 const FunctionDifferenceEngine &fde;
144 explicit QueueSorter(const FunctionDifferenceEngine &fde) : fde(fde) {}
145
146 bool operator()(const BlockPair &Old, const BlockPair &New) {
147 return fde.getUnprocPredCount(Old.first)
148 < fde.getUnprocPredCount(New.first);
149 }
150 };
151
152 /// A queue of unified blocks to process.
153 PriorityQueue<BlockPair, QueueSorter, 20> Queue;
154
155 /// Try to unify the given two blocks. Enqueues them for processing
156 /// if they haven't already been processed.
157 ///
158 /// Returns true if there was a problem unifying them.
159 bool tryUnify(BasicBlock *L, BasicBlock *R) {
160 BasicBlock *&Ref = Blocks[L];
161
162 if (Ref) {
163 if (Ref == R) return false;
164
John McCall62dc1f32010-07-29 08:14:41 +0000165 Engine.logf("successor %l cannot be equivalent to %r; "
166 "it's already equivalent to %r")
John McCall3dd706b2010-07-29 07:53:27 +0000167 << L << R << Ref;
168 return true;
169 }
170
171 Ref = R;
172 Queue.insert(BlockPair(L, R));
173 return false;
174 }
John McCall82bd5ea2010-07-29 09:20:34 +0000175
176 /// Unifies two instructions, given that they're known not to have
177 /// structural differences.
178 void unify(Instruction *L, Instruction *R) {
179 DifferenceEngine::Context C(Engine, L, R);
180
181 bool Result = diff(L, R, true, true);
182 assert(!Result && "structural differences second time around?");
183 (void) Result;
184 if (!L->use_empty())
185 Values[L] = R;
186 }
John McCall3dd706b2010-07-29 07:53:27 +0000187
188 void processQueue() {
189 while (!Queue.empty()) {
190 BlockPair Pair = Queue.remove_min();
191 diff(Pair.first, Pair.second);
192 }
193 }
194
195 void diff(BasicBlock *L, BasicBlock *R) {
196 DifferenceEngine::Context C(Engine, L, R);
197
198 BasicBlock::iterator LI = L->begin(), LE = L->end();
199 BasicBlock::iterator RI = R->begin(), RE = R->end();
200
John McCalldfb44ac2010-07-29 08:53:59 +0000201 llvm::SmallVector<std::pair<Instruction*,Instruction*>, 20> TentativePairs;
202
John McCall3dd706b2010-07-29 07:53:27 +0000203 do {
204 assert(LI != LE && RI != RE);
205 Instruction *LeftI = &*LI, *RightI = &*RI;
206
207 // If the instructions differ, start the more sophisticated diff
John McCalldfb44ac2010-07-29 08:53:59 +0000208 // algorithm at the start of the block.
John McCalle2921432010-07-29 09:04:45 +0000209 if (diff(LeftI, RightI, false, false)) {
John McCalldfb44ac2010-07-29 08:53:59 +0000210 TentativeValues.clear();
211 return runBlockDiff(L->begin(), R->begin());
212 }
John McCall3dd706b2010-07-29 07:53:27 +0000213
John McCalldfb44ac2010-07-29 08:53:59 +0000214 // Otherwise, tentatively unify them.
John McCall3dd706b2010-07-29 07:53:27 +0000215 if (!LeftI->use_empty())
John McCalldfb44ac2010-07-29 08:53:59 +0000216 TentativeValues.insert(std::make_pair(LeftI, RightI));
John McCall3dd706b2010-07-29 07:53:27 +0000217
218 ++LI, ++RI;
219 } while (LI != LE); // This is sufficient: we can't get equality of
220 // terminators if there are residual instructions.
John McCalldfb44ac2010-07-29 08:53:59 +0000221
John McCall82bd5ea2010-07-29 09:20:34 +0000222 // Unify everything in the block, non-tentatively this time.
John McCalldfb44ac2010-07-29 08:53:59 +0000223 TentativeValues.clear();
John McCall82bd5ea2010-07-29 09:20:34 +0000224 for (LI = L->begin(), RI = R->begin(); LI != LE; ++LI, ++RI)
225 unify(&*LI, &*RI);
John McCall3dd706b2010-07-29 07:53:27 +0000226 }
227
228 bool matchForBlockDiff(Instruction *L, Instruction *R);
229 void runBlockDiff(BasicBlock::iterator LI, BasicBlock::iterator RI);
230
231 bool diffCallSites(CallSite L, CallSite R, bool Complain) {
232 // FIXME: call attributes
233 if (!equivalentAsOperands(L.getCalledValue(), R.getCalledValue())) {
234 if (Complain) Engine.log("called functions differ");
235 return true;
236 }
237 if (L.arg_size() != R.arg_size()) {
238 if (Complain) Engine.log("argument counts differ");
239 return true;
240 }
241 for (unsigned I = 0, E = L.arg_size(); I != E; ++I)
242 if (!equivalentAsOperands(L.getArgument(I), R.getArgument(I))) {
243 if (Complain)
John McCall62dc1f32010-07-29 08:14:41 +0000244 Engine.logf("arguments %l and %r differ")
John McCall3dd706b2010-07-29 07:53:27 +0000245 << L.getArgument(I) << R.getArgument(I);
246 return true;
247 }
248 return false;
249 }
250
251 bool diff(Instruction *L, Instruction *R, bool Complain, bool TryUnify) {
252 // FIXME: metadata (if Complain is set)
253
254 // Different opcodes always imply different operations.
255 if (L->getOpcode() != R->getOpcode()) {
256 if (Complain) Engine.log("different instruction types");
257 return true;
258 }
259
260 if (isa<CmpInst>(L)) {
261 if (cast<CmpInst>(L)->getPredicate()
262 != cast<CmpInst>(R)->getPredicate()) {
263 if (Complain) Engine.log("different predicates");
264 return true;
265 }
266 } else if (isa<CallInst>(L)) {
267 return diffCallSites(CallSite(L), CallSite(R), Complain);
268 } else if (isa<PHINode>(L)) {
269 // FIXME: implement.
270
271 // This is really wierd; type uniquing is broken?
272 if (L->getType() != R->getType()) {
273 if (!L->getType()->isPointerTy() || !R->getType()->isPointerTy()) {
274 if (Complain) Engine.log("different phi types");
275 return true;
276 }
277 }
278 return false;
279
280 // Terminators.
281 } else if (isa<InvokeInst>(L)) {
282 InvokeInst *LI = cast<InvokeInst>(L);
283 InvokeInst *RI = cast<InvokeInst>(R);
284 if (diffCallSites(CallSite(LI), CallSite(RI), Complain))
285 return true;
286
287 if (TryUnify) {
288 tryUnify(LI->getNormalDest(), RI->getNormalDest());
289 tryUnify(LI->getUnwindDest(), RI->getUnwindDest());
290 }
291 return false;
292
293 } else if (isa<BranchInst>(L)) {
294 BranchInst *LI = cast<BranchInst>(L);
295 BranchInst *RI = cast<BranchInst>(R);
296 if (LI->isConditional() != RI->isConditional()) {
297 if (Complain) Engine.log("branch conditionality differs");
298 return true;
299 }
300
301 if (LI->isConditional()) {
302 if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) {
303 if (Complain) Engine.log("branch conditions differ");
304 return true;
305 }
306 if (TryUnify) tryUnify(LI->getSuccessor(1), RI->getSuccessor(1));
307 }
308 if (TryUnify) tryUnify(LI->getSuccessor(0), RI->getSuccessor(0));
309 return false;
310
311 } else if (isa<SwitchInst>(L)) {
312 SwitchInst *LI = cast<SwitchInst>(L);
313 SwitchInst *RI = cast<SwitchInst>(R);
314 if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) {
315 if (Complain) Engine.log("switch conditions differ");
316 return true;
317 }
318 if (TryUnify) tryUnify(LI->getDefaultDest(), RI->getDefaultDest());
319
320 bool Difference = false;
321
322 DenseMap<ConstantInt*,BasicBlock*> LCases;
323 for (unsigned I = 1, E = LI->getNumCases(); I != E; ++I)
324 LCases[LI->getCaseValue(I)] = LI->getSuccessor(I);
325 for (unsigned I = 1, E = RI->getNumCases(); I != E; ++I) {
326 ConstantInt *CaseValue = RI->getCaseValue(I);
327 BasicBlock *LCase = LCases[CaseValue];
328 if (LCase) {
329 if (TryUnify) tryUnify(LCase, RI->getSuccessor(I));
330 LCases.erase(CaseValue);
331 } else if (!Difference) {
332 if (Complain)
John McCall62dc1f32010-07-29 08:14:41 +0000333 Engine.logf("right switch has extra case %r") << CaseValue;
John McCall3dd706b2010-07-29 07:53:27 +0000334 Difference = true;
335 }
336 }
337 if (!Difference)
338 for (DenseMap<ConstantInt*,BasicBlock*>::iterator
339 I = LCases.begin(), E = LCases.end(); I != E; ++I) {
340 if (Complain)
John McCall62dc1f32010-07-29 08:14:41 +0000341 Engine.logf("left switch has extra case %l") << I->first;
John McCall3dd706b2010-07-29 07:53:27 +0000342 Difference = true;
343 }
344 return Difference;
345 } else if (isa<UnreachableInst>(L)) {
346 return false;
347 }
348
349 if (L->getNumOperands() != R->getNumOperands()) {
350 if (Complain) Engine.log("instructions have different operand counts");
351 return true;
352 }
353
354 for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) {
355 Value *LO = L->getOperand(I), *RO = R->getOperand(I);
356 if (!equivalentAsOperands(LO, RO)) {
John McCall62dc1f32010-07-29 08:14:41 +0000357 if (Complain) Engine.logf("operands %l and %r differ") << LO << RO;
John McCall3dd706b2010-07-29 07:53:27 +0000358 return true;
359 }
360 }
361
362 return false;
363 }
364
365 bool equivalentAsOperands(Constant *L, Constant *R) {
366 // Use equality as a preliminary filter.
367 if (L == R)
368 return true;
369
370 if (L->getValueID() != R->getValueID())
371 return false;
372
373 // Ask the engine about global values.
374 if (isa<GlobalValue>(L))
375 return Engine.equivalentAsOperands(cast<GlobalValue>(L),
376 cast<GlobalValue>(R));
377
378 // Compare constant expressions structurally.
379 if (isa<ConstantExpr>(L))
380 return equivalentAsOperands(cast<ConstantExpr>(L),
381 cast<ConstantExpr>(R));
382
383 // Nulls of the "same type" don't always actually have the same
384 // type; I don't know why. Just white-list them.
385 if (isa<ConstantPointerNull>(L))
386 return true;
387
388 // Block addresses only match if we've already encountered the
389 // block. FIXME: tentative matches?
390 if (isa<BlockAddress>(L))
391 return Blocks[cast<BlockAddress>(L)->getBasicBlock()]
392 == cast<BlockAddress>(R)->getBasicBlock();
393
394 return false;
395 }
396
397 bool equivalentAsOperands(ConstantExpr *L, ConstantExpr *R) {
398 if (L == R)
399 return true;
400 if (L->getOpcode() != R->getOpcode())
401 return false;
402
403 switch (L->getOpcode()) {
404 case Instruction::ICmp:
405 case Instruction::FCmp:
406 if (L->getPredicate() != R->getPredicate())
407 return false;
408 break;
409
410 case Instruction::GetElementPtr:
411 // FIXME: inbounds?
412 break;
413
414 default:
415 break;
416 }
417
418 if (L->getNumOperands() != R->getNumOperands())
419 return false;
420
421 for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I)
422 if (!equivalentAsOperands(L->getOperand(I), R->getOperand(I)))
423 return false;
424
425 return true;
426 }
427
428 bool equivalentAsOperands(Value *L, Value *R) {
429 // Fall out if the values have different kind.
430 // This possibly shouldn't take priority over oracles.
431 if (L->getValueID() != R->getValueID())
432 return false;
433
434 // Value subtypes: Argument, Constant, Instruction, BasicBlock,
435 // InlineAsm, MDNode, MDString, PseudoSourceValue
436
437 if (isa<Constant>(L))
438 return equivalentAsOperands(cast<Constant>(L), cast<Constant>(R));
439
440 if (isa<Instruction>(L))
John McCalldfb44ac2010-07-29 08:53:59 +0000441 return Values[L] == R || TentativeValues.count(std::make_pair(L, R));
John McCall3dd706b2010-07-29 07:53:27 +0000442
443 if (isa<Argument>(L))
444 return Values[L] == R;
445
446 if (isa<BasicBlock>(L))
447 return Blocks[cast<BasicBlock>(L)] != R;
448
449 // Pretend everything else is identical.
450 return true;
451 }
452
453 // Avoid a gcc warning about accessing 'this' in an initializer.
454 FunctionDifferenceEngine *this_() { return this; }
455
456public:
457 FunctionDifferenceEngine(DifferenceEngine &Engine) :
458 Engine(Engine), Queue(QueueSorter(*this_())) {}
459
460 void diff(Function *L, Function *R) {
461 if (L->arg_size() != R->arg_size())
462 Engine.log("different argument counts");
463
464 // Map the arguments.
465 for (Function::arg_iterator
466 LI = L->arg_begin(), LE = L->arg_end(),
467 RI = R->arg_begin(), RE = R->arg_end();
468 LI != LE && RI != RE; ++LI, ++RI)
469 Values[&*LI] = &*RI;
470
471 tryUnify(&*L->begin(), &*R->begin());
472 processQueue();
473 }
474};
475
476struct DiffEntry {
477 DiffEntry() : Cost(0) {}
478
479 unsigned Cost;
480 llvm::SmallVector<char, 8> Path; // actually of DifferenceEngine::DiffChange
481};
482
483bool FunctionDifferenceEngine::matchForBlockDiff(Instruction *L,
484 Instruction *R) {
485 return !diff(L, R, false, false);
486}
487
488void FunctionDifferenceEngine::runBlockDiff(BasicBlock::iterator LStart,
489 BasicBlock::iterator RStart) {
490 BasicBlock::iterator LE = LStart->getParent()->end();
491 BasicBlock::iterator RE = RStart->getParent()->end();
492
493 unsigned NL = std::distance(LStart, LE);
494
495 SmallVector<DiffEntry, 20> Paths1(NL+1);
496 SmallVector<DiffEntry, 20> Paths2(NL+1);
497
498 DiffEntry *Cur = Paths1.data();
499 DiffEntry *Next = Paths2.data();
500
John McCalldfb44ac2010-07-29 08:53:59 +0000501 const unsigned LeftCost = 2;
502 const unsigned RightCost = 2;
503 const unsigned MatchCost = 0;
504
505 assert(TentativeValues.empty());
John McCall3dd706b2010-07-29 07:53:27 +0000506
507 // Initialize the first column.
508 for (unsigned I = 0; I != NL+1; ++I) {
John McCalldfb44ac2010-07-29 08:53:59 +0000509 Cur[I].Cost = I * LeftCost;
John McCall3dd706b2010-07-29 07:53:27 +0000510 for (unsigned J = 0; J != I; ++J)
511 Cur[I].Path.push_back(DifferenceEngine::DC_left);
512 }
513
514 for (BasicBlock::iterator RI = RStart; RI != RE; ++RI) {
515 // Initialize the first row.
516 Next[0] = Cur[0];
John McCalldfb44ac2010-07-29 08:53:59 +0000517 Next[0].Cost += RightCost;
John McCall3dd706b2010-07-29 07:53:27 +0000518 Next[0].Path.push_back(DifferenceEngine::DC_right);
519
520 unsigned Index = 1;
521 for (BasicBlock::iterator LI = LStart; LI != LE; ++LI, ++Index) {
522 if (matchForBlockDiff(&*LI, &*RI)) {
523 Next[Index] = Cur[Index-1];
John McCalldfb44ac2010-07-29 08:53:59 +0000524 Next[Index].Cost += MatchCost;
John McCall3dd706b2010-07-29 07:53:27 +0000525 Next[Index].Path.push_back(DifferenceEngine::DC_match);
John McCalldfb44ac2010-07-29 08:53:59 +0000526 TentativeValues.insert(std::make_pair(&*LI, &*RI));
John McCall3dd706b2010-07-29 07:53:27 +0000527 } else if (Next[Index-1].Cost <= Cur[Index].Cost) {
528 Next[Index] = Next[Index-1];
John McCalldfb44ac2010-07-29 08:53:59 +0000529 Next[Index].Cost += LeftCost;
John McCall3dd706b2010-07-29 07:53:27 +0000530 Next[Index].Path.push_back(DifferenceEngine::DC_left);
531 } else {
532 Next[Index] = Cur[Index];
John McCalldfb44ac2010-07-29 08:53:59 +0000533 Next[Index].Cost += RightCost;
John McCall3dd706b2010-07-29 07:53:27 +0000534 Next[Index].Path.push_back(DifferenceEngine::DC_right);
535 }
536 }
537
538 std::swap(Cur, Next);
539 }
540
John McCall82bd5ea2010-07-29 09:20:34 +0000541 // We don't need the tentative values anymore; everything from here
542 // on out should be non-tentative.
543 TentativeValues.clear();
544
John McCall3dd706b2010-07-29 07:53:27 +0000545 SmallVectorImpl<char> &Path = Cur[NL].Path;
546 BasicBlock::iterator LI = LStart, RI = RStart;
547
548 DifferenceEngine::DiffLogBuilder Diff(Engine);
549
550 // Drop trailing matches.
551 while (Path.back() == DifferenceEngine::DC_match)
552 Path.pop_back();
553
John McCalldfb44ac2010-07-29 08:53:59 +0000554 // Skip leading matches.
555 SmallVectorImpl<char>::iterator
556 PI = Path.begin(), PE = Path.end();
John McCall82bd5ea2010-07-29 09:20:34 +0000557 while (PI != PE && *PI == DifferenceEngine::DC_match) {
558 unify(&*LI, &*RI);
John McCalldfb44ac2010-07-29 08:53:59 +0000559 ++PI, ++LI, ++RI;
John McCall82bd5ea2010-07-29 09:20:34 +0000560 }
John McCalldfb44ac2010-07-29 08:53:59 +0000561
562 for (; PI != PE; ++PI) {
John McCall3dd706b2010-07-29 07:53:27 +0000563 switch (static_cast<DifferenceEngine::DiffChange>(*PI)) {
564 case DifferenceEngine::DC_match:
565 assert(LI != LE && RI != RE);
566 {
567 Instruction *L = &*LI, *R = &*RI;
John McCall82bd5ea2010-07-29 09:20:34 +0000568 unify(L, R);
John McCall3dd706b2010-07-29 07:53:27 +0000569 Diff.addMatch(L, R);
570 }
571 ++LI; ++RI;
572 break;
573
574 case DifferenceEngine::DC_left:
575 assert(LI != LE);
576 Diff.addLeft(&*LI);
577 ++LI;
578 break;
579
580 case DifferenceEngine::DC_right:
581 assert(RI != RE);
582 Diff.addRight(&*RI);
583 ++RI;
584 break;
585 }
586 }
587
John McCall82bd5ea2010-07-29 09:20:34 +0000588 // Finishing unifying and complaining about the tails of the block,
589 // which should be matches all the way through.
590 while (LI != LE) {
591 assert(RI != RE);
592 unify(&*LI, &*RI);
593 ++LI, ++RI;
594 }
John McCall3dd706b2010-07-29 07:53:27 +0000595}
596
597}
598
599void DifferenceEngine::diff(Function *L, Function *R) {
600 Context C(*this, L, R);
601
602 // FIXME: types
603 // FIXME: attributes and CC
604 // FIXME: parameter attributes
605
606 // If both are declarations, we're done.
607 if (L->empty() && R->empty())
608 return;
609 else if (L->empty())
610 log("left function is declaration, right function is definition");
611 else if (R->empty())
612 log("right function is declaration, left function is definition");
613 else
614 FunctionDifferenceEngine(*this).diff(L, R);
615}
616
617void DifferenceEngine::diff(Module *L, Module *R) {
618 StringSet<> LNames;
619 SmallVector<std::pair<Function*,Function*>, 20> Queue;
620
621 for (Module::iterator I = L->begin(), E = L->end(); I != E; ++I) {
622 Function *LFn = &*I;
623 LNames.insert(LFn->getName());
624
625 if (Function *RFn = R->getFunction(LFn->getName()))
626 Queue.push_back(std::make_pair(LFn, RFn));
627 else
John McCall62dc1f32010-07-29 08:14:41 +0000628 logf("function %l exists only in left module") << LFn;
John McCall3dd706b2010-07-29 07:53:27 +0000629 }
630
631 for (Module::iterator I = R->begin(), E = R->end(); I != E; ++I) {
632 Function *RFn = &*I;
633 if (!LNames.count(RFn->getName()))
John McCall62dc1f32010-07-29 08:14:41 +0000634 logf("function %r exists only in right module") << RFn;
John McCall3dd706b2010-07-29 07:53:27 +0000635 }
636
637 for (SmallVectorImpl<std::pair<Function*,Function*> >::iterator
638 I = Queue.begin(), E = Queue.end(); I != E; ++I)
639 diff(I->first, I->second);
640}
641
642bool DifferenceEngine::equivalentAsOperands(GlobalValue *L, GlobalValue *R) {
643 if (globalValueOracle) return (*globalValueOracle)(L, R);
644 return L->getName() == R->getName();
645}