1 //===---------- AArch64CollectLOH.cpp - AArch64 collect LOH pass --*- C++ -*-=//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains a pass that collect the Linker Optimization Hint (LOH).
10 // This pass should be run at the very end of the compilation flow, just before
12 // To be useful for the linker, the LOH must be printed into the assembly file.
14 // A LOH describes a sequence of instructions that may be optimized by the
16 // This same sequence cannot be optimized by the compiler because some of
17 // the information will be known at link time.
18 // For instance, consider the following sequence:
19 // L1: adrp xA, sym@PAGE
20 // L2: add xB, xA, sym@PAGEOFF
21 // L3: ldr xC, [xB, #imm]
22 // This sequence can be turned into:
23 // A literal load if sym@PAGE + sym@PAGEOFF + #imm - address(L3) is < 1MB:
24 // L3: ldr xC, sym+#imm
25 // It may also be turned into either the following more efficient
27 // - If sym@PAGEOFF + #imm fits the encoding space of L3.
28 // L1: adrp xA, sym@PAGE
29 // L3: ldr xC, [xB, sym@PAGEOFF + #imm]
30 // - If sym@PAGE + sym@PAGEOFF - address(L1) < 1MB:
32 // L3: ldr xC, [xB, #imm]
34 // To be valid a LOH must meet all the requirements needed by all the related
35 // possible linker transformations.
36 // For instance, using the running example, the constraints to emit
37 // ".loh AdrpAddLdr" are:
38 // - L1, L2, and L3 instructions are of the expected type, i.e.,
39 // respectively ADRP, ADD (immediate), and LD.
40 // - The result of L1 is used only by L2.
41 // - The register argument (xA) used in the ADD instruction is defined
43 // - The result of L2 is used only by L3.
44 // - The base address (xB) in L3 is defined only L2.
45 // - The ADRP in L1 and the ADD in L2 must reference the same symbol using
46 // @PAGE/@PAGEOFF with no additional constants
48 // Currently supported LOHs are:
49 // * So called non-ADRP-related:
50 // - .loh AdrpAddLdr L1, L2, L3:
51 // L1: adrp xA, sym@PAGE
52 // L2: add xB, xA, sym@PAGEOFF
53 // L3: ldr xC, [xB, #imm]
54 // - .loh AdrpLdrGotLdr L1, L2, L3:
55 // L1: adrp xA, sym@GOTPAGE
56 // L2: ldr xB, [xA, sym@GOTPAGEOFF]
57 // L3: ldr xC, [xB, #imm]
58 // - .loh AdrpLdr L1, L3:
59 // L1: adrp xA, sym@PAGE
60 // L3: ldr xC, [xA, sym@PAGEOFF]
61 // - .loh AdrpAddStr L1, L2, L3:
62 // L1: adrp xA, sym@PAGE
63 // L2: add xB, xA, sym@PAGEOFF
64 // L3: str xC, [xB, #imm]
65 // - .loh AdrpLdrGotStr L1, L2, L3:
66 // L1: adrp xA, sym@GOTPAGE
67 // L2: ldr xB, [xA, sym@GOTPAGEOFF]
68 // L3: str xC, [xB, #imm]
69 // - .loh AdrpAdd L1, L2:
70 // L1: adrp xA, sym@PAGE
71 // L2: add xB, xA, sym@PAGEOFF
72 // For all these LOHs, L1, L2, L3 form a simple chain:
73 // L1 result is used only by L2 and L2 result by L3.
74 // L3 LOH-related argument is defined only by L2 and L2 LOH-related argument
76 // All these LOHs aim at using more efficient load/store patterns by folding
77 // some instructions used to compute the address directly into the load/store.
79 // * So called ADRP-related:
80 // - .loh AdrpAdrp L2, L1:
81 // L2: ADRP xA, sym1@PAGE
82 // L1: ADRP xA, sym2@PAGE
83 // L2 dominates L1 and xA is not redifined between L2 and L1
84 // This LOH aims at getting rid of redundant ADRP instructions.
86 // The overall design for emitting the LOHs is:
87 // 1. AArch64CollectLOH (this pass) records the LOHs in the AArch64FunctionInfo.
88 // 2. AArch64AsmPrinter reads the LOHs from AArch64FunctionInfo and it:
89 // 1. Associates them a label.
90 // 2. Emits them in a MCStreamer (EmitLOHDirective).
91 // - The MCMachOStreamer records them into the MCAssembler.
92 // - The MCAsmStreamer prints them.
93 // - Other MCStreamers ignore them.
94 // 3. Closes the MCStreamer:
95 // - The MachObjectWriter gets them from the MCAssembler and writes
96 // them in the object file.
97 // - Other ObjectWriters ignore them.
98 //===----------------------------------------------------------------------===//
101 #include "AArch64InstrInfo.h"
102 #include "AArch64MachineFunctionInfo.h"
103 #include "llvm/ADT/BitVector.h"
104 #include "llvm/ADT/DenseMap.h"
105 #include "llvm/ADT/MapVector.h"
106 #include "llvm/ADT/SmallVector.h"
107 #include "llvm/ADT/Statistic.h"
108 #include "llvm/CodeGen/MachineBasicBlock.h"
109 #include "llvm/CodeGen/MachineFunctionPass.h"
110 #include "llvm/CodeGen/MachineInstr.h"
111 #include "llvm/CodeGen/TargetRegisterInfo.h"
112 #include "llvm/Support/Debug.h"
113 #include "llvm/Support/ErrorHandling.h"
114 #include "llvm/Support/raw_ostream.h"
115 #include "llvm/Target/TargetMachine.h"
116 using namespace llvm
;
118 #define DEBUG_TYPE "aarch64-collect-loh"
120 STATISTIC(NumADRPSimpleCandidate
,
121 "Number of simplifiable ADRP dominate by another");
122 STATISTIC(NumADDToSTR
, "Number of simplifiable STR reachable by ADD");
123 STATISTIC(NumLDRToSTR
, "Number of simplifiable STR reachable by LDR");
124 STATISTIC(NumADDToLDR
, "Number of simplifiable LDR reachable by ADD");
125 STATISTIC(NumLDRToLDR
, "Number of simplifiable LDR reachable by LDR");
126 STATISTIC(NumADRPToLDR
, "Number of simplifiable LDR reachable by ADRP");
127 STATISTIC(NumADRSimpleCandidate
, "Number of simplifiable ADRP + ADD");
129 #define AARCH64_COLLECT_LOH_NAME "AArch64 Collect Linker Optimization Hint (LOH)"
133 struct AArch64CollectLOH
: public MachineFunctionPass
{
135 AArch64CollectLOH() : MachineFunctionPass(ID
) {}
137 bool runOnMachineFunction(MachineFunction
&MF
) override
;
139 MachineFunctionProperties
getRequiredProperties() const override
{
140 return MachineFunctionProperties().set(
141 MachineFunctionProperties::Property::NoVRegs
);
144 StringRef
getPassName() const override
{ return AARCH64_COLLECT_LOH_NAME
; }
146 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
147 MachineFunctionPass::getAnalysisUsage(AU
);
148 AU
.setPreservesAll();
152 char AArch64CollectLOH::ID
= 0;
154 } // end anonymous namespace.
156 INITIALIZE_PASS(AArch64CollectLOH
, "aarch64-collect-loh",
157 AARCH64_COLLECT_LOH_NAME
, false, false)
159 static bool canAddBePartOfLOH(const MachineInstr
&MI
) {
160 // Check immediate to see if the immediate is an address.
161 switch (MI
.getOperand(2).getType()) {
164 case MachineOperand::MO_GlobalAddress
:
165 case MachineOperand::MO_JumpTableIndex
:
166 case MachineOperand::MO_ConstantPoolIndex
:
167 case MachineOperand::MO_BlockAddress
:
172 /// Answer the following question: Can Def be one of the definition
173 /// involved in a part of a LOH?
174 static bool canDefBePartOfLOH(const MachineInstr
&MI
) {
175 // Accept ADRP, ADDLow and LOADGot.
176 switch (MI
.getOpcode()) {
181 case AArch64::ADDXri
:
182 return canAddBePartOfLOH(MI
);
183 case AArch64::LDRXui
:
184 // Check immediate to see if the immediate is an address.
185 switch (MI
.getOperand(2).getType()) {
188 case MachineOperand::MO_GlobalAddress
:
189 return MI
.getOperand(2).getTargetFlags() & AArch64II::MO_GOT
;
194 /// Check whether the given instruction can the end of a LOH chain involving a
196 static bool isCandidateStore(const MachineInstr
&MI
, const MachineOperand
&MO
) {
197 switch (MI
.getOpcode()) {
200 case AArch64::STRBBui
:
201 case AArch64::STRHHui
:
202 case AArch64::STRBui
:
203 case AArch64::STRHui
:
204 case AArch64::STRWui
:
205 case AArch64::STRXui
:
206 case AArch64::STRSui
:
207 case AArch64::STRDui
:
208 case AArch64::STRQui
:
209 // We can only optimize the index operand.
210 // In case we have str xA, [xA, #imm], this is two different uses
211 // of xA and we cannot fold, otherwise the xA stored may be wrong,
212 // even if #imm == 0.
213 return MI
.getOperandNo(&MO
) == 1 &&
214 MI
.getOperand(0).getReg() != MI
.getOperand(1).getReg();
218 /// Check whether the given instruction can be the end of a LOH chain
219 /// involving a load.
220 static bool isCandidateLoad(const MachineInstr
&MI
) {
221 switch (MI
.getOpcode()) {
224 case AArch64::LDRSBWui
:
225 case AArch64::LDRSBXui
:
226 case AArch64::LDRSHWui
:
227 case AArch64::LDRSHXui
:
228 case AArch64::LDRSWui
:
229 case AArch64::LDRBui
:
230 case AArch64::LDRHui
:
231 case AArch64::LDRWui
:
232 case AArch64::LDRXui
:
233 case AArch64::LDRSui
:
234 case AArch64::LDRDui
:
235 case AArch64::LDRQui
:
236 return !(MI
.getOperand(2).getTargetFlags() & AArch64II::MO_GOT
);
240 /// Check whether the given instruction can load a litteral.
241 static bool supportLoadFromLiteral(const MachineInstr
&MI
) {
242 switch (MI
.getOpcode()) {
245 case AArch64::LDRSWui
:
246 case AArch64::LDRWui
:
247 case AArch64::LDRXui
:
248 case AArch64::LDRSui
:
249 case AArch64::LDRDui
:
250 case AArch64::LDRQui
:
255 /// Number of GPR registers traked by mapRegToGPRIndex()
256 static const unsigned N_GPR_REGS
= 31;
257 /// Map register number to index from 0-30.
258 static int mapRegToGPRIndex(MCPhysReg Reg
) {
259 static_assert(AArch64::X28
- AArch64::X0
+ 3 == N_GPR_REGS
, "Number of GPRs");
260 static_assert(AArch64::W30
- AArch64::W0
+ 1 == N_GPR_REGS
, "Number of GPRs");
261 if (AArch64::X0
<= Reg
&& Reg
<= AArch64::X28
)
262 return Reg
- AArch64::X0
;
263 if (AArch64::W0
<= Reg
&& Reg
<= AArch64::W30
)
264 return Reg
- AArch64::W0
;
265 // TableGen gives "FP" and "LR" an index not adjacent to X28 so we have to
266 // handle them as special cases.
267 if (Reg
== AArch64::FP
)
269 if (Reg
== AArch64::LR
)
274 /// State tracked per register.
275 /// The main algorithm walks backwards over a basic block maintaining this
276 /// datastructure for each tracked general purpose register.
278 MCLOHType Type
: 8; ///< "Best" type of LOH possible.
279 bool IsCandidate
: 1; ///< Possible LOH candidate.
280 bool OneUser
: 1; ///< Found exactly one user (yet).
281 bool MultiUsers
: 1; ///< Found multiple users.
282 const MachineInstr
*MI0
; ///< First instruction involved in the LOH.
283 const MachineInstr
*MI1
; ///< Second instruction involved in the LOH
285 const MachineInstr
*LastADRP
; ///< Last ADRP in same register.
288 /// Update state \p Info given \p MI uses the tracked register.
289 static void handleUse(const MachineInstr
&MI
, const MachineOperand
&MO
,
291 // We have multiple uses if we already found one before.
292 if (Info
.MultiUsers
|| Info
.OneUser
) {
293 Info
.IsCandidate
= false;
294 Info
.MultiUsers
= true;
299 // Start new LOHInfo if applicable.
300 if (isCandidateLoad(MI
)) {
301 Info
.Type
= MCLOH_AdrpLdr
;
302 Info
.IsCandidate
= true;
304 // Note that even this is AdrpLdr now, we can switch to a Ldr variant
306 } else if (isCandidateStore(MI
, MO
)) {
307 Info
.Type
= MCLOH_AdrpAddStr
;
308 Info
.IsCandidate
= true;
311 } else if (MI
.getOpcode() == AArch64::ADDXri
) {
312 Info
.Type
= MCLOH_AdrpAdd
;
313 Info
.IsCandidate
= true;
315 } else if (MI
.getOpcode() == AArch64::LDRXui
&&
316 MI
.getOperand(2).getTargetFlags() & AArch64II::MO_GOT
) {
317 Info
.Type
= MCLOH_AdrpLdrGot
;
318 Info
.IsCandidate
= true;
323 /// Update state \p Info given the tracked register is clobbered.
324 static void handleClobber(LOHInfo
&Info
) {
325 Info
.IsCandidate
= false;
326 Info
.OneUser
= false;
327 Info
.MultiUsers
= false;
328 Info
.LastADRP
= nullptr;
331 /// Update state \p Info given that \p MI is possibly the middle instruction
332 /// of an LOH involving 3 instructions.
333 static bool handleMiddleInst(const MachineInstr
&MI
, LOHInfo
&DefInfo
,
335 if (!DefInfo
.IsCandidate
|| (&DefInfo
!= &OpInfo
&& OpInfo
.OneUser
))
337 // Copy LOHInfo for dest register to LOHInfo for source register.
338 if (&DefInfo
!= &OpInfo
) {
340 // Invalidate \p DefInfo because we track it in \p OpInfo now.
341 handleClobber(DefInfo
);
343 DefInfo
.LastADRP
= nullptr;
345 // Advance state machine.
346 assert(OpInfo
.IsCandidate
&& "Expect valid state");
347 if (MI
.getOpcode() == AArch64::ADDXri
&& canAddBePartOfLOH(MI
)) {
348 if (OpInfo
.Type
== MCLOH_AdrpLdr
) {
349 OpInfo
.Type
= MCLOH_AdrpAddLdr
;
350 OpInfo
.IsCandidate
= true;
353 } else if (OpInfo
.Type
== MCLOH_AdrpAddStr
&& OpInfo
.MI1
== nullptr) {
354 OpInfo
.Type
= MCLOH_AdrpAddStr
;
355 OpInfo
.IsCandidate
= true;
360 assert(MI
.getOpcode() == AArch64::LDRXui
&& "Expect LDRXui");
361 assert((MI
.getOperand(2).getTargetFlags() & AArch64II::MO_GOT
) &&
362 "Expected GOT relocation");
363 if (OpInfo
.Type
== MCLOH_AdrpAddStr
&& OpInfo
.MI1
== nullptr) {
364 OpInfo
.Type
= MCLOH_AdrpLdrGotStr
;
365 OpInfo
.IsCandidate
= true;
368 } else if (OpInfo
.Type
== MCLOH_AdrpLdr
) {
369 OpInfo
.Type
= MCLOH_AdrpLdrGotLdr
;
370 OpInfo
.IsCandidate
= true;
378 /// Update state when seeing and ADRP instruction.
379 static void handleADRP(const MachineInstr
&MI
, AArch64FunctionInfo
&AFI
,
381 if (Info
.LastADRP
!= nullptr) {
382 LLVM_DEBUG(dbgs() << "Adding MCLOH_AdrpAdrp:\n"
383 << '\t' << MI
<< '\t' << *Info
.LastADRP
);
384 AFI
.addLOHDirective(MCLOH_AdrpAdrp
, {&MI
, Info
.LastADRP
});
385 ++NumADRPSimpleCandidate
;
388 // Produce LOH directive if possible.
389 if (Info
.IsCandidate
) {
392 LLVM_DEBUG(dbgs() << "Adding MCLOH_AdrpAdd:\n"
393 << '\t' << MI
<< '\t' << *Info
.MI0
);
394 AFI
.addLOHDirective(MCLOH_AdrpAdd
, {&MI
, Info
.MI0
});
395 ++NumADRSimpleCandidate
;
398 if (supportLoadFromLiteral(*Info
.MI0
)) {
399 LLVM_DEBUG(dbgs() << "Adding MCLOH_AdrpLdr:\n"
400 << '\t' << MI
<< '\t' << *Info
.MI0
);
401 AFI
.addLOHDirective(MCLOH_AdrpLdr
, {&MI
, Info
.MI0
});
405 case MCLOH_AdrpAddLdr
:
406 LLVM_DEBUG(dbgs() << "Adding MCLOH_AdrpAddLdr:\n"
407 << '\t' << MI
<< '\t' << *Info
.MI1
<< '\t'
409 AFI
.addLOHDirective(MCLOH_AdrpAddLdr
, {&MI
, Info
.MI1
, Info
.MI0
});
412 case MCLOH_AdrpAddStr
:
413 if (Info
.MI1
!= nullptr) {
414 LLVM_DEBUG(dbgs() << "Adding MCLOH_AdrpAddStr:\n"
415 << '\t' << MI
<< '\t' << *Info
.MI1
<< '\t'
417 AFI
.addLOHDirective(MCLOH_AdrpAddStr
, {&MI
, Info
.MI1
, Info
.MI0
});
421 case MCLOH_AdrpLdrGotLdr
:
422 LLVM_DEBUG(dbgs() << "Adding MCLOH_AdrpLdrGotLdr:\n"
423 << '\t' << MI
<< '\t' << *Info
.MI1
<< '\t'
425 AFI
.addLOHDirective(MCLOH_AdrpLdrGotLdr
, {&MI
, Info
.MI1
, Info
.MI0
});
428 case MCLOH_AdrpLdrGotStr
:
429 LLVM_DEBUG(dbgs() << "Adding MCLOH_AdrpLdrGotStr:\n"
430 << '\t' << MI
<< '\t' << *Info
.MI1
<< '\t'
432 AFI
.addLOHDirective(MCLOH_AdrpLdrGotStr
, {&MI
, Info
.MI1
, Info
.MI0
});
435 case MCLOH_AdrpLdrGot
:
436 LLVM_DEBUG(dbgs() << "Adding MCLOH_AdrpLdrGot:\n"
437 << '\t' << MI
<< '\t' << *Info
.MI0
);
438 AFI
.addLOHDirective(MCLOH_AdrpLdrGot
, {&MI
, Info
.MI0
});
441 llvm_unreachable("MCLOH_AdrpAdrp not used in state machine");
449 static void handleRegMaskClobber(const uint32_t *RegMask
, MCPhysReg Reg
,
451 if (!MachineOperand::clobbersPhysReg(RegMask
, Reg
))
453 int Idx
= mapRegToGPRIndex(Reg
);
455 handleClobber(LOHInfos
[Idx
]);
458 static void handleNormalInst(const MachineInstr
&MI
, LOHInfo
*LOHInfos
) {
459 // Handle defs and regmasks.
460 for (const MachineOperand
&MO
: MI
.operands()) {
461 if (MO
.isRegMask()) {
462 const uint32_t *RegMask
= MO
.getRegMask();
463 for (MCPhysReg Reg
: AArch64::GPR32RegClass
)
464 handleRegMaskClobber(RegMask
, Reg
, LOHInfos
);
465 for (MCPhysReg Reg
: AArch64::GPR64RegClass
)
466 handleRegMaskClobber(RegMask
, Reg
, LOHInfos
);
469 if (!MO
.isReg() || !MO
.isDef())
471 int Idx
= mapRegToGPRIndex(MO
.getReg());
474 handleClobber(LOHInfos
[Idx
]);
477 for (const MachineOperand
&MO
: MI
.uses()) {
478 if (!MO
.isReg() || !MO
.readsReg())
480 int Idx
= mapRegToGPRIndex(MO
.getReg());
483 handleUse(MI
, MO
, LOHInfos
[Idx
]);
487 bool AArch64CollectLOH::runOnMachineFunction(MachineFunction
&MF
) {
488 if (skipFunction(MF
.getFunction()))
491 LLVM_DEBUG(dbgs() << "********** AArch64 Collect LOH **********\n"
492 << "Looking in function " << MF
.getName() << '\n');
494 LOHInfo LOHInfos
[N_GPR_REGS
];
495 AArch64FunctionInfo
&AFI
= *MF
.getInfo
<AArch64FunctionInfo
>();
496 for (const MachineBasicBlock
&MBB
: MF
) {
497 // Reset register tracking state.
498 memset(LOHInfos
, 0, sizeof(LOHInfos
));
499 // Live-out registers are used.
500 for (const MachineBasicBlock
*Succ
: MBB
.successors()) {
501 for (const auto &LI
: Succ
->liveins()) {
502 int RegIdx
= mapRegToGPRIndex(LI
.PhysReg
);
504 LOHInfos
[RegIdx
].OneUser
= true;
508 // Walk the basic block backwards and update the per register state machine
510 for (const MachineInstr
&MI
: make_range(MBB
.rbegin(), MBB
.rend())) {
511 unsigned Opcode
= MI
.getOpcode();
513 case AArch64::ADDXri
:
514 case AArch64::LDRXui
:
515 if (canDefBePartOfLOH(MI
)) {
516 const MachineOperand
&Def
= MI
.getOperand(0);
517 const MachineOperand
&Op
= MI
.getOperand(1);
518 assert(Def
.isReg() && Def
.isDef() && "Expected reg def");
519 assert(Op
.isReg() && Op
.isUse() && "Expected reg use");
520 int DefIdx
= mapRegToGPRIndex(Def
.getReg());
521 int OpIdx
= mapRegToGPRIndex(Op
.getReg());
522 if (DefIdx
>= 0 && OpIdx
>= 0 &&
523 handleMiddleInst(MI
, LOHInfos
[DefIdx
], LOHInfos
[OpIdx
]))
528 const MachineOperand
&Op0
= MI
.getOperand(0);
529 int Idx
= mapRegToGPRIndex(Op0
.getReg());
531 handleADRP(MI
, AFI
, LOHInfos
[Idx
]);
536 handleNormalInst(MI
, LOHInfos
);
540 // Return "no change": The pass only collects information.
544 FunctionPass
*llvm::createAArch64CollectLOHPass() {
545 return new AArch64CollectLOH();