MergeFunctions.cpp 55 KB

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