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