1*9880d681SAndroid Build Coastguard Worker=============================== 2*9880d681SAndroid Build Coastguard WorkerHow To Use Instruction Mappings 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 Worker 11*9880d681SAndroid Build Coastguard WorkerThis document contains information about adding instruction mapping support 12*9880d681SAndroid Build Coastguard Workerfor a target. The motivation behind this feature comes from the need to switch 13*9880d681SAndroid Build Coastguard Workerbetween different instruction formats during various optimizations. One approach 14*9880d681SAndroid Build Coastguard Workercould be to use switch cases which list all the instructions along with formats 15*9880d681SAndroid Build Coastguard Workerthey can transition to. However, it has large maintenance overhead 16*9880d681SAndroid Build Coastguard Workerbecause of the hardcoded instruction names. Also, whenever a new instruction is 17*9880d681SAndroid Build Coastguard Workeradded in the .td files, all the relevant switch cases should be modified 18*9880d681SAndroid Build Coastguard Workeraccordingly. Instead, the same functionality could be achieved with TableGen and 19*9880d681SAndroid Build Coastguard Workersome support from the .td files for a fraction of maintenance cost. 20*9880d681SAndroid Build Coastguard Worker 21*9880d681SAndroid Build Coastguard Worker``InstrMapping`` Class Overview 22*9880d681SAndroid Build Coastguard Worker=============================== 23*9880d681SAndroid Build Coastguard Worker 24*9880d681SAndroid Build Coastguard WorkerTableGen uses relationship models to map instructions with each other. These 25*9880d681SAndroid Build Coastguard Workermodels are described using ``InstrMapping`` class as a base. Each model sets 26*9880d681SAndroid Build Coastguard Workervarious fields of the ``InstrMapping`` class such that they can uniquely 27*9880d681SAndroid Build Coastguard Workerdescribe all the instructions using that model. TableGen parses all the relation 28*9880d681SAndroid Build Coastguard Workermodels and uses the information to construct relation tables which relate 29*9880d681SAndroid Build Coastguard Workerinstructions with each other. These tables are emitted in the 30*9880d681SAndroid Build Coastguard Worker``XXXInstrInfo.inc`` file along with the functions to query them. Following 31*9880d681SAndroid Build Coastguard Workeris the definition of ``InstrMapping`` class definied in Target.td file: 32*9880d681SAndroid Build Coastguard Worker 33*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm 34*9880d681SAndroid Build Coastguard Worker 35*9880d681SAndroid Build Coastguard Worker class InstrMapping { 36*9880d681SAndroid Build Coastguard Worker // Used to reduce search space only to the instructions using this 37*9880d681SAndroid Build Coastguard Worker // relation model. 38*9880d681SAndroid Build Coastguard Worker string FilterClass; 39*9880d681SAndroid Build Coastguard Worker 40*9880d681SAndroid Build Coastguard Worker // List of fields/attributes that should be same for all the instructions in 41*9880d681SAndroid Build Coastguard Worker // a row of the relation table. Think of this as a set of properties shared 42*9880d681SAndroid Build Coastguard Worker // by all the instructions related by this relationship. 43*9880d681SAndroid Build Coastguard Worker list<string> RowFields = []; 44*9880d681SAndroid Build Coastguard Worker 45*9880d681SAndroid Build Coastguard Worker // List of fields/attributes that are same for all the instructions 46*9880d681SAndroid Build Coastguard Worker // in a column of the relation table. 47*9880d681SAndroid Build Coastguard Worker list<string> ColFields = []; 48*9880d681SAndroid Build Coastguard Worker 49*9880d681SAndroid Build Coastguard Worker // Values for the fields/attributes listed in 'ColFields' corresponding to 50*9880d681SAndroid Build Coastguard Worker // the key instruction. This is the instruction that will be transformed 51*9880d681SAndroid Build Coastguard Worker // using this relation model. 52*9880d681SAndroid Build Coastguard Worker list<string> KeyCol = []; 53*9880d681SAndroid Build Coastguard Worker 54*9880d681SAndroid Build Coastguard Worker // List of values for the fields/attributes listed in 'ColFields', one for 55*9880d681SAndroid Build Coastguard Worker // each column in the relation table. These are the instructions a key 56*9880d681SAndroid Build Coastguard Worker // instruction will be transformed into. 57*9880d681SAndroid Build Coastguard Worker list<list<string> > ValueCols = []; 58*9880d681SAndroid Build Coastguard Worker } 59*9880d681SAndroid Build Coastguard Worker 60*9880d681SAndroid Build Coastguard WorkerSample Example 61*9880d681SAndroid Build Coastguard Worker-------------- 62*9880d681SAndroid Build Coastguard Worker 63*9880d681SAndroid Build Coastguard WorkerLet's say that we want to have a function 64*9880d681SAndroid Build Coastguard Worker``int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)`` which 65*9880d681SAndroid Build Coastguard Workertakes a non-predicated instruction and returns its predicated true or false form 66*9880d681SAndroid Build Coastguard Workerdepending on some input flag, ``inPredSense``. The first step in the process is 67*9880d681SAndroid Build Coastguard Workerto define a relationship model that relates predicated instructions to their 68*9880d681SAndroid Build Coastguard Workernon-predicated form by assigning appropriate values to the ``InstrMapping`` 69*9880d681SAndroid Build Coastguard Workerfields. For this relationship, non-predicated instructions are treated as key 70*9880d681SAndroid Build Coastguard Workerinstruction since they are the one used to query the interface function. 71*9880d681SAndroid Build Coastguard Worker 72*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm 73*9880d681SAndroid Build Coastguard Worker 74*9880d681SAndroid Build Coastguard Worker def getPredOpcode : InstrMapping { 75*9880d681SAndroid Build Coastguard Worker // Choose a FilterClass that is used as a base class for all the 76*9880d681SAndroid Build Coastguard Worker // instructions modeling this relationship. This is done to reduce the 77*9880d681SAndroid Build Coastguard Worker // search space only to these set of instructions. 78*9880d681SAndroid Build Coastguard Worker let FilterClass = "PredRel"; 79*9880d681SAndroid Build Coastguard Worker 80*9880d681SAndroid Build Coastguard Worker // Instructions with same values for all the fields in RowFields form a 81*9880d681SAndroid Build Coastguard Worker // row in the resulting relation table. 82*9880d681SAndroid Build Coastguard Worker // For example, if we want to relate 'ADD' (non-predicated) with 'Add_pt' 83*9880d681SAndroid Build Coastguard Worker // (predicated true) and 'Add_pf' (predicated false), then all 3 84*9880d681SAndroid Build Coastguard Worker // instructions need to have same value for BaseOpcode field. It can be any 85*9880d681SAndroid Build Coastguard Worker // unique value (Ex: XYZ) and should not be shared with any other 86*9880d681SAndroid Build Coastguard Worker // instruction not related to 'add'. 87*9880d681SAndroid Build Coastguard Worker let RowFields = ["BaseOpcode"]; 88*9880d681SAndroid Build Coastguard Worker 89*9880d681SAndroid Build Coastguard Worker // List of attributes that can be used to define key and column instructions 90*9880d681SAndroid Build Coastguard Worker // for a relation. Key instruction is passed as an argument 91*9880d681SAndroid Build Coastguard Worker // to the function used for querying relation tables. Column instructions 92*9880d681SAndroid Build Coastguard Worker // are the instructions they (key) can transform into. 93*9880d681SAndroid Build Coastguard Worker // 94*9880d681SAndroid Build Coastguard Worker // Here, we choose 'PredSense' as ColFields since this is the unique 95*9880d681SAndroid Build Coastguard Worker // attribute of the key (non-predicated) and column (true/false) 96*9880d681SAndroid Build Coastguard Worker // instructions involved in this relationship model. 97*9880d681SAndroid Build Coastguard Worker let ColFields = ["PredSense"]; 98*9880d681SAndroid Build Coastguard Worker 99*9880d681SAndroid Build Coastguard Worker // The key column contains non-predicated instructions. 100*9880d681SAndroid Build Coastguard Worker let KeyCol = ["none"]; 101*9880d681SAndroid Build Coastguard Worker 102*9880d681SAndroid Build Coastguard Worker // Two value columns - first column contains instructions with 103*9880d681SAndroid Build Coastguard Worker // PredSense=true while second column has instructions with PredSense=false. 104*9880d681SAndroid Build Coastguard Worker let ValueCols = [["true"], ["false"]]; 105*9880d681SAndroid Build Coastguard Worker } 106*9880d681SAndroid Build Coastguard Worker 107*9880d681SAndroid Build Coastguard WorkerTableGen uses the above relationship model to emit relation table that maps 108*9880d681SAndroid Build Coastguard Workernon-predicated instructions with their predicated forms. It also outputs the 109*9880d681SAndroid Build Coastguard Workerinterface function 110*9880d681SAndroid Build Coastguard Worker``int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)`` to query 111*9880d681SAndroid Build Coastguard Workerthe table. Here, Function ``getPredOpcode`` takes two arguments, opcode of the 112*9880d681SAndroid Build Coastguard Workercurrent instruction and PredSense of the desired instruction, and returns 113*9880d681SAndroid Build Coastguard Workerpredicated form of the instruction, if found in the relation table. 114*9880d681SAndroid Build Coastguard WorkerIn order for an instruction to be added into the relation table, it needs 115*9880d681SAndroid Build Coastguard Workerto include relevant information in its definition. For example, consider 116*9880d681SAndroid Build Coastguard Workerfollowing to be the current definitions of ADD, ADD_pt (true) and ADD_pf (false) 117*9880d681SAndroid Build Coastguard Workerinstructions: 118*9880d681SAndroid Build Coastguard Worker 119*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm 120*9880d681SAndroid Build Coastguard Worker 121*9880d681SAndroid Build Coastguard Worker def ADD : ALU32_rr<(outs IntRegs:$dst), (ins IntRegs:$a, IntRegs:$b), 122*9880d681SAndroid Build Coastguard Worker "$dst = add($a, $b)", 123*9880d681SAndroid Build Coastguard Worker [(set (i32 IntRegs:$dst), (add (i32 IntRegs:$a), 124*9880d681SAndroid Build Coastguard Worker (i32 IntRegs:$b)))]>; 125*9880d681SAndroid Build Coastguard Worker 126*9880d681SAndroid Build Coastguard Worker def ADD_Pt : ALU32_rr<(outs IntRegs:$dst), 127*9880d681SAndroid Build Coastguard Worker (ins PredRegs:$p, IntRegs:$a, IntRegs:$b), 128*9880d681SAndroid Build Coastguard Worker "if ($p) $dst = add($a, $b)", 129*9880d681SAndroid Build Coastguard Worker []>; 130*9880d681SAndroid Build Coastguard Worker 131*9880d681SAndroid Build Coastguard Worker def ADD_Pf : ALU32_rr<(outs IntRegs:$dst), 132*9880d681SAndroid Build Coastguard Worker (ins PredRegs:$p, IntRegs:$a, IntRegs:$b), 133*9880d681SAndroid Build Coastguard Worker "if (!$p) $dst = add($a, $b)", 134*9880d681SAndroid Build Coastguard Worker []>; 135*9880d681SAndroid Build Coastguard Worker 136*9880d681SAndroid Build Coastguard WorkerIn this step, we modify these instructions to include the information 137*9880d681SAndroid Build Coastguard Workerrequired by the relationship model, <tt>getPredOpcode</tt>, so that they can 138*9880d681SAndroid Build Coastguard Workerbe related. 139*9880d681SAndroid Build Coastguard Worker 140*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm 141*9880d681SAndroid Build Coastguard Worker 142*9880d681SAndroid Build Coastguard Worker def ADD : PredRel, ALU32_rr<(outs IntRegs:$dst), (ins IntRegs:$a, IntRegs:$b), 143*9880d681SAndroid Build Coastguard Worker "$dst = add($a, $b)", 144*9880d681SAndroid Build Coastguard Worker [(set (i32 IntRegs:$dst), (add (i32 IntRegs:$a), 145*9880d681SAndroid Build Coastguard Worker (i32 IntRegs:$b)))]> { 146*9880d681SAndroid Build Coastguard Worker let BaseOpcode = "ADD"; 147*9880d681SAndroid Build Coastguard Worker let PredSense = "none"; 148*9880d681SAndroid Build Coastguard Worker } 149*9880d681SAndroid Build Coastguard Worker 150*9880d681SAndroid Build Coastguard Worker def ADD_Pt : PredRel, ALU32_rr<(outs IntRegs:$dst), 151*9880d681SAndroid Build Coastguard Worker (ins PredRegs:$p, IntRegs:$a, IntRegs:$b), 152*9880d681SAndroid Build Coastguard Worker "if ($p) $dst = add($a, $b)", 153*9880d681SAndroid Build Coastguard Worker []> { 154*9880d681SAndroid Build Coastguard Worker let BaseOpcode = "ADD"; 155*9880d681SAndroid Build Coastguard Worker let PredSense = "true"; 156*9880d681SAndroid Build Coastguard Worker } 157*9880d681SAndroid Build Coastguard Worker 158*9880d681SAndroid Build Coastguard Worker def ADD_Pf : PredRel, ALU32_rr<(outs IntRegs:$dst), 159*9880d681SAndroid Build Coastguard Worker (ins PredRegs:$p, IntRegs:$a, IntRegs:$b), 160*9880d681SAndroid Build Coastguard Worker "if (!$p) $dst = add($a, $b)", 161*9880d681SAndroid Build Coastguard Worker []> { 162*9880d681SAndroid Build Coastguard Worker let BaseOpcode = "ADD"; 163*9880d681SAndroid Build Coastguard Worker let PredSense = "false"; 164*9880d681SAndroid Build Coastguard Worker } 165*9880d681SAndroid Build Coastguard Worker 166*9880d681SAndroid Build Coastguard WorkerPlease note that all the above instructions use ``PredRel`` as a base class. 167*9880d681SAndroid Build Coastguard WorkerThis is extremely important since TableGen uses it as a filter for selecting 168*9880d681SAndroid Build Coastguard Workerinstructions for ``getPredOpcode`` model. Any instruction not derived from 169*9880d681SAndroid Build Coastguard Worker``PredRel`` is excluded from the analysis. ``BaseOpcode`` is another important 170*9880d681SAndroid Build Coastguard Workerfield. Since it's selected as a ``RowFields`` of the model, it is required 171*9880d681SAndroid Build Coastguard Workerto have the same value for all 3 instructions in order to be related. Next, 172*9880d681SAndroid Build Coastguard Worker``PredSense`` is used to determine their column positions by comparing its value 173*9880d681SAndroid Build Coastguard Workerwith ``KeyCol`` and ``ValueCols``. If an instruction sets its ``PredSense`` 174*9880d681SAndroid Build Coastguard Workervalue to something not used in the relation model, it will not be assigned 175*9880d681SAndroid Build Coastguard Workera column in the relation table. 176