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