1*0a6a1f1dSLionel Sambuc================================= 2*0a6a1f1dSLionel SambucMergeFunctions pass, how it works 3*0a6a1f1dSLionel Sambuc================================= 4*0a6a1f1dSLionel Sambuc 5*0a6a1f1dSLionel Sambuc.. contents:: 6*0a6a1f1dSLionel Sambuc :local: 7*0a6a1f1dSLionel Sambuc 8*0a6a1f1dSLionel SambucIntroduction 9*0a6a1f1dSLionel Sambuc============ 10*0a6a1f1dSLionel SambucSometimes code contains equal functions, or functions that does exactly the same 11*0a6a1f1dSLionel Sambucthing even though they are non-equal on the IR level (e.g.: multiplication on 2 12*0a6a1f1dSLionel Sambucand 'shl 1'). It could happen due to several reasons: mainly, the usage of 13*0a6a1f1dSLionel Sambuctemplates and automatic code generators. Though, sometimes user itself could 14*0a6a1f1dSLionel Sambucwrite the same thing twice :-) 15*0a6a1f1dSLionel Sambuc 16*0a6a1f1dSLionel SambucThe main purpose of this pass is to recognize such functions and merge them. 17*0a6a1f1dSLionel Sambuc 18*0a6a1f1dSLionel SambucWhy would I want to read this document? 19*0a6a1f1dSLionel Sambuc--------------------------------------- 20*0a6a1f1dSLionel SambucDocument is the extension to pass comments and describes the pass logic. It 21*0a6a1f1dSLionel Sambucdescribes algorithm that is used in order to compare functions, it also 22*0a6a1f1dSLionel Sambucexplains how we could combine equal functions correctly, keeping module valid. 23*0a6a1f1dSLionel Sambuc 24*0a6a1f1dSLionel SambucMaterial is brought in top-down form, so reader could start learn pass from 25*0a6a1f1dSLionel Sambucideas and end up with low-level algorithm details, thus preparing him for 26*0a6a1f1dSLionel Sambucreading the sources. 27*0a6a1f1dSLionel Sambuc 28*0a6a1f1dSLionel SambucSo main goal is do describe algorithm and logic here; the concept. This document 29*0a6a1f1dSLionel Sambucis good for you, if you *don't want* to read the source code, but want to 30*0a6a1f1dSLionel Sambucunderstand pass algorithms. Author tried not to repeat the source-code and 31*0a6a1f1dSLionel Sambuccover only common cases, and thus avoid cases when after minor code changes we 32*0a6a1f1dSLionel Sambucneed to update this document. 33*0a6a1f1dSLionel Sambuc 34*0a6a1f1dSLionel Sambuc 35*0a6a1f1dSLionel SambucWhat should I know to be able to follow along with this document? 36*0a6a1f1dSLionel Sambuc----------------------------------------------------------------- 37*0a6a1f1dSLionel Sambuc 38*0a6a1f1dSLionel SambucReader should be familiar with common compile-engineering principles and LLVM 39*0a6a1f1dSLionel Sambuccode fundamentals. In this article we suppose reader is familiar with 40*0a6a1f1dSLionel Sambuc`Single Static Assingment <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_ 41*0a6a1f1dSLionel Sambucconcepts. Understanding of 42*0a6a1f1dSLionel Sambuc`IR structure <http://llvm.org/docs/LangRef.html#high-level-structure>`_ is 43*0a6a1f1dSLionel Sambucalso important. 44*0a6a1f1dSLionel Sambuc 45*0a6a1f1dSLionel SambucWe will use such terms as 46*0a6a1f1dSLionel Sambuc"`module <http://llvm.org/docs/LangRef.html#high-level-structure>`_", 47*0a6a1f1dSLionel Sambuc"`function <http://llvm.org/docs/ProgrammersManual.html#the-function-class>`_", 48*0a6a1f1dSLionel Sambuc"`basic block <http://en.wikipedia.org/wiki/Basic_block>`_", 49*0a6a1f1dSLionel Sambuc"`user <http://llvm.org/docs/ProgrammersManual.html#the-user-class>`_", 50*0a6a1f1dSLionel Sambuc"`value <http://llvm.org/docs/ProgrammersManual.html#the-value-class>`_", 51*0a6a1f1dSLionel Sambuc"`instruction <http://llvm.org/docs/ProgrammersManual.html#the-instruction-class>`_". 52*0a6a1f1dSLionel Sambuc 53*0a6a1f1dSLionel SambucAs a good start point, Kaleidoscope tutorial could be used: 54*0a6a1f1dSLionel Sambuc 55*0a6a1f1dSLionel Sambuc:doc:`tutorial/index` 56*0a6a1f1dSLionel Sambuc 57*0a6a1f1dSLionel SambucEspecially it's important to understand chapter 3 of tutorial: 58*0a6a1f1dSLionel Sambuc 59*0a6a1f1dSLionel Sambuc:doc:`tutorial/LangImpl3` 60*0a6a1f1dSLionel Sambuc 61*0a6a1f1dSLionel SambucReader also should know how passes work in LLVM, he could use next article as a 62*0a6a1f1dSLionel Sambucreference and start point here: 63*0a6a1f1dSLionel Sambuc 64*0a6a1f1dSLionel Sambuc:doc:`WritingAnLLVMPass` 65*0a6a1f1dSLionel Sambuc 66*0a6a1f1dSLionel SambucWhat else? Well perhaps reader also should have some experience in LLVM pass 67*0a6a1f1dSLionel Sambucdebugging and bug-fixing. 68*0a6a1f1dSLionel Sambuc 69*0a6a1f1dSLionel SambucWhat I gain by reading this document? 70*0a6a1f1dSLionel Sambuc------------------------------------- 71*0a6a1f1dSLionel SambucMain purpose is to provide reader with comfortable form of algorithms 72*0a6a1f1dSLionel Sambucdescription, namely the human reading text. Since it could be hard to 73*0a6a1f1dSLionel Sambucunderstand algorithm straight from the source code: pass uses some principles 74*0a6a1f1dSLionel Sambucthat have to be explained first. 75*0a6a1f1dSLionel Sambuc 76*0a6a1f1dSLionel SambucAuthor wishes to everybody to avoid case, when you read code from top to bottom 77*0a6a1f1dSLionel Sambucagain and again, and yet you don't understand why we implemented it that way. 78*0a6a1f1dSLionel Sambuc 79*0a6a1f1dSLionel SambucWe hope that after this article reader could easily debug and improve 80*0a6a1f1dSLionel SambucMergeFunctions pass and thus help LLVM project. 81*0a6a1f1dSLionel Sambuc 82*0a6a1f1dSLionel SambucNarrative structure 83*0a6a1f1dSLionel Sambuc------------------- 84*0a6a1f1dSLionel SambucArticle consists of three parts. First part explains pass functionality on the 85*0a6a1f1dSLionel Sambuctop-level. Second part describes the comparison procedure itself. The third 86*0a6a1f1dSLionel Sambucpart describes the merging process. 87*0a6a1f1dSLionel Sambuc 88*0a6a1f1dSLionel SambucIn every part author also tried to put the contents into the top-down form. 89*0a6a1f1dSLionel SambucFirst, the top-level methods will be described, while the terminal ones will be 90*0a6a1f1dSLionel Sambucat the end, in the tail of each part. If reader will see the reference to the 91*0a6a1f1dSLionel Sambucmethod that wasn't described yet, he will find its description a bit below. 92*0a6a1f1dSLionel Sambuc 93*0a6a1f1dSLionel SambucBasics 94*0a6a1f1dSLionel Sambuc====== 95*0a6a1f1dSLionel Sambuc 96*0a6a1f1dSLionel SambucHow to do it? 97*0a6a1f1dSLionel Sambuc------------- 98*0a6a1f1dSLionel SambucDo we need to merge functions? Obvious thing is: yes that's a quite possible 99*0a6a1f1dSLionel Sambuccase, since usually we *do* have duplicates. And it would be good to get rid of 100*0a6a1f1dSLionel Sambucthem. But how to detect such a duplicates? The idea is next: we split functions 101*0a6a1f1dSLionel Sambuconto small bricks (parts), then we compare "bricks" amount, and if it equal, 102*0a6a1f1dSLionel Sambuccompare "bricks" themselves, and then do our conclusions about functions 103*0a6a1f1dSLionel Sambucthemselves. 104*0a6a1f1dSLionel Sambuc 105*0a6a1f1dSLionel SambucWhat the difference it could be? For example, on machine with 64-bit pointers 106*0a6a1f1dSLionel Sambuc(let's assume we have only one address space), one function stores 64-bit 107*0a6a1f1dSLionel Sambucinteger, while another one stores a pointer. So if the target is a machine 108*0a6a1f1dSLionel Sambucmentioned above, and if functions are identical, except the parameter type (we 109*0a6a1f1dSLionel Sambuccould consider it as a part of function type), then we can treat ``uint64_t`` 110*0a6a1f1dSLionel Sambucand``void*`` as equal. 111*0a6a1f1dSLionel Sambuc 112*0a6a1f1dSLionel SambucIt was just an example; possible details are described a bit below. 113*0a6a1f1dSLionel Sambuc 114*0a6a1f1dSLionel SambucAs another example reader may imagine two more functions. First function 115*0a6a1f1dSLionel Sambucperforms multiplication on 2, while the second one performs arithmetic right 116*0a6a1f1dSLionel Sambucshift on 1. 117*0a6a1f1dSLionel Sambuc 118*0a6a1f1dSLionel SambucPossible solutions 119*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^ 120*0a6a1f1dSLionel SambucLet's briefly consider possible options about how and what we have to implement 121*0a6a1f1dSLionel Sambucin order to create full-featured functions merging, and also what it would 122*0a6a1f1dSLionel Sambucmeant for us. 123*0a6a1f1dSLionel Sambuc 124*0a6a1f1dSLionel SambucEqual functions detection, obviously supposes "detector" method to be 125*0a6a1f1dSLionel Sambucimplemented, latter should answer the question "whether functions are equal". 126*0a6a1f1dSLionel SambucThis "detector" method consists of tiny "sub-detectors", each of them answers 127*0a6a1f1dSLionel Sambucexactly the same question, but for function parts. 128*0a6a1f1dSLionel Sambuc 129*0a6a1f1dSLionel SambucAs the second step, we should merge equal functions. So it should be a "merger" 130*0a6a1f1dSLionel Sambucmethod. "Merger" accepts two functions *F1* and *F2*, and produces *F1F2* 131*0a6a1f1dSLionel Sambucfunction, the result of merging. 132*0a6a1f1dSLionel Sambuc 133*0a6a1f1dSLionel SambucHaving such a routines in our hands, we can process whole module, and merge all 134*0a6a1f1dSLionel Sambucequal functions. 135*0a6a1f1dSLionel Sambuc 136*0a6a1f1dSLionel SambucIn this case, we have to compare every function with every another function. As 137*0a6a1f1dSLionel Sambucreader could notice, this way seems to be quite expensive. Of course we could 138*0a6a1f1dSLionel Sambucintroduce hashing and other helpers, but it is still just an optimization, and 139*0a6a1f1dSLionel Sambucthus the level of O(N*N) complexity. 140*0a6a1f1dSLionel Sambuc 141*0a6a1f1dSLionel SambucCan we reach another level? Could we introduce logarithmical search, or random 142*0a6a1f1dSLionel Sambucaccess lookup? The answer is: "yes". 143*0a6a1f1dSLionel Sambuc 144*0a6a1f1dSLionel SambucRandom-access 145*0a6a1f1dSLionel Sambuc""""""""""""" 146*0a6a1f1dSLionel SambucHow it could be done? Just convert each function to number, and gather all of 147*0a6a1f1dSLionel Sambucthem in special hash-table. Functions with equal hash are equal. Good hashing 148*0a6a1f1dSLionel Sambucmeans, that every function part must be taken into account. That means we have 149*0a6a1f1dSLionel Sambucto convert every function part into some number, and then add it into hash. 150*0a6a1f1dSLionel SambucLookup-up time would be small, but such approach adds some delay due to hashing 151*0a6a1f1dSLionel Sambucroutine. 152*0a6a1f1dSLionel Sambuc 153*0a6a1f1dSLionel SambucLogarithmical search 154*0a6a1f1dSLionel Sambuc"""""""""""""""""""" 155*0a6a1f1dSLionel SambucWe could introduce total ordering among the functions set, once we had it we 156*0a6a1f1dSLionel Sambuccould then implement a logarithmical search. Lookup time still depends on N, 157*0a6a1f1dSLionel Sambucbut adds a little of delay (*log(N)*). 158*0a6a1f1dSLionel Sambuc 159*0a6a1f1dSLionel SambucPresent state 160*0a6a1f1dSLionel Sambuc""""""""""""" 161*0a6a1f1dSLionel SambucBoth of approaches (random-access and logarithmical) has been implemented and 162*0a6a1f1dSLionel Sambuctested. And both of them gave a very good improvement. And what was most 163*0a6a1f1dSLionel Sambucsurprising, logarithmical search was faster; sometimes up to 15%. Hashing needs 164*0a6a1f1dSLionel Sambucsome extra CPU time, and it is the main reason why it works slower; in most of 165*0a6a1f1dSLionel Sambuccases total "hashing" time was greater than total "logarithmical-search" time. 166*0a6a1f1dSLionel Sambuc 167*0a6a1f1dSLionel SambucSo, preference has been granted to the "logarithmical search". 168*0a6a1f1dSLionel Sambuc 169*0a6a1f1dSLionel SambucThough in the case of need, *logarithmical-search* (read "total-ordering") could 170*0a6a1f1dSLionel Sambucbe used as a milestone on our way to the *random-access* implementation. 171*0a6a1f1dSLionel Sambuc 172*0a6a1f1dSLionel SambucEvery comparison is based either on the numbers or on flags comparison. In 173*0a6a1f1dSLionel Sambuc*random-access* approach we could use the same comparison algorithm. During 174*0a6a1f1dSLionel Sambuccomparison we exit once we find the difference, but here we might have to scan 175*0a6a1f1dSLionel Sambucwhole function body every time (note, it could be slower). Like in 176*0a6a1f1dSLionel Sambuc"total-ordering", we will track every numbers and flags, but instead of 177*0a6a1f1dSLionel Sambuccomparison, we should get numbers sequence and then create the hash number. So, 178*0a6a1f1dSLionel Sambuconce again, *total-ordering* could be considered as a milestone for even faster 179*0a6a1f1dSLionel Sambuc(in theory) random-access approach. 180*0a6a1f1dSLionel Sambuc 181*0a6a1f1dSLionel SambucMergeFunctions, main fields and runOnModule 182*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 183*0a6a1f1dSLionel SambucThere are two most important fields in class: 184*0a6a1f1dSLionel Sambuc 185*0a6a1f1dSLionel Sambuc``FnTree`` – the set of all unique functions. It keeps items that couldn't be 186*0a6a1f1dSLionel Sambucmerged with each other. It is defined as: 187*0a6a1f1dSLionel Sambuc 188*0a6a1f1dSLionel Sambuc``std::set<FunctionNode> FnTree;`` 189*0a6a1f1dSLionel Sambuc 190*0a6a1f1dSLionel SambucHere ``FunctionNode`` is a wrapper for ``llvm::Function`` class, with 191*0a6a1f1dSLionel Sambucimplemented “<” operator among the functions set (below we explain how it works 192*0a6a1f1dSLionel Sambucexactly; this is a key point in fast functions comparison). 193*0a6a1f1dSLionel Sambuc 194*0a6a1f1dSLionel Sambuc``Deferred`` – merging process can affect bodies of functions that are in 195*0a6a1f1dSLionel Sambuc``FnTree`` already. Obviously such functions should be rechecked again. In this 196*0a6a1f1dSLionel Sambuccase we remove them from ``FnTree``, and mark them as to be rescanned, namely 197*0a6a1f1dSLionel Sambucput them into ``Deferred`` list. 198*0a6a1f1dSLionel Sambuc 199*0a6a1f1dSLionel SambucrunOnModule 200*0a6a1f1dSLionel Sambuc""""""""""" 201*0a6a1f1dSLionel SambucThe algorithm is pretty simple: 202*0a6a1f1dSLionel Sambuc 203*0a6a1f1dSLionel Sambuc1. Put all module's functions into the *worklist*. 204*0a6a1f1dSLionel Sambuc 205*0a6a1f1dSLionel Sambuc2. Scan *worklist*'s functions twice: first enumerate only strong functions and 206*0a6a1f1dSLionel Sambucthen only weak ones: 207*0a6a1f1dSLionel Sambuc 208*0a6a1f1dSLionel Sambuc 2.1. Loop body: take function from *worklist* (call it *FCur*) and try to 209*0a6a1f1dSLionel Sambuc insert it into *FnTree*: check whether *FCur* is equal to one of functions 210*0a6a1f1dSLionel Sambuc in *FnTree*. If there *is* equal function in *FnTree* (call it *FExists*): 211*0a6a1f1dSLionel Sambuc merge function *FCur* with *FExists*. Otherwise add function from *worklist* 212*0a6a1f1dSLionel Sambuc to *FnTree*. 213*0a6a1f1dSLionel Sambuc 214*0a6a1f1dSLionel Sambuc3. Once *worklist* scanning and merging operations is complete, check *Deferred* 215*0a6a1f1dSLionel Sambuclist. If it is not empty: refill *worklist* contents with *Deferred* list and 216*0a6a1f1dSLionel Sambucdo step 2 again, if *Deferred* is empty, then exit from method. 217*0a6a1f1dSLionel Sambuc 218*0a6a1f1dSLionel SambucComparison and logarithmical search 219*0a6a1f1dSLionel Sambuc""""""""""""""""""""""""""""""""""" 220*0a6a1f1dSLionel SambucLet's recall our task: for every function *F* from module *M*, we have to find 221*0a6a1f1dSLionel Sambucequal functions *F`* in shortest time, and merge them into the single function. 222*0a6a1f1dSLionel Sambuc 223*0a6a1f1dSLionel SambucDefining total ordering among the functions set allows to organize functions 224*0a6a1f1dSLionel Sambucinto the binary tree. The lookup procedure complexity would be estimated as 225*0a6a1f1dSLionel SambucO(log(N)) in this case. But how to define *total-ordering*? 226*0a6a1f1dSLionel Sambuc 227*0a6a1f1dSLionel SambucWe have to introduce a single rule applicable to every pair of functions, and 228*0a6a1f1dSLionel Sambucfollowing this rule then evaluate which of them is greater. What kind of rule 229*0a6a1f1dSLionel Sambucit could be? Let's declare it as "compare" method, that returns one of 3 230*0a6a1f1dSLionel Sambucpossible values: 231*0a6a1f1dSLionel Sambuc 232*0a6a1f1dSLionel Sambuc-1, left is *less* than right, 233*0a6a1f1dSLionel Sambuc 234*0a6a1f1dSLionel Sambuc0, left and right are *equal*, 235*0a6a1f1dSLionel Sambuc 236*0a6a1f1dSLionel Sambuc1, left is *greater* than right. 237*0a6a1f1dSLionel Sambuc 238*0a6a1f1dSLionel SambucOf course it means, that we have to maintain 239*0a6a1f1dSLionel Sambuc*strict and non-strict order relation properties*: 240*0a6a1f1dSLionel Sambuc 241*0a6a1f1dSLionel Sambuc* reflexivity (``a <= a``, ``a == a``, ``a >= a``), 242*0a6a1f1dSLionel Sambuc* antisymmetry (if ``a <= b`` and ``b <= a`` then ``a == b``), 243*0a6a1f1dSLionel Sambuc* transitivity (``a <= b`` and ``b <= c``, then ``a <= c``) 244*0a6a1f1dSLionel Sambuc* asymmetry (if ``a < b``, then ``a > b`` or ``a == b``). 245*0a6a1f1dSLionel Sambuc 246*0a6a1f1dSLionel SambucAs it was mentioned before, comparison routine consists of 247*0a6a1f1dSLionel Sambuc"sub-comparison-routines", each of them also consists 248*0a6a1f1dSLionel Sambuc"sub-comparison-routines", and so on, finally it ends up with a primitives 249*0a6a1f1dSLionel Sambuccomparison. 250*0a6a1f1dSLionel Sambuc 251*0a6a1f1dSLionel SambucBelow, we will use the next operations: 252*0a6a1f1dSLionel Sambuc 253*0a6a1f1dSLionel Sambuc#. ``cmpNumbers(number1, number2)`` is method that returns -1 if left is less 254*0a6a1f1dSLionel Sambuc than right; 0, if left and right are equal; and 1 otherwise. 255*0a6a1f1dSLionel Sambuc 256*0a6a1f1dSLionel Sambuc#. ``cmpFlags(flag1, flag2)`` is hypothetical method that compares two flags. 257*0a6a1f1dSLionel Sambuc The logic is the same as in ``cmpNumbers``, where ``true`` is 1, and 258*0a6a1f1dSLionel Sambuc ``false`` is 0. 259*0a6a1f1dSLionel Sambuc 260*0a6a1f1dSLionel SambucThe rest of article is based on *MergeFunctions.cpp* source code 261*0a6a1f1dSLionel Sambuc(*<llvm_dir>/lib/Transforms/IPO/MergeFunctions.cpp*). We would like to ask 262*0a6a1f1dSLionel Sambucreader to keep this file open nearby, so we could use it as a reference for 263*0a6a1f1dSLionel Sambucfurther explanations. 264*0a6a1f1dSLionel Sambuc 265*0a6a1f1dSLionel SambucNow we're ready to proceed to the next chapter and see how it works. 266*0a6a1f1dSLionel Sambuc 267*0a6a1f1dSLionel SambucFunctions comparison 268*0a6a1f1dSLionel Sambuc==================== 269*0a6a1f1dSLionel SambucAt first, let's define how exactly we compare complex objects. 270*0a6a1f1dSLionel Sambuc 271*0a6a1f1dSLionel SambucComplex objects comparison (function, basic-block, etc) is mostly based on its 272*0a6a1f1dSLionel Sambucsub-objects comparison results. So it is similar to the next "tree" objects 273*0a6a1f1dSLionel Sambuccomparison: 274*0a6a1f1dSLionel Sambuc 275*0a6a1f1dSLionel Sambuc#. For two trees *T1* and *T2* we perform *depth-first-traversal* and have 276*0a6a1f1dSLionel Sambuc two sequences as a product: "*T1Items*" and "*T2Items*". 277*0a6a1f1dSLionel Sambuc 278*0a6a1f1dSLionel Sambuc#. Then compare chains "*T1Items*" and "*T2Items*" in 279*0a6a1f1dSLionel Sambuc most-significant-item-first order. Result of items comparison would be the 280*0a6a1f1dSLionel Sambuc result of *T1* and *T2* comparison itself. 281*0a6a1f1dSLionel Sambuc 282*0a6a1f1dSLionel SambucFunctionComparator::compare(void) 283*0a6a1f1dSLionel Sambuc--------------------------------- 284*0a6a1f1dSLionel SambucBrief look at the source code tells us, that comparison starts in 285*0a6a1f1dSLionel Sambuc“``int FunctionComparator::compare(void)``” method. 286*0a6a1f1dSLionel Sambuc 287*0a6a1f1dSLionel Sambuc1. First parts to be compared are function's attributes and some properties that 288*0a6a1f1dSLionel Sambucoutsides “attributes” term, but still could make function different without 289*0a6a1f1dSLionel Sambucchanging its body. This part of comparison is usually done within simple 290*0a6a1f1dSLionel Sambuc*cmpNumbers* or *cmpFlags* operations (e.g. 291*0a6a1f1dSLionel Sambuc``cmpFlags(F1->hasGC(), F2->hasGC())``). Below is full list of function's 292*0a6a1f1dSLionel Sambucproperties to be compared on this stage: 293*0a6a1f1dSLionel Sambuc 294*0a6a1f1dSLionel Sambuc * *Attributes* (those are returned by ``Function::getAttributes()`` 295*0a6a1f1dSLionel Sambuc method). 296*0a6a1f1dSLionel Sambuc 297*0a6a1f1dSLionel Sambuc * *GC*, for equivalence, *RHS* and *LHS* should be both either without 298*0a6a1f1dSLionel Sambuc *GC* or with the same one. 299*0a6a1f1dSLionel Sambuc 300*0a6a1f1dSLionel Sambuc * *Section*, just like a *GC*: *RHS* and *LHS* should be defined in the 301*0a6a1f1dSLionel Sambuc same section. 302*0a6a1f1dSLionel Sambuc 303*0a6a1f1dSLionel Sambuc * *Variable arguments*. *LHS* and *RHS* should be both either with or 304*0a6a1f1dSLionel Sambuc without *var-args*. 305*0a6a1f1dSLionel Sambuc 306*0a6a1f1dSLionel Sambuc * *Calling convention* should be the same. 307*0a6a1f1dSLionel Sambuc 308*0a6a1f1dSLionel Sambuc2. Function type. Checked by ``FunctionComparator::cmpType(Type*, Type*)`` 309*0a6a1f1dSLionel Sambucmethod. It checks return type and parameters type; the method itself will be 310*0a6a1f1dSLionel Sambucdescribed later. 311*0a6a1f1dSLionel Sambuc 312*0a6a1f1dSLionel Sambuc3. Associate function formal parameters with each other. Then comparing function 313*0a6a1f1dSLionel Sambucbodies, if we see the usage of *LHS*'s *i*-th argument in *LHS*'s body, then, 314*0a6a1f1dSLionel Sambucwe want to see usage of *RHS*'s *i*-th argument at the same place in *RHS*'s 315*0a6a1f1dSLionel Sambucbody, otherwise functions are different. On this stage we grant the preference 316*0a6a1f1dSLionel Sambucto those we met later in function body (value we met first would be *less*). 317*0a6a1f1dSLionel SambucThis is done by “``FunctionComparator::cmpValues(const Value*, const Value*)``” 318*0a6a1f1dSLionel Sambucmethod (will be described a bit later). 319*0a6a1f1dSLionel Sambuc 320*0a6a1f1dSLionel Sambuc4. Function body comparison. As it written in method comments: 321*0a6a1f1dSLionel Sambuc 322*0a6a1f1dSLionel Sambuc“We do a CFG-ordered walk since the actual ordering of the blocks in the linked 323*0a6a1f1dSLionel Sambuclist is immaterial. Our walk starts at the entry block for both functions, then 324*0a6a1f1dSLionel Sambuctakes each block from each terminator in order. As an artifact, this also means 325*0a6a1f1dSLionel Sambucthat unreachable blocks are ignored.” 326*0a6a1f1dSLionel Sambuc 327*0a6a1f1dSLionel SambucSo, using this walk we get BBs from *left* and *right* in the same order, and 328*0a6a1f1dSLionel Sambuccompare them by “``FunctionComparator::compare(const BasicBlock*, const 329*0a6a1f1dSLionel SambucBasicBlock*)``” method. 330*0a6a1f1dSLionel Sambuc 331*0a6a1f1dSLionel SambucWe also associate BBs with each other, like we did it with function formal 332*0a6a1f1dSLionel Sambucarguments (see ``cmpValues`` method below). 333*0a6a1f1dSLionel Sambuc 334*0a6a1f1dSLionel SambucFunctionComparator::cmpType 335*0a6a1f1dSLionel Sambuc--------------------------- 336*0a6a1f1dSLionel SambucConsider how types comparison works. 337*0a6a1f1dSLionel Sambuc 338*0a6a1f1dSLionel Sambuc1. Coerce pointer to integer. If left type is a pointer, try to coerce it to the 339*0a6a1f1dSLionel Sambucinteger type. It could be done if its address space is 0, or if address spaces 340*0a6a1f1dSLionel Sambucare ignored at all. Do the same thing for the right type. 341*0a6a1f1dSLionel Sambuc 342*0a6a1f1dSLionel Sambuc2. If left and right types are equal, return 0. Otherwise we need to give 343*0a6a1f1dSLionel Sambucpreference to one of them. So proceed to the next step. 344*0a6a1f1dSLionel Sambuc 345*0a6a1f1dSLionel Sambuc3. If types are of different kind (different type IDs). Return result of type 346*0a6a1f1dSLionel SambucIDs comparison, treating them as a numbers (use ``cmpNumbers`` operation). 347*0a6a1f1dSLionel Sambuc 348*0a6a1f1dSLionel Sambuc4. If types are vectors or integers, return result of their pointers comparison, 349*0a6a1f1dSLionel Sambuccomparing them as numbers. 350*0a6a1f1dSLionel Sambuc 351*0a6a1f1dSLionel Sambuc5. Check whether type ID belongs to the next group (call it equivalent-group): 352*0a6a1f1dSLionel Sambuc 353*0a6a1f1dSLionel Sambuc * Void 354*0a6a1f1dSLionel Sambuc 355*0a6a1f1dSLionel Sambuc * Float 356*0a6a1f1dSLionel Sambuc 357*0a6a1f1dSLionel Sambuc * Double 358*0a6a1f1dSLionel Sambuc 359*0a6a1f1dSLionel Sambuc * X86_FP80 360*0a6a1f1dSLionel Sambuc 361*0a6a1f1dSLionel Sambuc * FP128 362*0a6a1f1dSLionel Sambuc 363*0a6a1f1dSLionel Sambuc * PPC_FP128 364*0a6a1f1dSLionel Sambuc 365*0a6a1f1dSLionel Sambuc * Label 366*0a6a1f1dSLionel Sambuc 367*0a6a1f1dSLionel Sambuc * Metadata. 368*0a6a1f1dSLionel Sambuc 369*0a6a1f1dSLionel Sambuc If ID belongs to group above, return 0. Since it's enough to see that 370*0a6a1f1dSLionel Sambuc types has the same ``TypeID``. No additional information is required. 371*0a6a1f1dSLionel Sambuc 372*0a6a1f1dSLionel Sambuc6. Left and right are pointers. Return result of address space comparison 373*0a6a1f1dSLionel Sambuc(numbers comparison). 374*0a6a1f1dSLionel Sambuc 375*0a6a1f1dSLionel Sambuc7. Complex types (structures, arrays, etc.). Follow complex objects comparison 376*0a6a1f1dSLionel Sambuctechnique (see the very first paragraph of this chapter). Both *left* and 377*0a6a1f1dSLionel Sambuc*right* are to be expanded and their element types will be checked the same 378*0a6a1f1dSLionel Sambucway. If we get -1 or 1 on some stage, return it. Otherwise return 0. 379*0a6a1f1dSLionel Sambuc 380*0a6a1f1dSLionel Sambuc8. Steps 1-6 describe all the possible cases, if we passed steps 1-6 and didn't 381*0a6a1f1dSLionel Sambucget any conclusions, then invoke ``llvm_unreachable``, since it's quite 382*0a6a1f1dSLionel Sambucunexpectable case. 383*0a6a1f1dSLionel Sambuc 384*0a6a1f1dSLionel SambuccmpValues(const Value*, const Value*) 385*0a6a1f1dSLionel Sambuc------------------------------------- 386*0a6a1f1dSLionel SambucMethod that compares local values. 387*0a6a1f1dSLionel Sambuc 388*0a6a1f1dSLionel SambucThis method gives us an answer on a very curious quesion: whether we could treat 389*0a6a1f1dSLionel Sambuclocal values as equal, and which value is greater otherwise. It's better to 390*0a6a1f1dSLionel Sambucstart from example: 391*0a6a1f1dSLionel Sambuc 392*0a6a1f1dSLionel SambucConsider situation when we're looking at the same place in left function "*FL*" 393*0a6a1f1dSLionel Sambucand in right function "*FR*". And every part of *left* place is equal to the 394*0a6a1f1dSLionel Sambuccorresponding part of *right* place, and (!) both parts use *Value* instances, 395*0a6a1f1dSLionel Sambucfor example: 396*0a6a1f1dSLionel Sambuc 397*0a6a1f1dSLionel Sambuc.. code-block:: llvm 398*0a6a1f1dSLionel Sambuc 399*0a6a1f1dSLionel Sambuc instr0 i32 %LV ; left side, function FL 400*0a6a1f1dSLionel Sambuc instr0 i32 %RV ; right side, function FR 401*0a6a1f1dSLionel Sambuc 402*0a6a1f1dSLionel SambucSo, now our conclusion depends on *Value* instances comparison. 403*0a6a1f1dSLionel Sambuc 404*0a6a1f1dSLionel SambucMain purpose of this method is to determine relation between such values. 405*0a6a1f1dSLionel Sambuc 406*0a6a1f1dSLionel SambucWhat we expect from equal functions? At the same place, in functions "*FL*" and 407*0a6a1f1dSLionel Sambuc"*FR*" we expect to see *equal* values, or values *defined* at the same place 408*0a6a1f1dSLionel Sambucin "*FL*" and "*FR*". 409*0a6a1f1dSLionel Sambuc 410*0a6a1f1dSLionel SambucConsider small example here: 411*0a6a1f1dSLionel Sambuc 412*0a6a1f1dSLionel Sambuc.. code-block:: llvm 413*0a6a1f1dSLionel Sambuc 414*0a6a1f1dSLionel Sambuc define void %f(i32 %pf0, i32 %pf1) { 415*0a6a1f1dSLionel Sambuc instr0 i32 %pf0 instr1 i32 %pf1 instr2 i32 123 416*0a6a1f1dSLionel Sambuc } 417*0a6a1f1dSLionel Sambuc 418*0a6a1f1dSLionel Sambuc.. code-block:: llvm 419*0a6a1f1dSLionel Sambuc 420*0a6a1f1dSLionel Sambuc define void %g(i32 %pg0, i32 %pg1) { 421*0a6a1f1dSLionel Sambuc instr0 i32 %pg0 instr1 i32 %pg0 instr2 i32 123 422*0a6a1f1dSLionel Sambuc } 423*0a6a1f1dSLionel Sambuc 424*0a6a1f1dSLionel SambucIn this example, *pf0* is associated with *pg0*, *pf1* is associated with *pg1*, 425*0a6a1f1dSLionel Sambucand we also declare that *pf0* < *pf1*, and thus *pg0* < *pf1*. 426*0a6a1f1dSLionel Sambuc 427*0a6a1f1dSLionel SambucInstructions with opcode "*instr0*" would be *equal*, since their types and 428*0a6a1f1dSLionel Sambucopcodes are equal, and values are *associated*. 429*0a6a1f1dSLionel Sambuc 430*0a6a1f1dSLionel SambucInstruction with opcode "*instr1*" from *f* is *greater* than instruction with 431*0a6a1f1dSLionel Sambucopcode "*instr1*" from *g*; here we have equal types and opcodes, but "*pf1* is 432*0a6a1f1dSLionel Sambucgreater than "*pg0*". 433*0a6a1f1dSLionel Sambuc 434*0a6a1f1dSLionel SambucAnd instructions with opcode "*instr2*" are equal, because their opcodes and 435*0a6a1f1dSLionel Sambuctypes are equal, and the same constant is used as a value. 436*0a6a1f1dSLionel Sambuc 437*0a6a1f1dSLionel SambucWhat we assiciate in cmpValues? 438*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 439*0a6a1f1dSLionel Sambuc* Function arguments. *i*-th argument from left function associated with 440*0a6a1f1dSLionel Sambuc *i*-th argument from right function. 441*0a6a1f1dSLionel Sambuc* BasicBlock instances. In basic-block enumeration loop we associate *i*-th 442*0a6a1f1dSLionel Sambuc BasicBlock from the left function with *i*-th BasicBlock from the right 443*0a6a1f1dSLionel Sambuc function. 444*0a6a1f1dSLionel Sambuc* Instructions. 445*0a6a1f1dSLionel Sambuc* Instruction operands. Note, we can meet *Value* here we have never seen 446*0a6a1f1dSLionel Sambuc before. In this case it is not a function argument, nor *BasicBlock*, nor 447*0a6a1f1dSLionel Sambuc *Instruction*. It is global value. It is constant, since its the only 448*0a6a1f1dSLionel Sambuc supposed global here. Method also compares: 449*0a6a1f1dSLionel Sambuc* Constants that are of the same type. 450*0a6a1f1dSLionel Sambuc* If right constant could be losslessly bit-casted to the left one, then we 451*0a6a1f1dSLionel Sambuc also compare them. 452*0a6a1f1dSLionel Sambuc 453*0a6a1f1dSLionel SambucHow to implement cmpValues? 454*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^ 455*0a6a1f1dSLionel Sambuc*Association* is a case of equality for us. We just treat such values as equal. 456*0a6a1f1dSLionel SambucBut, in general, we need to implement antisymmetric relation. As it was 457*0a6a1f1dSLionel Sambucmentioned above, to understand what is *less*, we can use order in which we 458*0a6a1f1dSLionel Sambucmeet values. If both of values has the same order in function (met at the same 459*0a6a1f1dSLionel Sambuctime), then treat values as *associated*. Otherwise – it depends on who was 460*0a6a1f1dSLionel Sambucfirst. 461*0a6a1f1dSLionel Sambuc 462*0a6a1f1dSLionel SambucEvery time we run top-level compare method, we initialize two identical maps 463*0a6a1f1dSLionel Sambuc(one for the left side, another one for the right side): 464*0a6a1f1dSLionel Sambuc 465*0a6a1f1dSLionel Sambuc``map<Value, int> sn_mapL, sn_mapR;`` 466*0a6a1f1dSLionel Sambuc 467*0a6a1f1dSLionel SambucThe key of the map is the *Value* itself, the *value* – is its order (call it 468*0a6a1f1dSLionel Sambuc*serial number*). 469*0a6a1f1dSLionel Sambuc 470*0a6a1f1dSLionel SambucTo add value *V* we need to perform the next procedure: 471*0a6a1f1dSLionel Sambuc 472*0a6a1f1dSLionel Sambuc``sn_map.insert(std::make_pair(V, sn_map.size()));`` 473*0a6a1f1dSLionel Sambuc 474*0a6a1f1dSLionel SambucFor the first *Value*, map will return *0*, for second *Value* map will return 475*0a6a1f1dSLionel Sambuc*1*, and so on. 476*0a6a1f1dSLionel Sambuc 477*0a6a1f1dSLionel SambucThen we can check whether left and right values met at the same time with simple 478*0a6a1f1dSLionel Sambuccomparison: 479*0a6a1f1dSLionel Sambuc 480*0a6a1f1dSLionel Sambuc``cmpNumbers(sn_mapL[Left], sn_mapR[Right]);`` 481*0a6a1f1dSLionel Sambuc 482*0a6a1f1dSLionel SambucOf course, we can combine insertion and comparison: 483*0a6a1f1dSLionel Sambuc 484*0a6a1f1dSLionel Sambuc.. code-block:: c++ 485*0a6a1f1dSLionel Sambuc 486*0a6a1f1dSLionel Sambuc std::pair<iterator, bool> 487*0a6a1f1dSLionel Sambuc LeftRes = sn_mapL.insert(std::make_pair(Left, sn_mapL.size())), RightRes 488*0a6a1f1dSLionel Sambuc = sn_mapR.insert(std::make_pair(Right, sn_mapR.size())); 489*0a6a1f1dSLionel Sambuc return cmpNumbers(LeftRes.first->second, RightRes.first->second); 490*0a6a1f1dSLionel Sambuc 491*0a6a1f1dSLionel SambucLet's look, how whole method could be implemented. 492*0a6a1f1dSLionel Sambuc 493*0a6a1f1dSLionel Sambuc1. we have to start from the bad news. Consider function self and 494*0a6a1f1dSLionel Sambuccross-referencing cases: 495*0a6a1f1dSLionel Sambuc 496*0a6a1f1dSLionel Sambuc.. code-block:: c++ 497*0a6a1f1dSLionel Sambuc 498*0a6a1f1dSLionel Sambuc // self-reference unsigned fact0(unsigned n) { return n > 1 ? n 499*0a6a1f1dSLionel Sambuc * fact0(n-1) : 1; } unsigned fact1(unsigned n) { return n > 1 ? n * 500*0a6a1f1dSLionel Sambuc fact1(n-1) : 1; } 501*0a6a1f1dSLionel Sambuc 502*0a6a1f1dSLionel Sambuc // cross-reference unsigned ping(unsigned n) { return n!= 0 ? pong(n-1) : 0; 503*0a6a1f1dSLionel Sambuc } unsigned pong(unsigned n) { return n!= 0 ? ping(n-1) : 0; } 504*0a6a1f1dSLionel Sambuc 505*0a6a1f1dSLionel Sambuc.. 506*0a6a1f1dSLionel Sambuc 507*0a6a1f1dSLionel Sambuc This comparison has been implemented in initial *MergeFunctions* pass 508*0a6a1f1dSLionel Sambuc version. But, unfortunately, it is not transitive. And this is the only case 509*0a6a1f1dSLionel Sambuc we can't convert to less-equal-greater comparison. It is a seldom case, 4-5 510*0a6a1f1dSLionel Sambuc functions of 10000 (checked on test-suite), and, we hope, reader would 511*0a6a1f1dSLionel Sambuc forgive us for such a sacrifice in order to get the O(log(N)) pass time. 512*0a6a1f1dSLionel Sambuc 513*0a6a1f1dSLionel Sambuc2. If left/right *Value* is a constant, we have to compare them. Return 0 if it 514*0a6a1f1dSLionel Sambucis the same constant, or use ``cmpConstants`` method otherwise. 515*0a6a1f1dSLionel Sambuc 516*0a6a1f1dSLionel Sambuc3. If left/right is *InlineAsm* instance. Return result of *Value* pointers 517*0a6a1f1dSLionel Sambuccomparison. 518*0a6a1f1dSLionel Sambuc 519*0a6a1f1dSLionel Sambuc4. Explicit association of *L* (left value) and *R* (right value). We need to 520*0a6a1f1dSLionel Sambucfind out whether values met at the same time, and thus are *associated*. Or we 521*0a6a1f1dSLionel Sambucneed to put the rule: when we treat *L* < *R*. Now it is easy: just return 522*0a6a1f1dSLionel Sambucresult of numbers comparison: 523*0a6a1f1dSLionel Sambuc 524*0a6a1f1dSLionel Sambuc.. code-block:: c++ 525*0a6a1f1dSLionel Sambuc 526*0a6a1f1dSLionel Sambuc std::pair<iterator, bool> 527*0a6a1f1dSLionel Sambuc LeftRes = sn_mapL.insert(std::make_pair(Left, sn_mapL.size())), 528*0a6a1f1dSLionel Sambuc RightRes = sn_mapR.insert(std::make_pair(Right, sn_mapR.size())); 529*0a6a1f1dSLionel Sambuc if (LeftRes.first->second == RightRes.first->second) return 0; 530*0a6a1f1dSLionel Sambuc if (LeftRes.first->second < RightRes.first->second) return -1; 531*0a6a1f1dSLionel Sambuc return 1; 532*0a6a1f1dSLionel Sambuc 533*0a6a1f1dSLionel SambucNow when *cmpValues* returns 0, we can proceed comparison procedure. Otherwise, 534*0a6a1f1dSLionel Sambucif we get (-1 or 1), we need to pass this result to the top level, and finish 535*0a6a1f1dSLionel Sambuccomparison procedure. 536*0a6a1f1dSLionel Sambuc 537*0a6a1f1dSLionel SambuccmpConstants 538*0a6a1f1dSLionel Sambuc------------ 539*0a6a1f1dSLionel SambucPerforms constants comparison as follows: 540*0a6a1f1dSLionel Sambuc 541*0a6a1f1dSLionel Sambuc1. Compare constant types using ``cmpType`` method. If result is -1 or 1, goto 542*0a6a1f1dSLionel Sambucstep 2, otherwise proceed to step 3. 543*0a6a1f1dSLionel Sambuc 544*0a6a1f1dSLionel Sambuc2. If types are different, we still can check whether constants could be 545*0a6a1f1dSLionel Sambuclosslessly bitcasted to each other. The further explanation is modification of 546*0a6a1f1dSLionel Sambuc``canLosslesslyBitCastTo`` method. 547*0a6a1f1dSLionel Sambuc 548*0a6a1f1dSLionel Sambuc 2.1 Check whether constants are of the first class types 549*0a6a1f1dSLionel Sambuc (``isFirstClassType`` check): 550*0a6a1f1dSLionel Sambuc 551*0a6a1f1dSLionel Sambuc 2.1.1. If both constants are *not* of the first class type: return result 552*0a6a1f1dSLionel Sambuc of ``cmpType``. 553*0a6a1f1dSLionel Sambuc 554*0a6a1f1dSLionel Sambuc 2.1.2. Otherwise, if left type is not of the first class, return -1. If 555*0a6a1f1dSLionel Sambuc right type is not of the first class, return 1. 556*0a6a1f1dSLionel Sambuc 557*0a6a1f1dSLionel Sambuc 2.1.3. If both types are of the first class type, proceed to the next step 558*0a6a1f1dSLionel Sambuc (2.1.3.1). 559*0a6a1f1dSLionel Sambuc 560*0a6a1f1dSLionel Sambuc 2.1.3.1. If types are vectors, compare their bitwidth using the 561*0a6a1f1dSLionel Sambuc *cmpNumbers*. If result is not 0, return it. 562*0a6a1f1dSLionel Sambuc 563*0a6a1f1dSLionel Sambuc 2.1.3.2. Different types, but not a vectors: 564*0a6a1f1dSLionel Sambuc 565*0a6a1f1dSLionel Sambuc * if both of them are pointers, good for us, we can proceed to step 3. 566*0a6a1f1dSLionel Sambuc * if one of types is pointer, return result of *isPointer* flags 567*0a6a1f1dSLionel Sambuc comparison (*cmpFlags* operation). 568*0a6a1f1dSLionel Sambuc * otherwise we have no methods to prove bitcastability, and thus return 569*0a6a1f1dSLionel Sambuc result of types comparison (-1 or 1). 570*0a6a1f1dSLionel Sambuc 571*0a6a1f1dSLionel SambucSteps below are for the case when types are equal, or case when constants are 572*0a6a1f1dSLionel Sambucbitcastable: 573*0a6a1f1dSLionel Sambuc 574*0a6a1f1dSLionel Sambuc3. One of constants is a "*null*" value. Return the result of 575*0a6a1f1dSLionel Sambuc``cmpFlags(L->isNullValue, R->isNullValue)`` comparison. 576*0a6a1f1dSLionel Sambuc 577*0a6a1f1dSLionel Sambuc4. Compare value IDs, and return result if it is not 0: 578*0a6a1f1dSLionel Sambuc 579*0a6a1f1dSLionel Sambuc.. code-block:: c++ 580*0a6a1f1dSLionel Sambuc 581*0a6a1f1dSLionel Sambuc if (int Res = cmpNumbers(L->getValueID(), R->getValueID())) 582*0a6a1f1dSLionel Sambuc return Res; 583*0a6a1f1dSLionel Sambuc 584*0a6a1f1dSLionel Sambuc5. Compare the contents of constants. The comparison depends on kind of 585*0a6a1f1dSLionel Sambucconstants, but on this stage it is just a lexicographical comparison. Just see 586*0a6a1f1dSLionel Sambuchow it was described in the beginning of "*Functions comparison*" paragraph. 587*0a6a1f1dSLionel SambucMathematically it is equal to the next case: we encode left constant and right 588*0a6a1f1dSLionel Sambucconstant (with similar way *bitcode-writer* does). Then compare left code 589*0a6a1f1dSLionel Sambucsequence and right code sequence. 590*0a6a1f1dSLionel Sambuc 591*0a6a1f1dSLionel Sambuccompare(const BasicBlock*, const BasicBlock*) 592*0a6a1f1dSLionel Sambuc--------------------------------------------- 593*0a6a1f1dSLionel SambucCompares two *BasicBlock* instances. 594*0a6a1f1dSLionel Sambuc 595*0a6a1f1dSLionel SambucIt enumerates instructions from left *BB* and right *BB*. 596*0a6a1f1dSLionel Sambuc 597*0a6a1f1dSLionel Sambuc1. It assigns serial numbers to the left and right instructions, using 598*0a6a1f1dSLionel Sambuc``cmpValues`` method. 599*0a6a1f1dSLionel Sambuc 600*0a6a1f1dSLionel Sambuc2. If one of left or right is *GEP* (``GetElementPtr``), then treat *GEP* as 601*0a6a1f1dSLionel Sambucgreater than other instructions, if both instructions are *GEPs* use ``cmpGEP`` 602*0a6a1f1dSLionel Sambucmethod for comparison. If result is -1 or 1, pass it to the top-level 603*0a6a1f1dSLionel Sambuccomparison (return it). 604*0a6a1f1dSLionel Sambuc 605*0a6a1f1dSLionel Sambuc 3.1. Compare operations. Call ``cmpOperation`` method. If result is -1 or 606*0a6a1f1dSLionel Sambuc 1, return it. 607*0a6a1f1dSLionel Sambuc 608*0a6a1f1dSLionel Sambuc 3.2. Compare number of operands, if result is -1 or 1, return it. 609*0a6a1f1dSLionel Sambuc 610*0a6a1f1dSLionel Sambuc 3.3. Compare operands themselves, use ``cmpValues`` method. Return result 611*0a6a1f1dSLionel Sambuc if it is -1 or 1. 612*0a6a1f1dSLionel Sambuc 613*0a6a1f1dSLionel Sambuc 3.4. Compare type of operands, using ``cmpType`` method. Return result if 614*0a6a1f1dSLionel Sambuc it is -1 or 1. 615*0a6a1f1dSLionel Sambuc 616*0a6a1f1dSLionel Sambuc 3.5. Proceed to the next instruction. 617*0a6a1f1dSLionel Sambuc 618*0a6a1f1dSLionel Sambuc4. We can finish instruction enumeration in 3 cases: 619*0a6a1f1dSLionel Sambuc 620*0a6a1f1dSLionel Sambuc 4.1. We reached the end of both left and right basic-blocks. We didn't 621*0a6a1f1dSLionel Sambuc exit on steps 1-3, so contents is equal, return 0. 622*0a6a1f1dSLionel Sambuc 623*0a6a1f1dSLionel Sambuc 4.2. We have reached the end of the left basic-block. Return -1. 624*0a6a1f1dSLionel Sambuc 625*0a6a1f1dSLionel Sambuc 4.3. Return 1 (the end of the right basic block). 626*0a6a1f1dSLionel Sambuc 627*0a6a1f1dSLionel SambuccmpGEP 628*0a6a1f1dSLionel Sambuc------ 629*0a6a1f1dSLionel SambucCompares two GEPs (``getelementptr`` instructions). 630*0a6a1f1dSLionel Sambuc 631*0a6a1f1dSLionel SambucIt differs from regular operations comparison with the only thing: possibility 632*0a6a1f1dSLionel Sambucto use ``accumulateConstantOffset`` method. 633*0a6a1f1dSLionel Sambuc 634*0a6a1f1dSLionel SambucSo, if we get constant offset for both left and right *GEPs*, then compare it as 635*0a6a1f1dSLionel Sambucnumbers, and return comparison result. 636*0a6a1f1dSLionel Sambuc 637*0a6a1f1dSLionel SambucOtherwise treat it like a regular operation (see previous paragraph). 638*0a6a1f1dSLionel Sambuc 639*0a6a1f1dSLionel SambuccmpOperation 640*0a6a1f1dSLionel Sambuc------------ 641*0a6a1f1dSLionel SambucCompares instruction opcodes and some important operation properties. 642*0a6a1f1dSLionel Sambuc 643*0a6a1f1dSLionel Sambuc1. Compare opcodes, if it differs return the result. 644*0a6a1f1dSLionel Sambuc 645*0a6a1f1dSLionel Sambuc2. Compare number of operands. If it differs – return the result. 646*0a6a1f1dSLionel Sambuc 647*0a6a1f1dSLionel Sambuc3. Compare operation types, use *cmpType*. All the same – if types are 648*0a6a1f1dSLionel Sambucdifferent, return result. 649*0a6a1f1dSLionel Sambuc 650*0a6a1f1dSLionel Sambuc4. Compare *subclassOptionalData*, get it with ``getRawSubclassOptionalData`` 651*0a6a1f1dSLionel Sambucmethod, and compare it like a numbers. 652*0a6a1f1dSLionel Sambuc 653*0a6a1f1dSLionel Sambuc5. Compare operand types. 654*0a6a1f1dSLionel Sambuc 655*0a6a1f1dSLionel Sambuc6. For some particular instructions check equivalence (relation in our case) of 656*0a6a1f1dSLionel Sambucsome significant attributes. For example we have to compare alignment for 657*0a6a1f1dSLionel Sambuc``load`` instructions. 658*0a6a1f1dSLionel Sambuc 659*0a6a1f1dSLionel SambucO(log(N)) 660*0a6a1f1dSLionel Sambuc--------- 661*0a6a1f1dSLionel SambucMethods described above implement order relationship. And latter, could be used 662*0a6a1f1dSLionel Sambucfor nodes comparison in a binary tree. So we can organize functions set into 663*0a6a1f1dSLionel Sambucthe binary tree and reduce the cost of lookup procedure from 664*0a6a1f1dSLionel SambucO(N*N) to O(log(N)). 665*0a6a1f1dSLionel Sambuc 666*0a6a1f1dSLionel SambucMerging process, mergeTwoFunctions 667*0a6a1f1dSLionel Sambuc================================== 668*0a6a1f1dSLionel SambucOnce *MergeFunctions* detected that current function (*G*) is equal to one that 669*0a6a1f1dSLionel Sambucwere analyzed before (function *F*) it calls ``mergeTwoFunctions(Function*, 670*0a6a1f1dSLionel SambucFunction*)``. 671*0a6a1f1dSLionel Sambuc 672*0a6a1f1dSLionel SambucOperation affects ``FnTree`` contents with next way: *F* will stay in 673*0a6a1f1dSLionel Sambuc``FnTree``. *G* being equal to *F* will not be added to ``FnTree``. Calls of 674*0a6a1f1dSLionel Sambuc*G* would be replaced with something else. It changes bodies of callers. So, 675*0a6a1f1dSLionel Sambucfunctions that calls *G* would be put into ``Deferred`` set and removed from 676*0a6a1f1dSLionel Sambuc``FnTree``, and analyzed again. 677*0a6a1f1dSLionel Sambuc 678*0a6a1f1dSLionel SambucThe approach is next: 679*0a6a1f1dSLionel Sambuc 680*0a6a1f1dSLionel Sambuc1. Most wished case: when we can use alias and both of *F* and *G* are weak. We 681*0a6a1f1dSLionel Sambucmake both of them with aliases to the third strong function *H*. Actually *H* 682*0a6a1f1dSLionel Sambucis *F*. See below how it's made (but it's better to look straight into the 683*0a6a1f1dSLionel Sambucsource code). Well, this is a case when we can just replace *G* with *F* 684*0a6a1f1dSLionel Sambuceverywhere, we use ``replaceAllUsesWith`` operation here (*RAUW*). 685*0a6a1f1dSLionel Sambuc 686*0a6a1f1dSLionel Sambuc2. *F* could not be overridden, while *G* could. It would be good to do the 687*0a6a1f1dSLionel Sambucnext: after merging the places where overridable function were used, still use 688*0a6a1f1dSLionel Sambucoverridable stub. So try to make *G* alias to *F*, or create overridable tail 689*0a6a1f1dSLionel Sambuccall wrapper around *F* and replace *G* with that call. 690*0a6a1f1dSLionel Sambuc 691*0a6a1f1dSLionel Sambuc3. Neither *F* nor *G* could be overridden. We can't use *RAUW*. We can just 692*0a6a1f1dSLionel Sambucchange the callers: call *F* instead of *G*. That's what 693*0a6a1f1dSLionel Sambuc``replaceDirectCallers`` does. 694*0a6a1f1dSLionel Sambuc 695*0a6a1f1dSLionel SambucBelow is detailed body description. 696*0a6a1f1dSLionel Sambuc 697*0a6a1f1dSLionel SambucIf “F” may be overridden 698*0a6a1f1dSLionel Sambuc------------------------ 699*0a6a1f1dSLionel SambucAs follows from ``mayBeOverridden`` comments: “whether the definition of this 700*0a6a1f1dSLionel Sambucglobal may be replaced by something non-equivalent at link time”. If so, thats 701*0a6a1f1dSLionel Sambucok: we can use alias to *F* instead of *G* or change call instructions itself. 702*0a6a1f1dSLionel Sambuc 703*0a6a1f1dSLionel SambucHasGlobalAliases, removeUsers 704*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 705*0a6a1f1dSLionel SambucFirst consider the case when we have global aliases of one function name to 706*0a6a1f1dSLionel Sambucanother. Our purpose is make both of them with aliases to the third strong 707*0a6a1f1dSLionel Sambucfunction. Though if we keep *F* alive and without major changes we can leave it 708*0a6a1f1dSLionel Sambucin ``FnTree``. Try to combine these two goals. 709*0a6a1f1dSLionel Sambuc 710*0a6a1f1dSLionel SambucDo stub replacement of *F* itself with an alias to *F*. 711*0a6a1f1dSLionel Sambuc 712*0a6a1f1dSLionel Sambuc1. Create stub function *H*, with the same name and attributes like function 713*0a6a1f1dSLionel Sambuc*F*. It takes maximum alignment of *F* and *G*. 714*0a6a1f1dSLionel Sambuc 715*0a6a1f1dSLionel Sambuc2. Replace all uses of function *F* with uses of function *H*. It is the two 716*0a6a1f1dSLionel Sambucsteps procedure instead. First of all, we must take into account, all functions 717*0a6a1f1dSLionel Sambucfrom whom *F* is called would be changed: since we change the call argument 718*0a6a1f1dSLionel Sambuc(from *F* to *H*). If so we must to review these caller functions again after 719*0a6a1f1dSLionel Sambucthis procedure. We remove callers from ``FnTree``, method with name 720*0a6a1f1dSLionel Sambuc``removeUsers(F)`` does that (don't confuse with ``replaceAllUsesWith``): 721*0a6a1f1dSLionel Sambuc 722*0a6a1f1dSLionel Sambuc 2.1. ``Inside removeUsers(Value* 723*0a6a1f1dSLionel Sambuc V)`` we go through the all values that use value *V* (or *F* in our context). 724*0a6a1f1dSLionel Sambuc If value is instruction, we go to function that holds this instruction and 725*0a6a1f1dSLionel Sambuc mark it as to-be-analyzed-again (put to ``Deferred`` set), we also remove 726*0a6a1f1dSLionel Sambuc caller from ``FnTree``. 727*0a6a1f1dSLionel Sambuc 728*0a6a1f1dSLionel Sambuc 2.2. Now we can do the replacement: call ``F->replaceAllUsesWith(H)``. 729*0a6a1f1dSLionel Sambuc 730*0a6a1f1dSLionel Sambuc3. *H* (that now "officially" plays *F*'s role) is replaced with alias to *F*. 731*0a6a1f1dSLionel SambucDo the same with *G*: replace it with alias to *F*. So finally everywhere *F* 732*0a6a1f1dSLionel Sambucwas used, we use *H* and it is alias to *F*, and everywhere *G* was used we 733*0a6a1f1dSLionel Sambucalso have alias to *F*. 734*0a6a1f1dSLionel Sambuc 735*0a6a1f1dSLionel Sambuc4. Set *F* linkage to private. Make it strong :-) 736*0a6a1f1dSLionel Sambuc 737*0a6a1f1dSLionel SambucNo global aliases, replaceDirectCallers 738*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 739*0a6a1f1dSLionel SambucIf global aliases are not supported. We call ``replaceDirectCallers`` then. Just 740*0a6a1f1dSLionel Sambucgo through all calls of *G* and replace it with calls of *F*. If you look into 741*0a6a1f1dSLionel Sambucmethod you will see that it scans all uses of *G* too, and if use is callee (if 742*0a6a1f1dSLionel Sambucuser is call instruction and *G* is used as what to be called), we replace it 743*0a6a1f1dSLionel Sambucwith use of *F*. 744*0a6a1f1dSLionel Sambuc 745*0a6a1f1dSLionel SambucIf “F” could not be overridden, fix it! 746*0a6a1f1dSLionel Sambuc""""""""""""""""""""""""""""""""""""""" 747*0a6a1f1dSLionel Sambuc 748*0a6a1f1dSLionel SambucWe call ``writeThunkOrAlias(Function *F, Function *G)``. Here we try to replace 749*0a6a1f1dSLionel Sambuc*G* with alias to *F* first. Next conditions are essential: 750*0a6a1f1dSLionel Sambuc 751*0a6a1f1dSLionel Sambuc* target should support global aliases, 752*0a6a1f1dSLionel Sambuc* the address itself of *G* should be not significant, not named and not 753*0a6a1f1dSLionel Sambuc referenced anywhere, 754*0a6a1f1dSLionel Sambuc* function should come with external, local or weak linkage. 755*0a6a1f1dSLionel Sambuc 756*0a6a1f1dSLionel SambucOtherwise we write thunk: some wrapper that has *G's* interface and calls *F*, 757*0a6a1f1dSLionel Sambucso *G* could be replaced with this wrapper. 758*0a6a1f1dSLionel Sambuc 759*0a6a1f1dSLionel Sambuc*writeAlias* 760*0a6a1f1dSLionel Sambuc 761*0a6a1f1dSLionel SambucAs follows from *llvm* reference: 762*0a6a1f1dSLionel Sambuc 763*0a6a1f1dSLionel Sambuc“Aliases act as *second name* for the aliasee value”. So we just want to create 764*0a6a1f1dSLionel Sambucsecond name for *F* and use it instead of *G*: 765*0a6a1f1dSLionel Sambuc 766*0a6a1f1dSLionel Sambuc1. create global alias itself (*GA*), 767*0a6a1f1dSLionel Sambuc 768*0a6a1f1dSLionel Sambuc2. adjust alignment of *F* so it must be maximum of current and *G's* alignment; 769*0a6a1f1dSLionel Sambuc 770*0a6a1f1dSLionel Sambuc3. replace uses of *G*: 771*0a6a1f1dSLionel Sambuc 772*0a6a1f1dSLionel Sambuc 3.1. first mark all callers of *G* as to-be-analyzed-again, using 773*0a6a1f1dSLionel Sambuc ``removeUsers`` method (see chapter above), 774*0a6a1f1dSLionel Sambuc 775*0a6a1f1dSLionel Sambuc 3.2. call ``G->replaceAllUsesWith(GA)``. 776*0a6a1f1dSLionel Sambuc 777*0a6a1f1dSLionel Sambuc4. Get rid of *G*. 778*0a6a1f1dSLionel Sambuc 779*0a6a1f1dSLionel Sambuc*writeThunk* 780*0a6a1f1dSLionel Sambuc 781*0a6a1f1dSLionel SambucAs it written in method comments: 782*0a6a1f1dSLionel Sambuc 783*0a6a1f1dSLionel Sambuc“Replace G with a simple tail call to bitcast(F). Also replace direct uses of G 784*0a6a1f1dSLionel Sambucwith bitcast(F). Deletes G.” 785*0a6a1f1dSLionel Sambuc 786*0a6a1f1dSLionel SambucIn general it does the same as usual when we want to replace callee, except the 787*0a6a1f1dSLionel Sambucfirst point: 788*0a6a1f1dSLionel Sambuc 789*0a6a1f1dSLionel Sambuc1. We generate tail call wrapper around *F*, but with interface that allows use 790*0a6a1f1dSLionel Sambucit instead of *G*. 791*0a6a1f1dSLionel Sambuc 792*0a6a1f1dSLionel Sambuc2. “As-usual”: ``removeUsers`` and ``replaceAllUsesWith`` then. 793*0a6a1f1dSLionel Sambuc 794*0a6a1f1dSLionel Sambuc3. Get rid of *G*. 795*0a6a1f1dSLionel Sambuc 796*0a6a1f1dSLionel SambucThat's it. 797*0a6a1f1dSLionel Sambuc========== 798*0a6a1f1dSLionel SambucWe have described how to detect equal functions, and how to merge them, and in 799*0a6a1f1dSLionel Sambucfirst chapter we have described how it works all-together. Author hopes, reader 800*0a6a1f1dSLionel Sambuchave some picture from now, and it helps him improve and debug this pass. 801*0a6a1f1dSLionel Sambuc 802*0a6a1f1dSLionel SambucReader is welcomed to send us any questions and proposals ;-) 803