Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 1 | //===----- AggressiveAntiDepBreaker.cpp - Anti-dep breaker ----------------===// |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 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 | |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "post-RA-sched" |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 18 | #include "AggressiveAntiDepBreaker.h" |
| 19 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineInstr.h" |
| 22 | #include "llvm/Target/TargetInstrInfo.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | 46df4eb | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetInstrInfo.h" |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 31 | // If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod |
| 32 | static cl::opt<int> |
| 33 | DebugDiv("agg-antidep-debugdiv", |
Bob Wilson | 347fa3f | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 34 | cl::desc("Debug control for aggressive anti-dep breaker"), |
| 35 | cl::init(0), cl::Hidden); |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 36 | static cl::opt<int> |
| 37 | DebugMod("agg-antidep-debugmod", |
Bob Wilson | 347fa3f | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 38 | cl::desc("Debug control for aggressive anti-dep breaker"), |
| 39 | cl::init(0), cl::Hidden); |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 40 | |
David Goodwin | 990d285 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 41 | AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs, |
| 42 | MachineBasicBlock *BB) : |
| 43 | NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0) { |
Bill Wendling | e010409 | 2010-07-15 06:04:38 +0000 | [diff] [blame^] | 44 | KillIndices.reserve(TargetRegs); |
| 45 | DefIndices.reserve(TargetRegs); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 46 | |
David Goodwin | 990d285 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 47 | const unsigned BBSize = BB->size(); |
| 48 | for (unsigned i = 0; i < NumTargetRegs; ++i) { |
| 49 | // Initialize all registers to be in their own group. Initially we |
| 50 | // assign the register to the same-indexed GroupNode. |
| 51 | GroupNodeIndices[i] = i; |
| 52 | // Initialize the indices to indicate that no registers are live. |
| 53 | KillIndices[i] = ~0u; |
| 54 | DefIndices[i] = BBSize; |
| 55 | } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 56 | } |
| 57 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 58 | unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 59 | { |
| 60 | unsigned Node = GroupNodeIndices[Reg]; |
| 61 | while (GroupNodes[Node] != Node) |
| 62 | Node = GroupNodes[Node]; |
| 63 | |
| 64 | return Node; |
| 65 | } |
| 66 | |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 67 | void AggressiveAntiDepState::GetGroupRegs( |
| 68 | unsigned Group, |
| 69 | std::vector<unsigned> &Regs, |
| 70 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs) |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 71 | { |
David Goodwin | 990d285 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 72 | for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) { |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 73 | if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0)) |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 74 | Regs.push_back(Reg); |
| 75 | } |
| 76 | } |
| 77 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 78 | unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2) |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 79 | { |
| 80 | assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!"); |
| 81 | assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!"); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 82 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 83 | // find group for each register |
| 84 | unsigned Group1 = GetGroup(Reg1); |
| 85 | unsigned Group2 = GetGroup(Reg2); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 86 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 87 | // if either group is 0, then that must become the parent |
| 88 | unsigned Parent = (Group1 == 0) ? Group1 : Group2; |
| 89 | unsigned Other = (Parent == Group1) ? Group2 : Group1; |
| 90 | GroupNodes.at(Other) = Parent; |
| 91 | return Parent; |
| 92 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 93 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 94 | unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg) |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 95 | { |
| 96 | // Create a new GroupNode for Reg. Reg's existing GroupNode must |
| 97 | // stay as is because there could be other GroupNodes referring to |
| 98 | // it. |
| 99 | unsigned idx = GroupNodes.size(); |
| 100 | GroupNodes.push_back(idx); |
| 101 | GroupNodeIndices[Reg] = idx; |
| 102 | return idx; |
| 103 | } |
| 104 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 105 | bool AggressiveAntiDepState::IsLive(unsigned Reg) |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 106 | { |
| 107 | // KillIndex must be defined and DefIndex not defined for a register |
| 108 | // to be live. |
| 109 | return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u)); |
| 110 | } |
| 111 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | AggressiveAntiDepBreaker:: |
David Goodwin | 0855dee | 2009-11-10 00:15:47 +0000 | [diff] [blame] | 115 | AggressiveAntiDepBreaker(MachineFunction& MFi, |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 116 | TargetSubtarget::RegClassVector& CriticalPathRCs) : |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 117 | AntiDepBreaker(), MF(MFi), |
| 118 | MRI(MF.getRegInfo()), |
Evan Cheng | 46df4eb | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 119 | TII(MF.getTarget().getInstrInfo()), |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 120 | TRI(MF.getTarget().getRegisterInfo()), |
| 121 | AllocatableSet(TRI->getAllocatableSet(MF)), |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 122 | State(NULL) { |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 123 | /* Collect a bitset of all registers that are only broken if they |
| 124 | are on the critical path. */ |
| 125 | for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) { |
| 126 | BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]); |
| 127 | if (CriticalPathSet.none()) |
| 128 | CriticalPathSet = CPSet; |
| 129 | else |
| 130 | CriticalPathSet |= CPSet; |
| 131 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 132 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 133 | DEBUG(dbgs() << "AntiDep Critical-Path Registers:"); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 134 | DEBUG(for (int r = CriticalPathSet.find_first(); r != -1; |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 135 | r = CriticalPathSet.find_next(r)) |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 136 | dbgs() << " " << TRI->getName(r)); |
| 137 | DEBUG(dbgs() << '\n'); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() { |
| 141 | delete State; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) { |
| 145 | assert(State == NULL); |
David Goodwin | 990d285 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 146 | State = new AggressiveAntiDepState(TRI->getNumRegs(), BB); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 147 | |
| 148 | bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn()); |
Bill Wendling | e010409 | 2010-07-15 06:04:38 +0000 | [diff] [blame^] | 149 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 150 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 151 | |
| 152 | // Determine the live-out physregs for this block. |
| 153 | if (IsReturnBlock) { |
| 154 | // In a return block, examine the function live-out regs. |
| 155 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 156 | E = MRI.liveout_end(); I != E; ++I) { |
| 157 | unsigned Reg = *I; |
| 158 | State->UnionGroups(Reg, 0); |
| 159 | KillIndices[Reg] = BB->size(); |
| 160 | DefIndices[Reg] = ~0u; |
| 161 | // Repeat, for all aliases. |
| 162 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 163 | unsigned AliasReg = *Alias; |
| 164 | State->UnionGroups(AliasReg, 0); |
| 165 | KillIndices[AliasReg] = BB->size(); |
| 166 | DefIndices[AliasReg] = ~0u; |
| 167 | } |
| 168 | } |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Evan Cheng | 46df4eb | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 171 | // In a non-return block, examine the live-in regs of all successors. |
| 172 | // Note a return block can have successors if the return instruction is |
| 173 | // predicated. |
| 174 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 175 | SE = BB->succ_end(); SI != SE; ++SI) |
| 176 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 177 | E = (*SI)->livein_end(); I != E; ++I) { |
| 178 | unsigned Reg = *I; |
| 179 | State->UnionGroups(Reg, 0); |
| 180 | KillIndices[Reg] = BB->size(); |
| 181 | DefIndices[Reg] = ~0u; |
| 182 | // Repeat, for all aliases. |
| 183 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 184 | unsigned AliasReg = *Alias; |
| 185 | State->UnionGroups(AliasReg, 0); |
| 186 | KillIndices[AliasReg] = BB->size(); |
| 187 | DefIndices[AliasReg] = ~0u; |
| 188 | } |
| 189 | } |
| 190 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 191 | // Mark live-out callee-saved registers. In a return block this is |
| 192 | // all callee-saved registers. In non-return this is any |
| 193 | // callee-saved register that is not saved in the prolog. |
| 194 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 195 | BitVector Pristine = MFI->getPristineRegs(BB); |
| 196 | for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) { |
| 197 | unsigned Reg = *I; |
| 198 | if (!IsReturnBlock && !Pristine.test(Reg)) continue; |
| 199 | State->UnionGroups(Reg, 0); |
| 200 | KillIndices[Reg] = BB->size(); |
| 201 | DefIndices[Reg] = ~0u; |
| 202 | // Repeat, for all aliases. |
| 203 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 204 | unsigned AliasReg = *Alias; |
| 205 | State->UnionGroups(AliasReg, 0); |
| 206 | KillIndices[AliasReg] = BB->size(); |
| 207 | DefIndices[AliasReg] = ~0u; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void AggressiveAntiDepBreaker::FinishBlock() { |
| 213 | delete State; |
| 214 | State = NULL; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count, |
Bob Wilson | 347fa3f | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 218 | unsigned InsertPosIndex) { |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 219 | assert(Count < InsertPosIndex && "Instruction index out of expected range!"); |
| 220 | |
David Goodwin | 5b3c308 | 2009-10-29 23:30:59 +0000 | [diff] [blame] | 221 | std::set<unsigned> PassthruRegs; |
| 222 | GetPassthruRegs(MI, PassthruRegs); |
| 223 | PrescanInstruction(MI, Count, PassthruRegs); |
| 224 | ScanInstruction(MI, Count); |
| 225 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 226 | DEBUG(dbgs() << "Observe: "); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 227 | DEBUG(MI->dump()); |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 228 | DEBUG(dbgs() << "\tRegs:"); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 229 | |
Bill Wendling | e010409 | 2010-07-15 06:04:38 +0000 | [diff] [blame^] | 230 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
David Goodwin | 990d285 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 231 | for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) { |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 232 | // If Reg is current live, then mark that it can't be renamed as |
| 233 | // we don't know the extent of its live-range anymore (now that it |
| 234 | // has been scheduled). If it is not live but was defined in the |
| 235 | // previous schedule region, then set its def index to the most |
| 236 | // conservative location (i.e. the beginning of the previous |
| 237 | // schedule region). |
| 238 | if (State->IsLive(Reg)) { |
| 239 | DEBUG(if (State->GetGroup(Reg) != 0) |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 240 | dbgs() << " " << TRI->getName(Reg) << "=g" << |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 241 | State->GetGroup(Reg) << "->g0(region live-out)"); |
| 242 | State->UnionGroups(Reg, 0); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 243 | } else if ((DefIndices[Reg] < InsertPosIndex) |
| 244 | && (DefIndices[Reg] >= Count)) { |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 245 | DefIndices[Reg] = Count; |
| 246 | } |
| 247 | } |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 248 | DEBUG(dbgs() << '\n'); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 249 | } |
| 250 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 251 | bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI, |
Bob Wilson | 347fa3f | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 252 | MachineOperand& MO) |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 253 | { |
| 254 | if (!MO.isReg() || !MO.isImplicit()) |
| 255 | return false; |
| 256 | |
| 257 | unsigned Reg = MO.getReg(); |
| 258 | if (Reg == 0) |
| 259 | return false; |
| 260 | |
| 261 | MachineOperand *Op = NULL; |
| 262 | if (MO.isDef()) |
| 263 | Op = MI->findRegisterUseOperand(Reg, true); |
| 264 | else |
| 265 | Op = MI->findRegisterDefOperand(Reg); |
| 266 | |
| 267 | return((Op != NULL) && Op->isImplicit()); |
| 268 | } |
| 269 | |
| 270 | void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI, |
| 271 | std::set<unsigned>& PassthruRegs) { |
| 272 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 273 | MachineOperand &MO = MI->getOperand(i); |
| 274 | if (!MO.isReg()) continue; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 275 | if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) || |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 276 | IsImplicitDefUse(MI, MO)) { |
| 277 | const unsigned Reg = MO.getReg(); |
| 278 | PassthruRegs.insert(Reg); |
| 279 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 280 | *Subreg; ++Subreg) { |
| 281 | PassthruRegs.insert(*Subreg); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 287 | /// AntiDepEdges - Return in Edges the anti- and output- dependencies |
| 288 | /// in SU that we want to consider for breaking. |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 289 | static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) { |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 290 | SmallSet<unsigned, 4> RegSet; |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 291 | for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 292 | P != PE; ++P) { |
David Goodwin | 12dd99d | 2009-11-12 19:08:21 +0000 | [diff] [blame] | 293 | if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) { |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 294 | unsigned Reg = P->getReg(); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 295 | if (RegSet.count(Reg) == 0) { |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 296 | Edges.push_back(&*P); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 297 | RegSet.insert(Reg); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 303 | /// CriticalPathStep - Return the next SUnit after SU on the bottom-up |
| 304 | /// critical path. |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 305 | static const SUnit *CriticalPathStep(const SUnit *SU) { |
| 306 | const SDep *Next = 0; |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 307 | unsigned NextDepth = 0; |
| 308 | // Find the predecessor edge with the greatest depth. |
| 309 | if (SU != 0) { |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 310 | for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 311 | P != PE; ++P) { |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 312 | const SUnit *PredSU = P->getSUnit(); |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 313 | unsigned PredLatency = P->getLatency(); |
| 314 | unsigned PredTotalLatency = PredSU->getDepth() + PredLatency; |
| 315 | // In the case of a latency tie, prefer an anti-dependency edge over |
| 316 | // other types of edges. |
| 317 | if (NextDepth < PredTotalLatency || |
| 318 | (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) { |
| 319 | NextDepth = PredTotalLatency; |
| 320 | Next = &*P; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return (Next) ? Next->getSUnit() : 0; |
| 326 | } |
| 327 | |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 328 | void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx, |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 329 | const char *tag, |
| 330 | const char *header, |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 331 | const char *footer) { |
Bill Wendling | e010409 | 2010-07-15 06:04:38 +0000 | [diff] [blame^] | 332 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 333 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 334 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 335 | RegRefs = State->GetRegRefs(); |
| 336 | |
| 337 | if (!State->IsLive(Reg)) { |
| 338 | KillIndices[Reg] = KillIdx; |
| 339 | DefIndices[Reg] = ~0u; |
| 340 | RegRefs.erase(Reg); |
| 341 | State->LeaveGroup(Reg); |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 342 | DEBUG(if (header != NULL) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 343 | dbgs() << header << TRI->getName(Reg); header = NULL; }); |
| 344 | DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag); |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 345 | } |
| 346 | // Repeat for subregisters. |
| 347 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 348 | *Subreg; ++Subreg) { |
| 349 | unsigned SubregReg = *Subreg; |
| 350 | if (!State->IsLive(SubregReg)) { |
| 351 | KillIndices[SubregReg] = KillIdx; |
| 352 | DefIndices[SubregReg] = ~0u; |
| 353 | RegRefs.erase(SubregReg); |
| 354 | State->LeaveGroup(SubregReg); |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 355 | DEBUG(if (header != NULL) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 356 | dbgs() << header << TRI->getName(Reg); header = NULL; }); |
| 357 | DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" << |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 358 | State->GetGroup(SubregReg) << tag); |
| 359 | } |
| 360 | } |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 361 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 362 | DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer); |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 365 | void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI, |
| 366 | unsigned Count, |
Bob Wilson | 347fa3f | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 367 | std::set<unsigned>& PassthruRegs) { |
Bill Wendling | e010409 | 2010-07-15 06:04:38 +0000 | [diff] [blame^] | 368 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 369 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 370 | RegRefs = State->GetRegRefs(); |
| 371 | |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 372 | // Handle dead defs by simulating a last-use of the register just |
| 373 | // after the def. A dead def can occur because the def is truely |
| 374 | // dead, or because only a subregister is live at the def. If we |
| 375 | // don't do this the dead def will be incorrectly merged into the |
| 376 | // previous def. |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 377 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 378 | MachineOperand &MO = MI->getOperand(i); |
| 379 | if (!MO.isReg() || !MO.isDef()) continue; |
| 380 | unsigned Reg = MO.getReg(); |
| 381 | if (Reg == 0) continue; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 382 | |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 383 | HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 384 | } |
| 385 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 386 | DEBUG(dbgs() << "\tDef Groups:"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 387 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 388 | MachineOperand &MO = MI->getOperand(i); |
| 389 | if (!MO.isReg() || !MO.isDef()) continue; |
| 390 | unsigned Reg = MO.getReg(); |
| 391 | if (Reg == 0) continue; |
| 392 | |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 393 | DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg)); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 394 | |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 395 | // If MI's defs have a special allocation requirement, don't allow |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 396 | // any def registers to be changed. Also assume all registers |
| 397 | // defined in a call must not be changed (ABI). |
Evan Cheng | 46df4eb | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 398 | if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq() || |
| 399 | TII->isPredicated(MI)) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 400 | DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)"); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 401 | State->UnionGroups(Reg, 0); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // Any aliased that are live at this point are completely or |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 405 | // partially defined here, so group those aliases with Reg. |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 406 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 407 | unsigned AliasReg = *Alias; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 408 | if (State->IsLive(AliasReg)) { |
| 409 | State->UnionGroups(Reg, AliasReg); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 410 | DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " << |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 411 | TRI->getName(AliasReg) << ")"); |
| 412 | } |
| 413 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 414 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 415 | // Note register reference... |
| 416 | const TargetRegisterClass *RC = NULL; |
| 417 | if (i < MI->getDesc().getNumOperands()) |
| 418 | RC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 419 | AggressiveAntiDepState::RegisterReference RR = { &MO, RC }; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 420 | RegRefs.insert(std::make_pair(Reg, RR)); |
| 421 | } |
| 422 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 423 | DEBUG(dbgs() << '\n'); |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 424 | |
| 425 | // Scan the register defs for this instruction and update |
| 426 | // live-ranges. |
| 427 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 428 | MachineOperand &MO = MI->getOperand(i); |
| 429 | if (!MO.isReg() || !MO.isDef()) continue; |
| 430 | unsigned Reg = MO.getReg(); |
| 431 | if (Reg == 0) continue; |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 432 | // Ignore KILLs and passthru registers for liveness... |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 433 | if (MI->isKill() || (PassthruRegs.count(Reg) != 0)) |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 434 | continue; |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 435 | |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 436 | // Update def for Reg and aliases. |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 437 | DefIndices[Reg] = Count; |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 438 | for (const unsigned *Alias = TRI->getAliasSet(Reg); |
| 439 | *Alias; ++Alias) { |
| 440 | unsigned AliasReg = *Alias; |
| 441 | DefIndices[AliasReg] = Count; |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI, |
Bob Wilson | 347fa3f | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 447 | unsigned Count) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 448 | DEBUG(dbgs() << "\tUse Groups:"); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 449 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 450 | RegRefs = State->GetRegRefs(); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 451 | |
Evan Cheng | 46df4eb | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 452 | // If MI's uses have special allocation requirement, don't allow |
| 453 | // any use registers to be changed. Also assume all registers |
| 454 | // used in a call must not be changed (ABI). |
| 455 | // FIXME: The issue with predicated instruction is more complex. We are being |
| 456 | // conservatively here because the kill markers cannot be trusted after |
| 457 | // if-conversion: |
| 458 | // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14] |
| 459 | // ... |
| 460 | // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395] |
| 461 | // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12] |
| 462 | // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8) |
| 463 | // |
| 464 | // The first R6 kill is not really a kill since it's killed by a predicated |
| 465 | // instruction which may not be executed. The second R6 def may or may not |
| 466 | // re-define R6 so it's not safe to change it since the last R6 use cannot be |
| 467 | // changed. |
| 468 | bool Special = MI->getDesc().isCall() || |
| 469 | MI->getDesc().hasExtraSrcRegAllocReq() || |
| 470 | TII->isPredicated(MI); |
| 471 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 472 | // Scan the register uses for this instruction and update |
| 473 | // live-ranges, groups and RegRefs. |
| 474 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 475 | MachineOperand &MO = MI->getOperand(i); |
| 476 | if (!MO.isReg() || !MO.isUse()) continue; |
| 477 | unsigned Reg = MO.getReg(); |
| 478 | if (Reg == 0) continue; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 479 | |
| 480 | DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << |
| 481 | State->GetGroup(Reg)); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 482 | |
| 483 | // It wasn't previously live but now it is, this is a kill. Forget |
| 484 | // the previous live-range information and start a new live-range |
| 485 | // for the register. |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 486 | HandleLastUse(Reg, Count, "(last-use)"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 487 | |
Evan Cheng | 46df4eb | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 488 | if (Special) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 489 | DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)"); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 490 | State->UnionGroups(Reg, 0); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // Note register reference... |
| 494 | const TargetRegisterClass *RC = NULL; |
| 495 | if (i < MI->getDesc().getNumOperands()) |
| 496 | RC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 497 | AggressiveAntiDepState::RegisterReference RR = { &MO, RC }; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 498 | RegRefs.insert(std::make_pair(Reg, RR)); |
| 499 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 500 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 501 | DEBUG(dbgs() << '\n'); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 502 | |
| 503 | // Form a group of all defs and uses of a KILL instruction to ensure |
| 504 | // that all registers are renamed as a group. |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 505 | if (MI->isKill()) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 506 | DEBUG(dbgs() << "\tKill Group:"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 507 | |
| 508 | unsigned FirstReg = 0; |
| 509 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 510 | MachineOperand &MO = MI->getOperand(i); |
| 511 | if (!MO.isReg()) continue; |
| 512 | unsigned Reg = MO.getReg(); |
| 513 | if (Reg == 0) continue; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 514 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 515 | if (FirstReg != 0) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 516 | DEBUG(dbgs() << "=" << TRI->getName(Reg)); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 517 | State->UnionGroups(FirstReg, Reg); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 518 | } else { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 519 | DEBUG(dbgs() << " " << TRI->getName(Reg)); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 520 | FirstReg = Reg; |
| 521 | } |
| 522 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 523 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 524 | DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n'); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) { |
| 529 | BitVector BV(TRI->getNumRegs(), false); |
| 530 | bool first = true; |
| 531 | |
| 532 | // Check all references that need rewriting for Reg. For each, use |
| 533 | // the corresponding register class to narrow the set of registers |
| 534 | // that are appropriate for renaming. |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 535 | std::pair<std::multimap<unsigned, |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 536 | AggressiveAntiDepState::RegisterReference>::iterator, |
| 537 | std::multimap<unsigned, |
| 538 | AggressiveAntiDepState::RegisterReference>::iterator> |
| 539 | Range = State->GetRegRefs().equal_range(Reg); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 540 | for (std::multimap<unsigned, |
| 541 | AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first, |
| 542 | QE = Range.second; Q != QE; ++Q) { |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 543 | const TargetRegisterClass *RC = Q->second.RC; |
| 544 | if (RC == NULL) continue; |
| 545 | |
| 546 | BitVector RCBV = TRI->getAllocatableSet(MF, RC); |
| 547 | if (first) { |
| 548 | BV |= RCBV; |
| 549 | first = false; |
| 550 | } else { |
| 551 | BV &= RCBV; |
| 552 | } |
| 553 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 554 | DEBUG(dbgs() << " " << RC->getName()); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 555 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 556 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 557 | return BV; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 558 | } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 559 | |
| 560 | bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters( |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 561 | unsigned AntiDepGroupIndex, |
| 562 | RenameOrderType& RenameOrder, |
| 563 | std::map<unsigned, unsigned> &RenameMap) { |
Bill Wendling | e010409 | 2010-07-15 06:04:38 +0000 | [diff] [blame^] | 564 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 565 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 566 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 567 | RegRefs = State->GetRegRefs(); |
| 568 | |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 569 | // Collect all referenced registers in the same group as |
| 570 | // AntiDepReg. These all need to be renamed together if we are to |
| 571 | // break the anti-dependence. |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 572 | std::vector<unsigned> Regs; |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 573 | State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 574 | assert(Regs.size() > 0 && "Empty register group!"); |
| 575 | if (Regs.size() == 0) |
| 576 | return false; |
| 577 | |
| 578 | // Find the "superest" register in the group. At the same time, |
| 579 | // collect the BitVector of registers that can be used to rename |
| 580 | // each register. |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 581 | DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex |
| 582 | << ":\n"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 583 | std::map<unsigned, BitVector> RenameRegisterMap; |
| 584 | unsigned SuperReg = 0; |
| 585 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 586 | unsigned Reg = Regs[i]; |
| 587 | if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg)) |
| 588 | SuperReg = Reg; |
| 589 | |
| 590 | // If Reg has any references, then collect possible rename regs |
| 591 | if (RegRefs.count(Reg) > 0) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 592 | DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":"); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 593 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 594 | BitVector BV = GetRenameRegisters(Reg); |
| 595 | RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV)); |
| 596 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 597 | DEBUG(dbgs() << " ::"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 598 | DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r)) |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 599 | dbgs() << " " << TRI->getName(r)); |
| 600 | DEBUG(dbgs() << "\n"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | // All group registers should be a subreg of SuperReg. |
| 605 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 606 | unsigned Reg = Regs[i]; |
| 607 | if (Reg == SuperReg) continue; |
| 608 | bool IsSub = TRI->isSubRegister(SuperReg, Reg); |
| 609 | assert(IsSub && "Expecting group subregister"); |
| 610 | if (!IsSub) |
| 611 | return false; |
| 612 | } |
| 613 | |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 614 | #ifndef NDEBUG |
| 615 | // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod |
| 616 | if (DebugDiv > 0) { |
| 617 | static int renamecnt = 0; |
| 618 | if (renamecnt++ % DebugDiv != DebugMod) |
| 619 | return false; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 620 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 621 | dbgs() << "*** Performing rename " << TRI->getName(SuperReg) << |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 622 | " for debug ***\n"; |
| 623 | } |
| 624 | #endif |
| 625 | |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 626 | // Check each possible rename register for SuperReg in round-robin |
| 627 | // order. If that register is available, and the corresponding |
| 628 | // registers are available for the other group subregisters, then we |
| 629 | // can use those registers to rename. |
Rafael Espindola | 7e1b566 | 2010-07-12 02:55:34 +0000 | [diff] [blame] | 630 | |
| 631 | // FIXME: Using getMinimalPhysRegClass is very conservative. We should |
| 632 | // check every use of the register and find the largest register class |
| 633 | // that can be used in all of them. |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 634 | const TargetRegisterClass *SuperRC = |
Rafael Espindola | 7e1b566 | 2010-07-12 02:55:34 +0000 | [diff] [blame] | 635 | TRI->getMinimalPhysRegClass(SuperReg, MVT::Other); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 636 | |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 637 | const TargetRegisterClass::iterator RB = SuperRC->allocation_order_begin(MF); |
| 638 | const TargetRegisterClass::iterator RE = SuperRC->allocation_order_end(MF); |
| 639 | if (RB == RE) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 640 | DEBUG(dbgs() << "\tEmpty Super Regclass!!\n"); |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 641 | return false; |
| 642 | } |
| 643 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 644 | DEBUG(dbgs() << "\tFind Registers:"); |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 645 | |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 646 | if (RenameOrder.count(SuperRC) == 0) |
| 647 | RenameOrder.insert(RenameOrderType::value_type(SuperRC, RE)); |
| 648 | |
David Goodwin | 98f2f1a | 2009-11-05 01:45:50 +0000 | [diff] [blame] | 649 | const TargetRegisterClass::iterator OrigR = RenameOrder[SuperRC]; |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 650 | const TargetRegisterClass::iterator EndR = ((OrigR == RE) ? RB : OrigR); |
| 651 | TargetRegisterClass::iterator R = OrigR; |
| 652 | do { |
| 653 | if (R == RB) R = RE; |
| 654 | --R; |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 655 | const unsigned NewSuperReg = *R; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 656 | // Don't replace a register with itself. |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 657 | if (NewSuperReg == SuperReg) continue; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 658 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 659 | DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':'); |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 660 | RenameMap.clear(); |
| 661 | |
| 662 | // For each referenced group register (which must be a SuperReg or |
| 663 | // a subregister of SuperReg), find the corresponding subregister |
| 664 | // of NewSuperReg and make sure it is free to be renamed. |
| 665 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 666 | unsigned Reg = Regs[i]; |
| 667 | unsigned NewReg = 0; |
| 668 | if (Reg == SuperReg) { |
| 669 | NewReg = NewSuperReg; |
| 670 | } else { |
| 671 | unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg); |
| 672 | if (NewSubRegIdx != 0) |
| 673 | NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 674 | } |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 675 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 676 | DEBUG(dbgs() << " " << TRI->getName(NewReg)); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 677 | |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 678 | // Check if Reg can be renamed to NewReg. |
| 679 | BitVector BV = RenameRegisterMap[Reg]; |
| 680 | if (!BV.test(NewReg)) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 681 | DEBUG(dbgs() << "(no rename)"); |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 682 | goto next_super_reg; |
| 683 | } |
| 684 | |
| 685 | // If NewReg is dead and NewReg's most recent def is not before |
| 686 | // Regs's kill, it's safe to replace Reg with NewReg. We |
| 687 | // must also check all aliases of NewReg, because we can't define a |
| 688 | // register when any sub or super is already live. |
| 689 | if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 690 | DEBUG(dbgs() << "(live)"); |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 691 | goto next_super_reg; |
| 692 | } else { |
| 693 | bool found = false; |
| 694 | for (const unsigned *Alias = TRI->getAliasSet(NewReg); |
| 695 | *Alias; ++Alias) { |
| 696 | unsigned AliasReg = *Alias; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 697 | if (State->IsLive(AliasReg) || |
| 698 | (KillIndices[Reg] > DefIndices[AliasReg])) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 699 | DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)"); |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 700 | found = true; |
| 701 | break; |
| 702 | } |
| 703 | } |
| 704 | if (found) |
| 705 | goto next_super_reg; |
| 706 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 707 | |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 708 | // Record that 'Reg' can be renamed to 'NewReg'. |
| 709 | RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg)); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 710 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 711 | |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 712 | // If we fall-out here, then every register in the group can be |
| 713 | // renamed, as recorded in RenameMap. |
| 714 | RenameOrder.erase(SuperRC); |
| 715 | RenameOrder.insert(RenameOrderType::value_type(SuperRC, R)); |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 716 | DEBUG(dbgs() << "]\n"); |
David Goodwin | 00621ef | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 717 | return true; |
| 718 | |
| 719 | next_super_reg: |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 720 | DEBUG(dbgs() << ']'); |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 721 | } while (R != EndR); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 722 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 723 | DEBUG(dbgs() << '\n'); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 724 | |
| 725 | // No registers are free and available! |
| 726 | return false; |
| 727 | } |
| 728 | |
| 729 | /// BreakAntiDependencies - Identifiy anti-dependencies within the |
| 730 | /// ScheduleDAG and break them by renaming registers. |
| 731 | /// |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 732 | unsigned AggressiveAntiDepBreaker::BreakAntiDependencies( |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 733 | const std::vector<SUnit>& SUnits, |
| 734 | MachineBasicBlock::iterator Begin, |
| 735 | MachineBasicBlock::iterator End, |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 736 | unsigned InsertPosIndex) { |
Bill Wendling | e010409 | 2010-07-15 06:04:38 +0000 | [diff] [blame^] | 737 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 738 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 739 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 740 | RegRefs = State->GetRegRefs(); |
| 741 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 742 | // The code below assumes that there is at least one instruction, |
| 743 | // so just duck out immediately if the block is empty. |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 744 | if (SUnits.empty()) return 0; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 745 | |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 746 | // For each regclass the next register to use for renaming. |
| 747 | RenameOrderType RenameOrder; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 748 | |
| 749 | // ...need a map from MI to SUnit. |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 750 | std::map<MachineInstr *, const SUnit *> MISUnitMap; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 751 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 752 | const SUnit *SU = &SUnits[i]; |
| 753 | MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(), |
| 754 | SU)); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 755 | } |
| 756 | |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 757 | // Track progress along the critical path through the SUnit graph as |
| 758 | // we walk the instructions. This is needed for regclasses that only |
| 759 | // break critical-path anti-dependencies. |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 760 | const SUnit *CriticalPathSU = 0; |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 761 | MachineInstr *CriticalPathMI = 0; |
| 762 | if (CriticalPathSet.any()) { |
| 763 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 764 | const SUnit *SU = &SUnits[i]; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 765 | if (!CriticalPathSU || |
| 766 | ((SU->getDepth() + SU->Latency) > |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 767 | (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) { |
| 768 | CriticalPathSU = SU; |
| 769 | } |
| 770 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 771 | |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 772 | CriticalPathMI = CriticalPathSU->getInstr(); |
| 773 | } |
| 774 | |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 775 | #ifndef NDEBUG |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 776 | DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n"); |
| 777 | DEBUG(dbgs() << "Available regs:"); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 778 | for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) { |
| 779 | if (!State->IsLive(Reg)) |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 780 | DEBUG(dbgs() << " " << TRI->getName(Reg)); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 781 | } |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 782 | DEBUG(dbgs() << '\n'); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 783 | #endif |
| 784 | |
| 785 | // Attempt to break anti-dependence edges. Walk the instructions |
| 786 | // from the bottom up, tracking information about liveness as we go |
| 787 | // to help determine which registers are available. |
| 788 | unsigned Broken = 0; |
| 789 | unsigned Count = InsertPosIndex - 1; |
| 790 | for (MachineBasicBlock::iterator I = End, E = Begin; |
| 791 | I != E; --Count) { |
| 792 | MachineInstr *MI = --I; |
| 793 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 794 | DEBUG(dbgs() << "Anti: "); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 795 | DEBUG(MI->dump()); |
| 796 | |
| 797 | std::set<unsigned> PassthruRegs; |
| 798 | GetPassthruRegs(MI, PassthruRegs); |
| 799 | |
| 800 | // Process the defs in MI... |
| 801 | PrescanInstruction(MI, Count, PassthruRegs); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 802 | |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 803 | // The dependence edges that represent anti- and output- |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 804 | // dependencies that are candidates for breaking. |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 805 | std::vector<const SDep *> Edges; |
| 806 | const SUnit *PathSU = MISUnitMap[MI]; |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 807 | AntiDepEdges(PathSU, Edges); |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 808 | |
| 809 | // If MI is not on the critical path, then we don't rename |
| 810 | // registers in the CriticalPathSet. |
| 811 | BitVector *ExcludeRegs = NULL; |
| 812 | if (MI == CriticalPathMI) { |
| 813 | CriticalPathSU = CriticalPathStep(CriticalPathSU); |
| 814 | CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : 0; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 815 | } else { |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 816 | ExcludeRegs = &CriticalPathSet; |
| 817 | } |
| 818 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 819 | // Ignore KILL instructions (they form a group in ScanInstruction |
| 820 | // but don't cause any anti-dependence breaking themselves) |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 821 | if (!MI->isKill()) { |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 822 | // Attempt to break each anti-dependency... |
| 823 | for (unsigned i = 0, e = Edges.size(); i != e; ++i) { |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 824 | const SDep *Edge = Edges[i]; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 825 | SUnit *NextSU = Edge->getSUnit(); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 826 | |
David Goodwin | 12dd99d | 2009-11-12 19:08:21 +0000 | [diff] [blame] | 827 | if ((Edge->getKind() != SDep::Anti) && |
| 828 | (Edge->getKind() != SDep::Output)) continue; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 829 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 830 | unsigned AntiDepReg = Edge->getReg(); |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 831 | DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg)); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 832 | assert(AntiDepReg != 0 && "Anti-dependence on reg0?"); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 833 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 834 | if (!AllocatableSet.test(AntiDepReg)) { |
| 835 | // Don't break anti-dependencies on non-allocatable registers. |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 836 | DEBUG(dbgs() << " (non-allocatable)\n"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 837 | continue; |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 838 | } else if ((ExcludeRegs != NULL) && ExcludeRegs->test(AntiDepReg)) { |
| 839 | // Don't break anti-dependencies for critical path registers |
| 840 | // if not on the critical path |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 841 | DEBUG(dbgs() << " (not critical-path)\n"); |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 842 | continue; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 843 | } else if (PassthruRegs.count(AntiDepReg) != 0) { |
| 844 | // If the anti-dep register liveness "passes-thru", then |
| 845 | // don't try to change it. It will be changed along with |
| 846 | // the use if required to break an earlier antidep. |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 847 | DEBUG(dbgs() << " (passthru)\n"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 848 | continue; |
| 849 | } else { |
| 850 | // No anti-dep breaking for implicit deps |
| 851 | MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 852 | assert(AntiDepOp != NULL && |
| 853 | "Can't find index for defined register operand"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 854 | if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 855 | DEBUG(dbgs() << " (implicit)\n"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 856 | continue; |
| 857 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 858 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 859 | // If the SUnit has other dependencies on the SUnit that |
| 860 | // it anti-depends on, don't bother breaking the |
| 861 | // anti-dependency since those edges would prevent such |
| 862 | // units from being scheduled past each other |
| 863 | // regardless. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 864 | // |
| 865 | // Also, if there are dependencies on other SUnits with the |
| 866 | // same register as the anti-dependency, don't attempt to |
| 867 | // break it. |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 868 | for (SUnit::const_pred_iterator P = PathSU->Preds.begin(), |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 869 | PE = PathSU->Preds.end(); P != PE; ++P) { |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 870 | if (P->getSUnit() == NextSU ? |
| 871 | (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) : |
| 872 | (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) { |
| 873 | AntiDepReg = 0; |
| 874 | break; |
| 875 | } |
| 876 | } |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 877 | for (SUnit::const_pred_iterator P = PathSU->Preds.begin(), |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 878 | PE = PathSU->Preds.end(); P != PE; ++P) { |
| 879 | if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) && |
| 880 | (P->getKind() != SDep::Output)) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 881 | DEBUG(dbgs() << " (real dependency)\n"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 882 | AntiDepReg = 0; |
| 883 | break; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 884 | } else if ((P->getSUnit() != NextSU) && |
| 885 | (P->getKind() == SDep::Data) && |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 886 | (P->getReg() == AntiDepReg)) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 887 | DEBUG(dbgs() << " (other dependency)\n"); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 888 | AntiDepReg = 0; |
| 889 | break; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 892 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 893 | if (AntiDepReg == 0) continue; |
| 894 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 895 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 896 | assert(AntiDepReg != 0); |
| 897 | if (AntiDepReg == 0) continue; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 898 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 899 | // Determine AntiDepReg's register group. |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 900 | const unsigned GroupIndex = State->GetGroup(AntiDepReg); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 901 | if (GroupIndex == 0) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 902 | DEBUG(dbgs() << " (zero group)\n"); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 903 | continue; |
| 904 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 905 | |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 906 | DEBUG(dbgs() << '\n'); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 907 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 908 | // Look for a suitable register to use to break the anti-dependence. |
| 909 | std::map<unsigned, unsigned> RenameMap; |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 910 | if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) { |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 911 | DEBUG(dbgs() << "\tBreaking anti-dependence edge on " |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 912 | << TRI->getName(AntiDepReg) << ":"); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 913 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 914 | // Handle each group register... |
| 915 | for (std::map<unsigned, unsigned>::iterator |
| 916 | S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) { |
| 917 | unsigned CurrReg = S->first; |
| 918 | unsigned NewReg = S->second; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 919 | |
| 920 | DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" << |
| 921 | TRI->getName(NewReg) << "(" << |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 922 | RegRefs.count(CurrReg) << " refs)"); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 923 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 924 | // Update the references to the old register CurrReg to |
| 925 | // refer to the new register NewReg. |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 926 | std::pair<std::multimap<unsigned, |
| 927 | AggressiveAntiDepState::RegisterReference>::iterator, |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 928 | std::multimap<unsigned, |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 929 | AggressiveAntiDepState::RegisterReference>::iterator> |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 930 | Range = RegRefs.equal_range(CurrReg); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 931 | for (std::multimap<unsigned, |
| 932 | AggressiveAntiDepState::RegisterReference>::iterator |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 933 | Q = Range.first, QE = Range.second; Q != QE; ++Q) { |
| 934 | Q->second.Operand->setReg(NewReg); |
Jim Grosbach | 533934e | 2010-06-01 23:48:44 +0000 | [diff] [blame] | 935 | // If the SU for the instruction being updated has debug |
| 936 | // information related to the anti-dependency register, make |
| 937 | // sure to update that as well. |
| 938 | const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()]; |
Jim Grosbach | 086723d | 2010-06-02 15:29:36 +0000 | [diff] [blame] | 939 | if (!SU) continue; |
Jim Grosbach | 533934e | 2010-06-01 23:48:44 +0000 | [diff] [blame] | 940 | for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i) { |
| 941 | MachineInstr *DI = SU->DbgInstrList[i]; |
| 942 | assert (DI->getNumOperands()==3 && DI->getOperand(0).isReg() && |
| 943 | DI->getOperand(0).getReg() |
| 944 | && "Non register dbg_value attached to SUnit!"); |
| 945 | if (DI->getOperand(0).getReg() == AntiDepReg) |
| 946 | DI->getOperand(0).setReg(NewReg); |
| 947 | } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 948 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 949 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 950 | // We just went back in time and modified history; the |
| 951 | // liveness information for CurrReg is now inconsistent. Set |
| 952 | // the state as if it were dead. |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 953 | State->UnionGroups(NewReg, 0); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 954 | RegRefs.erase(NewReg); |
| 955 | DefIndices[NewReg] = DefIndices[CurrReg]; |
| 956 | KillIndices[NewReg] = KillIndices[CurrReg]; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 957 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 958 | State->UnionGroups(CurrReg, 0); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 959 | RegRefs.erase(CurrReg); |
| 960 | DefIndices[CurrReg] = KillIndices[CurrReg]; |
| 961 | KillIndices[CurrReg] = ~0u; |
| 962 | assert(((KillIndices[CurrReg] == ~0u) != |
| 963 | (DefIndices[CurrReg] == ~0u)) && |
| 964 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
| 965 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 966 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 967 | ++Broken; |
David Greene | 5393b25 | 2009-12-24 00:14:25 +0000 | [diff] [blame] | 968 | DEBUG(dbgs() << '\n'); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | ScanInstruction(MI, Count); |
| 974 | } |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 975 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 976 | return Broken; |
| 977 | } |