blob: b1114859513639de3d3281a4768f9899a23191c2 [file] [log] [blame]
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +00001//===- llvm/CodeGen/AntiDepBreaker.h - Anti-Dependence Breaking -*- C++ -*-===//
David Goodwin83704852009-10-26 16:59:04 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David Goodwin83704852009-10-26 16:59:04 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the AntiDepBreaker class, which implements
10// anti-dependence breaking heuristics for post-register-allocation scheduling.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
15#define LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
David Goodwin83704852009-10-26 16:59:04 +000016
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000017#include "llvm/ADT/iterator_range.h"
David Goodwin83704852009-10-26 16:59:04 +000018#include "llvm/CodeGen/MachineBasicBlock.h"
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000019#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/CodeGen/MachineOperand.h"
David Goodwin83704852009-10-26 16:59:04 +000021#include "llvm/CodeGen/ScheduleDAG.h"
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000022#include "llvm/Support/Compiler.h"
23#include <cassert>
24#include <utility>
David Goodwin80a03cc2009-11-20 19:32:48 +000025#include <vector>
David Goodwin83704852009-10-26 16:59:04 +000026
27namespace llvm {
28
Sanjay Pateld6492352014-09-21 14:48:16 +000029/// This class works in conjunction with the post-RA scheduler to rename
30/// registers to break register anti-dependencies (WAR hazards).
Benjamin Kramerf4c20252015-07-01 14:47:39 +000031class LLVM_LIBRARY_VISIBILITY AntiDepBreaker {
David Goodwin83704852009-10-26 16:59:04 +000032public:
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000033 using DbgValueVector =
34 std::vector<std::pair<MachineInstr *, MachineInstr *>>;
Devang Patelf02a3762011-06-02 21:26:52 +000035
David Goodwin661ea982009-10-26 19:41:00 +000036 virtual ~AntiDepBreaker();
David Goodwin6b16b5e2009-10-26 19:00:47 +000037
Sanjay Pateld6492352014-09-21 14:48:16 +000038 /// Initialize anti-dep breaking for a new basic block.
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000039 virtual void StartBlock(MachineBasicBlock *BB) = 0;
David Goodwin83704852009-10-26 16:59:04 +000040
Sanjay Pateld6492352014-09-21 14:48:16 +000041 /// Identifiy anti-dependencies within a basic-block region and break them by
42 /// renaming registers. Return the number of anti-dependencies broken.
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000043 virtual unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits,
Devang Patelf02a3762011-06-02 21:26:52 +000044 MachineBasicBlock::iterator Begin,
45 MachineBasicBlock::iterator End,
46 unsigned InsertPosIndex,
47 DbgValueVector &DbgValues) = 0;
Fangrui Songf78650a2018-07-30 19:41:25 +000048
Sanjay Pateld6492352014-09-21 14:48:16 +000049 /// Update liveness information to account for the current
David Goodwin83704852009-10-26 16:59:04 +000050 /// instruction, which will not be scheduled.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +000051 virtual void Observe(MachineInstr &MI, unsigned Count,
52 unsigned InsertPosIndex) = 0;
53
Sanjay Pateld6492352014-09-21 14:48:16 +000054 /// Finish anti-dep breaking for a basic block.
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000055 virtual void FinishBlock() = 0;
Devang Patelf02a3762011-06-02 21:26:52 +000056
Sanjay Pateld6492352014-09-21 14:48:16 +000057 /// Update DBG_VALUE if dependency breaker is updating
Devang Patelf02a3762011-06-02 21:26:52 +000058 /// other machine instruction to use NewReg.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +000059 void UpdateDbgValue(MachineInstr &MI, unsigned OldReg, unsigned NewReg) {
60 assert(MI.isDebugValue() && "MI is not DBG_VALUE!");
61 if (MI.getOperand(0).isReg() && MI.getOperand(0).getReg() == OldReg)
62 MI.getOperand(0).setReg(NewReg);
Devang Patelf02a3762011-06-02 21:26:52 +000063 }
Andrew Ng10ebfe02017-04-25 15:39:57 +000064
65 /// Update all DBG_VALUE instructions that may be affected by the dependency
66 /// breaker's update of ParentMI to use NewReg.
67 void UpdateDbgValues(const DbgValueVector &DbgValues, MachineInstr *ParentMI,
68 unsigned OldReg, unsigned NewReg) {
69 // The following code is dependent on the order in which the DbgValues are
70 // constructed in ScheduleDAGInstrs::buildSchedGraph.
71 MachineInstr *PrevDbgMI = nullptr;
72 for (const auto &DV : make_range(DbgValues.crbegin(), DbgValues.crend())) {
73 MachineInstr *PrevMI = DV.second;
74 if ((PrevMI == ParentMI) || (PrevMI == PrevDbgMI)) {
75 MachineInstr *DbgMI = DV.first;
76 UpdateDbgValue(*DbgMI, OldReg, NewReg);
77 PrevDbgMI = DbgMI;
78 } else if (PrevDbgMI) {
79 break; // If no match and already found a DBG_VALUE, we're done.
80 }
81 }
82 }
David Goodwin83704852009-10-26 16:59:04 +000083};
84
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000085} // end namespace llvm
David Goodwin83704852009-10-26 16:59:04 +000086
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000087#endif // LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H