blob: 8154d2dd57256117b67dea434f4070da57319d6d [file] [log] [blame]
David Goodwin34877712009-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
17#ifndef LLVM_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
18#define LLVM_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
19
David Goodwin82c72482009-10-28 18:29:54 +000020#include "AntiDepBreaker.h"
David Goodwin34877712009-10-26 19:32:42 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/ScheduleDAG.h"
David Goodwin0855dee2009-11-10 00:15:47 +000026#include "llvm/Target/TargetSubtarget.h"
David Goodwin34877712009-10-26 19:32:42 +000027#include "llvm/Target/TargetRegisterInfo.h"
28#include "llvm/ADT/BitVector.h"
29#include "llvm/ADT/SmallSet.h"
David Goodwin557bbe62009-11-20 19:32:48 +000030#include <map>
David Goodwin34877712009-10-26 19:32:42 +000031
32namespace llvm {
David Goodwine10deca2009-10-26 22:31:16 +000033 /// Class AggressiveAntiDepState
David Goodwin557bbe62009-11-20 19:32:48 +000034 /// Contains all the state necessary for anti-dep breaking.
David Goodwine10deca2009-10-26 22:31:16 +000035 class AggressiveAntiDepState {
36 public:
David Goodwin34877712009-10-26 19:32:42 +000037 /// RegisterReference - Information about a register reference
38 /// within a liverange
39 typedef struct {
40 /// Operand - The registers operand
41 MachineOperand *Operand;
42 /// RC - The register class
43 const TargetRegisterClass *RC;
44 } RegisterReference;
45
David Goodwine10deca2009-10-26 22:31:16 +000046 private:
David Goodwin34877712009-10-26 19:32:42 +000047 /// GroupNodes - Implements a disjoint-union data structure to
48 /// form register groups. A node is represented by an index into
49 /// the vector. A node can "point to" itself to indicate that it
50 /// is the parent of a group, or point to another node to indicate
51 /// that it is a member of the same group as that node.
52 std::vector<unsigned> GroupNodes;
David Goodwine10deca2009-10-26 22:31:16 +000053
David Goodwin34877712009-10-26 19:32:42 +000054 /// GroupNodeIndices - For each register, the index of the GroupNode
55 /// currently representing the group that the register belongs to.
56 /// Register 0 is always represented by the 0 group, a group
57 /// composed of registers that are not eligible for anti-aliasing.
58 unsigned GroupNodeIndices[TargetRegisterInfo::FirstVirtualRegister];
David Goodwine10deca2009-10-26 22:31:16 +000059
60 /// RegRefs - Map registers to all their references within a live range.
David Goodwin34877712009-10-26 19:32:42 +000061 std::multimap<unsigned, RegisterReference> RegRefs;
David Goodwine10deca2009-10-26 22:31:16 +000062
David Goodwin34877712009-10-26 19:32:42 +000063 /// KillIndices - The index of the most recent kill (proceding bottom-up),
64 /// or ~0u if the register is not live.
65 unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
David Goodwine10deca2009-10-26 22:31:16 +000066
David Goodwin34877712009-10-26 19:32:42 +000067 /// DefIndices - The index of the most recent complete def (proceding bottom
68 /// up), or ~0u if the register is live.
69 unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister];
70
71 public:
David Goodwine10deca2009-10-26 22:31:16 +000072 AggressiveAntiDepState(MachineBasicBlock *BB);
David Goodwin34877712009-10-26 19:32:42 +000073
David Goodwine10deca2009-10-26 22:31:16 +000074 /// GetKillIndices - Return the kill indices.
75 unsigned *GetKillIndices() { return KillIndices; }
David Goodwin34877712009-10-26 19:32:42 +000076
David Goodwine10deca2009-10-26 22:31:16 +000077 /// GetDefIndices - Return the define indices.
78 unsigned *GetDefIndices() { return DefIndices; }
David Goodwin34877712009-10-26 19:32:42 +000079
David Goodwine10deca2009-10-26 22:31:16 +000080 /// GetRegRefs - Return the RegRefs map.
81 std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; }
David Goodwin34877712009-10-26 19:32:42 +000082
David Goodwin34877712009-10-26 19:32:42 +000083 // GetGroup - Get the group for a register. The returned value is
84 // the index of the GroupNode representing the group.
85 unsigned GetGroup(unsigned Reg);
86
87 // GetGroupRegs - Return a vector of the registers belonging to a
David Goodwin87d21b92009-11-13 19:52:48 +000088 // group. If RegRefs is non-NULL then only included referenced registers.
89 void GetGroupRegs(
90 unsigned Group,
91 std::vector<unsigned> &Regs,
92 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs);
David Goodwin34877712009-10-26 19:32:42 +000093
94 // UnionGroups - Union Reg1's and Reg2's groups to form a new
95 // group. Return the index of the GroupNode representing the
96 // group.
97 unsigned UnionGroups(unsigned Reg1, unsigned Reg2);
98
99 // LeaveGroup - Remove a register from its current group and place
100 // it alone in its own group. Return the index of the GroupNode
101 // representing the registers new group.
102 unsigned LeaveGroup(unsigned Reg);
103
104 /// IsLive - Return true if Reg is live
105 bool IsLive(unsigned Reg);
David Goodwine10deca2009-10-26 22:31:16 +0000106 };
107
108
109 /// Class AggressiveAntiDepBreaker
110 class AggressiveAntiDepBreaker : public AntiDepBreaker {
111 MachineFunction& MF;
112 MachineRegisterInfo &MRI;
113 const TargetRegisterInfo *TRI;
114
115 /// AllocatableSet - The set of allocatable registers.
116 /// We'll be ignoring anti-dependencies on non-allocatable registers,
117 /// because they may not be safe to break.
David Goodwin87d21b92009-11-13 19:52:48 +0000118 const BitVector AllocatableSet;
119
120 /// CriticalPathSet - The set of registers that should only be
121 /// renamed if they are on the critical path.
122 BitVector CriticalPathSet;
David Goodwine10deca2009-10-26 22:31:16 +0000123
124 /// State - The state used to identify and rename anti-dependence
125 /// registers.
126 AggressiveAntiDepState *State;
127
David Goodwine10deca2009-10-26 22:31:16 +0000128 public:
David Goodwin0855dee2009-11-10 00:15:47 +0000129 AggressiveAntiDepBreaker(MachineFunction& MFi,
David Goodwin87d21b92009-11-13 19:52:48 +0000130 TargetSubtarget::RegClassVector& CriticalPathRCs);
David Goodwine10deca2009-10-26 22:31:16 +0000131 ~AggressiveAntiDepBreaker();
David Goodwin34877712009-10-26 19:32:42 +0000132
David Goodwine10deca2009-10-26 22:31:16 +0000133 /// Start - Initialize anti-dep breaking for a new basic block.
134 void StartBlock(MachineBasicBlock *BB);
135
136 /// BreakAntiDependencies - Identifiy anti-dependencies along the critical path
137 /// of the ScheduleDAG and break them by renaming registers.
138 ///
139 unsigned BreakAntiDependencies(std::vector<SUnit>& SUnits,
140 MachineBasicBlock::iterator& Begin,
141 MachineBasicBlock::iterator& End,
142 unsigned InsertPosIndex);
143
144 /// Observe - Update liveness information to account for the current
145 /// instruction, which will not be scheduled.
146 ///
147 void Observe(MachineInstr *MI, unsigned Count, unsigned InsertPosIndex);
148
149 /// Finish - Finish anti-dep breaking for a basic block.
150 void FinishBlock();
151
152 private:
David Goodwin54097832009-11-05 01:19:35 +0000153 typedef std::map<const TargetRegisterClass *,
154 TargetRegisterClass::const_iterator> RenameOrderType;
155
David Goodwin34877712009-10-26 19:32:42 +0000156 /// IsImplicitDefUse - Return true if MO represents a register
157 /// that is both implicitly used and defined in MI
158 bool IsImplicitDefUse(MachineInstr *MI, MachineOperand& MO);
159
160 /// GetPassthruRegs - If MI implicitly def/uses a register, then
161 /// return that register and all subregisters.
162 void GetPassthruRegs(MachineInstr *MI, std::set<unsigned>& PassthruRegs);
163
David Goodwin3e72d302009-11-19 23:12:37 +0000164 void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag,
165 const char *header =NULL, const char *footer =NULL);
166
David Goodwin34877712009-10-26 19:32:42 +0000167 void PrescanInstruction(MachineInstr *MI, unsigned Count,
168 std::set<unsigned>& PassthruRegs);
169 void ScanInstruction(MachineInstr *MI, unsigned Count);
170 BitVector GetRenameRegisters(unsigned Reg);
171 bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,
David Goodwin54097832009-11-05 01:19:35 +0000172 RenameOrderType& RenameOrder,
David Goodwin34877712009-10-26 19:32:42 +0000173 std::map<unsigned, unsigned> &RenameMap);
174 };
175}
176
177#endif