blob: f49ffcb0abb70360a131abb73e6e34b4029bc708 [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/ADT/BitVector.h"
28#include "llvm/ADT/SmallSet.h"
David Goodwin557bbe62009-11-20 19:32:48 +000029#include <map>
Bill Wendlinge0104092010-07-15 06:04:38 +000030#include <vector>
David Goodwin34877712009-10-26 19:32:42 +000031
32namespace llvm {
Bill Wendlinge0104092010-07-15 06:04:38 +000033 class TargetRegisterInfo;
34
Jim Grosbach2973b572010-01-06 16:48:02 +000035 /// Class AggressiveAntiDepState
David Goodwin557bbe62009-11-20 19:32:48 +000036 /// Contains all the state necessary for anti-dep breaking.
David Goodwine10deca2009-10-26 22:31:16 +000037 class AggressiveAntiDepState {
38 public:
David Goodwin34877712009-10-26 19:32:42 +000039 /// RegisterReference - Information about a register reference
40 /// within a liverange
41 typedef struct {
42 /// Operand - The registers operand
43 MachineOperand *Operand;
44 /// RC - The register class
45 const TargetRegisterClass *RC;
46 } RegisterReference;
47
David Goodwine10deca2009-10-26 22:31:16 +000048 private:
David Goodwin990d2852009-12-09 17:18:22 +000049 /// NumTargetRegs - Number of non-virtual target registers
50 /// (i.e. TRI->getNumRegs()).
51 const unsigned NumTargetRegs;
52
David Goodwin34877712009-10-26 19:32:42 +000053 /// GroupNodes - Implements a disjoint-union data structure to
54 /// form register groups. A node is represented by an index into
55 /// the vector. A node can "point to" itself to indicate that it
56 /// is the parent of a group, or point to another node to indicate
57 /// that it is a member of the same group as that node.
58 std::vector<unsigned> GroupNodes;
Jim Grosbach2973b572010-01-06 16:48:02 +000059
David Goodwin34877712009-10-26 19:32:42 +000060 /// GroupNodeIndices - For each register, the index of the GroupNode
61 /// currently representing the group that the register belongs to.
62 /// Register 0 is always represented by the 0 group, a group
63 /// composed of registers that are not eligible for anti-aliasing.
Bill Wendlinge0104092010-07-15 06:04:38 +000064 std::vector<unsigned> GroupNodeIndices;
Jim Grosbach2973b572010-01-06 16:48:02 +000065
David Goodwine10deca2009-10-26 22:31:16 +000066 /// RegRefs - Map registers to all their references within a live range.
David Goodwin34877712009-10-26 19:32:42 +000067 std::multimap<unsigned, RegisterReference> RegRefs;
Jim Grosbach2973b572010-01-06 16:48:02 +000068
David Goodwin34877712009-10-26 19:32:42 +000069 /// KillIndices - The index of the most recent kill (proceding bottom-up),
70 /// or ~0u if the register is not live.
Bill Wendlinge0104092010-07-15 06:04:38 +000071 std::vector<unsigned> KillIndices;
Jim Grosbach2973b572010-01-06 16:48:02 +000072
David Goodwin34877712009-10-26 19:32:42 +000073 /// DefIndices - The index of the most recent complete def (proceding bottom
74 /// up), or ~0u if the register is live.
Bill Wendlinge0104092010-07-15 06:04:38 +000075 std::vector<unsigned> DefIndices;
David Goodwin34877712009-10-26 19:32:42 +000076
77 public:
David Goodwin990d2852009-12-09 17:18:22 +000078 AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB);
Jim Grosbach2973b572010-01-06 16:48:02 +000079
David Goodwine10deca2009-10-26 22:31:16 +000080 /// GetKillIndices - Return the kill indices.
Bill Wendlinge0104092010-07-15 06:04:38 +000081 std::vector<unsigned> &GetKillIndices() { return KillIndices; }
David Goodwin34877712009-10-26 19:32:42 +000082
David Goodwine10deca2009-10-26 22:31:16 +000083 /// GetDefIndices - Return the define indices.
Bill Wendlinge0104092010-07-15 06:04:38 +000084 std::vector<unsigned> &GetDefIndices() { return DefIndices; }
David Goodwin34877712009-10-26 19:32:42 +000085
David Goodwine10deca2009-10-26 22:31:16 +000086 /// GetRegRefs - Return the RegRefs map.
87 std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; }
David Goodwin34877712009-10-26 19:32:42 +000088
David Goodwin34877712009-10-26 19:32:42 +000089 // GetGroup - Get the group for a register. The returned value is
90 // the index of the GroupNode representing the group.
91 unsigned GetGroup(unsigned Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +000092
David Goodwin34877712009-10-26 19:32:42 +000093 // GetGroupRegs - Return a vector of the registers belonging to a
David Goodwin87d21b92009-11-13 19:52:48 +000094 // group. If RegRefs is non-NULL then only included referenced registers.
95 void GetGroupRegs(
96 unsigned Group,
97 std::vector<unsigned> &Regs,
Jim Grosbach2973b572010-01-06 16:48:02 +000098 std::multimap<unsigned,
99 AggressiveAntiDepState::RegisterReference> *RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000100
101 // UnionGroups - Union Reg1's and Reg2's groups to form a new
102 // group. Return the index of the GroupNode representing the
103 // group.
104 unsigned UnionGroups(unsigned Reg1, unsigned Reg2);
105
106 // LeaveGroup - Remove a register from its current group and place
107 // it alone in its own group. Return the index of the GroupNode
108 // representing the registers new group.
109 unsigned LeaveGroup(unsigned Reg);
110
111 /// IsLive - Return true if Reg is live
112 bool IsLive(unsigned Reg);
David Goodwine10deca2009-10-26 22:31:16 +0000113 };
114
115
Jim Grosbach2973b572010-01-06 16:48:02 +0000116 /// Class AggressiveAntiDepBreaker
David Goodwine10deca2009-10-26 22:31:16 +0000117 class AggressiveAntiDepBreaker : public AntiDepBreaker {
118 MachineFunction& MF;
119 MachineRegisterInfo &MRI;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000120 const TargetInstrInfo *TII;
David Goodwine10deca2009-10-26 22:31:16 +0000121 const TargetRegisterInfo *TRI;
122
123 /// AllocatableSet - The set of allocatable registers.
124 /// We'll be ignoring anti-dependencies on non-allocatable registers,
125 /// because they may not be safe to break.
David Goodwin87d21b92009-11-13 19:52:48 +0000126 const BitVector AllocatableSet;
127
128 /// CriticalPathSet - The set of registers that should only be
129 /// renamed if they are on the critical path.
130 BitVector CriticalPathSet;
David Goodwine10deca2009-10-26 22:31:16 +0000131
132 /// State - The state used to identify and rename anti-dependence
133 /// registers.
134 AggressiveAntiDepState *State;
135
David Goodwine10deca2009-10-26 22:31:16 +0000136 public:
Jim Grosbach2973b572010-01-06 16:48:02 +0000137 AggressiveAntiDepBreaker(MachineFunction& MFi,
David Goodwin87d21b92009-11-13 19:52:48 +0000138 TargetSubtarget::RegClassVector& CriticalPathRCs);
David Goodwine10deca2009-10-26 22:31:16 +0000139 ~AggressiveAntiDepBreaker();
Jim Grosbach2973b572010-01-06 16:48:02 +0000140
David Goodwine10deca2009-10-26 22:31:16 +0000141 /// Start - Initialize anti-dep breaking for a new basic block.
142 void StartBlock(MachineBasicBlock *BB);
143
Jim Grosbach2973b572010-01-06 16:48:02 +0000144 /// BreakAntiDependencies - Identifiy anti-dependencies along the critical
145 /// path
David Goodwine10deca2009-10-26 22:31:16 +0000146 /// of the ScheduleDAG and break them by renaming registers.
147 ///
Dan Gohman66db3a02010-04-19 23:11:58 +0000148 unsigned BreakAntiDependencies(const std::vector<SUnit>& SUnits,
149 MachineBasicBlock::iterator Begin,
150 MachineBasicBlock::iterator End,
David Goodwine10deca2009-10-26 22:31:16 +0000151 unsigned InsertPosIndex);
152
153 /// Observe - Update liveness information to account for the current
154 /// instruction, which will not be scheduled.
155 ///
156 void Observe(MachineInstr *MI, unsigned Count, unsigned InsertPosIndex);
157
158 /// Finish - Finish anti-dep breaking for a basic block.
159 void FinishBlock();
160
161 private:
David Goodwin54097832009-11-05 01:19:35 +0000162 typedef std::map<const TargetRegisterClass *,
163 TargetRegisterClass::const_iterator> RenameOrderType;
164
David Goodwin34877712009-10-26 19:32:42 +0000165 /// IsImplicitDefUse - Return true if MO represents a register
166 /// that is both implicitly used and defined in MI
167 bool IsImplicitDefUse(MachineInstr *MI, MachineOperand& MO);
Jim Grosbach2973b572010-01-06 16:48:02 +0000168
David Goodwin34877712009-10-26 19:32:42 +0000169 /// GetPassthruRegs - If MI implicitly def/uses a register, then
170 /// return that register and all subregisters.
171 void GetPassthruRegs(MachineInstr *MI, std::set<unsigned>& PassthruRegs);
172
David Goodwin3e72d302009-11-19 23:12:37 +0000173 void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag,
174 const char *header =NULL, const char *footer =NULL);
175
David Goodwin34877712009-10-26 19:32:42 +0000176 void PrescanInstruction(MachineInstr *MI, unsigned Count,
177 std::set<unsigned>& PassthruRegs);
178 void ScanInstruction(MachineInstr *MI, unsigned Count);
179 BitVector GetRenameRegisters(unsigned Reg);
180 bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,
David Goodwin54097832009-11-05 01:19:35 +0000181 RenameOrderType& RenameOrder,
David Goodwin34877712009-10-26 19:32:42 +0000182 std::map<unsigned, unsigned> &RenameMap);
183 };
184}
185
186#endif