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