blob: 5dce3c2499e51f4e719550f1559a0f010b8d46da [file] [log] [blame]
Eugene Zelenko94348112017-09-29 21:55:49 +00001//==- llvm/CodeGen/AggressiveAntiDepBreaker.h - Anti-Dep Support -*- C++ -*-==//
David Goodwin34877712009-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 Kramer00e08fc2014-08-13 16:26:38 +000017#ifndef LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
18#define LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
David Goodwin34877712009-10-26 19:32:42 +000019
David Goodwin82c72482009-10-28 18:29:54 +000020#include "AntiDepBreaker.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000021#include "llvm/ADT/BitVector.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000022#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko94348112017-09-29 21:55:49 +000023#include "llvm/Support/Compiler.h"
David Goodwin557bbe62009-11-20 19:32:48 +000024#include <map>
Eugene Zelenko94348112017-09-29 21:55:49 +000025#include <set>
26#include <vector>
David Goodwin34877712009-10-26 19:32:42 +000027
28namespace llvm {
Eugene Zelenko94348112017-09-29 21:55:49 +000029
30class MachineBasicBlock;
31class MachineFunction;
32class MachineInstr;
33class MachineOperand;
34class MachineRegisterInfo;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000035class RegisterClassInfo;
Eugene Zelenko94348112017-09-29 21:55:49 +000036class TargetInstrInfo;
37class TargetRegisterClass;
38class TargetRegisterInfo;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000039
David Goodwin557bbe62009-11-20 19:32:48 +000040 /// Contains all the state necessary for anti-dep breaking.
Benjamin Kramerb4537752015-07-01 14:47:39 +000041class LLVM_LIBRARY_VISIBILITY AggressiveAntiDepState {
David Goodwine10deca2009-10-26 22:31:16 +000042 public:
Sanjay Patel42dac652014-09-21 14:48:16 +000043 /// Information about a register reference within a liverange
Eugene Zelenko94348112017-09-29 21:55:49 +000044 struct RegisterReference {
Sanjay Patel42dac652014-09-21 14:48:16 +000045 /// The registers operand
David Goodwin34877712009-10-26 19:32:42 +000046 MachineOperand *Operand;
Eugene Zelenko94348112017-09-29 21:55:49 +000047
Sanjay Patel42dac652014-09-21 14:48:16 +000048 /// The register class
David Goodwin34877712009-10-26 19:32:42 +000049 const TargetRegisterClass *RC;
Eugene Zelenko94348112017-09-29 21:55:49 +000050 };
David Goodwin34877712009-10-26 19:32:42 +000051
David Goodwine10deca2009-10-26 22:31:16 +000052 private:
Sanjay Patel42dac652014-09-21 14:48:16 +000053 /// Number of non-virtual target registers (i.e. TRI->getNumRegs()).
David Goodwin990d2852009-12-09 17:18:22 +000054 const unsigned NumTargetRegs;
55
Sanjay Patel42dac652014-09-21 14:48:16 +000056 /// Implements a disjoint-union data structure to
David Goodwin34877712009-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 Grosbach2973b572010-01-06 16:48:02 +000062
Sanjay Patel42dac652014-09-21 14:48:16 +000063 /// For each register, the index of the GroupNode
David Goodwin34877712009-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 Wendlingdfb4eeb2010-07-15 18:40:50 +000067 std::vector<unsigned> GroupNodeIndices;
Jim Grosbach2973b572010-01-06 16:48:02 +000068
Sanjay Patel42dac652014-09-21 14:48:16 +000069 /// Map registers to all their references within a live range.
David Goodwin34877712009-10-26 19:32:42 +000070 std::multimap<unsigned, RegisterReference> RegRefs;
Jim Grosbach2973b572010-01-06 16:48:02 +000071
Luqman Aden440ae312015-04-22 17:42:37 +000072 /// The index of the most recent kill (proceeding bottom-up),
David Goodwin34877712009-10-26 19:32:42 +000073 /// or ~0u if the register is not live.
Bill Wendling38306d52010-07-15 18:43:09 +000074 std::vector<unsigned> KillIndices;
Jim Grosbach2973b572010-01-06 16:48:02 +000075
Luqman Aden440ae312015-04-22 17:42:37 +000076 /// The index of the most recent complete def (proceeding bottom
David Goodwin34877712009-10-26 19:32:42 +000077 /// up), or ~0u if the register is live.
Bill Wendling38306d52010-07-15 18:43:09 +000078 std::vector<unsigned> DefIndices;
David Goodwin34877712009-10-26 19:32:42 +000079
80 public:
David Goodwin990d2852009-12-09 17:18:22 +000081 AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB);
Jim Grosbach2973b572010-01-06 16:48:02 +000082
Sanjay Patel42dac652014-09-21 14:48:16 +000083 /// Return the kill indices.
Bill Wendling38306d52010-07-15 18:43:09 +000084 std::vector<unsigned> &GetKillIndices() { return KillIndices; }
David Goodwin34877712009-10-26 19:32:42 +000085
Sanjay Patel42dac652014-09-21 14:48:16 +000086 /// Return the define indices.
Bill Wendling38306d52010-07-15 18:43:09 +000087 std::vector<unsigned> &GetDefIndices() { return DefIndices; }
David Goodwin34877712009-10-26 19:32:42 +000088
Sanjay Patel42dac652014-09-21 14:48:16 +000089 /// Return the RegRefs map.
David Goodwine10deca2009-10-26 22:31:16 +000090 std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; }
David Goodwin34877712009-10-26 19:32:42 +000091
Sanjay Patel42dac652014-09-21 14:48:16 +000092 // Get the group for a register. The returned value is
David Goodwin34877712009-10-26 19:32:42 +000093 // the index of the GroupNode representing the group.
94 unsigned GetGroup(unsigned Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +000095
Sanjay Patel42dac652014-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 Goodwin87d21b92009-11-13 19:52:48 +000098 void GetGroupRegs(
99 unsigned Group,
100 std::vector<unsigned> &Regs,
Jim Grosbach2973b572010-01-06 16:48:02 +0000101 std::multimap<unsigned,
102 AggressiveAntiDepState::RegisterReference> *RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000103
Sanjay Patel42dac652014-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 Goodwin34877712009-10-26 19:32:42 +0000106 unsigned UnionGroups(unsigned Reg1, unsigned Reg2);
107
Sanjay Patel42dac652014-09-21 14:48:16 +0000108 // Remove a register from its current group and place
David Goodwin34877712009-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 Patel42dac652014-09-21 14:48:16 +0000113 /// Return true if Reg is live.
David Goodwin34877712009-10-26 19:32:42 +0000114 bool IsLive(unsigned Reg);
David Goodwine10deca2009-10-26 22:31:16 +0000115 };
116
Benjamin Kramerb4537752015-07-01 14:47:39 +0000117 class LLVM_LIBRARY_VISIBILITY AggressiveAntiDepBreaker
118 : public AntiDepBreaker {
Eugene Zelenko94348112017-09-29 21:55:49 +0000119 MachineFunction &MF;
David Goodwine10deca2009-10-26 22:31:16 +0000120 MachineRegisterInfo &MRI;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000121 const TargetInstrInfo *TII;
David Goodwine10deca2009-10-26 22:31:16 +0000122 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000123 const RegisterClassInfo &RegClassInfo;
David Goodwin87d21b92009-11-13 19:52:48 +0000124
Sanjay Patel42dac652014-09-21 14:48:16 +0000125 /// The set of registers that should only be
David Goodwin87d21b92009-11-13 19:52:48 +0000126 /// renamed if they are on the critical path.
127 BitVector CriticalPathSet;
David Goodwine10deca2009-10-26 22:31:16 +0000128
Sanjay Patel42dac652014-09-21 14:48:16 +0000129 /// The state used to identify and rename anti-dependence registers.
Eugene Zelenko94348112017-09-29 21:55:49 +0000130 AggressiveAntiDepState *State = nullptr;
David Goodwine10deca2009-10-26 22:31:16 +0000131
David Goodwine10deca2009-10-26 22:31:16 +0000132 public:
Eugene Zelenko94348112017-09-29 21:55:49 +0000133 AggressiveAntiDepBreaker(MachineFunction &MFi,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000134 const RegisterClassInfo &RCI,
135 TargetSubtargetInfo::RegClassVector& CriticalPathRCs);
Alexander Kornienkoc16fc542015-04-11 02:11:45 +0000136 ~AggressiveAntiDepBreaker() override;
Jim Grosbach2973b572010-01-06 16:48:02 +0000137
Sanjay Patel42dac652014-09-21 14:48:16 +0000138 /// Initialize anti-dep breaking for a new basic block.
Craig Topper9f998de2014-03-07 09:26:03 +0000139 void StartBlock(MachineBasicBlock *BB) override;
David Goodwine10deca2009-10-26 22:31:16 +0000140
Sanjay Patel42dac652014-09-21 14:48:16 +0000141 /// Identifiy anti-dependencies along the critical path
David Goodwine10deca2009-10-26 22:31:16 +0000142 /// of the ScheduleDAG and break them by renaming registers.
Eugene Zelenko94348112017-09-29 21:55:49 +0000143 unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits,
Dan Gohman66db3a02010-04-19 23:11:58 +0000144 MachineBasicBlock::iterator Begin,
145 MachineBasicBlock::iterator End,
Devang Patele29e8e12011-06-02 21:26:52 +0000146 unsigned InsertPosIndex,
Craig Topper9f998de2014-03-07 09:26:03 +0000147 DbgValueVector &DbgValues) override;
David Goodwine10deca2009-10-26 22:31:16 +0000148
Sanjay Patel42dac652014-09-21 14:48:16 +0000149 /// Update liveness information to account for the current
David Goodwine10deca2009-10-26 22:31:16 +0000150 /// instruction, which will not be scheduled.
Duncan P. N. Exon Smitha26cd9c2016-02-27 19:33:37 +0000151 void Observe(MachineInstr &MI, unsigned Count,
Craig Topper9f998de2014-03-07 09:26:03 +0000152 unsigned InsertPosIndex) override;
David Goodwine10deca2009-10-26 22:31:16 +0000153
Sanjay Patel42dac652014-09-21 14:48:16 +0000154 /// Finish anti-dep breaking for a basic block.
Craig Topper9f998de2014-03-07 09:26:03 +0000155 void FinishBlock() override;
David Goodwine10deca2009-10-26 22:31:16 +0000156
157 private:
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000158 /// Keep track of a position in the allocation order for each regclass.
Eugene Zelenko94348112017-09-29 21:55:49 +0000159 using RenameOrderType = std::map<const TargetRegisterClass *, unsigned>;
David Goodwin54097832009-11-05 01:19:35 +0000160
Sanjay Patel42dac652014-09-21 14:48:16 +0000161 /// Return true if MO represents a register
David Goodwin34877712009-10-26 19:32:42 +0000162 /// that is both implicitly used and defined in MI
Duncan P. N. Exon Smitha26cd9c2016-02-27 19:33:37 +0000163 bool IsImplicitDefUse(MachineInstr &MI, MachineOperand &MO);
Jim Grosbach2973b572010-01-06 16:48:02 +0000164
Sanjay Patel42dac652014-09-21 14:48:16 +0000165 /// If MI implicitly def/uses a register, then
David Goodwin34877712009-10-26 19:32:42 +0000166 /// return that register and all subregisters.
Duncan P. N. Exon Smitha26cd9c2016-02-27 19:33:37 +0000167 void GetPassthruRegs(MachineInstr &MI, std::set<unsigned> &PassthruRegs);
David Goodwin34877712009-10-26 19:32:42 +0000168
David Goodwin3e72d302009-11-19 23:12:37 +0000169 void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag,
Craig Topper695aa802014-04-16 04:21:27 +0000170 const char *header = nullptr,
171 const char *footer = nullptr);
David Goodwin3e72d302009-11-19 23:12:37 +0000172
Duncan P. N. Exon Smitha26cd9c2016-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 Goodwin34877712009-10-26 19:32:42 +0000176 BitVector GetRenameRegisters(unsigned Reg);
177 bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,
David Goodwin54097832009-11-05 01:19:35 +0000178 RenameOrderType& RenameOrder,
David Goodwin34877712009-10-26 19:32:42 +0000179 std::map<unsigned, unsigned> &RenameMap);
180 };
David Goodwin34877712009-10-26 19:32:42 +0000181
Eugene Zelenko94348112017-09-29 21:55:49 +0000182} // end namespace llvm
183
184#endif // LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H