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