blob: 9d715ccf79f8d4fb326fb4e35dc0b1dcfd1134ee [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"
Chris Lattner7fa889b2010-07-15 06:51:46 +000027#include "llvm/Target/TargetRegisterInfo.h"
David Goodwin34877712009-10-26 19:32:42 +000028#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 {
Jim Grosbach2973b572010-01-06 16:48:02 +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 Goodwin990d2852009-12-09 17:18:22 +000047 /// NumTargetRegs - Number of non-virtual target registers
48 /// (i.e. TRI->getNumRegs()).
49 const unsigned NumTargetRegs;
50
David Goodwin34877712009-10-26 19:32:42 +000051 /// GroupNodes - Implements a disjoint-union data structure to
52 /// form register groups. A node is represented by an index into
53 /// the vector. A node can "point to" itself to indicate that it
54 /// is the parent of a group, or point to another node to indicate
55 /// that it is a member of the same group as that node.
56 std::vector<unsigned> GroupNodes;
Jim Grosbach2973b572010-01-06 16:48:02 +000057
David Goodwin34877712009-10-26 19:32:42 +000058 /// GroupNodeIndices - For each register, the index of the GroupNode
59 /// currently representing the group that the register belongs to.
60 /// Register 0 is always represented by the 0 group, a group
61 /// composed of registers that are not eligible for anti-aliasing.
Bill Wendlingdfb4eeb2010-07-15 18:40:50 +000062 std::vector<unsigned> GroupNodeIndices;
Jim Grosbach2973b572010-01-06 16:48:02 +000063
David Goodwine10deca2009-10-26 22:31:16 +000064 /// RegRefs - Map registers to all their references within a live range.
David Goodwin34877712009-10-26 19:32:42 +000065 std::multimap<unsigned, RegisterReference> RegRefs;
Jim Grosbach2973b572010-01-06 16:48:02 +000066
David Goodwin34877712009-10-26 19:32:42 +000067 /// KillIndices - The index of the most recent kill (proceding bottom-up),
68 /// or ~0u if the register is not live.
Bill Wendling38306d52010-07-15 18:43:09 +000069 std::vector<unsigned> KillIndices;
Jim Grosbach2973b572010-01-06 16:48:02 +000070
David Goodwin34877712009-10-26 19:32:42 +000071 /// DefIndices - The index of the most recent complete def (proceding bottom
72 /// up), or ~0u if the register is live.
Bill Wendling38306d52010-07-15 18:43:09 +000073 std::vector<unsigned> DefIndices;
David Goodwin34877712009-10-26 19:32:42 +000074
75 public:
David Goodwin990d2852009-12-09 17:18:22 +000076 AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB);
Jim Grosbach2973b572010-01-06 16:48:02 +000077
David Goodwine10deca2009-10-26 22:31:16 +000078 /// GetKillIndices - Return the kill indices.
Bill Wendling38306d52010-07-15 18:43:09 +000079 std::vector<unsigned> &GetKillIndices() { return KillIndices; }
David Goodwin34877712009-10-26 19:32:42 +000080
David Goodwine10deca2009-10-26 22:31:16 +000081 /// GetDefIndices - Return the define indices.
Bill Wendling38306d52010-07-15 18:43:09 +000082 std::vector<unsigned> &GetDefIndices() { return DefIndices; }
David Goodwin34877712009-10-26 19:32:42 +000083
David Goodwine10deca2009-10-26 22:31:16 +000084 /// GetRegRefs - Return the RegRefs map.
85 std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; }
David Goodwin34877712009-10-26 19:32:42 +000086
David Goodwin34877712009-10-26 19:32:42 +000087 // GetGroup - Get the group for a register. The returned value is
88 // the index of the GroupNode representing the group.
89 unsigned GetGroup(unsigned Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +000090
David Goodwin34877712009-10-26 19:32:42 +000091 // GetGroupRegs - Return a vector of the registers belonging to a
David Goodwin87d21b92009-11-13 19:52:48 +000092 // group. If RegRefs is non-NULL then only included referenced registers.
93 void GetGroupRegs(
94 unsigned Group,
95 std::vector<unsigned> &Regs,
Jim Grosbach2973b572010-01-06 16:48:02 +000096 std::multimap<unsigned,
97 AggressiveAntiDepState::RegisterReference> *RegRefs);
David Goodwin34877712009-10-26 19:32:42 +000098
99 // UnionGroups - Union Reg1's and Reg2's groups to form a new
100 // group. Return the index of the GroupNode representing the
101 // group.
102 unsigned UnionGroups(unsigned Reg1, unsigned Reg2);
103
104 // LeaveGroup - Remove a register from its current group and place
105 // it alone in its own group. Return the index of the GroupNode
106 // representing the registers new group.
107 unsigned LeaveGroup(unsigned Reg);
108
109 /// IsLive - Return true if Reg is live
110 bool IsLive(unsigned Reg);
David Goodwine10deca2009-10-26 22:31:16 +0000111 };
112
113
Jim Grosbach2973b572010-01-06 16:48:02 +0000114 /// Class AggressiveAntiDepBreaker
David Goodwine10deca2009-10-26 22:31:16 +0000115 class AggressiveAntiDepBreaker : public AntiDepBreaker {
116 MachineFunction& MF;
117 MachineRegisterInfo &MRI;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000118 const TargetInstrInfo *TII;
David Goodwine10deca2009-10-26 22:31:16 +0000119 const TargetRegisterInfo *TRI;
120
121 /// AllocatableSet - The set of allocatable registers.
122 /// We'll be ignoring anti-dependencies on non-allocatable registers,
123 /// because they may not be safe to break.
David Goodwin87d21b92009-11-13 19:52:48 +0000124 const BitVector AllocatableSet;
125
126 /// CriticalPathSet - The set of registers that should only be
127 /// renamed if they are on the critical path.
128 BitVector CriticalPathSet;
David Goodwine10deca2009-10-26 22:31:16 +0000129
130 /// State - The state used to identify and rename anti-dependence
131 /// registers.
132 AggressiveAntiDepState *State;
133
David Goodwine10deca2009-10-26 22:31:16 +0000134 public:
Jim Grosbach2973b572010-01-06 16:48:02 +0000135 AggressiveAntiDepBreaker(MachineFunction& MFi,
David Goodwin87d21b92009-11-13 19:52:48 +0000136 TargetSubtarget::RegClassVector& CriticalPathRCs);
David Goodwine10deca2009-10-26 22:31:16 +0000137 ~AggressiveAntiDepBreaker();
Jim Grosbach2973b572010-01-06 16:48:02 +0000138
David Goodwine10deca2009-10-26 22:31:16 +0000139 /// Start - Initialize anti-dep breaking for a new basic block.
140 void StartBlock(MachineBasicBlock *BB);
141
Jim Grosbach2973b572010-01-06 16:48:02 +0000142 /// BreakAntiDependencies - Identifiy anti-dependencies along the critical
143 /// path
David Goodwine10deca2009-10-26 22:31:16 +0000144 /// of the ScheduleDAG and break them by renaming registers.
145 ///
Dan Gohman66db3a02010-04-19 23:11:58 +0000146 unsigned BreakAntiDependencies(const std::vector<SUnit>& SUnits,
147 MachineBasicBlock::iterator Begin,
148 MachineBasicBlock::iterator End,
David Goodwine10deca2009-10-26 22:31:16 +0000149 unsigned InsertPosIndex);
150
151 /// Observe - Update liveness information to account for the current
152 /// instruction, which will not be scheduled.
153 ///
154 void Observe(MachineInstr *MI, unsigned Count, unsigned InsertPosIndex);
155
156 /// Finish - Finish anti-dep breaking for a basic block.
157 void FinishBlock();
158
159 private:
David Goodwin54097832009-11-05 01:19:35 +0000160 typedef std::map<const TargetRegisterClass *,
161 TargetRegisterClass::const_iterator> RenameOrderType;
162
David Goodwin34877712009-10-26 19:32:42 +0000163 /// IsImplicitDefUse - Return true if MO represents a register
164 /// that is both implicitly used and defined in MI
165 bool IsImplicitDefUse(MachineInstr *MI, MachineOperand& MO);
Jim Grosbach2973b572010-01-06 16:48:02 +0000166
David Goodwin34877712009-10-26 19:32:42 +0000167 /// GetPassthruRegs - If MI implicitly def/uses a register, then
168 /// return that register and all subregisters.
169 void GetPassthruRegs(MachineInstr *MI, std::set<unsigned>& PassthruRegs);
170
David Goodwin3e72d302009-11-19 23:12:37 +0000171 void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag,
172 const char *header =NULL, const char *footer =NULL);
173
David Goodwin34877712009-10-26 19:32:42 +0000174 void PrescanInstruction(MachineInstr *MI, unsigned Count,
175 std::set<unsigned>& PassthruRegs);
176 void ScanInstruction(MachineInstr *MI, unsigned Count);
177 BitVector GetRenameRegisters(unsigned Reg);
178 bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,
David Goodwin54097832009-11-05 01:19:35 +0000179 RenameOrderType& RenameOrder,
David Goodwin34877712009-10-26 19:32:42 +0000180 std::map<unsigned, unsigned> &RenameMap);
181 };
182}
183
184#endif