2
0

DifferenceEngine.cpp 20 KB

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