LLVM  5.0.0
AArch64FalkorHWPFFix.cpp
Go to the documentation of this file.
1 //===-- AArch64FalkorHWPFFix.cpp - Avoid HW prefetcher pitfalls on Falkor--===//
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 /// \file For Falkor, we want to avoid HW prefetcher instruction tag collisions
10 /// that may inhibit the HW prefetching. This is done in two steps. Before
11 /// ISel, we mark strided loads (i.e. those that will likely benefit from
12 /// prefetching) with metadata. Then, after opcodes have been finalized, we
13 /// insert MOVs and re-write loads to prevent unintnentional tag collisions.
14 // ===---------------------------------------------------------------------===//
15 
16 #include "AArch64.h"
17 #include "AArch64InstrInfo.h"
18 #include "AArch64TargetMachine.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/LoopInfo.h"
28 #include "llvm/IR/Dominators.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/Module.h"
32 #include "llvm/Support/Debug.h"
33 
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "falkor-hwpf-fix"
37 
38 STATISTIC(NumStridedLoadsMarked, "Number of strided loads marked");
39 STATISTIC(NumCollisionsAvoided,
40  "Number of HW prefetch tag collisions avoided");
41 STATISTIC(NumCollisionsNotAvoided,
42  "Number of HW prefetch tag collisions not avoided due to lack of regsiters");
43 
44 namespace {
45 
46 class FalkorMarkStridedAccesses {
47 public:
48  FalkorMarkStridedAccesses(LoopInfo &LI, ScalarEvolution &SE)
49  : LI(LI), SE(SE) {}
50 
51  bool run();
52 
53 private:
54  bool runOnLoop(Loop &L);
55 
56  LoopInfo &LI;
57  ScalarEvolution &SE;
58 };
59 
60 class FalkorMarkStridedAccessesLegacy : public FunctionPass {
61 public:
62  static char ID; // Pass ID, replacement for typeid
63  FalkorMarkStridedAccessesLegacy() : FunctionPass(ID) {
66  }
67 
68  void getAnalysisUsage(AnalysisUsage &AU) const override {
74  // FIXME: For some reason, preserving SE here breaks LSR (even if
75  // this pass changes nothing).
76  // AU.addPreserved<ScalarEvolutionWrapperPass>();
77  }
78 
79  bool runOnFunction(Function &F) override;
80 };
81 } // namespace
82 
84 INITIALIZE_PASS_BEGIN(FalkorMarkStridedAccessesLegacy, DEBUG_TYPE,
85  "Falkor HW Prefetch Fix", false, false)
89 INITIALIZE_PASS_END(FalkorMarkStridedAccessesLegacy, DEBUG_TYPE,
90  "Falkor HW Prefetch Fix", false, false)
91 
93  return new FalkorMarkStridedAccessesLegacy();
94 }
95 
96 bool FalkorMarkStridedAccessesLegacy::runOnFunction(Function &F) {
97  TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
98  const AArch64Subtarget *ST =
99  TPC.getTM<AArch64TargetMachine>().getSubtargetImpl(F);
100  if (ST->getProcFamily() != AArch64Subtarget::Falkor)
101  return false;
102 
103  if (skipFunction(F))
104  return false;
105 
106  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
107  ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
108 
109  FalkorMarkStridedAccesses LDP(LI, SE);
110  return LDP.run();
111 }
112 
113 bool FalkorMarkStridedAccesses::run() {
114  bool MadeChange = false;
115 
116  for (Loop *L : LI)
117  for (auto LIt = df_begin(L), LE = df_end(L); LIt != LE; ++LIt)
118  MadeChange |= runOnLoop(**LIt);
119 
120  return MadeChange;
121 }
122 
123 bool FalkorMarkStridedAccesses::runOnLoop(Loop &L) {
124  // Only mark strided loads in the inner-most loop
125  if (!L.empty())
126  return false;
127 
128  bool MadeChange = false;
129 
130  for (BasicBlock *BB : L.blocks()) {
131  for (Instruction &I : *BB) {
132  LoadInst *LoadI = dyn_cast<LoadInst>(&I);
133  if (!LoadI)
134  continue;
135 
136  Value *PtrValue = LoadI->getPointerOperand();
137  if (L.isLoopInvariant(PtrValue))
138  continue;
139 
140  const SCEV *LSCEV = SE.getSCEV(PtrValue);
141  const SCEVAddRecExpr *LSCEVAddRec = dyn_cast<SCEVAddRecExpr>(LSCEV);
142  if (!LSCEVAddRec || !LSCEVAddRec->isAffine())
143  continue;
144 
146  MDNode::get(LoadI->getContext(), {}));
147  ++NumStridedLoadsMarked;
148  DEBUG(dbgs() << "Load: " << I << " marked as strided\n");
149  MadeChange = true;
150  }
151  }
152 
153  return MadeChange;
154 }
155 
156 namespace {
157 
158 class FalkorHWPFFix : public MachineFunctionPass {
159 public:
160  static char ID;
161 
162  FalkorHWPFFix() : MachineFunctionPass(ID) {
164  }
165 
166  bool runOnMachineFunction(MachineFunction &Fn) override;
167 
168  virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
171  }
172 
173  MachineFunctionProperties getRequiredProperties() const override {
176  }
177 
178 private:
179  void runOnLoop(MachineLoop &L, MachineFunction &Fn);
180 
181  const AArch64InstrInfo *TII;
182  const TargetRegisterInfo *TRI;
184  bool Modified;
185 };
186 
187 /// Bits from load opcodes used to compute HW prefetcher instruction tags.
188 struct LoadInfo {
189  LoadInfo()
190  : DestReg(0), BaseReg(0), BaseRegIdx(-1), OffsetOpnd(nullptr),
191  IsPrePost(false) {}
192  unsigned DestReg;
193  unsigned BaseReg;
194  int BaseRegIdx;
195  const MachineOperand *OffsetOpnd;
196  bool IsPrePost;
197 };
198 
199 } // namespace
200 
201 char FalkorHWPFFix::ID = 0;
202 
203 INITIALIZE_PASS_BEGIN(FalkorHWPFFix, "falkor-hwpf-fix-late",
204  "Falkor HW Prefetch Fix Late Phase", false, false)
206 INITIALIZE_PASS_END(FalkorHWPFFix, "falkor-hwpf-fix-late",
207  "Falkor HW Prefetch Fix Late Phase", false, false)
208 
209 static unsigned makeTag(unsigned Dest, unsigned Base, unsigned Offset) {
210  return (Dest & 0xf) | ((Base & 0xf) << 4) | ((Offset & 0x3f) << 8);
211 }
212 
214  int DestRegIdx;
215  int BaseRegIdx;
216  int OffsetIdx;
217  bool IsPrePost;
218 
219  switch (MI.getOpcode()) {
220  default:
221  return None;
222 
223  case AArch64::LD1i8:
224  case AArch64::LD1i16:
225  case AArch64::LD1i32:
226  case AArch64::LD1i64:
227  case AArch64::LD2i8:
228  case AArch64::LD2i16:
229  case AArch64::LD2i32:
230  case AArch64::LD2i64:
231  case AArch64::LD3i8:
232  case AArch64::LD3i16:
233  case AArch64::LD3i32:
234  case AArch64::LD4i8:
235  case AArch64::LD4i16:
236  case AArch64::LD4i32:
237  DestRegIdx = 0;
238  BaseRegIdx = 3;
239  OffsetIdx = -1;
240  IsPrePost = false;
241  break;
242 
243  case AArch64::LD3i64:
244  case AArch64::LD4i64:
245  DestRegIdx = -1;
246  BaseRegIdx = 3;
247  OffsetIdx = -1;
248  IsPrePost = false;
249  break;
250 
251  case AArch64::LD1Onev1d:
252  case AArch64::LD1Onev2s:
253  case AArch64::LD1Onev4h:
254  case AArch64::LD1Onev8b:
255  case AArch64::LD1Onev2d:
256  case AArch64::LD1Onev4s:
257  case AArch64::LD1Onev8h:
258  case AArch64::LD1Onev16b:
259  case AArch64::LD1Rv1d:
260  case AArch64::LD1Rv2s:
261  case AArch64::LD1Rv4h:
262  case AArch64::LD1Rv8b:
263  case AArch64::LD1Rv2d:
264  case AArch64::LD1Rv4s:
265  case AArch64::LD1Rv8h:
266  case AArch64::LD1Rv16b:
267  case AArch64::LD1Twov1d:
268  case AArch64::LD1Twov2s:
269  case AArch64::LD1Twov4h:
270  case AArch64::LD1Twov8b:
271  case AArch64::LD2Twov2s:
272  case AArch64::LD2Twov4s:
273  case AArch64::LD2Twov8b:
274  case AArch64::LD2Rv1d:
275  case AArch64::LD2Rv2s:
276  case AArch64::LD2Rv4s:
277  case AArch64::LD2Rv8b:
278  DestRegIdx = 0;
279  BaseRegIdx = 1;
280  OffsetIdx = -1;
281  IsPrePost = false;
282  break;
283 
284  case AArch64::LD1Twov2d:
285  case AArch64::LD1Twov4s:
286  case AArch64::LD1Twov8h:
287  case AArch64::LD1Twov16b:
288  case AArch64::LD1Threev1d:
289  case AArch64::LD1Threev2s:
290  case AArch64::LD1Threev4h:
291  case AArch64::LD1Threev8b:
292  case AArch64::LD1Threev2d:
293  case AArch64::LD1Threev4s:
294  case AArch64::LD1Threev8h:
295  case AArch64::LD1Threev16b:
296  case AArch64::LD1Fourv1d:
297  case AArch64::LD1Fourv2s:
298  case AArch64::LD1Fourv4h:
299  case AArch64::LD1Fourv8b:
300  case AArch64::LD1Fourv2d:
301  case AArch64::LD1Fourv4s:
302  case AArch64::LD1Fourv8h:
303  case AArch64::LD1Fourv16b:
304  case AArch64::LD2Twov2d:
305  case AArch64::LD2Twov4h:
306  case AArch64::LD2Twov8h:
307  case AArch64::LD2Twov16b:
308  case AArch64::LD2Rv2d:
309  case AArch64::LD2Rv4h:
310  case AArch64::LD2Rv8h:
311  case AArch64::LD2Rv16b:
312  case AArch64::LD3Threev2s:
313  case AArch64::LD3Threev4h:
314  case AArch64::LD3Threev8b:
315  case AArch64::LD3Threev2d:
316  case AArch64::LD3Threev4s:
317  case AArch64::LD3Threev8h:
318  case AArch64::LD3Threev16b:
319  case AArch64::LD3Rv1d:
320  case AArch64::LD3Rv2s:
321  case AArch64::LD3Rv4h:
322  case AArch64::LD3Rv8b:
323  case AArch64::LD3Rv2d:
324  case AArch64::LD3Rv4s:
325  case AArch64::LD3Rv8h:
326  case AArch64::LD3Rv16b:
327  case AArch64::LD4Fourv2s:
328  case AArch64::LD4Fourv4h:
329  case AArch64::LD4Fourv8b:
330  case AArch64::LD4Fourv2d:
331  case AArch64::LD4Fourv4s:
332  case AArch64::LD4Fourv8h:
333  case AArch64::LD4Fourv16b:
334  case AArch64::LD4Rv1d:
335  case AArch64::LD4Rv2s:
336  case AArch64::LD4Rv4h:
337  case AArch64::LD4Rv8b:
338  case AArch64::LD4Rv2d:
339  case AArch64::LD4Rv4s:
340  case AArch64::LD4Rv8h:
341  case AArch64::LD4Rv16b:
342  DestRegIdx = -1;
343  BaseRegIdx = 1;
344  OffsetIdx = -1;
345  IsPrePost = false;
346  break;
347 
348  case AArch64::LD1i8_POST:
349  case AArch64::LD1i16_POST:
350  case AArch64::LD1i32_POST:
351  case AArch64::LD1i64_POST:
352  case AArch64::LD2i8_POST:
353  case AArch64::LD2i16_POST:
354  case AArch64::LD2i32_POST:
355  case AArch64::LD2i64_POST:
356  case AArch64::LD3i8_POST:
357  case AArch64::LD3i16_POST:
358  case AArch64::LD3i32_POST:
359  case AArch64::LD4i8_POST:
360  case AArch64::LD4i16_POST:
361  case AArch64::LD4i32_POST:
362  DestRegIdx = 1;
363  BaseRegIdx = 4;
364  OffsetIdx = 5;
365  IsPrePost = false;
366  break;
367 
368  case AArch64::LD3i64_POST:
369  case AArch64::LD4i64_POST:
370  DestRegIdx = -1;
371  BaseRegIdx = 4;
372  OffsetIdx = 5;
373  IsPrePost = false;
374  break;
375 
376  case AArch64::LD1Onev1d_POST:
377  case AArch64::LD1Onev2s_POST:
378  case AArch64::LD1Onev4h_POST:
379  case AArch64::LD1Onev8b_POST:
380  case AArch64::LD1Onev2d_POST:
381  case AArch64::LD1Onev4s_POST:
382  case AArch64::LD1Onev8h_POST:
383  case AArch64::LD1Onev16b_POST:
384  case AArch64::LD1Rv1d_POST:
385  case AArch64::LD1Rv2s_POST:
386  case AArch64::LD1Rv4h_POST:
387  case AArch64::LD1Rv8b_POST:
388  case AArch64::LD1Rv2d_POST:
389  case AArch64::LD1Rv4s_POST:
390  case AArch64::LD1Rv8h_POST:
391  case AArch64::LD1Rv16b_POST:
392  case AArch64::LD1Twov1d_POST:
393  case AArch64::LD1Twov2s_POST:
394  case AArch64::LD1Twov4h_POST:
395  case AArch64::LD1Twov8b_POST:
396  case AArch64::LD2Twov2s_POST:
397  case AArch64::LD2Twov4s_POST:
398  case AArch64::LD2Twov8b_POST:
399  case AArch64::LD2Rv1d_POST:
400  case AArch64::LD2Rv2s_POST:
401  case AArch64::LD2Rv4s_POST:
402  case AArch64::LD2Rv8b_POST:
403  DestRegIdx = 1;
404  BaseRegIdx = 2;
405  OffsetIdx = 3;
406  IsPrePost = false;
407  break;
408 
409  case AArch64::LD1Twov2d_POST:
410  case AArch64::LD1Twov4s_POST:
411  case AArch64::LD1Twov8h_POST:
412  case AArch64::LD1Twov16b_POST:
413  case AArch64::LD1Threev1d_POST:
414  case AArch64::LD1Threev2s_POST:
415  case AArch64::LD1Threev4h_POST:
416  case AArch64::LD1Threev8b_POST:
417  case AArch64::LD1Threev2d_POST:
418  case AArch64::LD1Threev4s_POST:
419  case AArch64::LD1Threev8h_POST:
420  case AArch64::LD1Threev16b_POST:
421  case AArch64::LD1Fourv1d_POST:
422  case AArch64::LD1Fourv2s_POST:
423  case AArch64::LD1Fourv4h_POST:
424  case AArch64::LD1Fourv8b_POST:
425  case AArch64::LD1Fourv2d_POST:
426  case AArch64::LD1Fourv4s_POST:
427  case AArch64::LD1Fourv8h_POST:
428  case AArch64::LD1Fourv16b_POST:
429  case AArch64::LD2Twov2d_POST:
430  case AArch64::LD2Twov4h_POST:
431  case AArch64::LD2Twov8h_POST:
432  case AArch64::LD2Twov16b_POST:
433  case AArch64::LD2Rv2d_POST:
434  case AArch64::LD2Rv4h_POST:
435  case AArch64::LD2Rv8h_POST:
436  case AArch64::LD2Rv16b_POST:
437  case AArch64::LD3Threev2s_POST:
438  case AArch64::LD3Threev4h_POST:
439  case AArch64::LD3Threev8b_POST:
440  case AArch64::LD3Threev2d_POST:
441  case AArch64::LD3Threev4s_POST:
442  case AArch64::LD3Threev8h_POST:
443  case AArch64::LD3Threev16b_POST:
444  case AArch64::LD3Rv1d_POST:
445  case AArch64::LD3Rv2s_POST:
446  case AArch64::LD3Rv4h_POST:
447  case AArch64::LD3Rv8b_POST:
448  case AArch64::LD3Rv2d_POST:
449  case AArch64::LD3Rv4s_POST:
450  case AArch64::LD3Rv8h_POST:
451  case AArch64::LD3Rv16b_POST:
452  case AArch64::LD4Fourv2s_POST:
453  case AArch64::LD4Fourv4h_POST:
454  case AArch64::LD4Fourv8b_POST:
455  case AArch64::LD4Fourv2d_POST:
456  case AArch64::LD4Fourv4s_POST:
457  case AArch64::LD4Fourv8h_POST:
458  case AArch64::LD4Fourv16b_POST:
459  case AArch64::LD4Rv1d_POST:
460  case AArch64::LD4Rv2s_POST:
461  case AArch64::LD4Rv4h_POST:
462  case AArch64::LD4Rv8b_POST:
463  case AArch64::LD4Rv2d_POST:
464  case AArch64::LD4Rv4s_POST:
465  case AArch64::LD4Rv8h_POST:
466  case AArch64::LD4Rv16b_POST:
467  DestRegIdx = -1;
468  BaseRegIdx = 2;
469  OffsetIdx = 3;
470  IsPrePost = false;
471  break;
472 
473  case AArch64::LDRBBroW:
474  case AArch64::LDRBBroX:
475  case AArch64::LDRBBui:
476  case AArch64::LDRBroW:
477  case AArch64::LDRBroX:
478  case AArch64::LDRBui:
479  case AArch64::LDRDl:
480  case AArch64::LDRDroW:
481  case AArch64::LDRDroX:
482  case AArch64::LDRDui:
483  case AArch64::LDRHHroW:
484  case AArch64::LDRHHroX:
485  case AArch64::LDRHHui:
486  case AArch64::LDRHroW:
487  case AArch64::LDRHroX:
488  case AArch64::LDRHui:
489  case AArch64::LDRQl:
490  case AArch64::LDRQroW:
491  case AArch64::LDRQroX:
492  case AArch64::LDRQui:
493  case AArch64::LDRSBWroW:
494  case AArch64::LDRSBWroX:
495  case AArch64::LDRSBWui:
496  case AArch64::LDRSBXroW:
497  case AArch64::LDRSBXroX:
498  case AArch64::LDRSBXui:
499  case AArch64::LDRSHWroW:
500  case AArch64::LDRSHWroX:
501  case AArch64::LDRSHWui:
502  case AArch64::LDRSHXroW:
503  case AArch64::LDRSHXroX:
504  case AArch64::LDRSHXui:
505  case AArch64::LDRSWl:
506  case AArch64::LDRSWroW:
507  case AArch64::LDRSWroX:
508  case AArch64::LDRSWui:
509  case AArch64::LDRSl:
510  case AArch64::LDRSroW:
511  case AArch64::LDRSroX:
512  case AArch64::LDRSui:
513  case AArch64::LDRWl:
514  case AArch64::LDRWroW:
515  case AArch64::LDRWroX:
516  case AArch64::LDRWui:
517  case AArch64::LDRXl:
518  case AArch64::LDRXroW:
519  case AArch64::LDRXroX:
520  case AArch64::LDRXui:
521  case AArch64::LDURBBi:
522  case AArch64::LDURBi:
523  case AArch64::LDURDi:
524  case AArch64::LDURHHi:
525  case AArch64::LDURHi:
526  case AArch64::LDURQi:
527  case AArch64::LDURSBWi:
528  case AArch64::LDURSBXi:
529  case AArch64::LDURSHWi:
530  case AArch64::LDURSHXi:
531  case AArch64::LDURSWi:
532  case AArch64::LDURSi:
533  case AArch64::LDURWi:
534  case AArch64::LDURXi:
535  DestRegIdx = 0;
536  BaseRegIdx = 1;
537  OffsetIdx = 2;
538  IsPrePost = false;
539  break;
540 
541  case AArch64::LDRBBpost:
542  case AArch64::LDRBBpre:
543  case AArch64::LDRBpost:
544  case AArch64::LDRBpre:
545  case AArch64::LDRDpost:
546  case AArch64::LDRDpre:
547  case AArch64::LDRHHpost:
548  case AArch64::LDRHHpre:
549  case AArch64::LDRHpost:
550  case AArch64::LDRHpre:
551  case AArch64::LDRQpost:
552  case AArch64::LDRQpre:
553  case AArch64::LDRSBWpost:
554  case AArch64::LDRSBWpre:
555  case AArch64::LDRSBXpost:
556  case AArch64::LDRSBXpre:
557  case AArch64::LDRSHWpost:
558  case AArch64::LDRSHWpre:
559  case AArch64::LDRSHXpost:
560  case AArch64::LDRSHXpre:
561  case AArch64::LDRSWpost:
562  case AArch64::LDRSWpre:
563  case AArch64::LDRSpost:
564  case AArch64::LDRSpre:
565  case AArch64::LDRWpost:
566  case AArch64::LDRWpre:
567  case AArch64::LDRXpost:
568  case AArch64::LDRXpre:
569  DestRegIdx = 1;
570  BaseRegIdx = 2;
571  OffsetIdx = 3;
572  IsPrePost = true;
573  break;
574 
575  case AArch64::LDPDi:
576  case AArch64::LDPQi:
577  DestRegIdx = -1;
578  BaseRegIdx = 2;
579  OffsetIdx = 3;
580  IsPrePost = false;
581  break;
582 
583  case AArch64::LDPSWi:
584  case AArch64::LDPSi:
585  case AArch64::LDPWi:
586  case AArch64::LDPXi:
587  DestRegIdx = 0;
588  BaseRegIdx = 2;
589  OffsetIdx = 3;
590  IsPrePost = false;
591  break;
592 
593  case AArch64::LDPQpost:
594  case AArch64::LDPQpre:
595  DestRegIdx = -1;
596  BaseRegIdx = 3;
597  OffsetIdx = 4;
598  IsPrePost = true;
599  break;
600 
601  case AArch64::LDPDpost:
602  case AArch64::LDPDpre:
603  case AArch64::LDPSWpost:
604  case AArch64::LDPSWpre:
605  case AArch64::LDPSpost:
606  case AArch64::LDPSpre:
607  case AArch64::LDPWpost:
608  case AArch64::LDPWpre:
609  case AArch64::LDPXpost:
610  case AArch64::LDPXpre:
611  DestRegIdx = 1;
612  BaseRegIdx = 3;
613  OffsetIdx = 4;
614  IsPrePost = true;
615  break;
616  }
617 
618  LoadInfo LI;
619  LI.DestReg = DestRegIdx == -1 ? 0 : MI.getOperand(DestRegIdx).getReg();
620  LI.BaseReg = MI.getOperand(BaseRegIdx).getReg();
621  LI.BaseRegIdx = BaseRegIdx;
622  LI.OffsetOpnd = OffsetIdx == -1 ? nullptr : &MI.getOperand(OffsetIdx);
623  LI.IsPrePost = IsPrePost;
624  return LI;
625 }
626 
628  const MachineInstr &MI, const LoadInfo &LI) {
629  unsigned Dest = LI.DestReg ? TRI->getEncodingValue(LI.DestReg) : 0;
630  unsigned Base = TRI->getEncodingValue(LI.BaseReg);
631  unsigned Off;
632  if (LI.OffsetOpnd == nullptr)
633  Off = 0;
634  else if (LI.OffsetOpnd->isGlobal() || LI.OffsetOpnd->isSymbol() ||
635  LI.OffsetOpnd->isCPI())
636  return None;
637  else if (LI.OffsetOpnd->isReg())
638  Off = (1 << 5) | TRI->getEncodingValue(LI.OffsetOpnd->getReg());
639  else
640  Off = LI.OffsetOpnd->getImm() >> 2;
641 
642  return makeTag(Dest, Base, Off);
643 }
644 
645 void FalkorHWPFFix::runOnLoop(MachineLoop &L, MachineFunction &Fn) {
646  // Build the initial tag map for the whole loop.
647  TagMap.clear();
648  for (MachineBasicBlock *MBB : L.getBlocks())
649  for (MachineInstr &MI : *MBB) {
651  if (!LInfo)
652  continue;
653  Optional<unsigned> Tag = getTag(TRI, MI, *LInfo);
654  if (!Tag)
655  continue;
656  TagMap[*Tag].push_back(&MI);
657  }
658 
659  bool AnyCollisions = false;
660  for (auto &P : TagMap) {
661  auto Size = P.second.size();
662  if (Size > 1) {
663  for (auto *MI : P.second) {
664  if (TII->isStridedAccess(*MI)) {
665  AnyCollisions = true;
666  break;
667  }
668  }
669  }
670  if (AnyCollisions)
671  break;
672  }
673  // Nothing to fix.
674  if (!AnyCollisions)
675  return;
676 
678 
679  // Go through all the basic blocks in the current loop and fix any streaming
680  // loads to avoid collisions with any other loads.
681  LiveRegUnits LR(*TRI);
682  for (MachineBasicBlock *MBB : L.getBlocks()) {
683  LR.clear();
684  LR.addLiveOuts(*MBB);
685  for (auto I = MBB->rbegin(); I != MBB->rend(); LR.stepBackward(*I), ++I) {
686  MachineInstr &MI = *I;
687  if (!TII->isStridedAccess(MI))
688  continue;
689 
690  LoadInfo LdI = *getLoadInfo(MI);
691  unsigned OldTag = *getTag(TRI, MI, LdI);
692  auto &OldCollisions = TagMap[OldTag];
693  if (OldCollisions.size() <= 1)
694  continue;
695 
696  bool Fixed = false;
697  DEBUG(dbgs() << "Attempting to fix tag collision: " << MI);
698 
699  for (unsigned ScratchReg : AArch64::GPR64RegClass) {
700  if (!LR.available(ScratchReg) || MRI.isReserved(ScratchReg))
701  continue;
702 
703  LoadInfo NewLdI(LdI);
704  NewLdI.BaseReg = ScratchReg;
705  unsigned NewTag = *getTag(TRI, MI, NewLdI);
706  // Scratch reg tag would collide too, so don't use it.
707  if (TagMap.count(NewTag))
708  continue;
709 
710  DEBUG(dbgs() << "Changing base reg to: " << PrintReg(ScratchReg, TRI)
711  << '\n');
712 
713  // Rewrite:
714  // Xd = LOAD Xb, off
715  // to:
716  // Xc = MOV Xb
717  // Xd = LOAD Xc, off
718  DebugLoc DL = MI.getDebugLoc();
719  BuildMI(*MBB, &MI, DL, TII->get(AArch64::ORRXrs), ScratchReg)
720  .addReg(AArch64::XZR)
721  .addReg(LdI.BaseReg)
722  .addImm(0);
723  MachineOperand &BaseOpnd = MI.getOperand(LdI.BaseRegIdx);
724  BaseOpnd.setReg(ScratchReg);
725 
726  // If the load does a pre/post increment, then insert a MOV after as
727  // well to update the real base register.
728  if (LdI.IsPrePost) {
729  DEBUG(dbgs() << "Doing post MOV of incremented reg: "
730  << PrintReg(ScratchReg, TRI) << '\n');
731  MI.getOperand(0).setReg(
732  ScratchReg); // Change tied operand pre/post update dest.
733  BuildMI(*MBB, std::next(MachineBasicBlock::iterator(MI)), DL,
734  TII->get(AArch64::ORRXrs), LdI.BaseReg)
735  .addReg(AArch64::XZR)
736  .addReg(ScratchReg)
737  .addImm(0);
738  }
739 
740  for (int I = 0, E = OldCollisions.size(); I != E; ++I)
741  if (OldCollisions[I] == &MI) {
742  std::swap(OldCollisions[I], OldCollisions[E - 1]);
743  OldCollisions.pop_back();
744  break;
745  }
746 
747  // Update TagMap to reflect instruction changes to reduce the number
748  // of later MOVs to be inserted. This needs to be done after
749  // OldCollisions is updated since it may be relocated by this
750  // insertion.
751  TagMap[NewTag].push_back(&MI);
752  ++NumCollisionsAvoided;
753  Fixed = true;
754  Modified = true;
755  break;
756  }
757  if (!Fixed)
758  ++NumCollisionsNotAvoided;
759  }
760  }
761 }
762 
763 bool FalkorHWPFFix::runOnMachineFunction(MachineFunction &Fn) {
764  auto &ST = static_cast<const AArch64Subtarget &>(Fn.getSubtarget());
765  if (ST.getProcFamily() != AArch64Subtarget::Falkor)
766  return false;
767 
768  if (skipFunction(*Fn.getFunction()))
769  return false;
770 
771  TII = static_cast<const AArch64InstrInfo *>(ST.getInstrInfo());
772  TRI = ST.getRegisterInfo();
773 
774  assert(TRI->trackLivenessAfterRegAlloc(Fn) &&
775  "Register liveness not available!");
776 
777  MachineLoopInfo &LI = getAnalysis<MachineLoopInfo>();
778 
779  Modified = false;
780 
781  for (MachineLoop *I : LI)
782  for (auto L = df_begin(I), LE = df_end(I); L != LE; ++L)
783  // Only process inner-loops
784  if (L->empty())
785  runOnLoop(**L, Fn);
786 
787  return Modified;
788 }
789 
790 FunctionPass *llvm::createFalkorHWPFFixPass() { return new FalkorHWPFFix(); }
MachineLoop * L
const NoneType None
Definition: None.h:24
const AArch64RegisterInfo & getRegisterInfo() const
getRegisterInfo - TargetInstrInfo is a superset of MRegister info.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
FunctionPass * createFalkorMarkStridedAccessesPass()
The main scalar evolution driver.
DataLayout DL
static Optional< LoadInfo > getLoadInfo(const MachineInstr &MI)
A debug info location.
Definition: DebugLoc.h:34
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
An instruction for reading from memory.
Definition: Instructions.h:164
const std::vector< BlockT * > & getBlocks() const
Get a list of the basic blocks which make up this loop.
Definition: LoopInfo.h:140
INITIALIZE_PASS_BEGIN(FalkorMarkStridedAccessesLegacy, DEBUG_TYPE,"Falkor HW Prefetch Fix", false, false) INITIALIZE_PASS_END(FalkorMarkStridedAccessesLegacy
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
TMC & getTM() const
Get the right type of TargetMachine for this target.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
Falkor HW Prefetch false
falkor hwpf fix Falkor HW Prefetch Fix Late static false unsigned makeTag(unsigned Dest, unsigned Base, unsigned Offset)
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition: LoopInfo.cpp:55
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Target-Independent Code Generator Pass Configuration Options.
#define F(x, y, z)
Definition: MD5.cpp:55
falkor hwpf fix Falkor HW Prefetch Fix Late Phase
This node represents a polynomial recurrence on the trip count of the specified loop.
Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubRegIdx=0)
Prints virtual and physical registers with or without a TRI instance.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:144
TracePC TPC
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
#define P(N)
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values...
unsigned const MachineRegisterInfo * MRI
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
df_iterator< T > df_end(const T &G)
#define DEBUG_TYPE
bool isReserved(unsigned PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Value * getPointerOperand()
Definition: Instructions.h:270
A set of register units.
Falkor HW Prefetch Fix
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:697
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1214
falkor hwpf fix late
void initializeFalkorMarkStridedAccessesLegacyPass(PassRegistry &)
#define E
Definition: LargeTest.cpp:27
MachineOperand class - Representation of each machine instruction operand.
Module.h This file contains the declarations for the Module class.
void initializeFalkorHWPFFixPass(PassRegistry &)
static Optional< unsigned > getTag(const TargetRegisterInfo *TRI, const MachineInstr &MI, const LoadInfo &LI)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:923
df_iterator< T > df_begin(const T &G)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:257
loop data Loop Data Prefetch
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1164
Representation of each machine instruction.
Definition: MachineInstr.h:59
#define FALKOR_STRIDED_ACCESS_MD
This class represents an analyzed expression in the program.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:360
void setReg(unsigned Reg)
Change the register this operand corresponds to.
#define I(x, y, z)
Definition: MD5.cpp:58
FunctionPass * createFalkorHWPFFixPass()
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
A set of register units used to track register liveness.
Definition: LiveRegUnits.h:30
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint16_t getEncodingValue(unsigned RegNo) const
Returns the encoding for RegNo.
LLVM Value Representation.
Definition: Value.h:73
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
bool empty() const
Definition: LoopInfo.h:137
#define DEBUG(X)
Definition: Debug.h:118
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:845
IRTranslator LLVM IR MI
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:261
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Properties which a MachineFunction may have at a given point in time.