1*4d293f21SBill Wendling //===-- DifferenceEngine.cpp - Structural function/module comparison ------===// 2*4d293f21SBill Wendling // 3*4d293f21SBill Wendling // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*4d293f21SBill Wendling // See https://llvm.org/LICENSE.txt for license information. 5*4d293f21SBill Wendling // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*4d293f21SBill Wendling // 7*4d293f21SBill Wendling //===----------------------------------------------------------------------===// 8*4d293f21SBill Wendling // 9*4d293f21SBill Wendling // This header defines the implementation of the LLVM difference 10*4d293f21SBill Wendling // engine, which structurally compares global values within a module. 11*4d293f21SBill Wendling // 12*4d293f21SBill Wendling //===----------------------------------------------------------------------===// 13*4d293f21SBill Wendling 14*4d293f21SBill Wendling #include "DifferenceEngine.h" 15*4d293f21SBill Wendling #include "llvm/ADT/DenseMap.h" 16*4d293f21SBill Wendling #include "llvm/ADT/DenseSet.h" 17*4d293f21SBill Wendling #include "llvm/ADT/SmallString.h" 18*4d293f21SBill Wendling #include "llvm/ADT/SmallVector.h" 19*4d293f21SBill Wendling #include "llvm/ADT/StringSet.h" 20*4d293f21SBill Wendling #include "llvm/IR/CFG.h" 21*4d293f21SBill Wendling #include "llvm/IR/Constants.h" 22*4d293f21SBill Wendling #include "llvm/IR/Function.h" 23*4d293f21SBill Wendling #include "llvm/IR/Instructions.h" 24*4d293f21SBill Wendling #include "llvm/IR/Module.h" 25*4d293f21SBill Wendling #include "llvm/Support/ErrorHandling.h" 26*4d293f21SBill Wendling #include "llvm/Support/raw_ostream.h" 27*4d293f21SBill Wendling #include "llvm/Support/type_traits.h" 28*4d293f21SBill Wendling #include <utility> 29*4d293f21SBill Wendling 30*4d293f21SBill Wendling using namespace llvm; 31*4d293f21SBill Wendling 32*4d293f21SBill Wendling namespace { 33*4d293f21SBill Wendling 34*4d293f21SBill Wendling /// A priority queue, implemented as a heap. 35*4d293f21SBill Wendling template <class T, class Sorter, unsigned InlineCapacity> 36*4d293f21SBill Wendling class PriorityQueue { 37*4d293f21SBill Wendling Sorter Precedes; 38*4d293f21SBill Wendling llvm::SmallVector<T, InlineCapacity> Storage; 39*4d293f21SBill Wendling 40*4d293f21SBill Wendling public: 41*4d293f21SBill Wendling PriorityQueue(const Sorter &Precedes) : Precedes(Precedes) {} 42*4d293f21SBill Wendling 43*4d293f21SBill Wendling /// Checks whether the heap is empty. 44*4d293f21SBill Wendling bool empty() const { return Storage.empty(); } 45*4d293f21SBill Wendling 46*4d293f21SBill Wendling /// Insert a new value on the heap. 47*4d293f21SBill Wendling void insert(const T &V) { 48*4d293f21SBill Wendling unsigned Index = Storage.size(); 49*4d293f21SBill Wendling Storage.push_back(V); 50*4d293f21SBill Wendling if (Index == 0) return; 51*4d293f21SBill Wendling 52*4d293f21SBill Wendling T *data = Storage.data(); 53*4d293f21SBill Wendling while (true) { 54*4d293f21SBill Wendling unsigned Target = (Index + 1) / 2 - 1; 55*4d293f21SBill Wendling if (!Precedes(data[Index], data[Target])) return; 56*4d293f21SBill Wendling std::swap(data[Index], data[Target]); 57*4d293f21SBill Wendling if (Target == 0) return; 58*4d293f21SBill Wendling Index = Target; 59*4d293f21SBill Wendling } 60*4d293f21SBill Wendling } 61*4d293f21SBill Wendling 62*4d293f21SBill Wendling /// Remove the minimum value in the heap. Only valid on a non-empty heap. 63*4d293f21SBill Wendling T remove_min() { 64*4d293f21SBill Wendling assert(!empty()); 65*4d293f21SBill Wendling T tmp = Storage[0]; 66*4d293f21SBill Wendling 67*4d293f21SBill Wendling unsigned NewSize = Storage.size() - 1; 68*4d293f21SBill Wendling if (NewSize) { 69*4d293f21SBill Wendling // Move the slot at the end to the beginning. 70*4d293f21SBill Wendling if (std::is_trivially_copyable<T>::value) 71*4d293f21SBill Wendling Storage[0] = Storage[NewSize]; 72*4d293f21SBill Wendling else 73*4d293f21SBill Wendling std::swap(Storage[0], Storage[NewSize]); 74*4d293f21SBill Wendling 75*4d293f21SBill Wendling // Bubble the root up as necessary. 76*4d293f21SBill Wendling unsigned Index = 0; 77*4d293f21SBill Wendling while (true) { 78*4d293f21SBill Wendling // With a 1-based index, the children would be Index*2 and Index*2+1. 79*4d293f21SBill Wendling unsigned R = (Index + 1) * 2; 80*4d293f21SBill Wendling unsigned L = R - 1; 81*4d293f21SBill Wendling 82*4d293f21SBill Wendling // If R is out of bounds, we're done after this in any case. 83*4d293f21SBill Wendling if (R >= NewSize) { 84*4d293f21SBill Wendling // If L is also out of bounds, we're done immediately. 85*4d293f21SBill Wendling if (L >= NewSize) break; 86*4d293f21SBill Wendling 87*4d293f21SBill Wendling // Otherwise, test whether we should swap L and Index. 88*4d293f21SBill Wendling if (Precedes(Storage[L], Storage[Index])) 89*4d293f21SBill Wendling std::swap(Storage[L], Storage[Index]); 90*4d293f21SBill Wendling break; 91*4d293f21SBill Wendling } 92*4d293f21SBill Wendling 93*4d293f21SBill Wendling // Otherwise, we need to compare with the smaller of L and R. 94*4d293f21SBill Wendling // Prefer R because it's closer to the end of the array. 95*4d293f21SBill Wendling unsigned IndexToTest = (Precedes(Storage[L], Storage[R]) ? L : R); 96*4d293f21SBill Wendling 97*4d293f21SBill Wendling // If Index is >= the min of L and R, then heap ordering is restored. 98*4d293f21SBill Wendling if (!Precedes(Storage[IndexToTest], Storage[Index])) 99*4d293f21SBill Wendling break; 100*4d293f21SBill Wendling 101*4d293f21SBill Wendling // Otherwise, keep bubbling up. 102*4d293f21SBill Wendling std::swap(Storage[IndexToTest], Storage[Index]); 103*4d293f21SBill Wendling Index = IndexToTest; 104*4d293f21SBill Wendling } 105*4d293f21SBill Wendling } 106*4d293f21SBill Wendling Storage.pop_back(); 107*4d293f21SBill Wendling 108*4d293f21SBill Wendling return tmp; 109*4d293f21SBill Wendling } 110*4d293f21SBill Wendling }; 111*4d293f21SBill Wendling 112*4d293f21SBill Wendling /// A function-scope difference engine. 113*4d293f21SBill Wendling class FunctionDifferenceEngine { 114*4d293f21SBill Wendling DifferenceEngine &Engine; 115*4d293f21SBill Wendling 116*4d293f21SBill Wendling // Some initializers may reference the variable we're currently checking. This 117*4d293f21SBill Wendling // can cause an infinite loop. The Saved[LR]HS ivars can be checked to prevent 118*4d293f21SBill Wendling // recursing. 119*4d293f21SBill Wendling const Value *SavedLHS; 120*4d293f21SBill Wendling const Value *SavedRHS; 121*4d293f21SBill Wendling 122*4d293f21SBill Wendling /// The current mapping from old local values to new local values. 123*4d293f21SBill Wendling DenseMap<const Value *, const Value *> Values; 124*4d293f21SBill Wendling 125*4d293f21SBill Wendling /// The current mapping from old blocks to new blocks. 126*4d293f21SBill Wendling DenseMap<const BasicBlock *, const BasicBlock *> Blocks; 127*4d293f21SBill Wendling 128*4d293f21SBill Wendling DenseSet<std::pair<const Value *, const Value *>> TentativeValues; 129*4d293f21SBill Wendling 130*4d293f21SBill Wendling unsigned getUnprocPredCount(const BasicBlock *Block) const { 131*4d293f21SBill Wendling unsigned Count = 0; 132*4d293f21SBill Wendling for (const_pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; 133*4d293f21SBill Wendling ++I) 134*4d293f21SBill Wendling if (!Blocks.count(*I)) Count++; 135*4d293f21SBill Wendling return Count; 136*4d293f21SBill Wendling } 137*4d293f21SBill Wendling 138*4d293f21SBill Wendling typedef std::pair<const BasicBlock *, const BasicBlock *> BlockPair; 139*4d293f21SBill Wendling 140*4d293f21SBill Wendling /// A type which sorts a priority queue by the number of unprocessed 141*4d293f21SBill Wendling /// predecessor blocks it has remaining. 142*4d293f21SBill Wendling /// 143*4d293f21SBill Wendling /// This is actually really expensive to calculate. 144*4d293f21SBill Wendling struct QueueSorter { 145*4d293f21SBill Wendling const FunctionDifferenceEngine &fde; 146*4d293f21SBill Wendling explicit QueueSorter(const FunctionDifferenceEngine &fde) : fde(fde) {} 147*4d293f21SBill Wendling 148*4d293f21SBill Wendling bool operator()(BlockPair &Old, BlockPair &New) { 149*4d293f21SBill Wendling return fde.getUnprocPredCount(Old.first) 150*4d293f21SBill Wendling < fde.getUnprocPredCount(New.first); 151*4d293f21SBill Wendling } 152*4d293f21SBill Wendling }; 153*4d293f21SBill Wendling 154*4d293f21SBill Wendling /// A queue of unified blocks to process. 155*4d293f21SBill Wendling PriorityQueue<BlockPair, QueueSorter, 20> Queue; 156*4d293f21SBill Wendling 157*4d293f21SBill Wendling /// Try to unify the given two blocks. Enqueues them for processing 158*4d293f21SBill Wendling /// if they haven't already been processed. 159*4d293f21SBill Wendling /// 160*4d293f21SBill Wendling /// Returns true if there was a problem unifying them. 161*4d293f21SBill Wendling bool tryUnify(const BasicBlock *L, const BasicBlock *R) { 162*4d293f21SBill Wendling const BasicBlock *&Ref = Blocks[L]; 163*4d293f21SBill Wendling 164*4d293f21SBill Wendling if (Ref) { 165*4d293f21SBill Wendling if (Ref == R) return false; 166*4d293f21SBill Wendling 167*4d293f21SBill Wendling Engine.logf("successor %l cannot be equivalent to %r; " 168*4d293f21SBill Wendling "it's already equivalent to %r") 169*4d293f21SBill Wendling << L << R << Ref; 170*4d293f21SBill Wendling return true; 171*4d293f21SBill Wendling } 172*4d293f21SBill Wendling 173*4d293f21SBill Wendling Ref = R; 174*4d293f21SBill Wendling Queue.insert(BlockPair(L, R)); 175*4d293f21SBill Wendling return false; 176*4d293f21SBill Wendling } 177*4d293f21SBill Wendling 178*4d293f21SBill Wendling /// Unifies two instructions, given that they're known not to have 179*4d293f21SBill Wendling /// structural differences. 180*4d293f21SBill Wendling void unify(const Instruction *L, const Instruction *R) { 181*4d293f21SBill Wendling DifferenceEngine::Context C(Engine, L, R); 182*4d293f21SBill Wendling 183*4d293f21SBill Wendling bool Result = diff(L, R, true, true); 184*4d293f21SBill Wendling assert(!Result && "structural differences second time around?"); 185*4d293f21SBill Wendling (void) Result; 186*4d293f21SBill Wendling if (!L->use_empty()) 187*4d293f21SBill Wendling Values[L] = R; 188*4d293f21SBill Wendling } 189*4d293f21SBill Wendling 190*4d293f21SBill Wendling void processQueue() { 191*4d293f21SBill Wendling while (!Queue.empty()) { 192*4d293f21SBill Wendling BlockPair Pair = Queue.remove_min(); 193*4d293f21SBill Wendling diff(Pair.first, Pair.second); 194*4d293f21SBill Wendling } 195*4d293f21SBill Wendling } 196*4d293f21SBill Wendling 197*4d293f21SBill Wendling void diff(const BasicBlock *L, const BasicBlock *R) { 198*4d293f21SBill Wendling DifferenceEngine::Context C(Engine, L, R); 199*4d293f21SBill Wendling 200*4d293f21SBill Wendling BasicBlock::const_iterator LI = L->begin(), LE = L->end(); 201*4d293f21SBill Wendling BasicBlock::const_iterator RI = R->begin(); 202*4d293f21SBill Wendling 203*4d293f21SBill Wendling do { 204*4d293f21SBill Wendling assert(LI != LE && RI != R->end()); 205*4d293f21SBill Wendling const Instruction *LeftI = &*LI, *RightI = &*RI; 206*4d293f21SBill Wendling 207*4d293f21SBill Wendling // If the instructions differ, start the more sophisticated diff 208*4d293f21SBill Wendling // algorithm at the start of the block. 209*4d293f21SBill Wendling if (diff(LeftI, RightI, false, false)) { 210*4d293f21SBill Wendling TentativeValues.clear(); 211*4d293f21SBill Wendling return runBlockDiff(L->begin(), R->begin()); 212*4d293f21SBill Wendling } 213*4d293f21SBill Wendling 214*4d293f21SBill Wendling // Otherwise, tentatively unify them. 215*4d293f21SBill Wendling if (!LeftI->use_empty()) 216*4d293f21SBill Wendling TentativeValues.insert(std::make_pair(LeftI, RightI)); 217*4d293f21SBill Wendling 218*4d293f21SBill Wendling ++LI; 219*4d293f21SBill Wendling ++RI; 220*4d293f21SBill Wendling } while (LI != LE); // This is sufficient: we can't get equality of 221*4d293f21SBill Wendling // terminators if there are residual instructions. 222*4d293f21SBill Wendling 223*4d293f21SBill Wendling // Unify everything in the block, non-tentatively this time. 224*4d293f21SBill Wendling TentativeValues.clear(); 225*4d293f21SBill Wendling for (LI = L->begin(), RI = R->begin(); LI != LE; ++LI, ++RI) 226*4d293f21SBill Wendling unify(&*LI, &*RI); 227*4d293f21SBill Wendling } 228*4d293f21SBill Wendling 229*4d293f21SBill Wendling bool matchForBlockDiff(const Instruction *L, const Instruction *R); 230*4d293f21SBill Wendling void runBlockDiff(BasicBlock::const_iterator LI, 231*4d293f21SBill Wendling BasicBlock::const_iterator RI); 232*4d293f21SBill Wendling 233*4d293f21SBill Wendling bool diffCallSites(const CallBase &L, const CallBase &R, bool Complain) { 234*4d293f21SBill Wendling // FIXME: call attributes 235*4d293f21SBill Wendling if (!equivalentAsOperands(L.getCalledOperand(), R.getCalledOperand())) { 236*4d293f21SBill Wendling if (Complain) Engine.log("called functions differ"); 237*4d293f21SBill Wendling return true; 238*4d293f21SBill Wendling } 239*4d293f21SBill Wendling if (L.arg_size() != R.arg_size()) { 240*4d293f21SBill Wendling if (Complain) Engine.log("argument counts differ"); 241*4d293f21SBill Wendling return true; 242*4d293f21SBill Wendling } 243*4d293f21SBill Wendling for (unsigned I = 0, E = L.arg_size(); I != E; ++I) 244*4d293f21SBill Wendling if (!equivalentAsOperands(L.getArgOperand(I), R.getArgOperand(I))) { 245*4d293f21SBill Wendling if (Complain) 246*4d293f21SBill Wendling Engine.logf("arguments %l and %r differ") 247*4d293f21SBill Wendling << L.getArgOperand(I) << R.getArgOperand(I); 248*4d293f21SBill Wendling return true; 249*4d293f21SBill Wendling } 250*4d293f21SBill Wendling return false; 251*4d293f21SBill Wendling } 252*4d293f21SBill Wendling 253*4d293f21SBill Wendling bool diff(const Instruction *L, const Instruction *R, bool Complain, 254*4d293f21SBill Wendling bool TryUnify) { 255*4d293f21SBill Wendling // FIXME: metadata (if Complain is set) 256*4d293f21SBill Wendling 257*4d293f21SBill Wendling // Different opcodes always imply different operations. 258*4d293f21SBill Wendling if (L->getOpcode() != R->getOpcode()) { 259*4d293f21SBill Wendling if (Complain) Engine.log("different instruction types"); 260*4d293f21SBill Wendling return true; 261*4d293f21SBill Wendling } 262*4d293f21SBill Wendling 263*4d293f21SBill Wendling if (isa<CmpInst>(L)) { 264*4d293f21SBill Wendling if (cast<CmpInst>(L)->getPredicate() 265*4d293f21SBill Wendling != cast<CmpInst>(R)->getPredicate()) { 266*4d293f21SBill Wendling if (Complain) Engine.log("different predicates"); 267*4d293f21SBill Wendling return true; 268*4d293f21SBill Wendling } 269*4d293f21SBill Wendling } else if (isa<CallInst>(L)) { 270*4d293f21SBill Wendling return diffCallSites(cast<CallInst>(*L), cast<CallInst>(*R), Complain); 271*4d293f21SBill Wendling } else if (isa<PHINode>(L)) { 272*4d293f21SBill Wendling // FIXME: implement. 273*4d293f21SBill Wendling 274*4d293f21SBill Wendling // This is really weird; type uniquing is broken? 275*4d293f21SBill Wendling if (L->getType() != R->getType()) { 276*4d293f21SBill Wendling if (!L->getType()->isPointerTy() || !R->getType()->isPointerTy()) { 277*4d293f21SBill Wendling if (Complain) Engine.log("different phi types"); 278*4d293f21SBill Wendling return true; 279*4d293f21SBill Wendling } 280*4d293f21SBill Wendling } 281*4d293f21SBill Wendling return false; 282*4d293f21SBill Wendling 283*4d293f21SBill Wendling // Terminators. 284*4d293f21SBill Wendling } else if (isa<InvokeInst>(L)) { 285*4d293f21SBill Wendling const InvokeInst &LI = cast<InvokeInst>(*L); 286*4d293f21SBill Wendling const InvokeInst &RI = cast<InvokeInst>(*R); 287*4d293f21SBill Wendling if (diffCallSites(LI, RI, Complain)) 288*4d293f21SBill Wendling return true; 289*4d293f21SBill Wendling 290*4d293f21SBill Wendling if (TryUnify) { 291*4d293f21SBill Wendling tryUnify(LI.getNormalDest(), RI.getNormalDest()); 292*4d293f21SBill Wendling tryUnify(LI.getUnwindDest(), RI.getUnwindDest()); 293*4d293f21SBill Wendling } 294*4d293f21SBill Wendling return false; 295*4d293f21SBill Wendling 296*4d293f21SBill Wendling } else if (isa<CallBrInst>(L)) { 297*4d293f21SBill Wendling const CallBrInst &LI = cast<CallBrInst>(*L); 298*4d293f21SBill Wendling const CallBrInst &RI = cast<CallBrInst>(*R); 299*4d293f21SBill Wendling if (LI.getNumIndirectDests() != RI.getNumIndirectDests()) { 300*4d293f21SBill Wendling if (Complain) 301*4d293f21SBill Wendling Engine.log("callbr # of indirect destinations differ"); 302*4d293f21SBill Wendling return true; 303*4d293f21SBill Wendling } 304*4d293f21SBill Wendling 305*4d293f21SBill Wendling // Perform the "try unify" step so that we can equate the indirect 306*4d293f21SBill Wendling // destinations before checking the call site. 307*4d293f21SBill Wendling for (unsigned I = 0; I < LI.getNumIndirectDests(); I++) 308*4d293f21SBill Wendling tryUnify(LI.getIndirectDest(I), RI.getIndirectDest(I)); 309*4d293f21SBill Wendling 310*4d293f21SBill Wendling if (diffCallSites(LI, RI, Complain)) 311*4d293f21SBill Wendling return true; 312*4d293f21SBill Wendling 313*4d293f21SBill Wendling if (TryUnify) 314*4d293f21SBill Wendling tryUnify(LI.getDefaultDest(), RI.getDefaultDest()); 315*4d293f21SBill Wendling return false; 316*4d293f21SBill Wendling 317*4d293f21SBill Wendling } else if (isa<BranchInst>(L)) { 318*4d293f21SBill Wendling const BranchInst *LI = cast<BranchInst>(L); 319*4d293f21SBill Wendling const BranchInst *RI = cast<BranchInst>(R); 320*4d293f21SBill Wendling if (LI->isConditional() != RI->isConditional()) { 321*4d293f21SBill Wendling if (Complain) Engine.log("branch conditionality differs"); 322*4d293f21SBill Wendling return true; 323*4d293f21SBill Wendling } 324*4d293f21SBill Wendling 325*4d293f21SBill Wendling if (LI->isConditional()) { 326*4d293f21SBill Wendling if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) { 327*4d293f21SBill Wendling if (Complain) Engine.log("branch conditions differ"); 328*4d293f21SBill Wendling return true; 329*4d293f21SBill Wendling } 330*4d293f21SBill Wendling if (TryUnify) tryUnify(LI->getSuccessor(1), RI->getSuccessor(1)); 331*4d293f21SBill Wendling } 332*4d293f21SBill Wendling if (TryUnify) tryUnify(LI->getSuccessor(0), RI->getSuccessor(0)); 333*4d293f21SBill Wendling return false; 334*4d293f21SBill Wendling 335*4d293f21SBill Wendling } else if (isa<IndirectBrInst>(L)) { 336*4d293f21SBill Wendling const IndirectBrInst *LI = cast<IndirectBrInst>(L); 337*4d293f21SBill Wendling const IndirectBrInst *RI = cast<IndirectBrInst>(R); 338*4d293f21SBill Wendling if (LI->getNumDestinations() != RI->getNumDestinations()) { 339*4d293f21SBill Wendling if (Complain) Engine.log("indirectbr # of destinations differ"); 340*4d293f21SBill Wendling return true; 341*4d293f21SBill Wendling } 342*4d293f21SBill Wendling 343*4d293f21SBill Wendling if (!equivalentAsOperands(LI->getAddress(), RI->getAddress())) { 344*4d293f21SBill Wendling if (Complain) Engine.log("indirectbr addresses differ"); 345*4d293f21SBill Wendling return true; 346*4d293f21SBill Wendling } 347*4d293f21SBill Wendling 348*4d293f21SBill Wendling if (TryUnify) { 349*4d293f21SBill Wendling for (unsigned i = 0; i < LI->getNumDestinations(); i++) { 350*4d293f21SBill Wendling tryUnify(LI->getDestination(i), RI->getDestination(i)); 351*4d293f21SBill Wendling } 352*4d293f21SBill Wendling } 353*4d293f21SBill Wendling return false; 354*4d293f21SBill Wendling 355*4d293f21SBill Wendling } else if (isa<SwitchInst>(L)) { 356*4d293f21SBill Wendling const SwitchInst *LI = cast<SwitchInst>(L); 357*4d293f21SBill Wendling const SwitchInst *RI = cast<SwitchInst>(R); 358*4d293f21SBill Wendling if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) { 359*4d293f21SBill Wendling if (Complain) Engine.log("switch conditions differ"); 360*4d293f21SBill Wendling return true; 361*4d293f21SBill Wendling } 362*4d293f21SBill Wendling if (TryUnify) tryUnify(LI->getDefaultDest(), RI->getDefaultDest()); 363*4d293f21SBill Wendling 364*4d293f21SBill Wendling bool Difference = false; 365*4d293f21SBill Wendling 366*4d293f21SBill Wendling DenseMap<const ConstantInt *, const BasicBlock *> LCases; 367*4d293f21SBill Wendling for (auto Case : LI->cases()) 368*4d293f21SBill Wendling LCases[Case.getCaseValue()] = Case.getCaseSuccessor(); 369*4d293f21SBill Wendling 370*4d293f21SBill Wendling for (auto Case : RI->cases()) { 371*4d293f21SBill Wendling const ConstantInt *CaseValue = Case.getCaseValue(); 372*4d293f21SBill Wendling const BasicBlock *LCase = LCases[CaseValue]; 373*4d293f21SBill Wendling if (LCase) { 374*4d293f21SBill Wendling if (TryUnify) 375*4d293f21SBill Wendling tryUnify(LCase, Case.getCaseSuccessor()); 376*4d293f21SBill Wendling LCases.erase(CaseValue); 377*4d293f21SBill Wendling } else if (Complain || !Difference) { 378*4d293f21SBill Wendling if (Complain) 379*4d293f21SBill Wendling Engine.logf("right switch has extra case %r") << CaseValue; 380*4d293f21SBill Wendling Difference = true; 381*4d293f21SBill Wendling } 382*4d293f21SBill Wendling } 383*4d293f21SBill Wendling if (!Difference) 384*4d293f21SBill Wendling for (DenseMap<const ConstantInt *, const BasicBlock *>::iterator 385*4d293f21SBill Wendling I = LCases.begin(), 386*4d293f21SBill Wendling E = LCases.end(); 387*4d293f21SBill Wendling I != E; ++I) { 388*4d293f21SBill Wendling if (Complain) 389*4d293f21SBill Wendling Engine.logf("left switch has extra case %l") << I->first; 390*4d293f21SBill Wendling Difference = true; 391*4d293f21SBill Wendling } 392*4d293f21SBill Wendling return Difference; 393*4d293f21SBill Wendling } else if (isa<UnreachableInst>(L)) { 394*4d293f21SBill Wendling return false; 395*4d293f21SBill Wendling } 396*4d293f21SBill Wendling 397*4d293f21SBill Wendling if (L->getNumOperands() != R->getNumOperands()) { 398*4d293f21SBill Wendling if (Complain) Engine.log("instructions have different operand counts"); 399*4d293f21SBill Wendling return true; 400*4d293f21SBill Wendling } 401*4d293f21SBill Wendling 402*4d293f21SBill Wendling for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) { 403*4d293f21SBill Wendling Value *LO = L->getOperand(I), *RO = R->getOperand(I); 404*4d293f21SBill Wendling if (!equivalentAsOperands(LO, RO)) { 405*4d293f21SBill Wendling if (Complain) Engine.logf("operands %l and %r differ") << LO << RO; 406*4d293f21SBill Wendling return true; 407*4d293f21SBill Wendling } 408*4d293f21SBill Wendling } 409*4d293f21SBill Wendling 410*4d293f21SBill Wendling return false; 411*4d293f21SBill Wendling } 412*4d293f21SBill Wendling 413*4d293f21SBill Wendling public: 414*4d293f21SBill Wendling bool equivalentAsOperands(const Constant *L, const Constant *R) { 415*4d293f21SBill Wendling // Use equality as a preliminary filter. 416*4d293f21SBill Wendling if (L == R) 417*4d293f21SBill Wendling return true; 418*4d293f21SBill Wendling 419*4d293f21SBill Wendling if (L->getValueID() != R->getValueID()) 420*4d293f21SBill Wendling return false; 421*4d293f21SBill Wendling 422*4d293f21SBill Wendling // Ask the engine about global values. 423*4d293f21SBill Wendling if (isa<GlobalValue>(L)) 424*4d293f21SBill Wendling return Engine.equivalentAsOperands(cast<GlobalValue>(L), 425*4d293f21SBill Wendling cast<GlobalValue>(R)); 426*4d293f21SBill Wendling 427*4d293f21SBill Wendling // Compare constant expressions structurally. 428*4d293f21SBill Wendling if (isa<ConstantExpr>(L)) 429*4d293f21SBill Wendling return equivalentAsOperands(cast<ConstantExpr>(L), 430*4d293f21SBill Wendling cast<ConstantExpr>(R)); 431*4d293f21SBill Wendling 432*4d293f21SBill Wendling // Constants of the "same type" don't always actually have the same 433*4d293f21SBill Wendling // type; I don't know why. Just white-list them. 434*4d293f21SBill Wendling if (isa<ConstantPointerNull>(L) || isa<UndefValue>(L) || isa<ConstantAggregateZero>(L)) 435*4d293f21SBill Wendling return true; 436*4d293f21SBill Wendling 437*4d293f21SBill Wendling // Block addresses only match if we've already encountered the 438*4d293f21SBill Wendling // block. FIXME: tentative matches? 439*4d293f21SBill Wendling if (isa<BlockAddress>(L)) 440*4d293f21SBill Wendling return Blocks[cast<BlockAddress>(L)->getBasicBlock()] 441*4d293f21SBill Wendling == cast<BlockAddress>(R)->getBasicBlock(); 442*4d293f21SBill Wendling 443*4d293f21SBill Wendling // If L and R are ConstantVectors, compare each element 444*4d293f21SBill Wendling if (isa<ConstantVector>(L)) { 445*4d293f21SBill Wendling const ConstantVector *CVL = cast<ConstantVector>(L); 446*4d293f21SBill Wendling const ConstantVector *CVR = cast<ConstantVector>(R); 447*4d293f21SBill Wendling if (CVL->getType()->getNumElements() != CVR->getType()->getNumElements()) 448*4d293f21SBill Wendling return false; 449*4d293f21SBill Wendling for (unsigned i = 0; i < CVL->getType()->getNumElements(); i++) { 450*4d293f21SBill Wendling if (!equivalentAsOperands(CVL->getOperand(i), CVR->getOperand(i))) 451*4d293f21SBill Wendling return false; 452*4d293f21SBill Wendling } 453*4d293f21SBill Wendling return true; 454*4d293f21SBill Wendling } 455*4d293f21SBill Wendling 456*4d293f21SBill Wendling // If L and R are ConstantArrays, compare the element count and types. 457*4d293f21SBill Wendling if (isa<ConstantArray>(L)) { 458*4d293f21SBill Wendling const ConstantArray *CAL = cast<ConstantArray>(L); 459*4d293f21SBill Wendling const ConstantArray *CAR = cast<ConstantArray>(R); 460*4d293f21SBill Wendling // Sometimes a type may be equivalent, but not uniquified---e.g. it may 461*4d293f21SBill Wendling // contain a GEP instruction. Do a deeper comparison of the types. 462*4d293f21SBill Wendling if (CAL->getType()->getNumElements() != CAR->getType()->getNumElements()) 463*4d293f21SBill Wendling return false; 464*4d293f21SBill Wendling 465*4d293f21SBill Wendling for (unsigned I = 0; I < CAL->getType()->getNumElements(); ++I) { 466*4d293f21SBill Wendling if (!equivalentAsOperands(CAL->getAggregateElement(I), 467*4d293f21SBill Wendling CAR->getAggregateElement(I))) 468*4d293f21SBill Wendling return false; 469*4d293f21SBill Wendling } 470*4d293f21SBill Wendling 471*4d293f21SBill Wendling return true; 472*4d293f21SBill Wendling } 473*4d293f21SBill Wendling 474*4d293f21SBill Wendling // If L and R are ConstantStructs, compare each field and type. 475*4d293f21SBill Wendling if (isa<ConstantStruct>(L)) { 476*4d293f21SBill Wendling const ConstantStruct *CSL = cast<ConstantStruct>(L); 477*4d293f21SBill Wendling const ConstantStruct *CSR = cast<ConstantStruct>(R); 478*4d293f21SBill Wendling 479*4d293f21SBill Wendling const StructType *LTy = cast<StructType>(CSL->getType()); 480*4d293f21SBill Wendling const StructType *RTy = cast<StructType>(CSR->getType()); 481*4d293f21SBill Wendling 482*4d293f21SBill Wendling // The StructTypes should have the same attributes. Don't use 483*4d293f21SBill Wendling // isLayoutIdentical(), because that just checks the element pointers, 484*4d293f21SBill Wendling // which may not work here. 485*4d293f21SBill Wendling if (LTy->getNumElements() != RTy->getNumElements() || 486*4d293f21SBill Wendling LTy->isPacked() != RTy->isPacked()) 487*4d293f21SBill Wendling return false; 488*4d293f21SBill Wendling 489*4d293f21SBill Wendling for (unsigned I = 0; I < LTy->getNumElements(); I++) { 490*4d293f21SBill Wendling const Value *LAgg = CSL->getAggregateElement(I); 491*4d293f21SBill Wendling const Value *RAgg = CSR->getAggregateElement(I); 492*4d293f21SBill Wendling 493*4d293f21SBill Wendling if (LAgg == SavedLHS || RAgg == SavedRHS) { 494*4d293f21SBill Wendling if (LAgg != SavedLHS || RAgg != SavedRHS) 495*4d293f21SBill Wendling // If the left and right operands aren't both re-analyzing the 496*4d293f21SBill Wendling // variable, then the initialiers don't match, so report "false". 497*4d293f21SBill Wendling // Otherwise, we skip these operands.. 498*4d293f21SBill Wendling return false; 499*4d293f21SBill Wendling 500*4d293f21SBill Wendling continue; 501*4d293f21SBill Wendling } 502*4d293f21SBill Wendling 503*4d293f21SBill Wendling if (!equivalentAsOperands(LAgg, RAgg)) { 504*4d293f21SBill Wendling return false; 505*4d293f21SBill Wendling } 506*4d293f21SBill Wendling } 507*4d293f21SBill Wendling 508*4d293f21SBill Wendling return true; 509*4d293f21SBill Wendling } 510*4d293f21SBill Wendling 511*4d293f21SBill Wendling return false; 512*4d293f21SBill Wendling } 513*4d293f21SBill Wendling 514*4d293f21SBill Wendling bool equivalentAsOperands(const ConstantExpr *L, const ConstantExpr *R) { 515*4d293f21SBill Wendling if (L == R) 516*4d293f21SBill Wendling return true; 517*4d293f21SBill Wendling 518*4d293f21SBill Wendling if (L->getOpcode() != R->getOpcode()) 519*4d293f21SBill Wendling return false; 520*4d293f21SBill Wendling 521*4d293f21SBill Wendling switch (L->getOpcode()) { 522*4d293f21SBill Wendling case Instruction::ICmp: 523*4d293f21SBill Wendling case Instruction::FCmp: 524*4d293f21SBill Wendling if (L->getPredicate() != R->getPredicate()) 525*4d293f21SBill Wendling return false; 526*4d293f21SBill Wendling break; 527*4d293f21SBill Wendling 528*4d293f21SBill Wendling case Instruction::GetElementPtr: 529*4d293f21SBill Wendling // FIXME: inbounds? 530*4d293f21SBill Wendling break; 531*4d293f21SBill Wendling 532*4d293f21SBill Wendling default: 533*4d293f21SBill Wendling break; 534*4d293f21SBill Wendling } 535*4d293f21SBill Wendling 536*4d293f21SBill Wendling if (L->getNumOperands() != R->getNumOperands()) 537*4d293f21SBill Wendling return false; 538*4d293f21SBill Wendling 539*4d293f21SBill Wendling for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) { 540*4d293f21SBill Wendling const auto *LOp = L->getOperand(I); 541*4d293f21SBill Wendling const auto *ROp = R->getOperand(I); 542*4d293f21SBill Wendling 543*4d293f21SBill Wendling if (LOp == SavedLHS || ROp == SavedRHS) { 544*4d293f21SBill Wendling if (LOp != SavedLHS || ROp != SavedRHS) 545*4d293f21SBill Wendling // If the left and right operands aren't both re-analyzing the 546*4d293f21SBill Wendling // variable, then the initialiers don't match, so report "false". 547*4d293f21SBill Wendling // Otherwise, we skip these operands.. 548*4d293f21SBill Wendling return false; 549*4d293f21SBill Wendling 550*4d293f21SBill Wendling continue; 551*4d293f21SBill Wendling } 552*4d293f21SBill Wendling 553*4d293f21SBill Wendling if (!equivalentAsOperands(LOp, ROp)) 554*4d293f21SBill Wendling return false; 555*4d293f21SBill Wendling } 556*4d293f21SBill Wendling 557*4d293f21SBill Wendling return true; 558*4d293f21SBill Wendling } 559*4d293f21SBill Wendling 560*4d293f21SBill Wendling bool equivalentAsOperands(const Value *L, const Value *R) { 561*4d293f21SBill Wendling // Fall out if the values have different kind. 562*4d293f21SBill Wendling // This possibly shouldn't take priority over oracles. 563*4d293f21SBill Wendling if (L->getValueID() != R->getValueID()) 564*4d293f21SBill Wendling return false; 565*4d293f21SBill Wendling 566*4d293f21SBill Wendling // Value subtypes: Argument, Constant, Instruction, BasicBlock, 567*4d293f21SBill Wendling // InlineAsm, MDNode, MDString, PseudoSourceValue 568*4d293f21SBill Wendling 569*4d293f21SBill Wendling if (isa<Constant>(L)) 570*4d293f21SBill Wendling return equivalentAsOperands(cast<Constant>(L), cast<Constant>(R)); 571*4d293f21SBill Wendling 572*4d293f21SBill Wendling if (isa<Instruction>(L)) 573*4d293f21SBill Wendling return Values[L] == R || TentativeValues.count(std::make_pair(L, R)); 574*4d293f21SBill Wendling 575*4d293f21SBill Wendling if (isa<Argument>(L)) 576*4d293f21SBill Wendling return Values[L] == R; 577*4d293f21SBill Wendling 578*4d293f21SBill Wendling if (isa<BasicBlock>(L)) 579*4d293f21SBill Wendling return Blocks[cast<BasicBlock>(L)] != R; 580*4d293f21SBill Wendling 581*4d293f21SBill Wendling // Pretend everything else is identical. 582*4d293f21SBill Wendling return true; 583*4d293f21SBill Wendling } 584*4d293f21SBill Wendling 585*4d293f21SBill Wendling // Avoid a gcc warning about accessing 'this' in an initializer. 586*4d293f21SBill Wendling FunctionDifferenceEngine *this_() { return this; } 587*4d293f21SBill Wendling 588*4d293f21SBill Wendling public: 589*4d293f21SBill Wendling FunctionDifferenceEngine(DifferenceEngine &Engine, 590*4d293f21SBill Wendling const Value *SavedLHS = nullptr, 591*4d293f21SBill Wendling const Value *SavedRHS = nullptr) 592*4d293f21SBill Wendling : Engine(Engine), SavedLHS(SavedLHS), SavedRHS(SavedRHS), 593*4d293f21SBill Wendling Queue(QueueSorter(*this_())) {} 594*4d293f21SBill Wendling 595*4d293f21SBill Wendling void diff(const Function *L, const Function *R) { 596*4d293f21SBill Wendling if (L->arg_size() != R->arg_size()) 597*4d293f21SBill Wendling Engine.log("different argument counts"); 598*4d293f21SBill Wendling 599*4d293f21SBill Wendling // Map the arguments. 600*4d293f21SBill Wendling for (Function::const_arg_iterator LI = L->arg_begin(), LE = L->arg_end(), 601*4d293f21SBill Wendling RI = R->arg_begin(), RE = R->arg_end(); 602*4d293f21SBill Wendling LI != LE && RI != RE; ++LI, ++RI) 603*4d293f21SBill Wendling Values[&*LI] = &*RI; 604*4d293f21SBill Wendling 605*4d293f21SBill Wendling tryUnify(&*L->begin(), &*R->begin()); 606*4d293f21SBill Wendling processQueue(); 607*4d293f21SBill Wendling } 608*4d293f21SBill Wendling }; 609*4d293f21SBill Wendling 610*4d293f21SBill Wendling struct DiffEntry { 611*4d293f21SBill Wendling DiffEntry() : Cost(0) {} 612*4d293f21SBill Wendling 613*4d293f21SBill Wendling unsigned Cost; 614*4d293f21SBill Wendling llvm::SmallVector<char, 8> Path; // actually of DifferenceEngine::DiffChange 615*4d293f21SBill Wendling }; 616*4d293f21SBill Wendling 617*4d293f21SBill Wendling bool FunctionDifferenceEngine::matchForBlockDiff(const Instruction *L, 618*4d293f21SBill Wendling const Instruction *R) { 619*4d293f21SBill Wendling return !diff(L, R, false, false); 620*4d293f21SBill Wendling } 621*4d293f21SBill Wendling 622*4d293f21SBill Wendling void FunctionDifferenceEngine::runBlockDiff(BasicBlock::const_iterator LStart, 623*4d293f21SBill Wendling BasicBlock::const_iterator RStart) { 624*4d293f21SBill Wendling BasicBlock::const_iterator LE = LStart->getParent()->end(); 625*4d293f21SBill Wendling BasicBlock::const_iterator RE = RStart->getParent()->end(); 626*4d293f21SBill Wendling 627*4d293f21SBill Wendling unsigned NL = std::distance(LStart, LE); 628*4d293f21SBill Wendling 629*4d293f21SBill Wendling SmallVector<DiffEntry, 20> Paths1(NL+1); 630*4d293f21SBill Wendling SmallVector<DiffEntry, 20> Paths2(NL+1); 631*4d293f21SBill Wendling 632*4d293f21SBill Wendling DiffEntry *Cur = Paths1.data(); 633*4d293f21SBill Wendling DiffEntry *Next = Paths2.data(); 634*4d293f21SBill Wendling 635*4d293f21SBill Wendling const unsigned LeftCost = 2; 636*4d293f21SBill Wendling const unsigned RightCost = 2; 637*4d293f21SBill Wendling const unsigned MatchCost = 0; 638*4d293f21SBill Wendling 639*4d293f21SBill Wendling assert(TentativeValues.empty()); 640*4d293f21SBill Wendling 641*4d293f21SBill Wendling // Initialize the first column. 642*4d293f21SBill Wendling for (unsigned I = 0; I != NL+1; ++I) { 643*4d293f21SBill Wendling Cur[I].Cost = I * LeftCost; 644*4d293f21SBill Wendling for (unsigned J = 0; J != I; ++J) 645*4d293f21SBill Wendling Cur[I].Path.push_back(DC_left); 646*4d293f21SBill Wendling } 647*4d293f21SBill Wendling 648*4d293f21SBill Wendling for (BasicBlock::const_iterator RI = RStart; RI != RE; ++RI) { 649*4d293f21SBill Wendling // Initialize the first row. 650*4d293f21SBill Wendling Next[0] = Cur[0]; 651*4d293f21SBill Wendling Next[0].Cost += RightCost; 652*4d293f21SBill Wendling Next[0].Path.push_back(DC_right); 653*4d293f21SBill Wendling 654*4d293f21SBill Wendling unsigned Index = 1; 655*4d293f21SBill Wendling for (BasicBlock::const_iterator LI = LStart; LI != LE; ++LI, ++Index) { 656*4d293f21SBill Wendling if (matchForBlockDiff(&*LI, &*RI)) { 657*4d293f21SBill Wendling Next[Index] = Cur[Index-1]; 658*4d293f21SBill Wendling Next[Index].Cost += MatchCost; 659*4d293f21SBill Wendling Next[Index].Path.push_back(DC_match); 660*4d293f21SBill Wendling TentativeValues.insert(std::make_pair(&*LI, &*RI)); 661*4d293f21SBill Wendling } else if (Next[Index-1].Cost <= Cur[Index].Cost) { 662*4d293f21SBill Wendling Next[Index] = Next[Index-1]; 663*4d293f21SBill Wendling Next[Index].Cost += LeftCost; 664*4d293f21SBill Wendling Next[Index].Path.push_back(DC_left); 665*4d293f21SBill Wendling } else { 666*4d293f21SBill Wendling Next[Index] = Cur[Index]; 667*4d293f21SBill Wendling Next[Index].Cost += RightCost; 668*4d293f21SBill Wendling Next[Index].Path.push_back(DC_right); 669*4d293f21SBill Wendling } 670*4d293f21SBill Wendling } 671*4d293f21SBill Wendling 672*4d293f21SBill Wendling std::swap(Cur, Next); 673*4d293f21SBill Wendling } 674*4d293f21SBill Wendling 675*4d293f21SBill Wendling // We don't need the tentative values anymore; everything from here 676*4d293f21SBill Wendling // on out should be non-tentative. 677*4d293f21SBill Wendling TentativeValues.clear(); 678*4d293f21SBill Wendling 679*4d293f21SBill Wendling SmallVectorImpl<char> &Path = Cur[NL].Path; 680*4d293f21SBill Wendling BasicBlock::const_iterator LI = LStart, RI = RStart; 681*4d293f21SBill Wendling 682*4d293f21SBill Wendling DiffLogBuilder Diff(Engine.getConsumer()); 683*4d293f21SBill Wendling 684*4d293f21SBill Wendling // Drop trailing matches. 685*4d293f21SBill Wendling while (Path.size() && Path.back() == DC_match) 686*4d293f21SBill Wendling Path.pop_back(); 687*4d293f21SBill Wendling 688*4d293f21SBill Wendling // Skip leading matches. 689*4d293f21SBill Wendling SmallVectorImpl<char>::iterator 690*4d293f21SBill Wendling PI = Path.begin(), PE = Path.end(); 691*4d293f21SBill Wendling while (PI != PE && *PI == DC_match) { 692*4d293f21SBill Wendling unify(&*LI, &*RI); 693*4d293f21SBill Wendling ++PI; 694*4d293f21SBill Wendling ++LI; 695*4d293f21SBill Wendling ++RI; 696*4d293f21SBill Wendling } 697*4d293f21SBill Wendling 698*4d293f21SBill Wendling for (; PI != PE; ++PI) { 699*4d293f21SBill Wendling switch (static_cast<DiffChange>(*PI)) { 700*4d293f21SBill Wendling case DC_match: 701*4d293f21SBill Wendling assert(LI != LE && RI != RE); 702*4d293f21SBill Wendling { 703*4d293f21SBill Wendling const Instruction *L = &*LI, *R = &*RI; 704*4d293f21SBill Wendling unify(L, R); 705*4d293f21SBill Wendling Diff.addMatch(L, R); 706*4d293f21SBill Wendling } 707*4d293f21SBill Wendling ++LI; ++RI; 708*4d293f21SBill Wendling break; 709*4d293f21SBill Wendling 710*4d293f21SBill Wendling case DC_left: 711*4d293f21SBill Wendling assert(LI != LE); 712*4d293f21SBill Wendling Diff.addLeft(&*LI); 713*4d293f21SBill Wendling ++LI; 714*4d293f21SBill Wendling break; 715*4d293f21SBill Wendling 716*4d293f21SBill Wendling case DC_right: 717*4d293f21SBill Wendling assert(RI != RE); 718*4d293f21SBill Wendling Diff.addRight(&*RI); 719*4d293f21SBill Wendling ++RI; 720*4d293f21SBill Wendling break; 721*4d293f21SBill Wendling } 722*4d293f21SBill Wendling } 723*4d293f21SBill Wendling 724*4d293f21SBill Wendling // Finishing unifying and complaining about the tails of the block, 725*4d293f21SBill Wendling // which should be matches all the way through. 726*4d293f21SBill Wendling while (LI != LE) { 727*4d293f21SBill Wendling assert(RI != RE); 728*4d293f21SBill Wendling unify(&*LI, &*RI); 729*4d293f21SBill Wendling ++LI; 730*4d293f21SBill Wendling ++RI; 731*4d293f21SBill Wendling } 732*4d293f21SBill Wendling 733*4d293f21SBill Wendling // If the terminators have different kinds, but one is an invoke and the 734*4d293f21SBill Wendling // other is an unconditional branch immediately following a call, unify 735*4d293f21SBill Wendling // the results and the destinations. 736*4d293f21SBill Wendling const Instruction *LTerm = LStart->getParent()->getTerminator(); 737*4d293f21SBill Wendling const Instruction *RTerm = RStart->getParent()->getTerminator(); 738*4d293f21SBill Wendling if (isa<BranchInst>(LTerm) && isa<InvokeInst>(RTerm)) { 739*4d293f21SBill Wendling if (cast<BranchInst>(LTerm)->isConditional()) return; 740*4d293f21SBill Wendling BasicBlock::const_iterator I = LTerm->getIterator(); 741*4d293f21SBill Wendling if (I == LStart->getParent()->begin()) return; 742*4d293f21SBill Wendling --I; 743*4d293f21SBill Wendling if (!isa<CallInst>(*I)) return; 744*4d293f21SBill Wendling const CallInst *LCall = cast<CallInst>(&*I); 745*4d293f21SBill Wendling const InvokeInst *RInvoke = cast<InvokeInst>(RTerm); 746*4d293f21SBill Wendling if (!equivalentAsOperands(LCall->getCalledOperand(), 747*4d293f21SBill Wendling RInvoke->getCalledOperand())) 748*4d293f21SBill Wendling return; 749*4d293f21SBill Wendling if (!LCall->use_empty()) 750*4d293f21SBill Wendling Values[LCall] = RInvoke; 751*4d293f21SBill Wendling tryUnify(LTerm->getSuccessor(0), RInvoke->getNormalDest()); 752*4d293f21SBill Wendling } else if (isa<InvokeInst>(LTerm) && isa<BranchInst>(RTerm)) { 753*4d293f21SBill Wendling if (cast<BranchInst>(RTerm)->isConditional()) return; 754*4d293f21SBill Wendling BasicBlock::const_iterator I = RTerm->getIterator(); 755*4d293f21SBill Wendling if (I == RStart->getParent()->begin()) return; 756*4d293f21SBill Wendling --I; 757*4d293f21SBill Wendling if (!isa<CallInst>(*I)) return; 758*4d293f21SBill Wendling const CallInst *RCall = cast<CallInst>(I); 759*4d293f21SBill Wendling const InvokeInst *LInvoke = cast<InvokeInst>(LTerm); 760*4d293f21SBill Wendling if (!equivalentAsOperands(LInvoke->getCalledOperand(), 761*4d293f21SBill Wendling RCall->getCalledOperand())) 762*4d293f21SBill Wendling return; 763*4d293f21SBill Wendling if (!LInvoke->use_empty()) 764*4d293f21SBill Wendling Values[LInvoke] = RCall; 765*4d293f21SBill Wendling tryUnify(LInvoke->getNormalDest(), RTerm->getSuccessor(0)); 766*4d293f21SBill Wendling } 767*4d293f21SBill Wendling } 768*4d293f21SBill Wendling } 769*4d293f21SBill Wendling 770*4d293f21SBill Wendling void DifferenceEngine::Oracle::anchor() { } 771*4d293f21SBill Wendling 772*4d293f21SBill Wendling void DifferenceEngine::diff(const Function *L, const Function *R) { 773*4d293f21SBill Wendling Context C(*this, L, R); 774*4d293f21SBill Wendling 775*4d293f21SBill Wendling // FIXME: types 776*4d293f21SBill Wendling // FIXME: attributes and CC 777*4d293f21SBill Wendling // FIXME: parameter attributes 778*4d293f21SBill Wendling 779*4d293f21SBill Wendling // If both are declarations, we're done. 780*4d293f21SBill Wendling if (L->empty() && R->empty()) 781*4d293f21SBill Wendling return; 782*4d293f21SBill Wendling else if (L->empty()) 783*4d293f21SBill Wendling log("left function is declaration, right function is definition"); 784*4d293f21SBill Wendling else if (R->empty()) 785*4d293f21SBill Wendling log("right function is declaration, left function is definition"); 786*4d293f21SBill Wendling else 787*4d293f21SBill Wendling FunctionDifferenceEngine(*this).diff(L, R); 788*4d293f21SBill Wendling } 789*4d293f21SBill Wendling 790*4d293f21SBill Wendling void DifferenceEngine::diff(const Module *L, const Module *R) { 791*4d293f21SBill Wendling StringSet<> LNames; 792*4d293f21SBill Wendling SmallVector<std::pair<const Function *, const Function *>, 20> Queue; 793*4d293f21SBill Wendling 794*4d293f21SBill Wendling unsigned LeftAnonCount = 0; 795*4d293f21SBill Wendling unsigned RightAnonCount = 0; 796*4d293f21SBill Wendling 797*4d293f21SBill Wendling for (Module::const_iterator I = L->begin(), E = L->end(); I != E; ++I) { 798*4d293f21SBill Wendling const Function *LFn = &*I; 799*4d293f21SBill Wendling StringRef Name = LFn->getName(); 800*4d293f21SBill Wendling if (Name.empty()) { 801*4d293f21SBill Wendling ++LeftAnonCount; 802*4d293f21SBill Wendling continue; 803*4d293f21SBill Wendling } 804*4d293f21SBill Wendling 805*4d293f21SBill Wendling LNames.insert(Name); 806*4d293f21SBill Wendling 807*4d293f21SBill Wendling if (Function *RFn = R->getFunction(LFn->getName())) 808*4d293f21SBill Wendling Queue.push_back(std::make_pair(LFn, RFn)); 809*4d293f21SBill Wendling else 810*4d293f21SBill Wendling logf("function %l exists only in left module") << LFn; 811*4d293f21SBill Wendling } 812*4d293f21SBill Wendling 813*4d293f21SBill Wendling for (Module::const_iterator I = R->begin(), E = R->end(); I != E; ++I) { 814*4d293f21SBill Wendling const Function *RFn = &*I; 815*4d293f21SBill Wendling StringRef Name = RFn->getName(); 816*4d293f21SBill Wendling if (Name.empty()) { 817*4d293f21SBill Wendling ++RightAnonCount; 818*4d293f21SBill Wendling continue; 819*4d293f21SBill Wendling } 820*4d293f21SBill Wendling 821*4d293f21SBill Wendling if (!LNames.count(Name)) 822*4d293f21SBill Wendling logf("function %r exists only in right module") << RFn; 823*4d293f21SBill Wendling } 824*4d293f21SBill Wendling 825*4d293f21SBill Wendling if (LeftAnonCount != 0 || RightAnonCount != 0) { 826*4d293f21SBill Wendling SmallString<32> Tmp; 827*4d293f21SBill Wendling logf(("not comparing " + Twine(LeftAnonCount) + 828*4d293f21SBill Wendling " anonymous functions in the left module and " + 829*4d293f21SBill Wendling Twine(RightAnonCount) + " in the right module") 830*4d293f21SBill Wendling .toStringRef(Tmp)); 831*4d293f21SBill Wendling } 832*4d293f21SBill Wendling 833*4d293f21SBill Wendling for (SmallVectorImpl<std::pair<const Function *, const Function *>>::iterator 834*4d293f21SBill Wendling I = Queue.begin(), 835*4d293f21SBill Wendling E = Queue.end(); 836*4d293f21SBill Wendling I != E; ++I) 837*4d293f21SBill Wendling diff(I->first, I->second); 838*4d293f21SBill Wendling } 839*4d293f21SBill Wendling 840*4d293f21SBill Wendling bool DifferenceEngine::equivalentAsOperands(const GlobalValue *L, 841*4d293f21SBill Wendling const GlobalValue *R) { 842*4d293f21SBill Wendling if (globalValueOracle) return (*globalValueOracle)(L, R); 843*4d293f21SBill Wendling 844*4d293f21SBill Wendling if (isa<GlobalVariable>(L) && isa<GlobalVariable>(R)) { 845*4d293f21SBill Wendling const GlobalVariable *GVL = cast<GlobalVariable>(L); 846*4d293f21SBill Wendling const GlobalVariable *GVR = cast<GlobalVariable>(R); 847*4d293f21SBill Wendling if (GVL->hasLocalLinkage() && GVL->hasUniqueInitializer() && 848*4d293f21SBill Wendling GVR->hasLocalLinkage() && GVR->hasUniqueInitializer()) 849*4d293f21SBill Wendling return FunctionDifferenceEngine(*this, GVL, GVR) 850*4d293f21SBill Wendling .equivalentAsOperands(GVL->getInitializer(), GVR->getInitializer()); 851*4d293f21SBill Wendling } 852*4d293f21SBill Wendling 853*4d293f21SBill Wendling return L->getName() == R->getName(); 854*4d293f21SBill Wendling } 855