Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 1 | //===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +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 | // |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 10 | // This implements the ScheduleDAGInstrs class, which implements re-scheduling |
| 11 | // of MachineInstrs. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "sched-instrs" |
| 16 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetMachine.h" |
| 19 | #include "llvm/Target/TargetInstrInfo.h" |
| 20 | #include "llvm/Target/TargetRegisterInfo.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 23 | #include <map> |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | ScheduleDAGInstrs::ScheduleDAGInstrs(MachineBasicBlock *bb, |
| 27 | const TargetMachine &tm) |
| 28 | : ScheduleDAG(0, bb, tm) {} |
| 29 | |
| 30 | void ScheduleDAGInstrs::BuildSchedUnits() { |
| 31 | SUnits.clear(); |
| 32 | SUnits.reserve(BB->size()); |
| 33 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 34 | // We build scheduling units by walking a block's instruction list from bottom |
| 35 | // to top. |
| 36 | |
| 37 | // Remember where defs and uses of each physical register are as we procede. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 38 | SUnit *Defs[TargetRegisterInfo::FirstVirtualRegister] = {}; |
| 39 | std::vector<SUnit *> Uses[TargetRegisterInfo::FirstVirtualRegister] = {}; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 40 | |
| 41 | // Remember where unknown loads are after the most recent unknown store |
| 42 | // as we procede. |
| 43 | std::vector<SUnit *> PendingLoads; |
| 44 | |
| 45 | // Remember where a generic side-effecting instruction is as we procede. If |
| 46 | // ChainMMO is null, this is assumed to have arbitrary side-effects. If |
| 47 | // ChainMMO is non-null, then Chain makes only a single memory reference. |
| 48 | SUnit *Chain = 0; |
| 49 | MachineMemOperand *ChainMMO = 0; |
| 50 | |
| 51 | // Memory references to specific known memory locations are tracked so that |
| 52 | // they can be given more precise dependencies. |
| 53 | std::map<const Value *, SUnit *> MemDefs; |
| 54 | std::map<const Value *, std::vector<SUnit *> > MemUses; |
| 55 | |
| 56 | // Terminators can perform control transfers, we we need to make sure that |
| 57 | // all the work of the block is done before the terminator. |
| 58 | SUnit *Terminator = 0; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 59 | |
| 60 | for (MachineBasicBlock::iterator MII = BB->end(), MIE = BB->begin(); |
| 61 | MII != MIE; --MII) { |
| 62 | MachineInstr *MI = prior(MII); |
| 63 | SUnit *SU = NewSUnit(MI); |
| 64 | |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 65 | // Assign the Latency field of SU using target-provided information. |
| 66 | ComputeLatency(SU); |
| 67 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 68 | // Add register-based dependencies (data, anti, and output). |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 69 | for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) { |
| 70 | const MachineOperand &MO = MI->getOperand(j); |
| 71 | if (!MO.isReg()) continue; |
| 72 | unsigned Reg = MO.getReg(); |
| 73 | if (Reg == 0) continue; |
| 74 | |
| 75 | assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!"); |
| 76 | std::vector<SUnit *> &UseList = Uses[Reg]; |
| 77 | SUnit *&Def = Defs[Reg]; |
Dan Gohman | fc626b6 | 2008-11-21 19:16:58 +0000 | [diff] [blame] | 78 | // Optionally add output and anti dependencies. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 79 | // TODO: Using a latency of 1 here assumes there's no cost for |
| 80 | // reusing registers. |
| 81 | SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 82 | if (Def && Def != SU) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 83 | Def->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/Reg)); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 84 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 85 | SUnit *&Def = Defs[*Alias]; |
| 86 | if (Def && Def != SU) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 87 | Def->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/ *Alias)); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if (MO.isDef()) { |
| 91 | // Add any data dependencies. |
| 92 | for (unsigned i = 0, e = UseList.size(); i != e; ++i) |
| 93 | if (UseList[i] != SU) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 94 | UseList[i]->addPred(SDep(SU, SDep::Data, SU->Latency, Reg)); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 95 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 96 | std::vector<SUnit *> &UseList = Uses[*Alias]; |
| 97 | for (unsigned i = 0, e = UseList.size(); i != e; ++i) |
| 98 | if (UseList[i] != SU) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 99 | UseList[i]->addPred(SDep(SU, SDep::Data, SU->Latency, *Alias)); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | UseList.clear(); |
| 103 | Def = SU; |
| 104 | } else { |
| 105 | UseList.push_back(SU); |
| 106 | } |
| 107 | } |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 108 | |
| 109 | // Add chain dependencies. |
| 110 | // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable |
| 111 | // after stack slots are lowered to actual addresses. |
| 112 | // TODO: Use an AliasAnalysis and do real alias-analysis queries, and |
| 113 | // produce more precise dependence information. |
| 114 | const TargetInstrDesc &TID = MI->getDesc(); |
| 115 | if (TID.isCall() || TID.isReturn() || TID.isBranch() || |
| 116 | TID.hasUnmodeledSideEffects()) { |
| 117 | new_chain: |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 118 | // This is the conservative case. Add dependencies on all memory |
| 119 | // references. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 120 | if (Chain) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 121 | Chain->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 122 | Chain = SU; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 123 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 124 | PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 125 | PendingLoads.clear(); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 126 | for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(), |
| 127 | E = MemDefs.end(); I != E; ++I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 128 | I->second->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 129 | I->second = SU; |
| 130 | } |
| 131 | for (std::map<const Value *, std::vector<SUnit *> >::iterator I = |
| 132 | MemUses.begin(), E = MemUses.end(); I != E; ++I) { |
| 133 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 134 | I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 135 | I->second.clear(); |
| 136 | } |
| 137 | // See if it is known to just have a single memory reference. |
| 138 | MachineInstr *ChainMI = Chain->getInstr(); |
| 139 | const TargetInstrDesc &ChainTID = ChainMI->getDesc(); |
| 140 | if (!ChainTID.isCall() && !ChainTID.isReturn() && !ChainTID.isBranch() && |
| 141 | !ChainTID.hasUnmodeledSideEffects() && |
| 142 | ChainMI->hasOneMemOperand() && |
| 143 | !ChainMI->memoperands_begin()->isVolatile() && |
| 144 | ChainMI->memoperands_begin()->getValue()) |
| 145 | // We know that the Chain accesses one specific memory location. |
| 146 | ChainMMO = &*ChainMI->memoperands_begin(); |
| 147 | else |
| 148 | // Unknown memory accesses. Assume the worst. |
| 149 | ChainMMO = 0; |
| 150 | } else if (TID.mayStore()) { |
| 151 | if (MI->hasOneMemOperand() && |
| 152 | MI->memoperands_begin()->getValue() && |
| 153 | !MI->memoperands_begin()->isVolatile() && |
| 154 | isa<PseudoSourceValue>(MI->memoperands_begin()->getValue())) { |
| 155 | // A store to a specific PseudoSourceValue. Add precise dependencies. |
| 156 | const Value *V = MI->memoperands_begin()->getValue(); |
| 157 | // Handle the def in MemDefs, if there is one. |
| 158 | std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V); |
| 159 | if (I != MemDefs.end()) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 160 | I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0, |
| 161 | /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 162 | I->second = SU; |
| 163 | } else { |
| 164 | MemDefs[V] = SU; |
| 165 | } |
| 166 | // Handle the uses in MemUses, if there are any. |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 167 | std::map<const Value *, std::vector<SUnit *> >::iterator J = |
| 168 | MemUses.find(V); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 169 | if (J != MemUses.end()) { |
| 170 | for (unsigned i = 0, e = J->second.size(); i != e; ++i) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 171 | J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0, |
| 172 | /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 173 | J->second.clear(); |
| 174 | } |
| 175 | // Add a general dependence too, if needed. |
| 176 | if (Chain) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 177 | Chain->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 178 | } else |
| 179 | // Treat all other stores conservatively. |
| 180 | goto new_chain; |
| 181 | } else if (TID.mayLoad()) { |
| 182 | if (TII->isInvariantLoad(MI)) { |
| 183 | // Invariant load, no chain dependencies needed! |
| 184 | } else if (MI->hasOneMemOperand() && |
| 185 | MI->memoperands_begin()->getValue() && |
| 186 | !MI->memoperands_begin()->isVolatile() && |
| 187 | isa<PseudoSourceValue>(MI->memoperands_begin()->getValue())) { |
| 188 | // A load from a specific PseudoSourceValue. Add precise dependencies. |
| 189 | const Value *V = MI->memoperands_begin()->getValue(); |
| 190 | std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V); |
| 191 | if (I != MemDefs.end()) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 192 | I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0, |
| 193 | /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 194 | MemUses[V].push_back(SU); |
| 195 | |
| 196 | // Add a general dependence too, if needed. |
| 197 | if (Chain && (!ChainMMO || |
| 198 | (ChainMMO->isStore() || ChainMMO->isVolatile()))) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 199 | Chain->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 200 | } else if (MI->hasVolatileMemoryRef()) { |
| 201 | // Treat volatile loads conservatively. Note that this includes |
| 202 | // cases where memoperand information is unavailable. |
| 203 | goto new_chain; |
| 204 | } else { |
| 205 | // A normal load. Just depend on the general chain. |
| 206 | if (Chain) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 207 | Chain->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 208 | PendingLoads.push_back(SU); |
| 209 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 210 | } |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 211 | |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 212 | // Add chain edges from the terminator to ensure that all the work of the |
| 213 | // block is completed before any control transfers. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 214 | if (Terminator && SU->Succs.empty()) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 215 | Terminator->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 216 | if (TID.isTerminator() || MI->isLabel()) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 217 | Terminator = SU; |
| 218 | } |
| 219 | } |
| 220 | |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 221 | void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) { |
| 222 | const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); |
| 223 | |
| 224 | // Compute the latency for the node. We use the sum of the latencies for |
| 225 | // all nodes flagged together into this SUnit. |
| 226 | SU->Latency = |
| 227 | InstrItins.getLatency(SU->getInstr()->getDesc().getSchedClass()); |
Dan Gohman | 4ea8e85 | 2008-12-16 02:38:22 +0000 | [diff] [blame] | 228 | |
| 229 | // Simplistic target-independent heuristic: assume that loads take |
| 230 | // extra time. |
| 231 | if (InstrItins.isEmpty()) |
| 232 | if (SU->getInstr()->getDesc().mayLoad()) |
| 233 | SU->Latency += 2; |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 236 | void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const { |
| 237 | SU->getInstr()->dump(); |
| 238 | } |
| 239 | |
| 240 | std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const { |
| 241 | std::string s; |
| 242 | raw_string_ostream oss(s); |
| 243 | SU->getInstr()->print(oss); |
| 244 | return oss.str(); |
| 245 | } |
| 246 | |
| 247 | // EmitSchedule - Emit the machine code in scheduled order. |
| 248 | MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() { |
| 249 | // For MachineInstr-based scheduling, we're rescheduling the instructions in |
| 250 | // the block, so start by removing them from the block. |
| 251 | while (!BB->empty()) |
| 252 | BB->remove(BB->begin()); |
| 253 | |
| 254 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 255 | SUnit *SU = Sequence[i]; |
| 256 | if (!SU) { |
| 257 | // Null SUnit* is a noop. |
| 258 | EmitNoop(); |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | BB->push_back(SU->getInstr()); |
| 263 | } |
| 264 | |
| 265 | return BB; |
| 266 | } |