120
|
1 //===- FunctionComparator.h - Function Comparator -------------------------===//
|
|
2 //
|
|
3 // The LLVM Compiler Infrastructure
|
|
4 //
|
|
5 // This file is distributed under the University of Illinois Open Source
|
|
6 // License. See LICENSE.TXT for details.
|
|
7 //
|
|
8 //===----------------------------------------------------------------------===//
|
|
9 //
|
|
10 // This file implements the FunctionComparator and GlobalNumberState classes
|
|
11 // which are used by the MergeFunctions pass for comparing functions.
|
|
12 //
|
|
13 //===----------------------------------------------------------------------===//
|
|
14
|
|
15 #include "llvm/Transforms/Utils/FunctionComparator.h"
|
121
|
16 #include "llvm/ADT/APFloat.h"
|
|
17 #include "llvm/ADT/APInt.h"
|
|
18 #include "llvm/ADT/ArrayRef.h"
|
|
19 #include "llvm/ADT/Hashing.h"
|
|
20 #include "llvm/ADT/SmallPtrSet.h"
|
120
|
21 #include "llvm/ADT/SmallSet.h"
|
121
|
22 #include "llvm/ADT/SmallVector.h"
|
|
23 #include "llvm/IR/Attributes.h"
|
|
24 #include "llvm/IR/BasicBlock.h"
|
120
|
25 #include "llvm/IR/CallSite.h"
|
121
|
26 #include "llvm/IR/Constant.h"
|
|
27 #include "llvm/IR/Constants.h"
|
|
28 #include "llvm/IR/DataLayout.h"
|
|
29 #include "llvm/IR/DerivedTypes.h"
|
|
30 #include "llvm/IR/Function.h"
|
|
31 #include "llvm/IR/GlobalValue.h"
|
120
|
32 #include "llvm/IR/InlineAsm.h"
|
121
|
33 #include "llvm/IR/InstrTypes.h"
|
|
34 #include "llvm/IR/Instruction.h"
|
|
35 #include "llvm/IR/Instructions.h"
|
|
36 #include "llvm/IR/LLVMContext.h"
|
|
37 #include "llvm/IR/Metadata.h"
|
120
|
38 #include "llvm/IR/Module.h"
|
121
|
39 #include "llvm/IR/Operator.h"
|
|
40 #include "llvm/IR/Type.h"
|
|
41 #include "llvm/IR/Value.h"
|
|
42 #include "llvm/Support/Casting.h"
|
|
43 #include "llvm/Support/Compiler.h"
|
120
|
44 #include "llvm/Support/Debug.h"
|
121
|
45 #include "llvm/Support/ErrorHandling.h"
|
120
|
46 #include "llvm/Support/raw_ostream.h"
|
121
|
47 #include <cassert>
|
|
48 #include <cstddef>
|
|
49 #include <cstdint>
|
|
50 #include <utility>
|
120
|
51
|
|
52 using namespace llvm;
|
|
53
|
|
54 #define DEBUG_TYPE "functioncomparator"
|
|
55
|
|
56 int FunctionComparator::cmpNumbers(uint64_t L, uint64_t R) const {
|
|
57 if (L < R) return -1;
|
|
58 if (L > R) return 1;
|
|
59 return 0;
|
|
60 }
|
|
61
|
|
62 int FunctionComparator::cmpOrderings(AtomicOrdering L, AtomicOrdering R) const {
|
|
63 if ((int)L < (int)R) return -1;
|
|
64 if ((int)L > (int)R) return 1;
|
|
65 return 0;
|
|
66 }
|
|
67
|
|
68 int FunctionComparator::cmpAPInts(const APInt &L, const APInt &R) const {
|
|
69 if (int Res = cmpNumbers(L.getBitWidth(), R.getBitWidth()))
|
|
70 return Res;
|
|
71 if (L.ugt(R)) return 1;
|
|
72 if (R.ugt(L)) return -1;
|
|
73 return 0;
|
|
74 }
|
|
75
|
|
76 int FunctionComparator::cmpAPFloats(const APFloat &L, const APFloat &R) const {
|
|
77 // Floats are ordered first by semantics (i.e. float, double, half, etc.),
|
|
78 // then by value interpreted as a bitstring (aka APInt).
|
|
79 const fltSemantics &SL = L.getSemantics(), &SR = R.getSemantics();
|
|
80 if (int Res = cmpNumbers(APFloat::semanticsPrecision(SL),
|
|
81 APFloat::semanticsPrecision(SR)))
|
|
82 return Res;
|
|
83 if (int Res = cmpNumbers(APFloat::semanticsMaxExponent(SL),
|
|
84 APFloat::semanticsMaxExponent(SR)))
|
|
85 return Res;
|
|
86 if (int Res = cmpNumbers(APFloat::semanticsMinExponent(SL),
|
|
87 APFloat::semanticsMinExponent(SR)))
|
|
88 return Res;
|
|
89 if (int Res = cmpNumbers(APFloat::semanticsSizeInBits(SL),
|
|
90 APFloat::semanticsSizeInBits(SR)))
|
|
91 return Res;
|
|
92 return cmpAPInts(L.bitcastToAPInt(), R.bitcastToAPInt());
|
|
93 }
|
|
94
|
|
95 int FunctionComparator::cmpMem(StringRef L, StringRef R) const {
|
|
96 // Prevent heavy comparison, compare sizes first.
|
|
97 if (int Res = cmpNumbers(L.size(), R.size()))
|
|
98 return Res;
|
|
99
|
|
100 // Compare strings lexicographically only when it is necessary: only when
|
|
101 // strings are equal in size.
|
|
102 return L.compare(R);
|
|
103 }
|
|
104
|
121
|
105 int FunctionComparator::cmpAttrs(const AttributeList L,
|
|
106 const AttributeList R) const {
|
|
107 if (int Res = cmpNumbers(L.getNumAttrSets(), R.getNumAttrSets()))
|
120
|
108 return Res;
|
|
109
|
121
|
110 for (unsigned i = L.index_begin(), e = L.index_end(); i != e; ++i) {
|
|
111 AttributeSet LAS = L.getAttributes(i);
|
|
112 AttributeSet RAS = R.getAttributes(i);
|
|
113 AttributeSet::iterator LI = LAS.begin(), LE = LAS.end();
|
|
114 AttributeSet::iterator RI = RAS.begin(), RE = RAS.end();
|
120
|
115 for (; LI != LE && RI != RE; ++LI, ++RI) {
|
|
116 Attribute LA = *LI;
|
|
117 Attribute RA = *RI;
|
|
118 if (LA < RA)
|
|
119 return -1;
|
|
120 if (RA < LA)
|
|
121 return 1;
|
|
122 }
|
|
123 if (LI != LE)
|
|
124 return 1;
|
|
125 if (RI != RE)
|
|
126 return -1;
|
|
127 }
|
|
128 return 0;
|
|
129 }
|
|
130
|
|
131 int FunctionComparator::cmpRangeMetadata(const MDNode *L,
|
|
132 const MDNode *R) const {
|
|
133 if (L == R)
|
|
134 return 0;
|
|
135 if (!L)
|
|
136 return -1;
|
|
137 if (!R)
|
|
138 return 1;
|
|
139 // Range metadata is a sequence of numbers. Make sure they are the same
|
|
140 // sequence.
|
|
141 // TODO: Note that as this is metadata, it is possible to drop and/or merge
|
|
142 // this data when considering functions to merge. Thus this comparison would
|
|
143 // return 0 (i.e. equivalent), but merging would become more complicated
|
|
144 // because the ranges would need to be unioned. It is not likely that
|
|
145 // functions differ ONLY in this metadata if they are actually the same
|
|
146 // function semantically.
|
|
147 if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands()))
|
|
148 return Res;
|
|
149 for (size_t I = 0; I < L->getNumOperands(); ++I) {
|
|
150 ConstantInt *LLow = mdconst::extract<ConstantInt>(L->getOperand(I));
|
|
151 ConstantInt *RLow = mdconst::extract<ConstantInt>(R->getOperand(I));
|
|
152 if (int Res = cmpAPInts(LLow->getValue(), RLow->getValue()))
|
|
153 return Res;
|
|
154 }
|
|
155 return 0;
|
|
156 }
|
|
157
|
|
158 int FunctionComparator::cmpOperandBundlesSchema(const Instruction *L,
|
|
159 const Instruction *R) const {
|
|
160 ImmutableCallSite LCS(L);
|
|
161 ImmutableCallSite RCS(R);
|
|
162
|
|
163 assert(LCS && RCS && "Must be calls or invokes!");
|
|
164 assert(LCS.isCall() == RCS.isCall() && "Can't compare otherwise!");
|
|
165
|
|
166 if (int Res =
|
|
167 cmpNumbers(LCS.getNumOperandBundles(), RCS.getNumOperandBundles()))
|
|
168 return Res;
|
|
169
|
|
170 for (unsigned i = 0, e = LCS.getNumOperandBundles(); i != e; ++i) {
|
|
171 auto OBL = LCS.getOperandBundleAt(i);
|
|
172 auto OBR = RCS.getOperandBundleAt(i);
|
|
173
|
|
174 if (int Res = OBL.getTagName().compare(OBR.getTagName()))
|
|
175 return Res;
|
|
176
|
|
177 if (int Res = cmpNumbers(OBL.Inputs.size(), OBR.Inputs.size()))
|
|
178 return Res;
|
|
179 }
|
|
180
|
|
181 return 0;
|
|
182 }
|
|
183
|
|
184 /// Constants comparison:
|
|
185 /// 1. Check whether type of L constant could be losslessly bitcasted to R
|
|
186 /// type.
|
|
187 /// 2. Compare constant contents.
|
|
188 /// For more details see declaration comments.
|
|
189 int FunctionComparator::cmpConstants(const Constant *L,
|
|
190 const Constant *R) const {
|
|
191 Type *TyL = L->getType();
|
|
192 Type *TyR = R->getType();
|
|
193
|
|
194 // Check whether types are bitcastable. This part is just re-factored
|
|
195 // Type::canLosslesslyBitCastTo method, but instead of returning true/false,
|
|
196 // we also pack into result which type is "less" for us.
|
|
197 int TypesRes = cmpTypes(TyL, TyR);
|
|
198 if (TypesRes != 0) {
|
|
199 // Types are different, but check whether we can bitcast them.
|
|
200 if (!TyL->isFirstClassType()) {
|
|
201 if (TyR->isFirstClassType())
|
|
202 return -1;
|
|
203 // Neither TyL nor TyR are values of first class type. Return the result
|
|
204 // of comparing the types
|
|
205 return TypesRes;
|
|
206 }
|
|
207 if (!TyR->isFirstClassType()) {
|
|
208 if (TyL->isFirstClassType())
|
|
209 return 1;
|
|
210 return TypesRes;
|
|
211 }
|
|
212
|
|
213 // Vector -> Vector conversions are always lossless if the two vector types
|
|
214 // have the same size, otherwise not.
|
|
215 unsigned TyLWidth = 0;
|
|
216 unsigned TyRWidth = 0;
|
|
217
|
|
218 if (auto *VecTyL = dyn_cast<VectorType>(TyL))
|
|
219 TyLWidth = VecTyL->getBitWidth();
|
|
220 if (auto *VecTyR = dyn_cast<VectorType>(TyR))
|
|
221 TyRWidth = VecTyR->getBitWidth();
|
|
222
|
|
223 if (TyLWidth != TyRWidth)
|
|
224 return cmpNumbers(TyLWidth, TyRWidth);
|
|
225
|
|
226 // Zero bit-width means neither TyL nor TyR are vectors.
|
|
227 if (!TyLWidth) {
|
|
228 PointerType *PTyL = dyn_cast<PointerType>(TyL);
|
|
229 PointerType *PTyR = dyn_cast<PointerType>(TyR);
|
|
230 if (PTyL && PTyR) {
|
|
231 unsigned AddrSpaceL = PTyL->getAddressSpace();
|
|
232 unsigned AddrSpaceR = PTyR->getAddressSpace();
|
|
233 if (int Res = cmpNumbers(AddrSpaceL, AddrSpaceR))
|
|
234 return Res;
|
|
235 }
|
|
236 if (PTyL)
|
|
237 return 1;
|
|
238 if (PTyR)
|
|
239 return -1;
|
|
240
|
|
241 // TyL and TyR aren't vectors, nor pointers. We don't know how to
|
|
242 // bitcast them.
|
|
243 return TypesRes;
|
|
244 }
|
|
245 }
|
|
246
|
|
247 // OK, types are bitcastable, now check constant contents.
|
|
248
|
|
249 if (L->isNullValue() && R->isNullValue())
|
|
250 return TypesRes;
|
|
251 if (L->isNullValue() && !R->isNullValue())
|
|
252 return 1;
|
|
253 if (!L->isNullValue() && R->isNullValue())
|
|
254 return -1;
|
|
255
|
121
|
256 auto GlobalValueL = const_cast<GlobalValue *>(dyn_cast<GlobalValue>(L));
|
|
257 auto GlobalValueR = const_cast<GlobalValue *>(dyn_cast<GlobalValue>(R));
|
120
|
258 if (GlobalValueL && GlobalValueR) {
|
|
259 return cmpGlobalValues(GlobalValueL, GlobalValueR);
|
|
260 }
|
|
261
|
|
262 if (int Res = cmpNumbers(L->getValueID(), R->getValueID()))
|
|
263 return Res;
|
|
264
|
|
265 if (const auto *SeqL = dyn_cast<ConstantDataSequential>(L)) {
|
|
266 const auto *SeqR = cast<ConstantDataSequential>(R);
|
|
267 // This handles ConstantDataArray and ConstantDataVector. Note that we
|
|
268 // compare the two raw data arrays, which might differ depending on the host
|
|
269 // endianness. This isn't a problem though, because the endiness of a module
|
|
270 // will affect the order of the constants, but this order is the same
|
|
271 // for a given input module and host platform.
|
|
272 return cmpMem(SeqL->getRawDataValues(), SeqR->getRawDataValues());
|
|
273 }
|
|
274
|
|
275 switch (L->getValueID()) {
|
|
276 case Value::UndefValueVal:
|
|
277 case Value::ConstantTokenNoneVal:
|
|
278 return TypesRes;
|
|
279 case Value::ConstantIntVal: {
|
|
280 const APInt &LInt = cast<ConstantInt>(L)->getValue();
|
|
281 const APInt &RInt = cast<ConstantInt>(R)->getValue();
|
|
282 return cmpAPInts(LInt, RInt);
|
|
283 }
|
|
284 case Value::ConstantFPVal: {
|
|
285 const APFloat &LAPF = cast<ConstantFP>(L)->getValueAPF();
|
|
286 const APFloat &RAPF = cast<ConstantFP>(R)->getValueAPF();
|
|
287 return cmpAPFloats(LAPF, RAPF);
|
|
288 }
|
|
289 case Value::ConstantArrayVal: {
|
|
290 const ConstantArray *LA = cast<ConstantArray>(L);
|
|
291 const ConstantArray *RA = cast<ConstantArray>(R);
|
|
292 uint64_t NumElementsL = cast<ArrayType>(TyL)->getNumElements();
|
|
293 uint64_t NumElementsR = cast<ArrayType>(TyR)->getNumElements();
|
|
294 if (int Res = cmpNumbers(NumElementsL, NumElementsR))
|
|
295 return Res;
|
|
296 for (uint64_t i = 0; i < NumElementsL; ++i) {
|
|
297 if (int Res = cmpConstants(cast<Constant>(LA->getOperand(i)),
|
|
298 cast<Constant>(RA->getOperand(i))))
|
|
299 return Res;
|
|
300 }
|
|
301 return 0;
|
|
302 }
|
|
303 case Value::ConstantStructVal: {
|
|
304 const ConstantStruct *LS = cast<ConstantStruct>(L);
|
|
305 const ConstantStruct *RS = cast<ConstantStruct>(R);
|
|
306 unsigned NumElementsL = cast<StructType>(TyL)->getNumElements();
|
|
307 unsigned NumElementsR = cast<StructType>(TyR)->getNumElements();
|
|
308 if (int Res = cmpNumbers(NumElementsL, NumElementsR))
|
|
309 return Res;
|
|
310 for (unsigned i = 0; i != NumElementsL; ++i) {
|
|
311 if (int Res = cmpConstants(cast<Constant>(LS->getOperand(i)),
|
|
312 cast<Constant>(RS->getOperand(i))))
|
|
313 return Res;
|
|
314 }
|
|
315 return 0;
|
|
316 }
|
|
317 case Value::ConstantVectorVal: {
|
|
318 const ConstantVector *LV = cast<ConstantVector>(L);
|
|
319 const ConstantVector *RV = cast<ConstantVector>(R);
|
|
320 unsigned NumElementsL = cast<VectorType>(TyL)->getNumElements();
|
|
321 unsigned NumElementsR = cast<VectorType>(TyR)->getNumElements();
|
|
322 if (int Res = cmpNumbers(NumElementsL, NumElementsR))
|
|
323 return Res;
|
|
324 for (uint64_t i = 0; i < NumElementsL; ++i) {
|
|
325 if (int Res = cmpConstants(cast<Constant>(LV->getOperand(i)),
|
|
326 cast<Constant>(RV->getOperand(i))))
|
|
327 return Res;
|
|
328 }
|
|
329 return 0;
|
|
330 }
|
|
331 case Value::ConstantExprVal: {
|
|
332 const ConstantExpr *LE = cast<ConstantExpr>(L);
|
|
333 const ConstantExpr *RE = cast<ConstantExpr>(R);
|
|
334 unsigned NumOperandsL = LE->getNumOperands();
|
|
335 unsigned NumOperandsR = RE->getNumOperands();
|
|
336 if (int Res = cmpNumbers(NumOperandsL, NumOperandsR))
|
|
337 return Res;
|
|
338 for (unsigned i = 0; i < NumOperandsL; ++i) {
|
|
339 if (int Res = cmpConstants(cast<Constant>(LE->getOperand(i)),
|
|
340 cast<Constant>(RE->getOperand(i))))
|
|
341 return Res;
|
|
342 }
|
|
343 return 0;
|
|
344 }
|
|
345 case Value::BlockAddressVal: {
|
|
346 const BlockAddress *LBA = cast<BlockAddress>(L);
|
|
347 const BlockAddress *RBA = cast<BlockAddress>(R);
|
|
348 if (int Res = cmpValues(LBA->getFunction(), RBA->getFunction()))
|
|
349 return Res;
|
|
350 if (LBA->getFunction() == RBA->getFunction()) {
|
|
351 // They are BBs in the same function. Order by which comes first in the
|
|
352 // BB order of the function. This order is deterministic.
|
|
353 Function* F = LBA->getFunction();
|
|
354 BasicBlock *LBB = LBA->getBasicBlock();
|
|
355 BasicBlock *RBB = RBA->getBasicBlock();
|
|
356 if (LBB == RBB)
|
|
357 return 0;
|
|
358 for(BasicBlock &BB : F->getBasicBlockList()) {
|
|
359 if (&BB == LBB) {
|
|
360 assert(&BB != RBB);
|
|
361 return -1;
|
|
362 }
|
|
363 if (&BB == RBB)
|
|
364 return 1;
|
|
365 }
|
|
366 llvm_unreachable("Basic Block Address does not point to a basic block in "
|
|
367 "its function.");
|
|
368 return -1;
|
|
369 } else {
|
|
370 // cmpValues said the functions are the same. So because they aren't
|
|
371 // literally the same pointer, they must respectively be the left and
|
|
372 // right functions.
|
|
373 assert(LBA->getFunction() == FnL && RBA->getFunction() == FnR);
|
|
374 // cmpValues will tell us if these are equivalent BasicBlocks, in the
|
|
375 // context of their respective functions.
|
|
376 return cmpValues(LBA->getBasicBlock(), RBA->getBasicBlock());
|
|
377 }
|
|
378 }
|
|
379 default: // Unknown constant, abort.
|
|
380 DEBUG(dbgs() << "Looking at valueID " << L->getValueID() << "\n");
|
|
381 llvm_unreachable("Constant ValueID not recognized.");
|
|
382 return -1;
|
|
383 }
|
|
384 }
|
|
385
|
|
386 int FunctionComparator::cmpGlobalValues(GlobalValue *L, GlobalValue *R) const {
|
|
387 uint64_t LNumber = GlobalNumbers->getNumber(L);
|
|
388 uint64_t RNumber = GlobalNumbers->getNumber(R);
|
|
389 return cmpNumbers(LNumber, RNumber);
|
|
390 }
|
|
391
|
|
392 /// cmpType - compares two types,
|
|
393 /// defines total ordering among the types set.
|
|
394 /// See method declaration comments for more details.
|
|
395 int FunctionComparator::cmpTypes(Type *TyL, Type *TyR) const {
|
|
396 PointerType *PTyL = dyn_cast<PointerType>(TyL);
|
|
397 PointerType *PTyR = dyn_cast<PointerType>(TyR);
|
|
398
|
|
399 const DataLayout &DL = FnL->getParent()->getDataLayout();
|
|
400 if (PTyL && PTyL->getAddressSpace() == 0)
|
|
401 TyL = DL.getIntPtrType(TyL);
|
|
402 if (PTyR && PTyR->getAddressSpace() == 0)
|
|
403 TyR = DL.getIntPtrType(TyR);
|
|
404
|
|
405 if (TyL == TyR)
|
|
406 return 0;
|
|
407
|
|
408 if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID()))
|
|
409 return Res;
|
|
410
|
|
411 switch (TyL->getTypeID()) {
|
|
412 default:
|
|
413 llvm_unreachable("Unknown type!");
|
|
414 // Fall through in Release mode.
|
|
415 LLVM_FALLTHROUGH;
|
|
416 case Type::IntegerTyID:
|
|
417 return cmpNumbers(cast<IntegerType>(TyL)->getBitWidth(),
|
|
418 cast<IntegerType>(TyR)->getBitWidth());
|
|
419 // TyL == TyR would have returned true earlier, because types are uniqued.
|
|
420 case Type::VoidTyID:
|
|
421 case Type::FloatTyID:
|
|
422 case Type::DoubleTyID:
|
|
423 case Type::X86_FP80TyID:
|
|
424 case Type::FP128TyID:
|
|
425 case Type::PPC_FP128TyID:
|
|
426 case Type::LabelTyID:
|
|
427 case Type::MetadataTyID:
|
|
428 case Type::TokenTyID:
|
126
|
429 #ifndef noCbC
|
|
430 case Type::__CodeTyID:
|
|
431 #endif
|
120
|
432 return 0;
|
|
433
|
121
|
434 case Type::PointerTyID:
|
120
|
435 assert(PTyL && PTyR && "Both types must be pointers here.");
|
|
436 return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace());
|
|
437
|
|
438 case Type::StructTyID: {
|
|
439 StructType *STyL = cast<StructType>(TyL);
|
|
440 StructType *STyR = cast<StructType>(TyR);
|
|
441 if (STyL->getNumElements() != STyR->getNumElements())
|
|
442 return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
|
|
443
|
|
444 if (STyL->isPacked() != STyR->isPacked())
|
|
445 return cmpNumbers(STyL->isPacked(), STyR->isPacked());
|
|
446
|
|
447 for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) {
|
|
448 if (int Res = cmpTypes(STyL->getElementType(i), STyR->getElementType(i)))
|
|
449 return Res;
|
|
450 }
|
|
451 return 0;
|
|
452 }
|
|
453
|
|
454 case Type::FunctionTyID: {
|
|
455 FunctionType *FTyL = cast<FunctionType>(TyL);
|
|
456 FunctionType *FTyR = cast<FunctionType>(TyR);
|
|
457 if (FTyL->getNumParams() != FTyR->getNumParams())
|
|
458 return cmpNumbers(FTyL->getNumParams(), FTyR->getNumParams());
|
|
459
|
|
460 if (FTyL->isVarArg() != FTyR->isVarArg())
|
|
461 return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg());
|
|
462
|
|
463 if (int Res = cmpTypes(FTyL->getReturnType(), FTyR->getReturnType()))
|
|
464 return Res;
|
|
465
|
|
466 for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) {
|
|
467 if (int Res = cmpTypes(FTyL->getParamType(i), FTyR->getParamType(i)))
|
|
468 return Res;
|
|
469 }
|
|
470 return 0;
|
|
471 }
|
|
472
|
121
|
473 case Type::ArrayTyID:
|
|
474 case Type::VectorTyID: {
|
|
475 auto *STyL = cast<SequentialType>(TyL);
|
|
476 auto *STyR = cast<SequentialType>(TyR);
|
|
477 if (STyL->getNumElements() != STyR->getNumElements())
|
|
478 return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
|
|
479 return cmpTypes(STyL->getElementType(), STyR->getElementType());
|
120
|
480 }
|
|
481 }
|
|
482 }
|
|
483
|
|
484 // Determine whether the two operations are the same except that pointer-to-A
|
|
485 // and pointer-to-B are equivalent. This should be kept in sync with
|
|
486 // Instruction::isSameOperationAs.
|
|
487 // Read method declaration comments for more details.
|
|
488 int FunctionComparator::cmpOperations(const Instruction *L,
|
|
489 const Instruction *R,
|
|
490 bool &needToCmpOperands) const {
|
|
491 needToCmpOperands = true;
|
|
492 if (int Res = cmpValues(L, R))
|
|
493 return Res;
|
|
494
|
|
495 // Differences from Instruction::isSameOperationAs:
|
|
496 // * replace type comparison with calls to cmpTypes.
|
|
497 // * we test for I->getRawSubclassOptionalData (nuw/nsw/tail) at the top.
|
|
498 // * because of the above, we don't test for the tail bit on calls later on.
|
|
499 if (int Res = cmpNumbers(L->getOpcode(), R->getOpcode()))
|
|
500 return Res;
|
|
501
|
|
502 if (const GetElementPtrInst *GEPL = dyn_cast<GetElementPtrInst>(L)) {
|
|
503 needToCmpOperands = false;
|
|
504 const GetElementPtrInst *GEPR = cast<GetElementPtrInst>(R);
|
|
505 if (int Res =
|
|
506 cmpValues(GEPL->getPointerOperand(), GEPR->getPointerOperand()))
|
|
507 return Res;
|
|
508 return cmpGEPs(GEPL, GEPR);
|
|
509 }
|
|
510
|
|
511 if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands()))
|
|
512 return Res;
|
|
513
|
|
514 if (int Res = cmpTypes(L->getType(), R->getType()))
|
|
515 return Res;
|
|
516
|
|
517 if (int Res = cmpNumbers(L->getRawSubclassOptionalData(),
|
|
518 R->getRawSubclassOptionalData()))
|
|
519 return Res;
|
|
520
|
|
521 // We have two instructions of identical opcode and #operands. Check to see
|
|
522 // if all operands are the same type
|
|
523 for (unsigned i = 0, e = L->getNumOperands(); i != e; ++i) {
|
|
524 if (int Res =
|
|
525 cmpTypes(L->getOperand(i)->getType(), R->getOperand(i)->getType()))
|
|
526 return Res;
|
|
527 }
|
|
528
|
|
529 // Check special state that is a part of some instructions.
|
|
530 if (const AllocaInst *AI = dyn_cast<AllocaInst>(L)) {
|
|
531 if (int Res = cmpTypes(AI->getAllocatedType(),
|
|
532 cast<AllocaInst>(R)->getAllocatedType()))
|
|
533 return Res;
|
|
534 return cmpNumbers(AI->getAlignment(), cast<AllocaInst>(R)->getAlignment());
|
|
535 }
|
|
536 if (const LoadInst *LI = dyn_cast<LoadInst>(L)) {
|
|
537 if (int Res = cmpNumbers(LI->isVolatile(), cast<LoadInst>(R)->isVolatile()))
|
|
538 return Res;
|
|
539 if (int Res =
|
|
540 cmpNumbers(LI->getAlignment(), cast<LoadInst>(R)->getAlignment()))
|
|
541 return Res;
|
|
542 if (int Res =
|
|
543 cmpOrderings(LI->getOrdering(), cast<LoadInst>(R)->getOrdering()))
|
|
544 return Res;
|
121
|
545 if (int Res = cmpNumbers(LI->getSyncScopeID(),
|
|
546 cast<LoadInst>(R)->getSyncScopeID()))
|
120
|
547 return Res;
|
|
548 return cmpRangeMetadata(LI->getMetadata(LLVMContext::MD_range),
|
|
549 cast<LoadInst>(R)->getMetadata(LLVMContext::MD_range));
|
|
550 }
|
|
551 if (const StoreInst *SI = dyn_cast<StoreInst>(L)) {
|
|
552 if (int Res =
|
|
553 cmpNumbers(SI->isVolatile(), cast<StoreInst>(R)->isVolatile()))
|
|
554 return Res;
|
|
555 if (int Res =
|
|
556 cmpNumbers(SI->getAlignment(), cast<StoreInst>(R)->getAlignment()))
|
|
557 return Res;
|
|
558 if (int Res =
|
|
559 cmpOrderings(SI->getOrdering(), cast<StoreInst>(R)->getOrdering()))
|
|
560 return Res;
|
121
|
561 return cmpNumbers(SI->getSyncScopeID(),
|
|
562 cast<StoreInst>(R)->getSyncScopeID());
|
120
|
563 }
|
|
564 if (const CmpInst *CI = dyn_cast<CmpInst>(L))
|
|
565 return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate());
|
|
566 if (const CallInst *CI = dyn_cast<CallInst>(L)) {
|
|
567 if (int Res = cmpNumbers(CI->getCallingConv(),
|
|
568 cast<CallInst>(R)->getCallingConv()))
|
|
569 return Res;
|
|
570 if (int Res =
|
|
571 cmpAttrs(CI->getAttributes(), cast<CallInst>(R)->getAttributes()))
|
|
572 return Res;
|
|
573 if (int Res = cmpOperandBundlesSchema(CI, R))
|
|
574 return Res;
|
|
575 return cmpRangeMetadata(
|
|
576 CI->getMetadata(LLVMContext::MD_range),
|
|
577 cast<CallInst>(R)->getMetadata(LLVMContext::MD_range));
|
|
578 }
|
|
579 if (const InvokeInst *II = dyn_cast<InvokeInst>(L)) {
|
|
580 if (int Res = cmpNumbers(II->getCallingConv(),
|
|
581 cast<InvokeInst>(R)->getCallingConv()))
|
|
582 return Res;
|
|
583 if (int Res =
|
|
584 cmpAttrs(II->getAttributes(), cast<InvokeInst>(R)->getAttributes()))
|
|
585 return Res;
|
|
586 if (int Res = cmpOperandBundlesSchema(II, R))
|
|
587 return Res;
|
|
588 return cmpRangeMetadata(
|
|
589 II->getMetadata(LLVMContext::MD_range),
|
|
590 cast<InvokeInst>(R)->getMetadata(LLVMContext::MD_range));
|
|
591 }
|
|
592 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) {
|
|
593 ArrayRef<unsigned> LIndices = IVI->getIndices();
|
|
594 ArrayRef<unsigned> RIndices = cast<InsertValueInst>(R)->getIndices();
|
|
595 if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
|
|
596 return Res;
|
|
597 for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
|
|
598 if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
|
|
599 return Res;
|
|
600 }
|
|
601 return 0;
|
|
602 }
|
|
603 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(L)) {
|
|
604 ArrayRef<unsigned> LIndices = EVI->getIndices();
|
|
605 ArrayRef<unsigned> RIndices = cast<ExtractValueInst>(R)->getIndices();
|
|
606 if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
|
|
607 return Res;
|
|
608 for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
|
|
609 if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
|
|
610 return Res;
|
|
611 }
|
|
612 }
|
|
613 if (const FenceInst *FI = dyn_cast<FenceInst>(L)) {
|
|
614 if (int Res =
|
|
615 cmpOrderings(FI->getOrdering(), cast<FenceInst>(R)->getOrdering()))
|
|
616 return Res;
|
121
|
617 return cmpNumbers(FI->getSyncScopeID(),
|
|
618 cast<FenceInst>(R)->getSyncScopeID());
|
120
|
619 }
|
|
620 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(L)) {
|
|
621 if (int Res = cmpNumbers(CXI->isVolatile(),
|
|
622 cast<AtomicCmpXchgInst>(R)->isVolatile()))
|
|
623 return Res;
|
|
624 if (int Res = cmpNumbers(CXI->isWeak(),
|
|
625 cast<AtomicCmpXchgInst>(R)->isWeak()))
|
|
626 return Res;
|
|
627 if (int Res =
|
|
628 cmpOrderings(CXI->getSuccessOrdering(),
|
|
629 cast<AtomicCmpXchgInst>(R)->getSuccessOrdering()))
|
|
630 return Res;
|
|
631 if (int Res =
|
|
632 cmpOrderings(CXI->getFailureOrdering(),
|
|
633 cast<AtomicCmpXchgInst>(R)->getFailureOrdering()))
|
|
634 return Res;
|
121
|
635 return cmpNumbers(CXI->getSyncScopeID(),
|
|
636 cast<AtomicCmpXchgInst>(R)->getSyncScopeID());
|
120
|
637 }
|
|
638 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) {
|
|
639 if (int Res = cmpNumbers(RMWI->getOperation(),
|
|
640 cast<AtomicRMWInst>(R)->getOperation()))
|
|
641 return Res;
|
|
642 if (int Res = cmpNumbers(RMWI->isVolatile(),
|
|
643 cast<AtomicRMWInst>(R)->isVolatile()))
|
|
644 return Res;
|
|
645 if (int Res = cmpOrderings(RMWI->getOrdering(),
|
|
646 cast<AtomicRMWInst>(R)->getOrdering()))
|
|
647 return Res;
|
121
|
648 return cmpNumbers(RMWI->getSyncScopeID(),
|
|
649 cast<AtomicRMWInst>(R)->getSyncScopeID());
|
120
|
650 }
|
|
651 if (const PHINode *PNL = dyn_cast<PHINode>(L)) {
|
|
652 const PHINode *PNR = cast<PHINode>(R);
|
|
653 // Ensure that in addition to the incoming values being identical
|
|
654 // (checked by the caller of this function), the incoming blocks
|
|
655 // are also identical.
|
|
656 for (unsigned i = 0, e = PNL->getNumIncomingValues(); i != e; ++i) {
|
|
657 if (int Res =
|
|
658 cmpValues(PNL->getIncomingBlock(i), PNR->getIncomingBlock(i)))
|
|
659 return Res;
|
|
660 }
|
|
661 }
|
|
662 return 0;
|
|
663 }
|
|
664
|
|
665 // Determine whether two GEP operations perform the same underlying arithmetic.
|
|
666 // Read method declaration comments for more details.
|
|
667 int FunctionComparator::cmpGEPs(const GEPOperator *GEPL,
|
|
668 const GEPOperator *GEPR) const {
|
|
669 unsigned int ASL = GEPL->getPointerAddressSpace();
|
|
670 unsigned int ASR = GEPR->getPointerAddressSpace();
|
|
671
|
|
672 if (int Res = cmpNumbers(ASL, ASR))
|
|
673 return Res;
|
|
674
|
|
675 // When we have target data, we can reduce the GEP down to the value in bytes
|
|
676 // added to the address.
|
|
677 const DataLayout &DL = FnL->getParent()->getDataLayout();
|
|
678 unsigned BitWidth = DL.getPointerSizeInBits(ASL);
|
|
679 APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0);
|
|
680 if (GEPL->accumulateConstantOffset(DL, OffsetL) &&
|
|
681 GEPR->accumulateConstantOffset(DL, OffsetR))
|
|
682 return cmpAPInts(OffsetL, OffsetR);
|
|
683 if (int Res = cmpTypes(GEPL->getSourceElementType(),
|
|
684 GEPR->getSourceElementType()))
|
|
685 return Res;
|
|
686
|
|
687 if (int Res = cmpNumbers(GEPL->getNumOperands(), GEPR->getNumOperands()))
|
|
688 return Res;
|
|
689
|
|
690 for (unsigned i = 0, e = GEPL->getNumOperands(); i != e; ++i) {
|
|
691 if (int Res = cmpValues(GEPL->getOperand(i), GEPR->getOperand(i)))
|
|
692 return Res;
|
|
693 }
|
|
694
|
|
695 return 0;
|
|
696 }
|
|
697
|
|
698 int FunctionComparator::cmpInlineAsm(const InlineAsm *L,
|
|
699 const InlineAsm *R) const {
|
|
700 // InlineAsm's are uniqued. If they are the same pointer, obviously they are
|
|
701 // the same, otherwise compare the fields.
|
|
702 if (L == R)
|
|
703 return 0;
|
|
704 if (int Res = cmpTypes(L->getFunctionType(), R->getFunctionType()))
|
|
705 return Res;
|
|
706 if (int Res = cmpMem(L->getAsmString(), R->getAsmString()))
|
|
707 return Res;
|
|
708 if (int Res = cmpMem(L->getConstraintString(), R->getConstraintString()))
|
|
709 return Res;
|
|
710 if (int Res = cmpNumbers(L->hasSideEffects(), R->hasSideEffects()))
|
|
711 return Res;
|
|
712 if (int Res = cmpNumbers(L->isAlignStack(), R->isAlignStack()))
|
|
713 return Res;
|
|
714 if (int Res = cmpNumbers(L->getDialect(), R->getDialect()))
|
|
715 return Res;
|
|
716 llvm_unreachable("InlineAsm blocks were not uniqued.");
|
|
717 return 0;
|
|
718 }
|
|
719
|
|
720 /// Compare two values used by the two functions under pair-wise comparison. If
|
|
721 /// this is the first time the values are seen, they're added to the mapping so
|
|
722 /// that we will detect mismatches on next use.
|
|
723 /// See comments in declaration for more details.
|
|
724 int FunctionComparator::cmpValues(const Value *L, const Value *R) const {
|
|
725 // Catch self-reference case.
|
|
726 if (L == FnL) {
|
|
727 if (R == FnR)
|
|
728 return 0;
|
|
729 return -1;
|
|
730 }
|
|
731 if (R == FnR) {
|
|
732 if (L == FnL)
|
|
733 return 0;
|
|
734 return 1;
|
|
735 }
|
|
736
|
|
737 const Constant *ConstL = dyn_cast<Constant>(L);
|
|
738 const Constant *ConstR = dyn_cast<Constant>(R);
|
|
739 if (ConstL && ConstR) {
|
|
740 if (L == R)
|
|
741 return 0;
|
|
742 return cmpConstants(ConstL, ConstR);
|
|
743 }
|
|
744
|
|
745 if (ConstL)
|
|
746 return 1;
|
|
747 if (ConstR)
|
|
748 return -1;
|
|
749
|
|
750 const InlineAsm *InlineAsmL = dyn_cast<InlineAsm>(L);
|
|
751 const InlineAsm *InlineAsmR = dyn_cast<InlineAsm>(R);
|
|
752
|
|
753 if (InlineAsmL && InlineAsmR)
|
|
754 return cmpInlineAsm(InlineAsmL, InlineAsmR);
|
|
755 if (InlineAsmL)
|
|
756 return 1;
|
|
757 if (InlineAsmR)
|
|
758 return -1;
|
|
759
|
|
760 auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())),
|
|
761 RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size()));
|
|
762
|
|
763 return cmpNumbers(LeftSN.first->second, RightSN.first->second);
|
|
764 }
|
|
765
|
|
766 // Test whether two basic blocks have equivalent behaviour.
|
|
767 int FunctionComparator::cmpBasicBlocks(const BasicBlock *BBL,
|
|
768 const BasicBlock *BBR) const {
|
|
769 BasicBlock::const_iterator InstL = BBL->begin(), InstLE = BBL->end();
|
|
770 BasicBlock::const_iterator InstR = BBR->begin(), InstRE = BBR->end();
|
|
771
|
|
772 do {
|
|
773 bool needToCmpOperands = true;
|
|
774 if (int Res = cmpOperations(&*InstL, &*InstR, needToCmpOperands))
|
|
775 return Res;
|
|
776 if (needToCmpOperands) {
|
|
777 assert(InstL->getNumOperands() == InstR->getNumOperands());
|
|
778
|
|
779 for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) {
|
|
780 Value *OpL = InstL->getOperand(i);
|
|
781 Value *OpR = InstR->getOperand(i);
|
|
782 if (int Res = cmpValues(OpL, OpR))
|
|
783 return Res;
|
|
784 // cmpValues should ensure this is true.
|
|
785 assert(cmpTypes(OpL->getType(), OpR->getType()) == 0);
|
|
786 }
|
|
787 }
|
|
788
|
|
789 ++InstL;
|
|
790 ++InstR;
|
|
791 } while (InstL != InstLE && InstR != InstRE);
|
|
792
|
|
793 if (InstL != InstLE && InstR == InstRE)
|
|
794 return 1;
|
|
795 if (InstL == InstLE && InstR != InstRE)
|
|
796 return -1;
|
|
797 return 0;
|
|
798 }
|
|
799
|
|
800 int FunctionComparator::compareSignature() const {
|
|
801 if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes()))
|
|
802 return Res;
|
|
803
|
|
804 if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC()))
|
|
805 return Res;
|
|
806
|
|
807 if (FnL->hasGC()) {
|
|
808 if (int Res = cmpMem(FnL->getGC(), FnR->getGC()))
|
|
809 return Res;
|
|
810 }
|
|
811
|
|
812 if (int Res = cmpNumbers(FnL->hasSection(), FnR->hasSection()))
|
|
813 return Res;
|
|
814
|
|
815 if (FnL->hasSection()) {
|
|
816 if (int Res = cmpMem(FnL->getSection(), FnR->getSection()))
|
|
817 return Res;
|
|
818 }
|
|
819
|
|
820 if (int Res = cmpNumbers(FnL->isVarArg(), FnR->isVarArg()))
|
|
821 return Res;
|
|
822
|
|
823 // TODO: if it's internal and only used in direct calls, we could handle this
|
|
824 // case too.
|
|
825 if (int Res = cmpNumbers(FnL->getCallingConv(), FnR->getCallingConv()))
|
|
826 return Res;
|
|
827
|
|
828 if (int Res = cmpTypes(FnL->getFunctionType(), FnR->getFunctionType()))
|
|
829 return Res;
|
|
830
|
|
831 assert(FnL->arg_size() == FnR->arg_size() &&
|
|
832 "Identically typed functions have different numbers of args!");
|
|
833
|
|
834 // Visit the arguments so that they get enumerated in the order they're
|
|
835 // passed in.
|
|
836 for (Function::const_arg_iterator ArgLI = FnL->arg_begin(),
|
|
837 ArgRI = FnR->arg_begin(),
|
|
838 ArgLE = FnL->arg_end();
|
|
839 ArgLI != ArgLE; ++ArgLI, ++ArgRI) {
|
|
840 if (cmpValues(&*ArgLI, &*ArgRI) != 0)
|
|
841 llvm_unreachable("Arguments repeat!");
|
|
842 }
|
|
843 return 0;
|
|
844 }
|
|
845
|
|
846 // Test whether the two functions have equivalent behaviour.
|
|
847 int FunctionComparator::compare() {
|
|
848 beginCompare();
|
|
849
|
|
850 if (int Res = compareSignature())
|
|
851 return Res;
|
|
852
|
|
853 // We do a CFG-ordered walk since the actual ordering of the blocks in the
|
|
854 // linked list is immaterial. Our walk starts at the entry block for both
|
|
855 // functions, then takes each block from each terminator in order. As an
|
|
856 // artifact, this also means that unreachable blocks are ignored.
|
|
857 SmallVector<const BasicBlock *, 8> FnLBBs, FnRBBs;
|
|
858 SmallPtrSet<const BasicBlock *, 32> VisitedBBs; // in terms of F1.
|
|
859
|
|
860 FnLBBs.push_back(&FnL->getEntryBlock());
|
|
861 FnRBBs.push_back(&FnR->getEntryBlock());
|
|
862
|
|
863 VisitedBBs.insert(FnLBBs[0]);
|
|
864 while (!FnLBBs.empty()) {
|
|
865 const BasicBlock *BBL = FnLBBs.pop_back_val();
|
|
866 const BasicBlock *BBR = FnRBBs.pop_back_val();
|
|
867
|
|
868 if (int Res = cmpValues(BBL, BBR))
|
|
869 return Res;
|
|
870
|
|
871 if (int Res = cmpBasicBlocks(BBL, BBR))
|
|
872 return Res;
|
|
873
|
|
874 const TerminatorInst *TermL = BBL->getTerminator();
|
|
875 const TerminatorInst *TermR = BBR->getTerminator();
|
|
876
|
|
877 assert(TermL->getNumSuccessors() == TermR->getNumSuccessors());
|
|
878 for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) {
|
|
879 if (!VisitedBBs.insert(TermL->getSuccessor(i)).second)
|
|
880 continue;
|
|
881
|
|
882 FnLBBs.push_back(TermL->getSuccessor(i));
|
|
883 FnRBBs.push_back(TermR->getSuccessor(i));
|
|
884 }
|
|
885 }
|
|
886 return 0;
|
|
887 }
|
|
888
|
|
889 namespace {
|
|
890
|
|
891 // Accumulate the hash of a sequence of 64-bit integers. This is similar to a
|
|
892 // hash of a sequence of 64bit ints, but the entire input does not need to be
|
|
893 // available at once. This interface is necessary for functionHash because it
|
|
894 // needs to accumulate the hash as the structure of the function is traversed
|
|
895 // without saving these values to an intermediate buffer. This form of hashing
|
|
896 // is not often needed, as usually the object to hash is just read from a
|
|
897 // buffer.
|
|
898 class HashAccumulator64 {
|
|
899 uint64_t Hash;
|
121
|
900
|
120
|
901 public:
|
|
902 // Initialize to random constant, so the state isn't zero.
|
|
903 HashAccumulator64() { Hash = 0x6acaa36bef8325c5ULL; }
|
121
|
904
|
120
|
905 void add(uint64_t V) {
|
121
|
906 Hash = hashing::detail::hash_16_bytes(Hash, V);
|
120
|
907 }
|
121
|
908
|
120
|
909 // No finishing is required, because the entire hash value is used.
|
|
910 uint64_t getHash() { return Hash; }
|
|
911 };
|
121
|
912
|
120
|
913 } // end anonymous namespace
|
|
914
|
|
915 // A function hash is calculated by considering only the number of arguments and
|
|
916 // whether a function is varargs, the order of basic blocks (given by the
|
|
917 // successors of each basic block in depth first order), and the order of
|
|
918 // opcodes of each instruction within each of these basic blocks. This mirrors
|
|
919 // the strategy compare() uses to compare functions by walking the BBs in depth
|
|
920 // first order and comparing each instruction in sequence. Because this hash
|
|
921 // does not look at the operands, it is insensitive to things such as the
|
|
922 // target of calls and the constants used in the function, which makes it useful
|
|
923 // when possibly merging functions which are the same modulo constants and call
|
|
924 // targets.
|
|
925 FunctionComparator::FunctionHash FunctionComparator::functionHash(Function &F) {
|
|
926 HashAccumulator64 H;
|
|
927 H.add(F.isVarArg());
|
|
928 H.add(F.arg_size());
|
|
929
|
|
930 SmallVector<const BasicBlock *, 8> BBs;
|
|
931 SmallSet<const BasicBlock *, 16> VisitedBBs;
|
|
932
|
|
933 // Walk the blocks in the same order as FunctionComparator::cmpBasicBlocks(),
|
|
934 // accumulating the hash of the function "structure." (BB and opcode sequence)
|
|
935 BBs.push_back(&F.getEntryBlock());
|
|
936 VisitedBBs.insert(BBs[0]);
|
|
937 while (!BBs.empty()) {
|
|
938 const BasicBlock *BB = BBs.pop_back_val();
|
|
939 // This random value acts as a block header, as otherwise the partition of
|
|
940 // opcodes into BBs wouldn't affect the hash, only the order of the opcodes
|
|
941 H.add(45798);
|
|
942 for (auto &Inst : *BB) {
|
|
943 H.add(Inst.getOpcode());
|
|
944 }
|
|
945 const TerminatorInst *Term = BB->getTerminator();
|
|
946 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
|
|
947 if (!VisitedBBs.insert(Term->getSuccessor(i)).second)
|
|
948 continue;
|
|
949 BBs.push_back(Term->getSuccessor(i));
|
|
950 }
|
|
951 }
|
|
952 return H.getHash();
|
|
953 }
|