blob: 0cf2e6d78f7ffaad097ee56181397b99b0bb8814 [file] [log] [blame]
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +00001//==- llvm/CodeGen/AggressiveAntiDepBreaker.h - Anti-Dep Support -*- C++ -*-==//
David Goodwinde11f362009-10-26 19:32:42 +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 Goodwinde11f362009-10-26 19:32:42 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the AggressiveAntiDepBreaker class, which
10// implements register anti-dependence breaking during post-RA
11// scheduling. It attempts to break all anti-dependencies within a
12// block.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000016#ifndef LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
17#define LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
David Goodwinde11f362009-10-26 19:32:42 +000018
David Goodwine30ed532009-10-28 18:29:54 +000019#include "AntiDepBreaker.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000020#include "llvm/ADT/BitVector.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000021#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000022#include "llvm/Support/Compiler.h"
David Goodwin80a03cc2009-11-20 19:32:48 +000023#include <map>
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000024#include <set>
25#include <vector>
David Goodwinde11f362009-10-26 19:32:42 +000026
27namespace llvm {
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000028
29class MachineBasicBlock;
30class MachineFunction;
31class MachineInstr;
32class MachineOperand;
33class MachineRegisterInfo;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +000034class RegisterClassInfo;
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000035class TargetInstrInfo;
36class TargetRegisterClass;
37class TargetRegisterInfo;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +000038
David Goodwin80a03cc2009-11-20 19:32:48 +000039 /// Contains all the state necessary for anti-dep breaking.
Benjamin Kramerf4c20252015-07-01 14:47:39 +000040class LLVM_LIBRARY_VISIBILITY AggressiveAntiDepState {
David Goodwine056d102009-10-26 22:31:16 +000041 public:
Sanjay Pateld6492352014-09-21 14:48:16 +000042 /// Information about a register reference within a liverange
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000043 struct RegisterReference {
Sanjay Pateld6492352014-09-21 14:48:16 +000044 /// The registers operand
David Goodwinde11f362009-10-26 19:32:42 +000045 MachineOperand *Operand;
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000046
Sanjay Pateld6492352014-09-21 14:48:16 +000047 /// The register class
David Goodwinde11f362009-10-26 19:32:42 +000048 const TargetRegisterClass *RC;
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000049 };
David Goodwinde11f362009-10-26 19:32:42 +000050
David Goodwine056d102009-10-26 22:31:16 +000051 private:
Sanjay Pateld6492352014-09-21 14:48:16 +000052 /// Number of non-virtual target registers (i.e. TRI->getNumRegs()).
David Goodwina45fe672009-12-09 17:18:22 +000053 const unsigned NumTargetRegs;
54
Sanjay Pateld6492352014-09-21 14:48:16 +000055 /// Implements a disjoint-union data structure to
David Goodwinde11f362009-10-26 19:32:42 +000056 /// form register groups. A node is represented by an index into
57 /// the vector. A node can "point to" itself to indicate that it
58 /// is the parent of a group, or point to another node to indicate
59 /// that it is a member of the same group as that node.
60 std::vector<unsigned> GroupNodes;
Jim Grosbacheb431da2010-01-06 16:48:02 +000061
Sanjay Pateld6492352014-09-21 14:48:16 +000062 /// For each register, the index of the GroupNode
David Goodwinde11f362009-10-26 19:32:42 +000063 /// currently representing the group that the register belongs to.
64 /// Register 0 is always represented by the 0 group, a group
65 /// composed of registers that are not eligible for anti-aliasing.
Bill Wendling57681402010-07-15 18:40:50 +000066 std::vector<unsigned> GroupNodeIndices;
Jim Grosbacheb431da2010-01-06 16:48:02 +000067
Sanjay Pateld6492352014-09-21 14:48:16 +000068 /// Map registers to all their references within a live range.
David Goodwinde11f362009-10-26 19:32:42 +000069 std::multimap<unsigned, RegisterReference> RegRefs;
Jim Grosbacheb431da2010-01-06 16:48:02 +000070
Luqman Adenc76f4702015-04-22 17:42:37 +000071 /// The index of the most recent kill (proceeding bottom-up),
David Goodwinde11f362009-10-26 19:32:42 +000072 /// or ~0u if the register is not live.
Bill Wendling030b0282010-07-15 18:43:09 +000073 std::vector<unsigned> KillIndices;
Jim Grosbacheb431da2010-01-06 16:48:02 +000074
Luqman Adenc76f4702015-04-22 17:42:37 +000075 /// The index of the most recent complete def (proceeding bottom
David Goodwinde11f362009-10-26 19:32:42 +000076 /// up), or ~0u if the register is live.
Bill Wendling030b0282010-07-15 18:43:09 +000077 std::vector<unsigned> DefIndices;
David Goodwinde11f362009-10-26 19:32:42 +000078
79 public:
David Goodwina45fe672009-12-09 17:18:22 +000080 AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB);
Jim Grosbacheb431da2010-01-06 16:48:02 +000081
Sanjay Pateld6492352014-09-21 14:48:16 +000082 /// Return the kill indices.
Bill Wendling030b0282010-07-15 18:43:09 +000083 std::vector<unsigned> &GetKillIndices() { return KillIndices; }
David Goodwinde11f362009-10-26 19:32:42 +000084
Sanjay Pateld6492352014-09-21 14:48:16 +000085 /// Return the define indices.
Bill Wendling030b0282010-07-15 18:43:09 +000086 std::vector<unsigned> &GetDefIndices() { return DefIndices; }
David Goodwinde11f362009-10-26 19:32:42 +000087
Sanjay Pateld6492352014-09-21 14:48:16 +000088 /// Return the RegRefs map.
David Goodwine056d102009-10-26 22:31:16 +000089 std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; }
David Goodwinde11f362009-10-26 19:32:42 +000090
Sanjay Pateld6492352014-09-21 14:48:16 +000091 // Get the group for a register. The returned value is
David Goodwinde11f362009-10-26 19:32:42 +000092 // the index of the GroupNode representing the group.
93 unsigned GetGroup(unsigned Reg);
Jim Grosbacheb431da2010-01-06 16:48:02 +000094
Sanjay Pateld6492352014-09-21 14:48:16 +000095 // Return a vector of the registers belonging to a group.
96 // If RegRefs is non-NULL then only included referenced registers.
David Goodwinb9fe5d52009-11-13 19:52:48 +000097 void GetGroupRegs(
98 unsigned Group,
99 std::vector<unsigned> &Regs,
Jim Grosbacheb431da2010-01-06 16:48:02 +0000100 std::multimap<unsigned,
101 AggressiveAntiDepState::RegisterReference> *RegRefs);
David Goodwinde11f362009-10-26 19:32:42 +0000102
Sanjay Pateld6492352014-09-21 14:48:16 +0000103 // Union Reg1's and Reg2's groups to form a new group.
104 // Return the index of the GroupNode representing the group.
David Goodwinde11f362009-10-26 19:32:42 +0000105 unsigned UnionGroups(unsigned Reg1, unsigned Reg2);
106
Sanjay Pateld6492352014-09-21 14:48:16 +0000107 // Remove a register from its current group and place
David Goodwinde11f362009-10-26 19:32:42 +0000108 // it alone in its own group. Return the index of the GroupNode
109 // representing the registers new group.
110 unsigned LeaveGroup(unsigned Reg);
111
Sanjay Pateld6492352014-09-21 14:48:16 +0000112 /// Return true if Reg is live.
David Goodwinde11f362009-10-26 19:32:42 +0000113 bool IsLive(unsigned Reg);
David Goodwine056d102009-10-26 22:31:16 +0000114 };
115
Benjamin Kramerf4c20252015-07-01 14:47:39 +0000116 class LLVM_LIBRARY_VISIBILITY AggressiveAntiDepBreaker
117 : public AntiDepBreaker {
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +0000118 MachineFunction &MF;
David Goodwine056d102009-10-26 22:31:16 +0000119 MachineRegisterInfo &MRI;
Evan Chengf128bdc2010-06-16 07:35:02 +0000120 const TargetInstrInfo *TII;
David Goodwine056d102009-10-26 22:31:16 +0000121 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000122 const RegisterClassInfo &RegClassInfo;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000123
Sanjay Pateld6492352014-09-21 14:48:16 +0000124 /// The set of registers that should only be
David Goodwinb9fe5d52009-11-13 19:52:48 +0000125 /// renamed if they are on the critical path.
126 BitVector CriticalPathSet;
David Goodwine056d102009-10-26 22:31:16 +0000127
Sanjay Pateld6492352014-09-21 14:48:16 +0000128 /// The state used to identify and rename anti-dependence registers.
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +0000129 AggressiveAntiDepState *State = nullptr;
David Goodwine056d102009-10-26 22:31:16 +0000130
David Goodwine056d102009-10-26 22:31:16 +0000131 public:
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +0000132 AggressiveAntiDepBreaker(MachineFunction &MFi,
Evan Cheng0d639a22011-07-01 21:01:15 +0000133 const RegisterClassInfo &RCI,
134 TargetSubtargetInfo::RegClassVector& CriticalPathRCs);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000135 ~AggressiveAntiDepBreaker() override;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000136
Sanjay Pateld6492352014-09-21 14:48:16 +0000137 /// Initialize anti-dep breaking for a new basic block.
Craig Topper4584cd52014-03-07 09:26:03 +0000138 void StartBlock(MachineBasicBlock *BB) override;
David Goodwine056d102009-10-26 22:31:16 +0000139
Sanjay Pateld6492352014-09-21 14:48:16 +0000140 /// Identifiy anti-dependencies along the critical path
David Goodwine056d102009-10-26 22:31:16 +0000141 /// of the ScheduleDAG and break them by renaming registers.
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +0000142 unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits,
Dan Gohman35bc4d42010-04-19 23:11:58 +0000143 MachineBasicBlock::iterator Begin,
144 MachineBasicBlock::iterator End,
Devang Patelf02a3762011-06-02 21:26:52 +0000145 unsigned InsertPosIndex,
Craig Topper4584cd52014-03-07 09:26:03 +0000146 DbgValueVector &DbgValues) override;
David Goodwine056d102009-10-26 22:31:16 +0000147
Sanjay Pateld6492352014-09-21 14:48:16 +0000148 /// Update liveness information to account for the current
David Goodwine056d102009-10-26 22:31:16 +0000149 /// instruction, which will not be scheduled.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000150 void Observe(MachineInstr &MI, unsigned Count,
Craig Topper4584cd52014-03-07 09:26:03 +0000151 unsigned InsertPosIndex) override;
David Goodwine056d102009-10-26 22:31:16 +0000152
Sanjay Pateld6492352014-09-21 14:48:16 +0000153 /// Finish anti-dep breaking for a basic block.
Craig Topper4584cd52014-03-07 09:26:03 +0000154 void FinishBlock() override;
David Goodwine056d102009-10-26 22:31:16 +0000155
156 private:
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000157 /// Keep track of a position in the allocation order for each regclass.
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +0000158 using RenameOrderType = std::map<const TargetRegisterClass *, unsigned>;
David Goodwin7d8878a2009-11-05 01:19:35 +0000159
Sanjay Pateld6492352014-09-21 14:48:16 +0000160 /// Return true if MO represents a register
David Goodwinde11f362009-10-26 19:32:42 +0000161 /// that is both implicitly used and defined in MI
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000162 bool IsImplicitDefUse(MachineInstr &MI, MachineOperand &MO);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000163
Sanjay Pateld6492352014-09-21 14:48:16 +0000164 /// If MI implicitly def/uses a register, then
David Goodwinde11f362009-10-26 19:32:42 +0000165 /// return that register and all subregisters.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000166 void GetPassthruRegs(MachineInstr &MI, std::set<unsigned> &PassthruRegs);
David Goodwinde11f362009-10-26 19:32:42 +0000167
David Goodwindd1c6192009-11-19 23:12:37 +0000168 void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag,
Craig Topperada08572014-04-16 04:21:27 +0000169 const char *header = nullptr,
170 const char *footer = nullptr);
David Goodwindd1c6192009-11-19 23:12:37 +0000171
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000172 void PrescanInstruction(MachineInstr &MI, unsigned Count,
173 std::set<unsigned> &PassthruRegs);
174 void ScanInstruction(MachineInstr &MI, unsigned Count);
David Goodwinde11f362009-10-26 19:32:42 +0000175 BitVector GetRenameRegisters(unsigned Reg);
176 bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,
David Goodwin7d8878a2009-11-05 01:19:35 +0000177 RenameOrderType& RenameOrder,
David Goodwinde11f362009-10-26 19:32:42 +0000178 std::map<unsigned, unsigned> &RenameMap);
179 };
David Goodwinde11f362009-10-26 19:32:42 +0000180
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +0000181} // end namespace llvm
182
183#endif // LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H