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