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