MergeFunctions.cpp 55 KB

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