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 | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ValueTracking.h" |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineMemOperand.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCInstrItineraries.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetSubtargetInfo.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 35 | ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 36 | const MachineLoopInfo &mli, |
Andrew Trick | 5e920d7 | 2012-01-14 02:17:12 +0000 | [diff] [blame] | 37 | const MachineDominatorTree &mdt, |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 38 | bool IsPostRAFlag, |
| 39 | LiveIntervals *lis) |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 40 | : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()), |
Andrew Trick | 5e920d7 | 2012-01-14 02:17:12 +0000 | [diff] [blame] | 41 | InstrItins(mf.getTarget().getInstrItineraryData()), IsPostRA(IsPostRAFlag), |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 42 | LIS(lis), UnitLatencies(false), LoopRegs(MLI, MDT), FirstDbgValue(0) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 43 | assert((IsPostRA || LIS) && "PreRA scheduling requires LiveIntervals"); |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 44 | DbgValues.clear(); |
Andrew Trick | cc77b54 | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 45 | assert(!(IsPostRA && MRI.getNumVirtRegs()) && |
Andrew Trick | 19273ae | 2012-02-21 04:51:23 +0000 | [diff] [blame] | 46 | "Virtual registers must be removed prior to PostRA scheduling"); |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 47 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 48 | |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 49 | /// Run - perform scheduling. |
| 50 | /// |
| 51 | void ScheduleDAGInstrs::Run(MachineBasicBlock *bb, |
| 52 | MachineBasicBlock::iterator begin, |
| 53 | MachineBasicBlock::iterator end, |
| 54 | unsigned endcount) { |
| 55 | BB = bb; |
| 56 | Begin = begin; |
| 57 | InsertPosIndex = endcount; |
| 58 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 59 | // Check to see if the scheduler cares about latencies. |
| 60 | UnitLatencies = ForceUnitLatencies(); |
| 61 | |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 62 | ScheduleDAG::Run(bb, end); |
| 63 | } |
| 64 | |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 65 | /// getUnderlyingObjectFromInt - This is the function that does the work of |
| 66 | /// looking through basic ptrtoint+arithmetic+inttoptr sequences. |
| 67 | static const Value *getUnderlyingObjectFromInt(const Value *V) { |
| 68 | do { |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 69 | if (const Operator *U = dyn_cast<Operator>(V)) { |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 70 | // If we find a ptrtoint, we can transfer control back to the |
| 71 | // regular getUnderlyingObjectFromInt. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 72 | if (U->getOpcode() == Instruction::PtrToInt) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 73 | return U->getOperand(0); |
| 74 | // If we find an add of a constant or a multiplied value, it's |
| 75 | // likely that the other operand will lead us to the base |
| 76 | // object. We don't have to worry about the case where the |
Dan Gohman | 748f98f | 2009-08-07 01:26:06 +0000 | [diff] [blame] | 77 | // object address is somehow being computed by the multiply, |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 78 | // because our callers only care when the result is an |
| 79 | // identifibale object. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 80 | if (U->getOpcode() != Instruction::Add || |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 81 | (!isa<ConstantInt>(U->getOperand(1)) && |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 82 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul)) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 83 | return V; |
| 84 | V = U->getOperand(0); |
| 85 | } else { |
| 86 | return V; |
| 87 | } |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 88 | assert(V->getType()->isIntegerTy() && "Unexpected operand type!"); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 89 | } while (1); |
| 90 | } |
| 91 | |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 92 | /// getUnderlyingObject - This is a wrapper around GetUnderlyingObject |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 93 | /// and adds support for basic ptrtoint+arithmetic+inttoptr sequences. |
| 94 | static const Value *getUnderlyingObject(const Value *V) { |
| 95 | // First just call Value::getUnderlyingObject to let it do what it does. |
| 96 | do { |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 97 | V = GetUnderlyingObject(V); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 98 | // If it found an inttoptr, use special code to continue climing. |
Dan Gohman | 8906f95 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 99 | if (Operator::getOpcode(V) != Instruction::IntToPtr) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 100 | break; |
| 101 | const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); |
| 102 | // If that succeeded in finding a pointer, continue the search. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 103 | if (!O->getType()->isPointerTy()) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 104 | break; |
| 105 | V = O; |
| 106 | } while (1); |
| 107 | return V; |
| 108 | } |
| 109 | |
| 110 | /// getUnderlyingObjectForInstr - If this machine instr has memory reference |
| 111 | /// information and it can be tracked to a normal reference to a known |
| 112 | /// object, return the Value for that object. Otherwise return null. |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 113 | static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI, |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 114 | const MachineFrameInfo *MFI, |
| 115 | bool &MayAlias) { |
| 116 | MayAlias = true; |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 117 | if (!MI->hasOneMemOperand() || |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 118 | !(*MI->memoperands_begin())->getValue() || |
| 119 | (*MI->memoperands_begin())->isVolatile()) |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 122 | const Value *V = (*MI->memoperands_begin())->getValue(); |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 123 | if (!V) |
| 124 | return 0; |
| 125 | |
| 126 | V = getUnderlyingObject(V); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 127 | if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) { |
| 128 | // For now, ignore PseudoSourceValues which may alias LLVM IR values |
| 129 | // because the code that uses this function has no way to cope with |
| 130 | // such aliases. |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 131 | if (PSV->isAliased(MFI)) |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 132 | return 0; |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 133 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 134 | MayAlias = PSV->mayAlias(MFI); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 135 | return V; |
| 136 | } |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 137 | |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 138 | if (isIdentifiedObject(V)) |
| 139 | return V; |
| 140 | |
| 141 | return 0; |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 144 | void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) { |
Andrew Trick | e8deca8 | 2011-10-07 06:33:09 +0000 | [diff] [blame] | 145 | LoopRegs.Deps.clear(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 146 | if (MachineLoop *ML = MLI.getLoopFor(BB)) |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 147 | if (BB == ML->getLoopLatch()) |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 148 | LoopRegs.VisitLoop(ML); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 151 | /// Initialize the map with the number of registers. |
| 152 | void ScheduleDAGInstrs::Reg2SUnitsMap::setRegLimit(unsigned Limit) { |
| 153 | PhysRegSet.setUniverse(Limit); |
| 154 | SUnits.resize(Limit); |
| 155 | } |
| 156 | |
| 157 | /// Clear the map without deallocating storage. |
| 158 | void ScheduleDAGInstrs::Reg2SUnitsMap::clear() { |
| 159 | for (const_iterator I = reg_begin(), E = reg_end(); I != E; ++I) { |
| 160 | SUnits[*I].clear(); |
| 161 | } |
| 162 | PhysRegSet.clear(); |
| 163 | } |
| 164 | |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 165 | /// AddSchedBarrierDeps - Add dependencies from instructions in the current |
| 166 | /// list of instructions being scheduled to scheduling barrier by adding |
| 167 | /// the exit SU to the register defs and use list. This is because we want to |
| 168 | /// make sure instructions which define registers that are either used by |
| 169 | /// the terminator or are live-out are properly scheduled. This is |
| 170 | /// especially important when the definition latency of the return value(s) |
| 171 | /// are too high to be hidden by the branch or when the liveout registers |
| 172 | /// used by instructions in the fallthrough block. |
| 173 | void ScheduleDAGInstrs::AddSchedBarrierDeps() { |
| 174 | MachineInstr *ExitMI = InsertPos != BB->end() ? &*InsertPos : 0; |
| 175 | ExitSU.setInstr(ExitMI); |
| 176 | bool AllDepKnown = ExitMI && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 177 | (ExitMI->isCall() || ExitMI->isBarrier()); |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 178 | if (ExitMI && AllDepKnown) { |
| 179 | // If it's a call or a barrier, add dependencies on the defs and uses of |
| 180 | // instruction. |
| 181 | for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) { |
| 182 | const MachineOperand &MO = ExitMI->getOperand(i); |
| 183 | if (!MO.isReg() || MO.isDef()) continue; |
| 184 | unsigned Reg = MO.getReg(); |
| 185 | if (Reg == 0) continue; |
| 186 | |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 187 | if (TRI->isPhysicalRegister(Reg)) |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 188 | Uses[Reg].push_back(&ExitSU); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 189 | else |
| 190 | assert(!IsPostRA && "Virtual register encountered after regalloc."); |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 191 | } |
| 192 | } else { |
| 193 | // For others, e.g. fallthrough, conditional branch, assume the exit |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 194 | // uses all the registers that are livein to the successor blocks. |
| 195 | SmallSet<unsigned, 8> Seen; |
| 196 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 197 | SE = BB->succ_end(); SI != SE; ++SI) |
| 198 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 199 | E = (*SI)->livein_end(); I != E; ++I) { |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 200 | unsigned Reg = *I; |
| 201 | if (Seen.insert(Reg)) |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 202 | Uses[Reg].push_back(&ExitSU); |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 203 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 207 | /// MO is an operand of SU's instruction that defines a physical register. Add |
| 208 | /// data dependencies from SU to any uses of the physical register. |
| 209 | void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU, |
| 210 | const MachineOperand &MO) { |
| 211 | assert(MO.isDef() && "expect physreg def"); |
| 212 | |
| 213 | // Ask the target if address-backscheduling is desirable, and if so how much. |
| 214 | const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>(); |
| 215 | unsigned SpecialAddressLatency = ST.getSpecialAddressLatency(); |
| 216 | unsigned DataLatency = SU->Latency; |
| 217 | |
| 218 | for (const unsigned *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 219 | if (!Uses.contains(*Alias)) |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 220 | continue; |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 221 | std::vector<SUnit*> &UseList = Uses[*Alias]; |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 222 | for (unsigned i = 0, e = UseList.size(); i != e; ++i) { |
| 223 | SUnit *UseSU = UseList[i]; |
| 224 | if (UseSU == SU) |
| 225 | continue; |
| 226 | unsigned LDataLatency = DataLatency; |
| 227 | // Optionally add in a special extra latency for nodes that |
| 228 | // feed addresses. |
| 229 | // TODO: Perhaps we should get rid of |
| 230 | // SpecialAddressLatency and just move this into |
| 231 | // adjustSchedDependency for the targets that care about it. |
| 232 | if (SpecialAddressLatency != 0 && !UnitLatencies && |
| 233 | UseSU != &ExitSU) { |
| 234 | MachineInstr *UseMI = UseSU->getInstr(); |
| 235 | const MCInstrDesc &UseMCID = UseMI->getDesc(); |
| 236 | int RegUseIndex = UseMI->findRegisterUseOperandIdx(*Alias); |
| 237 | assert(RegUseIndex >= 0 && "UseMI doesn't use register!"); |
| 238 | if (RegUseIndex >= 0 && |
| 239 | (UseMI->mayLoad() || UseMI->mayStore()) && |
| 240 | (unsigned)RegUseIndex < UseMCID.getNumOperands() && |
| 241 | UseMCID.OpInfo[RegUseIndex].isLookupPtrRegClass()) |
| 242 | LDataLatency += SpecialAddressLatency; |
| 243 | } |
| 244 | // Adjust the dependence latency using operand def/use |
| 245 | // information (if any), and then allow the target to |
| 246 | // perform its own adjustments. |
| 247 | const SDep& dep = SDep(SU, SDep::Data, LDataLatency, *Alias); |
| 248 | if (!UnitLatencies) { |
| 249 | ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep)); |
| 250 | ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep)); |
| 251 | } |
| 252 | UseSU->addPred(dep); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 257 | /// addPhysRegDeps - Add register dependencies (data, anti, and output) from |
| 258 | /// this SUnit to following instructions in the same scheduling region that |
| 259 | /// depend the physical register referenced at OperIdx. |
| 260 | void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) { |
| 261 | const MachineInstr *MI = SU->getInstr(); |
| 262 | const MachineOperand &MO = MI->getOperand(OperIdx); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 263 | |
| 264 | // Optionally add output and anti dependencies. For anti |
| 265 | // dependencies we use a latency of 0 because for a multi-issue |
| 266 | // target we want to allow the defining instruction to issue |
| 267 | // in the same cycle as the using instruction. |
| 268 | // TODO: Using a latency of 1 here for output dependencies assumes |
| 269 | // there's no cost for reusing registers. |
| 270 | SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output; |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 271 | for (const unsigned *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 272 | if (!Defs.contains(*Alias)) |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 273 | continue; |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 274 | std::vector<SUnit *> &DefList = Defs[*Alias]; |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 275 | for (unsigned i = 0, e = DefList.size(); i != e; ++i) { |
| 276 | SUnit *DefSU = DefList[i]; |
| 277 | if (DefSU == &ExitSU) |
| 278 | continue; |
| 279 | if (DefSU != SU && |
| 280 | (Kind != SDep::Output || !MO.isDead() || |
| 281 | !DefSU->getInstr()->registerDefIsDead(*Alias))) { |
| 282 | if (Kind == SDep::Anti) |
| 283 | DefSU->addPred(SDep(SU, Kind, 0, /*Reg=*/*Alias)); |
| 284 | else { |
| 285 | unsigned AOLat = TII->getOutputLatency(InstrItins, MI, OperIdx, |
| 286 | DefSU->getInstr()); |
| 287 | DefSU->addPred(SDep(SU, Kind, AOLat, /*Reg=*/*Alias)); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 293 | if (!MO.isDef()) { |
| 294 | // Either insert a new Reg2SUnits entry with an empty SUnits list, or |
| 295 | // retrieve the existing SUnits list for this register's uses. |
| 296 | // Push this SUnit on the use list. |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 297 | Uses[MO.getReg()].push_back(SU); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 298 | } |
| 299 | else { |
| 300 | addPhysRegDataDeps(SU, MO); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 301 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 302 | // Either insert a new Reg2SUnits entry with an empty SUnits list, or |
| 303 | // retrieve the existing SUnits list for this register's defs. |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 304 | std::vector<SUnit *> &DefList = Defs[MO.getReg()]; |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 305 | |
| 306 | // If a def is going to wrap back around to the top of the loop, |
| 307 | // backschedule it. |
| 308 | if (!UnitLatencies && DefList.empty()) { |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 309 | LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(MO.getReg()); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 310 | if (I != LoopRegs.Deps.end()) { |
| 311 | const MachineOperand *UseMO = I->second.first; |
| 312 | unsigned Count = I->second.second; |
| 313 | const MachineInstr *UseMI = UseMO->getParent(); |
| 314 | unsigned UseMOIdx = UseMO - &UseMI->getOperand(0); |
| 315 | const MCInstrDesc &UseMCID = UseMI->getDesc(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 316 | const TargetSubtargetInfo &ST = |
| 317 | TM.getSubtarget<TargetSubtargetInfo>(); |
| 318 | unsigned SpecialAddressLatency = ST.getSpecialAddressLatency(); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 319 | // TODO: If we knew the total depth of the region here, we could |
| 320 | // handle the case where the whole loop is inside the region but |
| 321 | // is large enough that the isScheduleHigh trick isn't needed. |
| 322 | if (UseMOIdx < UseMCID.getNumOperands()) { |
| 323 | // Currently, we only support scheduling regions consisting of |
| 324 | // single basic blocks. Check to see if the instruction is in |
| 325 | // the same region by checking to see if it has the same parent. |
| 326 | if (UseMI->getParent() != MI->getParent()) { |
| 327 | unsigned Latency = SU->Latency; |
| 328 | if (UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) |
| 329 | Latency += SpecialAddressLatency; |
| 330 | // This is a wild guess as to the portion of the latency which |
| 331 | // will be overlapped by work done outside the current |
| 332 | // scheduling region. |
| 333 | Latency -= std::min(Latency, Count); |
| 334 | // Add the artificial edge. |
| 335 | ExitSU.addPred(SDep(SU, SDep::Order, Latency, |
| 336 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 337 | /*isMustAlias=*/false, |
| 338 | /*isArtificial=*/true)); |
| 339 | } else if (SpecialAddressLatency > 0 && |
| 340 | UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) { |
| 341 | // The entire loop body is within the current scheduling region |
| 342 | // and the latency of this operation is assumed to be greater |
| 343 | // than the latency of the loop. |
| 344 | // TODO: Recursively mark data-edge predecessors as |
| 345 | // isScheduleHigh too. |
| 346 | SU->isScheduleHigh = true; |
| 347 | } |
| 348 | } |
| 349 | LoopRegs.Deps.erase(I); |
| 350 | } |
| 351 | } |
| 352 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 353 | // clear this register's use list |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 354 | if (Uses.contains(MO.getReg())) |
| 355 | Uses[MO.getReg()].clear(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 356 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 357 | if (!MO.isDead()) |
| 358 | DefList.clear(); |
| 359 | |
| 360 | // Calls will not be reordered because of chain dependencies (see |
| 361 | // below). Since call operands are dead, calls may continue to be added |
| 362 | // to the DefList making dependence checking quadratic in the size of |
| 363 | // the block. Instead, we leave only one call at the back of the |
| 364 | // DefList. |
| 365 | if (SU->isCall) { |
| 366 | while (!DefList.empty() && DefList.back()->isCall) |
| 367 | DefList.pop_back(); |
| 368 | } |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 369 | // Defs are pushed in the order they are visited and never reordered. |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 370 | DefList.push_back(SU); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 374 | /// addVRegDefDeps - Add register output and data dependencies from this SUnit |
| 375 | /// to instructions that occur later in the same scheduling region if they read |
| 376 | /// from or write to the virtual register defined at OperIdx. |
| 377 | /// |
| 378 | /// TODO: Hoist loop induction variable increments. This has to be |
| 379 | /// reevaluated. Generally, IV scheduling should be done before coalescing. |
| 380 | void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) { |
| 381 | const MachineInstr *MI = SU->getInstr(); |
| 382 | unsigned Reg = MI->getOperand(OperIdx).getReg(); |
| 383 | |
Andrew Trick | cc77b54 | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 384 | // SSA defs do not have output/anti dependencies. |
Andrew Trick | 2fc0977 | 2012-02-22 18:34:49 +0000 | [diff] [blame] | 385 | // The current operand is a def, so we have at least one. |
Andrew Trick | cc77b54 | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 386 | if (llvm::next(MRI.def_begin(Reg)) == MRI.def_end()) |
| 387 | return; |
| 388 | |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 389 | // Add output dependence to the next nearest def of this vreg. |
| 390 | // |
| 391 | // Unless this definition is dead, the output dependence should be |
| 392 | // transitively redundant with antidependencies from this definition's |
| 393 | // uses. We're conservative for now until we have a way to guarantee the uses |
| 394 | // are not eliminated sometime during scheduling. The output dependence edge |
| 395 | // is also useful if output latency exceeds def-use latency. |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 396 | VReg2SUnitMap::iterator DefI = findVRegDef(Reg); |
| 397 | if (DefI == VRegDefs.end()) |
| 398 | VRegDefs.insert(VReg2SUnit(Reg, SU)); |
| 399 | else { |
| 400 | SUnit *DefSU = DefI->SU; |
| 401 | if (DefSU != SU && DefSU != &ExitSU) { |
| 402 | unsigned OutLatency = TII->getOutputLatency(InstrItins, MI, OperIdx, |
| 403 | DefSU->getInstr()); |
| 404 | DefSU->addPred(SDep(SU, SDep::Output, OutLatency, Reg)); |
| 405 | } |
| 406 | DefI->SU = SU; |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 407 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 410 | /// addVRegUseDeps - Add a register data dependency if the instruction that |
| 411 | /// defines the virtual register used at OperIdx is mapped to an SUnit. Add a |
| 412 | /// register antidependency from this SUnit to instructions that occur later in |
| 413 | /// the same scheduling region if they write the virtual register. |
| 414 | /// |
| 415 | /// TODO: Handle ExitSU "uses" properly. |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 416 | void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 417 | MachineInstr *MI = SU->getInstr(); |
| 418 | unsigned Reg = MI->getOperand(OperIdx).getReg(); |
| 419 | |
| 420 | // Lookup this operand's reaching definition. |
| 421 | assert(LIS && "vreg dependencies requires LiveIntervals"); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 422 | SlotIndex UseIdx = LIS->getInstructionIndex(MI).getRegSlot(); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 423 | LiveInterval *LI = &LIS->getInterval(Reg); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 424 | VNInfo *VNI = LI->getVNInfoBefore(UseIdx); |
| 425 | // VNI will be valid because MachineOperand::readsReg() is checked by caller. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 426 | MachineInstr *Def = LIS->getInstructionFromIndex(VNI->def); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 427 | // Phis and other noninstructions (after coalescing) have a NULL Def. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 428 | if (Def) { |
| 429 | SUnit *DefSU = getSUnit(Def); |
| 430 | if (DefSU) { |
| 431 | // The reaching Def lives within this scheduling region. |
| 432 | // Create a data dependence. |
| 433 | // |
| 434 | // TODO: Handle "special" address latencies cleanly. |
| 435 | const SDep &dep = SDep(DefSU, SDep::Data, DefSU->Latency, Reg); |
| 436 | if (!UnitLatencies) { |
| 437 | // Adjust the dependence latency using operand def/use information, then |
| 438 | // allow the target to perform its own adjustments. |
| 439 | ComputeOperandLatency(DefSU, SU, const_cast<SDep &>(dep)); |
| 440 | const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>(); |
| 441 | ST.adjustSchedDependency(DefSU, SU, const_cast<SDep &>(dep)); |
| 442 | } |
| 443 | SU->addPred(dep); |
| 444 | } |
| 445 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 446 | |
| 447 | // Add antidependence to the following def of the vreg it uses. |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 448 | VReg2SUnitMap::iterator DefI = findVRegDef(Reg); |
| 449 | if (DefI != VRegDefs.end() && DefI->SU != SU) |
| 450 | DefI->SU->addPred(SDep(SU, SDep::Anti, 0, Reg)); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 451 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 452 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 453 | /// Create an SUnit for each real instruction, numbered in top-down toplological |
| 454 | /// order. The instruction order A < B, implies that no edge exists from B to A. |
| 455 | /// |
| 456 | /// Map each real instruction to its SUnit. |
| 457 | /// |
| 458 | /// After initSUnits, the SUnits vector is cannot be resized and the scheduler |
| 459 | /// may hang onto SUnit pointers. We may relax this in the future by using SUnit |
| 460 | /// IDs instead of pointers. |
| 461 | void ScheduleDAGInstrs::initSUnits() { |
| 462 | // We'll be allocating one SUnit for each real instruction in the region, |
| 463 | // which is contained within a basic block. |
| 464 | SUnits.reserve(BB->size()); |
| 465 | |
| 466 | for (MachineBasicBlock::iterator I = Begin; I != InsertPos; ++I) { |
| 467 | MachineInstr *MI = I; |
| 468 | if (MI->isDebugValue()) |
| 469 | continue; |
| 470 | |
| 471 | SUnit *SU = NewSUnit(MI); |
| 472 | MISUnitMap[MI] = SU; |
| 473 | |
| 474 | SU->isCall = MI->isCall(); |
| 475 | SU->isCommutable = MI->isCommutable(); |
| 476 | |
| 477 | // Assign the Latency field of SU using target-provided information. |
| 478 | if (UnitLatencies) |
| 479 | SU->Latency = 1; |
| 480 | else |
| 481 | ComputeLatency(SU); |
| 482 | } |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 485 | void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) { |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 486 | // Create an SUnit for each real instruction. |
| 487 | initSUnits(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 488 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 489 | // We build scheduling units by walking a block's instruction list from bottom |
| 490 | // to top. |
| 491 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 492 | // Remember where a generic side-effecting instruction is as we procede. |
| 493 | SUnit *BarrierChain = 0, *AliasChain = 0; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 494 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 495 | // Memory references to specific known memory locations are tracked |
| 496 | // so that they can be given more precise dependencies. We track |
| 497 | // separately the known memory locations that may alias and those |
| 498 | // that are known not to alias |
| 499 | std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs; |
| 500 | std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 501 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 502 | // Remove any stale debug info; sometimes BuildSchedGraph is called again |
| 503 | // without emitting the info from the previous call. |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 504 | DbgValues.clear(); |
| 505 | FirstDbgValue = NULL; |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 506 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 507 | assert(Defs.empty() && Uses.empty() && |
| 508 | "Only BuildGraph should update Defs/Uses"); |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 509 | Defs.setRegLimit(TRI->getNumRegs()); |
| 510 | Uses.setRegLimit(TRI->getNumRegs()); |
Andrew Trick | 9b66853 | 2011-05-06 21:52:52 +0000 | [diff] [blame] | 511 | |
Andrew Trick | 8ae3ac7 | 2012-02-22 21:59:00 +0000 | [diff] [blame] | 512 | assert(VRegDefs.empty() && "Only BuildSchedGraph may access VRegDefs"); |
| 513 | // FIXME: Allow SparseSet to reserve space for the creation of virtual |
| 514 | // registers during scheduling. Don't artificially inflate the Universe |
| 515 | // because we want to assert that vregs are not created during DAG building. |
| 516 | VRegDefs.setUniverse(MRI.getNumVirtRegs()); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 517 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 518 | // Model data dependencies between instructions being scheduled and the |
| 519 | // ExitSU. |
| 520 | AddSchedBarrierDeps(); |
| 521 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 522 | // Walk the list of instructions, from bottom moving up. |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 523 | MachineInstr *PrevMI = NULL; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 524 | for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 525 | MII != MIE; --MII) { |
| 526 | MachineInstr *MI = prior(MII); |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 527 | if (MI && PrevMI) { |
| 528 | DbgValues.push_back(std::make_pair(PrevMI, MI)); |
| 529 | PrevMI = NULL; |
| 530 | } |
| 531 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 532 | if (MI->isDebugValue()) { |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 533 | PrevMI = MI; |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 534 | continue; |
| 535 | } |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 536 | |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 537 | assert(!MI->isTerminator() && !MI->isLabel() && |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 538 | "Cannot schedule terminators or labels!"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 539 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 540 | SUnit *SU = MISUnitMap[MI]; |
| 541 | assert(SU && "No SUnit mapped to this MI"); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 542 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 543 | // Add register-based dependencies (data, anti, and output). |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 544 | for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) { |
| 545 | const MachineOperand &MO = MI->getOperand(j); |
| 546 | if (!MO.isReg()) continue; |
| 547 | unsigned Reg = MO.getReg(); |
| 548 | if (Reg == 0) continue; |
| 549 | |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 550 | if (TRI->isPhysicalRegister(Reg)) |
| 551 | addPhysRegDeps(SU, j); |
| 552 | else { |
| 553 | assert(!IsPostRA && "Virtual register encountered!"); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 554 | if (MO.isDef()) |
| 555 | addVRegDefDeps(SU, j); |
Andrew Trick | 63d578b | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 556 | else if (MO.readsReg()) // ignore undef operands |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 557 | addVRegUseDeps(SU, j); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 560 | |
| 561 | // Add chain dependencies. |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 562 | // Chain dependencies used to enforce memory order should have |
| 563 | // latency of 0 (except for true dependency of Store followed by |
| 564 | // aliased Load... we estimate that with a single cycle of latency |
| 565 | // assuming the hardware will bypass) |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 566 | // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable |
| 567 | // after stack slots are lowered to actual addresses. |
| 568 | // TODO: Use an AliasAnalysis and do real alias-analysis queries, and |
| 569 | // produce more precise dependence information. |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 570 | #define STORE_LOAD_LATENCY 1 |
| 571 | unsigned TrueMemOrderLatency = 0; |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 572 | if (MI->isCall() || MI->hasUnmodeledSideEffects() || |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 573 | (MI->hasVolatileMemoryRef() && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 574 | (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 575 | // Be conservative with these and add dependencies on all memory |
| 576 | // references, even those that are known to not alias. |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 577 | for (std::map<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 578 | NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) { |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 579 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 580 | } |
| 581 | for (std::map<const Value *, std::vector<SUnit *> >::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 582 | NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 583 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 584 | I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 585 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 586 | NonAliasMemDefs.clear(); |
| 587 | NonAliasMemUses.clear(); |
| 588 | // Add SU to the barrier chain. |
| 589 | if (BarrierChain) |
| 590 | BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 591 | BarrierChain = SU; |
| 592 | |
| 593 | // fall-through |
| 594 | new_alias_chain: |
| 595 | // Chain all possibly aliasing memory references though SU. |
| 596 | if (AliasChain) |
| 597 | AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 598 | AliasChain = SU; |
| 599 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
| 600 | PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
| 601 | for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(), |
| 602 | E = AliasMemDefs.end(); I != E; ++I) { |
| 603 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 604 | } |
| 605 | for (std::map<const Value *, std::vector<SUnit *> >::iterator I = |
| 606 | AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) { |
| 607 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
| 608 | I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
| 609 | } |
| 610 | PendingLoads.clear(); |
| 611 | AliasMemDefs.clear(); |
| 612 | AliasMemUses.clear(); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 613 | } else if (MI->mayStore()) { |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 614 | bool MayAlias = true; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 615 | TrueMemOrderLatency = STORE_LOAD_LATENCY; |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 616 | if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 617 | // A store to a specific PseudoSourceValue. Add precise dependencies. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 618 | // Record the def in MemDefs, first adding a dep if there is |
| 619 | // an existing def. |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 620 | std::map<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 621 | ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V)); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 622 | std::map<const Value *, SUnit *>::iterator IE = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 623 | ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end()); |
| 624 | if (I != IE) { |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 625 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0, |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 626 | /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 627 | I->second = SU; |
| 628 | } else { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 629 | if (MayAlias) |
| 630 | AliasMemDefs[V] = SU; |
| 631 | else |
| 632 | NonAliasMemDefs[V] = SU; |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 633 | } |
| 634 | // Handle the uses in MemUses, if there are any. |
Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 635 | std::map<const Value *, std::vector<SUnit *> >::iterator J = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 636 | ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V)); |
| 637 | std::map<const Value *, std::vector<SUnit *> >::iterator JE = |
| 638 | ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end()); |
| 639 | if (J != JE) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 640 | for (unsigned i = 0, e = J->second.size(); i != e; ++i) |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 641 | J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency, |
| 642 | /*Reg=*/0, /*isNormalMemory=*/true)); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 643 | J->second.clear(); |
| 644 | } |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 645 | if (MayAlias) { |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 646 | // Add dependencies from all the PendingLoads, i.e. loads |
| 647 | // with no underlying object. |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 648 | for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k) |
| 649 | PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency)); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 650 | // Add dependence on alias chain, if needed. |
| 651 | if (AliasChain) |
| 652 | AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 653 | } |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 654 | // Add dependence on barrier chain, if needed. |
| 655 | if (BarrierChain) |
| 656 | BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
David Goodwin | 5be870a | 2009-11-05 00:16:44 +0000 | [diff] [blame] | 657 | } else { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 658 | // Treat all other stores conservatively. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 659 | goto new_alias_chain; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 660 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 661 | |
| 662 | if (!ExitSU.isPred(SU)) |
| 663 | // Push store's up a bit to avoid them getting in between cmp |
| 664 | // and branches. |
| 665 | ExitSU.addPred(SDep(SU, SDep::Order, 0, |
| 666 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 667 | /*isMustAlias=*/false, |
| 668 | /*isArtificial=*/true)); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 669 | } else if (MI->mayLoad()) { |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 670 | bool MayAlias = true; |
David Goodwin | 7c9b1ac | 2009-11-02 17:06:28 +0000 | [diff] [blame] | 671 | TrueMemOrderLatency = 0; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 672 | if (MI->isInvariantLoad(AA)) { |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 673 | // Invariant load, no chain dependencies needed! |
David Goodwin | 5be870a | 2009-11-05 00:16:44 +0000 | [diff] [blame] | 674 | } else { |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 675 | if (const Value *V = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 676 | getUnderlyingObjectForInstr(MI, MFI, MayAlias)) { |
| 677 | // A load from a specific PseudoSourceValue. Add precise dependencies. |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 678 | std::map<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 679 | ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V)); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 680 | std::map<const Value *, SUnit *>::iterator IE = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 681 | ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end()); |
| 682 | if (I != IE) |
| 683 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0, |
| 684 | /*isNormalMemory=*/true)); |
| 685 | if (MayAlias) |
| 686 | AliasMemUses[V].push_back(SU); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 687 | else |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 688 | NonAliasMemUses[V].push_back(SU); |
| 689 | } else { |
| 690 | // A load with no underlying object. Depend on all |
| 691 | // potentially aliasing stores. |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 692 | for (std::map<const Value *, SUnit *>::iterator I = |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 693 | AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I) |
| 694 | I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 695 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 696 | PendingLoads.push_back(SU); |
| 697 | MayAlias = true; |
David Goodwin | a9e6107 | 2009-11-03 20:15:00 +0000 | [diff] [blame] | 698 | } |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 699 | |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 700 | // Add dependencies on alias and barrier chains, if needed. |
| 701 | if (MayAlias && AliasChain) |
| 702 | AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
| 703 | if (BarrierChain) |
| 704 | BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0)); |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 705 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 706 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 707 | } |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 708 | if (PrevMI) |
| 709 | FirstDbgValue = PrevMI; |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 710 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 711 | Defs.clear(); |
| 712 | Uses.clear(); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 713 | VRegDefs.clear(); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 714 | PendingLoads.clear(); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 715 | MISUnitMap.clear(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 718 | void ScheduleDAGInstrs::FinishBlock() { |
| 719 | // Nothing to do. |
| 720 | } |
| 721 | |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 722 | void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) { |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 723 | // Compute the latency for the node. |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 724 | if (!InstrItins || InstrItins->isEmpty()) { |
| 725 | SU->Latency = 1; |
Dan Gohman | 4ea8e85 | 2008-12-16 02:38:22 +0000 | [diff] [blame] | 726 | |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 727 | // Simplistic target-independent heuristic: assume that loads take |
| 728 | // extra time. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 729 | if (SU->getInstr()->mayLoad()) |
Dan Gohman | 4ea8e85 | 2008-12-16 02:38:22 +0000 | [diff] [blame] | 730 | SU->Latency += 2; |
Evan Cheng | 8239daf | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 731 | } else { |
| 732 | SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr()); |
| 733 | } |
Dan Gohman | c8c2827 | 2008-11-21 00:12:10 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 736 | void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use, |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 737 | SDep& dep) const { |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 738 | if (!InstrItins || InstrItins->isEmpty()) |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 739 | return; |
Andrew Trick | f405b1a | 2011-05-05 19:24:06 +0000 | [diff] [blame] | 740 | |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 741 | // For a data dependency with a known register... |
| 742 | if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0)) |
| 743 | return; |
| 744 | |
| 745 | const unsigned Reg = dep.getReg(); |
| 746 | |
| 747 | // ... find the definition of the register in the defining |
| 748 | // instruction |
| 749 | MachineInstr *DefMI = Def->getInstr(); |
| 750 | int DefIdx = DefMI->findRegisterDefOperandIdx(Reg); |
| 751 | if (DefIdx != -1) { |
Evan Cheng | 1aca5bc | 2010-10-08 18:42:25 +0000 | [diff] [blame] | 752 | const MachineOperand &MO = DefMI->getOperand(DefIdx); |
| 753 | if (MO.isReg() && MO.isImplicit() && |
Evan Cheng | d82de83 | 2010-10-08 23:01:57 +0000 | [diff] [blame] | 754 | DefIdx >= (int)DefMI->getDesc().getNumOperands()) { |
Evan Cheng | 1aca5bc | 2010-10-08 18:42:25 +0000 | [diff] [blame] | 755 | // This is an implicit def, getOperandLatency() won't return the correct |
| 756 | // latency. e.g. |
| 757 | // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def> |
| 758 | // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ... |
| 759 | // What we want is to compute latency between def of %D6/%D7 and use of |
| 760 | // %Q3 instead. |
Jakob Stoklund Olesen | 02634be | 2012-02-22 22:52:52 +0000 | [diff] [blame] | 761 | unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI); |
| 762 | if (DefMI->getOperand(Op2).isReg()) |
| 763 | DefIdx = Op2; |
Evan Cheng | 1aca5bc | 2010-10-08 18:42:25 +0000 | [diff] [blame] | 764 | } |
Evan Cheng | a0792de | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 765 | MachineInstr *UseMI = Use->getInstr(); |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 766 | // For all uses of the register, calculate the maxmimum latency |
| 767 | int Latency = -1; |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 768 | if (UseMI) { |
| 769 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { |
| 770 | const MachineOperand &MO = UseMI->getOperand(i); |
| 771 | if (!MO.isReg() || !MO.isUse()) |
| 772 | continue; |
| 773 | unsigned MOReg = MO.getReg(); |
| 774 | if (MOReg != Reg) |
| 775 | continue; |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 776 | |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 777 | int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx, |
| 778 | UseMI, i); |
| 779 | Latency = std::max(Latency, UseCycle); |
| 780 | } |
| 781 | } else { |
| 782 | // UseMI is null, then it must be a scheduling barrier. |
| 783 | if (!InstrItins || InstrItins->isEmpty()) |
| 784 | return; |
| 785 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 786 | Latency = InstrItins->getOperandCycle(DefClass, DefIdx); |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 787 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 788 | |
| 789 | // If we found a latency, then replace the existing dependence latency. |
| 790 | if (Latency >= 0) |
| 791 | dep.setLatency(Latency); |
David Goodwin | dc4bdcd | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 795 | void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const { |
| 796 | SU->getInstr()->dump(); |
| 797 | } |
| 798 | |
| 799 | std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const { |
| 800 | std::string s; |
| 801 | raw_string_ostream oss(s); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 802 | if (SU == &EntrySU) |
| 803 | oss << "<entry>"; |
| 804 | else if (SU == &ExitSU) |
| 805 | oss << "<exit>"; |
| 806 | else |
| 807 | SU->getInstr()->print(oss); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 808 | return oss.str(); |
| 809 | } |
| 810 | |
| 811 | // EmitSchedule - Emit the machine code in scheduled order. |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 812 | MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() { |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 813 | Begin = InsertPos; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 814 | |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 815 | // If first instruction was a DBG_VALUE then put it back. |
| 816 | if (FirstDbgValue) |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 817 | BB->splice(InsertPos, BB, FirstDbgValue); |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 818 | |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 819 | // Then re-insert them according to the given schedule. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 820 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
Devang Patel | ee1f878 | 2011-06-02 21:31:00 +0000 | [diff] [blame] | 821 | if (SUnit *SU = Sequence[i]) |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 822 | BB->splice(InsertPos, BB, SU->getInstr()); |
Devang Patel | ee1f878 | 2011-06-02 21:31:00 +0000 | [diff] [blame] | 823 | else |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 824 | // Null SUnit* is a noop. |
| 825 | EmitNoop(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 826 | |
Hal Finkel | db809e0 | 2011-12-02 04:58:07 +0000 | [diff] [blame] | 827 | // Update the Begin iterator, as the first instruction in the block |
| 828 | // may have been scheduled later. |
| 829 | if (i == 0) |
| 830 | Begin = prior(InsertPos); |
| 831 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 832 | |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 833 | // Reinsert any remaining debug_values. |
| 834 | for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator |
| 835 | DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { |
| 836 | std::pair<MachineInstr *, MachineInstr *> P = *prior(DI); |
| 837 | MachineInstr *DbgValue = P.first; |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 838 | MachineBasicBlock::iterator OrigPrivMI = P.second; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 839 | BB->splice(++OrigPrivMI, BB, DbgValue); |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 840 | } |
| 841 | DbgValues.clear(); |
| 842 | FirstDbgValue = NULL; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 843 | return BB; |
| 844 | } |