DifferenceEngine.cpp 23 KB

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