]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/Mips/Relocation.txt
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / Mips / Relocation.txt
1 MIPS Relocation Principles
2
3 In LLVM, there are several elements of the llvm::ISD::NodeType enum
4 that deal with addresses and/or relocations. These are defined in
5 include/llvm/Target/TargetSelectionDAG.td, namely:
6     GlobalAddress, GlobalTLSAddress, JumpTable, ConstantPool,
7     ExternalSymbol, BlockAddress
8 The MIPS backend uses several principles to handle these.
9
10 1. Code for lowering addresses references to machine dependent code is
11 factored into common code for generating different address forms and
12 is called by the relocation model specific lowering function, using
13 templated functions. For example:
14
15   // lib/Target/Mips/MipsISelLowering.cpp
16   SDValue MipsTargetLowering::
17   lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
18
19 calls
20
21   template <class NodeTy> // lib/Target/Mips/MipsISelLowering.h
22   SDValue getAddrLocal(NodeTy *N, const SDLoc &DL, EVT Ty,
23                        SelectionDAG &DAG, bool IsN32OrN64) const
24
25 which calls the overloaded function:
26
27   // lib/Target/Mips/MipsISelLowering.h
28   SDValue getTargetNode(JumpTableSDNode *N, EVT Ty, SelectionDAG &DAG,
29                         unsigned Flag) const;
30
31 2. Generic address nodes are lowered to some combination of target
32 independent and machine specific SDNodes (for example:
33 MipsISD::{Highest, Higher, Hi, Lo}) depending upon relocation model,
34 ABI, and compilation options.
35
36 The choice of specific instructions that are to be used is delegated
37 to ISel which in turn relies on TableGen patterns to choose subtarget
38 specific instructions. For example, in getAddrLocal, the pseudo-code
39 generated is:
40
41   (add (load (wrapper $gp, %got(sym)), %lo(sym))
42
43 where "%lo" represents an instance of an SDNode with opcode
44 "MipsISD::Lo", "wrapper" indicates one with opcode "MipsISD::Wrapper",
45 and "%got" the global table pointer "getGlobalReg(...)". The "add" is
46 "ISD::ADD", not a target dependent one.
47
48 3. A TableGen multiclass pattern "MipsHiLoRelocs" is used to define a
49 template pattern parameterized over the load upper immediate
50 instruction, add operation, the zero register, and register class.
51 Here the instantiation of MipsHiLoRelocs in MipsInstrInfo.td is used
52 to MIPS32 to compute addresses for the static relocation model.
53
54   // lib/Target/Mips/MipsInstrInfo.td
55   multiclass MipsHiLoRelocs<Instruction Lui, Instruction Addiu,
56                             Register ZeroReg, RegisterOperand GPROpnd> {
57     def : MipsPat<(MipsHi tglobaladdr:$in), (Lui tglobaladdr:$in)>;
58     ...
59     def : MipsPat<(MipsLo tglobaladdr:$in), (Addiu ZeroReg, tglobaladdr:$in)>;
60     ...
61     def : MipsPat<(add GPROpnd:$hi, (MipsLo tglobaladdr:$lo)),
62                 (Addiu GPROpnd:$hi, tglobaladdr:$lo)>;
63     ...
64   }
65   defm : MipsHiLoRelocs<LUi, ADDiu, ZERO, GPR32Opnd>;
66
67   // lib/Target/Mips/Mips64InstrInfo.td
68   defm : MipsHiLoRelocs<LUi64, DADDiu, ZERO_64, GPR64Opnd>, SYM_32;
69
70 The instantiation in Mips64InstrInfo.td is used for MIPS64 in ILP32
71 mode, as guarded by the predicate "SYM_32" and also for a submode of
72 LP64 where symbols are assumed to be 32 bits wide. A similar
73 multiclass for MIPS64 in LP64 mode is also defined:
74
75   // lib/Target/Mips/Mips64InstrInfo.td
76   multiclass MipsHighestHigherHiLoRelocs<Instruction Lui,
77                                          Instruction Daddiu> {
78   ...
79     def : MipsPat<(MipsHighest (i64 tglobaladdr:$in)),
80                   (Lui tglobaladdr:$in)>;
81   ...
82     def : MipsPat<(MipsHigher (i64 tglobaladdr:$in)),
83                   (Daddiu ZERO_64, tglobaladdr:$in)>;
84   ...
85     def : MipsPat<(add GPR64:$hi, (MipsHigher (i64 tglobaladdr:$lo))),
86                   (Daddiu GPR64:$hi, tglobaladdr:$lo)>;
87   ...
88     def : MipsPat<(add GPR64:$hi, (MipsHi (i64 tglobaladdr:$lo))),
89                   (Daddiu GPR64:$hi, tglobaladdr:$lo)>;
90   ...
91     def : MipsPat<(add GPR64:$hi, (MipsLo (i64 tglobaladdr:$lo))),
92                   (Daddiu GPR64:$hi, tglobaladdr:$lo)>;
93   }
94
95 and it is instantiated twice:
96
97   // lib/Target/Mips/Mips64InstrInfo.td
98   defm : MipsHighestHigherHiLoRelocs<LUi64, DADDiu>, SYM_64;
99   // lib/Target/Mips/MicroMips64r6InstrInfo.td
100   defm : MipsHighestHigherHiLoRelocs<LUi64, DADDIU_MM64R6>, SYM_64,
101                                      ISA_MICROMIPS64R6;
102
103 These patterns are used during instruction selection to match
104 MipsISD::{Highest, Higher, Hi, Lo} to a specific machine instruction
105 and operands.
106
107 More details on how multiclasses in TableGen work can be found in the
108 section "Multiclass definitions and instances" in the document
109 "TableGen Language Introduction"
110
111 4. Instruction definitions are multiply defined to cover the different
112 register classes. In some cases, such as LW/LW64, this also accounts
113 for the difference in the results of instruction execution. On MIPS32,
114 "lw" loads a 32 bit value from memory. On MIPS64, "lw" loads a 32 bit
115 value from memory and sign extends the value to 64 bits.
116
117   // lib/Target/Mips/MipsInstrInfo.td
118   def LUi   : MMRel, LoadUpper<"lui", GPR32Opnd, uimm16_relaxed>, LUI_FM;
119   // lib/Target/Mips/Mips64InstrInfo.td
120   def LUi64   : LoadUpper<"lui", GPR64Opnd, uimm16_64_relaxed>, LUI_FM;
121
122 defines two names "LUi" and "LUi64" with two different register
123 classes, but with the same encoding---"LUI_FM". These instructions load a
124 16-bit immediate into bits 31-16 and clear the lower 15 bits. On MIPS64,
125 the result is sign-extended to 64 bits.