1 //===---------- AArch64CollectLOH.cpp - AArch64 collect LOH pass --*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a pass that collect the Linker Optimization Hint (LOH).
11 // This pass should be run at the very end of the compilation flow, just before
12 // assembly printer.
13 // To be useful for the linker, the LOH must be printed into the assembly file.
14 //
15 // A LOH describes a sequence of instructions that may be optimized by the
16 // linker.
17 // This same sequence cannot be optimized by the compiler because some of
18 // the information will be known at link time.
19 // For instance, consider the following sequence:
20 //     L1: adrp xA, sym@PAGE
21 //     L2: add xB, xA, sym@PAGEOFF
22 //     L3: ldr xC, [xB, #imm]
23 // This sequence can be turned into:
24 // A literal load if sym@PAGE + sym@PAGEOFF + #imm - address(L3) is < 1MB:
25 //     L3: ldr xC, sym+#imm
26 // It may also be turned into either the following more efficient
27 // code sequences:
28 // - If sym@PAGEOFF + #imm fits the encoding space of L3.
29 //     L1: adrp xA, sym@PAGE
30 //     L3: ldr xC, [xB, sym@PAGEOFF + #imm]
31 // - If sym@PAGE + sym@PAGEOFF - address(L1) < 1MB:
32 //     L1: adr xA, sym
33 //     L3: ldr xC, [xB, #imm]
34 //
35 // To be valid a LOH must meet all the requirements needed by all the related
36 // possible linker transformations.
37 // For instance, using the running example, the constraints to emit
38 // ".loh AdrpAddLdr" are:
39 // - L1, L2, and L3 instructions are of the expected type, i.e.,
40 //   respectively ADRP, ADD (immediate), and LD.
41 // - The result of L1 is used only by L2.
42 // - The register argument (xA) used in the ADD instruction is defined
43 //   only by L1.
44 // - The result of L2 is used only by L3.
45 // - The base address (xB) in L3 is defined only L2.
46 // - The ADRP in L1 and the ADD in L2 must reference the same symbol using
47 //   @PAGE/@PAGEOFF with no additional constants
48 //
49 // Currently supported LOHs are:
50 // * So called non-ADRP-related:
51 //   - .loh AdrpAddLdr L1, L2, L3:
52 //     L1: adrp xA, sym@PAGE
53 //     L2: add xB, xA, sym@PAGEOFF
54 //     L3: ldr xC, [xB, #imm]
55 //   - .loh AdrpLdrGotLdr L1, L2, L3:
56 //     L1: adrp xA, sym@GOTPAGE
57 //     L2: ldr xB, [xA, sym@GOTPAGEOFF]
58 //     L3: ldr xC, [xB, #imm]
59 //   - .loh AdrpLdr L1, L3:
60 //     L1: adrp xA, sym@PAGE
61 //     L3: ldr xC, [xA, sym@PAGEOFF]
62 //   - .loh AdrpAddStr L1, L2, L3:
63 //     L1: adrp xA, sym@PAGE
64 //     L2: add xB, xA, sym@PAGEOFF
65 //     L3: str xC, [xB, #imm]
66 //   - .loh AdrpLdrGotStr L1, L2, L3:
67 //     L1: adrp xA, sym@GOTPAGE
68 //     L2: ldr xB, [xA, sym@GOTPAGEOFF]
69 //     L3: str xC, [xB, #imm]
70 //   - .loh AdrpAdd L1, L2:
71 //     L1: adrp xA, sym@PAGE
72 //     L2: add xB, xA, sym@PAGEOFF
73 //   For all these LOHs, L1, L2, L3 form a simple chain:
74 //   L1 result is used only by L2 and L2 result by L3.
75 //   L3 LOH-related argument is defined only by L2 and L2 LOH-related argument
76 //   by L1.
77 // All these LOHs aim at using more efficient load/store patterns by folding
78 // some instructions used to compute the address directly into the load/store.
79 //
80 // * So called ADRP-related:
81 //  - .loh AdrpAdrp L2, L1:
82 //    L2: ADRP xA, sym1@PAGE
83 //    L1: ADRP xA, sym2@PAGE
84 //    L2 dominates L1 and xA is not redifined between L2 and L1
85 // This LOH aims at getting rid of redundant ADRP instructions.
86 //
87 // The overall design for emitting the LOHs is:
88 // 1. AArch64CollectLOH (this pass) records the LOHs in the AArch64FunctionInfo.
89 // 2. AArch64AsmPrinter reads the LOHs from AArch64FunctionInfo and it:
90 //     1. Associates them a label.
91 //     2. Emits them in a MCStreamer (EmitLOHDirective).
92 //         - The MCMachOStreamer records them into the MCAssembler.
93 //         - The MCAsmStreamer prints them.
94 //         - Other MCStreamers ignore them.
95 //     3. Closes the MCStreamer:
96 //         - The MachObjectWriter gets them from the MCAssembler and writes
97 //           them in the object file.
98 //         - Other ObjectWriters ignore them.
99 //===----------------------------------------------------------------------===//
100 
101 #include "AArch64.h"
102 #include "AArch64InstrInfo.h"
103 #include "AArch64MachineFunctionInfo.h"
104 #include "AArch64Subtarget.h"
105 #include "MCTargetDesc/AArch64AddressingModes.h"
106 #include "llvm/ADT/BitVector.h"
107 #include "llvm/ADT/DenseMap.h"
108 #include "llvm/ADT/MapVector.h"
109 #include "llvm/ADT/SetVector.h"
110 #include "llvm/ADT/SmallVector.h"
111 #include "llvm/ADT/Statistic.h"
112 #include "llvm/CodeGen/MachineBasicBlock.h"
113 #include "llvm/CodeGen/MachineDominators.h"
114 #include "llvm/CodeGen/MachineFunctionPass.h"
115 #include "llvm/CodeGen/MachineInstr.h"
116 #include "llvm/CodeGen/MachineInstrBuilder.h"
117 #include "llvm/Support/CommandLine.h"
118 #include "llvm/Support/Debug.h"
119 #include "llvm/Support/ErrorHandling.h"
120 #include "llvm/Support/raw_ostream.h"
121 #include "llvm/Target/TargetInstrInfo.h"
122 #include "llvm/Target/TargetMachine.h"
123 #include "llvm/Target/TargetRegisterInfo.h"
124 using namespace llvm;
125 
126 #define DEBUG_TYPE "aarch64-collect-loh"
127 
128 static cl::opt<bool>
129 PreCollectRegister("aarch64-collect-loh-pre-collect-register", cl::Hidden,
130                    cl::desc("Restrict analysis to registers invovled"
131                             " in LOHs"),
132                    cl::init(true));
133 
134 static cl::opt<bool>
135 BasicBlockScopeOnly("aarch64-collect-loh-bb-only", cl::Hidden,
136                     cl::desc("Restrict analysis at basic block scope"),
137                     cl::init(true));
138 
139 STATISTIC(NumADRPSimpleCandidate,
140           "Number of simplifiable ADRP dominate by another");
141 #ifndef NDEBUG
142 STATISTIC(NumADRPComplexCandidate2,
143           "Number of simplifiable ADRP reachable by 2 defs");
144 STATISTIC(NumADRPComplexCandidate3,
145           "Number of simplifiable ADRP reachable by 3 defs");
146 STATISTIC(NumADRPComplexCandidateOther,
147           "Number of simplifiable ADRP reachable by 4 or more defs");
148 STATISTIC(NumADDToSTRWithImm,
149           "Number of simplifiable STR with imm reachable by ADD");
150 STATISTIC(NumLDRToSTRWithImm,
151           "Number of simplifiable STR with imm reachable by LDR");
152 STATISTIC(NumADDToSTR, "Number of simplifiable STR reachable by ADD");
153 STATISTIC(NumLDRToSTR, "Number of simplifiable STR reachable by LDR");
154 STATISTIC(NumADDToLDRWithImm,
155           "Number of simplifiable LDR with imm reachable by ADD");
156 STATISTIC(NumLDRToLDRWithImm,
157           "Number of simplifiable LDR with imm reachable by LDR");
158 STATISTIC(NumADDToLDR, "Number of simplifiable LDR reachable by ADD");
159 STATISTIC(NumLDRToLDR, "Number of simplifiable LDR reachable by LDR");
160 #endif // NDEBUG
161 STATISTIC(NumADRPToLDR, "Number of simplifiable LDR reachable by ADRP");
162 #ifndef NDEBUG
163 STATISTIC(NumCplxLvl1, "Number of complex case of level 1");
164 STATISTIC(NumTooCplxLvl1, "Number of too complex case of level 1");
165 STATISTIC(NumCplxLvl2, "Number of complex case of level 2");
166 STATISTIC(NumTooCplxLvl2, "Number of too complex case of level 2");
167 #endif // NDEBUG
168 STATISTIC(NumADRSimpleCandidate, "Number of simplifiable ADRP + ADD");
169 STATISTIC(NumADRComplexCandidate, "Number of too complex ADRP + ADD");
170 
171 #define AARCH64_COLLECT_LOH_NAME "AArch64 Collect Linker Optimization Hint (LOH)"
172 
173 namespace {
174 struct AArch64CollectLOH : public MachineFunctionPass {
175   static char ID;
176   AArch64CollectLOH() : MachineFunctionPass(ID) {
177     initializeAArch64CollectLOHPass(*PassRegistry::getPassRegistry());
178   }
179 
180   bool runOnMachineFunction(MachineFunction &MF) override;
181 
182   MachineFunctionProperties getRequiredProperties() const override {
183     return MachineFunctionProperties().set(
184         MachineFunctionProperties::Property::NoVRegs);
185   }
186 
187   StringRef getPassName() const override { return AARCH64_COLLECT_LOH_NAME; }
188 
189   void getAnalysisUsage(AnalysisUsage &AU) const override {
190     AU.setPreservesAll();
191     MachineFunctionPass::getAnalysisUsage(AU);
192     AU.addRequired<MachineDominatorTree>();
193   }
194 
195 private:
196 };
197 
198 /// A set of MachineInstruction.
199 typedef SetVector<const MachineInstr *> SetOfMachineInstr;
200 /// Map a basic block to a set of instructions per register.
201 /// This is used to represent the exposed uses of a basic block
202 /// per register.
203 typedef MapVector<const MachineBasicBlock *,
204                   std::unique_ptr<SetOfMachineInstr[]>>
205 BlockToSetOfInstrsPerColor;
206 /// Map a basic block to an instruction per register.
207 /// This is used to represent the live-out definitions of a basic block
208 /// per register.
209 typedef MapVector<const MachineBasicBlock *,
210                   std::unique_ptr<const MachineInstr *[]>>
211 BlockToInstrPerColor;
212 /// Map an instruction to a set of instructions. Used to represent the
213 /// mapping def to reachable uses or use to definitions.
214 typedef MapVector<const MachineInstr *, SetOfMachineInstr> InstrToInstrs;
215 /// Map a basic block to a BitVector.
216 /// This is used to record the kill registers per basic block.
217 typedef MapVector<const MachineBasicBlock *, BitVector> BlockToRegSet;
218 
219 /// Map a register to a dense id.
220 typedef DenseMap<unsigned, unsigned> MapRegToId;
221 /// Map a dense id to a register. Used for debug purposes.
222 typedef SmallVector<unsigned, 32> MapIdToReg;
223 } // end anonymous namespace.
224 
225 char AArch64CollectLOH::ID = 0;
226 
227 INITIALIZE_PASS_BEGIN(AArch64CollectLOH, "aarch64-collect-loh",
228                       AARCH64_COLLECT_LOH_NAME, false, false)
229 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
230 INITIALIZE_PASS_END(AArch64CollectLOH, "aarch64-collect-loh",
231                     AARCH64_COLLECT_LOH_NAME, false, false)
232 
233 /// Given a couple (MBB, reg) get the corresponding set of instruction from
234 /// the given "sets".
235 /// If this couple does not reference any set, an empty set is added to "sets"
236 /// for this couple and returned.
237 /// \param nbRegs is used internally allocate some memory. It must be consistent
238 /// with the way sets is used.
239 static SetOfMachineInstr &getSet(BlockToSetOfInstrsPerColor &sets,
240                                  const MachineBasicBlock &MBB, unsigned reg,
241                                  unsigned nbRegs) {
242   SetOfMachineInstr *result;
243   BlockToSetOfInstrsPerColor::iterator it = sets.find(&MBB);
244   if (it != sets.end())
245     result = it->second.get();
246   else
247     result = (sets[&MBB] = make_unique<SetOfMachineInstr[]>(nbRegs)).get();
248 
249   return result[reg];
250 }
251 
252 /// Given a couple (reg, MI) get the corresponding set of instructions from the
253 /// the given "sets".
254 /// This is used to get the uses record in sets of a definition identified by
255 /// MI and reg, i.e., MI defines reg.
256 /// If the couple does not reference anything, an empty set is added to
257 /// "sets[reg]".
258 /// \pre set[reg] is valid.
259 static SetOfMachineInstr &getUses(InstrToInstrs *sets, unsigned reg,
260                                   const MachineInstr &MI) {
261   return sets[reg][&MI];
262 }
263 
264 /// Same as getUses but does not modify the input map: sets.
265 /// \return NULL if the couple (reg, MI) is not in sets.
266 static const SetOfMachineInstr *getUses(const InstrToInstrs *sets, unsigned reg,
267                                         const MachineInstr &MI) {
268   InstrToInstrs::const_iterator Res = sets[reg].find(&MI);
269   if (Res != sets[reg].end())
270     return &(Res->second);
271   return nullptr;
272 }
273 
274 /// Initialize the reaching definition algorithm:
275 /// For each basic block BB in MF, record:
276 /// - its kill set.
277 /// - its reachable uses (uses that are exposed to BB's predecessors).
278 /// - its the generated definitions.
279 /// \param DummyOp if not NULL, specifies a Dummy Operation to be added to
280 /// the list of uses of exposed defintions.
281 /// \param ADRPMode specifies to only consider ADRP instructions for generated
282 /// definition. It also consider definitions of ADRP instructions as uses and
283 /// ignore other uses. The ADRPMode is used to collect the information for LHO
284 /// that involve ADRP operation only.
285 static void initReachingDef(const MachineFunction &MF,
286                             InstrToInstrs *ColorOpToReachedUses,
287                             BlockToInstrPerColor &Gen, BlockToRegSet &Kill,
288                             BlockToSetOfInstrsPerColor &ReachableUses,
289                             const MapRegToId &RegToId,
290                             const MachineInstr *DummyOp, bool ADRPMode) {
291   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
292   unsigned NbReg = RegToId.size();
293 
294   for (const MachineBasicBlock &MBB : MF) {
295     auto &BBGen = Gen[&MBB];
296     BBGen = make_unique<const MachineInstr *[]>(NbReg);
297     std::fill(BBGen.get(), BBGen.get() + NbReg, nullptr);
298 
299     BitVector &BBKillSet = Kill[&MBB];
300     BBKillSet.resize(NbReg);
301     for (const MachineInstr &MI : MBB) {
302       bool IsADRP = MI.getOpcode() == AArch64::ADRP;
303 
304       // Process uses first.
305       if (IsADRP || !ADRPMode)
306         for (const MachineOperand &MO : MI.operands()) {
307           // Treat ADRP def as use, as the goal of the analysis is to find
308           // ADRP defs reached by other ADRP defs.
309           if (!MO.isReg() || (!ADRPMode && !MO.isUse()) ||
310               (ADRPMode && (!IsADRP || !MO.isDef())))
311             continue;
312           unsigned CurReg = MO.getReg();
313           MapRegToId::const_iterator ItCurRegId = RegToId.find(CurReg);
314           if (ItCurRegId == RegToId.end())
315             continue;
316           CurReg = ItCurRegId->second;
317 
318           // if CurReg has not been defined, this use is reachable.
319           if (!BBGen[CurReg] && !BBKillSet.test(CurReg))
320             getSet(ReachableUses, MBB, CurReg, NbReg).insert(&MI);
321           // current basic block definition for this color, if any, is in Gen.
322           if (BBGen[CurReg])
323             getUses(ColorOpToReachedUses, CurReg, *BBGen[CurReg]).insert(&MI);
324         }
325 
326       // Process clobbers.
327       for (const MachineOperand &MO : MI.operands()) {
328         if (!MO.isRegMask())
329           continue;
330         // Clobbers kill the related colors.
331         const uint32_t *PreservedRegs = MO.getRegMask();
332 
333         // Set generated regs.
334         for (const auto &Entry : RegToId) {
335           unsigned Reg = Entry.second;
336           // Use the global register ID when querying APIs external to this
337           // pass.
338           if (MachineOperand::clobbersPhysReg(PreservedRegs, Entry.first)) {
339             // Do not register clobbered definition for no ADRP.
340             // This definition is not used anyway (otherwise register
341             // allocation is wrong).
342             BBGen[Reg] = ADRPMode ? &MI : nullptr;
343             BBKillSet.set(Reg);
344           }
345         }
346       }
347 
348       // Process register defs.
349       for (const MachineOperand &MO : MI.operands()) {
350         if (!MO.isReg() || !MO.isDef())
351           continue;
352         unsigned CurReg = MO.getReg();
353         MapRegToId::const_iterator ItCurRegId = RegToId.find(CurReg);
354         if (ItCurRegId == RegToId.end())
355           continue;
356 
357         for (MCRegAliasIterator AI(CurReg, TRI, true); AI.isValid(); ++AI) {
358           MapRegToId::const_iterator ItRegId = RegToId.find(*AI);
359           // If this alias has not been recorded, then it is not interesting
360           // for the current analysis.
361           // We can end up in this situation because of tuple registers.
362           // E.g., Let say we are interested in S1. When we register
363           // S1, we will also register its aliases and in particular
364           // the tuple Q1_Q2.
365           // Now, when we encounter Q1_Q2, we will look through its aliases
366           // and will find that S2 is not registered.
367           if (ItRegId == RegToId.end())
368             continue;
369 
370           BBKillSet.set(ItRegId->second);
371           BBGen[ItRegId->second] = &MI;
372         }
373         BBGen[ItCurRegId->second] = &MI;
374       }
375     }
376 
377     // If we restrict our analysis to basic block scope, conservatively add a
378     // dummy
379     // use for each generated value.
380     if (!ADRPMode && DummyOp && !MBB.succ_empty())
381       for (unsigned CurReg = 0; CurReg < NbReg; ++CurReg)
382         if (BBGen[CurReg])
383           getUses(ColorOpToReachedUses, CurReg, *BBGen[CurReg]).insert(DummyOp);
384   }
385 }
386 
387 /// Reaching def core algorithm:
388 /// while an Out has changed
389 ///    for each bb
390 ///       for each color
391 ///           In[bb][color] = U Out[bb.predecessors][color]
392 ///           insert reachableUses[bb][color] in each in[bb][color]
393 ///                 op.reachedUses
394 ///
395 ///           Out[bb] = Gen[bb] U (In[bb] - Kill[bb])
396 static void reachingDefAlgorithm(const MachineFunction &MF,
397                                  InstrToInstrs *ColorOpToReachedUses,
398                                  BlockToSetOfInstrsPerColor &In,
399                                  BlockToSetOfInstrsPerColor &Out,
400                                  BlockToInstrPerColor &Gen, BlockToRegSet &Kill,
401                                  BlockToSetOfInstrsPerColor &ReachableUses,
402                                  unsigned NbReg) {
403   bool HasChanged;
404   do {
405     HasChanged = false;
406     for (const MachineBasicBlock &MBB : MF) {
407       unsigned CurReg;
408       for (CurReg = 0; CurReg < NbReg; ++CurReg) {
409         SetOfMachineInstr &BBInSet = getSet(In, MBB, CurReg, NbReg);
410         SetOfMachineInstr &BBReachableUses =
411             getSet(ReachableUses, MBB, CurReg, NbReg);
412         SetOfMachineInstr &BBOutSet = getSet(Out, MBB, CurReg, NbReg);
413         unsigned Size = BBOutSet.size();
414         //   In[bb][color] = U Out[bb.predecessors][color]
415         for (const MachineBasicBlock *PredMBB : MBB.predecessors()) {
416           SetOfMachineInstr &PredOutSet = getSet(Out, *PredMBB, CurReg, NbReg);
417           BBInSet.insert(PredOutSet.begin(), PredOutSet.end());
418         }
419         //   insert reachableUses[bb][color] in each in[bb][color] op.reachedses
420         for (const MachineInstr *MI : BBInSet) {
421           SetOfMachineInstr &OpReachedUses =
422               getUses(ColorOpToReachedUses, CurReg, *MI);
423           OpReachedUses.insert(BBReachableUses.begin(), BBReachableUses.end());
424         }
425         //           Out[bb] = Gen[bb] U (In[bb] - Kill[bb])
426         if (!Kill[&MBB].test(CurReg))
427           BBOutSet.insert(BBInSet.begin(), BBInSet.end());
428         if (Gen[&MBB][CurReg])
429           BBOutSet.insert(Gen[&MBB][CurReg]);
430         HasChanged |= BBOutSet.size() != Size;
431       }
432     }
433   } while (HasChanged);
434 }
435 
436 /// Reaching definition algorithm.
437 /// \param MF function on which the algorithm will operate.
438 /// \param[out] ColorOpToReachedUses will contain the result of the reaching
439 /// def algorithm.
440 /// \param ADRPMode specify whether the reaching def algorithm should be tuned
441 /// for ADRP optimization. \see initReachingDef for more details.
442 /// \param DummyOp if not NULL, the algorithm will work at
443 /// basic block scope and will set for every exposed definition a use to
444 /// @p DummyOp.
445 /// \pre ColorOpToReachedUses is an array of at least number of registers of
446 /// InstrToInstrs.
447 static void reachingDef(const MachineFunction &MF,
448                         InstrToInstrs *ColorOpToReachedUses,
449                         const MapRegToId &RegToId, bool ADRPMode = false,
450                         const MachineInstr *DummyOp = nullptr) {
451   // structures:
452   // For each basic block.
453   // Out: a set per color of definitions that reach the
454   //      out boundary of this block.
455   // In: Same as Out but for in boundary.
456   // Gen: generated color in this block (one operation per color).
457   // Kill: register set of killed color in this block.
458   // ReachableUses: a set per color of uses (operation) reachable
459   //                for "In" definitions.
460   BlockToSetOfInstrsPerColor Out, In, ReachableUses;
461   BlockToInstrPerColor Gen;
462   BlockToRegSet Kill;
463 
464   // Initialize Gen, kill and reachableUses.
465   initReachingDef(MF, ColorOpToReachedUses, Gen, Kill, ReachableUses, RegToId,
466                   DummyOp, ADRPMode);
467 
468   // Algo.
469   if (!DummyOp)
470     reachingDefAlgorithm(MF, ColorOpToReachedUses, In, Out, Gen, Kill,
471                          ReachableUses, RegToId.size());
472 }
473 
474 #ifndef NDEBUG
475 /// print the result of the reaching definition algorithm.
476 static void printReachingDef(const InstrToInstrs *ColorOpToReachedUses,
477                              unsigned NbReg, const TargetRegisterInfo *TRI,
478                              const MapIdToReg &IdToReg) {
479   unsigned CurReg;
480   for (CurReg = 0; CurReg < NbReg; ++CurReg) {
481     if (ColorOpToReachedUses[CurReg].empty())
482       continue;
483     DEBUG(dbgs() << "*** Reg " << PrintReg(IdToReg[CurReg], TRI) << " ***\n");
484 
485     for (const auto &DefsIt : ColorOpToReachedUses[CurReg]) {
486       DEBUG(dbgs() << "Def:\n");
487       DEBUG(DefsIt.first->print(dbgs()));
488       DEBUG(dbgs() << "Reachable uses:\n");
489       for (const MachineInstr *MI : DefsIt.second) {
490         DEBUG(MI->print(dbgs()));
491       }
492     }
493   }
494 }
495 #endif // NDEBUG
496 
497 /// Answer the following question: Can Def be one of the definition
498 /// involved in a part of a LOH?
499 static bool canDefBePartOfLOH(const MachineInstr *Def) {
500   unsigned Opc = Def->getOpcode();
501   // Accept ADRP, ADDLow and LOADGot.
502   switch (Opc) {
503   default:
504     return false;
505   case AArch64::ADRP:
506     return true;
507   case AArch64::ADDXri:
508     // Check immediate to see if the immediate is an address.
509     switch (Def->getOperand(2).getType()) {
510     default:
511       return false;
512     case MachineOperand::MO_GlobalAddress:
513     case MachineOperand::MO_JumpTableIndex:
514     case MachineOperand::MO_ConstantPoolIndex:
515     case MachineOperand::MO_BlockAddress:
516       return true;
517     }
518   case AArch64::LDRXui:
519     // Check immediate to see if the immediate is an address.
520     switch (Def->getOperand(2).getType()) {
521     default:
522       return false;
523     case MachineOperand::MO_GlobalAddress:
524       return true;
525     }
526   }
527   // Unreachable.
528   return false;
529 }
530 
531 /// Check whether the given instruction can the end of a LOH chain involving a
532 /// store.
533 static bool isCandidateStore(const MachineInstr *Instr) {
534   switch (Instr->getOpcode()) {
535   default:
536     return false;
537   case AArch64::STRBBui:
538   case AArch64::STRHHui:
539   case AArch64::STRBui:
540   case AArch64::STRHui:
541   case AArch64::STRWui:
542   case AArch64::STRXui:
543   case AArch64::STRSui:
544   case AArch64::STRDui:
545   case AArch64::STRQui:
546     // In case we have str xA, [xA, #imm], this is two different uses
547     // of xA and we cannot fold, otherwise the xA stored may be wrong,
548     // even if #imm == 0.
549     if (Instr->getOperand(0).getReg() != Instr->getOperand(1).getReg())
550       return true;
551   }
552   return false;
553 }
554 
555 /// Given the result of a reaching definition algorithm in ColorOpToReachedUses,
556 /// Build the Use to Defs information and filter out obvious non-LOH candidates.
557 /// In ADRPMode, non-LOH candidates are "uses" with non-ADRP definitions.
558 /// In non-ADRPMode, non-LOH candidates are "uses" with several definition,
559 /// i.e., no simple chain.
560 /// \param ADRPMode -- \see initReachingDef.
561 static void reachedUsesToDefs(InstrToInstrs &UseToReachingDefs,
562                               const InstrToInstrs *ColorOpToReachedUses,
563                               const MapRegToId &RegToId,
564                               bool ADRPMode = false) {
565 
566   SetOfMachineInstr NotCandidate;
567   unsigned NbReg = RegToId.size();
568   MapRegToId::const_iterator EndIt = RegToId.end();
569   for (unsigned CurReg = 0; CurReg < NbReg; ++CurReg) {
570     // If this color is never defined, continue.
571     if (ColorOpToReachedUses[CurReg].empty())
572       continue;
573 
574     for (const auto &DefsIt : ColorOpToReachedUses[CurReg]) {
575       for (const MachineInstr *MI : DefsIt.second) {
576         const MachineInstr *Def = DefsIt.first;
577         MapRegToId::const_iterator It;
578         // if all the reaching defs are not adrp, this use will not be
579         // simplifiable.
580         if ((ADRPMode && Def->getOpcode() != AArch64::ADRP) ||
581             (!ADRPMode && !canDefBePartOfLOH(Def)) ||
582             (!ADRPMode && isCandidateStore(MI) &&
583              // store are LOH candidate iff the end of the chain is used as
584              // base.
585              ((It = RegToId.find((MI)->getOperand(1).getReg())) == EndIt ||
586               It->second != CurReg))) {
587           NotCandidate.insert(MI);
588           continue;
589         }
590         // Do not consider self reaching as a simplifiable case for ADRP.
591         if (!ADRPMode || MI != DefsIt.first) {
592           UseToReachingDefs[MI].insert(DefsIt.first);
593           // If UsesIt has several reaching definitions, it is not
594           // candidate for simplificaton in non-ADRPMode.
595           if (!ADRPMode && UseToReachingDefs[MI].size() > 1)
596             NotCandidate.insert(MI);
597         }
598       }
599     }
600   }
601   for (const MachineInstr *Elem : NotCandidate) {
602     DEBUG(dbgs() << "Too many reaching defs: " << *Elem << "\n");
603     // It would have been better if we could just remove the entry
604     // from the map.  Because of that, we have to filter the garbage
605     // (second.empty) in the subsequence analysis.
606     UseToReachingDefs[Elem].clear();
607   }
608 }
609 
610 /// Based on the use to defs information (in ADRPMode), compute the
611 /// opportunities of LOH ADRP-related.
612 static void computeADRP(const InstrToInstrs &UseToDefs,
613                         AArch64FunctionInfo &AArch64FI,
614                         const MachineDominatorTree *MDT) {
615   DEBUG(dbgs() << "*** Compute LOH for ADRP\n");
616   for (const auto &Entry : UseToDefs) {
617     unsigned Size = Entry.second.size();
618     if (Size == 0)
619       continue;
620     if (Size == 1) {
621       const MachineInstr *L2 = *Entry.second.begin();
622       const MachineInstr *L1 = Entry.first;
623       if (!MDT->dominates(L2, L1)) {
624         DEBUG(dbgs() << "Dominance check failed:\n" << *L2 << '\n' << *L1
625                      << '\n');
626         continue;
627       }
628       DEBUG(dbgs() << "Record AdrpAdrp:\n" << *L2 << '\n' << *L1 << '\n');
629       AArch64FI.addLOHDirective(MCLOH_AdrpAdrp, {L2, L1});
630       ++NumADRPSimpleCandidate;
631     }
632 #ifndef NDEBUG
633     else if (Size == 2)
634       ++NumADRPComplexCandidate2;
635     else if (Size == 3)
636       ++NumADRPComplexCandidate3;
637     else
638       ++NumADRPComplexCandidateOther;
639 #endif
640     // if Size < 1, the use should have been removed from the candidates
641     assert(Size >= 1 && "No reaching defs for that use!");
642   }
643 }
644 
645 /// Check whether the given instruction can be the end of a LOH chain
646 /// involving a load.
647 static bool isCandidateLoad(const MachineInstr *Instr) {
648   switch (Instr->getOpcode()) {
649   default:
650     return false;
651   case AArch64::LDRSBWui:
652   case AArch64::LDRSBXui:
653   case AArch64::LDRSHWui:
654   case AArch64::LDRSHXui:
655   case AArch64::LDRSWui:
656   case AArch64::LDRBui:
657   case AArch64::LDRHui:
658   case AArch64::LDRWui:
659   case AArch64::LDRXui:
660   case AArch64::LDRSui:
661   case AArch64::LDRDui:
662   case AArch64::LDRQui:
663     if (Instr->getOperand(2).getTargetFlags() & AArch64II::MO_GOT)
664       return false;
665     return true;
666   }
667   // Unreachable.
668   return false;
669 }
670 
671 /// Check whether the given instruction can load a litteral.
672 static bool supportLoadFromLiteral(const MachineInstr *Instr) {
673   switch (Instr->getOpcode()) {
674   default:
675     return false;
676   case AArch64::LDRSWui:
677   case AArch64::LDRWui:
678   case AArch64::LDRXui:
679   case AArch64::LDRSui:
680   case AArch64::LDRDui:
681   case AArch64::LDRQui:
682     return true;
683   }
684   // Unreachable.
685   return false;
686 }
687 
688 /// Check whether the given instruction is a LOH candidate.
689 /// \param UseToDefs is used to check that Instr is at the end of LOH supported
690 /// chain.
691 /// \pre UseToDefs contains only on def per use, i.e., obvious non candidate are
692 /// already been filtered out.
693 static bool isCandidate(const MachineInstr *Instr,
694                         const InstrToInstrs &UseToDefs,
695                         const MachineDominatorTree *MDT) {
696   if (!isCandidateLoad(Instr) && !isCandidateStore(Instr))
697     return false;
698 
699   const MachineInstr *Def = *UseToDefs.find(Instr)->second.begin();
700   if (Def->getOpcode() != AArch64::ADRP) {
701     // At this point, Def is ADDXri or LDRXui of the right type of
702     // symbol, because we filtered out the uses that were not defined
703     // by these kind of instructions (+ ADRP).
704 
705     // Check if this forms a simple chain: each intermediate node must
706     // dominates the next one.
707     if (!MDT->dominates(Def, Instr))
708       return false;
709     // Move one node up in the simple chain.
710     if (UseToDefs.find(Def) ==
711             UseToDefs.end()
712             // The map may contain garbage we have to ignore.
713         ||
714         UseToDefs.find(Def)->second.empty())
715       return false;
716     Instr = Def;
717     Def = *UseToDefs.find(Def)->second.begin();
718   }
719   // Check if we reached the top of the simple chain:
720   // - top is ADRP.
721   // - check the simple chain property: each intermediate node must
722   // dominates the next one.
723   if (Def->getOpcode() == AArch64::ADRP)
724     return MDT->dominates(Def, Instr);
725   return false;
726 }
727 
728 static bool registerADRCandidate(const MachineInstr &Use,
729                                  const InstrToInstrs &UseToDefs,
730                                  const InstrToInstrs *DefsPerColorToUses,
731                                  AArch64FunctionInfo &AArch64FI,
732                                  SetOfMachineInstr *InvolvedInLOHs,
733                                  const MapRegToId &RegToId) {
734   // Look for opportunities to turn ADRP -> ADD or
735   // ADRP -> LDR GOTPAGEOFF into ADR.
736   // If ADRP has more than one use. Give up.
737   if (Use.getOpcode() != AArch64::ADDXri &&
738       (Use.getOpcode() != AArch64::LDRXui ||
739        !(Use.getOperand(2).getTargetFlags() & AArch64II::MO_GOT)))
740     return false;
741   InstrToInstrs::const_iterator It = UseToDefs.find(&Use);
742   // The map may contain garbage that we need to ignore.
743   if (It == UseToDefs.end() || It->second.empty())
744     return false;
745   const MachineInstr &Def = **It->second.begin();
746   if (Def.getOpcode() != AArch64::ADRP)
747     return false;
748   // Check the number of users of ADRP.
749   const SetOfMachineInstr *Users =
750       getUses(DefsPerColorToUses,
751               RegToId.find(Def.getOperand(0).getReg())->second, Def);
752   if (Users->size() > 1) {
753     ++NumADRComplexCandidate;
754     return false;
755   }
756   ++NumADRSimpleCandidate;
757   assert((!InvolvedInLOHs || InvolvedInLOHs->insert(&Def)) &&
758          "ADRP already involved in LOH.");
759   assert((!InvolvedInLOHs || InvolvedInLOHs->insert(&Use)) &&
760          "ADD already involved in LOH.");
761   DEBUG(dbgs() << "Record AdrpAdd\n" << Def << '\n' << Use << '\n');
762 
763   AArch64FI.addLOHDirective(
764       Use.getOpcode() == AArch64::ADDXri ? MCLOH_AdrpAdd : MCLOH_AdrpLdrGot,
765       {&Def, &Use});
766   return true;
767 }
768 
769 /// Based on the use to defs information (in non-ADRPMode), compute the
770 /// opportunities of LOH non-ADRP-related
771 static void computeOthers(const InstrToInstrs &UseToDefs,
772                           const InstrToInstrs *DefsPerColorToUses,
773                           AArch64FunctionInfo &AArch64FI, const MapRegToId &RegToId,
774                           const MachineDominatorTree *MDT) {
775   SetOfMachineInstr *InvolvedInLOHs = nullptr;
776 #ifndef NDEBUG
777   SetOfMachineInstr InvolvedInLOHsStorage;
778   InvolvedInLOHs = &InvolvedInLOHsStorage;
779 #endif // NDEBUG
780   DEBUG(dbgs() << "*** Compute LOH for Others\n");
781   // ADRP -> ADD/LDR -> LDR/STR pattern.
782   // Fall back to ADRP -> ADD pattern if we fail to catch the bigger pattern.
783 
784   // FIXME: When the statistics are not important,
785   // This initial filtering loop can be merged into the next loop.
786   // Currently, we didn't do it to have the same code for both DEBUG and
787   // NDEBUG builds. Indeed, the iterator of the second loop would need
788   // to be changed.
789   SetOfMachineInstr PotentialCandidates;
790   SetOfMachineInstr PotentialADROpportunities;
791   for (auto &Use : UseToDefs) {
792     // If no definition is available, this is a non candidate.
793     if (Use.second.empty())
794       continue;
795     // Keep only instructions that are load or store and at the end of
796     // a ADRP -> ADD/LDR/Nothing chain.
797     // We already filtered out the no-chain cases.
798     if (!isCandidate(Use.first, UseToDefs, MDT)) {
799       PotentialADROpportunities.insert(Use.first);
800       continue;
801     }
802     PotentialCandidates.insert(Use.first);
803   }
804 
805   // Make the following distinctions for statistics as the linker does
806   // know how to decode instructions:
807   // - ADD/LDR/Nothing make there different patterns.
808   // - LDR/STR make two different patterns.
809   // Hence, 6 - 1 base patterns.
810   // (because ADRP-> Nothing -> STR is not simplifiable)
811 
812   // The linker is only able to have a simple semantic, i.e., if pattern A
813   // do B.
814   // However, we want to see the opportunity we may miss if we were able to
815   // catch more complex cases.
816 
817   // PotentialCandidates are result of a chain ADRP -> ADD/LDR ->
818   // A potential candidate becomes a candidate, if its current immediate
819   // operand is zero and all nodes of the chain have respectively only one user
820 #ifndef NDEBUG
821   SetOfMachineInstr DefsOfPotentialCandidates;
822 #endif
823   for (const MachineInstr *Candidate : PotentialCandidates) {
824     // Get the definition of the candidate i.e., ADD or LDR.
825     const MachineInstr *Def = *UseToDefs.find(Candidate)->second.begin();
826     // Record the elements of the chain.
827     const MachineInstr *L1 = Def;
828     const MachineInstr *L2 = nullptr;
829     unsigned ImmediateDefOpc = Def->getOpcode();
830     if (Def->getOpcode() != AArch64::ADRP) {
831       // Check the number of users of this node.
832       const SetOfMachineInstr *Users =
833           getUses(DefsPerColorToUses,
834                   RegToId.find(Def->getOperand(0).getReg())->second, *Def);
835       if (Users->size() > 1) {
836 #ifndef NDEBUG
837         // if all the uses of this def are in potential candidate, this is
838         // a complex candidate of level 2.
839         bool IsLevel2 = true;
840         for (const MachineInstr *MI : *Users) {
841           if (!PotentialCandidates.count(MI)) {
842             ++NumTooCplxLvl2;
843             IsLevel2 = false;
844             break;
845           }
846         }
847         if (IsLevel2)
848           ++NumCplxLvl2;
849 #endif // NDEBUG
850         PotentialADROpportunities.insert(Def);
851         continue;
852       }
853       L2 = Def;
854       Def = *UseToDefs.find(Def)->second.begin();
855       L1 = Def;
856     } // else the element in the middle of the chain is nothing, thus
857       // Def already contains the first element of the chain.
858 
859     // Check the number of users of the first node in the chain, i.e., ADRP
860     const SetOfMachineInstr *Users =
861         getUses(DefsPerColorToUses,
862                 RegToId.find(Def->getOperand(0).getReg())->second, *Def);
863     if (Users->size() > 1) {
864 #ifndef NDEBUG
865       // if all the uses of this def are in the defs of the potential candidate,
866       // this is a complex candidate of level 1
867       if (DefsOfPotentialCandidates.empty()) {
868         // lazy init
869         DefsOfPotentialCandidates = PotentialCandidates;
870         for (const MachineInstr *Candidate : PotentialCandidates) {
871           if (!UseToDefs.find(Candidate)->second.empty())
872             DefsOfPotentialCandidates.insert(
873                 *UseToDefs.find(Candidate)->second.begin());
874         }
875       }
876       bool Found = false;
877       for (auto &Use : *Users) {
878         if (!DefsOfPotentialCandidates.count(Use)) {
879           ++NumTooCplxLvl1;
880           Found = true;
881           break;
882         }
883       }
884       if (!Found)
885         ++NumCplxLvl1;
886 #endif // NDEBUG
887       continue;
888     }
889 
890     bool IsL2Add = (ImmediateDefOpc == AArch64::ADDXri);
891     // If the chain is three instructions long and ldr is the second element,
892     // then this ldr must load form GOT, otherwise this is not a correct chain.
893     if (L2 && !IsL2Add &&
894         !(L2->getOperand(2).getTargetFlags() & AArch64II::MO_GOT))
895       continue;
896     SmallVector<const MachineInstr *, 3> Args;
897     MCLOHType Kind;
898     if (isCandidateLoad(Candidate)) {
899       if (!L2) {
900         // At this point, the candidate LOH indicates that the ldr instruction
901         // may use a direct access to the symbol. There is not such encoding
902         // for loads of byte and half.
903         if (!supportLoadFromLiteral(Candidate))
904           continue;
905 
906         DEBUG(dbgs() << "Record AdrpLdr:\n" << *L1 << '\n' << *Candidate
907                      << '\n');
908         Kind = MCLOH_AdrpLdr;
909         Args.push_back(L1);
910         Args.push_back(Candidate);
911         assert((!InvolvedInLOHs || InvolvedInLOHs->insert(L1)) &&
912                "L1 already involved in LOH.");
913         assert((!InvolvedInLOHs || InvolvedInLOHs->insert(Candidate)) &&
914                "Candidate already involved in LOH.");
915         ++NumADRPToLDR;
916       } else {
917         DEBUG(dbgs() << "Record Adrp" << (IsL2Add ? "Add" : "LdrGot")
918                      << "Ldr:\n" << *L1 << '\n' << *L2 << '\n' << *Candidate
919                      << '\n');
920 
921         Kind = IsL2Add ? MCLOH_AdrpAddLdr : MCLOH_AdrpLdrGotLdr;
922         Args.push_back(L1);
923         Args.push_back(L2);
924         Args.push_back(Candidate);
925 
926         PotentialADROpportunities.remove(L2);
927         assert((!InvolvedInLOHs || InvolvedInLOHs->insert(L1)) &&
928                "L1 already involved in LOH.");
929         assert((!InvolvedInLOHs || InvolvedInLOHs->insert(L2)) &&
930                "L2 already involved in LOH.");
931         assert((!InvolvedInLOHs || InvolvedInLOHs->insert(Candidate)) &&
932                "Candidate already involved in LOH.");
933 #ifndef NDEBUG
934         // get the immediate of the load
935         if (Candidate->getOperand(2).getImm() == 0)
936           if (ImmediateDefOpc == AArch64::ADDXri)
937             ++NumADDToLDR;
938           else
939             ++NumLDRToLDR;
940         else if (ImmediateDefOpc == AArch64::ADDXri)
941           ++NumADDToLDRWithImm;
942         else
943           ++NumLDRToLDRWithImm;
944 #endif // NDEBUG
945       }
946     } else {
947       if (ImmediateDefOpc == AArch64::ADRP)
948         continue;
949       else {
950 
951         DEBUG(dbgs() << "Record Adrp" << (IsL2Add ? "Add" : "LdrGot")
952                      << "Str:\n" << *L1 << '\n' << *L2 << '\n' << *Candidate
953                      << '\n');
954 
955         Kind = IsL2Add ? MCLOH_AdrpAddStr : MCLOH_AdrpLdrGotStr;
956         Args.push_back(L1);
957         Args.push_back(L2);
958         Args.push_back(Candidate);
959 
960         PotentialADROpportunities.remove(L2);
961         assert((!InvolvedInLOHs || InvolvedInLOHs->insert(L1)) &&
962                "L1 already involved in LOH.");
963         assert((!InvolvedInLOHs || InvolvedInLOHs->insert(L2)) &&
964                "L2 already involved in LOH.");
965         assert((!InvolvedInLOHs || InvolvedInLOHs->insert(Candidate)) &&
966                "Candidate already involved in LOH.");
967 #ifndef NDEBUG
968         // get the immediate of the store
969         if (Candidate->getOperand(2).getImm() == 0)
970           if (ImmediateDefOpc == AArch64::ADDXri)
971             ++NumADDToSTR;
972           else
973             ++NumLDRToSTR;
974         else if (ImmediateDefOpc == AArch64::ADDXri)
975           ++NumADDToSTRWithImm;
976         else
977           ++NumLDRToSTRWithImm;
978 #endif // DEBUG
979       }
980     }
981     AArch64FI.addLOHDirective(Kind, Args);
982   }
983 
984   // Now, we grabbed all the big patterns, check ADR opportunities.
985   for (const MachineInstr *Candidate : PotentialADROpportunities)
986     registerADRCandidate(*Candidate, UseToDefs, DefsPerColorToUses, AArch64FI,
987                          InvolvedInLOHs, RegToId);
988 }
989 
990 /// Look for every register defined by potential LOHs candidates.
991 /// Map these registers with dense id in @p RegToId and vice-versa in
992 /// @p IdToReg. @p IdToReg is populated only in DEBUG mode.
993 static void collectInvolvedReg(const MachineFunction &MF, MapRegToId &RegToId,
994                                MapIdToReg &IdToReg,
995                                const TargetRegisterInfo *TRI) {
996   unsigned CurRegId = 0;
997   if (!PreCollectRegister) {
998     unsigned NbReg = TRI->getNumRegs();
999     for (; CurRegId < NbReg; ++CurRegId) {
1000       RegToId[CurRegId] = CurRegId;
1001       DEBUG(IdToReg.push_back(CurRegId));
1002       DEBUG(assert(IdToReg[CurRegId] == CurRegId && "Reg index mismatches"));
1003     }
1004     return;
1005   }
1006 
1007   DEBUG(dbgs() << "** Collect Involved Register\n");
1008   for (const auto &MBB : MF) {
1009     for (const MachineInstr &MI : MBB) {
1010       if (!canDefBePartOfLOH(&MI) &&
1011           !isCandidateLoad(&MI) && !isCandidateStore(&MI))
1012         continue;
1013 
1014       // Process defs
1015       for (MachineInstr::const_mop_iterator IO = MI.operands_begin(),
1016                                             IOEnd = MI.operands_end();
1017            IO != IOEnd; ++IO) {
1018         if (!IO->isReg() || !IO->isDef())
1019           continue;
1020         unsigned CurReg = IO->getReg();
1021         for (MCRegAliasIterator AI(CurReg, TRI, true); AI.isValid(); ++AI)
1022           if (RegToId.find(*AI) == RegToId.end()) {
1023             DEBUG(IdToReg.push_back(*AI);
1024                   assert(IdToReg[CurRegId] == *AI &&
1025                          "Reg index mismatches insertion index."));
1026             RegToId[*AI] = CurRegId++;
1027             DEBUG(dbgs() << "Register: " << PrintReg(*AI, TRI) << '\n');
1028           }
1029       }
1030     }
1031   }
1032 }
1033 
1034 bool AArch64CollectLOH::runOnMachineFunction(MachineFunction &MF) {
1035   if (skipFunction(*MF.getFunction()))
1036     return false;
1037 
1038   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1039   const MachineDominatorTree *MDT = &getAnalysis<MachineDominatorTree>();
1040 
1041   MapRegToId RegToId;
1042   MapIdToReg IdToReg;
1043   AArch64FunctionInfo *AArch64FI = MF.getInfo<AArch64FunctionInfo>();
1044   assert(AArch64FI && "No MachineFunctionInfo for this function!");
1045 
1046   DEBUG(dbgs() << "Looking for LOH in " << MF.getName() << '\n');
1047 
1048   collectInvolvedReg(MF, RegToId, IdToReg, TRI);
1049   if (RegToId.empty())
1050     return false;
1051 
1052   MachineInstr *DummyOp = nullptr;
1053   if (BasicBlockScopeOnly) {
1054     const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
1055     // For local analysis, create a dummy operation to record uses that are not
1056     // local.
1057     DummyOp = MF.CreateMachineInstr(TII->get(AArch64::COPY), DebugLoc());
1058   }
1059 
1060   unsigned NbReg = RegToId.size();
1061   bool Modified = false;
1062 
1063   // Start with ADRP.
1064   InstrToInstrs *ColorOpToReachedUses = new InstrToInstrs[NbReg];
1065 
1066   // Compute the reaching def in ADRP mode, meaning ADRP definitions
1067   // are first considered as uses.
1068   reachingDef(MF, ColorOpToReachedUses, RegToId, true, DummyOp);
1069   DEBUG(dbgs() << "ADRP reaching defs\n");
1070   DEBUG(printReachingDef(ColorOpToReachedUses, NbReg, TRI, IdToReg));
1071 
1072   // Translate the definition to uses map into a use to definitions map to ease
1073   // statistic computation.
1074   InstrToInstrs ADRPToReachingDefs;
1075   reachedUsesToDefs(ADRPToReachingDefs, ColorOpToReachedUses, RegToId, true);
1076 
1077   // Compute LOH for ADRP.
1078   computeADRP(ADRPToReachingDefs, *AArch64FI, MDT);
1079   delete[] ColorOpToReachedUses;
1080 
1081   // Continue with general ADRP -> ADD/LDR -> LDR/STR pattern.
1082   ColorOpToReachedUses = new InstrToInstrs[NbReg];
1083 
1084   // first perform a regular reaching def analysis.
1085   reachingDef(MF, ColorOpToReachedUses, RegToId, false, DummyOp);
1086   DEBUG(dbgs() << "All reaching defs\n");
1087   DEBUG(printReachingDef(ColorOpToReachedUses, NbReg, TRI, IdToReg));
1088 
1089   // Turn that into a use to defs to ease statistic computation.
1090   InstrToInstrs UsesToReachingDefs;
1091   reachedUsesToDefs(UsesToReachingDefs, ColorOpToReachedUses, RegToId, false);
1092 
1093   // Compute other than AdrpAdrp LOH.
1094   computeOthers(UsesToReachingDefs, ColorOpToReachedUses, *AArch64FI, RegToId,
1095                 MDT);
1096   delete[] ColorOpToReachedUses;
1097 
1098   if (BasicBlockScopeOnly)
1099     MF.DeleteMachineInstr(DummyOp);
1100 
1101   return Modified;
1102 }
1103 
1104 /// createAArch64CollectLOHPass - returns an instance of the Statistic for
1105 /// linker optimization pass.
1106 FunctionPass *llvm::createAArch64CollectLOHPass() {
1107   return new AArch64CollectLOH();
1108 }
1109