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" |
Dan Gohman | 6dc75fe | 2009-02-06 17:12:10 +0000 | [diff] [blame] | 16 | #include "ScheduleDAGInstrs.h" |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 17 | #include "llvm/Operator.h" |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame^] | 20 | #include "llvm/CodeGen/MachineMemOperand.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetInstrInfo.h" |
| 25 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetSubtarget.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 32 | ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 33 | const MachineLoopInfo &mli, |
| 34 | const MachineDominatorTree &mdt) |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 35 | : ScheduleDAG(mf), MLI(mli), MDT(mdt), LoopRegs(MLI, MDT) {} |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 36 | |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 37 | /// Run - perform scheduling. |
| 38 | /// |
| 39 | void ScheduleDAGInstrs::Run(MachineBasicBlock *bb, |
| 40 | MachineBasicBlock::iterator begin, |
| 41 | MachineBasicBlock::iterator end, |
| 42 | unsigned endcount) { |
| 43 | BB = bb; |
| 44 | Begin = begin; |
| 45 | InsertPosIndex = endcount; |
| 46 | |
| 47 | ScheduleDAG::Run(bb, end); |
| 48 | } |
| 49 | |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 50 | /// getUnderlyingObjectFromInt - This is the function that does the work of |
| 51 | /// looking through basic ptrtoint+arithmetic+inttoptr sequences. |
| 52 | static const Value *getUnderlyingObjectFromInt(const Value *V) { |
| 53 | do { |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 54 | if (const Operator *U = dyn_cast<Operator>(V)) { |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 55 | // If we find a ptrtoint, we can transfer control back to the |
| 56 | // regular getUnderlyingObjectFromInt. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 57 | if (U->getOpcode() == Instruction::PtrToInt) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 58 | return U->getOperand(0); |
| 59 | // If we find an add of a constant or a multiplied value, it's |
| 60 | // likely that the other operand will lead us to the base |
| 61 | // object. We don't have to worry about the case where the |
Dan Gohman | 748f98f | 2009-08-07 01:26:06 +0000 | [diff] [blame] | 62 | // object address is somehow being computed by the multiply, |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 63 | // because our callers only care when the result is an |
| 64 | // identifibale object. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 65 | if (U->getOpcode() != Instruction::Add || |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 66 | (!isa<ConstantInt>(U->getOperand(1)) && |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 67 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul)) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 68 | return V; |
| 69 | V = U->getOperand(0); |
| 70 | } else { |
| 71 | return V; |
| 72 | } |
| 73 | assert(isa<IntegerType>(V->getType()) && "Unexpected operand type!"); |
| 74 | } while (1); |
| 75 | } |
| 76 | |
| 77 | /// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject |
| 78 | /// and adds support for basic ptrtoint+arithmetic+inttoptr sequences. |
| 79 | static const Value *getUnderlyingObject(const Value *V) { |
| 80 | // First just call Value::getUnderlyingObject to let it do what it does. |
| 81 | do { |
| 82 | V = V->getUnderlyingObject(); |
| 83 | // If it found an inttoptr, use special code to continue climing. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 84 | if (Operator::getOpcode(V) != Instruction::IntToPtr) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 85 | break; |
| 86 | const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); |
| 87 | // If that succeeded in finding a pointer, continue the search. |
| 88 | if (!isa<PointerType>(O->getType())) |
| 89 | break; |
| 90 | V = O; |
| 91 | } while (1); |
| 92 | return V; |
| 93 | } |
| 94 | |
| 95 | /// getUnderlyingObjectForInstr - If this machine instr has memory reference |
| 96 | /// information and it can be tracked to a normal reference to a known |
| 97 | /// object, return the Value for that object. Otherwise return null. |
| 98 | static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI) { |
| 99 | if (!MI->hasOneMemOperand() || |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame^] | 100 | !(*MI->memoperands_begin())->getValue() || |
| 101 | (*MI->memoperands_begin())->isVolatile()) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame^] | 104 | const Value *V = (*MI->memoperands_begin())->getValue(); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 105 | if (!V) |
| 106 | return 0; |
| 107 | |
| 108 | V = getUnderlyingObject(V); |
| 109 | if (!isa<PseudoSourceValue>(V) && !isIdentifiedObject(V)) |
| 110 | return 0; |
| 111 | |
| 112 | return V; |
| 113 | } |
| 114 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 115 | void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) { |
| 116 | if (MachineLoop *ML = MLI.getLoopFor(BB)) |
| 117 | if (BB == ML->getLoopLatch()) { |
| 118 | MachineBasicBlock *Header = ML->getHeader(); |
| 119 | for (MachineBasicBlock::livein_iterator I = Header->livein_begin(), |
| 120 | E = Header->livein_end(); I != E; ++I) |
| 121 | LoopLiveInRegs.insert(*I); |
| 122 | LoopRegs.VisitLoop(ML); |
| 123 | } |
| 124 | } |
| 125 | |
Dan Gohman | c9a5b9e | 2008-12-23 18:36:58 +0000 | [diff] [blame] | 126 | void ScheduleDAGInstrs::BuildSchedGraph() { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 127 | // We'll be allocating one SUnit for each instruction, plus one for |
| 128 | // the region exit node. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 129 | SUnits.reserve(BB->size()); |
| 130 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 131 | // We build scheduling units by walking a block's instruction list from bottom |
| 132 | // to top. |
| 133 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 134 | // Remember where a generic side-effecting instruction is as we procede. If |
| 135 | // ChainMMO is null, this is assumed to have arbitrary side-effects. If |
| 136 | // ChainMMO is non-null, then Chain makes only a single memory reference. |
| 137 | SUnit *Chain = 0; |
| 138 | MachineMemOperand *ChainMMO = 0; |
| 139 | |
| 140 | // Memory references to specific known memory locations are tracked so that |
| 141 | // they can be given more precise dependencies. |
| 142 | std::map<const Value *, SUnit *> MemDefs; |
| 143 | std::map<const Value *, std::vector<SUnit *> > MemUses; |
| 144 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 145 | // Check to see if the scheduler cares about latencies. |
| 146 | bool UnitLatencies = ForceUnitLatencies(); |
| 147 | |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 148 | // Ask the target if address-backscheduling is desirable, and if so how much. |
David Goodwin | 7104616 | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 149 | const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>(); |
| 150 | unsigned SpecialAddressLatency = ST.getSpecialAddressLatency(); |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 151 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 152 | // Walk the list of instructions, from bottom moving up. |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 153 | for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 154 | MII != MIE; --MII) { |
| 155 | MachineInstr *MI = prior(MII); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 156 | const TargetInstrDesc &TID = MI->getDesc(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 157 | assert(!TID.isTerminator() && !MI->isLabel() && |
| 158 | "Cannot schedule terminators or labels!"); |
| 159 | // Create the SUnit for this MI. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 160 | SUnit *SU = NewSUnit(MI); |
| 161 | |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 162 | // Assign the Latency field of SU using target-provided information. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 163 | if (UnitLatencies) |
| 164 | SU->Latency = 1; |
| 165 | else |
| 166 | ComputeLatency(SU); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 167 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 168 | // Add register-based dependencies (data, anti, and output). |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 169 | for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) { |
| 170 | const MachineOperand &MO = MI->getOperand(j); |
| 171 | if (!MO.isReg()) continue; |
| 172 | unsigned Reg = MO.getReg(); |
| 173 | if (Reg == 0) continue; |
| 174 | |
| 175 | assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!"); |
| 176 | std::vector<SUnit *> &UseList = Uses[Reg]; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 177 | std::vector<SUnit *> &DefList = Defs[Reg]; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 178 | // Optionally add output and anti dependencies. For anti |
| 179 | // dependencies we use a latency of 0 because for a multi-issue |
| 180 | // target we want to allow the defining instruction to issue |
| 181 | // in the same cycle as the using instruction. |
| 182 | // TODO: Using a latency of 1 here for output dependencies assumes |
| 183 | // there's no cost for reusing registers. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 184 | SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 185 | unsigned AOLatency = (Kind == SDep::Anti) ? 0 : 1; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 186 | for (unsigned i = 0, e = DefList.size(); i != e; ++i) { |
| 187 | SUnit *DefSU = DefList[i]; |
| 188 | if (DefSU != SU && |
| 189 | (Kind != SDep::Output || !MO.isDead() || |
| 190 | !DefSU->getInstr()->registerDefIsDead(Reg))) |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 191 | DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/Reg)); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 192 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 193 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 194 | std::vector<SUnit *> &DefList = Defs[*Alias]; |
| 195 | for (unsigned i = 0, e = DefList.size(); i != e; ++i) { |
| 196 | SUnit *DefSU = DefList[i]; |
| 197 | if (DefSU != SU && |
| 198 | (Kind != SDep::Output || !MO.isDead() || |
| 199 | !DefSU->getInstr()->registerDefIsDead(Reg))) |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 200 | DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/ *Alias)); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 201 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (MO.isDef()) { |
| 205 | // Add any data dependencies. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 206 | unsigned DataLatency = SU->Latency; |
| 207 | for (unsigned i = 0, e = UseList.size(); i != e; ++i) { |
| 208 | SUnit *UseSU = UseList[i]; |
| 209 | if (UseSU != SU) { |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 210 | unsigned LDataLatency = DataLatency; |
| 211 | // Optionally add in a special extra latency for nodes that |
| 212 | // feed addresses. |
| 213 | // TODO: Do this for register aliases too. |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 214 | // TODO: Perhaps we should get rid of |
| 215 | // SpecialAddressLatency and just move this into |
| 216 | // adjustSchedDependency for the targets that care about |
| 217 | // it. |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 218 | if (SpecialAddressLatency != 0 && !UnitLatencies) { |
| 219 | MachineInstr *UseMI = UseSU->getInstr(); |
| 220 | const TargetInstrDesc &UseTID = UseMI->getDesc(); |
| 221 | int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg); |
| 222 | assert(RegUseIndex >= 0 && "UseMI doesn's use register!"); |
| 223 | if ((UseTID.mayLoad() || UseTID.mayStore()) && |
| 224 | (unsigned)RegUseIndex < UseTID.getNumOperands() && |
| 225 | UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass()) |
| 226 | LDataLatency += SpecialAddressLatency; |
| 227 | } |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 228 | // Adjust the dependence latency using operand def/use |
| 229 | // information (if any), and then allow the target to |
| 230 | // perform its own adjustments. |
David Goodwin | 7104616 | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 231 | const SDep& dep = SDep(SU, SDep::Data, LDataLatency, Reg); |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 232 | if (!UnitLatencies) { |
| 233 | ComputeOperandLatency(SU, UseSU, (SDep &)dep); |
| 234 | ST.adjustSchedDependency(SU, UseSU, (SDep &)dep); |
| 235 | } |
David Goodwin | 7104616 | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 236 | UseSU->addPred(dep); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 239 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 240 | std::vector<SUnit *> &UseList = Uses[*Alias]; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 241 | for (unsigned i = 0, e = UseList.size(); i != e; ++i) { |
| 242 | SUnit *UseSU = UseList[i]; |
David Goodwin | 7104616 | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 243 | if (UseSU != SU) { |
| 244 | const SDep& dep = SDep(SU, SDep::Data, DataLatency, *Alias); |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 245 | if (!UnitLatencies) { |
| 246 | ComputeOperandLatency(SU, UseSU, (SDep &)dep); |
| 247 | ST.adjustSchedDependency(SU, UseSU, (SDep &)dep); |
| 248 | } |
David Goodwin | 7104616 | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 249 | UseSU->addPred(dep); |
| 250 | } |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 251 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 254 | // If a def is going to wrap back around to the top of the loop, |
| 255 | // backschedule it. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 256 | if (!UnitLatencies && DefList.empty()) { |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 257 | LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg); |
| 258 | if (I != LoopRegs.Deps.end()) { |
| 259 | const MachineOperand *UseMO = I->second.first; |
| 260 | unsigned Count = I->second.second; |
| 261 | const MachineInstr *UseMI = UseMO->getParent(); |
| 262 | unsigned UseMOIdx = UseMO - &UseMI->getOperand(0); |
| 263 | const TargetInstrDesc &UseTID = UseMI->getDesc(); |
| 264 | // TODO: If we knew the total depth of the region here, we could |
| 265 | // handle the case where the whole loop is inside the region but |
| 266 | // is large enough that the isScheduleHigh trick isn't needed. |
| 267 | if (UseMOIdx < UseTID.getNumOperands()) { |
| 268 | // Currently, we only support scheduling regions consisting of |
| 269 | // single basic blocks. Check to see if the instruction is in |
| 270 | // the same region by checking to see if it has the same parent. |
| 271 | if (UseMI->getParent() != MI->getParent()) { |
| 272 | unsigned Latency = SU->Latency; |
| 273 | if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) |
| 274 | Latency += SpecialAddressLatency; |
| 275 | // This is a wild guess as to the portion of the latency which |
| 276 | // will be overlapped by work done outside the current |
| 277 | // scheduling region. |
| 278 | Latency -= std::min(Latency, Count); |
| 279 | // Add the artifical edge. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 280 | ExitSU.addPred(SDep(SU, SDep::Order, Latency, |
| 281 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 282 | /*isMustAlias=*/false, |
| 283 | /*isArtificial=*/true)); |
Dan Gohman | 8749b61 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 284 | } else if (SpecialAddressLatency > 0 && |
| 285 | UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) { |
| 286 | // The entire loop body is within the current scheduling region |
| 287 | // and the latency of this operation is assumed to be greater |
| 288 | // than the latency of the loop. |
| 289 | // TODO: Recursively mark data-edge predecessors as |
| 290 | // isScheduleHigh too. |
| 291 | SU->isScheduleHigh = true; |
| 292 | } |
| 293 | } |
| 294 | LoopRegs.Deps.erase(I); |
| 295 | } |
| 296 | } |
| 297 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 298 | UseList.clear(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 299 | if (!MO.isDead()) |
| 300 | DefList.clear(); |
| 301 | DefList.push_back(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 302 | } else { |
| 303 | UseList.push_back(SU); |
| 304 | } |
| 305 | } |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 306 | |
| 307 | // Add chain dependencies. |
| 308 | // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable |
| 309 | // after stack slots are lowered to actual addresses. |
| 310 | // TODO: Use an AliasAnalysis and do real alias-analysis queries, and |
| 311 | // produce more precise dependence information. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 312 | if (TID.isCall() || TID.hasUnmodeledSideEffects()) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 313 | new_chain: |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 314 | // This is the conservative case. Add dependencies on all memory |
| 315 | // references. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 316 | if (Chain) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 317 | Chain->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 318 | Chain = SU; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 319 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 320 | PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 321 | PendingLoads.clear(); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 322 | for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(), |
| 323 | E = MemDefs.end(); I != E; ++I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 324 | I->second->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 325 | I->second = SU; |
| 326 | } |
| 327 | for (std::map<const Value *, std::vector<SUnit *> >::iterator I = |
| 328 | MemUses.begin(), E = MemUses.end(); I != E; ++I) { |
| 329 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 330 | I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 331 | I->second.clear(); |
| 332 | } |
| 333 | // See if it is known to just have a single memory reference. |
| 334 | MachineInstr *ChainMI = Chain->getInstr(); |
| 335 | const TargetInstrDesc &ChainTID = ChainMI->getDesc(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 336 | if (!ChainTID.isCall() && |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 337 | !ChainTID.hasUnmodeledSideEffects() && |
| 338 | ChainMI->hasOneMemOperand() && |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame^] | 339 | !(*ChainMI->memoperands_begin())->isVolatile() && |
| 340 | (*ChainMI->memoperands_begin())->getValue()) |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 341 | // We know that the Chain accesses one specific memory location. |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame^] | 342 | ChainMMO = *ChainMI->memoperands_begin(); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 343 | else |
| 344 | // Unknown memory accesses. Assume the worst. |
| 345 | ChainMMO = 0; |
| 346 | } else if (TID.mayStore()) { |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 347 | if (const Value *V = getUnderlyingObjectForInstr(MI)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 348 | // A store to a specific PseudoSourceValue. Add precise dependencies. |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 349 | // Handle the def in MemDefs, if there is one. |
| 350 | std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V); |
| 351 | if (I != MemDefs.end()) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 352 | I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0, |
| 353 | /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 354 | I->second = SU; |
| 355 | } else { |
| 356 | MemDefs[V] = SU; |
| 357 | } |
| 358 | // Handle the uses in MemUses, if there are any. |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 359 | std::map<const Value *, std::vector<SUnit *> >::iterator J = |
| 360 | MemUses.find(V); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 361 | if (J != MemUses.end()) { |
| 362 | for (unsigned i = 0, e = J->second.size(); i != e; ++i) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 363 | J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0, |
| 364 | /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 365 | J->second.clear(); |
| 366 | } |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 367 | // Add dependencies from all the PendingLoads, since without |
| 368 | // memoperands we must assume they alias anything. |
| 369 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
| 370 | PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 371 | // Add a general dependence too, if needed. |
| 372 | if (Chain) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 373 | Chain->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 374 | } else |
| 375 | // Treat all other stores conservatively. |
| 376 | goto new_chain; |
| 377 | } else if (TID.mayLoad()) { |
| 378 | if (TII->isInvariantLoad(MI)) { |
| 379 | // Invariant load, no chain dependencies needed! |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 380 | } else if (const Value *V = getUnderlyingObjectForInstr(MI)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 381 | // A load from a specific PseudoSourceValue. Add precise dependencies. |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 382 | std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V); |
| 383 | if (I != MemDefs.end()) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 384 | I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0, |
| 385 | /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 386 | MemUses[V].push_back(SU); |
| 387 | |
| 388 | // Add a general dependence too, if needed. |
| 389 | if (Chain && (!ChainMMO || |
| 390 | (ChainMMO->isStore() || ChainMMO->isVolatile()))) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 391 | Chain->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 392 | } else if (MI->hasVolatileMemoryRef()) { |
| 393 | // Treat volatile loads conservatively. Note that this includes |
| 394 | // cases where memoperand information is unavailable. |
| 395 | goto new_chain; |
| 396 | } else { |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 397 | // A normal load. Depend on the general chain, as well as on |
| 398 | // all stores. In the absense of MachineMemOperand information, |
| 399 | // we can't even assume that the load doesn't alias well-behaved |
| 400 | // memory locations. |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 401 | if (Chain) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 402 | Chain->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 403 | for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(), |
| 404 | E = MemDefs.end(); I != E; ++I) |
| 405 | I->second->addPred(SDep(SU, SDep::Order, SU->Latency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 406 | PendingLoads.push_back(SU); |
| 407 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 408 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 409 | } |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 410 | |
| 411 | for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) { |
| 412 | Defs[i].clear(); |
| 413 | Uses[i].clear(); |
| 414 | } |
| 415 | PendingLoads.clear(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 418 | void ScheduleDAGInstrs::FinishBlock() { |
| 419 | // Nothing to do. |
| 420 | } |
| 421 | |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 422 | void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) { |
| 423 | const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); |
| 424 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 425 | // Compute the latency for the node. |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 426 | SU->Latency = |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 427 | InstrItins.getStageLatency(SU->getInstr()->getDesc().getSchedClass()); |
Dan Gohman | 4ea8e85 | 2008-12-16 02:38:22 +0000 | [diff] [blame] | 428 | |
| 429 | // Simplistic target-independent heuristic: assume that loads take |
| 430 | // extra time. |
| 431 | if (InstrItins.isEmpty()) |
| 432 | if (SU->getInstr()->getDesc().mayLoad()) |
| 433 | SU->Latency += 2; |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 434 | } |
| 435 | |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 436 | void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use, |
| 437 | SDep& dep) const { |
| 438 | const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); |
| 439 | if (InstrItins.isEmpty()) |
| 440 | return; |
| 441 | |
| 442 | // For a data dependency with a known register... |
| 443 | if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0)) |
| 444 | return; |
| 445 | |
| 446 | const unsigned Reg = dep.getReg(); |
| 447 | |
| 448 | // ... find the definition of the register in the defining |
| 449 | // instruction |
| 450 | MachineInstr *DefMI = Def->getInstr(); |
| 451 | int DefIdx = DefMI->findRegisterDefOperandIdx(Reg); |
| 452 | if (DefIdx != -1) { |
| 453 | int DefCycle = InstrItins.getOperandCycle(DefMI->getDesc().getSchedClass(), DefIdx); |
| 454 | if (DefCycle >= 0) { |
| 455 | MachineInstr *UseMI = Use->getInstr(); |
| 456 | const unsigned UseClass = UseMI->getDesc().getSchedClass(); |
| 457 | |
| 458 | // For all uses of the register, calculate the maxmimum latency |
| 459 | int Latency = -1; |
| 460 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { |
| 461 | const MachineOperand &MO = UseMI->getOperand(i); |
| 462 | if (!MO.isReg() || !MO.isUse()) |
| 463 | continue; |
| 464 | unsigned MOReg = MO.getReg(); |
| 465 | if (MOReg != Reg) |
| 466 | continue; |
| 467 | |
| 468 | int UseCycle = InstrItins.getOperandCycle(UseClass, i); |
| 469 | if (UseCycle >= 0) |
| 470 | Latency = std::max(Latency, DefCycle - UseCycle + 1); |
| 471 | } |
| 472 | |
| 473 | // If we found a latency, then replace the existing dependence latency. |
| 474 | if (Latency >= 0) |
| 475 | dep.setLatency(Latency); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 480 | void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const { |
| 481 | SU->getInstr()->dump(); |
| 482 | } |
| 483 | |
| 484 | std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const { |
| 485 | std::string s; |
| 486 | raw_string_ostream oss(s); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 487 | if (SU == &EntrySU) |
| 488 | oss << "<entry>"; |
| 489 | else if (SU == &ExitSU) |
| 490 | oss << "<exit>"; |
| 491 | else |
| 492 | SU->getInstr()->print(oss); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 493 | return oss.str(); |
| 494 | } |
| 495 | |
| 496 | // EmitSchedule - Emit the machine code in scheduled order. |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 497 | MachineBasicBlock *ScheduleDAGInstrs:: |
| 498 | EmitSchedule(DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 499 | // For MachineInstr-based scheduling, we're rescheduling the instructions in |
| 500 | // the block, so start by removing them from the block. |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 501 | while (Begin != InsertPos) { |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 502 | MachineBasicBlock::iterator I = Begin; |
| 503 | ++Begin; |
| 504 | BB->remove(I); |
| 505 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 506 | |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 507 | // Then re-insert them according to the given schedule. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 508 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 509 | SUnit *SU = Sequence[i]; |
| 510 | if (!SU) { |
| 511 | // Null SUnit* is a noop. |
| 512 | EmitNoop(); |
| 513 | continue; |
| 514 | } |
| 515 | |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 516 | BB->insert(InsertPos, SU->getInstr()); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 519 | // Update the Begin iterator, as the first instruction in the block |
| 520 | // may have been scheduled later. |
| 521 | if (!Sequence.empty()) |
| 522 | Begin = Sequence[0]->getInstr(); |
| 523 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 524 | return BB; |
| 525 | } |