1349cc55cSDimitry Andric //===-- DifferenceEngine.cpp - Structural function/module comparison ------===// 2349cc55cSDimitry Andric // 3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6349cc55cSDimitry Andric // 7349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 8349cc55cSDimitry Andric // 9349cc55cSDimitry Andric // This header defines the implementation of the LLVM difference 10349cc55cSDimitry Andric // engine, which structurally compares global values within a module. 11349cc55cSDimitry Andric // 12349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 13349cc55cSDimitry Andric 14349cc55cSDimitry Andric #include "DifferenceEngine.h" 15349cc55cSDimitry Andric #include "llvm/ADT/DenseMap.h" 16349cc55cSDimitry Andric #include "llvm/ADT/DenseSet.h" 17349cc55cSDimitry Andric #include "llvm/ADT/SmallString.h" 18349cc55cSDimitry Andric #include "llvm/ADT/SmallVector.h" 19349cc55cSDimitry Andric #include "llvm/ADT/StringSet.h" 20349cc55cSDimitry Andric #include "llvm/IR/CFG.h" 21349cc55cSDimitry Andric #include "llvm/IR/Constants.h" 22349cc55cSDimitry Andric #include "llvm/IR/Function.h" 23349cc55cSDimitry Andric #include "llvm/IR/Instructions.h" 24349cc55cSDimitry Andric #include "llvm/IR/Module.h" 25349cc55cSDimitry Andric #include "llvm/Support/ErrorHandling.h" 26349cc55cSDimitry Andric #include "llvm/Support/raw_ostream.h" 27349cc55cSDimitry Andric #include "llvm/Support/type_traits.h" 28349cc55cSDimitry Andric #include <utility> 29349cc55cSDimitry Andric 30349cc55cSDimitry Andric using namespace llvm; 31349cc55cSDimitry Andric 32349cc55cSDimitry Andric namespace { 33349cc55cSDimitry Andric 34349cc55cSDimitry Andric /// A priority queue, implemented as a heap. 35349cc55cSDimitry Andric template <class T, class Sorter, unsigned InlineCapacity> 36349cc55cSDimitry Andric class PriorityQueue { 37349cc55cSDimitry Andric Sorter Precedes; 38349cc55cSDimitry Andric llvm::SmallVector<T, InlineCapacity> Storage; 39349cc55cSDimitry Andric 40349cc55cSDimitry Andric public: 41349cc55cSDimitry Andric PriorityQueue(const Sorter &Precedes) : Precedes(Precedes) {} 42349cc55cSDimitry Andric 43349cc55cSDimitry Andric /// Checks whether the heap is empty. 44349cc55cSDimitry Andric bool empty() const { return Storage.empty(); } 45349cc55cSDimitry Andric 46349cc55cSDimitry Andric /// Insert a new value on the heap. 47349cc55cSDimitry Andric void insert(const T &V) { 48349cc55cSDimitry Andric unsigned Index = Storage.size(); 49349cc55cSDimitry Andric Storage.push_back(V); 50349cc55cSDimitry Andric if (Index == 0) return; 51349cc55cSDimitry Andric 52349cc55cSDimitry Andric T *data = Storage.data(); 53349cc55cSDimitry Andric while (true) { 54349cc55cSDimitry Andric unsigned Target = (Index + 1) / 2 - 1; 55349cc55cSDimitry Andric if (!Precedes(data[Index], data[Target])) return; 56349cc55cSDimitry Andric std::swap(data[Index], data[Target]); 57349cc55cSDimitry Andric if (Target == 0) return; 58349cc55cSDimitry Andric Index = Target; 59349cc55cSDimitry Andric } 60349cc55cSDimitry Andric } 61349cc55cSDimitry Andric 62349cc55cSDimitry Andric /// Remove the minimum value in the heap. Only valid on a non-empty heap. 63349cc55cSDimitry Andric T remove_min() { 64349cc55cSDimitry Andric assert(!empty()); 65349cc55cSDimitry Andric T tmp = Storage[0]; 66349cc55cSDimitry Andric 67349cc55cSDimitry Andric unsigned NewSize = Storage.size() - 1; 68349cc55cSDimitry Andric if (NewSize) { 69349cc55cSDimitry Andric // Move the slot at the end to the beginning. 70349cc55cSDimitry Andric if (std::is_trivially_copyable<T>::value) 71349cc55cSDimitry Andric Storage[0] = Storage[NewSize]; 72349cc55cSDimitry Andric else 73349cc55cSDimitry Andric std::swap(Storage[0], Storage[NewSize]); 74349cc55cSDimitry Andric 75349cc55cSDimitry Andric // Bubble the root up as necessary. 76349cc55cSDimitry Andric unsigned Index = 0; 77349cc55cSDimitry Andric while (true) { 78349cc55cSDimitry Andric // With a 1-based index, the children would be Index*2 and Index*2+1. 79349cc55cSDimitry Andric unsigned R = (Index + 1) * 2; 80349cc55cSDimitry Andric unsigned L = R - 1; 81349cc55cSDimitry Andric 82349cc55cSDimitry Andric // If R is out of bounds, we're done after this in any case. 83349cc55cSDimitry Andric if (R >= NewSize) { 84349cc55cSDimitry Andric // If L is also out of bounds, we're done immediately. 85349cc55cSDimitry Andric if (L >= NewSize) break; 86349cc55cSDimitry Andric 87349cc55cSDimitry Andric // Otherwise, test whether we should swap L and Index. 88349cc55cSDimitry Andric if (Precedes(Storage[L], Storage[Index])) 89349cc55cSDimitry Andric std::swap(Storage[L], Storage[Index]); 90349cc55cSDimitry Andric break; 91349cc55cSDimitry Andric } 92349cc55cSDimitry Andric 93349cc55cSDimitry Andric // Otherwise, we need to compare with the smaller of L and R. 94349cc55cSDimitry Andric // Prefer R because it's closer to the end of the array. 95349cc55cSDimitry Andric unsigned IndexToTest = (Precedes(Storage[L], Storage[R]) ? L : R); 96349cc55cSDimitry Andric 97349cc55cSDimitry Andric // If Index is >= the min of L and R, then heap ordering is restored. 98349cc55cSDimitry Andric if (!Precedes(Storage[IndexToTest], Storage[Index])) 99349cc55cSDimitry Andric break; 100349cc55cSDimitry Andric 101349cc55cSDimitry Andric // Otherwise, keep bubbling up. 102349cc55cSDimitry Andric std::swap(Storage[IndexToTest], Storage[Index]); 103349cc55cSDimitry Andric Index = IndexToTest; 104349cc55cSDimitry Andric } 105349cc55cSDimitry Andric } 106349cc55cSDimitry Andric Storage.pop_back(); 107349cc55cSDimitry Andric 108349cc55cSDimitry Andric return tmp; 109349cc55cSDimitry Andric } 110349cc55cSDimitry Andric }; 111349cc55cSDimitry Andric 112349cc55cSDimitry Andric /// A function-scope difference engine. 113349cc55cSDimitry Andric class FunctionDifferenceEngine { 114349cc55cSDimitry Andric DifferenceEngine &Engine; 115349cc55cSDimitry Andric 116349cc55cSDimitry Andric // Some initializers may reference the variable we're currently checking. This 117349cc55cSDimitry Andric // can cause an infinite loop. The Saved[LR]HS ivars can be checked to prevent 118349cc55cSDimitry Andric // recursing. 119349cc55cSDimitry Andric const Value *SavedLHS; 120349cc55cSDimitry Andric const Value *SavedRHS; 121349cc55cSDimitry Andric 122349cc55cSDimitry Andric /// The current mapping from old local values to new local values. 123349cc55cSDimitry Andric DenseMap<const Value *, const Value *> Values; 124349cc55cSDimitry Andric 125349cc55cSDimitry Andric /// The current mapping from old blocks to new blocks. 126349cc55cSDimitry Andric DenseMap<const BasicBlock *, const BasicBlock *> Blocks; 127349cc55cSDimitry Andric 128349cc55cSDimitry Andric DenseSet<std::pair<const Value *, const Value *>> TentativeValues; 129349cc55cSDimitry Andric 130349cc55cSDimitry Andric unsigned getUnprocPredCount(const BasicBlock *Block) const { 131349cc55cSDimitry Andric unsigned Count = 0; 132349cc55cSDimitry Andric for (const_pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; 133349cc55cSDimitry Andric ++I) 134349cc55cSDimitry Andric if (!Blocks.count(*I)) Count++; 135349cc55cSDimitry Andric return Count; 136349cc55cSDimitry Andric } 137349cc55cSDimitry Andric 138349cc55cSDimitry Andric typedef std::pair<const BasicBlock *, const BasicBlock *> BlockPair; 139349cc55cSDimitry Andric 140349cc55cSDimitry Andric /// A type which sorts a priority queue by the number of unprocessed 141349cc55cSDimitry Andric /// predecessor blocks it has remaining. 142349cc55cSDimitry Andric /// 143349cc55cSDimitry Andric /// This is actually really expensive to calculate. 144349cc55cSDimitry Andric struct QueueSorter { 145349cc55cSDimitry Andric const FunctionDifferenceEngine &fde; 146349cc55cSDimitry Andric explicit QueueSorter(const FunctionDifferenceEngine &fde) : fde(fde) {} 147349cc55cSDimitry Andric 148349cc55cSDimitry Andric bool operator()(BlockPair &Old, BlockPair &New) { 149349cc55cSDimitry Andric return fde.getUnprocPredCount(Old.first) 150349cc55cSDimitry Andric < fde.getUnprocPredCount(New.first); 151349cc55cSDimitry Andric } 152349cc55cSDimitry Andric }; 153349cc55cSDimitry Andric 154349cc55cSDimitry Andric /// A queue of unified blocks to process. 155349cc55cSDimitry Andric PriorityQueue<BlockPair, QueueSorter, 20> Queue; 156349cc55cSDimitry Andric 157349cc55cSDimitry Andric /// Try to unify the given two blocks. Enqueues them for processing 158349cc55cSDimitry Andric /// if they haven't already been processed. 159349cc55cSDimitry Andric /// 160349cc55cSDimitry Andric /// Returns true if there was a problem unifying them. 161349cc55cSDimitry Andric bool tryUnify(const BasicBlock *L, const BasicBlock *R) { 162349cc55cSDimitry Andric const BasicBlock *&Ref = Blocks[L]; 163349cc55cSDimitry Andric 164349cc55cSDimitry Andric if (Ref) { 165349cc55cSDimitry Andric if (Ref == R) return false; 166349cc55cSDimitry Andric 167349cc55cSDimitry Andric Engine.logf("successor %l cannot be equivalent to %r; " 168349cc55cSDimitry Andric "it's already equivalent to %r") 169349cc55cSDimitry Andric << L << R << Ref; 170349cc55cSDimitry Andric return true; 171349cc55cSDimitry Andric } 172349cc55cSDimitry Andric 173349cc55cSDimitry Andric Ref = R; 174349cc55cSDimitry Andric Queue.insert(BlockPair(L, R)); 175349cc55cSDimitry Andric return false; 176349cc55cSDimitry Andric } 177349cc55cSDimitry Andric 178349cc55cSDimitry Andric /// Unifies two instructions, given that they're known not to have 179349cc55cSDimitry Andric /// structural differences. 180349cc55cSDimitry Andric void unify(const Instruction *L, const Instruction *R) { 181349cc55cSDimitry Andric DifferenceEngine::Context C(Engine, L, R); 182349cc55cSDimitry Andric 183349cc55cSDimitry Andric bool Result = diff(L, R, true, true); 184349cc55cSDimitry Andric assert(!Result && "structural differences second time around?"); 185349cc55cSDimitry Andric (void) Result; 186349cc55cSDimitry Andric if (!L->use_empty()) 187349cc55cSDimitry Andric Values[L] = R; 188349cc55cSDimitry Andric } 189349cc55cSDimitry Andric 190349cc55cSDimitry Andric void processQueue() { 191349cc55cSDimitry Andric while (!Queue.empty()) { 192349cc55cSDimitry Andric BlockPair Pair = Queue.remove_min(); 193349cc55cSDimitry Andric diff(Pair.first, Pair.second); 194349cc55cSDimitry Andric } 195349cc55cSDimitry Andric } 196349cc55cSDimitry Andric 197349cc55cSDimitry Andric void diff(const BasicBlock *L, const BasicBlock *R) { 198349cc55cSDimitry Andric DifferenceEngine::Context C(Engine, L, R); 199349cc55cSDimitry Andric 200349cc55cSDimitry Andric BasicBlock::const_iterator LI = L->begin(), LE = L->end(); 201349cc55cSDimitry Andric BasicBlock::const_iterator RI = R->begin(); 202349cc55cSDimitry Andric 203349cc55cSDimitry Andric do { 204349cc55cSDimitry Andric assert(LI != LE && RI != R->end()); 205349cc55cSDimitry Andric const Instruction *LeftI = &*LI, *RightI = &*RI; 206349cc55cSDimitry Andric 207349cc55cSDimitry Andric // If the instructions differ, start the more sophisticated diff 208349cc55cSDimitry Andric // algorithm at the start of the block. 209349cc55cSDimitry Andric if (diff(LeftI, RightI, false, false)) { 210349cc55cSDimitry Andric TentativeValues.clear(); 211349cc55cSDimitry Andric return runBlockDiff(L->begin(), R->begin()); 212349cc55cSDimitry Andric } 213349cc55cSDimitry Andric 214349cc55cSDimitry Andric // Otherwise, tentatively unify them. 215349cc55cSDimitry Andric if (!LeftI->use_empty()) 216349cc55cSDimitry Andric TentativeValues.insert(std::make_pair(LeftI, RightI)); 217349cc55cSDimitry Andric 218349cc55cSDimitry Andric ++LI; 219349cc55cSDimitry Andric ++RI; 220349cc55cSDimitry Andric } while (LI != LE); // This is sufficient: we can't get equality of 221349cc55cSDimitry Andric // terminators if there are residual instructions. 222349cc55cSDimitry Andric 223349cc55cSDimitry Andric // Unify everything in the block, non-tentatively this time. 224349cc55cSDimitry Andric TentativeValues.clear(); 225349cc55cSDimitry Andric for (LI = L->begin(), RI = R->begin(); LI != LE; ++LI, ++RI) 226349cc55cSDimitry Andric unify(&*LI, &*RI); 227349cc55cSDimitry Andric } 228349cc55cSDimitry Andric 229349cc55cSDimitry Andric bool matchForBlockDiff(const Instruction *L, const Instruction *R); 230349cc55cSDimitry Andric void runBlockDiff(BasicBlock::const_iterator LI, 231349cc55cSDimitry Andric BasicBlock::const_iterator RI); 232349cc55cSDimitry Andric 233349cc55cSDimitry Andric bool diffCallSites(const CallBase &L, const CallBase &R, bool Complain) { 234349cc55cSDimitry Andric // FIXME: call attributes 235349cc55cSDimitry Andric if (!equivalentAsOperands(L.getCalledOperand(), R.getCalledOperand())) { 236349cc55cSDimitry Andric if (Complain) Engine.log("called functions differ"); 237349cc55cSDimitry Andric return true; 238349cc55cSDimitry Andric } 239349cc55cSDimitry Andric if (L.arg_size() != R.arg_size()) { 240349cc55cSDimitry Andric if (Complain) Engine.log("argument counts differ"); 241349cc55cSDimitry Andric return true; 242349cc55cSDimitry Andric } 243349cc55cSDimitry Andric for (unsigned I = 0, E = L.arg_size(); I != E; ++I) 244349cc55cSDimitry Andric if (!equivalentAsOperands(L.getArgOperand(I), R.getArgOperand(I))) { 245349cc55cSDimitry Andric if (Complain) 246349cc55cSDimitry Andric Engine.logf("arguments %l and %r differ") 247349cc55cSDimitry Andric << L.getArgOperand(I) << R.getArgOperand(I); 248349cc55cSDimitry Andric return true; 249349cc55cSDimitry Andric } 250349cc55cSDimitry Andric return false; 251349cc55cSDimitry Andric } 252349cc55cSDimitry Andric 253349cc55cSDimitry Andric bool diff(const Instruction *L, const Instruction *R, bool Complain, 254349cc55cSDimitry Andric bool TryUnify) { 255349cc55cSDimitry Andric // FIXME: metadata (if Complain is set) 256349cc55cSDimitry Andric 257349cc55cSDimitry Andric // Different opcodes always imply different operations. 258349cc55cSDimitry Andric if (L->getOpcode() != R->getOpcode()) { 259349cc55cSDimitry Andric if (Complain) Engine.log("different instruction types"); 260349cc55cSDimitry Andric return true; 261349cc55cSDimitry Andric } 262349cc55cSDimitry Andric 263349cc55cSDimitry Andric if (isa<CmpInst>(L)) { 264349cc55cSDimitry Andric if (cast<CmpInst>(L)->getPredicate() 265349cc55cSDimitry Andric != cast<CmpInst>(R)->getPredicate()) { 266349cc55cSDimitry Andric if (Complain) Engine.log("different predicates"); 267349cc55cSDimitry Andric return true; 268349cc55cSDimitry Andric } 269349cc55cSDimitry Andric } else if (isa<CallInst>(L)) { 270349cc55cSDimitry Andric return diffCallSites(cast<CallInst>(*L), cast<CallInst>(*R), Complain); 271349cc55cSDimitry Andric } else if (isa<PHINode>(L)) { 272*4824e7fdSDimitry Andric const PHINode &LI = cast<PHINode>(*L); 273*4824e7fdSDimitry Andric const PHINode &RI = cast<PHINode>(*R); 274349cc55cSDimitry Andric 275349cc55cSDimitry Andric // This is really weird; type uniquing is broken? 276*4824e7fdSDimitry Andric if (LI.getType() != RI.getType()) { 277*4824e7fdSDimitry Andric if (!LI.getType()->isPointerTy() || !RI.getType()->isPointerTy()) { 278349cc55cSDimitry Andric if (Complain) Engine.log("different phi types"); 279349cc55cSDimitry Andric return true; 280349cc55cSDimitry Andric } 281349cc55cSDimitry Andric } 282*4824e7fdSDimitry Andric 283*4824e7fdSDimitry Andric if (LI.getNumIncomingValues() != RI.getNumIncomingValues()) { 284*4824e7fdSDimitry Andric if (Complain) 285*4824e7fdSDimitry Andric Engine.log("PHI node # of incoming values differ"); 286*4824e7fdSDimitry Andric return true; 287*4824e7fdSDimitry Andric } 288*4824e7fdSDimitry Andric 289*4824e7fdSDimitry Andric for (unsigned I = 0; I < LI.getNumIncomingValues(); ++I) { 290*4824e7fdSDimitry Andric if (TryUnify) 291*4824e7fdSDimitry Andric tryUnify(LI.getIncomingBlock(I), RI.getIncomingBlock(I)); 292*4824e7fdSDimitry Andric 293*4824e7fdSDimitry Andric if (!equivalentAsOperands(LI.getIncomingValue(I), 294*4824e7fdSDimitry Andric RI.getIncomingValue(I))) { 295*4824e7fdSDimitry Andric if (Complain) 296*4824e7fdSDimitry Andric Engine.log("PHI node incoming values differ"); 297*4824e7fdSDimitry Andric return true; 298*4824e7fdSDimitry Andric } 299*4824e7fdSDimitry Andric } 300*4824e7fdSDimitry Andric 301349cc55cSDimitry Andric return false; 302349cc55cSDimitry Andric 303349cc55cSDimitry Andric // Terminators. 304349cc55cSDimitry Andric } else if (isa<InvokeInst>(L)) { 305349cc55cSDimitry Andric const InvokeInst &LI = cast<InvokeInst>(*L); 306349cc55cSDimitry Andric const InvokeInst &RI = cast<InvokeInst>(*R); 307349cc55cSDimitry Andric if (diffCallSites(LI, RI, Complain)) 308349cc55cSDimitry Andric return true; 309349cc55cSDimitry Andric 310349cc55cSDimitry Andric if (TryUnify) { 311349cc55cSDimitry Andric tryUnify(LI.getNormalDest(), RI.getNormalDest()); 312349cc55cSDimitry Andric tryUnify(LI.getUnwindDest(), RI.getUnwindDest()); 313349cc55cSDimitry Andric } 314349cc55cSDimitry Andric return false; 315349cc55cSDimitry Andric 316349cc55cSDimitry Andric } else if (isa<CallBrInst>(L)) { 317349cc55cSDimitry Andric const CallBrInst &LI = cast<CallBrInst>(*L); 318349cc55cSDimitry Andric const CallBrInst &RI = cast<CallBrInst>(*R); 319349cc55cSDimitry Andric if (LI.getNumIndirectDests() != RI.getNumIndirectDests()) { 320349cc55cSDimitry Andric if (Complain) 321349cc55cSDimitry Andric Engine.log("callbr # of indirect destinations differ"); 322349cc55cSDimitry Andric return true; 323349cc55cSDimitry Andric } 324349cc55cSDimitry Andric 325349cc55cSDimitry Andric // Perform the "try unify" step so that we can equate the indirect 326349cc55cSDimitry Andric // destinations before checking the call site. 327349cc55cSDimitry Andric for (unsigned I = 0; I < LI.getNumIndirectDests(); I++) 328349cc55cSDimitry Andric tryUnify(LI.getIndirectDest(I), RI.getIndirectDest(I)); 329349cc55cSDimitry Andric 330349cc55cSDimitry Andric if (diffCallSites(LI, RI, Complain)) 331349cc55cSDimitry Andric return true; 332349cc55cSDimitry Andric 333349cc55cSDimitry Andric if (TryUnify) 334349cc55cSDimitry Andric tryUnify(LI.getDefaultDest(), RI.getDefaultDest()); 335349cc55cSDimitry Andric return false; 336349cc55cSDimitry Andric 337349cc55cSDimitry Andric } else if (isa<BranchInst>(L)) { 338349cc55cSDimitry Andric const BranchInst *LI = cast<BranchInst>(L); 339349cc55cSDimitry Andric const BranchInst *RI = cast<BranchInst>(R); 340349cc55cSDimitry Andric if (LI->isConditional() != RI->isConditional()) { 341349cc55cSDimitry Andric if (Complain) Engine.log("branch conditionality differs"); 342349cc55cSDimitry Andric return true; 343349cc55cSDimitry Andric } 344349cc55cSDimitry Andric 345349cc55cSDimitry Andric if (LI->isConditional()) { 346349cc55cSDimitry Andric if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) { 347349cc55cSDimitry Andric if (Complain) Engine.log("branch conditions differ"); 348349cc55cSDimitry Andric return true; 349349cc55cSDimitry Andric } 350349cc55cSDimitry Andric if (TryUnify) tryUnify(LI->getSuccessor(1), RI->getSuccessor(1)); 351349cc55cSDimitry Andric } 352349cc55cSDimitry Andric if (TryUnify) tryUnify(LI->getSuccessor(0), RI->getSuccessor(0)); 353349cc55cSDimitry Andric return false; 354349cc55cSDimitry Andric 355349cc55cSDimitry Andric } else if (isa<IndirectBrInst>(L)) { 356349cc55cSDimitry Andric const IndirectBrInst *LI = cast<IndirectBrInst>(L); 357349cc55cSDimitry Andric const IndirectBrInst *RI = cast<IndirectBrInst>(R); 358349cc55cSDimitry Andric if (LI->getNumDestinations() != RI->getNumDestinations()) { 359349cc55cSDimitry Andric if (Complain) Engine.log("indirectbr # of destinations differ"); 360349cc55cSDimitry Andric return true; 361349cc55cSDimitry Andric } 362349cc55cSDimitry Andric 363349cc55cSDimitry Andric if (!equivalentAsOperands(LI->getAddress(), RI->getAddress())) { 364349cc55cSDimitry Andric if (Complain) Engine.log("indirectbr addresses differ"); 365349cc55cSDimitry Andric return true; 366349cc55cSDimitry Andric } 367349cc55cSDimitry Andric 368349cc55cSDimitry Andric if (TryUnify) { 369349cc55cSDimitry Andric for (unsigned i = 0; i < LI->getNumDestinations(); i++) { 370349cc55cSDimitry Andric tryUnify(LI->getDestination(i), RI->getDestination(i)); 371349cc55cSDimitry Andric } 372349cc55cSDimitry Andric } 373349cc55cSDimitry Andric return false; 374349cc55cSDimitry Andric 375349cc55cSDimitry Andric } else if (isa<SwitchInst>(L)) { 376349cc55cSDimitry Andric const SwitchInst *LI = cast<SwitchInst>(L); 377349cc55cSDimitry Andric const SwitchInst *RI = cast<SwitchInst>(R); 378349cc55cSDimitry Andric if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) { 379349cc55cSDimitry Andric if (Complain) Engine.log("switch conditions differ"); 380349cc55cSDimitry Andric return true; 381349cc55cSDimitry Andric } 382349cc55cSDimitry Andric if (TryUnify) tryUnify(LI->getDefaultDest(), RI->getDefaultDest()); 383349cc55cSDimitry Andric 384349cc55cSDimitry Andric bool Difference = false; 385349cc55cSDimitry Andric 386349cc55cSDimitry Andric DenseMap<const ConstantInt *, const BasicBlock *> LCases; 387349cc55cSDimitry Andric for (auto Case : LI->cases()) 388349cc55cSDimitry Andric LCases[Case.getCaseValue()] = Case.getCaseSuccessor(); 389349cc55cSDimitry Andric 390349cc55cSDimitry Andric for (auto Case : RI->cases()) { 391349cc55cSDimitry Andric const ConstantInt *CaseValue = Case.getCaseValue(); 392349cc55cSDimitry Andric const BasicBlock *LCase = LCases[CaseValue]; 393349cc55cSDimitry Andric if (LCase) { 394349cc55cSDimitry Andric if (TryUnify) 395349cc55cSDimitry Andric tryUnify(LCase, Case.getCaseSuccessor()); 396349cc55cSDimitry Andric LCases.erase(CaseValue); 397349cc55cSDimitry Andric } else if (Complain || !Difference) { 398349cc55cSDimitry Andric if (Complain) 399349cc55cSDimitry Andric Engine.logf("right switch has extra case %r") << CaseValue; 400349cc55cSDimitry Andric Difference = true; 401349cc55cSDimitry Andric } 402349cc55cSDimitry Andric } 403349cc55cSDimitry Andric if (!Difference) 404349cc55cSDimitry Andric for (DenseMap<const ConstantInt *, const BasicBlock *>::iterator 405349cc55cSDimitry Andric I = LCases.begin(), 406349cc55cSDimitry Andric E = LCases.end(); 407349cc55cSDimitry Andric I != E; ++I) { 408349cc55cSDimitry Andric if (Complain) 409349cc55cSDimitry Andric Engine.logf("left switch has extra case %l") << I->first; 410349cc55cSDimitry Andric Difference = true; 411349cc55cSDimitry Andric } 412349cc55cSDimitry Andric return Difference; 413349cc55cSDimitry Andric } else if (isa<UnreachableInst>(L)) { 414349cc55cSDimitry Andric return false; 415349cc55cSDimitry Andric } 416349cc55cSDimitry Andric 417349cc55cSDimitry Andric if (L->getNumOperands() != R->getNumOperands()) { 418349cc55cSDimitry Andric if (Complain) Engine.log("instructions have different operand counts"); 419349cc55cSDimitry Andric return true; 420349cc55cSDimitry Andric } 421349cc55cSDimitry Andric 422349cc55cSDimitry Andric for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) { 423349cc55cSDimitry Andric Value *LO = L->getOperand(I), *RO = R->getOperand(I); 424349cc55cSDimitry Andric if (!equivalentAsOperands(LO, RO)) { 425349cc55cSDimitry Andric if (Complain) Engine.logf("operands %l and %r differ") << LO << RO; 426349cc55cSDimitry Andric return true; 427349cc55cSDimitry Andric } 428349cc55cSDimitry Andric } 429349cc55cSDimitry Andric 430349cc55cSDimitry Andric return false; 431349cc55cSDimitry Andric } 432349cc55cSDimitry Andric 433349cc55cSDimitry Andric public: 434349cc55cSDimitry Andric bool equivalentAsOperands(const Constant *L, const Constant *R) { 435349cc55cSDimitry Andric // Use equality as a preliminary filter. 436349cc55cSDimitry Andric if (L == R) 437349cc55cSDimitry Andric return true; 438349cc55cSDimitry Andric 439349cc55cSDimitry Andric if (L->getValueID() != R->getValueID()) 440349cc55cSDimitry Andric return false; 441349cc55cSDimitry Andric 442349cc55cSDimitry Andric // Ask the engine about global values. 443349cc55cSDimitry Andric if (isa<GlobalValue>(L)) 444349cc55cSDimitry Andric return Engine.equivalentAsOperands(cast<GlobalValue>(L), 445349cc55cSDimitry Andric cast<GlobalValue>(R)); 446349cc55cSDimitry Andric 447349cc55cSDimitry Andric // Compare constant expressions structurally. 448349cc55cSDimitry Andric if (isa<ConstantExpr>(L)) 449349cc55cSDimitry Andric return equivalentAsOperands(cast<ConstantExpr>(L), 450349cc55cSDimitry Andric cast<ConstantExpr>(R)); 451349cc55cSDimitry Andric 452349cc55cSDimitry Andric // Constants of the "same type" don't always actually have the same 453349cc55cSDimitry Andric // type; I don't know why. Just white-list them. 454349cc55cSDimitry Andric if (isa<ConstantPointerNull>(L) || isa<UndefValue>(L) || isa<ConstantAggregateZero>(L)) 455349cc55cSDimitry Andric return true; 456349cc55cSDimitry Andric 457349cc55cSDimitry Andric // Block addresses only match if we've already encountered the 458349cc55cSDimitry Andric // block. FIXME: tentative matches? 459349cc55cSDimitry Andric if (isa<BlockAddress>(L)) 460349cc55cSDimitry Andric return Blocks[cast<BlockAddress>(L)->getBasicBlock()] 461349cc55cSDimitry Andric == cast<BlockAddress>(R)->getBasicBlock(); 462349cc55cSDimitry Andric 463349cc55cSDimitry Andric // If L and R are ConstantVectors, compare each element 464349cc55cSDimitry Andric if (isa<ConstantVector>(L)) { 465349cc55cSDimitry Andric const ConstantVector *CVL = cast<ConstantVector>(L); 466349cc55cSDimitry Andric const ConstantVector *CVR = cast<ConstantVector>(R); 467349cc55cSDimitry Andric if (CVL->getType()->getNumElements() != CVR->getType()->getNumElements()) 468349cc55cSDimitry Andric return false; 469349cc55cSDimitry Andric for (unsigned i = 0; i < CVL->getType()->getNumElements(); i++) { 470349cc55cSDimitry Andric if (!equivalentAsOperands(CVL->getOperand(i), CVR->getOperand(i))) 471349cc55cSDimitry Andric return false; 472349cc55cSDimitry Andric } 473349cc55cSDimitry Andric return true; 474349cc55cSDimitry Andric } 475349cc55cSDimitry Andric 476349cc55cSDimitry Andric // If L and R are ConstantArrays, compare the element count and types. 477349cc55cSDimitry Andric if (isa<ConstantArray>(L)) { 478349cc55cSDimitry Andric const ConstantArray *CAL = cast<ConstantArray>(L); 479349cc55cSDimitry Andric const ConstantArray *CAR = cast<ConstantArray>(R); 480349cc55cSDimitry Andric // Sometimes a type may be equivalent, but not uniquified---e.g. it may 481349cc55cSDimitry Andric // contain a GEP instruction. Do a deeper comparison of the types. 482349cc55cSDimitry Andric if (CAL->getType()->getNumElements() != CAR->getType()->getNumElements()) 483349cc55cSDimitry Andric return false; 484349cc55cSDimitry Andric 485349cc55cSDimitry Andric for (unsigned I = 0; I < CAL->getType()->getNumElements(); ++I) { 486349cc55cSDimitry Andric if (!equivalentAsOperands(CAL->getAggregateElement(I), 487349cc55cSDimitry Andric CAR->getAggregateElement(I))) 488349cc55cSDimitry Andric return false; 489349cc55cSDimitry Andric } 490349cc55cSDimitry Andric 491349cc55cSDimitry Andric return true; 492349cc55cSDimitry Andric } 493349cc55cSDimitry Andric 494349cc55cSDimitry Andric // If L and R are ConstantStructs, compare each field and type. 495349cc55cSDimitry Andric if (isa<ConstantStruct>(L)) { 496349cc55cSDimitry Andric const ConstantStruct *CSL = cast<ConstantStruct>(L); 497349cc55cSDimitry Andric const ConstantStruct *CSR = cast<ConstantStruct>(R); 498349cc55cSDimitry Andric 499349cc55cSDimitry Andric const StructType *LTy = cast<StructType>(CSL->getType()); 500349cc55cSDimitry Andric const StructType *RTy = cast<StructType>(CSR->getType()); 501349cc55cSDimitry Andric 502349cc55cSDimitry Andric // The StructTypes should have the same attributes. Don't use 503349cc55cSDimitry Andric // isLayoutIdentical(), because that just checks the element pointers, 504349cc55cSDimitry Andric // which may not work here. 505349cc55cSDimitry Andric if (LTy->getNumElements() != RTy->getNumElements() || 506349cc55cSDimitry Andric LTy->isPacked() != RTy->isPacked()) 507349cc55cSDimitry Andric return false; 508349cc55cSDimitry Andric 509349cc55cSDimitry Andric for (unsigned I = 0; I < LTy->getNumElements(); I++) { 510349cc55cSDimitry Andric const Value *LAgg = CSL->getAggregateElement(I); 511349cc55cSDimitry Andric const Value *RAgg = CSR->getAggregateElement(I); 512349cc55cSDimitry Andric 513349cc55cSDimitry Andric if (LAgg == SavedLHS || RAgg == SavedRHS) { 514349cc55cSDimitry Andric if (LAgg != SavedLHS || RAgg != SavedRHS) 515349cc55cSDimitry Andric // If the left and right operands aren't both re-analyzing the 516349cc55cSDimitry Andric // variable, then the initialiers don't match, so report "false". 517349cc55cSDimitry Andric // Otherwise, we skip these operands.. 518349cc55cSDimitry Andric return false; 519349cc55cSDimitry Andric 520349cc55cSDimitry Andric continue; 521349cc55cSDimitry Andric } 522349cc55cSDimitry Andric 523349cc55cSDimitry Andric if (!equivalentAsOperands(LAgg, RAgg)) { 524349cc55cSDimitry Andric return false; 525349cc55cSDimitry Andric } 526349cc55cSDimitry Andric } 527349cc55cSDimitry Andric 528349cc55cSDimitry Andric return true; 529349cc55cSDimitry Andric } 530349cc55cSDimitry Andric 531349cc55cSDimitry Andric return false; 532349cc55cSDimitry Andric } 533349cc55cSDimitry Andric 534349cc55cSDimitry Andric bool equivalentAsOperands(const ConstantExpr *L, const ConstantExpr *R) { 535349cc55cSDimitry Andric if (L == R) 536349cc55cSDimitry Andric return true; 537349cc55cSDimitry Andric 538349cc55cSDimitry Andric if (L->getOpcode() != R->getOpcode()) 539349cc55cSDimitry Andric return false; 540349cc55cSDimitry Andric 541349cc55cSDimitry Andric switch (L->getOpcode()) { 542349cc55cSDimitry Andric case Instruction::ICmp: 543349cc55cSDimitry Andric case Instruction::FCmp: 544349cc55cSDimitry Andric if (L->getPredicate() != R->getPredicate()) 545349cc55cSDimitry Andric return false; 546349cc55cSDimitry Andric break; 547349cc55cSDimitry Andric 548349cc55cSDimitry Andric case Instruction::GetElementPtr: 549349cc55cSDimitry Andric // FIXME: inbounds? 550349cc55cSDimitry Andric break; 551349cc55cSDimitry Andric 552349cc55cSDimitry Andric default: 553349cc55cSDimitry Andric break; 554349cc55cSDimitry Andric } 555349cc55cSDimitry Andric 556349cc55cSDimitry Andric if (L->getNumOperands() != R->getNumOperands()) 557349cc55cSDimitry Andric return false; 558349cc55cSDimitry Andric 559349cc55cSDimitry Andric for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) { 560349cc55cSDimitry Andric const auto *LOp = L->getOperand(I); 561349cc55cSDimitry Andric const auto *ROp = R->getOperand(I); 562349cc55cSDimitry Andric 563349cc55cSDimitry Andric if (LOp == SavedLHS || ROp == SavedRHS) { 564349cc55cSDimitry Andric if (LOp != SavedLHS || ROp != SavedRHS) 565349cc55cSDimitry Andric // If the left and right operands aren't both re-analyzing the 566349cc55cSDimitry Andric // variable, then the initialiers don't match, so report "false". 567349cc55cSDimitry Andric // Otherwise, we skip these operands.. 568349cc55cSDimitry Andric return false; 569349cc55cSDimitry Andric 570349cc55cSDimitry Andric continue; 571349cc55cSDimitry Andric } 572349cc55cSDimitry Andric 573349cc55cSDimitry Andric if (!equivalentAsOperands(LOp, ROp)) 574349cc55cSDimitry Andric return false; 575349cc55cSDimitry Andric } 576349cc55cSDimitry Andric 577349cc55cSDimitry Andric return true; 578349cc55cSDimitry Andric } 579349cc55cSDimitry Andric 580349cc55cSDimitry Andric bool equivalentAsOperands(const Value *L, const Value *R) { 581349cc55cSDimitry Andric // Fall out if the values have different kind. 582349cc55cSDimitry Andric // This possibly shouldn't take priority over oracles. 583349cc55cSDimitry Andric if (L->getValueID() != R->getValueID()) 584349cc55cSDimitry Andric return false; 585349cc55cSDimitry Andric 586349cc55cSDimitry Andric // Value subtypes: Argument, Constant, Instruction, BasicBlock, 587349cc55cSDimitry Andric // InlineAsm, MDNode, MDString, PseudoSourceValue 588349cc55cSDimitry Andric 589349cc55cSDimitry Andric if (isa<Constant>(L)) 590349cc55cSDimitry Andric return equivalentAsOperands(cast<Constant>(L), cast<Constant>(R)); 591349cc55cSDimitry Andric 592349cc55cSDimitry Andric if (isa<Instruction>(L)) 593349cc55cSDimitry Andric return Values[L] == R || TentativeValues.count(std::make_pair(L, R)); 594349cc55cSDimitry Andric 595349cc55cSDimitry Andric if (isa<Argument>(L)) 596349cc55cSDimitry Andric return Values[L] == R; 597349cc55cSDimitry Andric 598349cc55cSDimitry Andric if (isa<BasicBlock>(L)) 599349cc55cSDimitry Andric return Blocks[cast<BasicBlock>(L)] != R; 600349cc55cSDimitry Andric 601349cc55cSDimitry Andric // Pretend everything else is identical. 602349cc55cSDimitry Andric return true; 603349cc55cSDimitry Andric } 604349cc55cSDimitry Andric 605349cc55cSDimitry Andric // Avoid a gcc warning about accessing 'this' in an initializer. 606349cc55cSDimitry Andric FunctionDifferenceEngine *this_() { return this; } 607349cc55cSDimitry Andric 608349cc55cSDimitry Andric public: 609349cc55cSDimitry Andric FunctionDifferenceEngine(DifferenceEngine &Engine, 610349cc55cSDimitry Andric const Value *SavedLHS = nullptr, 611349cc55cSDimitry Andric const Value *SavedRHS = nullptr) 612349cc55cSDimitry Andric : Engine(Engine), SavedLHS(SavedLHS), SavedRHS(SavedRHS), 613349cc55cSDimitry Andric Queue(QueueSorter(*this_())) {} 614349cc55cSDimitry Andric 615349cc55cSDimitry Andric void diff(const Function *L, const Function *R) { 616349cc55cSDimitry Andric if (L->arg_size() != R->arg_size()) 617349cc55cSDimitry Andric Engine.log("different argument counts"); 618349cc55cSDimitry Andric 619349cc55cSDimitry Andric // Map the arguments. 620349cc55cSDimitry Andric for (Function::const_arg_iterator LI = L->arg_begin(), LE = L->arg_end(), 621349cc55cSDimitry Andric RI = R->arg_begin(), RE = R->arg_end(); 622349cc55cSDimitry Andric LI != LE && RI != RE; ++LI, ++RI) 623349cc55cSDimitry Andric Values[&*LI] = &*RI; 624349cc55cSDimitry Andric 625349cc55cSDimitry Andric tryUnify(&*L->begin(), &*R->begin()); 626349cc55cSDimitry Andric processQueue(); 627349cc55cSDimitry Andric } 628349cc55cSDimitry Andric }; 629349cc55cSDimitry Andric 630349cc55cSDimitry Andric struct DiffEntry { 631349cc55cSDimitry Andric DiffEntry() : Cost(0) {} 632349cc55cSDimitry Andric 633349cc55cSDimitry Andric unsigned Cost; 634349cc55cSDimitry Andric llvm::SmallVector<char, 8> Path; // actually of DifferenceEngine::DiffChange 635349cc55cSDimitry Andric }; 636349cc55cSDimitry Andric 637349cc55cSDimitry Andric bool FunctionDifferenceEngine::matchForBlockDiff(const Instruction *L, 638349cc55cSDimitry Andric const Instruction *R) { 639349cc55cSDimitry Andric return !diff(L, R, false, false); 640349cc55cSDimitry Andric } 641349cc55cSDimitry Andric 642349cc55cSDimitry Andric void FunctionDifferenceEngine::runBlockDiff(BasicBlock::const_iterator LStart, 643349cc55cSDimitry Andric BasicBlock::const_iterator RStart) { 644349cc55cSDimitry Andric BasicBlock::const_iterator LE = LStart->getParent()->end(); 645349cc55cSDimitry Andric BasicBlock::const_iterator RE = RStart->getParent()->end(); 646349cc55cSDimitry Andric 647349cc55cSDimitry Andric unsigned NL = std::distance(LStart, LE); 648349cc55cSDimitry Andric 649349cc55cSDimitry Andric SmallVector<DiffEntry, 20> Paths1(NL+1); 650349cc55cSDimitry Andric SmallVector<DiffEntry, 20> Paths2(NL+1); 651349cc55cSDimitry Andric 652349cc55cSDimitry Andric DiffEntry *Cur = Paths1.data(); 653349cc55cSDimitry Andric DiffEntry *Next = Paths2.data(); 654349cc55cSDimitry Andric 655349cc55cSDimitry Andric const unsigned LeftCost = 2; 656349cc55cSDimitry Andric const unsigned RightCost = 2; 657349cc55cSDimitry Andric const unsigned MatchCost = 0; 658349cc55cSDimitry Andric 659349cc55cSDimitry Andric assert(TentativeValues.empty()); 660349cc55cSDimitry Andric 661349cc55cSDimitry Andric // Initialize the first column. 662349cc55cSDimitry Andric for (unsigned I = 0; I != NL+1; ++I) { 663349cc55cSDimitry Andric Cur[I].Cost = I * LeftCost; 664349cc55cSDimitry Andric for (unsigned J = 0; J != I; ++J) 665349cc55cSDimitry Andric Cur[I].Path.push_back(DC_left); 666349cc55cSDimitry Andric } 667349cc55cSDimitry Andric 668349cc55cSDimitry Andric for (BasicBlock::const_iterator RI = RStart; RI != RE; ++RI) { 669349cc55cSDimitry Andric // Initialize the first row. 670349cc55cSDimitry Andric Next[0] = Cur[0]; 671349cc55cSDimitry Andric Next[0].Cost += RightCost; 672349cc55cSDimitry Andric Next[0].Path.push_back(DC_right); 673349cc55cSDimitry Andric 674349cc55cSDimitry Andric unsigned Index = 1; 675349cc55cSDimitry Andric for (BasicBlock::const_iterator LI = LStart; LI != LE; ++LI, ++Index) { 676349cc55cSDimitry Andric if (matchForBlockDiff(&*LI, &*RI)) { 677349cc55cSDimitry Andric Next[Index] = Cur[Index-1]; 678349cc55cSDimitry Andric Next[Index].Cost += MatchCost; 679349cc55cSDimitry Andric Next[Index].Path.push_back(DC_match); 680349cc55cSDimitry Andric TentativeValues.insert(std::make_pair(&*LI, &*RI)); 681349cc55cSDimitry Andric } else if (Next[Index-1].Cost <= Cur[Index].Cost) { 682349cc55cSDimitry Andric Next[Index] = Next[Index-1]; 683349cc55cSDimitry Andric Next[Index].Cost += LeftCost; 684349cc55cSDimitry Andric Next[Index].Path.push_back(DC_left); 685349cc55cSDimitry Andric } else { 686349cc55cSDimitry Andric Next[Index] = Cur[Index]; 687349cc55cSDimitry Andric Next[Index].Cost += RightCost; 688349cc55cSDimitry Andric Next[Index].Path.push_back(DC_right); 689349cc55cSDimitry Andric } 690349cc55cSDimitry Andric } 691349cc55cSDimitry Andric 692349cc55cSDimitry Andric std::swap(Cur, Next); 693349cc55cSDimitry Andric } 694349cc55cSDimitry Andric 695349cc55cSDimitry Andric // We don't need the tentative values anymore; everything from here 696349cc55cSDimitry Andric // on out should be non-tentative. 697349cc55cSDimitry Andric TentativeValues.clear(); 698349cc55cSDimitry Andric 699349cc55cSDimitry Andric SmallVectorImpl<char> &Path = Cur[NL].Path; 700349cc55cSDimitry Andric BasicBlock::const_iterator LI = LStart, RI = RStart; 701349cc55cSDimitry Andric 702349cc55cSDimitry Andric DiffLogBuilder Diff(Engine.getConsumer()); 703349cc55cSDimitry Andric 704349cc55cSDimitry Andric // Drop trailing matches. 705349cc55cSDimitry Andric while (Path.size() && Path.back() == DC_match) 706349cc55cSDimitry Andric Path.pop_back(); 707349cc55cSDimitry Andric 708349cc55cSDimitry Andric // Skip leading matches. 709349cc55cSDimitry Andric SmallVectorImpl<char>::iterator 710349cc55cSDimitry Andric PI = Path.begin(), PE = Path.end(); 711349cc55cSDimitry Andric while (PI != PE && *PI == DC_match) { 712349cc55cSDimitry Andric unify(&*LI, &*RI); 713349cc55cSDimitry Andric ++PI; 714349cc55cSDimitry Andric ++LI; 715349cc55cSDimitry Andric ++RI; 716349cc55cSDimitry Andric } 717349cc55cSDimitry Andric 718349cc55cSDimitry Andric for (; PI != PE; ++PI) { 719349cc55cSDimitry Andric switch (static_cast<DiffChange>(*PI)) { 720349cc55cSDimitry Andric case DC_match: 721349cc55cSDimitry Andric assert(LI != LE && RI != RE); 722349cc55cSDimitry Andric { 723349cc55cSDimitry Andric const Instruction *L = &*LI, *R = &*RI; 724349cc55cSDimitry Andric unify(L, R); 725349cc55cSDimitry Andric Diff.addMatch(L, R); 726349cc55cSDimitry Andric } 727349cc55cSDimitry Andric ++LI; ++RI; 728349cc55cSDimitry Andric break; 729349cc55cSDimitry Andric 730349cc55cSDimitry Andric case DC_left: 731349cc55cSDimitry Andric assert(LI != LE); 732349cc55cSDimitry Andric Diff.addLeft(&*LI); 733349cc55cSDimitry Andric ++LI; 734349cc55cSDimitry Andric break; 735349cc55cSDimitry Andric 736349cc55cSDimitry Andric case DC_right: 737349cc55cSDimitry Andric assert(RI != RE); 738349cc55cSDimitry Andric Diff.addRight(&*RI); 739349cc55cSDimitry Andric ++RI; 740349cc55cSDimitry Andric break; 741349cc55cSDimitry Andric } 742349cc55cSDimitry Andric } 743349cc55cSDimitry Andric 744349cc55cSDimitry Andric // Finishing unifying and complaining about the tails of the block, 745349cc55cSDimitry Andric // which should be matches all the way through. 746349cc55cSDimitry Andric while (LI != LE) { 747349cc55cSDimitry Andric assert(RI != RE); 748349cc55cSDimitry Andric unify(&*LI, &*RI); 749349cc55cSDimitry Andric ++LI; 750349cc55cSDimitry Andric ++RI; 751349cc55cSDimitry Andric } 752349cc55cSDimitry Andric 753349cc55cSDimitry Andric // If the terminators have different kinds, but one is an invoke and the 754349cc55cSDimitry Andric // other is an unconditional branch immediately following a call, unify 755349cc55cSDimitry Andric // the results and the destinations. 756349cc55cSDimitry Andric const Instruction *LTerm = LStart->getParent()->getTerminator(); 757349cc55cSDimitry Andric const Instruction *RTerm = RStart->getParent()->getTerminator(); 758349cc55cSDimitry Andric if (isa<BranchInst>(LTerm) && isa<InvokeInst>(RTerm)) { 759349cc55cSDimitry Andric if (cast<BranchInst>(LTerm)->isConditional()) return; 760349cc55cSDimitry Andric BasicBlock::const_iterator I = LTerm->getIterator(); 761349cc55cSDimitry Andric if (I == LStart->getParent()->begin()) return; 762349cc55cSDimitry Andric --I; 763349cc55cSDimitry Andric if (!isa<CallInst>(*I)) return; 764349cc55cSDimitry Andric const CallInst *LCall = cast<CallInst>(&*I); 765349cc55cSDimitry Andric const InvokeInst *RInvoke = cast<InvokeInst>(RTerm); 766349cc55cSDimitry Andric if (!equivalentAsOperands(LCall->getCalledOperand(), 767349cc55cSDimitry Andric RInvoke->getCalledOperand())) 768349cc55cSDimitry Andric return; 769349cc55cSDimitry Andric if (!LCall->use_empty()) 770349cc55cSDimitry Andric Values[LCall] = RInvoke; 771349cc55cSDimitry Andric tryUnify(LTerm->getSuccessor(0), RInvoke->getNormalDest()); 772349cc55cSDimitry Andric } else if (isa<InvokeInst>(LTerm) && isa<BranchInst>(RTerm)) { 773349cc55cSDimitry Andric if (cast<BranchInst>(RTerm)->isConditional()) return; 774349cc55cSDimitry Andric BasicBlock::const_iterator I = RTerm->getIterator(); 775349cc55cSDimitry Andric if (I == RStart->getParent()->begin()) return; 776349cc55cSDimitry Andric --I; 777349cc55cSDimitry Andric if (!isa<CallInst>(*I)) return; 778349cc55cSDimitry Andric const CallInst *RCall = cast<CallInst>(I); 779349cc55cSDimitry Andric const InvokeInst *LInvoke = cast<InvokeInst>(LTerm); 780349cc55cSDimitry Andric if (!equivalentAsOperands(LInvoke->getCalledOperand(), 781349cc55cSDimitry Andric RCall->getCalledOperand())) 782349cc55cSDimitry Andric return; 783349cc55cSDimitry Andric if (!LInvoke->use_empty()) 784349cc55cSDimitry Andric Values[LInvoke] = RCall; 785349cc55cSDimitry Andric tryUnify(LInvoke->getNormalDest(), RTerm->getSuccessor(0)); 786349cc55cSDimitry Andric } 787349cc55cSDimitry Andric } 788349cc55cSDimitry Andric } 789349cc55cSDimitry Andric 790349cc55cSDimitry Andric void DifferenceEngine::Oracle::anchor() { } 791349cc55cSDimitry Andric 792349cc55cSDimitry Andric void DifferenceEngine::diff(const Function *L, const Function *R) { 793349cc55cSDimitry Andric Context C(*this, L, R); 794349cc55cSDimitry Andric 795349cc55cSDimitry Andric // FIXME: types 796349cc55cSDimitry Andric // FIXME: attributes and CC 797349cc55cSDimitry Andric // FIXME: parameter attributes 798349cc55cSDimitry Andric 799349cc55cSDimitry Andric // If both are declarations, we're done. 800349cc55cSDimitry Andric if (L->empty() && R->empty()) 801349cc55cSDimitry Andric return; 802349cc55cSDimitry Andric else if (L->empty()) 803349cc55cSDimitry Andric log("left function is declaration, right function is definition"); 804349cc55cSDimitry Andric else if (R->empty()) 805349cc55cSDimitry Andric log("right function is declaration, left function is definition"); 806349cc55cSDimitry Andric else 807349cc55cSDimitry Andric FunctionDifferenceEngine(*this).diff(L, R); 808349cc55cSDimitry Andric } 809349cc55cSDimitry Andric 810349cc55cSDimitry Andric void DifferenceEngine::diff(const Module *L, const Module *R) { 811349cc55cSDimitry Andric StringSet<> LNames; 812349cc55cSDimitry Andric SmallVector<std::pair<const Function *, const Function *>, 20> Queue; 813349cc55cSDimitry Andric 814349cc55cSDimitry Andric unsigned LeftAnonCount = 0; 815349cc55cSDimitry Andric unsigned RightAnonCount = 0; 816349cc55cSDimitry Andric 817349cc55cSDimitry Andric for (Module::const_iterator I = L->begin(), E = L->end(); I != E; ++I) { 818349cc55cSDimitry Andric const Function *LFn = &*I; 819349cc55cSDimitry Andric StringRef Name = LFn->getName(); 820349cc55cSDimitry Andric if (Name.empty()) { 821349cc55cSDimitry Andric ++LeftAnonCount; 822349cc55cSDimitry Andric continue; 823349cc55cSDimitry Andric } 824349cc55cSDimitry Andric 825349cc55cSDimitry Andric LNames.insert(Name); 826349cc55cSDimitry Andric 827349cc55cSDimitry Andric if (Function *RFn = R->getFunction(LFn->getName())) 828349cc55cSDimitry Andric Queue.push_back(std::make_pair(LFn, RFn)); 829349cc55cSDimitry Andric else 830349cc55cSDimitry Andric logf("function %l exists only in left module") << LFn; 831349cc55cSDimitry Andric } 832349cc55cSDimitry Andric 833349cc55cSDimitry Andric for (Module::const_iterator I = R->begin(), E = R->end(); I != E; ++I) { 834349cc55cSDimitry Andric const Function *RFn = &*I; 835349cc55cSDimitry Andric StringRef Name = RFn->getName(); 836349cc55cSDimitry Andric if (Name.empty()) { 837349cc55cSDimitry Andric ++RightAnonCount; 838349cc55cSDimitry Andric continue; 839349cc55cSDimitry Andric } 840349cc55cSDimitry Andric 841349cc55cSDimitry Andric if (!LNames.count(Name)) 842349cc55cSDimitry Andric logf("function %r exists only in right module") << RFn; 843349cc55cSDimitry Andric } 844349cc55cSDimitry Andric 845349cc55cSDimitry Andric if (LeftAnonCount != 0 || RightAnonCount != 0) { 846349cc55cSDimitry Andric SmallString<32> Tmp; 847349cc55cSDimitry Andric logf(("not comparing " + Twine(LeftAnonCount) + 848349cc55cSDimitry Andric " anonymous functions in the left module and " + 849349cc55cSDimitry Andric Twine(RightAnonCount) + " in the right module") 850349cc55cSDimitry Andric .toStringRef(Tmp)); 851349cc55cSDimitry Andric } 852349cc55cSDimitry Andric 853349cc55cSDimitry Andric for (SmallVectorImpl<std::pair<const Function *, const Function *>>::iterator 854349cc55cSDimitry Andric I = Queue.begin(), 855349cc55cSDimitry Andric E = Queue.end(); 856349cc55cSDimitry Andric I != E; ++I) 857349cc55cSDimitry Andric diff(I->first, I->second); 858349cc55cSDimitry Andric } 859349cc55cSDimitry Andric 860349cc55cSDimitry Andric bool DifferenceEngine::equivalentAsOperands(const GlobalValue *L, 861349cc55cSDimitry Andric const GlobalValue *R) { 862349cc55cSDimitry Andric if (globalValueOracle) return (*globalValueOracle)(L, R); 863349cc55cSDimitry Andric 864349cc55cSDimitry Andric if (isa<GlobalVariable>(L) && isa<GlobalVariable>(R)) { 865349cc55cSDimitry Andric const GlobalVariable *GVL = cast<GlobalVariable>(L); 866349cc55cSDimitry Andric const GlobalVariable *GVR = cast<GlobalVariable>(R); 867349cc55cSDimitry Andric if (GVL->hasLocalLinkage() && GVL->hasUniqueInitializer() && 868349cc55cSDimitry Andric GVR->hasLocalLinkage() && GVR->hasUniqueInitializer()) 869349cc55cSDimitry Andric return FunctionDifferenceEngine(*this, GVL, GVR) 870349cc55cSDimitry Andric .equivalentAsOperands(GVL->getInitializer(), GVR->getInitializer()); 871349cc55cSDimitry Andric } 872349cc55cSDimitry Andric 873349cc55cSDimitry Andric return L->getName() == R->getName(); 874349cc55cSDimitry Andric } 875