blob: 12cf95b9b4f9bf30a0b3fe48374a8887425f6839 [file] [log] [blame]
David Goodwinde11f362009-10-26 19:32:42 +00001//=- llvm/CodeGen/AggressiveAntiDepBreaker.h - Anti-Dep Support -*- C++ -*-=//
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//
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"
22#include "llvm/ADT/SmallSet.h"
David Goodwinde11f362009-10-26 19:32:42 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattnerc48adb62010-07-15 06:51:46 +000028#include "llvm/Target/TargetRegisterInfo.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000029#include "llvm/Target/TargetSubtargetInfo.h"
David Goodwin80a03cc2009-11-20 19:32:48 +000030#include <map>
David Goodwinde11f362009-10-26 19:32:42 +000031
32namespace llvm {
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +000033class RegisterClassInfo;
34
David Goodwin80a03cc2009-11-20 19:32:48 +000035 /// Contains all the state necessary for anti-dep breaking.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000036 class AggressiveAntiDepState {
David Goodwine056d102009-10-26 22:31:16 +000037 public:
Sanjay Pateld6492352014-09-21 14:48:16 +000038 /// Information about a register reference within a liverange
David Goodwinde11f362009-10-26 19:32:42 +000039 typedef struct {
Sanjay Pateld6492352014-09-21 14:48:16 +000040 /// The registers operand
David Goodwinde11f362009-10-26 19:32:42 +000041 MachineOperand *Operand;
Sanjay Pateld6492352014-09-21 14:48:16 +000042 /// The register class
David Goodwinde11f362009-10-26 19:32:42 +000043 const TargetRegisterClass *RC;
44 } RegisterReference;
45
David Goodwine056d102009-10-26 22:31:16 +000046 private:
Sanjay Pateld6492352014-09-21 14:48:16 +000047 /// Number of non-virtual target registers (i.e. TRI->getNumRegs()).
David Goodwina45fe672009-12-09 17:18:22 +000048 const unsigned NumTargetRegs;
49
Sanjay Pateld6492352014-09-21 14:48:16 +000050 /// Implements a disjoint-union data structure to
David Goodwinde11f362009-10-26 19:32:42 +000051 /// form register groups. A node is represented by an index into
52 /// the vector. A node can "point to" itself to indicate that it
53 /// is the parent of a group, or point to another node to indicate
54 /// that it is a member of the same group as that node.
55 std::vector<unsigned> GroupNodes;
Jim Grosbacheb431da2010-01-06 16:48:02 +000056
Sanjay Pateld6492352014-09-21 14:48:16 +000057 /// For each register, the index of the GroupNode
David Goodwinde11f362009-10-26 19:32:42 +000058 /// currently representing the group that the register belongs to.
59 /// Register 0 is always represented by the 0 group, a group
60 /// composed of registers that are not eligible for anti-aliasing.
Bill Wendling57681402010-07-15 18:40:50 +000061 std::vector<unsigned> GroupNodeIndices;
Jim Grosbacheb431da2010-01-06 16:48:02 +000062
Sanjay Pateld6492352014-09-21 14:48:16 +000063 /// Map registers to all their references within a live range.
David Goodwinde11f362009-10-26 19:32:42 +000064 std::multimap<unsigned, RegisterReference> RegRefs;
Jim Grosbacheb431da2010-01-06 16:48:02 +000065
Sanjay Pateld6492352014-09-21 14:48:16 +000066 /// The index of the most recent kill (proceding bottom-up),
David Goodwinde11f362009-10-26 19:32:42 +000067 /// or ~0u if the register is not live.
Bill Wendling030b0282010-07-15 18:43:09 +000068 std::vector<unsigned> KillIndices;
Jim Grosbacheb431da2010-01-06 16:48:02 +000069
Sanjay Pateld6492352014-09-21 14:48:16 +000070 /// The index of the most recent complete def (proceding bottom
David Goodwinde11f362009-10-26 19:32:42 +000071 /// up), or ~0u if the register is live.
Bill Wendling030b0282010-07-15 18:43:09 +000072 std::vector<unsigned> DefIndices;
David Goodwinde11f362009-10-26 19:32:42 +000073
74 public:
David Goodwina45fe672009-12-09 17:18:22 +000075 AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB);
Jim Grosbacheb431da2010-01-06 16:48:02 +000076
Sanjay Pateld6492352014-09-21 14:48:16 +000077 /// Return the kill indices.
Bill Wendling030b0282010-07-15 18:43:09 +000078 std::vector<unsigned> &GetKillIndices() { return KillIndices; }
David Goodwinde11f362009-10-26 19:32:42 +000079
Sanjay Pateld6492352014-09-21 14:48:16 +000080 /// Return the define indices.
Bill Wendling030b0282010-07-15 18:43:09 +000081 std::vector<unsigned> &GetDefIndices() { return DefIndices; }
David Goodwinde11f362009-10-26 19:32:42 +000082
Sanjay Pateld6492352014-09-21 14:48:16 +000083 /// Return the RegRefs map.
David Goodwine056d102009-10-26 22:31:16 +000084 std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; }
David Goodwinde11f362009-10-26 19:32:42 +000085
Sanjay Pateld6492352014-09-21 14:48:16 +000086 // Get the group for a register. The returned value is
David Goodwinde11f362009-10-26 19:32:42 +000087 // the index of the GroupNode representing the group.
88 unsigned GetGroup(unsigned Reg);
Jim Grosbacheb431da2010-01-06 16:48:02 +000089
Sanjay Pateld6492352014-09-21 14:48:16 +000090 // Return a vector of the registers belonging to a group.
91 // If RegRefs is non-NULL then only included referenced registers.
David Goodwinb9fe5d52009-11-13 19:52:48 +000092 void GetGroupRegs(
93 unsigned Group,
94 std::vector<unsigned> &Regs,
Jim Grosbacheb431da2010-01-06 16:48:02 +000095 std::multimap<unsigned,
96 AggressiveAntiDepState::RegisterReference> *RegRefs);
David Goodwinde11f362009-10-26 19:32:42 +000097
Sanjay Pateld6492352014-09-21 14:48:16 +000098 // Union Reg1's and Reg2's groups to form a new group.
99 // Return the index of the GroupNode representing the group.
David Goodwinde11f362009-10-26 19:32:42 +0000100 unsigned UnionGroups(unsigned Reg1, unsigned Reg2);
101
Sanjay Pateld6492352014-09-21 14:48:16 +0000102 // Remove a register from its current group and place
David Goodwinde11f362009-10-26 19:32:42 +0000103 // it alone in its own group. Return the index of the GroupNode
104 // representing the registers new group.
105 unsigned LeaveGroup(unsigned Reg);
106
Sanjay Pateld6492352014-09-21 14:48:16 +0000107 /// Return true if Reg is live.
David Goodwinde11f362009-10-26 19:32:42 +0000108 bool IsLive(unsigned Reg);
David Goodwine056d102009-10-26 22:31:16 +0000109 };
110
111
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000112 class AggressiveAntiDepBreaker : public AntiDepBreaker {
David Goodwine056d102009-10-26 22:31:16 +0000113 MachineFunction& MF;
114 MachineRegisterInfo &MRI;
Evan Chengf128bdc2010-06-16 07:35:02 +0000115 const TargetInstrInfo *TII;
David Goodwine056d102009-10-26 22:31:16 +0000116 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000117 const RegisterClassInfo &RegClassInfo;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000118
Sanjay Pateld6492352014-09-21 14:48:16 +0000119 /// The set of registers that should only be
David Goodwinb9fe5d52009-11-13 19:52:48 +0000120 /// renamed if they are on the critical path.
121 BitVector CriticalPathSet;
David Goodwine056d102009-10-26 22:31:16 +0000122
Sanjay Pateld6492352014-09-21 14:48:16 +0000123 /// The state used to identify and rename anti-dependence registers.
David Goodwine056d102009-10-26 22:31:16 +0000124 AggressiveAntiDepState *State;
125
David Goodwine056d102009-10-26 22:31:16 +0000126 public:
Jim Grosbacheb431da2010-01-06 16:48:02 +0000127 AggressiveAntiDepBreaker(MachineFunction& MFi,
Evan Cheng0d639a22011-07-01 21:01:15 +0000128 const RegisterClassInfo &RCI,
129 TargetSubtargetInfo::RegClassVector& CriticalPathRCs);
David Goodwine056d102009-10-26 22:31:16 +0000130 ~AggressiveAntiDepBreaker();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000131
Sanjay Pateld6492352014-09-21 14:48:16 +0000132 /// Initialize anti-dep breaking for a new basic block.
Craig Topper4584cd52014-03-07 09:26:03 +0000133 void StartBlock(MachineBasicBlock *BB) override;
David Goodwine056d102009-10-26 22:31:16 +0000134
Sanjay Pateld6492352014-09-21 14:48:16 +0000135 /// Identifiy anti-dependencies along the critical path
David Goodwine056d102009-10-26 22:31:16 +0000136 /// of the ScheduleDAG and break them by renaming registers.
137 ///
Dan Gohman35bc4d42010-04-19 23:11:58 +0000138 unsigned BreakAntiDependencies(const std::vector<SUnit>& SUnits,
139 MachineBasicBlock::iterator Begin,
140 MachineBasicBlock::iterator End,
Devang Patelf02a3762011-06-02 21:26:52 +0000141 unsigned InsertPosIndex,
Craig Topper4584cd52014-03-07 09:26:03 +0000142 DbgValueVector &DbgValues) override;
David Goodwine056d102009-10-26 22:31:16 +0000143
Sanjay Pateld6492352014-09-21 14:48:16 +0000144 /// Update liveness information to account for the current
David Goodwine056d102009-10-26 22:31:16 +0000145 /// instruction, which will not be scheduled.
146 ///
Craig Topper4584cd52014-03-07 09:26:03 +0000147 void Observe(MachineInstr *MI, unsigned Count,
148 unsigned InsertPosIndex) override;
David Goodwine056d102009-10-26 22:31:16 +0000149
Sanjay Pateld6492352014-09-21 14:48:16 +0000150 /// Finish anti-dep breaking for a basic block.
Craig Topper4584cd52014-03-07 09:26:03 +0000151 void FinishBlock() override;
David Goodwine056d102009-10-26 22:31:16 +0000152
153 private:
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000154 /// Keep track of a position in the allocation order for each regclass.
155 typedef std::map<const TargetRegisterClass *, unsigned> RenameOrderType;
David Goodwin7d8878a2009-11-05 01:19:35 +0000156
Sanjay Pateld6492352014-09-21 14:48:16 +0000157 /// Return true if MO represents a register
David Goodwinde11f362009-10-26 19:32:42 +0000158 /// that is both implicitly used and defined in MI
159 bool IsImplicitDefUse(MachineInstr *MI, MachineOperand& MO);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000160
Sanjay Pateld6492352014-09-21 14:48:16 +0000161 /// If MI implicitly def/uses a register, then
David Goodwinde11f362009-10-26 19:32:42 +0000162 /// return that register and all subregisters.
163 void GetPassthruRegs(MachineInstr *MI, std::set<unsigned>& PassthruRegs);
164
David Goodwindd1c6192009-11-19 23:12:37 +0000165 void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag,
Craig Topperada08572014-04-16 04:21:27 +0000166 const char *header = nullptr,
167 const char *footer = nullptr);
David Goodwindd1c6192009-11-19 23:12:37 +0000168
David Goodwinde11f362009-10-26 19:32:42 +0000169 void PrescanInstruction(MachineInstr *MI, unsigned Count,
170 std::set<unsigned>& PassthruRegs);
171 void ScanInstruction(MachineInstr *MI, unsigned Count);
172 BitVector GetRenameRegisters(unsigned Reg);
173 bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,
David Goodwin7d8878a2009-11-05 01:19:35 +0000174 RenameOrderType& RenameOrder,
David Goodwinde11f362009-10-26 19:32:42 +0000175 std::map<unsigned, unsigned> &RenameMap);
176 };
177}
178
179#endif