blob: d9365a5120b0701aaf49b6c6c44b889c30e16e51 [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;
Jim Grosbach2973b572010-01-06 16:48:02 +000034 /// Class AggressiveAntiDepState
David Goodwin557bbe62009-11-20 19:32:48 +000035 /// Contains all the state necessary for anti-dep breaking.
David Goodwine10deca2009-10-26 22:31:16 +000036 class AggressiveAntiDepState {
37 public:
David Goodwin34877712009-10-26 19:32:42 +000038 /// RegisterReference - Information about a register reference
39 /// within a liverange
40 typedef struct {
41 /// Operand - The registers operand
42 MachineOperand *Operand;
43 /// RC - The register class
44 const TargetRegisterClass *RC;
45 } RegisterReference;
46
David Goodwine10deca2009-10-26 22:31:16 +000047 private:
David Goodwin990d2852009-12-09 17:18:22 +000048 /// NumTargetRegs - Number of non-virtual target registers
49 /// (i.e. TRI->getNumRegs()).
50 const unsigned NumTargetRegs;
51
David Goodwin34877712009-10-26 19:32:42 +000052 /// GroupNodes - Implements a disjoint-union data structure to
53 /// form register groups. A node is represented by an index into
54 /// the vector. A node can "point to" itself to indicate that it
55 /// is the parent of a group, or point to another node to indicate
56 /// that it is a member of the same group as that node.
57 std::vector<unsigned> GroupNodes;
Jim Grosbach2973b572010-01-06 16:48:02 +000058
David Goodwin34877712009-10-26 19:32:42 +000059 /// GroupNodeIndices - For each register, the index of the GroupNode
60 /// currently representing the group that the register belongs to.
61 /// Register 0 is always represented by the 0 group, a group
62 /// composed of registers that are not eligible for anti-aliasing.
Bill Wendlinge0104092010-07-15 06:04:38 +000063 std::vector<unsigned> GroupNodeIndices;
Jim Grosbach2973b572010-01-06 16:48:02 +000064
David Goodwine10deca2009-10-26 22:31:16 +000065 /// RegRefs - Map registers to all their references within a live range.
David Goodwin34877712009-10-26 19:32:42 +000066 std::multimap<unsigned, RegisterReference> RegRefs;
Jim Grosbach2973b572010-01-06 16:48:02 +000067
David Goodwin34877712009-10-26 19:32:42 +000068 /// KillIndices - The index of the most recent kill (proceding bottom-up),
69 /// or ~0u if the register is not live.
Bill Wendlinge0104092010-07-15 06:04:38 +000070 std::vector<unsigned> KillIndices;
Jim Grosbach2973b572010-01-06 16:48:02 +000071
David Goodwin34877712009-10-26 19:32:42 +000072 /// DefIndices - The index of the most recent complete def (proceding bottom
73 /// up), or ~0u if the register is live.
Bill Wendlinge0104092010-07-15 06:04:38 +000074 std::vector<unsigned> DefIndices;
David Goodwin34877712009-10-26 19:32:42 +000075
76 public:
David Goodwin990d2852009-12-09 17:18:22 +000077 AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB);
Jim Grosbach2973b572010-01-06 16:48:02 +000078
David Goodwine10deca2009-10-26 22:31:16 +000079 /// GetKillIndices - Return the kill indices.
Bill Wendlinge0104092010-07-15 06:04:38 +000080 std::vector<unsigned> &GetKillIndices() { return KillIndices; }
David Goodwin34877712009-10-26 19:32:42 +000081
David Goodwine10deca2009-10-26 22:31:16 +000082 /// GetDefIndices - Return the define indices.
Bill Wendlinge0104092010-07-15 06:04:38 +000083 std::vector<unsigned> &GetDefIndices() { return DefIndices; }
David Goodwin34877712009-10-26 19:32:42 +000084
David Goodwine10deca2009-10-26 22:31:16 +000085 /// GetRegRefs - Return the RegRefs map.
86 std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; }
David Goodwin34877712009-10-26 19:32:42 +000087
David Goodwin34877712009-10-26 19:32:42 +000088 // GetGroup - Get the group for a register. The returned value is
89 // the index of the GroupNode representing the group.
90 unsigned GetGroup(unsigned Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +000091
David Goodwin34877712009-10-26 19:32:42 +000092 // GetGroupRegs - Return a vector of the registers belonging to a
David Goodwin87d21b92009-11-13 19:52:48 +000093 // group. If RegRefs is non-NULL then only included referenced registers.
94 void GetGroupRegs(
95 unsigned Group,
96 std::vector<unsigned> &Regs,
Jim Grosbach2973b572010-01-06 16:48:02 +000097 std::multimap<unsigned,
98 AggressiveAntiDepState::RegisterReference> *RegRefs);
David Goodwin34877712009-10-26 19:32:42 +000099
100 // UnionGroups - Union Reg1's and Reg2's groups to form a new
101 // group. Return the index of the GroupNode representing the
102 // group.
103 unsigned UnionGroups(unsigned Reg1, unsigned Reg2);
104
105 // LeaveGroup - Remove a register from its current group and place
106 // it alone in its own group. Return the index of the GroupNode
107 // representing the registers new group.
108 unsigned LeaveGroup(unsigned Reg);
109
110 /// IsLive - Return true if Reg is live
111 bool IsLive(unsigned Reg);
David Goodwine10deca2009-10-26 22:31:16 +0000112 };
113
114
Jim Grosbach2973b572010-01-06 16:48:02 +0000115 /// Class AggressiveAntiDepBreaker
David Goodwine10deca2009-10-26 22:31:16 +0000116 class AggressiveAntiDepBreaker : public AntiDepBreaker {
117 MachineFunction& MF;
118 MachineRegisterInfo &MRI;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000119 const TargetInstrInfo *TII;
David Goodwine10deca2009-10-26 22:31:16 +0000120 const TargetRegisterInfo *TRI;
121
122 /// AllocatableSet - The set of allocatable registers.
123 /// We'll be ignoring anti-dependencies on non-allocatable registers,
124 /// because they may not be safe to break.
David Goodwin87d21b92009-11-13 19:52:48 +0000125 const BitVector AllocatableSet;
126
127 /// CriticalPathSet - The set of registers that should only be
128 /// renamed if they are on the critical path.
129 BitVector CriticalPathSet;
David Goodwine10deca2009-10-26 22:31:16 +0000130
131 /// State - The state used to identify and rename anti-dependence
132 /// registers.
133 AggressiveAntiDepState *State;
134
David Goodwine10deca2009-10-26 22:31:16 +0000135 public:
Jim Grosbach2973b572010-01-06 16:48:02 +0000136 AggressiveAntiDepBreaker(MachineFunction& MFi,
David Goodwin87d21b92009-11-13 19:52:48 +0000137 TargetSubtarget::RegClassVector& CriticalPathRCs);
David Goodwine10deca2009-10-26 22:31:16 +0000138 ~AggressiveAntiDepBreaker();
Jim Grosbach2973b572010-01-06 16:48:02 +0000139
David Goodwine10deca2009-10-26 22:31:16 +0000140 /// Start - Initialize anti-dep breaking for a new basic block.
141 void StartBlock(MachineBasicBlock *BB);
142
Jim Grosbach2973b572010-01-06 16:48:02 +0000143 /// BreakAntiDependencies - Identifiy anti-dependencies along the critical
144 /// path
David Goodwine10deca2009-10-26 22:31:16 +0000145 /// of the ScheduleDAG and break them by renaming registers.
146 ///
Dan Gohman66db3a02010-04-19 23:11:58 +0000147 unsigned BreakAntiDependencies(const std::vector<SUnit>& SUnits,
148 MachineBasicBlock::iterator Begin,
149 MachineBasicBlock::iterator End,
David Goodwine10deca2009-10-26 22:31:16 +0000150 unsigned InsertPosIndex);
151
152 /// Observe - Update liveness information to account for the current
153 /// instruction, which will not be scheduled.
154 ///
155 void Observe(MachineInstr *MI, unsigned Count, unsigned InsertPosIndex);
156
157 /// Finish - Finish anti-dep breaking for a basic block.
158 void FinishBlock();
159
160 private:
David Goodwin54097832009-11-05 01:19:35 +0000161 typedef std::map<const TargetRegisterClass *,
162 TargetRegisterClass::const_iterator> RenameOrderType;
163
David Goodwin34877712009-10-26 19:32:42 +0000164 /// IsImplicitDefUse - Return true if MO represents a register
165 /// that is both implicitly used and defined in MI
166 bool IsImplicitDefUse(MachineInstr *MI, MachineOperand& MO);
Jim Grosbach2973b572010-01-06 16:48:02 +0000167
David Goodwin34877712009-10-26 19:32:42 +0000168 /// GetPassthruRegs - If MI implicitly def/uses a register, then
169 /// return that register and all subregisters.
170 void GetPassthruRegs(MachineInstr *MI, std::set<unsigned>& PassthruRegs);
171
David Goodwin3e72d302009-11-19 23:12:37 +0000172 void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag,
173 const char *header =NULL, const char *footer =NULL);
174
David Goodwin34877712009-10-26 19:32:42 +0000175 void PrescanInstruction(MachineInstr *MI, unsigned Count,
176 std::set<unsigned>& PassthruRegs);
177 void ScanInstruction(MachineInstr *MI, unsigned Count);
178 BitVector GetRenameRegisters(unsigned Reg);
179 bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,
David Goodwin54097832009-11-05 01:19:35 +0000180 RenameOrderType& RenameOrder,
David Goodwin34877712009-10-26 19:32:42 +0000181 std::map<unsigned, unsigned> &RenameMap);
182 };
183}
184
185#endif