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