Coverage Report

Created: 2026-06-08 19:29

/home/runner/work/DirectXShaderCompiler/DirectXShaderCompiler/lib/Transforms/IPO/MergeFunctions.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MergeFunctions.cpp - Merge identical functions ---------------------===//
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 pass looks for equivalent functions that are mergable and folds them.
11
//
12
// Order relation is defined on set of functions. It was made through
13
// special function comparison procedure that returns
14
// 0 when functions are equal,
15
// -1 when Left function is less than right function, and
16
// 1 for opposite case. We need total-ordering, so we need to maintain
17
// four properties on the functions set:
18
// a <= a (reflexivity)
19
// if a <= b and b <= a then a = b (antisymmetry)
20
// if a <= b and b <= c then a <= c (transitivity).
21
// for all a and b: a <= b or b <= a (totality).
22
//
23
// Comparison iterates through each instruction in each basic block.
24
// Functions are kept on binary tree. For each new function F we perform
25
// lookup in binary tree.
26
// In practice it works the following way:
27
// -- We define Function* container class with custom "operator<" (FunctionPtr).
28
// -- "FunctionPtr" instances are stored in std::set collection, so every
29
//    std::set::insert operation will give you result in log(N) time.
30
//
31
// When a match is found the functions are folded. If both functions are
32
// overridable, we move the functionality into a new internal function and
33
// leave two overridable thunks to it.
34
//
35
//===----------------------------------------------------------------------===//
36
//
37
// Future work:
38
//
39
// * virtual functions.
40
//
41
// Many functions have their address taken by the virtual function table for
42
// the object they belong to. However, as long as it's only used for a lookup
43
// and call, this is irrelevant, and we'd like to fold such functions.
44
//
45
// * be smarter about bitcasts.
46
//
47
// In order to fold functions, we will sometimes add either bitcast instructions
48
// or bitcast constant expressions. Unfortunately, this can confound further
49
// analysis since the two functions differ where one has a bitcast and the
50
// other doesn't. We should learn to look through bitcasts.
51
//
52
// * Compare complex types with pointer types inside.
53
// * Compare cross-reference cases.
54
// * Compare complex expressions.
55
//
56
// All the three issues above could be described as ability to prove that
57
// fA == fB == fC == fE == fF == fG in example below:
58
//
59
//  void fA() {
60
//    fB();
61
//  }
62
//  void fB() {
63
//    fA();
64
//  }
65
//
66
//  void fE() {
67
//    fF();
68
//  }
69
//  void fF() {
70
//    fG();
71
//  }
72
//  void fG() {
73
//    fE();
74
//  }
75
//
76
// Simplest cross-reference case (fA <--> fB) was implemented in previous
77
// versions of MergeFunctions, though it presented only in two function pairs
78
// in test-suite (that counts >50k functions)
79
// Though possibility to detect complex cross-referencing (e.g.: A->B->C->D->A)
80
// could cover much more cases.
81
//
82
//===----------------------------------------------------------------------===//
83
84
#include "llvm/Transforms/IPO.h"
85
#include "llvm/ADT/DenseSet.h"
86
#include "llvm/ADT/FoldingSet.h"
87
#include "llvm/ADT/STLExtras.h"
88
#include "llvm/ADT/SmallSet.h"
89
#include "llvm/ADT/Statistic.h"
90
#include "llvm/IR/CallSite.h"
91
#include "llvm/IR/Constants.h"
92
#include "llvm/IR/DataLayout.h"
93
#include "llvm/IR/IRBuilder.h"
94
#include "llvm/IR/InlineAsm.h"
95
#include "llvm/IR/Instructions.h"
96
#include "llvm/IR/LLVMContext.h"
97
#include "llvm/IR/Module.h"
98
#include "llvm/IR/Operator.h"
99
#include "llvm/IR/ValueHandle.h"
100
#include "llvm/Pass.h"
101
#include "llvm/Support/CommandLine.h"
102
#include "llvm/Support/Debug.h"
103
#include "llvm/Support/ErrorHandling.h"
104
#include "llvm/Support/raw_ostream.h"
105
#include <vector>
106
using namespace llvm;
107
108
#define DEBUG_TYPE "mergefunc"
109
110
STATISTIC(NumFunctionsMerged, "Number of functions merged");
111
STATISTIC(NumThunksWritten, "Number of thunks generated");
112
STATISTIC(NumAliasesWritten, "Number of aliases generated");
113
STATISTIC(NumDoubleWeak, "Number of new functions created");
114
115
#if 0 // HLSL Change
116
static cl::opt<unsigned> NumFunctionsForSanityCheck(
117
    "mergefunc-sanity",
118
    cl::desc("How many functions in module could be used for "
119
             "MergeFunctions pass sanity check. "
120
             "'0' disables this check. Works only with '-debug' key."),
121
    cl::init(0), cl::Hidden);
122
#endif
123
124
namespace {
125
126
/// FunctionComparator - Compares two functions to determine whether or not
127
/// they will generate machine code with the same behaviour. DataLayout is
128
/// used if available. The comparator always fails conservatively (erring on the
129
/// side of claiming that two functions are different).
130
class FunctionComparator {
131
public:
132
  FunctionComparator(const Function *F1, const Function *F2)
133
0
      : FnL(F1), FnR(F2) {}
134
135
  /// Test whether the two functions have equivalent behaviour.
136
  int compare();
137
138
private:
139
  /// Test whether two basic blocks have equivalent behaviour.
140
  int compare(const BasicBlock *BBL, const BasicBlock *BBR);
141
142
  /// Constants comparison.
143
  /// Its analog to lexicographical comparison between hypothetical numbers
144
  /// of next format:
145
  /// <bitcastability-trait><raw-bit-contents>
146
  ///
147
  /// 1. Bitcastability.
148
  /// Check whether L's type could be losslessly bitcasted to R's type.
149
  /// On this stage method, in case when lossless bitcast is not possible
150
  /// method returns -1 or 1, thus also defining which type is greater in
151
  /// context of bitcastability.
152
  /// Stage 0: If types are equal in terms of cmpTypes, then we can go straight
153
  ///          to the contents comparison.
154
  ///          If types differ, remember types comparison result and check
155
  ///          whether we still can bitcast types.
156
  /// Stage 1: Types that satisfies isFirstClassType conditions are always
157
  ///          greater then others.
158
  /// Stage 2: Vector is greater then non-vector.
159
  ///          If both types are vectors, then vector with greater bitwidth is
160
  ///          greater.
161
  ///          If both types are vectors with the same bitwidth, then types
162
  ///          are bitcastable, and we can skip other stages, and go to contents
163
  ///          comparison.
164
  /// Stage 3: Pointer types are greater than non-pointers. If both types are
165
  ///          pointers of the same address space - go to contents comparison.
166
  ///          Different address spaces: pointer with greater address space is
167
  ///          greater.
168
  /// Stage 4: Types are neither vectors, nor pointers. And they differ.
169
  ///          We don't know how to bitcast them. So, we better don't do it,
170
  ///          and return types comparison result (so it determines the
171
  ///          relationship among constants we don't know how to bitcast).
172
  ///
173
  /// Just for clearance, let's see how the set of constants could look
174
  /// on single dimension axis:
175
  ///
176
  /// [NFCT], [FCT, "others"], [FCT, pointers], [FCT, vectors]
177
  /// Where: NFCT - Not a FirstClassType
178
  ///        FCT - FirstClassTyp:
179
  ///
180
  /// 2. Compare raw contents.
181
  /// It ignores types on this stage and only compares bits from L and R.
182
  /// Returns 0, if L and R has equivalent contents.
183
  /// -1 or 1 if values are different.
184
  /// Pretty trivial:
185
  /// 2.1. If contents are numbers, compare numbers.
186
  ///    Ints with greater bitwidth are greater. Ints with same bitwidths
187
  ///    compared by their contents.
188
  /// 2.2. "And so on". Just to avoid discrepancies with comments
189
  /// perhaps it would be better to read the implementation itself.
190
  /// 3. And again about overall picture. Let's look back at how the ordered set
191
  /// of constants will look like:
192
  /// [NFCT], [FCT, "others"], [FCT, pointers], [FCT, vectors]
193
  ///
194
  /// Now look, what could be inside [FCT, "others"], for example:
195
  /// [FCT, "others"] =
196
  /// [
197
  ///   [double 0.1], [double 1.23],
198
  ///   [i32 1], [i32 2],
199
  ///   { double 1.0 },       ; StructTyID, NumElements = 1
200
  ///   { i32 1 },            ; StructTyID, NumElements = 1
201
  ///   { double 1, i32 1 },  ; StructTyID, NumElements = 2
202
  ///   { i32 1, double 1 }   ; StructTyID, NumElements = 2
203
  /// ]
204
  ///
205
  /// Let's explain the order. Float numbers will be less than integers, just
206
  /// because of cmpType terms: FloatTyID < IntegerTyID.
207
  /// Floats (with same fltSemantics) are sorted according to their value.
208
  /// Then you can see integers, and they are, like a floats,
209
  /// could be easy sorted among each others.
210
  /// The structures. Structures are grouped at the tail, again because of their
211
  /// TypeID: StructTyID > IntegerTyID > FloatTyID.
212
  /// Structures with greater number of elements are greater. Structures with
213
  /// greater elements going first are greater.
214
  /// The same logic with vectors, arrays and other possible complex types.
215
  ///
216
  /// Bitcastable constants.
217
  /// Let's assume, that some constant, belongs to some group of
218
  /// "so-called-equal" values with different types, and at the same time
219
  /// belongs to another group of constants with equal types
220
  /// and "really" equal values.
221
  ///
222
  /// Now, prove that this is impossible:
223
  ///
224
  /// If constant A with type TyA is bitcastable to B with type TyB, then:
225
  /// 1. All constants with equal types to TyA, are bitcastable to B. Since
226
  ///    those should be vectors (if TyA is vector), pointers
227
  ///    (if TyA is pointer), or else (if TyA equal to TyB), those types should
228
  ///    be equal to TyB.
229
  /// 2. All constants with non-equal, but bitcastable types to TyA, are
230
  ///    bitcastable to B.
231
  ///    Once again, just because we allow it to vectors and pointers only.
232
  ///    This statement could be expanded as below:
233
  /// 2.1. All vectors with equal bitwidth to vector A, has equal bitwidth to
234
  ///      vector B, and thus bitcastable to B as well.
235
  /// 2.2. All pointers of the same address space, no matter what they point to,
236
  ///      bitcastable. So if C is pointer, it could be bitcasted to A and to B.
237
  /// So any constant equal or bitcastable to A is equal or bitcastable to B.
238
  /// QED.
239
  ///
240
  /// In another words, for pointers and vectors, we ignore top-level type and
241
  /// look at their particular properties (bit-width for vectors, and
242
  /// address space for pointers).
243
  /// If these properties are equal - compare their contents.
244
  int cmpConstants(const Constant *L, const Constant *R);
245
246
  /// Assign or look up previously assigned numbers for the two values, and
247
  /// return whether the numbers are equal. Numbers are assigned in the order
248
  /// visited.
249
  /// Comparison order:
250
  /// Stage 0: Value that is function itself is always greater then others.
251
  ///          If left and right values are references to their functions, then
252
  ///          they are equal.
253
  /// Stage 1: Constants are greater than non-constants.
254
  ///          If both left and right are constants, then the result of
255
  ///          cmpConstants is used as cmpValues result.
256
  /// Stage 2: InlineAsm instances are greater than others. If both left and
257
  ///          right are InlineAsm instances, InlineAsm* pointers casted to
258
  ///          integers and compared as numbers.
259
  /// Stage 3: For all other cases we compare order we meet these values in
260
  ///          their functions. If right value was met first during scanning,
261
  ///          then left value is greater.
262
  ///          In another words, we compare serial numbers, for more details
263
  ///          see comments for sn_mapL and sn_mapR.
264
  int cmpValues(const Value *L, const Value *R);
265
266
  /// Compare two Instructions for equivalence, similar to
267
  /// Instruction::isSameOperationAs but with modifications to the type
268
  /// comparison.
269
  /// Stages are listed in "most significant stage first" order:
270
  /// On each stage below, we do comparison between some left and right
271
  /// operation parts. If parts are non-equal, we assign parts comparison
272
  /// result to the operation comparison result and exit from method.
273
  /// Otherwise we proceed to the next stage.
274
  /// Stages:
275
  /// 1. Operations opcodes. Compared as numbers.
276
  /// 2. Number of operands.
277
  /// 3. Operation types. Compared with cmpType method.
278
  /// 4. Compare operation subclass optional data as stream of bytes:
279
  /// just convert it to integers and call cmpNumbers.
280
  /// 5. Compare in operation operand types with cmpType in
281
  /// most significant operand first order.
282
  /// 6. Last stage. Check operations for some specific attributes.
283
  /// For example, for Load it would be:
284
  /// 6.1.Load: volatile (as boolean flag)
285
  /// 6.2.Load: alignment (as integer numbers)
286
  /// 6.3.Load: synch-scope (as integer numbers)
287
  /// 6.4.Load: range metadata (as integer numbers)
288
  /// On this stage its better to see the code, since its not more than 10-15
289
  /// strings for particular instruction, and could change sometimes.
290
  int cmpOperations(const Instruction *L, const Instruction *R) const;
291
292
  /// Compare two GEPs for equivalent pointer arithmetic.
293
  /// Parts to be compared for each comparison stage,
294
  /// most significant stage first:
295
  /// 1. Address space. As numbers.
296
  /// 2. Constant offset, (using GEPOperator::accumulateConstantOffset method).
297
  /// 3. Pointer operand type (using cmpType method).
298
  /// 4. Number of operands.
299
  /// 5. Compare operands, using cmpValues method.
300
  int cmpGEPs(const GEPOperator *GEPL, const GEPOperator *GEPR);
301
0
  int cmpGEPs(const GetElementPtrInst *GEPL, const GetElementPtrInst *GEPR) {
302
0
    return cmpGEPs(cast<GEPOperator>(GEPL), cast<GEPOperator>(GEPR));
303
0
  }
304
305
  /// cmpType - compares two types,
306
  /// defines total ordering among the types set.
307
  ///
308
  /// Return values:
309
  /// 0 if types are equal,
310
  /// -1 if Left is less than Right,
311
  /// +1 if Left is greater than Right.
312
  ///
313
  /// Description:
314
  /// Comparison is broken onto stages. Like in lexicographical comparison
315
  /// stage coming first has higher priority.
316
  /// On each explanation stage keep in mind total ordering properties.
317
  ///
318
  /// 0. Before comparison we coerce pointer types of 0 address space to
319
  /// integer.
320
  /// We also don't bother with same type at left and right, so
321
  /// just return 0 in this case.
322
  ///
323
  /// 1. If types are of different kind (different type IDs).
324
  ///    Return result of type IDs comparison, treating them as numbers.
325
  /// 2. If types are vectors or integers, compare Type* values as numbers.
326
  /// 3. Types has same ID, so check whether they belongs to the next group:
327
  /// * Void
328
  /// * Float
329
  /// * Double
330
  /// * X86_FP80
331
  /// * FP128
332
  /// * PPC_FP128
333
  /// * Label
334
  /// * Metadata
335
  /// If so - return 0, yes - we can treat these types as equal only because
336
  /// their IDs are same.
337
  /// 4. If Left and Right are pointers, return result of address space
338
  /// comparison (numbers comparison). We can treat pointer types of same
339
  /// address space as equal.
340
  /// 5. If types are complex.
341
  /// Then both Left and Right are to be expanded and their element types will
342
  /// be checked with the same way. If we get Res != 0 on some stage, return it.
343
  /// Otherwise return 0.
344
  /// 6. For all other cases put llvm_unreachable.
345
  int cmpTypes(Type *TyL, Type *TyR) const;
346
347
  int cmpNumbers(uint64_t L, uint64_t R) const;
348
349
  int cmpAPInts(const APInt &L, const APInt &R) const;
350
  int cmpAPFloats(const APFloat &L, const APFloat &R) const;
351
  int cmpStrings(StringRef L, StringRef R) const;
352
  int cmpAttrs(const AttributeSet L, const AttributeSet R) const;
353
354
  // The two functions undergoing comparison.
355
  const Function *FnL, *FnR;
356
357
  /// Assign serial numbers to values from left function, and values from
358
  /// right function.
359
  /// Explanation:
360
  /// Being comparing functions we need to compare values we meet at left and
361
  /// right sides.
362
  /// Its easy to sort things out for external values. It just should be
363
  /// the same value at left and right.
364
  /// But for local values (those were introduced inside function body)
365
  /// we have to ensure they were introduced at exactly the same place,
366
  /// and plays the same role.
367
  /// Let's assign serial number to each value when we meet it first time.
368
  /// Values that were met at same place will be with same serial numbers.
369
  /// In this case it would be good to explain few points about values assigned
370
  /// to BBs and other ways of implementation (see below).
371
  ///
372
  /// 1. Safety of BB reordering.
373
  /// It's safe to change the order of BasicBlocks in function.
374
  /// Relationship with other functions and serial numbering will not be
375
  /// changed in this case.
376
  /// As follows from FunctionComparator::compare(), we do CFG walk: we start
377
  /// from the entry, and then take each terminator. So it doesn't matter how in
378
  /// fact BBs are ordered in function. And since cmpValues are called during
379
  /// this walk, the numbering depends only on how BBs located inside the CFG.
380
  /// So the answer is - yes. We will get the same numbering.
381
  ///
382
  /// 2. Impossibility to use dominance properties of values.
383
  /// If we compare two instruction operands: first is usage of local
384
  /// variable AL from function FL, and second is usage of local variable AR
385
  /// from FR, we could compare their origins and check whether they are
386
  /// defined at the same place.
387
  /// But, we are still not able to compare operands of PHI nodes, since those
388
  /// could be operands from further BBs we didn't scan yet.
389
  /// So it's impossible to use dominance properties in general.
390
  DenseMap<const Value*, int> sn_mapL, sn_mapR;
391
};
392
393
class FunctionNode {
394
  mutable AssertingVH<Function> F;
395
396
public:
397
0
  FunctionNode(Function *F) : F(F) {}
398
0
  Function *getFunc() const { return F; }
399
400
  /// Replace the reference to the function F by the function G, assuming their
401
  /// implementations are equal.
402
0
  void replaceBy(Function *G) const {
403
0
    assert(!(*this < FunctionNode(G)) && !(FunctionNode(G) < *this) &&
404
0
           "The two functions must be equal");
405
406
0
    F = G;
407
0
  }
408
409
0
  void release() { F = 0; }
410
0
  bool operator<(const FunctionNode &RHS) const {
411
0
    return (FunctionComparator(F, RHS.getFunc()).compare()) == -1;
412
0
  }
413
};
414
}
415
416
0
int FunctionComparator::cmpNumbers(uint64_t L, uint64_t R) const {
417
0
  if (L < R) return -1;
418
0
  if (L > R) return 1;
419
0
  return 0;
420
0
}
421
422
0
int FunctionComparator::cmpAPInts(const APInt &L, const APInt &R) const {
423
0
  if (int Res = cmpNumbers(L.getBitWidth(), R.getBitWidth()))
424
0
    return Res;
425
0
  if (L.ugt(R)) return 1;
426
0
  if (R.ugt(L)) return -1;
427
0
  return 0;
428
0
}
429
430
0
int FunctionComparator::cmpAPFloats(const APFloat &L, const APFloat &R) const {
431
0
  if (int Res = cmpNumbers((uint64_t)&L.getSemantics(),
432
0
                           (uint64_t)&R.getSemantics()))
433
0
    return Res;
434
0
  return cmpAPInts(L.bitcastToAPInt(), R.bitcastToAPInt());
435
0
}
436
437
0
int FunctionComparator::cmpStrings(StringRef L, StringRef R) const {
438
  // Prevent heavy comparison, compare sizes first.
439
0
  if (int Res = cmpNumbers(L.size(), R.size()))
440
0
    return Res;
441
442
  // Compare strings lexicographically only when it is necessary: only when
443
  // strings are equal in size.
444
0
  return L.compare(R);
445
0
}
446
447
int FunctionComparator::cmpAttrs(const AttributeSet L,
448
0
                                 const AttributeSet R) const {
449
0
  if (int Res = cmpNumbers(L.getNumSlots(), R.getNumSlots()))
450
0
    return Res;
451
452
0
  for (unsigned i = 0, e = L.getNumSlots(); i != e; ++i) {
453
0
    AttributeSet::iterator LI = L.begin(i), LE = L.end(i), RI = R.begin(i),
454
0
                           RE = R.end(i);
455
0
    for (; LI != LE && RI != RE; ++LI, ++RI) {
456
0
      Attribute LA = *LI;
457
0
      Attribute RA = *RI;
458
0
      if (LA < RA)
459
0
        return -1;
460
0
      if (RA < LA)
461
0
        return 1;
462
0
    }
463
0
    if (LI != LE)
464
0
      return 1;
465
0
    if (RI != RE)
466
0
      return -1;
467
0
  }
468
0
  return 0;
469
0
}
470
471
/// Constants comparison:
472
/// 1. Check whether type of L constant could be losslessly bitcasted to R
473
/// type.
474
/// 2. Compare constant contents.
475
/// For more details see declaration comments.
476
0
int FunctionComparator::cmpConstants(const Constant *L, const Constant *R) {
477
478
0
  Type *TyL = L->getType();
479
0
  Type *TyR = R->getType();
480
481
  // Check whether types are bitcastable. This part is just re-factored
482
  // Type::canLosslesslyBitCastTo method, but instead of returning true/false,
483
  // we also pack into result which type is "less" for us.
484
0
  int TypesRes = cmpTypes(TyL, TyR);
485
0
  if (TypesRes != 0) {
486
    // Types are different, but check whether we can bitcast them.
487
0
    if (!TyL->isFirstClassType()) {
488
0
      if (TyR->isFirstClassType())
489
0
        return -1;
490
      // Neither TyL nor TyR are values of first class type. Return the result
491
      // of comparing the types
492
0
      return TypesRes;
493
0
    }
494
0
    if (!TyR->isFirstClassType()) {
495
0
      if (TyL->isFirstClassType())
496
0
        return 1;
497
0
      return TypesRes;
498
0
    }
499
500
    // Vector -> Vector conversions are always lossless if the two vector types
501
    // have the same size, otherwise not.
502
0
    unsigned TyLWidth = 0;
503
0
    unsigned TyRWidth = 0;
504
505
0
    if (const VectorType *VecTyL = dyn_cast<VectorType>(TyL))
506
0
      TyLWidth = VecTyL->getBitWidth();
507
0
    if (const VectorType *VecTyR = dyn_cast<VectorType>(TyR))
508
0
      TyRWidth = VecTyR->getBitWidth();
509
510
0
    if (TyLWidth != TyRWidth)
511
0
      return cmpNumbers(TyLWidth, TyRWidth);
512
513
    // Zero bit-width means neither TyL nor TyR are vectors.
514
0
    if (!TyLWidth) {
515
0
      PointerType *PTyL = dyn_cast<PointerType>(TyL);
516
0
      PointerType *PTyR = dyn_cast<PointerType>(TyR);
517
0
      if (PTyL && PTyR) {
518
0
        unsigned AddrSpaceL = PTyL->getAddressSpace();
519
0
        unsigned AddrSpaceR = PTyR->getAddressSpace();
520
0
        if (int Res = cmpNumbers(AddrSpaceL, AddrSpaceR))
521
0
          return Res;
522
0
      }
523
0
      if (PTyL)
524
0
        return 1;
525
0
      if (PTyR)
526
0
        return -1;
527
528
      // TyL and TyR aren't vectors, nor pointers. We don't know how to
529
      // bitcast them.
530
0
      return TypesRes;
531
0
    }
532
0
  }
533
534
  // OK, types are bitcastable, now check constant contents.
535
536
0
  if (L->isNullValue() && R->isNullValue())
537
0
    return TypesRes;
538
0
  if (L->isNullValue() && !R->isNullValue())
539
0
    return 1;
540
0
  if (!L->isNullValue() && R->isNullValue())
541
0
    return -1;
542
543
0
  if (int Res = cmpNumbers(L->getValueID(), R->getValueID()))
544
0
    return Res;
545
546
0
  switch (L->getValueID()) {
547
0
  case Value::UndefValueVal: return TypesRes;
548
0
  case Value::ConstantIntVal: {
549
0
    const APInt &LInt = cast<ConstantInt>(L)->getValue();
550
0
    const APInt &RInt = cast<ConstantInt>(R)->getValue();
551
0
    return cmpAPInts(LInt, RInt);
552
0
  }
553
0
  case Value::ConstantFPVal: {
554
0
    const APFloat &LAPF = cast<ConstantFP>(L)->getValueAPF();
555
0
    const APFloat &RAPF = cast<ConstantFP>(R)->getValueAPF();
556
0
    return cmpAPFloats(LAPF, RAPF);
557
0
  }
558
0
  case Value::ConstantArrayVal: {
559
0
    const ConstantArray *LA = cast<ConstantArray>(L);
560
0
    const ConstantArray *RA = cast<ConstantArray>(R);
561
0
    uint64_t NumElementsL = cast<ArrayType>(TyL)->getNumElements();
562
0
    uint64_t NumElementsR = cast<ArrayType>(TyR)->getNumElements();
563
0
    if (int Res = cmpNumbers(NumElementsL, NumElementsR))
564
0
      return Res;
565
0
    for (uint64_t i = 0; i < NumElementsL; ++i) {
566
0
      if (int Res = cmpConstants(cast<Constant>(LA->getOperand(i)),
567
0
                                 cast<Constant>(RA->getOperand(i))))
568
0
        return Res;
569
0
    }
570
0
    return 0;
571
0
  }
572
0
  case Value::ConstantStructVal: {
573
0
    const ConstantStruct *LS = cast<ConstantStruct>(L);
574
0
    const ConstantStruct *RS = cast<ConstantStruct>(R);
575
0
    unsigned NumElementsL = cast<StructType>(TyL)->getNumElements();
576
0
    unsigned NumElementsR = cast<StructType>(TyR)->getNumElements();
577
0
    if (int Res = cmpNumbers(NumElementsL, NumElementsR))
578
0
      return Res;
579
0
    for (unsigned i = 0; i != NumElementsL; ++i) {
580
0
      if (int Res = cmpConstants(cast<Constant>(LS->getOperand(i)),
581
0
                                 cast<Constant>(RS->getOperand(i))))
582
0
        return Res;
583
0
    }
584
0
    return 0;
585
0
  }
586
0
  case Value::ConstantVectorVal: {
587
0
    const ConstantVector *LV = cast<ConstantVector>(L);
588
0
    const ConstantVector *RV = cast<ConstantVector>(R);
589
0
    unsigned NumElementsL = cast<VectorType>(TyL)->getNumElements();
590
0
    unsigned NumElementsR = cast<VectorType>(TyR)->getNumElements();
591
0
    if (int Res = cmpNumbers(NumElementsL, NumElementsR))
592
0
      return Res;
593
0
    for (uint64_t i = 0; i < NumElementsL; ++i) {
594
0
      if (int Res = cmpConstants(cast<Constant>(LV->getOperand(i)),
595
0
                                 cast<Constant>(RV->getOperand(i))))
596
0
        return Res;
597
0
    }
598
0
    return 0;
599
0
  }
600
0
  case Value::ConstantExprVal: {
601
0
    const ConstantExpr *LE = cast<ConstantExpr>(L);
602
0
    const ConstantExpr *RE = cast<ConstantExpr>(R);
603
0
    unsigned NumOperandsL = LE->getNumOperands();
604
0
    unsigned NumOperandsR = RE->getNumOperands();
605
0
    if (int Res = cmpNumbers(NumOperandsL, NumOperandsR))
606
0
      return Res;
607
0
    for (unsigned i = 0; i < NumOperandsL; ++i) {
608
0
      if (int Res = cmpConstants(cast<Constant>(LE->getOperand(i)),
609
0
                                 cast<Constant>(RE->getOperand(i))))
610
0
        return Res;
611
0
    }
612
0
    return 0;
613
0
  }
614
0
  case Value::FunctionVal:
615
0
  case Value::GlobalVariableVal:
616
0
  case Value::GlobalAliasVal:
617
0
  default: // Unknown constant, cast L and R pointers to numbers and compare.
618
0
    return cmpNumbers((uint64_t)L, (uint64_t)R);
619
0
  }
620
0
}
621
622
/// cmpType - compares two types,
623
/// defines total ordering among the types set.
624
/// See method declaration comments for more details.
625
0
int FunctionComparator::cmpTypes(Type *TyL, Type *TyR) const {
626
627
0
  PointerType *PTyL = dyn_cast<PointerType>(TyL);
628
0
  PointerType *PTyR = dyn_cast<PointerType>(TyR);
629
630
0
  const DataLayout &DL = FnL->getParent()->getDataLayout();
631
0
  if (PTyL && PTyL->getAddressSpace() == 0)
632
0
    TyL = DL.getIntPtrType(TyL);
633
0
  if (PTyR && PTyR->getAddressSpace() == 0)
634
0
    TyR = DL.getIntPtrType(TyR);
635
636
0
  if (TyL == TyR)
637
0
    return 0;
638
639
0
  if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID()))
640
0
    return Res;
641
642
0
  switch (TyL->getTypeID()) {
643
0
  default:
644
0
    llvm_unreachable("Unknown type!");
645
    // Fall through in Release mode.
646
0
  case Type::IntegerTyID:
647
0
  case Type::VectorTyID:
648
    // TyL == TyR would have returned true earlier.
649
0
    return cmpNumbers((uint64_t)TyL, (uint64_t)TyR);
650
651
0
  case Type::VoidTyID:
652
0
  case Type::FloatTyID:
653
0
  case Type::DoubleTyID:
654
0
  case Type::X86_FP80TyID:
655
0
  case Type::FP128TyID:
656
0
  case Type::PPC_FP128TyID:
657
0
  case Type::LabelTyID:
658
0
  case Type::MetadataTyID:
659
0
    return 0;
660
661
0
  case Type::PointerTyID: {
662
0
    assert(PTyL && PTyR && "Both types must be pointers here.");
663
0
    return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace());
664
0
  }
665
666
0
  case Type::StructTyID: {
667
0
    StructType *STyL = cast<StructType>(TyL);
668
0
    StructType *STyR = cast<StructType>(TyR);
669
0
    if (STyL->getNumElements() != STyR->getNumElements())
670
0
      return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
671
672
0
    if (STyL->isPacked() != STyR->isPacked())
673
0
      return cmpNumbers(STyL->isPacked(), STyR->isPacked());
674
675
0
    for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) {
676
0
      if (int Res = cmpTypes(STyL->getElementType(i), STyR->getElementType(i)))
677
0
        return Res;
678
0
    }
679
0
    return 0;
680
0
  }
681
682
0
  case Type::FunctionTyID: {
683
0
    FunctionType *FTyL = cast<FunctionType>(TyL);
684
0
    FunctionType *FTyR = cast<FunctionType>(TyR);
685
0
    if (FTyL->getNumParams() != FTyR->getNumParams())
686
0
      return cmpNumbers(FTyL->getNumParams(), FTyR->getNumParams());
687
688
0
    if (FTyL->isVarArg() != FTyR->isVarArg())
689
0
      return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg());
690
691
0
    if (int Res = cmpTypes(FTyL->getReturnType(), FTyR->getReturnType()))
692
0
      return Res;
693
694
0
    for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) {
695
0
      if (int Res = cmpTypes(FTyL->getParamType(i), FTyR->getParamType(i)))
696
0
        return Res;
697
0
    }
698
0
    return 0;
699
0
  }
700
701
0
  case Type::ArrayTyID: {
702
0
    ArrayType *ATyL = cast<ArrayType>(TyL);
703
0
    ArrayType *ATyR = cast<ArrayType>(TyR);
704
0
    if (ATyL->getNumElements() != ATyR->getNumElements())
705
0
      return cmpNumbers(ATyL->getNumElements(), ATyR->getNumElements());
706
0
    return cmpTypes(ATyL->getElementType(), ATyR->getElementType());
707
0
  }
708
0
  }
709
0
}
710
711
// Determine whether the two operations are the same except that pointer-to-A
712
// and pointer-to-B are equivalent. This should be kept in sync with
713
// Instruction::isSameOperationAs.
714
// Read method declaration comments for more details.
715
int FunctionComparator::cmpOperations(const Instruction *L,
716
0
                                      const Instruction *R) const {
717
  // Differences from Instruction::isSameOperationAs:
718
  //  * replace type comparison with calls to isEquivalentType.
719
  //  * we test for I->hasSameSubclassOptionalData (nuw/nsw/tail) at the top
720
  //  * because of the above, we don't test for the tail bit on calls later on
721
0
  if (int Res = cmpNumbers(L->getOpcode(), R->getOpcode()))
722
0
    return Res;
723
724
0
  if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands()))
725
0
    return Res;
726
727
0
  if (int Res = cmpTypes(L->getType(), R->getType()))
728
0
    return Res;
729
730
0
  if (int Res = cmpNumbers(L->getRawSubclassOptionalData(),
731
0
                           R->getRawSubclassOptionalData()))
732
0
    return Res;
733
734
0
  if (const AllocaInst *AI = dyn_cast<AllocaInst>(L)) {
735
0
    if (int Res = cmpTypes(AI->getAllocatedType(),
736
0
                           cast<AllocaInst>(R)->getAllocatedType()))
737
0
      return Res;
738
0
    if (int Res =
739
0
            cmpNumbers(AI->getAlignment(), cast<AllocaInst>(R)->getAlignment()))
740
0
      return Res;
741
0
  }
742
743
  // We have two instructions of identical opcode and #operands.  Check to see
744
  // if all operands are the same type
745
0
  for (unsigned i = 0, e = L->getNumOperands(); i != e; ++i) {
746
0
    if (int Res =
747
0
            cmpTypes(L->getOperand(i)->getType(), R->getOperand(i)->getType()))
748
0
      return Res;
749
0
  }
750
751
  // Check special state that is a part of some instructions.
752
0
  if (const LoadInst *LI = dyn_cast<LoadInst>(L)) {
753
0
    if (int Res = cmpNumbers(LI->isVolatile(), cast<LoadInst>(R)->isVolatile()))
754
0
      return Res;
755
0
    if (int Res =
756
0
            cmpNumbers(LI->getAlignment(), cast<LoadInst>(R)->getAlignment()))
757
0
      return Res;
758
0
    if (int Res =
759
0
            cmpNumbers(LI->getOrdering(), cast<LoadInst>(R)->getOrdering()))
760
0
      return Res;
761
0
    if (int Res =
762
0
            cmpNumbers(LI->getSynchScope(), cast<LoadInst>(R)->getSynchScope()))
763
0
      return Res;
764
0
    return cmpNumbers((uint64_t)LI->getMetadata(LLVMContext::MD_range),
765
0
                      (uint64_t)cast<LoadInst>(R)->getMetadata(LLVMContext::MD_range));
766
0
  }
767
0
  if (const StoreInst *SI = dyn_cast<StoreInst>(L)) {
768
0
    if (int Res =
769
0
            cmpNumbers(SI->isVolatile(), cast<StoreInst>(R)->isVolatile()))
770
0
      return Res;
771
0
    if (int Res =
772
0
            cmpNumbers(SI->getAlignment(), cast<StoreInst>(R)->getAlignment()))
773
0
      return Res;
774
0
    if (int Res =
775
0
            cmpNumbers(SI->getOrdering(), cast<StoreInst>(R)->getOrdering()))
776
0
      return Res;
777
0
    return cmpNumbers(SI->getSynchScope(), cast<StoreInst>(R)->getSynchScope());
778
0
  }
779
0
  if (const CmpInst *CI = dyn_cast<CmpInst>(L))
780
0
    return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate());
781
0
  if (const CallInst *CI = dyn_cast<CallInst>(L)) {
782
0
    if (int Res = cmpNumbers(CI->getCallingConv(),
783
0
                             cast<CallInst>(R)->getCallingConv()))
784
0
      return Res;
785
0
    if (int Res =
786
0
            cmpAttrs(CI->getAttributes(), cast<CallInst>(R)->getAttributes()))
787
0
      return Res;
788
0
    return cmpNumbers(
789
0
        (uint64_t)CI->getMetadata(LLVMContext::MD_range),
790
0
        (uint64_t)cast<CallInst>(R)->getMetadata(LLVMContext::MD_range));
791
0
  }
792
0
  if (const InvokeInst *CI = dyn_cast<InvokeInst>(L)) {
793
0
    if (int Res = cmpNumbers(CI->getCallingConv(),
794
0
                             cast<InvokeInst>(R)->getCallingConv()))
795
0
      return Res;
796
0
    if (int Res =
797
0
            cmpAttrs(CI->getAttributes(), cast<InvokeInst>(R)->getAttributes()))
798
0
      return Res;
799
0
    return cmpNumbers(
800
0
        (uint64_t)CI->getMetadata(LLVMContext::MD_range),
801
0
        (uint64_t)cast<InvokeInst>(R)->getMetadata(LLVMContext::MD_range));
802
0
  }
803
0
  if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) {
804
0
    ArrayRef<unsigned> LIndices = IVI->getIndices();
805
0
    ArrayRef<unsigned> RIndices = cast<InsertValueInst>(R)->getIndices();
806
0
    if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
807
0
      return Res;
808
0
    for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
809
0
      if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
810
0
        return Res;
811
0
    }
812
0
  }
813
0
  if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(L)) {
814
0
    ArrayRef<unsigned> LIndices = EVI->getIndices();
815
0
    ArrayRef<unsigned> RIndices = cast<ExtractValueInst>(R)->getIndices();
816
0
    if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
817
0
      return Res;
818
0
    for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
819
0
      if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
820
0
        return Res;
821
0
    }
822
0
  }
823
0
  if (const FenceInst *FI = dyn_cast<FenceInst>(L)) {
824
0
    if (int Res =
825
0
            cmpNumbers(FI->getOrdering(), cast<FenceInst>(R)->getOrdering()))
826
0
      return Res;
827
0
    return cmpNumbers(FI->getSynchScope(), cast<FenceInst>(R)->getSynchScope());
828
0
  }
829
830
0
  if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(L)) {
831
0
    if (int Res = cmpNumbers(CXI->isVolatile(),
832
0
                             cast<AtomicCmpXchgInst>(R)->isVolatile()))
833
0
      return Res;
834
0
    if (int Res = cmpNumbers(CXI->isWeak(),
835
0
                             cast<AtomicCmpXchgInst>(R)->isWeak()))
836
0
      return Res;
837
0
    if (int Res = cmpNumbers(CXI->getSuccessOrdering(),
838
0
                             cast<AtomicCmpXchgInst>(R)->getSuccessOrdering()))
839
0
      return Res;
840
0
    if (int Res = cmpNumbers(CXI->getFailureOrdering(),
841
0
                             cast<AtomicCmpXchgInst>(R)->getFailureOrdering()))
842
0
      return Res;
843
0
    return cmpNumbers(CXI->getSynchScope(),
844
0
                      cast<AtomicCmpXchgInst>(R)->getSynchScope());
845
0
  }
846
0
  if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) {
847
0
    if (int Res = cmpNumbers(RMWI->getOperation(),
848
0
                             cast<AtomicRMWInst>(R)->getOperation()))
849
0
      return Res;
850
0
    if (int Res = cmpNumbers(RMWI->isVolatile(),
851
0
                             cast<AtomicRMWInst>(R)->isVolatile()))
852
0
      return Res;
853
0
    if (int Res = cmpNumbers(RMWI->getOrdering(),
854
0
                             cast<AtomicRMWInst>(R)->getOrdering()))
855
0
      return Res;
856
0
    return cmpNumbers(RMWI->getSynchScope(),
857
0
                      cast<AtomicRMWInst>(R)->getSynchScope());
858
0
  }
859
0
  return 0;
860
0
}
861
862
// Determine whether two GEP operations perform the same underlying arithmetic.
863
// Read method declaration comments for more details.
864
int FunctionComparator::cmpGEPs(const GEPOperator *GEPL,
865
0
                               const GEPOperator *GEPR) {
866
867
0
  unsigned int ASL = GEPL->getPointerAddressSpace();
868
0
  unsigned int ASR = GEPR->getPointerAddressSpace();
869
870
0
  if (int Res = cmpNumbers(ASL, ASR))
871
0
    return Res;
872
873
  // When we have target data, we can reduce the GEP down to the value in bytes
874
  // added to the address.
875
0
  const DataLayout &DL = FnL->getParent()->getDataLayout();
876
0
  unsigned BitWidth = DL.getPointerSizeInBits(ASL);
877
0
  APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0);
878
0
  if (GEPL->accumulateConstantOffset(DL, OffsetL) &&
879
0
      GEPR->accumulateConstantOffset(DL, OffsetR))
880
0
    return cmpAPInts(OffsetL, OffsetR);
881
882
0
  if (int Res = cmpNumbers((uint64_t)GEPL->getPointerOperand()->getType(),
883
0
                           (uint64_t)GEPR->getPointerOperand()->getType()))
884
0
    return Res;
885
886
0
  if (int Res = cmpNumbers(GEPL->getNumOperands(), GEPR->getNumOperands()))
887
0
    return Res;
888
889
0
  for (unsigned i = 0, e = GEPL->getNumOperands(); i != e; ++i) {
890
0
    if (int Res = cmpValues(GEPL->getOperand(i), GEPR->getOperand(i)))
891
0
      return Res;
892
0
  }
893
894
0
  return 0;
895
0
}
896
897
/// Compare two values used by the two functions under pair-wise comparison. If
898
/// this is the first time the values are seen, they're added to the mapping so
899
/// that we will detect mismatches on next use.
900
/// See comments in declaration for more details.
901
0
int FunctionComparator::cmpValues(const Value *L, const Value *R) {
902
  // Catch self-reference case.
903
0
  if (L == FnL) {
904
0
    if (R == FnR)
905
0
      return 0;
906
0
    return -1;
907
0
  }
908
0
  if (R == FnR) {
909
0
    if (L == FnL)
910
0
      return 0;
911
0
    return 1;
912
0
  }
913
914
0
  const Constant *ConstL = dyn_cast<Constant>(L);
915
0
  const Constant *ConstR = dyn_cast<Constant>(R);
916
0
  if (ConstL && ConstR) {
917
0
    if (L == R)
918
0
      return 0;
919
0
    return cmpConstants(ConstL, ConstR);
920
0
  }
921
922
0
  if (ConstL)
923
0
    return 1;
924
0
  if (ConstR)
925
0
    return -1;
926
927
0
  const InlineAsm *InlineAsmL = dyn_cast<InlineAsm>(L);
928
0
  const InlineAsm *InlineAsmR = dyn_cast<InlineAsm>(R);
929
930
0
  if (InlineAsmL && InlineAsmR)
931
0
    return cmpNumbers((uint64_t)L, (uint64_t)R);
932
0
  if (InlineAsmL)
933
0
    return 1;
934
0
  if (InlineAsmR)
935
0
    return -1;
936
937
0
  auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())),
938
0
       RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size()));
939
940
0
  return cmpNumbers(LeftSN.first->second, RightSN.first->second);
941
0
}
942
// Test whether two basic blocks have equivalent behaviour.
943
0
int FunctionComparator::compare(const BasicBlock *BBL, const BasicBlock *BBR) {
944
0
  BasicBlock::const_iterator InstL = BBL->begin(), InstLE = BBL->end();
945
0
  BasicBlock::const_iterator InstR = BBR->begin(), InstRE = BBR->end();
946
947
0
  do {
948
0
    if (int Res = cmpValues(InstL, InstR))
949
0
      return Res;
950
951
0
    const GetElementPtrInst *GEPL = dyn_cast<GetElementPtrInst>(InstL);
952
0
    const GetElementPtrInst *GEPR = dyn_cast<GetElementPtrInst>(InstR);
953
954
0
    if (GEPL && !GEPR)
955
0
      return 1;
956
0
    if (GEPR && !GEPL)
957
0
      return -1;
958
959
0
    if (GEPL && GEPR) {
960
0
      if (int Res =
961
0
              cmpValues(GEPL->getPointerOperand(), GEPR->getPointerOperand()))
962
0
        return Res;
963
0
      if (int Res = cmpGEPs(GEPL, GEPR))
964
0
        return Res;
965
0
    } else {
966
0
      if (int Res = cmpOperations(InstL, InstR))
967
0
        return Res;
968
0
      assert(InstL->getNumOperands() == InstR->getNumOperands());
969
970
0
      for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) {
971
0
        Value *OpL = InstL->getOperand(i);
972
0
        Value *OpR = InstR->getOperand(i);
973
0
        if (int Res = cmpValues(OpL, OpR))
974
0
          return Res;
975
0
        if (int Res = cmpNumbers(OpL->getValueID(), OpR->getValueID()))
976
0
          return Res;
977
        // TODO: Already checked in cmpOperation
978
0
        if (int Res = cmpTypes(OpL->getType(), OpR->getType()))
979
0
          return Res;
980
0
      }
981
0
    }
982
983
0
    ++InstL, ++InstR;
984
0
  } while (InstL != InstLE && InstR != InstRE);
985
986
0
  if (InstL != InstLE && InstR == InstRE)
987
0
    return 1;
988
0
  if (InstL == InstLE && InstR != InstRE)
989
0
    return -1;
990
0
  return 0;
991
0
}
992
993
// Test whether the two functions have equivalent behaviour.
994
0
int FunctionComparator::compare() {
995
996
0
  sn_mapL.clear();
997
0
  sn_mapR.clear();
998
999
0
  if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes()))
1000
0
    return Res;
1001
1002
0
  if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC()))
1003
0
    return Res;
1004
1005
0
  if (FnL->hasGC()) {
1006
0
    if (int Res = cmpNumbers((uint64_t)FnL->getGC(), (uint64_t)FnR->getGC()))
1007
0
      return Res;
1008
0
  }
1009
1010
0
  if (int Res = cmpNumbers(FnL->hasSection(), FnR->hasSection()))
1011
0
    return Res;
1012
1013
0
  if (FnL->hasSection()) {
1014
0
    if (int Res = cmpStrings(FnL->getSection(), FnR->getSection()))
1015
0
      return Res;
1016
0
  }
1017
1018
0
  if (int Res = cmpNumbers(FnL->isVarArg(), FnR->isVarArg()))
1019
0
    return Res;
1020
1021
  // TODO: if it's internal and only used in direct calls, we could handle this
1022
  // case too.
1023
0
  if (int Res = cmpNumbers(FnL->getCallingConv(), FnR->getCallingConv()))
1024
0
    return Res;
1025
1026
0
  if (int Res = cmpTypes(FnL->getFunctionType(), FnR->getFunctionType()))
1027
0
    return Res;
1028
1029
0
  assert(FnL->arg_size() == FnR->arg_size() &&
1030
0
         "Identically typed functions have different numbers of args!");
1031
1032
  // Visit the arguments so that they get enumerated in the order they're
1033
  // passed in.
1034
0
  for (Function::const_arg_iterator ArgLI = FnL->arg_begin(),
1035
0
                                    ArgRI = FnR->arg_begin(),
1036
0
                                    ArgLE = FnL->arg_end();
1037
0
       ArgLI != ArgLE; ++ArgLI, ++ArgRI) {
1038
0
    if (cmpValues(ArgLI, ArgRI) != 0)
1039
0
      llvm_unreachable("Arguments repeat!");
1040
0
  }
1041
1042
  // We do a CFG-ordered walk since the actual ordering of the blocks in the
1043
  // linked list is immaterial. Our walk starts at the entry block for both
1044
  // functions, then takes each block from each terminator in order. As an
1045
  // artifact, this also means that unreachable blocks are ignored.
1046
0
  SmallVector<const BasicBlock *, 8> FnLBBs, FnRBBs;
1047
0
  SmallSet<const BasicBlock *, 128> VisitedBBs; // in terms of F1.
1048
1049
0
  FnLBBs.push_back(&FnL->getEntryBlock());
1050
0
  FnRBBs.push_back(&FnR->getEntryBlock());
1051
1052
0
  VisitedBBs.insert(FnLBBs[0]);
1053
0
  while (!FnLBBs.empty()) {
1054
0
    const BasicBlock *BBL = FnLBBs.pop_back_val();
1055
0
    const BasicBlock *BBR = FnRBBs.pop_back_val();
1056
1057
0
    if (int Res = cmpValues(BBL, BBR))
1058
0
      return Res;
1059
1060
0
    if (int Res = compare(BBL, BBR))
1061
0
      return Res;
1062
1063
0
    const TerminatorInst *TermL = BBL->getTerminator();
1064
0
    const TerminatorInst *TermR = BBR->getTerminator();
1065
1066
0
    assert(TermL->getNumSuccessors() == TermR->getNumSuccessors());
1067
0
    for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) {
1068
0
      if (!VisitedBBs.insert(TermL->getSuccessor(i)).second)
1069
0
        continue;
1070
1071
0
      FnLBBs.push_back(TermL->getSuccessor(i));
1072
0
      FnRBBs.push_back(TermR->getSuccessor(i));
1073
0
    }
1074
0
  }
1075
0
  return 0;
1076
0
}
1077
1078
namespace {
1079
1080
/// MergeFunctions finds functions which will generate identical machine code,
1081
/// by considering all pointer types to be equivalent. Once identified,
1082
/// MergeFunctions will fold them by replacing a call to one to a call to a
1083
/// bitcast of the other.
1084
///
1085
class MergeFunctions : public ModulePass {
1086
public:
1087
  static char ID;
1088
  MergeFunctions()
1089
0
    : ModulePass(ID), HasGlobalAliases(false) {
1090
0
    initializeMergeFunctionsPass(*PassRegistry::getPassRegistry());
1091
0
  }
1092
1093
  bool runOnModule(Module &M) override;
1094
1095
private:
1096
  typedef std::set<FunctionNode> FnTreeType;
1097
1098
  /// A work queue of functions that may have been modified and should be
1099
  /// analyzed again.
1100
  std::vector<WeakTrackingVH> Deferred;
1101
1102
  /// Checks the rules of order relation introduced among functions set.
1103
  /// Returns true, if sanity check has been passed, and false if failed.
1104
  bool doSanityCheck(std::vector<WeakTrackingVH> &Worklist);
1105
1106
  /// Insert a ComparableFunction into the FnTree, or merge it away if it's
1107
  /// equal to one that's already present.
1108
  bool insert(Function *NewFunction);
1109
1110
  /// Remove a Function from the FnTree and queue it up for a second sweep of
1111
  /// analysis.
1112
  void remove(Function *F);
1113
1114
  /// Find the functions that use this Value and remove them from FnTree and
1115
  /// queue the functions.
1116
  void removeUsers(Value *V);
1117
1118
  /// Replace all direct calls of Old with calls of New. Will bitcast New if
1119
  /// necessary to make types match.
1120
  void replaceDirectCallers(Function *Old, Function *New);
1121
1122
  /// Merge two equivalent functions. Upon completion, G may be deleted, or may
1123
  /// be converted into a thunk. In either case, it should never be visited
1124
  /// again.
1125
  void mergeTwoFunctions(Function *F, Function *G);
1126
1127
  /// Replace G with a thunk or an alias to F. Deletes G.
1128
  void writeThunkOrAlias(Function *F, Function *G);
1129
1130
  /// Replace G with a simple tail call to bitcast(F). Also replace direct uses
1131
  /// of G with bitcast(F). Deletes G.
1132
  void writeThunk(Function *F, Function *G);
1133
1134
  /// Replace G with an alias to F. Deletes G.
1135
  void writeAlias(Function *F, Function *G);
1136
1137
  /// Replace function F with function G in the function tree.
1138
  void replaceFunctionInTree(FnTreeType::iterator &IterToF, Function *G);
1139
1140
  /// The set of all distinct functions. Use the insert() and remove() methods
1141
  /// to modify it.
1142
  FnTreeType FnTree;
1143
1144
  /// Whether or not the target supports global aliases.
1145
  bool HasGlobalAliases;
1146
};
1147
1148
}  // end anonymous namespace
1149
1150
char MergeFunctions::ID = 0;
1151
INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false)
1152
1153
0
ModulePass *llvm::createMergeFunctionsPass() {
1154
0
  return new MergeFunctions();
1155
0
}
1156
1157
0
bool MergeFunctions::doSanityCheck(std::vector<WeakTrackingVH> &Worklist) {
1158
0
#if 0 // Begin HLSL Change (NumFunctionsForSanityCheck is always zero)
1159
0
  if (const unsigned Max = NumFunctionsForSanityCheck) {
1160
0
    unsigned TripleNumber = 0;
1161
0
    bool Valid = true;
1162
0
1163
0
    dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n";
1164
0
1165
0
    unsigned i = 0;
1166
0
    for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(), E = Worklist.end();
1167
0
         I != E && i < Max; ++I, ++i) {
1168
0
      unsigned j = i;
1169
0
      for (std::vector<WeakTrackingVH>::iterator J = I; J != E && j < Max; ++J, ++j) {
1170
0
        Function *F1 = cast<Function>(*I);
1171
0
        Function *F2 = cast<Function>(*J);
1172
0
        int Res1 = FunctionComparator(F1, F2).compare();
1173
0
        int Res2 = FunctionComparator(F2, F1).compare();
1174
0
1175
0
        // If F1 <= F2, then F2 >= F1, otherwise report failure.
1176
0
        if (Res1 != -Res2) {
1177
0
          dbgs() << "MERGEFUNC-SANITY: Non-symmetric; triple: " << TripleNumber
1178
0
                 << "\n";
1179
0
          F1->dump();
1180
0
          F2->dump();
1181
0
          Valid = false;
1182
0
        }
1183
0
1184
0
        if (Res1 == 0)
1185
0
          continue;
1186
0
1187
0
        unsigned k = j;
1188
0
        for (std::vector<WeakTrackingVH>::iterator K = J; K != E && k < Max;
1189
0
             ++k, ++K, ++TripleNumber) {
1190
0
          if (K == J)
1191
0
            continue;
1192
0
1193
0
          Function *F3 = cast<Function>(*K);
1194
0
          int Res3 = FunctionComparator(F1, F3).compare();
1195
0
          int Res4 = FunctionComparator(F2, F3).compare();
1196
0
1197
0
          bool Transitive = true;
1198
0
1199
0
          if (Res1 != 0 && Res1 == Res4) {
1200
0
            // F1 > F2, F2 > F3 => F1 > F3
1201
0
            Transitive = Res3 == Res1;
1202
0
          } else if (Res3 != 0 && Res3 == -Res4) {
1203
0
            // F1 > F3, F3 > F2 => F1 > F2
1204
0
            Transitive = Res3 == Res1;
1205
0
          } else if (Res4 != 0 && -Res3 == Res4) {
1206
0
            // F2 > F3, F3 > F1 => F2 > F1
1207
0
            Transitive = Res4 == -Res1;
1208
0
          }
1209
0
1210
0
          if (!Transitive) {
1211
0
            dbgs() << "MERGEFUNC-SANITY: Non-transitive; triple: "
1212
0
                   << TripleNumber << "\n";
1213
0
            dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", "
1214
0
                   << Res4 << "\n";
1215
0
            F1->dump();
1216
0
            F2->dump();
1217
0
            F3->dump();
1218
0
            Valid = false;
1219
0
          }
1220
0
        }
1221
0
      }
1222
0
    }
1223
0
1224
0
    dbgs() << "MERGEFUNC-SANITY: " << (Valid ? "Passed." : "Failed.") << "\n";
1225
0
    return Valid;
1226
0
  }
1227
0
#endif // End HLSL Change
1228
0
  return true;
1229
0
}
1230
1231
0
bool MergeFunctions::runOnModule(Module &M) {
1232
0
  bool Changed = false;
1233
1234
0
  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1235
0
    if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
1236
0
      Deferred.push_back(WeakTrackingVH(I));
1237
0
  }
1238
1239
0
  do {
1240
0
    std::vector<WeakTrackingVH> Worklist;
1241
0
    Deferred.swap(Worklist);
1242
1243
0
    DEBUG(doSanityCheck(Worklist));
1244
1245
0
    DEBUG(dbgs() << "size of module: " << M.size() << '\n');
1246
0
    DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n');
1247
1248
    // Insert only strong functions and merge them. Strong function merging
1249
    // always deletes one of them.
1250
0
    for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(),
1251
0
                                               E = Worklist.end();
1252
0
         I != E; ++I) {
1253
0
      if (!*I) continue;
1254
0
      Function *F = cast<Function>(*I);
1255
0
      if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
1256
0
          !F->mayBeOverridden()) {
1257
0
        Changed |= insert(F);
1258
0
      }
1259
0
    }
1260
1261
    // Insert only weak functions and merge them. By doing these second we
1262
    // create thunks to the strong function when possible. When two weak
1263
    // functions are identical, we create a new strong function with two weak
1264
    // weak thunks to it which are identical but not mergable.
1265
0
    for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(),
1266
0
                                               E = Worklist.end();
1267
0
         I != E; ++I) {
1268
0
      if (!*I) continue;
1269
0
      Function *F = cast<Function>(*I);
1270
0
      if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
1271
0
          F->mayBeOverridden()) {
1272
0
        Changed |= insert(F);
1273
0
      }
1274
0
    }
1275
0
    DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n');
1276
0
  } while (!Deferred.empty());
1277
1278
0
  FnTree.clear();
1279
1280
0
  return Changed;
1281
0
}
1282
1283
// Replace direct callers of Old with New.
1284
0
void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) {
1285
0
  Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType());
1286
0
  for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) {
1287
0
    Use *U = &*UI;
1288
0
    ++UI;
1289
0
    CallSite CS(U->getUser());
1290
0
    if (CS && CS.isCallee(U)) {
1291
0
      remove(CS.getInstruction()->getParent()->getParent());
1292
0
      U->set(BitcastNew);
1293
0
    }
1294
0
  }
1295
0
}
1296
1297
// Replace G with an alias to F if possible, or else a thunk to F. Deletes G.
1298
0
void MergeFunctions::writeThunkOrAlias(Function *F, Function *G) {
1299
0
  if (HasGlobalAliases && G->hasUnnamedAddr()) {
1300
0
    if (G->hasExternalLinkage() || G->hasLocalLinkage() ||
1301
0
        G->hasWeakLinkage()) {
1302
0
      writeAlias(F, G);
1303
0
      return;
1304
0
    }
1305
0
  }
1306
1307
0
  writeThunk(F, G);
1308
0
}
1309
1310
// Helper for writeThunk,
1311
// Selects proper bitcast operation,
1312
// but a bit simpler then CastInst::getCastOpcode.
1313
0
static Value *createCast(IRBuilder<false> &Builder, Value *V, Type *DestTy) {
1314
0
  Type *SrcTy = V->getType();
1315
0
  if (SrcTy->isStructTy()) {
1316
0
    assert(DestTy->isStructTy());
1317
0
    assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements());
1318
0
    Value *Result = UndefValue::get(DestTy);
1319
0
    for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) {
1320
0
      Value *Element = createCast(
1321
0
          Builder, Builder.CreateExtractValue(V, makeArrayRef(I)),
1322
0
          DestTy->getStructElementType(I));
1323
1324
0
      Result =
1325
0
          Builder.CreateInsertValue(Result, Element, makeArrayRef(I));
1326
0
    }
1327
0
    return Result;
1328
0
  }
1329
0
  assert(!DestTy->isStructTy());
1330
0
  if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
1331
0
    return Builder.CreateIntToPtr(V, DestTy);
1332
0
  else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
1333
0
    return Builder.CreatePtrToInt(V, DestTy);
1334
0
  else
1335
0
    return Builder.CreateBitCast(V, DestTy);
1336
0
}
1337
1338
// Replace G with a simple tail call to bitcast(F). Also replace direct uses
1339
// of G with bitcast(F). Deletes G.
1340
0
void MergeFunctions::writeThunk(Function *F, Function *G) {
1341
0
  if (!G->mayBeOverridden()) {
1342
    // Redirect direct callers of G to F.
1343
0
    replaceDirectCallers(G, F);
1344
0
  }
1345
1346
  // If G was internal then we may have replaced all uses of G with F. If so,
1347
  // stop here and delete G. There's no need for a thunk.
1348
0
  if (G->hasLocalLinkage() && G->use_empty()) {
1349
0
    G->eraseFromParent();
1350
0
    return;
1351
0
  }
1352
1353
0
  Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "",
1354
0
                                    G->getParent());
1355
0
  BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG);
1356
0
  IRBuilder<false> Builder(BB);
1357
1358
0
  SmallVector<Value *, 16> Args;
1359
0
  unsigned i = 0;
1360
0
  FunctionType *FFTy = F->getFunctionType();
1361
0
  for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end();
1362
0
       AI != AE; ++AI) {
1363
0
    Args.push_back(createCast(Builder, (Value*)AI, FFTy->getParamType(i)));
1364
0
    ++i;
1365
0
  }
1366
1367
0
  CallInst *CI = Builder.CreateCall(F, Args);
1368
0
  CI->setTailCall();
1369
0
  CI->setCallingConv(F->getCallingConv());
1370
0
  if (NewG->getReturnType()->isVoidTy()) {
1371
0
    Builder.CreateRetVoid();
1372
0
  } else {
1373
0
    Builder.CreateRet(createCast(Builder, CI, NewG->getReturnType()));
1374
0
  }
1375
1376
0
  NewG->copyAttributesFrom(G);
1377
0
  NewG->takeName(G);
1378
0
  removeUsers(G);
1379
0
  G->replaceAllUsesWith(NewG);
1380
0
  G->eraseFromParent();
1381
1382
0
  DEBUG(dbgs() << "writeThunk: " << NewG->getName() << '\n');
1383
0
  ++NumThunksWritten;
1384
0
}
1385
1386
// Replace G with an alias to F and delete G.
1387
0
void MergeFunctions::writeAlias(Function *F, Function *G) {
1388
0
  PointerType *PTy = G->getType();
1389
0
  auto *GA = GlobalAlias::create(PTy, G->getLinkage(), "", F);
1390
0
  F->setAlignment(std::max(F->getAlignment(), G->getAlignment()));
1391
0
  GA->takeName(G);
1392
0
  GA->setVisibility(G->getVisibility());
1393
0
  removeUsers(G);
1394
0
  G->replaceAllUsesWith(GA);
1395
0
  G->eraseFromParent();
1396
1397
0
  DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n');
1398
0
  ++NumAliasesWritten;
1399
0
}
1400
1401
// Merge two equivalent functions. Upon completion, Function G is deleted.
1402
0
void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
1403
0
  if (F->mayBeOverridden()) {
1404
0
    assert(G->mayBeOverridden());
1405
1406
    // Make them both thunks to the same internal function.
1407
0
    Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "",
1408
0
                                   F->getParent());
1409
0
    H->copyAttributesFrom(F);
1410
0
    H->takeName(F);
1411
0
    removeUsers(F);
1412
0
    F->replaceAllUsesWith(H);
1413
1414
0
    unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment());
1415
1416
0
    if (HasGlobalAliases) {
1417
0
      writeAlias(F, G);
1418
0
      writeAlias(F, H);
1419
0
    } else {
1420
0
      writeThunk(F, G);
1421
0
      writeThunk(F, H);
1422
0
    }
1423
1424
0
    F->setAlignment(MaxAlignment);
1425
0
    F->setLinkage(GlobalValue::PrivateLinkage);
1426
0
    ++NumDoubleWeak;
1427
0
  } else {
1428
0
    writeThunkOrAlias(F, G);
1429
0
  }
1430
1431
0
  ++NumFunctionsMerged;
1432
0
}
1433
1434
/// Replace function F for function G in the map.
1435
void MergeFunctions::replaceFunctionInTree(FnTreeType::iterator &IterToF,
1436
0
                                           Function *G) {
1437
0
  Function *F = IterToF->getFunc();
1438
1439
  // A total order is already guaranteed otherwise because we process strong
1440
  // functions before weak functions.
1441
0
  assert(((F->mayBeOverridden() && G->mayBeOverridden()) ||
1442
0
          (!F->mayBeOverridden() && !G->mayBeOverridden())) &&
1443
0
         "Only change functions if both are strong or both are weak");
1444
0
  (void)F;
1445
1446
0
  IterToF->replaceBy(G);
1447
0
}
1448
1449
// Insert a ComparableFunction into the FnTree, or merge it away if equal to one
1450
// that was already inserted.
1451
0
bool MergeFunctions::insert(Function *NewFunction) {
1452
0
  std::pair<FnTreeType::iterator, bool> Result =
1453
0
      FnTree.insert(FunctionNode(NewFunction));
1454
1455
0
  if (Result.second) {
1456
0
    DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName() << '\n');
1457
0
    return false;
1458
0
  }
1459
1460
0
  const FunctionNode &OldF = *Result.first;
1461
1462
  // Don't merge tiny functions, since it can just end up making the function
1463
  // larger.
1464
  // FIXME: Should still merge them if they are unnamed_addr and produce an
1465
  // alias.
1466
0
  if (NewFunction->size() == 1) {
1467
0
    if (NewFunction->front().size() <= 2) {
1468
0
      DEBUG(dbgs() << NewFunction->getName()
1469
0
                   << " is to small to bother merging\n");
1470
0
      return false;
1471
0
    }
1472
0
  }
1473
1474
  // Impose a total order (by name) on the replacement of functions. This is
1475
  // important when operating on more than one module independently to prevent
1476
  // cycles of thunks calling each other when the modules are linked together.
1477
  //
1478
  // When one function is weak and the other is strong there is an order imposed
1479
  // already. We process strong functions before weak functions.
1480
0
  if ((OldF.getFunc()->mayBeOverridden() && NewFunction->mayBeOverridden()) ||
1481
0
      (!OldF.getFunc()->mayBeOverridden() && !NewFunction->mayBeOverridden()))
1482
0
    if (OldF.getFunc()->getName() > NewFunction->getName()) {
1483
      // Swap the two functions.
1484
0
      Function *F = OldF.getFunc();
1485
0
      replaceFunctionInTree(Result.first, NewFunction);
1486
0
      NewFunction = F;
1487
0
      assert(OldF.getFunc() != F && "Must have swapped the functions.");
1488
0
    }
1489
1490
  // Never thunk a strong function to a weak function.
1491
0
  assert(!OldF.getFunc()->mayBeOverridden() || NewFunction->mayBeOverridden());
1492
1493
0
  DEBUG(dbgs() << "  " << OldF.getFunc()->getName()
1494
0
               << " == " << NewFunction->getName() << '\n');
1495
1496
0
  Function *DeleteF = NewFunction;
1497
0
  mergeTwoFunctions(OldF.getFunc(), DeleteF);
1498
0
  return true;
1499
0
}
1500
1501
// Remove a function from FnTree. If it was already in FnTree, add
1502
// it to Deferred so that we'll look at it in the next round.
1503
0
void MergeFunctions::remove(Function *F) {
1504
  // We need to make sure we remove F, not a function "equal" to F per the
1505
  // function equality comparator.
1506
0
  FnTreeType::iterator found = FnTree.find(FunctionNode(F));
1507
0
  size_t Erased = 0;
1508
0
  if (found != FnTree.end() && found->getFunc() == F) {
1509
0
    Erased = 1;
1510
0
    FnTree.erase(found);
1511
0
  }
1512
1513
0
  if (Erased) {
1514
0
    DEBUG(dbgs() << "Removed " << F->getName()
1515
0
                 << " from set and deferred it.\n");
1516
0
    Deferred.emplace_back(F);
1517
0
  }
1518
0
}
1519
1520
// For each instruction used by the value, remove() the function that contains
1521
// the instruction. This should happen right before a call to RAUW.
1522
0
void MergeFunctions::removeUsers(Value *V) {
1523
0
  std::vector<Value *> Worklist;
1524
0
  Worklist.push_back(V);
1525
0
  while (!Worklist.empty()) {
1526
0
    Value *V = Worklist.back();
1527
0
    Worklist.pop_back();
1528
1529
0
    for (User *U : V->users()) {
1530
0
      if (Instruction *I = dyn_cast<Instruction>(U)) {
1531
0
        remove(I->getParent()->getParent());
1532
0
      } else if (isa<GlobalValue>(U)) {
1533
        // do nothing
1534
0
      } else if (Constant *C = dyn_cast<Constant>(U)) {
1535
0
        for (User *UU : C->users())
1536
0
          Worklist.push_back(UU);
1537
0
      }
1538
0
    }
1539
0
  }
1540
0
}