DifferenceEngine.cpp 20 KB

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